Skip to content

Commit

Permalink
compiler-rt: apply armv6l patches to llvm >= 15 where applicable (#33…
Browse files Browse the repository at this point in the history
  • Loading branch information
symphorien authored Sep 15, 2024
2 parents ae51a98 + 7b6139f commit 07c8996
Show file tree
Hide file tree
Showing 4 changed files with 154 additions and 6 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
From a56bb19a9dc303a50ef12d83cd24c2395bf81076 Mon Sep 17 00:00:00 2001
From: Ben Wolsieffer <benwolsieffer@gmail.com>
Date: Wed, 7 Dec 2022 21:25:46 -0500
Subject: [PATCH] [scudo][standalone] Use CheckAtomic to decide to link to
libatomic

Standalone scudo uses the atomic operation builtin functions, which require
linking to libatomic on some platforms. Currently, this is done in an ad-hoc
manner. MIPS platforms always link to libatomic, and the tests are always linked
to it as well. libatomic is required on base ARMv6 (but not ARMv6K), but it is
currently not linked, causing the build to fail.

This patch replaces this ad-hoc logic with the CheckAtomic CMake module already
used in other parts of LLVM. The CheckAtomic module checks whether std::atomic
requires libatomic, which is not strictly the same as checking the atomic
builtins, but should have the same results as far as I know. If this is
problematic, a custom version of CheckAtomic could be used to specifically test
the builtins.
---
compiler-rt/lib/scudo/standalone/CMakeLists.txt | 7 +++++++
compiler-rt/lib/scudo/standalone/tests/CMakeLists.txt | 4 +---
2 files changed, 8 insertions(+), 3 deletions(-)

diff --git a/lib/scudo/standalone/CMakeLists.txt b/lib/scudo/standalone/CMakeLists.txt
index ae5c354768c8..eb27374ca520 100644
--- a/lib/scudo/standalone/CMakeLists.txt
+++ b/lib/scudo/standalone/CMakeLists.txt
@@ -1,5 +1,8 @@
add_compiler_rt_component(scudo_standalone)

+include(DetermineGCCCompatible)
+include(CheckAtomic)
+
include_directories(../.. include)

set(SCUDO_CFLAGS)
@@ -34,6 +37,10 @@ list(APPEND SCUDO_LINK_FLAGS -Wl,-z,defs,-z,now,-z,relro)

list(APPEND SCUDO_LINK_FLAGS -ffunction-sections -fdata-sections -Wl,--gc-sections)

+if(HAVE_CXX_ATOMICS_WITH_LIB OR HAVE_CXX_ATOMICS64_WITH_LIB)
+ list(APPEND SCUDO_LINK_FLAGS -latomic)
+endif()
+
# We don't use the C++ standard library, so avoid including it by mistake.
append_list_if(COMPILER_RT_HAS_NOSTDLIBXX_FLAG -nostdlib++ SCUDO_LINK_FLAGS)
append_list_if(CXX_SUPPORTS_UNWINDLIB_NONE_FLAG --unwindlib=none SCUDO_LINK_FLAGS)
diff --git a/lib/scudo/standalone/tests/CMakeLists.txt b/lib/scudo/standalone/tests/CMakeLists.txt
index 8200cd2588b3..73b3e9403c35 100644
--- a/lib/scudo/standalone/tests/CMakeLists.txt
+++ b/lib/scudo/standalone/tests/CMakeLists.txt
@@ -39,9 +39,7 @@ set(SCUDO_UNITTEST_LINK_FLAGS
${COMPILER_RT_UNWINDER_LINK_LIBS}
${SANITIZER_TEST_CXX_LIBRARIES})
list(APPEND SCUDO_UNITTEST_LINK_FLAGS -pthread -no-pie)
-# Linking against libatomic is required with some compilers
-check_library_exists(atomic __atomic_load_8 "" COMPILER_RT_HAS_LIBATOMIC)
-if (COMPILER_RT_HAS_LIBATOMIC)
+if (HAVE_CXX_ATOMICS_WITH_LIB OR HAVE_CXX_ATOMICS64_WITH_LIB)
list(APPEND SCUDO_UNITTEST_LINK_FLAGS -latomic)
endif()

--
2.38.1

Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
From a56bb19a9dc303a50ef12d83cd24c2395bf81076 Mon Sep 17 00:00:00 2001
From: Ben Wolsieffer <benwolsieffer@gmail.com>
Date: Wed, 7 Dec 2022 21:25:46 -0500
Subject: [PATCH] [scudo][standalone] Use CheckAtomic to decide to link to
libatomic

Standalone scudo uses the atomic operation builtin functions, which require
linking to libatomic on some platforms. Currently, this is done in an ad-hoc
manner. MIPS platforms always link to libatomic, and the tests are always linked
to it as well. libatomic is required on base ARMv6 (but not ARMv6K), but it is
currently not linked, causing the build to fail.

This patch replaces this ad-hoc logic with the CheckAtomic CMake module already
used in other parts of LLVM. The CheckAtomic module checks whether std::atomic
requires libatomic, which is not strictly the same as checking the atomic
builtins, but should have the same results as far as I know. If this is
problematic, a custom version of CheckAtomic could be used to specifically test
the builtins.
---
compiler-rt/lib/scudo/standalone/CMakeLists.txt | 7 +++++++
compiler-rt/lib/scudo/standalone/tests/CMakeLists.txt | 4 +---
2 files changed, 8 insertions(+), 3 deletions(-)

diff --git a/lib/scudo/standalone/CMakeLists.txt b/lib/scudo/standalone/CMakeLists.txt
index dc700cec9bec..671dc7046604 100644
--- a/lib/scudo/standalone/CMakeLists.txt
+++ b/lib/scudo/standalone/CMakeLists.txt
@@ -1,5 +1,8 @@
add_compiler_rt_component(scudo_standalone)

+include(DetermineGCCCompatible)
+include(CheckAtomic)
+
include_directories(../.. include)

set(SCUDO_CFLAGS)
@@ -39,6 +42,10 @@ list(APPEND SCUDO_LINK_FLAGS -Wl,-z,defs,-z,now,-z,relro)

list(APPEND SCUDO_LINK_FLAGS -ffunction-sections -fdata-sections -Wl,--gc-sections)

+if(HAVE_CXX_ATOMICS_WITH_LIB OR HAVE_CXX_ATOMICS64_WITH_LIB)
+ list(APPEND SCUDO_LINK_FLAGS -latomic)
+endif()
+
# We don't use the C++ standard library, so avoid including it by mistake.
append_list_if(COMPILER_RT_HAS_NOSTDLIBXX_FLAG -nostdlib++ SCUDO_LINK_FLAGS)
append_list_if(CXX_SUPPORTS_UNWINDLIB_NONE_FLAG --unwindlib=none SCUDO_LINK_FLAGS)
diff --git a/lib/scudo/standalone/tests/CMakeLists.txt b/lib/scudo/standalone/tests/CMakeLists.txt
index a85eb737dba0..a23cf4d494f6 100644
--- a/lib/scudo/standalone/tests/CMakeLists.txt
+++ b/lib/scudo/standalone/tests/CMakeLists.txt
@@ -47,7 +47,7 @@ set(SCUDO_UNITTEST_LINK_FLAGS
${SANITIZER_TEST_CXX_LIBRARIES})
list(APPEND SCUDO_UNITTEST_LINK_FLAGS -pthread -no-pie)

-append_list_if(COMPILER_RT_HAS_LIBATOMIC -latomic SCUDO_UNITTEST_LINK_FLAGS)
+append_list_if((HAVE_CXX_ATOMICS_WITH_LIB OR HAVE_CXX_ATOMICS64_WITH_LIB) -latomic SCUDO_UNITTEST_LINK_FLAGS)

set(SCUDO_TEST_HEADERS
scudo_unit_test.h
2.38.1

33 changes: 27 additions & 6 deletions pkgs/development/compilers/llvm/common/default.nix
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,21 @@ let
path = ../12;
}
];
"compiler-rt/armv6-scudo-libatomic.patch" = [
{
after = "19";
path = ../19;
}
{
after = "15";
before = "19";
path = ../15;
}
{
before = "15";
path = ../14;
}
];
"compiler-rt/armv7l.patch" = [
{
before = "15";
Expand Down Expand Up @@ -935,10 +950,10 @@ let
lib.optional (lib.versionOlder metadata.release_version "18")
# Prevent a compilation error on darwin
(metadata.getVersionFile "compiler-rt/darwin-targetconditionals.patch")
++
lib.optional (lib.versionAtLeast metadata.release_version "15")
# See: https://github.com/NixOS/nixpkgs/pull/186575
./compiler-rt/darwin-plistbuddy-workaround.patch
++ lib.optionals (lib.versionAtLeast metadata.release_version "15") [
# See: https://github.com/NixOS/nixpkgs/pull/186575
./compiler-rt/darwin-plistbuddy-workaround.patch
]
++
lib.optional (lib.versions.major metadata.release_version == "15")
# See: https://github.com/NixOS/nixpkgs/pull/194634#discussion_r999829893
Expand All @@ -949,9 +964,15 @@ let
# Fix build on armv6l
./compiler-rt/armv6-mcr-dmb.patch
./compiler-rt/armv6-sync-ops-no-thumb.patch
./compiler-rt/armv6-no-ldrexd-strexd.patch
]
++ lib.optionals (lib.versionOlder metadata.release_version "18") [
# Fix build on armv6l
./compiler-rt/armv6-scudo-no-yield.patch
./compiler-rt/armv6-scudo-libatomic.patch
]
++ [
# Fix build on armv6l
./compiler-rt/armv6-no-ldrexd-strexd.patch
(metadata.getVersionFile "compiler-rt/armv6-scudo-libatomic.patch")
]
++ lib.optional (lib.versionAtLeast metadata.release_version "19") (fetchpatch {
url = "https://github.com/llvm/llvm-project/pull/99837/commits/14ae0a660a38e1feb151928a14f35ff0f4487351.patch";
Expand Down

0 comments on commit 07c8996

Please sign in to comment.