diff --git a/lld/MachO/Config.h b/lld/MachO/Config.h --- a/lld/MachO/Config.h +++ b/lld/MachO/Config.h @@ -75,6 +75,7 @@ bool isPic = false; bool headerPadMaxInstallNames = false; bool ltoNewPassManager = LLVM_ENABLE_NEW_PASS_MANAGER; + bool markDeadStrippableDylib = false; bool printEachFile = false; bool printWhyLoad = false; bool searchDylibsFirst = false; diff --git a/lld/MachO/Driver.cpp b/lld/MachO/Driver.cpp --- a/lld/MachO/Driver.cpp +++ b/lld/MachO/Driver.cpp @@ -850,6 +850,13 @@ config->demangle = args.hasArg(OPT_demangle); config->implicitDylibs = !args.hasArg(OPT_no_implicit_dylibs); + if (args.hasArg(OPT_mark_dead_strippable_dylib)) { + if (config->outputType != MH_DYLIB) + warn("-mark_dead_strippable_dylib: ignored, only has effect with -dylib"); + else + config->markDeadStrippableDylib = true; + } + if (const Arg *arg = args.getLastArg(OPT_static, OPT_dynamic)) config->staticLink = (arg->getOption().getID() == OPT_static); diff --git a/lld/MachO/Options.td b/lld/MachO/Options.td --- a/lld/MachO/Options.td +++ b/lld/MachO/Options.td @@ -335,8 +335,7 @@ Flags<[HelpHidden]>, Group; def mark_dead_strippable_dylib : Flag<["-"], "mark_dead_strippable_dylib">, - HelpText<"Clients can discard this dylib if it is unreferenced">, - Flags<[HelpHidden]>, + 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">, Group; def compatibility_version : Separate<["-"], "compatibility_version">, MetaVarName<"">, diff --git a/lld/MachO/SyntheticSections.cpp b/lld/MachO/SyntheticSections.cpp --- a/lld/MachO/SyntheticSections.cpp +++ b/lld/MachO/SyntheticSections.cpp @@ -86,6 +86,9 @@ if (config->outputType == MachO::MH_DYLIB && !config->hasReexports) hdr->flags |= MachO::MH_NO_REEXPORTED_DYLIBS; + if (config->markDeadStrippableDylib) + hdr->flags |= MachO::MH_DEAD_STRIPPABLE_DYLIB; + if (config->outputType == MachO::MH_EXECUTE && config->isPic) hdr->flags |= MachO::MH_PIE; diff --git a/lld/test/MachO/header.s b/lld/test/MachO/header.s --- a/lld/test/MachO/header.s +++ b/lld/test/MachO/header.s @@ -7,10 +7,10 @@ # RUN: %lld -arch x86_64 -dylib -o %t/x86-64-dylib %t/x86_64-test.o # RUN: %lld -arch arm64 -dylib -o %t/arm64-dylib %t/arm64-test.o -# RUN: llvm-objdump --macho --all-headers %t/x86-64-executable | FileCheck %s -DCAPS=LIB64 -# RUN: llvm-objdump --macho --all-headers %t/arm64-executable | FileCheck %s -DCAPS=0x00 -# RUN: llvm-objdump --macho --all-headers %t/x86-64-dylib | FileCheck %s -DCAPS=0x00 -# RUN: llvm-objdump --macho --all-headers %t/arm64-dylib | FileCheck %s -DCAPS=0x00 +# RUN: llvm-objdump --macho --private-header %t/x86-64-executable | FileCheck %s -DCAPS=LIB64 +# RUN: llvm-objdump --macho --private-header %t/arm64-executable | FileCheck %s -DCAPS=0x00 +# RUN: llvm-objdump --macho --private-header %t/x86-64-dylib | FileCheck %s -DCAPS=0x00 +# RUN: llvm-objdump --macho --private-header %t/arm64-dylib | FileCheck %s -DCAPS=0x00 # CHECK: magic cputype cpusubtype caps filetype {{.*}} flags # CHECK-NEXT: MH_MAGIC_64 {{.*}} ALL [[CAPS]] {{.*}} NOUNDEFS {{.*}} TWOLEVEL diff --git a/lld/test/MachO/mark-dead-strippable-dylib.s b/lld/test/MachO/mark-dead-strippable-dylib.s new file mode 100644 --- /dev/null +++ b/lld/test/MachO/mark-dead-strippable-dylib.s @@ -0,0 +1,27 @@ +# REQUIRES: x86 + +# RUN: llvm-mc -filetype=obj -triple=x86_64-apple-macos -o %t.o %s + +# RUN: %no_fatal_warnings_lld -o %t.exec %t.o -mark_dead_strippable_dylib 2>&1 \ +# RUN: | FileCheck --check-prefix=WARN %s +# RUN: llvm-objdump --macho --private-header %t.exec \ +# RUN: | FileCheck --check-prefix=NO-DS %s + +# RUN: %no_fatal_warnings_lld -bundle -o %t.bundle %t.o \ +# RUN: -mark_dead_strippable_dylib 2>&1 \ +# RUN: | FileCheck --check-prefix=WARN %s +# RUN: llvm-objdump --macho --private-header %t.bundle \ +# RUN: | FileCheck --check-prefix=NO-DS %s + +# RUN: %lld -dylib -o %t.dylib %t.o -mark_dead_strippable_dylib 2>&1 +# RUN: llvm-objdump --macho --private-header %t.dylib \ +# RUN: | FileCheck --check-prefix=DS %s + +# WARN: warning: -mark_dead_strippable_dylib: ignored, only has effect with -dylib + +# NO-DS-NOT: DEAD_STRIPPABLE_DYLIB +# DS: DEAD_STRIPPABLE_DYLIB + +.globl _main +_main: + ret