xref: /freebsd/contrib/llvm-project/lld/ELF/ScriptParser.cpp (revision d396c67f26b079c2808002c07212d9df9818a11b)
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("elf32-powerpcle", {ELF32LEKind, EM_PPC})
417       .Case("elf64-powerpc", {ELF64BEKind, EM_PPC64})
418       .Case("elf64-powerpcle", {ELF64LEKind, EM_PPC64})
419       .Case("elf64-x86-64", {ELF64LEKind, EM_X86_64})
420       .Cases("elf32-tradbigmips", "elf32-bigmips", {ELF32BEKind, EM_MIPS})
421       .Case("elf32-ntradbigmips", {ELF32BEKind, EM_MIPS})
422       .Case("elf32-tradlittlemips", {ELF32LEKind, EM_MIPS})
423       .Case("elf32-ntradlittlemips", {ELF32LEKind, EM_MIPS})
424       .Case("elf64-tradbigmips", {ELF64BEKind, EM_MIPS})
425       .Case("elf64-tradlittlemips", {ELF64LEKind, EM_MIPS})
426       .Case("elf32-littleriscv", {ELF32LEKind, EM_RISCV})
427       .Case("elf64-littleriscv", {ELF64LEKind, EM_RISCV})
428       .Case("elf64-sparc", {ELF64BEKind, EM_SPARCV9})
429       .Case("elf32-msp430", {ELF32LEKind, EM_MSP430})
430       .Default({ELFNoneKind, EM_NONE});
431 }
432 
433 // Parse OUTPUT_FORMAT(bfdname) or OUTPUT_FORMAT(bfdname, big, little).
434 // Currently we ignore big and little parameters.
435 void ScriptParser::readOutputFormat() {
436   expect("(");
437 
438   config->bfdname = unquote(next());
439   StringRef s = config->bfdname;
440   if (s.consume_back("-freebsd"))
441     config->osabi = ELFOSABI_FREEBSD;
442 
443   std::tie(config->ekind, config->emachine) = parseBfdName(s);
444   if (config->emachine == EM_NONE)
445     setError("unknown output format name: " + config->bfdname);
446   if (s == "elf32-ntradlittlemips" || s == "elf32-ntradbigmips")
447     config->mipsN32Abi = true;
448   if (config->emachine == EM_MSP430)
449     config->osabi = ELFOSABI_STANDALONE;
450 
451   if (consume(")"))
452     return;
453   expect(",");
454   skip();
455   expect(",");
456   skip();
457   expect(")");
458 }
459 
460 void ScriptParser::readPhdrs() {
461   expect("{");
462 
463   while (!errorCount() && !consume("}")) {
464     PhdrsCommand cmd;
465     cmd.name = next();
466     cmd.type = readPhdrType();
467 
468     while (!errorCount() && !consume(";")) {
469       if (consume("FILEHDR"))
470         cmd.hasFilehdr = true;
471       else if (consume("PHDRS"))
472         cmd.hasPhdrs = true;
473       else if (consume("AT"))
474         cmd.lmaExpr = readParenExpr();
475       else if (consume("FLAGS"))
476         cmd.flags = readParenExpr()().getValue();
477       else
478         setError("unexpected header attribute: " + next());
479     }
480 
481     script->phdrsCommands.push_back(cmd);
482   }
483 }
484 
485 void ScriptParser::readRegionAlias() {
486   expect("(");
487   StringRef alias = unquote(next());
488   expect(",");
489   StringRef name = next();
490   expect(")");
491 
492   if (script->memoryRegions.count(alias))
493     setError("redefinition of memory region '" + alias + "'");
494   if (!script->memoryRegions.count(name))
495     setError("memory region '" + name + "' is not defined");
496   script->memoryRegions.insert({alias, script->memoryRegions[name]});
497 }
498 
499 void ScriptParser::readSearchDir() {
500   expect("(");
501   StringRef tok = next();
502   if (!config->nostdlib)
503     config->searchPaths.push_back(unquote(tok));
504   expect(")");
505 }
506 
507 // This reads an overlay description. Overlays are used to describe output
508 // sections that use the same virtual memory range and normally would trigger
509 // linker's sections sanity check failures.
510 // https://sourceware.org/binutils/docs/ld/Overlay-Description.html#Overlay-Description
511 std::vector<BaseCommand *> ScriptParser::readOverlay() {
512   // VA and LMA expressions are optional, though for simplicity of
513   // implementation we assume they are not. That is what OVERLAY was designed
514   // for first of all: to allow sections with overlapping VAs at different LMAs.
515   Expr addrExpr = readExpr();
516   expect(":");
517   expect("AT");
518   Expr lmaExpr = readParenExpr();
519   expect("{");
520 
521   std::vector<BaseCommand *> v;
522   OutputSection *prev = nullptr;
523   while (!errorCount() && !consume("}")) {
524     // VA is the same for all sections. The LMAs are consecutive in memory
525     // starting from the base load address specified.
526     OutputSection *os = readOverlaySectionDescription();
527     os->addrExpr = addrExpr;
528     if (prev)
529       os->lmaExpr = [=] { return prev->getLMA() + prev->size; };
530     else
531       os->lmaExpr = lmaExpr;
532     v.push_back(os);
533     prev = os;
534   }
535 
536   // According to the specification, at the end of the overlay, the location
537   // counter should be equal to the overlay base address plus size of the
538   // largest section seen in the overlay.
539   // Here we want to create the Dot assignment command to achieve that.
540   Expr moveDot = [=] {
541     uint64_t max = 0;
542     for (BaseCommand *cmd : v)
543       max = std::max(max, cast<OutputSection>(cmd)->size);
544     return addrExpr().getValue() + max;
545   };
546   v.push_back(make<SymbolAssignment>(".", moveDot, getCurrentLocation()));
547   return v;
548 }
549 
550 void ScriptParser::readSections() {
551   expect("{");
552   std::vector<BaseCommand *> v;
553   while (!errorCount() && !consume("}")) {
554     StringRef tok = next();
555     if (tok == "OVERLAY") {
556       for (BaseCommand *cmd : readOverlay())
557         v.push_back(cmd);
558       continue;
559     } else if (tok == "INCLUDE") {
560       readInclude();
561       continue;
562     }
563 
564     if (BaseCommand *cmd = readAssignment(tok))
565       v.push_back(cmd);
566     else
567       v.push_back(readOutputSectionDescription(tok));
568   }
569   script->sectionCommands.insert(script->sectionCommands.end(), v.begin(),
570                                  v.end());
571 
572   if (atEOF() || !consume("INSERT")) {
573     script->hasSectionsCommand = true;
574     return;
575   }
576 
577   bool isAfter = false;
578   if (consume("AFTER"))
579     isAfter = true;
580   else if (!consume("BEFORE"))
581     setError("expected AFTER/BEFORE, but got '" + next() + "'");
582   StringRef where = next();
583   for (BaseCommand *cmd : v)
584     if (auto *os = dyn_cast<OutputSection>(cmd))
585       script->insertCommands.push_back({os, isAfter, where});
586 }
587 
588 void ScriptParser::readTarget() {
589   // TARGET(foo) is an alias for "--format foo". Unlike GNU linkers,
590   // we accept only a limited set of BFD names (i.e. "elf" or "binary")
591   // for --format. We recognize only /^elf/ and "binary" in the linker
592   // script as well.
593   expect("(");
594   StringRef tok = next();
595   expect(")");
596 
597   if (tok.startswith("elf"))
598     config->formatBinary = false;
599   else if (tok == "binary")
600     config->formatBinary = true;
601   else
602     setError("unknown target: " + tok);
603 }
604 
605 static int precedence(StringRef op) {
606   return StringSwitch<int>(op)
607       .Cases("*", "/", "%", 8)
608       .Cases("+", "-", 7)
609       .Cases("<<", ">>", 6)
610       .Cases("<", "<=", ">", ">=", "==", "!=", 5)
611       .Case("&", 4)
612       .Case("|", 3)
613       .Case("&&", 2)
614       .Case("||", 1)
615       .Default(-1);
616 }
617 
618 StringMatcher ScriptParser::readFilePatterns() {
619   StringMatcher Matcher;
620 
621   while (!errorCount() && !consume(")"))
622     Matcher.addPattern(SingleStringMatcher(next()));
623   return Matcher;
624 }
625 
626 SortSectionPolicy ScriptParser::peekSortKind() {
627   return StringSwitch<SortSectionPolicy>(peek())
628       .Cases("SORT", "SORT_BY_NAME", SortSectionPolicy::Name)
629       .Case("SORT_BY_ALIGNMENT", SortSectionPolicy::Alignment)
630       .Case("SORT_BY_INIT_PRIORITY", SortSectionPolicy::Priority)
631       .Case("SORT_NONE", SortSectionPolicy::None)
632       .Default(SortSectionPolicy::Default);
633 }
634 
635 SortSectionPolicy ScriptParser::readSortKind() {
636   SortSectionPolicy ret = peekSortKind();
637   if (ret != SortSectionPolicy::Default)
638     skip();
639   return ret;
640 }
641 
642 // Reads SECTIONS command contents in the following form:
643 //
644 // <contents> ::= <elem>*
645 // <elem>     ::= <exclude>? <glob-pattern>
646 // <exclude>  ::= "EXCLUDE_FILE" "(" <glob-pattern>+ ")"
647 //
648 // For example,
649 //
650 // *(.foo EXCLUDE_FILE (a.o) .bar EXCLUDE_FILE (b.o) .baz)
651 //
652 // is parsed as ".foo", ".bar" with "a.o", and ".baz" with "b.o".
653 // The semantics of that is section .foo in any file, section .bar in
654 // any file but a.o, and section .baz in any file but b.o.
655 std::vector<SectionPattern> ScriptParser::readInputSectionsList() {
656   std::vector<SectionPattern> ret;
657   while (!errorCount() && peek() != ")") {
658     StringMatcher excludeFilePat;
659     if (consume("EXCLUDE_FILE")) {
660       expect("(");
661       excludeFilePat = readFilePatterns();
662     }
663 
664     StringMatcher SectionMatcher;
665     // Break if the next token is ), EXCLUDE_FILE, or SORT*.
666     while (!errorCount() && peek() != ")" && peek() != "EXCLUDE_FILE" &&
667            peekSortKind() == SortSectionPolicy::Default)
668       SectionMatcher.addPattern(unquote(next()));
669 
670     if (!SectionMatcher.empty())
671       ret.push_back({std::move(excludeFilePat), std::move(SectionMatcher)});
672     else if (excludeFilePat.empty())
673       break;
674     else
675       setError("section pattern is expected");
676   }
677   return ret;
678 }
679 
680 // Reads contents of "SECTIONS" directive. That directive contains a
681 // list of glob patterns for input sections. The grammar is as follows.
682 //
683 // <patterns> ::= <section-list>
684 //              | <sort> "(" <section-list> ")"
685 //              | <sort> "(" <sort> "(" <section-list> ")" ")"
686 //
687 // <sort>     ::= "SORT" | "SORT_BY_NAME" | "SORT_BY_ALIGNMENT"
688 //              | "SORT_BY_INIT_PRIORITY" | "SORT_NONE"
689 //
690 // <section-list> is parsed by readInputSectionsList().
691 InputSectionDescription *
692 ScriptParser::readInputSectionRules(StringRef filePattern, uint64_t withFlags,
693                                     uint64_t withoutFlags) {
694   auto *cmd =
695       make<InputSectionDescription>(filePattern, withFlags, withoutFlags);
696   expect("(");
697 
698   while (!errorCount() && !consume(")")) {
699     SortSectionPolicy outer = readSortKind();
700     SortSectionPolicy inner = SortSectionPolicy::Default;
701     std::vector<SectionPattern> v;
702     if (outer != SortSectionPolicy::Default) {
703       expect("(");
704       inner = readSortKind();
705       if (inner != SortSectionPolicy::Default) {
706         expect("(");
707         v = readInputSectionsList();
708         expect(")");
709       } else {
710         v = readInputSectionsList();
711       }
712       expect(")");
713     } else {
714       v = readInputSectionsList();
715     }
716 
717     for (SectionPattern &pat : v) {
718       pat.sortInner = inner;
719       pat.sortOuter = outer;
720     }
721 
722     std::move(v.begin(), v.end(), std::back_inserter(cmd->sectionPatterns));
723   }
724   return cmd;
725 }
726 
727 InputSectionDescription *
728 ScriptParser::readInputSectionDescription(StringRef tok) {
729   // Input section wildcard can be surrounded by KEEP.
730   // https://sourceware.org/binutils/docs/ld/Input-Section-Keep.html#Input-Section-Keep
731   uint64_t withFlags = 0;
732   uint64_t withoutFlags = 0;
733   if (tok == "KEEP") {
734     expect("(");
735     if (consume("INPUT_SECTION_FLAGS"))
736       std::tie(withFlags, withoutFlags) = readInputSectionFlags();
737     InputSectionDescription *cmd =
738         readInputSectionRules(next(), withFlags, withoutFlags);
739     expect(")");
740     script->keptSections.push_back(cmd);
741     return cmd;
742   }
743   if (tok == "INPUT_SECTION_FLAGS") {
744     std::tie(withFlags, withoutFlags) = readInputSectionFlags();
745     tok = next();
746   }
747   return readInputSectionRules(tok, withFlags, withoutFlags);
748 }
749 
750 void ScriptParser::readSort() {
751   expect("(");
752   expect("CONSTRUCTORS");
753   expect(")");
754 }
755 
756 Expr ScriptParser::readAssert() {
757   expect("(");
758   Expr e = readExpr();
759   expect(",");
760   StringRef msg = unquote(next());
761   expect(")");
762 
763   return [=] {
764     if (!e().getValue())
765       errorOrWarn(msg);
766     return script->getDot();
767   };
768 }
769 
770 // Tries to read the special directive for an output section definition which
771 // can be one of following: "(NOLOAD)", "(COPY)", "(INFO)" or "(OVERLAY)".
772 // Tok1 and Tok2 are next 2 tokens peeked. See comment for readSectionAddressType below.
773 bool ScriptParser::readSectionDirective(OutputSection *cmd, StringRef tok1, StringRef tok2) {
774   if (tok1 != "(")
775     return false;
776   if (tok2 != "NOLOAD" && tok2 != "COPY" && tok2 != "INFO" && tok2 != "OVERLAY")
777     return false;
778 
779   expect("(");
780   if (consume("NOLOAD")) {
781     cmd->noload = true;
782     cmd->type = SHT_NOBITS;
783   } else {
784     skip(); // This is "COPY", "INFO" or "OVERLAY".
785     cmd->nonAlloc = true;
786   }
787   expect(")");
788   return true;
789 }
790 
791 // Reads an expression and/or the special directive for an output
792 // section definition. Directive is one of following: "(NOLOAD)",
793 // "(COPY)", "(INFO)" or "(OVERLAY)".
794 //
795 // An output section name can be followed by an address expression
796 // and/or directive. This grammar is not LL(1) because "(" can be
797 // interpreted as either the beginning of some expression or beginning
798 // of directive.
799 //
800 // https://sourceware.org/binutils/docs/ld/Output-Section-Address.html
801 // https://sourceware.org/binutils/docs/ld/Output-Section-Type.html
802 void ScriptParser::readSectionAddressType(OutputSection *cmd) {
803   if (readSectionDirective(cmd, peek(), peek2()))
804     return;
805 
806   cmd->addrExpr = readExpr();
807   if (peek() == "(" && !readSectionDirective(cmd, "(", peek2()))
808     setError("unknown section directive: " + peek2());
809 }
810 
811 static Expr checkAlignment(Expr e, std::string &loc) {
812   return [=] {
813     uint64_t alignment = std::max((uint64_t)1, e().getValue());
814     if (!isPowerOf2_64(alignment)) {
815       error(loc + ": alignment must be power of 2");
816       return (uint64_t)1; // Return a dummy value.
817     }
818     return alignment;
819   };
820 }
821 
822 OutputSection *ScriptParser::readOverlaySectionDescription() {
823   OutputSection *cmd =
824       script->createOutputSection(next(), getCurrentLocation());
825   cmd->inOverlay = true;
826   expect("{");
827   while (!errorCount() && !consume("}")) {
828     uint64_t withFlags = 0;
829     uint64_t withoutFlags = 0;
830     if (consume("INPUT_SECTION_FLAGS"))
831       std::tie(withFlags, withoutFlags) = readInputSectionFlags();
832     cmd->sectionCommands.push_back(
833         readInputSectionRules(next(), withFlags, withoutFlags));
834   }
835   return cmd;
836 }
837 
838 OutputSection *ScriptParser::readOutputSectionDescription(StringRef outSec) {
839   OutputSection *cmd =
840       script->createOutputSection(outSec, getCurrentLocation());
841 
842   size_t symbolsReferenced = script->referencedSymbols.size();
843 
844   if (peek() != ":")
845     readSectionAddressType(cmd);
846   expect(":");
847 
848   std::string location = getCurrentLocation();
849   if (consume("AT"))
850     cmd->lmaExpr = readParenExpr();
851   if (consume("ALIGN"))
852     cmd->alignExpr = checkAlignment(readParenExpr(), location);
853   if (consume("SUBALIGN"))
854     cmd->subalignExpr = checkAlignment(readParenExpr(), location);
855 
856   // Parse constraints.
857   if (consume("ONLY_IF_RO"))
858     cmd->constraint = ConstraintKind::ReadOnly;
859   if (consume("ONLY_IF_RW"))
860     cmd->constraint = ConstraintKind::ReadWrite;
861   expect("{");
862 
863   while (!errorCount() && !consume("}")) {
864     StringRef tok = next();
865     if (tok == ";") {
866       // Empty commands are allowed. Do nothing here.
867     } else if (SymbolAssignment *assign = readAssignment(tok)) {
868       cmd->sectionCommands.push_back(assign);
869     } else if (ByteCommand *data = readByteCommand(tok)) {
870       cmd->sectionCommands.push_back(data);
871     } else if (tok == "CONSTRUCTORS") {
872       // CONSTRUCTORS is a keyword to make the linker recognize C++ ctors/dtors
873       // by name. This is for very old file formats such as ECOFF/XCOFF.
874       // For ELF, we should ignore.
875     } else if (tok == "FILL") {
876       // We handle the FILL command as an alias for =fillexp section attribute,
877       // which is different from what GNU linkers do.
878       // https://sourceware.org/binutils/docs/ld/Output-Section-Data.html
879       if (peek() != "(")
880         setError("( expected, but got " + peek());
881       cmd->filler = readFill();
882     } else if (tok == "SORT") {
883       readSort();
884     } else if (tok == "INCLUDE") {
885       readInclude();
886     } else if (peek() == "(") {
887       cmd->sectionCommands.push_back(readInputSectionDescription(tok));
888     } else {
889       // We have a file name and no input sections description. It is not a
890       // commonly used syntax, but still acceptable. In that case, all sections
891       // from the file will be included.
892       // FIXME: GNU ld permits INPUT_SECTION_FLAGS to be used here. We do not
893       // handle this case here as it will already have been matched by the
894       // case above.
895       auto *isd = make<InputSectionDescription>(tok);
896       isd->sectionPatterns.push_back({{}, StringMatcher("*")});
897       cmd->sectionCommands.push_back(isd);
898     }
899   }
900 
901   if (consume(">"))
902     cmd->memoryRegionName = std::string(next());
903 
904   if (consume("AT")) {
905     expect(">");
906     cmd->lmaRegionName = std::string(next());
907   }
908 
909   if (cmd->lmaExpr && !cmd->lmaRegionName.empty())
910     error("section can't have both LMA and a load region");
911 
912   cmd->phdrs = readOutputSectionPhdrs();
913 
914   if (peek() == "=" || peek().startswith("=")) {
915     inExpr = true;
916     consume("=");
917     cmd->filler = readFill();
918     inExpr = false;
919   }
920 
921   // Consume optional comma following output section command.
922   consume(",");
923 
924   if (script->referencedSymbols.size() > symbolsReferenced)
925     cmd->expressionsUseSymbols = true;
926   return cmd;
927 }
928 
929 // Reads a `=<fillexp>` expression and returns its value as a big-endian number.
930 // https://sourceware.org/binutils/docs/ld/Output-Section-Fill.html
931 // We do not support using symbols in such expressions.
932 //
933 // When reading a hexstring, ld.bfd handles it as a blob of arbitrary
934 // size, while ld.gold always handles it as a 32-bit big-endian number.
935 // We are compatible with ld.gold because it's easier to implement.
936 // Also, we require that expressions with operators must be wrapped into
937 // round brackets. We did it to resolve the ambiguity when parsing scripts like:
938 // SECTIONS { .foo : { ... } =120+3 /DISCARD/ : { ... } }
939 std::array<uint8_t, 4> ScriptParser::readFill() {
940   uint64_t value = readPrimary()().val;
941   if (value > UINT32_MAX)
942     setError("filler expression result does not fit 32-bit: 0x" +
943              Twine::utohexstr(value));
944 
945   std::array<uint8_t, 4> buf;
946   write32be(buf.data(), (uint32_t)value);
947   return buf;
948 }
949 
950 SymbolAssignment *ScriptParser::readProvideHidden(bool provide, bool hidden) {
951   expect("(");
952   SymbolAssignment *cmd = readSymbolAssignment(next());
953   cmd->provide = provide;
954   cmd->hidden = hidden;
955   expect(")");
956   return cmd;
957 }
958 
959 SymbolAssignment *ScriptParser::readAssignment(StringRef tok) {
960   // Assert expression returns Dot, so this is equal to ".=."
961   if (tok == "ASSERT")
962     return make<SymbolAssignment>(".", readAssert(), getCurrentLocation());
963 
964   size_t oldPos = pos;
965   SymbolAssignment *cmd = nullptr;
966   if (peek() == "=" || peek() == "+=")
967     cmd = readSymbolAssignment(tok);
968   else if (tok == "PROVIDE")
969     cmd = readProvideHidden(true, false);
970   else if (tok == "HIDDEN")
971     cmd = readProvideHidden(false, true);
972   else if (tok == "PROVIDE_HIDDEN")
973     cmd = readProvideHidden(true, true);
974 
975   if (cmd) {
976     cmd->commandString =
977         tok.str() + " " +
978         llvm::join(tokens.begin() + oldPos, tokens.begin() + pos, " ");
979     expect(";");
980   }
981   return cmd;
982 }
983 
984 SymbolAssignment *ScriptParser::readSymbolAssignment(StringRef name) {
985   StringRef op = next();
986   assert(op == "=" || op == "+=");
987   Expr e = readExpr();
988   if (op == "+=") {
989     std::string loc = getCurrentLocation();
990     e = [=] { return add(script->getSymbolValue(name, loc), e()); };
991   }
992   return make<SymbolAssignment>(name, e, getCurrentLocation());
993 }
994 
995 // This is an operator-precedence parser to parse a linker
996 // script expression.
997 Expr ScriptParser::readExpr() {
998   // Our lexer is context-aware. Set the in-expression bit so that
999   // they apply different tokenization rules.
1000   bool orig = inExpr;
1001   inExpr = true;
1002   Expr e = readExpr1(readPrimary(), 0);
1003   inExpr = orig;
1004   return e;
1005 }
1006 
1007 Expr ScriptParser::combine(StringRef op, Expr l, Expr r) {
1008   if (op == "+")
1009     return [=] { return add(l(), r()); };
1010   if (op == "-")
1011     return [=] { return sub(l(), r()); };
1012   if (op == "*")
1013     return [=] { return l().getValue() * r().getValue(); };
1014   if (op == "/") {
1015     std::string loc = getCurrentLocation();
1016     return [=]() -> uint64_t {
1017       if (uint64_t rv = r().getValue())
1018         return l().getValue() / rv;
1019       error(loc + ": division by zero");
1020       return 0;
1021     };
1022   }
1023   if (op == "%") {
1024     std::string loc = getCurrentLocation();
1025     return [=]() -> uint64_t {
1026       if (uint64_t rv = r().getValue())
1027         return l().getValue() % rv;
1028       error(loc + ": modulo by zero");
1029       return 0;
1030     };
1031   }
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 l().getValue() || r().getValue(); };
1050   if (op == "&&")
1051     return [=] { return l().getValue() && r().getValue(); };
1052   if (op == "&")
1053     return [=] { return bitAnd(l(), r()); };
1054   if (op == "|")
1055     return [=] { return bitOr(l(), r()); };
1056   llvm_unreachable("invalid operator");
1057 }
1058 
1059 // This is a part of the operator-precedence parser. This function
1060 // assumes that the remaining token stream starts with an operator.
1061 Expr ScriptParser::readExpr1(Expr lhs, int minPrec) {
1062   while (!atEOF() && !errorCount()) {
1063     // Read an operator and an expression.
1064     if (consume("?"))
1065       return readTernary(lhs);
1066     StringRef op1 = peek();
1067     if (precedence(op1) < minPrec)
1068       break;
1069     skip();
1070     Expr rhs = readPrimary();
1071 
1072     // Evaluate the remaining part of the expression first if the
1073     // next operator has greater precedence than the previous one.
1074     // For example, if we have read "+" and "3", and if the next
1075     // operator is "*", then we'll evaluate 3 * ... part first.
1076     while (!atEOF()) {
1077       StringRef op2 = peek();
1078       if (precedence(op2) <= precedence(op1))
1079         break;
1080       rhs = readExpr1(rhs, precedence(op2));
1081     }
1082 
1083     lhs = combine(op1, lhs, rhs);
1084   }
1085   return lhs;
1086 }
1087 
1088 Expr ScriptParser::getPageSize() {
1089   std::string location = getCurrentLocation();
1090   return [=]() -> uint64_t {
1091     if (target)
1092       return config->commonPageSize;
1093     error(location + ": unable to calculate page size");
1094     return 4096; // Return a dummy value.
1095   };
1096 }
1097 
1098 Expr ScriptParser::readConstant() {
1099   StringRef s = readParenLiteral();
1100   if (s == "COMMONPAGESIZE")
1101     return getPageSize();
1102   if (s == "MAXPAGESIZE")
1103     return [] { return config->maxPageSize; };
1104   setError("unknown constant: " + s);
1105   return [] { return 0; };
1106 }
1107 
1108 // Parses Tok as an integer. It recognizes hexadecimal (prefixed with
1109 // "0x" or suffixed with "H") and decimal numbers. Decimal numbers may
1110 // have "K" (Ki) or "M" (Mi) suffixes.
1111 static Optional<uint64_t> parseInt(StringRef tok) {
1112   // Hexadecimal
1113   uint64_t val;
1114   if (tok.startswith_lower("0x")) {
1115     if (!to_integer(tok.substr(2), val, 16))
1116       return None;
1117     return val;
1118   }
1119   if (tok.endswith_lower("H")) {
1120     if (!to_integer(tok.drop_back(), val, 16))
1121       return None;
1122     return val;
1123   }
1124 
1125   // Decimal
1126   if (tok.endswith_lower("K")) {
1127     if (!to_integer(tok.drop_back(), val, 10))
1128       return None;
1129     return val * 1024;
1130   }
1131   if (tok.endswith_lower("M")) {
1132     if (!to_integer(tok.drop_back(), val, 10))
1133       return None;
1134     return val * 1024 * 1024;
1135   }
1136   if (!to_integer(tok, val, 10))
1137     return None;
1138   return val;
1139 }
1140 
1141 ByteCommand *ScriptParser::readByteCommand(StringRef tok) {
1142   int size = StringSwitch<int>(tok)
1143                  .Case("BYTE", 1)
1144                  .Case("SHORT", 2)
1145                  .Case("LONG", 4)
1146                  .Case("QUAD", 8)
1147                  .Default(-1);
1148   if (size == -1)
1149     return nullptr;
1150 
1151   size_t oldPos = pos;
1152   Expr e = readParenExpr();
1153   std::string commandString =
1154       tok.str() + " " +
1155       llvm::join(tokens.begin() + oldPos, tokens.begin() + pos, " ");
1156   return make<ByteCommand>(e, size, commandString);
1157 }
1158 
1159 static llvm::Optional<uint64_t> parseFlag(StringRef tok) {
1160   if (llvm::Optional<uint64_t> asInt = parseInt(tok))
1161     return asInt;
1162 #define CASE_ENT(enum) #enum, ELF::enum
1163   return StringSwitch<llvm::Optional<uint64_t>>(tok)
1164       .Case(CASE_ENT(SHF_WRITE))
1165       .Case(CASE_ENT(SHF_ALLOC))
1166       .Case(CASE_ENT(SHF_EXECINSTR))
1167       .Case(CASE_ENT(SHF_MERGE))
1168       .Case(CASE_ENT(SHF_STRINGS))
1169       .Case(CASE_ENT(SHF_INFO_LINK))
1170       .Case(CASE_ENT(SHF_LINK_ORDER))
1171       .Case(CASE_ENT(SHF_OS_NONCONFORMING))
1172       .Case(CASE_ENT(SHF_GROUP))
1173       .Case(CASE_ENT(SHF_TLS))
1174       .Case(CASE_ENT(SHF_COMPRESSED))
1175       .Case(CASE_ENT(SHF_EXCLUDE))
1176       .Case(CASE_ENT(SHF_ARM_PURECODE))
1177       .Default(None);
1178 #undef CASE_ENT
1179 }
1180 
1181 // Reads the '(' <flags> ')' list of section flags in
1182 // INPUT_SECTION_FLAGS '(' <flags> ')' in the
1183 // following form:
1184 // <flags> ::= <flag>
1185 //           | <flags> & flag
1186 // <flag>  ::= Recognized Flag Name, or Integer value of flag.
1187 // If the first character of <flag> is a ! then this means without flag,
1188 // otherwise with flag.
1189 // Example: SHF_EXECINSTR & !SHF_WRITE means with flag SHF_EXECINSTR and
1190 // without flag SHF_WRITE.
1191 std::pair<uint64_t, uint64_t> ScriptParser::readInputSectionFlags() {
1192    uint64_t withFlags = 0;
1193    uint64_t withoutFlags = 0;
1194    expect("(");
1195    while (!errorCount()) {
1196     StringRef tok = unquote(next());
1197     bool without = tok.consume_front("!");
1198     if (llvm::Optional<uint64_t> flag = parseFlag(tok)) {
1199       if (without)
1200         withoutFlags |= *flag;
1201       else
1202         withFlags |= *flag;
1203     } else {
1204       setError("unrecognised flag: " + tok);
1205     }
1206     if (consume(")"))
1207       break;
1208     if (!consume("&")) {
1209       next();
1210       setError("expected & or )");
1211     }
1212   }
1213   return std::make_pair(withFlags, withoutFlags);
1214 }
1215 
1216 StringRef ScriptParser::readParenLiteral() {
1217   expect("(");
1218   bool orig = inExpr;
1219   inExpr = false;
1220   StringRef tok = next();
1221   inExpr = orig;
1222   expect(")");
1223   return tok;
1224 }
1225 
1226 static void checkIfExists(OutputSection *cmd, StringRef location) {
1227   if (cmd->location.empty() && script->errorOnMissingSection)
1228     error(location + ": undefined section " + cmd->name);
1229 }
1230 
1231 Expr ScriptParser::readPrimary() {
1232   if (peek() == "(")
1233     return readParenExpr();
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   if (consume("-")) {
1244     Expr e = readPrimary();
1245     return [=] { return -e().getValue(); };
1246   }
1247 
1248   StringRef tok = next();
1249   std::string location = getCurrentLocation();
1250 
1251   // Built-in functions are parsed here.
1252   // https://sourceware.org/binutils/docs/ld/Builtin-Functions.html.
1253   if (tok == "ABSOLUTE") {
1254     Expr inner = readParenExpr();
1255     return [=] {
1256       ExprValue i = inner();
1257       i.forceAbsolute = true;
1258       return i;
1259     };
1260   }
1261   if (tok == "ADDR") {
1262     StringRef name = readParenLiteral();
1263     OutputSection *sec = script->getOrCreateOutputSection(name);
1264     sec->usedInExpression = true;
1265     return [=]() -> ExprValue {
1266       checkIfExists(sec, location);
1267       return {sec, false, 0, location};
1268     };
1269   }
1270   if (tok == "ALIGN") {
1271     expect("(");
1272     Expr e = readExpr();
1273     if (consume(")")) {
1274       e = checkAlignment(e, location);
1275       return [=] { return alignTo(script->getDot(), e().getValue()); };
1276     }
1277     expect(",");
1278     Expr e2 = checkAlignment(readExpr(), location);
1279     expect(")");
1280     return [=] {
1281       ExprValue v = e();
1282       v.alignment = e2().getValue();
1283       return v;
1284     };
1285   }
1286   if (tok == "ALIGNOF") {
1287     StringRef name = readParenLiteral();
1288     OutputSection *cmd = script->getOrCreateOutputSection(name);
1289     return [=] {
1290       checkIfExists(cmd, location);
1291       return cmd->alignment;
1292     };
1293   }
1294   if (tok == "ASSERT")
1295     return readAssert();
1296   if (tok == "CONSTANT")
1297     return readConstant();
1298   if (tok == "DATA_SEGMENT_ALIGN") {
1299     expect("(");
1300     Expr e = readExpr();
1301     expect(",");
1302     readExpr();
1303     expect(")");
1304     return [=] {
1305       return alignTo(script->getDot(), std::max((uint64_t)1, e().getValue()));
1306     };
1307   }
1308   if (tok == "DATA_SEGMENT_END") {
1309     expect("(");
1310     expect(".");
1311     expect(")");
1312     return [] { return script->getDot(); };
1313   }
1314   if (tok == "DATA_SEGMENT_RELRO_END") {
1315     // GNU linkers implements more complicated logic to handle
1316     // DATA_SEGMENT_RELRO_END. We instead ignore the arguments and
1317     // just align to the next page boundary for simplicity.
1318     expect("(");
1319     readExpr();
1320     expect(",");
1321     readExpr();
1322     expect(")");
1323     Expr e = getPageSize();
1324     return [=] { return alignTo(script->getDot(), e().getValue()); };
1325   }
1326   if (tok == "DEFINED") {
1327     StringRef name = readParenLiteral();
1328     return [=] {
1329       Symbol *b = symtab->find(name);
1330       return (b && b->isDefined()) ? 1 : 0;
1331     };
1332   }
1333   if (tok == "LENGTH") {
1334     StringRef name = readParenLiteral();
1335     if (script->memoryRegions.count(name) == 0) {
1336       setError("memory region not defined: " + name);
1337       return [] { return 0; };
1338     }
1339     return script->memoryRegions[name]->length;
1340   }
1341   if (tok == "LOADADDR") {
1342     StringRef name = readParenLiteral();
1343     OutputSection *cmd = script->getOrCreateOutputSection(name);
1344     cmd->usedInExpression = true;
1345     return [=] {
1346       checkIfExists(cmd, location);
1347       return cmd->getLMA();
1348     };
1349   }
1350   if (tok == "LOG2CEIL") {
1351     expect("(");
1352     Expr a = readExpr();
1353     expect(")");
1354     return [=] {
1355       // LOG2CEIL(0) is defined to be 0.
1356       return llvm::Log2_64_Ceil(std::max(a().getValue(), UINT64_C(1)));
1357     };
1358   }
1359   if (tok == "MAX" || tok == "MIN") {
1360     expect("(");
1361     Expr a = readExpr();
1362     expect(",");
1363     Expr b = readExpr();
1364     expect(")");
1365     if (tok == "MIN")
1366       return [=] { return std::min(a().getValue(), b().getValue()); };
1367     return [=] { return std::max(a().getValue(), b().getValue()); };
1368   }
1369   if (tok == "ORIGIN") {
1370     StringRef name = readParenLiteral();
1371     if (script->memoryRegions.count(name) == 0) {
1372       setError("memory region not defined: " + name);
1373       return [] { return 0; };
1374     }
1375     return script->memoryRegions[name]->origin;
1376   }
1377   if (tok == "SEGMENT_START") {
1378     expect("(");
1379     skip();
1380     expect(",");
1381     Expr e = readExpr();
1382     expect(")");
1383     return [=] { return e(); };
1384   }
1385   if (tok == "SIZEOF") {
1386     StringRef name = readParenLiteral();
1387     OutputSection *cmd = script->getOrCreateOutputSection(name);
1388     // Linker script does not create an output section if its content is empty.
1389     // We want to allow SIZEOF(.foo) where .foo is a section which happened to
1390     // be empty.
1391     return [=] { return cmd->size; };
1392   }
1393   if (tok == "SIZEOF_HEADERS")
1394     return [=] { return elf::getHeaderSize(); };
1395 
1396   // Tok is the dot.
1397   if (tok == ".")
1398     return [=] { return script->getSymbolValue(tok, location); };
1399 
1400   // Tok is a literal number.
1401   if (Optional<uint64_t> val = parseInt(tok))
1402     return [=] { return *val; };
1403 
1404   // Tok is a symbol name.
1405   if (!isValidCIdentifier(tok))
1406     setError("malformed number: " + tok);
1407   script->referencedSymbols.push_back(tok);
1408   return [=] { return script->getSymbolValue(tok, location); };
1409 }
1410 
1411 Expr ScriptParser::readTernary(Expr cond) {
1412   Expr l = readExpr();
1413   expect(":");
1414   Expr r = readExpr();
1415   return [=] { return cond().getValue() ? l() : r(); };
1416 }
1417 
1418 Expr ScriptParser::readParenExpr() {
1419   expect("(");
1420   Expr e = readExpr();
1421   expect(")");
1422   return e;
1423 }
1424 
1425 std::vector<StringRef> ScriptParser::readOutputSectionPhdrs() {
1426   std::vector<StringRef> phdrs;
1427   while (!errorCount() && peek().startswith(":")) {
1428     StringRef tok = next();
1429     phdrs.push_back((tok.size() == 1) ? next() : tok.substr(1));
1430   }
1431   return phdrs;
1432 }
1433 
1434 // Read a program header type name. The next token must be a
1435 // name of a program header type or a constant (e.g. "0x3").
1436 unsigned ScriptParser::readPhdrType() {
1437   StringRef tok = next();
1438   if (Optional<uint64_t> val = parseInt(tok))
1439     return *val;
1440 
1441   unsigned ret = StringSwitch<unsigned>(tok)
1442                      .Case("PT_NULL", PT_NULL)
1443                      .Case("PT_LOAD", PT_LOAD)
1444                      .Case("PT_DYNAMIC", PT_DYNAMIC)
1445                      .Case("PT_INTERP", PT_INTERP)
1446                      .Case("PT_NOTE", PT_NOTE)
1447                      .Case("PT_SHLIB", PT_SHLIB)
1448                      .Case("PT_PHDR", PT_PHDR)
1449                      .Case("PT_TLS", PT_TLS)
1450                      .Case("PT_GNU_EH_FRAME", PT_GNU_EH_FRAME)
1451                      .Case("PT_GNU_STACK", PT_GNU_STACK)
1452                      .Case("PT_GNU_RELRO", PT_GNU_RELRO)
1453                      .Case("PT_OPENBSD_RANDOMIZE", PT_OPENBSD_RANDOMIZE)
1454                      .Case("PT_OPENBSD_WXNEEDED", PT_OPENBSD_WXNEEDED)
1455                      .Case("PT_OPENBSD_BOOTDATA", PT_OPENBSD_BOOTDATA)
1456                      .Default(-1);
1457 
1458   if (ret == (unsigned)-1) {
1459     setError("invalid program header type: " + tok);
1460     return PT_NULL;
1461   }
1462   return ret;
1463 }
1464 
1465 // Reads an anonymous version declaration.
1466 void ScriptParser::readAnonymousDeclaration() {
1467   std::vector<SymbolVersion> locals;
1468   std::vector<SymbolVersion> globals;
1469   std::tie(locals, globals) = readSymbols();
1470   for (const SymbolVersion &pat : locals)
1471     config->versionDefinitions[VER_NDX_LOCAL].patterns.push_back(pat);
1472   for (const SymbolVersion &pat : globals)
1473     config->versionDefinitions[VER_NDX_GLOBAL].patterns.push_back(pat);
1474 
1475   expect(";");
1476 }
1477 
1478 // Reads a non-anonymous version definition,
1479 // e.g. "VerStr { global: foo; bar; local: *; };".
1480 void ScriptParser::readVersionDeclaration(StringRef verStr) {
1481   // Read a symbol list.
1482   std::vector<SymbolVersion> locals;
1483   std::vector<SymbolVersion> globals;
1484   std::tie(locals, globals) = readSymbols();
1485   for (const SymbolVersion &pat : locals)
1486     config->versionDefinitions[VER_NDX_LOCAL].patterns.push_back(pat);
1487 
1488   // Create a new version definition and add that to the global symbols.
1489   VersionDefinition ver;
1490   ver.name = verStr;
1491   ver.patterns = globals;
1492   ver.id = config->versionDefinitions.size();
1493   config->versionDefinitions.push_back(ver);
1494 
1495   // Each version may have a parent version. For example, "Ver2"
1496   // defined as "Ver2 { global: foo; local: *; } Ver1;" has "Ver1"
1497   // as a parent. This version hierarchy is, probably against your
1498   // instinct, purely for hint; the runtime doesn't care about it
1499   // at all. In LLD, we simply ignore it.
1500   if (next() != ";")
1501     expect(";");
1502 }
1503 
1504 bool elf::hasWildcard(StringRef s) {
1505   return s.find_first_of("?*[") != StringRef::npos;
1506 }
1507 
1508 // Reads a list of symbols, e.g. "{ global: foo; bar; local: *; };".
1509 std::pair<std::vector<SymbolVersion>, std::vector<SymbolVersion>>
1510 ScriptParser::readSymbols() {
1511   std::vector<SymbolVersion> locals;
1512   std::vector<SymbolVersion> globals;
1513   std::vector<SymbolVersion> *v = &globals;
1514 
1515   while (!errorCount()) {
1516     if (consume("}"))
1517       break;
1518     if (consumeLabel("local")) {
1519       v = &locals;
1520       continue;
1521     }
1522     if (consumeLabel("global")) {
1523       v = &globals;
1524       continue;
1525     }
1526 
1527     if (consume("extern")) {
1528       std::vector<SymbolVersion> ext = readVersionExtern();
1529       v->insert(v->end(), ext.begin(), ext.end());
1530     } else {
1531       StringRef tok = next();
1532       v->push_back({unquote(tok), false, hasWildcard(tok)});
1533     }
1534     expect(";");
1535   }
1536   return {locals, globals};
1537 }
1538 
1539 // Reads an "extern C++" directive, e.g.,
1540 // "extern "C++" { ns::*; "f(int, double)"; };"
1541 //
1542 // The last semicolon is optional. E.g. this is OK:
1543 // "extern "C++" { ns::*; "f(int, double)" };"
1544 std::vector<SymbolVersion> ScriptParser::readVersionExtern() {
1545   StringRef tok = next();
1546   bool isCXX = tok == "\"C++\"";
1547   if (!isCXX && tok != "\"C\"")
1548     setError("Unknown language");
1549   expect("{");
1550 
1551   std::vector<SymbolVersion> ret;
1552   while (!errorCount() && peek() != "}") {
1553     StringRef tok = next();
1554     ret.push_back(
1555         {unquote(tok), isCXX, !tok.startswith("\"") && hasWildcard(tok)});
1556     if (consume("}"))
1557       return ret;
1558     expect(";");
1559   }
1560 
1561   expect("}");
1562   return ret;
1563 }
1564 
1565 Expr ScriptParser::readMemoryAssignment(StringRef s1, StringRef s2,
1566                                         StringRef s3) {
1567   if (!consume(s1) && !consume(s2) && !consume(s3)) {
1568     setError("expected one of: " + s1 + ", " + s2 + ", or " + s3);
1569     return [] { return 0; };
1570   }
1571   expect("=");
1572   return readExpr();
1573 }
1574 
1575 // Parse the MEMORY command as specified in:
1576 // https://sourceware.org/binutils/docs/ld/MEMORY.html
1577 //
1578 // MEMORY { name [(attr)] : ORIGIN = origin, LENGTH = len ... }
1579 void ScriptParser::readMemory() {
1580   expect("{");
1581   while (!errorCount() && !consume("}")) {
1582     StringRef tok = next();
1583     if (tok == "INCLUDE") {
1584       readInclude();
1585       continue;
1586     }
1587 
1588     uint32_t flags = 0;
1589     uint32_t negFlags = 0;
1590     if (consume("(")) {
1591       std::tie(flags, negFlags) = readMemoryAttributes();
1592       expect(")");
1593     }
1594     expect(":");
1595 
1596     Expr origin = readMemoryAssignment("ORIGIN", "org", "o");
1597     expect(",");
1598     Expr length = readMemoryAssignment("LENGTH", "len", "l");
1599 
1600     // Add the memory region to the region map.
1601     MemoryRegion *mr = make<MemoryRegion>(tok, origin, length, flags, negFlags);
1602     if (!script->memoryRegions.insert({tok, mr}).second)
1603       setError("region '" + tok + "' already defined");
1604   }
1605 }
1606 
1607 // This function parses the attributes used to match against section
1608 // flags when placing output sections in a memory region. These flags
1609 // are only used when an explicit memory region name is not used.
1610 std::pair<uint32_t, uint32_t> ScriptParser::readMemoryAttributes() {
1611   uint32_t flags = 0;
1612   uint32_t negFlags = 0;
1613   bool invert = false;
1614 
1615   for (char c : next().lower()) {
1616     uint32_t flag = 0;
1617     if (c == '!')
1618       invert = !invert;
1619     else if (c == 'w')
1620       flag = SHF_WRITE;
1621     else if (c == 'x')
1622       flag = SHF_EXECINSTR;
1623     else if (c == 'a')
1624       flag = SHF_ALLOC;
1625     else if (c != 'r')
1626       setError("invalid memory region attribute");
1627 
1628     if (invert)
1629       negFlags |= flag;
1630     else
1631       flags |= flag;
1632   }
1633   return {flags, negFlags};
1634 }
1635 
1636 void elf::readLinkerScript(MemoryBufferRef mb) {
1637   llvm::TimeTraceScope timeScope("Read linker script",
1638                                  mb.getBufferIdentifier());
1639   ScriptParser(mb).readLinkerScript();
1640 }
1641 
1642 void elf::readVersionScript(MemoryBufferRef mb) {
1643   llvm::TimeTraceScope timeScope("Read version script",
1644                                  mb.getBufferIdentifier());
1645   ScriptParser(mb).readVersionScript();
1646 }
1647 
1648 void elf::readDynamicList(MemoryBufferRef mb) {
1649   llvm::TimeTraceScope timeScope("Read dynamic list", mb.getBufferIdentifier());
1650   ScriptParser(mb).readDynamicList();
1651 }
1652 
1653 void elf::readDefsym(StringRef name, MemoryBufferRef mb) {
1654   llvm::TimeTraceScope timeScope("Read defsym input", name);
1655   ScriptParser(mb).readDefsym(name);
1656 }
1657