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 template <class ELFT>
47 static void addRegular(SymbolAssignment *Cmd) {
48   Symbol *Sym = Symtab<ELFT>::X->addRegular(Cmd->Name, STB_GLOBAL, STV_DEFAULT);
49   Sym->Visibility = Cmd->Hidden ? STV_HIDDEN : STV_DEFAULT;
50   Cmd->Sym = Sym->body();
51 }
52 
53 template <class ELFT> static void addSynthetic(SymbolAssignment *Cmd) {
54   Symbol *Sym = Symtab<ELFT>::X->addSynthetic(
55       Cmd->Name, nullptr, 0, Cmd->Hidden ? STV_HIDDEN : STV_DEFAULT);
56   Cmd->Sym = Sym->body();
57 }
58 
59 // If a symbol was in PROVIDE(), we need to define it only when
60 // it is an undefined symbol.
61 template <class ELFT> static bool shouldDefine(SymbolAssignment *Cmd) {
62   if (Cmd->Name == ".")
63     return false;
64   if (!Cmd->Provide)
65     return true;
66   SymbolBody *B = Symtab<ELFT>::X->find(Cmd->Name);
67   return B && B->isUndefined();
68 }
69 
70 bool SymbolAssignment::classof(const BaseCommand *C) {
71   return C->Kind == AssignmentKind;
72 }
73 
74 bool OutputSectionCommand::classof(const BaseCommand *C) {
75   return C->Kind == OutputSectionKind;
76 }
77 
78 bool InputSectionDescription::classof(const BaseCommand *C) {
79   return C->Kind == InputSectionKind;
80 }
81 
82 bool AssertCommand::classof(const BaseCommand *C) {
83   return C->Kind == AssertKind;
84 }
85 
86 template <class ELFT> static bool isDiscarded(InputSectionBase<ELFT> *S) {
87   return !S || !S->Live;
88 }
89 
90 template <class ELFT> LinkerScript<ELFT>::LinkerScript() {}
91 template <class ELFT> LinkerScript<ELFT>::~LinkerScript() {}
92 
93 template <class ELFT>
94 bool LinkerScript<ELFT>::shouldKeep(InputSectionBase<ELFT> *S) {
95   for (Regex *Re : Opt.KeptSections)
96     if (Re->match(S->getSectionName()))
97       return true;
98   return false;
99 }
100 
101 static bool fileMatches(const InputSectionDescription *Desc,
102                         StringRef Filename) {
103   return const_cast<Regex &>(Desc->FileRe).match(Filename) &&
104          !const_cast<Regex &>(Desc->ExcludedFileRe).match(Filename);
105 }
106 
107 // Returns input sections filtered by given glob patterns.
108 template <class ELFT>
109 std::vector<InputSectionBase<ELFT> *>
110 LinkerScript<ELFT>::getInputSections(const InputSectionDescription *I) {
111   const Regex &Re = I->SectionRe;
112   std::vector<InputSectionBase<ELFT> *> Ret;
113   for (const std::unique_ptr<ObjectFile<ELFT>> &F :
114        Symtab<ELFT>::X->getObjectFiles()) {
115     if (fileMatches(I, sys::path::filename(F->getName())))
116       for (InputSectionBase<ELFT> *S : F->getSections())
117         if (!isDiscarded(S) && !S->OutSec &&
118             const_cast<Regex &>(Re).match(S->getSectionName()))
119           Ret.push_back(S);
120   }
121 
122   if (const_cast<Regex &>(Re).match("COMMON"))
123     Ret.push_back(CommonInputSection<ELFT>::X);
124   return Ret;
125 }
126 
127 template <class ELFT>
128 static bool compareName(InputSectionBase<ELFT> *A, InputSectionBase<ELFT> *B) {
129   return A->getSectionName() < B->getSectionName();
130 }
131 
132 template <class ELFT>
133 static bool compareAlignment(InputSectionBase<ELFT> *A,
134                              InputSectionBase<ELFT> *B) {
135   // ">" is not a mistake. Larger alignments are placed before smaller
136   // alignments in order to reduce the amount of padding necessary.
137   // This is compatible with GNU.
138   return A->Alignment > B->Alignment;
139 }
140 
141 template <class ELFT>
142 static std::function<bool(InputSectionBase<ELFT> *, InputSectionBase<ELFT> *)>
143 getComparator(SortKind K) {
144   if (K == SortByName)
145     return compareName<ELFT>;
146   return compareAlignment<ELFT>;
147 }
148 
149 template <class ELFT>
150 void LinkerScript<ELFT>::discard(OutputSectionCommand &Cmd) {
151   for (const std::unique_ptr<BaseCommand> &Base : Cmd.Commands) {
152     if (auto *Cmd = dyn_cast<InputSectionDescription>(Base.get())) {
153       for (InputSectionBase<ELFT> *S : getInputSections(Cmd)) {
154         S->Live = false;
155         reportDiscarded(S);
156       }
157     }
158   }
159 }
160 
161 static bool checkConstraint(uint64_t Flags, ConstraintKind Kind) {
162   bool RO = (Kind == ConstraintKind::ReadOnly);
163   bool RW = (Kind == ConstraintKind::ReadWrite);
164   bool Writable = Flags & SHF_WRITE;
165   return !((RO && Writable) || (RW && !Writable));
166 }
167 
168 template <class ELFT>
169 static bool matchConstraints(ArrayRef<InputSectionBase<ELFT> *> Sections,
170                              ConstraintKind Kind) {
171   if (Kind == ConstraintKind::NoConstraint)
172     return true;
173   return llvm::all_of(Sections, [=](InputSectionBase<ELFT> *Sec) {
174     return checkConstraint(Sec->getSectionHdr()->sh_flags, Kind);
175   });
176 }
177 
178 template <class ELFT>
179 std::vector<InputSectionBase<ELFT> *>
180 LinkerScript<ELFT>::createInputSectionList(OutputSectionCommand &OutCmd) {
181   std::vector<InputSectionBase<ELFT> *> Ret;
182   DenseSet<InputSectionBase<ELFT> *> SectionIndex;
183 
184   for (const std::unique_ptr<BaseCommand> &Base : OutCmd.Commands) {
185     if (auto *OutCmd = dyn_cast<SymbolAssignment>(Base.get())) {
186       if (shouldDefine<ELFT>(OutCmd))
187         addSynthetic<ELFT>(OutCmd);
188       OutCmd->GoesAfter = Ret.empty() ? nullptr : Ret.back();
189       continue;
190     }
191 
192     auto *Cmd = cast<InputSectionDescription>(Base.get());
193     std::vector<InputSectionBase<ELFT> *> V = getInputSections(Cmd);
194     if (!matchConstraints<ELFT>(V, OutCmd.Constraint))
195       continue;
196     if (Cmd->SortInner)
197       std::stable_sort(V.begin(), V.end(), getComparator<ELFT>(Cmd->SortInner));
198     if (Cmd->SortOuter)
199       std::stable_sort(V.begin(), V.end(), getComparator<ELFT>(Cmd->SortOuter));
200 
201     // Add all input sections corresponding to rule 'Cmd' to
202     // resulting vector. We do not add duplicate input sections.
203     for (InputSectionBase<ELFT> *S : V)
204       if (SectionIndex.insert(S).second)
205         Ret.push_back(S);
206   }
207   return Ret;
208 }
209 
210 template <class ELFT>
211 void LinkerScript<ELFT>::createAssignments() {
212   for (const std::unique_ptr<SymbolAssignment> &Cmd : Opt.Assignments) {
213     if (shouldDefine<ELFT>(Cmd.get()))
214       addRegular<ELFT>(Cmd.get());
215     if (Cmd->Sym)
216       cast<DefinedRegular<ELFT>>(Cmd->Sym)->Value = Cmd->Expression(0);
217   }
218 }
219 
220 template <class ELFT>
221 void LinkerScript<ELFT>::createSections(OutputSectionFactory<ELFT> &Factory) {
222   for (const std::unique_ptr<BaseCommand> &Base1 : Opt.Commands) {
223     if (auto *Cmd = dyn_cast<SymbolAssignment>(Base1.get())) {
224       if (shouldDefine<ELFT>(Cmd))
225         addRegular<ELFT>(Cmd);
226       continue;
227     }
228 
229     if (auto *Cmd = dyn_cast<OutputSectionCommand>(Base1.get())) {
230       if (Cmd->Name == "/DISCARD/") {
231         discard(*Cmd);
232         continue;
233       }
234 
235       std::vector<InputSectionBase<ELFT> *> V = createInputSectionList(*Cmd);
236       if (V.empty())
237         continue;
238 
239       OutputSectionBase<ELFT> *OutSec;
240       bool IsNew;
241       std::tie(OutSec, IsNew) = Factory.create(V.front(), Cmd->Name);
242       if (IsNew)
243         OutputSections->push_back(OutSec);
244 
245       uint32_t Subalign = Cmd->SubalignExpr ? Cmd->SubalignExpr(0) : 0;
246       for (InputSectionBase<ELFT> *Sec : V) {
247         if (Subalign)
248           Sec->Alignment = Subalign;
249         OutSec->addSection(Sec);
250       }
251     }
252   }
253 
254   // Add orphan sections.
255   for (const std::unique_ptr<ObjectFile<ELFT>> &F :
256        Symtab<ELFT>::X->getObjectFiles()) {
257     for (InputSectionBase<ELFT> *S : F->getSections()) {
258       if (isDiscarded(S) || S->OutSec)
259         continue;
260       OutputSectionBase<ELFT> *OutSec;
261       bool IsNew;
262       std::tie(OutSec, IsNew) = Factory.create(S, getOutputSectionName(S));
263       if (IsNew)
264         OutputSections->push_back(OutSec);
265       OutSec->addSection(S);
266     }
267   }
268 }
269 
270 // Linker script may define start and end symbols for special section types,
271 // like .got, .eh_frame_hdr, .eh_frame and others. Those sections are not a list
272 // of regular input input sections, therefore our way of defining symbols for
273 // regular sections will not work. The approach we use for special section types
274 // is not perfect - it handles only start and end symbols.
275 template <class ELFT>
276 void addStartEndSymbols(OutputSectionCommand *Cmd,
277                         OutputSectionBase<ELFT> *Sec) {
278   bool Start = true;
279   BaseCommand *PrevCmd = nullptr;
280 
281   for (std::unique_ptr<BaseCommand> &Base : Cmd->Commands) {
282     if (auto *AssignCmd = dyn_cast<SymbolAssignment>(Base.get())) {
283       if (auto *Sym = cast_or_null<DefinedSynthetic<ELFT>>(AssignCmd->Sym)) {
284         Sym->Section = Sec;
285         Sym->Value =
286             AssignCmd->Expression(Sec->getVA() + (Start ? 0 : Sec->getSize())) -
287             Sec->getVA();
288       }
289     } else {
290       if (!Start && isa<SymbolAssignment>(PrevCmd))
291         error("section '" + Sec->getName() +
292               "' supports only start and end symbols");
293       Start = false;
294     }
295     PrevCmd = Base.get();
296   }
297 }
298 
299 template <class ELFT>
300 void assignOffsets(OutputSectionCommand *Cmd, OutputSectionBase<ELFT> *Sec) {
301   auto *OutSec = dyn_cast<OutputSection<ELFT>>(Sec);
302   if (!OutSec) {
303     Sec->assignOffsets();
304     // This section is not regular output section. However linker script may
305     // have defined start/end symbols for it. This case is handled below.
306     addStartEndSymbols(Cmd, Sec);
307     return;
308   }
309   typedef typename ELFT::uint uintX_t;
310   uintX_t Off = 0;
311   auto ItCmd = Cmd->Commands.begin();
312 
313   // Assigns values to all symbols following the given
314   // input section 'D' in output section 'Sec'. When symbols
315   // are in the beginning of output section the value of 'D'
316   // is nullptr.
317   auto AssignSuccessors = [&](InputSectionData *D) {
318     for (; ItCmd != Cmd->Commands.end(); ++ItCmd) {
319       auto *AssignCmd = dyn_cast<SymbolAssignment>(ItCmd->get());
320       if (!AssignCmd)
321         continue;
322       if (D != AssignCmd->GoesAfter)
323         break;
324 
325       uintX_t Value = AssignCmd->Expression(Sec->getVA() + Off) - Sec->getVA();
326       if (AssignCmd->Name == ".") {
327         // Update to location counter means update to section size.
328         Off = Value;
329         Sec->setSize(Off);
330         continue;
331       }
332 
333       if (DefinedSynthetic<ELFT> *Sym =
334               cast_or_null<DefinedSynthetic<ELFT>>(AssignCmd->Sym)) {
335         Sym->Section = OutSec;
336         Sym->Value = Value;
337       }
338     }
339   };
340 
341   AssignSuccessors(nullptr);
342   for (InputSection<ELFT> *I : OutSec->Sections) {
343     Off = alignTo(Off, I->Alignment);
344     I->OutSecOff = Off;
345     Off += I->getSize();
346     // Update section size inside for-loop, so that SIZEOF
347     // works correctly in the case below:
348     // .foo { *(.aaa) a = SIZEOF(.foo); *(.bbb) }
349     Sec->setSize(Off);
350     // Add symbols following current input section.
351     AssignSuccessors(I);
352   }
353 }
354 
355 template <class ELFT>
356 static OutputSectionBase<ELFT> *
357 findSection(OutputSectionCommand &Cmd,
358             ArrayRef<OutputSectionBase<ELFT> *> Sections) {
359   for (OutputSectionBase<ELFT> *Sec : Sections) {
360     if (Sec->getName() != Cmd.Name)
361       continue;
362     if (checkConstraint(Sec->getFlags(), Cmd.Constraint))
363       return Sec;
364   }
365   return nullptr;
366 }
367 
368 template <class ELFT> void LinkerScript<ELFT>::assignAddresses() {
369   // Orphan sections are sections present in the input files which
370   // are not explicitly placed into the output file by the linker script.
371   // We place orphan sections at end of file.
372   // Other linkers places them using some heuristics as described in
373   // https://sourceware.org/binutils/docs/ld/Orphan-Sections.html#Orphan-Sections.
374   for (OutputSectionBase<ELFT> *Sec : *OutputSections) {
375     StringRef Name = Sec->getName();
376     if (getSectionIndex(Name) == INT_MAX)
377       Opt.Commands.push_back(llvm::make_unique<OutputSectionCommand>(Name));
378   }
379 
380   // Assign addresses as instructed by linker script SECTIONS sub-commands.
381   Dot = getHeaderSize();
382   uintX_t MinVA = std::numeric_limits<uintX_t>::max();
383   uintX_t ThreadBssOffset = 0;
384 
385   for (const std::unique_ptr<BaseCommand> &Base : Opt.Commands) {
386     if (auto *Cmd = dyn_cast<SymbolAssignment>(Base.get())) {
387       if (Cmd->Name == ".") {
388         Dot = Cmd->Expression(Dot);
389       } else if (Cmd->Sym) {
390         cast<DefinedRegular<ELFT>>(Cmd->Sym)->Value = Cmd->Expression(Dot);
391       }
392       continue;
393     }
394 
395     if (auto *Cmd = dyn_cast<AssertCommand>(Base.get())) {
396       Cmd->Expression(Dot);
397       continue;
398     }
399 
400     auto *Cmd = cast<OutputSectionCommand>(Base.get());
401     OutputSectionBase<ELFT> *Sec = findSection<ELFT>(*Cmd, *OutputSections);
402     if (!Sec)
403       continue;
404 
405     if (Cmd->AddrExpr)
406       Dot = Cmd->AddrExpr(Dot);
407 
408     if (Cmd->AlignExpr)
409       Sec->updateAlignment(Cmd->AlignExpr(Dot));
410 
411     if ((Sec->getFlags() & SHF_TLS) && Sec->getType() == SHT_NOBITS) {
412       uintX_t TVA = Dot + ThreadBssOffset;
413       TVA = alignTo(TVA, Sec->getAlignment());
414       Sec->setVA(TVA);
415       assignOffsets(Cmd, Sec);
416       ThreadBssOffset = TVA - Dot + Sec->getSize();
417       continue;
418     }
419 
420     if (!(Sec->getFlags() & SHF_ALLOC)) {
421       assignOffsets(Cmd, Sec);
422       continue;
423     }
424 
425     Dot = alignTo(Dot, Sec->getAlignment());
426     Sec->setVA(Dot);
427     assignOffsets(Cmd, Sec);
428     MinVA = std::min(MinVA, Dot);
429     Dot += Sec->getSize();
430   }
431 
432   // ELF and Program headers need to be right before the first section in
433   // memory. Set their addresses accordingly.
434   MinVA = alignDown(MinVA - Out<ELFT>::ElfHeader->getSize() -
435                         Out<ELFT>::ProgramHeaders->getSize(),
436                     Target->PageSize);
437   Out<ELFT>::ElfHeader->setVA(MinVA);
438   Out<ELFT>::ProgramHeaders->setVA(Out<ELFT>::ElfHeader->getSize() + MinVA);
439 }
440 
441 // Creates program headers as instructed by PHDRS linker script command.
442 template <class ELFT>
443 std::vector<PhdrEntry<ELFT>> LinkerScript<ELFT>::createPhdrs() {
444   std::vector<PhdrEntry<ELFT>> Ret;
445 
446   // Process PHDRS and FILEHDR keywords because they are not
447   // real output sections and cannot be added in the following loop.
448   for (const PhdrsCommand &Cmd : Opt.PhdrsCommands) {
449     Ret.emplace_back(Cmd.Type, Cmd.Flags == UINT_MAX ? PF_R : Cmd.Flags);
450     PhdrEntry<ELFT> &Phdr = Ret.back();
451 
452     if (Cmd.HasFilehdr)
453       Phdr.add(Out<ELFT>::ElfHeader);
454     if (Cmd.HasPhdrs)
455       Phdr.add(Out<ELFT>::ProgramHeaders);
456   }
457 
458   // Add output sections to program headers.
459   PhdrEntry<ELFT> *Load = nullptr;
460   uintX_t Flags = PF_R;
461   for (OutputSectionBase<ELFT> *Sec : *OutputSections) {
462     if (!(Sec->getFlags() & SHF_ALLOC))
463       break;
464 
465     std::vector<size_t> PhdrIds = getPhdrIndices(Sec->getName());
466     if (!PhdrIds.empty()) {
467       // Assign headers specified by linker script
468       for (size_t Id : PhdrIds) {
469         Ret[Id].add(Sec);
470         if (Opt.PhdrsCommands[Id].Flags == UINT_MAX)
471           Ret[Id].H.p_flags |= Sec->getPhdrFlags();
472       }
473     } else {
474       // If we have no load segment or flags've changed then we want new load
475       // segment.
476       uintX_t NewFlags = Sec->getPhdrFlags();
477       if (Load == nullptr || Flags != NewFlags) {
478         Load = &*Ret.emplace(Ret.end(), PT_LOAD, NewFlags);
479         Flags = NewFlags;
480       }
481       Load->add(Sec);
482     }
483   }
484   return Ret;
485 }
486 
487 template <class ELFT> bool LinkerScript<ELFT>::ignoreInterpSection() {
488   // Ignore .interp section in case we have PHDRS specification
489   // and PT_INTERP isn't listed.
490   return !Opt.PhdrsCommands.empty() &&
491          llvm::find_if(Opt.PhdrsCommands, [](const PhdrsCommand &Cmd) {
492            return Cmd.Type == PT_INTERP;
493          }) == Opt.PhdrsCommands.end();
494 }
495 
496 template <class ELFT>
497 ArrayRef<uint8_t> LinkerScript<ELFT>::getFiller(StringRef Name) {
498   for (const std::unique_ptr<BaseCommand> &Base : Opt.Commands)
499     if (auto *Cmd = dyn_cast<OutputSectionCommand>(Base.get()))
500       if (Cmd->Name == Name)
501         return Cmd->Filler;
502   return {};
503 }
504 
505 template <class ELFT> Expr LinkerScript<ELFT>::getLma(StringRef Name) {
506   for (const std::unique_ptr<BaseCommand> &Base : Opt.Commands)
507     if (auto *Cmd = dyn_cast<OutputSectionCommand>(Base.get()))
508       if (Cmd->LmaExpr && Cmd->Name == Name)
509         return Cmd->LmaExpr;
510   return {};
511 }
512 
513 // Returns the index of the given section name in linker script
514 // SECTIONS commands. Sections are laid out as the same order as they
515 // were in the script. If a given name did not appear in the script,
516 // it returns INT_MAX, so that it will be laid out at end of file.
517 template <class ELFT> int LinkerScript<ELFT>::getSectionIndex(StringRef Name) {
518   int I = 0;
519   for (std::unique_ptr<BaseCommand> &Base : Opt.Commands) {
520     if (auto *Cmd = dyn_cast<OutputSectionCommand>(Base.get()))
521       if (Cmd->Name == Name)
522         return I;
523     ++I;
524   }
525   return INT_MAX;
526 }
527 
528 // A compartor to sort output sections. Returns -1 or 1 if
529 // A or B are mentioned in linker script. Otherwise, returns 0.
530 template <class ELFT>
531 int LinkerScript<ELFT>::compareSections(StringRef A, StringRef B) {
532   int I = getSectionIndex(A);
533   int J = getSectionIndex(B);
534   if (I == INT_MAX && J == INT_MAX)
535     return 0;
536   return I < J ? -1 : 1;
537 }
538 
539 template <class ELFT> bool LinkerScript<ELFT>::hasPhdrsCommands() {
540   return !Opt.PhdrsCommands.empty();
541 }
542 
543 template <class ELFT>
544 typename ELFT::uint
545 LinkerScript<ELFT>::getOutputSectionAddress(StringRef Name) {
546   for (OutputSectionBase<ELFT> *Sec : *OutputSections)
547     if (Sec->getName() == Name)
548       return Sec->getVA();
549   error("undefined section " + Name);
550   return 0;
551 }
552 
553 template <class ELFT>
554 typename ELFT::uint LinkerScript<ELFT>::getOutputSectionSize(StringRef Name) {
555   for (OutputSectionBase<ELFT> *Sec : *OutputSections)
556     if (Sec->getName() == Name)
557       return Sec->getSize();
558   error("undefined section " + Name);
559   return 0;
560 }
561 
562 template <class ELFT>
563 typename ELFT::uint LinkerScript<ELFT>::getHeaderSize() {
564   return Out<ELFT>::ElfHeader->getSize() + Out<ELFT>::ProgramHeaders->getSize();
565 }
566 
567 // Returns indices of ELF headers containing specific section, identified
568 // by Name. Each index is a zero based number of ELF header listed within
569 // PHDRS {} script block.
570 template <class ELFT>
571 std::vector<size_t> LinkerScript<ELFT>::getPhdrIndices(StringRef SectionName) {
572   for (const std::unique_ptr<BaseCommand> &Base : Opt.Commands) {
573     auto *Cmd = dyn_cast<OutputSectionCommand>(Base.get());
574     if (!Cmd || Cmd->Name != SectionName)
575       continue;
576 
577     std::vector<size_t> Ret;
578     for (StringRef PhdrName : Cmd->Phdrs)
579       Ret.push_back(getPhdrIndex(PhdrName));
580     return Ret;
581   }
582   return {};
583 }
584 
585 template <class ELFT>
586 size_t LinkerScript<ELFT>::getPhdrIndex(StringRef PhdrName) {
587   size_t I = 0;
588   for (PhdrsCommand &Cmd : Opt.PhdrsCommands) {
589     if (Cmd.Name == PhdrName)
590       return I;
591     ++I;
592   }
593   error("section header '" + PhdrName + "' is not listed in PHDRS");
594   return 0;
595 }
596 
597 class elf::ScriptParser : public ScriptParserBase {
598   typedef void (ScriptParser::*Handler)();
599 
600 public:
601   ScriptParser(StringRef S, bool B) : ScriptParserBase(S), IsUnderSysroot(B) {}
602 
603   void readLinkerScript();
604   void readVersionScript();
605 
606 private:
607   void addFile(StringRef Path);
608 
609   void readAsNeeded();
610   void readEntry();
611   void readExtern();
612   void readGroup();
613   void readInclude();
614   void readOutput();
615   void readOutputArch();
616   void readOutputFormat();
617   void readPhdrs();
618   void readSearchDir();
619   void readSections();
620   void readVersion();
621   void readVersionScriptCommand();
622 
623   SymbolAssignment *readAssignment(StringRef Name);
624   std::vector<uint8_t> readFill();
625   OutputSectionCommand *readOutputSectionDescription(StringRef OutSec);
626   std::vector<uint8_t> readOutputSectionFiller(StringRef Tok);
627   std::vector<StringRef> readOutputSectionPhdrs();
628   InputSectionDescription *readInputSectionDescription(StringRef Tok);
629   Regex readFilePatterns();
630   InputSectionDescription *readInputSectionRules(StringRef FilePattern);
631   unsigned readPhdrType();
632   SortKind readSortKind();
633   SymbolAssignment *readProvideHidden(bool Provide, bool Hidden);
634   SymbolAssignment *readProvideOrAssignment(StringRef Tok);
635   void readSort();
636   Expr readAssert();
637 
638   Expr readExpr();
639   Expr readExpr1(Expr Lhs, int MinPrec);
640   Expr readPrimary();
641   Expr readTernary(Expr Cond);
642   Expr readParenExpr();
643 
644   // For parsing version script.
645   void readExtern(std::vector<SymbolVersion> *Globals);
646   void readVersionDeclaration(StringRef VerStr);
647   void readGlobal(StringRef VerStr);
648   void readLocal();
649 
650   ScriptConfiguration &Opt = *ScriptConfig;
651   StringSaver Saver = {ScriptConfig->Alloc};
652   bool IsUnderSysroot;
653 };
654 
655 void ScriptParser::readVersionScript() {
656   readVersionScriptCommand();
657   if (!atEOF())
658     setError("EOF expected, but got " + next());
659 }
660 
661 void ScriptParser::readVersionScriptCommand() {
662   if (skip("{")) {
663     readVersionDeclaration("");
664     return;
665   }
666 
667   while (!atEOF() && !Error && peek() != "}") {
668     StringRef VerStr = next();
669     if (VerStr == "{") {
670       setError("anonymous version definition is used in "
671                "combination with other version definitions");
672       return;
673     }
674     expect("{");
675     readVersionDeclaration(VerStr);
676   }
677 }
678 
679 void ScriptParser::readVersion() {
680   expect("{");
681   readVersionScriptCommand();
682   expect("}");
683 }
684 
685 void ScriptParser::readLinkerScript() {
686   while (!atEOF()) {
687     StringRef Tok = next();
688     if (Tok == ";")
689       continue;
690 
691     if (Tok == "ENTRY") {
692       readEntry();
693     } else if (Tok == "EXTERN") {
694       readExtern();
695     } else if (Tok == "GROUP" || Tok == "INPUT") {
696       readGroup();
697     } else if (Tok == "INCLUDE") {
698       readInclude();
699     } else if (Tok == "OUTPUT") {
700       readOutput();
701     } else if (Tok == "OUTPUT_ARCH") {
702       readOutputArch();
703     } else if (Tok == "OUTPUT_FORMAT") {
704       readOutputFormat();
705     } else if (Tok == "PHDRS") {
706       readPhdrs();
707     } else if (Tok == "SEARCH_DIR") {
708       readSearchDir();
709     } else if (Tok == "SECTIONS") {
710       readSections();
711     } else if (Tok == "VERSION") {
712       readVersion();
713     } else if (SymbolAssignment *Cmd = readProvideOrAssignment(Tok)) {
714       if (Opt.HasContents)
715         Opt.Commands.emplace_back(Cmd);
716       else
717         Opt.Assignments.emplace_back(Cmd);
718     } else {
719       setError("unknown directive: " + Tok);
720     }
721   }
722 }
723 
724 void ScriptParser::addFile(StringRef S) {
725   if (IsUnderSysroot && S.startswith("/")) {
726     SmallString<128> Path;
727     (Config->Sysroot + S).toStringRef(Path);
728     if (sys::fs::exists(Path)) {
729       Driver->addFile(Saver.save(Path.str()));
730       return;
731     }
732   }
733 
734   if (sys::path::is_absolute(S)) {
735     Driver->addFile(S);
736   } else if (S.startswith("=")) {
737     if (Config->Sysroot.empty())
738       Driver->addFile(S.substr(1));
739     else
740       Driver->addFile(Saver.save(Config->Sysroot + "/" + S.substr(1)));
741   } else if (S.startswith("-l")) {
742     Driver->addLibrary(S.substr(2));
743   } else if (sys::fs::exists(S)) {
744     Driver->addFile(S);
745   } else {
746     std::string Path = findFromSearchPaths(S);
747     if (Path.empty())
748       setError("unable to find " + S);
749     else
750       Driver->addFile(Saver.save(Path));
751   }
752 }
753 
754 void ScriptParser::readAsNeeded() {
755   expect("(");
756   bool Orig = Config->AsNeeded;
757   Config->AsNeeded = true;
758   while (!Error && !skip(")"))
759     addFile(next());
760   Config->AsNeeded = Orig;
761 }
762 
763 void ScriptParser::readEntry() {
764   // -e <symbol> takes predecence over ENTRY(<symbol>).
765   expect("(");
766   StringRef Tok = next();
767   if (Config->Entry.empty())
768     Config->Entry = Tok;
769   expect(")");
770 }
771 
772 void ScriptParser::readExtern() {
773   expect("(");
774   while (!Error && !skip(")"))
775     Config->Undefined.push_back(next());
776 }
777 
778 void ScriptParser::readGroup() {
779   expect("(");
780   while (!Error && !skip(")")) {
781     StringRef Tok = next();
782     if (Tok == "AS_NEEDED")
783       readAsNeeded();
784     else
785       addFile(Tok);
786   }
787 }
788 
789 void ScriptParser::readInclude() {
790   StringRef Tok = next();
791   auto MBOrErr = MemoryBuffer::getFile(Tok);
792   if (!MBOrErr) {
793     setError("cannot open " + Tok);
794     return;
795   }
796   std::unique_ptr<MemoryBuffer> &MB = *MBOrErr;
797   StringRef S = Saver.save(MB->getMemBufferRef().getBuffer());
798   std::vector<StringRef> V = tokenize(S);
799   Tokens.insert(Tokens.begin() + Pos, V.begin(), V.end());
800 }
801 
802 void ScriptParser::readOutput() {
803   // -o <file> takes predecence over OUTPUT(<file>).
804   expect("(");
805   StringRef Tok = next();
806   if (Config->OutputFile.empty())
807     Config->OutputFile = Tok;
808   expect(")");
809 }
810 
811 void ScriptParser::readOutputArch() {
812   // Error checking only for now.
813   expect("(");
814   next();
815   expect(")");
816 }
817 
818 void ScriptParser::readOutputFormat() {
819   // Error checking only for now.
820   expect("(");
821   next();
822   StringRef Tok = next();
823   if (Tok == ")")
824    return;
825   if (Tok != ",") {
826     setError("unexpected token: " + Tok);
827     return;
828   }
829   next();
830   expect(",");
831   next();
832   expect(")");
833 }
834 
835 void ScriptParser::readPhdrs() {
836   expect("{");
837   while (!Error && !skip("}")) {
838     StringRef Tok = next();
839     Opt.PhdrsCommands.push_back({Tok, PT_NULL, false, false, UINT_MAX});
840     PhdrsCommand &PhdrCmd = Opt.PhdrsCommands.back();
841 
842     PhdrCmd.Type = readPhdrType();
843     do {
844       Tok = next();
845       if (Tok == ";")
846         break;
847       if (Tok == "FILEHDR")
848         PhdrCmd.HasFilehdr = true;
849       else if (Tok == "PHDRS")
850         PhdrCmd.HasPhdrs = true;
851       else if (Tok == "FLAGS") {
852         expect("(");
853         // Passing 0 for the value of dot is a bit of a hack. It means that
854         // we accept expressions like ".|1".
855         PhdrCmd.Flags = readExpr()(0);
856         expect(")");
857       } else
858         setError("unexpected header attribute: " + Tok);
859     } while (!Error);
860   }
861 }
862 
863 void ScriptParser::readSearchDir() {
864   expect("(");
865   if (!Config->Nostdlib)
866     Config->SearchPaths.push_back(next());
867   expect(")");
868 }
869 
870 void ScriptParser::readSections() {
871   Opt.HasContents = true;
872   expect("{");
873   while (!Error && !skip("}")) {
874     StringRef Tok = next();
875     BaseCommand *Cmd = readProvideOrAssignment(Tok);
876     if (!Cmd) {
877       if (Tok == "ASSERT")
878         Cmd = new AssertCommand(readAssert());
879       else
880         Cmd = readOutputSectionDescription(Tok);
881     }
882     Opt.Commands.emplace_back(Cmd);
883   }
884 }
885 
886 static int precedence(StringRef Op) {
887   return StringSwitch<int>(Op)
888       .Case("*", 4)
889       .Case("/", 4)
890       .Case("+", 3)
891       .Case("-", 3)
892       .Case("<", 2)
893       .Case(">", 2)
894       .Case(">=", 2)
895       .Case("<=", 2)
896       .Case("==", 2)
897       .Case("!=", 2)
898       .Case("&", 1)
899       .Case("|", 1)
900       .Default(-1);
901 }
902 
903 Regex ScriptParser::readFilePatterns() {
904   std::vector<StringRef> V;
905   while (!Error && !skip(")"))
906     V.push_back(next());
907   return compileGlobPatterns(V);
908 }
909 
910 SortKind ScriptParser::readSortKind() {
911   if (skip("SORT") || skip("SORT_BY_NAME"))
912     return SortByName;
913   if (skip("SORT_BY_ALIGNMENT"))
914     return SortByAlignment;
915   return SortNone;
916 }
917 
918 InputSectionDescription *
919 ScriptParser::readInputSectionRules(StringRef FilePattern) {
920   auto *Cmd = new InputSectionDescription(FilePattern);
921   expect("(");
922 
923   // Read EXCLUDE_FILE().
924   if (skip("EXCLUDE_FILE")) {
925     expect("(");
926     Cmd->ExcludedFileRe = readFilePatterns();
927   }
928 
929   // Read SORT().
930   if (SortKind K1 = readSortKind()) {
931     Cmd->SortOuter = K1;
932     expect("(");
933     if (SortKind K2 = readSortKind()) {
934       Cmd->SortInner = K2;
935       expect("(");
936       Cmd->SectionRe = readFilePatterns();
937       expect(")");
938     } else {
939       Cmd->SectionRe = readFilePatterns();
940     }
941     expect(")");
942     return Cmd;
943   }
944 
945   Cmd->SectionRe = readFilePatterns();
946   return Cmd;
947 }
948 
949 InputSectionDescription *
950 ScriptParser::readInputSectionDescription(StringRef Tok) {
951   // Input section wildcard can be surrounded by KEEP.
952   // https://sourceware.org/binutils/docs/ld/Input-Section-Keep.html#Input-Section-Keep
953   if (Tok == "KEEP") {
954     expect("(");
955     StringRef FilePattern = next();
956     InputSectionDescription *Cmd = readInputSectionRules(FilePattern);
957     expect(")");
958     Opt.KeptSections.push_back(&Cmd->SectionRe);
959     return Cmd;
960   }
961   return readInputSectionRules(Tok);
962 }
963 
964 void ScriptParser::readSort() {
965   expect("(");
966   expect("CONSTRUCTORS");
967   expect(")");
968 }
969 
970 Expr ScriptParser::readAssert() {
971   expect("(");
972   Expr E = readExpr();
973   expect(",");
974   StringRef Msg = next();
975   expect(")");
976   return [=](uint64_t Dot) {
977     uint64_t V = E(Dot);
978     if (!V)
979       error(Msg);
980     return V;
981   };
982 }
983 
984 // Reads a FILL(expr) command. We handle the FILL command as an
985 // alias for =fillexp section attribute, which is different from
986 // what GNU linkers do.
987 // https://sourceware.org/binutils/docs/ld/Output-Section-Data.html
988 std::vector<uint8_t> ScriptParser::readFill() {
989   expect("(");
990   std::vector<uint8_t> V = readOutputSectionFiller(next());
991   expect(")");
992   expect(";");
993   return V;
994 }
995 
996 OutputSectionCommand *
997 ScriptParser::readOutputSectionDescription(StringRef OutSec) {
998   OutputSectionCommand *Cmd = new OutputSectionCommand(OutSec);
999 
1000   // Read an address expression.
1001   // https://sourceware.org/binutils/docs/ld/Output-Section-Address.html#Output-Section-Address
1002   if (peek() != ":")
1003     Cmd->AddrExpr = readExpr();
1004 
1005   expect(":");
1006 
1007   if (skip("AT"))
1008     Cmd->LmaExpr = readParenExpr();
1009   if (skip("ALIGN"))
1010     Cmd->AlignExpr = readParenExpr();
1011   if (skip("SUBALIGN"))
1012     Cmd->SubalignExpr = readParenExpr();
1013 
1014   // Parse constraints.
1015   if (skip("ONLY_IF_RO"))
1016     Cmd->Constraint = ConstraintKind::ReadOnly;
1017   if (skip("ONLY_IF_RW"))
1018     Cmd->Constraint = ConstraintKind::ReadWrite;
1019   expect("{");
1020 
1021   while (!Error && !skip("}")) {
1022     StringRef Tok = next();
1023     if (SymbolAssignment *Assignment = readProvideOrAssignment(Tok))
1024       Cmd->Commands.emplace_back(Assignment);
1025     else if (Tok == "FILL")
1026       Cmd->Filler = readFill();
1027     else if (Tok == "SORT")
1028       readSort();
1029     else if (peek() == "(")
1030       Cmd->Commands.emplace_back(readInputSectionDescription(Tok));
1031     else
1032       setError("unknown command " + Tok);
1033   }
1034   Cmd->Phdrs = readOutputSectionPhdrs();
1035   if (peek().startswith("="))
1036     Cmd->Filler = readOutputSectionFiller(next().drop_front());
1037   return Cmd;
1038 }
1039 
1040 // Read "=<number>" where <number> is an octal/decimal/hexadecimal number.
1041 // https://sourceware.org/binutils/docs/ld/Output-Section-Fill.html
1042 //
1043 // ld.gold is not fully compatible with ld.bfd. ld.bfd handles
1044 // hexstrings as blobs of arbitrary sizes, while ld.gold handles them
1045 // as 32-bit big-endian values. We will do the same as ld.gold does
1046 // because it's simpler than what ld.bfd does.
1047 std::vector<uint8_t> ScriptParser::readOutputSectionFiller(StringRef Tok) {
1048   uint32_t V;
1049   if (Tok.getAsInteger(0, V)) {
1050     setError("invalid filler expression: " + Tok);
1051     return {};
1052   }
1053   return {uint8_t(V >> 24), uint8_t(V >> 16), uint8_t(V >> 8), uint8_t(V)};
1054 }
1055 
1056 SymbolAssignment *ScriptParser::readProvideHidden(bool Provide, bool Hidden) {
1057   expect("(");
1058   SymbolAssignment *Cmd = readAssignment(next());
1059   Cmd->Provide = Provide;
1060   Cmd->Hidden = Hidden;
1061   expect(")");
1062   expect(";");
1063   return Cmd;
1064 }
1065 
1066 SymbolAssignment *ScriptParser::readProvideOrAssignment(StringRef Tok) {
1067   SymbolAssignment *Cmd = nullptr;
1068   if (peek() == "=" || peek() == "+=") {
1069     Cmd = readAssignment(Tok);
1070     expect(";");
1071   } else if (Tok == "PROVIDE") {
1072     Cmd = readProvideHidden(true, false);
1073   } else if (Tok == "HIDDEN") {
1074     Cmd = readProvideHidden(false, true);
1075   } else if (Tok == "PROVIDE_HIDDEN") {
1076     Cmd = readProvideHidden(true, true);
1077   }
1078   return Cmd;
1079 }
1080 
1081 static uint64_t getSymbolValue(StringRef S, uint64_t Dot) {
1082   if (S == ".")
1083     return Dot;
1084 
1085   switch (Config->EKind) {
1086   case ELF32LEKind:
1087     if (SymbolBody *B = Symtab<ELF32LE>::X->find(S))
1088       return B->getVA<ELF32LE>();
1089     break;
1090   case ELF32BEKind:
1091     if (SymbolBody *B = Symtab<ELF32BE>::X->find(S))
1092       return B->getVA<ELF32BE>();
1093     break;
1094   case ELF64LEKind:
1095     if (SymbolBody *B = Symtab<ELF64LE>::X->find(S))
1096       return B->getVA<ELF64LE>();
1097     break;
1098   case ELF64BEKind:
1099     if (SymbolBody *B = Symtab<ELF64BE>::X->find(S))
1100       return B->getVA<ELF64BE>();
1101     break;
1102   default:
1103     llvm_unreachable("unsupported target");
1104   }
1105   error("symbol not found: " + S);
1106   return 0;
1107 }
1108 
1109 static uint64_t getSectionSize(StringRef Name) {
1110   switch (Config->EKind) {
1111   case ELF32LEKind:
1112     return Script<ELF32LE>::X->getOutputSectionSize(Name);
1113   case ELF32BEKind:
1114     return Script<ELF32BE>::X->getOutputSectionSize(Name);
1115   case ELF64LEKind:
1116     return Script<ELF64LE>::X->getOutputSectionSize(Name);
1117   case ELF64BEKind:
1118     return Script<ELF64BE>::X->getOutputSectionSize(Name);
1119   default:
1120     llvm_unreachable("unsupported target");
1121   }
1122 }
1123 
1124 static uint64_t getSectionAddress(StringRef Name) {
1125   switch (Config->EKind) {
1126   case ELF32LEKind:
1127     return Script<ELF32LE>::X->getOutputSectionAddress(Name);
1128   case ELF32BEKind:
1129     return Script<ELF32BE>::X->getOutputSectionAddress(Name);
1130   case ELF64LEKind:
1131     return Script<ELF64LE>::X->getOutputSectionAddress(Name);
1132   case ELF64BEKind:
1133     return Script<ELF64BE>::X->getOutputSectionAddress(Name);
1134   default:
1135     llvm_unreachable("unsupported target");
1136   }
1137 }
1138 
1139 static uint64_t getHeaderSize() {
1140   switch (Config->EKind) {
1141   case ELF32LEKind:
1142     return Script<ELF32LE>::X->getHeaderSize();
1143   case ELF32BEKind:
1144     return Script<ELF32BE>::X->getHeaderSize();
1145   case ELF64LEKind:
1146     return Script<ELF64LE>::X->getHeaderSize();
1147   case ELF64BEKind:
1148     return Script<ELF64BE>::X->getHeaderSize();
1149   default:
1150     llvm_unreachable("unsupported target");
1151   }
1152 }
1153 
1154 SymbolAssignment *ScriptParser::readAssignment(StringRef Name) {
1155   StringRef Op = next();
1156   assert(Op == "=" || Op == "+=");
1157   Expr E = readExpr();
1158   if (Op == "+=")
1159     E = [=](uint64_t Dot) { return getSymbolValue(Name, Dot) + E(Dot); };
1160   return new SymbolAssignment(Name, E);
1161 }
1162 
1163 // This is an operator-precedence parser to parse a linker
1164 // script expression.
1165 Expr ScriptParser::readExpr() { return readExpr1(readPrimary(), 0); }
1166 
1167 static Expr combine(StringRef Op, Expr L, Expr R) {
1168   if (Op == "*")
1169     return [=](uint64_t Dot) { return L(Dot) * R(Dot); };
1170   if (Op == "/") {
1171     return [=](uint64_t Dot) -> uint64_t {
1172       uint64_t RHS = R(Dot);
1173       if (RHS == 0) {
1174         error("division by zero");
1175         return 0;
1176       }
1177       return L(Dot) / RHS;
1178     };
1179   }
1180   if (Op == "+")
1181     return [=](uint64_t Dot) { return L(Dot) + R(Dot); };
1182   if (Op == "-")
1183     return [=](uint64_t Dot) { return L(Dot) - R(Dot); };
1184   if (Op == "<")
1185     return [=](uint64_t Dot) { return L(Dot) < R(Dot); };
1186   if (Op == ">")
1187     return [=](uint64_t Dot) { return L(Dot) > R(Dot); };
1188   if (Op == ">=")
1189     return [=](uint64_t Dot) { return L(Dot) >= R(Dot); };
1190   if (Op == "<=")
1191     return [=](uint64_t Dot) { return L(Dot) <= R(Dot); };
1192   if (Op == "==")
1193     return [=](uint64_t Dot) { return L(Dot) == R(Dot); };
1194   if (Op == "!=")
1195     return [=](uint64_t Dot) { return L(Dot) != R(Dot); };
1196   if (Op == "&")
1197     return [=](uint64_t Dot) { return L(Dot) & R(Dot); };
1198   if (Op == "|")
1199     return [=](uint64_t Dot) { return L(Dot) | R(Dot); };
1200   llvm_unreachable("invalid operator");
1201 }
1202 
1203 // This is a part of the operator-precedence parser. This function
1204 // assumes that the remaining token stream starts with an operator.
1205 Expr ScriptParser::readExpr1(Expr Lhs, int MinPrec) {
1206   while (!atEOF() && !Error) {
1207     // Read an operator and an expression.
1208     StringRef Op1 = peek();
1209     if (Op1 == "?")
1210       return readTernary(Lhs);
1211     if (precedence(Op1) < MinPrec)
1212       break;
1213     next();
1214     Expr Rhs = readPrimary();
1215 
1216     // Evaluate the remaining part of the expression first if the
1217     // next operator has greater precedence than the previous one.
1218     // For example, if we have read "+" and "3", and if the next
1219     // operator is "*", then we'll evaluate 3 * ... part first.
1220     while (!atEOF()) {
1221       StringRef Op2 = peek();
1222       if (precedence(Op2) <= precedence(Op1))
1223         break;
1224       Rhs = readExpr1(Rhs, precedence(Op2));
1225     }
1226 
1227     Lhs = combine(Op1, Lhs, Rhs);
1228   }
1229   return Lhs;
1230 }
1231 
1232 uint64_t static getConstant(StringRef S) {
1233   if (S == "COMMONPAGESIZE")
1234     return Target->PageSize;
1235   if (S == "MAXPAGESIZE")
1236     return Target->MaxPageSize;
1237   error("unknown constant: " + S);
1238   return 0;
1239 }
1240 
1241 // Parses Tok as an integer. Returns true if successful.
1242 // It recognizes hexadecimal (prefixed with "0x" or suffixed with "H")
1243 // and decimal numbers. Decimal numbers may have "K" (kilo) or
1244 // "M" (mega) prefixes.
1245 static bool readInteger(StringRef Tok, uint64_t &Result) {
1246   if (Tok.startswith("-")) {
1247     if (!readInteger(Tok.substr(1), Result))
1248       return false;
1249     Result = -Result;
1250     return true;
1251   }
1252   if (Tok.startswith_lower("0x"))
1253     return !Tok.substr(2).getAsInteger(16, Result);
1254   if (Tok.endswith_lower("H"))
1255     return !Tok.drop_back().getAsInteger(16, Result);
1256 
1257   int Suffix = 1;
1258   if (Tok.endswith_lower("K")) {
1259     Suffix = 1024;
1260     Tok = Tok.drop_back();
1261   } else if (Tok.endswith_lower("M")) {
1262     Suffix = 1024 * 1024;
1263     Tok = Tok.drop_back();
1264   }
1265   if (Tok.getAsInteger(10, Result))
1266     return false;
1267   Result *= Suffix;
1268   return true;
1269 }
1270 
1271 Expr ScriptParser::readPrimary() {
1272   if (peek() == "(")
1273     return readParenExpr();
1274 
1275   StringRef Tok = next();
1276 
1277   if (Tok == "~") {
1278     Expr E = readPrimary();
1279     return [=](uint64_t Dot) { return ~E(Dot); };
1280   }
1281   if (Tok == "-") {
1282     Expr E = readPrimary();
1283     return [=](uint64_t Dot) { return -E(Dot); };
1284   }
1285 
1286   // Built-in functions are parsed here.
1287   // https://sourceware.org/binutils/docs/ld/Builtin-Functions.html.
1288   if (Tok == "ADDR") {
1289     expect("(");
1290     StringRef Name = next();
1291     expect(")");
1292     return [=](uint64_t Dot) { return getSectionAddress(Name); };
1293   }
1294   if (Tok == "ASSERT")
1295     return readAssert();
1296   if (Tok == "ALIGN") {
1297     Expr E = readParenExpr();
1298     return [=](uint64_t Dot) { return alignTo(Dot, E(Dot)); };
1299   }
1300   if (Tok == "CONSTANT") {
1301     expect("(");
1302     StringRef Tok = next();
1303     expect(")");
1304     return [=](uint64_t Dot) { return getConstant(Tok); };
1305   }
1306   if (Tok == "SEGMENT_START") {
1307     expect("(");
1308     next();
1309     expect(",");
1310     uint64_t Val;
1311     next().getAsInteger(0, Val);
1312     expect(")");
1313     return [=](uint64_t Dot) { return Val; };
1314   }
1315   if (Tok == "DATA_SEGMENT_ALIGN") {
1316     expect("(");
1317     Expr E = readExpr();
1318     expect(",");
1319     readExpr();
1320     expect(")");
1321     return [=](uint64_t Dot) { return alignTo(Dot, E(Dot)); };
1322   }
1323   if (Tok == "DATA_SEGMENT_END") {
1324     expect("(");
1325     expect(".");
1326     expect(")");
1327     return [](uint64_t Dot) { return Dot; };
1328   }
1329   // GNU linkers implements more complicated logic to handle
1330   // DATA_SEGMENT_RELRO_END. We instead ignore the arguments and just align to
1331   // the next page boundary for simplicity.
1332   if (Tok == "DATA_SEGMENT_RELRO_END") {
1333     expect("(");
1334     next();
1335     expect(",");
1336     readExpr();
1337     expect(")");
1338     return [](uint64_t Dot) { return alignTo(Dot, Target->PageSize); };
1339   }
1340   if (Tok == "SIZEOF") {
1341     expect("(");
1342     StringRef Name = next();
1343     expect(")");
1344     return [=](uint64_t Dot) { return getSectionSize(Name); };
1345   }
1346   if (Tok == "SIZEOF_HEADERS")
1347     return [=](uint64_t Dot) { return getHeaderSize(); };
1348 
1349   // Tok is a literal number.
1350   uint64_t V;
1351   if (readInteger(Tok, V))
1352     return [=](uint64_t Dot) { return V; };
1353 
1354   // Tok is a symbol name.
1355   if (Tok != "." && !isValidCIdentifier(Tok))
1356     setError("malformed number: " + Tok);
1357   return [=](uint64_t Dot) { return getSymbolValue(Tok, Dot); };
1358 }
1359 
1360 Expr ScriptParser::readTernary(Expr Cond) {
1361   next();
1362   Expr L = readExpr();
1363   expect(":");
1364   Expr R = readExpr();
1365   return [=](uint64_t Dot) { return Cond(Dot) ? L(Dot) : R(Dot); };
1366 }
1367 
1368 Expr ScriptParser::readParenExpr() {
1369   expect("(");
1370   Expr E = readExpr();
1371   expect(")");
1372   return E;
1373 }
1374 
1375 std::vector<StringRef> ScriptParser::readOutputSectionPhdrs() {
1376   std::vector<StringRef> Phdrs;
1377   while (!Error && peek().startswith(":")) {
1378     StringRef Tok = next();
1379     Tok = (Tok.size() == 1) ? next() : Tok.substr(1);
1380     if (Tok.empty()) {
1381       setError("section header name is empty");
1382       break;
1383     }
1384     Phdrs.push_back(Tok);
1385   }
1386   return Phdrs;
1387 }
1388 
1389 unsigned ScriptParser::readPhdrType() {
1390   StringRef Tok = next();
1391   unsigned Ret = StringSwitch<unsigned>(Tok)
1392       .Case("PT_NULL", PT_NULL)
1393       .Case("PT_LOAD", PT_LOAD)
1394       .Case("PT_DYNAMIC", PT_DYNAMIC)
1395       .Case("PT_INTERP", PT_INTERP)
1396       .Case("PT_NOTE", PT_NOTE)
1397       .Case("PT_SHLIB", PT_SHLIB)
1398       .Case("PT_PHDR", PT_PHDR)
1399       .Case("PT_TLS", PT_TLS)
1400       .Case("PT_GNU_EH_FRAME", PT_GNU_EH_FRAME)
1401       .Case("PT_GNU_STACK", PT_GNU_STACK)
1402       .Case("PT_GNU_RELRO", PT_GNU_RELRO)
1403       .Default(-1);
1404 
1405   if (Ret == (unsigned)-1) {
1406     setError("invalid program header type: " + Tok);
1407     return PT_NULL;
1408   }
1409   return Ret;
1410 }
1411 
1412 void ScriptParser::readVersionDeclaration(StringRef VerStr) {
1413   // Identifiers start at 2 because 0 and 1 are reserved
1414   // for VER_NDX_LOCAL and VER_NDX_GLOBAL constants.
1415   size_t VersionId = Config->VersionDefinitions.size() + 2;
1416   Config->VersionDefinitions.push_back({VerStr, VersionId});
1417 
1418   if (skip("global:") || peek() != "local:")
1419     readGlobal(VerStr);
1420   if (skip("local:"))
1421     readLocal();
1422   expect("}");
1423 
1424   // Each version may have a parent version. For example, "Ver2" defined as
1425   // "Ver2 { global: foo; local: *; } Ver1;" has "Ver1" as a parent. This
1426   // version hierarchy is, probably against your instinct, purely for human; the
1427   // runtime doesn't care about them at all. In LLD, we simply skip the token.
1428   if (!VerStr.empty() && peek() != ";")
1429     next();
1430   expect(";");
1431 }
1432 
1433 void ScriptParser::readLocal() {
1434   Config->DefaultSymbolVersion = VER_NDX_LOCAL;
1435   expect("*");
1436   expect(";");
1437 }
1438 
1439 void ScriptParser::readExtern(std::vector<SymbolVersion> *Globals) {
1440   expect("C++");
1441   expect("{");
1442 
1443   for (;;) {
1444     if (peek() == "}" || Error)
1445       break;
1446     Globals->push_back({next(), true});
1447     expect(";");
1448   }
1449 
1450   expect("}");
1451   expect(";");
1452 }
1453 
1454 void ScriptParser::readGlobal(StringRef VerStr) {
1455   std::vector<SymbolVersion> *Globals;
1456   if (VerStr.empty())
1457     Globals = &Config->VersionScriptGlobals;
1458   else
1459     Globals = &Config->VersionDefinitions.back().Globals;
1460 
1461   for (;;) {
1462     if (skip("extern"))
1463       readExtern(Globals);
1464 
1465     StringRef Cur = peek();
1466     if (Cur == "}" || Cur == "local:" || Error)
1467       return;
1468     next();
1469     Globals->push_back({Cur, false});
1470     expect(";");
1471   }
1472 }
1473 
1474 static bool isUnderSysroot(StringRef Path) {
1475   if (Config->Sysroot == "")
1476     return false;
1477   for (; !Path.empty(); Path = sys::path::parent_path(Path))
1478     if (sys::fs::equivalent(Config->Sysroot, Path))
1479       return true;
1480   return false;
1481 }
1482 
1483 void elf::readLinkerScript(MemoryBufferRef MB) {
1484   StringRef Path = MB.getBufferIdentifier();
1485   ScriptParser(MB.getBuffer(), isUnderSysroot(Path)).readLinkerScript();
1486 }
1487 
1488 void elf::readVersionScript(MemoryBufferRef MB) {
1489   ScriptParser(MB.getBuffer(), false).readVersionScript();
1490 }
1491 
1492 template class elf::LinkerScript<ELF32LE>;
1493 template class elf::LinkerScript<ELF32BE>;
1494 template class elf::LinkerScript<ELF64LE>;
1495 template class elf::LinkerScript<ELF64BE>;
1496