llvm / llvm-project

The LLVM Project is a collection of modular and reusable compiler and toolchain technologies.
http://llvm.org
Other
28.7k stars 11.87k forks source link

libunwind fails to build on Debian Buster on a `error: undefined reference to 'pthread_rwlock_wrlock'` #55629

Closed sylvestre closed 2 years ago

sylvestre commented 2 years ago

Fails on arm64 & amd64

FAILED: /build/llvm-toolchain-snapshot-15~++20220521115334+295d032762ad/build-llvm/lib/libunwind.so.1.0 
: && "/build/llvm-toolchain-snapshot-15~++20220521115334+295d032762ad/build-llvm/./bin/clang" --target=aarch64-unknown-linux-gnu -fPIC -fstack-protector-strong -Wformat -Werror=format-security -Wno-unused-command-line-argument -Wdate-time -D_FORTIFY_SOURCE=2 -fPIC -fno-semantic-interposition -Werror=date-time -Werror=unguarded-availability-new -Wall -Wextra -Wno-unused-parameter -Wwrite-strings -Wmissing-field-initializers -Wimplicit-fallthrough -Wcovered-switch-default -Wstring-conversion -Wmisleading-indentation -fdiagnostics-color -ffunction-sections -fdata-sections -ffile-prefix-map=/build/llvm-toolchain-snapshot-15~++20220521115334+295d032762ad/build-llvm/runtimes/runtimes-bins=build-llvm/runtimes/runtimes-bins -ffile-prefix-map=/build/llvm-toolchain-snapshot-15~++20220521115334+295d032762ad/= -no-canonical-prefixes -O3 -DNDEBUG  -Wl,-z,relro -Wl,--build-id -Wl,-z,defs -Wl,-z,nodelete -fuse-ld=gold  -rtlib=compiler-rt --unwindlib=none -nostdlib++ -shared -Wl,-soname,libunwind.so.1 -o "/build/llvm-toolchain-snapshot-15~++20220521115334+295d032762ad/build-llvm/lib/libunwind.so.1.0" libunwind/src/CMakeFiles/unwind_shared_objects.dir/libunwind.cpp.o libunwind/src/CMakeFiles/unwind_shared_objects.dir/Unwind-EHABI.cpp.o libunwind/src/CMakeFiles/unwind_shared_objects.dir/Unwind-seh.cpp.o libunwind/src/CMakeFiles/unwind_shared_objects.dir/UnwindLevel1.c.o libunwind/src/CMakeFiles/unwind_shared_objects.dir/UnwindLevel1-gcc-ext.c.o libunwind/src/CMakeFiles/unwind_shared_objects.dir/Unwind-sjlj.c.o libunwind/src/CMakeFiles/unwind_shared_objects.dir/UnwindRegistersRestore.S.o libunwind/src/CMakeFiles/unwind_shared_objects.dir/UnwindRegistersSave.S.o   && :
libunwind/src/CMakeFiles/unwind_shared_objects.dir/libunwind.cpp.o:libunwind.cpp:function __unw_iterate_dwarf_unwind_cache: error: undefined reference to 'pthread_rwlock_wrlock'
libunwind/src/CMakeFiles/unwind_shared_objects.dir/libunwind.cpp.o:libunwind.cpp:function __unw_iterate_dwarf_unwind_cache: error: undefined reference to 'pthread_rwlock_unlock'
libunwind/src/CMakeFiles/unwind_shared_objects.dir/libunwind.cpp.o:libunwind.cpp:function libunwind::DwarfFDECache<libunwind::LocalAddressSpace>::add(unsigned long, unsigned long, unsigned long, unsigned long): error: undefined reference to 'pthread_rwlock_wrlock'
libunwind/src/CMakeFiles/unwind_shared_objects.dir/libunwind.cpp.o:libunwind.cpp:function libunwind::DwarfFDECache<libunwind::LocalAddressSpace>::add(unsigned long, unsigned long, unsigned long, unsigned long): error: undefined reference to 'pthread_rwlock_unlock'
libunwind/src/CMakeFiles/unwind_shared_objects.dir/libunwind.cpp.o:libunwind.cpp:function __unw_remove_dynamic_fde: error: undefined reference to 'pthread_rwlock_wrlock'
libunwind/src/CMakeFiles/unwind_shared_objects.dir/libunwind.cpp.o:libunwind.cpp:function __unw_remove_dynamic_fde: error: undefined reference to 'pthread_rwlock_unlock'
libunwind/src/CMakeFiles/unwind_shared_objects.dir/libunwind.cpp.o:libunwind.cpp:function __unw_remove_dynamic_eh_frame_section: error: undefined reference to 'pthread_rwlock_wrlock'
libunwind/src/CMakeFiles/unwind_shared_objects.dir/libunwind.cpp.o:libunwind.cpp:function __unw_remove_dynamic_eh_frame_section: error: undefined reference to 'pthread_rwlock_unlock'
libunwind/src/CMakeFiles/unwind_shared_objects.dir/libunwind.cpp.o:libunwind.cpp:function libunwind::UnwindCursor<libunwind::LocalAddressSpace, libunwind::Registers_arm64>::getFunctionName(char*, unsigned long, unsigned long*): error: undefined reference to 'dladdr'
libunwind/src/CMakeFiles/unwind_shared_objects.dir/libunwind.cpp.o:libunwind.cpp:function libunwind::UnwindCursor<libunwind::LocalAddressSpace, libunwind::Registers_arm64>::setInfoBasedOnIPRegister(bool): error: undefined reference to 'pthread_rwlock_rdlock'
libunwind/src/CMakeFiles/unwind_shared_objects.dir/libunwind.cpp.o:libunwind.cpp:function libunwind::DwarfFDECache<libunwind::LocalAddressSpace>::findFDE(unsigned long, unsigned long): error: undefined reference to 'pthread_rwlock_rdlock'
clang: error: linker command failed with exit code 1 (use -v to see invocation)
sylvestre commented 2 years ago

