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