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