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