12ec34544SRui Ueyama //===- ScriptParser.cpp ---------------------------------------------------===// 22ec34544SRui Ueyama // 32ec34544SRui Ueyama // The LLVM Linker 42ec34544SRui Ueyama // 52ec34544SRui Ueyama // This file is distributed under the University of Illinois Open Source 62ec34544SRui Ueyama // License. See LICENSE.TXT for details. 72ec34544SRui Ueyama // 82ec34544SRui Ueyama //===----------------------------------------------------------------------===// 905f6b852SRui Ueyama // 1005f6b852SRui Ueyama // This file contains a recursive-descendent parser for linker scripts. 1105f6b852SRui Ueyama // Parsed results are stored to Config and Script global objects. 1205f6b852SRui Ueyama // 1305f6b852SRui Ueyama //===----------------------------------------------------------------------===// 142ec34544SRui Ueyama 152ec34544SRui Ueyama #include "ScriptParser.h" 162ec34544SRui Ueyama #include "Config.h" 172ec34544SRui Ueyama #include "Driver.h" 182ec34544SRui Ueyama #include "InputSection.h" 192ec34544SRui Ueyama #include "LinkerScript.h" 202ec34544SRui Ueyama #include "Memory.h" 212ec34544SRui Ueyama #include "OutputSections.h" 222ec34544SRui Ueyama #include "ScriptLexer.h" 232ec34544SRui Ueyama #include "Symbols.h" 242ec34544SRui Ueyama #include "Target.h" 252ec34544SRui Ueyama #include "llvm/ADT/SmallString.h" 262ec34544SRui Ueyama #include "llvm/ADT/StringRef.h" 272ec34544SRui Ueyama #include "llvm/ADT/StringSwitch.h" 28264b5d9eSZachary Turner #include "llvm/BinaryFormat/ELF.h" 292ec34544SRui Ueyama #include "llvm/Support/Casting.h" 302ec34544SRui Ueyama #include "llvm/Support/ErrorHandling.h" 312ec34544SRui Ueyama #include "llvm/Support/FileSystem.h" 322ec34544SRui Ueyama #include "llvm/Support/Path.h" 332ec34544SRui Ueyama #include <cassert> 342ec34544SRui Ueyama #include <limits> 352ec34544SRui Ueyama #include <vector> 362ec34544SRui Ueyama 372ec34544SRui Ueyama using namespace llvm; 382ec34544SRui Ueyama using namespace llvm::ELF; 39b58079d4SRui Ueyama using namespace llvm::support::endian; 402ec34544SRui Ueyama using namespace lld; 412ec34544SRui Ueyama using namespace lld::elf; 422ec34544SRui Ueyama 432ec34544SRui Ueyama static bool isUnderSysroot(StringRef Path); 442ec34544SRui Ueyama 4596b3fe02SRui Ueyama namespace { 4696b3fe02SRui Ueyama class ScriptParser final : ScriptLexer { 472ec34544SRui Ueyama public: 482ec34544SRui Ueyama ScriptParser(MemoryBufferRef MB) 492ec34544SRui Ueyama : ScriptLexer(MB), 502ec34544SRui Ueyama IsUnderSysroot(isUnderSysroot(MB.getBufferIdentifier())) {} 512ec34544SRui Ueyama 522ec34544SRui Ueyama void readLinkerScript(); 532ec34544SRui Ueyama void readVersionScript(); 542ec34544SRui Ueyama void readDynamicList(); 552ec34544SRui Ueyama 562ec34544SRui Ueyama private: 572ec34544SRui Ueyama void addFile(StringRef Path); 588c022ca7SRafael Espindola OutputSection *checkSection(OutputSection *Cmd, StringRef Loccation); 592ec34544SRui Ueyama 602ec34544SRui Ueyama void readAsNeeded(); 612ec34544SRui Ueyama void readEntry(); 622ec34544SRui Ueyama void readExtern(); 632ec34544SRui Ueyama void readGroup(); 642ec34544SRui Ueyama void readInclude(); 652ec34544SRui Ueyama void readMemory(); 662ec34544SRui Ueyama void readOutput(); 672ec34544SRui Ueyama void readOutputArch(); 682ec34544SRui Ueyama void readOutputFormat(); 692ec34544SRui Ueyama void readPhdrs(); 702ec34544SRui Ueyama void readSearchDir(); 712ec34544SRui Ueyama void readSections(); 722ec34544SRui Ueyama void readVersion(); 732ec34544SRui Ueyama void readVersionScriptCommand(); 742ec34544SRui Ueyama 752ec34544SRui Ueyama SymbolAssignment *readAssignment(StringRef Name); 762ec34544SRui Ueyama BytesDataCommand *readBytesDataCommand(StringRef Tok); 772ec34544SRui Ueyama uint32_t readFill(); 788acbf1ccSRui Ueyama uint32_t parseFill(StringRef Tok); 798c022ca7SRafael Espindola void readSectionAddressType(OutputSection *Cmd); 808c022ca7SRafael Espindola OutputSection *readOutputSectionDescription(StringRef OutSec); 812ec34544SRui Ueyama std::vector<StringRef> readOutputSectionPhdrs(); 822ec34544SRui Ueyama InputSectionDescription *readInputSectionDescription(StringRef Tok); 832ec34544SRui Ueyama StringMatcher readFilePatterns(); 842ec34544SRui Ueyama std::vector<SectionPattern> readInputSectionsList(); 852ec34544SRui Ueyama InputSectionDescription *readInputSectionRules(StringRef FilePattern); 862ec34544SRui Ueyama unsigned readPhdrType(); 872ec34544SRui Ueyama SortSectionPolicy readSortKind(); 882ec34544SRui Ueyama SymbolAssignment *readProvideHidden(bool Provide, bool Hidden); 892ec34544SRui Ueyama SymbolAssignment *readProvideOrAssignment(StringRef Tok); 902ec34544SRui Ueyama void readSort(); 9123af89ccSRui Ueyama AssertCommand *readAssert(); 9223af89ccSRui Ueyama Expr readAssertExpr(); 935fb17128SGeorge Rimar Expr readConstant(); 945fb17128SGeorge Rimar Expr getPageSize(); 952ec34544SRui Ueyama 962ec34544SRui Ueyama uint64_t readMemoryAssignment(StringRef, StringRef, StringRef); 972ec34544SRui Ueyama std::pair<uint32_t, uint32_t> readMemoryAttributes(); 982ec34544SRui Ueyama 992ec34544SRui Ueyama Expr readExpr(); 1002ec34544SRui Ueyama Expr readExpr1(Expr Lhs, int MinPrec); 1012ec34544SRui Ueyama StringRef readParenLiteral(); 1022ec34544SRui Ueyama Expr readPrimary(); 1032ec34544SRui Ueyama Expr readTernary(Expr Cond); 1042ec34544SRui Ueyama Expr readParenExpr(); 1052ec34544SRui Ueyama 1062ec34544SRui Ueyama // For parsing version script. 1072ec34544SRui Ueyama std::vector<SymbolVersion> readVersionExtern(); 1082ec34544SRui Ueyama void readAnonymousDeclaration(); 1092ec34544SRui Ueyama void readVersionDeclaration(StringRef VerStr); 1102ec34544SRui Ueyama 1112ec34544SRui Ueyama std::pair<std::vector<SymbolVersion>, std::vector<SymbolVersion>> 1122ec34544SRui Ueyama readSymbols(); 1132ec34544SRui Ueyama 1142ec34544SRui Ueyama bool IsUnderSysroot; 1152ec34544SRui Ueyama }; 11696b3fe02SRui Ueyama } // namespace 1172ec34544SRui Ueyama 1181e77ad14SRui Ueyama static StringRef unquote(StringRef S) { 1191e77ad14SRui Ueyama if (S.startswith("\"")) 1201e77ad14SRui Ueyama return S.substr(1, S.size() - 2); 1211e77ad14SRui Ueyama return S; 1221e77ad14SRui Ueyama } 1231e77ad14SRui Ueyama 1242ec34544SRui Ueyama static bool isUnderSysroot(StringRef Path) { 1252ec34544SRui Ueyama if (Config->Sysroot == "") 1262ec34544SRui Ueyama return false; 1272ec34544SRui Ueyama for (; !Path.empty(); Path = sys::path::parent_path(Path)) 1282ec34544SRui Ueyama if (sys::fs::equivalent(Config->Sysroot, Path)) 1292ec34544SRui Ueyama return true; 1302ec34544SRui Ueyama return false; 1312ec34544SRui Ueyama } 1322ec34544SRui Ueyama 1332ec34544SRui Ueyama // Some operations only support one non absolute value. Move the 1342ec34544SRui Ueyama // absolute one to the right hand side for convenience. 1352ec34544SRui Ueyama static void moveAbsRight(ExprValue &A, ExprValue &B) { 1362ec34544SRui Ueyama if (A.isAbsolute()) 1372ec34544SRui Ueyama std::swap(A, B); 1382ec34544SRui Ueyama if (!B.isAbsolute()) 13941c7ab4aSGeorge Rimar error(A.Loc + ": at least one side of the expression must be absolute"); 1402ec34544SRui Ueyama } 1412ec34544SRui Ueyama 1422ec34544SRui Ueyama static ExprValue add(ExprValue A, ExprValue B) { 1432ec34544SRui Ueyama moveAbsRight(A, B); 144039fb8c2SPetr Hosek uint64_t Val = alignTo(A.Val, A.Alignment) + B.getValue(); 145039fb8c2SPetr Hosek return {A.Sec, A.ForceAbsolute, Val, A.Loc}; 1462ec34544SRui Ueyama } 1472ec34544SRui Ueyama 1482ec34544SRui Ueyama static ExprValue sub(ExprValue A, ExprValue B) { 149039fb8c2SPetr Hosek uint64_t Val = alignTo(A.Val, A.Alignment) - B.getValue(); 150039fb8c2SPetr Hosek return {A.Sec, Val, A.Loc}; 1512ec34544SRui Ueyama } 1522ec34544SRui Ueyama 1532ec34544SRui Ueyama static ExprValue mul(ExprValue A, ExprValue B) { 1542ec34544SRui Ueyama return A.getValue() * B.getValue(); 1552ec34544SRui Ueyama } 1562ec34544SRui Ueyama 1572ec34544SRui Ueyama static ExprValue div(ExprValue A, ExprValue B) { 1582ec34544SRui Ueyama if (uint64_t BV = B.getValue()) 1592ec34544SRui Ueyama return A.getValue() / BV; 1602ec34544SRui Ueyama error("division by zero"); 1612ec34544SRui Ueyama return 0; 1622ec34544SRui Ueyama } 1632ec34544SRui Ueyama 1642ec34544SRui Ueyama static ExprValue bitAnd(ExprValue A, ExprValue B) { 1652ec34544SRui Ueyama moveAbsRight(A, B); 1662ec34544SRui Ueyama return {A.Sec, A.ForceAbsolute, 16741c7ab4aSGeorge Rimar (A.getValue() & B.getValue()) - A.getSecAddr(), A.Loc}; 1682ec34544SRui Ueyama } 1692ec34544SRui Ueyama 1702ec34544SRui Ueyama static ExprValue bitOr(ExprValue A, ExprValue B) { 1712ec34544SRui Ueyama moveAbsRight(A, B); 1722ec34544SRui Ueyama return {A.Sec, A.ForceAbsolute, 17341c7ab4aSGeorge Rimar (A.getValue() | B.getValue()) - A.getSecAddr(), A.Loc}; 1742ec34544SRui Ueyama } 1752ec34544SRui Ueyama 1762ec34544SRui Ueyama void ScriptParser::readDynamicList() { 1772ec34544SRui Ueyama expect("{"); 178*e158f7c3SRui Ueyama readAnonymousDeclaration(); 179*e158f7c3SRui Ueyama if (!atEOF()) 1802ec34544SRui Ueyama setError("EOF expected, but got " + next()); 1812ec34544SRui Ueyama } 1822ec34544SRui Ueyama 1832ec34544SRui Ueyama void ScriptParser::readVersionScript() { 1842ec34544SRui Ueyama readVersionScriptCommand(); 1852ec34544SRui Ueyama if (!atEOF()) 1862ec34544SRui Ueyama setError("EOF expected, but got " + next()); 1872ec34544SRui Ueyama } 1882ec34544SRui Ueyama 1892ec34544SRui Ueyama void ScriptParser::readVersionScriptCommand() { 1902ec34544SRui Ueyama if (consume("{")) { 1912ec34544SRui Ueyama readAnonymousDeclaration(); 1922ec34544SRui Ueyama return; 1932ec34544SRui Ueyama } 1942ec34544SRui Ueyama 195ce608081SGeorge Rimar while (!atEOF() && !ErrorCount && peek() != "}") { 1962ec34544SRui Ueyama StringRef VerStr = next(); 1972ec34544SRui Ueyama if (VerStr == "{") { 1982ec34544SRui Ueyama setError("anonymous version definition is used in " 1992ec34544SRui Ueyama "combination with other version definitions"); 2002ec34544SRui Ueyama return; 2012ec34544SRui Ueyama } 2022ec34544SRui Ueyama expect("{"); 2032ec34544SRui Ueyama readVersionDeclaration(VerStr); 2042ec34544SRui Ueyama } 2052ec34544SRui Ueyama } 2062ec34544SRui Ueyama 2072ec34544SRui Ueyama void ScriptParser::readVersion() { 2082ec34544SRui Ueyama expect("{"); 2092ec34544SRui Ueyama readVersionScriptCommand(); 2102ec34544SRui Ueyama expect("}"); 2112ec34544SRui Ueyama } 2122ec34544SRui Ueyama 2132ec34544SRui Ueyama void ScriptParser::readLinkerScript() { 2142ec34544SRui Ueyama while (!atEOF()) { 2152ec34544SRui Ueyama StringRef Tok = next(); 2162ec34544SRui Ueyama if (Tok == ";") 2172ec34544SRui Ueyama continue; 2182ec34544SRui Ueyama 2192ec34544SRui Ueyama if (Tok == "ASSERT") { 22023af89ccSRui Ueyama Script->Opt.Commands.push_back(readAssert()); 2212ec34544SRui Ueyama } else if (Tok == "ENTRY") { 2222ec34544SRui Ueyama readEntry(); 2232ec34544SRui Ueyama } else if (Tok == "EXTERN") { 2242ec34544SRui Ueyama readExtern(); 2252ec34544SRui Ueyama } else if (Tok == "GROUP" || Tok == "INPUT") { 2262ec34544SRui Ueyama readGroup(); 2272ec34544SRui Ueyama } else if (Tok == "INCLUDE") { 2282ec34544SRui Ueyama readInclude(); 2292ec34544SRui Ueyama } else if (Tok == "MEMORY") { 2302ec34544SRui Ueyama readMemory(); 2312ec34544SRui Ueyama } else if (Tok == "OUTPUT") { 2322ec34544SRui Ueyama readOutput(); 2332ec34544SRui Ueyama } else if (Tok == "OUTPUT_ARCH") { 2342ec34544SRui Ueyama readOutputArch(); 2352ec34544SRui Ueyama } else if (Tok == "OUTPUT_FORMAT") { 2362ec34544SRui Ueyama readOutputFormat(); 2372ec34544SRui Ueyama } else if (Tok == "PHDRS") { 2382ec34544SRui Ueyama readPhdrs(); 2392ec34544SRui Ueyama } else if (Tok == "SEARCH_DIR") { 2402ec34544SRui Ueyama readSearchDir(); 2412ec34544SRui Ueyama } else if (Tok == "SECTIONS") { 2422ec34544SRui Ueyama readSections(); 2432ec34544SRui Ueyama } else if (Tok == "VERSION") { 2442ec34544SRui Ueyama readVersion(); 2452ec34544SRui Ueyama } else if (SymbolAssignment *Cmd = readProvideOrAssignment(Tok)) { 2462ec34544SRui Ueyama Script->Opt.Commands.push_back(Cmd); 2472ec34544SRui Ueyama } else { 2482ec34544SRui Ueyama setError("unknown directive: " + Tok); 2492ec34544SRui Ueyama } 2502ec34544SRui Ueyama } 2512ec34544SRui Ueyama } 2522ec34544SRui Ueyama 2532ec34544SRui Ueyama void ScriptParser::addFile(StringRef S) { 2542ec34544SRui Ueyama if (IsUnderSysroot && S.startswith("/")) { 2552ec34544SRui Ueyama SmallString<128> PathData; 2562ec34544SRui Ueyama StringRef Path = (Config->Sysroot + S).toStringRef(PathData); 2572ec34544SRui Ueyama if (sys::fs::exists(Path)) { 258a76349bfSEvgeniy Stepanov Driver->addFile(Saver.save(Path), /*WithLOption=*/false); 2592ec34544SRui Ueyama return; 2602ec34544SRui Ueyama } 2612ec34544SRui Ueyama } 2622ec34544SRui Ueyama 263875ae82bSRui Ueyama if (S.startswith("/")) { 264a76349bfSEvgeniy Stepanov Driver->addFile(S, /*WithLOption=*/false); 2652ec34544SRui Ueyama } else if (S.startswith("=")) { 2662ec34544SRui Ueyama if (Config->Sysroot.empty()) 267a76349bfSEvgeniy Stepanov Driver->addFile(S.substr(1), /*WithLOption=*/false); 2682ec34544SRui Ueyama else 269a76349bfSEvgeniy Stepanov Driver->addFile(Saver.save(Config->Sysroot + "/" + S.substr(1)), 270a76349bfSEvgeniy Stepanov /*WithLOption=*/false); 2712ec34544SRui Ueyama } else if (S.startswith("-l")) { 2722ec34544SRui Ueyama Driver->addLibrary(S.substr(2)); 2732ec34544SRui Ueyama } else if (sys::fs::exists(S)) { 274a76349bfSEvgeniy Stepanov Driver->addFile(S, /*WithLOption=*/false); 2752ec34544SRui Ueyama } else { 2762ec34544SRui Ueyama if (Optional<std::string> Path = findFromSearchPaths(S)) 277a76349bfSEvgeniy Stepanov Driver->addFile(Saver.save(*Path), /*WithLOption=*/true); 2782ec34544SRui Ueyama else 2792ec34544SRui Ueyama setError("unable to find " + S); 2802ec34544SRui Ueyama } 2812ec34544SRui Ueyama } 2822ec34544SRui Ueyama 2832ec34544SRui Ueyama void ScriptParser::readAsNeeded() { 2842ec34544SRui Ueyama expect("("); 2852ec34544SRui Ueyama bool Orig = Config->AsNeeded; 2862ec34544SRui Ueyama Config->AsNeeded = true; 287ce608081SGeorge Rimar while (!ErrorCount && !consume(")")) 2882ec34544SRui Ueyama addFile(unquote(next())); 2892ec34544SRui Ueyama Config->AsNeeded = Orig; 2902ec34544SRui Ueyama } 2912ec34544SRui Ueyama 2922ec34544SRui Ueyama void ScriptParser::readEntry() { 2932ec34544SRui Ueyama // -e <symbol> takes predecence over ENTRY(<symbol>). 2942ec34544SRui Ueyama expect("("); 2952ec34544SRui Ueyama StringRef Tok = next(); 2962ec34544SRui Ueyama if (Config->Entry.empty()) 2972ec34544SRui Ueyama Config->Entry = Tok; 2982ec34544SRui Ueyama expect(")"); 2992ec34544SRui Ueyama } 3002ec34544SRui Ueyama 3012ec34544SRui Ueyama void ScriptParser::readExtern() { 3022ec34544SRui Ueyama expect("("); 303ce608081SGeorge Rimar while (!ErrorCount && !consume(")")) 3042ec34544SRui Ueyama Config->Undefined.push_back(next()); 3052ec34544SRui Ueyama } 3062ec34544SRui Ueyama 3072ec34544SRui Ueyama void ScriptParser::readGroup() { 3082ec34544SRui Ueyama expect("("); 309ce608081SGeorge Rimar while (!ErrorCount && !consume(")")) { 310b579c439SRui Ueyama if (consume("AS_NEEDED")) 3112ec34544SRui Ueyama readAsNeeded(); 3122ec34544SRui Ueyama else 313b579c439SRui Ueyama addFile(unquote(next())); 3142ec34544SRui Ueyama } 3152ec34544SRui Ueyama } 3162ec34544SRui Ueyama 3172ec34544SRui Ueyama void ScriptParser::readInclude() { 3182ec34544SRui Ueyama StringRef Tok = unquote(next()); 3192ec34544SRui Ueyama 3202ec34544SRui Ueyama // https://sourceware.org/binutils/docs/ld/File-Commands.html: 3212ec34544SRui Ueyama // The file will be searched for in the current directory, and in any 3222ec34544SRui Ueyama // directory specified with the -L option. 3232ec34544SRui Ueyama if (sys::fs::exists(Tok)) { 3242ec34544SRui Ueyama if (Optional<MemoryBufferRef> MB = readFile(Tok)) 3252ec34544SRui Ueyama tokenize(*MB); 3262ec34544SRui Ueyama return; 3272ec34544SRui Ueyama } 3282ec34544SRui Ueyama if (Optional<std::string> Path = findFromSearchPaths(Tok)) { 3292ec34544SRui Ueyama if (Optional<MemoryBufferRef> MB = readFile(*Path)) 3302ec34544SRui Ueyama tokenize(*MB); 3312ec34544SRui Ueyama return; 3322ec34544SRui Ueyama } 3332ec34544SRui Ueyama setError("cannot open " + Tok); 3342ec34544SRui Ueyama } 3352ec34544SRui Ueyama 3362ec34544SRui Ueyama void ScriptParser::readOutput() { 3372ec34544SRui Ueyama // -o <file> takes predecence over OUTPUT(<file>). 3382ec34544SRui Ueyama expect("("); 3392ec34544SRui Ueyama StringRef Tok = next(); 3402ec34544SRui Ueyama if (Config->OutputFile.empty()) 3412ec34544SRui Ueyama Config->OutputFile = unquote(Tok); 3422ec34544SRui Ueyama expect(")"); 3432ec34544SRui Ueyama } 3442ec34544SRui Ueyama 3452ec34544SRui Ueyama void ScriptParser::readOutputArch() { 3462ec34544SRui Ueyama // OUTPUT_ARCH is ignored for now. 3472ec34544SRui Ueyama expect("("); 348ce608081SGeorge Rimar while (!ErrorCount && !consume(")")) 3492ec34544SRui Ueyama skip(); 3502ec34544SRui Ueyama } 3512ec34544SRui Ueyama 3522ec34544SRui Ueyama void ScriptParser::readOutputFormat() { 3532ec34544SRui Ueyama // Error checking only for now. 3542ec34544SRui Ueyama expect("("); 3552ec34544SRui Ueyama skip(); 356b579c439SRui Ueyama if (consume(")")) 3572ec34544SRui Ueyama return; 358b579c439SRui Ueyama expect(","); 3592ec34544SRui Ueyama skip(); 3602ec34544SRui Ueyama expect(","); 3612ec34544SRui Ueyama skip(); 3622ec34544SRui Ueyama expect(")"); 3632ec34544SRui Ueyama } 3642ec34544SRui Ueyama 3652ec34544SRui Ueyama void ScriptParser::readPhdrs() { 3662ec34544SRui Ueyama expect("{"); 367ce608081SGeorge Rimar while (!ErrorCount && !consume("}")) { 3682ec34544SRui Ueyama Script->Opt.PhdrsCommands.push_back( 369b579c439SRui Ueyama {next(), PT_NULL, false, false, UINT_MAX, nullptr}); 3702ec34544SRui Ueyama 371b579c439SRui Ueyama PhdrsCommand &PhdrCmd = Script->Opt.PhdrsCommands.back(); 3722ec34544SRui Ueyama PhdrCmd.Type = readPhdrType(); 373b579c439SRui Ueyama 374ce608081SGeorge Rimar while (!ErrorCount && !consume(";")) { 375b579c439SRui Ueyama if (consume("FILEHDR")) 3762ec34544SRui Ueyama PhdrCmd.HasFilehdr = true; 377b579c439SRui Ueyama else if (consume("PHDRS")) 3782ec34544SRui Ueyama PhdrCmd.HasPhdrs = true; 379b579c439SRui Ueyama else if (consume("AT")) 3802ec34544SRui Ueyama PhdrCmd.LMAExpr = readParenExpr(); 381b579c439SRui Ueyama else if (consume("FLAGS")) 382b579c439SRui Ueyama PhdrCmd.Flags = readParenExpr()().getValue(); 383b579c439SRui Ueyama else 384b579c439SRui Ueyama setError("unexpected header attribute: " + next()); 385b579c439SRui Ueyama } 3862ec34544SRui Ueyama } 3872ec34544SRui Ueyama } 3882ec34544SRui Ueyama 3892ec34544SRui Ueyama void ScriptParser::readSearchDir() { 3902ec34544SRui Ueyama expect("("); 3912ec34544SRui Ueyama StringRef Tok = next(); 3922ec34544SRui Ueyama if (!Config->Nostdlib) 3932ec34544SRui Ueyama Config->SearchPaths.push_back(unquote(Tok)); 3942ec34544SRui Ueyama expect(")"); 3952ec34544SRui Ueyama } 3962ec34544SRui Ueyama 3972ec34544SRui Ueyama void ScriptParser::readSections() { 3982ec34544SRui Ueyama Script->Opt.HasSections = true; 399b579c439SRui Ueyama 4002ec34544SRui Ueyama // -no-rosegment is used to avoid placing read only non-executable sections in 4012ec34544SRui Ueyama // their own segment. We do the same if SECTIONS command is present in linker 4022ec34544SRui Ueyama // script. See comment for computeFlags(). 4032ec34544SRui Ueyama Config->SingleRoRx = true; 4042ec34544SRui Ueyama 4052ec34544SRui Ueyama expect("{"); 406ce608081SGeorge Rimar while (!ErrorCount && !consume("}")) { 4072ec34544SRui Ueyama StringRef Tok = next(); 4082ec34544SRui Ueyama BaseCommand *Cmd = readProvideOrAssignment(Tok); 4092ec34544SRui Ueyama if (!Cmd) { 4102ec34544SRui Ueyama if (Tok == "ASSERT") 41123af89ccSRui Ueyama Cmd = readAssert(); 4122ec34544SRui Ueyama else 4132ec34544SRui Ueyama Cmd = readOutputSectionDescription(Tok); 4142ec34544SRui Ueyama } 4152ec34544SRui Ueyama Script->Opt.Commands.push_back(Cmd); 4162ec34544SRui Ueyama } 4172ec34544SRui Ueyama } 4182ec34544SRui Ueyama 4192ec34544SRui Ueyama static int precedence(StringRef Op) { 4202ec34544SRui Ueyama return StringSwitch<int>(Op) 4212ec34544SRui Ueyama .Cases("*", "/", 5) 4222ec34544SRui Ueyama .Cases("+", "-", 4) 4232ec34544SRui Ueyama .Cases("<<", ">>", 3) 4242ec34544SRui Ueyama .Cases("<", "<=", ">", ">=", "==", "!=", 2) 4252ec34544SRui Ueyama .Cases("&", "|", 1) 4262ec34544SRui Ueyama .Default(-1); 4272ec34544SRui Ueyama } 4282ec34544SRui Ueyama 4292ec34544SRui Ueyama StringMatcher ScriptParser::readFilePatterns() { 4302ec34544SRui Ueyama std::vector<StringRef> V; 431ce608081SGeorge Rimar while (!ErrorCount && !consume(")")) 4322ec34544SRui Ueyama V.push_back(next()); 4332ec34544SRui Ueyama return StringMatcher(V); 4342ec34544SRui Ueyama } 4352ec34544SRui Ueyama 4362ec34544SRui Ueyama SortSectionPolicy ScriptParser::readSortKind() { 4372ec34544SRui Ueyama if (consume("SORT") || consume("SORT_BY_NAME")) 4382ec34544SRui Ueyama return SortSectionPolicy::Name; 4392ec34544SRui Ueyama if (consume("SORT_BY_ALIGNMENT")) 4402ec34544SRui Ueyama return SortSectionPolicy::Alignment; 4412ec34544SRui Ueyama if (consume("SORT_BY_INIT_PRIORITY")) 4422ec34544SRui Ueyama return SortSectionPolicy::Priority; 4432ec34544SRui Ueyama if (consume("SORT_NONE")) 4442ec34544SRui Ueyama return SortSectionPolicy::None; 4452ec34544SRui Ueyama return SortSectionPolicy::Default; 4462ec34544SRui Ueyama } 4472ec34544SRui Ueyama 44803fc8d1eSRui Ueyama // Reads SECTIONS command contents in the following form: 44903fc8d1eSRui Ueyama // 45003fc8d1eSRui Ueyama // <contents> ::= <elem>* 45103fc8d1eSRui Ueyama // <elem> ::= <exclude>? <glob-pattern> 45203fc8d1eSRui Ueyama // <exclude> ::= "EXCLUDE_FILE" "(" <glob-pattern>+ ")" 45303fc8d1eSRui Ueyama // 45403fc8d1eSRui Ueyama // For example, 45503fc8d1eSRui Ueyama // 45603fc8d1eSRui Ueyama // *(.foo EXCLUDE_FILE (a.o) .bar EXCLUDE_FILE (b.o) .baz) 45703fc8d1eSRui Ueyama // 45803fc8d1eSRui Ueyama // is parsed as ".foo", ".bar" with "a.o", and ".baz" with "b.o". 45903fc8d1eSRui Ueyama // The semantics of that is section .foo in any file, section .bar in 46003fc8d1eSRui Ueyama // any file but a.o, and section .baz in any file but b.o. 4612ec34544SRui Ueyama std::vector<SectionPattern> ScriptParser::readInputSectionsList() { 4622ec34544SRui Ueyama std::vector<SectionPattern> Ret; 463ce608081SGeorge Rimar while (!ErrorCount && peek() != ")") { 4642ec34544SRui Ueyama StringMatcher ExcludeFilePat; 4652ec34544SRui Ueyama if (consume("EXCLUDE_FILE")) { 4662ec34544SRui Ueyama expect("("); 4672ec34544SRui Ueyama ExcludeFilePat = readFilePatterns(); 4682ec34544SRui Ueyama } 4692ec34544SRui Ueyama 4702ec34544SRui Ueyama std::vector<StringRef> V; 471ce608081SGeorge Rimar while (!ErrorCount && peek() != ")" && peek() != "EXCLUDE_FILE") 4722ec34544SRui Ueyama V.push_back(next()); 4732ec34544SRui Ueyama 4742ec34544SRui Ueyama if (!V.empty()) 4752ec34544SRui Ueyama Ret.push_back({std::move(ExcludeFilePat), StringMatcher(V)}); 4762ec34544SRui Ueyama else 4772ec34544SRui Ueyama setError("section pattern is expected"); 4782ec34544SRui Ueyama } 4792ec34544SRui Ueyama return Ret; 4802ec34544SRui Ueyama } 4812ec34544SRui Ueyama 4822ec34544SRui Ueyama // Reads contents of "SECTIONS" directive. That directive contains a 4832ec34544SRui Ueyama // list of glob patterns for input sections. The grammar is as follows. 4842ec34544SRui Ueyama // 4852ec34544SRui Ueyama // <patterns> ::= <section-list> 4862ec34544SRui Ueyama // | <sort> "(" <section-list> ")" 4872ec34544SRui Ueyama // | <sort> "(" <sort> "(" <section-list> ")" ")" 4882ec34544SRui Ueyama // 4892ec34544SRui Ueyama // <sort> ::= "SORT" | "SORT_BY_NAME" | "SORT_BY_ALIGNMENT" 4902ec34544SRui Ueyama // | "SORT_BY_INIT_PRIORITY" | "SORT_NONE" 4912ec34544SRui Ueyama // 4922ec34544SRui Ueyama // <section-list> is parsed by readInputSectionsList(). 4932ec34544SRui Ueyama InputSectionDescription * 4942ec34544SRui Ueyama ScriptParser::readInputSectionRules(StringRef FilePattern) { 4952ec34544SRui Ueyama auto *Cmd = make<InputSectionDescription>(FilePattern); 4962ec34544SRui Ueyama expect("("); 4972ec34544SRui Ueyama 498ce608081SGeorge Rimar while (!ErrorCount && !consume(")")) { 4992ec34544SRui Ueyama SortSectionPolicy Outer = readSortKind(); 5002ec34544SRui Ueyama SortSectionPolicy Inner = SortSectionPolicy::Default; 5012ec34544SRui Ueyama std::vector<SectionPattern> V; 5022ec34544SRui Ueyama if (Outer != SortSectionPolicy::Default) { 5032ec34544SRui Ueyama expect("("); 5042ec34544SRui Ueyama Inner = readSortKind(); 5052ec34544SRui Ueyama if (Inner != SortSectionPolicy::Default) { 5062ec34544SRui Ueyama expect("("); 5072ec34544SRui Ueyama V = readInputSectionsList(); 5082ec34544SRui Ueyama expect(")"); 5092ec34544SRui Ueyama } else { 5102ec34544SRui Ueyama V = readInputSectionsList(); 5112ec34544SRui Ueyama } 5122ec34544SRui Ueyama expect(")"); 5132ec34544SRui Ueyama } else { 5142ec34544SRui Ueyama V = readInputSectionsList(); 5152ec34544SRui Ueyama } 5162ec34544SRui Ueyama 5172ec34544SRui Ueyama for (SectionPattern &Pat : V) { 5182ec34544SRui Ueyama Pat.SortInner = Inner; 5192ec34544SRui Ueyama Pat.SortOuter = Outer; 5202ec34544SRui Ueyama } 5212ec34544SRui Ueyama 5222ec34544SRui Ueyama std::move(V.begin(), V.end(), std::back_inserter(Cmd->SectionPatterns)); 5232ec34544SRui Ueyama } 5242ec34544SRui Ueyama return Cmd; 5252ec34544SRui Ueyama } 5262ec34544SRui Ueyama 5272ec34544SRui Ueyama InputSectionDescription * 5282ec34544SRui Ueyama ScriptParser::readInputSectionDescription(StringRef Tok) { 5292ec34544SRui Ueyama // Input section wildcard can be surrounded by KEEP. 5302ec34544SRui Ueyama // https://sourceware.org/binutils/docs/ld/Input-Section-Keep.html#Input-Section-Keep 5312ec34544SRui Ueyama if (Tok == "KEEP") { 5322ec34544SRui Ueyama expect("("); 5332ec34544SRui Ueyama StringRef FilePattern = next(); 5342ec34544SRui Ueyama InputSectionDescription *Cmd = readInputSectionRules(FilePattern); 5352ec34544SRui Ueyama expect(")"); 5362ec34544SRui Ueyama Script->Opt.KeptSections.push_back(Cmd); 5372ec34544SRui Ueyama return Cmd; 5382ec34544SRui Ueyama } 5392ec34544SRui Ueyama return readInputSectionRules(Tok); 5402ec34544SRui Ueyama } 5412ec34544SRui Ueyama 5422ec34544SRui Ueyama void ScriptParser::readSort() { 5432ec34544SRui Ueyama expect("("); 5442ec34544SRui Ueyama expect("CONSTRUCTORS"); 5452ec34544SRui Ueyama expect(")"); 5462ec34544SRui Ueyama } 5472ec34544SRui Ueyama 54823af89ccSRui Ueyama AssertCommand *ScriptParser::readAssert() { 54923af89ccSRui Ueyama return make<AssertCommand>(readAssertExpr()); 55023af89ccSRui Ueyama } 55123af89ccSRui Ueyama 55223af89ccSRui Ueyama Expr ScriptParser::readAssertExpr() { 5532ec34544SRui Ueyama expect("("); 5542ec34544SRui Ueyama Expr E = readExpr(); 5552ec34544SRui Ueyama expect(","); 5562ec34544SRui Ueyama StringRef Msg = unquote(next()); 5572ec34544SRui Ueyama expect(")"); 558b579c439SRui Ueyama 5592ec34544SRui Ueyama return [=] { 5602ec34544SRui Ueyama if (!E().getValue()) 5612ec34544SRui Ueyama error(Msg); 5622ec34544SRui Ueyama return Script->getDot(); 5632ec34544SRui Ueyama }; 5642ec34544SRui Ueyama } 5652ec34544SRui Ueyama 5662ec34544SRui Ueyama // Reads a FILL(expr) command. We handle the FILL command as an 5672ec34544SRui Ueyama // alias for =fillexp section attribute, which is different from 5682ec34544SRui Ueyama // what GNU linkers do. 5692ec34544SRui Ueyama // https://sourceware.org/binutils/docs/ld/Output-Section-Data.html 5702ec34544SRui Ueyama uint32_t ScriptParser::readFill() { 5712ec34544SRui Ueyama expect("("); 5728acbf1ccSRui Ueyama uint32_t V = parseFill(next()); 5732ec34544SRui Ueyama expect(")"); 5742ec34544SRui Ueyama return V; 5752ec34544SRui Ueyama } 5762ec34544SRui Ueyama 5773271d370SRui Ueyama // Reads an expression and/or the special directive "(NOLOAD)" for an 5783271d370SRui Ueyama // output section definition. 5793271d370SRui Ueyama // 5803271d370SRui Ueyama // An output section name can be followed by an address expression 5813271d370SRui Ueyama // and/or by "(NOLOAD)". This grammar is not LL(1) because "(" can be 5823271d370SRui Ueyama // interpreted as either the beginning of some expression or "(NOLOAD)". 5833271d370SRui Ueyama // 584b579c439SRui Ueyama // https://sourceware.org/binutils/docs/ld/Output-Section-Address.html 585fbb0463fSGeorge Rimar // https://sourceware.org/binutils/docs/ld/Output-Section-Type.html 5868c022ca7SRafael Espindola void ScriptParser::readSectionAddressType(OutputSection *Cmd) { 5873271d370SRui Ueyama if (consume("(")) { 5883271d370SRui Ueyama if (consume("NOLOAD")) { 5893271d370SRui Ueyama expect(")"); 5903271d370SRui Ueyama Cmd->Noload = true; 5913271d370SRui Ueyama return; 5923271d370SRui Ueyama } 5933271d370SRui Ueyama Cmd->AddrExpr = readExpr(); 5943271d370SRui Ueyama expect(")"); 5953271d370SRui Ueyama } else { 5963271d370SRui Ueyama Cmd->AddrExpr = readExpr(); 5973271d370SRui Ueyama } 5983271d370SRui Ueyama 599fbb0463fSGeorge Rimar if (consume("(")) { 600fbb0463fSGeorge Rimar expect("NOLOAD"); 601fbb0463fSGeorge Rimar expect(")"); 602fbb0463fSGeorge Rimar Cmd->Noload = true; 603fbb0463fSGeorge Rimar } 604fbb0463fSGeorge Rimar } 605fbb0463fSGeorge Rimar 6068c022ca7SRafael Espindola OutputSection *ScriptParser::readOutputSectionDescription(StringRef OutSec) { 6078c022ca7SRafael Espindola OutputSection *Cmd = 6088c022ca7SRafael Espindola Script->createOutputSection(OutSec, getCurrentLocation()); 6093271d370SRui Ueyama 6103271d370SRui Ueyama if (peek() != ":") 6113271d370SRui Ueyama readSectionAddressType(Cmd); 6122ec34544SRui Ueyama expect(":"); 6132ec34544SRui Ueyama 6142ec34544SRui Ueyama if (consume("AT")) 6152ec34544SRui Ueyama Cmd->LMAExpr = readParenExpr(); 6162ec34544SRui Ueyama if (consume("ALIGN")) 6172ec34544SRui Ueyama Cmd->AlignExpr = readParenExpr(); 6182ec34544SRui Ueyama if (consume("SUBALIGN")) 6192ec34544SRui Ueyama Cmd->SubalignExpr = readParenExpr(); 6202ec34544SRui Ueyama 6212ec34544SRui Ueyama // Parse constraints. 6222ec34544SRui Ueyama if (consume("ONLY_IF_RO")) 6232ec34544SRui Ueyama Cmd->Constraint = ConstraintKind::ReadOnly; 6242ec34544SRui Ueyama if (consume("ONLY_IF_RW")) 6252ec34544SRui Ueyama Cmd->Constraint = ConstraintKind::ReadWrite; 6262ec34544SRui Ueyama expect("{"); 6272ec34544SRui Ueyama 628ce608081SGeorge Rimar while (!ErrorCount && !consume("}")) { 6292ec34544SRui Ueyama StringRef Tok = next(); 6302ec34544SRui Ueyama if (Tok == ";") { 6312ec34544SRui Ueyama // Empty commands are allowed. Do nothing here. 632b579c439SRui Ueyama } else if (SymbolAssignment *Assign = readProvideOrAssignment(Tok)) { 633b579c439SRui Ueyama Cmd->Commands.push_back(Assign); 6342ec34544SRui Ueyama } else if (BytesDataCommand *Data = readBytesDataCommand(Tok)) { 6352ec34544SRui Ueyama Cmd->Commands.push_back(Data); 6362ec34544SRui Ueyama } else if (Tok == "ASSERT") { 63723af89ccSRui Ueyama Cmd->Commands.push_back(readAssert()); 6382ec34544SRui Ueyama expect(";"); 6392ec34544SRui Ueyama } else if (Tok == "CONSTRUCTORS") { 6402ec34544SRui Ueyama // CONSTRUCTORS is a keyword to make the linker recognize C++ ctors/dtors 6412ec34544SRui Ueyama // by name. This is for very old file formats such as ECOFF/XCOFF. 6422ec34544SRui Ueyama // For ELF, we should ignore. 6432ec34544SRui Ueyama } else if (Tok == "FILL") { 6442ec34544SRui Ueyama Cmd->Filler = readFill(); 6452ec34544SRui Ueyama } else if (Tok == "SORT") { 6462ec34544SRui Ueyama readSort(); 6472ec34544SRui Ueyama } else if (peek() == "(") { 6482ec34544SRui Ueyama Cmd->Commands.push_back(readInputSectionDescription(Tok)); 6492ec34544SRui Ueyama } else { 6502ec34544SRui Ueyama setError("unknown command " + Tok); 6512ec34544SRui Ueyama } 6522ec34544SRui Ueyama } 6532ec34544SRui Ueyama 6542ec34544SRui Ueyama if (consume(">")) 6552ec34544SRui Ueyama Cmd->MemoryRegionName = next(); 656b0e62297SMeador Inge else if (peek().startswith(">")) 657b0e62297SMeador Inge Cmd->MemoryRegionName = next().drop_front(); 6582ec34544SRui Ueyama 6592ec34544SRui Ueyama Cmd->Phdrs = readOutputSectionPhdrs(); 6602ec34544SRui Ueyama 6612ec34544SRui Ueyama if (consume("=")) 6628acbf1ccSRui Ueyama Cmd->Filler = parseFill(next()); 6632ec34544SRui Ueyama else if (peek().startswith("=")) 6648acbf1ccSRui Ueyama Cmd->Filler = parseFill(next().drop_front()); 6652ec34544SRui Ueyama 6662ec34544SRui Ueyama // Consume optional comma following output section command. 6672ec34544SRui Ueyama consume(","); 6682ec34544SRui Ueyama 6692ec34544SRui Ueyama return Cmd; 6702ec34544SRui Ueyama } 6712ec34544SRui Ueyama 6728acbf1ccSRui Ueyama // Parses a given string as a octal/decimal/hexadecimal number and 6738acbf1ccSRui Ueyama // returns it as a big-endian number. Used for `=<fillexp>`. 6742ec34544SRui Ueyama // https://sourceware.org/binutils/docs/ld/Output-Section-Fill.html 6752ec34544SRui Ueyama // 6768acbf1ccSRui Ueyama // When reading a hexstring, ld.bfd handles it as a blob of arbitrary 6778acbf1ccSRui Ueyama // size, while ld.gold always handles it as a 32-bit big-endian number. 6788acbf1ccSRui Ueyama // We are compatible with ld.gold because it's easier to implement. 6798acbf1ccSRui Ueyama uint32_t ScriptParser::parseFill(StringRef Tok) { 680b58079d4SRui Ueyama uint32_t V = 0; 681ab94768cSGeorge Rimar if (!to_integer(Tok, V)) 6822ec34544SRui Ueyama setError("invalid filler expression: " + Tok); 683b58079d4SRui Ueyama 684b58079d4SRui Ueyama uint32_t Buf; 685b58079d4SRui Ueyama write32be(&Buf, V); 686b58079d4SRui Ueyama return Buf; 6872ec34544SRui Ueyama } 6882ec34544SRui Ueyama 6892ec34544SRui Ueyama SymbolAssignment *ScriptParser::readProvideHidden(bool Provide, bool Hidden) { 6902ec34544SRui Ueyama expect("("); 6912ec34544SRui Ueyama SymbolAssignment *Cmd = readAssignment(next()); 6922ec34544SRui Ueyama Cmd->Provide = Provide; 6932ec34544SRui Ueyama Cmd->Hidden = Hidden; 6942ec34544SRui Ueyama expect(")"); 6952ec34544SRui Ueyama expect(";"); 6962ec34544SRui Ueyama return Cmd; 6972ec34544SRui Ueyama } 6982ec34544SRui Ueyama 6992ec34544SRui Ueyama SymbolAssignment *ScriptParser::readProvideOrAssignment(StringRef Tok) { 7002ec34544SRui Ueyama SymbolAssignment *Cmd = nullptr; 7012ec34544SRui Ueyama if (peek() == "=" || peek() == "+=") { 7022ec34544SRui Ueyama Cmd = readAssignment(Tok); 7032ec34544SRui Ueyama expect(";"); 7042ec34544SRui Ueyama } else if (Tok == "PROVIDE") { 7052ec34544SRui Ueyama Cmd = readProvideHidden(true, false); 7062ec34544SRui Ueyama } else if (Tok == "HIDDEN") { 7072ec34544SRui Ueyama Cmd = readProvideHidden(false, true); 7082ec34544SRui Ueyama } else if (Tok == "PROVIDE_HIDDEN") { 7092ec34544SRui Ueyama Cmd = readProvideHidden(true, true); 7102ec34544SRui Ueyama } 7112ec34544SRui Ueyama return Cmd; 7122ec34544SRui Ueyama } 7132ec34544SRui Ueyama 7142ec34544SRui Ueyama SymbolAssignment *ScriptParser::readAssignment(StringRef Name) { 7152ec34544SRui Ueyama StringRef Op = next(); 7162ec34544SRui Ueyama assert(Op == "=" || Op == "+="); 7172ec34544SRui Ueyama Expr E = readExpr(); 7182ec34544SRui Ueyama if (Op == "+=") { 7192ec34544SRui Ueyama std::string Loc = getCurrentLocation(); 7202ec34544SRui Ueyama E = [=] { return add(Script->getSymbolValue(Loc, Name), E()); }; 7212ec34544SRui Ueyama } 7222ec34544SRui Ueyama return make<SymbolAssignment>(Name, E, getCurrentLocation()); 7232ec34544SRui Ueyama } 7242ec34544SRui Ueyama 7252ec34544SRui Ueyama // This is an operator-precedence parser to parse a linker 7262ec34544SRui Ueyama // script expression. 7272ec34544SRui Ueyama Expr ScriptParser::readExpr() { 7282ec34544SRui Ueyama // Our lexer is context-aware. Set the in-expression bit so that 7292ec34544SRui Ueyama // they apply different tokenization rules. 7302ec34544SRui Ueyama bool Orig = InExpr; 7312ec34544SRui Ueyama InExpr = true; 7322ec34544SRui Ueyama Expr E = readExpr1(readPrimary(), 0); 7332ec34544SRui Ueyama InExpr = Orig; 7342ec34544SRui Ueyama return E; 7352ec34544SRui Ueyama } 7362ec34544SRui Ueyama 7372ec34544SRui Ueyama static Expr combine(StringRef Op, Expr L, Expr R) { 7382ec34544SRui Ueyama if (Op == "+") 7392ec34544SRui Ueyama return [=] { return add(L(), R()); }; 7402ec34544SRui Ueyama if (Op == "-") 7412ec34544SRui Ueyama return [=] { return sub(L(), R()); }; 742b579c439SRui Ueyama if (Op == "*") 743b579c439SRui Ueyama return [=] { return mul(L(), R()); }; 744b579c439SRui Ueyama if (Op == "/") 745b579c439SRui Ueyama return [=] { return div(L(), R()); }; 7462ec34544SRui Ueyama if (Op == "<<") 7477e915511SRui Ueyama return [=] { return L().getValue() << R().getValue(); }; 7482ec34544SRui Ueyama if (Op == ">>") 7497e915511SRui Ueyama return [=] { return L().getValue() >> R().getValue(); }; 7502ec34544SRui Ueyama if (Op == "<") 7512ec34544SRui Ueyama return [=] { return L().getValue() < R().getValue(); }; 7522ec34544SRui Ueyama if (Op == ">") 7532ec34544SRui Ueyama return [=] { return L().getValue() > R().getValue(); }; 7542ec34544SRui Ueyama if (Op == ">=") 7552ec34544SRui Ueyama return [=] { return L().getValue() >= R().getValue(); }; 7562ec34544SRui Ueyama if (Op == "<=") 7572ec34544SRui Ueyama return [=] { return L().getValue() <= R().getValue(); }; 7582ec34544SRui Ueyama if (Op == "==") 7592ec34544SRui Ueyama return [=] { return L().getValue() == R().getValue(); }; 7602ec34544SRui Ueyama if (Op == "!=") 7612ec34544SRui Ueyama return [=] { return L().getValue() != R().getValue(); }; 7622ec34544SRui Ueyama if (Op == "&") 7632ec34544SRui Ueyama return [=] { return bitAnd(L(), R()); }; 7642ec34544SRui Ueyama if (Op == "|") 7652ec34544SRui Ueyama return [=] { return bitOr(L(), R()); }; 7662ec34544SRui Ueyama llvm_unreachable("invalid operator"); 7672ec34544SRui Ueyama } 7682ec34544SRui Ueyama 7692ec34544SRui Ueyama // This is a part of the operator-precedence parser. This function 7702ec34544SRui Ueyama // assumes that the remaining token stream starts with an operator. 7712ec34544SRui Ueyama Expr ScriptParser::readExpr1(Expr Lhs, int MinPrec) { 772ce608081SGeorge Rimar while (!atEOF() && !ErrorCount) { 7732ec34544SRui Ueyama // Read an operator and an expression. 7742ec34544SRui Ueyama if (consume("?")) 7752ec34544SRui Ueyama return readTernary(Lhs); 7762ec34544SRui Ueyama StringRef Op1 = peek(); 7772ec34544SRui Ueyama if (precedence(Op1) < MinPrec) 7782ec34544SRui Ueyama break; 7792ec34544SRui Ueyama skip(); 7802ec34544SRui Ueyama Expr Rhs = readPrimary(); 7812ec34544SRui Ueyama 7822ec34544SRui Ueyama // Evaluate the remaining part of the expression first if the 7832ec34544SRui Ueyama // next operator has greater precedence than the previous one. 7842ec34544SRui Ueyama // For example, if we have read "+" and "3", and if the next 7852ec34544SRui Ueyama // operator is "*", then we'll evaluate 3 * ... part first. 7862ec34544SRui Ueyama while (!atEOF()) { 7872ec34544SRui Ueyama StringRef Op2 = peek(); 7882ec34544SRui Ueyama if (precedence(Op2) <= precedence(Op1)) 7892ec34544SRui Ueyama break; 7902ec34544SRui Ueyama Rhs = readExpr1(Rhs, precedence(Op2)); 7912ec34544SRui Ueyama } 7922ec34544SRui Ueyama 7932ec34544SRui Ueyama Lhs = combine(Op1, Lhs, Rhs); 7942ec34544SRui Ueyama } 7952ec34544SRui Ueyama return Lhs; 7962ec34544SRui Ueyama } 7972ec34544SRui Ueyama 7985fb17128SGeorge Rimar Expr ScriptParser::getPageSize() { 7995fb17128SGeorge Rimar std::string Location = getCurrentLocation(); 8005fb17128SGeorge Rimar return [=]() -> uint64_t { 8015fb17128SGeorge Rimar if (Target) 8022ec34544SRui Ueyama return Target->PageSize; 8035fb17128SGeorge Rimar error(Location + ": unable to calculate page size"); 8045fb17128SGeorge Rimar return 4096; // Return a dummy value. 8055fb17128SGeorge Rimar }; 8065fb17128SGeorge Rimar } 8075fb17128SGeorge Rimar 8085fb17128SGeorge Rimar Expr ScriptParser::readConstant() { 8095fb17128SGeorge Rimar StringRef S = readParenLiteral(); 8105fb17128SGeorge Rimar if (S == "COMMONPAGESIZE") 8115fb17128SGeorge Rimar return getPageSize(); 8122ec34544SRui Ueyama if (S == "MAXPAGESIZE") 8135fb17128SGeorge Rimar return [] { return Config->MaxPageSize; }; 8145fb17128SGeorge Rimar setError("unknown constant: " + S); 8155fb17128SGeorge Rimar return {}; 8162ec34544SRui Ueyama } 8172ec34544SRui Ueyama 8185c65088fSRui Ueyama // Parses Tok as an integer. It recognizes hexadecimal (prefixed with 8195c65088fSRui Ueyama // "0x" or suffixed with "H") and decimal numbers. Decimal numbers may 8205c65088fSRui Ueyama // have "K" (Ki) or "M" (Mi) suffixes. 8215c65088fSRui Ueyama static Optional<uint64_t> parseInt(StringRef Tok) { 8222ec34544SRui Ueyama // Negative number 8232ec34544SRui Ueyama if (Tok.startswith("-")) { 8245c65088fSRui Ueyama if (Optional<uint64_t> Val = parseInt(Tok.substr(1))) 8255c65088fSRui Ueyama return -*Val; 8265c65088fSRui Ueyama return None; 8272ec34544SRui Ueyama } 8282ec34544SRui Ueyama 8292ec34544SRui Ueyama // Hexadecimal 8305c65088fSRui Ueyama uint64_t Val; 831ab94768cSGeorge Rimar if (Tok.startswith_lower("0x") && to_integer(Tok.substr(2), Val, 16)) 8325c65088fSRui Ueyama return Val; 833ab94768cSGeorge Rimar if (Tok.endswith_lower("H") && to_integer(Tok.drop_back(), Val, 16)) 8345c65088fSRui Ueyama return Val; 8352ec34544SRui Ueyama 8362ec34544SRui Ueyama // Decimal 8372ec34544SRui Ueyama if (Tok.endswith_lower("K")) { 838ab94768cSGeorge Rimar if (!to_integer(Tok.drop_back(), Val, 10)) 8395c65088fSRui Ueyama return None; 8405c65088fSRui Ueyama return Val * 1024; 8412ec34544SRui Ueyama } 8425c65088fSRui Ueyama if (Tok.endswith_lower("M")) { 843ab94768cSGeorge Rimar if (!to_integer(Tok.drop_back(), Val, 10)) 8445c65088fSRui Ueyama return None; 8455c65088fSRui Ueyama return Val * 1024 * 1024; 8465c65088fSRui Ueyama } 847ab94768cSGeorge Rimar if (!to_integer(Tok, Val, 10)) 8485c65088fSRui Ueyama return None; 8495c65088fSRui Ueyama return Val; 8502ec34544SRui Ueyama } 8512ec34544SRui Ueyama 8522ec34544SRui Ueyama BytesDataCommand *ScriptParser::readBytesDataCommand(StringRef Tok) { 853b579c439SRui Ueyama int Size = StringSwitch<int>(Tok) 8542ec34544SRui Ueyama .Case("BYTE", 1) 8552ec34544SRui Ueyama .Case("SHORT", 2) 8562ec34544SRui Ueyama .Case("LONG", 4) 8572ec34544SRui Ueyama .Case("QUAD", 8) 8582ec34544SRui Ueyama .Default(-1); 8592ec34544SRui Ueyama if (Size == -1) 8602ec34544SRui Ueyama return nullptr; 8612ec34544SRui Ueyama 8622ec34544SRui Ueyama return make<BytesDataCommand>(readParenExpr(), Size); 8632ec34544SRui Ueyama } 8642ec34544SRui Ueyama 8652ec34544SRui Ueyama StringRef ScriptParser::readParenLiteral() { 8662ec34544SRui Ueyama expect("("); 8672ec34544SRui Ueyama StringRef Tok = next(); 8682ec34544SRui Ueyama expect(")"); 8692ec34544SRui Ueyama return Tok; 8702ec34544SRui Ueyama } 8712ec34544SRui Ueyama 8728c022ca7SRafael Espindola OutputSection *ScriptParser::checkSection(OutputSection *Cmd, 87305c4f67cSRafael Espindola StringRef Location) { 87405c4f67cSRafael Espindola if (Cmd->Location.empty() && Script->ErrorOnMissingSection) 87505c4f67cSRafael Espindola error(Location + ": undefined section " + Cmd->Name); 8768c022ca7SRafael Espindola return Cmd; 87705c4f67cSRafael Espindola } 87805c4f67cSRafael Espindola 8792ec34544SRui Ueyama Expr ScriptParser::readPrimary() { 8802ec34544SRui Ueyama if (peek() == "(") 8812ec34544SRui Ueyama return readParenExpr(); 8822ec34544SRui Ueyama 8835c65088fSRui Ueyama if (consume("~")) { 8842ec34544SRui Ueyama Expr E = readPrimary(); 885b2fb84a1SRui Ueyama return [=] { return ~E().getValue(); }; 8862ec34544SRui Ueyama } 8876f1d954eSHafiz Abid Qadeer if (consume("!")) { 8886f1d954eSHafiz Abid Qadeer Expr E = readPrimary(); 8896f1d954eSHafiz Abid Qadeer return [=] { return !E().getValue(); }; 8906f1d954eSHafiz Abid Qadeer } 8915c65088fSRui Ueyama if (consume("-")) { 8922ec34544SRui Ueyama Expr E = readPrimary(); 893b2fb84a1SRui Ueyama return [=] { return -E().getValue(); }; 8942ec34544SRui Ueyama } 8952ec34544SRui Ueyama 8965c65088fSRui Ueyama StringRef Tok = next(); 8975c65088fSRui Ueyama std::string Location = getCurrentLocation(); 8985c65088fSRui Ueyama 8992ec34544SRui Ueyama // Built-in functions are parsed here. 9002ec34544SRui Ueyama // https://sourceware.org/binutils/docs/ld/Builtin-Functions.html. 9012ec34544SRui Ueyama if (Tok == "ABSOLUTE") { 9022ec34544SRui Ueyama Expr Inner = readParenExpr(); 9032ec34544SRui Ueyama return [=] { 9042ec34544SRui Ueyama ExprValue I = Inner(); 9052ec34544SRui Ueyama I.ForceAbsolute = true; 9062ec34544SRui Ueyama return I; 9072ec34544SRui Ueyama }; 9082ec34544SRui Ueyama } 9092ec34544SRui Ueyama if (Tok == "ADDR") { 9102ec34544SRui Ueyama StringRef Name = readParenLiteral(); 9118c022ca7SRafael Espindola OutputSection *Cmd = Script->getOrCreateOutputSection(Name); 91241c7ab4aSGeorge Rimar return [=]() -> ExprValue { 91341c7ab4aSGeorge Rimar return {checkSection(Cmd, Location), 0, Location}; 91441c7ab4aSGeorge Rimar }; 9152ec34544SRui Ueyama } 9162ec34544SRui Ueyama if (Tok == "ALIGN") { 9172ec34544SRui Ueyama expect("("); 9182ec34544SRui Ueyama Expr E = readExpr(); 919b579c439SRui Ueyama if (consume(")")) 92060833f6eSGeorge Rimar return [=] { 92160833f6eSGeorge Rimar return alignTo(Script->getDot(), std::max((uint64_t)1, E().getValue())); 92260833f6eSGeorge Rimar }; 923b579c439SRui Ueyama expect(","); 9242ec34544SRui Ueyama Expr E2 = readExpr(); 9252ec34544SRui Ueyama expect(")"); 9263c6de1a6SPetr Hosek return [=] { 9273c6de1a6SPetr Hosek ExprValue V = E(); 92860833f6eSGeorge Rimar V.Alignment = std::max((uint64_t)1, E2().getValue()); 9293c6de1a6SPetr Hosek return V; 9303c6de1a6SPetr Hosek }; 9312ec34544SRui Ueyama } 9322ec34544SRui Ueyama if (Tok == "ALIGNOF") { 9332ec34544SRui Ueyama StringRef Name = readParenLiteral(); 9348c022ca7SRafael Espindola OutputSection *Cmd = Script->getOrCreateOutputSection(Name); 93505c4f67cSRafael Espindola return [=] { return checkSection(Cmd, Location)->Alignment; }; 9362ec34544SRui Ueyama } 9372ec34544SRui Ueyama if (Tok == "ASSERT") 93823af89ccSRui Ueyama return readAssertExpr(); 9395fb17128SGeorge Rimar if (Tok == "CONSTANT") 9405fb17128SGeorge Rimar return readConstant(); 9412ec34544SRui Ueyama if (Tok == "DATA_SEGMENT_ALIGN") { 9422ec34544SRui Ueyama expect("("); 9432ec34544SRui Ueyama Expr E = readExpr(); 9442ec34544SRui Ueyama expect(","); 9452ec34544SRui Ueyama readExpr(); 9462ec34544SRui Ueyama expect(")"); 94760833f6eSGeorge Rimar return [=] { 94860833f6eSGeorge Rimar return alignTo(Script->getDot(), std::max((uint64_t)1, E().getValue())); 94960833f6eSGeorge Rimar }; 9502ec34544SRui Ueyama } 9512ec34544SRui Ueyama if (Tok == "DATA_SEGMENT_END") { 9522ec34544SRui Ueyama expect("("); 9532ec34544SRui Ueyama expect("."); 9542ec34544SRui Ueyama expect(")"); 9552ec34544SRui Ueyama return [] { return Script->getDot(); }; 9562ec34544SRui Ueyama } 9572ec34544SRui Ueyama if (Tok == "DATA_SEGMENT_RELRO_END") { 9582ec34544SRui Ueyama // GNU linkers implements more complicated logic to handle 9592ec34544SRui Ueyama // DATA_SEGMENT_RELRO_END. We instead ignore the arguments and 9602ec34544SRui Ueyama // just align to the next page boundary for simplicity. 9612ec34544SRui Ueyama expect("("); 9622ec34544SRui Ueyama readExpr(); 9632ec34544SRui Ueyama expect(","); 9642ec34544SRui Ueyama readExpr(); 9652ec34544SRui Ueyama expect(")"); 9665fb17128SGeorge Rimar Expr E = getPageSize(); 9675fb17128SGeorge Rimar return [=] { return alignTo(Script->getDot(), E().getValue()); }; 9682ec34544SRui Ueyama } 9692ec34544SRui Ueyama if (Tok == "DEFINED") { 9702ec34544SRui Ueyama StringRef Name = readParenLiteral(); 9712ec34544SRui Ueyama return [=] { return Script->isDefined(Name) ? 1 : 0; }; 9722ec34544SRui Ueyama } 97391b95b61SRui Ueyama if (Tok == "LENGTH") { 97491b95b61SRui Ueyama StringRef Name = readParenLiteral(); 97591b95b61SRui Ueyama if (Script->Opt.MemoryRegions.count(Name) == 0) 97691b95b61SRui Ueyama setError("memory region not defined: " + Name); 97791b95b61SRui Ueyama return [=] { return Script->Opt.MemoryRegions[Name].Length; }; 97891b95b61SRui Ueyama } 9792ec34544SRui Ueyama if (Tok == "LOADADDR") { 9802ec34544SRui Ueyama StringRef Name = readParenLiteral(); 9818c022ca7SRafael Espindola OutputSection *Cmd = Script->getOrCreateOutputSection(Name); 98205c4f67cSRafael Espindola return [=] { return checkSection(Cmd, Location)->getLMA(); }; 9832ec34544SRui Ueyama } 98491b95b61SRui Ueyama if (Tok == "ORIGIN") { 98591b95b61SRui Ueyama StringRef Name = readParenLiteral(); 98691b95b61SRui Ueyama if (Script->Opt.MemoryRegions.count(Name) == 0) 98791b95b61SRui Ueyama setError("memory region not defined: " + Name); 98891b95b61SRui Ueyama return [=] { return Script->Opt.MemoryRegions[Name].Origin; }; 98991b95b61SRui Ueyama } 9902ec34544SRui Ueyama if (Tok == "SEGMENT_START") { 9912ec34544SRui Ueyama expect("("); 9922ec34544SRui Ueyama skip(); 9932ec34544SRui Ueyama expect(","); 9942ec34544SRui Ueyama Expr E = readExpr(); 9952ec34544SRui Ueyama expect(")"); 9962ec34544SRui Ueyama return [=] { return E(); }; 9972ec34544SRui Ueyama } 9982ec34544SRui Ueyama if (Tok == "SIZEOF") { 9992ec34544SRui Ueyama StringRef Name = readParenLiteral(); 10008c022ca7SRafael Espindola OutputSection *Cmd = Script->getOrCreateOutputSection(Name); 100105c4f67cSRafael Espindola // Linker script does not create an output section if its content is empty. 100205c4f67cSRafael Espindola // We want to allow SIZEOF(.foo) where .foo is a section which happened to 100305c4f67cSRafael Espindola // be empty. 10048c022ca7SRafael Espindola return [=] { return Cmd->Size; }; 10052ec34544SRui Ueyama } 10062ec34544SRui Ueyama if (Tok == "SIZEOF_HEADERS") 10072ec34544SRui Ueyama return [=] { return elf::getHeaderSize(); }; 10082ec34544SRui Ueyama 10094eb2eccbSRui Ueyama // Tok is the dot. 10104eb2eccbSRui Ueyama if (Tok == ".") 10114eb2eccbSRui Ueyama return [=] { return Script->getSymbolValue(Location, Tok); }; 10124eb2eccbSRui Ueyama 10132ec34544SRui Ueyama // Tok is a literal number. 10145c65088fSRui Ueyama if (Optional<uint64_t> Val = parseInt(Tok)) 10155c65088fSRui Ueyama return [=] { return *Val; }; 10162ec34544SRui Ueyama 10172ec34544SRui Ueyama // Tok is a symbol name. 10182ec34544SRui Ueyama if (!isValidCIdentifier(Tok)) 10192ec34544SRui Ueyama setError("malformed number: " + Tok); 10204eb2eccbSRui Ueyama Script->Opt.ReferencedSymbols.push_back(Tok); 10212ec34544SRui Ueyama return [=] { return Script->getSymbolValue(Location, Tok); }; 10222ec34544SRui Ueyama } 10232ec34544SRui Ueyama 10242ec34544SRui Ueyama Expr ScriptParser::readTernary(Expr Cond) { 10252ec34544SRui Ueyama Expr L = readExpr(); 10262ec34544SRui Ueyama expect(":"); 10272ec34544SRui Ueyama Expr R = readExpr(); 10282ec34544SRui Ueyama return [=] { return Cond().getValue() ? L() : R(); }; 10292ec34544SRui Ueyama } 10302ec34544SRui Ueyama 10312ec34544SRui Ueyama Expr ScriptParser::readParenExpr() { 10322ec34544SRui Ueyama expect("("); 10332ec34544SRui Ueyama Expr E = readExpr(); 10342ec34544SRui Ueyama expect(")"); 10352ec34544SRui Ueyama return E; 10362ec34544SRui Ueyama } 10372ec34544SRui Ueyama 10382ec34544SRui Ueyama std::vector<StringRef> ScriptParser::readOutputSectionPhdrs() { 10392ec34544SRui Ueyama std::vector<StringRef> Phdrs; 1040ce608081SGeorge Rimar while (!ErrorCount && peek().startswith(":")) { 10412ec34544SRui Ueyama StringRef Tok = next(); 10422ec34544SRui Ueyama Phdrs.push_back((Tok.size() == 1) ? next() : Tok.substr(1)); 10432ec34544SRui Ueyama } 10442ec34544SRui Ueyama return Phdrs; 10452ec34544SRui Ueyama } 10462ec34544SRui Ueyama 10472ec34544SRui Ueyama // Read a program header type name. The next token must be a 10482ec34544SRui Ueyama // name of a program header type or a constant (e.g. "0x3"). 10492ec34544SRui Ueyama unsigned ScriptParser::readPhdrType() { 10502ec34544SRui Ueyama StringRef Tok = next(); 10515c65088fSRui Ueyama if (Optional<uint64_t> Val = parseInt(Tok)) 10525c65088fSRui Ueyama return *Val; 10532ec34544SRui Ueyama 10542ec34544SRui Ueyama unsigned Ret = StringSwitch<unsigned>(Tok) 10552ec34544SRui Ueyama .Case("PT_NULL", PT_NULL) 10562ec34544SRui Ueyama .Case("PT_LOAD", PT_LOAD) 10572ec34544SRui Ueyama .Case("PT_DYNAMIC", PT_DYNAMIC) 10582ec34544SRui Ueyama .Case("PT_INTERP", PT_INTERP) 10592ec34544SRui Ueyama .Case("PT_NOTE", PT_NOTE) 10602ec34544SRui Ueyama .Case("PT_SHLIB", PT_SHLIB) 10612ec34544SRui Ueyama .Case("PT_PHDR", PT_PHDR) 10622ec34544SRui Ueyama .Case("PT_TLS", PT_TLS) 10632ec34544SRui Ueyama .Case("PT_GNU_EH_FRAME", PT_GNU_EH_FRAME) 10642ec34544SRui Ueyama .Case("PT_GNU_STACK", PT_GNU_STACK) 10652ec34544SRui Ueyama .Case("PT_GNU_RELRO", PT_GNU_RELRO) 10662ec34544SRui Ueyama .Case("PT_OPENBSD_RANDOMIZE", PT_OPENBSD_RANDOMIZE) 10672ec34544SRui Ueyama .Case("PT_OPENBSD_WXNEEDED", PT_OPENBSD_WXNEEDED) 10682ec34544SRui Ueyama .Case("PT_OPENBSD_BOOTDATA", PT_OPENBSD_BOOTDATA) 10692ec34544SRui Ueyama .Default(-1); 10702ec34544SRui Ueyama 10712ec34544SRui Ueyama if (Ret == (unsigned)-1) { 10722ec34544SRui Ueyama setError("invalid program header type: " + Tok); 10732ec34544SRui Ueyama return PT_NULL; 10742ec34544SRui Ueyama } 10752ec34544SRui Ueyama return Ret; 10762ec34544SRui Ueyama } 10772ec34544SRui Ueyama 10782ec34544SRui Ueyama // Reads an anonymous version declaration. 10792ec34544SRui Ueyama void ScriptParser::readAnonymousDeclaration() { 10802ec34544SRui Ueyama std::vector<SymbolVersion> Locals; 10812ec34544SRui Ueyama std::vector<SymbolVersion> Globals; 10822ec34544SRui Ueyama std::tie(Locals, Globals) = readSymbols(); 10832ec34544SRui Ueyama 10842ec34544SRui Ueyama for (SymbolVersion V : Locals) { 10852ec34544SRui Ueyama if (V.Name == "*") 10862ec34544SRui Ueyama Config->DefaultSymbolVersion = VER_NDX_LOCAL; 10872ec34544SRui Ueyama else 10882ec34544SRui Ueyama Config->VersionScriptLocals.push_back(V); 10892ec34544SRui Ueyama } 10902ec34544SRui Ueyama 10912ec34544SRui Ueyama for (SymbolVersion V : Globals) 10922ec34544SRui Ueyama Config->VersionScriptGlobals.push_back(V); 10932ec34544SRui Ueyama 10942ec34544SRui Ueyama expect(";"); 10952ec34544SRui Ueyama } 10962ec34544SRui Ueyama 10972ec34544SRui Ueyama // Reads a non-anonymous version definition, 10982ec34544SRui Ueyama // e.g. "VerStr { global: foo; bar; local: *; };". 10992ec34544SRui Ueyama void ScriptParser::readVersionDeclaration(StringRef VerStr) { 11002ec34544SRui Ueyama // Read a symbol list. 11012ec34544SRui Ueyama std::vector<SymbolVersion> Locals; 11022ec34544SRui Ueyama std::vector<SymbolVersion> Globals; 11032ec34544SRui Ueyama std::tie(Locals, Globals) = readSymbols(); 11042ec34544SRui Ueyama 11052ec34544SRui Ueyama for (SymbolVersion V : Locals) { 11062ec34544SRui Ueyama if (V.Name == "*") 11072ec34544SRui Ueyama Config->DefaultSymbolVersion = VER_NDX_LOCAL; 11082ec34544SRui Ueyama else 11092ec34544SRui Ueyama Config->VersionScriptLocals.push_back(V); 11102ec34544SRui Ueyama } 11112ec34544SRui Ueyama 11122ec34544SRui Ueyama // Create a new version definition and add that to the global symbols. 11132ec34544SRui Ueyama VersionDefinition Ver; 11142ec34544SRui Ueyama Ver.Name = VerStr; 11152ec34544SRui Ueyama Ver.Globals = Globals; 11162ec34544SRui Ueyama 11172ec34544SRui Ueyama // User-defined version number starts from 2 because 0 and 1 are 11182ec34544SRui Ueyama // reserved for VER_NDX_LOCAL and VER_NDX_GLOBAL, respectively. 11192ec34544SRui Ueyama Ver.Id = Config->VersionDefinitions.size() + 2; 11202ec34544SRui Ueyama Config->VersionDefinitions.push_back(Ver); 11212ec34544SRui Ueyama 11222ec34544SRui Ueyama // Each version may have a parent version. For example, "Ver2" 11232ec34544SRui Ueyama // defined as "Ver2 { global: foo; local: *; } Ver1;" has "Ver1" 11242ec34544SRui Ueyama // as a parent. This version hierarchy is, probably against your 11252ec34544SRui Ueyama // instinct, purely for hint; the runtime doesn't care about it 11262ec34544SRui Ueyama // at all. In LLD, we simply ignore it. 11272ec34544SRui Ueyama if (peek() != ";") 11282ec34544SRui Ueyama skip(); 11292ec34544SRui Ueyama expect(";"); 11302ec34544SRui Ueyama } 11312ec34544SRui Ueyama 11321e77ad14SRui Ueyama static bool hasWildcard(StringRef S) { 11331e77ad14SRui Ueyama return S.find_first_of("?*[") != StringRef::npos; 11341e77ad14SRui Ueyama } 11351e77ad14SRui Ueyama 11362ec34544SRui Ueyama // Reads a list of symbols, e.g. "{ global: foo; bar; local: *; };". 11372ec34544SRui Ueyama std::pair<std::vector<SymbolVersion>, std::vector<SymbolVersion>> 11382ec34544SRui Ueyama ScriptParser::readSymbols() { 11392ec34544SRui Ueyama std::vector<SymbolVersion> Locals; 11402ec34544SRui Ueyama std::vector<SymbolVersion> Globals; 11412ec34544SRui Ueyama std::vector<SymbolVersion> *V = &Globals; 11422ec34544SRui Ueyama 1143ce608081SGeorge Rimar while (!ErrorCount) { 11442ec34544SRui Ueyama if (consume("}")) 11452ec34544SRui Ueyama break; 11462ec34544SRui Ueyama if (consumeLabel("local")) { 11472ec34544SRui Ueyama V = &Locals; 11482ec34544SRui Ueyama continue; 11492ec34544SRui Ueyama } 11502ec34544SRui Ueyama if (consumeLabel("global")) { 11512ec34544SRui Ueyama V = &Globals; 11522ec34544SRui Ueyama continue; 11532ec34544SRui Ueyama } 11542ec34544SRui Ueyama 11552ec34544SRui Ueyama if (consume("extern")) { 11562ec34544SRui Ueyama std::vector<SymbolVersion> Ext = readVersionExtern(); 11572ec34544SRui Ueyama V->insert(V->end(), Ext.begin(), Ext.end()); 11582ec34544SRui Ueyama } else { 11592ec34544SRui Ueyama StringRef Tok = next(); 11602ec34544SRui Ueyama V->push_back({unquote(Tok), false, hasWildcard(Tok)}); 11612ec34544SRui Ueyama } 11622ec34544SRui Ueyama expect(";"); 11632ec34544SRui Ueyama } 11642ec34544SRui Ueyama return {Locals, Globals}; 11652ec34544SRui Ueyama } 11662ec34544SRui Ueyama 11672ec34544SRui Ueyama // Reads an "extern C++" directive, e.g., 11682ec34544SRui Ueyama // "extern "C++" { ns::*; "f(int, double)"; };" 11692ec34544SRui Ueyama std::vector<SymbolVersion> ScriptParser::readVersionExtern() { 11702ec34544SRui Ueyama StringRef Tok = next(); 11712ec34544SRui Ueyama bool IsCXX = Tok == "\"C++\""; 11722ec34544SRui Ueyama if (!IsCXX && Tok != "\"C\"") 11732ec34544SRui Ueyama setError("Unknown language"); 11742ec34544SRui Ueyama expect("{"); 11752ec34544SRui Ueyama 11762ec34544SRui Ueyama std::vector<SymbolVersion> Ret; 1177ce608081SGeorge Rimar while (!ErrorCount && peek() != "}") { 11782ec34544SRui Ueyama StringRef Tok = next(); 11792ec34544SRui Ueyama bool HasWildcard = !Tok.startswith("\"") && hasWildcard(Tok); 11802ec34544SRui Ueyama Ret.push_back({unquote(Tok), IsCXX, HasWildcard}); 11812ec34544SRui Ueyama expect(";"); 11822ec34544SRui Ueyama } 11832ec34544SRui Ueyama 11842ec34544SRui Ueyama expect("}"); 11852ec34544SRui Ueyama return Ret; 11862ec34544SRui Ueyama } 11872ec34544SRui Ueyama 11882ec34544SRui Ueyama uint64_t ScriptParser::readMemoryAssignment(StringRef S1, StringRef S2, 11892ec34544SRui Ueyama StringRef S3) { 1190b579c439SRui Ueyama if (!consume(S1) && !consume(S2) && !consume(S3)) { 11912ec34544SRui Ueyama setError("expected one of: " + S1 + ", " + S2 + ", or " + S3); 11922ec34544SRui Ueyama return 0; 11932ec34544SRui Ueyama } 11942ec34544SRui Ueyama expect("="); 1195040af7deSRui Ueyama return readExpr()().getValue(); 11962ec34544SRui Ueyama } 11972ec34544SRui Ueyama 11982ec34544SRui Ueyama // Parse the MEMORY command as specified in: 11992ec34544SRui Ueyama // https://sourceware.org/binutils/docs/ld/MEMORY.html 12002ec34544SRui Ueyama // 12012ec34544SRui Ueyama // MEMORY { name [(attr)] : ORIGIN = origin, LENGTH = len ... } 12022ec34544SRui Ueyama void ScriptParser::readMemory() { 12032ec34544SRui Ueyama expect("{"); 1204ce608081SGeorge Rimar while (!ErrorCount && !consume("}")) { 12052ec34544SRui Ueyama StringRef Name = next(); 12062ec34544SRui Ueyama 12072ec34544SRui Ueyama uint32_t Flags = 0; 12082ec34544SRui Ueyama uint32_t NegFlags = 0; 12092ec34544SRui Ueyama if (consume("(")) { 12102ec34544SRui Ueyama std::tie(Flags, NegFlags) = readMemoryAttributes(); 12112ec34544SRui Ueyama expect(")"); 12122ec34544SRui Ueyama } 12132ec34544SRui Ueyama expect(":"); 12142ec34544SRui Ueyama 12152ec34544SRui Ueyama uint64_t Origin = readMemoryAssignment("ORIGIN", "org", "o"); 12162ec34544SRui Ueyama expect(","); 12172ec34544SRui Ueyama uint64_t Length = readMemoryAssignment("LENGTH", "len", "l"); 12182ec34544SRui Ueyama 12192ec34544SRui Ueyama // Add the memory region to the region map (if it doesn't already exist). 12202ec34544SRui Ueyama auto It = Script->Opt.MemoryRegions.find(Name); 12212ec34544SRui Ueyama if (It != Script->Opt.MemoryRegions.end()) 12222ec34544SRui Ueyama setError("region '" + Name + "' already defined"); 12232ec34544SRui Ueyama else 1224906e9a18SPeter Smith Script->Opt.MemoryRegions[Name] = {Name, Origin, Length, Flags, NegFlags}; 12252ec34544SRui Ueyama } 12262ec34544SRui Ueyama } 12272ec34544SRui Ueyama 12282ec34544SRui Ueyama // This function parses the attributes used to match against section 12292ec34544SRui Ueyama // flags when placing output sections in a memory region. These flags 12302ec34544SRui Ueyama // are only used when an explicit memory region name is not used. 12312ec34544SRui Ueyama std::pair<uint32_t, uint32_t> ScriptParser::readMemoryAttributes() { 12322ec34544SRui Ueyama uint32_t Flags = 0; 12332ec34544SRui Ueyama uint32_t NegFlags = 0; 12342ec34544SRui Ueyama bool Invert = false; 12352ec34544SRui Ueyama 12362ec34544SRui Ueyama for (char C : next().lower()) { 12372ec34544SRui Ueyama uint32_t Flag = 0; 12382ec34544SRui Ueyama if (C == '!') 12392ec34544SRui Ueyama Invert = !Invert; 12402ec34544SRui Ueyama else if (C == 'w') 12412ec34544SRui Ueyama Flag = SHF_WRITE; 12422ec34544SRui Ueyama else if (C == 'x') 12432ec34544SRui Ueyama Flag = SHF_EXECINSTR; 12442ec34544SRui Ueyama else if (C == 'a') 12452ec34544SRui Ueyama Flag = SHF_ALLOC; 12462ec34544SRui Ueyama else if (C != 'r') 12472ec34544SRui Ueyama setError("invalid memory region attribute"); 12482ec34544SRui Ueyama 12492ec34544SRui Ueyama if (Invert) 12502ec34544SRui Ueyama NegFlags |= Flag; 12512ec34544SRui Ueyama else 12522ec34544SRui Ueyama Flags |= Flag; 12532ec34544SRui Ueyama } 12542ec34544SRui Ueyama return {Flags, NegFlags}; 12552ec34544SRui Ueyama } 12562ec34544SRui Ueyama 12572ec34544SRui Ueyama void elf::readLinkerScript(MemoryBufferRef MB) { 12582ec34544SRui Ueyama ScriptParser(MB).readLinkerScript(); 12592ec34544SRui Ueyama } 12602ec34544SRui Ueyama 12612ec34544SRui Ueyama void elf::readVersionScript(MemoryBufferRef MB) { 12622ec34544SRui Ueyama ScriptParser(MB).readVersionScript(); 12632ec34544SRui Ueyama } 12642ec34544SRui Ueyama 12652ec34544SRui Ueyama void elf::readDynamicList(MemoryBufferRef MB) { 12662ec34544SRui Ueyama ScriptParser(MB).readDynamicList(); 12672ec34544SRui Ueyama } 1268