orocos-toolchain / rtt

Orocos Real-Time Toolkit
http://www.orocos.org
Other
72 stars 79 forks source link

omit linker flag in presence of -fsanitize #175

Closed rhaschke closed 6 years ago

rhaschke commented 8 years ago

In gcc environment orocos is compiled with linker option -z defs to ensure that all symbols in linked object files are defined. This is perfectly fine. However, when used with AddressSanitizer (option -fsanitize=address), the linker fails with lots of undefined symbols, because gcc/ld don't link shared libraries against libasan. This is only done for executables.

Hence, to enable linking, the linker option -z defs needs to be omitted when address sanitizer is used. This patch solved the issue for me. However, checking CMAKE_CXX_FLAGS might not be the best option. Alternatively, I considered CMAKE_SHARED_LINKER_FLAGS. However, in both case there are several version of these variables depending on the choosen CMAKE_BUILD_TYPE.

meyerj commented 8 years ago

Is it necessary to patch UseOROCOS-RTT.cmake for this? I am not familiar with AddressSanitizer, but would it be an option to simply link all libraries to libasan once you add -fsanitize=address?

Something like

set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fsanitize=address")
link_libraries(-lasan)

There might be some differences between GCC and Clang, but the above snippet works for me with gcc-4.8 in Ubuntu 14.04.

From https://github.com/google/sanitizers/wiki/AddressSanitizer#faq:

Q: When I link my shared library with -fsanitize=address, it fails due to some undefined ASan symbols (e.g. asan_init_v4)? A: Most probably you link with -Wl,-z,defs or -Wl,--no-undefined. These flags don't work with ASan unless you also use -shared-libasan (which is the default mode for GCC, but not for Clang).

rhaschke commented 8 years ago

I just propose to take some action in orocos's cmake file if -fsanitize=address is present. As the -Wl,-z,defs option is introduced by orocos, orocos should also deal with potential conflicts. Of course adding -lasan would be feasible as well - even better, because you don't loose the symbol-checking feature.

I'm using a global setting on the command-line / in my environment to configure a whole bunch of orocos packages. It wouldn't make sense to fix the conflict in all packages individually.

UPDATE Using the cmake command link_libraries is deprecated.

rhaschke commented 6 years ago

Closing, because there is no more communication. I'm using a local patch now.