Obj doesn't link under MSVC


I'm using LLVM (10.0.1) in a project I'm working on. Unfortunately, I've
encountered a problem a few days ago I could not find a solution yet.
I'm using LLVM in combination with the Microsoft's compiler and the platform I'm
targeting is also MSVC x64.

When linking a generated .obj file with code from another cpp into the dll, the
linker complains as follows:

s.obj : error LNK2017: 'ADDR32' relocation to '.rdata' invalid without
LINK : fatal error LNK1165: link failed because of fixup errors

which is funny because as far as I can tell, that flag should have no effect on
x64 anyway. If I add it to the linker command, it says it's ignored.

I've noticed this happens when I use a global constant, eg. a string, but not
always - for some inputs it triggers the error, for some it doesn't.

The bat that links into exe looks like:

@call set path=C:\Program Files (x86)\Microsoft Visual
call vcvarsall.bat x64
cl /EHsc /DEBUG /Z7 /LD s.obj code.cpp

and the IR: Compiler Explorer

Researching a bit I could find an issue on Rust's Github that seems to have been
solved by time, and the statement `default rel` for an assembler to be inserted
at the beginning of the ASM file, which I'm not sure how would be related.

The only workaround I could find is to allocate the string on stack and set
every element one by one, but it polutes the IR and it makes LLVM not to be able
to optimize the thing into the readonly section, from what I noticed.

I kindly request your help, thanks.

It sounds like LLVM isn’t generating PIC code. Adding llc -relocation-model=static makes it generate assembly that probably uses this relocation:

I think clang usually configures LLVM to generate PIC code for Win x64.