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     } else if (Tok == "INCLUDE") {
505       readInclude();
506       continue;
507     }
508 
509     if (BaseCommand *Cmd = readAssignment(Tok))
510       V.push_back(Cmd);
511     else
512       V.push_back(readOutputSectionDescription(Tok));
513   }
514 
515   if (!atEOF() && consume("INSERT")) {
516     std::vector<BaseCommand *> *Dest = nullptr;
517     if (consume("AFTER"))
518       Dest = &Script->InsertAfterCommands[next()];
519     else if (consume("BEFORE"))
520       Dest = &Script->InsertBeforeCommands[next()];
521     else
522       setError("expected AFTER/BEFORE, but got '" + next() + "'");
523     if (Dest)
524       Dest->insert(Dest->end(), V.begin(), V.end());
525     return;
526   }
527 
528   Script->SectionCommands.insert(Script->SectionCommands.end(), V.begin(),
529                                  V.end());
530 }
531 
532 void ScriptParser::readTarget() {
533   // TARGET(foo) is an alias for "--format foo". Unlike GNU linkers,
534   // we accept only a limited set of BFD names (i.e. "elf" or "binary")
535   // for --format. We recognize only /^elf/ and "binary" in the linker
536   // script as well.
537   expect("(");
538   StringRef Tok = next();
539   expect(")");
540 
541   if (Tok.startswith("elf"))
542     Config->FormatBinary = false;
543   else if (Tok == "binary")
544     Config->FormatBinary = true;
545   else
546     setError("unknown target: " + Tok);
547 }
548 
549 static int precedence(StringRef Op) {
550   return StringSwitch<int>(Op)
551       .Cases("*", "/", "%", 8)
552       .Cases("+", "-", 7)
553       .Cases("<<", ">>", 6)
554       .Cases("<", "<=", ">", ">=", "==", "!=", 5)
555       .Case("&", 4)
556       .Case("|", 3)
557       .Case("&&", 2)
558       .Case("||", 1)
559       .Default(-1);
560 }
561 
562 StringMatcher ScriptParser::readFilePatterns() {
563   std::vector<StringRef> V;
564   while (!errorCount() && !consume(")"))
565     V.push_back(next());
566   return StringMatcher(V);
567 }
568 
569 SortSectionPolicy ScriptParser::readSortKind() {
570   if (consume("SORT") || consume("SORT_BY_NAME"))
571     return SortSectionPolicy::Name;
572   if (consume("SORT_BY_ALIGNMENT"))
573     return SortSectionPolicy::Alignment;
574   if (consume("SORT_BY_INIT_PRIORITY"))
575     return SortSectionPolicy::Priority;
576   if (consume("SORT_NONE"))
577     return SortSectionPolicy::None;
578   return SortSectionPolicy::Default;
579 }
580 
581 // Reads SECTIONS command contents in the following form:
582 //
583 // <contents> ::= <elem>*
584 // <elem>     ::= <exclude>? <glob-pattern>
585 // <exclude>  ::= "EXCLUDE_FILE" "(" <glob-pattern>+ ")"
586 //
587 // For example,
588 //
589 // *(.foo EXCLUDE_FILE (a.o) .bar EXCLUDE_FILE (b.o) .baz)
590 //
591 // is parsed as ".foo", ".bar" with "a.o", and ".baz" with "b.o".
592 // The semantics of that is section .foo in any file, section .bar in
593 // any file but a.o, and section .baz in any file but b.o.
594 std::vector<SectionPattern> ScriptParser::readInputSectionsList() {
595   std::vector<SectionPattern> Ret;
596   while (!errorCount() && peek() != ")") {
597     StringMatcher ExcludeFilePat;
598     if (consume("EXCLUDE_FILE")) {
599       expect("(");
600       ExcludeFilePat = readFilePatterns();
601     }
602 
603     std::vector<StringRef> V;
604     while (!errorCount() && peek() != ")" && peek() != "EXCLUDE_FILE")
605       V.push_back(next());
606 
607     if (!V.empty())
608       Ret.push_back({std::move(ExcludeFilePat), StringMatcher(V)});
609     else
610       setError("section pattern is expected");
611   }
612   return Ret;
613 }
614 
615 // Reads contents of "SECTIONS" directive. That directive contains a
616 // list of glob patterns for input sections. The grammar is as follows.
617 //
618 // <patterns> ::= <section-list>
619 //              | <sort> "(" <section-list> ")"
620 //              | <sort> "(" <sort> "(" <section-list> ")" ")"
621 //
622 // <sort>     ::= "SORT" | "SORT_BY_NAME" | "SORT_BY_ALIGNMENT"
623 //              | "SORT_BY_INIT_PRIORITY" | "SORT_NONE"
624 //
625 // <section-list> is parsed by readInputSectionsList().
626 InputSectionDescription *
627 ScriptParser::readInputSectionRules(StringRef FilePattern) {
628   auto *Cmd = make<InputSectionDescription>(FilePattern);
629   expect("(");
630 
631   while (!errorCount() && !consume(")")) {
632     SortSectionPolicy Outer = readSortKind();
633     SortSectionPolicy Inner = SortSectionPolicy::Default;
634     std::vector<SectionPattern> V;
635     if (Outer != SortSectionPolicy::Default) {
636       expect("(");
637       Inner = readSortKind();
638       if (Inner != SortSectionPolicy::Default) {
639         expect("(");
640         V = readInputSectionsList();
641         expect(")");
642       } else {
643         V = readInputSectionsList();
644       }
645       expect(")");
646     } else {
647       V = readInputSectionsList();
648     }
649 
650     for (SectionPattern &Pat : V) {
651       Pat.SortInner = Inner;
652       Pat.SortOuter = Outer;
653     }
654 
655     std::move(V.begin(), V.end(), std::back_inserter(Cmd->SectionPatterns));
656   }
657   return Cmd;
658 }
659 
660 InputSectionDescription *
661 ScriptParser::readInputSectionDescription(StringRef Tok) {
662   // Input section wildcard can be surrounded by KEEP.
663   // https://sourceware.org/binutils/docs/ld/Input-Section-Keep.html#Input-Section-Keep
664   if (Tok == "KEEP") {
665     expect("(");
666     StringRef FilePattern = next();
667     InputSectionDescription *Cmd = readInputSectionRules(FilePattern);
668     expect(")");
669     Script->KeptSections.push_back(Cmd);
670     return Cmd;
671   }
672   return readInputSectionRules(Tok);
673 }
674 
675 void ScriptParser::readSort() {
676   expect("(");
677   expect("CONSTRUCTORS");
678   expect(")");
679 }
680 
681 Expr ScriptParser::readAssert() {
682   expect("(");
683   Expr E = readExpr();
684   expect(",");
685   StringRef Msg = unquote(next());
686   expect(")");
687 
688   return [=] {
689     if (!E().getValue())
690       error(Msg);
691     return Script->getDot();
692   };
693 }
694 
695 // Reads a FILL(expr) command. We handle the FILL command as an
696 // alias for =fillexp section attribute, which is different from
697 // what GNU linkers do.
698 // https://sourceware.org/binutils/docs/ld/Output-Section-Data.html
699 uint32_t ScriptParser::readFill() {
700   expect("(");
701   uint32_t V = parseFill(next());
702   expect(")");
703   return V;
704 }
705 
706 // Tries to read the special directive for an output section definition which
707 // can be one of following: "(NOLOAD)", "(COPY)", "(INFO)" or "(OVERLAY)".
708 // Tok1 and Tok2 are next 2 tokens peeked. See comment for readSectionAddressType below.
709 bool ScriptParser::readSectionDirective(OutputSection *Cmd, StringRef Tok1, StringRef Tok2) {
710   if (Tok1 != "(")
711     return false;
712   if (Tok2 != "NOLOAD" && Tok2 != "COPY" && Tok2 != "INFO" && Tok2 != "OVERLAY")
713     return false;
714 
715   expect("(");
716   if (consume("NOLOAD")) {
717     Cmd->Noload = true;
718   } else {
719     skip(); // This is "COPY", "INFO" or "OVERLAY".
720     Cmd->NonAlloc = true;
721   }
722   expect(")");
723   return true;
724 }
725 
726 // Reads an expression and/or the special directive for an output
727 // section definition. Directive is one of following: "(NOLOAD)",
728 // "(COPY)", "(INFO)" or "(OVERLAY)".
729 //
730 // An output section name can be followed by an address expression
731 // and/or directive. This grammar is not LL(1) because "(" can be
732 // interpreted as either the beginning of some expression or beginning
733 // of directive.
734 //
735 // https://sourceware.org/binutils/docs/ld/Output-Section-Address.html
736 // https://sourceware.org/binutils/docs/ld/Output-Section-Type.html
737 void ScriptParser::readSectionAddressType(OutputSection *Cmd) {
738   if (readSectionDirective(Cmd, peek(), peek2()))
739     return;
740 
741   Cmd->AddrExpr = readExpr();
742   if (peek() == "(" && !readSectionDirective(Cmd, "(", peek2()))
743     setError("unknown section directive: " + peek2());
744 }
745 
746 static Expr checkAlignment(Expr E, std::string &Loc) {
747   return [=] {
748     uint64_t Alignment = std::max((uint64_t)1, E().getValue());
749     if (!isPowerOf2_64(Alignment)) {
750       error(Loc + ": alignment must be power of 2");
751       return (uint64_t)1; // Return a dummy value.
752     }
753     return Alignment;
754   };
755 }
756 
757 OutputSection *ScriptParser::readOverlaySectionDescription() {
758   OutputSection *Cmd =
759       Script->createOutputSection(next(), getCurrentLocation());
760   Cmd->InOverlay = true;
761   expect("{");
762   while (!errorCount() && !consume("}"))
763     Cmd->SectionCommands.push_back(readInputSectionRules(next()));
764   Cmd->Phdrs = readOutputSectionPhdrs();
765   return Cmd;
766 }
767 
768 OutputSection *ScriptParser::readOutputSectionDescription(StringRef OutSec) {
769   OutputSection *Cmd =
770       Script->createOutputSection(OutSec, getCurrentLocation());
771 
772   size_t SymbolsReferenced = Script->ReferencedSymbols.size();
773 
774   if (peek() != ":")
775     readSectionAddressType(Cmd);
776   expect(":");
777 
778   std::string Location = getCurrentLocation();
779   if (consume("AT"))
780     Cmd->LMAExpr = readParenExpr();
781   if (consume("ALIGN"))
782     Cmd->AlignExpr = checkAlignment(readParenExpr(), Location);
783   if (consume("SUBALIGN"))
784     Cmd->SubalignExpr = checkAlignment(readParenExpr(), Location);
785 
786   // Parse constraints.
787   if (consume("ONLY_IF_RO"))
788     Cmd->Constraint = ConstraintKind::ReadOnly;
789   if (consume("ONLY_IF_RW"))
790     Cmd->Constraint = ConstraintKind::ReadWrite;
791   expect("{");
792 
793   while (!errorCount() && !consume("}")) {
794     StringRef Tok = next();
795     if (Tok == ";") {
796       // Empty commands are allowed. Do nothing here.
797     } else if (SymbolAssignment *Assign = readAssignment(Tok)) {
798       Cmd->SectionCommands.push_back(Assign);
799     } else if (ByteCommand *Data = readByteCommand(Tok)) {
800       Cmd->SectionCommands.push_back(Data);
801     } else if (Tok == "CONSTRUCTORS") {
802       // CONSTRUCTORS is a keyword to make the linker recognize C++ ctors/dtors
803       // by name. This is for very old file formats such as ECOFF/XCOFF.
804       // For ELF, we should ignore.
805     } else if (Tok == "FILL") {
806       Cmd->Filler = readFill();
807     } else if (Tok == "SORT") {
808       readSort();
809     } else if (Tok == "INCLUDE") {
810       readInclude();
811     } else if (peek() == "(") {
812       Cmd->SectionCommands.push_back(readInputSectionDescription(Tok));
813     } else {
814       setError("unknown command " + Tok);
815     }
816   }
817 
818   if (consume(">"))
819     Cmd->MemoryRegionName = next();
820 
821   if (consume("AT")) {
822     expect(">");
823     Cmd->LMARegionName = next();
824   }
825 
826   if (Cmd->LMAExpr && !Cmd->LMARegionName.empty())
827     error("section can't have both LMA and a load region");
828 
829   Cmd->Phdrs = readOutputSectionPhdrs();
830 
831   if (consume("="))
832     Cmd->Filler = parseFill(next());
833   else if (peek().startswith("="))
834     Cmd->Filler = parseFill(next().drop_front());
835 
836   // Consume optional comma following output section command.
837   consume(",");
838 
839   if (Script->ReferencedSymbols.size() > SymbolsReferenced)
840     Cmd->ExpressionsUseSymbols = true;
841   return Cmd;
842 }
843 
844 // Parses a given string as a octal/decimal/hexadecimal number and
845 // returns it as a big-endian number. Used for `=<fillexp>`.
846 // https://sourceware.org/binutils/docs/ld/Output-Section-Fill.html
847 //
848 // When reading a hexstring, ld.bfd handles it as a blob of arbitrary
849 // size, while ld.gold always handles it as a 32-bit big-endian number.
850 // We are compatible with ld.gold because it's easier to implement.
851 uint32_t ScriptParser::parseFill(StringRef Tok) {
852   uint32_t V = 0;
853   if (!to_integer(Tok, V))
854     setError("invalid filler expression: " + Tok);
855 
856   uint32_t Buf;
857   write32be(&Buf, V);
858   return Buf;
859 }
860 
861 SymbolAssignment *ScriptParser::readProvideHidden(bool Provide, bool Hidden) {
862   expect("(");
863   SymbolAssignment *Cmd = readSymbolAssignment(next());
864   Cmd->Provide = Provide;
865   Cmd->Hidden = Hidden;
866   expect(")");
867   return Cmd;
868 }
869 
870 SymbolAssignment *ScriptParser::readAssignment(StringRef Tok) {
871   // Assert expression returns Dot, so this is equal to ".=."
872   if (Tok == "ASSERT")
873     return make<SymbolAssignment>(".", readAssert(), getCurrentLocation());
874 
875   size_t OldPos = Pos;
876   SymbolAssignment *Cmd = nullptr;
877   if (peek() == "=" || peek() == "+=")
878     Cmd = readSymbolAssignment(Tok);
879   else if (Tok == "PROVIDE")
880     Cmd = readProvideHidden(true, false);
881   else if (Tok == "HIDDEN")
882     Cmd = readProvideHidden(false, true);
883   else if (Tok == "PROVIDE_HIDDEN")
884     Cmd = readProvideHidden(true, true);
885 
886   if (Cmd) {
887     Cmd->CommandString =
888         Tok.str() + " " +
889         llvm::join(Tokens.begin() + OldPos, Tokens.begin() + Pos, " ");
890     expect(";");
891   }
892   return Cmd;
893 }
894 
895 SymbolAssignment *ScriptParser::readSymbolAssignment(StringRef Name) {
896   StringRef Op = next();
897   assert(Op == "=" || Op == "+=");
898   Expr E = readExpr();
899   if (Op == "+=") {
900     std::string Loc = getCurrentLocation();
901     E = [=] { return add(Script->getSymbolValue(Name, Loc), E()); };
902   }
903   return make<SymbolAssignment>(Name, E, getCurrentLocation());
904 }
905 
906 // This is an operator-precedence parser to parse a linker
907 // script expression.
908 Expr ScriptParser::readExpr() {
909   // Our lexer is context-aware. Set the in-expression bit so that
910   // they apply different tokenization rules.
911   bool Orig = InExpr;
912   InExpr = true;
913   Expr E = readExpr1(readPrimary(), 0);
914   InExpr = Orig;
915   return E;
916 }
917 
918 Expr ScriptParser::combine(StringRef Op, Expr L, Expr R) {
919   if (Op == "+")
920     return [=] { return add(L(), R()); };
921   if (Op == "-")
922     return [=] { return sub(L(), R()); };
923   if (Op == "*")
924     return [=] { return L().getValue() * R().getValue(); };
925   if (Op == "/") {
926     std::string Loc = getCurrentLocation();
927     return [=]() -> uint64_t {
928       if (uint64_t RV = R().getValue())
929         return L().getValue() / RV;
930       error(Loc + ": division by zero");
931       return 0;
932     };
933   }
934   if (Op == "%") {
935     std::string Loc = getCurrentLocation();
936     return [=]() -> uint64_t {
937       if (uint64_t RV = R().getValue())
938         return L().getValue() % RV;
939       error(Loc + ": modulo by zero");
940       return 0;
941     };
942   }
943   if (Op == "<<")
944     return [=] { return L().getValue() << R().getValue(); };
945   if (Op == ">>")
946     return [=] { return L().getValue() >> R().getValue(); };
947   if (Op == "<")
948     return [=] { return L().getValue() < R().getValue(); };
949   if (Op == ">")
950     return [=] { return L().getValue() > R().getValue(); };
951   if (Op == ">=")
952     return [=] { return L().getValue() >= R().getValue(); };
953   if (Op == "<=")
954     return [=] { return L().getValue() <= R().getValue(); };
955   if (Op == "==")
956     return [=] { return L().getValue() == R().getValue(); };
957   if (Op == "!=")
958     return [=] { return L().getValue() != R().getValue(); };
959   if (Op == "||")
960     return [=] { return L().getValue() || R().getValue(); };
961   if (Op == "&&")
962     return [=] { return L().getValue() && R().getValue(); };
963   if (Op == "&")
964     return [=] { return bitAnd(L(), R()); };
965   if (Op == "|")
966     return [=] { return bitOr(L(), R()); };
967   llvm_unreachable("invalid operator");
968 }
969 
970 // This is a part of the operator-precedence parser. This function
971 // assumes that the remaining token stream starts with an operator.
972 Expr ScriptParser::readExpr1(Expr Lhs, int MinPrec) {
973   while (!atEOF() && !errorCount()) {
974     // Read an operator and an expression.
975     if (consume("?"))
976       return readTernary(Lhs);
977     StringRef Op1 = peek();
978     if (precedence(Op1) < MinPrec)
979       break;
980     skip();
981     Expr Rhs = readPrimary();
982 
983     // Evaluate the remaining part of the expression first if the
984     // next operator has greater precedence than the previous one.
985     // For example, if we have read "+" and "3", and if the next
986     // operator is "*", then we'll evaluate 3 * ... part first.
987     while (!atEOF()) {
988       StringRef Op2 = peek();
989       if (precedence(Op2) <= precedence(Op1))
990         break;
991       Rhs = readExpr1(Rhs, precedence(Op2));
992     }
993 
994     Lhs = combine(Op1, Lhs, Rhs);
995   }
996   return Lhs;
997 }
998 
999 Expr ScriptParser::getPageSize() {
1000   std::string Location = getCurrentLocation();
1001   return [=]() -> uint64_t {
1002     if (Target)
1003       return Target->PageSize;
1004     error(Location + ": unable to calculate page size");
1005     return 4096; // Return a dummy value.
1006   };
1007 }
1008 
1009 Expr ScriptParser::readConstant() {
1010   StringRef S = readParenLiteral();
1011   if (S == "COMMONPAGESIZE")
1012     return getPageSize();
1013   if (S == "MAXPAGESIZE")
1014     return [] { return Config->MaxPageSize; };
1015   setError("unknown constant: " + S);
1016   return [] { return 0; };
1017 }
1018 
1019 // Parses Tok as an integer. It recognizes hexadecimal (prefixed with
1020 // "0x" or suffixed with "H") and decimal numbers. Decimal numbers may
1021 // have "K" (Ki) or "M" (Mi) suffixes.
1022 static Optional<uint64_t> parseInt(StringRef Tok) {
1023   // Hexadecimal
1024   uint64_t Val;
1025   if (Tok.startswith_lower("0x")) {
1026     if (!to_integer(Tok.substr(2), Val, 16))
1027       return None;
1028     return Val;
1029   }
1030   if (Tok.endswith_lower("H")) {
1031     if (!to_integer(Tok.drop_back(), Val, 16))
1032       return None;
1033     return Val;
1034   }
1035 
1036   // Decimal
1037   if (Tok.endswith_lower("K")) {
1038     if (!to_integer(Tok.drop_back(), Val, 10))
1039       return None;
1040     return Val * 1024;
1041   }
1042   if (Tok.endswith_lower("M")) {
1043     if (!to_integer(Tok.drop_back(), Val, 10))
1044       return None;
1045     return Val * 1024 * 1024;
1046   }
1047   if (!to_integer(Tok, Val, 10))
1048     return None;
1049   return Val;
1050 }
1051 
1052 ByteCommand *ScriptParser::readByteCommand(StringRef Tok) {
1053   int Size = StringSwitch<int>(Tok)
1054                  .Case("BYTE", 1)
1055                  .Case("SHORT", 2)
1056                  .Case("LONG", 4)
1057                  .Case("QUAD", 8)
1058                  .Default(-1);
1059   if (Size == -1)
1060     return nullptr;
1061 
1062   size_t OldPos = Pos;
1063   Expr E = readParenExpr();
1064   std::string CommandString =
1065       Tok.str() + " " +
1066       llvm::join(Tokens.begin() + OldPos, Tokens.begin() + Pos, " ");
1067   return make<ByteCommand>(E, Size, CommandString);
1068 }
1069 
1070 StringRef ScriptParser::readParenLiteral() {
1071   expect("(");
1072   bool Orig = InExpr;
1073   InExpr = false;
1074   StringRef Tok = next();
1075   InExpr = Orig;
1076   expect(")");
1077   return Tok;
1078 }
1079 
1080 static void checkIfExists(OutputSection *Cmd, StringRef Location) {
1081   if (Cmd->Location.empty() && Script->ErrorOnMissingSection)
1082     error(Location + ": undefined section " + Cmd->Name);
1083 }
1084 
1085 Expr ScriptParser::readPrimary() {
1086   if (peek() == "(")
1087     return readParenExpr();
1088 
1089   if (consume("~")) {
1090     Expr E = readPrimary();
1091     return [=] { return ~E().getValue(); };
1092   }
1093   if (consume("!")) {
1094     Expr E = readPrimary();
1095     return [=] { return !E().getValue(); };
1096   }
1097   if (consume("-")) {
1098     Expr E = readPrimary();
1099     return [=] { return -E().getValue(); };
1100   }
1101 
1102   StringRef Tok = next();
1103   std::string Location = getCurrentLocation();
1104 
1105   // Built-in functions are parsed here.
1106   // https://sourceware.org/binutils/docs/ld/Builtin-Functions.html.
1107   if (Tok == "ABSOLUTE") {
1108     Expr Inner = readParenExpr();
1109     return [=] {
1110       ExprValue I = Inner();
1111       I.ForceAbsolute = true;
1112       return I;
1113     };
1114   }
1115   if (Tok == "ADDR") {
1116     StringRef Name = readParenLiteral();
1117     OutputSection *Sec = Script->getOrCreateOutputSection(Name);
1118     return [=]() -> ExprValue {
1119       checkIfExists(Sec, Location);
1120       return {Sec, false, 0, Location};
1121     };
1122   }
1123   if (Tok == "ALIGN") {
1124     expect("(");
1125     Expr E = readExpr();
1126     if (consume(")")) {
1127       E = checkAlignment(E, Location);
1128       return [=] { return alignTo(Script->getDot(), E().getValue()); };
1129     }
1130     expect(",");
1131     Expr E2 = checkAlignment(readExpr(), Location);
1132     expect(")");
1133     return [=] {
1134       ExprValue V = E();
1135       V.Alignment = E2().getValue();
1136       return V;
1137     };
1138   }
1139   if (Tok == "ALIGNOF") {
1140     StringRef Name = readParenLiteral();
1141     OutputSection *Cmd = Script->getOrCreateOutputSection(Name);
1142     return [=] {
1143       checkIfExists(Cmd, Location);
1144       return Cmd->Alignment;
1145     };
1146   }
1147   if (Tok == "ASSERT")
1148     return readAssert();
1149   if (Tok == "CONSTANT")
1150     return readConstant();
1151   if (Tok == "DATA_SEGMENT_ALIGN") {
1152     expect("(");
1153     Expr E = readExpr();
1154     expect(",");
1155     readExpr();
1156     expect(")");
1157     return [=] {
1158       return alignTo(Script->getDot(), std::max((uint64_t)1, E().getValue()));
1159     };
1160   }
1161   if (Tok == "DATA_SEGMENT_END") {
1162     expect("(");
1163     expect(".");
1164     expect(")");
1165     return [] { return Script->getDot(); };
1166   }
1167   if (Tok == "DATA_SEGMENT_RELRO_END") {
1168     // GNU linkers implements more complicated logic to handle
1169     // DATA_SEGMENT_RELRO_END. We instead ignore the arguments and
1170     // just align to the next page boundary for simplicity.
1171     expect("(");
1172     readExpr();
1173     expect(",");
1174     readExpr();
1175     expect(")");
1176     Expr E = getPageSize();
1177     return [=] { return alignTo(Script->getDot(), E().getValue()); };
1178   }
1179   if (Tok == "DEFINED") {
1180     StringRef Name = readParenLiteral();
1181     return [=] { return Symtab->find(Name) ? 1 : 0; };
1182   }
1183   if (Tok == "LENGTH") {
1184     StringRef Name = readParenLiteral();
1185     if (Script->MemoryRegions.count(Name) == 0) {
1186       setError("memory region not defined: " + Name);
1187       return [] { return 0; };
1188     }
1189     return [=] { return Script->MemoryRegions[Name]->Length; };
1190   }
1191   if (Tok == "LOADADDR") {
1192     StringRef Name = readParenLiteral();
1193     OutputSection *Cmd = Script->getOrCreateOutputSection(Name);
1194     return [=] {
1195       checkIfExists(Cmd, Location);
1196       return Cmd->getLMA();
1197     };
1198   }
1199   if (Tok == "MAX" || Tok == "MIN") {
1200     expect("(");
1201     Expr A = readExpr();
1202     expect(",");
1203     Expr B = readExpr();
1204     expect(")");
1205     if (Tok == "MIN")
1206       return [=] { return std::min(A().getValue(), B().getValue()); };
1207     return [=] { return std::max(A().getValue(), B().getValue()); };
1208   }
1209   if (Tok == "ORIGIN") {
1210     StringRef Name = readParenLiteral();
1211     if (Script->MemoryRegions.count(Name) == 0) {
1212       setError("memory region not defined: " + Name);
1213       return [] { return 0; };
1214     }
1215     return [=] { return Script->MemoryRegions[Name]->Origin; };
1216   }
1217   if (Tok == "SEGMENT_START") {
1218     expect("(");
1219     skip();
1220     expect(",");
1221     Expr E = readExpr();
1222     expect(")");
1223     return [=] { return E(); };
1224   }
1225   if (Tok == "SIZEOF") {
1226     StringRef Name = readParenLiteral();
1227     OutputSection *Cmd = Script->getOrCreateOutputSection(Name);
1228     // Linker script does not create an output section if its content is empty.
1229     // We want to allow SIZEOF(.foo) where .foo is a section which happened to
1230     // be empty.
1231     return [=] { return Cmd->Size; };
1232   }
1233   if (Tok == "SIZEOF_HEADERS")
1234     return [=] { return elf::getHeaderSize(); };
1235 
1236   // Tok is the dot.
1237   if (Tok == ".")
1238     return [=] { return Script->getSymbolValue(Tok, Location); };
1239 
1240   // Tok is a literal number.
1241   if (Optional<uint64_t> Val = parseInt(Tok))
1242     return [=] { return *Val; };
1243 
1244   // Tok is a symbol name.
1245   if (!isValidCIdentifier(Tok))
1246     setError("malformed number: " + Tok);
1247   Script->ReferencedSymbols.push_back(Tok);
1248   return [=] { return Script->getSymbolValue(Tok, Location); };
1249 }
1250 
1251 Expr ScriptParser::readTernary(Expr Cond) {
1252   Expr L = readExpr();
1253   expect(":");
1254   Expr R = readExpr();
1255   return [=] { return Cond().getValue() ? L() : R(); };
1256 }
1257 
1258 Expr ScriptParser::readParenExpr() {
1259   expect("(");
1260   Expr E = readExpr();
1261   expect(")");
1262   return E;
1263 }
1264 
1265 std::vector<StringRef> ScriptParser::readOutputSectionPhdrs() {
1266   std::vector<StringRef> Phdrs;
1267   while (!errorCount() && peek().startswith(":")) {
1268     StringRef Tok = next();
1269     Phdrs.push_back((Tok.size() == 1) ? next() : Tok.substr(1));
1270   }
1271   return Phdrs;
1272 }
1273 
1274 // Read a program header type name. The next token must be a
1275 // name of a program header type or a constant (e.g. "0x3").
1276 unsigned ScriptParser::readPhdrType() {
1277   StringRef Tok = next();
1278   if (Optional<uint64_t> Val = parseInt(Tok))
1279     return *Val;
1280 
1281   unsigned Ret = StringSwitch<unsigned>(Tok)
1282                      .Case("PT_NULL", PT_NULL)
1283                      .Case("PT_LOAD", PT_LOAD)
1284                      .Case("PT_DYNAMIC", PT_DYNAMIC)
1285                      .Case("PT_INTERP", PT_INTERP)
1286                      .Case("PT_NOTE", PT_NOTE)
1287                      .Case("PT_SHLIB", PT_SHLIB)
1288                      .Case("PT_PHDR", PT_PHDR)
1289                      .Case("PT_TLS", PT_TLS)
1290                      .Case("PT_GNU_EH_FRAME", PT_GNU_EH_FRAME)
1291                      .Case("PT_GNU_STACK", PT_GNU_STACK)
1292                      .Case("PT_GNU_RELRO", PT_GNU_RELRO)
1293                      .Case("PT_OPENBSD_RANDOMIZE", PT_OPENBSD_RANDOMIZE)
1294                      .Case("PT_OPENBSD_WXNEEDED", PT_OPENBSD_WXNEEDED)
1295                      .Case("PT_OPENBSD_BOOTDATA", PT_OPENBSD_BOOTDATA)
1296                      .Default(-1);
1297 
1298   if (Ret == (unsigned)-1) {
1299     setError("invalid program header type: " + Tok);
1300     return PT_NULL;
1301   }
1302   return Ret;
1303 }
1304 
1305 // Reads an anonymous version declaration.
1306 void ScriptParser::readAnonymousDeclaration() {
1307   std::vector<SymbolVersion> Locals;
1308   std::vector<SymbolVersion> Globals;
1309   std::tie(Locals, Globals) = readSymbols();
1310 
1311   for (SymbolVersion V : Locals) {
1312     if (V.Name == "*")
1313       Config->DefaultSymbolVersion = VER_NDX_LOCAL;
1314     else
1315       Config->VersionScriptLocals.push_back(V);
1316   }
1317 
1318   for (SymbolVersion V : Globals)
1319     Config->VersionScriptGlobals.push_back(V);
1320 
1321   expect(";");
1322 }
1323 
1324 // Reads a non-anonymous version definition,
1325 // e.g. "VerStr { global: foo; bar; local: *; };".
1326 void ScriptParser::readVersionDeclaration(StringRef VerStr) {
1327   // Read a symbol list.
1328   std::vector<SymbolVersion> Locals;
1329   std::vector<SymbolVersion> Globals;
1330   std::tie(Locals, Globals) = readSymbols();
1331 
1332   for (SymbolVersion V : Locals) {
1333     if (V.Name == "*")
1334       Config->DefaultSymbolVersion = VER_NDX_LOCAL;
1335     else
1336       Config->VersionScriptLocals.push_back(V);
1337   }
1338 
1339   // Create a new version definition and add that to the global symbols.
1340   VersionDefinition Ver;
1341   Ver.Name = VerStr;
1342   Ver.Globals = Globals;
1343 
1344   // User-defined version number starts from 2 because 0 and 1 are
1345   // reserved for VER_NDX_LOCAL and VER_NDX_GLOBAL, respectively.
1346   Ver.Id = Config->VersionDefinitions.size() + 2;
1347   Config->VersionDefinitions.push_back(Ver);
1348 
1349   // Each version may have a parent version. For example, "Ver2"
1350   // defined as "Ver2 { global: foo; local: *; } Ver1;" has "Ver1"
1351   // as a parent. This version hierarchy is, probably against your
1352   // instinct, purely for hint; the runtime doesn't care about it
1353   // at all. In LLD, we simply ignore it.
1354   if (peek() != ";")
1355     skip();
1356   expect(";");
1357 }
1358 
1359 static bool hasWildcard(StringRef S) {
1360   return S.find_first_of("?*[") != StringRef::npos;
1361 }
1362 
1363 // Reads a list of symbols, e.g. "{ global: foo; bar; local: *; };".
1364 std::pair<std::vector<SymbolVersion>, std::vector<SymbolVersion>>
1365 ScriptParser::readSymbols() {
1366   std::vector<SymbolVersion> Locals;
1367   std::vector<SymbolVersion> Globals;
1368   std::vector<SymbolVersion> *V = &Globals;
1369 
1370   while (!errorCount()) {
1371     if (consume("}"))
1372       break;
1373     if (consumeLabel("local")) {
1374       V = &Locals;
1375       continue;
1376     }
1377     if (consumeLabel("global")) {
1378       V = &Globals;
1379       continue;
1380     }
1381 
1382     if (consume("extern")) {
1383       std::vector<SymbolVersion> Ext = readVersionExtern();
1384       V->insert(V->end(), Ext.begin(), Ext.end());
1385     } else {
1386       StringRef Tok = next();
1387       V->push_back({unquote(Tok), false, hasWildcard(Tok)});
1388     }
1389     expect(";");
1390   }
1391   return {Locals, Globals};
1392 }
1393 
1394 // Reads an "extern C++" directive, e.g.,
1395 // "extern "C++" { ns::*; "f(int, double)"; };"
1396 //
1397 // The last semicolon is optional. E.g. this is OK:
1398 // "extern "C++" { ns::*; "f(int, double)" };"
1399 std::vector<SymbolVersion> ScriptParser::readVersionExtern() {
1400   StringRef Tok = next();
1401   bool IsCXX = Tok == "\"C++\"";
1402   if (!IsCXX && Tok != "\"C\"")
1403     setError("Unknown language");
1404   expect("{");
1405 
1406   std::vector<SymbolVersion> Ret;
1407   while (!errorCount() && peek() != "}") {
1408     StringRef Tok = next();
1409     bool HasWildcard = !Tok.startswith("\"") && hasWildcard(Tok);
1410     Ret.push_back({unquote(Tok), IsCXX, HasWildcard});
1411     if (consume("}"))
1412       return Ret;
1413     expect(";");
1414   }
1415 
1416   expect("}");
1417   return Ret;
1418 }
1419 
1420 uint64_t ScriptParser::readMemoryAssignment(StringRef S1, StringRef S2,
1421                                             StringRef S3) {
1422   if (!consume(S1) && !consume(S2) && !consume(S3)) {
1423     setError("expected one of: " + S1 + ", " + S2 + ", or " + S3);
1424     return 0;
1425   }
1426   expect("=");
1427   return readExpr()().getValue();
1428 }
1429 
1430 // Parse the MEMORY command as specified in:
1431 // https://sourceware.org/binutils/docs/ld/MEMORY.html
1432 //
1433 // MEMORY { name [(attr)] : ORIGIN = origin, LENGTH = len ... }
1434 void ScriptParser::readMemory() {
1435   expect("{");
1436   while (!errorCount() && !consume("}")) {
1437     StringRef Tok = next();
1438     if (Tok == "INCLUDE") {
1439       readInclude();
1440       continue;
1441     }
1442 
1443     uint32_t Flags = 0;
1444     uint32_t NegFlags = 0;
1445     if (consume("(")) {
1446       std::tie(Flags, NegFlags) = readMemoryAttributes();
1447       expect(")");
1448     }
1449     expect(":");
1450 
1451     uint64_t Origin = readMemoryAssignment("ORIGIN", "org", "o");
1452     expect(",");
1453     uint64_t Length = readMemoryAssignment("LENGTH", "len", "l");
1454 
1455     // Add the memory region to the region map.
1456     MemoryRegion *MR = make<MemoryRegion>(Tok, Origin, Length, Flags, NegFlags);
1457     if (!Script->MemoryRegions.insert({Tok, MR}).second)
1458       setError("region '" + Tok + "' already defined");
1459   }
1460 }
1461 
1462 // This function parses the attributes used to match against section
1463 // flags when placing output sections in a memory region. These flags
1464 // are only used when an explicit memory region name is not used.
1465 std::pair<uint32_t, uint32_t> ScriptParser::readMemoryAttributes() {
1466   uint32_t Flags = 0;
1467   uint32_t NegFlags = 0;
1468   bool Invert = false;
1469 
1470   for (char C : next().lower()) {
1471     uint32_t Flag = 0;
1472     if (C == '!')
1473       Invert = !Invert;
1474     else if (C == 'w')
1475       Flag = SHF_WRITE;
1476     else if (C == 'x')
1477       Flag = SHF_EXECINSTR;
1478     else if (C == 'a')
1479       Flag = SHF_ALLOC;
1480     else if (C != 'r')
1481       setError("invalid memory region attribute");
1482 
1483     if (Invert)
1484       NegFlags |= Flag;
1485     else
1486       Flags |= Flag;
1487   }
1488   return {Flags, NegFlags};
1489 }
1490 
1491 void elf::readLinkerScript(MemoryBufferRef MB) {
1492   ScriptParser(MB).readLinkerScript();
1493 }
1494 
1495 void elf::readVersionScript(MemoryBufferRef MB) {
1496   ScriptParser(MB).readVersionScript();
1497 }
1498 
1499 void elf::readDynamicList(MemoryBufferRef MB) {
1500   ScriptParser(MB).readDynamicList();
1501 }
1502 
1503 void elf::readDefsym(StringRef Name, MemoryBufferRef MB) {
1504   ScriptParser(MB).readDefsym(Name);
1505 }
1506