[PATCH] D105781: [lld/mac] Use normal Undefined machinery for dyld_stub_binder lookup

Nico Weber via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Sun Jul 11 09:40:55 PDT 2021


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

This is for aesthetic reasons, I'm not aware of anything that needs
this in practice. It does have a few effects:

- `-undefined dynamic_lookup` now has an effect for dyld_stub_binder. This matches ld64.

- `-U dyld_stub_binder` now works like you'd expect (it doesn't work in ld64).

- The error message for a missing dyld_stub_binder symbol now looks like other undefined reference symbols, it changes from

  symbol dyld_stub_binder not found (normally in libSystem.dylib). Needed to perform lazy binding.

  to

  error: undefined symbol: dyld_stub_binder >>> referenced by lazy binding (normally in libSystem.dylib)

Also add test coverage for that error message.

But in practice, this should have no interesting effects since everything links
in dyld_stub_binder via libSystem anyways.


https://reviews.llvm.org/D105781

Files:
  lld/MachO/SyntheticSections.cpp
  lld/test/MachO/dyld-stub-binder.s


Index: lld/test/MachO/dyld-stub-binder.s
===================================================================
--- /dev/null
+++ lld/test/MachO/dyld-stub-binder.s
@@ -0,0 +1,59 @@
+# REQUIRES: aarch64
+# RUN: rm -rf %t; split-file %s %t
+# RUN: llvm-mc -filetype=obj -triple=arm64-apple-darwin %t/foo.s -o %t/foo.o
+# RUN: llvm-mc -filetype=obj -triple=arm64-apple-darwin %t/bar.s -o %t/bar.o
+# RUN: llvm-mc -filetype=obj -triple=arm64-apple-darwin %t/test.s -o %t/test.o
+
+## Dylibs that don't do lazy dynamic calls don't need dyld_stub_binder.
+# RUN: %lld -arch arm64 -dylib %t/foo.o -o %t/libfoo.dylib
+# RUN: llvm-nm -m %t/libfoo.dylib | FileCheck --check-prefix=NOSTUB %s
+
+## Dylibs that do lazy dynamic calls do need dyld_stub_binder.
+# RUN: not %lld -arch arm64 -dylib %t/bar.o %t/libfoo.dylib \
+# RUN:     -o %t/libbar.dylib 2>&1 | FileCheck --check-prefix=MISSINGSTUB %s
+# RUN: %lld -arch arm64 -lSystem -dylib %t/bar.o  %t/libfoo.dylib \
+# RUN:     -o %t/libbar.dylib
+# RUN: llvm-nm -m %t/libbar.dylib | FileCheck --check-prefix=STUB %s
+
+## As do executables.
+# RUN: not %lld -arch arm64 %t/libfoo.dylib %t/libbar.dylib %t/test.o \
+# RUN:     -o %t/test 2>&1 | FileCheck --check-prefix=MISSINGSTUB %s
+# RUN: %lld -arch arm64 -lSystem %t/libfoo.dylib %t/libbar.dylib %t/test.o \
+# RUN:     -o %t/test
+# RUN: llvm-nm -m %t/test | FileCheck --check-prefix=STUB %s
+
+## Test dynamic lookup of dyld_stub_binder.
+# RUN: %lld -arch arm64 %t/libfoo.dylib %t/libbar.dylib %t/test.o \
+# RUN:     -o %t/test -undefined dynamic_lookup
+# RUN: llvm-nm -m %t/test | FileCheck --check-prefix=DYNSTUB %s
+# RUN: %lld -arch arm64 %t/libfoo.dylib %t/libbar.dylib %t/test.o \
+# RUN:     -o %t/test -U dyld_stub_binder
+# RUN: llvm-nm -m %t/test | FileCheck --check-prefix=DYNSTUB %s
+
+# MISSINGSTUB:      error: undefined symbol: dyld_stub_binder
+# MISSINGSTUB-NEXT: >>> referenced by lazy binding (normally in libSystem.dylib)
+
+# NOSTUB-NOT: dyld_stub_binder
+# STUB: (undefined) external dyld_stub_binder (from libSystem)
+# DYNSTUB: (undefined) external dyld_stub_binder (dynamically looked up)
+
+#--- foo.s
+.globl _foo
+_foo:
+
+#--- bar.s
+.text
+.globl _bar
+_bar:
+  bl _foo
+  ret
+
+#--- test.s
+.text
+.globl _main
+
+.p2align 2
+_main:
+  bl _foo
+  bl _bar
+  ret
Index: lld/MachO/SyntheticSections.cpp
===================================================================
--- lld/MachO/SyntheticSections.cpp
+++ lld/MachO/SyntheticSections.cpp
@@ -497,13 +497,17 @@
 }
 
 void StubHelperSection::setup() {
-  stubBinder = dyn_cast_or_null<DylibSymbol>(symtab->find("dyld_stub_binder"));
-  if (stubBinder == nullptr) {
-    error("symbol dyld_stub_binder not found (normally in libSystem.dylib). "
-          "Needed to perform lazy binding.");
+  Symbol *binder = symtab->addUndefined("dyld_stub_binder", /*file=*/nullptr,
+                                        /*isWeakRef=*/false);
+  if (auto *undefined = dyn_cast<Undefined>(binder))
+    treatUndefinedSymbol(*undefined,
+                         "lazy binding (normally in libSystem.dylib)");
+
+  // treatUndefinedSymbol() can replace binder with a DylibSymbol; re-check.
+  stubBinder = dyn_cast_or_null<DylibSymbol>(binder);
+  if (stubBinder == nullptr)
     return;
-  }
-  stubBinder->reference(RefState::Strong);
+
   in.got->addEntry(stubBinder);
 
   inputSections.push_back(in.imageLoaderCache);


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D105781.357806.patch
Type: text/x-patch
Size: 3392 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20210711/28b60207/attachment-0001.bin>


More information about the llvm-commits mailing list