From a34da93847cd62e630fdc7c995c712241f99aa32 Mon Sep 17 00:00:00 2001 From: Rui Ueyama Date: Tue, 21 Mar 2017 23:03:09 +0000 Subject: [PATCH] Make elf::ScriptConfig a LinkerScript class member variable. LinkerScript used to be a template class, so we couldn't instantiate that class in elf::link. We instantiated ScriptConfig class earlier instead so that the linker script parser can store configurations to the object. Now that LinkerScript is not a template, it doesn't make sense to separate ScriptConfig from LinkerScript. This patch merges them. llvm-svn: 298457 --- lld/ELF/Driver.cpp | 3 +-- lld/ELF/LinkerScript.cpp | 27 +++++++++++++-------------- lld/ELF/LinkerScript.h | 8 +++----- lld/ELF/Writer.cpp | 12 ++++++------ 4 files changed, 23 insertions(+), 27 deletions(-) diff --git a/lld/ELF/Driver.cpp b/lld/ELF/Driver.cpp index c86d878..128b2ee 100644 --- a/lld/ELF/Driver.cpp +++ b/lld/ELF/Driver.cpp @@ -78,7 +78,7 @@ bool elf::link(ArrayRef Args, bool CanExitEarly, Config = make(); Driver = make(); - ScriptConfig = make(); + Script = make(); Driver->main(Args, CanExitEarly); freeArena(); @@ -855,7 +855,6 @@ template void LinkerDriver::link(opt::InputArgList &Args) { SymbolTable Symtab; elf::Symtab::X = &Symtab; Target = createTarget(); - Script = make(); Config->MaxPageSize = getMaxPageSize(Args); Config->ImageBase = getImageBase(Args); diff --git a/lld/ELF/LinkerScript.cpp b/lld/ELF/LinkerScript.cpp index 9c765fa..f98e689 100644 --- a/lld/ELF/LinkerScript.cpp +++ b/lld/ELF/LinkerScript.cpp @@ -53,6 +53,8 @@ using namespace llvm::support::endian; using namespace lld; using namespace lld::elf; +LinkerScript *elf::Script; + uint64_t ExprValue::getValue() const { if (Sec) return Sec->getOffset(Val) + Sec->getOutputSection()->Addr; @@ -109,9 +111,6 @@ static ExprValue bitOr(ExprValue A, ExprValue B) { static ExprValue bitNot(ExprValue A) { return ~A.getValue(); } static ExprValue minus(ExprValue A) { return -A.getValue(); } -LinkerScript *elf::Script; -ScriptConfiguration *elf::ScriptConfig; - template static SymbolBody *addRegular(SymbolAssignment *Cmd) { Symbol *Sym; uint8_t Visibility = Cmd->Hidden ? STV_HIDDEN : STV_DEFAULT; @@ -1102,7 +1101,6 @@ private: std::pair, std::vector> readSymbols(); - ScriptConfiguration &Opt = *ScriptConfig; bool IsUnderSysroot; }; @@ -1150,7 +1148,7 @@ void ScriptParser::readLinkerScript() { continue; if (Tok == "ASSERT") { - Opt.Commands.emplace_back(new AssertCommand(readAssert())); + Script->Opt.Commands.emplace_back(new AssertCommand(readAssert())); } else if (Tok == "ENTRY") { readEntry(); } else if (Tok == "EXTERN") { @@ -1176,7 +1174,7 @@ void ScriptParser::readLinkerScript() { } else if (Tok == "VERSION") { readVersion(); } else if (SymbolAssignment *Cmd = readProvideOrAssignment(Tok)) { - Opt.Commands.emplace_back(Cmd); + Script->Opt.Commands.emplace_back(Cmd); } else { setError("unknown directive: " + Tok); } @@ -1303,9 +1301,9 @@ void ScriptParser::readPhdrs() { expect("{"); while (!Error && !consume("}")) { StringRef Tok = next(); - Opt.PhdrsCommands.push_back( + Script->Opt.PhdrsCommands.push_back( {Tok, PT_NULL, false, false, UINT_MAX, nullptr}); - PhdrsCommand &PhdrCmd = Opt.PhdrsCommands.back(); + PhdrsCommand &PhdrCmd = Script->Opt.PhdrsCommands.back(); PhdrCmd.Type = readPhdrType(); do { @@ -1339,7 +1337,7 @@ void ScriptParser::readSearchDir() { } void ScriptParser::readSections() { - Opt.HasSections = true; + Script->Opt.HasSections = true; // -no-rosegment is used to avoid placing read only non-executable sections in // their own segment. We do the same if SECTIONS command is present in linker // script. See comment for computeFlags(). @@ -1355,7 +1353,7 @@ void ScriptParser::readSections() { else Cmd = readOutputSectionDescription(Tok); } - Opt.Commands.emplace_back(Cmd); + Script->Opt.Commands.emplace_back(Cmd); } } @@ -1469,7 +1467,7 @@ ScriptParser::readInputSectionDescription(StringRef Tok) { StringRef FilePattern = next(); InputSectionDescription *Cmd = readInputSectionRules(FilePattern); expect(")"); - Opt.KeptSections.push_back(Cmd); + Script->Opt.KeptSections.push_back(Cmd); return Cmd; } return readInputSectionRules(Tok); @@ -2072,11 +2070,12 @@ void ScriptParser::readMemory() { uint64_t Length = readMemoryAssignment("LENGTH", "len", "l"); // Add the memory region to the region map (if it doesn't already exist). - auto It = Opt.MemoryRegions.find(Name); - if (It != Opt.MemoryRegions.end()) + auto It = Script->Opt.MemoryRegions.find(Name); + if (It != Script->Opt.MemoryRegions.end()) setError("region '" + Name + "' already defined"); else - Opt.MemoryRegions[Name] = {Name, Origin, Length, Origin, Flags, NegFlags}; + Script->Opt.MemoryRegions[Name] = {Name, Origin, Length, + Origin, Flags, NegFlags}; } } diff --git a/lld/ELF/LinkerScript.h b/lld/ELF/LinkerScript.h index 38c89ec..00e4f9e 100644 --- a/lld/ELF/LinkerScript.h +++ b/lld/ELF/LinkerScript.h @@ -219,8 +219,6 @@ struct ScriptConfiguration { llvm::DenseMap MemoryRegions; }; -extern ScriptConfiguration *ScriptConfig; - class LinkerScript { protected: void assignSymbol(SymbolAssignment *Cmd, bool InSec = false); @@ -243,9 +241,6 @@ protected: OutputSection *Aether; bool ErrorOnMissingSection = false; - // "ScriptConfig" is a bit too long, so define a short name for it. - ScriptConfiguration &Opt = *ScriptConfig; - uint64_t Dot; uint64_t ThreadBssOffset = 0; @@ -287,6 +282,9 @@ public: void writeDataBytes(StringRef Name, uint8_t *Buf); void addSymbol(SymbolAssignment *Cmd); void processCommands(OutputSectionFactory &Factory); + + // Parsed linker script configurations are set to this struct. + ScriptConfiguration Opt; }; extern LinkerScript *Script; diff --git a/lld/ELF/Writer.cpp b/lld/ELF/Writer.cpp index 50c0e6c..024d1f2 100644 --- a/lld/ELF/Writer.cpp +++ b/lld/ELF/Writer.cpp @@ -228,7 +228,7 @@ template void Writer::run() { // Create output sections. Script->OutputSections = &OutputSections; - if (ScriptConfig->HasSections) { + if (Script->Opt.HasSections) { // If linker script contains SECTIONS commands, let it create sections. Script->processCommands(Factory); @@ -261,7 +261,7 @@ template void Writer::run() { if (Config->Relocatable) { assignFileOffsets(); } else { - if (ScriptConfig->HasSections) { + if (Script->Opt.HasSections) { Script->assignAddresses(Phdrs); } else { fixSectionAlignments(); @@ -841,7 +841,7 @@ template void Writer::addReservedSymbols() { Symtab::X->addIgnored("__tls_get_addr"); // If linker script do layout we do not need to create any standart symbols. - if (ScriptConfig->HasSections) + if (Script->Opt.HasSections) return; // __ehdr_start is the location of ELF file headers. @@ -960,7 +960,7 @@ template void Writer::sortSections() { // relative order for SHF_LINK_ORDER sections. if (Config->Relocatable) return; - if (!ScriptConfig->HasSections) { + if (!Script->Opt.HasSections) { std::stable_sort(OutputSections.begin(), OutputSections.end(), compareSectionsNonScript); return; @@ -1437,7 +1437,7 @@ bool elf::allocateHeaders(std::vector &Phdrs, } Min = alignDown(Min - HeaderSize, Config->MaxPageSize); - if (!ScriptConfig->HasSections) + if (!Script->Opt.HasSections) Config->ImageBase = Min = std::min(Min, Config->ImageBase); Out::ElfHeader->Addr = Min; @@ -1462,7 +1462,7 @@ bool elf::allocateHeaders(std::vector &Phdrs, template void Writer::fixHeaders() { Out::ProgramHeaders->Size = sizeof(Elf_Phdr) * Phdrs.size(); // If the script has SECTIONS, assignAddresses will compute the values. - if (ScriptConfig->HasSections) + if (Script->Opt.HasSections) return; // When -T
option is specified, lower the base to make room for those -- 2.7.4