1 //===- LinkerScript.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 the parser/evaluator of the linker script.
11 // It parses a linker script and write the result to Config or ScriptConfig
12 // objects.
13 //
14 // If SECTIONS command is used, a ScriptConfig contains an AST
15 // of the command which will later be consumed by createSections() and
16 // assignAddresses().
17 //
18 //===----------------------------------------------------------------------===//
19 
20 #include "LinkerScript.h"
21 #include "Config.h"
22 #include "Driver.h"
23 #include "InputSection.h"
24 #include "OutputSections.h"
25 #include "ScriptParser.h"
26 #include "Strings.h"
27 #include "Symbols.h"
28 #include "SymbolTable.h"
29 #include "Target.h"
30 #include "Writer.h"
31 #include "llvm/ADT/StringSwitch.h"
32 #include "llvm/Support/ELF.h"
33 #include "llvm/Support/FileSystem.h"
34 #include "llvm/Support/MemoryBuffer.h"
35 #include "llvm/Support/Path.h"
36 #include "llvm/Support/StringSaver.h"
37 
38 using namespace llvm;
39 using namespace llvm::ELF;
40 using namespace llvm::object;
41 using namespace lld;
42 using namespace lld::elf;
43 
44 ScriptConfiguration *elf::ScriptConfig;
45 
46 bool SymbolAssignment::classof(const BaseCommand *C) {
47   return C->Kind == AssignmentKind;
48 }
49 
50 bool OutputSectionCommand::classof(const BaseCommand *C) {
51   return C->Kind == OutputSectionKind;
52 }
53 
54 bool InputSectionDescription::classof(const BaseCommand *C) {
55   return C->Kind == InputSectionKind;
56 }
57 
58 bool AssertCommand::classof(const BaseCommand *C) {
59   return C->Kind == AssertKind;
60 }
61 
62 template <class ELFT> static bool isDiscarded(InputSectionBase<ELFT> *S) {
63   return !S || !S->Live;
64 }
65 
66 template <class ELFT>
67 bool LinkerScript<ELFT>::shouldKeep(InputSectionBase<ELFT> *S) {
68   for (StringRef Pat : Opt.KeptSections)
69     if (globMatch(Pat, S->getSectionName()))
70       return true;
71   return false;
72 }
73 
74 static bool match(ArrayRef<StringRef> Patterns, StringRef S) {
75   for (StringRef Pat : Patterns)
76     if (globMatch(Pat, S))
77       return true;
78   return false;
79 }
80 
81 // Create a vector of (<output section name>, <input section description>).
82 template <class ELFT>
83 std::vector<std::pair<StringRef, const InputSectionDescription *>>
84 LinkerScript<ELFT>::getSectionMap() {
85   std::vector<std::pair<StringRef, const InputSectionDescription *>> Ret;
86 
87   for (const std::unique_ptr<BaseCommand> &Base1 : Opt.Commands)
88     if (auto *Cmd1 = dyn_cast<OutputSectionCommand>(Base1.get()))
89       for (const std::unique_ptr<BaseCommand> &Base2 : Cmd1->Commands)
90         if (auto *Cmd2 = dyn_cast<InputSectionDescription>(Base2.get()))
91           Ret.emplace_back(Cmd1->Name, Cmd2);
92 
93   return Ret;
94 }
95 
96 static bool fileMatches(const InputSectionDescription *Desc,
97                         StringRef Filename) {
98   if (!globMatch(Desc->FilePattern, Filename))
99     return false;
100   return Desc->ExcludedFiles.empty() || !match(Desc->ExcludedFiles, Filename);
101 }
102 
103 // Returns input sections filtered by given glob patterns.
104 template <class ELFT>
105 std::vector<InputSectionBase<ELFT> *>
106 LinkerScript<ELFT>::getInputSections(const InputSectionDescription *I) {
107   ArrayRef<StringRef> Patterns = I->SectionPatterns;
108   std::vector<InputSectionBase<ELFT> *> Ret;
109   for (const std::unique_ptr<ObjectFile<ELFT>> &F :
110        Symtab<ELFT>::X->getObjectFiles()) {
111     if (fileMatches(I, sys::path::filename(F->getName())))
112       for (InputSectionBase<ELFT> *S : F->getSections())
113         if (!isDiscarded(S) && !S->OutSec &&
114             match(Patterns, S->getSectionName()))
115           Ret.push_back(S);
116   }
117 
118   if ((llvm::find(Patterns, "COMMON") != Patterns.end()))
119     Ret.push_back(CommonInputSection<ELFT>::X);
120 
121   return Ret;
122 }
123 
124 // Add input section to output section. If there is no output section yet,
125 // then create it and add to output section list.
126 template <class ELFT>
127 static void addSection(OutputSectionFactory<ELFT> &Factory,
128                        std::vector<OutputSectionBase<ELFT> *> &Out,
129                        InputSectionBase<ELFT> *C, StringRef Name) {
130   OutputSectionBase<ELFT> *Sec;
131   bool IsNew;
132   std::tie(Sec, IsNew) = Factory.create(C, Name);
133   if (IsNew)
134     Out.push_back(Sec);
135   Sec->addSection(C);
136 }
137 
138 template <class ELFT>
139 static bool compareName(InputSectionBase<ELFT> *A, InputSectionBase<ELFT> *B) {
140   return A->getSectionName() < B->getSectionName();
141 }
142 
143 template <class ELFT>
144 static bool compareAlignment(InputSectionBase<ELFT> *A,
145                              InputSectionBase<ELFT> *B) {
146   // ">" is not a mistake. Larger alignments are placed before smaller
147   // alignments in order to reduce the amount of padding necessary.
148   // This is compatible with GNU.
149   return A->Alignment > B->Alignment;
150 }
151 
152 template <class ELFT>
153 static std::function<bool(InputSectionBase<ELFT> *, InputSectionBase<ELFT> *)>
154 getComparator(SortKind K) {
155   if (K == SortByName)
156     return compareName<ELFT>;
157   return compareAlignment<ELFT>;
158 }
159 
160 template <class ELFT>
161 void LinkerScript<ELFT>::createSections(
162     OutputSectionFactory<ELFT> &Factory) {
163   for (auto &P : getSectionMap()) {
164     StringRef OutputName = P.first;
165     const InputSectionDescription *Cmd = P.second;
166     std::vector<InputSectionBase<ELFT> *> Sections = getInputSections(Cmd);
167 
168     if (OutputName == "/DISCARD/") {
169       for (InputSectionBase<ELFT> *S : Sections) {
170         S->Live = false;
171         reportDiscarded(S);
172       }
173       continue;
174     }
175 
176     if (Cmd->SortInner)
177       std::stable_sort(Sections.begin(), Sections.end(),
178                        getComparator<ELFT>(Cmd->SortInner));
179     if (Cmd->SortOuter)
180       std::stable_sort(Sections.begin(), Sections.end(),
181                        getComparator<ELFT>(Cmd->SortOuter));
182 
183     for (InputSectionBase<ELFT> *S : Sections)
184       addSection(Factory, *OutputSections, S, OutputName);
185   }
186 
187   // Add all other input sections, which are not listed in script.
188   for (const std::unique_ptr<ObjectFile<ELFT>> &F :
189        Symtab<ELFT>::X->getObjectFiles())
190     for (InputSectionBase<ELFT> *S : F->getSections())
191       if (!isDiscarded(S) && !S->OutSec)
192         addSection(Factory, *OutputSections, S, getOutputSectionName(S));
193 
194   // Remove from the output all the sections which did not meet
195   // the optional constraints.
196   filter();
197 }
198 
199 template <class R, class T>
200 static inline void removeElementsIf(R &Range, const T &Pred) {
201   Range.erase(std::remove_if(Range.begin(), Range.end(), Pred), Range.end());
202 }
203 
204 // Process ONLY_IF_RO and ONLY_IF_RW.
205 template <class ELFT> void LinkerScript<ELFT>::filter() {
206   // In this loop, we remove output sections if they don't satisfy
207   // requested properties.
208   for (const std::unique_ptr<BaseCommand> &Base : Opt.Commands) {
209     auto *Cmd = dyn_cast<OutputSectionCommand>(Base.get());
210     if (!Cmd || Cmd->Name == "/DISCARD/")
211       continue;
212 
213     if (Cmd->Constraint == ConstraintKind::NoConstraint)
214       continue;
215 
216     bool RO = (Cmd->Constraint == ConstraintKind::ReadOnly);
217     bool RW = (Cmd->Constraint == ConstraintKind::ReadWrite);
218 
219     removeElementsIf(*OutputSections, [&](OutputSectionBase<ELFT> *S) {
220       bool Writable = (S->getFlags() & SHF_WRITE);
221       return S->getName() == Cmd->Name &&
222              ((RO && Writable) || (RW && !Writable));
223     });
224   }
225 }
226 
227 template <class ELFT> void LinkerScript<ELFT>::assignAddresses() {
228   ArrayRef<OutputSectionBase<ELFT> *> Sections = *OutputSections;
229   // Orphan sections are sections present in the input files which
230   // are not explicitly placed into the output file by the linker script.
231   // We place orphan sections at end of file.
232   // Other linkers places them using some heuristics as described in
233   // https://sourceware.org/binutils/docs/ld/Orphan-Sections.html#Orphan-Sections.
234   for (OutputSectionBase<ELFT> *Sec : Sections) {
235     StringRef Name = Sec->getName();
236     if (getSectionIndex(Name) == INT_MAX)
237       Opt.Commands.push_back(llvm::make_unique<OutputSectionCommand>(Name));
238   }
239 
240   // Assign addresses as instructed by linker script SECTIONS sub-commands.
241   Dot = Out<ELFT>::ElfHeader->getSize() + Out<ELFT>::ProgramHeaders->getSize();
242   uintX_t MinVA = std::numeric_limits<uintX_t>::max();
243   uintX_t ThreadBssOffset = 0;
244 
245   for (const std::unique_ptr<BaseCommand> &Base : Opt.Commands) {
246     if (auto *Cmd = dyn_cast<SymbolAssignment>(Base.get())) {
247       if (Cmd->Name == ".") {
248         Dot = Cmd->Expression(Dot);
249       } else if (Cmd->Sym) {
250         cast<DefinedRegular<ELFT>>(Cmd->Sym)->Value = Cmd->Expression(Dot);
251       }
252       continue;
253     }
254 
255     if (auto *Cmd = dyn_cast<AssertCommand>(Base.get())) {
256       Cmd->Expression(Dot);
257       continue;
258     }
259 
260     // Find all the sections with required name. There can be more than
261     // one section with such name, if the alignment, flags or type
262     // attribute differs.
263     auto *Cmd = cast<OutputSectionCommand>(Base.get());
264     for (OutputSectionBase<ELFT> *Sec : Sections) {
265       if (Sec->getName() != Cmd->Name)
266         continue;
267 
268       if (Cmd->AddrExpr)
269         Dot = Cmd->AddrExpr(Dot);
270 
271       if (Cmd->AlignExpr)
272         Sec->updateAlignment(Cmd->AlignExpr(Dot));
273 
274       if ((Sec->getFlags() & SHF_TLS) && Sec->getType() == SHT_NOBITS) {
275         uintX_t TVA = Dot + ThreadBssOffset;
276         TVA = alignTo(TVA, Sec->getAlignment());
277         Sec->setVA(TVA);
278         ThreadBssOffset = TVA - Dot + Sec->getSize();
279         continue;
280       }
281 
282       if (Sec->getFlags() & SHF_ALLOC) {
283         Dot = alignTo(Dot, Sec->getAlignment());
284         Sec->setVA(Dot);
285         MinVA = std::min(MinVA, Dot);
286         Dot += Sec->getSize();
287         continue;
288       }
289     }
290   }
291 
292   // ELF and Program headers need to be right before the first section in
293   // memory. Set their addresses accordingly.
294   MinVA = alignDown(MinVA - Out<ELFT>::ElfHeader->getSize() -
295                         Out<ELFT>::ProgramHeaders->getSize(),
296                     Target->PageSize);
297   Out<ELFT>::ElfHeader->setVA(MinVA);
298   Out<ELFT>::ProgramHeaders->setVA(Out<ELFT>::ElfHeader->getSize() + MinVA);
299 }
300 
301 template <class ELFT>
302 std::vector<PhdrEntry<ELFT>> LinkerScript<ELFT>::createPhdrs() {
303   ArrayRef<OutputSectionBase<ELFT> *> Sections = *OutputSections;
304   std::vector<PhdrEntry<ELFT>> Ret;
305 
306   for (const PhdrsCommand &Cmd : Opt.PhdrsCommands) {
307     Ret.emplace_back(Cmd.Type, Cmd.Flags == UINT_MAX ? PF_R : Cmd.Flags);
308     PhdrEntry<ELFT> &Phdr = Ret.back();
309 
310     if (Cmd.HasFilehdr)
311       Phdr.add(Out<ELFT>::ElfHeader);
312     if (Cmd.HasPhdrs)
313       Phdr.add(Out<ELFT>::ProgramHeaders);
314 
315     switch (Cmd.Type) {
316     case PT_INTERP:
317       if (Out<ELFT>::Interp)
318         Phdr.add(Out<ELFT>::Interp);
319       break;
320     case PT_DYNAMIC:
321       if (Out<ELFT>::DynSymTab) {
322         Phdr.H.p_flags = Out<ELFT>::Dynamic->getPhdrFlags();
323         Phdr.add(Out<ELFT>::Dynamic);
324       }
325       break;
326     case PT_GNU_EH_FRAME:
327       if (!Out<ELFT>::EhFrame->empty() && Out<ELFT>::EhFrameHdr) {
328         Phdr.H.p_flags = Out<ELFT>::EhFrameHdr->getPhdrFlags();
329         Phdr.add(Out<ELFT>::EhFrameHdr);
330       }
331       break;
332     }
333   }
334 
335   PhdrEntry<ELFT> *Load = nullptr;
336   uintX_t Flags = PF_R;
337   for (OutputSectionBase<ELFT> *Sec : Sections) {
338     if (!(Sec->getFlags() & SHF_ALLOC))
339       break;
340 
341     std::vector<size_t> PhdrIds = getPhdrIndices(Sec->getName());
342     if (!PhdrIds.empty()) {
343       // Assign headers specified by linker script
344       for (size_t Id : PhdrIds) {
345         Ret[Id].add(Sec);
346         if (Opt.PhdrsCommands[Id].Flags == UINT_MAX)
347           Ret[Id].H.p_flags |= Sec->getPhdrFlags();
348       }
349     } else {
350       // If we have no load segment or flags've changed then we want new load
351       // segment.
352       uintX_t NewFlags = Sec->getPhdrFlags();
353       if (Load == nullptr || Flags != NewFlags) {
354         Load = &*Ret.emplace(Ret.end(), PT_LOAD, NewFlags);
355         Flags = NewFlags;
356       }
357       Load->add(Sec);
358     }
359   }
360   return Ret;
361 }
362 
363 template <class ELFT>
364 ArrayRef<uint8_t> LinkerScript<ELFT>::getFiller(StringRef Name) {
365   for (const std::unique_ptr<BaseCommand> &Base : Opt.Commands)
366     if (auto *Cmd = dyn_cast<OutputSectionCommand>(Base.get()))
367       if (Cmd->Name == Name)
368         return Cmd->Filler;
369   return {};
370 }
371 
372 // Returns the index of the given section name in linker script
373 // SECTIONS commands. Sections are laid out as the same order as they
374 // were in the script. If a given name did not appear in the script,
375 // it returns INT_MAX, so that it will be laid out at end of file.
376 template <class ELFT> int LinkerScript<ELFT>::getSectionIndex(StringRef Name) {
377   int I = 0;
378   for (std::unique_ptr<BaseCommand> &Base : Opt.Commands) {
379     if (auto *Cmd = dyn_cast<OutputSectionCommand>(Base.get()))
380       if (Cmd->Name == Name)
381         return I;
382     ++I;
383   }
384   return INT_MAX;
385 }
386 
387 // A compartor to sort output sections. Returns -1 or 1 if
388 // A or B are mentioned in linker script. Otherwise, returns 0.
389 template <class ELFT>
390 int LinkerScript<ELFT>::compareSections(StringRef A, StringRef B) {
391   int I = getSectionIndex(A);
392   int J = getSectionIndex(B);
393   if (I == INT_MAX && J == INT_MAX)
394     return 0;
395   return I < J ? -1 : 1;
396 }
397 
398 // Add symbols defined by linker scripts.
399 template <class ELFT> void LinkerScript<ELFT>::addScriptedSymbols() {
400   for (const std::unique_ptr<BaseCommand> &Base : Opt.Commands) {
401     auto *Cmd = dyn_cast<SymbolAssignment>(Base.get());
402     if (!Cmd || Cmd->Name == ".")
403       continue;
404 
405     // If a symbol was in PROVIDE(), define it only when it is an
406     // undefined symbol.
407     SymbolBody *B = Symtab<ELFT>::X->find(Cmd->Name);
408     if (Cmd->Provide && !(B && B->isUndefined()))
409       continue;
410 
411     // Define an absolute symbol. The symbol value will be assigned later.
412     // (At this point, we don't know the final address yet.)
413     Symbol *Sym = Symtab<ELFT>::X->addUndefined(Cmd->Name);
414     replaceBody<DefinedRegular<ELFT>>(Sym, Cmd->Name, STV_DEFAULT);
415     Sym->Visibility = Cmd->Hidden ? STV_HIDDEN : STV_DEFAULT;
416     Cmd->Sym = Sym->body();
417   }
418 }
419 
420 template <class ELFT> bool LinkerScript<ELFT>::hasPhdrsCommands() {
421   return !Opt.PhdrsCommands.empty();
422 }
423 
424 template <class ELFT>
425 typename ELFT::uint LinkerScript<ELFT>::getOutputSectionSize(StringRef Name) {
426   for (OutputSectionBase<ELFT> *Sec : *OutputSections)
427     if (Sec->getName() == Name)
428       return Sec->getSize();
429   error("undefined section " + Name);
430   return 0;
431 }
432 
433 // Returns indices of ELF headers containing specific section, identified
434 // by Name. Each index is a zero based number of ELF header listed within
435 // PHDRS {} script block.
436 template <class ELFT>
437 std::vector<size_t> LinkerScript<ELFT>::getPhdrIndices(StringRef SectionName) {
438   for (const std::unique_ptr<BaseCommand> &Base : Opt.Commands) {
439     auto *Cmd = dyn_cast<OutputSectionCommand>(Base.get());
440     if (!Cmd || Cmd->Name != SectionName)
441       continue;
442 
443     std::vector<size_t> Ret;
444     for (StringRef PhdrName : Cmd->Phdrs)
445       Ret.push_back(getPhdrIndex(PhdrName));
446     return Ret;
447   }
448   return {};
449 }
450 
451 template <class ELFT>
452 size_t LinkerScript<ELFT>::getPhdrIndex(StringRef PhdrName) {
453   size_t I = 0;
454   for (PhdrsCommand &Cmd : Opt.PhdrsCommands) {
455     if (Cmd.Name == PhdrName)
456       return I;
457     ++I;
458   }
459   error("section header '" + PhdrName + "' is not listed in PHDRS");
460   return 0;
461 }
462 
463 class elf::ScriptParser : public ScriptParserBase {
464   typedef void (ScriptParser::*Handler)();
465 
466 public:
467   ScriptParser(StringRef S, bool B) : ScriptParserBase(S), IsUnderSysroot(B) {}
468 
469   void run();
470 
471 private:
472   void addFile(StringRef Path);
473 
474   void readAsNeeded();
475   void readEntry();
476   void readExtern();
477   void readGroup();
478   void readInclude();
479   void readNothing() {}
480   void readOutput();
481   void readOutputArch();
482   void readOutputFormat();
483   void readPhdrs();
484   void readSearchDir();
485   void readSections();
486 
487   SymbolAssignment *readAssignment(StringRef Name);
488   OutputSectionCommand *readOutputSectionDescription(StringRef OutSec);
489   std::vector<uint8_t> readOutputSectionFiller();
490   std::vector<StringRef> readOutputSectionPhdrs();
491   InputSectionDescription *readInputSectionDescription();
492   std::vector<StringRef> readInputFilePatterns();
493   InputSectionDescription *readInputSectionRules();
494   unsigned readPhdrType();
495   SortKind readSortKind();
496   SymbolAssignment *readProvide(bool Hidden);
497   Expr readAlign();
498   void readSort();
499   Expr readAssert();
500 
501   Expr readExpr();
502   Expr readExpr1(Expr Lhs, int MinPrec);
503   Expr readPrimary();
504   Expr readTernary(Expr Cond);
505 
506   const static StringMap<Handler> Cmd;
507   ScriptConfiguration &Opt = *ScriptConfig;
508   StringSaver Saver = {ScriptConfig->Alloc};
509   bool IsUnderSysroot;
510 };
511 
512 const StringMap<elf::ScriptParser::Handler> elf::ScriptParser::Cmd = {
513     {"ENTRY", &ScriptParser::readEntry},
514     {"EXTERN", &ScriptParser::readExtern},
515     {"GROUP", &ScriptParser::readGroup},
516     {"INCLUDE", &ScriptParser::readInclude},
517     {"INPUT", &ScriptParser::readGroup},
518     {"OUTPUT", &ScriptParser::readOutput},
519     {"OUTPUT_ARCH", &ScriptParser::readOutputArch},
520     {"OUTPUT_FORMAT", &ScriptParser::readOutputFormat},
521     {"PHDRS", &ScriptParser::readPhdrs},
522     {"SEARCH_DIR", &ScriptParser::readSearchDir},
523     {"SECTIONS", &ScriptParser::readSections},
524     {";", &ScriptParser::readNothing}};
525 
526 void ScriptParser::run() {
527   while (!atEOF()) {
528     StringRef Tok = next();
529     if (Handler Fn = Cmd.lookup(Tok))
530       (this->*Fn)();
531     else
532       setError("unknown directive: " + Tok);
533   }
534 }
535 
536 void ScriptParser::addFile(StringRef S) {
537   if (IsUnderSysroot && S.startswith("/")) {
538     SmallString<128> Path;
539     (Config->Sysroot + S).toStringRef(Path);
540     if (sys::fs::exists(Path)) {
541       Driver->addFile(Saver.save(Path.str()));
542       return;
543     }
544   }
545 
546   if (sys::path::is_absolute(S)) {
547     Driver->addFile(S);
548   } else if (S.startswith("=")) {
549     if (Config->Sysroot.empty())
550       Driver->addFile(S.substr(1));
551     else
552       Driver->addFile(Saver.save(Config->Sysroot + "/" + S.substr(1)));
553   } else if (S.startswith("-l")) {
554     Driver->addLibrary(S.substr(2));
555   } else if (sys::fs::exists(S)) {
556     Driver->addFile(S);
557   } else {
558     std::string Path = findFromSearchPaths(S);
559     if (Path.empty())
560       setError("unable to find " + S);
561     else
562       Driver->addFile(Saver.save(Path));
563   }
564 }
565 
566 void ScriptParser::readAsNeeded() {
567   expect("(");
568   bool Orig = Config->AsNeeded;
569   Config->AsNeeded = true;
570   while (!Error && !skip(")"))
571     addFile(next());
572   Config->AsNeeded = Orig;
573 }
574 
575 void ScriptParser::readEntry() {
576   // -e <symbol> takes predecence over ENTRY(<symbol>).
577   expect("(");
578   StringRef Tok = next();
579   if (Config->Entry.empty())
580     Config->Entry = Tok;
581   expect(")");
582 }
583 
584 void ScriptParser::readExtern() {
585   expect("(");
586   while (!Error && !skip(")"))
587     Config->Undefined.push_back(next());
588 }
589 
590 void ScriptParser::readGroup() {
591   expect("(");
592   while (!Error && !skip(")")) {
593     StringRef Tok = next();
594     if (Tok == "AS_NEEDED")
595       readAsNeeded();
596     else
597       addFile(Tok);
598   }
599 }
600 
601 void ScriptParser::readInclude() {
602   StringRef Tok = next();
603   auto MBOrErr = MemoryBuffer::getFile(Tok);
604   if (!MBOrErr) {
605     setError("cannot open " + Tok);
606     return;
607   }
608   std::unique_ptr<MemoryBuffer> &MB = *MBOrErr;
609   StringRef S = Saver.save(MB->getMemBufferRef().getBuffer());
610   std::vector<StringRef> V = tokenize(S);
611   Tokens.insert(Tokens.begin() + Pos, V.begin(), V.end());
612 }
613 
614 void ScriptParser::readOutput() {
615   // -o <file> takes predecence over OUTPUT(<file>).
616   expect("(");
617   StringRef Tok = next();
618   if (Config->OutputFile.empty())
619     Config->OutputFile = Tok;
620   expect(")");
621 }
622 
623 void ScriptParser::readOutputArch() {
624   // Error checking only for now.
625   expect("(");
626   next();
627   expect(")");
628 }
629 
630 void ScriptParser::readOutputFormat() {
631   // Error checking only for now.
632   expect("(");
633   next();
634   StringRef Tok = next();
635   if (Tok == ")")
636    return;
637   if (Tok != ",") {
638     setError("unexpected token: " + Tok);
639     return;
640   }
641   next();
642   expect(",");
643   next();
644   expect(")");
645 }
646 
647 void ScriptParser::readPhdrs() {
648   expect("{");
649   while (!Error && !skip("}")) {
650     StringRef Tok = next();
651     Opt.PhdrsCommands.push_back({Tok, PT_NULL, false, false, UINT_MAX});
652     PhdrsCommand &PhdrCmd = Opt.PhdrsCommands.back();
653 
654     PhdrCmd.Type = readPhdrType();
655     do {
656       Tok = next();
657       if (Tok == ";")
658         break;
659       if (Tok == "FILEHDR")
660         PhdrCmd.HasFilehdr = true;
661       else if (Tok == "PHDRS")
662         PhdrCmd.HasPhdrs = true;
663       else if (Tok == "FLAGS") {
664         expect("(");
665         // Passing 0 for the value of dot is a bit of a hack. It means that
666         // we accept expressions like ".|1".
667         PhdrCmd.Flags = readExpr()(0);
668         expect(")");
669       } else
670         setError("unexpected header attribute: " + Tok);
671     } while (!Error);
672   }
673 }
674 
675 void ScriptParser::readSearchDir() {
676   expect("(");
677   Config->SearchPaths.push_back(next());
678   expect(")");
679 }
680 
681 void ScriptParser::readSections() {
682   Opt.HasContents = true;
683   expect("{");
684   while (!Error && !skip("}")) {
685     StringRef Tok = next();
686     BaseCommand *Cmd;
687     if (peek() == "=" || peek() == "+=") {
688       Cmd = readAssignment(Tok);
689       expect(";");
690     } else if (Tok == "PROVIDE") {
691       Cmd = readProvide(false);
692     } else if (Tok == "PROVIDE_HIDDEN") {
693       Cmd = readProvide(true);
694     } else if (Tok == "ASSERT") {
695       Cmd = new AssertCommand(readAssert());
696     } else {
697       Cmd = readOutputSectionDescription(Tok);
698     }
699     Opt.Commands.emplace_back(Cmd);
700   }
701 }
702 
703 static int precedence(StringRef Op) {
704   return StringSwitch<int>(Op)
705       .Case("*", 4)
706       .Case("/", 4)
707       .Case("+", 3)
708       .Case("-", 3)
709       .Case("<", 2)
710       .Case(">", 2)
711       .Case(">=", 2)
712       .Case("<=", 2)
713       .Case("==", 2)
714       .Case("!=", 2)
715       .Case("&", 1)
716       .Default(-1);
717 }
718 
719 std::vector<StringRef> ScriptParser::readInputFilePatterns() {
720   std::vector<StringRef> V;
721   while (!Error && !skip(")"))
722     V.push_back(next());
723   return V;
724 }
725 
726 SortKind ScriptParser::readSortKind() {
727   if (skip("SORT") || skip("SORT_BY_NAME"))
728     return SortByName;
729   if (skip("SORT_BY_ALIGNMENT"))
730     return SortByAlignment;
731   return SortNone;
732 }
733 
734 InputSectionDescription *ScriptParser::readInputSectionRules() {
735   auto *Cmd = new InputSectionDescription;
736   Cmd->FilePattern = next();
737   expect("(");
738 
739   // Read EXCLUDE_FILE().
740   if (skip("EXCLUDE_FILE")) {
741     expect("(");
742     while (!Error && !skip(")"))
743       Cmd->ExcludedFiles.push_back(next());
744   }
745 
746   // Read SORT().
747   if (SortKind K1 = readSortKind()) {
748     Cmd->SortOuter = K1;
749     expect("(");
750     if (SortKind K2 = readSortKind()) {
751       Cmd->SortInner = K2;
752       expect("(");
753       Cmd->SectionPatterns = readInputFilePatterns();
754       expect(")");
755     } else {
756       Cmd->SectionPatterns = readInputFilePatterns();
757     }
758     expect(")");
759     return Cmd;
760   }
761 
762   Cmd->SectionPatterns = readInputFilePatterns();
763   return Cmd;
764 }
765 
766 InputSectionDescription *ScriptParser::readInputSectionDescription() {
767   // Input section wildcard can be surrounded by KEEP.
768   // https://sourceware.org/binutils/docs/ld/Input-Section-Keep.html#Input-Section-Keep
769   if (skip("KEEP")) {
770     expect("(");
771     InputSectionDescription *Cmd = readInputSectionRules();
772     expect(")");
773     Opt.KeptSections.insert(Opt.KeptSections.end(),
774                             Cmd->SectionPatterns.begin(),
775                             Cmd->SectionPatterns.end());
776     return Cmd;
777   }
778   return readInputSectionRules();
779 }
780 
781 Expr ScriptParser::readAlign() {
782   expect("(");
783   Expr E = readExpr();
784   expect(")");
785   return E;
786 }
787 
788 void ScriptParser::readSort() {
789   expect("(");
790   expect("CONSTRUCTORS");
791   expect(")");
792 }
793 
794 Expr ScriptParser::readAssert() {
795   expect("(");
796   Expr E = readExpr();
797   expect(",");
798   StringRef Msg = next();
799   expect(")");
800   return [=](uint64_t Dot) {
801     uint64_t V = E(Dot);
802     if (!V)
803       error(Msg);
804     return V;
805   };
806 }
807 
808 OutputSectionCommand *
809 ScriptParser::readOutputSectionDescription(StringRef OutSec) {
810   OutputSectionCommand *Cmd = new OutputSectionCommand(OutSec);
811 
812   // Read an address expression.
813   // https://sourceware.org/binutils/docs/ld/Output-Section-Address.html#Output-Section-Address
814   if (peek() != ":")
815     Cmd->AddrExpr = readExpr();
816 
817   expect(":");
818 
819   if (skip("ALIGN"))
820     Cmd->AlignExpr = readAlign();
821 
822   // Parse constraints.
823   if (skip("ONLY_IF_RO"))
824     Cmd->Constraint = ConstraintKind::ReadOnly;
825   if (skip("ONLY_IF_RW"))
826     Cmd->Constraint = ConstraintKind::ReadWrite;
827   expect("{");
828 
829   while (!Error && !skip("}")) {
830     if (peek().startswith("*") || peek() == "KEEP") {
831       Cmd->Commands.emplace_back(readInputSectionDescription());
832       continue;
833     }
834     if (skip("SORT")) {
835       readSort();
836       continue;
837     }
838     setError("unknown command " + peek());
839   }
840   Cmd->Phdrs = readOutputSectionPhdrs();
841   Cmd->Filler = readOutputSectionFiller();
842   return Cmd;
843 }
844 
845 std::vector<uint8_t> ScriptParser::readOutputSectionFiller() {
846   StringRef Tok = peek();
847   if (!Tok.startswith("="))
848     return {};
849   next();
850 
851   // Read a hexstring of arbitrary length.
852   if (Tok.startswith("=0x"))
853     return parseHex(Tok.substr(3));
854 
855   // Read a decimal or octal value as a big-endian 32 bit value.
856   // Why do this? I don't know, but that's what gold does.
857   uint32_t V;
858   if (Tok.substr(1).getAsInteger(0, V)) {
859     setError("invalid filler expression: " + Tok);
860     return {};
861   }
862   return { uint8_t(V >> 24), uint8_t(V >> 16), uint8_t(V >> 8), uint8_t(V) };
863 }
864 
865 SymbolAssignment *ScriptParser::readProvide(bool Hidden) {
866   expect("(");
867   SymbolAssignment *Cmd = readAssignment(next());
868   Cmd->Provide = true;
869   Cmd->Hidden = Hidden;
870   expect(")");
871   expect(";");
872   return Cmd;
873 }
874 
875 static uint64_t getSymbolValue(StringRef S, uint64_t Dot) {
876   if (S == ".")
877     return Dot;
878 
879   switch (Config->EKind) {
880   case ELF32LEKind:
881     if (SymbolBody *B = Symtab<ELF32LE>::X->find(S))
882       return B->getVA<ELF32LE>();
883     break;
884   case ELF32BEKind:
885     if (SymbolBody *B = Symtab<ELF32BE>::X->find(S))
886       return B->getVA<ELF32BE>();
887     break;
888   case ELF64LEKind:
889     if (SymbolBody *B = Symtab<ELF64LE>::X->find(S))
890       return B->getVA<ELF64LE>();
891     break;
892   case ELF64BEKind:
893     if (SymbolBody *B = Symtab<ELF64BE>::X->find(S))
894       return B->getVA<ELF64BE>();
895     break;
896   default:
897     llvm_unreachable("unsupported target");
898   }
899   error("symbol not found: " + S);
900   return 0;
901 }
902 
903 static uint64_t getSectionSize(StringRef Name) {
904   switch (Config->EKind) {
905   case ELF32LEKind:
906     return Script<ELF32LE>::X->getOutputSectionSize(Name);
907   case ELF32BEKind:
908     return Script<ELF32BE>::X->getOutputSectionSize(Name);
909   case ELF64LEKind:
910     return Script<ELF64LE>::X->getOutputSectionSize(Name);
911   case ELF64BEKind:
912     return Script<ELF64BE>::X->getOutputSectionSize(Name);
913   default:
914     llvm_unreachable("unsupported target");
915   }
916 }
917 
918 SymbolAssignment *ScriptParser::readAssignment(StringRef Name) {
919   StringRef Op = next();
920   assert(Op == "=" || Op == "+=");
921   Expr E = readExpr();
922   if (Op == "+=")
923     E = [=](uint64_t Dot) { return getSymbolValue(Name, Dot) + E(Dot); };
924   return new SymbolAssignment(Name, E);
925 }
926 
927 // This is an operator-precedence parser to parse a linker
928 // script expression.
929 Expr ScriptParser::readExpr() { return readExpr1(readPrimary(), 0); }
930 
931 static Expr combine(StringRef Op, Expr L, Expr R) {
932   if (Op == "*")
933     return [=](uint64_t Dot) { return L(Dot) * R(Dot); };
934   if (Op == "/") {
935     return [=](uint64_t Dot) -> uint64_t {
936       uint64_t RHS = R(Dot);
937       if (RHS == 0) {
938         error("division by zero");
939         return 0;
940       }
941       return L(Dot) / RHS;
942     };
943   }
944   if (Op == "+")
945     return [=](uint64_t Dot) { return L(Dot) + R(Dot); };
946   if (Op == "-")
947     return [=](uint64_t Dot) { return L(Dot) - R(Dot); };
948   if (Op == "<")
949     return [=](uint64_t Dot) { return L(Dot) < R(Dot); };
950   if (Op == ">")
951     return [=](uint64_t Dot) { return L(Dot) > R(Dot); };
952   if (Op == ">=")
953     return [=](uint64_t Dot) { return L(Dot) >= R(Dot); };
954   if (Op == "<=")
955     return [=](uint64_t Dot) { return L(Dot) <= R(Dot); };
956   if (Op == "==")
957     return [=](uint64_t Dot) { return L(Dot) == R(Dot); };
958   if (Op == "!=")
959     return [=](uint64_t Dot) { return L(Dot) != R(Dot); };
960   if (Op == "&")
961     return [=](uint64_t Dot) { return L(Dot) & R(Dot); };
962   llvm_unreachable("invalid operator");
963 }
964 
965 // This is a part of the operator-precedence parser. This function
966 // assumes that the remaining token stream starts with an operator.
967 Expr ScriptParser::readExpr1(Expr Lhs, int MinPrec) {
968   while (!atEOF() && !Error) {
969     // Read an operator and an expression.
970     StringRef Op1 = peek();
971     if (Op1 == "?")
972       return readTernary(Lhs);
973     if (precedence(Op1) < MinPrec)
974       break;
975     next();
976     Expr Rhs = readPrimary();
977 
978     // Evaluate the remaining part of the expression first if the
979     // next operator has greater precedence than the previous one.
980     // For example, if we have read "+" and "3", and if the next
981     // operator is "*", then we'll evaluate 3 * ... part first.
982     while (!atEOF()) {
983       StringRef Op2 = peek();
984       if (precedence(Op2) <= precedence(Op1))
985         break;
986       Rhs = readExpr1(Rhs, precedence(Op2));
987     }
988 
989     Lhs = combine(Op1, Lhs, Rhs);
990   }
991   return Lhs;
992 }
993 
994 uint64_t static getConstant(StringRef S) {
995   if (S == "COMMONPAGESIZE" || S == "MAXPAGESIZE")
996     return Target->PageSize;
997   error("unknown constant: " + S);
998   return 0;
999 }
1000 
1001 Expr ScriptParser::readPrimary() {
1002   StringRef Tok = next();
1003 
1004   if (Tok == "(") {
1005     Expr E = readExpr();
1006     expect(")");
1007     return E;
1008   }
1009 
1010   // Built-in functions are parsed here.
1011   // https://sourceware.org/binutils/docs/ld/Builtin-Functions.html.
1012   if (Tok == "ASSERT")
1013     return readAssert();
1014   if (Tok == "ALIGN") {
1015     expect("(");
1016     Expr E = readExpr();
1017     expect(")");
1018     return [=](uint64_t Dot) { return alignTo(Dot, E(Dot)); };
1019   }
1020   if (Tok == "CONSTANT") {
1021     expect("(");
1022     StringRef Tok = next();
1023     expect(")");
1024     return [=](uint64_t Dot) { return getConstant(Tok); };
1025   }
1026   if (Tok == "SEGMENT_START") {
1027     expect("(");
1028     next();
1029     expect(",");
1030     uint64_t Val;
1031     next().getAsInteger(0, Val);
1032     expect(")");
1033     return [=](uint64_t Dot) { return Val; };
1034   }
1035   if (Tok == "DATA_SEGMENT_ALIGN") {
1036     expect("(");
1037     Expr E = readExpr();
1038     expect(",");
1039     readExpr();
1040     expect(")");
1041     return [=](uint64_t Dot) { return alignTo(Dot, E(Dot)); };
1042   }
1043   if (Tok == "DATA_SEGMENT_END") {
1044     expect("(");
1045     expect(".");
1046     expect(")");
1047     return [](uint64_t Dot) { return Dot; };
1048   }
1049   // GNU linkers implements more complicated logic to handle
1050   // DATA_SEGMENT_RELRO_END. We instead ignore the arguments and just align to
1051   // the next page boundary for simplicity.
1052   if (Tok == "DATA_SEGMENT_RELRO_END") {
1053     expect("(");
1054     next();
1055     expect(",");
1056     readExpr();
1057     expect(")");
1058     return [](uint64_t Dot) { return alignTo(Dot, Target->PageSize); };
1059   }
1060   if (Tok == "SIZEOF") {
1061     expect("(");
1062     StringRef Name = next();
1063     expect(")");
1064     return [=](uint64_t Dot) { return getSectionSize(Name); };
1065   }
1066 
1067   // Parse a symbol name or a number literal.
1068   uint64_t V = 0;
1069   if (Tok.getAsInteger(0, V)) {
1070     if (Tok != "." && !isValidCIdentifier(Tok))
1071       setError("malformed number: " + Tok);
1072     return [=](uint64_t Dot) { return getSymbolValue(Tok, Dot); };
1073   }
1074   return [=](uint64_t Dot) { return V; };
1075 }
1076 
1077 Expr ScriptParser::readTernary(Expr Cond) {
1078   next();
1079   Expr L = readExpr();
1080   expect(":");
1081   Expr R = readExpr();
1082   return [=](uint64_t Dot) { return Cond(Dot) ? L(Dot) : R(Dot); };
1083 }
1084 
1085 std::vector<StringRef> ScriptParser::readOutputSectionPhdrs() {
1086   std::vector<StringRef> Phdrs;
1087   while (!Error && peek().startswith(":")) {
1088     StringRef Tok = next();
1089     Tok = (Tok.size() == 1) ? next() : Tok.substr(1);
1090     if (Tok.empty()) {
1091       setError("section header name is empty");
1092       break;
1093     }
1094     Phdrs.push_back(Tok);
1095   }
1096   return Phdrs;
1097 }
1098 
1099 unsigned ScriptParser::readPhdrType() {
1100   StringRef Tok = next();
1101   unsigned Ret = StringSwitch<unsigned>(Tok)
1102       .Case("PT_NULL", PT_NULL)
1103       .Case("PT_LOAD", PT_LOAD)
1104       .Case("PT_DYNAMIC", PT_DYNAMIC)
1105       .Case("PT_INTERP", PT_INTERP)
1106       .Case("PT_NOTE", PT_NOTE)
1107       .Case("PT_SHLIB", PT_SHLIB)
1108       .Case("PT_PHDR", PT_PHDR)
1109       .Case("PT_TLS", PT_TLS)
1110       .Case("PT_GNU_EH_FRAME", PT_GNU_EH_FRAME)
1111       .Case("PT_GNU_STACK", PT_GNU_STACK)
1112       .Case("PT_GNU_RELRO", PT_GNU_RELRO)
1113       .Default(-1);
1114 
1115   if (Ret == (unsigned)-1) {
1116     setError("invalid program header type: " + Tok);
1117     return PT_NULL;
1118   }
1119   return Ret;
1120 }
1121 
1122 static bool isUnderSysroot(StringRef Path) {
1123   if (Config->Sysroot == "")
1124     return false;
1125   for (; !Path.empty(); Path = sys::path::parent_path(Path))
1126     if (sys::fs::equivalent(Config->Sysroot, Path))
1127       return true;
1128   return false;
1129 }
1130 
1131 // Entry point.
1132 void elf::readLinkerScript(MemoryBufferRef MB) {
1133   StringRef Path = MB.getBufferIdentifier();
1134   ScriptParser(MB.getBuffer(), isUnderSysroot(Path)).run();
1135 }
1136 
1137 template class elf::LinkerScript<ELF32LE>;
1138 template class elf::LinkerScript<ELF32BE>;
1139 template class elf::LinkerScript<ELF64LE>;
1140 template class elf::LinkerScript<ELF64BE>;
1141