diff --git a/lld/MachO/Driver.cpp b/lld/MachO/Driver.cpp --- a/lld/MachO/Driver.cpp +++ b/lld/MachO/Driver.cpp @@ -555,6 +555,21 @@ 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 argsArr, bool canExitEarly, raw_ostream &stdoutOS, raw_ostream &stderrOS) { lld::stdoutOS = &stdoutOS; @@ -690,8 +705,7 @@ } 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 --- a/lld/MachO/Options.td +++ b/lld/MachO/Options.td @@ -315,11 +315,9 @@ def pie : Flag<["-"], "pie">, HelpText<"Build a position independent executable (default for macOS 10.7 and later)">, - Flags<[HelpHidden]>, Group; def no_pie : Flag<["-"], "no_pie">, HelpText<"Do not build a position independent executable (default for macOS 10.6 and earlier)">, - Flags<[HelpHidden]>, Group; def pagezero_size : Separate<["-"], "pagezero_size">, MetaVarName<"">, diff --git a/lld/test/MachO/x86-64-reloc-unsigned.s b/lld/test/MachO/x86-64-reloc-unsigned.s --- 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