[PATCH] D115092: [lld-macho] Unreferenced weak dylib symbols shouldn't fetch archive symbols

Jez Ng via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Fri Dec 3 18:28:41 PST 2021


int3 created this revision.
int3 added a reviewer: lld-macho.
Herald added a project: lld-macho.
int3 requested review of this revision.
Herald added a project: LLVM.
Herald added a subscriber: llvm-commits.

We were fetching archive symbols too eagerly, bloating binary size as well as
just screwing up binaries that expected to look up certain symbols only at
runtime.


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D115092

Files:
  lld/MachO/SymbolTable.cpp
  lld/test/MachO/weak-definition-direct-fetch.s


Index: lld/test/MachO/weak-definition-direct-fetch.s
===================================================================
--- lld/test/MachO/weak-definition-direct-fetch.s
+++ lld/test/MachO/weak-definition-direct-fetch.s
@@ -55,7 +55,7 @@
 # RUN: %lld -lSystem -o %t/nonweak-weak-archives %t/foo.a %t/weakfoo.a %t/test.o
 # RUN: llvm-objdump --macho --lazy-bind --syms %t/nonweak-weak-archives | FileCheck %s --check-prefix=PREFER-NONWEAK-OBJECT
 
-## The remaining lines test symbol pairs of different types.
+## The next 5 lines test symbol pairs of different types.
 
 ## (Weak) archive symbols take precedence over weak dylib symbols.
 # RUN: %lld -lSystem -o %t/weak-dylib-weak-ar -L%t -lweakfoo %t/weakfoo.a %t/test.o
@@ -87,6 +87,16 @@
 # RUN: %lld -lSystem -o %t/nonweak-ar-weak-obj %t/foo.a %t/weakfoo.o %t/test.o
 # RUN: llvm-objdump --macho --lazy-bind --syms %t/nonweak-ar-weak-obj | FileCheck %s --check-prefix=PREFER-WEAK-OBJECT
 
+## Regression test: A weak dylib symbol that isn't referenced by an undefined
+## symbol should not cause an archive symbol to get loaded.
+# RUN: %lld -dylib -lSystem -o %t/weak-ar-weak-unref-dylib -L%t %t/weakfoo.a -lweakfoo
+# RUN: llvm-objdump --macho --lazy-bind --syms %t/weak-ar-weak-unref-dylib | FileCheck %s --check-prefix=NO-SYM
+# RUN: %lld -dylib -lSystem -o %t/weak-unref-dylib-weak-ar -L%t -lweakfoo %t/weakfoo.a
+# RUN: llvm-objdump --macho --lazy-bind --syms %t/weak-unref-dylib-weak-ar | FileCheck %s --check-prefix=NO-SYM
+
+# NO-SYM: SYMBOL TABLE:
+# NO-SYM-NOT: _foo
+
 #--- foo.s
 .globl _foo
 .section __TEXT,nonweak
Index: lld/MachO/SymbolTable.cpp
===================================================================
--- lld/MachO/SymbolTable.cpp
+++ lld/MachO/SymbolTable.cpp
@@ -184,10 +184,18 @@
   bool wasInserted;
   std::tie(s, wasInserted) = insert(name, file);
 
-  if (wasInserted)
+  if (wasInserted) {
     replaceSymbol<LazySymbol>(s, file, sym);
-  else if (isa<Undefined>(s) || (isa<DylibSymbol>(s) && s->isWeakDef()))
+  } else if (isa<Undefined>(s)) {
     file->fetch(sym);
+  } else if (auto *dysym = dyn_cast<DylibSymbol>(s)) {
+    if (dysym->isWeakDef()) {
+      if (dysym->getRefState() != RefState::Unreferenced)
+        file->fetch(sym);
+      else
+        replaceSymbol<LazySymbol>(s, file, sym);
+    }
+  }
   return s;
 }
 


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D115092.391799.patch
Type: text/x-patch
Size: 2327 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20211204/1d10db84/attachment.bin>


More information about the llvm-commits mailing list