[lld] r298241 - [ELF] - Combine LinkerScriptBase and LinkerScript<ELFT>
George Rimar via llvm-commits
llvm-commits at lists.llvm.org
Mon Mar 20 03:09:58 PDT 2017
Author: grimar
Date: Mon Mar 20 05:09:58 2017
New Revision: 298241
URL: http://llvm.org/viewvc/llvm-project?rev=298241&view=rev
Log:
[ELF] - Combine LinkerScriptBase and LinkerScript<ELFT>
Patch removes templated linkerscript class.
Unfortunately that required 2 additional static methods
findSymbol() and addRegularSymbol() because code
depends on Symtab<ELFT>::X
Differential revision: https://reviews.llvm.org/D30982
Modified:
lld/trunk/ELF/Driver.cpp
lld/trunk/ELF/LinkerScript.cpp
lld/trunk/ELF/LinkerScript.h
lld/trunk/ELF/MarkLive.cpp
lld/trunk/ELF/OutputSections.cpp
lld/trunk/ELF/SyntheticSections.cpp
lld/trunk/ELF/Writer.cpp
Modified: lld/trunk/ELF/Driver.cpp
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/ELF/Driver.cpp?rev=298241&r1=298240&r2=298241&view=diff
==============================================================================
--- lld/trunk/ELF/Driver.cpp (original)
+++ lld/trunk/ELF/Driver.cpp Mon Mar 20 05:09:58 2017
@@ -31,6 +31,7 @@
#include "InputSection.h"
#include "LinkerScript.h"
#include "Memory.h"
+#include "OutputSections.h"
#include "Strings.h"
#include "SymbolTable.h"
#include "Target.h"
@@ -852,7 +853,7 @@ template <class ELFT> void LinkerDriver:
SymbolTable<ELFT> Symtab;
elf::Symtab<ELFT>::X = &Symtab;
Target = createTarget();
- ScriptBase = Script<ELFT>::X = make<LinkerScript<ELFT>>();
+ Script = make<LinkerScriptBase>();
Config->MaxPageSize = getMaxPageSize(Args);
Config->ImageBase = getImageBase(Args);
Modified: lld/trunk/ELF/LinkerScript.cpp
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/ELF/LinkerScript.cpp?rev=298241&r1=298240&r2=298241&view=diff
==============================================================================
--- lld/trunk/ELF/LinkerScript.cpp (original)
+++ lld/trunk/ELF/LinkerScript.cpp Mon Mar 20 05:09:58 2017
@@ -127,7 +127,7 @@ static ExprValue bitOr(ExprValue A, Expr
static ExprValue bitNot(ExprValue A) { return ~A.getValue(); }
static ExprValue minus(ExprValue A) { return -A.getValue(); }
-LinkerScriptBase *elf::ScriptBase;
+LinkerScriptBase *elf::Script;
ScriptConfiguration *elf::ScriptConfig;
template <class ELFT> static SymbolBody *addRegular(SymbolAssignment *Cmd) {
@@ -219,18 +219,47 @@ void LinkerScriptBase::assignSymbol(Symb
}
}
-template <class ELFT>
-void LinkerScript<ELFT>::addSymbol(SymbolAssignment *Cmd) {
+static SymbolBody *findSymbol(StringRef S) {
+ switch (Config->EKind) {
+ case ELF32LEKind:
+ return Symtab<ELF32LE>::X->find(S);
+ case ELF32BEKind:
+ return Symtab<ELF32BE>::X->find(S);
+ case ELF64LEKind:
+ return Symtab<ELF64LE>::X->find(S);
+ case ELF64BEKind:
+ return Symtab<ELF64BE>::X->find(S);
+ default:
+ llvm_unreachable("unknown Config->EKind");
+ }
+}
+
+static SymbolBody *addRegularSymbol(SymbolAssignment *Cmd) {
+ switch (Config->EKind) {
+ case ELF32LEKind:
+ return addRegular<ELF32LE>(Cmd);
+ case ELF32BEKind:
+ return addRegular<ELF32BE>(Cmd);
+ case ELF64LEKind:
+ return addRegular<ELF64LE>(Cmd);
+ case ELF64BEKind:
+ return addRegular<ELF64BE>(Cmd);
+ default:
+ llvm_unreachable("unknown Config->EKind");
+ }
+}
+
+void LinkerScriptBase::addSymbol(SymbolAssignment *Cmd) {
if (Cmd->Name == ".")
return;
// If a symbol was in PROVIDE(), we need to define it only when
// it is a referenced undefined symbol.
- SymbolBody *B = Symtab<ELFT>::X->find(Cmd->Name);
+ SymbolBody *B = findSymbol(Cmd->Name);
if (Cmd->Provide && (!B || B->isDefined()))
return;
- Cmd->Sym = addRegular<ELFT>(Cmd);
+ Cmd->Sym = addRegularSymbol(Cmd);
}
bool SymbolAssignment::classof(const BaseCommand *C) {
@@ -253,9 +282,6 @@ bool BytesDataCommand::classof(const Bas
return C->Kind == BytesDataKind;
}
-template <class ELFT> LinkerScript<ELFT>::LinkerScript() = default;
-template <class ELFT> LinkerScript<ELFT>::~LinkerScript() = default;
-
static StringRef basename(InputSectionBase *S) {
if (S->File)
return sys::path::filename(S->File->getName());
@@ -391,8 +417,7 @@ LinkerScriptBase::createInputSectionList
return Ret;
}
-template <class ELFT>
-void LinkerScript<ELFT>::processCommands(OutputSectionFactory &Factory) {
+void LinkerScriptBase::processCommands(OutputSectionFactory &Factory) {
// A symbol can be assigned before any section is mentioned in the linker
// script. In an DSO, the symbol values are addresses, so the only important
// section values are:
@@ -936,30 +961,31 @@ uint32_t LinkerScriptBase::getFiller(Str
return 0;
}
-template <class ELFT>
static void writeInt(uint8_t *Buf, uint64_t Data, uint64_t Size) {
- const endianness E = ELFT::TargetEndianness;
+ const endianness E =
+ (Config->EKind == ELF32LEKind || Config->EKind == ELF64LEKind)
+ ? llvm::support::endianness::little
+ : llvm::support::endianness::big;
switch (Size) {
case 1:
*Buf = (uint8_t)Data;
break;
case 2:
- write16<E>(Buf, Data);
+ write16(Buf, Data, E);
break;
case 4:
- write32<E>(Buf, Data);
+ write32(Buf, Data, E);
break;
case 8:
- write64<E>(Buf, Data);
+ write64(Buf, Data, E);
break;
default:
llvm_unreachable("unsupported Size argument");
}
}
-template <class ELFT>
-void LinkerScript<ELFT>::writeDataBytes(StringRef Name, uint8_t *Buf) {
+void LinkerScriptBase::writeDataBytes(StringRef Name, uint8_t *Buf) {
int I = getSectionIndex(Name);
if (I == INT_MAX)
return;
@@ -967,8 +993,7 @@ void LinkerScript<ELFT>::writeDataBytes(
auto *Cmd = dyn_cast<OutputSectionCommand>(Opt.Commands[I].get());
for (const std::unique_ptr<BaseCommand> &Base : Cmd->Commands)
if (auto *Data = dyn_cast<BytesDataCommand>(Base.get()))
- writeInt<ELFT>(Buf + Data->Offset, Data->Expression().getValue(),
- Data->Size);
+ writeInt(Buf + Data->Offset, Data->Expression().getValue(), Data->Size);
}
bool LinkerScriptBase::hasLMA(StringRef Name) {
@@ -991,22 +1016,21 @@ int LinkerScriptBase::getSectionIndex(St
return INT_MAX;
}
-template <class ELFT>
-ExprValue LinkerScript<ELFT>::getSymbolValue(const Twine &Loc, StringRef S) {
+ExprValue LinkerScriptBase::getSymbolValue(const Twine &Loc, StringRef S) {
if (S == ".")
return {CurOutSec, Dot - CurOutSec->Addr};
- if (SymbolBody *B = Symtab<ELFT>::X->find(S)) {
+ if (SymbolBody *B = findSymbol(S)) {
if (auto *D = dyn_cast<DefinedRegular>(B))
return {D->Section, D->Value};
auto *C = cast<DefinedCommon>(B);
- return {In<ELFT>::Common, C->Offset};
+ return {InX::Common, C->Offset};
}
error(Loc + ": symbol not found: " + S);
return 0;
}
-template <class ELFT> bool LinkerScript<ELFT>::isDefined(StringRef S) {
- return Symtab<ELFT>::X->find(S) != nullptr;
+bool LinkerScriptBase::isDefined(StringRef S) {
+ return findSymbol(S) != nullptr;
}
// Returns indices of ELF headers containing specific section, identified
@@ -1490,7 +1514,7 @@ Expr ScriptParser::readAssert() {
return [=] {
if (!E().getValue())
error(Msg);
- return ScriptBase->getDot();
+ return Script->getDot();
};
}
@@ -1620,7 +1644,7 @@ SymbolAssignment *ScriptParser::readAssi
Expr E = readExpr();
if (Op == "+=") {
std::string Loc = getCurrentLocation();
- E = [=] { return add(ScriptBase->getSymbolValue(Loc, Name), E()); };
+ E = [=] { return add(Script->getSymbolValue(Loc, Name), E()); };
}
return new SymbolAssignment(Name, E, getCurrentLocation());
}
@@ -1791,13 +1815,12 @@ Expr ScriptParser::readPrimary() {
if (Tok == "ADDR") {
StringRef Name = readParenLiteral();
return [=]() -> ExprValue {
- return {ScriptBase->getOutputSection(Location, Name), 0};
+ return {Script->getOutputSection(Location, Name), 0};
};
}
if (Tok == "LOADADDR") {
StringRef Name = readParenLiteral();
- return
- [=] { return ScriptBase->getOutputSection(Location, Name)->getLMA(); };
+ return [=] { return Script->getOutputSection(Location, Name)->getLMA(); };
}
if (Tok == "ASSERT")
return readAssert();
@@ -1810,7 +1833,7 @@ Expr ScriptParser::readPrimary() {
return [=] { return alignTo(E().getValue(), E2().getValue()); };
}
expect(")");
- return [=] { return alignTo(ScriptBase->getDot(), E().getValue()); };
+ return [=] { return alignTo(Script->getDot(), E().getValue()); };
}
if (Tok == "CONSTANT") {
StringRef Name = readParenLiteral();
@@ -1818,7 +1841,7 @@ Expr ScriptParser::readPrimary() {
}
if (Tok == "DEFINED") {
StringRef Name = readParenLiteral();
- return [=] { return ScriptBase->isDefined(Name) ? 1 : 0; };
+ return [=] { return Script->isDefined(Name) ? 1 : 0; };
}
if (Tok == "SEGMENT_START") {
expect("(");
@@ -1834,13 +1857,13 @@ Expr ScriptParser::readPrimary() {
expect(",");
readExpr();
expect(")");
- return [=] { return alignTo(ScriptBase->getDot(), E().getValue()); };
+ return [=] { return alignTo(Script->getDot(), E().getValue()); };
}
if (Tok == "DATA_SEGMENT_END") {
expect("(");
expect(".");
expect(")");
- return [] { return ScriptBase->getDot(); };
+ return [] { return Script->getDot(); };
}
// GNU linkers implements more complicated logic to handle
// DATA_SEGMENT_RELRO_END. We instead ignore the arguments and just align to
@@ -1851,16 +1874,15 @@ Expr ScriptParser::readPrimary() {
expect(",");
readExpr();
expect(")");
- return [] { return alignTo(ScriptBase->getDot(), Target->PageSize); };
+ return [] { return alignTo(Script->getDot(), Target->PageSize); };
}
if (Tok == "SIZEOF") {
StringRef Name = readParenLiteral();
- return [=] { return ScriptBase->getOutputSectionSize(Name); };
+ return [=] { return Script->getOutputSectionSize(Name); };
}
if (Tok == "ALIGNOF") {
StringRef Name = readParenLiteral();
- return
- [=] { return ScriptBase->getOutputSection(Location, Name)->Alignment; };
+ return [=] { return Script->getOutputSection(Location, Name)->Alignment; };
}
if (Tok == "SIZEOF_HEADERS")
return [=] { return elf::getHeaderSize(); };
@@ -1873,7 +1895,7 @@ Expr ScriptParser::readPrimary() {
// Tok is a symbol name.
if (Tok != "." && !isValidCIdentifier(Tok))
setError("malformed number: " + Tok);
- return [=] { return ScriptBase->getSymbolValue(Location, Tok); };
+ return [=] { return Script->getSymbolValue(Location, Tok); };
}
Expr ScriptParser::readTernary(Expr Cond) {
@@ -2122,8 +2144,3 @@ void elf::readVersionScript(MemoryBuffer
void elf::readDynamicList(MemoryBufferRef MB) {
ScriptParser(MB).readDynamicList();
}
-
-template class elf::LinkerScript<ELF32LE>;
-template class elf::LinkerScript<ELF32BE>;
-template class elf::LinkerScript<ELF64LE>;
-template class elf::LinkerScript<ELF64BE>;
Modified: lld/trunk/ELF/LinkerScript.h
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/ELF/LinkerScript.h?rev=298241&r1=298240&r2=298241&view=diff
==============================================================================
--- lld/trunk/ELF/LinkerScript.h (original)
+++ lld/trunk/ELF/LinkerScript.h Mon Mar 20 05:09:58 2017
@@ -223,8 +223,6 @@ extern ScriptConfiguration *ScriptConfig
class LinkerScriptBase {
protected:
- ~LinkerScriptBase() = default;
-
void assignSymbol(SymbolAssignment *Cmd, bool InSec = false);
void computeInputSections(InputSectionDescription *);
void setDot(Expr E, const Twine &Loc, bool InSec = false);
@@ -265,8 +263,8 @@ public:
uint64_t getOutputSectionSize(StringRef S);
void discard(ArrayRef<InputSectionBase *> V);
- virtual ExprValue getSymbolValue(const Twine &Loc, StringRef S) = 0;
- virtual bool isDefined(StringRef S) = 0;
+ ExprValue getSymbolValue(const Twine &Loc, StringRef S);
+ bool isDefined(StringRef S);
std::vector<OutputSection *> *OutputSections;
void addOrphanSections(OutputSectionFactory &Factory);
@@ -285,28 +283,13 @@ public:
void processNonSectionCommands();
void assignAddresses(std::vector<PhdrEntry> &Phdrs);
int getSectionIndex(StringRef Name);
-};
-
-// This is a runner of the linker script.
-template <class ELFT> class LinkerScript final : public LinkerScriptBase {
-public:
- LinkerScript();
- ~LinkerScript();
void writeDataBytes(StringRef Name, uint8_t *Buf);
void addSymbol(SymbolAssignment *Cmd);
void processCommands(OutputSectionFactory &Factory);
-
- ExprValue getSymbolValue(const Twine &Loc, StringRef S) override;
- bool isDefined(StringRef S) override;
};
-// Variable template is a C++14 feature, so we can't template
-// a global variable. Use a struct to workaround.
-template <class ELFT> struct Script { static LinkerScript<ELFT> *X; };
-template <class ELFT> LinkerScript<ELFT> *Script<ELFT>::X;
-
-extern LinkerScriptBase *ScriptBase;
+extern LinkerScriptBase *Script;
} // end namespace elf
} // end namespace lld
Modified: lld/trunk/ELF/MarkLive.cpp
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/ELF/MarkLive.cpp?rev=298241&r1=298240&r2=298241&view=diff
==============================================================================
--- lld/trunk/ELF/MarkLive.cpp (original)
+++ lld/trunk/ELF/MarkLive.cpp Mon Mar 20 05:09:58 2017
@@ -246,7 +246,7 @@ template <class ELFT> void elf::markLive
scanEhFrameSection<ELFT>(*EH, Enqueue);
if (Sec->Flags & SHF_LINK_ORDER)
continue;
- if (isReserved<ELFT>(Sec) || Script<ELFT>::X->shouldKeep(Sec))
+ if (isReserved<ELFT>(Sec) || Script->shouldKeep(Sec))
Enqueue({Sec, 0});
else if (isValidCIdentifier(Sec->Name)) {
CNamedSections[Saver.save("__start_" + Sec->Name)].push_back(Sec);
Modified: lld/trunk/ELF/OutputSections.cpp
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/ELF/OutputSections.cpp?rev=298241&r1=298240&r2=298241&view=diff
==============================================================================
--- lld/trunk/ELF/OutputSections.cpp (original)
+++ lld/trunk/ELF/OutputSections.cpp Mon Mar 20 05:09:58 2017
@@ -236,7 +236,7 @@ void fill(uint8_t *Buf, size_t Size, uin
template <class ELFT> void OutputSection::writeTo(uint8_t *Buf) {
Loc = Buf;
- if (uint32_t Filler = Script<ELFT>::X->getFiller(this->Name))
+ if (uint32_t Filler = Script->getFiller(this->Name))
fill(Buf, this->Size, Filler);
auto Fn = [=](InputSection *IS) { IS->writeTo<ELFT>(Buf); };
@@ -244,7 +244,7 @@ template <class ELFT> void OutputSection
// Linker scripts may have BYTE()-family commands with which you
// can write arbitrary bytes to the output. Process them if any.
- Script<ELFT>::X->writeDataBytes(this->Name, Buf);
+ Script->writeDataBytes(this->Name, Buf);
}
static uint64_t getOutFlags(InputSectionBase *S) {
Modified: lld/trunk/ELF/SyntheticSections.cpp
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/ELF/SyntheticSections.cpp?rev=298241&r1=298240&r2=298241&view=diff
==============================================================================
--- lld/trunk/ELF/SyntheticSections.cpp (original)
+++ lld/trunk/ELF/SyntheticSections.cpp Mon Mar 20 05:09:58 2017
@@ -2199,7 +2199,7 @@ MipsRldMapSection::MipsRldMapSection()
void MipsRldMapSection::writeTo(uint8_t *Buf) {
// Apply filler from linker script.
- uint64_t Filler = ScriptBase->getFiller(this->Name);
+ uint64_t Filler = Script->getFiller(this->Name);
Filler = (Filler << 32) | Filler;
memcpy(Buf, &Filler, getSize());
}
Modified: lld/trunk/ELF/Writer.cpp
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/ELF/Writer.cpp?rev=298241&r1=298240&r2=298241&view=diff
==============================================================================
--- lld/trunk/ELF/Writer.cpp (original)
+++ lld/trunk/ELF/Writer.cpp Mon Mar 20 05:09:58 2017
@@ -133,8 +133,7 @@ StringRef elf::getOutputSectionName(Stri
template <class ELFT> static bool needsInterpSection() {
return !Symtab<ELFT>::X->getSharedFiles().empty() &&
- !Config->DynamicLinker.empty() &&
- !Script<ELFT>::X->ignoreInterpSection();
+ !Config->DynamicLinker.empty() && !Script->ignoreInterpSection();
}
template <class ELFT> void elf::writeResult() { Writer<ELFT>().run(); }
@@ -228,21 +227,21 @@ template <class ELFT> void Writer<ELFT>:
addReservedSymbols();
// Create output sections.
- Script<ELFT>::X->OutputSections = &OutputSections;
+ Script->OutputSections = &OutputSections;
if (ScriptConfig->HasSections) {
// If linker script contains SECTIONS commands, let it create sections.
- Script<ELFT>::X->processCommands(Factory);
+ Script->processCommands(Factory);
// Linker scripts may have left some input sections unassigned.
// Assign such sections using the default rule.
- Script<ELFT>::X->addOrphanSections(Factory);
+ Script->addOrphanSections(Factory);
} else {
// If linker script does not contain SECTIONS commands, create
// output sections by default rules. We still need to give the
// linker script a chance to run, because it might contain
// non-SECTIONS commands such as ASSERT.
createSections();
- Script<ELFT>::X->processCommands(Factory);
+ Script->processCommands(Factory);
}
if (Config->Discard != DiscardPolicy::All)
@@ -263,11 +262,11 @@ template <class ELFT> void Writer<ELFT>:
assignFileOffsets();
} else {
if (ScriptConfig->HasSections) {
- Script<ELFT>::X->assignAddresses(Phdrs);
+ Script->assignAddresses(Phdrs);
} else {
fixSectionAlignments();
assignAddresses();
- Script<ELFT>::X->processNonSectionCommands();
+ Script->processNonSectionCommands();
}
// Remove empty PT_LOAD to avoid causing the dynamic linker to try to mmap a
@@ -723,8 +722,8 @@ static bool compareSectionsNonScript(con
template <class ELFT>
static bool compareSections(const OutputSection *A, const OutputSection *B) {
// For now, put sections mentioned in a linker script first.
- int AIndex = Script<ELFT>::X->getSectionIndex(A->Name);
- int BIndex = Script<ELFT>::X->getSectionIndex(B->Name);
+ int AIndex = Script->getSectionIndex(A->Name);
+ int BIndex = Script->getSectionIndex(B->Name);
bool AInScript = AIndex != INT_MAX;
bool BInScript = BIndex != INT_MAX;
if (AInScript != BInScript)
@@ -968,7 +967,7 @@ template <class ELFT> void Writer<ELFT>:
compareSectionsNonScript<ELFT>);
return;
}
- Script<ELFT>::X->adjustSectionsBeforeSorting();
+ Script->adjustSectionsBeforeSorting();
// The order of the sections in the script is arbitrary and may not agree with
// compareSectionsNonScript. This means that we cannot easily define a
@@ -1001,7 +1000,7 @@ template <class ELFT> void Writer<ELFT>:
auto E = OutputSections.end();
auto NonScriptI =
std::find_if(OutputSections.begin(), E, [](OutputSection *S) {
- return Script<ELFT>::X->getSectionIndex(S->Name) == INT_MAX;
+ return Script->getSectionIndex(S->Name) == INT_MAX;
});
while (NonScriptI != E) {
auto BestPos = std::max_element(
@@ -1031,7 +1030,7 @@ template <class ELFT> void Writer<ELFT>:
++NonScriptI;
}
- Script<ELFT>::X->adjustSectionsAfterSorting();
+ Script->adjustSectionsAfterSorting();
}
static void applySynthetic(const std::vector<SyntheticSection *> &Sections,
@@ -1152,8 +1151,7 @@ template <class ELFT> void Writer<ELFT>:
// The headers have to be created before finalize as that can influence the
// image base and the dynamic section on mips includes the image base.
if (!Config->Relocatable && !Config->OFormatBinary) {
- Phdrs = Script<ELFT>::X->hasPhdrsCommands() ? Script<ELFT>::X->createPhdrs()
- : createPhdrs();
+ Phdrs = Script->hasPhdrsCommands() ? Script->createPhdrs() : createPhdrs();
addPtArmExid(Phdrs);
fixHeaders();
}
@@ -1306,7 +1304,7 @@ template <class ELFT> std::vector<PhdrEn
// different flags or is loaded at a discontiguous address using AT linker
// script command.
uintX_t NewFlags = computeFlags<ELFT>(Sec->getPhdrFlags());
- if (Script<ELFT>::X->hasLMA(Sec->Name) || Flags != NewFlags) {
+ if (Script->hasLMA(Sec->Name) || Flags != NewFlags) {
Load = AddHdr(PT_LOAD, NewFlags);
Flags = NewFlags;
}
@@ -1370,7 +1368,7 @@ template <class ELFT> std::vector<PhdrEn
PhdrEntry *Note = nullptr;
for (OutputSection *Sec : OutputSections) {
if (Sec->Type == SHT_NOTE) {
- if (!Note || Script<ELFT>::X->hasLMA(Sec->Name))
+ if (!Note || Script->hasLMA(Sec->Name))
Note = AddHdr(PT_NOTE, PF_R);
Note->add(Sec);
} else {
@@ -1447,7 +1445,7 @@ bool elf::allocateHeaders(std::vector<Ph
Out::ElfHeader->Addr = Min;
Out::ProgramHeaders->Addr = Min + Out::ElfHeader->Size;
- if (ScriptBase->hasPhdrsCommands())
+ if (Script->hasPhdrsCommands())
return true;
if (FirstPTLoad->First)
More information about the llvm-commits
mailing list