antlr / antlr4

ANTLR (ANother Tool for Language Recognition) is a powerful parser generator for reading, processing, executing, or translating structured text or binary files.
http://antlr.org
BSD 3-Clause "New" or "Revised" License
17.14k stars 3.28k forks source link

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

Open bobkocisko opened 4 years ago

bobkocisko commented 4 years ago

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, std::allocator > >(std::__cxx11::basic_string<char32_t, std::char_traits, std::allocator > 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::toString[abi: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, std::allocator > >(std::__cxx11::basic_string<char32_t, std::char_traits, std::allocator > 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::toString[abi: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?

victorpaleologue commented 4 years ago

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.

Daniel-Xu commented 3 years ago

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?

victorpaleologue commented 3 years ago

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 ...).

Daniel-Xu commented 3 years ago

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!

zyingp commented 2 years ago

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.

Enerccio commented 2 years ago

Should be part of the documentation guys!