[PATCH] D102100: [lld-macho] Explicitly undefine literal exported symbols

Greg McGary via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Fri May 7 21:37:56 PDT 2021


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

Symbols explicitly exported via command-line options `--exported_symbol SYM` and `--exported_symbols_list FILE` must be defined. Before this fix, lazy symbols defined in archives would be left to languish. We now force them to be included in the linked output.


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D102100

Files:
  lld/MachO/Driver.cpp
  lld/test/MachO/export-options.s


Index: lld/test/MachO/export-options.s
===================================================================
--- lld/test/MachO/export-options.s
+++ lld/test/MachO/export-options.s
@@ -2,6 +2,8 @@
 
 # RUN: split-file %s %t
 # RUN: llvm-mc -filetype=obj -triple=x86_64-apple-macos %t/default.s -o %t/default.o
+# RUN: llvm-mc -filetype=obj -triple=x86_64-apple-macos %t/lazydef.s -o %t/lazydef.o
+# RUN: llvm-ar --format=darwin rcs %t/lazydef.a %t/lazydef.o
 
 ## Check that mixing exported and unexported symbol options yields an error
 # RUN: not %lld -dylib %t/default.o -o /dev/null \
@@ -11,16 +13,28 @@
 # CONFLICT: error: cannot use both -exported_symbol* and -unexported_symbol* options
 # CONFLICT-NEXT: >>> ignoring unexports
 
-## Check that exported literal symbol name is present in symbol table
+## Check that an exported literal name with no symbol definition yields an error
+## but that an exported glob-pattern with no matching symbol definition is OK
 # RUN: not %lld -dylib %t/default.o -o /dev/null \
 # RUN:         -exported_symbol absent_literal \
 # RUN:         -exported_symbol absent_gl?b 2>&1 | \
 # RUN:     FileCheck --check-prefix=UNDEF %s
 
-# UNDEF: error: undefined symbol absent_literal
+# UNDEF: error: undefined symbol: absent_literal
 # UNDEF-NEXT: >>> referenced from option -exported_symbol(s_list)
 # UNDEF-NOT: error: {{.*}} absent_gl{{.}}b
 
+## Check that exported literal symbols are present in output's
+## symbol table, even lazy symbols which would otherwise be omitted
+# RUN: %lld -dylib %t/default.o %t/lazydef.a -o %t/lazydef \
+# RUN:         -exported_symbol _keep_globl \
+# RUN:         -exported_symbol _keep_lazy
+# RUN: llvm-objdump --syms %t/lazydef | \
+# RUN:     FileCheck --check-prefix=EXPORT %s
+
+# EXPORT-DAG: g     F __TEXT,__text _keep_globl
+# EXPORT-DAG: g     F __TEXT,__text _keep_lazy
+
 ## Check that exported symbol is global
 # RUN: not %lld -dylib %t/default.o -o /dev/null \
 # RUN:         -exported_symbol _private_extern 2>&1 | \
@@ -41,6 +55,12 @@
 _private:
   retq
 
+#--- lazydef.s
+
+.globl _keep_lazy
+_keep_lazy:
+  retq
+
 ## Check that the export trie is unaltered
 # RUN: %lld -dylib %t/default.o -o %t/default
 # RUN: llvm-objdump --macho --exports-trie %t/default | \
Index: lld/MachO/Driver.cpp
===================================================================
--- lld/MachO/Driver.cpp
+++ lld/MachO/Driver.cpp
@@ -1059,6 +1059,12 @@
           ">>> ignoring unexports");
     config->unexportedSymbols.clear();
   }
+  // Explicitly-exported literal symbols must be defined, but might
+  // languish in an archive if unreferenced elsewhere. Light a fire
+  // under those lazy symbols!
+  for (const CachedHashStringRef &cachedName : config->exportedSymbols.literals)
+    symtab->addUndefined(cachedName.val(), /*file=*/nullptr,
+                         /*isWeakRef=*/false);
 
   config->saveTemps = args.hasArg(OPT_save_temps);
 
@@ -1156,7 +1162,7 @@
       if (const Symbol *sym = symtab->find(cachedName))
         if (isa<Defined>(sym))
           continue;
-      error("undefined symbol " + cachedName.val() +
+      error("undefined symbol: " + cachedName.val() +
             "\n>>> referenced from option -exported_symbol(s_list)");
     }
 


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D102100.343807.patch
Type: text/x-patch
Size: 3253 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20210508/843e230d/attachment.bin>


More information about the llvm-commits mailing list