[lld] 7e115da - [lld-macho] Make everything PIE by default
Jez Ng via llvm-commits
llvm-commits at lists.llvm.org
Thu Apr 29 12:11:31 PDT 2021
Author: Jez Ng
Date: 2021-04-29T15:11:23-04:00
New Revision: 7e115da5df47dbbbef141987845c1258f0c52874
URL: https://github.com/llvm/llvm-project/commit/7e115da5df47dbbbef141987845c1258f0c52874
DIFF: https://github.com/llvm/llvm-project/commit/7e115da5df47dbbbef141987845c1258f0c52874.diff
LOG: [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
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 207a329eeff80..fe72e0a7c8d49 100644
--- a/lld/MachO/Driver.cpp
+++ b/lld/MachO/Driver.cpp
@@ -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.
diff --git a/lld/MachO/Options.td b/lld/MachO/Options.td
index e502ed4d734b7..344e8b2b4ce56 100644
--- a/lld/MachO/Options.td
+++ b/lld/MachO/Options.td
@@ -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>">,
diff --git a/lld/test/MachO/x86-64-reloc-unsigned.s b/lld/test/MachO/x86-64-reloc-unsigned.s
index 1d2f0fbd3e437..7a157443162ca 100644
--- a/lld/test/MachO/x86-64-reloc-unsigned.s
+++ b/lld/test/MachO/x86-64-reloc-unsigned.s
@@ -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:
More information about the llvm-commits
mailing list