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