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