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