[GNU/ELF] Add support for -x/--discard-all.
authorDavide Italiano <davide@freebsd.org>
Sat, 18 Apr 2015 00:32:38 +0000 (00:32 +0000)
committerDavide Italiano <davide@freebsd.org>
Sat, 18 Apr 2015 00:32:38 +0000 (00:32 +0000)
PR: 23232
llvm-svn: 235247

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-all.test [new file with mode: 0644]

index a9e9874..7f1e730 100644 (file)
@@ -310,6 +310,10 @@ public:
   bool getEnableNewDtags() const { return _enableNewDtags; }
   void setEnableNewDtags(bool e) { _enableNewDtags = e; }
 
+  /// \brief Discard local symbols.
+  bool discardLocals() const { return _discardLocals; }
+  void setDiscardLocals(bool d) { _discardLocals = d; }
+
   /// \brief Strip symbols.
   bool stripSymbols() const { return _stripSymbols; }
   void setStripSymbols(bool strip) { _stripSymbols = strip; }
@@ -366,6 +370,7 @@ protected:
   bool _noAllowDynamicLibraries = false;
   bool _mergeRODataToTextSegment = true;
   bool _demangle = true;
+  bool _discardLocals = false;
   bool _stripSymbols = false;
   bool _alignSegments = true;
   bool _enableNewDtags = false;
index 706f3bd..76af3e2 100644 (file)
@@ -475,6 +475,9 @@ bool GnuLdDriver::parse(int argc, const char *argv[],
     }
   }
 
+  if (parsedArgs->hasArg(OPT_discard_loc))
+    ctx->setDiscardLocals(true);
+
   if (parsedArgs->hasArg(OPT_strip_all))
     ctx->setStripSymbols(true);
 
index b8b3fb6..28dfd59 100644 (file)
@@ -255,6 +255,11 @@ def grp_symbolopts : OptionGroup<"opts">,
 def demangle : Flag<["--"], "demangle">,
      HelpText<"Demangle C++ symbols">,
      Group<grp_symbolopts>;
+def discard_loc : Flag<["--"], "discard-all">,
+     HelpText<"Discard all local symbols">,
+     Group<grp_symbolopts>;
+def alias_discard_loc: Flag<["-"], "x">,
+     Alias<discard_loc>;
 def no_demangle : Flag<["--"], "no-demangle">,
      HelpText<"Dont demangle C++ symbols">,
      Group<grp_symbolopts>;
index 0598cda..5f7e1c1 100644 (file)
@@ -484,6 +484,11 @@ void SymbolTable<ELFT>::addSymbol(const Atom *atom, int32_t sectionIndex,
   else
     addUndefinedAtom(symbol, dyn_cast<UndefinedAtom>(atom));
 
+  // If --discard-local is on, don't add to the symbol table
+  // symbols with local binding.
+  if (this->_ctx.discardLocals() && symbol.getBinding() == llvm::ELF::STB_LOCAL)
+    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-all.test b/lld/test/elf/discard-all.test
new file mode 100644 (file)
index 0000000..7146455
--- /dev/null
@@ -0,0 +1,89 @@
+# Test that -x/--discard all works.
+#
+#RUN: yaml2obj -format=elf %s -o=%t.o
+#RUN: lld -flavor gnu -target x86_64 %t.o -e=main -x -o %t1
+#RUN: llvm-objdump -t %t1 | FileCheck %s
+
+#CHECK-NOT: 0000000000400210 l F .text 00000009 local
+
+---
+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:    0x0000000000000010
+    Content:         554889E54883EC10C745FC00000000E81C000000B9000000008945F889C84883C4105DC36666662E0F1F840000000000554889E58B45FC5DC3
+  - 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:            .comment
+    Type:            SHT_PROGBITS
+    Flags:           [ SHF_MERGE, SHF_STRINGS ]
+    AddressAlign:    0x0000000000000001
+    Content:         004672656542534420636C616E672076657273696F6E20332E342E312028746167732F52454C454153455F33342F646F74312D66696E616C203230383033322920323031343035313200
+  - Name:            .note.GNU-stack
+    Type:            SHT_PROGBITS
+    AddressAlign:    0x0000000000000001
+    Content:         ''
+  - Name:            .eh_frame
+    Type:            SHT_PROGBITS
+    Flags:           [ SHF_ALLOC ]
+    AddressAlign:    0x0000000000000008
+    Content:         1400000000000000017A5200017810011B0C070890010000180000001C000000000000002400000000410E108602430D060000001800000038000000000000000900000000410E108602430D06000000
+  - Name:            .rela.eh_frame
+    Type:            SHT_RELA
+    Link:            .symtab
+    AddressAlign:    0x0000000000000008
+    Info:            .eh_frame
+    Relocations:
+      - Offset:          0x0000000000000020
+        Symbol:          .text
+        Type:            R_X86_64_PC32
+      - Offset:          0x000000000000003C
+        Symbol:          .text
+        Type:            R_X86_64_PC32
+        Addend:          48
+Symbols:
+  Local:
+    - Name:            local
+      Type:            STT_FUNC
+      Section:         .text
+      Value:           0x0000000000000030
+      Size:            0x0000000000000009
+    - Name:            .text
+      Type:            STT_SECTION
+      Section:         .text
+    - Name:            .data
+      Type:            STT_SECTION
+      Section:         .data
+    - Name:            .bss
+      Type:            STT_SECTION
+      Section:         .bss
+    - Name:            .comment
+      Type:            STT_SECTION
+      Section:         .comment
+    - Name:            .note.GNU-stack
+      Type:            STT_SECTION
+      Section:         .note.GNU-stack
+    - Name:            .eh_frame
+      Type:            STT_SECTION
+      Section:         .eh_frame
+  Global:
+    - Name:            main
+      Type:            STT_FUNC
+      Section:         .text
+      Size:            0x0000000000000024
+...