[lld] 6e05c1c - [lld/mac] Always reference dyld_stub_binder when linked with libSystem

Nico Weber via llvm-commits llvm-commits at lists.llvm.org
Sun Jul 11 10:38:02 PDT 2021


Author: Nico Weber
Date: 2021-07-11T13:37:48-04:00
New Revision: 6e05c1cd5f98350520147d932efc853f2638ce25

URL: https://github.com/llvm/llvm-project/commit/6e05c1cd5f98350520147d932efc853f2638ce25
DIFF: https://github.com/llvm/llvm-project/commit/6e05c1cd5f98350520147d932efc853f2638ce25.diff

LOG: [lld/mac] Always reference dyld_stub_binder when linked with libSystem

lld currently only references dyld_stub_binder when it's needed.
ld64 always references it when libSystem is linked.
Match ld64.

The (somewhat lame) motivation is that `nm` on a binary without any
export writes a "no symbols" warning to stderr, and this change makes
it so that every binary in practice has at least a reference to
dyld_stub_binder, which suppresses that.

Every "real" output file will reference dyld_stub_binder, so most
of the time this shouldn't make much of a difference. And if you
really don't want to have this reference for whatever reason, you
can stop passing -lSystem, like you have to for ld64 anyways.

(After linking any dylib, we dump the exported list of symbols to
a txt file with `nm` and only relink downstream deps if that txt
file changes. A nicer fix is to make lld optionally write .tbd files
with the public interface of a linked dylib and use that instead,
but for now the txt files are what we do.)

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

Added: 
    

Modified: 
    lld/MachO/Driver.cpp
    lld/test/MachO/dyld-stub-binder.s
    lld/test/MachO/lto-internalize.ll
    lld/test/MachO/stabs.s

Removed: 
    


################################################################################
diff  --git a/lld/MachO/Driver.cpp b/lld/MachO/Driver.cpp
index 045ca85dcab3..86e9f4de0d47 100644
--- a/lld/MachO/Driver.cpp
+++ b/lld/MachO/Driver.cpp
@@ -1034,6 +1034,23 @@ static void foldIdenticalLiterals() {
     in.wordLiteralSection->finalizeContents();
 }
 
+static void referenceStubBinder() {
+  bool needsStubHelper = config->outputType == MH_DYLIB ||
+                         config->outputType == MH_EXECUTE ||
+                         config->outputType == MH_BUNDLE;
+  if (!needsStubHelper || !symtab->find("dyld_stub_binder"))
+    return;
+
+  // dyld_stub_binder is used by dyld to resolve lazy bindings. This code here
+  // adds a opportunistic reference to dyld_stub_binder if it happens to exist.
+  // dyld_stub_binder is in libSystem.dylib, which is usually linked in. This
+  // isn't needed for correctness, but the presence of that symbol suppresses
+  // "no symbols" diagnostics from `nm`.
+  // StubHelperSection::setup() adds a reference and errors out if
+  // dyld_stub_binder doesn't exist in case it is actually needed.
+  symtab->addUndefined("dyld_stub_binder", /*file=*/nullptr, /*isWeak=*/false);
+}
+
 bool macho::link(ArrayRef<const char *> argsArr, bool canExitEarly,
                  raw_ostream &stdoutOS, raw_ostream &stderrOS) {
   lld::stdoutOS = &stdoutOS;
@@ -1370,6 +1387,8 @@ bool macho::link(ArrayRef<const char *> argsArr, bool canExitEarly,
           treatUndefinedSymbol(*undefined, "-exported_symbol(s_list)");
     }
 
+    referenceStubBinder();
+
     // FIXME: should terminate the link early based on errors encountered so
     // far?
 

diff  --git a/lld/test/MachO/dyld-stub-binder.s b/lld/test/MachO/dyld-stub-binder.s
index 36685580b5e1..475ece2d8be7 100644
--- a/lld/test/MachO/dyld-stub-binder.s
+++ b/lld/test/MachO/dyld-stub-binder.s
@@ -8,6 +8,13 @@
 # RUN: %lld -arch arm64 -dylib %t/foo.o -o %t/libfoo.dylib
 # RUN: llvm-nm -m %t/libfoo.dylib | FileCheck --check-prefix=NOSTUB %s
 
+## Binaries that don't do lazy dynamic calls but are linked against
+## libSystem.dylib get a reference to dyld_stub_binder even if it's
+## not needed.
+# RUN: %lld -arch arm64 -lSystem -dylib %t/foo.o -o %t/libfoo.dylib
+# RUN: llvm-nm -m %t/libfoo.dylib | FileCheck --check-prefix=STUB %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

diff  --git a/lld/test/MachO/lto-internalize.ll b/lld/test/MachO/lto-internalize.ll
index 74c39be3efad..477669375e3f 100644
--- a/lld/test/MachO/lto-internalize.ll
+++ b/lld/test/MachO/lto-internalize.ll
@@ -33,6 +33,7 @@
 ; SYMTAB-NEXT:  g     F __TEXT,__text _main
 ; SYMTAB-NEXT:  g     F __TEXT,__text _used_in_regular_obj
 ; SYMTAB-NEXT:  g     F __TEXT,__text __mh_execute_header
+; SYMTAB-NEXT:          *UND* dyld_stub_binder
 ; SYMTAB-EMPTY:
 
 ;--- test.s

diff  --git a/lld/test/MachO/stabs.s b/lld/test/MachO/stabs.s
index bafd6e2c6a9c..c222536030de 100644
--- a/lld/test/MachO/stabs.s
+++ b/lld/test/MachO/stabs.s
@@ -105,6 +105,7 @@
 # CHECK-DAG:  (     SECT EXT) [[#%.2d,MORE_TEXT_ID + 1]] 0000   [[#FUN]]           '_fun'
 # CHECK-DAG:  (     SECT EXT) [[#%.2d,TEXT_ID + 1]]      0000   {{[0-9a-f]+}}      '_no_debug'
 # CHECK-DAG:  (       {{.*}}) {{[0-9]+}}                 0010   {{[0-9a-f]+}}      '__mh_execute_header'
+# CHECK-DAG:  (       {{.*}}) {{[0-9]+}}                 0100   0000000000000000   'dyld_stub_binder'
 # CHECK-EMPTY:
 
 ## Check that we don't attempt to emit rebase opcodes for the debug sections


        


More information about the llvm-commits mailing list