Skip to content

Commit 1aafaac

Browse files
committed
[lld/mac] Implement support for -mark_dead_strippable_dylib
lld doesn't read MH_DEAD_STRIPPABLE_DYLIB to strip dead dylibs yet, but now it can produce dylibs with it set. While here, also switch an existing test that looks only at the main Mach-O header from --all-headers to --private-header. Differential Revision: https://reviews.llvm.org/D98262
1 parent b599f46 commit 1aafaac

File tree

6 files changed

+43
-6
lines changed

6 files changed

+43
-6
lines changed

lld/MachO/Config.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,7 @@ struct Configuration {
7575
bool isPic = false;
7676
bool headerPadMaxInstallNames = false;
7777
bool ltoNewPassManager = LLVM_ENABLE_NEW_PASS_MANAGER;
78+
bool markDeadStrippableDylib = false;
7879
bool printEachFile = false;
7980
bool printWhyLoad = false;
8081
bool searchDylibsFirst = false;

lld/MachO/Driver.cpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -850,6 +850,13 @@ bool macho::link(ArrayRef<const char *> argsArr, bool canExitEarly,
850850
config->demangle = args.hasArg(OPT_demangle);
851851
config->implicitDylibs = !args.hasArg(OPT_no_implicit_dylibs);
852852

853+
if (args.hasArg(OPT_mark_dead_strippable_dylib)) {
854+
if (config->outputType != MH_DYLIB)
855+
warn("-mark_dead_strippable_dylib: ignored, only has effect with -dylib");
856+
else
857+
config->markDeadStrippableDylib = true;
858+
}
859+
853860
if (const Arg *arg = args.getLastArg(OPT_static, OPT_dynamic))
854861
config->staticLink = (arg->getOption().getID() == OPT_static);
855862

lld/MachO/Options.td

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -335,8 +335,7 @@ def dylinker_install_name : Separate<["-"], "dylinker_install_name">,
335335
Flags<[HelpHidden]>,
336336
Group<grp_dylib>;
337337
def mark_dead_strippable_dylib : Flag<["-"], "mark_dead_strippable_dylib">,
338-
HelpText<"Clients can discard this dylib if it is unreferenced">,
339-
Flags<[HelpHidden]>,
338+
HelpText<"Mark output dylib as dead-strippable: When a client links against it but does not use any of its symbols, the dylib will not be added to the client's list of needed dylibs">,
340339
Group<grp_dylib>;
341340
def compatibility_version : Separate<["-"], "compatibility_version">,
342341
MetaVarName<"<version>">,

lld/MachO/SyntheticSections.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,9 @@ void MachHeaderSection::writeTo(uint8_t *buf) const {
8686
if (config->outputType == MachO::MH_DYLIB && !config->hasReexports)
8787
hdr->flags |= MachO::MH_NO_REEXPORTED_DYLIBS;
8888

89+
if (config->markDeadStrippableDylib)
90+
hdr->flags |= MachO::MH_DEAD_STRIPPABLE_DYLIB;
91+
8992
if (config->outputType == MachO::MH_EXECUTE && config->isPic)
9093
hdr->flags |= MachO::MH_PIE;
9194

lld/test/MachO/header.s

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,10 @@
77
# RUN: %lld -arch x86_64 -dylib -o %t/x86-64-dylib %t/x86_64-test.o
88
# RUN: %lld -arch arm64 -dylib -o %t/arm64-dylib %t/arm64-test.o
99

10-
# RUN: llvm-objdump --macho --all-headers %t/x86-64-executable | FileCheck %s -DCAPS=LIB64
11-
# RUN: llvm-objdump --macho --all-headers %t/arm64-executable | FileCheck %s -DCAPS=0x00
12-
# RUN: llvm-objdump --macho --all-headers %t/x86-64-dylib | FileCheck %s -DCAPS=0x00
13-
# RUN: llvm-objdump --macho --all-headers %t/arm64-dylib | FileCheck %s -DCAPS=0x00
10+
# RUN: llvm-objdump --macho --private-header %t/x86-64-executable | FileCheck %s -DCAPS=LIB64
11+
# RUN: llvm-objdump --macho --private-header %t/arm64-executable | FileCheck %s -DCAPS=0x00
12+
# RUN: llvm-objdump --macho --private-header %t/x86-64-dylib | FileCheck %s -DCAPS=0x00
13+
# RUN: llvm-objdump --macho --private-header %t/arm64-dylib | FileCheck %s -DCAPS=0x00
1414

1515
# CHECK: magic cputype cpusubtype caps filetype {{.*}} flags
1616
# CHECK-NEXT: MH_MAGIC_64 {{.*}} ALL [[CAPS]] {{.*}} NOUNDEFS {{.*}} TWOLEVEL
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
# REQUIRES: x86
2+
3+
# RUN: llvm-mc -filetype=obj -triple=x86_64-apple-macos -o %t.o %s
4+
5+
# RUN: %no_fatal_warnings_lld -o %t.exec %t.o -mark_dead_strippable_dylib 2>&1 \
6+
# RUN: | FileCheck --check-prefix=WARN %s
7+
# RUN: llvm-objdump --macho --private-header %t.exec \
8+
# RUN: | FileCheck --check-prefix=NO-DS %s
9+
10+
# RUN: %no_fatal_warnings_lld -bundle -o %t.bundle %t.o \
11+
# RUN: -mark_dead_strippable_dylib 2>&1 \
12+
# RUN: | FileCheck --check-prefix=WARN %s
13+
# RUN: llvm-objdump --macho --private-header %t.bundle \
14+
# RUN: | FileCheck --check-prefix=NO-DS %s
15+
16+
# RUN: %lld -dylib -o %t.dylib %t.o -mark_dead_strippable_dylib 2>&1
17+
# RUN: llvm-objdump --macho --private-header %t.dylib \
18+
# RUN: | FileCheck --check-prefix=DS %s
19+
20+
# WARN: warning: -mark_dead_strippable_dylib: ignored, only has effect with -dylib
21+
22+
# NO-DS-NOT: DEAD_STRIPPABLE_DYLIB
23+
# DS: DEAD_STRIPPABLE_DYLIB
24+
25+
.globl _main
26+
_main:
27+
ret

0 commit comments

Comments
 (0)