Gentoo Websites Logo
Go to: Gentoo Home Documentation Forums Lists Bugs Planet Store Wiki Get Gentoo!
Bug 946494 - llvm-runtimes/compiler-rt-sanitizers: linked to libgcc_s.so.1 and libstdc++.so.6 despite clang-common[default-compiler-rt,default-libcxx]
Summary: llvm-runtimes/compiler-rt-sanitizers: linked to libgcc_s.so.1 and libstdc++.s...
Status: UNCONFIRMED
Alias: None
Product: Gentoo Linux
Classification: Unclassified
Component: Current packages (show other bugs)
Hardware: All Linux
: Normal normal
Assignee: LLVM support project
URL:
Whiteboard:
Keywords: PATCH
Depends on:
Blocks:
 
Reported: 2024-12-15 03:00 UTC by Stef Simoens
Modified: 2024-12-15 20:02 UTC (History)
4 users (show)

See Also:
Package list:
Runtime testing required: ---


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Stef Simoens 2024-12-15 03:00:14 UTC
Hi,

When I emerged clang & friends with clang-common[default-compiler-rt,default-libcxx], I notice that the libraries from the package compiler-rt-sanitizers were still linked to libgcc_s.so.1 and libstdc++.so.6.

This seems to be caused by the CMakefile that includes -nodefaultlibs (therefore the settings stored in /etc/clang/gentoo-runtimes.cfg are not used).

Reproducible: Always

Steps to Reproduce:
1. Emerge clang-common with USE="default-compiler-rt default-libcxx"
2. Check how the libraries are linked:
   ldd $(qlist compiler-rt-sanitizers | grep -E '.so$')
3. Libraries are linked to libgcc_s.so.1 and libstdc++.so.6 (and not to libc++abi.so.1)
Actual Results:  
Libraries linked to libgcc_s.so.1 and libstdc++.so.6 

Expected Results:  
Libraries linked to libc++abi.so.1

The following (naive?) patch to the ebuild gives the expected behavior:

diff -u /var/db/repos/gentoo/llvm-runtimes/compiler-rt-sanitizers/compiler-rt-sanitizers-19.1.4.ebuild /usr/local/portage/llvm-runtimes/compiler-rt-sanitizers/compiler-rt-sanitizers-19.1.4.ebuild 
--- /var/db/repos/gentoo/llvm-runtimes/compiler-rt-sanitizers/compiler-rt-sanitizers-19.1.4.ebuild      2024-12-11 13:28:52.000000000 +0100
+++ /usr/local/portage/llvm-runtimes/compiler-rt-sanitizers/compiler-rt-sanitizers-19.1.4.ebuild        2024-12-15 00:43:28.458403776 +0100
@@ -118,6 +118,7 @@
                local -x CC=${CHOST}-clang
                local -x CXX=${CHOST}-clang++
                strip-unsupported-flags
+               local -x CXXFLAGS="${CXXFLAGS} -stdlib=libc++"
        fi
 
        local flag want_sanitizer=OFF
@@ -138,6 +139,7 @@
                # builtins & crt installed by llvm-runtimes/compiler-rt
                -DCOMPILER_RT_BUILD_BUILTINS=OFF
                -DCOMPILER_RT_BUILD_CRT=OFF
+               -DCOMPILER_RT_USE_BUILTINS_LIBRARY=$(grep -q '^--rtlib=compiler-rt' /etc/clang/gentoo-runtimes.cfg && echo 'YES' || echo 'NO')
                -DCOMPILER_RT_BUILD_CTX_PROFILE=$(usex ctx-profile)
                -DCOMPILER_RT_BUILD_LIBFUZZER=$(usex libfuzzer)
                -DCOMPILER_RT_BUILD_MEMPROF=$(usex memprof)
Comment 1 mojyack 2024-12-15 17:14:54 UTC
I can reproduce this on ppc64 and the patch works.
Comment 2 Stef Simoens 2024-12-15 18:35:54 UTC
apologies, as I was mainly looking to make my fix work
this patch will work better for users who don't choose to have libcxx (added check before adding -stdlib=libc++ in CXXFLAGS)
I also added some comments

The modified flags are picked up by the CMakefile to trigger the desired linking

