diff --git a/lld/MachO/Driver.cpp b/lld/MachO/Driver.cpp --- a/lld/MachO/Driver.cpp +++ b/lld/MachO/Driver.cpp @@ -931,6 +931,11 @@ } } +static bool supportsNoPie() { + return !(config->arch() == AK_arm64 || config->arch() == AK_arm64e || + config->arch() == AK_arm64_32); +} + static bool dataConstDefault(const InputArgList &args) { static const std::array, 5> minVersion = {{{PLATFORM_MACOS, VersionTuple(10, 15)}, @@ -947,7 +952,7 @@ switch (config->outputType) { case MH_EXECUTE: - return !args.hasArg(OPT_no_pie); + return !(args.hasArg(OPT_no_pie) && supportsNoPie()); case MH_BUNDLE: // FIXME: return false when -final_name ... // has prefix "/System/Library/UserEventPlugins/" @@ -1425,10 +1430,15 @@ } } + bool pie = args.hasFlag(OPT_pie, OPT_no_pie, true); + if (!supportsNoPie() && !pie) { + warn("-no_pie ignored for arm64"); + pie = true; + } + config->isPic = config->outputType == MH_DYLIB || config->outputType == MH_BUNDLE || - (config->outputType == MH_EXECUTE && - args.hasFlag(OPT_pie, OPT_no_pie, true)); + (config->outputType == MH_EXECUTE && pie); // Must be set before any InputSections and Symbols are created. config->deadStrip = args.hasArg(OPT_dead_strip); diff --git a/lld/test/MachO/no-pie.s b/lld/test/MachO/no-pie.s new file mode 100644 --- /dev/null +++ b/lld/test/MachO/no-pie.s @@ -0,0 +1,17 @@ +# REQUIRES: aarch64, x86 + +# RUN: llvm-mc -filetype=obj -triple=x86_64-apple-darwin %s -o %t.x86_64.o +# RUN: llvm-mc -filetype=obj -triple=arm64-apple-darwin %s -o %t.arm64.o +# RUN: llvm-mc -filetype=obj -triple=arm64e-apple-darwin %s -o %t.arm64e.o +# RUN: llvm-mc -filetype=obj -triple=arm64_32-apple-watchos %s -o %t.arm64_32.o + +# RUN: %lld -arch x86_64 -lSystem -no_pie -o %t %t.x86_64.o +# RUN: not %lld -arch arm64 -lSystem -no_pie -o %t %t.arm64.o 2>&1 | FileCheck %s +# RUN: not %lld -arch arm64e -lSystem -no_pie -o %t %t.arm64e.o 2>&1 | FileCheck %s +# RUN: not %lld-watchos -arch arm64_32 -lSystem -no_pie -o %t %t.arm64_32.o 2>&1 | FileCheck %s + +# CHECK: error: -no_pie ignored for arm64 + +.globl _main +_main: + ret