Skip to content

Commit

Permalink
[libunwind] Use -nostdlib++ when linking libunwind (#75646)
Browse files Browse the repository at this point in the history
We shouldn't need to link against libc++ or libc++abi when building
libunwind, since that would otherwise be a circular dependency.
  • Loading branch information
ldionne committed Dec 19, 2023
1 parent e9af57d commit ab41ea4
Showing 1 changed file with 13 additions and 9 deletions.
22 changes: 13 additions & 9 deletions libunwind/src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -66,20 +66,24 @@ set(LIBUNWIND_SOURCES
${LIBUNWIND_ASM_SOURCES})

# Generate library list.
if (LIBUNWIND_USE_COMPILER_RT)
add_library_flags("${LIBUNWIND_BUILTINS_LIBRARY}")
if (CXX_SUPPORTS_NOSTDLIBXX_FLAG)
add_link_flags_if_supported(-nostdlib++)
else()
add_library_flags_if(LIBUNWIND_HAS_GCC_S_LIB gcc_s)
add_library_flags_if(LIBUNWIND_HAS_GCC_LIB gcc)
endif()
if (NOT APPLE) # On Apple platforms, we don't need to link explicitly against system libraries
if (LIBUNWIND_USE_COMPILER_RT)
add_library_flags("${LIBUNWIND_BUILTINS_LIBRARY}")
else()
add_library_flags_if(LIBUNWIND_HAS_GCC_S_LIB gcc_s)
add_library_flags_if(LIBUNWIND_HAS_GCC_LIB gcc)
endif()
add_library_flags_if(LIBUNWIND_HAS_C_LIB c)

This comment has been minimized.

Copy link
@androm3da

androm3da Jan 31, 2024

Member

@ldionne - I'm investigating a regression with my toolchain build of the libunwind shared object.

Before this commit landed, IIUC we would unconditionally check LIBUNWIND_HAS_C_LIB and add -lc if it was present. But now, we only check LIBUNWIND_HAS_C_LIB if CXX_SUPPORTS_NOSTDLIBXX_FLAG is OFF. Is that what's expected? I'm wondering if this commit guarded the add_library_flags_if(LIBUNWIND_HAS_C_LIB c) incorrectly, or if perhaps my libunwind build shouldn't have depended on libc?

: && /local/mnt/workspace/upstream/toolchain_for_hexagon/clang+llvm-17.0.0-rc3-cross-hexagon-unknown-linux-musl/x86_64-linux-gnu/bin/he
xagon-unknown-linux-musl-clang -fPIC -Werror=date-time -Werror=unguarded-availability-new -Wall -Wextra -Wno-unused-parameter -Wwrite-s
trings -Wcast-qual -Wmissing-field-initializers -Wimplicit-fallthrough -Wcovered-switch-default -Wstring-conversion -Wmisleading-indent
ation -Wctad-maybe-unsupported -fdiagnostics-color -ffunction-sections -fdata-sections -O3 -DNDEBUG  -lclang_rt.builtins-hexagon -nostd
lib -Wl,-z,defs -Wl,-z,nodelete -Wl,--color-diagnostics  -nostdlib++ --unwindlib=none -shared -Wl,-soname,libunwind.so.1 -o lib/libunwi
nd.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/Unwi
ndLevel1.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/Unwind-wasm.c.o libunwind/src/CMakeFiles/unwind_shared_objects.
dir/UnwindRegistersRestore.S.o libunwind/src/CMakeFiles/unwind_shared_objects.dir/UnwindRegistersSave.S.o   && :
hexagon-unknown-linux-musl-clang: warning: argument unused during compilation: '--unwindlib=none' [-Wunused-command-line-argument]
ld.lld: error: undefined symbol: stderr
>>> referenced by UnwindLevel1-gcc-ext.c
>>>               libunwind/src/CMakeFiles/unwind_shared_objects.dir/UnwindLevel1-gcc-ext.c.o:(_Unwind_Resume_or_Rethrow)
>>> referenced by UnwindLevel1-gcc-ext.c
>>>               libunwind/src/CMakeFiles/unwind_shared_objects.dir/UnwindLevel1-gcc-ext.c.o:(_Unwind_Resume_or_Rethrow)
>>> referenced by UnwindLevel1-gcc-ext.c
>>>               libunwind/src/CMakeFiles/unwind_shared_objects.dir/UnwindLevel1-gcc-ext.c.o:(_Unwind_Resume_or_Rethrow)
>>> referenced 161 more times

ld.lld: error: undefined symbol: fprintf
>>> referenced by UnwindLevel1-gcc-ext.c
>>>               libunwind/src/CMakeFiles/unwind_shared_objects.dir/UnwindLevel1-gcc-ext.c.o:(_Unwind_Resume_or_Rethrow)
>>> referenced by UnwindLevel1-gcc-ext.c
>>>               libunwind/src/CMakeFiles/unwind_shared_objects.dir/UnwindLevel1-gcc-ext.c.o:(_Unwind_Resume_or_Rethrow)
>>> referenced by UnwindLevel1-gcc-ext.c
>>>               libunwind/src/CMakeFiles/unwind_shared_objects.dir/UnwindLevel1-gcc-ext.c.o:(_Unwind_GetDataRelBase)
>>> referenced 97 more times
 
...

Unabridged (success) log from e9af57dfea1637a3872052b317c9fe6c71b8d290.log

Unabridged (failing) log from ab41ea4be364dcac32d0c4ec990735c8adb279c8.log

This comment has been minimized.

Copy link
@ldionne

ldionne Jan 31, 2024

Author Member

The issue must be that Clang isn't linking against your C Standard Library by default, so adding -lc explicitly is required. What this patch does is basically let the compiler supply the required runtime libraries to link against except the C++ Standard Library, which should include the C Standard Library if the driver is behaving correctly on your platform. Does that make sense?

This comment has been minimized.

Copy link
@androm3da

androm3da Jan 31, 2024

Member

Yeah - looks like it might be a driver issue indeed. Thanks!

/local/mnt/workspace/upstream/toolchain_for_hexagon/clang+llvm-17.0.0-rc3-cross-hexagon-unknown-linux-musl/x86_64-linux-gnu/bin/hexagon-unknown-linux-musl-clang -fPIC -Werror=date-time -Werror=unguarded-availability-new -Wall -Wextra -Wno-unused-parameter -Wwrite-strings -Wcast-qual -Wmissing-field-initializers -Wimplicit-fallthrough -Wcovered-switch-default -Wstring-conversion -Wmisleading-indentation -Wctad-maybe-unsupported -fdiagnostics-color -ffunction-sections -fdata-sections -O3 -DNDEBUG  -lclang_rt.builtins-hexagon -nostdlib -Wl,-z,defs -Wl,-z,nodelete -Wl,--color-diagnostics  -nostdlib++ --unwindlib=none -shared -Wl,-soname,libunwind.so.1 -o 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/Unwind-wasm.c.o libunwind/src/CMakeFiles/unwind_shared_objects.dir/UnwindRegistersRestore.S.o libunwind/src/CMakeFiles/unwind_shared_objects.dir/UnwindRegistersSave.S.o -###
...
 "/local/mnt/workspace/upstream/toolchain_for_hexagon/clang+llvm-17.0.0-rc3-cross-hexagon-unknown-linux-musl/x86_64-linux-gnu/bin/ld.lld" "-z" "relro" "-shared" "-call_shared" "-G0" "-o" "lib/libunwind.so.1.0" "-L/local/mnt/workspace/upstream/toolchain_for_hexagon/clang+llvm-17.0.0-rc3-cross-hexagon-unknown-linux-musl/x86_64-linux-gnu/bin/../target/hexagon-unknown-linux-musl//usr/lib" "-lclang_rt.builtins-hexagon" "-z" "defs" "-z" "nodelete" "--color-diagnostics" "-soname" "libunwind.so.1" "-L/local/mnt/workspace/upstream/toolchain_for_hexagon/clang+llvm-17.0.0-rc3-cross-hexagon-unknown-linux-musl/x86_64-linux-gnu/bin/../target/hexagon/lib/v60/G0/pic" "-L/local/mnt/workspace/upstream/toolchain_for_hexagon/clang+llvm-17.0.0-rc3-cross-hexagon-unknown-linux-musl/x86_64-linux-gnu/bin/../target/hexagon/lib/v60/G0" "-L/local/mnt/workspace/upstream/toolchain_for_hexagon/clang+llvm-17.0.0-rc3-cross-hexagon-unknown-linux-musl/x86_64-linux-gnu/bin/../target/hexagon/lib/v60" "-L/local/mnt/workspace/upstream/toolchain_for_hexagon/clang+llvm-17.0.0-rc3-cross-hexagon-unknown-linux-musl/x86_64-linux-gnu/bin/../target/hexagon/lib"

This comment has been minimized.

Copy link
@androm3da

androm3da Jan 31, 2024

Member

Oh but wait there's also a -nostdlib too - that must be suppressing the -lc. That suggests that the driver is working as expected.

This comment has been minimized.

Copy link
@androm3da

androm3da Jan 31, 2024

Member

For the time being, I am able to workaround this issue by forcing CXX_SUPPORTS_NOSTDLIBXX_FLAG to OFF. That setting is not accurate for clang, but gets the "right enough" (?) behavior (-lc but not -nostdlib++) for me to continue.

This comment has been minimized.

Copy link
@ldionne

ldionne Jan 31, 2024

Author Member

Can you open an issue? If we're passing -nostdlib, then this is expected to fail, and I think we need to fix that in libunwind.

This comment has been minimized.

Copy link
@androm3da

androm3da Jan 31, 2024

Member

opened #80189

endif()

if (NOT APPLE)
add_library_flags_if(LIBUNWIND_HAS_DL_LIB dl)
endif()

if (LIBUNWIND_ENABLE_THREADS)
if (LIBUNWIND_ENABLE_THREADS AND NOT APPLE)
add_library_flags_if(LIBUNWIND_HAS_PTHREAD_LIB pthread)
add_compile_flags_if(LIBUNWIND_WEAK_PTHREAD_LIB -DLIBUNWIND_USE_WEAK_PTHREAD=1)
endif()
endif()

if (LIBUNWIND_ENABLE_THREADS)
Expand Down

0 comments on commit ab41ea4

Please sign in to comment.