[lld] 0e319bd - [lld/mac] ad-hoc sign dylibs and bundles on arm64 by default, support -(no_)adhoc_codesign flags

Nico Weber via llvm-commits llvm-commits at lists.llvm.org
Fri Mar 5 06:12:42 PST 2021


Author: Nico Weber
Date: 2021-03-05T09:12:34-05:00
New Revision: 0e319bd0be26c3a11ab1876da4bbb9259d3f6107

URL: https://github.com/llvm/llvm-project/commit/0e319bd0be26c3a11ab1876da4bbb9259d3f6107
DIFF: https://github.com/llvm/llvm-project/commit/0e319bd0be26c3a11ab1876da4bbb9259d3f6107.diff

LOG: [lld/mac] ad-hoc sign dylibs and bundles on arm64 by default, support -(no_)adhoc_codesign flags

Previously, lld/mac only ad-hoc codesigned executables on arm64.

Matches ld64 behavior. Part of PR49443. Fixes 14 of 17 failures when running
check-llvm with lld as host linker on an M1 MBP.

Differential Revision: https://reviews.llvm.org/D97994

Added: 
    lld/test/MachO/adhoc-codesign.s

Modified: 
    lld/MachO/Config.h
    lld/MachO/Driver.cpp
    lld/MachO/Options.td
    lld/MachO/Writer.cpp

Removed: 
    


################################################################################
diff  --git a/lld/MachO/Config.h b/lld/MachO/Config.h
index f7a22e1e3301..31195457eeef 100644
--- a/lld/MachO/Config.h
+++ b/lld/MachO/Config.h
@@ -61,6 +61,7 @@ struct Configuration {
   bool printWhyLoad = false;
   bool searchDylibsFirst = false;
   bool saveTemps = false;
+  bool adhocCodesign = false;
   uint32_t headerPad;
   uint32_t dylibCompatibilityVersion = 0;
   uint32_t dylibCurrentVersion = 0;

diff  --git a/lld/MachO/Driver.cpp b/lld/MachO/Driver.cpp
index 48685370d2b5..280a32b4aa6d 100644
--- a/lld/MachO/Driver.cpp
+++ b/lld/MachO/Driver.cpp
@@ -845,6 +845,11 @@ bool macho::link(ArrayRef<const char *> argsArr, bool canExitEarly,
 
   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:") +

diff  --git a/lld/MachO/Options.td b/lld/MachO/Options.td
index 1349b64f2882..66628fdd62e4 100644
--- a/lld/MachO/Options.td
+++ b/lld/MachO/Options.td
@@ -566,11 +566,10 @@ def v : Flag<["-"], "v">,
      HelpText<"Print the linker version and search paths in addition to linking">,
      Group<grp_rare>;
 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<grp_rare>;
 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<grp_rare>;
 def version_details : Flag<["-"], "version_details">,
      HelpText<"Print the linker version in JSON form">,

diff  --git a/lld/MachO/Writer.cpp b/lld/MachO/Writer.cpp
index a4f83390bbb0..2d6a41bc5d55 100644
--- a/lld/MachO/Writer.cpp
+++ b/lld/MachO/Writer.cpp
@@ -725,8 +725,7 @@ void Writer::createOutputSections() {
   unwindInfoSection = make<UnwindInfoSection>(); // TODO(gkm): only when no -r
   symtabSection = make<SymtabSection>(*stringTableSection);
   indirectSymtabSection = make<IndirectSymtabSection>();
-  if (config->outputType == MH_EXECUTE &&
-      (config->target.Arch == AK_arm64 || config->target.Arch == AK_arm64e))
+  if (config->adhocCodesign)
     codeSignatureSection = make<CodeSignatureSection>();
 
   switch (config->outputType) {

diff  --git a/lld/test/MachO/adhoc-codesign.s b/lld/test/MachO/adhoc-codesign.s
new file mode 100644
index 000000000000..1d240ff7e124
--- /dev/null
+++ b/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


        


More information about the llvm-commits mailing list