xref: /llvm-project/lld/ELF/ScriptParser.cpp (revision 8c2030b7d4c4f21270577187d6413c6b05784861)
1 //===- ScriptParser.cpp ---------------------------------------------------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 // This file contains a recursive-descendent parser for linker scripts.
10 // Parsed results are stored to Config and Script global objects.
11 //
12 //===----------------------------------------------------------------------===//
13 
14 #include "ScriptParser.h"
15 #include "Config.h"
16 #include "Driver.h"
17 #include "InputFiles.h"
18 #include "LinkerScript.h"
19 #include "OutputSections.h"
20 #include "ScriptLexer.h"
21 #include "SymbolTable.h"
22 #include "Symbols.h"
23 #include "Target.h"
24 #include "lld/Common/CommonLinkerContext.h"
25 #include "llvm/ADT/SmallString.h"
26 #include "llvm/ADT/StringRef.h"
27 #include "llvm/ADT/StringSwitch.h"
28 #include "llvm/BinaryFormat/ELF.h"
29 #include "llvm/Support/Casting.h"
30 #include "llvm/Support/ErrorHandling.h"
31 #include "llvm/Support/FileSystem.h"
32 #include "llvm/Support/MathExtras.h"
33 #include "llvm/Support/Path.h"
34 #include "llvm/Support/SaveAndRestore.h"
35 #include "llvm/Support/TimeProfiler.h"
36 #include <cassert>
37 #include <limits>
38 #include <optional>
39 #include <vector>
40 
41 using namespace llvm;
42 using namespace llvm::ELF;
43 using namespace llvm::support::endian;
44 using namespace lld;
45 using namespace lld::elf;
46 
47 namespace {
48 class ScriptParser final : ScriptLexer {
49 public:
50   ScriptParser(Ctx &ctx, MemoryBufferRef mb) : ScriptLexer(ctx, mb), ctx(ctx) {}
51 
52   void readLinkerScript();
53   void readVersionScript();
54   void readDynamicList();
55   void readDefsym();
56 
57 private:
58   void addFile(StringRef path);
59 
60   void readAsNeeded();
61   void readEntry();
62   void readExtern();
63   void readGroup();
64   void readInclude();
65   void readInput();
66   void readMemory();
67   void readOutput();
68   void readOutputArch();
69   void readOutputFormat();
70   void readOverwriteSections();
71   void readPhdrs();
72   void readRegionAlias();
73   void readSearchDir();
74   void readSections();
75   void readTarget();
76   void readVersion();
77   void readVersionScriptCommand();
78   void readNoCrossRefs(bool to);
79 
80   StringRef readName();
81   SymbolAssignment *readSymbolAssignment(StringRef name);
82   ByteCommand *readByteCommand(StringRef tok);
83   std::array<uint8_t, 4> readFill();
84   bool readSectionDirective(OutputSection *cmd, StringRef tok);
85   void readSectionAddressType(OutputSection *cmd);
86   OutputDesc *readOverlaySectionDescription();
87   OutputDesc *readOutputSectionDescription(StringRef outSec);
88   SmallVector<SectionCommand *, 0> readOverlay();
89   SectionClassDesc *readSectionClassDescription();
90   StringRef readSectionClassName();
91   SmallVector<StringRef, 0> readOutputSectionPhdrs();
92   std::pair<uint64_t, uint64_t> readInputSectionFlags();
93   InputSectionDescription *readInputSectionDescription(StringRef tok);
94   StringMatcher readFilePatterns();
95   SmallVector<SectionPattern, 0> readInputSectionsList();
96   InputSectionDescription *readInputSectionRules(StringRef filePattern,
97                                                  uint64_t withFlags,
98                                                  uint64_t withoutFlags);
99   unsigned readPhdrType();
100   SortSectionPolicy peekSortKind();
101   SortSectionPolicy readSortKind();
102   SymbolAssignment *readProvideHidden(bool provide, bool hidden);
103   SymbolAssignment *readAssignment(StringRef tok);
104   void readSort();
105   Expr readAssert();
106   Expr readConstant();
107   Expr getPageSize();
108 
109   Expr readMemoryAssignment(StringRef, StringRef, StringRef);
110   void readMemoryAttributes(uint32_t &flags, uint32_t &invFlags,
111                             uint32_t &negFlags, uint32_t &negInvFlags);
112 
113   Expr combine(StringRef op, Expr l, Expr r);
114   Expr readExpr();
115   Expr readExpr1(Expr lhs, int minPrec);
116   StringRef readParenName();
117   Expr readPrimary();
118   Expr readTernary(Expr cond);
119   Expr readParenExpr();
120 
121   // For parsing version script.
122   SmallVector<SymbolVersion, 0> readVersionExtern();
123   void readAnonymousDeclaration();
124   void readVersionDeclaration(StringRef verStr);
125 
126   std::pair<SmallVector<SymbolVersion, 0>, SmallVector<SymbolVersion, 0>>
127   readSymbols();
128 
129   Ctx &ctx;
130 
131   // If we are currently parsing a PROVIDE|PROVIDE_HIDDEN command,
132   // then this member is set to the PROVIDE symbol name.
133   std::optional<llvm::StringRef> activeProvideSym;
134 };
135 } // namespace
136 
137 static StringRef unquote(StringRef s) {
138   if (s.starts_with("\""))
139     return s.substr(1, s.size() - 2);
140   return s;
141 }
142 
143 // Some operations only support one non absolute value. Move the
144 // absolute one to the right hand side for convenience.
145 static void moveAbsRight(LinkerScript &s, ExprValue &a, ExprValue &b) {
146   if (a.sec == nullptr || (a.forceAbsolute && !b.isAbsolute()))
147     std::swap(a, b);
148   if (!b.isAbsolute())
149     s.recordError(a.loc +
150                   ": at least one side of the expression must be absolute");
151 }
152 
153 static ExprValue add(LinkerScript &s, ExprValue a, ExprValue b) {
154   moveAbsRight(s, a, b);
155   return {a.sec, a.forceAbsolute, a.getSectionOffset() + b.getValue(), a.loc};
156 }
157 
158 static ExprValue sub(ExprValue a, ExprValue b) {
159   // The distance between two symbols in sections is absolute.
160   if (!a.isAbsolute() && !b.isAbsolute())
161     return a.getValue() - b.getValue();
162   return {a.sec, false, a.getSectionOffset() - b.getValue(), a.loc};
163 }
164 
165 static ExprValue bitAnd(LinkerScript &s, ExprValue a, ExprValue b) {
166   moveAbsRight(s, a, b);
167   return {a.sec, a.forceAbsolute,
168           (a.getValue() & b.getValue()) - a.getSecAddr(), a.loc};
169 }
170 
171 static ExprValue bitXor(LinkerScript &s, ExprValue a, ExprValue b) {
172   moveAbsRight(s, a, b);
173   return {a.sec, a.forceAbsolute,
174           (a.getValue() ^ b.getValue()) - a.getSecAddr(), a.loc};
175 }
176 
177 static ExprValue bitOr(LinkerScript &s, ExprValue a, ExprValue b) {
178   moveAbsRight(s, a, b);
179   return {a.sec, a.forceAbsolute,
180           (a.getValue() | b.getValue()) - a.getSecAddr(), a.loc};
181 }
182 
183 void ScriptParser::readDynamicList() {
184   expect("{");
185   SmallVector<SymbolVersion, 0> locals;
186   SmallVector<SymbolVersion, 0> globals;
187   std::tie(locals, globals) = readSymbols();
188   expect(";");
189 
190   StringRef tok = peek();
191   if (tok.size()) {
192     setError("EOF expected, but got " + tok);
193     return;
194   }
195   if (!locals.empty()) {
196     setError("\"local:\" scope not supported in --dynamic-list");
197     return;
198   }
199 
200   for (SymbolVersion v : globals)
201     ctx.arg.dynamicList.push_back(v);
202 }
203 
204 void ScriptParser::readVersionScript() {
205   readVersionScriptCommand();
206   StringRef tok = peek();
207   if (tok.size())
208     setError("EOF expected, but got " + tok);
209 }
210 
211 void ScriptParser::readVersionScriptCommand() {
212   if (consume("{")) {
213     readAnonymousDeclaration();
214     return;
215   }
216 
217   if (atEOF())
218     setError("unexpected EOF");
219   while (peek() != "}" && !atEOF()) {
220     StringRef verStr = next();
221     if (verStr == "{") {
222       setError("anonymous version definition is used in "
223                "combination with other version definitions");
224       return;
225     }
226     expect("{");
227     readVersionDeclaration(verStr);
228   }
229 }
230 
231 void ScriptParser::readVersion() {
232   expect("{");
233   readVersionScriptCommand();
234   expect("}");
235 }
236 
237 void ScriptParser::readLinkerScript() {
238   while (!atEOF()) {
239     StringRef tok = next();
240     if (atEOF())
241       break;
242     if (tok == ";")
243       continue;
244 
245     if (tok == "ENTRY") {
246       readEntry();
247     } else if (tok == "EXTERN") {
248       readExtern();
249     } else if (tok == "GROUP") {
250       readGroup();
251     } else if (tok == "INCLUDE") {
252       readInclude();
253     } else if (tok == "INPUT") {
254       readInput();
255     } else if (tok == "MEMORY") {
256       readMemory();
257     } else if (tok == "OUTPUT") {
258       readOutput();
259     } else if (tok == "OUTPUT_ARCH") {
260       readOutputArch();
261     } else if (tok == "OUTPUT_FORMAT") {
262       readOutputFormat();
263     } else if (tok == "OVERWRITE_SECTIONS") {
264       readOverwriteSections();
265     } else if (tok == "PHDRS") {
266       readPhdrs();
267     } else if (tok == "REGION_ALIAS") {
268       readRegionAlias();
269     } else if (tok == "SEARCH_DIR") {
270       readSearchDir();
271     } else if (tok == "SECTIONS") {
272       readSections();
273     } else if (tok == "TARGET") {
274       readTarget();
275     } else if (tok == "VERSION") {
276       readVersion();
277     } else if (tok == "NOCROSSREFS") {
278       readNoCrossRefs(/*to=*/false);
279     } else if (tok == "NOCROSSREFS_TO") {
280       readNoCrossRefs(/*to=*/true);
281     } else if (SymbolAssignment *cmd = readAssignment(tok)) {
282       ctx.script->sectionCommands.push_back(cmd);
283     } else {
284       setError("unknown directive: " + tok);
285     }
286   }
287 }
288 
289 void ScriptParser::readDefsym() {
290   if (errCount(ctx))
291     return;
292   inExpr = true;
293   StringRef name = readName();
294   expect("=");
295   Expr e = readExpr();
296   if (!atEOF())
297     setError("EOF expected, but got " + next());
298   auto *cmd = make<SymbolAssignment>(
299       name, e, 0, getCurrentMB().getBufferIdentifier().str());
300   ctx.script->sectionCommands.push_back(cmd);
301 }
302 
303 void ScriptParser::readNoCrossRefs(bool to) {
304   expect("(");
305   NoCrossRefCommand cmd{{}, to};
306   while (auto tok = till(")"))
307     cmd.outputSections.push_back(unquote(tok));
308   if (cmd.outputSections.size() < 2)
309     Warn(ctx) << getCurrentLocation()
310               << ": ignored with fewer than 2 output sections";
311   else
312     ctx.script->noCrossRefs.push_back(std::move(cmd));
313 }
314 
315 void ScriptParser::addFile(StringRef s) {
316   if (curBuf.isUnderSysroot && s.starts_with("/")) {
317     SmallString<128> pathData;
318     StringRef path = (ctx.arg.sysroot + s).toStringRef(pathData);
319     if (sys::fs::exists(path))
320       ctx.driver.addFile(ctx.saver.save(path), /*withLOption=*/false);
321     else
322       setError("cannot find " + s + " inside " + ctx.arg.sysroot);
323     return;
324   }
325 
326   if (s.starts_with("/")) {
327     // Case 1: s is an absolute path. Just open it.
328     ctx.driver.addFile(s, /*withLOption=*/false);
329   } else if (s.starts_with("=")) {
330     // Case 2: relative to the sysroot.
331     if (ctx.arg.sysroot.empty())
332       ctx.driver.addFile(s.substr(1), /*withLOption=*/false);
333     else
334       ctx.driver.addFile(ctx.saver.save(ctx.arg.sysroot + "/" + s.substr(1)),
335                          /*withLOption=*/false);
336   } else if (s.starts_with("-l")) {
337     // Case 3: search in the list of library paths.
338     ctx.driver.addLibrary(s.substr(2));
339   } else {
340     // Case 4: s is a relative path. Search in the directory of the script file.
341     std::string filename = std::string(getCurrentMB().getBufferIdentifier());
342     StringRef directory = sys::path::parent_path(filename);
343     if (!directory.empty()) {
344       SmallString<0> path(directory);
345       sys::path::append(path, s);
346       if (sys::fs::exists(path)) {
347         ctx.driver.addFile(path, /*withLOption=*/false);
348         return;
349       }
350     }
351     // Then search in the current working directory.
352     if (sys::fs::exists(s)) {
353       ctx.driver.addFile(s, /*withLOption=*/false);
354     } else {
355       // Finally, search in the list of library paths.
356       if (std::optional<std::string> path = findFromSearchPaths(ctx, s))
357         ctx.driver.addFile(ctx.saver.save(*path), /*withLOption=*/true);
358       else
359         setError("unable to find " + s);
360     }
361   }
362 }
363 
364 void ScriptParser::readAsNeeded() {
365   expect("(");
366   bool orig = ctx.arg.asNeeded;
367   ctx.arg.asNeeded = true;
368   while (auto tok = till(")"))
369     addFile(unquote(tok));
370   ctx.arg.asNeeded = orig;
371 }
372 
373 void ScriptParser::readEntry() {
374   // -e <symbol> takes predecence over ENTRY(<symbol>).
375   expect("(");
376   StringRef name = readName();
377   if (ctx.arg.entry.empty())
378     ctx.arg.entry = name;
379   expect(")");
380 }
381 
382 void ScriptParser::readExtern() {
383   expect("(");
384   while (auto tok = till(")"))
385     ctx.arg.undefined.push_back(unquote(tok));
386 }
387 
388 void ScriptParser::readGroup() {
389   SaveAndRestore saved(ctx.driver.isInGroup, true);
390   readInput();
391   if (!saved.get())
392     ++ctx.driver.nextGroupId;
393 }
394 
395 void ScriptParser::readInclude() {
396   StringRef name = readName();
397   if (!activeFilenames.insert(name).second) {
398     setError("there is a cycle in linker script INCLUDEs");
399     return;
400   }
401 
402   if (std::optional<std::string> path = searchScript(ctx, name)) {
403     if (std::optional<MemoryBufferRef> mb = readFile(ctx, *path)) {
404       buffers.push_back(curBuf);
405       curBuf = Buffer(ctx, *mb);
406       mbs.push_back(*mb);
407     }
408     return;
409   }
410   setError("cannot find linker script " + name);
411 }
412 
413 void ScriptParser::readInput() {
414   expect("(");
415   while (auto tok = till(")")) {
416     if (tok == "AS_NEEDED")
417       readAsNeeded();
418     else
419       addFile(unquote(tok));
420   }
421 }
422 
423 void ScriptParser::readOutput() {
424   // -o <file> takes predecence over OUTPUT(<file>).
425   expect("(");
426   StringRef name = readName();
427   if (ctx.arg.outputFile.empty())
428     ctx.arg.outputFile = name;
429   expect(")");
430 }
431 
432 void ScriptParser::readOutputArch() {
433   // OUTPUT_ARCH is ignored for now.
434   expect("(");
435   while (till(")"))
436     ;
437 }
438 
439 static std::pair<ELFKind, uint16_t> parseBfdName(StringRef s) {
440   return StringSwitch<std::pair<ELFKind, uint16_t>>(s)
441       .Case("elf32-i386", {ELF32LEKind, EM_386})
442       .Case("elf32-avr", {ELF32LEKind, EM_AVR})
443       .Case("elf32-iamcu", {ELF32LEKind, EM_IAMCU})
444       .Case("elf32-littlearm", {ELF32LEKind, EM_ARM})
445       .Case("elf32-bigarm", {ELF32BEKind, EM_ARM})
446       .Case("elf32-x86-64", {ELF32LEKind, EM_X86_64})
447       .Case("elf64-aarch64", {ELF64LEKind, EM_AARCH64})
448       .Case("elf64-littleaarch64", {ELF64LEKind, EM_AARCH64})
449       .Case("elf64-bigaarch64", {ELF64BEKind, EM_AARCH64})
450       .Case("elf32-powerpc", {ELF32BEKind, EM_PPC})
451       .Case("elf32-powerpcle", {ELF32LEKind, EM_PPC})
452       .Case("elf64-powerpc", {ELF64BEKind, EM_PPC64})
453       .Case("elf64-powerpcle", {ELF64LEKind, EM_PPC64})
454       .Case("elf64-x86-64", {ELF64LEKind, EM_X86_64})
455       .Cases("elf32-tradbigmips", "elf32-bigmips", {ELF32BEKind, EM_MIPS})
456       .Case("elf32-ntradbigmips", {ELF32BEKind, EM_MIPS})
457       .Case("elf32-tradlittlemips", {ELF32LEKind, EM_MIPS})
458       .Case("elf32-ntradlittlemips", {ELF32LEKind, EM_MIPS})
459       .Case("elf64-tradbigmips", {ELF64BEKind, EM_MIPS})
460       .Case("elf64-tradlittlemips", {ELF64LEKind, EM_MIPS})
461       .Case("elf32-littleriscv", {ELF32LEKind, EM_RISCV})
462       .Case("elf64-littleriscv", {ELF64LEKind, EM_RISCV})
463       .Case("elf64-sparc", {ELF64BEKind, EM_SPARCV9})
464       .Case("elf32-msp430", {ELF32LEKind, EM_MSP430})
465       .Case("elf32-loongarch", {ELF32LEKind, EM_LOONGARCH})
466       .Case("elf64-loongarch", {ELF64LEKind, EM_LOONGARCH})
467       .Case("elf64-s390", {ELF64BEKind, EM_S390})
468       .Cases("elf32-hexagon", "elf32-littlehexagon", {ELF32LEKind, EM_HEXAGON})
469       .Default({ELFNoneKind, EM_NONE});
470 }
471 
472 // Parse OUTPUT_FORMAT(bfdname) or OUTPUT_FORMAT(default, big, little). Choose
473 // big if -EB is specified, little if -EL is specified, or default if neither is
474 // specified.
475 void ScriptParser::readOutputFormat() {
476   expect("(");
477 
478   StringRef s = readName();
479   if (!consume(")")) {
480     expect(",");
481     StringRef tmp = readName();
482     if (ctx.arg.optEB)
483       s = tmp;
484     expect(",");
485     tmp = readName();
486     if (ctx.arg.optEL)
487       s = tmp;
488     consume(")");
489   }
490   // If more than one OUTPUT_FORMAT is specified, only the first is checked.
491   if (!ctx.arg.bfdname.empty())
492     return;
493   ctx.arg.bfdname = s;
494 
495   if (s == "binary") {
496     ctx.arg.oFormatBinary = true;
497     return;
498   }
499 
500   if (s.consume_back("-freebsd"))
501     ctx.arg.osabi = ELFOSABI_FREEBSD;
502 
503   std::tie(ctx.arg.ekind, ctx.arg.emachine) = parseBfdName(s);
504   if (ctx.arg.emachine == EM_NONE)
505     setError("unknown output format name: " + ctx.arg.bfdname);
506   if (s == "elf32-ntradlittlemips" || s == "elf32-ntradbigmips")
507     ctx.arg.mipsN32Abi = true;
508   if (ctx.arg.emachine == EM_MSP430)
509     ctx.arg.osabi = ELFOSABI_STANDALONE;
510 }
511 
512 void ScriptParser::readPhdrs() {
513   expect("{");
514   while (auto tok = till("}")) {
515     PhdrsCommand cmd;
516     cmd.name = tok;
517     cmd.type = readPhdrType();
518 
519     while (!errCount(ctx) && !consume(";")) {
520       if (consume("FILEHDR"))
521         cmd.hasFilehdr = true;
522       else if (consume("PHDRS"))
523         cmd.hasPhdrs = true;
524       else if (consume("AT"))
525         cmd.lmaExpr = readParenExpr();
526       else if (consume("FLAGS"))
527         cmd.flags = readParenExpr()().getValue();
528       else
529         setError("unexpected header attribute: " + next());
530     }
531 
532     ctx.script->phdrsCommands.push_back(cmd);
533   }
534 }
535 
536 void ScriptParser::readRegionAlias() {
537   expect("(");
538   StringRef alias = readName();
539   expect(",");
540   StringRef name = readName();
541   expect(")");
542 
543   if (ctx.script->memoryRegions.count(alias))
544     setError("redefinition of memory region '" + alias + "'");
545   if (!ctx.script->memoryRegions.count(name))
546     setError("memory region '" + name + "' is not defined");
547   ctx.script->memoryRegions.insert({alias, ctx.script->memoryRegions[name]});
548 }
549 
550 void ScriptParser::readSearchDir() {
551   expect("(");
552   StringRef name = readName();
553   if (!ctx.arg.nostdlib)
554     ctx.arg.searchPaths.push_back(name);
555   expect(")");
556 }
557 
558 // This reads an overlay description. Overlays are used to describe output
559 // sections that use the same virtual memory range and normally would trigger
560 // linker's sections sanity check failures.
561 // https://sourceware.org/binutils/docs/ld/Overlay-Description.html#Overlay-Description
562 SmallVector<SectionCommand *, 0> ScriptParser::readOverlay() {
563   Expr addrExpr;
564   if (consume(":")) {
565     addrExpr = [s = ctx.script] { return s->getDot(); };
566   } else {
567     addrExpr = readExpr();
568     expect(":");
569   }
570   // When AT is omitted, LMA should equal VMA. script->getDot() when evaluating
571   // lmaExpr will ensure this, even if the start address is specified.
572   Expr lmaExpr = consume("AT") ? readParenExpr()
573                                : [s = ctx.script] { return s->getDot(); };
574   expect("{");
575 
576   SmallVector<SectionCommand *, 0> v;
577   OutputSection *prev = nullptr;
578   while (!errCount(ctx) && !consume("}")) {
579     // VA is the same for all sections. The LMAs are consecutive in memory
580     // starting from the base load address specified.
581     OutputDesc *osd = readOverlaySectionDescription();
582     osd->osec.addrExpr = addrExpr;
583     if (prev) {
584       osd->osec.lmaExpr = [=] { return prev->getLMA() + prev->size; };
585     } else {
586       osd->osec.lmaExpr = lmaExpr;
587       // Use first section address for subsequent sections as initial addrExpr
588       // can be DOT. Ensure the first section, even if empty, is not discarded.
589       osd->osec.usedInExpression = true;
590       addrExpr = [=]() -> ExprValue { return {&osd->osec, false, 0, ""}; };
591     }
592     v.push_back(osd);
593     prev = &osd->osec;
594   }
595 
596   // According to the specification, at the end of the overlay, the location
597   // counter should be equal to the overlay base address plus size of the
598   // largest section seen in the overlay.
599   // Here we want to create the Dot assignment command to achieve that.
600   Expr moveDot = [=] {
601     uint64_t max = 0;
602     for (SectionCommand *cmd : v)
603       max = std::max(max, cast<OutputDesc>(cmd)->osec.size);
604     return addrExpr().getValue() + max;
605   };
606   v.push_back(make<SymbolAssignment>(".", moveDot, 0, getCurrentLocation()));
607   return v;
608 }
609 
610 SectionClassDesc *ScriptParser::readSectionClassDescription() {
611   StringRef name = readSectionClassName();
612   SectionClassDesc *desc = make<SectionClassDesc>(name);
613   if (!ctx.script->sectionClasses.insert({CachedHashStringRef(name), desc})
614            .second)
615     setError("section class '" + name + "' already defined");
616   expect("{");
617   while (auto tok = till("}")) {
618     if (tok == "(" || tok == ")") {
619       setError("expected filename pattern");
620     } else if (peek() == "(") {
621       InputSectionDescription *isd = readInputSectionDescription(tok);
622       if (!isd->classRef.empty())
623         setError("section class '" + name + "' references class '" +
624                  isd->classRef + "'");
625       desc->sc.commands.push_back(isd);
626     }
627   }
628   return desc;
629 }
630 
631 StringRef ScriptParser::readSectionClassName() {
632   expect("(");
633   StringRef name = unquote(next());
634   expect(")");
635   return name;
636 }
637 
638 void ScriptParser::readOverwriteSections() {
639   expect("{");
640   while (auto tok = till("}"))
641     ctx.script->overwriteSections.push_back(readOutputSectionDescription(tok));
642 }
643 
644 void ScriptParser::readSections() {
645   expect("{");
646   SmallVector<SectionCommand *, 0> v;
647   while (auto tok = till("}")) {
648     if (tok == "OVERLAY") {
649       for (SectionCommand *cmd : readOverlay())
650         v.push_back(cmd);
651       continue;
652     }
653     if (tok == "CLASS") {
654       v.push_back(readSectionClassDescription());
655       continue;
656     }
657     if (tok == "INCLUDE") {
658       readInclude();
659       continue;
660     }
661 
662     if (SectionCommand *cmd = readAssignment(tok))
663       v.push_back(cmd);
664     else
665       v.push_back(readOutputSectionDescription(tok));
666   }
667 
668   // If DATA_SEGMENT_RELRO_END is absent, for sections after DATA_SEGMENT_ALIGN,
669   // the relro fields should be cleared.
670   if (!ctx.script->seenRelroEnd)
671     for (SectionCommand *cmd : v)
672       if (auto *osd = dyn_cast<OutputDesc>(cmd))
673         osd->osec.relro = false;
674 
675   ctx.script->sectionCommands.insert(ctx.script->sectionCommands.end(),
676                                      v.begin(), v.end());
677 
678   if (atEOF() || !consume("INSERT")) {
679     ctx.script->hasSectionsCommand = true;
680     return;
681   }
682 
683   bool isAfter = false;
684   if (consume("AFTER"))
685     isAfter = true;
686   else if (!consume("BEFORE"))
687     setError("expected AFTER/BEFORE, but got '" + next() + "'");
688   StringRef where = readName();
689   SmallVector<StringRef, 0> names;
690   for (SectionCommand *cmd : v)
691     if (auto *os = dyn_cast<OutputDesc>(cmd))
692       names.push_back(os->osec.name);
693   if (!names.empty())
694     ctx.script->insertCommands.push_back({std::move(names), isAfter, where});
695 }
696 
697 void ScriptParser::readTarget() {
698   // TARGET(foo) is an alias for "--format foo". Unlike GNU linkers,
699   // we accept only a limited set of BFD names (i.e. "elf" or "binary")
700   // for --format. We recognize only /^elf/ and "binary" in the linker
701   // script as well.
702   expect("(");
703   StringRef tok = readName();
704   expect(")");
705 
706   if (tok.starts_with("elf"))
707     ctx.arg.formatBinary = false;
708   else if (tok == "binary")
709     ctx.arg.formatBinary = true;
710   else
711     setError("unknown target: " + tok);
712 }
713 
714 static int precedence(StringRef op) {
715   return StringSwitch<int>(op)
716       .Cases("*", "/", "%", 11)
717       .Cases("+", "-", 10)
718       .Cases("<<", ">>", 9)
719       .Cases("<", "<=", ">", ">=", 8)
720       .Cases("==", "!=", 7)
721       .Case("&", 6)
722       .Case("^", 5)
723       .Case("|", 4)
724       .Case("&&", 3)
725       .Case("||", 2)
726       .Case("?", 1)
727       .Default(-1);
728 }
729 
730 StringMatcher ScriptParser::readFilePatterns() {
731   StringMatcher Matcher;
732   while (auto tok = till(")"))
733     Matcher.addPattern(SingleStringMatcher(tok));
734   return Matcher;
735 }
736 
737 SortSectionPolicy ScriptParser::peekSortKind() {
738   return StringSwitch<SortSectionPolicy>(peek())
739       .Case("REVERSE", SortSectionPolicy::Reverse)
740       .Cases("SORT", "SORT_BY_NAME", SortSectionPolicy::Name)
741       .Case("SORT_BY_ALIGNMENT", SortSectionPolicy::Alignment)
742       .Case("SORT_BY_INIT_PRIORITY", SortSectionPolicy::Priority)
743       .Case("SORT_NONE", SortSectionPolicy::None)
744       .Default(SortSectionPolicy::Default);
745 }
746 
747 SortSectionPolicy ScriptParser::readSortKind() {
748   SortSectionPolicy ret = peekSortKind();
749   if (ret != SortSectionPolicy::Default)
750     skip();
751   return ret;
752 }
753 
754 // Reads SECTIONS command contents in the following form:
755 //
756 // <contents> ::= <elem>*
757 // <elem>     ::= <exclude>? <glob-pattern>
758 // <exclude>  ::= "EXCLUDE_FILE" "(" <glob-pattern>+ ")"
759 //
760 // For example,
761 //
762 // *(.foo EXCLUDE_FILE (a.o) .bar EXCLUDE_FILE (b.o) .baz)
763 //
764 // is parsed as ".foo", ".bar" with "a.o", and ".baz" with "b.o".
765 // The semantics of that is section .foo in any file, section .bar in
766 // any file but a.o, and section .baz in any file but b.o.
767 SmallVector<SectionPattern, 0> ScriptParser::readInputSectionsList() {
768   SmallVector<SectionPattern, 0> ret;
769   while (!errCount(ctx) && peek() != ")") {
770     StringMatcher excludeFilePat;
771     if (consume("EXCLUDE_FILE")) {
772       expect("(");
773       excludeFilePat = readFilePatterns();
774     }
775 
776     StringMatcher SectionMatcher;
777     // Break if the next token is ), EXCLUDE_FILE, or SORT*.
778     while (!errCount(ctx) && peekSortKind() == SortSectionPolicy::Default) {
779       StringRef s = peek();
780       if (s == ")" || s == "EXCLUDE_FILE")
781         break;
782       // Detect common mistakes when certain non-wildcard meta characters are
783       // used without a closing ')'.
784       if (!s.empty() && strchr("(){}", s[0])) {
785         skip();
786         setError("section pattern is expected");
787         break;
788       }
789       SectionMatcher.addPattern(readName());
790     }
791 
792     if (!SectionMatcher.empty())
793       ret.push_back({std::move(excludeFilePat), std::move(SectionMatcher)});
794     else if (excludeFilePat.empty())
795       break;
796     else
797       setError("section pattern is expected");
798   }
799   return ret;
800 }
801 
802 // Reads contents of "SECTIONS" directive. That directive contains a
803 // list of glob patterns for input sections. The grammar is as follows.
804 //
805 // <patterns> ::= <section-list>
806 //              | <sort> "(" <section-list> ")"
807 //              | <sort> "(" <sort> "(" <section-list> ")" ")"
808 //
809 // <sort>     ::= "SORT" | "SORT_BY_NAME" | "SORT_BY_ALIGNMENT"
810 //              | "SORT_BY_INIT_PRIORITY" | "SORT_NONE"
811 //
812 // <section-list> is parsed by readInputSectionsList().
813 InputSectionDescription *
814 ScriptParser::readInputSectionRules(StringRef filePattern, uint64_t withFlags,
815                                     uint64_t withoutFlags) {
816   auto *cmd =
817       make<InputSectionDescription>(filePattern, withFlags, withoutFlags);
818   expect("(");
819 
820   while (peek() != ")" && !atEOF()) {
821     SortSectionPolicy outer = readSortKind();
822     SortSectionPolicy inner = SortSectionPolicy::Default;
823     SmallVector<SectionPattern, 0> v;
824     if (outer != SortSectionPolicy::Default) {
825       expect("(");
826       inner = readSortKind();
827       if (inner != SortSectionPolicy::Default) {
828         expect("(");
829         v = readInputSectionsList();
830         expect(")");
831       } else {
832         v = readInputSectionsList();
833       }
834       expect(")");
835     } else {
836       v = readInputSectionsList();
837     }
838 
839     for (SectionPattern &pat : v) {
840       pat.sortInner = inner;
841       pat.sortOuter = outer;
842     }
843 
844     std::move(v.begin(), v.end(), std::back_inserter(cmd->sectionPatterns));
845   }
846   expect(")");
847   return cmd;
848 }
849 
850 InputSectionDescription *
851 ScriptParser::readInputSectionDescription(StringRef tok) {
852   // Input section wildcard can be surrounded by KEEP.
853   // https://sourceware.org/binutils/docs/ld/Input-Section-Keep.html#Input-Section-Keep
854   uint64_t withFlags = 0;
855   uint64_t withoutFlags = 0;
856   if (tok == "KEEP") {
857     expect("(");
858     if (consume("INPUT_SECTION_FLAGS"))
859       std::tie(withFlags, withoutFlags) = readInputSectionFlags();
860 
861     tok = next();
862     InputSectionDescription *cmd;
863     if (tok == "CLASS")
864       cmd = make<InputSectionDescription>(StringRef{}, withFlags, withoutFlags,
865                                           readSectionClassName());
866     else
867       cmd = readInputSectionRules(tok, withFlags, withoutFlags);
868     expect(")");
869     ctx.script->keptSections.push_back(cmd);
870     return cmd;
871   }
872   if (tok == "INPUT_SECTION_FLAGS") {
873     std::tie(withFlags, withoutFlags) = readInputSectionFlags();
874     tok = next();
875   }
876   if (tok == "CLASS")
877     return make<InputSectionDescription>(StringRef{}, withFlags, withoutFlags,
878                                          readSectionClassName());
879   return readInputSectionRules(tok, withFlags, withoutFlags);
880 }
881 
882 void ScriptParser::readSort() {
883   expect("(");
884   expect("CONSTRUCTORS");
885   expect(")");
886 }
887 
888 Expr ScriptParser::readAssert() {
889   expect("(");
890   Expr e = readExpr();
891   expect(",");
892   StringRef msg = readName();
893   expect(")");
894 
895   return [=, s = ctx.script, &ctx = ctx]() -> ExprValue {
896     if (!e().getValue())
897       Err(ctx) << msg;
898     return s->getDot();
899   };
900 }
901 
902 #define ECase(X)                                                               \
903   { #X, X }
904 constexpr std::pair<const char *, unsigned> typeMap[] = {
905     ECase(SHT_PROGBITS),   ECase(SHT_NOTE),       ECase(SHT_NOBITS),
906     ECase(SHT_INIT_ARRAY), ECase(SHT_FINI_ARRAY), ECase(SHT_PREINIT_ARRAY),
907 };
908 #undef ECase
909 
910 // Tries to read the special directive for an output section definition which
911 // can be one of following: "(NOLOAD)", "(COPY)", "(INFO)", "(OVERLAY)", and
912 // "(TYPE=<value>)".
913 bool ScriptParser::readSectionDirective(OutputSection *cmd, StringRef tok) {
914   if (tok != "NOLOAD" && tok != "COPY" && tok != "INFO" && tok != "OVERLAY" &&
915       tok != "TYPE")
916     return false;
917 
918   if (consume("NOLOAD")) {
919     cmd->type = SHT_NOBITS;
920     cmd->typeIsSet = true;
921   } else if (consume("TYPE")) {
922     expect("=");
923     StringRef value = peek();
924     auto it = llvm::find_if(typeMap, [=](auto e) { return e.first == value; });
925     if (it != std::end(typeMap)) {
926       // The value is a recognized literal SHT_*.
927       cmd->type = it->second;
928       skip();
929     } else if (value.starts_with("SHT_")) {
930       setError("unknown section type " + value);
931     } else {
932       // Otherwise, read an expression.
933       cmd->type = readExpr()().getValue();
934     }
935     cmd->typeIsSet = true;
936   } else {
937     skip(); // This is "COPY", "INFO" or "OVERLAY".
938     cmd->nonAlloc = true;
939   }
940   expect(")");
941   return true;
942 }
943 
944 // Reads an expression and/or the special directive for an output
945 // section definition. Directive is one of following: "(NOLOAD)",
946 // "(COPY)", "(INFO)" or "(OVERLAY)".
947 //
948 // An output section name can be followed by an address expression
949 // and/or directive. This grammar is not LL(1) because "(" can be
950 // interpreted as either the beginning of some expression or beginning
951 // of directive.
952 //
953 // https://sourceware.org/binutils/docs/ld/Output-Section-Address.html
954 // https://sourceware.org/binutils/docs/ld/Output-Section-Type.html
955 void ScriptParser::readSectionAddressType(OutputSection *cmd) {
956   if (consume("(")) {
957     // Temporarily set inExpr to support TYPE=<value> without spaces.
958     SaveAndRestore saved(inExpr, true);
959     if (readSectionDirective(cmd, peek()))
960       return;
961     cmd->addrExpr = readExpr();
962     expect(")");
963   } else {
964     cmd->addrExpr = readExpr();
965   }
966 
967   if (consume("(")) {
968     SaveAndRestore saved(inExpr, true);
969     StringRef tok = peek();
970     if (!readSectionDirective(cmd, tok))
971       setError("unknown section directive: " + tok);
972   }
973 }
974 
975 static Expr checkAlignment(Ctx &ctx, Expr e, std::string &loc) {
976   return [=, &ctx] {
977     uint64_t alignment = std::max((uint64_t)1, e().getValue());
978     if (!isPowerOf2_64(alignment)) {
979       ErrAlways(ctx) << loc << ": alignment must be power of 2";
980       return (uint64_t)1; // Return a dummy value.
981     }
982     return alignment;
983   };
984 }
985 
986 OutputDesc *ScriptParser::readOverlaySectionDescription() {
987   OutputDesc *osd =
988       ctx.script->createOutputSection(readName(), getCurrentLocation());
989   osd->osec.inOverlay = true;
990   expect("{");
991   while (auto tok = till("}")) {
992     uint64_t withFlags = 0;
993     uint64_t withoutFlags = 0;
994     if (tok == "INPUT_SECTION_FLAGS") {
995       std::tie(withFlags, withoutFlags) = readInputSectionFlags();
996       tok = till("");
997     }
998     if (tok == "CLASS")
999       osd->osec.commands.push_back(make<InputSectionDescription>(
1000           StringRef{}, withFlags, withoutFlags, readSectionClassName()));
1001     else
1002       osd->osec.commands.push_back(
1003           readInputSectionRules(tok, withFlags, withoutFlags));
1004   }
1005   osd->osec.phdrs = readOutputSectionPhdrs();
1006   return osd;
1007 }
1008 
1009 OutputDesc *ScriptParser::readOutputSectionDescription(StringRef outSec) {
1010   OutputDesc *cmd =
1011       ctx.script->createOutputSection(unquote(outSec), getCurrentLocation());
1012   OutputSection *osec = &cmd->osec;
1013   // Maybe relro. Will reset to false if DATA_SEGMENT_RELRO_END is absent.
1014   osec->relro = ctx.script->seenDataAlign && !ctx.script->seenRelroEnd;
1015 
1016   size_t symbolsReferenced = ctx.script->referencedSymbols.size();
1017 
1018   if (peek() != ":")
1019     readSectionAddressType(osec);
1020   expect(":");
1021 
1022   std::string location = getCurrentLocation();
1023   if (consume("AT"))
1024     osec->lmaExpr = readParenExpr();
1025   if (consume("ALIGN"))
1026     osec->alignExpr = checkAlignment(ctx, readParenExpr(), location);
1027   if (consume("SUBALIGN"))
1028     osec->subalignExpr = checkAlignment(ctx, readParenExpr(), location);
1029 
1030   // Parse constraints.
1031   if (consume("ONLY_IF_RO"))
1032     osec->constraint = ConstraintKind::ReadOnly;
1033   if (consume("ONLY_IF_RW"))
1034     osec->constraint = ConstraintKind::ReadWrite;
1035   expect("{");
1036 
1037   while (auto tok = till("}")) {
1038     if (tok == ";") {
1039       // Empty commands are allowed. Do nothing here.
1040     } else if (SymbolAssignment *assign = readAssignment(tok)) {
1041       osec->commands.push_back(assign);
1042     } else if (ByteCommand *data = readByteCommand(tok)) {
1043       osec->commands.push_back(data);
1044     } else if (tok == "CONSTRUCTORS") {
1045       // CONSTRUCTORS is a keyword to make the linker recognize C++ ctors/dtors
1046       // by name. This is for very old file formats such as ECOFF/XCOFF.
1047       // For ELF, we should ignore.
1048     } else if (tok == "FILL") {
1049       // We handle the FILL command as an alias for =fillexp section attribute,
1050       // which is different from what GNU linkers do.
1051       // https://sourceware.org/binutils/docs/ld/Output-Section-Data.html
1052       if (peek() != "(")
1053         setError("( expected, but got " + peek());
1054       osec->filler = readFill();
1055     } else if (tok == "SORT") {
1056       readSort();
1057     } else if (tok == "INCLUDE") {
1058       readInclude();
1059     } else if (tok == "(" || tok == ")") {
1060       setError("expected filename pattern");
1061     } else if (peek() == "(") {
1062       osec->commands.push_back(readInputSectionDescription(tok));
1063     } else {
1064       // We have a file name and no input sections description. It is not a
1065       // commonly used syntax, but still acceptable. In that case, all sections
1066       // from the file will be included.
1067       // FIXME: GNU ld permits INPUT_SECTION_FLAGS to be used here. We do not
1068       // handle this case here as it will already have been matched by the
1069       // case above.
1070       auto *isd = make<InputSectionDescription>(tok);
1071       isd->sectionPatterns.push_back({{}, StringMatcher("*")});
1072       osec->commands.push_back(isd);
1073     }
1074   }
1075 
1076   if (consume(">"))
1077     osec->memoryRegionName = std::string(readName());
1078 
1079   if (consume("AT")) {
1080     expect(">");
1081     osec->lmaRegionName = std::string(readName());
1082   }
1083 
1084   if (osec->lmaExpr && !osec->lmaRegionName.empty())
1085     ErrAlways(ctx) << "section can't have both LMA and a load region";
1086 
1087   osec->phdrs = readOutputSectionPhdrs();
1088 
1089   if (peek() == "=" || peek().starts_with("=")) {
1090     inExpr = true;
1091     consume("=");
1092     osec->filler = readFill();
1093     inExpr = false;
1094   }
1095 
1096   // Consume optional comma following output section command.
1097   consume(",");
1098 
1099   if (ctx.script->referencedSymbols.size() > symbolsReferenced)
1100     osec->expressionsUseSymbols = true;
1101   return cmd;
1102 }
1103 
1104 // Reads a `=<fillexp>` expression and returns its value as a big-endian number.
1105 // https://sourceware.org/binutils/docs/ld/Output-Section-Fill.html
1106 // We do not support using symbols in such expressions.
1107 //
1108 // When reading a hexstring, ld.bfd handles it as a blob of arbitrary
1109 // size, while ld.gold always handles it as a 32-bit big-endian number.
1110 // We are compatible with ld.gold because it's easier to implement.
1111 // Also, we require that expressions with operators must be wrapped into
1112 // round brackets. We did it to resolve the ambiguity when parsing scripts like:
1113 // SECTIONS { .foo : { ... } =120+3 /DISCARD/ : { ... } }
1114 std::array<uint8_t, 4> ScriptParser::readFill() {
1115   uint64_t value = readPrimary()().val;
1116   if (value > UINT32_MAX)
1117     setError("filler expression result does not fit 32-bit: 0x" +
1118              Twine::utohexstr(value));
1119 
1120   std::array<uint8_t, 4> buf;
1121   write32be(buf.data(), (uint32_t)value);
1122   return buf;
1123 }
1124 
1125 SymbolAssignment *ScriptParser::readProvideHidden(bool provide, bool hidden) {
1126   expect("(");
1127   StringRef name = readName(), eq = peek();
1128   if (eq != "=") {
1129     setError("= expected, but got " + next());
1130     while (till(")"))
1131       ;
1132     return nullptr;
1133   }
1134   llvm::SaveAndRestore saveActiveProvideSym(activeProvideSym);
1135   if (provide)
1136     activeProvideSym = name;
1137   SymbolAssignment *cmd = readSymbolAssignment(name);
1138   cmd->provide = provide;
1139   cmd->hidden = hidden;
1140   expect(")");
1141   return cmd;
1142 }
1143 
1144 // Replace whitespace sequence (including \n) with one single space. The output
1145 // is used by -Map.
1146 static void squeezeSpaces(std::string &str) {
1147   char prev = '\0';
1148   auto it = str.begin();
1149   for (char c : str)
1150     if (!isSpace(c) || (c = ' ') != prev)
1151       *it++ = prev = c;
1152   str.erase(it, str.end());
1153 }
1154 
1155 SymbolAssignment *ScriptParser::readAssignment(StringRef tok) {
1156   // Assert expression returns Dot, so this is equal to ".=."
1157   if (tok == "ASSERT")
1158     return make<SymbolAssignment>(".", readAssert(), 0, getCurrentLocation());
1159 
1160   const char *oldS = prevTok.data();
1161   SymbolAssignment *cmd = nullptr;
1162   bool savedSeenRelroEnd = ctx.script->seenRelroEnd;
1163   const StringRef op = peek();
1164   {
1165     SaveAndRestore saved(inExpr, true);
1166     if (op.starts_with("=")) {
1167       // Support = followed by an expression without whitespace.
1168       cmd = readSymbolAssignment(unquote(tok));
1169     } else if ((op.size() == 2 && op[1] == '=' && strchr("+-*/&^|", op[0])) ||
1170                op == "<<=" || op == ">>=") {
1171       cmd = readSymbolAssignment(unquote(tok));
1172     } else if (tok == "PROVIDE") {
1173       cmd = readProvideHidden(true, false);
1174     } else if (tok == "HIDDEN") {
1175       cmd = readProvideHidden(false, true);
1176     } else if (tok == "PROVIDE_HIDDEN") {
1177       cmd = readProvideHidden(true, true);
1178     }
1179   }
1180 
1181   if (cmd) {
1182     cmd->dataSegmentRelroEnd = !savedSeenRelroEnd && ctx.script->seenRelroEnd;
1183     cmd->commandString = StringRef(oldS, curTok.data() - oldS).str();
1184     squeezeSpaces(cmd->commandString);
1185     expect(";");
1186   }
1187   return cmd;
1188 }
1189 
1190 StringRef ScriptParser::readName() { return unquote(next()); }
1191 
1192 SymbolAssignment *ScriptParser::readSymbolAssignment(StringRef name) {
1193   StringRef op = next();
1194   assert(op == "=" || op == "*=" || op == "/=" || op == "+=" || op == "-=" ||
1195          op == "&=" || op == "^=" || op == "|=" || op == "<<=" || op == ">>=");
1196   // Note: GNU ld does not support %=.
1197   Expr e = readExpr();
1198   if (op != "=") {
1199     std::string loc = getCurrentLocation();
1200     e = [=, s = ctx.script, c = op[0], &ctx = ctx]() -> ExprValue {
1201       ExprValue lhs = s->getSymbolValue(name, loc);
1202       switch (c) {
1203       case '*':
1204         return lhs.getValue() * e().getValue();
1205       case '/':
1206         if (uint64_t rv = e().getValue())
1207           return lhs.getValue() / rv;
1208         ErrAlways(ctx) << loc << ": division by zero";
1209         return 0;
1210       case '+':
1211         return add(*s, lhs, e());
1212       case '-':
1213         return sub(lhs, e());
1214       case '<':
1215         return lhs.getValue() << e().getValue() % 64;
1216       case '>':
1217         return lhs.getValue() >> e().getValue() % 64;
1218       case '&':
1219         return lhs.getValue() & e().getValue();
1220       case '^':
1221         return lhs.getValue() ^ e().getValue();
1222       case '|':
1223         return lhs.getValue() | e().getValue();
1224       default:
1225         llvm_unreachable("");
1226       }
1227     };
1228   }
1229   return make<SymbolAssignment>(name, e, ctx.scriptSymOrderCounter++,
1230                                 getCurrentLocation());
1231 }
1232 
1233 // This is an operator-precedence parser to parse a linker
1234 // script expression.
1235 Expr ScriptParser::readExpr() {
1236   // Our lexer is context-aware. Set the in-expression bit so that
1237   // they apply different tokenization rules.
1238   SaveAndRestore saved(inExpr, true);
1239   Expr e = readExpr1(readPrimary(), 0);
1240   return e;
1241 }
1242 
1243 Expr ScriptParser::combine(StringRef op, Expr l, Expr r) {
1244   if (op == "+")
1245     return [=, s = ctx.script] { return add(*s, l(), r()); };
1246   if (op == "-")
1247     return [=] { return sub(l(), r()); };
1248   if (op == "*")
1249     return [=] { return l().getValue() * r().getValue(); };
1250   if (op == "/") {
1251     std::string loc = getCurrentLocation();
1252     return [=, &ctx = ctx]() -> uint64_t {
1253       if (uint64_t rv = r().getValue())
1254         return l().getValue() / rv;
1255       ErrAlways(ctx) << loc << ": division by zero";
1256       return 0;
1257     };
1258   }
1259   if (op == "%") {
1260     std::string loc = getCurrentLocation();
1261     return [=, &ctx = ctx]() -> uint64_t {
1262       if (uint64_t rv = r().getValue())
1263         return l().getValue() % rv;
1264       ErrAlways(ctx) << loc << ": modulo by zero";
1265       return 0;
1266     };
1267   }
1268   if (op == "<<")
1269     return [=] { return l().getValue() << r().getValue() % 64; };
1270   if (op == ">>")
1271     return [=] { return l().getValue() >> r().getValue() % 64; };
1272   if (op == "<")
1273     return [=] { return l().getValue() < r().getValue(); };
1274   if (op == ">")
1275     return [=] { return l().getValue() > r().getValue(); };
1276   if (op == ">=")
1277     return [=] { return l().getValue() >= r().getValue(); };
1278   if (op == "<=")
1279     return [=] { return l().getValue() <= r().getValue(); };
1280   if (op == "==")
1281     return [=] { return l().getValue() == r().getValue(); };
1282   if (op == "!=")
1283     return [=] { return l().getValue() != r().getValue(); };
1284   if (op == "||")
1285     return [=] { return l().getValue() || r().getValue(); };
1286   if (op == "&&")
1287     return [=] { return l().getValue() && r().getValue(); };
1288   if (op == "&")
1289     return [=, s = ctx.script] { return bitAnd(*s, l(), r()); };
1290   if (op == "^")
1291     return [=, s = ctx.script] { return bitXor(*s, l(), r()); };
1292   if (op == "|")
1293     return [=, s = ctx.script] { return bitOr(*s, l(), r()); };
1294   llvm_unreachable("invalid operator");
1295 }
1296 
1297 // This is a part of the operator-precedence parser. This function
1298 // assumes that the remaining token stream starts with an operator.
1299 Expr ScriptParser::readExpr1(Expr lhs, int minPrec) {
1300   while (!atEOF() && !errCount(ctx)) {
1301     // Read an operator and an expression.
1302     StringRef op1 = peek();
1303     if (precedence(op1) < minPrec)
1304       break;
1305     skip();
1306     if (op1 == "?")
1307       return readTernary(lhs);
1308     Expr rhs = readPrimary();
1309 
1310     // Evaluate the remaining part of the expression first if the
1311     // next operator has greater precedence than the previous one.
1312     // For example, if we have read "+" and "3", and if the next
1313     // operator is "*", then we'll evaluate 3 * ... part first.
1314     while (!atEOF()) {
1315       StringRef op2 = peek();
1316       if (precedence(op2) <= precedence(op1))
1317         break;
1318       rhs = readExpr1(rhs, precedence(op2));
1319     }
1320 
1321     lhs = combine(op1, lhs, rhs);
1322   }
1323   return lhs;
1324 }
1325 
1326 Expr ScriptParser::getPageSize() {
1327   std::string location = getCurrentLocation();
1328   return [=, &ctx = this->ctx]() -> uint64_t {
1329     if (ctx.target)
1330       return ctx.arg.commonPageSize;
1331     ErrAlways(ctx) << location << ": unable to calculate page size";
1332     return 4096; // Return a dummy value.
1333   };
1334 }
1335 
1336 Expr ScriptParser::readConstant() {
1337   StringRef s = readParenName();
1338   if (s == "COMMONPAGESIZE")
1339     return getPageSize();
1340   if (s == "MAXPAGESIZE")
1341     return [&ctx = this->ctx] { return ctx.arg.maxPageSize; };
1342   setError("unknown constant: " + s);
1343   return [] { return 0; };
1344 }
1345 
1346 // Parses Tok as an integer. It recognizes hexadecimal (prefixed with
1347 // "0x" or suffixed with "H") and decimal numbers. Decimal numbers may
1348 // have "K" (Ki) or "M" (Mi) suffixes.
1349 static std::optional<uint64_t> parseInt(StringRef tok) {
1350   // Hexadecimal
1351   uint64_t val;
1352   if (tok.starts_with_insensitive("0x")) {
1353     if (!to_integer(tok.substr(2), val, 16))
1354       return std::nullopt;
1355     return val;
1356   }
1357   if (tok.ends_with_insensitive("H")) {
1358     if (!to_integer(tok.drop_back(), val, 16))
1359       return std::nullopt;
1360     return val;
1361   }
1362 
1363   // Decimal
1364   if (tok.ends_with_insensitive("K")) {
1365     if (!to_integer(tok.drop_back(), val, 10))
1366       return std::nullopt;
1367     return val * 1024;
1368   }
1369   if (tok.ends_with_insensitive("M")) {
1370     if (!to_integer(tok.drop_back(), val, 10))
1371       return std::nullopt;
1372     return val * 1024 * 1024;
1373   }
1374   if (!to_integer(tok, val, 10))
1375     return std::nullopt;
1376   return val;
1377 }
1378 
1379 ByteCommand *ScriptParser::readByteCommand(StringRef tok) {
1380   int size = StringSwitch<int>(tok)
1381                  .Case("BYTE", 1)
1382                  .Case("SHORT", 2)
1383                  .Case("LONG", 4)
1384                  .Case("QUAD", 8)
1385                  .Default(-1);
1386   if (size == -1)
1387     return nullptr;
1388 
1389   const char *oldS = prevTok.data();
1390   Expr e = readParenExpr();
1391   std::string commandString = StringRef(oldS, curBuf.s.data() - oldS).str();
1392   squeezeSpaces(commandString);
1393   return make<ByteCommand>(e, size, std::move(commandString));
1394 }
1395 
1396 static std::optional<uint64_t> parseFlag(StringRef tok) {
1397   if (std::optional<uint64_t> asInt = parseInt(tok))
1398     return asInt;
1399 #define CASE_ENT(enum) #enum, ELF::enum
1400   return StringSwitch<std::optional<uint64_t>>(tok)
1401       .Case(CASE_ENT(SHF_WRITE))
1402       .Case(CASE_ENT(SHF_ALLOC))
1403       .Case(CASE_ENT(SHF_EXECINSTR))
1404       .Case(CASE_ENT(SHF_MERGE))
1405       .Case(CASE_ENT(SHF_STRINGS))
1406       .Case(CASE_ENT(SHF_INFO_LINK))
1407       .Case(CASE_ENT(SHF_LINK_ORDER))
1408       .Case(CASE_ENT(SHF_OS_NONCONFORMING))
1409       .Case(CASE_ENT(SHF_GROUP))
1410       .Case(CASE_ENT(SHF_TLS))
1411       .Case(CASE_ENT(SHF_COMPRESSED))
1412       .Case(CASE_ENT(SHF_EXCLUDE))
1413       .Case(CASE_ENT(SHF_ARM_PURECODE))
1414       .Default(std::nullopt);
1415 #undef CASE_ENT
1416 }
1417 
1418 // Reads the '(' <flags> ')' list of section flags in
1419 // INPUT_SECTION_FLAGS '(' <flags> ')' in the
1420 // following form:
1421 // <flags> ::= <flag>
1422 //           | <flags> & flag
1423 // <flag>  ::= Recognized Flag Name, or Integer value of flag.
1424 // If the first character of <flag> is a ! then this means without flag,
1425 // otherwise with flag.
1426 // Example: SHF_EXECINSTR & !SHF_WRITE means with flag SHF_EXECINSTR and
1427 // without flag SHF_WRITE.
1428 std::pair<uint64_t, uint64_t> ScriptParser::readInputSectionFlags() {
1429   uint64_t withFlags = 0;
1430   uint64_t withoutFlags = 0;
1431   expect("(");
1432   while (!errCount(ctx)) {
1433     StringRef tok = readName();
1434     bool without = tok.consume_front("!");
1435     if (std::optional<uint64_t> flag = parseFlag(tok)) {
1436       if (without)
1437         withoutFlags |= *flag;
1438       else
1439         withFlags |= *flag;
1440     } else {
1441       setError("unrecognised flag: " + tok);
1442     }
1443     if (consume(")"))
1444       break;
1445     if (!consume("&")) {
1446       next();
1447       setError("expected & or )");
1448     }
1449   }
1450   return std::make_pair(withFlags, withoutFlags);
1451 }
1452 
1453 StringRef ScriptParser::readParenName() {
1454   expect("(");
1455   bool orig = inExpr;
1456   inExpr = false;
1457   StringRef tok = readName();
1458   inExpr = orig;
1459   expect(")");
1460   return tok;
1461 }
1462 
1463 static void checkIfExists(LinkerScript &script, const OutputSection &osec,
1464                           StringRef location) {
1465   if (osec.location.empty() && script.errorOnMissingSection)
1466     script.recordError(location + ": undefined section " + osec.name);
1467 }
1468 
1469 static bool isValidSymbolName(StringRef s) {
1470   auto valid = [](char c) {
1471     return isAlnum(c) || c == '$' || c == '.' || c == '_';
1472   };
1473   return !s.empty() && !isDigit(s[0]) && llvm::all_of(s, valid);
1474 }
1475 
1476 Expr ScriptParser::readPrimary() {
1477   if (peek() == "(")
1478     return readParenExpr();
1479 
1480   if (consume("~")) {
1481     Expr e = readPrimary();
1482     return [=] { return ~e().getValue(); };
1483   }
1484   if (consume("!")) {
1485     Expr e = readPrimary();
1486     return [=] { return !e().getValue(); };
1487   }
1488   if (consume("-")) {
1489     Expr e = readPrimary();
1490     return [=] { return -e().getValue(); };
1491   }
1492   if (consume("+"))
1493     return readPrimary();
1494 
1495   StringRef tok = next();
1496   std::string location = getCurrentLocation();
1497 
1498   // Built-in functions are parsed here.
1499   // https://sourceware.org/binutils/docs/ld/Builtin-Functions.html.
1500   if (tok == "ABSOLUTE") {
1501     Expr inner = readParenExpr();
1502     return [=] {
1503       ExprValue i = inner();
1504       i.forceAbsolute = true;
1505       return i;
1506     };
1507   }
1508   if (tok == "ADDR") {
1509     StringRef name = readParenName();
1510     OutputSection *osec = &ctx.script->getOrCreateOutputSection(name)->osec;
1511     osec->usedInExpression = true;
1512     return [=, s = ctx.script]() -> ExprValue {
1513       checkIfExists(*s, *osec, location);
1514       return {osec, false, 0, location};
1515     };
1516   }
1517   if (tok == "ALIGN") {
1518     expect("(");
1519     Expr e = readExpr();
1520     if (consume(")")) {
1521       e = checkAlignment(ctx, e, location);
1522       return [=, s = ctx.script] {
1523         return alignToPowerOf2(s->getDot(), e().getValue());
1524       };
1525     }
1526     expect(",");
1527     Expr e2 = checkAlignment(ctx, readExpr(), location);
1528     expect(")");
1529     return [=] {
1530       ExprValue v = e();
1531       v.alignment = e2().getValue();
1532       return v;
1533     };
1534   }
1535   if (tok == "ALIGNOF") {
1536     StringRef name = readParenName();
1537     OutputSection *osec = &ctx.script->getOrCreateOutputSection(name)->osec;
1538     return [=, s = ctx.script] {
1539       checkIfExists(*s, *osec, location);
1540       return osec->addralign;
1541     };
1542   }
1543   if (tok == "ASSERT")
1544     return readAssert();
1545   if (tok == "CONSTANT")
1546     return readConstant();
1547   if (tok == "DATA_SEGMENT_ALIGN") {
1548     expect("(");
1549     Expr e = readExpr();
1550     expect(",");
1551     readExpr();
1552     expect(")");
1553     ctx.script->seenDataAlign = true;
1554     return [=, s = ctx.script] {
1555       uint64_t align = std::max(uint64_t(1), e().getValue());
1556       return (s->getDot() + align - 1) & -align;
1557     };
1558   }
1559   if (tok == "DATA_SEGMENT_END") {
1560     expect("(");
1561     expect(".");
1562     expect(")");
1563     return [s = ctx.script] { return s->getDot(); };
1564   }
1565   if (tok == "DATA_SEGMENT_RELRO_END") {
1566     // GNU linkers implements more complicated logic to handle
1567     // DATA_SEGMENT_RELRO_END. We instead ignore the arguments and
1568     // just align to the next page boundary for simplicity.
1569     expect("(");
1570     readExpr();
1571     expect(",");
1572     readExpr();
1573     expect(")");
1574     ctx.script->seenRelroEnd = true;
1575     return [&ctx = this->ctx] {
1576       return alignToPowerOf2(ctx.script->getDot(), ctx.arg.maxPageSize);
1577     };
1578   }
1579   if (tok == "DEFINED") {
1580     StringRef name = readParenName();
1581     // Return 1 if s is defined. If the definition is only found in a linker
1582     // script, it must happen before this DEFINED.
1583     auto order = ctx.scriptSymOrderCounter++;
1584     return [=, &ctx = this->ctx] {
1585       Symbol *s = ctx.symtab->find(name);
1586       return s && s->isDefined() && ctx.scriptSymOrder.lookup(s) < order ? 1
1587                                                                          : 0;
1588     };
1589   }
1590   if (tok == "LENGTH") {
1591     StringRef name = readParenName();
1592     if (ctx.script->memoryRegions.count(name) == 0) {
1593       setError("memory region not defined: " + name);
1594       return [] { return 0; };
1595     }
1596     return ctx.script->memoryRegions[name]->length;
1597   }
1598   if (tok == "LOADADDR") {
1599     StringRef name = readParenName();
1600     OutputSection *osec = &ctx.script->getOrCreateOutputSection(name)->osec;
1601     osec->usedInExpression = true;
1602     return [=, s = ctx.script] {
1603       checkIfExists(*s, *osec, location);
1604       return osec->getLMA();
1605     };
1606   }
1607   if (tok == "LOG2CEIL") {
1608     expect("(");
1609     Expr a = readExpr();
1610     expect(")");
1611     return [=] {
1612       // LOG2CEIL(0) is defined to be 0.
1613       return llvm::Log2_64_Ceil(std::max(a().getValue(), UINT64_C(1)));
1614     };
1615   }
1616   if (tok == "MAX" || tok == "MIN") {
1617     expect("(");
1618     Expr a = readExpr();
1619     expect(",");
1620     Expr b = readExpr();
1621     expect(")");
1622     if (tok == "MIN")
1623       return [=] { return std::min(a().getValue(), b().getValue()); };
1624     return [=] { return std::max(a().getValue(), b().getValue()); };
1625   }
1626   if (tok == "ORIGIN") {
1627     StringRef name = readParenName();
1628     if (ctx.script->memoryRegions.count(name) == 0) {
1629       setError("memory region not defined: " + name);
1630       return [] { return 0; };
1631     }
1632     return ctx.script->memoryRegions[name]->origin;
1633   }
1634   if (tok == "SEGMENT_START") {
1635     expect("(");
1636     skip();
1637     expect(",");
1638     Expr e = readExpr();
1639     expect(")");
1640     return [=] { return e(); };
1641   }
1642   if (tok == "SIZEOF") {
1643     StringRef name = readParenName();
1644     OutputSection *cmd = &ctx.script->getOrCreateOutputSection(name)->osec;
1645     // Linker script does not create an output section if its content is empty.
1646     // We want to allow SIZEOF(.foo) where .foo is a section which happened to
1647     // be empty.
1648     return [=] { return cmd->size; };
1649   }
1650   if (tok == "SIZEOF_HEADERS")
1651     return [=, &ctx = ctx] { return elf::getHeaderSize(ctx); };
1652 
1653   // Tok is the dot.
1654   if (tok == ".")
1655     return [=, s = ctx.script] { return s->getSymbolValue(tok, location); };
1656 
1657   // Tok is a literal number.
1658   if (std::optional<uint64_t> val = parseInt(tok))
1659     return [=] { return *val; };
1660 
1661   // Tok is a symbol name.
1662   if (tok.starts_with("\""))
1663     tok = unquote(tok);
1664   else if (!isValidSymbolName(tok))
1665     setError("malformed number: " + tok);
1666   if (activeProvideSym)
1667     ctx.script->provideMap[*activeProvideSym].push_back(tok);
1668   else
1669     ctx.script->referencedSymbols.push_back(tok);
1670   return [=, s = ctx.script] { return s->getSymbolValue(tok, location); };
1671 }
1672 
1673 Expr ScriptParser::readTernary(Expr cond) {
1674   Expr l = readExpr();
1675   expect(":");
1676   Expr r = readExpr();
1677   return [=] { return cond().getValue() ? l() : r(); };
1678 }
1679 
1680 Expr ScriptParser::readParenExpr() {
1681   expect("(");
1682   Expr e = readExpr();
1683   expect(")");
1684   return e;
1685 }
1686 
1687 SmallVector<StringRef, 0> ScriptParser::readOutputSectionPhdrs() {
1688   SmallVector<StringRef, 0> phdrs;
1689   while (!errCount(ctx) && peek().starts_with(":")) {
1690     StringRef tok = next();
1691     phdrs.push_back((tok.size() == 1) ? readName() : tok.substr(1));
1692   }
1693   return phdrs;
1694 }
1695 
1696 // Read a program header type name. The next token must be a
1697 // name of a program header type or a constant (e.g. "0x3").
1698 unsigned ScriptParser::readPhdrType() {
1699   StringRef tok = next();
1700   if (std::optional<uint64_t> val = parseInt(tok))
1701     return *val;
1702 
1703   unsigned ret = StringSwitch<unsigned>(tok)
1704                      .Case("PT_NULL", PT_NULL)
1705                      .Case("PT_LOAD", PT_LOAD)
1706                      .Case("PT_DYNAMIC", PT_DYNAMIC)
1707                      .Case("PT_INTERP", PT_INTERP)
1708                      .Case("PT_NOTE", PT_NOTE)
1709                      .Case("PT_SHLIB", PT_SHLIB)
1710                      .Case("PT_PHDR", PT_PHDR)
1711                      .Case("PT_TLS", PT_TLS)
1712                      .Case("PT_GNU_EH_FRAME", PT_GNU_EH_FRAME)
1713                      .Case("PT_GNU_STACK", PT_GNU_STACK)
1714                      .Case("PT_GNU_RELRO", PT_GNU_RELRO)
1715                      .Case("PT_OPENBSD_MUTABLE", PT_OPENBSD_MUTABLE)
1716                      .Case("PT_OPENBSD_RANDOMIZE", PT_OPENBSD_RANDOMIZE)
1717                      .Case("PT_OPENBSD_SYSCALLS", PT_OPENBSD_SYSCALLS)
1718                      .Case("PT_OPENBSD_WXNEEDED", PT_OPENBSD_WXNEEDED)
1719                      .Case("PT_OPENBSD_BOOTDATA", PT_OPENBSD_BOOTDATA)
1720                      .Default(-1);
1721 
1722   if (ret == (unsigned)-1) {
1723     setError("invalid program header type: " + tok);
1724     return PT_NULL;
1725   }
1726   return ret;
1727 }
1728 
1729 // Reads an anonymous version declaration.
1730 void ScriptParser::readAnonymousDeclaration() {
1731   SmallVector<SymbolVersion, 0> locals;
1732   SmallVector<SymbolVersion, 0> globals;
1733   std::tie(locals, globals) = readSymbols();
1734   for (const SymbolVersion &pat : locals)
1735     ctx.arg.versionDefinitions[VER_NDX_LOCAL].localPatterns.push_back(pat);
1736   for (const SymbolVersion &pat : globals)
1737     ctx.arg.versionDefinitions[VER_NDX_GLOBAL].nonLocalPatterns.push_back(pat);
1738 
1739   expect(";");
1740 }
1741 
1742 // Reads a non-anonymous version definition,
1743 // e.g. "VerStr { global: foo; bar; local: *; };".
1744 void ScriptParser::readVersionDeclaration(StringRef verStr) {
1745   // Read a symbol list.
1746   SmallVector<SymbolVersion, 0> locals;
1747   SmallVector<SymbolVersion, 0> globals;
1748   std::tie(locals, globals) = readSymbols();
1749 
1750   // Create a new version definition and add that to the global symbols.
1751   VersionDefinition ver;
1752   ver.name = verStr;
1753   ver.nonLocalPatterns = std::move(globals);
1754   ver.localPatterns = std::move(locals);
1755   ver.id = ctx.arg.versionDefinitions.size();
1756   ctx.arg.versionDefinitions.push_back(ver);
1757 
1758   // Each version may have a parent version. For example, "Ver2"
1759   // defined as "Ver2 { global: foo; local: *; } Ver1;" has "Ver1"
1760   // as a parent. This version hierarchy is, probably against your
1761   // instinct, purely for hint; the runtime doesn't care about it
1762   // at all. In LLD, we simply ignore it.
1763   if (next() != ";")
1764     expect(";");
1765 }
1766 
1767 bool elf::hasWildcard(StringRef s) {
1768   return s.find_first_of("?*[") != StringRef::npos;
1769 }
1770 
1771 // Reads a list of symbols, e.g. "{ global: foo; bar; local: *; };".
1772 std::pair<SmallVector<SymbolVersion, 0>, SmallVector<SymbolVersion, 0>>
1773 ScriptParser::readSymbols() {
1774   SmallVector<SymbolVersion, 0> locals;
1775   SmallVector<SymbolVersion, 0> globals;
1776   SmallVector<SymbolVersion, 0> *v = &globals;
1777 
1778   while (auto tok = till("}")) {
1779     if (tok == "extern") {
1780       SmallVector<SymbolVersion, 0> ext = readVersionExtern();
1781       v->insert(v->end(), ext.begin(), ext.end());
1782     } else {
1783       if (tok == "local:" || (tok == "local" && consume(":"))) {
1784         v = &locals;
1785         continue;
1786       }
1787       if (tok == "global:" || (tok == "global" && consume(":"))) {
1788         v = &globals;
1789         continue;
1790       }
1791       v->push_back({unquote(tok), false, hasWildcard(tok)});
1792     }
1793     expect(";");
1794   }
1795   return {locals, globals};
1796 }
1797 
1798 // Reads an "extern C++" directive, e.g.,
1799 // "extern "C++" { ns::*; "f(int, double)"; };"
1800 //
1801 // The last semicolon is optional. E.g. this is OK:
1802 // "extern "C++" { ns::*; "f(int, double)" };"
1803 SmallVector<SymbolVersion, 0> ScriptParser::readVersionExtern() {
1804   StringRef tok = next();
1805   bool isCXX = tok == "\"C++\"";
1806   if (!isCXX && tok != "\"C\"")
1807     setError("Unknown language");
1808   expect("{");
1809 
1810   SmallVector<SymbolVersion, 0> ret;
1811   while (auto tok = till("}")) {
1812     ret.push_back(
1813         {unquote(tok), isCXX, !tok.str.starts_with("\"") && hasWildcard(tok)});
1814     if (consume("}"))
1815       return ret;
1816     expect(";");
1817   }
1818   return ret;
1819 }
1820 
1821 Expr ScriptParser::readMemoryAssignment(StringRef s1, StringRef s2,
1822                                         StringRef s3) {
1823   if (!consume(s1) && !consume(s2) && !consume(s3)) {
1824     setError("expected one of: " + s1 + ", " + s2 + ", or " + s3);
1825     return [] { return 0; };
1826   }
1827   expect("=");
1828   return readExpr();
1829 }
1830 
1831 // Parse the MEMORY command as specified in:
1832 // https://sourceware.org/binutils/docs/ld/MEMORY.html
1833 //
1834 // MEMORY { name [(attr)] : ORIGIN = origin, LENGTH = len ... }
1835 void ScriptParser::readMemory() {
1836   expect("{");
1837   while (auto tok = till("}")) {
1838     if (tok == "INCLUDE") {
1839       readInclude();
1840       continue;
1841     }
1842 
1843     uint32_t flags = 0;
1844     uint32_t invFlags = 0;
1845     uint32_t negFlags = 0;
1846     uint32_t negInvFlags = 0;
1847     if (consume("(")) {
1848       readMemoryAttributes(flags, invFlags, negFlags, negInvFlags);
1849       expect(")");
1850     }
1851     expect(":");
1852 
1853     Expr origin = readMemoryAssignment("ORIGIN", "org", "o");
1854     expect(",");
1855     Expr length = readMemoryAssignment("LENGTH", "len", "l");
1856 
1857     // Add the memory region to the region map.
1858     MemoryRegion *mr = make<MemoryRegion>(tok, origin, length, flags, invFlags,
1859                                           negFlags, negInvFlags);
1860     if (!ctx.script->memoryRegions.insert({tok, mr}).second)
1861       setError("region '" + tok + "' already defined");
1862   }
1863 }
1864 
1865 // This function parses the attributes used to match against section
1866 // flags when placing output sections in a memory region. These flags
1867 // are only used when an explicit memory region name is not used.
1868 void ScriptParser::readMemoryAttributes(uint32_t &flags, uint32_t &invFlags,
1869                                         uint32_t &negFlags,
1870                                         uint32_t &negInvFlags) {
1871   bool invert = false;
1872 
1873   for (char c : next().lower()) {
1874     if (c == '!') {
1875       invert = !invert;
1876       std::swap(flags, negFlags);
1877       std::swap(invFlags, negInvFlags);
1878       continue;
1879     }
1880     if (c == 'w')
1881       flags |= SHF_WRITE;
1882     else if (c == 'x')
1883       flags |= SHF_EXECINSTR;
1884     else if (c == 'a')
1885       flags |= SHF_ALLOC;
1886     else if (c == 'r')
1887       invFlags |= SHF_WRITE;
1888     else
1889       setError("invalid memory region attribute");
1890   }
1891 
1892   if (invert) {
1893     std::swap(flags, negFlags);
1894     std::swap(invFlags, negInvFlags);
1895   }
1896 }
1897 
1898 void elf::readLinkerScript(Ctx &ctx, MemoryBufferRef mb) {
1899   llvm::TimeTraceScope timeScope("Read linker script",
1900                                  mb.getBufferIdentifier());
1901   ScriptParser(ctx, mb).readLinkerScript();
1902 }
1903 
1904 void elf::readVersionScript(Ctx &ctx, MemoryBufferRef mb) {
1905   llvm::TimeTraceScope timeScope("Read version script",
1906                                  mb.getBufferIdentifier());
1907   ScriptParser(ctx, mb).readVersionScript();
1908 }
1909 
1910 void elf::readDynamicList(Ctx &ctx, MemoryBufferRef mb) {
1911   llvm::TimeTraceScope timeScope("Read dynamic list", mb.getBufferIdentifier());
1912   ScriptParser(ctx, mb).readDynamicList();
1913 }
1914 
1915 void elf::readDefsym(Ctx &ctx, MemoryBufferRef mb) {
1916   ScriptParser(ctx, mb).readDefsym();
1917 }
1918