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

Nico Weber via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Thu Jul 22 08:39:44 PDT 2021


thakis created this revision.
thakis added a reviewer: lld-macho.
Herald added a reviewer: gkm.
Herald added a project: lld-macho.
thakis requested review of this revision.

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


https://reviews.llvm.org/D106565

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


Index: lld/test/MachO/dead-strip.s
===================================================================
--- lld/test/MachO/dead-strip.s
+++ 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 @@
   .quad L._bar4
 
 .subsections_via_symbols
+
+#--- ref-undef.s
+.globl _main
+_main:
+  callq _ref_undef_fun
+.subsections_via_symbols
Index: lld/MachO/Writer.cpp
===================================================================
--- lld/MachO/Writer.cpp
+++ lld/MachO/Writer.cpp
@@ -589,6 +589,7 @@
 
 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)) {
Index: lld/MachO/Symbols.h
===================================================================
--- lld/MachO/Symbols.h
+++ lld/MachO/Symbols.h
@@ -306,8 +306,10 @@
          "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;
 }
 


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D106565.360837.patch
Type: text/x-patch
Size: 2059 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20210722/ed657764/attachment.bin>


More information about the llvm-commits mailing list