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