12ec34544SRui Ueyama //===- ScriptParser.cpp ---------------------------------------------------===//
22ec34544SRui Ueyama //
32946cd70SChandler Carruth // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
42946cd70SChandler Carruth // See https://llvm.org/LICENSE.txt for license information.
52946cd70SChandler Carruth // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
62ec34544SRui Ueyama //
72ec34544SRui Ueyama //===----------------------------------------------------------------------===//
805f6b852SRui Ueyama //
905f6b852SRui Ueyama // This file contains a recursive-descendent parser for linker scripts.
1005f6b852SRui Ueyama // Parsed results are stored to Config and Script global objects.
1105f6b852SRui Ueyama //
1205f6b852SRui Ueyama //===----------------------------------------------------------------------===//
132ec34544SRui Ueyama 
142ec34544SRui Ueyama #include "ScriptParser.h"
152ec34544SRui Ueyama #include "Config.h"
162ec34544SRui Ueyama #include "Driver.h"
172ec34544SRui Ueyama #include "InputSection.h"
182ec34544SRui Ueyama #include "LinkerScript.h"
192ec34544SRui Ueyama #include "OutputSections.h"
202ec34544SRui Ueyama #include "ScriptLexer.h"
212ec34544SRui Ueyama #include "Symbols.h"
222ec34544SRui Ueyama #include "Target.h"
232017d52bSRui Ueyama #include "lld/Common/Memory.h"
242ec34544SRui Ueyama #include "llvm/ADT/SmallString.h"
252ec34544SRui Ueyama #include "llvm/ADT/StringRef.h"
260440be4aSRui Ueyama #include "llvm/ADT/StringSet.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"
33dbd0ad33SPeter Smith #include "llvm/Support/ScopedPrinter.h"
342ec34544SRui Ueyama #include <cassert>
352ec34544SRui Ueyama #include <limits>
362ec34544SRui Ueyama #include <vector>
372ec34544SRui Ueyama 
382ec34544SRui Ueyama using namespace llvm;
392ec34544SRui Ueyama using namespace llvm::ELF;
40b58079d4SRui Ueyama using namespace llvm::support::endian;
412ec34544SRui Ueyama 
42bd8cfe65SFangrui Song namespace lld {
43bd8cfe65SFangrui Song namespace elf {
4496b3fe02SRui Ueyama namespace {
4596b3fe02SRui Ueyama class ScriptParser final : ScriptLexer {
462ec34544SRui Ueyama public:
473837f427SRui Ueyama   ScriptParser(MemoryBufferRef mb) : ScriptLexer(mb) {
4811ae59f0SRui Ueyama     // Initialize IsUnderSysroot
493837f427SRui Ueyama     if (config->sysroot == "")
5011ae59f0SRui Ueyama       return;
513837f427SRui Ueyama     StringRef path = mb.getBufferIdentifier();
523837f427SRui Ueyama     for (; !path.empty(); path = sys::path::parent_path(path)) {
533837f427SRui Ueyama       if (!sys::fs::equivalent(config->sysroot, path))
5411ae59f0SRui Ueyama         continue;
553837f427SRui Ueyama       isUnderSysroot = true;
5611ae59f0SRui Ueyama       return;
5711ae59f0SRui Ueyama     }
5811ae59f0SRui Ueyama   }
592ec34544SRui Ueyama 
602ec34544SRui Ueyama   void readLinkerScript();
612ec34544SRui Ueyama   void readVersionScript();
622ec34544SRui Ueyama   void readDynamicList();
633837f427SRui Ueyama   void readDefsym(StringRef name);
642ec34544SRui Ueyama 
652ec34544SRui Ueyama private:
663837f427SRui Ueyama   void addFile(StringRef path);
672ec34544SRui Ueyama 
682ec34544SRui Ueyama   void readAsNeeded();
692ec34544SRui Ueyama   void readEntry();
702ec34544SRui Ueyama   void readExtern();
712ec34544SRui Ueyama   void readGroup();
722ec34544SRui Ueyama   void readInclude();
731d92aa73SRui Ueyama   void readInput();
742ec34544SRui Ueyama   void readMemory();
752ec34544SRui Ueyama   void readOutput();
762ec34544SRui Ueyama   void readOutputArch();
772ec34544SRui Ueyama   void readOutputFormat();
782ec34544SRui Ueyama   void readPhdrs();
795f37541cSGeorge Rimar   void readRegionAlias();
802ec34544SRui Ueyama   void readSearchDir();
812ec34544SRui Ueyama   void readSections();
82e262bb1aSRui Ueyama   void readTarget();
832ec34544SRui Ueyama   void readVersion();
842ec34544SRui Ueyama   void readVersionScriptCommand();
852ec34544SRui Ueyama 
863837f427SRui Ueyama   SymbolAssignment *readSymbolAssignment(StringRef name);
873837f427SRui Ueyama   ByteCommand *readByteCommand(StringRef tok);
88b0486051SSimon Atanasyan   std::array<uint8_t, 4> readFill();
893837f427SRui Ueyama   bool readSectionDirective(OutputSection *cmd, StringRef tok1, StringRef tok2);
903837f427SRui Ueyama   void readSectionAddressType(OutputSection *cmd);
91a582419aSGeorge Rimar   OutputSection *readOverlaySectionDescription();
923837f427SRui Ueyama   OutputSection *readOutputSectionDescription(StringRef outSec);
93a582419aSGeorge Rimar   std::vector<BaseCommand *> readOverlay();
942ec34544SRui Ueyama   std::vector<StringRef> readOutputSectionPhdrs();
95dbd0ad33SPeter Smith   std::pair<uint64_t, uint64_t> readInputSectionFlags();
963837f427SRui Ueyama   InputSectionDescription *readInputSectionDescription(StringRef tok);
972ec34544SRui Ueyama   StringMatcher readFilePatterns();
982ec34544SRui Ueyama   std::vector<SectionPattern> readInputSectionsList();
99dbd0ad33SPeter Smith   InputSectionDescription *readInputSectionRules(StringRef filePattern,
100dbd0ad33SPeter Smith                                                  uint64_t withFlags,
101dbd0ad33SPeter Smith                                                  uint64_t withoutFlags);
1022ec34544SRui Ueyama   unsigned readPhdrType();
1032ec34544SRui Ueyama   SortSectionPolicy readSortKind();
1043837f427SRui Ueyama   SymbolAssignment *readProvideHidden(bool provide, bool hidden);
1053837f427SRui Ueyama   SymbolAssignment *readAssignment(StringRef tok);
1062ec34544SRui Ueyama   void readSort();
107d30a78b3SGeorge Rimar   Expr readAssert();
1085fb17128SGeorge Rimar   Expr readConstant();
1095fb17128SGeorge Rimar   Expr getPageSize();
1102ec34544SRui Ueyama 
11192b5b980SFangrui Song   Expr readMemoryAssignment(StringRef, StringRef, StringRef);
1122ec34544SRui Ueyama   std::pair<uint32_t, uint32_t> readMemoryAttributes();
1132ec34544SRui Ueyama 
1143837f427SRui Ueyama   Expr combine(StringRef op, Expr l, Expr r);
1152ec34544SRui Ueyama   Expr readExpr();
1163837f427SRui Ueyama   Expr readExpr1(Expr lhs, int minPrec);
1172ec34544SRui Ueyama   StringRef readParenLiteral();
1182ec34544SRui Ueyama   Expr readPrimary();
1193837f427SRui Ueyama   Expr readTernary(Expr cond);
1202ec34544SRui Ueyama   Expr readParenExpr();
1212ec34544SRui Ueyama 
1222ec34544SRui Ueyama   // For parsing version script.
1232ec34544SRui Ueyama   std::vector<SymbolVersion> readVersionExtern();
1242ec34544SRui Ueyama   void readAnonymousDeclaration();
1253837f427SRui Ueyama   void readVersionDeclaration(StringRef verStr);
1262ec34544SRui Ueyama 
1272ec34544SRui Ueyama   std::pair<std::vector<SymbolVersion>, std::vector<SymbolVersion>>
1282ec34544SRui Ueyama   readSymbols();
1292ec34544SRui Ueyama 
130fd06b025SRui Ueyama   // True if a script being read is in a subdirectory specified by -sysroot.
1313837f427SRui Ueyama   bool isUnderSysroot = false;
1320440be4aSRui Ueyama 
1330440be4aSRui Ueyama   // A set to detect an INCLUDE() cycle.
1343837f427SRui Ueyama   StringSet<> seen;
1352ec34544SRui Ueyama };
13696b3fe02SRui Ueyama } // namespace
1372ec34544SRui Ueyama 
1383837f427SRui Ueyama static StringRef unquote(StringRef s) {
1393837f427SRui Ueyama   if (s.startswith("\""))
1403837f427SRui Ueyama     return s.substr(1, s.size() - 2);
1413837f427SRui Ueyama   return s;
1421e77ad14SRui Ueyama }
1431e77ad14SRui Ueyama 
1442ec34544SRui Ueyama // Some operations only support one non absolute value. Move the
1452ec34544SRui Ueyama // absolute one to the right hand side for convenience.
1463837f427SRui Ueyama static void moveAbsRight(ExprValue &a, ExprValue &b) {
1473837f427SRui Ueyama   if (a.sec == nullptr || (a.forceAbsolute && !b.isAbsolute()))
1483837f427SRui Ueyama     std::swap(a, b);
1493837f427SRui Ueyama   if (!b.isAbsolute())
1503837f427SRui Ueyama     error(a.loc + ": at least one side of the expression must be absolute");
1512ec34544SRui Ueyama }
1522ec34544SRui Ueyama 
1533837f427SRui Ueyama static ExprValue add(ExprValue a, ExprValue b) {
1543837f427SRui Ueyama   moveAbsRight(a, b);
1553837f427SRui Ueyama   return {a.sec, a.forceAbsolute, a.getSectionOffset() + b.getValue(), a.loc};
1562ec34544SRui Ueyama }
1572ec34544SRui Ueyama 
1583837f427SRui Ueyama static ExprValue sub(ExprValue a, ExprValue b) {
15963a4a98eSRafael Espindola   // The distance between two symbols in sections is absolute.
1603837f427SRui Ueyama   if (!a.isAbsolute() && !b.isAbsolute())
1613837f427SRui Ueyama     return a.getValue() - b.getValue();
1623837f427SRui Ueyama   return {a.sec, false, a.getSectionOffset() - b.getValue(), a.loc};
1632ec34544SRui Ueyama }
1642ec34544SRui Ueyama 
1653837f427SRui Ueyama static ExprValue bitAnd(ExprValue a, ExprValue b) {
1663837f427SRui Ueyama   moveAbsRight(a, b);
1673837f427SRui Ueyama   return {a.sec, a.forceAbsolute,
1683837f427SRui Ueyama           (a.getValue() & b.getValue()) - a.getSecAddr(), a.loc};
1692ec34544SRui Ueyama }
1702ec34544SRui Ueyama 
1713837f427SRui Ueyama static ExprValue bitOr(ExprValue a, ExprValue b) {
1723837f427SRui Ueyama   moveAbsRight(a, b);
1733837f427SRui Ueyama   return {a.sec, a.forceAbsolute,
1743837f427SRui Ueyama           (a.getValue() | b.getValue()) - a.getSecAddr(), a.loc};
1752ec34544SRui Ueyama }
1762ec34544SRui Ueyama 
1772ec34544SRui Ueyama void ScriptParser::readDynamicList() {
1783837f427SRui Ueyama   config->hasDynamicList = true;
1792ec34544SRui Ueyama   expect("{");
1803837f427SRui Ueyama   std::vector<SymbolVersion> locals;
1813837f427SRui Ueyama   std::vector<SymbolVersion> globals;
1823837f427SRui Ueyama   std::tie(locals, globals) = readSymbols();
183d72d97b3SRafael Espindola   expect(";");
184d72d97b3SRafael Espindola 
185d72d97b3SRafael Espindola   if (!atEOF()) {
1862ec34544SRui Ueyama     setError("EOF expected, but got " + next());
187d72d97b3SRafael Espindola     return;
188d72d97b3SRafael Espindola   }
1893837f427SRui Ueyama   if (!locals.empty()) {
190d72d97b3SRafael Espindola     setError("\"local:\" scope not supported in --dynamic-list");
191d72d97b3SRafael Espindola     return;
192d72d97b3SRafael Espindola   }
193d72d97b3SRafael Espindola 
1943837f427SRui Ueyama   for (SymbolVersion v : globals)
1953837f427SRui Ueyama     config->dynamicList.push_back(v);
1962ec34544SRui Ueyama }
1972ec34544SRui Ueyama 
1982ec34544SRui Ueyama void ScriptParser::readVersionScript() {
1992ec34544SRui Ueyama   readVersionScriptCommand();
2002ec34544SRui Ueyama   if (!atEOF())
2012ec34544SRui Ueyama     setError("EOF expected, but got " + next());
2022ec34544SRui Ueyama }
2032ec34544SRui Ueyama 
2042ec34544SRui Ueyama void ScriptParser::readVersionScriptCommand() {
2052ec34544SRui Ueyama   if (consume("{")) {
2062ec34544SRui Ueyama     readAnonymousDeclaration();
2072ec34544SRui Ueyama     return;
2082ec34544SRui Ueyama   }
2092ec34544SRui Ueyama 
210b8a59c8aSBob Haarman   while (!atEOF() && !errorCount() && peek() != "}") {
2113837f427SRui Ueyama     StringRef verStr = next();
2123837f427SRui Ueyama     if (verStr == "{") {
2132ec34544SRui Ueyama       setError("anonymous version definition is used in "
2142ec34544SRui Ueyama                "combination with other version definitions");
2152ec34544SRui Ueyama       return;
2162ec34544SRui Ueyama     }
2172ec34544SRui Ueyama     expect("{");
2183837f427SRui Ueyama     readVersionDeclaration(verStr);
2192ec34544SRui Ueyama   }
2202ec34544SRui Ueyama }
2212ec34544SRui Ueyama 
2222ec34544SRui Ueyama void ScriptParser::readVersion() {
2232ec34544SRui Ueyama   expect("{");
2242ec34544SRui Ueyama   readVersionScriptCommand();
2252ec34544SRui Ueyama   expect("}");
2262ec34544SRui Ueyama }
2272ec34544SRui Ueyama 
2282ec34544SRui Ueyama void ScriptParser::readLinkerScript() {
2292ec34544SRui Ueyama   while (!atEOF()) {
2303837f427SRui Ueyama     StringRef tok = next();
2313837f427SRui Ueyama     if (tok == ";")
2322ec34544SRui Ueyama       continue;
2332ec34544SRui Ueyama 
2343837f427SRui Ueyama     if (tok == "ENTRY") {
2352ec34544SRui Ueyama       readEntry();
2363837f427SRui Ueyama     } else if (tok == "EXTERN") {
2372ec34544SRui Ueyama       readExtern();
2383837f427SRui Ueyama     } else if (tok == "GROUP") {
2392ec34544SRui Ueyama       readGroup();
2403837f427SRui Ueyama     } else if (tok == "INCLUDE") {
2412ec34544SRui Ueyama       readInclude();
2423837f427SRui Ueyama     } else if (tok == "INPUT") {
2431d92aa73SRui Ueyama       readInput();
2443837f427SRui Ueyama     } else if (tok == "MEMORY") {
2452ec34544SRui Ueyama       readMemory();
2463837f427SRui Ueyama     } else if (tok == "OUTPUT") {
2472ec34544SRui Ueyama       readOutput();
2483837f427SRui Ueyama     } else if (tok == "OUTPUT_ARCH") {
2492ec34544SRui Ueyama       readOutputArch();
2503837f427SRui Ueyama     } else if (tok == "OUTPUT_FORMAT") {
2512ec34544SRui Ueyama       readOutputFormat();
2523837f427SRui Ueyama     } else if (tok == "PHDRS") {
2532ec34544SRui Ueyama       readPhdrs();
2543837f427SRui Ueyama     } else if (tok == "REGION_ALIAS") {
2555f37541cSGeorge Rimar       readRegionAlias();
2563837f427SRui Ueyama     } else if (tok == "SEARCH_DIR") {
2572ec34544SRui Ueyama       readSearchDir();
2583837f427SRui Ueyama     } else if (tok == "SECTIONS") {
2592ec34544SRui Ueyama       readSections();
2603837f427SRui Ueyama     } else if (tok == "TARGET") {
261e262bb1aSRui Ueyama       readTarget();
2623837f427SRui Ueyama     } else if (tok == "VERSION") {
2632ec34544SRui Ueyama       readVersion();
2643837f427SRui Ueyama     } else if (SymbolAssignment *cmd = readAssignment(tok)) {
2653837f427SRui Ueyama       script->sectionCommands.push_back(cmd);
2662ec34544SRui Ueyama     } else {
2673837f427SRui Ueyama       setError("unknown directive: " + tok);
2682ec34544SRui Ueyama     }
2692ec34544SRui Ueyama   }
2702ec34544SRui Ueyama }
2712ec34544SRui Ueyama 
2723837f427SRui Ueyama void ScriptParser::readDefsym(StringRef name) {
273c1522816SGeorge Rimar   if (errorCount())
274c1522816SGeorge Rimar     return;
2753837f427SRui Ueyama   Expr e = readExpr();
2768c7e8cceSPetr Hosek   if (!atEOF())
2778c7e8cceSPetr Hosek     setError("EOF expected, but got " + next());
2783837f427SRui Ueyama   SymbolAssignment *cmd = make<SymbolAssignment>(name, e, getCurrentLocation());
2793837f427SRui Ueyama   script->sectionCommands.push_back(cmd);
2808c7e8cceSPetr Hosek }
2818c7e8cceSPetr Hosek 
2823837f427SRui Ueyama void ScriptParser::addFile(StringRef s) {
2833837f427SRui Ueyama   if (isUnderSysroot && s.startswith("/")) {
2843837f427SRui Ueyama     SmallString<128> pathData;
2853837f427SRui Ueyama     StringRef path = (config->sysroot + s).toStringRef(pathData);
2863837f427SRui Ueyama     if (sys::fs::exists(path)) {
28749a3ad21SRui Ueyama       driver->addFile(saver.save(path), /*withLOption=*/false);
2882ec34544SRui Ueyama       return;
2892ec34544SRui Ueyama     }
2902ec34544SRui Ueyama   }
2912ec34544SRui Ueyama 
2923837f427SRui Ueyama   if (s.startswith("/")) {
29349a3ad21SRui Ueyama     driver->addFile(s, /*withLOption=*/false);
2943837f427SRui Ueyama   } else if (s.startswith("=")) {
2953837f427SRui Ueyama     if (config->sysroot.empty())
29649a3ad21SRui Ueyama       driver->addFile(s.substr(1), /*withLOption=*/false);
2972ec34544SRui Ueyama     else
298136d27abSRui Ueyama       driver->addFile(saver.save(config->sysroot + "/" + s.substr(1)),
29949a3ad21SRui Ueyama                       /*withLOption=*/false);
3003837f427SRui Ueyama   } else if (s.startswith("-l")) {
3013837f427SRui Ueyama     driver->addLibrary(s.substr(2));
3023837f427SRui Ueyama   } else if (sys::fs::exists(s)) {
30349a3ad21SRui Ueyama     driver->addFile(s, /*withLOption=*/false);
3042ec34544SRui Ueyama   } else {
3053837f427SRui Ueyama     if (Optional<std::string> path = findFromSearchPaths(s))
30649a3ad21SRui Ueyama       driver->addFile(saver.save(*path), /*withLOption=*/true);
3072ec34544SRui Ueyama     else
3083837f427SRui Ueyama       setError("unable to find " + s);
3092ec34544SRui Ueyama   }
3102ec34544SRui Ueyama }
3112ec34544SRui Ueyama 
3122ec34544SRui Ueyama void ScriptParser::readAsNeeded() {
3132ec34544SRui Ueyama   expect("(");
3143837f427SRui Ueyama   bool orig = config->asNeeded;
3153837f427SRui Ueyama   config->asNeeded = true;
316b8a59c8aSBob Haarman   while (!errorCount() && !consume(")"))
3172ec34544SRui Ueyama     addFile(unquote(next()));
3183837f427SRui Ueyama   config->asNeeded = orig;
3192ec34544SRui Ueyama }
3202ec34544SRui Ueyama 
3212ec34544SRui Ueyama void ScriptParser::readEntry() {
3222ec34544SRui Ueyama   // -e <symbol> takes predecence over ENTRY(<symbol>).
3232ec34544SRui Ueyama   expect("(");
3243837f427SRui Ueyama   StringRef tok = next();
3253837f427SRui Ueyama   if (config->entry.empty())
3263837f427SRui Ueyama     config->entry = tok;
3272ec34544SRui Ueyama   expect(")");
3282ec34544SRui Ueyama }
3292ec34544SRui Ueyama 
3302ec34544SRui Ueyama void ScriptParser::readExtern() {
3312ec34544SRui Ueyama   expect("(");
332b8a59c8aSBob Haarman   while (!errorCount() && !consume(")"))
3333837f427SRui Ueyama     config->undefined.push_back(unquote(next()));
3342ec34544SRui Ueyama }
3352ec34544SRui Ueyama 
3362ec34544SRui Ueyama void ScriptParser::readGroup() {
3373837f427SRui Ueyama   bool orig = InputFile::isInGroup;
3383837f427SRui Ueyama   InputFile::isInGroup = true;
3391d92aa73SRui Ueyama   readInput();
3403837f427SRui Ueyama   InputFile::isInGroup = orig;
3413837f427SRui Ueyama   if (!orig)
3423837f427SRui Ueyama     ++InputFile::nextGroupId;
3432ec34544SRui Ueyama }
3442ec34544SRui Ueyama 
3452ec34544SRui Ueyama void ScriptParser::readInclude() {
3463837f427SRui Ueyama   StringRef tok = unquote(next());
3472ec34544SRui Ueyama 
3483837f427SRui Ueyama   if (!seen.insert(tok).second) {
3490440be4aSRui Ueyama     setError("there is a cycle in linker script INCLUDEs");
3500440be4aSRui Ueyama     return;
3510440be4aSRui Ueyama   }
3520440be4aSRui Ueyama 
3533837f427SRui Ueyama   if (Optional<std::string> path = searchScript(tok)) {
3543837f427SRui Ueyama     if (Optional<MemoryBufferRef> mb = readFile(*path))
3553837f427SRui Ueyama       tokenize(*mb);
3562ec34544SRui Ueyama     return;
3572ec34544SRui Ueyama   }
3583837f427SRui Ueyama   setError("cannot find linker script " + tok);
3592ec34544SRui Ueyama }
3602ec34544SRui Ueyama 
3611d92aa73SRui Ueyama void ScriptParser::readInput() {
3621d92aa73SRui Ueyama   expect("(");
3631d92aa73SRui Ueyama   while (!errorCount() && !consume(")")) {
3641d92aa73SRui Ueyama     if (consume("AS_NEEDED"))
3651d92aa73SRui Ueyama       readAsNeeded();
3661d92aa73SRui Ueyama     else
3671d92aa73SRui Ueyama       addFile(unquote(next()));
3681d92aa73SRui Ueyama   }
3691d92aa73SRui Ueyama }
3701d92aa73SRui Ueyama 
3712ec34544SRui Ueyama void ScriptParser::readOutput() {
3722ec34544SRui Ueyama   // -o <file> takes predecence over OUTPUT(<file>).
3732ec34544SRui Ueyama   expect("(");
3743837f427SRui Ueyama   StringRef tok = next();
3753837f427SRui Ueyama   if (config->outputFile.empty())
3763837f427SRui Ueyama     config->outputFile = unquote(tok);
3772ec34544SRui Ueyama   expect(")");
3782ec34544SRui Ueyama }
3792ec34544SRui Ueyama 
3802ec34544SRui Ueyama void ScriptParser::readOutputArch() {
3812ec34544SRui Ueyama   // OUTPUT_ARCH is ignored for now.
3822ec34544SRui Ueyama   expect("(");
383b8a59c8aSBob Haarman   while (!errorCount() && !consume(")"))
3842ec34544SRui Ueyama     skip();
3852ec34544SRui Ueyama }
3862ec34544SRui Ueyama 
3873837f427SRui Ueyama static std::pair<ELFKind, uint16_t> parseBfdName(StringRef s) {
3883837f427SRui Ueyama   return StringSwitch<std::pair<ELFKind, uint16_t>>(s)
3894f8c8228SRui Ueyama       .Case("elf32-i386", {ELF32LEKind, EM_386})
3904f8c8228SRui Ueyama       .Case("elf32-iamcu", {ELF32LEKind, EM_IAMCU})
3914f8c8228SRui Ueyama       .Case("elf32-littlearm", {ELF32LEKind, EM_ARM})
3924f8c8228SRui Ueyama       .Case("elf32-x86-64", {ELF32LEKind, EM_X86_64})
39319b134ccSDimitry Andric       .Case("elf64-aarch64", {ELF64LEKind, EM_AARCH64})
3944f8c8228SRui Ueyama       .Case("elf64-littleaarch64", {ELF64LEKind, EM_AARCH64})
3954134143cSRui Ueyama       .Case("elf32-powerpc", {ELF32BEKind, EM_PPC})
3964f8c8228SRui Ueyama       .Case("elf64-powerpc", {ELF64BEKind, EM_PPC64})
3974f8c8228SRui Ueyama       .Case("elf64-powerpcle", {ELF64LEKind, EM_PPC64})
3984f8c8228SRui Ueyama       .Case("elf64-x86-64", {ELF64LEKind, EM_X86_64})
3994134143cSRui Ueyama       .Cases("elf32-tradbigmips", "elf32-bigmips", {ELF32BEKind, EM_MIPS})
4004f8c8228SRui Ueyama       .Case("elf32-ntradbigmips", {ELF32BEKind, EM_MIPS})
4014f8c8228SRui Ueyama       .Case("elf32-tradlittlemips", {ELF32LEKind, EM_MIPS})
4024f8c8228SRui Ueyama       .Case("elf32-ntradlittlemips", {ELF32LEKind, EM_MIPS})
4034f8c8228SRui Ueyama       .Case("elf64-tradbigmips", {ELF64BEKind, EM_MIPS})
4044f8c8228SRui Ueyama       .Case("elf64-tradlittlemips", {ELF64LEKind, EM_MIPS})
40544d908d7SFangrui Song       .Case("elf32-littleriscv", {ELF32LEKind, EM_RISCV})
40644d908d7SFangrui Song       .Case("elf64-littleriscv", {ELF64LEKind, EM_RISCV})
407*aff950e9SLemonBoy       .Case("elf64-sparc", {ELF64BEKind, EM_SPARCV9})
4084f8c8228SRui Ueyama       .Default({ELFNoneKind, EM_NONE});
409ea8cd00aSRui Ueyama }
410ea8cd00aSRui Ueyama 
411ea8cd00aSRui Ueyama // Parse OUTPUT_FORMAT(bfdname) or OUTPUT_FORMAT(bfdname, big, little).
412ea8cd00aSRui Ueyama // Currently we ignore big and little parameters.
4132ec34544SRui Ueyama void ScriptParser::readOutputFormat() {
4142ec34544SRui Ueyama   expect("(");
415ea8cd00aSRui Ueyama 
4162822852fSShoaib Meenai   config->bfdname = unquote(next());
4172822852fSShoaib Meenai   StringRef s = config->bfdname;
4183837f427SRui Ueyama   if (s.consume_back("-freebsd"))
4193837f427SRui Ueyama     config->osabi = ELFOSABI_FREEBSD;
4204f8c8228SRui Ueyama 
4213837f427SRui Ueyama   std::tie(config->ekind, config->emachine) = parseBfdName(s);
4223837f427SRui Ueyama   if (config->emachine == EM_NONE)
4232822852fSShoaib Meenai     setError("unknown output format name: " + config->bfdname);
4243837f427SRui Ueyama   if (s == "elf32-ntradlittlemips" || s == "elf32-ntradbigmips")
4253837f427SRui Ueyama     config->mipsN32Abi = true;
426ea8cd00aSRui Ueyama 
427b579c439SRui Ueyama   if (consume(")"))
4282ec34544SRui Ueyama     return;
429b579c439SRui Ueyama   expect(",");
4302ec34544SRui Ueyama   skip();
4312ec34544SRui Ueyama   expect(",");
4322ec34544SRui Ueyama   skip();
4332ec34544SRui Ueyama   expect(")");
4342ec34544SRui Ueyama }
4352ec34544SRui Ueyama 
4362ec34544SRui Ueyama void ScriptParser::readPhdrs() {
4372ec34544SRui Ueyama   expect("{");
4382ec34544SRui Ueyama 
439b8a59c8aSBob Haarman   while (!errorCount() && !consume("}")) {
4403837f427SRui Ueyama     PhdrsCommand cmd;
4413837f427SRui Ueyama     cmd.name = next();
4423837f427SRui Ueyama     cmd.type = readPhdrType();
443b579c439SRui Ueyama 
444b8a59c8aSBob Haarman     while (!errorCount() && !consume(";")) {
445b579c439SRui Ueyama       if (consume("FILEHDR"))
4463837f427SRui Ueyama         cmd.hasFilehdr = true;
447b579c439SRui Ueyama       else if (consume("PHDRS"))
4483837f427SRui Ueyama         cmd.hasPhdrs = true;
449b579c439SRui Ueyama       else if (consume("AT"))
4503837f427SRui Ueyama         cmd.lmaExpr = readParenExpr();
451b579c439SRui Ueyama       else if (consume("FLAGS"))
4523837f427SRui Ueyama         cmd.flags = readParenExpr()().getValue();
453b579c439SRui Ueyama       else
454b579c439SRui Ueyama         setError("unexpected header attribute: " + next());
455b579c439SRui Ueyama     }
4560ae2c24cSRui Ueyama 
4573837f427SRui Ueyama     script->phdrsCommands.push_back(cmd);
4582ec34544SRui Ueyama   }
4592ec34544SRui Ueyama }
4602ec34544SRui Ueyama 
4615f37541cSGeorge Rimar void ScriptParser::readRegionAlias() {
4625f37541cSGeorge Rimar   expect("(");
4633837f427SRui Ueyama   StringRef alias = unquote(next());
4645f37541cSGeorge Rimar   expect(",");
4653837f427SRui Ueyama   StringRef name = next();
4665f37541cSGeorge Rimar   expect(")");
4675f37541cSGeorge Rimar 
4683837f427SRui Ueyama   if (script->memoryRegions.count(alias))
4693837f427SRui Ueyama     setError("redefinition of memory region '" + alias + "'");
4703837f427SRui Ueyama   if (!script->memoryRegions.count(name))
4713837f427SRui Ueyama     setError("memory region '" + name + "' is not defined");
4723837f427SRui Ueyama   script->memoryRegions.insert({alias, script->memoryRegions[name]});
4735f37541cSGeorge Rimar }
4745f37541cSGeorge Rimar 
4752ec34544SRui Ueyama void ScriptParser::readSearchDir() {
4762ec34544SRui Ueyama   expect("(");
4773837f427SRui Ueyama   StringRef tok = next();
4783837f427SRui Ueyama   if (!config->nostdlib)
4793837f427SRui Ueyama     config->searchPaths.push_back(unquote(tok));
4802ec34544SRui Ueyama   expect(")");
4812ec34544SRui Ueyama }
4822ec34544SRui Ueyama 
483a582419aSGeorge Rimar // This reads an overlay description. Overlays are used to describe output
484a582419aSGeorge Rimar // sections that use the same virtual memory range and normally would trigger
485a582419aSGeorge Rimar // linker's sections sanity check failures.
486a582419aSGeorge Rimar // https://sourceware.org/binutils/docs/ld/Overlay-Description.html#Overlay-Description
487a582419aSGeorge Rimar std::vector<BaseCommand *> ScriptParser::readOverlay() {
488a582419aSGeorge Rimar   // VA and LMA expressions are optional, though for simplicity of
489a582419aSGeorge Rimar   // implementation we assume they are not. That is what OVERLAY was designed
490a582419aSGeorge Rimar   // for first of all: to allow sections with overlapping VAs at different LMAs.
4913837f427SRui Ueyama   Expr addrExpr = readExpr();
492a582419aSGeorge Rimar   expect(":");
493a582419aSGeorge Rimar   expect("AT");
4943837f427SRui Ueyama   Expr lmaExpr = readParenExpr();
495a582419aSGeorge Rimar   expect("{");
496a582419aSGeorge Rimar 
4973837f427SRui Ueyama   std::vector<BaseCommand *> v;
4983837f427SRui Ueyama   OutputSection *prev = nullptr;
499a582419aSGeorge Rimar   while (!errorCount() && !consume("}")) {
500a582419aSGeorge Rimar     // VA is the same for all sections. The LMAs are consecutive in memory
501a582419aSGeorge Rimar     // starting from the base load address specified.
5023837f427SRui Ueyama     OutputSection *os = readOverlaySectionDescription();
5033837f427SRui Ueyama     os->addrExpr = addrExpr;
5043837f427SRui Ueyama     if (prev)
5053837f427SRui Ueyama       os->lmaExpr = [=] { return prev->getLMA() + prev->size; };
506a582419aSGeorge Rimar     else
5073837f427SRui Ueyama       os->lmaExpr = lmaExpr;
5083837f427SRui Ueyama     v.push_back(os);
5093837f427SRui Ueyama     prev = os;
510a582419aSGeorge Rimar   }
511a582419aSGeorge Rimar 
512a582419aSGeorge Rimar   // According to the specification, at the end of the overlay, the location
513a582419aSGeorge Rimar   // counter should be equal to the overlay base address plus size of the
514a582419aSGeorge Rimar   // largest section seen in the overlay.
515a582419aSGeorge Rimar   // Here we want to create the Dot assignment command to achieve that.
5163837f427SRui Ueyama   Expr moveDot = [=] {
5173837f427SRui Ueyama     uint64_t max = 0;
5183837f427SRui Ueyama     for (BaseCommand *cmd : v)
5193837f427SRui Ueyama       max = std::max(max, cast<OutputSection>(cmd)->size);
5203837f427SRui Ueyama     return addrExpr().getValue() + max;
521a582419aSGeorge Rimar   };
5223837f427SRui Ueyama   v.push_back(make<SymbolAssignment>(".", moveDot, getCurrentLocation()));
5233837f427SRui Ueyama   return v;
524a582419aSGeorge Rimar }
525a582419aSGeorge Rimar 
5262ec34544SRui Ueyama void ScriptParser::readSections() {
5272ec34544SRui Ueyama   expect("{");
5283837f427SRui Ueyama   std::vector<BaseCommand *> v;
529b8a59c8aSBob Haarman   while (!errorCount() && !consume("}")) {
5303837f427SRui Ueyama     StringRef tok = next();
5313837f427SRui Ueyama     if (tok == "OVERLAY") {
5323837f427SRui Ueyama       for (BaseCommand *cmd : readOverlay())
5333837f427SRui Ueyama         v.push_back(cmd);
534a582419aSGeorge Rimar       continue;
5353837f427SRui Ueyama     } else if (tok == "INCLUDE") {
5362e9d40d5SRui Ueyama       readInclude();
5372e9d40d5SRui Ueyama       continue;
538a582419aSGeorge Rimar     }
539a582419aSGeorge Rimar 
5403837f427SRui Ueyama     if (BaseCommand *cmd = readAssignment(tok))
5413837f427SRui Ueyama       v.push_back(cmd);
542d30a78b3SGeorge Rimar     else
5433837f427SRui Ueyama       v.push_back(readOutputSectionDescription(tok));
5442ec34544SRui Ueyama   }
5457c426fb1SFangrui Song   script->sectionCommands.insert(script->sectionCommands.end(), v.begin(),
5467c426fb1SFangrui Song                                  v.end());
5479e2c8a9dSGeorge Rimar 
5487c426fb1SFangrui Song   if (atEOF() || !consume("INSERT")) {
5497c426fb1SFangrui Song     script->hasSectionsCommand = true;
5509e2c8a9dSGeorge Rimar     return;
5519e2c8a9dSGeorge Rimar   }
5529e2c8a9dSGeorge Rimar 
5537c426fb1SFangrui Song   bool isAfter = false;
5547c426fb1SFangrui Song   if (consume("AFTER"))
5557c426fb1SFangrui Song     isAfter = true;
5567c426fb1SFangrui Song   else if (!consume("BEFORE"))
5577c426fb1SFangrui Song     setError("expected AFTER/BEFORE, but got '" + next() + "'");
5587c426fb1SFangrui Song   StringRef where = next();
5597c426fb1SFangrui Song   for (BaseCommand *cmd : v)
5607c426fb1SFangrui Song     if (auto *os = dyn_cast<OutputSection>(cmd))
5617c426fb1SFangrui Song       script->insertCommands.push_back({os, isAfter, where});
5622ec34544SRui Ueyama }
5632ec34544SRui Ueyama 
564e262bb1aSRui Ueyama void ScriptParser::readTarget() {
565e262bb1aSRui Ueyama   // TARGET(foo) is an alias for "--format foo". Unlike GNU linkers,
566e262bb1aSRui Ueyama   // we accept only a limited set of BFD names (i.e. "elf" or "binary")
567e262bb1aSRui Ueyama   // for --format. We recognize only /^elf/ and "binary" in the linker
568e262bb1aSRui Ueyama   // script as well.
569e262bb1aSRui Ueyama   expect("(");
5703837f427SRui Ueyama   StringRef tok = next();
571e262bb1aSRui Ueyama   expect(")");
572e262bb1aSRui Ueyama 
5733837f427SRui Ueyama   if (tok.startswith("elf"))
5743837f427SRui Ueyama     config->formatBinary = false;
5753837f427SRui Ueyama   else if (tok == "binary")
5763837f427SRui Ueyama     config->formatBinary = true;
577e262bb1aSRui Ueyama   else
5783837f427SRui Ueyama     setError("unknown target: " + tok);
579e262bb1aSRui Ueyama }
580e262bb1aSRui Ueyama 
5813837f427SRui Ueyama static int precedence(StringRef op) {
5823837f427SRui Ueyama   return StringSwitch<int>(op)
583a5005482SGeorge Rimar       .Cases("*", "/", "%", 8)
584a5005482SGeorge Rimar       .Cases("+", "-", 7)
585a5005482SGeorge Rimar       .Cases("<<", ">>", 6)
586a5005482SGeorge Rimar       .Cases("<", "<=", ">", ">=", "==", "!=", 5)
587a5005482SGeorge Rimar       .Case("&", 4)
588a5005482SGeorge Rimar       .Case("|", 3)
589a5005482SGeorge Rimar       .Case("&&", 2)
590a5005482SGeorge Rimar       .Case("||", 1)
5912ec34544SRui Ueyama       .Default(-1);
5922ec34544SRui Ueyama }
5932ec34544SRui Ueyama 
5942ec34544SRui Ueyama StringMatcher ScriptParser::readFilePatterns() {
595c42fe247SThomas Preud'homme   StringMatcher Matcher;
596c42fe247SThomas Preud'homme 
597b8a59c8aSBob Haarman   while (!errorCount() && !consume(")"))
598c42fe247SThomas Preud'homme     Matcher.addPattern(SingleStringMatcher(next()));
599c42fe247SThomas Preud'homme   return Matcher;
6002ec34544SRui Ueyama }
6012ec34544SRui Ueyama 
6022ec34544SRui Ueyama SortSectionPolicy ScriptParser::readSortKind() {
6032ec34544SRui Ueyama   if (consume("SORT") || consume("SORT_BY_NAME"))
6042ec34544SRui Ueyama     return SortSectionPolicy::Name;
6052ec34544SRui Ueyama   if (consume("SORT_BY_ALIGNMENT"))
6062ec34544SRui Ueyama     return SortSectionPolicy::Alignment;
6072ec34544SRui Ueyama   if (consume("SORT_BY_INIT_PRIORITY"))
6082ec34544SRui Ueyama     return SortSectionPolicy::Priority;
6092ec34544SRui Ueyama   if (consume("SORT_NONE"))
6102ec34544SRui Ueyama     return SortSectionPolicy::None;
6112ec34544SRui Ueyama   return SortSectionPolicy::Default;
6122ec34544SRui Ueyama }
6132ec34544SRui Ueyama 
61403fc8d1eSRui Ueyama // Reads SECTIONS command contents in the following form:
61503fc8d1eSRui Ueyama //
61603fc8d1eSRui Ueyama // <contents> ::= <elem>*
61703fc8d1eSRui Ueyama // <elem>     ::= <exclude>? <glob-pattern>
61803fc8d1eSRui Ueyama // <exclude>  ::= "EXCLUDE_FILE" "(" <glob-pattern>+ ")"
61903fc8d1eSRui Ueyama //
62003fc8d1eSRui Ueyama // For example,
62103fc8d1eSRui Ueyama //
62203fc8d1eSRui Ueyama // *(.foo EXCLUDE_FILE (a.o) .bar EXCLUDE_FILE (b.o) .baz)
62303fc8d1eSRui Ueyama //
62403fc8d1eSRui Ueyama // is parsed as ".foo", ".bar" with "a.o", and ".baz" with "b.o".
62503fc8d1eSRui Ueyama // The semantics of that is section .foo in any file, section .bar in
62603fc8d1eSRui Ueyama // any file but a.o, and section .baz in any file but b.o.
6272ec34544SRui Ueyama std::vector<SectionPattern> ScriptParser::readInputSectionsList() {
6283837f427SRui Ueyama   std::vector<SectionPattern> ret;
629b8a59c8aSBob Haarman   while (!errorCount() && peek() != ")") {
6303837f427SRui Ueyama     StringMatcher excludeFilePat;
6312ec34544SRui Ueyama     if (consume("EXCLUDE_FILE")) {
6322ec34544SRui Ueyama       expect("(");
6333837f427SRui Ueyama       excludeFilePat = readFilePatterns();
6342ec34544SRui Ueyama     }
6352ec34544SRui Ueyama 
636c42fe247SThomas Preud'homme     StringMatcher SectionMatcher;
637b8a59c8aSBob Haarman     while (!errorCount() && peek() != ")" && peek() != "EXCLUDE_FILE")
638c42fe247SThomas Preud'homme       SectionMatcher.addPattern(unquote(next()));
6392ec34544SRui Ueyama 
640c42fe247SThomas Preud'homme     if (!SectionMatcher.empty())
641c42fe247SThomas Preud'homme       ret.push_back({std::move(excludeFilePat), std::move(SectionMatcher)});
6422ec34544SRui Ueyama     else
6432ec34544SRui Ueyama       setError("section pattern is expected");
6442ec34544SRui Ueyama   }
6453837f427SRui Ueyama   return ret;
6462ec34544SRui Ueyama }
6472ec34544SRui Ueyama 
6482ec34544SRui Ueyama // Reads contents of "SECTIONS" directive. That directive contains a
6492ec34544SRui Ueyama // list of glob patterns for input sections. The grammar is as follows.
6502ec34544SRui Ueyama //
6512ec34544SRui Ueyama // <patterns> ::= <section-list>
6522ec34544SRui Ueyama //              | <sort> "(" <section-list> ")"
6532ec34544SRui Ueyama //              | <sort> "(" <sort> "(" <section-list> ")" ")"
6542ec34544SRui Ueyama //
6552ec34544SRui Ueyama // <sort>     ::= "SORT" | "SORT_BY_NAME" | "SORT_BY_ALIGNMENT"
6562ec34544SRui Ueyama //              | "SORT_BY_INIT_PRIORITY" | "SORT_NONE"
6572ec34544SRui Ueyama //
6582ec34544SRui Ueyama // <section-list> is parsed by readInputSectionsList().
6592ec34544SRui Ueyama InputSectionDescription *
660dbd0ad33SPeter Smith ScriptParser::readInputSectionRules(StringRef filePattern, uint64_t withFlags,
661dbd0ad33SPeter Smith                                     uint64_t withoutFlags) {
662dbd0ad33SPeter Smith   auto *cmd =
663dbd0ad33SPeter Smith       make<InputSectionDescription>(filePattern, withFlags, withoutFlags);
6642ec34544SRui Ueyama   expect("(");
6652ec34544SRui Ueyama 
666b8a59c8aSBob Haarman   while (!errorCount() && !consume(")")) {
6673837f427SRui Ueyama     SortSectionPolicy outer = readSortKind();
6683837f427SRui Ueyama     SortSectionPolicy inner = SortSectionPolicy::Default;
6693837f427SRui Ueyama     std::vector<SectionPattern> v;
6703837f427SRui Ueyama     if (outer != SortSectionPolicy::Default) {
6712ec34544SRui Ueyama       expect("(");
6723837f427SRui Ueyama       inner = readSortKind();
6733837f427SRui Ueyama       if (inner != SortSectionPolicy::Default) {
6742ec34544SRui Ueyama         expect("(");
6753837f427SRui Ueyama         v = readInputSectionsList();
6762ec34544SRui Ueyama         expect(")");
6772ec34544SRui Ueyama       } else {
6783837f427SRui Ueyama         v = readInputSectionsList();
6792ec34544SRui Ueyama       }
6802ec34544SRui Ueyama       expect(")");
6812ec34544SRui Ueyama     } else {
6823837f427SRui Ueyama       v = readInputSectionsList();
6832ec34544SRui Ueyama     }
6842ec34544SRui Ueyama 
6853837f427SRui Ueyama     for (SectionPattern &pat : v) {
6863837f427SRui Ueyama       pat.sortInner = inner;
6873837f427SRui Ueyama       pat.sortOuter = outer;
6882ec34544SRui Ueyama     }
6892ec34544SRui Ueyama 
6903837f427SRui Ueyama     std::move(v.begin(), v.end(), std::back_inserter(cmd->sectionPatterns));
6912ec34544SRui Ueyama   }
6923837f427SRui Ueyama   return cmd;
6932ec34544SRui Ueyama }
6942ec34544SRui Ueyama 
6952ec34544SRui Ueyama InputSectionDescription *
6963837f427SRui Ueyama ScriptParser::readInputSectionDescription(StringRef tok) {
6972ec34544SRui Ueyama   // Input section wildcard can be surrounded by KEEP.
6982ec34544SRui Ueyama   // https://sourceware.org/binutils/docs/ld/Input-Section-Keep.html#Input-Section-Keep
699dbd0ad33SPeter Smith   uint64_t withFlags = 0;
700dbd0ad33SPeter Smith   uint64_t withoutFlags = 0;
7013837f427SRui Ueyama   if (tok == "KEEP") {
7022ec34544SRui Ueyama     expect("(");
703dbd0ad33SPeter Smith     if (consume("INPUT_SECTION_FLAGS"))
704dbd0ad33SPeter Smith       std::tie(withFlags, withoutFlags) = readInputSectionFlags();
705dbd0ad33SPeter Smith     InputSectionDescription *cmd =
706dbd0ad33SPeter Smith         readInputSectionRules(next(), withFlags, withoutFlags);
7072ec34544SRui Ueyama     expect(")");
7083837f427SRui Ueyama     script->keptSections.push_back(cmd);
7093837f427SRui Ueyama     return cmd;
7102ec34544SRui Ueyama   }
711dbd0ad33SPeter Smith   if (tok == "INPUT_SECTION_FLAGS") {
712dbd0ad33SPeter Smith     std::tie(withFlags, withoutFlags) = readInputSectionFlags();
713dbd0ad33SPeter Smith     tok = next();
714dbd0ad33SPeter Smith   }
715dbd0ad33SPeter Smith   return readInputSectionRules(tok, withFlags, withoutFlags);
7162ec34544SRui Ueyama }
7172ec34544SRui Ueyama 
7182ec34544SRui Ueyama void ScriptParser::readSort() {
7192ec34544SRui Ueyama   expect("(");
7202ec34544SRui Ueyama   expect("CONSTRUCTORS");
7212ec34544SRui Ueyama   expect(")");
7222ec34544SRui Ueyama }
7232ec34544SRui Ueyama 
724d30a78b3SGeorge Rimar Expr ScriptParser::readAssert() {
7252ec34544SRui Ueyama   expect("(");
7263837f427SRui Ueyama   Expr e = readExpr();
7272ec34544SRui Ueyama   expect(",");
7283837f427SRui Ueyama   StringRef msg = unquote(next());
7292ec34544SRui Ueyama   expect(")");
730b579c439SRui Ueyama 
7312ec34544SRui Ueyama   return [=] {
7323837f427SRui Ueyama     if (!e().getValue())
7332682bc3cSFangrui Song       errorOrWarn(msg);
7343837f427SRui Ueyama     return script->getDot();
7352ec34544SRui Ueyama   };
7362ec34544SRui Ueyama }
7372ec34544SRui Ueyama 
738a46d08ebSGeorge Rimar // Tries to read the special directive for an output section definition which
739a46d08ebSGeorge Rimar // can be one of following: "(NOLOAD)", "(COPY)", "(INFO)" or "(OVERLAY)".
740a46d08ebSGeorge Rimar // Tok1 and Tok2 are next 2 tokens peeked. See comment for readSectionAddressType below.
7413837f427SRui Ueyama bool ScriptParser::readSectionDirective(OutputSection *cmd, StringRef tok1, StringRef tok2) {
7423837f427SRui Ueyama   if (tok1 != "(")
743a46d08ebSGeorge Rimar     return false;
7443837f427SRui Ueyama   if (tok2 != "NOLOAD" && tok2 != "COPY" && tok2 != "INFO" && tok2 != "OVERLAY")
745a46d08ebSGeorge Rimar     return false;
746a46d08ebSGeorge Rimar 
747a46d08ebSGeorge Rimar   expect("(");
748a46d08ebSGeorge Rimar   if (consume("NOLOAD")) {
7493837f427SRui Ueyama     cmd->noload = true;
750fdc41aa2SMatt Schulte     cmd->type = SHT_NOBITS;
751a46d08ebSGeorge Rimar   } else {
752a46d08ebSGeorge Rimar     skip(); // This is "COPY", "INFO" or "OVERLAY".
7533837f427SRui Ueyama     cmd->nonAlloc = true;
754a46d08ebSGeorge Rimar   }
755a46d08ebSGeorge Rimar   expect(")");
756a46d08ebSGeorge Rimar   return true;
757a46d08ebSGeorge Rimar }
758a46d08ebSGeorge Rimar 
7591c08e9f5SGeorge Rimar // Reads an expression and/or the special directive for an output
7601c08e9f5SGeorge Rimar // section definition. Directive is one of following: "(NOLOAD)",
7611c08e9f5SGeorge Rimar // "(COPY)", "(INFO)" or "(OVERLAY)".
7623271d370SRui Ueyama //
7633271d370SRui Ueyama // An output section name can be followed by an address expression
7641c08e9f5SGeorge Rimar // and/or directive. This grammar is not LL(1) because "(" can be
76597f4d158SGeorge Rimar // interpreted as either the beginning of some expression or beginning
7661c08e9f5SGeorge Rimar // of directive.
7673271d370SRui Ueyama //
768b579c439SRui Ueyama // https://sourceware.org/binutils/docs/ld/Output-Section-Address.html
769fbb0463fSGeorge Rimar // https://sourceware.org/binutils/docs/ld/Output-Section-Type.html
7703837f427SRui Ueyama void ScriptParser::readSectionAddressType(OutputSection *cmd) {
7713837f427SRui Ueyama   if (readSectionDirective(cmd, peek(), peek2()))
7723271d370SRui Ueyama     return;
7733271d370SRui Ueyama 
7743837f427SRui Ueyama   cmd->addrExpr = readExpr();
7753837f427SRui Ueyama   if (peek() == "(" && !readSectionDirective(cmd, "(", peek2()))
776a46d08ebSGeorge Rimar     setError("unknown section directive: " + peek2());
777fbb0463fSGeorge Rimar }
778fbb0463fSGeorge Rimar 
7793837f427SRui Ueyama static Expr checkAlignment(Expr e, std::string &loc) {
780f22ec9ddSGeorge Rimar   return [=] {
7813837f427SRui Ueyama     uint64_t alignment = std::max((uint64_t)1, e().getValue());
7823837f427SRui Ueyama     if (!isPowerOf2_64(alignment)) {
7833837f427SRui Ueyama       error(loc + ": alignment must be power of 2");
784f22ec9ddSGeorge Rimar       return (uint64_t)1; // Return a dummy value.
785f22ec9ddSGeorge Rimar     }
7863837f427SRui Ueyama     return alignment;
787f22ec9ddSGeorge Rimar   };
788f22ec9ddSGeorge Rimar }
789f22ec9ddSGeorge Rimar 
790a582419aSGeorge Rimar OutputSection *ScriptParser::readOverlaySectionDescription() {
7913837f427SRui Ueyama   OutputSection *cmd =
7923837f427SRui Ueyama       script->createOutputSection(next(), getCurrentLocation());
7933837f427SRui Ueyama   cmd->inOverlay = true;
794a582419aSGeorge Rimar   expect("{");
795dbd0ad33SPeter Smith   while (!errorCount() && !consume("}")) {
796dbd0ad33SPeter Smith     uint64_t withFlags = 0;
797dbd0ad33SPeter Smith     uint64_t withoutFlags = 0;
798dbd0ad33SPeter Smith     if (consume("INPUT_SECTION_FLAGS"))
799dbd0ad33SPeter Smith       std::tie(withFlags, withoutFlags) = readInputSectionFlags();
800dbd0ad33SPeter Smith     cmd->sectionCommands.push_back(
801dbd0ad33SPeter Smith         readInputSectionRules(next(), withFlags, withoutFlags));
802dbd0ad33SPeter Smith   }
8033837f427SRui Ueyama   return cmd;
804a582419aSGeorge Rimar }
805a582419aSGeorge Rimar 
8063837f427SRui Ueyama OutputSection *ScriptParser::readOutputSectionDescription(StringRef outSec) {
8073837f427SRui Ueyama   OutputSection *cmd =
8083837f427SRui Ueyama       script->createOutputSection(outSec, getCurrentLocation());
8093271d370SRui Ueyama 
8103837f427SRui Ueyama   size_t symbolsReferenced = script->referencedSymbols.size();
811c4df670dSGeorge Rimar 
8123271d370SRui Ueyama   if (peek() != ":")
8133837f427SRui Ueyama     readSectionAddressType(cmd);
8142ec34544SRui Ueyama   expect(":");
8152ec34544SRui Ueyama 
8163837f427SRui Ueyama   std::string location = getCurrentLocation();
8172ec34544SRui Ueyama   if (consume("AT"))
8183837f427SRui Ueyama     cmd->lmaExpr = readParenExpr();
8192ec34544SRui Ueyama   if (consume("ALIGN"))
8203837f427SRui Ueyama     cmd->alignExpr = checkAlignment(readParenExpr(), location);
8212ec34544SRui Ueyama   if (consume("SUBALIGN"))
8223837f427SRui Ueyama     cmd->subalignExpr = checkAlignment(readParenExpr(), location);
8232ec34544SRui Ueyama 
8242ec34544SRui Ueyama   // Parse constraints.
8252ec34544SRui Ueyama   if (consume("ONLY_IF_RO"))
8263837f427SRui Ueyama     cmd->constraint = ConstraintKind::ReadOnly;
8272ec34544SRui Ueyama   if (consume("ONLY_IF_RW"))
8283837f427SRui Ueyama     cmd->constraint = ConstraintKind::ReadWrite;
8292ec34544SRui Ueyama   expect("{");
8302ec34544SRui Ueyama 
831b8a59c8aSBob Haarman   while (!errorCount() && !consume("}")) {
8323837f427SRui Ueyama     StringRef tok = next();
8333837f427SRui Ueyama     if (tok == ";") {
8342ec34544SRui Ueyama       // Empty commands are allowed. Do nothing here.
8353837f427SRui Ueyama     } else if (SymbolAssignment *assign = readAssignment(tok)) {
8363837f427SRui Ueyama       cmd->sectionCommands.push_back(assign);
8373837f427SRui Ueyama     } else if (ByteCommand *data = readByteCommand(tok)) {
8383837f427SRui Ueyama       cmd->sectionCommands.push_back(data);
8393837f427SRui Ueyama     } else if (tok == "CONSTRUCTORS") {
8402ec34544SRui Ueyama       // CONSTRUCTORS is a keyword to make the linker recognize C++ ctors/dtors
8412ec34544SRui Ueyama       // by name. This is for very old file formats such as ECOFF/XCOFF.
8422ec34544SRui Ueyama       // For ELF, we should ignore.
8433837f427SRui Ueyama     } else if (tok == "FILL") {
8440810f16fSGeorge Rimar       // We handle the FILL command as an alias for =fillexp section attribute,
8450810f16fSGeorge Rimar       // which is different from what GNU linkers do.
8460810f16fSGeorge Rimar       // https://sourceware.org/binutils/docs/ld/Output-Section-Data.html
847bb7d2b17SGeorgii Rymar       if (peek() != "(")
848bb7d2b17SGeorgii Rymar         setError("( expected, but got " + peek());
8493837f427SRui Ueyama       cmd->filler = readFill();
8503837f427SRui Ueyama     } else if (tok == "SORT") {
8512ec34544SRui Ueyama       readSort();
8523837f427SRui Ueyama     } else if (tok == "INCLUDE") {
8532e9d40d5SRui Ueyama       readInclude();
8542ec34544SRui Ueyama     } else if (peek() == "(") {
8553837f427SRui Ueyama       cmd->sectionCommands.push_back(readInputSectionDescription(tok));
8562ec34544SRui Ueyama     } else {
857f49fe218SGeorge Rimar       // We have a file name and no input sections description. It is not a
858f49fe218SGeorge Rimar       // commonly used syntax, but still acceptable. In that case, all sections
859f49fe218SGeorge Rimar       // from the file will be included.
860dbd0ad33SPeter Smith       // FIXME: GNU ld permits INPUT_SECTION_FLAGS to be used here. We do not
861dbd0ad33SPeter Smith       // handle this case here as it will already have been matched by the
862dbd0ad33SPeter Smith       // case above.
8633837f427SRui Ueyama       auto *isd = make<InputSectionDescription>(tok);
864c42fe247SThomas Preud'homme       isd->sectionPatterns.push_back({{}, StringMatcher("*")});
8653837f427SRui Ueyama       cmd->sectionCommands.push_back(isd);
8662ec34544SRui Ueyama     }
8672ec34544SRui Ueyama   }
8682ec34544SRui Ueyama 
8692ec34544SRui Ueyama   if (consume(">"))
870adcd0268SBenjamin Kramer     cmd->memoryRegionName = std::string(next());
8712ec34544SRui Ueyama 
8725d01a8beSGeorge Rimar   if (consume("AT")) {
8735d01a8beSGeorge Rimar     expect(">");
874adcd0268SBenjamin Kramer     cmd->lmaRegionName = std::string(next());
8755d01a8beSGeorge Rimar   }
8765d01a8beSGeorge Rimar 
8773837f427SRui Ueyama   if (cmd->lmaExpr && !cmd->lmaRegionName.empty())
8785d01a8beSGeorge Rimar     error("section can't have both LMA and a load region");
8795d01a8beSGeorge Rimar 
8803837f427SRui Ueyama   cmd->phdrs = readOutputSectionPhdrs();
8812ec34544SRui Ueyama 
8820810f16fSGeorge Rimar   if (peek() == "=" || peek().startswith("=")) {
8833837f427SRui Ueyama     inExpr = true;
8840810f16fSGeorge Rimar     consume("=");
8853837f427SRui Ueyama     cmd->filler = readFill();
8863837f427SRui Ueyama     inExpr = false;
8870810f16fSGeorge Rimar   }
8882ec34544SRui Ueyama 
8892ec34544SRui Ueyama   // Consume optional comma following output section command.
8902ec34544SRui Ueyama   consume(",");
8912ec34544SRui Ueyama 
8923837f427SRui Ueyama   if (script->referencedSymbols.size() > symbolsReferenced)
8933837f427SRui Ueyama     cmd->expressionsUseSymbols = true;
8943837f427SRui Ueyama   return cmd;
8952ec34544SRui Ueyama }
8962ec34544SRui Ueyama 
8970810f16fSGeorge Rimar // Reads a `=<fillexp>` expression and returns its value as a big-endian number.
8982ec34544SRui Ueyama // https://sourceware.org/binutils/docs/ld/Output-Section-Fill.html
8990810f16fSGeorge Rimar // We do not support using symbols in such expressions.
9002ec34544SRui Ueyama //
9018acbf1ccSRui Ueyama // When reading a hexstring, ld.bfd handles it as a blob of arbitrary
9028acbf1ccSRui Ueyama // size, while ld.gold always handles it as a 32-bit big-endian number.
9038acbf1ccSRui Ueyama // We are compatible with ld.gold because it's easier to implement.
904bb7d2b17SGeorgii Rymar // Also, we require that expressions with operators must be wrapped into
905bb7d2b17SGeorgii Rymar // round brackets. We did it to resolve the ambiguity when parsing scripts like:
906bb7d2b17SGeorgii Rymar // SECTIONS { .foo : { ... } =120+3 /DISCARD/ : { ... } }
9070810f16fSGeorge Rimar std::array<uint8_t, 4> ScriptParser::readFill() {
908bb7d2b17SGeorgii Rymar   uint64_t value = readPrimary()().val;
9093837f427SRui Ueyama   if (value > UINT32_MAX)
9100810f16fSGeorge Rimar     setError("filler expression result does not fit 32-bit: 0x" +
9113837f427SRui Ueyama              Twine::utohexstr(value));
912b58079d4SRui Ueyama 
9133837f427SRui Ueyama   std::array<uint8_t, 4> buf;
9143837f427SRui Ueyama   write32be(buf.data(), (uint32_t)value);
9153837f427SRui Ueyama   return buf;
9162ec34544SRui Ueyama }
9172ec34544SRui Ueyama 
9183837f427SRui Ueyama SymbolAssignment *ScriptParser::readProvideHidden(bool provide, bool hidden) {
9192ec34544SRui Ueyama   expect("(");
9203837f427SRui Ueyama   SymbolAssignment *cmd = readSymbolAssignment(next());
9213837f427SRui Ueyama   cmd->provide = provide;
9223837f427SRui Ueyama   cmd->hidden = hidden;
9232ec34544SRui Ueyama   expect(")");
9243837f427SRui Ueyama   return cmd;
9252ec34544SRui Ueyama }
9262ec34544SRui Ueyama 
9273837f427SRui Ueyama SymbolAssignment *ScriptParser::readAssignment(StringRef tok) {
928d30a78b3SGeorge Rimar   // Assert expression returns Dot, so this is equal to ".=."
9293837f427SRui Ueyama   if (tok == "ASSERT")
930d30a78b3SGeorge Rimar     return make<SymbolAssignment>(".", readAssert(), getCurrentLocation());
931d30a78b3SGeorge Rimar 
9323837f427SRui Ueyama   size_t oldPos = pos;
9333837f427SRui Ueyama   SymbolAssignment *cmd = nullptr;
934e88b76a9SGeorge Rimar   if (peek() == "=" || peek() == "+=")
9353837f427SRui Ueyama     cmd = readSymbolAssignment(tok);
9363837f427SRui Ueyama   else if (tok == "PROVIDE")
9373837f427SRui Ueyama     cmd = readProvideHidden(true, false);
9383837f427SRui Ueyama   else if (tok == "HIDDEN")
9393837f427SRui Ueyama     cmd = readProvideHidden(false, true);
9403837f427SRui Ueyama   else if (tok == "PROVIDE_HIDDEN")
9413837f427SRui Ueyama     cmd = readProvideHidden(true, true);
942e88b76a9SGeorge Rimar 
9433837f427SRui Ueyama   if (cmd) {
9443837f427SRui Ueyama     cmd->commandString =
9453837f427SRui Ueyama         tok.str() + " " +
9463837f427SRui Ueyama         llvm::join(tokens.begin() + oldPos, tokens.begin() + pos, " ");
947e88b76a9SGeorge Rimar     expect(";");
9482ec34544SRui Ueyama   }
9493837f427SRui Ueyama   return cmd;
9502ec34544SRui Ueyama }
9512ec34544SRui Ueyama 
9523837f427SRui Ueyama SymbolAssignment *ScriptParser::readSymbolAssignment(StringRef name) {
9533837f427SRui Ueyama   StringRef op = next();
9543837f427SRui Ueyama   assert(op == "=" || op == "+=");
9553837f427SRui Ueyama   Expr e = readExpr();
9563837f427SRui Ueyama   if (op == "+=") {
9573837f427SRui Ueyama     std::string loc = getCurrentLocation();
9583837f427SRui Ueyama     e = [=] { return add(script->getSymbolValue(name, loc), e()); };
9592ec34544SRui Ueyama   }
9603837f427SRui Ueyama   return make<SymbolAssignment>(name, e, getCurrentLocation());
9612ec34544SRui Ueyama }
9622ec34544SRui Ueyama 
9632ec34544SRui Ueyama // This is an operator-precedence parser to parse a linker
9642ec34544SRui Ueyama // script expression.
9652ec34544SRui Ueyama Expr ScriptParser::readExpr() {
9662ec34544SRui Ueyama   // Our lexer is context-aware. Set the in-expression bit so that
9672ec34544SRui Ueyama   // they apply different tokenization rules.
9683837f427SRui Ueyama   bool orig = inExpr;
9693837f427SRui Ueyama   inExpr = true;
9703837f427SRui Ueyama   Expr e = readExpr1(readPrimary(), 0);
9713837f427SRui Ueyama   inExpr = orig;
9723837f427SRui Ueyama   return e;
9732ec34544SRui Ueyama }
9742ec34544SRui Ueyama 
9753837f427SRui Ueyama Expr ScriptParser::combine(StringRef op, Expr l, Expr r) {
9763837f427SRui Ueyama   if (op == "+")
9773837f427SRui Ueyama     return [=] { return add(l(), r()); };
9783837f427SRui Ueyama   if (op == "-")
9793837f427SRui Ueyama     return [=] { return sub(l(), r()); };
9803837f427SRui Ueyama   if (op == "*")
9813837f427SRui Ueyama     return [=] { return l().getValue() * r().getValue(); };
9823837f427SRui Ueyama   if (op == "/") {
9833837f427SRui Ueyama     std::string loc = getCurrentLocation();
9847b91e213SGeorge Rimar     return [=]() -> uint64_t {
9853837f427SRui Ueyama       if (uint64_t rv = r().getValue())
9863837f427SRui Ueyama         return l().getValue() / rv;
9873837f427SRui Ueyama       error(loc + ": division by zero");
988067617f9SRui Ueyama       return 0;
9897b91e213SGeorge Rimar     };
9907b91e213SGeorge Rimar   }
9913837f427SRui Ueyama   if (op == "%") {
9923837f427SRui Ueyama     std::string loc = getCurrentLocation();
9937b91e213SGeorge Rimar     return [=]() -> uint64_t {
9943837f427SRui Ueyama       if (uint64_t rv = r().getValue())
9953837f427SRui Ueyama         return l().getValue() % rv;
9963837f427SRui Ueyama       error(loc + ": modulo by zero");
997067617f9SRui Ueyama       return 0;
9987b91e213SGeorge Rimar     };
9997b91e213SGeorge Rimar   }
10003837f427SRui Ueyama   if (op == "<<")
10013837f427SRui Ueyama     return [=] { return l().getValue() << r().getValue(); };
10023837f427SRui Ueyama   if (op == ">>")
10033837f427SRui Ueyama     return [=] { return l().getValue() >> r().getValue(); };
10043837f427SRui Ueyama   if (op == "<")
10053837f427SRui Ueyama     return [=] { return l().getValue() < r().getValue(); };
10063837f427SRui Ueyama   if (op == ">")
10073837f427SRui Ueyama     return [=] { return l().getValue() > r().getValue(); };
10083837f427SRui Ueyama   if (op == ">=")
10093837f427SRui Ueyama     return [=] { return l().getValue() >= r().getValue(); };
10103837f427SRui Ueyama   if (op == "<=")
10113837f427SRui Ueyama     return [=] { return l().getValue() <= r().getValue(); };
10123837f427SRui Ueyama   if (op == "==")
10133837f427SRui Ueyama     return [=] { return l().getValue() == r().getValue(); };
10143837f427SRui Ueyama   if (op == "!=")
10153837f427SRui Ueyama     return [=] { return l().getValue() != r().getValue(); };
10163837f427SRui Ueyama   if (op == "||")
10173837f427SRui Ueyama     return [=] { return l().getValue() || r().getValue(); };
10183837f427SRui Ueyama   if (op == "&&")
10193837f427SRui Ueyama     return [=] { return l().getValue() && r().getValue(); };
10203837f427SRui Ueyama   if (op == "&")
10213837f427SRui Ueyama     return [=] { return bitAnd(l(), r()); };
10223837f427SRui Ueyama   if (op == "|")
10233837f427SRui Ueyama     return [=] { return bitOr(l(), r()); };
10242ec34544SRui Ueyama   llvm_unreachable("invalid operator");
10252ec34544SRui Ueyama }
10262ec34544SRui Ueyama 
10272ec34544SRui Ueyama // This is a part of the operator-precedence parser. This function
10282ec34544SRui Ueyama // assumes that the remaining token stream starts with an operator.
10293837f427SRui Ueyama Expr ScriptParser::readExpr1(Expr lhs, int minPrec) {
1030b8a59c8aSBob Haarman   while (!atEOF() && !errorCount()) {
10312ec34544SRui Ueyama     // Read an operator and an expression.
10322ec34544SRui Ueyama     if (consume("?"))
10333837f427SRui Ueyama       return readTernary(lhs);
10343837f427SRui Ueyama     StringRef op1 = peek();
10353837f427SRui Ueyama     if (precedence(op1) < minPrec)
10362ec34544SRui Ueyama       break;
10372ec34544SRui Ueyama     skip();
10383837f427SRui Ueyama     Expr rhs = readPrimary();
10392ec34544SRui Ueyama 
10402ec34544SRui Ueyama     // Evaluate the remaining part of the expression first if the
10412ec34544SRui Ueyama     // next operator has greater precedence than the previous one.
10422ec34544SRui Ueyama     // For example, if we have read "+" and "3", and if the next
10432ec34544SRui Ueyama     // operator is "*", then we'll evaluate 3 * ... part first.
10442ec34544SRui Ueyama     while (!atEOF()) {
10453837f427SRui Ueyama       StringRef op2 = peek();
10463837f427SRui Ueyama       if (precedence(op2) <= precedence(op1))
10472ec34544SRui Ueyama         break;
10483837f427SRui Ueyama       rhs = readExpr1(rhs, precedence(op2));
10492ec34544SRui Ueyama     }
10502ec34544SRui Ueyama 
10513837f427SRui Ueyama     lhs = combine(op1, lhs, rhs);
10522ec34544SRui Ueyama   }
10533837f427SRui Ueyama   return lhs;
10542ec34544SRui Ueyama }
10552ec34544SRui Ueyama 
10565fb17128SGeorge Rimar Expr ScriptParser::getPageSize() {
10573837f427SRui Ueyama   std::string location = getCurrentLocation();
10585fb17128SGeorge Rimar   return [=]() -> uint64_t {
10593837f427SRui Ueyama     if (target)
10603837f427SRui Ueyama       return config->commonPageSize;
10613837f427SRui Ueyama     error(location + ": unable to calculate page size");
10625fb17128SGeorge Rimar     return 4096; // Return a dummy value.
10635fb17128SGeorge Rimar   };
10645fb17128SGeorge Rimar }
10655fb17128SGeorge Rimar 
10665fb17128SGeorge Rimar Expr ScriptParser::readConstant() {
10673837f427SRui Ueyama   StringRef s = readParenLiteral();
10683837f427SRui Ueyama   if (s == "COMMONPAGESIZE")
10695fb17128SGeorge Rimar     return getPageSize();
10703837f427SRui Ueyama   if (s == "MAXPAGESIZE")
10713837f427SRui Ueyama     return [] { return config->maxPageSize; };
10723837f427SRui Ueyama   setError("unknown constant: " + s);
1073b068b037SGeorge Rimar   return [] { return 0; };
10742ec34544SRui Ueyama }
10752ec34544SRui Ueyama 
10765c65088fSRui Ueyama // Parses Tok as an integer. It recognizes hexadecimal (prefixed with
10775c65088fSRui Ueyama // "0x" or suffixed with "H") and decimal numbers. Decimal numbers may
10785c65088fSRui Ueyama // have "K" (Ki) or "M" (Mi) suffixes.
10793837f427SRui Ueyama static Optional<uint64_t> parseInt(StringRef tok) {
10802ec34544SRui Ueyama   // Hexadecimal
10813837f427SRui Ueyama   uint64_t val;
10823837f427SRui Ueyama   if (tok.startswith_lower("0x")) {
10833837f427SRui Ueyama     if (!to_integer(tok.substr(2), val, 16))
10844092016bSRui Ueyama       return None;
10853837f427SRui Ueyama     return val;
10864092016bSRui Ueyama   }
10873837f427SRui Ueyama   if (tok.endswith_lower("H")) {
10883837f427SRui Ueyama     if (!to_integer(tok.drop_back(), val, 16))
10894092016bSRui Ueyama       return None;
10903837f427SRui Ueyama     return val;
10914092016bSRui Ueyama   }
10922ec34544SRui Ueyama 
10932ec34544SRui Ueyama   // Decimal
10943837f427SRui Ueyama   if (tok.endswith_lower("K")) {
10953837f427SRui Ueyama     if (!to_integer(tok.drop_back(), val, 10))
10965c65088fSRui Ueyama       return None;
10973837f427SRui Ueyama     return val * 1024;
10982ec34544SRui Ueyama   }
10993837f427SRui Ueyama   if (tok.endswith_lower("M")) {
11003837f427SRui Ueyama     if (!to_integer(tok.drop_back(), val, 10))
11015c65088fSRui Ueyama       return None;
11023837f427SRui Ueyama     return val * 1024 * 1024;
11035c65088fSRui Ueyama   }
11043837f427SRui Ueyama   if (!to_integer(tok, val, 10))
11055c65088fSRui Ueyama     return None;
11063837f427SRui Ueyama   return val;
11072ec34544SRui Ueyama }
11082ec34544SRui Ueyama 
11093837f427SRui Ueyama ByteCommand *ScriptParser::readByteCommand(StringRef tok) {
11103837f427SRui Ueyama   int size = StringSwitch<int>(tok)
11112ec34544SRui Ueyama                  .Case("BYTE", 1)
11122ec34544SRui Ueyama                  .Case("SHORT", 2)
11132ec34544SRui Ueyama                  .Case("LONG", 4)
11142ec34544SRui Ueyama                  .Case("QUAD", 8)
11152ec34544SRui Ueyama                  .Default(-1);
11163837f427SRui Ueyama   if (size == -1)
11172ec34544SRui Ueyama     return nullptr;
111884bcabcbSGeorge Rimar 
11193837f427SRui Ueyama   size_t oldPos = pos;
11203837f427SRui Ueyama   Expr e = readParenExpr();
11213837f427SRui Ueyama   std::string commandString =
11223837f427SRui Ueyama       tok.str() + " " +
11233837f427SRui Ueyama       llvm::join(tokens.begin() + oldPos, tokens.begin() + pos, " ");
11243837f427SRui Ueyama   return make<ByteCommand>(e, size, commandString);
11252ec34544SRui Ueyama }
11262ec34544SRui Ueyama 
1127dbd0ad33SPeter Smith static llvm::Optional<uint64_t> parseFlag(StringRef tok) {
1128dbd0ad33SPeter Smith   if (llvm::Optional<uint64_t> asInt = parseInt(tok))
1129dbd0ad33SPeter Smith     return asInt;
1130dbd0ad33SPeter Smith #define CASE_ENT(enum) #enum, ELF::enum
1131dbd0ad33SPeter Smith   return StringSwitch<llvm::Optional<uint64_t>>(tok)
1132dbd0ad33SPeter Smith       .Case(CASE_ENT(SHF_WRITE))
1133dbd0ad33SPeter Smith       .Case(CASE_ENT(SHF_ALLOC))
1134dbd0ad33SPeter Smith       .Case(CASE_ENT(SHF_EXECINSTR))
1135dbd0ad33SPeter Smith       .Case(CASE_ENT(SHF_MERGE))
1136dbd0ad33SPeter Smith       .Case(CASE_ENT(SHF_STRINGS))
1137dbd0ad33SPeter Smith       .Case(CASE_ENT(SHF_INFO_LINK))
1138dbd0ad33SPeter Smith       .Case(CASE_ENT(SHF_LINK_ORDER))
1139dbd0ad33SPeter Smith       .Case(CASE_ENT(SHF_OS_NONCONFORMING))
1140dbd0ad33SPeter Smith       .Case(CASE_ENT(SHF_GROUP))
1141dbd0ad33SPeter Smith       .Case(CASE_ENT(SHF_TLS))
1142dbd0ad33SPeter Smith       .Case(CASE_ENT(SHF_COMPRESSED))
1143dbd0ad33SPeter Smith       .Case(CASE_ENT(SHF_EXCLUDE))
1144dbd0ad33SPeter Smith       .Case(CASE_ENT(SHF_ARM_PURECODE))
1145dbd0ad33SPeter Smith       .Default(None);
1146dbd0ad33SPeter Smith #undef CASE_ENT
1147dbd0ad33SPeter Smith }
1148dbd0ad33SPeter Smith 
1149dbd0ad33SPeter Smith // Reads the '(' <flags> ')' list of section flags in
1150dbd0ad33SPeter Smith // INPUT_SECTION_FLAGS '(' <flags> ')' in the
1151dbd0ad33SPeter Smith // following form:
1152dbd0ad33SPeter Smith // <flags> ::= <flag>
1153dbd0ad33SPeter Smith //           | <flags> & flag
1154dbd0ad33SPeter Smith // <flag>  ::= Recognized Flag Name, or Integer value of flag.
1155dbd0ad33SPeter Smith // If the first character of <flag> is a ! then this means without flag,
1156dbd0ad33SPeter Smith // otherwise with flag.
1157dbd0ad33SPeter Smith // Example: SHF_EXECINSTR & !SHF_WRITE means with flag SHF_EXECINSTR and
1158dbd0ad33SPeter Smith // without flag SHF_WRITE.
1159dbd0ad33SPeter Smith std::pair<uint64_t, uint64_t> ScriptParser::readInputSectionFlags() {
1160dbd0ad33SPeter Smith    uint64_t withFlags = 0;
1161dbd0ad33SPeter Smith    uint64_t withoutFlags = 0;
1162dbd0ad33SPeter Smith    expect("(");
1163dbd0ad33SPeter Smith    while (!errorCount()) {
1164dbd0ad33SPeter Smith     StringRef tok = unquote(next());
1165dbd0ad33SPeter Smith     bool without = tok.consume_front("!");
1166dbd0ad33SPeter Smith     if (llvm::Optional<uint64_t> flag = parseFlag(tok)) {
1167dbd0ad33SPeter Smith       if (without)
1168dbd0ad33SPeter Smith         withoutFlags |= *flag;
1169dbd0ad33SPeter Smith       else
1170dbd0ad33SPeter Smith         withFlags |= *flag;
1171dbd0ad33SPeter Smith     } else {
1172dbd0ad33SPeter Smith       setError("unrecognised flag: " + tok);
1173dbd0ad33SPeter Smith     }
1174dbd0ad33SPeter Smith     if (consume(")"))
1175dbd0ad33SPeter Smith       break;
1176dbd0ad33SPeter Smith     if (!consume("&")) {
1177dbd0ad33SPeter Smith       next();
1178dbd0ad33SPeter Smith       setError("expected & or )");
1179dbd0ad33SPeter Smith     }
1180dbd0ad33SPeter Smith   }
1181dbd0ad33SPeter Smith   return std::make_pair(withFlags, withoutFlags);
1182dbd0ad33SPeter Smith }
1183dbd0ad33SPeter Smith 
11842ec34544SRui Ueyama StringRef ScriptParser::readParenLiteral() {
11852ec34544SRui Ueyama   expect("(");
11863837f427SRui Ueyama   bool orig = inExpr;
11873837f427SRui Ueyama   inExpr = false;
11883837f427SRui Ueyama   StringRef tok = next();
11893837f427SRui Ueyama   inExpr = orig;
11902ec34544SRui Ueyama   expect(")");
11913837f427SRui Ueyama   return tok;
11922ec34544SRui Ueyama }
11932ec34544SRui Ueyama 
11943837f427SRui Ueyama static void checkIfExists(OutputSection *cmd, StringRef location) {
11953837f427SRui Ueyama   if (cmd->location.empty() && script->errorOnMissingSection)
11963837f427SRui Ueyama     error(location + ": undefined section " + cmd->name);
119705c4f67cSRafael Espindola }
119805c4f67cSRafael Espindola 
11992ec34544SRui Ueyama Expr ScriptParser::readPrimary() {
12002ec34544SRui Ueyama   if (peek() == "(")
12012ec34544SRui Ueyama     return readParenExpr();
12022ec34544SRui Ueyama 
12035c65088fSRui Ueyama   if (consume("~")) {
12043837f427SRui Ueyama     Expr e = readPrimary();
12053837f427SRui Ueyama     return [=] { return ~e().getValue(); };
12062ec34544SRui Ueyama   }
12076f1d954eSHafiz Abid Qadeer   if (consume("!")) {
12083837f427SRui Ueyama     Expr e = readPrimary();
12093837f427SRui Ueyama     return [=] { return !e().getValue(); };
12106f1d954eSHafiz Abid Qadeer   }
12115c65088fSRui Ueyama   if (consume("-")) {
12123837f427SRui Ueyama     Expr e = readPrimary();
12133837f427SRui Ueyama     return [=] { return -e().getValue(); };
12142ec34544SRui Ueyama   }
12152ec34544SRui Ueyama 
12163837f427SRui Ueyama   StringRef tok = next();
12173837f427SRui Ueyama   std::string location = getCurrentLocation();
12185c65088fSRui Ueyama 
12192ec34544SRui Ueyama   // Built-in functions are parsed here.
12202ec34544SRui Ueyama   // https://sourceware.org/binutils/docs/ld/Builtin-Functions.html.
12213837f427SRui Ueyama   if (tok == "ABSOLUTE") {
12223837f427SRui Ueyama     Expr inner = readParenExpr();
12232ec34544SRui Ueyama     return [=] {
12243837f427SRui Ueyama       ExprValue i = inner();
12253837f427SRui Ueyama       i.forceAbsolute = true;
12263837f427SRui Ueyama       return i;
12272ec34544SRui Ueyama     };
12282ec34544SRui Ueyama   }
12293837f427SRui Ueyama   if (tok == "ADDR") {
12303837f427SRui Ueyama     StringRef name = readParenLiteral();
12313837f427SRui Ueyama     OutputSection *sec = script->getOrCreateOutputSection(name);
12323837f427SRui Ueyama     sec->usedInExpression = true;
123341c7ab4aSGeorge Rimar     return [=]() -> ExprValue {
12343837f427SRui Ueyama       checkIfExists(sec, location);
12353837f427SRui Ueyama       return {sec, false, 0, location};
123641c7ab4aSGeorge Rimar     };
12372ec34544SRui Ueyama   }
12383837f427SRui Ueyama   if (tok == "ALIGN") {
12392ec34544SRui Ueyama     expect("(");
12403837f427SRui Ueyama     Expr e = readExpr();
1241f22ec9ddSGeorge Rimar     if (consume(")")) {
12423837f427SRui Ueyama       e = checkAlignment(e, location);
12433837f427SRui Ueyama       return [=] { return alignTo(script->getDot(), e().getValue()); };
1244f22ec9ddSGeorge Rimar     }
1245b579c439SRui Ueyama     expect(",");
12463837f427SRui Ueyama     Expr e2 = checkAlignment(readExpr(), location);
12472ec34544SRui Ueyama     expect(")");
12483c6de1a6SPetr Hosek     return [=] {
12493837f427SRui Ueyama       ExprValue v = e();
12503837f427SRui Ueyama       v.alignment = e2().getValue();
12513837f427SRui Ueyama       return v;
12523c6de1a6SPetr Hosek     };
12532ec34544SRui Ueyama   }
12543837f427SRui Ueyama   if (tok == "ALIGNOF") {
12553837f427SRui Ueyama     StringRef name = readParenLiteral();
12563837f427SRui Ueyama     OutputSection *cmd = script->getOrCreateOutputSection(name);
1257617e2f98SRui Ueyama     return [=] {
12583837f427SRui Ueyama       checkIfExists(cmd, location);
12593837f427SRui Ueyama       return cmd->alignment;
1260617e2f98SRui Ueyama     };
12612ec34544SRui Ueyama   }
12623837f427SRui Ueyama   if (tok == "ASSERT")
1263d30a78b3SGeorge Rimar     return readAssert();
12643837f427SRui Ueyama   if (tok == "CONSTANT")
12655fb17128SGeorge Rimar     return readConstant();
12663837f427SRui Ueyama   if (tok == "DATA_SEGMENT_ALIGN") {
12672ec34544SRui Ueyama     expect("(");
12683837f427SRui Ueyama     Expr e = readExpr();
12692ec34544SRui Ueyama     expect(",");
12702ec34544SRui Ueyama     readExpr();
12712ec34544SRui Ueyama     expect(")");
127260833f6eSGeorge Rimar     return [=] {
12733837f427SRui Ueyama       return alignTo(script->getDot(), std::max((uint64_t)1, e().getValue()));
127460833f6eSGeorge Rimar     };
12752ec34544SRui Ueyama   }
12763837f427SRui Ueyama   if (tok == "DATA_SEGMENT_END") {
12772ec34544SRui Ueyama     expect("(");
12782ec34544SRui Ueyama     expect(".");
12792ec34544SRui Ueyama     expect(")");
12803837f427SRui Ueyama     return [] { return script->getDot(); };
12812ec34544SRui Ueyama   }
12823837f427SRui Ueyama   if (tok == "DATA_SEGMENT_RELRO_END") {
12832ec34544SRui Ueyama     // GNU linkers implements more complicated logic to handle
12842ec34544SRui Ueyama     // DATA_SEGMENT_RELRO_END. We instead ignore the arguments and
12852ec34544SRui Ueyama     // just align to the next page boundary for simplicity.
12862ec34544SRui Ueyama     expect("(");
12872ec34544SRui Ueyama     readExpr();
12882ec34544SRui Ueyama     expect(",");
12892ec34544SRui Ueyama     readExpr();
12902ec34544SRui Ueyama     expect(")");
12913837f427SRui Ueyama     Expr e = getPageSize();
12923837f427SRui Ueyama     return [=] { return alignTo(script->getDot(), e().getValue()); };
12932ec34544SRui Ueyama   }
12943837f427SRui Ueyama   if (tok == "DEFINED") {
12953837f427SRui Ueyama     StringRef name = readParenLiteral();
12963837f427SRui Ueyama     return [=] { return symtab->find(name) ? 1 : 0; };
12972ec34544SRui Ueyama   }
12983837f427SRui Ueyama   if (tok == "LENGTH") {
12993837f427SRui Ueyama     StringRef name = readParenLiteral();
13003837f427SRui Ueyama     if (script->memoryRegions.count(name) == 0) {
13013837f427SRui Ueyama       setError("memory region not defined: " + name);
1302b068b037SGeorge Rimar       return [] { return 0; };
1303b068b037SGeorge Rimar     }
130492b5b980SFangrui Song     return script->memoryRegions[name]->length;
130591b95b61SRui Ueyama   }
13063837f427SRui Ueyama   if (tok == "LOADADDR") {
13073837f427SRui Ueyama     StringRef name = readParenLiteral();
13083837f427SRui Ueyama     OutputSection *cmd = script->getOrCreateOutputSection(name);
13093837f427SRui Ueyama     cmd->usedInExpression = true;
1310617e2f98SRui Ueyama     return [=] {
13113837f427SRui Ueyama       checkIfExists(cmd, location);
13123837f427SRui Ueyama       return cmd->getLMA();
1313617e2f98SRui Ueyama     };
13142ec34544SRui Ueyama   }
13153837f427SRui Ueyama   if (tok == "MAX" || tok == "MIN") {
1316fd11560fSGeorge Rimar     expect("(");
13173837f427SRui Ueyama     Expr a = readExpr();
1318fd11560fSGeorge Rimar     expect(",");
13193837f427SRui Ueyama     Expr b = readExpr();
1320fd11560fSGeorge Rimar     expect(")");
13213837f427SRui Ueyama     if (tok == "MIN")
13223837f427SRui Ueyama       return [=] { return std::min(a().getValue(), b().getValue()); };
13233837f427SRui Ueyama     return [=] { return std::max(a().getValue(), b().getValue()); };
1324fd11560fSGeorge Rimar   }
13253837f427SRui Ueyama   if (tok == "ORIGIN") {
13263837f427SRui Ueyama     StringRef name = readParenLiteral();
13273837f427SRui Ueyama     if (script->memoryRegions.count(name) == 0) {
13283837f427SRui Ueyama       setError("memory region not defined: " + name);
1329b068b037SGeorge Rimar       return [] { return 0; };
1330b068b037SGeorge Rimar     }
133192b5b980SFangrui Song     return script->memoryRegions[name]->origin;
133291b95b61SRui Ueyama   }
13333837f427SRui Ueyama   if (tok == "SEGMENT_START") {
13342ec34544SRui Ueyama     expect("(");
13352ec34544SRui Ueyama     skip();
13362ec34544SRui Ueyama     expect(",");
13373837f427SRui Ueyama     Expr e = readExpr();
13382ec34544SRui Ueyama     expect(")");
13393837f427SRui Ueyama     return [=] { return e(); };
13402ec34544SRui Ueyama   }
13413837f427SRui Ueyama   if (tok == "SIZEOF") {
13423837f427SRui Ueyama     StringRef name = readParenLiteral();
13433837f427SRui Ueyama     OutputSection *cmd = script->getOrCreateOutputSection(name);
134405c4f67cSRafael Espindola     // Linker script does not create an output section if its content is empty.
134505c4f67cSRafael Espindola     // We want to allow SIZEOF(.foo) where .foo is a section which happened to
134605c4f67cSRafael Espindola     // be empty.
13473837f427SRui Ueyama     return [=] { return cmd->size; };
13482ec34544SRui Ueyama   }
13493837f427SRui Ueyama   if (tok == "SIZEOF_HEADERS")
1350bd8cfe65SFangrui Song     return [=] { return getHeaderSize(); };
13512ec34544SRui Ueyama 
13524eb2eccbSRui Ueyama   // Tok is the dot.
13533837f427SRui Ueyama   if (tok == ".")
13543837f427SRui Ueyama     return [=] { return script->getSymbolValue(tok, location); };
13554eb2eccbSRui Ueyama 
13562ec34544SRui Ueyama   // Tok is a literal number.
13573837f427SRui Ueyama   if (Optional<uint64_t> val = parseInt(tok))
13583837f427SRui Ueyama     return [=] { return *val; };
13592ec34544SRui Ueyama 
13602ec34544SRui Ueyama   // Tok is a symbol name.
13613837f427SRui Ueyama   if (!isValidCIdentifier(tok))
13623837f427SRui Ueyama     setError("malformed number: " + tok);
13633837f427SRui Ueyama   script->referencedSymbols.push_back(tok);
13643837f427SRui Ueyama   return [=] { return script->getSymbolValue(tok, location); };
13652ec34544SRui Ueyama }
13662ec34544SRui Ueyama 
13673837f427SRui Ueyama Expr ScriptParser::readTernary(Expr cond) {
13683837f427SRui Ueyama   Expr l = readExpr();
13692ec34544SRui Ueyama   expect(":");
13703837f427SRui Ueyama   Expr r = readExpr();
13713837f427SRui Ueyama   return [=] { return cond().getValue() ? l() : r(); };
13722ec34544SRui Ueyama }
13732ec34544SRui Ueyama 
13742ec34544SRui Ueyama Expr ScriptParser::readParenExpr() {
13752ec34544SRui Ueyama   expect("(");
13763837f427SRui Ueyama   Expr e = readExpr();
13772ec34544SRui Ueyama   expect(")");
13783837f427SRui Ueyama   return e;
13792ec34544SRui Ueyama }
13802ec34544SRui Ueyama 
13812ec34544SRui Ueyama std::vector<StringRef> ScriptParser::readOutputSectionPhdrs() {
13823837f427SRui Ueyama   std::vector<StringRef> phdrs;
1383b8a59c8aSBob Haarman   while (!errorCount() && peek().startswith(":")) {
13843837f427SRui Ueyama     StringRef tok = next();
13853837f427SRui Ueyama     phdrs.push_back((tok.size() == 1) ? next() : tok.substr(1));
13862ec34544SRui Ueyama   }
13873837f427SRui Ueyama   return phdrs;
13882ec34544SRui Ueyama }
13892ec34544SRui Ueyama 
13902ec34544SRui Ueyama // Read a program header type name. The next token must be a
13912ec34544SRui Ueyama // name of a program header type or a constant (e.g. "0x3").
13922ec34544SRui Ueyama unsigned ScriptParser::readPhdrType() {
13933837f427SRui Ueyama   StringRef tok = next();
13943837f427SRui Ueyama   if (Optional<uint64_t> val = parseInt(tok))
13953837f427SRui Ueyama     return *val;
13962ec34544SRui Ueyama 
13973837f427SRui Ueyama   unsigned ret = StringSwitch<unsigned>(tok)
13982ec34544SRui Ueyama                      .Case("PT_NULL", PT_NULL)
13992ec34544SRui Ueyama                      .Case("PT_LOAD", PT_LOAD)
14002ec34544SRui Ueyama                      .Case("PT_DYNAMIC", PT_DYNAMIC)
14012ec34544SRui Ueyama                      .Case("PT_INTERP", PT_INTERP)
14022ec34544SRui Ueyama                      .Case("PT_NOTE", PT_NOTE)
14032ec34544SRui Ueyama                      .Case("PT_SHLIB", PT_SHLIB)
14042ec34544SRui Ueyama                      .Case("PT_PHDR", PT_PHDR)
14052ec34544SRui Ueyama                      .Case("PT_TLS", PT_TLS)
14062ec34544SRui Ueyama                      .Case("PT_GNU_EH_FRAME", PT_GNU_EH_FRAME)
14072ec34544SRui Ueyama                      .Case("PT_GNU_STACK", PT_GNU_STACK)
14082ec34544SRui Ueyama                      .Case("PT_GNU_RELRO", PT_GNU_RELRO)
14092ec34544SRui Ueyama                      .Case("PT_OPENBSD_RANDOMIZE", PT_OPENBSD_RANDOMIZE)
14102ec34544SRui Ueyama                      .Case("PT_OPENBSD_WXNEEDED", PT_OPENBSD_WXNEEDED)
14112ec34544SRui Ueyama                      .Case("PT_OPENBSD_BOOTDATA", PT_OPENBSD_BOOTDATA)
14122ec34544SRui Ueyama                      .Default(-1);
14132ec34544SRui Ueyama 
14143837f427SRui Ueyama   if (ret == (unsigned)-1) {
14153837f427SRui Ueyama     setError("invalid program header type: " + tok);
14162ec34544SRui Ueyama     return PT_NULL;
14172ec34544SRui Ueyama   }
14183837f427SRui Ueyama   return ret;
14192ec34544SRui Ueyama }
14202ec34544SRui Ueyama 
14212ec34544SRui Ueyama // Reads an anonymous version declaration.
14222ec34544SRui Ueyama void ScriptParser::readAnonymousDeclaration() {
14233837f427SRui Ueyama   std::vector<SymbolVersion> locals;
14243837f427SRui Ueyama   std::vector<SymbolVersion> globals;
14253837f427SRui Ueyama   std::tie(locals, globals) = readSymbols();
1426e28a70daSFangrui Song   for (const SymbolVersion &pat : locals)
1427e28a70daSFangrui Song     config->versionDefinitions[VER_NDX_LOCAL].patterns.push_back(pat);
1428e28a70daSFangrui Song   for (const SymbolVersion &pat : globals)
1429e28a70daSFangrui Song     config->versionDefinitions[VER_NDX_GLOBAL].patterns.push_back(pat);
14302ec34544SRui Ueyama 
14312ec34544SRui Ueyama   expect(";");
14322ec34544SRui Ueyama }
14332ec34544SRui Ueyama 
14342ec34544SRui Ueyama // Reads a non-anonymous version definition,
14352ec34544SRui Ueyama // e.g. "VerStr { global: foo; bar; local: *; };".
14363837f427SRui Ueyama void ScriptParser::readVersionDeclaration(StringRef verStr) {
14372ec34544SRui Ueyama   // Read a symbol list.
14383837f427SRui Ueyama   std::vector<SymbolVersion> locals;
14393837f427SRui Ueyama   std::vector<SymbolVersion> globals;
14403837f427SRui Ueyama   std::tie(locals, globals) = readSymbols();
1441e28a70daSFangrui Song   for (const SymbolVersion &pat : locals)
1442e28a70daSFangrui Song     config->versionDefinitions[VER_NDX_LOCAL].patterns.push_back(pat);
14432ec34544SRui Ueyama 
14442ec34544SRui Ueyama   // Create a new version definition and add that to the global symbols.
14453837f427SRui Ueyama   VersionDefinition ver;
14463837f427SRui Ueyama   ver.name = verStr;
1447e28a70daSFangrui Song   ver.patterns = globals;
1448e28a70daSFangrui Song   ver.id = config->versionDefinitions.size();
14493837f427SRui Ueyama   config->versionDefinitions.push_back(ver);
14502ec34544SRui Ueyama 
14512ec34544SRui Ueyama   // Each version may have a parent version. For example, "Ver2"
14522ec34544SRui Ueyama   // defined as "Ver2 { global: foo; local: *; } Ver1;" has "Ver1"
14532ec34544SRui Ueyama   // as a parent. This version hierarchy is, probably against your
14542ec34544SRui Ueyama   // instinct, purely for hint; the runtime doesn't care about it
14552ec34544SRui Ueyama   // at all. In LLD, we simply ignore it.
14565f380403SFangrui Song   if (next() != ";")
14572ec34544SRui Ueyama     expect(";");
14582ec34544SRui Ueyama }
14592ec34544SRui Ueyama 
14603837f427SRui Ueyama static bool hasWildcard(StringRef s) {
14613837f427SRui Ueyama   return s.find_first_of("?*[") != StringRef::npos;
14621e77ad14SRui Ueyama }
14631e77ad14SRui Ueyama 
14642ec34544SRui Ueyama // Reads a list of symbols, e.g. "{ global: foo; bar; local: *; };".
14652ec34544SRui Ueyama std::pair<std::vector<SymbolVersion>, std::vector<SymbolVersion>>
14662ec34544SRui Ueyama ScriptParser::readSymbols() {
14673837f427SRui Ueyama   std::vector<SymbolVersion> locals;
14683837f427SRui Ueyama   std::vector<SymbolVersion> globals;
14693837f427SRui Ueyama   std::vector<SymbolVersion> *v = &globals;
14702ec34544SRui Ueyama 
1471b8a59c8aSBob Haarman   while (!errorCount()) {
14722ec34544SRui Ueyama     if (consume("}"))
14732ec34544SRui Ueyama       break;
14742ec34544SRui Ueyama     if (consumeLabel("local")) {
14753837f427SRui Ueyama       v = &locals;
14762ec34544SRui Ueyama       continue;
14772ec34544SRui Ueyama     }
14782ec34544SRui Ueyama     if (consumeLabel("global")) {
14793837f427SRui Ueyama       v = &globals;
14802ec34544SRui Ueyama       continue;
14812ec34544SRui Ueyama     }
14822ec34544SRui Ueyama 
14832ec34544SRui Ueyama     if (consume("extern")) {
14843837f427SRui Ueyama       std::vector<SymbolVersion> ext = readVersionExtern();
14853837f427SRui Ueyama       v->insert(v->end(), ext.begin(), ext.end());
14862ec34544SRui Ueyama     } else {
14873837f427SRui Ueyama       StringRef tok = next();
14883837f427SRui Ueyama       v->push_back({unquote(tok), false, hasWildcard(tok)});
14892ec34544SRui Ueyama     }
14902ec34544SRui Ueyama     expect(";");
14912ec34544SRui Ueyama   }
14923837f427SRui Ueyama   return {locals, globals};
14932ec34544SRui Ueyama }
14942ec34544SRui Ueyama 
14952ec34544SRui Ueyama // Reads an "extern C++" directive, e.g.,
14962ec34544SRui Ueyama // "extern "C++" { ns::*; "f(int, double)"; };"
149717324d8bSRui Ueyama //
149817324d8bSRui Ueyama // The last semicolon is optional. E.g. this is OK:
149917324d8bSRui Ueyama // "extern "C++" { ns::*; "f(int, double)" };"
15002ec34544SRui Ueyama std::vector<SymbolVersion> ScriptParser::readVersionExtern() {
15013837f427SRui Ueyama   StringRef tok = next();
15023837f427SRui Ueyama   bool isCXX = tok == "\"C++\"";
15033837f427SRui Ueyama   if (!isCXX && tok != "\"C\"")
15042ec34544SRui Ueyama     setError("Unknown language");
15052ec34544SRui Ueyama   expect("{");
15062ec34544SRui Ueyama 
15073837f427SRui Ueyama   std::vector<SymbolVersion> ret;
1508b8a59c8aSBob Haarman   while (!errorCount() && peek() != "}") {
15093837f427SRui Ueyama     StringRef tok = next();
15103837f427SRui Ueyama     ret.push_back(
15113837f427SRui Ueyama         {unquote(tok), isCXX, !tok.startswith("\"") && hasWildcard(tok)});
151217324d8bSRui Ueyama     if (consume("}"))
15133837f427SRui Ueyama       return ret;
15142ec34544SRui Ueyama     expect(";");
15152ec34544SRui Ueyama   }
15162ec34544SRui Ueyama 
15172ec34544SRui Ueyama   expect("}");
15183837f427SRui Ueyama   return ret;
15192ec34544SRui Ueyama }
15202ec34544SRui Ueyama 
152192b5b980SFangrui Song Expr ScriptParser::readMemoryAssignment(StringRef s1, StringRef s2,
15223837f427SRui Ueyama                                         StringRef s3) {
15233837f427SRui Ueyama   if (!consume(s1) && !consume(s2) && !consume(s3)) {
15243837f427SRui Ueyama     setError("expected one of: " + s1 + ", " + s2 + ", or " + s3);
152592b5b980SFangrui Song     return [] { return 0; };
15262ec34544SRui Ueyama   }
15272ec34544SRui Ueyama   expect("=");
152892b5b980SFangrui Song   return readExpr();
15292ec34544SRui Ueyama }
15302ec34544SRui Ueyama 
15312ec34544SRui Ueyama // Parse the MEMORY command as specified in:
15322ec34544SRui Ueyama // https://sourceware.org/binutils/docs/ld/MEMORY.html
15332ec34544SRui Ueyama //
15342ec34544SRui Ueyama // MEMORY { name [(attr)] : ORIGIN = origin, LENGTH = len ... }
15352ec34544SRui Ueyama void ScriptParser::readMemory() {
15362ec34544SRui Ueyama   expect("{");
1537b8a59c8aSBob Haarman   while (!errorCount() && !consume("}")) {
15383837f427SRui Ueyama     StringRef tok = next();
15393837f427SRui Ueyama     if (tok == "INCLUDE") {
15402e9d40d5SRui Ueyama       readInclude();
15412e9d40d5SRui Ueyama       continue;
15422e9d40d5SRui Ueyama     }
15432ec34544SRui Ueyama 
15443837f427SRui Ueyama     uint32_t flags = 0;
15453837f427SRui Ueyama     uint32_t negFlags = 0;
15462ec34544SRui Ueyama     if (consume("(")) {
15473837f427SRui Ueyama       std::tie(flags, negFlags) = readMemoryAttributes();
15482ec34544SRui Ueyama       expect(")");
15492ec34544SRui Ueyama     }
15502ec34544SRui Ueyama     expect(":");
15512ec34544SRui Ueyama 
155292b5b980SFangrui Song     Expr origin = readMemoryAssignment("ORIGIN", "org", "o");
15532ec34544SRui Ueyama     expect(",");
155492b5b980SFangrui Song     Expr length = readMemoryAssignment("LENGTH", "len", "l");
15552ec34544SRui Ueyama 
15565f37541cSGeorge Rimar     // Add the memory region to the region map.
15573837f427SRui Ueyama     MemoryRegion *mr = make<MemoryRegion>(tok, origin, length, flags, negFlags);
15583837f427SRui Ueyama     if (!script->memoryRegions.insert({tok, mr}).second)
15593837f427SRui Ueyama       setError("region '" + tok + "' already defined");
15602ec34544SRui Ueyama   }
15612ec34544SRui Ueyama }
15622ec34544SRui Ueyama 
15632ec34544SRui Ueyama // This function parses the attributes used to match against section
15642ec34544SRui Ueyama // flags when placing output sections in a memory region. These flags
15652ec34544SRui Ueyama // are only used when an explicit memory region name is not used.
15662ec34544SRui Ueyama std::pair<uint32_t, uint32_t> ScriptParser::readMemoryAttributes() {
15673837f427SRui Ueyama   uint32_t flags = 0;
15683837f427SRui Ueyama   uint32_t negFlags = 0;
15693837f427SRui Ueyama   bool invert = false;
15702ec34544SRui Ueyama 
15713837f427SRui Ueyama   for (char c : next().lower()) {
15723837f427SRui Ueyama     uint32_t flag = 0;
15733837f427SRui Ueyama     if (c == '!')
15743837f427SRui Ueyama       invert = !invert;
15753837f427SRui Ueyama     else if (c == 'w')
15763837f427SRui Ueyama       flag = SHF_WRITE;
15773837f427SRui Ueyama     else if (c == 'x')
15783837f427SRui Ueyama       flag = SHF_EXECINSTR;
15793837f427SRui Ueyama     else if (c == 'a')
15803837f427SRui Ueyama       flag = SHF_ALLOC;
15813837f427SRui Ueyama     else if (c != 'r')
15822ec34544SRui Ueyama       setError("invalid memory region attribute");
15832ec34544SRui Ueyama 
15843837f427SRui Ueyama     if (invert)
15853837f427SRui Ueyama       negFlags |= flag;
15862ec34544SRui Ueyama     else
15873837f427SRui Ueyama       flags |= flag;
15882ec34544SRui Ueyama   }
15893837f427SRui Ueyama   return {flags, negFlags};
15902ec34544SRui Ueyama }
15912ec34544SRui Ueyama 
1592bd8cfe65SFangrui Song void readLinkerScript(MemoryBufferRef mb) {
15933837f427SRui Ueyama   ScriptParser(mb).readLinkerScript();
15942ec34544SRui Ueyama }
15952ec34544SRui Ueyama 
1596bd8cfe65SFangrui Song void readVersionScript(MemoryBufferRef mb) {
15973837f427SRui Ueyama   ScriptParser(mb).readVersionScript();
15982ec34544SRui Ueyama }
15992ec34544SRui Ueyama 
1600bd8cfe65SFangrui Song void readDynamicList(MemoryBufferRef mb) { ScriptParser(mb).readDynamicList(); }
16018c7e8cceSPetr Hosek 
1602bd8cfe65SFangrui Song void readDefsym(StringRef name, MemoryBufferRef mb) {
16033837f427SRui Ueyama   ScriptParser(mb).readDefsym(name);
16048c7e8cceSPetr Hosek }
1605bd8cfe65SFangrui Song 
1606bd8cfe65SFangrui Song } // namespace elf
1607bd8cfe65SFangrui Song } // namespace lld
1608