[lld] c702bf1 - [lld-macho] Reject -no_pie for unsupported archs

Keith Smiley via llvm-commits llvm-commits at lists.llvm.org
Tue Nov 29 11:17:17 PST 2022


Author: Keith Smiley
Date: 2022-11-29T11:17:08-08:00
New Revision: c702bf1400fbc7fc1e54b0efc8cd934291fd81a2

URL: https://github.com/llvm/llvm-project/commit/c702bf1400fbc7fc1e54b0efc8cd934291fd81a2
DIFF: https://github.com/llvm/llvm-project/commit/c702bf1400fbc7fc1e54b0efc8cd934291fd81a2.diff

LOG: [lld-macho] Reject -no_pie for unsupported archs

ld64 rejects `-no_pie` when targeting arm64, this mirrors that behavior.
Newer versions of ld64 also reject it based on minimum OS versions, but
that logic isn't in an open source dump yet so it isn't implemented
here.

Fixes https://github.com/llvm/llvm-project/issues/59115

Differential Revision: https://reviews.llvm.org/D138884

Added: 
    lld/test/MachO/no-pie.s

Modified: 
    lld/MachO/Driver.cpp

Removed: 
    


################################################################################
diff  --git a/lld/MachO/Driver.cpp b/lld/MachO/Driver.cpp
index 50d045b68330..a42c1af6078c 100644
--- a/lld/MachO/Driver.cpp
+++ b/lld/MachO/Driver.cpp
@@ -931,6 +931,11 @@ PlatformType macho::removeSimulator(PlatformType platform) {
   }
 }
 
+static bool supportsNoPie() {
+  return !(config->arch() == AK_arm64 || config->arch() == AK_arm64e ||
+           config->arch() == AK_arm64_32);
+}
+
 static bool dataConstDefault(const InputArgList &args) {
   static const std::array<std::pair<PlatformType, VersionTuple>, 5> minVersion =
       {{{PLATFORM_MACOS, VersionTuple(10, 15)},
@@ -947,7 +952,7 @@ static bool dataConstDefault(const InputArgList &args) {
 
   switch (config->outputType) {
   case MH_EXECUTE:
-    return !args.hasArg(OPT_no_pie);
+    return !(args.hasArg(OPT_no_pie) && supportsNoPie());
   case MH_BUNDLE:
     // FIXME: return false when -final_name ...
     // has prefix "/System/Library/UserEventPlugins/"
@@ -1425,10 +1430,15 @@ bool macho::link(ArrayRef<const char *> argsArr, llvm::raw_ostream &stdoutOS,
     }
   }
 
+  bool pie = args.hasFlag(OPT_pie, OPT_no_pie, true);
+  if (!supportsNoPie() && !pie) {
+    warn("-no_pie ignored for arm64");
+    pie = true;
+  }
+
   config->isPic = config->outputType == MH_DYLIB ||
                   config->outputType == MH_BUNDLE ||
-                  (config->outputType == MH_EXECUTE &&
-                   args.hasFlag(OPT_pie, OPT_no_pie, true));
+                  (config->outputType == MH_EXECUTE && pie);
 
   // Must be set before any InputSections and Symbols are created.
   config->deadStrip = args.hasArg(OPT_dead_strip);

diff  --git a/lld/test/MachO/no-pie.s b/lld/test/MachO/no-pie.s
new file mode 100644
index 000000000000..c51e2b3f90f1
--- /dev/null
+++ b/lld/test/MachO/no-pie.s
@@ -0,0 +1,17 @@
+# REQUIRES: aarch64, x86
+
+# RUN: llvm-mc -filetype=obj -triple=x86_64-apple-darwin %s -o %t.x86_64.o
+# RUN: llvm-mc -filetype=obj -triple=arm64-apple-darwin %s -o %t.arm64.o
+# RUN: llvm-mc -filetype=obj -triple=arm64e-apple-darwin %s -o %t.arm64e.o
+# RUN: llvm-mc -filetype=obj -triple=arm64_32-apple-watchos %s -o %t.arm64_32.o
+
+# RUN: %lld -arch x86_64 -lSystem -no_pie -o %t %t.x86_64.o
+# RUN: not %lld -arch arm64 -lSystem -no_pie -o %t %t.arm64.o 2>&1 | FileCheck %s
+# RUN: not %lld -arch arm64e -lSystem -no_pie -o %t %t.arm64e.o 2>&1 | FileCheck %s
+# RUN: not %lld-watchos -arch arm64_32 -lSystem -no_pie -o %t %t.arm64_32.o 2>&1 | FileCheck %s
+
+# CHECK: error: -no_pie ignored for arm64
+
+.globl _main
+_main:
+  ret


        


More information about the llvm-commits mailing list