Skip to content

Commit 0e319bd

Browse files
committed
[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
1 parent a7137b2 commit 0e319bd

File tree

5 files changed

+80
-5
lines changed

5 files changed

+80
-5
lines changed

lld/MachO/Config.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@ struct Configuration {
6161
bool printWhyLoad = false;
6262
bool searchDylibsFirst = false;
6363
bool saveTemps = false;
64+
bool adhocCodesign = false;
6465
uint32_t headerPad;
6566
uint32_t dylibCompatibilityVersion = 0;
6667
uint32_t dylibCurrentVersion = 0;

lld/MachO/Driver.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -845,6 +845,11 @@ bool macho::link(ArrayRef<const char *> argsArr, bool canExitEarly,
845845

846846
config->saveTemps = args.hasArg(OPT_save_temps);
847847

848+
config->adhocCodesign =
849+
args.hasFlag(OPT_adhoc_codesign, OPT_no_adhoc_codesign,
850+
config->target.Arch == AK_arm64 ||
851+
config->target.Arch == AK_arm64e);
852+
848853
if (args.hasArg(OPT_v)) {
849854
message(getLLDVersion());
850855
message(StringRef("Library search paths:") +

lld/MachO/Options.td

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -566,11 +566,10 @@ def v : Flag<["-"], "v">,
566566
HelpText<"Print the linker version and search paths in addition to linking">,
567567
Group<grp_rare>;
568568
def adhoc_codesign : Flag<["-"], "adhoc_codesign">,
569-
HelpText<"Write an ad-hoc code signature to the output file.">,
570-
Flags<[HelpHidden]>,
569+
HelpText<"Write an ad-hoc code signature to the output file (default for arm64 binaries)">,
571570
Group<grp_rare>;
572571
def no_adhoc_codesign : Flag<["-"], "no_adhoc_codesign">,
573-
HelpText<"Do not write an ad-hoc code signature to the output file.">,
572+
HelpText<"Do not write an ad-hoc code signature to the output file (default for x86_64 binaries)">,
574573
Group<grp_rare>;
575574
def version_details : Flag<["-"], "version_details">,
576575
HelpText<"Print the linker version in JSON form">,

lld/MachO/Writer.cpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -725,8 +725,7 @@ void Writer::createOutputSections() {
725725
unwindInfoSection = make<UnwindInfoSection>(); // TODO(gkm): only when no -r
726726
symtabSection = make<SymtabSection>(*stringTableSection);
727727
indirectSymtabSection = make<IndirectSymtabSection>();
728-
if (config->outputType == MH_EXECUTE &&
729-
(config->target.Arch == AK_arm64 || config->target.Arch == AK_arm64e))
728+
if (config->adhocCodesign)
730729
codeSignatureSection = make<CodeSignatureSection>();
731730

732731
switch (config->outputType) {

lld/test/MachO/adhoc-codesign.s

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
# REQUIRES: x86, aarch64
2+
3+
# RUN: rm -rf %t
4+
# RUN: split-file %s %t
5+
6+
7+
# RUN: llvm-mc -filetype=obj -triple=arm64-apple-macos -o %t/main-arm64.o %t/main.s
8+
# RUN: llvm-mc -filetype=obj -triple=x86_64-apple-macos -o %t/main-x86_64.o %t/main.s
9+
# RUN: llvm-mc -filetype=obj -triple=arm64-apple-macos -o %t/foo-arm64.o %t/foo.s
10+
# RUN: llvm-mc -filetype=obj -triple=x86_64-apple-macos -o %t/foo-x86_64.o %t/foo.s
11+
12+
# Exhaustive test for:
13+
# (x86_64, arm64) x (default, -adhoc_codesign, -no_adhoc-codesign) x (execute, dylib, bundle)
14+
15+
# RUN: %lld -lSystem -arch x86_64 -execute -o %t/out %t/main-x86_64.o
16+
# RUN: llvm-objdump --macho --all-headers %t/out | FileCheck --check-prefix=NO-ADHOC %s
17+
# RUN: %lld -arch x86_64 -dylib -o %t/out %t/foo-x86_64.o
18+
# RUN: llvm-objdump --macho --all-headers %t/out| FileCheck --check-prefix=NO-ADHOC %s
19+
# RUN: %lld -arch x86_64 -bundle -o %t/out %t/foo-x86_64.o
20+
# RUN: llvm-objdump --macho --all-headers %t/out| FileCheck --check-prefix=NO-ADHOC %s
21+
22+
# RUN: %lld -lSystem -arch x86_64 -execute -adhoc_codesign -o %t/out %t/main-x86_64.o
23+
# RUN: llvm-objdump --macho --all-headers %t/out| FileCheck --check-prefix=ADHOC %s
24+
# RUN: %lld -arch x86_64 -dylib -adhoc_codesign -o %t/out %t/foo-x86_64.o
25+
# RUN: llvm-objdump --macho --all-headers %t/out| FileCheck --check-prefix=ADHOC %s
26+
# RUN: %lld -arch x86_64 -bundle -adhoc_codesign -o %t/out %t/foo-x86_64.o
27+
# RUN: llvm-objdump --macho --all-headers %t/out| FileCheck --check-prefix=ADHOC %s
28+
29+
# RUN: %lld -lSystem -arch x86_64 -execute -no_adhoc_codesign -o %t/out %t/main-x86_64.o
30+
# RUN: llvm-objdump --macho --all-headers %t/out| FileCheck --check-prefix=NO-ADHOC %s
31+
# RUN: %lld -arch x86_64 -dylib -no_adhoc_codesign -o %t/out %t/foo-x86_64.o
32+
# RUN: llvm-objdump --macho --all-headers %t/out| FileCheck --check-prefix=NO-ADHOC %s
33+
# RUN: %lld -arch x86_64 -bundle -no_adhoc_codesign -o %t/out %t/foo-x86_64.o
34+
# RUN: llvm-objdump --macho --all-headers %t/out| FileCheck --check-prefix=NO-ADHOC %s
35+
36+
37+
# RUN: %lld -lSystem -arch arm64 -execute -o %t/out %t/main-arm64.o
38+
# RUN: llvm-objdump --macho --all-headers %t/out | FileCheck --check-prefix=ADHOC %s
39+
# RUN: %lld -arch arm64 -dylib -o %t/out %t/foo-arm64.o
40+
# RUN: llvm-objdump --macho --all-headers %t/out| FileCheck --check-prefix=ADHOC %s
41+
# RUN: %lld -arch arm64 -bundle -o %t/out %t/foo-arm64.o
42+
# RUN: llvm-objdump --macho --all-headers %t/out| FileCheck --check-prefix=ADHOC %s
43+
44+
# RUN: %lld -lSystem -arch arm64 -execute -adhoc_codesign -o %t/out %t/main-arm64.o
45+
# RUN: llvm-objdump --macho --all-headers %t/out| FileCheck --check-prefix=ADHOC %s
46+
# RUN: %lld -arch arm64 -dylib -adhoc_codesign -o %t/out %t/foo-arm64.o
47+
# RUN: llvm-objdump --macho --all-headers %t/out| FileCheck --check-prefix=ADHOC %s
48+
# RUN: %lld -arch arm64 -bundle -adhoc_codesign -o %t/out %t/foo-arm64.o
49+
# RUN: llvm-objdump --macho --all-headers %t/out| FileCheck --check-prefix=ADHOC %s
50+
51+
# RUN: %lld -lSystem -arch arm64 -execute -no_adhoc_codesign -o %t/out %t/main-arm64.o
52+
# RUN: llvm-objdump --macho --all-headers %t/out| FileCheck --check-prefix=NO-ADHOC %s
53+
# RUN: %lld -arch arm64 -dylib -no_adhoc_codesign -o %t/out %t/foo-arm64.o
54+
# RUN: llvm-objdump --macho --all-headers %t/out| FileCheck --check-prefix=NO-ADHOC %s
55+
# RUN: %lld -arch arm64 -bundle -no_adhoc_codesign -o %t/out %t/foo-arm64.o
56+
# RUN: llvm-objdump --macho --all-headers %t/out| FileCheck --check-prefix=NO-ADHOC %s
57+
58+
# ADHOC: cmd LC_CODE_SIGNATURE
59+
# ADHOC-NEXT: cmdsize 16
60+
61+
# NO-ADHOC-NOT: cmd LC_CODE_SIGNATURE
62+
63+
#--- foo.s
64+
.globl _foo
65+
_foo:
66+
ret
67+
68+
#--- main.s
69+
.globl _main
70+
_main:
71+
ret

0 commit comments

Comments
 (0)