Thanks to visit codestin.com
Credit goes to github.com

Skip to content

[Clang] Link libgcc_s.1.dylib when building for macOS 10.5 and older #124651

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 11 commits into
base: main
Choose a base branch
from

Conversation

Un1q32
Copy link
Contributor

@Un1q32 Un1q32 commented Jan 27, 2025

There are a lot of symbols that the compiler expects to be in libSystem that were only in libgcc_s before 10.6, like libunwind things and some builtins on i386. We already do this for iOS versions < 5.0 for the same reason.

@llvmbot llvmbot added clang Clang issues not falling into any other category clang:driver 'clang' and 'clang++' user-facing binaries. Not 'clang-cl' labels Jan 27, 2025
@llvmbot
Copy link
Member

llvmbot commented Jan 27, 2025

@llvm/pr-subscribers-backend-arm

@llvm/pr-subscribers-clang

Author: None (Un1q32)

Changes

There are a lot of symbols that the compiler expects to be in libSystem that were only in libgcc_s before 10.6, like libunwind things and some builtins on i386. We already do this for iOS versions < 5.0 for the same reason.


Full diff: https://github.com/llvm/llvm-project/pull/124651.diff

2 Files Affected:

  • (modified) clang/lib/Driver/ToolChains/Darwin.cpp (+5-4)
  • (modified) clang/test/Driver/darwin-ld.c (+6-1)
