[lld] r247365 - Create a dynamic symbol table.
Rafael Espindola via llvm-commits
llvm-commits at lists.llvm.org
Thu Sep 10 17:30:13 PDT 2015
Author: rafael
Date: Thu Sep 10 19:30:13 2015
New Revision: 247365
URL: http://llvm.org/viewvc/llvm-project?rev=247365&view=rev
Log:
Create a dynamic symbol table.
For now it includes every symbol in the regular table. Since we don't
create dynamic relocations yet, we don't have a good way of knowing which
symbols are actually needed.
Modified:
lld/trunk/ELF/Writer.cpp
lld/trunk/test/elf2/shared.s
Modified: lld/trunk/ELF/Writer.cpp
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/ELF/Writer.cpp?rev=247365&r1=247364&r2=247365&view=diff
==============================================================================
--- lld/trunk/ELF/Writer.cpp (original)
+++ lld/trunk/ELF/Writer.cpp Thu Sep 10 19:30:13 2015
@@ -114,7 +114,8 @@ public:
typedef typename OutputSectionBase<Is64Bits>::uintX_t uintX_t;
StringTableSection(bool Dynamic)
: OutputSectionBase<Is64Bits>(Dynamic ? ".dynstr" : ".strtab", SHT_STRTAB,
- Dynamic ? (uintX_t)SHF_ALLOC : 0) {
+ Dynamic ? (uintX_t)SHF_ALLOC : 0),
+ Dynamic(Dynamic) {
this->Header.sh_addralign = 1;
}
@@ -128,7 +129,10 @@ public:
this->Header.sh_size = StrTabBuilder.data().size();
}
+ bool isDynamic() const { return Dynamic; }
+
private:
+ const bool Dynamic;
llvm::StringTableBuilder StrTabBuilder;
};
@@ -141,7 +145,10 @@ public:
typedef typename OutputSectionBase<ELFT::Is64Bits>::uintX_t uintX_t;
SymbolTableSection(Writer<ELFT> &W, SymbolTable &Table,
StringTableSection<ELFT::Is64Bits> &StrTabSec)
- : OutputSectionBase<ELFT::Is64Bits>(".symtab", SHT_SYMTAB, 0),
+ : OutputSectionBase<ELFT::Is64Bits>(
+ StrTabSec.isDynamic() ? ".dynsym" : ".symtab",
+ StrTabSec.isDynamic() ? SHT_DYNSYM : SHT_SYMTAB,
+ StrTabSec.isDynamic() ? (uintX_t)SHF_ALLOC : 0),
Table(Table), StrTabSec(StrTabSec), W(W) {
typedef OutputSectionBase<ELFT::Is64Bits> Base;
typename Base::HeaderT &Header = this->Header;
@@ -247,8 +254,8 @@ public:
typedef typename ELFFile<ELFT>::Elf_Phdr Elf_Phdr;
typedef typename ELFFile<ELFT>::Elf_Sym Elf_Sym;
Writer(SymbolTable *T)
- : StrTabSec(false), DynStrSec(true), SymTable(*this, *T, StrTabSec),
- DynamicSec(*T, DynStrSec) {}
+ : StrTabSec(false), DynStrSec(true), SymTabSec(*this, *T, StrTabSec),
+ DynSymSec(*this, *T, DynStrSec), DynamicSec(*T, DynStrSec) {}
void run();
const OutputSection<ELFT> &getBSS() const {
@@ -276,7 +283,8 @@ private:
StringTableSection<ELFT::Is64Bits> StrTabSec;
StringTableSection<ELFT::Is64Bits> DynStrSec;
- SymbolTableSection<ELFT> SymTable;
+ SymbolTableSection<ELFT> SymTabSec;
+ SymbolTableSection<ELFT> DynSymSec;
DynamicSection<ELFT::Is64Bits> DynamicSec;
@@ -541,7 +549,7 @@ template <class ELFT> void Writer<ELFT>:
return Sec;
};
- const SymbolTable &Symtab = SymTable.getSymTable();
+ const SymbolTable &Symtab = SymTabSec.getSymTable();
for (const std::unique_ptr<ObjectFileBase> &FileB : Symtab.getObjectFiles()) {
auto &File = cast<ObjectFile<ELFT>>(*FileB);
for (SectionChunk<ELFT> *C : File.getChunks()) {
@@ -554,6 +562,8 @@ template <class ELFT> void Writer<ELFT>:
}
}
+ const std::vector<std::unique_ptr<SharedFileBase>> &SharedFiles =
+ Symtab.getSharedFiles();
BSSSec = getSection(".bss", SHT_NOBITS, SHF_ALLOC | SHF_WRITE);
// FIXME: Try to avoid the extra walk over all global symbols.
std::vector<DefinedCommon<ELFT> *> CommonSymbols;
@@ -564,7 +574,13 @@ template <class ELFT> void Writer<ELFT>:
CommonSymbols.push_back(C);
if (!includeInSymtab(*Body))
continue;
- SymTable.addSymbol(Name);
+ SymTabSec.addSymbol(Name);
+
+ // FIXME: This adds way too much to the dynamic symbol table. We only
+ // need to add the symbols use by dynamic relocations when producing
+ // an executable (ignoring --export-dynamic).
+ if (!SharedFiles.empty())
+ DynSymSec.addSymbol(Name);
}
// Sort the common symbols by alignment as an heuristic to pack them better.
@@ -580,12 +596,11 @@ template <class ELFT> void Writer<ELFT>:
BSSSec->setSize(Off);
- OutputSections.push_back(&SymTable);
+ OutputSections.push_back(&SymTabSec);
OutputSections.push_back(&StrTabSec);
- const std::vector<std::unique_ptr<SharedFileBase>> &SharedFiles =
- Symtab.getSharedFiles();
if (!SharedFiles.empty()) {
+ OutputSections.push_back(&DynSymSec);
OutputSections.push_back(&DynamicSec);
OutputSections.push_back(&DynStrSec);
}
@@ -644,7 +659,7 @@ template <class ELFT> void Writer<ELFT>:
}
// Add a PHDR for the dynamic table.
- if (!SymTable.getSymTable().getSharedFiles().empty())
+ if (!SymTabSec.getSymTable().getSharedFiles().empty())
++NumPhdrs;
FileOff += OffsetToAlignment(FileOff, ELFT::Is64Bits ? 8 : 4);
@@ -682,7 +697,7 @@ template <class ELFT> void Writer<ELFT>:
// FIXME: Generalize the segment construction similar to how we create
// output sections.
- const SymbolTable &Symtab = SymTable.getSymTable();
+ const SymbolTable &Symtab = SymTabSec.getSymTable();
bool HasDynamicSegment = !Symtab.getSharedFiles().empty();
EHdr->e_type = ET_EXEC;
Modified: lld/trunk/test/elf2/shared.s
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/test/elf2/shared.s?rev=247365&r1=247364&r2=247365&view=diff
==============================================================================
--- lld/trunk/test/elf2/shared.s (original)
+++ lld/trunk/test/elf2/shared.s Thu Sep 10 19:30:13 2015
@@ -1,6 +1,6 @@
// RUN: llvm-mc -filetype=obj -triple=i686-unknown-linux %s -o %t.o
// RUN: lld -flavor gnu2 %t.o %p/Inputs/i686-simple-library.so -o %t
-// RUN: llvm-readobj --program-headers --dynamic-table -t -s %t | FileCheck %s
+// RUN: llvm-readobj --program-headers --dynamic-table -t -s -dyn-symbols %t | FileCheck %s
// REQUIRES: x86
// CHECK: Name: .dynamic
@@ -58,6 +58,36 @@
// CHECK-NEXT: Value: 0x0
// CHECK-NEXT: Size: 0
// CHECK-NEXT: Binding: Global
+// CHECK-NEXT: Type: Function
+// CHECK-NEXT: Other: 0
+// CHECK-NEXT: Section: Undefined
+// CHECK-NEXT: }
+// CHECK-NEXT: ]
+
+// CHECK: DynamicSymbols [
+// CHECK-NEXT: Symbol {
+// CHECK-NEXT: Name: @ (0)
+// 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: Non
+// CHECK-NEXT: Other: 0
+// CHECK-NEXT: Section: .text
+// CHECK-NEXT: }
+// CHECK-NEXT: Symbol {
+// CHECK-NEXT: Name: bar@
+// CHECK-NEXT: Value: 0x0
+// CHECK-NEXT: Size: 0
+// CHECK-NEXT: Binding: Global
// CHECK-NEXT: Type: Function
// CHECK-NEXT: Other: 0
// CHECK-NEXT: Section: Undefined
More information about the llvm-commits
mailing list