[GNU/ELF] Add support for -X/--discard-locals.
authorDavide Italiano <davide@freebsd.org>
Mon, 20 Apr 2015 22:52:56 +0000 (22:52 +0000)
committerDavide Italiano <davide@freebsd.org>
Mon, 20 Apr 2015 22:52:56 +0000 (22:52 +0000)
There's (almost) never need to keep .L symbols around for production
builds. In fact, the FreeBSD kernel explicitly specify -X beacuse the
size impact (and the subsequent performance impact) might be significant,
because we keep symbols in memory.
I was tempted to make this the default, but I haven't (yet).

PR: 23232
llvm-svn: 235357

lld/include/lld/ReaderWriter/ELFLinkingContext.h
lld/lib/Driver/GnuLdDriver.cpp
lld/lib/Driver/GnuLdOptions.td
lld/lib/ReaderWriter/ELF/SectionChunks.cpp
lld/test/elf/discard-locals.test [new file with mode: 0644]

index 7f1e730..28c6ce1 100644 (file)
@@ -314,6 +314,10 @@ public:
   bool discardLocals() const { return _discardLocals; }
   void setDiscardLocals(bool d) { _discardLocals = d; }
 
+  /// \brief Discard temprorary local symbols.
+  bool discardTempLocals() const { return _discardTempLocals; }
+  void setDiscardTempLocals(bool d) { _discardTempLocals = d; }
+
   /// \brief Strip symbols.
   bool stripSymbols() const { return _stripSymbols; }
   void setStripSymbols(bool strip) { _stripSymbols = strip; }
@@ -370,6 +374,7 @@ protected:
   bool _noAllowDynamicLibraries = false;
   bool _mergeRODataToTextSegment = true;
   bool _demangle = true;
+  bool _discardTempLocals = false;
   bool _discardLocals = false;
   bool _stripSymbols = false;
   bool _alignSegments = true;
index 76af3e2..c7cfe5d 100644 (file)
@@ -478,6 +478,9 @@ bool GnuLdDriver::parse(int argc, const char *argv[],
   if (parsedArgs->hasArg(OPT_discard_loc))
     ctx->setDiscardLocals(true);
 
+  if (parsedArgs->hasArg(OPT_discard_temp_loc))
+    ctx->setDiscardTempLocals(true);
+
   if (parsedArgs->hasArg(OPT_strip_all))
     ctx->setStripSymbols(true);
 
index e6b9cf0..8f4b042 100644 (file)
@@ -260,6 +260,11 @@ def discard_loc : Flag<["--"], "discard-all">,
      Group<grp_symbolopts>;
 def alias_discard_loc: Flag<["-"], "x">,
      Alias<discard_loc>;
+def discard_temp_loc : Flag<["--"], "discard-locals">,
+     HelpText<"Discard temporary local symbols">,
+     Group<grp_symbolopts>;
+def alias_discard_temp_loc : Flag<["-"], "X">,
+     Alias<discard_temp_loc>;
 def no_demangle : Flag<["--"], "no-demangle">,
      HelpText<"Dont demangle C++ symbols">,
      Group<grp_symbolopts>;
index 5f7e1c1..ea3f8c2 100644 (file)
@@ -489,6 +489,11 @@ void SymbolTable<ELFT>::addSymbol(const Atom *atom, int32_t sectionIndex,
   if (this->_ctx.discardLocals() && symbol.getBinding() == llvm::ELF::STB_LOCAL)
     return;
 
+  // Temporary locals are all the symbols which name starts with .L.
+  // This is defined by the ELF standard.
+  if (this->_ctx.discardTempLocals() && atom->name().startswith(".L"))
+    return;
+
   _symbolTable.push_back(SymbolEntry(atom, symbol, atomLayout));
   this->_fsize += sizeof(Elf_Sym);
   if (this->_flags & SHF_ALLOC)
diff --git a/lld/test/elf/discard-locals.test b/lld/test/elf/discard-locals.test
new file mode 100644 (file)
index 0000000..78c1216
--- /dev/null
@@ -0,0 +1,66 @@
+# Test that -X/--discard-locals works.
+#
+#RUN: yaml2obj -format=elf %s -o=%t.o
+#RUN: lld -flavor gnu %t.o -shared -X -o %t1
+#RUN: llvm-objdump -t %t1 | FileCheck %s
+
+#CHECK-NOT: 0000000000400121 l .rodata 00000000 .Lsym8
+
+---
+FileHeader:
+  Class:           ELFCLASS64
+  Data:            ELFDATA2LSB
+  OSABI:           ELFOSABI_FREEBSD
+  Type:            ET_REL
+  Machine:         EM_X86_64
+Sections:
+  - Name:            .text
+    Type:            SHT_PROGBITS
+    Flags:           [ SHF_ALLOC, SHF_EXECINSTR ]
+    AddressAlign:    0x0000000000000004
+    Content:         00000000F20F100D00000000C3
+  - Name:            .data
+    Type:            SHT_PROGBITS
+    Flags:           [ SHF_WRITE, SHF_ALLOC ]
+    AddressAlign:    0x0000000000000004
+    Content:         ''
+  - Name:            .bss
+    Type:            SHT_NOBITS
+    Flags:           [ SHF_WRITE, SHF_ALLOC ]
+    AddressAlign:    0x0000000000000004
+    Content:         ''
+  - Name:            .rodata.str1.1
+    Type:            SHT_PROGBITS
+    Flags:           [ SHF_ALLOC, SHF_MERGE, SHF_STRINGS ]
+    AddressAlign:    0x0000000000000001
+    Content:         ''
+  - Name:            .rela.text
+    Type:            SHT_RELA
+    Link:            .symtab
+    AddressAlign:    0x0000000000000008
+    Info:            .text
+    Relocations:
+      - Offset:          0x0000000000000008
+        Symbol:          .Lsym8
+        Type:            R_X86_64_PC32
+        Addend:          -4
+Symbols:
+  Local:
+    - Name:            .Lsym8
+      Section:         .rodata.str1.1
+    - Name:            test
+      Section:         .text
+      Value:           0x000000000000000C
+    - Name:            .text
+      Type:            STT_SECTION
+      Section:         .text
+    - Name:            .data
+      Type:            STT_SECTION
+      Section:         .data
+    - Name:            .bss
+      Type:            STT_SECTION
+      Section:         .bss
+    - Name:            .rodata.str1.1
+      Type:            STT_SECTION
+      Section:         .rodata.str1.1
+...