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 //
12 //===----------------------------------------------------------------------===//
13 
14 #include "LinkerScript.h"
15 #include "Config.h"
16 #include "Driver.h"
17 #include "InputSection.h"
18 #include "Memory.h"
19 #include "OutputSections.h"
20 #include "ScriptParser.h"
21 #include "Strings.h"
22 #include "SymbolTable.h"
23 #include "Symbols.h"
24 #include "SyntheticSections.h"
25 #include "Target.h"
26 #include "Writer.h"
27 #include "llvm/ADT/STLExtras.h"
28 #include "llvm/ADT/SmallString.h"
29 #include "llvm/ADT/StringRef.h"
30 #include "llvm/ADT/StringSwitch.h"
31 #include "llvm/Support/Casting.h"
32 #include "llvm/Support/ELF.h"
33 #include "llvm/Support/Endian.h"
34 #include "llvm/Support/ErrorHandling.h"
35 #include "llvm/Support/FileSystem.h"
36 #include "llvm/Support/MathExtras.h"
37 #include "llvm/Support/Path.h"
38 #include <algorithm>
39 #include <cassert>
40 #include <cstddef>
41 #include <cstdint>
42 #include <iterator>
43 #include <limits>
44 #include <memory>
45 #include <string>
46 #include <tuple>
47 #include <vector>
48 
49 using namespace llvm;
50 using namespace llvm::ELF;
51 using namespace llvm::object;
52 using namespace llvm::support::endian;
53 using namespace lld;
54 using namespace lld::elf;
55 
56 LinkerScriptBase *elf::ScriptBase;
57 ScriptConfiguration *elf::ScriptConfig;
58 
59 template <class ELFT> static void addRegular(SymbolAssignment *Cmd) {
60   uint8_t Visibility = Cmd->Hidden ? STV_HIDDEN : STV_DEFAULT;
61   Symbol *Sym = Symtab<ELFT>::X->addRegular(Cmd->Name, Visibility, STT_NOTYPE,
62                                             0, 0, STB_GLOBAL, nullptr, nullptr);
63   Cmd->Sym = Sym->body();
64 
65   // If we have no SECTIONS then we don't have '.' and don't call
66   // assignAddresses(). We calculate symbol value immediately in this case.
67   if (!ScriptConfig->HasSections)
68     cast<DefinedRegular<ELFT>>(Cmd->Sym)->Value = Cmd->Expression(0);
69 }
70 
71 template <class ELFT> static void addSynthetic(SymbolAssignment *Cmd) {
72   // If we have SECTIONS block then output sections haven't been created yet.
73   const OutputSectionBase *Sec =
74       ScriptConfig->HasSections ? nullptr : Cmd->Expression.Section();
75   Symbol *Sym = Symtab<ELFT>::X->addSynthetic(
76       Cmd->Name, Sec, 0, Cmd->Hidden ? STV_HIDDEN : STV_DEFAULT);
77   Cmd->Sym = Sym->body();
78 
79   // If we already know section then we can calculate symbol value immediately.
80   if (Sec)
81     cast<DefinedSynthetic>(Cmd->Sym)->Value = Cmd->Expression(0) - Sec->Addr;
82 }
83 
84 static bool isUnderSysroot(StringRef Path) {
85   if (Config->Sysroot == "")
86     return false;
87   for (; !Path.empty(); Path = sys::path::parent_path(Path))
88     if (sys::fs::equivalent(Config->Sysroot, Path))
89       return true;
90   return false;
91 }
92 
93 template <class ELFT> static void addSymbol(SymbolAssignment *Cmd) {
94   if (Cmd->Expression.IsAbsolute())
95     addRegular<ELFT>(Cmd);
96   else
97     addSynthetic<ELFT>(Cmd);
98 }
99 // If a symbol was in PROVIDE(), we need to define it only when
100 // it is an undefined symbol.
101 template <class ELFT> static bool shouldDefine(SymbolAssignment *Cmd) {
102   if (Cmd->Name == ".")
103     return false;
104   if (!Cmd->Provide)
105     return true;
106   SymbolBody *B = Symtab<ELFT>::X->find(Cmd->Name);
107   return B && B->isUndefined();
108 }
109 
110 bool SymbolAssignment::classof(const BaseCommand *C) {
111   return C->Kind == AssignmentKind;
112 }
113 
114 bool OutputSectionCommand::classof(const BaseCommand *C) {
115   return C->Kind == OutputSectionKind;
116 }
117 
118 bool InputSectionDescription::classof(const BaseCommand *C) {
119   return C->Kind == InputSectionKind;
120 }
121 
122 bool AssertCommand::classof(const BaseCommand *C) {
123   return C->Kind == AssertKind;
124 }
125 
126 bool BytesDataCommand::classof(const BaseCommand *C) {
127   return C->Kind == BytesDataKind;
128 }
129 
130 template <class ELFT> LinkerScript<ELFT>::LinkerScript() = default;
131 template <class ELFT> LinkerScript<ELFT>::~LinkerScript() = default;
132 
133 template <class ELFT> static StringRef basename(InputSectionBase<ELFT> *S) {
134   if (S->getFile())
135     return sys::path::filename(S->getFile()->getName());
136   return "";
137 }
138 
139 template <class ELFT>
140 bool LinkerScript<ELFT>::shouldKeep(InputSectionBase<ELFT> *S) {
141   for (InputSectionDescription *ID : Opt.KeptSections)
142     if (ID->FilePat.match(basename(S)))
143       for (SectionPattern &P : ID->SectionPatterns)
144         if (P.SectionPat.match(S->Name))
145           return true;
146   return false;
147 }
148 
149 static bool comparePriority(InputSectionData *A, InputSectionData *B) {
150   return getPriority(A->Name) < getPriority(B->Name);
151 }
152 
153 static bool compareName(InputSectionData *A, InputSectionData *B) {
154   return A->Name < B->Name;
155 }
156 
157 static bool compareAlignment(InputSectionData *A, InputSectionData *B) {
158   // ">" is not a mistake. Larger alignments are placed before smaller
159   // alignments in order to reduce the amount of padding necessary.
160   // This is compatible with GNU.
161   return A->Alignment > B->Alignment;
162 }
163 
164 static std::function<bool(InputSectionData *, InputSectionData *)>
165 getComparator(SortSectionPolicy K) {
166   switch (K) {
167   case SortSectionPolicy::Alignment:
168     return compareAlignment;
169   case SortSectionPolicy::Name:
170     return compareName;
171   case SortSectionPolicy::Priority:
172     return comparePriority;
173   default:
174     llvm_unreachable("unknown sort policy");
175   }
176 }
177 
178 template <class ELFT>
179 static bool matchConstraints(ArrayRef<InputSectionBase<ELFT> *> Sections,
180                              ConstraintKind Kind) {
181   if (Kind == ConstraintKind::NoConstraint)
182     return true;
183   bool IsRW = llvm::any_of(Sections, [=](InputSectionData *Sec2) {
184     auto *Sec = static_cast<InputSectionBase<ELFT> *>(Sec2);
185     return Sec->Flags & SHF_WRITE;
186   });
187   return (IsRW && Kind == ConstraintKind::ReadWrite) ||
188          (!IsRW && Kind == ConstraintKind::ReadOnly);
189 }
190 
191 static void sortSections(InputSectionData **Begin, InputSectionData **End,
192                          SortSectionPolicy K) {
193   if (K != SortSectionPolicy::Default && K != SortSectionPolicy::None)
194     std::stable_sort(Begin, End, getComparator(K));
195 }
196 
197 // Compute and remember which sections the InputSectionDescription matches.
198 template <class ELFT>
199 void LinkerScript<ELFT>::computeInputSections(InputSectionDescription *I) {
200   // Collects all sections that satisfy constraints of I
201   // and attach them to I.
202   for (SectionPattern &Pat : I->SectionPatterns) {
203     size_t SizeBefore = I->Sections.size();
204 
205     for (InputSectionBase<ELFT> *S : Symtab<ELFT>::X->Sections) {
206       if (!S->Live || S->Assigned)
207         continue;
208 
209       StringRef Filename = basename(S);
210       if (!I->FilePat.match(Filename) || Pat.ExcludedFilePat.match(Filename))
211         continue;
212       if (!Pat.SectionPat.match(S->Name))
213         continue;
214       I->Sections.push_back(S);
215       S->Assigned = true;
216     }
217 
218     // Sort sections as instructed by SORT-family commands and --sort-section
219     // option. Because SORT-family commands can be nested at most two depth
220     // (e.g. SORT_BY_NAME(SORT_BY_ALIGNMENT(.text.*))) and because the command
221     // line option is respected even if a SORT command is given, the exact
222     // behavior we have here is a bit complicated. Here are the rules.
223     //
224     // 1. If two SORT commands are given, --sort-section is ignored.
225     // 2. If one SORT command is given, and if it is not SORT_NONE,
226     //    --sort-section is handled as an inner SORT command.
227     // 3. If one SORT command is given, and if it is SORT_NONE, don't sort.
228     // 4. If no SORT command is given, sort according to --sort-section.
229     InputSectionData **Begin = I->Sections.data() + SizeBefore;
230     InputSectionData **End = I->Sections.data() + I->Sections.size();
231     if (Pat.SortOuter != SortSectionPolicy::None) {
232       if (Pat.SortInner == SortSectionPolicy::Default)
233         sortSections(Begin, End, Config->SortSection);
234       else
235         sortSections(Begin, End, Pat.SortInner);
236       sortSections(Begin, End, Pat.SortOuter);
237     }
238   }
239 }
240 
241 template <class ELFT>
242 void LinkerScript<ELFT>::discard(ArrayRef<InputSectionBase<ELFT> *> V) {
243   for (InputSectionBase<ELFT> *S : V) {
244     S->Live = false;
245     reportDiscarded(S);
246   }
247 }
248 
249 template <class ELFT>
250 std::vector<InputSectionBase<ELFT> *>
251 LinkerScript<ELFT>::createInputSectionList(OutputSectionCommand &OutCmd) {
252   std::vector<InputSectionBase<ELFT> *> Ret;
253 
254   for (const std::unique_ptr<BaseCommand> &Base : OutCmd.Commands) {
255     auto *Cmd = dyn_cast<InputSectionDescription>(Base.get());
256     if (!Cmd)
257       continue;
258     computeInputSections(Cmd);
259     for (InputSectionData *S : Cmd->Sections)
260       Ret.push_back(static_cast<InputSectionBase<ELFT> *>(S));
261   }
262 
263   return Ret;
264 }
265 
266 template <class ELFT>
267 static SectionKey<ELFT::Is64Bits> createKey(InputSectionBase<ELFT> *C,
268                                             StringRef OutsecName) {
269   // When using linker script the merge rules are different.
270   // Unfortunately, linker scripts are name based. This means that expressions
271   // like *(.foo*) can refer to multiple input sections that would normally be
272   // placed in different output sections. We cannot put them in different
273   // output sections or we would produce wrong results for
274   // start = .; *(.foo.*) end = .; *(.bar)
275   // and a mapping of .foo1 and .bar1 to one section and .foo2 and .bar2 to
276   // another. The problem is that there is no way to layout those output
277   // sections such that the .foo sections are the only thing between the
278   // start and end symbols.
279 
280   // An extra annoyance is that we cannot simply disable merging of the contents
281   // of SHF_MERGE sections, but our implementation requires one output section
282   // per "kind" (string or not, which size/aligment).
283   // Fortunately, creating symbols in the middle of a merge section is not
284   // supported by bfd or gold, so we can just create multiple section in that
285   // case.
286   typedef typename ELFT::uint uintX_t;
287   uintX_t Flags = C->Flags & (SHF_MERGE | SHF_STRINGS);
288 
289   uintX_t Alignment = 0;
290   if (isa<MergeInputSection<ELFT>>(C))
291     Alignment = std::max<uintX_t>(C->Alignment, C->Entsize);
292 
293   return SectionKey<ELFT::Is64Bits>{OutsecName, /*Type*/ 0, Flags, Alignment};
294 }
295 
296 template <class ELFT>
297 void LinkerScript<ELFT>::addSection(OutputSectionFactory<ELFT> &Factory,
298                                     InputSectionBase<ELFT> *Sec,
299                                     StringRef Name) {
300   OutputSectionBase *OutSec;
301   bool IsNew;
302   std::tie(OutSec, IsNew) = Factory.create(createKey(Sec, Name), Sec);
303   if (IsNew)
304     OutputSections->push_back(OutSec);
305   OutSec->addSection(Sec);
306 }
307 
308 template <class ELFT>
309 void LinkerScript<ELFT>::processCommands(OutputSectionFactory<ELFT> &Factory) {
310   for (unsigned I = 0; I < Opt.Commands.size(); ++I) {
311     auto Iter = Opt.Commands.begin() + I;
312     const std::unique_ptr<BaseCommand> &Base1 = *Iter;
313 
314     // Handle symbol assignments outside of any output section.
315     if (auto *Cmd = dyn_cast<SymbolAssignment>(Base1.get())) {
316       if (shouldDefine<ELFT>(Cmd))
317         addSymbol<ELFT>(Cmd);
318       continue;
319     }
320 
321     if (auto *Cmd = dyn_cast<AssertCommand>(Base1.get())) {
322       // If we don't have SECTIONS then output sections have already been
323       // created by Writer<ELFT>. The LinkerScript<ELFT>::assignAddresses
324       // will not be called, so ASSERT should be evaluated now.
325       if (!Opt.HasSections)
326         Cmd->Expression(0);
327       continue;
328     }
329 
330     if (auto *Cmd = dyn_cast<OutputSectionCommand>(Base1.get())) {
331       std::vector<InputSectionBase<ELFT> *> V = createInputSectionList(*Cmd);
332 
333       // The output section name `/DISCARD/' is special.
334       // Any input section assigned to it is discarded.
335       if (Cmd->Name == "/DISCARD/") {
336         discard(V);
337         continue;
338       }
339 
340       // This is for ONLY_IF_RO and ONLY_IF_RW. An output section directive
341       // ".foo : ONLY_IF_R[OW] { ... }" is handled only if all member input
342       // sections satisfy a given constraint. If not, a directive is handled
343       // as if it wasn't present from the beginning.
344       //
345       // Because we'll iterate over Commands many more times, the easiest
346       // way to "make it as if it wasn't present" is to just remove it.
347       if (!matchConstraints<ELFT>(V, Cmd->Constraint)) {
348         for (InputSectionBase<ELFT> *S : V)
349           S->Assigned = false;
350         Opt.Commands.erase(Iter);
351         --I;
352         continue;
353       }
354 
355       // A directive may contain symbol definitions like this:
356       // ".foo : { ...; bar = .; }". Handle them.
357       for (const std::unique_ptr<BaseCommand> &Base : Cmd->Commands)
358         if (auto *OutCmd = dyn_cast<SymbolAssignment>(Base.get()))
359           if (shouldDefine<ELFT>(OutCmd))
360             addSymbol<ELFT>(OutCmd);
361 
362       // Handle subalign (e.g. ".foo : SUBALIGN(32) { ... }"). If subalign
363       // is given, input sections are aligned to that value, whether the
364       // given value is larger or smaller than the original section alignment.
365       if (Cmd->SubalignExpr) {
366         uint32_t Subalign = Cmd->SubalignExpr(0);
367         for (InputSectionBase<ELFT> *S : V)
368           S->Alignment = Subalign;
369       }
370 
371       // Add input sections to an output section.
372       for (InputSectionBase<ELFT> *S : V)
373         addSection(Factory, S, Cmd->Name);
374     }
375   }
376 }
377 
378 // Add sections that didn't match any sections command.
379 template <class ELFT>
380 void LinkerScript<ELFT>::addOrphanSections(
381     OutputSectionFactory<ELFT> &Factory) {
382   for (InputSectionBase<ELFT> *S : Symtab<ELFT>::X->Sections)
383     if (S->Live && !S->OutSec)
384       addSection(Factory, S, getOutputSectionName(S->Name));
385 }
386 
387 // Sets value of a section-defined symbol. Two kinds of
388 // symbols are processed: synthetic symbols, whose value
389 // is an offset from beginning of section and regular
390 // symbols whose value is absolute.
391 template <class ELFT>
392 static void assignSectionSymbol(SymbolAssignment *Cmd,
393                                 typename ELFT::uint Value) {
394   if (!Cmd->Sym)
395     return;
396 
397   if (auto *Body = dyn_cast<DefinedSynthetic>(Cmd->Sym)) {
398     Body->Section = Cmd->Expression.Section();
399     Body->Value = Cmd->Expression(Value) - Body->Section->Addr;
400     return;
401   }
402   auto *Body = cast<DefinedRegular<ELFT>>(Cmd->Sym);
403   Body->Value = Cmd->Expression(Value);
404 }
405 
406 template <class ELFT> static bool isTbss(OutputSectionBase *Sec) {
407   return (Sec->Flags & SHF_TLS) && Sec->Type == SHT_NOBITS;
408 }
409 
410 template <class ELFT> void LinkerScript<ELFT>::output(InputSection<ELFT> *S) {
411   if (!AlreadyOutputIS.insert(S).second)
412     return;
413   bool IsTbss = isTbss<ELFT>(CurOutSec);
414 
415   uintX_t Pos = IsTbss ? Dot + ThreadBssOffset : Dot;
416   Pos = alignTo(Pos, S->Alignment);
417   S->OutSecOff = Pos - CurOutSec->Addr;
418   Pos += S->getSize();
419 
420   // Update output section size after adding each section. This is so that
421   // SIZEOF works correctly in the case below:
422   // .foo { *(.aaa) a = SIZEOF(.foo); *(.bbb) }
423   CurOutSec->Size = Pos - CurOutSec->Addr;
424 
425   if (IsTbss)
426     ThreadBssOffset = Pos - Dot;
427   else
428     Dot = Pos;
429 }
430 
431 template <class ELFT> void LinkerScript<ELFT>::flush() {
432   if (!CurOutSec || !AlreadyOutputOS.insert(CurOutSec).second)
433     return;
434   if (auto *OutSec = dyn_cast<OutputSection<ELFT>>(CurOutSec)) {
435     for (InputSection<ELFT> *I : OutSec->Sections)
436       output(I);
437   } else {
438     Dot += CurOutSec->Size;
439   }
440 }
441 
442 template <class ELFT>
443 void LinkerScript<ELFT>::switchTo(OutputSectionBase *Sec) {
444   if (CurOutSec == Sec)
445     return;
446   if (AlreadyOutputOS.count(Sec))
447     return;
448 
449   flush();
450   CurOutSec = Sec;
451 
452   Dot = alignTo(Dot, CurOutSec->Addralign);
453   CurOutSec->Addr = isTbss<ELFT>(CurOutSec) ? Dot + ThreadBssOffset : Dot;
454 
455   // If neither AT nor AT> is specified for an allocatable section, the linker
456   // will set the LMA such that the difference between VMA and LMA for the
457   // section is the same as the preceding output section in the same region
458   // https://sourceware.org/binutils/docs-2.20/ld/Output-Section-LMA.html
459   CurOutSec->setLMAOffset(LMAOffset);
460 }
461 
462 template <class ELFT> void LinkerScript<ELFT>::process(BaseCommand &Base) {
463   // This handles the assignments to symbol or to a location counter (.)
464   if (auto *AssignCmd = dyn_cast<SymbolAssignment>(&Base)) {
465     if (AssignCmd->Name == ".") {
466       // Update to location counter means update to section size.
467       uintX_t Val = AssignCmd->Expression(Dot);
468       if (Val < Dot)
469         error("unable to move location counter backward for: " +
470               CurOutSec->Name);
471       Dot = Val;
472       CurOutSec->Size = Dot - CurOutSec->Addr;
473       return;
474     }
475     assignSectionSymbol<ELFT>(AssignCmd, Dot);
476     return;
477   }
478 
479   // Handle BYTE(), SHORT(), LONG(), or QUAD().
480   if (auto *DataCmd = dyn_cast<BytesDataCommand>(&Base)) {
481     DataCmd->Offset = Dot - CurOutSec->Addr;
482     Dot += DataCmd->Size;
483     CurOutSec->Size = Dot - CurOutSec->Addr;
484     return;
485   }
486 
487   if (auto *AssertCmd = dyn_cast<AssertCommand>(&Base)) {
488     AssertCmd->Expression(Dot);
489     return;
490   }
491 
492   // It handles single input section description command,
493   // calculates and assigns the offsets for each section and also
494   // updates the output section size.
495   auto &ICmd = cast<InputSectionDescription>(Base);
496   for (InputSectionData *ID : ICmd.Sections) {
497     // We tentatively added all synthetic sections at the beginning and removed
498     // empty ones afterwards (because there is no way to know whether they were
499     // going be empty or not other than actually running linker scripts.)
500     // We need to ignore remains of empty sections.
501     if (auto *Sec = dyn_cast<SyntheticSection<ELFT>>(ID))
502       if (Sec->empty())
503         continue;
504 
505     auto *IB = static_cast<InputSectionBase<ELFT> *>(ID);
506     switchTo(IB->OutSec);
507     if (auto *I = dyn_cast<InputSection<ELFT>>(IB))
508       output(I);
509     else
510       flush();
511   }
512 }
513 
514 template <class ELFT>
515 static std::vector<OutputSectionBase *>
516 findSections(StringRef Name, const std::vector<OutputSectionBase *> &Sections) {
517   std::vector<OutputSectionBase *> Ret;
518   for (OutputSectionBase *Sec : Sections)
519     if (Sec->getName() == Name)
520       Ret.push_back(Sec);
521   return Ret;
522 }
523 
524 // This function assigns offsets to input sections and an output section
525 // for a single sections command (e.g. ".text { *(.text); }").
526 template <class ELFT>
527 void LinkerScript<ELFT>::assignOffsets(OutputSectionCommand *Cmd) {
528   if (Cmd->LMAExpr)
529     LMAOffset = Cmd->LMAExpr(Dot) - Dot;
530   std::vector<OutputSectionBase *> Sections =
531       findSections<ELFT>(Cmd->Name, *OutputSections);
532   if (Sections.empty())
533     return;
534   switchTo(Sections[0]);
535 
536   // Find the last section output location. We will output orphan sections
537   // there so that end symbols point to the correct location.
538   auto E = std::find_if(Cmd->Commands.rbegin(), Cmd->Commands.rend(),
539                         [](const std::unique_ptr<BaseCommand> &Cmd) {
540                           return !isa<SymbolAssignment>(*Cmd);
541                         })
542                .base();
543   for (auto I = Cmd->Commands.begin(); I != E; ++I)
544     process(**I);
545   for (OutputSectionBase *Base : Sections)
546     switchTo(Base);
547   flush();
548   std::for_each(E, Cmd->Commands.end(),
549                 [this](std::unique_ptr<BaseCommand> &B) { process(*B.get()); });
550 }
551 
552 template <class ELFT> void LinkerScript<ELFT>::removeEmptyCommands() {
553   // It is common practice to use very generic linker scripts. So for any
554   // given run some of the output sections in the script will be empty.
555   // We could create corresponding empty output sections, but that would
556   // clutter the output.
557   // We instead remove trivially empty sections. The bfd linker seems even
558   // more aggressive at removing them.
559   auto Pos = std::remove_if(
560       Opt.Commands.begin(), Opt.Commands.end(),
561       [&](const std::unique_ptr<BaseCommand> &Base) {
562         if (auto *Cmd = dyn_cast<OutputSectionCommand>(Base.get()))
563           return findSections<ELFT>(Cmd->Name, *OutputSections).empty();
564         return false;
565       });
566   Opt.Commands.erase(Pos, Opt.Commands.end());
567 }
568 
569 static bool isAllSectionDescription(const OutputSectionCommand &Cmd) {
570   for (const std::unique_ptr<BaseCommand> &I : Cmd.Commands)
571     if (!isa<InputSectionDescription>(*I))
572       return false;
573   return true;
574 }
575 
576 template <class ELFT> void LinkerScript<ELFT>::adjustSectionsBeforeSorting() {
577   // If the output section contains only symbol assignments, create a
578   // corresponding output section. The bfd linker seems to only create them if
579   // '.' is assigned to, but creating these section should not have any bad
580   // consequeces and gives us a section to put the symbol in.
581   uintX_t Flags = SHF_ALLOC;
582   uint32_t Type = SHT_NOBITS;
583   for (const std::unique_ptr<BaseCommand> &Base : Opt.Commands) {
584     auto *Cmd = dyn_cast<OutputSectionCommand>(Base.get());
585     if (!Cmd)
586       continue;
587     std::vector<OutputSectionBase *> Secs =
588         findSections<ELFT>(Cmd->Name, *OutputSections);
589     if (!Secs.empty()) {
590       Flags = Secs[0]->Flags;
591       Type = Secs[0]->Type;
592       continue;
593     }
594 
595     if (isAllSectionDescription(*Cmd))
596       continue;
597 
598     auto *OutSec = make<OutputSection<ELFT>>(Cmd->Name, Type, Flags);
599     OutputSections->push_back(OutSec);
600   }
601 }
602 
603 template <class ELFT> void LinkerScript<ELFT>::adjustSectionsAfterSorting() {
604   placeOrphanSections();
605 
606   // If output section command doesn't specify any segments,
607   // and we haven't previously assigned any section to segment,
608   // then we simply assign section to the very first load segment.
609   // Below is an example of such linker script:
610   // PHDRS { seg PT_LOAD; }
611   // SECTIONS { .aaa : { *(.aaa) } }
612   std::vector<StringRef> DefPhdrs;
613   auto FirstPtLoad =
614       std::find_if(Opt.PhdrsCommands.begin(), Opt.PhdrsCommands.end(),
615                    [](const PhdrsCommand &Cmd) { return Cmd.Type == PT_LOAD; });
616   if (FirstPtLoad != Opt.PhdrsCommands.end())
617     DefPhdrs.push_back(FirstPtLoad->Name);
618 
619   // Walk the commands and propagate the program headers to commands that don't
620   // explicitly specify them.
621   for (const std::unique_ptr<BaseCommand> &Base : Opt.Commands) {
622     auto *Cmd = dyn_cast<OutputSectionCommand>(Base.get());
623     if (!Cmd)
624       continue;
625     if (Cmd->Phdrs.empty())
626       Cmd->Phdrs = DefPhdrs;
627     else
628       DefPhdrs = Cmd->Phdrs;
629   }
630 
631   removeEmptyCommands();
632 }
633 
634 // When placing orphan sections, we want to place them after symbol assignments
635 // so that an orphan after
636 //   begin_foo = .;
637 //   foo : { *(foo) }
638 //   end_foo = .;
639 // doesn't break the intended meaning of the begin/end symbols.
640 // We don't want to go over sections since Writer<ELFT>::sortSections is the
641 // one in charge of deciding the order of the sections.
642 // We don't want to go over alignments, since doing so in
643 //  rx_sec : { *(rx_sec) }
644 //  . = ALIGN(0x1000);
645 //  /* The RW PT_LOAD starts here*/
646 //  rw_sec : { *(rw_sec) }
647 // would mean that the RW PT_LOAD would become unaligned.
648 static bool shouldSkip(const BaseCommand &Cmd) {
649   if (isa<OutputSectionCommand>(Cmd))
650     return false;
651   const auto *Assign = dyn_cast<SymbolAssignment>(&Cmd);
652   if (!Assign)
653     return true;
654   return Assign->Name != ".";
655 }
656 
657 // Orphan sections are sections present in the input files which are not
658 // explicitly placed into the output file by the linker script. This just
659 // places them in the order already decided in OutputSections.
660 template <class ELFT> void LinkerScript<ELFT>::placeOrphanSections() {
661   // The OutputSections are already in the correct order.
662   // This loops creates or moves commands as needed so that they are in the
663   // correct order.
664   int CmdIndex = 0;
665 
666   // As a horrible special case, skip the first . assignment if it is before any
667   // section. We do this because it is common to set a load address by starting
668   // the script with ". = 0xabcd" and the expectation is that every section is
669   // after that.
670   auto FirstSectionOrDotAssignment =
671       std::find_if(Opt.Commands.begin(), Opt.Commands.end(),
672                    [](const std::unique_ptr<BaseCommand> &Cmd) {
673                      if (isa<OutputSectionCommand>(*Cmd))
674                        return true;
675                      const auto *Assign = dyn_cast<SymbolAssignment>(Cmd.get());
676                      if (!Assign)
677                        return false;
678                      return Assign->Name == ".";
679                    });
680   if (FirstSectionOrDotAssignment != Opt.Commands.end()) {
681     CmdIndex = FirstSectionOrDotAssignment - Opt.Commands.begin();
682     if (isa<SymbolAssignment>(**FirstSectionOrDotAssignment))
683       ++CmdIndex;
684   }
685 
686   for (OutputSectionBase *Sec : *OutputSections) {
687     StringRef Name = Sec->getName();
688 
689     // Find the last spot where we can insert a command and still get the
690     // correct result.
691     auto CmdIter = Opt.Commands.begin() + CmdIndex;
692     auto E = Opt.Commands.end();
693     while (CmdIter != E && shouldSkip(**CmdIter)) {
694       ++CmdIter;
695       ++CmdIndex;
696     }
697 
698     auto Pos =
699         std::find_if(CmdIter, E, [&](const std::unique_ptr<BaseCommand> &Base) {
700           auto *Cmd = dyn_cast<OutputSectionCommand>(Base.get());
701           return Cmd && Cmd->Name == Name;
702         });
703     if (Pos == E) {
704       Opt.Commands.insert(CmdIter,
705                           llvm::make_unique<OutputSectionCommand>(Name));
706       ++CmdIndex;
707       continue;
708     }
709 
710     // Continue from where we found it.
711     CmdIndex = (Pos - Opt.Commands.begin()) + 1;
712   }
713 }
714 
715 template <class ELFT>
716 void LinkerScript<ELFT>::assignAddresses(std::vector<PhdrEntry> &Phdrs) {
717   // Assign addresses as instructed by linker script SECTIONS sub-commands.
718   Dot = 0;
719 
720   for (const std::unique_ptr<BaseCommand> &Base : Opt.Commands) {
721     if (auto *Cmd = dyn_cast<SymbolAssignment>(Base.get())) {
722       if (Cmd->Name == ".") {
723         Dot = Cmd->Expression(Dot);
724       } else if (Cmd->Sym) {
725         assignSectionSymbol<ELFT>(Cmd, Dot);
726       }
727       continue;
728     }
729 
730     if (auto *Cmd = dyn_cast<AssertCommand>(Base.get())) {
731       Cmd->Expression(Dot);
732       continue;
733     }
734 
735     auto *Cmd = cast<OutputSectionCommand>(Base.get());
736     if (Cmd->AddrExpr)
737       Dot = Cmd->AddrExpr(Dot);
738     assignOffsets(Cmd);
739   }
740 
741   uintX_t MinVA = std::numeric_limits<uintX_t>::max();
742   for (OutputSectionBase *Sec : *OutputSections) {
743     if (Sec->Flags & SHF_ALLOC)
744       MinVA = std::min<uint64_t>(MinVA, Sec->Addr);
745     else
746       Sec->Addr = 0;
747   }
748 
749   uintX_t HeaderSize = getHeaderSize();
750   // If the linker script doesn't have PHDRS, add ElfHeader and ProgramHeaders
751   // now that we know we have space.
752   if (HeaderSize <= MinVA && !hasPhdrsCommands())
753     allocateHeaders<ELFT>(Phdrs, *OutputSections);
754 
755   // ELF and Program headers need to be right before the first section in
756   // memory. Set their addresses accordingly.
757   MinVA = alignDown(MinVA - HeaderSize, Config->MaxPageSize);
758   Out<ELFT>::ElfHeader->Addr = MinVA;
759   Out<ELFT>::ProgramHeaders->Addr = Out<ELFT>::ElfHeader->Size + MinVA;
760 }
761 
762 // Creates program headers as instructed by PHDRS linker script command.
763 template <class ELFT> std::vector<PhdrEntry> LinkerScript<ELFT>::createPhdrs() {
764   std::vector<PhdrEntry> Ret;
765 
766   // Process PHDRS and FILEHDR keywords because they are not
767   // real output sections and cannot be added in the following loop.
768   for (const PhdrsCommand &Cmd : Opt.PhdrsCommands) {
769     Ret.emplace_back(Cmd.Type, Cmd.Flags == UINT_MAX ? PF_R : Cmd.Flags);
770     PhdrEntry &Phdr = Ret.back();
771 
772     if (Cmd.HasFilehdr)
773       Phdr.add(Out<ELFT>::ElfHeader);
774     if (Cmd.HasPhdrs)
775       Phdr.add(Out<ELFT>::ProgramHeaders);
776 
777     if (Cmd.LMAExpr) {
778       Phdr.p_paddr = Cmd.LMAExpr(0);
779       Phdr.HasLMA = true;
780     }
781   }
782 
783   // Add output sections to program headers.
784   for (OutputSectionBase *Sec : *OutputSections) {
785     if (!(Sec->Flags & SHF_ALLOC))
786       break;
787 
788     // Assign headers specified by linker script
789     for (size_t Id : getPhdrIndices(Sec->getName())) {
790       Ret[Id].add(Sec);
791       if (Opt.PhdrsCommands[Id].Flags == UINT_MAX)
792         Ret[Id].p_flags |= Sec->getPhdrFlags();
793     }
794   }
795   return Ret;
796 }
797 
798 template <class ELFT> bool LinkerScript<ELFT>::ignoreInterpSection() {
799   // Ignore .interp section in case we have PHDRS specification
800   // and PT_INTERP isn't listed.
801   return !Opt.PhdrsCommands.empty() &&
802          llvm::find_if(Opt.PhdrsCommands, [](const PhdrsCommand &Cmd) {
803            return Cmd.Type == PT_INTERP;
804          }) == Opt.PhdrsCommands.end();
805 }
806 
807 template <class ELFT> uint32_t LinkerScript<ELFT>::getFiller(StringRef Name) {
808   for (const std::unique_ptr<BaseCommand> &Base : Opt.Commands)
809     if (auto *Cmd = dyn_cast<OutputSectionCommand>(Base.get()))
810       if (Cmd->Name == Name)
811         return Cmd->Filler;
812   return 0;
813 }
814 
815 template <class ELFT>
816 static void writeInt(uint8_t *Buf, uint64_t Data, uint64_t Size) {
817   const endianness E = ELFT::TargetEndianness;
818 
819   switch (Size) {
820   case 1:
821     *Buf = (uint8_t)Data;
822     break;
823   case 2:
824     write16<E>(Buf, Data);
825     break;
826   case 4:
827     write32<E>(Buf, Data);
828     break;
829   case 8:
830     write64<E>(Buf, Data);
831     break;
832   default:
833     llvm_unreachable("unsupported Size argument");
834   }
835 }
836 
837 template <class ELFT>
838 void LinkerScript<ELFT>::writeDataBytes(StringRef Name, uint8_t *Buf) {
839   int I = getSectionIndex(Name);
840   if (I == INT_MAX)
841     return;
842 
843   auto *Cmd = dyn_cast<OutputSectionCommand>(Opt.Commands[I].get());
844   for (const std::unique_ptr<BaseCommand> &Base : Cmd->Commands)
845     if (auto *Data = dyn_cast<BytesDataCommand>(Base.get()))
846       writeInt<ELFT>(Buf + Data->Offset, Data->Expression(0), Data->Size);
847 }
848 
849 template <class ELFT> bool LinkerScript<ELFT>::hasLMA(StringRef Name) {
850   for (const std::unique_ptr<BaseCommand> &Base : Opt.Commands)
851     if (auto *Cmd = dyn_cast<OutputSectionCommand>(Base.get()))
852       if (Cmd->LMAExpr && Cmd->Name == Name)
853         return true;
854   return false;
855 }
856 
857 // Returns the index of the given section name in linker script
858 // SECTIONS commands. Sections are laid out as the same order as they
859 // were in the script. If a given name did not appear in the script,
860 // it returns INT_MAX, so that it will be laid out at end of file.
861 template <class ELFT> int LinkerScript<ELFT>::getSectionIndex(StringRef Name) {
862   for (int I = 0, E = Opt.Commands.size(); I != E; ++I)
863     if (auto *Cmd = dyn_cast<OutputSectionCommand>(Opt.Commands[I].get()))
864       if (Cmd->Name == Name)
865         return I;
866   return INT_MAX;
867 }
868 
869 template <class ELFT> bool LinkerScript<ELFT>::hasPhdrsCommands() {
870   return !Opt.PhdrsCommands.empty();
871 }
872 
873 template <class ELFT>
874 const OutputSectionBase *LinkerScript<ELFT>::getOutputSection(const Twine &Loc,
875                                                               StringRef Name) {
876   static OutputSectionBase FakeSec("", 0, 0);
877 
878   for (OutputSectionBase *Sec : *OutputSections)
879     if (Sec->getName() == Name)
880       return Sec;
881 
882   error(Loc + ": undefined section " + Name);
883   return &FakeSec;
884 }
885 
886 // This function is essentially the same as getOutputSection(Name)->Size,
887 // but it won't print out an error message if a given section is not found.
888 //
889 // Linker script does not create an output section if its content is empty.
890 // We want to allow SIZEOF(.foo) where .foo is a section which happened to
891 // be empty. That is why this function is different from getOutputSection().
892 template <class ELFT>
893 uint64_t LinkerScript<ELFT>::getOutputSectionSize(StringRef Name) {
894   for (OutputSectionBase *Sec : *OutputSections)
895     if (Sec->getName() == Name)
896       return Sec->Size;
897   return 0;
898 }
899 
900 template <class ELFT> uint64_t LinkerScript<ELFT>::getHeaderSize() {
901   return elf::getHeaderSize<ELFT>();
902 }
903 
904 template <class ELFT>
905 uint64_t LinkerScript<ELFT>::getSymbolValue(const Twine &Loc, StringRef S) {
906   if (SymbolBody *B = Symtab<ELFT>::X->find(S))
907     return B->getVA<ELFT>();
908   error(Loc + ": symbol not found: " + S);
909   return 0;
910 }
911 
912 template <class ELFT> bool LinkerScript<ELFT>::isDefined(StringRef S) {
913   return Symtab<ELFT>::X->find(S) != nullptr;
914 }
915 
916 template <class ELFT> bool LinkerScript<ELFT>::isAbsolute(StringRef S) {
917   SymbolBody *Sym = Symtab<ELFT>::X->find(S);
918   auto *DR = dyn_cast_or_null<DefinedRegular<ELFT>>(Sym);
919   return DR && !DR->Section;
920 }
921 
922 // Gets section symbol belongs to. Symbol "." doesn't belong to any
923 // specific section but isn't absolute at the same time, so we try
924 // to find suitable section for it as well.
925 template <class ELFT>
926 const OutputSectionBase *LinkerScript<ELFT>::getSymbolSection(StringRef S) {
927   SymbolBody *Sym = Symtab<ELFT>::X->find(S);
928   if (!Sym) {
929     if (OutputSections->empty())
930       return nullptr;
931     return CurOutSec ? CurOutSec : (*OutputSections)[0];
932   }
933 
934   if (auto *DR = dyn_cast_or_null<DefinedRegular<ELFT>>(Sym))
935     return DR->Section ? DR->Section->OutSec : nullptr;
936   if (auto *DS = dyn_cast_or_null<DefinedSynthetic>(Sym))
937     return DS->Section;
938 
939   return nullptr;
940 }
941 
942 // Returns indices of ELF headers containing specific section, identified
943 // by Name. Each index is a zero based number of ELF header listed within
944 // PHDRS {} script block.
945 template <class ELFT>
946 std::vector<size_t> LinkerScript<ELFT>::getPhdrIndices(StringRef SectionName) {
947   for (const std::unique_ptr<BaseCommand> &Base : Opt.Commands) {
948     auto *Cmd = dyn_cast<OutputSectionCommand>(Base.get());
949     if (!Cmd || Cmd->Name != SectionName)
950       continue;
951 
952     std::vector<size_t> Ret;
953     for (StringRef PhdrName : Cmd->Phdrs)
954       Ret.push_back(getPhdrIndex(Cmd->Location, PhdrName));
955     return Ret;
956   }
957   return {};
958 }
959 
960 template <class ELFT>
961 size_t LinkerScript<ELFT>::getPhdrIndex(const Twine &Loc, StringRef PhdrName) {
962   size_t I = 0;
963   for (PhdrsCommand &Cmd : Opt.PhdrsCommands) {
964     if (Cmd.Name == PhdrName)
965       return I;
966     ++I;
967   }
968   error(Loc + ": section header '" + PhdrName + "' is not listed in PHDRS");
969   return 0;
970 }
971 
972 class elf::ScriptParser final : public ScriptParserBase {
973   typedef void (ScriptParser::*Handler)();
974 
975 public:
976   ScriptParser(MemoryBufferRef MB)
977       : ScriptParserBase(MB),
978         IsUnderSysroot(isUnderSysroot(MB.getBufferIdentifier())) {}
979 
980   void readLinkerScript();
981   void readVersionScript();
982   void readDynamicList();
983 
984 private:
985   void addFile(StringRef Path);
986 
987   void readAsNeeded();
988   void readEntry();
989   void readExtern();
990   void readGroup();
991   void readInclude();
992   void readOutput();
993   void readOutputArch();
994   void readOutputFormat();
995   void readPhdrs();
996   void readSearchDir();
997   void readSections();
998   void readVersion();
999   void readVersionScriptCommand();
1000 
1001   SymbolAssignment *readAssignment(StringRef Name);
1002   BytesDataCommand *readBytesDataCommand(StringRef Tok);
1003   uint32_t readFill();
1004   OutputSectionCommand *readOutputSectionDescription(StringRef OutSec);
1005   uint32_t readOutputSectionFiller(StringRef Tok);
1006   std::vector<StringRef> readOutputSectionPhdrs();
1007   InputSectionDescription *readInputSectionDescription(StringRef Tok);
1008   StringMatcher readFilePatterns();
1009   std::vector<SectionPattern> readInputSectionsList();
1010   InputSectionDescription *readInputSectionRules(StringRef FilePattern);
1011   unsigned readPhdrType();
1012   SortSectionPolicy readSortKind();
1013   SymbolAssignment *readProvideHidden(bool Provide, bool Hidden);
1014   SymbolAssignment *readProvideOrAssignment(StringRef Tok);
1015   void readSort();
1016   Expr readAssert();
1017 
1018   Expr readExpr();
1019   Expr readExpr1(Expr Lhs, int MinPrec);
1020   StringRef readParenLiteral();
1021   Expr readPrimary();
1022   Expr readTernary(Expr Cond);
1023   Expr readParenExpr();
1024 
1025   // For parsing version script.
1026   std::vector<SymbolVersion> readVersionExtern();
1027   void readAnonymousDeclaration();
1028   void readVersionDeclaration(StringRef VerStr);
1029   std::vector<SymbolVersion> readSymbols();
1030 
1031   ScriptConfiguration &Opt = *ScriptConfig;
1032   bool IsUnderSysroot;
1033 };
1034 
1035 void ScriptParser::readDynamicList() {
1036   expect("{");
1037   readAnonymousDeclaration();
1038   if (!atEOF())
1039     setError("EOF expected, but got " + next());
1040 }
1041 
1042 void ScriptParser::readVersionScript() {
1043   readVersionScriptCommand();
1044   if (!atEOF())
1045     setError("EOF expected, but got " + next());
1046 }
1047 
1048 void ScriptParser::readVersionScriptCommand() {
1049   if (consume("{")) {
1050     readAnonymousDeclaration();
1051     return;
1052   }
1053 
1054   while (!atEOF() && !Error && peek() != "}") {
1055     StringRef VerStr = next();
1056     if (VerStr == "{") {
1057       setError("anonymous version definition is used in "
1058                "combination with other version definitions");
1059       return;
1060     }
1061     expect("{");
1062     readVersionDeclaration(VerStr);
1063   }
1064 }
1065 
1066 void ScriptParser::readVersion() {
1067   expect("{");
1068   readVersionScriptCommand();
1069   expect("}");
1070 }
1071 
1072 void ScriptParser::readLinkerScript() {
1073   while (!atEOF()) {
1074     StringRef Tok = next();
1075     if (Tok == ";")
1076       continue;
1077 
1078     if (Tok == "ASSERT") {
1079       Opt.Commands.emplace_back(new AssertCommand(readAssert()));
1080     } else if (Tok == "ENTRY") {
1081       readEntry();
1082     } else if (Tok == "EXTERN") {
1083       readExtern();
1084     } else if (Tok == "GROUP" || Tok == "INPUT") {
1085       readGroup();
1086     } else if (Tok == "INCLUDE") {
1087       readInclude();
1088     } else if (Tok == "OUTPUT") {
1089       readOutput();
1090     } else if (Tok == "OUTPUT_ARCH") {
1091       readOutputArch();
1092     } else if (Tok == "OUTPUT_FORMAT") {
1093       readOutputFormat();
1094     } else if (Tok == "PHDRS") {
1095       readPhdrs();
1096     } else if (Tok == "SEARCH_DIR") {
1097       readSearchDir();
1098     } else if (Tok == "SECTIONS") {
1099       readSections();
1100     } else if (Tok == "VERSION") {
1101       readVersion();
1102     } else if (SymbolAssignment *Cmd = readProvideOrAssignment(Tok)) {
1103       Opt.Commands.emplace_back(Cmd);
1104     } else {
1105       setError("unknown directive: " + Tok);
1106     }
1107   }
1108 }
1109 
1110 void ScriptParser::addFile(StringRef S) {
1111   if (IsUnderSysroot && S.startswith("/")) {
1112     SmallString<128> PathData;
1113     StringRef Path = (Config->Sysroot + S).toStringRef(PathData);
1114     if (sys::fs::exists(Path)) {
1115       Driver->addFile(Saver.save(Path));
1116       return;
1117     }
1118   }
1119 
1120   if (sys::path::is_absolute(S)) {
1121     Driver->addFile(S);
1122   } else if (S.startswith("=")) {
1123     if (Config->Sysroot.empty())
1124       Driver->addFile(S.substr(1));
1125     else
1126       Driver->addFile(Saver.save(Config->Sysroot + "/" + S.substr(1)));
1127   } else if (S.startswith("-l")) {
1128     Driver->addLibrary(S.substr(2));
1129   } else if (sys::fs::exists(S)) {
1130     Driver->addFile(S);
1131   } else {
1132     if (Optional<std::string> Path = findFromSearchPaths(S))
1133       Driver->addFile(Saver.save(*Path));
1134     else
1135       setError("unable to find " + S);
1136   }
1137 }
1138 
1139 void ScriptParser::readAsNeeded() {
1140   expect("(");
1141   bool Orig = Config->AsNeeded;
1142   Config->AsNeeded = true;
1143   while (!Error && !consume(")"))
1144     addFile(unquote(next()));
1145   Config->AsNeeded = Orig;
1146 }
1147 
1148 void ScriptParser::readEntry() {
1149   // -e <symbol> takes predecence over ENTRY(<symbol>).
1150   expect("(");
1151   StringRef Tok = next();
1152   if (Config->Entry.empty())
1153     Config->Entry = Tok;
1154   expect(")");
1155 }
1156 
1157 void ScriptParser::readExtern() {
1158   expect("(");
1159   while (!Error && !consume(")"))
1160     Config->Undefined.push_back(next());
1161 }
1162 
1163 void ScriptParser::readGroup() {
1164   expect("(");
1165   while (!Error && !consume(")")) {
1166     StringRef Tok = next();
1167     if (Tok == "AS_NEEDED")
1168       readAsNeeded();
1169     else
1170       addFile(unquote(Tok));
1171   }
1172 }
1173 
1174 void ScriptParser::readInclude() {
1175   StringRef Tok = unquote(next());
1176   // https://sourceware.org/binutils/docs/ld/File-Commands.html:
1177   // The file will be searched for in the current directory, and in any
1178   // directory specified with the -L option.
1179   auto MBOrErr = MemoryBuffer::getFile(Tok);
1180   if (!MBOrErr)
1181     if (Optional<std::string> Path = findFromSearchPaths(Tok))
1182       MBOrErr = MemoryBuffer::getFile(*Path);
1183   if (!MBOrErr) {
1184     setError("cannot open " + Tok);
1185     return;
1186   }
1187   std::unique_ptr<MemoryBuffer> &MB = *MBOrErr;
1188   tokenize({Saver.save(MB->getBuffer()), unquote(Tok)});
1189 }
1190 
1191 void ScriptParser::readOutput() {
1192   // -o <file> takes predecence over OUTPUT(<file>).
1193   expect("(");
1194   StringRef Tok = next();
1195   if (Config->OutputFile.empty())
1196     Config->OutputFile = unquote(Tok);
1197   expect(")");
1198 }
1199 
1200 void ScriptParser::readOutputArch() {
1201   // Error checking only for now.
1202   expect("(");
1203   skip();
1204   expect(")");
1205 }
1206 
1207 void ScriptParser::readOutputFormat() {
1208   // Error checking only for now.
1209   expect("(");
1210   skip();
1211   StringRef Tok = next();
1212   if (Tok == ")")
1213     return;
1214   if (Tok != ",") {
1215     setError("unexpected token: " + Tok);
1216     return;
1217   }
1218   skip();
1219   expect(",");
1220   skip();
1221   expect(")");
1222 }
1223 
1224 void ScriptParser::readPhdrs() {
1225   expect("{");
1226   while (!Error && !consume("}")) {
1227     StringRef Tok = next();
1228     Opt.PhdrsCommands.push_back(
1229         {Tok, PT_NULL, false, false, UINT_MAX, nullptr});
1230     PhdrsCommand &PhdrCmd = Opt.PhdrsCommands.back();
1231 
1232     PhdrCmd.Type = readPhdrType();
1233     do {
1234       Tok = next();
1235       if (Tok == ";")
1236         break;
1237       if (Tok == "FILEHDR")
1238         PhdrCmd.HasFilehdr = true;
1239       else if (Tok == "PHDRS")
1240         PhdrCmd.HasPhdrs = true;
1241       else if (Tok == "AT")
1242         PhdrCmd.LMAExpr = readParenExpr();
1243       else if (Tok == "FLAGS") {
1244         expect("(");
1245         // Passing 0 for the value of dot is a bit of a hack. It means that
1246         // we accept expressions like ".|1".
1247         PhdrCmd.Flags = readExpr()(0);
1248         expect(")");
1249       } else
1250         setError("unexpected header attribute: " + Tok);
1251     } while (!Error);
1252   }
1253 }
1254 
1255 void ScriptParser::readSearchDir() {
1256   expect("(");
1257   StringRef Tok = next();
1258   if (!Config->Nostdlib)
1259     Config->SearchPaths.push_back(unquote(Tok));
1260   expect(")");
1261 }
1262 
1263 void ScriptParser::readSections() {
1264   Opt.HasSections = true;
1265   // -no-rosegment is used to avoid placing read only non-executable sections in
1266   // their own segment. We do the same if SECTIONS command is present in linker
1267   // script. See comment for computeFlags().
1268   Config->SingleRoRx = true;
1269 
1270   expect("{");
1271   while (!Error && !consume("}")) {
1272     StringRef Tok = next();
1273     BaseCommand *Cmd = readProvideOrAssignment(Tok);
1274     if (!Cmd) {
1275       if (Tok == "ASSERT")
1276         Cmd = new AssertCommand(readAssert());
1277       else
1278         Cmd = readOutputSectionDescription(Tok);
1279     }
1280     Opt.Commands.emplace_back(Cmd);
1281   }
1282 }
1283 
1284 static int precedence(StringRef Op) {
1285   return StringSwitch<int>(Op)
1286       .Cases("*", "/", 5)
1287       .Cases("+", "-", 4)
1288       .Cases("<<", ">>", 3)
1289       .Cases("<", "<=", ">", ">=", "==", "!=", 2)
1290       .Cases("&", "|", 1)
1291       .Default(-1);
1292 }
1293 
1294 StringMatcher ScriptParser::readFilePatterns() {
1295   std::vector<StringRef> V;
1296   while (!Error && !consume(")"))
1297     V.push_back(next());
1298   return StringMatcher(V);
1299 }
1300 
1301 SortSectionPolicy ScriptParser::readSortKind() {
1302   if (consume("SORT") || consume("SORT_BY_NAME"))
1303     return SortSectionPolicy::Name;
1304   if (consume("SORT_BY_ALIGNMENT"))
1305     return SortSectionPolicy::Alignment;
1306   if (consume("SORT_BY_INIT_PRIORITY"))
1307     return SortSectionPolicy::Priority;
1308   if (consume("SORT_NONE"))
1309     return SortSectionPolicy::None;
1310   return SortSectionPolicy::Default;
1311 }
1312 
1313 // Method reads a list of sequence of excluded files and section globs given in
1314 // a following form: ((EXCLUDE_FILE(file_pattern+))? section_pattern+)+
1315 // Example: *(.foo.1 EXCLUDE_FILE (*a.o) .foo.2 EXCLUDE_FILE (*b.o) .foo.3)
1316 // The semantics of that is next:
1317 // * Include .foo.1 from every file.
1318 // * Include .foo.2 from every file but a.o
1319 // * Include .foo.3 from every file but b.o
1320 std::vector<SectionPattern> ScriptParser::readInputSectionsList() {
1321   std::vector<SectionPattern> Ret;
1322   while (!Error && peek() != ")") {
1323     StringMatcher ExcludeFilePat;
1324     if (consume("EXCLUDE_FILE")) {
1325       expect("(");
1326       ExcludeFilePat = readFilePatterns();
1327     }
1328 
1329     std::vector<StringRef> V;
1330     while (!Error && peek() != ")" && peek() != "EXCLUDE_FILE")
1331       V.push_back(next());
1332 
1333     if (!V.empty())
1334       Ret.push_back({std::move(ExcludeFilePat), StringMatcher(V)});
1335     else
1336       setError("section pattern is expected");
1337   }
1338   return Ret;
1339 }
1340 
1341 // Reads contents of "SECTIONS" directive. That directive contains a
1342 // list of glob patterns for input sections. The grammar is as follows.
1343 //
1344 // <patterns> ::= <section-list>
1345 //              | <sort> "(" <section-list> ")"
1346 //              | <sort> "(" <sort> "(" <section-list> ")" ")"
1347 //
1348 // <sort>     ::= "SORT" | "SORT_BY_NAME" | "SORT_BY_ALIGNMENT"
1349 //              | "SORT_BY_INIT_PRIORITY" | "SORT_NONE"
1350 //
1351 // <section-list> is parsed by readInputSectionsList().
1352 InputSectionDescription *
1353 ScriptParser::readInputSectionRules(StringRef FilePattern) {
1354   auto *Cmd = new InputSectionDescription(FilePattern);
1355   expect("(");
1356   while (!Error && !consume(")")) {
1357     SortSectionPolicy Outer = readSortKind();
1358     SortSectionPolicy Inner = SortSectionPolicy::Default;
1359     std::vector<SectionPattern> V;
1360     if (Outer != SortSectionPolicy::Default) {
1361       expect("(");
1362       Inner = readSortKind();
1363       if (Inner != SortSectionPolicy::Default) {
1364         expect("(");
1365         V = readInputSectionsList();
1366         expect(")");
1367       } else {
1368         V = readInputSectionsList();
1369       }
1370       expect(")");
1371     } else {
1372       V = readInputSectionsList();
1373     }
1374 
1375     for (SectionPattern &Pat : V) {
1376       Pat.SortInner = Inner;
1377       Pat.SortOuter = Outer;
1378     }
1379 
1380     std::move(V.begin(), V.end(), std::back_inserter(Cmd->SectionPatterns));
1381   }
1382   return Cmd;
1383 }
1384 
1385 InputSectionDescription *
1386 ScriptParser::readInputSectionDescription(StringRef Tok) {
1387   // Input section wildcard can be surrounded by KEEP.
1388   // https://sourceware.org/binutils/docs/ld/Input-Section-Keep.html#Input-Section-Keep
1389   if (Tok == "KEEP") {
1390     expect("(");
1391     StringRef FilePattern = next();
1392     InputSectionDescription *Cmd = readInputSectionRules(FilePattern);
1393     expect(")");
1394     Opt.KeptSections.push_back(Cmd);
1395     return Cmd;
1396   }
1397   return readInputSectionRules(Tok);
1398 }
1399 
1400 void ScriptParser::readSort() {
1401   expect("(");
1402   expect("CONSTRUCTORS");
1403   expect(")");
1404 }
1405 
1406 Expr ScriptParser::readAssert() {
1407   expect("(");
1408   Expr E = readExpr();
1409   expect(",");
1410   StringRef Msg = unquote(next());
1411   expect(")");
1412   return [=](uint64_t Dot) {
1413     uint64_t V = E(Dot);
1414     if (!V)
1415       error(Msg);
1416     return V;
1417   };
1418 }
1419 
1420 // Reads a FILL(expr) command. We handle the FILL command as an
1421 // alias for =fillexp section attribute, which is different from
1422 // what GNU linkers do.
1423 // https://sourceware.org/binutils/docs/ld/Output-Section-Data.html
1424 uint32_t ScriptParser::readFill() {
1425   expect("(");
1426   uint32_t V = readOutputSectionFiller(next());
1427   expect(")");
1428   expect(";");
1429   return V;
1430 }
1431 
1432 OutputSectionCommand *
1433 ScriptParser::readOutputSectionDescription(StringRef OutSec) {
1434   OutputSectionCommand *Cmd = new OutputSectionCommand(OutSec);
1435   Cmd->Location = getCurrentLocation();
1436 
1437   // Read an address expression.
1438   // https://sourceware.org/binutils/docs/ld/Output-Section-Address.html#Output-Section-Address
1439   if (peek() != ":")
1440     Cmd->AddrExpr = readExpr();
1441 
1442   expect(":");
1443 
1444   if (consume("AT"))
1445     Cmd->LMAExpr = readParenExpr();
1446   if (consume("ALIGN"))
1447     Cmd->AlignExpr = readParenExpr();
1448   if (consume("SUBALIGN"))
1449     Cmd->SubalignExpr = readParenExpr();
1450 
1451   // Parse constraints.
1452   if (consume("ONLY_IF_RO"))
1453     Cmd->Constraint = ConstraintKind::ReadOnly;
1454   if (consume("ONLY_IF_RW"))
1455     Cmd->Constraint = ConstraintKind::ReadWrite;
1456   expect("{");
1457 
1458   while (!Error && !consume("}")) {
1459     StringRef Tok = next();
1460     if (SymbolAssignment *Assignment = readProvideOrAssignment(Tok)) {
1461       Cmd->Commands.emplace_back(Assignment);
1462     } else if (BytesDataCommand *Data = readBytesDataCommand(Tok)) {
1463       Cmd->Commands.emplace_back(Data);
1464     } else if (Tok == "ASSERT") {
1465       Cmd->Commands.emplace_back(new AssertCommand(readAssert()));
1466       expect(";");
1467     } else if (Tok == "FILL") {
1468       Cmd->Filler = readFill();
1469     } else if (Tok == "SORT") {
1470       readSort();
1471     } else if (peek() == "(") {
1472       Cmd->Commands.emplace_back(readInputSectionDescription(Tok));
1473     } else {
1474       setError("unknown command " + Tok);
1475     }
1476   }
1477   Cmd->Phdrs = readOutputSectionPhdrs();
1478 
1479   if (consume("="))
1480     Cmd->Filler = readOutputSectionFiller(next());
1481   else if (peek().startswith("="))
1482     Cmd->Filler = readOutputSectionFiller(next().drop_front());
1483 
1484   return Cmd;
1485 }
1486 
1487 // Read "=<number>" where <number> is an octal/decimal/hexadecimal number.
1488 // https://sourceware.org/binutils/docs/ld/Output-Section-Fill.html
1489 //
1490 // ld.gold is not fully compatible with ld.bfd. ld.bfd handles
1491 // hexstrings as blobs of arbitrary sizes, while ld.gold handles them
1492 // as 32-bit big-endian values. We will do the same as ld.gold does
1493 // because it's simpler than what ld.bfd does.
1494 uint32_t ScriptParser::readOutputSectionFiller(StringRef Tok) {
1495   uint32_t V;
1496   if (!Tok.getAsInteger(0, V))
1497     return V;
1498   setError("invalid filler expression: " + Tok);
1499   return 0;
1500 }
1501 
1502 SymbolAssignment *ScriptParser::readProvideHidden(bool Provide, bool Hidden) {
1503   expect("(");
1504   SymbolAssignment *Cmd = readAssignment(next());
1505   Cmd->Provide = Provide;
1506   Cmd->Hidden = Hidden;
1507   expect(")");
1508   expect(";");
1509   return Cmd;
1510 }
1511 
1512 SymbolAssignment *ScriptParser::readProvideOrAssignment(StringRef Tok) {
1513   SymbolAssignment *Cmd = nullptr;
1514   if (peek() == "=" || peek() == "+=") {
1515     Cmd = readAssignment(Tok);
1516     expect(";");
1517   } else if (Tok == "PROVIDE") {
1518     Cmd = readProvideHidden(true, false);
1519   } else if (Tok == "HIDDEN") {
1520     Cmd = readProvideHidden(false, true);
1521   } else if (Tok == "PROVIDE_HIDDEN") {
1522     Cmd = readProvideHidden(true, true);
1523   }
1524   return Cmd;
1525 }
1526 
1527 static uint64_t getSymbolValue(const Twine &Loc, StringRef S, uint64_t Dot) {
1528   if (S == ".")
1529     return Dot;
1530   return ScriptBase->getSymbolValue(Loc, S);
1531 }
1532 
1533 static bool isAbsolute(StringRef S) {
1534   if (S == ".")
1535     return false;
1536   return ScriptBase->isAbsolute(S);
1537 }
1538 
1539 SymbolAssignment *ScriptParser::readAssignment(StringRef Name) {
1540   StringRef Op = next();
1541   Expr E;
1542   assert(Op == "=" || Op == "+=");
1543   if (consume("ABSOLUTE")) {
1544     // The RHS may be something like "ABSOLUTE(.) & 0xff".
1545     // Call readExpr1 to read the whole expression.
1546     E = readExpr1(readParenExpr(), 0);
1547     E.IsAbsolute = [] { return true; };
1548   } else {
1549     E = readExpr();
1550   }
1551   if (Op == "+=") {
1552     std::string Loc = getCurrentLocation();
1553     E = [=](uint64_t Dot) {
1554       return getSymbolValue(Loc, Name, Dot) + E(Dot);
1555     };
1556   }
1557   return new SymbolAssignment(Name, E);
1558 }
1559 
1560 // This is an operator-precedence parser to parse a linker
1561 // script expression.
1562 Expr ScriptParser::readExpr() { return readExpr1(readPrimary(), 0); }
1563 
1564 static Expr combine(StringRef Op, Expr L, Expr R) {
1565   if (Op == "*")
1566     return [=](uint64_t Dot) { return L(Dot) * R(Dot); };
1567   if (Op == "/") {
1568     return [=](uint64_t Dot) -> uint64_t {
1569       uint64_t RHS = R(Dot);
1570       if (RHS == 0) {
1571         error("division by zero");
1572         return 0;
1573       }
1574       return L(Dot) / RHS;
1575     };
1576   }
1577   if (Op == "+")
1578     return {[=](uint64_t Dot) { return L(Dot) + R(Dot); },
1579             [=] { return L.IsAbsolute() && R.IsAbsolute(); },
1580             [=] {
1581               const OutputSectionBase *S = L.Section();
1582               return S ? S : R.Section();
1583             }};
1584   if (Op == "-")
1585     return [=](uint64_t Dot) { return L(Dot) - R(Dot); };
1586   if (Op == "<<")
1587     return [=](uint64_t Dot) { return L(Dot) << R(Dot); };
1588   if (Op == ">>")
1589     return [=](uint64_t Dot) { return L(Dot) >> R(Dot); };
1590   if (Op == "<")
1591     return [=](uint64_t Dot) { return L(Dot) < R(Dot); };
1592   if (Op == ">")
1593     return [=](uint64_t Dot) { return L(Dot) > R(Dot); };
1594   if (Op == ">=")
1595     return [=](uint64_t Dot) { return L(Dot) >= R(Dot); };
1596   if (Op == "<=")
1597     return [=](uint64_t Dot) { return L(Dot) <= R(Dot); };
1598   if (Op == "==")
1599     return [=](uint64_t Dot) { return L(Dot) == R(Dot); };
1600   if (Op == "!=")
1601     return [=](uint64_t Dot) { return L(Dot) != R(Dot); };
1602   if (Op == "&")
1603     return [=](uint64_t Dot) { return L(Dot) & R(Dot); };
1604   if (Op == "|")
1605     return [=](uint64_t Dot) { return L(Dot) | R(Dot); };
1606   llvm_unreachable("invalid operator");
1607 }
1608 
1609 // This is a part of the operator-precedence parser. This function
1610 // assumes that the remaining token stream starts with an operator.
1611 Expr ScriptParser::readExpr1(Expr Lhs, int MinPrec) {
1612   while (!atEOF() && !Error) {
1613     // Read an operator and an expression.
1614     if (consume("?"))
1615       return readTernary(Lhs);
1616     StringRef Op1 = peek();
1617     if (precedence(Op1) < MinPrec)
1618       break;
1619     skip();
1620     Expr Rhs = readPrimary();
1621 
1622     // Evaluate the remaining part of the expression first if the
1623     // next operator has greater precedence than the previous one.
1624     // For example, if we have read "+" and "3", and if the next
1625     // operator is "*", then we'll evaluate 3 * ... part first.
1626     while (!atEOF()) {
1627       StringRef Op2 = peek();
1628       if (precedence(Op2) <= precedence(Op1))
1629         break;
1630       Rhs = readExpr1(Rhs, precedence(Op2));
1631     }
1632 
1633     Lhs = combine(Op1, Lhs, Rhs);
1634   }
1635   return Lhs;
1636 }
1637 
1638 uint64_t static getConstant(StringRef S) {
1639   if (S == "COMMONPAGESIZE")
1640     return Target->PageSize;
1641   if (S == "MAXPAGESIZE")
1642     return Config->MaxPageSize;
1643   error("unknown constant: " + S);
1644   return 0;
1645 }
1646 
1647 // Parses Tok as an integer. Returns true if successful.
1648 // It recognizes hexadecimal (prefixed with "0x" or suffixed with "H")
1649 // and decimal numbers. Decimal numbers may have "K" (kilo) or
1650 // "M" (mega) prefixes.
1651 static bool readInteger(StringRef Tok, uint64_t &Result) {
1652   // Negative number
1653   if (Tok.startswith("-")) {
1654     if (!readInteger(Tok.substr(1), Result))
1655       return false;
1656     Result = -Result;
1657     return true;
1658   }
1659 
1660   // Hexadecimal
1661   if (Tok.startswith_lower("0x"))
1662     return !Tok.substr(2).getAsInteger(16, Result);
1663   if (Tok.endswith_lower("H"))
1664     return !Tok.drop_back().getAsInteger(16, Result);
1665 
1666   // Decimal
1667   int Suffix = 1;
1668   if (Tok.endswith_lower("K")) {
1669     Suffix = 1024;
1670     Tok = Tok.drop_back();
1671   } else if (Tok.endswith_lower("M")) {
1672     Suffix = 1024 * 1024;
1673     Tok = Tok.drop_back();
1674   }
1675   if (Tok.getAsInteger(10, Result))
1676     return false;
1677   Result *= Suffix;
1678   return true;
1679 }
1680 
1681 BytesDataCommand *ScriptParser::readBytesDataCommand(StringRef Tok) {
1682   int Size = StringSwitch<unsigned>(Tok)
1683                  .Case("BYTE", 1)
1684                  .Case("SHORT", 2)
1685                  .Case("LONG", 4)
1686                  .Case("QUAD", 8)
1687                  .Default(-1);
1688   if (Size == -1)
1689     return nullptr;
1690 
1691   return new BytesDataCommand(readParenExpr(), Size);
1692 }
1693 
1694 StringRef ScriptParser::readParenLiteral() {
1695   expect("(");
1696   StringRef Tok = next();
1697   expect(")");
1698   return Tok;
1699 }
1700 
1701 Expr ScriptParser::readPrimary() {
1702   if (peek() == "(")
1703     return readParenExpr();
1704 
1705   StringRef Tok = next();
1706   std::string Location = getCurrentLocation();
1707 
1708   if (Tok == "~") {
1709     Expr E = readPrimary();
1710     return [=](uint64_t Dot) { return ~E(Dot); };
1711   }
1712   if (Tok == "-") {
1713     Expr E = readPrimary();
1714     return [=](uint64_t Dot) { return -E(Dot); };
1715   }
1716 
1717   // Built-in functions are parsed here.
1718   // https://sourceware.org/binutils/docs/ld/Builtin-Functions.html.
1719   if (Tok == "ADDR") {
1720     StringRef Name = readParenLiteral();
1721     return {[=](uint64_t Dot) {
1722               return ScriptBase->getOutputSection(Location, Name)->Addr;
1723             },
1724             [=] { return false; },
1725             [=] { return ScriptBase->getOutputSection(Location, Name); }};
1726   }
1727   if (Tok == "LOADADDR") {
1728     StringRef Name = readParenLiteral();
1729     return [=](uint64_t Dot) {
1730       return ScriptBase->getOutputSection(Location, Name)->getLMA();
1731     };
1732   }
1733   if (Tok == "ASSERT")
1734     return readAssert();
1735   if (Tok == "ALIGN") {
1736     expect("(");
1737     Expr E = readExpr();
1738     if (consume(",")) {
1739       Expr E2 = readExpr();
1740       expect(")");
1741       return [=](uint64_t Dot) { return alignTo(E(Dot), E2(Dot)); };
1742     }
1743     expect(")");
1744     return [=](uint64_t Dot) { return alignTo(Dot, E(Dot)); };
1745   }
1746   if (Tok == "CONSTANT") {
1747     StringRef Name = readParenLiteral();
1748     return [=](uint64_t Dot) { return getConstant(Name); };
1749   }
1750   if (Tok == "DEFINED") {
1751     StringRef Name = readParenLiteral();
1752     return [=](uint64_t Dot) { return ScriptBase->isDefined(Name) ? 1 : 0; };
1753   }
1754   if (Tok == "SEGMENT_START") {
1755     expect("(");
1756     skip();
1757     expect(",");
1758     Expr E = readExpr();
1759     expect(")");
1760     return [=](uint64_t Dot) { return E(Dot); };
1761   }
1762   if (Tok == "DATA_SEGMENT_ALIGN") {
1763     expect("(");
1764     Expr E = readExpr();
1765     expect(",");
1766     readExpr();
1767     expect(")");
1768     return [=](uint64_t Dot) { return alignTo(Dot, E(Dot)); };
1769   }
1770   if (Tok == "DATA_SEGMENT_END") {
1771     expect("(");
1772     expect(".");
1773     expect(")");
1774     return [](uint64_t Dot) { return Dot; };
1775   }
1776   // GNU linkers implements more complicated logic to handle
1777   // DATA_SEGMENT_RELRO_END. We instead ignore the arguments and just align to
1778   // the next page boundary for simplicity.
1779   if (Tok == "DATA_SEGMENT_RELRO_END") {
1780     expect("(");
1781     readExpr();
1782     expect(",");
1783     readExpr();
1784     expect(")");
1785     return [](uint64_t Dot) { return alignTo(Dot, Target->PageSize); };
1786   }
1787   if (Tok == "SIZEOF") {
1788     StringRef Name = readParenLiteral();
1789     return [=](uint64_t Dot) { return ScriptBase->getOutputSectionSize(Name); };
1790   }
1791   if (Tok == "ALIGNOF") {
1792     StringRef Name = readParenLiteral();
1793     return [=](uint64_t Dot) {
1794       return ScriptBase->getOutputSection(Location, Name)->Addralign;
1795     };
1796   }
1797   if (Tok == "SIZEOF_HEADERS")
1798     return [=](uint64_t Dot) { return ScriptBase->getHeaderSize(); };
1799 
1800   // Tok is a literal number.
1801   uint64_t V;
1802   if (readInteger(Tok, V))
1803     return [=](uint64_t Dot) { return V; };
1804 
1805   // Tok is a symbol name.
1806   if (Tok != "." && !isValidCIdentifier(Tok))
1807     setError("malformed number: " + Tok);
1808   return {[=](uint64_t Dot) { return getSymbolValue(Location, Tok, Dot); },
1809           [=] { return isAbsolute(Tok); },
1810           [=] { return ScriptBase->getSymbolSection(Tok); }};
1811 }
1812 
1813 Expr ScriptParser::readTernary(Expr Cond) {
1814   Expr L = readExpr();
1815   expect(":");
1816   Expr R = readExpr();
1817   return [=](uint64_t Dot) { return Cond(Dot) ? L(Dot) : R(Dot); };
1818 }
1819 
1820 Expr ScriptParser::readParenExpr() {
1821   expect("(");
1822   Expr E = readExpr();
1823   expect(")");
1824   return E;
1825 }
1826 
1827 std::vector<StringRef> ScriptParser::readOutputSectionPhdrs() {
1828   std::vector<StringRef> Phdrs;
1829   while (!Error && peek().startswith(":")) {
1830     StringRef Tok = next();
1831     Phdrs.push_back((Tok.size() == 1) ? next() : Tok.substr(1));
1832   }
1833   return Phdrs;
1834 }
1835 
1836 // Read a program header type name. The next token must be a
1837 // name of a program header type or a constant (e.g. "0x3").
1838 unsigned ScriptParser::readPhdrType() {
1839   StringRef Tok = next();
1840   uint64_t Val;
1841   if (readInteger(Tok, Val))
1842     return Val;
1843 
1844   unsigned Ret = StringSwitch<unsigned>(Tok)
1845                      .Case("PT_NULL", PT_NULL)
1846                      .Case("PT_LOAD", PT_LOAD)
1847                      .Case("PT_DYNAMIC", PT_DYNAMIC)
1848                      .Case("PT_INTERP", PT_INTERP)
1849                      .Case("PT_NOTE", PT_NOTE)
1850                      .Case("PT_SHLIB", PT_SHLIB)
1851                      .Case("PT_PHDR", PT_PHDR)
1852                      .Case("PT_TLS", PT_TLS)
1853                      .Case("PT_GNU_EH_FRAME", PT_GNU_EH_FRAME)
1854                      .Case("PT_GNU_STACK", PT_GNU_STACK)
1855                      .Case("PT_GNU_RELRO", PT_GNU_RELRO)
1856                      .Case("PT_OPENBSD_RANDOMIZE", PT_OPENBSD_RANDOMIZE)
1857                      .Case("PT_OPENBSD_WXNEEDED", PT_OPENBSD_WXNEEDED)
1858                      .Case("PT_OPENBSD_BOOTDATA", PT_OPENBSD_BOOTDATA)
1859                      .Default(-1);
1860 
1861   if (Ret == (unsigned)-1) {
1862     setError("invalid program header type: " + Tok);
1863     return PT_NULL;
1864   }
1865   return Ret;
1866 }
1867 
1868 // Reads a list of symbols, e.g. "{ global: foo; bar; local: *; };".
1869 void ScriptParser::readAnonymousDeclaration() {
1870   // Read global symbols first. "global:" is default, so if there's
1871   // no label, we assume global symbols.
1872   if (consume("global:") || peek() != "local:")
1873     Config->VersionScriptGlobals = readSymbols();
1874 
1875   // Next, read local symbols.
1876   if (consume("local:")) {
1877     if (consume("*")) {
1878       Config->DefaultSymbolVersion = VER_NDX_LOCAL;
1879       expect(";");
1880     } else {
1881       setError("local symbol list for anonymous version is not supported");
1882     }
1883   }
1884   expect("}");
1885   expect(";");
1886 }
1887 
1888 // Reads a list of symbols, e.g. "VerStr { global: foo; bar; local: *; };".
1889 void ScriptParser::readVersionDeclaration(StringRef VerStr) {
1890   // Identifiers start at 2 because 0 and 1 are reserved
1891   // for VER_NDX_LOCAL and VER_NDX_GLOBAL constants.
1892   uint16_t VersionId = Config->VersionDefinitions.size() + 2;
1893   Config->VersionDefinitions.push_back({VerStr, VersionId});
1894 
1895   // Read global symbols.
1896   if (consume("global:") || peek() != "local:")
1897     Config->VersionDefinitions.back().Globals = readSymbols();
1898 
1899   // Read local symbols.
1900   if (consume("local:")) {
1901     if (consume("*")) {
1902       Config->DefaultSymbolVersion = VER_NDX_LOCAL;
1903       expect(";");
1904     } else {
1905       for (SymbolVersion V : readSymbols())
1906         Config->VersionScriptLocals.push_back(V);
1907     }
1908   }
1909   expect("}");
1910 
1911   // Each version may have a parent version. For example, "Ver2"
1912   // defined as "Ver2 { global: foo; local: *; } Ver1;" has "Ver1"
1913   // as a parent. This version hierarchy is, probably against your
1914   // instinct, purely for hint; the runtime doesn't care about it
1915   // at all. In LLD, we simply ignore it.
1916   if (peek() != ";")
1917     skip();
1918   expect(";");
1919 }
1920 
1921 // Reads a list of symbols for a versions cript.
1922 std::vector<SymbolVersion> ScriptParser::readSymbols() {
1923   std::vector<SymbolVersion> Ret;
1924   for (;;) {
1925     if (consume("extern")) {
1926       for (SymbolVersion V : readVersionExtern())
1927         Ret.push_back(V);
1928       continue;
1929     }
1930 
1931     if (peek() == "}" || peek() == "local:" || Error)
1932       break;
1933     StringRef Tok = next();
1934     Ret.push_back({unquote(Tok), false, hasWildcard(Tok)});
1935     expect(";");
1936   }
1937   return Ret;
1938 }
1939 
1940 // Reads an "extern C++" directive, e.g.,
1941 // "extern "C++" { ns::*; "f(int, double)"; };"
1942 std::vector<SymbolVersion> ScriptParser::readVersionExtern() {
1943   StringRef Tok = next();
1944   bool IsCXX = Tok == "\"C++\"";
1945   if (!IsCXX && Tok != "\"C\"")
1946     setError("Unknown language");
1947   expect("{");
1948 
1949   std::vector<SymbolVersion> Ret;
1950   while (!Error && peek() != "}") {
1951     StringRef Tok = next();
1952     bool HasWildcard = !Tok.startswith("\"") && hasWildcard(Tok);
1953     Ret.push_back({unquote(Tok), IsCXX, HasWildcard});
1954     expect(";");
1955   }
1956 
1957   expect("}");
1958   expect(";");
1959   return Ret;
1960 }
1961 
1962 void elf::readLinkerScript(MemoryBufferRef MB) {
1963   ScriptParser(MB).readLinkerScript();
1964 }
1965 
1966 void elf::readVersionScript(MemoryBufferRef MB) {
1967   ScriptParser(MB).readVersionScript();
1968 }
1969 
1970 void elf::readDynamicList(MemoryBufferRef MB) {
1971   ScriptParser(MB).readDynamicList();
1972 }
1973 
1974 template class elf::LinkerScript<ELF32LE>;
1975 template class elf::LinkerScript<ELF32BE>;
1976 template class elf::LinkerScript<ELF64LE>;
1977 template class elf::LinkerScript<ELF64BE>;
1978