antlr4 icon indicating copy to clipboard operation
antlr4 copied to clipboard

[c++ runtime] Can't link with lld

Open bobkocisko opened this issue 4 years ago • 6 comments

I'm attemping to link the cpp runtime project with the lld linker. I followed compiling on linux and it works fine with the regular linker GNU ld (GNU Binutils for Ubuntu) 2.30 but when I attempt ld LLD 8.0.0 (compatible with GNU linkers) it fails with the following

[100%] Linking CXX shared library ../../dist/libantlr4-runtime.so ld: error: can't create dynamic relocation R_X86_64_DTPOFF32 against symbol: guard variable for std::__cxx11::basic_string<char, std::char_traits, std::allocator > antlrcpp::utf32_to_utf8<std::__cxx11::basic_string<char32_t, std::char_traits<char32_t>, std::allocator<char32_t> > >(std::__cxx11::basic_string<char32_t, std::char_traits<char32_t>, std::allocator<char32_t> > const&)::converter[abi:cxx11] in readonly segment; recompile object files with -fPIC or pass '-Wl,-z,notext' to allow text relocations in the output >>> defined in CMakeFiles/antlr4_shared.dir/src/ANTLRInputStream.cpp.o >>> referenced by ANTLRInputStream.cpp >>> CMakeFiles/antlr4_shared.dir/src/ANTLRInputStream.cpp.o:(antlr4::ANTLRInputStream::toStringabi:cxx11 const)

If I use the following cmake switch to attempt to resolve this cmake .. -DCMAKE_SHARED_LINKER_FLAGS="-Wl,-z,notext" I get this

ld: error: relocation R_X86_64_DTPOFF32 cannot be used against symbol guard variable for std::__cxx11::basic_string<char, std::char_traits, std::allocator > antlrcpp::utf32_to_utf8<std::__cxx11::basic_string<char32_t, std::char_traits<char32_t>, std::allocator<char32_t> > >(std::__cxx11::basic_string<char32_t, std::char_traits<char32_t>, std::allocator<char32_t> > const&)::converter[abi:cxx11]; recompile with -fPIC >>> defined in CMakeFiles/antlr4_shared.dir/src/ANTLRInputStream.cpp.o >>> referenced by ANTLRInputStream.cpp >>> CMakeFiles/antlr4_shared.dir/src/ANTLRInputStream.cpp.o:(antlr4::ANTLRInputStream::toStringabi:cxx11 const)

which is very strange because cmake is supposed to be turning on -fPIC automatically when building a shared library.

But I tried to force it on anyways as follows cmake .. -DCMAKE_CXX_FLAGS="-fPIC" -DCMAKE_SHARED_LINKER_FLAGS="-Wl,-z,notext" and cmake .. -DCMAKE_POSITION_INDEPENDENT_CODE=ON -DCMAKE_SHARED_LINKER_FLAGS="-Wl,-z,notext" and in both cases the output was identical to the above

Any thoughts?

bobkocisko avatar Mar 12 '20 23:03 bobkocisko

I compile ANTLR normally, but I link against the static library, and got a similar error: /usr/bin/ld: antlr-runtime-4.8/runtime/libantlr4-runtime.a(ANTLRInputStream.cpp.o): relocation R_X86_64_TPOFF32 against symbol By adding set_property(TARGET antlr4_static PROPERTY POSITION_INDEPENDENT_CODE ON) (adding -fPIC to build the static library), I managed to link properly.

victorpaleologue avatar Aug 20 '20 07:08 victorpaleologue

I compile ANTLR normally, but I link against the static library, and got a similar error: /usr/bin/ld: antlr-runtime-4.8/runtime/libantlr4-runtime.a(ANTLRInputStream.cpp.o): relocation R_X86_64_TPOFF32 against symbol By adding set_property(TARGET antlr4_static PROPERTY POSITION_INDEPENDENT_CODE ON) (adding -fPIC to build the static library), I managed to link properly.

Hey, did you put set_property(TARGET antlr4_static PROPERTY POSITION_INDEPENDENT_CODE ON) in ExternalAntlr4Cpp.cmake?

Daniel-Xu avatar Sep 06 '21 09:09 Daniel-Xu

I do not remember and I have not kept a copy of that project, sorry. It is probably set wherever add_library(antlr4_static ...) is called. But it could also be where you call target_link_libraries(... antlr4_static ...).

victorpaleologue avatar Sep 06 '21 11:09 victorpaleologue

I do not remember and I have not kept a copy of that project, sorry. It is probably set wherever add_library(antlr4_static ...) is called. But it could also be where you call target_link_libraries(... antlr4_static ...).

Thanks, I was able to use the cmake cache variable:

image

Hope this can help other people!

Daniel-Xu avatar Sep 08 '21 03:09 Daniel-Xu

I added the line "set_property(TARGET antlr4_static PROPERTY POSITION_INDEPENDENT_CODE ON)" @victorpaleologue mentioned in runtime/CMakeLists.txt
like add_library(antlr4_static STATIC ${libantlrcpp_SRC}) set_property(TARGET antlr4_static PROPERTY POSITION_INDEPENDENT_CODE ON) It works as well.

zyingp avatar Dec 18 '21 12:12 zyingp

Should be part of the documentation guys!

Enerccio avatar Sep 05 '22 08:09 Enerccio