diff --git a/clang/lib/Driver/ToolChains/Darwin.cpp b/clang/lib/Driver/ToolChains/Darwin.cpp
index 55c55bad739347..67f27a599b4c38 100644
--- a/clang/lib/Driver/ToolChains/Darwin.cpp
+++ b/clang/lib/Driver/ToolChains/Darwin.cpp
@@ -1645,12 +1645,13 @@ void DarwinClang::AddLinkRuntimeLibArgs(const ArgList &Args,
     CmdArgs.push_back("-lSystem");
 
   // Select the dynamic runtime library and the target specific static library.
-  if (isTargetIOSBased()) {
+  if (isTargetIOSBased() || isTargetMacOSBased()) {
     // If we are compiling as iOS / simulator, don't attempt to link libgcc_s.1,
     // it never went into the SDK.
-    // Linking against libgcc_s.1 isn't needed for iOS 5.0+
-    if (isIPhoneOSVersionLT(5, 0) && !isTargetIOSSimulator() &&
-        getTriple().getArch() != llvm::Triple::aarch64)
+    // Linking against libgcc_s.1 isn't needed for iOS 5.0+ or macOS 10.6+
+    if ((isIPhoneOSVersionLT(5, 0) && !isTargetIOSSimulator() &&
+         getTriple().getArch() != llvm::Triple::aarch64) ||
+        isMacosxVersionLT(10, 6))
       CmdArgs.push_back("-lgcc_s.1");
   }
   AddLinkRuntimeLib(Args, CmdArgs, "builtins");
diff --git a/clang/test/Driver/darwin-ld.c b/clang/test/Driver/darwin-ld.c
index f0ca411430cc78..5b10daaf007a2f 100644
--- a/clang/test/Driver/darwin-ld.c
+++ b/clang/test/Driver/darwin-ld.c
@@ -240,6 +240,11 @@
 // RUN: FileCheck -check-prefix=LINK_NO_IOS_ARM64_LIBGCC_S %s < %t.log
 // LINK_NO_IOS_ARM64_LIBGCC_S-NOT: lgcc_s.1
 
+// Check that clang links with libgcc_s.1 for macOS 10.5 and earlier
+// RUN: %clang -target x86_64-apple-macos10.5 -mmacosx-version-min=10.5 -### %t.o 2> %t.log
+// RUN: FileCheck -check-prefix=LINK_OSX_LIBGCC_S %s < %t.log
+// LINK_OSX_LIBGCC_S: lgcc_s.1
+
 // RUN: %clang -target x86_64-apple-darwin12 -rdynamic -### %t.o \
 // RUN:   -fuse-ld= -mlinker-version=100 2> %t.log
 // RUN: FileCheck -check-prefix=LINK_NO_EXPORT_DYNAMIC %s < %t.log
@@ -385,4 +390,4 @@
 // RUN:   %clang -target armv7em-apple-darwin -mno-outline -### %t.o 2> %t.log
 // RUN: FileCheck -check-prefix=ARMV7EM-MNO_OUTLINE %s < %t.log
 // ARMV7EM-MNO_OUTLINE: {{ld(.exe)?"}}
-// ARMV7EM-MNO_OUTLINE-SAME: "-mllvm" "-enable-machine-outliner=never" "-mllvm" "-enable-linkonceodr-outlining"
\ No newline at end of file
+// ARMV7EM-MNO_OUTLINE-SAME: "-mllvm" "-enable-machine-outliner=never" "-mllvm" "-enable-linkonceodr-outlining"

@llvmbot
Copy link
Member

llvmbot commented Jan 27, 2025

@llvm/pr-subscribers-clang-driver

Author: None (Un1q32)

Changes

There are a lot of symbols that the compiler expects to be in libSystem that were only in libgcc_s before 10.6, like libunwind things and some builtins on i386. We already do this for iOS versions < 5.0 for the same reason.


Full diff: https://github.com/llvm/llvm-project/pull/124651.diff

2 Files Affected:

  • (modified) clang/lib/Driver/ToolChains/Darwin.cpp (+5-4)
  • (modified) clang/test/Driver/darwin-ld.c (+6-1)
diff --git a/clang/lib/Driver/ToolChains/Darwin.cpp b/clang/lib/Driver/ToolChains/Darwin.cpp
index 55c55bad739347..67f27a599b4c38 100644
--- a/clang/lib/Driver/ToolChains/Darwin.cpp
+++ b/clang/lib/Driver/ToolChains/Darwin.cpp
@@ -1645,12 +1645,13 @@ void DarwinClang::AddLinkRuntimeLibArgs(const ArgList &Args,
     CmdArgs.push_back("-lSystem");
 
   // Select the dynamic runtime library and the target specific static library.
-  if (isTargetIOSBased()) {
+  if (isTargetIOSBased() || isTargetMacOSBased()) {
     // If we are compiling as iOS / simulator, don't attempt to link libgcc_s.1,
     // it never went into the SDK.
-    // Linking against libgcc_s.1 isn't needed for iOS 5.0+
-    if (isIPhoneOSVersionLT(5, 0) && !isTargetIOSSimulator() &&
-        getTriple().getArch() != llvm::Triple::aarch64)
+    // Linking against libgcc_s.1 isn't needed for iOS 5.0+ or macOS 10.6+
+    if ((isIPhoneOSVersionLT(5, 0) && !isTargetIOSSimulator() &&
+         getTriple().getArch() != llvm::Triple::aarch64) ||
+        isMacosxVersionLT(10, 6))
       CmdArgs.push_back("-lgcc_s.1");
   }
   AddLinkRuntimeLib(Args, CmdArgs, "builtins");
diff --git a/clang/test/Driver/darwin-ld.c b/clang/test/Driver/darwin-ld.c
index f0ca411430cc78..5b10daaf007a2f 100644
--- a/clang/test/Driver/darwin-ld.c
+++ b/clang/test/Driver/darwin-ld.c
@@ -240,6 +240,11 @@
 // RUN: FileCheck -check-prefix=LINK_NO_IOS_ARM64_LIBGCC_S %s < %t.log
 // LINK_NO_IOS_ARM64_LIBGCC_S-NOT: lgcc_s.1
 
+// Check that clang links with libgcc_s.1 for macOS 10.5 and earlier
+// RUN: %clang -target x86_64-apple-macos10.5 -mmacosx-version-min=10.5 -### %t.o 2> %t.log
+// RUN: FileCheck -check-prefix=LINK_OSX_LIBGCC_S %s < %t.log
+// LINK_OSX_LIBGCC_S: lgcc_s.1
+
 // RUN: %clang -target x86_64-apple-darwin12 -rdynamic -### %t.o \
 // RUN:   -fuse-ld= -mlinker-version=100 2> %t.log
 // RUN: FileCheck -check-prefix=LINK_NO_EXPORT_DYNAMIC %s < %t.log
@@ -385,4 +390,4 @@
 // RUN:   %clang -target armv7em-apple-darwin -mno-outline -### %t.o 2> %t.log
 // RUN: FileCheck -check-prefix=ARMV7EM-MNO_OUTLINE %s < %t.log
 // ARMV7EM-MNO_OUTLINE: {{ld(.exe)?"}}
-// ARMV7EM-MNO_OUTLINE-SAME: "-mllvm" "-enable-machine-outliner=never" "-mllvm" "-enable-linkonceodr-outlining"
\ No newline at end of file
+// ARMV7EM-MNO_OUTLINE-SAME: "-mllvm" "-enable-machine-outliner=never" "-mllvm" "-enable-linkonceodr-outlining"

@Un1q32
Copy link
Contributor Author

Un1q32 commented Feb 11, 2025

By the way, this matches the behavior of Apple's old GCC 4.2 build that they shipped with XCode before switching to Clang.

@Un1q32
Copy link
Contributor Author

Un1q32 commented Mar 28, 2025

Accidentally pushed a commit that was supposed to be for another pr to this branch, the PR has nothing to do with ARM

@Un1q32
Copy link
Contributor Author

Un1q32 commented Apr 30, 2025

bump

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
backend:ARM clang:driver 'clang' and 'clang++' user-facing binaries. Not 'clang-cl' clang Clang issues not falling into any other category
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants