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