diff -u /var/db/repos/gentoo/llvm-runtimes/compiler-rt-sanitizers/compiler-rt-sanitizers-19.1.4.ebuild /usr/local/portage/llvm-runtimes/compiler-rt-sanitizers/compiler-rt-sanitizers-19.1.4.ebuild
--- /var/db/repos/gentoo/llvm-runtimes/compiler-rt-sanitizers/compiler-rt-sanitizers-19.1.4.ebuild      2024-12-11 13:28:52.000000000 +0100
+++ /usr/local/portage/llvm-runtimes/compiler-rt-sanitizers/compiler-rt-sanitizers-19.1.4.ebuild        2024-12-15 19:33:59.753376360 +0100
@@ -118,6 +118,12 @@
                local -x CC=${CHOST}-clang
                local -x CXX=${CHOST}-clang++
                strip-unsupported-flags
+
+               # check for llvm-core/clang-common[default-libcxx]
+               grep -qs '^--stdlib=libc++' /etc/clang/gentoo-runtimes.cfg
+               if [ $? -eq 0 ]; then
+                       local -x CXXFLAGS="${CXXFLAGS} -stdlib=libc++"
+               fi
        fi
 
        local flag want_sanitizer=OFF
@@ -138,6 +144,8 @@
                # builtins & crt installed by llvm-runtimes/compiler-rt
                -DCOMPILER_RT_BUILD_BUILTINS=OFF
                -DCOMPILER_RT_BUILD_CRT=OFF
+               # check for llvm-core/clang-common[default-compiler-rt]
+               -DCOMPILER_RT_USE_BUILTINS_LIBRARY=$(grep -q '^--rtlib=compiler-rt' /etc/clang/gentoo-runtimes.cfg && echo 'YES' || echo 'NO')
                -DCOMPILER_RT_BUILD_CTX_PROFILE=$(usex ctx-profile)
                -DCOMPILER_RT_BUILD_LIBFUZZER=$(usex libfuzzer)
                -DCOMPILER_RT_BUILD_MEMPROF=$(usex memprof)
Comment 3 Sam James archtester Gentoo Infrastructure gentoo-dev Security 2024-12-15 18:37:34 UTC
(In reply to Stef Simoens from comment #2)
> apologies, as I was mainly looking to make my fix work
> this patch will work better for users who don't choose to have libcxx (added
> check before adding -stdlib=libc++ in CXXFLAGS)
> I also added some comments
> 
> The modified flags are picked up by the CMakefile to trigger the desired
> linking
> 
> diff -u
> /var/db/repos/gentoo/llvm-runtimes/compiler-rt-sanitizers/compiler-rt-
> sanitizers-19.1.4.ebuild
> /usr/local/portage/llvm-runtimes/compiler-rt-sanitizers/compiler-rt-
> sanitizers-19.1.4.ebuild
> ---
> /var/db/repos/gentoo/llvm-runtimes/compiler-rt-sanitizers/compiler-rt-
> sanitizers-19.1.4.ebuild      2024-12-11 13:28:52.000000000 +0100
> +++
> /usr/local/portage/llvm-runtimes/compiler-rt-sanitizers/compiler-rt-
> sanitizers-19.1.4.ebuild        2024-12-15 19:33:59.753376360 +0100
> @@ -118,6 +118,12 @@
>                 local -x CC=${CHOST}-clang
>                 local -x CXX=${CHOST}-clang++
>                 strip-unsupported-flags
> +
> +               # check for llvm-core/clang-common[default-libcxx]
> +               grep -qs '^--stdlib=libc++' /etc/clang/gentoo-runtimes.cfg
> +               if [ $? -eq 0 ]; then
> +                       local -x CXXFLAGS="${CXXFLAGS} -stdlib=libc++"
> +               fi

We should use tc-get-cxx-stdlib from toolchain-funcs.eclass.

