[llvm-branch-commits] [lld] 57ffbe0 - glld/mac] Don't add names of unreferenced symbols to string table

Nico Weber via llvm-branch-commits llvm-branch-commits at lists.llvm.org
Tue Dec 22 13:07:17 PST 2020


Author: Nico Weber
Date: 2020-12-22T15:52:33-05:00
New Revision: 57ffbe020af6469b7c2fdb599f2f7e5e5d0322f0

URL: https://github.com/llvm/llvm-project/commit/57ffbe020af6469b7c2fdb599f2f7e5e5d0322f0
DIFF: https://github.com/llvm/llvm-project/commit/57ffbe020af6469b7c2fdb599f2f7e5e5d0322f0.diff

LOG: glld/mac] Don't add names of unreferenced symbols to string table

Before this, a hello world program would contain many many unnecessary
entries in its string table.

No behavior change, just makes the string table in the output smaller
and more like ld64's.

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

Added: 
    

Modified: 
    lld/MachO/SyntheticSections.cpp
    lld/test/MachO/symtab.s

Removed: 
    


################################################################################
diff  --git a/lld/MachO/SyntheticSections.cpp b/lld/MachO/SyntheticSections.cpp
index 8b2ebd36e1ae..2ed1f2eb34fb 100644
--- a/lld/MachO/SyntheticSections.cpp
+++ b/lld/MachO/SyntheticSections.cpp
@@ -694,6 +694,11 @@ void SymtabSection::emitStabs() {
 }
 
 void SymtabSection::finalizeContents() {
+  auto addSymbol = [&](std::vector<SymtabEntry> &symbols, Symbol *sym) {
+    uint32_t strx = stringTableSection.addString(sym->getName());
+    symbols.push_back({sym, strx});
+  };
+
   // Local symbols aren't in the SymbolTable, so we walk the list of object
   // files to gather them.
   for (InputFile *file : inputFiles) {
@@ -702,10 +707,8 @@ void SymtabSection::finalizeContents() {
         // TODO: when we implement -dead_strip, we should filter out symbols
         // that belong to dead sections.
         if (auto *defined = dyn_cast<Defined>(sym)) {
-          if (!defined->isExternal()) {
-            uint32_t strx = stringTableSection.addString(sym->getName());
-            localSymbols.push_back({sym, strx});
-          }
+          if (!defined->isExternal())
+            addSymbol(localSymbols, sym);
         }
       }
     }
@@ -713,19 +716,16 @@ void SymtabSection::finalizeContents() {
 
   // __dyld_private is a local symbol too. It's linker-created and doesn't
   // exist in any object file.
-  if (Defined* dyldPrivate = in.stubHelper->dyldPrivate) {
-    uint32_t strx = stringTableSection.addString(dyldPrivate->getName());
-    localSymbols.push_back({dyldPrivate, strx});
-  }
+  if (Defined* dyldPrivate = in.stubHelper->dyldPrivate)
+    addSymbol(localSymbols, dyldPrivate);
 
   for (Symbol *sym : symtab->getSymbols()) {
-    uint32_t strx = stringTableSection.addString(sym->getName());
     if (auto *defined = dyn_cast<Defined>(sym)) {
       assert(defined->isExternal());
-      externalSymbols.push_back({sym, strx});
+      addSymbol(externalSymbols, sym);
     } else if (auto *dysym = dyn_cast<DylibSymbol>(sym)) {
       if (dysym->isReferenced())
-        undefinedSymbols.push_back({sym, strx});
+        addSymbol(undefinedSymbols, sym);
     }
   }
 

diff  --git a/lld/test/MachO/symtab.s b/lld/test/MachO/symtab.s
index d18986c9d91c..fa784a34e16a 100644
--- a/lld/test/MachO/symtab.s
+++ b/lld/test/MachO/symtab.s
@@ -86,17 +86,19 @@
 # CHECK-NEXT:   iundefsym: 5
 # CHECK-NEXT:   nundefsym: 2
 
-## Verify that the first entry in the StringTable is a space.
+## Verify that the first entry in the StringTable is a space, and that
+## unreferenced symbols aren't emitted.
 # RUN: obj2yaml %t/test | FileCheck %s --check-prefix=YAML
 # YAML:      StringTable:
 # YAML-NEXT: ' '
+# YAML-NOT: _unreferenced
 
 #--- libfoo.s
 .globl _dynamic
 _dynamic:
 
 #--- test.s
-.globl _main, _external, _external_weak
+.globl _main, _external, _external_weak, _unreferenced
 
 .data
 _external:


        


More information about the llvm-branch-commits mailing list