[lld] r247849 - [ELF2] Initial support for local symbols.
Davide Italiano via llvm-commits
llvm-commits at lists.llvm.org
Wed Sep 16 13:45:57 PDT 2015
Author: davide
Date: Wed Sep 16 15:45:57 2015
New Revision: 247849
URL: http://llvm.org/viewvc/llvm-project?rev=247849&view=rev
Log:
[ELF2] Initial support for local symbols.
Symbol table is now populated correctly, but some fields are missing,
they'll be added in the future. This patch also adds --discard-all
flag, which was the default behavior until now.
Differential Revision: http://reviews.llvm.org/D12874
Added:
lld/trunk/test/elf2/local-dynamic.s
lld/trunk/test/elf2/local.s
Modified:
lld/trunk/ELF/Config.h
lld/trunk/ELF/Driver.cpp
lld/trunk/ELF/InputFiles.cpp
lld/trunk/ELF/InputFiles.h
lld/trunk/ELF/Options.td
lld/trunk/ELF/Writer.cpp
lld/trunk/test/elf2/basic64be.s
lld/trunk/test/elf2/resolution.s
Modified: lld/trunk/ELF/Config.h
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/ELF/Config.h?rev=247849&r1=247848&r2=247849&view=diff
==============================================================================
--- lld/trunk/ELF/Config.h (original)
+++ lld/trunk/ELF/Config.h Wed Sep 16 15:45:57 2015
@@ -20,6 +20,7 @@ struct Configuration {
llvm::StringRef DynamicLinker;
std::string RPath;
bool Shared = false;
+ bool DiscardAll = false;
};
extern Configuration *Config;
Modified: lld/trunk/ELF/Driver.cpp
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/ELF/Driver.cpp?rev=247849&r1=247848&r2=247849&view=diff
==============================================================================
--- lld/trunk/ELF/Driver.cpp (original)
+++ lld/trunk/ELF/Driver.cpp Wed Sep 16 15:45:57 2015
@@ -82,6 +82,9 @@ void LinkerDriver::link(ArrayRef<const c
if (Args.hasArg(OPT_shared))
Config->Shared = true;
+ if (Args.hasArg(OPT_discard_all))
+ Config->DiscardAll = true;
+
// Create a list of input files.
std::vector<MemoryBufferRef> Inputs;
Modified: lld/trunk/ELF/InputFiles.cpp
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/ELF/InputFiles.cpp?rev=247849&r1=247848&r2=247849&view=diff
==============================================================================
--- lld/trunk/ELF/InputFiles.cpp (original)
+++ lld/trunk/ELF/InputFiles.cpp Wed Sep 16 15:45:57 2015
@@ -55,21 +55,36 @@ template <class ELFT> void ELFData<ELFT>
}
template <class ELFT>
-typename ELFData<ELFT>::Elf_Sym_Range ELFData<ELFT>::getNonLocalSymbols() {
+typename ELFData<ELFT>::Elf_Sym_Range
+ELFData<ELFT>::getSymbolsHelper(bool Local) {
if (!Symtab)
return Elf_Sym_Range(nullptr, nullptr);
+ Elf_Sym_Range Syms = ELFObj->symbols(Symtab);
+ uint32_t NumSymbols = std::distance(Syms.begin(), Syms.end());
+ uint32_t FirstNonLocal = Symtab->sh_info;
+ if (FirstNonLocal > NumSymbols)
+ error("Invalid sh_info in symbol table");
+ if (!Local)
+ return llvm::make_range(Syms.begin() + FirstNonLocal, Syms.end());
+ else
+ // Skip over dummy symbol.
+ return llvm::make_range(Syms.begin() + 1, Syms.begin() + FirstNonLocal);
+}
+template <class ELFT>
+typename ELFData<ELFT>::Elf_Sym_Range ELFData<ELFT>::getNonLocalSymbols() {
+ if (!Symtab)
+ return Elf_Sym_Range(nullptr, nullptr);
ErrorOr<StringRef> StringTableOrErr =
ELFObj->getStringTableForSymtab(*Symtab);
error(StringTableOrErr.getError());
StringTable = *StringTableOrErr;
+ return getSymbolsHelper(false);
+}
- Elf_Sym_Range Syms = ELFObj->symbols(Symtab);
- uint32_t NumSymbols = std::distance(Syms.begin(), Syms.end());
- uint32_t FirstNonLocal = Symtab->sh_info;
- if (FirstNonLocal > NumSymbols)
- error("Invalid sh_info in symbol table");
- return llvm::make_range(Syms.begin() + FirstNonLocal, Syms.end());
+template <class ELFT>
+typename ObjectFile<ELFT>::Elf_Sym_Range ObjectFile<ELFT>::getLocalSymbols() {
+ return this->getSymbolsHelper(true);
}
template <class ELFT> void elf2::ObjectFile<ELFT>::parse() {
@@ -217,6 +232,7 @@ template <class ELFT> void SharedFile<EL
namespace lld {
namespace elf2 {
+
template class elf2::ObjectFile<llvm::object::ELF32LE>;
template class elf2::ObjectFile<llvm::object::ELF32BE>;
template class elf2::ObjectFile<llvm::object::ELF64LE>;
Modified: lld/trunk/ELF/InputFiles.h
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/ELF/InputFiles.h?rev=247849&r1=247848&r2=247849&view=diff
==============================================================================
--- lld/trunk/ELF/InputFiles.h (original)
+++ lld/trunk/ELF/InputFiles.h Wed Sep 16 15:45:57 2015
@@ -102,11 +102,14 @@ public:
uint16_t getEMachine() const { return getObj()->getHeader()->e_machine; }
+ StringRef getStringTable() const { return StringTable; }
+
protected:
std::unique_ptr<llvm::object::ELFFile<ELFT>> ELFObj;
const Elf_Shdr *Symtab = nullptr;
StringRef StringTable;
Elf_Sym_Range getNonLocalSymbols();
+ Elf_Sym_Range getSymbolsHelper(bool);
void openELF(MemoryBufferRef MB);
};
@@ -138,6 +141,8 @@ public:
return SymbolBodies[SymbolIndex - FirstNonLocal]->getReplacement();
}
+ Elf_Sym_Range getLocalSymbols();
+
private:
void initializeChunks();
void initializeSymbols();
Modified: lld/trunk/ELF/Options.td
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/ELF/Options.td?rev=247849&r1=247848&r2=247849&view=diff
==============================================================================
--- lld/trunk/ELF/Options.td (original)
+++ lld/trunk/ELF/Options.td Wed Sep 16 15:45:57 2015
@@ -15,3 +15,6 @@ def rpath : Separate<["-"], "rpath">,
def shared : Flag<["-"], "shared">,
HelpText<"Build a shared object">;
+
+def discard_all : Flag<["-"], "discard-all">,
+ HelpText<"Delete all local symbols">;
Modified: lld/trunk/ELF/Writer.cpp
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/ELF/Writer.cpp?rev=247849&r1=247848&r2=247849&view=diff
==============================================================================
--- lld/trunk/ELF/Writer.cpp (original)
+++ lld/trunk/ELF/Writer.cpp Wed Sep 16 15:45:57 2015
@@ -210,6 +210,7 @@ template <class ELFT>
class SymbolTableSection final : public OutputSectionBase<ELFT::Is64Bits> {
public:
typedef typename ELFFile<ELFT>::Elf_Sym Elf_Sym;
+ typedef typename ELFFile<ELFT>::Elf_Sym_Range Elf_Sym_Range;
typedef typename OutputSectionBase<ELFT::Is64Bits>::uintX_t uintX_t;
SymbolTableSection(Writer<ELFT> &W, SymbolTable &Table,
StringTableSection<ELFT::Is64Bits> &StrTabSec)
@@ -221,9 +222,6 @@ public:
typedef OutputSectionBase<ELFT::Is64Bits> Base;
typename Base::HeaderT &Header = this->Header;
- // For now the only local symbol is going to be the one at index 0
- Header.sh_info = 1;
-
Header.sh_entsize = sizeof(Elf_Sym);
Header.sh_addralign = ELFT::Is64Bits ? 8 : 4;
}
@@ -231,15 +229,18 @@ public:
void finalize() override {
this->Header.sh_size = getNumSymbols() * sizeof(Elf_Sym);
this->Header.sh_link = StrTabSec.getSectionIndex();
+ this->Header.sh_info = NumLocals + 1;
}
void writeTo(uint8_t *Buf) override;
const SymbolTable &getSymTable() const { return Table; }
- void addSymbol(StringRef Name) {
+ void addSymbol(StringRef Name, bool isLocal = false) {
StrTabSec.add(Name);
++NumVisible;
+ if (isLocal)
+ ++NumLocals;
}
StringTableSection<ELFT::Is64Bits> &getStrTabSec() { return StrTabSec; }
@@ -249,6 +250,7 @@ private:
SymbolTable &Table;
StringTableSection<ELFT::Is64Bits> &StrTabSec;
unsigned NumVisible = 0;
+ unsigned NumLocals = 0;
const Writer<ELFT> &W;
};
@@ -434,6 +436,7 @@ public:
typedef typename ELFFile<ELFT>::Elf_Ehdr Elf_Ehdr;
typedef typename ELFFile<ELFT>::Elf_Phdr Elf_Phdr;
typedef typename ELFFile<ELFT>::Elf_Sym Elf_Sym;
+ typedef typename ELFFile<ELFT>::Elf_Sym_Range Elf_Sym_Range;
typedef typename ELFFile<ELFT>::Elf_Rela Elf_Rela;
Writer(SymbolTable *T)
: SymTabSec(*this, *T, StrTabSec), DynSymSec(*this, *T, DynStrSec),
@@ -623,9 +626,28 @@ static bool includeInSymtab(const Symbol
}
template <class ELFT> void SymbolTableSection<ELFT>::writeTo(uint8_t *Buf) {
- uint8_t *BufStart = Buf;
-
Buf += sizeof(Elf_Sym);
+
+ // All symbols with STB_LOCAL binding precede the weak and global symbols.
+ // .dynsym only contains global symbols.
+ if (!Config->DiscardAll && !StrTabSec.isDynamic()) {
+ for (const std::unique_ptr<ObjectFileBase> &FileB :
+ Table.getObjectFiles()) {
+ auto &File = cast<ObjectFile<ELFT>>(*FileB);
+ Elf_Sym_Range Syms = File.getLocalSymbols();
+ for (const Elf_Sym &Sym : Syms) {
+ auto *ESym = reinterpret_cast<Elf_Sym *>(Buf);
+ ErrorOr<StringRef> SymName = Sym.getName(File.getStringTable());
+ ESym->st_name = (SymName) ? StrTabSec.getFileOff(*SymName) : 0;
+ ESym->st_value = Sym.st_value;
+ ESym->st_size = Sym.st_size;
+ Buf += sizeof(Elf_Sym);
+ }
+ }
+ }
+
+ uint8_t *GlobalStart = Buf;
+
for (auto &P : Table.getSymbols()) {
StringRef Name = P.first;
Symbol *Sym = P.second;
@@ -686,9 +708,8 @@ template <class ELFT> void SymbolTableSe
// The default hashing of StringRef produces different results on 32 and 64
// bit systems so we sort by st_name. That is arbitrary but deterministic.
// FIXME: Experiment with passing in a custom hashing instead.
- auto *Syms = reinterpret_cast<Elf_Sym *>(BufStart);
- ++Syms;
- array_pod_sort(Syms, Syms + NumVisible, compareSym<ELFT>);
+ auto *Syms = reinterpret_cast<Elf_Sym *>(GlobalStart);
+ array_pod_sort(Syms, Syms + NumVisible - NumLocals, compareSym<ELFT>);
}
template <bool Is64Bits>
@@ -819,6 +840,14 @@ template <class ELFT> void Writer<ELFT>:
for (const std::unique_ptr<ObjectFileBase> &FileB : Symtab.getObjectFiles()) {
auto &File = cast<ObjectFile<ELFT>>(*FileB);
+ if (!Config->DiscardAll) {
+ Elf_Sym_Range Syms = File.getLocalSymbols();
+ for (const Elf_Sym &Sym : Syms) {
+ ErrorOr<StringRef> SymName = Sym.getName(File.getStringTable());
+ if (SymName)
+ SymTabSec.addSymbol(*SymName, true);
+ }
+ }
for (SectionChunk<ELFT> *C : File.getChunks()) {
if (!C)
continue;
Modified: lld/trunk/test/elf2/basic64be.s
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/test/elf2/basic64be.s?rev=247849&r1=247848&r2=247849&view=diff
==============================================================================
--- lld/trunk/test/elf2/basic64be.s (original)
+++ lld/trunk/test/elf2/basic64be.s Wed Sep 16 15:45:57 2015
@@ -1,5 +1,5 @@
# RUN: llvm-mc -filetype=obj -triple=powerpc64-unknown-linux %s -o %t
-# RUN: lld -flavor gnu2 %t -o %t2
+# RUN: lld -flavor gnu2 -discard-all %t -o %t2
# RUN: llvm-readobj -file-headers -sections -program-headers %t2 | FileCheck %s
# REQUIRES: ppc
Added: lld/trunk/test/elf2/local-dynamic.s
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/test/elf2/local-dynamic.s?rev=247849&view=auto
==============================================================================
--- lld/trunk/test/elf2/local-dynamic.s (added)
+++ lld/trunk/test/elf2/local-dynamic.s Wed Sep 16 15:45:57 2015
@@ -0,0 +1,83 @@
+// Check that local symbols are not inserted into dynamic table.
+// RUN: llvm-mc -filetype=obj -triple=x86_64-pc-linux %s -o %t
+// RUN: lld -flavor gnu2 %t -shared -o %t1.so
+// RUN: llvm-readobj -t -dyn-symbols %t1.so | FileCheck %s
+// REQUIRES: x86
+
+// 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: blah
+// 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: foo
+// 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: goo
+// 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: 0x1000
+// CHECK-NEXT: Size: 0
+// CHECK-NEXT: Binding: Global
+// CHECK-NEXT: Type: None
+// CHECK-NEXT: Other: 0
+// CHECK-NEXT: Section: .text
+// CHECK-NEXT: }
+// CHECK-NEXT: ]
+
+// CHECK: DynamicSymbols [
+// 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: 0x1000
+// CHECK-NEXT: Size: 0
+// CHECK-NEXT: Binding: Global
+// CHECK-NEXT: Type: None
+// CHECK-NEXT: Other: 0
+// CHECK-NEXT: Section: .text
+// CHECK-NEXT: }
+// CHECK-NEXT: ]
+
+.global _start
+_start:
+
+blah:
+foo:
+goo:
+
+
Added: lld/trunk/test/elf2/local.s
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/test/elf2/local.s?rev=247849&view=auto
==============================================================================
--- lld/trunk/test/elf2/local.s (added)
+++ lld/trunk/test/elf2/local.s Wed Sep 16 15:45:57 2015
@@ -0,0 +1,72 @@
+// Check that symbol table is correctly populated with local symbols.
+// RUN: llvm-mc -filetype=obj -triple=x86_64-pc-linux %s -o %t
+// RUN: lld -flavor gnu2 %t -o %t1
+// RUN: llvm-readobj -t -s %t1 | FileCheck %s
+// REQUIRES: x86
+
+// Check that Info is equal to the number of local symbols.
+// CHECK: Section {
+// CHECK: Name: .symtab
+// CHECK-NEXT: Type: SHT_SYMTAB
+// CHECK-NEXT: Flags [
+// CHECK-NEXT: ]
+// CHECK-NEXT: Address:
+// CHECK-NEXT: Offset:
+// CHECK-NEXT: Size:
+// CHECK-NEXT: Link:
+// CHECK-NEXT: Info: 4
+
+// 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: blah
+// 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: foo
+// 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: goo
+// 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: ]
+
+.global _start
+_start:
+
+blah:
+foo:
+goo:
Modified: lld/trunk/test/elf2/resolution.s
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/test/elf2/resolution.s?rev=247849&r1=247848&r2=247849&view=diff
==============================================================================
--- lld/trunk/test/elf2/resolution.s (original)
+++ lld/trunk/test/elf2/resolution.s Wed Sep 16 15:45:57 2015
@@ -1,6 +1,6 @@
// RUN: llvm-mc -filetype=obj -triple=x86_64-pc-linux %s -o %t
// RUN: llvm-mc -filetype=obj -triple=x86_64-pc-linux %p/Inputs/resolution.s -o %t2
-// RUN: lld -flavor gnu2 %t %t2 -o %t3
+// RUN: lld -flavor gnu2 -discard-all %t %t2 -o %t3
// RUN: llvm-readobj -t %t3 | FileCheck %s
// REQUIRES: x86
More information about the llvm-commits
mailing list