Index: ELF/LinkerScript.h =================================================================== --- ELF/LinkerScript.h +++ ELF/LinkerScript.h @@ -55,8 +55,6 @@ // Output sections are sorted by this order. std::vector SectionOrder; - - llvm::BumpPtrAllocator Alloc; }; extern LinkerScript *Script; Index: ELF/LinkerScript.cpp =================================================================== --- ELF/LinkerScript.cpp +++ ELF/LinkerScript.cpp @@ -84,8 +84,7 @@ class elf2::ScriptParser { public: - ScriptParser(BumpPtrAllocator *A, StringRef S, bool B) - : Saver(*A), Tokens(tokenize(S)), IsUnderSysroot(B) {} + ScriptParser(StringRef S, bool B) : Tokens(tokenize(S)), IsUnderSysroot(B) {} void run(); private: @@ -112,40 +111,40 @@ void readOutputSectionDescription(); - StringSaver Saver; + void init(); + + llvm::BumpPtrAllocator Alloc; + StringSaver Saver{Alloc}; std::vector Tokens; - bool Error = false; + llvm::StringMap> Cmd; size_t Pos = 0; bool IsUnderSysroot; + bool Error = false; }; +void ScriptParser::init() { + Cmd["ENTRY"] = &ScriptParser::readEntry; + Cmd["EXTERN"] = &ScriptParser::readExtern; + Cmd["GROUP"] = &ScriptParser::readGroup; + Cmd["INCLUDE"] = &ScriptParser::readInclude; + Cmd["INPUT"] = &ScriptParser::readGroup; + Cmd["OUTPUT"] = &ScriptParser::readOutput; + Cmd["OUTPUT_ARCH"] = &ScriptParser::readOutputArch; + Cmd["OUTPUT_FORMAT"] = &ScriptParser::readOutputFormat; + Cmd["SEARCH_DIR"] = &ScriptParser::readSearchDir; + Cmd["SECTIONS"] = &ScriptParser::readSections; + Cmd[";"] = [](ScriptParser &) {}; +} + void ScriptParser::run() { + init(); while (!atEOF()) { StringRef Tok = next(); - if (Tok == ";") - continue; - if (Tok == "ENTRY") { - readEntry(); - } else if (Tok == "EXTERN") { - readExtern(); - } else if (Tok == "GROUP" || Tok == "INPUT") { - readGroup(); - } else if (Tok == "INCLUDE") { - readInclude(); - } else if (Tok == "OUTPUT") { - readOutput(); - } else if (Tok == "OUTPUT_ARCH") { - readOutputArch(); - } else if (Tok == "OUTPUT_FORMAT") { - readOutputFormat(); - } else if (Tok == "SEARCH_DIR") { - readSearchDir(); - } else if (Tok == "SECTIONS") { - readSections(); - } else { + auto C = Cmd.find(Tok); + if (Cmd.find(Tok) != Cmd.end()) + C->second(*this); + else setError("unknown directive: " + Tok); - return; - } } } @@ -373,7 +372,7 @@ void ScriptParser::readSections() { expect("{"); - while (!Error && !skip("}")) + while (!skip("}")) readOutputSectionDescription(); } @@ -382,10 +381,10 @@ Script->SectionOrder.push_back(OutSec); expect(":"); expect("{"); - while (!Error && !skip("}")) { + while (!skip("}")) { next(); // Skip input file name. expect("("); - while (!Error && !skip(")")) + while (!skip(")")) Script->Sections.push_back({OutSec, next()}); } } @@ -402,7 +401,7 @@ // Entry point. The other functions or classes are private to this file. void LinkerScript::read(MemoryBufferRef MB) { StringRef Path = MB.getBufferIdentifier(); - ScriptParser(&Alloc, MB.getBuffer(), isUnderSysroot(Path)).run(); + ScriptParser(MB.getBuffer(), isUnderSysroot(Path)).run(); } template StringRef LinkerScript::getOutputSection(InputSectionBase *);