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