Index: ELF/LinkerScript.h =================================================================== --- ELF/LinkerScript.h +++ ELF/LinkerScript.h @@ -34,6 +34,8 @@ // Config and ScriptConfig. void readLinkerScript(MemoryBufferRef MB); +void parseVersionScript(MemoryBufferRef MB); + // This enum is used to implement linker script SECTIONS command. // https://sourceware.org/binutils/docs/ld/SECTIONS.html#SECTIONS enum SectionsCommandKind { Index: ELF/LinkerScript.cpp =================================================================== --- ELF/LinkerScript.cpp +++ ELF/LinkerScript.cpp @@ -580,7 +580,8 @@ typedef void (ScriptParser::*Handler)(); public: - ScriptParser(StringRef S, bool B) : ScriptParserBase(S), IsUnderSysroot(B) {} + ScriptParser(StringRef S, bool B, bool V) + : ScriptParserBase(S), IsUnderSysroot(B), IsVersionScript(V) {} void run(); @@ -613,6 +614,7 @@ SymbolAssignment *readProvideOrAssignment(StringRef Tok); void readSort(); Expr readAssert(); + void readVersionScript(); Expr readExpr(); Expr readExpr1(Expr Lhs, int MinPrec); @@ -620,10 +622,17 @@ Expr readTernary(Expr Cond); Expr readParenExpr(); + // For parsing version script. + void parseExtern(std::vector *Globals); + void parseVersion(StringRef VerStr); + void parseGlobal(StringRef VerStr); + void parseLocal(); + const static StringMap Cmd; ScriptConfiguration &Opt = *ScriptConfig; StringSaver Saver = {ScriptConfig->Alloc}; bool IsUnderSysroot; + bool IsVersionScript; }; const StringMap elf::ScriptParser::Cmd = { @@ -641,6 +650,11 @@ {";", &ScriptParser::readNothing}}; void ScriptParser::run() { + if (IsVersionScript) { + readVersionScript(); + return; + } + while (!atEOF()) { StringRef Tok = next(); if (Handler Fn = Cmd.lookup(Tok)) @@ -1268,6 +1282,89 @@ return Ret; } +void ScriptParser::readVersionScript() { + StringRef Msg = "anonymous version definition is used in " + "combination with other version definitions"; + if (skip("{")) { + parseVersion(""); + if (!atEOF()) + setError(Msg); + return; + } + + while (!atEOF() && !Error) { + StringRef VerStr = next(); + if (VerStr == "{") { + setError(Msg); + return; + } + expect("{"); + parseVersion(VerStr); + } +} + +void ScriptParser::parseVersion(StringRef VerStr) { + // Identifiers start at 2 because 0 and 1 are reserved + // for VER_NDX_LOCAL and VER_NDX_GLOBAL constants. + size_t VersionId = Config->VersionDefinitions.size() + 2; + Config->VersionDefinitions.push_back({VerStr, VersionId}); + + if (skip("global:") || peek() != "local:") + parseGlobal(VerStr); + if (skip("local:")) + parseLocal(); + expect("}"); + + // Each version may have a parent version. For example, "Ver2" defined as + // "Ver2 { global: foo; local: *; } Ver1;" has "Ver1" as a parent. This + // version hierarchy is, probably against your instinct, purely for human; the + // runtime doesn't care about them at all. In LLD, we simply skip the token. + if (!VerStr.empty() && peek() != ";") + next(); + expect(";"); +} + +void ScriptParser::parseLocal() { + Config->DefaultSymbolVersion = VER_NDX_LOCAL; + expect("*"); + expect(";"); +} + +void ScriptParser::parseExtern(std::vector *Globals) { + expect("C++"); + expect("{"); + + for (;;) { + if (peek() == "}" || Error) + break; + Globals->push_back({next(), true}); + expect(";"); + } + + expect("}"); + expect(";"); +} + +void ScriptParser::parseGlobal(StringRef VerStr) { + std::vector *Globals; + if (VerStr.empty()) + Globals = &Config->VersionScriptGlobals; + else + Globals = &Config->VersionDefinitions.back().Globals; + + for (;;) { + if (skip("extern")) + parseExtern(Globals); + + StringRef Cur = peek(); + if (Cur == "}" || Cur == "local:" || Error) + return; + next(); + Globals->push_back({Cur, false}); + expect(";"); + } +} + static bool isUnderSysroot(StringRef Path) { if (Config->Sysroot == "") return false; @@ -1280,7 +1377,18 @@ // Entry point. void elf::readLinkerScript(MemoryBufferRef MB) { StringRef Path = MB.getBufferIdentifier(); - ScriptParser(MB.getBuffer(), isUnderSysroot(Path)).run(); + bool UnderSysRoot = isUnderSysroot(Path); + ScriptParser(MB.getBuffer(), UnderSysRoot, false /*Version script*/).run(); +} + +// Parse the --version-script argument. We currently only accept the following +// version script syntax: +// +// { [ global: symbol1; symbol2; [...]; symbolN; ] local: *; }; +// + +void elf::parseVersionScript(MemoryBufferRef MB) { + ScriptParser(MB.getBuffer(), false, true /*Version script*/).run(); } template class elf::LinkerScript; Index: ELF/SymbolListFile.h =================================================================== --- ELF/SymbolListFile.h +++ ELF/SymbolListFile.h @@ -17,7 +17,6 @@ namespace elf { void parseDynamicList(MemoryBufferRef MB); -void parseVersionScript(MemoryBufferRef MB); } // namespace elf } // namespace lld Index: ELF/SymbolListFile.cpp =================================================================== --- ELF/SymbolListFile.cpp +++ ELF/SymbolListFile.cpp @@ -55,113 +55,3 @@ void elf::parseDynamicList(MemoryBufferRef MB) { DynamicListParser(MB.getBuffer()).run(); } - -// Parse the --version-script argument. We currently only accept the following -// version script syntax: -// -// { [ global: symbol1; symbol2; [...]; symbolN; ] local: *; }; -// -// No wildcards are supported, other than for the local entry. Symbol versioning -// is also not supported. - -namespace { -class VersionScriptParser final : public ScriptParserBase { -public: - VersionScriptParser(StringRef S) : ScriptParserBase(S) {} - - void run(); - -private: - void parseExtern(std::vector *Globals); - void parseVersion(StringRef VerStr); - void parseGlobal(StringRef VerStr); - void parseLocal(); -}; -} // end anonymous namespace - -void VersionScriptParser::parseVersion(StringRef VerStr) { - // Identifiers start at 2 because 0 and 1 are reserved - // for VER_NDX_LOCAL and VER_NDX_GLOBAL constants. - size_t VersionId = Config->VersionDefinitions.size() + 2; - Config->VersionDefinitions.push_back({VerStr, VersionId}); - - if (skip("global:") || peek() != "local:") - parseGlobal(VerStr); - if (skip("local:")) - parseLocal(); - expect("}"); - - // Each version may have a parent version. For example, "Ver2" defined as - // "Ver2 { global: foo; local: *; } Ver1;" has "Ver1" as a parent. This - // version hierarchy is, probably against your instinct, purely for human; the - // runtime doesn't care about them at all. In LLD, we simply skip the token. - if (!VerStr.empty() && peek() != ";") - next(); - expect(";"); -} - -void VersionScriptParser::parseLocal() { - Config->DefaultSymbolVersion = VER_NDX_LOCAL; - expect("*"); - expect(";"); -} - -void VersionScriptParser::parseExtern(std::vector *Globals) { - expect("C++"); - expect("{"); - - for (;;) { - if (peek() == "}" || Error) - break; - Globals->push_back({next(), true}); - expect(";"); - } - - expect("}"); - expect(";"); -} - -void VersionScriptParser::parseGlobal(StringRef VerStr) { - std::vector *Globals; - if (VerStr.empty()) - Globals = &Config->VersionScriptGlobals; - else - Globals = &Config->VersionDefinitions.back().Globals; - - for (;;) { - if (skip("extern")) - parseExtern(Globals); - - StringRef Cur = peek(); - if (Cur == "}" || Cur == "local:" || Error) - return; - next(); - Globals->push_back({Cur, false}); - expect(";"); - } -} - -void VersionScriptParser::run() { - StringRef Msg = "anonymous version definition is used in " - "combination with other version definitions"; - if (skip("{")) { - parseVersion(""); - if (!atEOF()) - setError(Msg); - return; - } - - while (!atEOF() && !Error) { - StringRef VerStr = next(); - if (VerStr == "{") { - setError(Msg); - return; - } - expect("{"); - parseVersion(VerStr); - } -} - -void elf::parseVersionScript(MemoryBufferRef MB) { - VersionScriptParser(MB.getBuffer()).run(); -}