Skip to content

Commit

Permalink
Do not pass the SymbolTable to writeResult.
Browse files Browse the repository at this point in the history
The SymbolTable is always accessible as Symtab<ELFT>::X,
so no need to pass it as an argument.

llvm-svn: 278091
  • Loading branch information
rui314 committed Aug 9, 2016
1 parent 9bd6241 commit 84907c5
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 42 deletions.
2 changes: 1 addition & 1 deletion lld/ELF/Driver.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -588,5 +588,5 @@ template <class ELFT> void LinkerDriver::link(opt::InputArgList &Args) {
MS->splitIntoPieces();
}

writeResult<ELFT>(&Symtab);
writeResult<ELFT>();
}
76 changes: 36 additions & 40 deletions lld/ELF/Writer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,6 @@ template <class ELFT> class Writer {
typedef typename ELFT::Sym Elf_Sym;
typedef typename ELFT::SymRange Elf_Sym_Range;
typedef typename ELFT::Rela Elf_Rela;
Writer(SymbolTable<ELFT> &S) : Symtab(S) {}
void run();

private:
Expand Down Expand Up @@ -79,7 +78,6 @@ template <class ELFT> class Writer {
void addStartStopSymbols(OutputSectionBase<ELFT> *Sec);
OutputSectionBase<ELFT> *findSection(StringRef Name);

SymbolTable<ELFT> &Symtab;
std::vector<Phdr> Phdrs;

uintX_t FileSize;
Expand Down Expand Up @@ -110,7 +108,7 @@ template <class ELFT> static bool needsInterpSection() {
!Config->DynamicLinker.empty();
}

template <class ELFT> void elf::writeResult(SymbolTable<ELFT> *Symtab) {
template <class ELFT> void elf::writeResult() {
typedef typename ELFT::uint uintX_t;
typedef typename ELFT::Ehdr Elf_Ehdr;

Expand Down Expand Up @@ -213,7 +211,7 @@ template <class ELFT> void elf::writeResult(SymbolTable<ELFT> *Symtab) {
Out<ELFT>::ElfHeader = &ElfHeader;
Out<ELFT>::ProgramHeaders = &ProgramHeaders;

Writer<ELFT>(*Symtab).run();
Writer<ELFT>().run();
}

template <class ELFT>
Expand Down Expand Up @@ -273,8 +271,7 @@ template <class ELFT> void Writer<ELFT>::run() {
error(EC, "failed to write to the output file");
}

template <class ELFT>
static void reportUndefined(SymbolTable<ELFT> &Symtab, SymbolBody *Sym) {
template <class ELFT> static void reportUndefined(SymbolBody *Sym) {
if (Config->UnresolvedSymbols == UnresolvedPolicy::Ignore)
return;

Expand Down Expand Up @@ -346,7 +343,7 @@ template <class ELFT> void Writer<ELFT>::copyLocalSymbols() {
if (!Out<ELFT>::SymTab)
return;
for (const std::unique_ptr<elf::ObjectFile<ELFT>> &F :
Symtab.getObjectFiles()) {
Symtab<ELFT>::X->getObjectFiles()) {
const char *StrTab = F->getStringTable().data();
for (SymbolBody *B : F->getLocalSymbols()) {
auto *DR = dyn_cast<DefinedRegular<ELFT>>(B);
Expand Down Expand Up @@ -504,15 +501,15 @@ void PhdrEntry<ELFT>::add(OutputSectionBase<ELFT> *Sec) {
}

template <class ELFT>
static Symbol *addOptionalSynthetic(SymbolTable<ELFT> &Table, StringRef Name,
static Symbol *addOptionalSynthetic(StringRef Name,
OutputSectionBase<ELFT> *Sec,
typename ELFT::uint Val) {
SymbolBody *S = Table.find(Name);
SymbolBody *S = Symtab<ELFT>::X->find(Name);
if (!S)
return nullptr;
if (!S->isUndefined() && !S->isShared())
return S->symbol();
return Table.addSynthetic(Name, Sec, Val);
return Symtab<ELFT>::X->addSynthetic(Name, Sec, Val);
}

// The beginning and the ending of .rel[a].plt section are marked
Expand All @@ -525,10 +522,10 @@ template <class ELFT> void Writer<ELFT>::addRelIpltSymbols() {
if (isOutputDynamic<ELFT>() || !Out<ELFT>::RelaPlt)
return;
StringRef S = Config->Rela ? "__rela_iplt_start" : "__rel_iplt_start";
addOptionalSynthetic(Symtab, S, Out<ELFT>::RelaPlt, 0);
addOptionalSynthetic(S, Out<ELFT>::RelaPlt, 0);

S = Config->Rela ? "__rela_iplt_end" : "__rel_iplt_end";
addOptionalSynthetic(Symtab, S, Out<ELFT>::RelaPlt,
addOptionalSynthetic(S, Out<ELFT>::RelaPlt,
DefinedSynthetic<ELFT>::SectionEnd);
}

Expand All @@ -540,21 +537,20 @@ template <class ELFT> void Writer<ELFT>::addReservedSymbols() {
// so that it points to an absolute address which is relative to GOT.
// See "Global Data Symbols" in Chapter 6 in the following document:
// ftp://www.linux-mips.org/pub/linux/mips/doc/ABI/mipsabi.pdf
Symtab.addSynthetic("_gp", Out<ELFT>::Got, MipsGPOffset);
Symtab<ELFT>::X->addSynthetic("_gp", Out<ELFT>::Got, MipsGPOffset);

// On MIPS O32 ABI, _gp_disp is a magic symbol designates offset between
// start of function and 'gp' pointer into GOT.
Symbol *Sym =
addOptionalSynthetic(Symtab, "_gp_disp", Out<ELFT>::Got, MipsGPOffset);
addOptionalSynthetic("_gp_disp", Out<ELFT>::Got, MipsGPOffset);
if (Sym)
ElfSym<ELFT>::MipsGpDisp = Sym->body();

// The __gnu_local_gp is a magic symbol equal to the current value of 'gp'
// pointer. This symbol is used in the code generated by .cpload pseudo-op
// in case of using -mno-shared option.
// https://sourceware.org/ml/binutils/2004-12/msg00094.html
addOptionalSynthetic(Symtab, "__gnu_local_gp", Out<ELFT>::Got,
MipsGPOffset);
addOptionalSynthetic("__gnu_local_gp", Out<ELFT>::Got, MipsGPOffset);
}

// In the assembly for 32 bit x86 the _GLOBAL_OFFSET_TABLE_ symbol
Expand All @@ -570,30 +566,30 @@ template <class ELFT> void Writer<ELFT>::addReservedSymbols() {
// Given that the symbol is effectively unused, we just create a dummy
// hidden one to avoid the undefined symbol error.
if (!Config->Relocatable)
Symtab.addIgnored("_GLOBAL_OFFSET_TABLE_");
Symtab<ELFT>::X->addIgnored("_GLOBAL_OFFSET_TABLE_");

// __tls_get_addr is defined by the dynamic linker for dynamic ELFs. For
// static linking the linker is required to optimize away any references to
// __tls_get_addr, so it's not defined anywhere. Create a hidden definition
// to avoid the undefined symbol error.
if (!isOutputDynamic<ELFT>())
Symtab.addIgnored("__tls_get_addr");
Symtab<ELFT>::X->addIgnored("__tls_get_addr");

// If linker script do layout we do not need to create any standart symbols.
if (ScriptConfig->HasContents)
return;

auto Define = [this](StringRef S, DefinedRegular<ELFT> *&Sym1,
DefinedRegular<ELFT> *&Sym2) {
Sym1 = Symtab.addIgnored(S, STV_DEFAULT);
Sym1 = Symtab<ELFT>::X->addIgnored(S, STV_DEFAULT);

// The name without the underscore is not a reserved name,
// so it is defined only when there is a reference against it.
assert(S.startswith("_"));
S = S.substr(1);
if (SymbolBody *B = Symtab.find(S))
if (SymbolBody *B = Symtab<ELFT>::X->find(S))
if (B->isUndefined())
Sym2 = Symtab.addAbsolute(S, STV_DEFAULT);
Sym2 = Symtab<ELFT>::X->addAbsolute(S, STV_DEFAULT);
};

Define("_end", ElfSym<ELFT>::End, ElfSym<ELFT>::End2);
Expand All @@ -619,7 +615,7 @@ void Writer<ELFT>::forEachRelSec(
std::function<void(InputSectionBase<ELFT> &, const typename ELFT::Shdr &)>
Fn) {
for (const std::unique_ptr<elf::ObjectFile<ELFT>> &F :
Symtab.getObjectFiles()) {
Symtab<ELFT>::X->getObjectFiles()) {
for (InputSectionBase<ELFT> *C : F->getSections()) {
if (isDiscarded(C))
continue;
Expand All @@ -644,7 +640,7 @@ void Writer<ELFT>::forEachRelSec(

template <class ELFT> void Writer<ELFT>::createSections() {
for (const std::unique_ptr<elf::ObjectFile<ELFT>> &F :
Symtab.getObjectFiles()) {
Symtab<ELFT>::X->getObjectFiles()) {
for (InputSectionBase<ELFT> *C : F->getSections()) {
if (isDiscarded(C)) {
reportDiscarded(C);
Expand Down Expand Up @@ -686,7 +682,7 @@ template <class ELFT> void Writer<ELFT>::finalizeSections() {
// Even the author of gold doesn't remember why gold behaves that way.
// https://sourceware.org/ml/binutils/2002-03/msg00360.html
if (isOutputDynamic<ELFT>())
Symtab.addSynthetic("_DYNAMIC", Out<ELFT>::Dynamic, 0);
Symtab<ELFT>::X->addSynthetic("_DYNAMIC", Out<ELFT>::Dynamic, 0);

// Define __rel[a]_iplt_{start,end} symbols if needed.
addRelIpltSymbols();
Expand All @@ -712,13 +708,13 @@ template <class ELFT> void Writer<ELFT>::finalizeSections() {

// Now that we have defined all possible symbols including linker-
// synthesized ones. Visit all symbols to give the finishing touches.
for (Symbol *S : Symtab.getSymbols()) {
for (Symbol *S : Symtab<ELFT>::X->getSymbols()) {
SymbolBody *Body = S->body();

// We only report undefined symbols in regular objects. This means that we
// will accept an undefined reference in bitcode if it can be optimized out.
if (S->IsUsedInRegularObj && Body->isUndefined() && !S->isWeak())
reportUndefined<ELFT>(Symtab, Body);
reportUndefined<ELFT>(Body);

if (!includeInSymtab<ELFT>(*Body))
continue;
Expand Down Expand Up @@ -861,13 +857,12 @@ template <class ELFT> void Writer<ELFT>::addStartEndSymbols() {
auto Define = [&](StringRef Start, StringRef End,
OutputSectionBase<ELFT> *OS) {
if (OS) {
this->Symtab.addSynthetic(Start, OS, 0);
this->Symtab.addSynthetic(End, OS, DefinedSynthetic<ELFT>::SectionEnd);
Symtab<ELFT>::X->addSynthetic(Start, OS, 0);
Symtab<ELFT>::X->addSynthetic(End, OS,
DefinedSynthetic<ELFT>::SectionEnd);
} else {
addOptionalSynthetic(this->Symtab, Start,
(OutputSectionBase<ELFT> *)nullptr, 0);
addOptionalSynthetic(this->Symtab, End,
(OutputSectionBase<ELFT> *)nullptr, 0);
addOptionalSynthetic(Start, (OutputSectionBase<ELFT> *)nullptr, 0);
addOptionalSynthetic(End, (OutputSectionBase<ELFT> *)nullptr, 0);
}
};

Expand All @@ -892,12 +887,13 @@ void Writer<ELFT>::addStartStopSymbols(OutputSectionBase<ELFT> *Sec) {
StringSaver Saver(Alloc);
StringRef Start = Saver.save("__start_" + S);
StringRef Stop = Saver.save("__stop_" + S);
if (SymbolBody *B = Symtab.find(Start))
if (SymbolBody *B = Symtab<ELFT>::X->find(Start))
if (B->isUndefined())
Symtab.addSynthetic(Start, Sec, 0);
if (SymbolBody *B = Symtab.find(Stop))
Symtab<ELFT>::X->addSynthetic(Start, Sec, 0);
if (SymbolBody *B = Symtab<ELFT>::X->find(Stop))
if (B->isUndefined())
Symtab.addSynthetic(Stop, Sec, DefinedSynthetic<ELFT>::SectionEnd);
Symtab<ELFT>::X->addSynthetic(Stop, Sec,
DefinedSynthetic<ELFT>::SectionEnd);
}

template <class ELFT>
Expand Down Expand Up @@ -1280,10 +1276,10 @@ template <class ELFT> void Writer<ELFT>::writeBuildId() {
Out<ELFT>::BuildId->writeBuildId(Regions);
}

template void elf::writeResult<ELF32LE>(SymbolTable<ELF32LE> *Symtab);
template void elf::writeResult<ELF32BE>(SymbolTable<ELF32BE> *Symtab);
template void elf::writeResult<ELF64LE>(SymbolTable<ELF64LE> *Symtab);
template void elf::writeResult<ELF64BE>(SymbolTable<ELF64BE> *Symtab);
template void elf::writeResult<ELF32LE>();
template void elf::writeResult<ELF32BE>();
template void elf::writeResult<ELF64LE>();
template void elf::writeResult<ELF64BE>();

template struct elf::PhdrEntry<ELF32LE>;
template struct elf::PhdrEntry<ELF32BE>;
Expand Down
2 changes: 1 addition & 1 deletion lld/ELF/Writer.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ template <class ELFT> class OutputSectionBase;
template <class ELFT> class InputSectionBase;
template <class ELFT> class ObjectFile;
template <class ELFT> class SymbolTable;
template <class ELFT> void writeResult(SymbolTable<ELFT> *Symtab);
template <class ELFT> void writeResult();
template <class ELFT> void markLive();
template <class ELFT> bool isOutputDynamic();
template <class ELFT> bool isRelroSection(OutputSectionBase<ELFT> *Sec);
Expand Down

0 comments on commit 84907c5

Please sign in to comment.