[ELF2] Implement support for -discard-locals.

This is not on by default, but it may make sense to change it in future.

llvm-svn: 248133
This commit is contained in:
Davide Italiano 2015-09-20 21:58:12 +00:00
parent 5f38915624
commit 5445b2de50
5 changed files with 62 additions and 1 deletions

View File

@ -21,6 +21,7 @@ struct Configuration {
std::string RPath;
bool Shared = false;
bool DiscardAll = false;
bool DiscardLocals = false;
};
extern Configuration *Config;

View File

@ -85,6 +85,9 @@ void LinkerDriver::link(ArrayRef<const char *> ArgsArr) {
if (Args.hasArg(OPT_discard_all))
Config->DiscardAll = true;
if (Args.hasArg(OPT_discard_locals))
Config->DiscardLocals = true;
// Create a list of input files.
std::vector<MemoryBufferRef> Inputs;

View File

@ -18,3 +18,6 @@ def shared : Flag<["-"], "shared">,
def discard_all : Flag<["-"], "discard-all">,
HelpText<"Delete all local symbols">;
def discard_locals : Flag<["-"], "discard-locals">,
HelpText<"Delete temporary local symbols">;

View File

@ -820,6 +820,8 @@ template <class ELFT> void SymbolTableSection<ELFT>::writeTo(uint8_t *Buf) {
auto *ESym = reinterpret_cast<Elf_Sym *>(Buf);
uint32_t SecIndex = Sym.st_shndx;
ErrorOr<StringRef> SymName = Sym.getName(File.getStringTable());
if (Config->DiscardLocals && SymName->startswith(".L"))
continue;
ESym->st_name = (SymName) ? StrTabSec.getFileOff(*SymName) : 0;
ESym->st_size = Sym.st_size;
ESym->setBindingAndType(Sym.getBinding(), Sym.getType());
@ -1044,7 +1046,7 @@ template <class ELFT> void Writer<ELFT>::createSections() {
Elf_Sym_Range Syms = File.getLocalSymbols();
for (const Elf_Sym &Sym : Syms) {
ErrorOr<StringRef> SymName = Sym.getName(File.getStringTable());
if (SymName)
if (SymName && !(Config->DiscardLocals && SymName->startswith(".L")))
SymTabSec.addSymbol(*SymName, true);
}
}

View File

@ -0,0 +1,52 @@
// RUN: llvm-mc -filetype=obj -triple=x86_64-pc-linux -save-temp-labels %s -o %t
// RUN: lld -flavor gnu2 -discard-locals %t -o %t2
// RUN: llvm-readobj -s -sd -t %t2 | FileCheck %s
// REQUIRES: x86
.global _start
_start:
.text
.Lmyvar:
.Lmyothervar:
// CHECK: Section {
// CHECK: Name: .strtab
// CHECK-NEXT: Type: SHT_STRTAB
// CHECK-NEXT: Flags [
// CHECK-NEXT: ]
// CHECK-NEXT: Address:
// CHECK-NEXT: Offset:
// CHECK-NEXT: Size:
// CHECK-NEXT: Link:
// CHECK-NEXT: Info:
// CHECK-NEXT: AddressAlignment:
// CHECK-NEXT: EntrySize:
// CHECK-NEXT: SectionData (
// CHECK-NEXT: 0000: 002E7465 7874005F 73746172 74002E62
// CHECK-NEXT: 0010: 7373002E 73747274 6162002E 73796D74
// CHECK-NEXT: 0020: 6162002E 64617461 00
// CHECK-NEXT: )
// CHECK-NEXT: }
// CHECK-NEXT: ]
// CHECK: Symbols [
// CHECK-NEXT: Symbol {
// CHECK-NEXT: Name:
// CHECK-NEXT: Value: 0x0
// CHECK-NEXT: Size: 0
// CHECK-NEXT: Binding: Local
// CHECK-NEXT: Type: None
// CHECK-NEXT: Other: 0
// CHECK-NEXT: Section: Undefined
// CHECK-NEXT: }
// CHECK-NEXT: Symbol {
// CHECK-NEXT: Name: _start
// CHECK-NEXT: Value: 0x11000
// CHECK-NEXT: Size: 0
// CHECK-NEXT: Binding: Global
// CHECK-NEXT: Type: None
// CHECK-NEXT: Other: 0
// CHECK-NEXT: Section: .text
// CHECK-NEXT: }
// CHECk-NEXT: ]