>         fi
>  
>         local flag want_sanitizer=OFF
> @@ -138,6 +144,8 @@
>                 # builtins & crt installed by llvm-runtimes/compiler-rt
>                 -DCOMPILER_RT_BUILD_BUILTINS=OFF
>                 -DCOMPILER_RT_BUILD_CRT=OFF
> +               # check for llvm-core/clang-common[default-compiler-rt]
> +               -DCOMPILER_RT_USE_BUILTINS_LIBRARY=$(grep -q
> '^--rtlib=compiler-rt' /etc/clang/gentoo-runtimes.cfg && echo 'YES' || echo
> 'NO')

Ditto tc-get-c-rtlib.

>                 -DCOMPILER_RT_BUILD_CTX_PROFILE=$(usex ctx-profile)
>                 -DCOMPILER_RT_BUILD_LIBFUZZER=$(usex libfuzzer)
>                 -DCOMPILER_RT_BUILD_MEMPROF=$(usex memprof)
Comment 4 Stef Simoens 2024-12-15 20:02:34 UTC
you mean like this?
(tested and works for my config)

FYI to give transparency regarding why these flags are modified, these are the relevant config from the CMakeLists.txt

# in CMakeLists.txt: -nostdlib is added, and an alternative way of including the library to link to is selected 
# macro(handle_default_cxx_lib var)
#  # Specifying -stdlib= in CMAKE_CXX_FLAGS overrides the defaults.
#  if (CMAKE_CXX_FLAGS MATCHES "-stdlib=([a-zA-Z+]*)")
#    set(${var}_LIBNAME "${CMAKE_MATCH_1}")
#    set(${var}_SYSTEM 1)
# (...)
# 
# likewise for gcc_s
# if (COMPILER_RT_USE_BUILTINS_LIBRARY)
#  string(REPLACE "-Wl,-z,defs" "" CMAKE_SHARED_LINKER_FLAGS "${CMAKE_SHARED_LINKER_FLAGS}")
# else()
#   if (ANDROID)
#     append_list_if(COMPILER_RT_HAS_GCC_LIB gcc SANITIZER_COMMON_LINK_LIBS)
#   else()
#     append_list_if(COMPILER_RT_HAS_GCC_S_LIB gcc_s SANITIZER_COMMON_LINK_LIBS)
#   endif()
# endif()

diff -u /var/db/repos/gentoo/llvm-runtimes/compiler-rt-sanitizers/compiler-rt-sanitizers-19.1.4.ebuild /usr/local/portage/llvm-runtimes/compiler-rt-sanitizers/compiler-rt-sanitizers-19.1.4.ebuild
--- /var/db/repos/gentoo/llvm-runtimes/compiler-rt-sanitizers/compiler-rt-sanitizers-19.1.4.ebuild      2024-12-11 13:28:52.000000000 +0100
+++ /usr/local/portage/llvm-runtimes/compiler-rt-sanitizers/compiler-rt-sanitizers-19.1.4.ebuild        2024-12-15 20:33:03.312575993 +0100
@@ -118,6 +118,9 @@
                local -x CC=${CHOST}-clang
                local -x CXX=${CHOST}-clang++
                strip-unsupported-flags
+
+               # link to libc++
+               local -x CXXFLAGS="${CXXFLAGS} -stdlib=$(tc-get-cxx-stdlib)"
        fi
 
        local flag want_sanitizer=OFF
@@ -128,6 +131,10 @@
                fi
        done
 
+       # link to compiler-rt
+       local use_compiler_rt=OFF
+       [[ $(tc-get-c-rtlib) == compiler-rt ]] && use_compiler_rt=ON
+
        local mycmakeargs=(
                -DCOMPILER_RT_INSTALL_PATH="${EPREFIX}/usr/lib/clang/${LLVM_MAJOR}"
                # use a build dir structure consistent with install
@@ -138,6 +145,7 @@
                # builtins & crt installed by llvm-runtimes/compiler-rt
                -DCOMPILER_RT_BUILD_BUILTINS=OFF
                -DCOMPILER_RT_BUILD_CRT=OFF
+               -DCOMPILER_RT_USE_BUILTINS_LIBRARY=${use_compiler_rt}
                -DCOMPILER_RT_BUILD_CTX_PROFILE=$(usex ctx-profile)
                -DCOMPILER_RT_BUILD_LIBFUZZER=$(usex libfuzzer)
                -DCOMPILER_RT_BUILD_MEMPROF=$(usex memprof)