[lld] 5ae39d4 - [lld/mac] Fix bug in interaction of -dead_strip and -undefined dynamic_lookup

Nico Weber via llvm-commits llvm-commits at lists.llvm.org
Thu Jul 22 16:30:59 PDT 2021


Author: Nico Weber
Date: 2021-07-22T19:30:46-04:00
New Revision: 5ae39d4f9773214a55b2166bb7bbf3a6d83ecf34

URL: https://github.com/llvm/llvm-project/commit/5ae39d4f9773214a55b2166bb7bbf3a6d83ecf34
DIFF: https://github.com/llvm/llvm-project/commit/5ae39d4f9773214a55b2166bb7bbf3a6d83ecf34.diff

LOG: [lld/mac] Fix bug in interaction of -dead_strip and -undefined dynamic_lookup

We lost the `used` bit on the Undefined when we replaced it with a DylibSymbol
in treatUndefined().

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

Added: 
    

Modified: 
    lld/MachO/Symbols.h
    lld/MachO/Writer.cpp
    lld/test/MachO/dead-strip.s

Removed: 
    


################################################################################
diff  --git a/lld/MachO/Symbols.h b/lld/MachO/Symbols.h
index c30fbb9d68853..f7aac7c5cde7d 100644
--- a/lld/MachO/Symbols.h
+++ b/lld/MachO/Symbols.h
@@ -306,8 +306,10 @@ T *replaceSymbol(Symbol *s, ArgT &&...arg) {
          "Not a Symbol");
 
   bool isUsedInRegularObj = s->isUsedInRegularObj;
+  bool used = s->used;
   T *sym = new (s) T(std::forward<ArgT>(arg)...);
   sym->isUsedInRegularObj |= isUsedInRegularObj;
+  sym->used |= used;
   return sym;
 }
 

diff  --git a/lld/MachO/Writer.cpp b/lld/MachO/Writer.cpp
index 6a9cd13fff62f..26c03f8787221 100644
--- a/lld/MachO/Writer.cpp
+++ b/lld/MachO/Writer.cpp
@@ -611,6 +611,7 @@ static bool needsBinding(const Symbol *sym) {
 
 static void prepareSymbolRelocation(Symbol *sym, const InputSection *isec,
                                     const Reloc &r) {
+  assert(sym->isLive());
   const RelocAttrs &relocAttrs = target->getRelocAttrs(r.type);
 
   if (relocAttrs.hasAttr(RelocAttrBits::BRANCH)) {

diff  --git a/lld/test/MachO/dead-strip.s b/lld/test/MachO/dead-strip.s
index bd7fa07f58f6b..c8d9ae1870e6f 100644
--- a/lld/test/MachO/dead-strip.s
+++ b/lld/test/MachO/dead-strip.s
@@ -173,6 +173,18 @@
 # RUN: %lld -lSystem -dead_strip %t/strip-dylib-ref.o %t/dylib.dylib \
 # RUN:     -o %t/strip-dylib-ref -U _ref_undef_fun
 
+## Check that referenced undefs are kept with -undefined dynamic_lookup.
+# RUN: llvm-mc -filetype=obj -triple=x86_64-apple-macos \
+# RUN:     %t/ref-undef.s -o %t/ref-undef.o
+# RUN: %lld -lSystem -dead_strip %t/ref-undef.o \
+# RUN:     -o %t/ref-undef -undefined dynamic_lookup
+# RUN: llvm-objdump --syms --lazy-bind %t/ref-undef | \
+# RUN:     FileCheck --check-prefix=STRIPDYNLOOKUP %s
+# STRIPDYNLOOKUP: SYMBOL TABLE:
+# STRIPDYNLOOKUP:   *UND* _ref_undef_fun
+# STRIPDYNLOOKUP: Lazy bind table:
+# STRIPDYNLOOKUP:   __DATA   __la_symbol_ptr {{.*}} flat-namespace _ref_undef_fun
+
 ## S_ATTR_LIVE_SUPPORT tests.
 # RUN: llvm-mc -filetype=obj -triple=x86_64-apple-macos \
 # RUN:     %t/live-support.s -o %t/live-support.o
@@ -812,3 +824,9 @@ _more_data:
   .quad L._bar4
 
 .subsections_via_symbols
+
+#--- ref-undef.s
+.globl _main
+_main:
+  callq _ref_undef_fun
+.subsections_via_symbols


        


More information about the llvm-commits mailing list