[lld] 1aafaac - [lld/mac] Implement support for -mark_dead_strippable_dylib

Nico Weber via llvm-commits llvm-commits at lists.llvm.org
Wed Mar 10 06:03:07 PST 2021


Author: Nico Weber
Date: 2021-03-10T08:57:46-05:00
New Revision: 1aafaaca6717d2cf3084e4ba38bde63f619dca90

URL: https://github.com/llvm/llvm-project/commit/1aafaaca6717d2cf3084e4ba38bde63f619dca90
DIFF: https://github.com/llvm/llvm-project/commit/1aafaaca6717d2cf3084e4ba38bde63f619dca90.diff

LOG: [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

Added: 
    lld/test/MachO/mark-dead-strippable-dylib.s

Modified: 
    lld/MachO/Config.h
    lld/MachO/Driver.cpp
    lld/MachO/Options.td
    lld/MachO/SyntheticSections.cpp
    lld/test/MachO/header.s

Removed: 
    


################################################################################
diff  --git a/lld/MachO/Config.h b/lld/MachO/Config.h
index 810b719cd09d1..6c85cd5950700 100644
--- a/lld/MachO/Config.h
+++ b/lld/MachO/Config.h
@@ -75,6 +75,7 @@ struct Configuration {
   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
index b901080723786..b53ac2ebbd7ee 100644
--- a/lld/MachO/Driver.cpp
+++ b/lld/MachO/Driver.cpp
@@ -850,6 +850,13 @@ bool macho::link(ArrayRef<const char *> argsArr, bool canExitEarly,
   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
index c83ed3e95ecbf..74eaabcaded4e 100644
--- a/lld/MachO/Options.td
+++ b/lld/MachO/Options.td
@@ -335,8 +335,7 @@ def dylinker_install_name : Separate<["-"], "dylinker_install_name">,
      Flags<[HelpHidden]>,
      Group<grp_dylib>;
 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<grp_dylib>;
 def compatibility_version : Separate<["-"], "compatibility_version">,
      MetaVarName<"<version>">,

diff  --git a/lld/MachO/SyntheticSections.cpp b/lld/MachO/SyntheticSections.cpp
index 336eb128a2643..423b645059a4f 100644
--- a/lld/MachO/SyntheticSections.cpp
+++ b/lld/MachO/SyntheticSections.cpp
@@ -86,6 +86,9 @@ void MachHeaderSection::writeTo(uint8_t *buf) const {
   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
index 426489352b2e6..17bcb0f3e039a 100644
--- 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
index 0000000000000..ad2f709df481b
--- /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


        


More information about the llvm-commits mailing list