[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
This commit is contained in:
Jez Ng 2020-11-30 21:07:16 -08:00
parent 615f63e149
commit 6b3eecd22a
3 changed files with 30 additions and 5 deletions

View File

@ -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.

View File

@ -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>">,

View File

@ -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