Index: lld/MachO/Config.h =================================================================== --- lld/MachO/Config.h +++ lld/MachO/Config.h @@ -61,6 +61,7 @@ bool printWhyLoad = false; bool searchDylibsFirst = false; bool saveTemps = false; + bool adhocCodesign = false; uint32_t headerPad; uint32_t dylibCompatibilityVersion = 0; uint32_t dylibCurrentVersion = 0; Index: lld/MachO/Driver.cpp =================================================================== --- lld/MachO/Driver.cpp +++ lld/MachO/Driver.cpp @@ -845,6 +845,11 @@ config->saveTemps = args.hasArg(OPT_save_temps); + config->adhocCodesign = + args.hasFlag(OPT_adhoc_codesign, OPT_no_adhoc_codesign, + config->target.Arch == AK_arm64 || + config->target.Arch == AK_arm64e); + if (args.hasArg(OPT_v)) { message(getLLDVersion()); message(StringRef("Library search paths:") + Index: lld/MachO/Options.td =================================================================== --- lld/MachO/Options.td +++ lld/MachO/Options.td @@ -566,11 +566,10 @@ HelpText<"Print the linker version and search paths in addition to linking">, Group; def adhoc_codesign : Flag<["-"], "adhoc_codesign">, - HelpText<"Write an ad-hoc code signature to the output file.">, - Flags<[HelpHidden]>, + HelpText<"Write an ad-hoc code signature to the output file (default for arm64 binaries)">, Group; def no_adhoc_codesign : Flag<["-"], "no_adhoc_codesign">, - HelpText<"Do not write an ad-hoc code signature to the output file.">, + HelpText<"Do not write an ad-hoc code signature to the output file (default for x86_64 binaries)">, Group; def version_details : Flag<["-"], "version_details">, HelpText<"Print the linker version in JSON form">, Index: lld/MachO/Writer.cpp =================================================================== --- lld/MachO/Writer.cpp +++ lld/MachO/Writer.cpp @@ -725,8 +725,7 @@ unwindInfoSection = make(); // TODO(gkm): only when no -r symtabSection = make(*stringTableSection); indirectSymtabSection = make(); - if (config->outputType == MH_EXECUTE && - (config->target.Arch == AK_arm64 || config->target.Arch == AK_arm64e)) + if (config->adhocCodesign) codeSignatureSection = make(); switch (config->outputType) { Index: lld/test/MachO/adhoc-codesign.s =================================================================== --- /dev/null +++ lld/test/MachO/adhoc-codesign.s @@ -0,0 +1,71 @@ +# REQUIRES: x86, aarch64 + +# RUN: rm -rf %t +# RUN: split-file %s %t + + +# RUN: llvm-mc -filetype=obj -triple=arm64-apple-macos -o %t/main-arm64.o %t/main.s +# RUN: llvm-mc -filetype=obj -triple=x86_64-apple-macos -o %t/main-x86_64.o %t/main.s +# RUN: llvm-mc -filetype=obj -triple=arm64-apple-macos -o %t/foo-arm64.o %t/foo.s +# RUN: llvm-mc -filetype=obj -triple=x86_64-apple-macos -o %t/foo-x86_64.o %t/foo.s + +# Exhaustive test for: +# (x86_64, arm64) x (default, -adhoc_codesign, -no_adhoc-codesign) x (execute, dylib, bundle) + +# RUN: %lld -lSystem -arch x86_64 -execute -o %t/out %t/main-x86_64.o +# RUN: llvm-objdump --macho --all-headers %t/out | FileCheck --check-prefix=NO-ADHOC %s +# RUN: %lld -arch x86_64 -dylib -o %t/out %t/foo-x86_64.o +# RUN: llvm-objdump --macho --all-headers %t/out| FileCheck --check-prefix=NO-ADHOC %s +# RUN: %lld -arch x86_64 -bundle -o %t/out %t/foo-x86_64.o +# RUN: llvm-objdump --macho --all-headers %t/out| FileCheck --check-prefix=NO-ADHOC %s + +# RUN: %lld -lSystem -arch x86_64 -execute -adhoc_codesign -o %t/out %t/main-x86_64.o +# RUN: llvm-objdump --macho --all-headers %t/out| FileCheck --check-prefix=ADHOC %s +# RUN: %lld -arch x86_64 -dylib -adhoc_codesign -o %t/out %t/foo-x86_64.o +# RUN: llvm-objdump --macho --all-headers %t/out| FileCheck --check-prefix=ADHOC %s +# RUN: %lld -arch x86_64 -bundle -adhoc_codesign -o %t/out %t/foo-x86_64.o +# RUN: llvm-objdump --macho --all-headers %t/out| FileCheck --check-prefix=ADHOC %s + +# RUN: %lld -lSystem -arch x86_64 -execute -no_adhoc_codesign -o %t/out %t/main-x86_64.o +# RUN: llvm-objdump --macho --all-headers %t/out| FileCheck --check-prefix=NO-ADHOC %s +# RUN: %lld -arch x86_64 -dylib -no_adhoc_codesign -o %t/out %t/foo-x86_64.o +# RUN: llvm-objdump --macho --all-headers %t/out| FileCheck --check-prefix=NO-ADHOC %s +# RUN: %lld -arch x86_64 -bundle -no_adhoc_codesign -o %t/out %t/foo-x86_64.o +# RUN: llvm-objdump --macho --all-headers %t/out| FileCheck --check-prefix=NO-ADHOC %s + + +# RUN: %lld -lSystem -arch arm64 -execute -o %t/out %t/main-arm64.o +# RUN: llvm-objdump --macho --all-headers %t/out | FileCheck --check-prefix=ADHOC %s +# RUN: %lld -arch arm64 -dylib -o %t/out %t/foo-arm64.o +# RUN: llvm-objdump --macho --all-headers %t/out| FileCheck --check-prefix=ADHOC %s +# RUN: %lld -arch arm64 -bundle -o %t/out %t/foo-arm64.o +# RUN: llvm-objdump --macho --all-headers %t/out| FileCheck --check-prefix=ADHOC %s + +# RUN: %lld -lSystem -arch arm64 -execute -adhoc_codesign -o %t/out %t/main-arm64.o +# RUN: llvm-objdump --macho --all-headers %t/out| FileCheck --check-prefix=ADHOC %s +# RUN: %lld -arch arm64 -dylib -adhoc_codesign -o %t/out %t/foo-arm64.o +# RUN: llvm-objdump --macho --all-headers %t/out| FileCheck --check-prefix=ADHOC %s +# RUN: %lld -arch arm64 -bundle -adhoc_codesign -o %t/out %t/foo-arm64.o +# RUN: llvm-objdump --macho --all-headers %t/out| FileCheck --check-prefix=ADHOC %s + +# RUN: %lld -lSystem -arch arm64 -execute -no_adhoc_codesign -o %t/out %t/main-arm64.o +# RUN: llvm-objdump --macho --all-headers %t/out| FileCheck --check-prefix=NO-ADHOC %s +# RUN: %lld -arch arm64 -dylib -no_adhoc_codesign -o %t/out %t/foo-arm64.o +# RUN: llvm-objdump --macho --all-headers %t/out| FileCheck --check-prefix=NO-ADHOC %s +# RUN: %lld -arch arm64 -bundle -no_adhoc_codesign -o %t/out %t/foo-arm64.o +# RUN: llvm-objdump --macho --all-headers %t/out| FileCheck --check-prefix=NO-ADHOC %s + +# ADHOC: cmd LC_CODE_SIGNATURE +# ADHOC-NEXT: cmdsize 16 + +# NO-ADHOC-NOT: cmd LC_CODE_SIGNATURE + +#--- foo.s +.globl _foo +_foo: + ret + +#--- main.s +.globl _main +_main: + ret