Page MenuHomePhabricator

Fix static linking failure with --unwindlib=libunwind
Needs ReviewPublic

Authored by raj.khem on Aug 1 2019, 6:22 AM.

Details

Summary

when trying to use llvm libunwind via clang driver option --unwindlib and using -static together, linking fails with missing symbols

libunwind/src/RWMutex.hpp:68: undefined reference to `pthread_rwlock_wrlock'
..
libunwind/src/AddressSpace.hpp:597: undefined reference to `dladdr'

There are missing symbols in linunwind.a which should be coming from libpthread and libdl

Diff Detail

Repository
rC Clang

Event Timeline

raj.khem created this revision.Aug 1 2019, 6:22 AM

This is a tricky one which may vary depending on the libraries available on different systems. Which toolchain is this? Can you add more context?

-ldl doesn't work on all platforms (e.g. android, FreeBSD, etc). -lpthread is wrong - if you want to add that, I think that we need to improve the -thread-model flag in clang first (it currently just always passes posix, which is ignored; but would identify the threading model). -lpthread is wrong - consider building on Solaris with Solaris threads rather than POSIX threads, or on Windows with the Win32 threading. This really is inline with the work that needs to be finished up with having library link dependencies for static libraries (i.e. #pragma comment(lib, …)).

This is a tricky one which may vary depending on the libraries available on different systems. Which toolchain is this? Can you add more context?

@rengolin this is when building linux system with glibc ( clang+clang-runtime which means it uses llvm libunwind too) it works fine with -lunwindlib=libgcc but llvm libunwind does have calls into libpthread and uses dlopen as well. I did not build non-linux systems so can not say if
its same issue on them too but on linux its the case. I am building Yocto/OE using clang as cross compiler.

-ldl doesn't work on all platforms (e.g. android, FreeBSD, etc). -lpthread is wrong - if you want to add that, I think that we need to improve the -thread-model flag in clang first (it currently just always passes posix, which is ignored; but would identify the threading model). -lpthread is wrong - consider building on Solaris with Solaris threads rather than POSIX threads, or on Windows with the Win32 threading. This really is inline with the work that needs to be finished up with having library link dependencies for static libraries (i.e. #pragma comment(lib, …)).

we are assuming llvm libunwind to have no other dependencies, but thats not the case atleast not on linux, I understand this code is in common area and will impact all, maybe this should be moved to toolchain specific argument processing, where we can then override it specifically for linux environment.

MaskRay added a subscriber: MaskRay.Aug 4 2019, 7:08 PM

If libunwind is configured with -DLIBUNWIND_ENABLE_THREADS=Off / it is on Windows / -DLIBUNWIND_USE_WEAK_PTHREAD=On, libpthread is not a hard dependency.

If you use lld:

#if _LIBUNWIND_USE_DLADDR
#include <dlfcn.h>
#if defined(__unix__) && defined(__ELF__) && defined(_LIBUNWIND_HAS_COMMENT_LIB_PRAGMA)
#pragma comment(lib, "dl")
#endif
#endif

#if defined(__unix__) && !defined(__ANDROID__) && defined(__ELF__) && defined(_LIBUNWIND_HAS_COMMENT_LIB_PRAGMA)
#pragma comment(lib, "pthread")
#endif