Adding -lpthread -ldl manually fixes the issue

sylvestre commented 2 years ago

I fixed this with an ugly patch to force the link to pthread & dl (as I am targeting only debian & ubuntu)

Index: llvm-toolchain-snapshot-15~++20220521115334+295d032762ad/libunwind/CMakeLists.txt
===================================================================
--- llvm-toolchain-snapshot-15~++20220521115334+295d032762ad.orig/libunwind/CMakeLists.txt
+++ llvm-toolchain-snapshot-15~++20220521115334+295d032762ad/libunwind/CMakeLists.txt
@@ -309,14 +309,17 @@ if (MSVC)
   add_definitions(-D_CRT_SECURE_NO_WARNINGS)
 endif()

-if (C_SUPPORTS_COMMENT_LIB_PRAGMA)
-  if (LIBUNWIND_HAS_DL_LIB)
+list(APPEND LIBUNWIND_LINK_FLAGS "-ldl")
+list(APPEND LIBUNWIND_LINK_FLAGS "-lpthread")
+
+#if (C_SUPPORTS_COMMENT_LIB_PRAGMA)
+#  if (LIBUNWIND_HAS_DL_LIB)
     add_definitions(-D_LIBUNWIND_LINK_DL_LIB)
-  endif()
-  if (LIBUNWIND_HAS_PTHREAD_LIB)
+#  endif()
+#  if (LIBUNWIND_HAS_PTHREAD_LIB)
     add_definitions(-D_LIBUNWIND_LINK_PTHREAD_LIB)
-  endif()
-endif()
+#  endif()
+#endif()

 #===============================================================================
 # Setup Source Code
sylvestre commented 2 years ago

I could not make it work with -DLIBUNWIND_LINK_FLAGS="-ldl -lpthread"

Enna1 commented 2 days ago

Seem like a cmake bug, upgrading the cmake version solves this issue for me.