[llvm-branch-commits] [lld] 6b3eecd - [lld-macho] Extend PIE option handling

Jez Ng via llvm-branch-commits llvm-branch-commits at lists.llvm.org
Tue Dec 1 14:40:31 PST 2020


Author: Jez Ng
Date: 2020-12-01T14:35:51-08:00
New Revision: 6b3eecd22ab2afd16be412d19eed0e01f10e6cc8

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

LOG: [lld-macho] Extend PIE option handling

* Enable PIE by default if targeting 10.6 or above on x86-64. (The
  manpage says 10.7, but that actually applies only to i386, and in
  general varies based on the target platform. I didn't update the
  manpage because listing all the different behaviors would make for a
  pretty long description.)
* Add support for `-no_pie`
* Remove `HelpHidden` from `-pie`

Reviewed By: thakis

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

Added: 
    

Modified: 
    lld/MachO/Driver.cpp
    lld/MachO/Options.td
    lld/test/MachO/x86-64-reloc-unsigned.s

Removed: 
    


################################################################################
diff  --git a/lld/MachO/Driver.cpp b/lld/MachO/Driver.cpp
index 58a41c2b3af1..141082985b4e 100644
--- a/lld/MachO/Driver.cpp
+++ b/lld/MachO/Driver.cpp
@@ -555,6 +555,21 @@ static const char *getReproduceOption(opt::InputArgList &args) {
   return getenv("LLD_REPRODUCE");
 }
 
+static bool isPie(opt::InputArgList &args) {
+  if (config->outputType != MH_EXECUTE || args.hasArg(OPT_no_pie))
+    return false;
+
+  // TODO: add logic here as we support more archs. E.g. i386 should default
+  // to PIE from 10.7, arm64 should always be PIE, etc
+  assert(config->arch == AK_x86_64 || config->arch == AK_x86_64h);
+
+  if (config->platform.kind == MachO::PlatformKind::macOS &&
+      config->platform.minimum >= VersionTuple(10, 6))
+    return true;
+
+  return args.hasArg(OPT_pie);
+}
+
 bool macho::link(llvm::ArrayRef<const char *> argsArr, bool canExitEarly,
                  raw_ostream &stdoutOS, raw_ostream &stderrOS) {
   lld::stdoutOS = &stdoutOS;
@@ -690,8 +705,7 @@ bool macho::link(llvm::ArrayRef<const char *> argsArr, bool canExitEarly,
   }
 
   config->isPic = config->outputType == MH_DYLIB ||
-                  config->outputType == MH_BUNDLE ||
-                  (config->outputType == MH_EXECUTE && args.hasArg(OPT_pie));
+                  config->outputType == MH_BUNDLE || isPie(args);
 
   // Now that all dylibs have been loaded, search for those that should be
   // re-exported.

diff  --git a/lld/MachO/Options.td b/lld/MachO/Options.td
index de7cbf7faf3e..399928e8d9ae 100644
--- a/lld/MachO/Options.td
+++ b/lld/MachO/Options.td
@@ -315,11 +315,9 @@ def grp_main : OptionGroup<"main">, HelpText<"MAIN EXECUTABLE">;
 
 def pie : Flag<["-"], "pie">,
      HelpText<"Build a position independent executable (default for macOS 10.7 and later)">,
-     Flags<[HelpHidden]>,
      Group<grp_main>;
 def no_pie : Flag<["-"], "no_pie">,
      HelpText<"Do not build a position independent executable (default for macOS 10.6 and earlier)">,
-     Flags<[HelpHidden]>,
      Group<grp_main>;
 def pagezero_size : Separate<["-"], "pagezero_size">,
      MetaVarName<"<size>">,

diff  --git a/lld/test/MachO/x86-64-reloc-unsigned.s b/lld/test/MachO/x86-64-reloc-unsigned.s
index e3608f318d22..c6e5eb665739 100644
--- a/lld/test/MachO/x86-64-reloc-unsigned.s
+++ b/lld/test/MachO/x86-64-reloc-unsigned.s
@@ -3,8 +3,17 @@
 # RUN: %lld -o %t %t.o
 # RUN: llvm-objdump --macho --rebase --full-contents %t | FileCheck %s
 
-# RUN: %lld -pie -o %t-pie %t.o
+# RUN: %lld -fatal_warnings -pie -o %t-pie %t.o
 # RUN: llvm-objdump --macho --rebase %t-pie | FileCheck %s --check-prefix=PIE
+# RUN: %lld -fatal_warnings -pie -no_pie -o %t-no-pie %t.o
+# RUN: llvm-objdump --macho --rebase %t-no-pie | FileCheck %s --check-prefix=NO-PIE
+# RUN: %lld -fatal_warnings -no_pie -pie -o %t-no-pie %t.o
+# RUN: llvm-objdump --macho --rebase %t-no-pie | FileCheck %s --check-prefix=NO-PIE
+
+# RUN: %lld -platform_version macos 10.6.0 11.0 -o %t-pie %t.o
+# RUN: llvm-objdump --macho --rebase %t-pie | FileCheck %s --check-prefix=PIE
+# RUN: %lld -platform_version macos 10.5.0 11.0 -o %t-no-pie %t.o
+# RUN: llvm-objdump --macho --rebase %t-no-pie | FileCheck %s --check-prefix=NO-PIE
 
 # CHECK:       Contents of section __DATA,foo:
 # CHECK-NEXT:  100001000 08100000 01000000
@@ -21,6 +30,10 @@
 # PIE-DAG:  __DATA   bar                0x[[#ADDR + 12]]  pointer
 # PIE-DAG:  __DATA   baz                0x[[#ADDR + 20]]  pointer
 
+# NO-PIE:      Rebase table:
+# NO-PIE-NEXT: segment  section            address           type
+# NO-PIE-EMPTY:
+
 .globl _main, _foo, _bar
 
 .section __DATA,foo


        


More information about the llvm-branch-commits mailing list