[lld-macho] Make everything PIE by default

Modern versions of macOS (>= 10.7) and in general all modern Mach-O
target archs want PIEs by default. ld64 defaults to PIE for iOS >= 4.3,
as well as for all versions of watchOS and simulators. Basically all the
platforms LLD is likely to target want PIE. So instead of cluttering LLD's
code with legacy version checks, I think it's simpler to just default to
PIE for everything.

Note that `-no_pie` still works, so users can still opt out of it.

Reviewed By: #lld-macho, thakis, MaskRay

Differential Revision: https://reviews.llvm.org/D101513
This commit is contained in:
Jez Ng 2021-04-29 15:09:01 -04:00
parent a6d92a9711
commit 7e115da5df
3 changed files with 6 additions and 36 deletions

View File

@ -697,29 +697,6 @@ static const char *getReproduceOption(InputArgList &args) {
return getenv("LLD_REPRODUCE");
}
static bool isPie(InputArgList &args) {
if (config->outputType != MH_EXECUTE || args.hasArg(OPT_no_pie))
return false;
if (config->arch() == AK_arm64 || config->arch() == AK_arm64e ||
config->arch() == AK_arm64_32)
return true;
// TODO: add logic here as we support more archs. E.g. i386 should default
// to PIE from 10.7
assert(config->arch() == AK_x86_64 || config->arch() == AK_x86_64h ||
config->arch() == AK_arm64_32);
PlatformKind kind = config->platformInfo.target.Platform;
if (kind == PlatformKind::macOS &&
config->platformInfo.minimum >= VersionTuple(10, 6))
return true;
if (kind == PlatformKind::iOSSimulator || kind == PlatformKind::driverKit)
return true;
return args.hasArg(OPT_pie);
}
static void parseClangOption(StringRef opt, const Twine &msg) {
std::string err;
raw_string_ostream os(err);
@ -1095,7 +1072,9 @@ bool macho::link(ArrayRef<const char *> argsArr, bool canExitEarly,
createFiles(args);
config->isPic = config->outputType == MH_DYLIB ||
config->outputType == MH_BUNDLE || isPie(args);
config->outputType == MH_BUNDLE ||
(config->outputType == MH_EXECUTE &&
args.hasFlag(OPT_pie, OPT_no_pie, true));
// Now that all dylibs have been loaded, search for those that should be
// re-exported.

View File

@ -370,10 +370,10 @@ def dylib_current_version : Separate<["-"], "dylib_current_version">,
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)">,
HelpText<"Build a position independent executable (default)">,
Group<grp_main>;
def no_pie : Flag<["-"], "no_pie">,
HelpText<"Do not build a position independent executable (default for macOS 10.6 and earlier)">,
HelpText<"Do not build a position independent executable">,
Group<grp_main>;
def pagezero_size : Separate<["-"], "pagezero_size">,
MetaVarName<"<size>">,

View File

@ -7,16 +7,7 @@
# RUN: llvm-objdump --macho --rebase %t-pie | FileCheck %s --check-prefix=PIE
# RUN: %lld -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 -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
# RUN: %lld -platform_version ios-simulator 11.0.0 14.2 -o %t-pie %t.o
# RUN: llvm-objdump --macho --rebase %t-pie | FileCheck %s --check-prefix=PIE
# RUN: %lld -platform_version driverkit 19.0 20.0 -o %t-pie %t.o
# RUN: %lld -no_pie -pie -o %t-pie %t.o
# RUN: llvm-objdump --macho --rebase %t-pie | FileCheck %s --check-prefix=PIE
# CHECK: Contents of section __DATA,foo: