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(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<ELFT>>(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       Dot = AssignCmd->Expression(Dot);
468       CurOutSec->Size = Dot - CurOutSec->Addr;
469       return;
470     }
471     assignSectionSymbol<ELFT>(AssignCmd, Dot);
472     return;
473   }
474 
475   // Handle BYTE(), SHORT(), LONG(), or QUAD().
476   if (auto *DataCmd = dyn_cast<BytesDataCommand>(&Base)) {
477     DataCmd->Offset = Dot - CurOutSec->Addr;
478     Dot += DataCmd->Size;
479     CurOutSec->Size = Dot - CurOutSec->Addr;
480     return;
481   }
482 
483   if (auto *AssertCmd = dyn_cast<AssertCommand>(&Base)) {
484     AssertCmd->Expression(Dot);
485     return;
486   }
487 
488   // It handles single input section description command,
489   // calculates and assigns the offsets for each section and also
490   // updates the output section size.
491   auto &ICmd = cast<InputSectionDescription>(Base);
492   for (InputSectionData *ID : ICmd.Sections) {
493     // We tentatively added all synthetic sections at the beginning and removed
494     // empty ones afterwards (because there is no way to know whether they were
495     // going be empty or not other than actually running linker scripts.)
496     // We need to ignore remains of empty sections.
497     if (auto *Sec = dyn_cast<SyntheticSection<ELFT>>(ID))
498       if (Sec->empty())
499         continue;
500 
501     auto *IB = static_cast<InputSectionBase<ELFT> *>(ID);
502     switchTo(IB->OutSec);
503     if (auto *I = dyn_cast<InputSection<ELFT>>(IB))
504       output(I);
505     else
506       flush();
507   }
508 }
509 
510 template <class ELFT>
511 static std::vector<OutputSectionBase *>
512 findSections(StringRef Name, const std::vector<OutputSectionBase *> &Sections) {
513   std::vector<OutputSectionBase *> Ret;
514   for (OutputSectionBase *Sec : Sections)
515     if (Sec->getName() == Name)
516       Ret.push_back(Sec);
517   return Ret;
518 }
519 
520 // This function assigns offsets to input sections and an output section
521 // for a single sections command (e.g. ".text { *(.text); }").
522 template <class ELFT>
523 void LinkerScript<ELFT>::assignOffsets(OutputSectionCommand *Cmd) {
524   if (Cmd->LMAExpr)
525     LMAOffset = Cmd->LMAExpr(Dot) - Dot;
526   std::vector<OutputSectionBase *> Sections =
527       findSections<ELFT>(Cmd->Name, *OutputSections);
528   if (Sections.empty())
529     return;
530   switchTo(Sections[0]);
531 
532   // Find the last section output location. We will output orphan sections
533   // there so that end symbols point to the correct location.
534   auto E = std::find_if(Cmd->Commands.rbegin(), Cmd->Commands.rend(),
535                         [](const std::unique_ptr<BaseCommand> &Cmd) {
536                           return !isa<SymbolAssignment>(*Cmd);
537                         })
538                .base();
539   for (auto I = Cmd->Commands.begin(); I != E; ++I)
540     process(**I);
541   for (OutputSectionBase *Base : Sections)
542     switchTo(Base);
543   flush();
544   std::for_each(E, Cmd->Commands.end(),
545                 [this](std::unique_ptr<BaseCommand> &B) { process(*B.get()); });
546 }
547 
548 template <class ELFT> void LinkerScript<ELFT>::removeEmptyCommands() {
549   // It is common practice to use very generic linker scripts. So for any
550   // given run some of the output sections in the script will be empty.
551   // We could create corresponding empty output sections, but that would
552   // clutter the output.
553   // We instead remove trivially empty sections. The bfd linker seems even
554   // more aggressive at removing them.
555   auto Pos = std::remove_if(
556       Opt.Commands.begin(), Opt.Commands.end(),
557       [&](const std::unique_ptr<BaseCommand> &Base) {
558         if (auto *Cmd = dyn_cast<OutputSectionCommand>(Base.get()))
559           return findSections<ELFT>(Cmd->Name, *OutputSections).empty();
560         return false;
561       });
562   Opt.Commands.erase(Pos, Opt.Commands.end());
563 }
564 
565 static bool isAllSectionDescription(const OutputSectionCommand &Cmd) {
566   for (const std::unique_ptr<BaseCommand> &I : Cmd.Commands)
567     if (!isa<InputSectionDescription>(*I))
568       return false;
569   return true;
570 }
571 
572 template <class ELFT> void LinkerScript<ELFT>::adjustSectionsBeforeSorting() {
573   // If the output section contains only symbol assignments, create a
574   // corresponding output section. The bfd linker seems to only create them if
575   // '.' is assigned to, but creating these section should not have any bad
576   // consequeces and gives us a section to put the symbol in.
577   uintX_t Flags = SHF_ALLOC;
578   uint32_t Type = SHT_NOBITS;
579   for (const std::unique_ptr<BaseCommand> &Base : Opt.Commands) {
580     auto *Cmd = dyn_cast<OutputSectionCommand>(Base.get());
581     if (!Cmd)
582       continue;
583     std::vector<OutputSectionBase *> Secs =
584         findSections<ELFT>(Cmd->Name, *OutputSections);
585     if (!Secs.empty()) {
586       Flags = Secs[0]->Flags;
587       Type = Secs[0]->Type;
588       continue;
589     }
590 
591     if (isAllSectionDescription(*Cmd))
592       continue;
593 
594     auto *OutSec = make<OutputSection<ELFT>>(Cmd->Name, Type, Flags);
595     OutputSections->push_back(OutSec);
596   }
597 }
598 
599 template <class ELFT> void LinkerScript<ELFT>::adjustSectionsAfterSorting() {
600   placeOrphanSections();
601 
602   // If output section command doesn't specify any segments,
603   // and we haven't previously assigned any section to segment,
604   // then we simply assign section to the very first load segment.
605   // Below is an example of such linker script:
606   // PHDRS { seg PT_LOAD; }
607   // SECTIONS { .aaa : { *(.aaa) } }
608   std::vector<StringRef> DefPhdrs;
609   auto FirstPtLoad =
610       std::find_if(Opt.PhdrsCommands.begin(), Opt.PhdrsCommands.end(),
611                    [](const PhdrsCommand &Cmd) { return Cmd.Type == PT_LOAD; });
612   if (FirstPtLoad != Opt.PhdrsCommands.end())
613     DefPhdrs.push_back(FirstPtLoad->Name);
614 
615   // Walk the commands and propagate the program headers to commands that don't
616   // explicitly specify them.
617   for (const std::unique_ptr<BaseCommand> &Base : Opt.Commands) {
618     auto *Cmd = dyn_cast<OutputSectionCommand>(Base.get());
619     if (!Cmd)
620       continue;
621     if (Cmd->Phdrs.empty())
622       Cmd->Phdrs = DefPhdrs;
623     else
624       DefPhdrs = Cmd->Phdrs;
625   }
626 
627   removeEmptyCommands();
628 }
629 
630 // When placing orphan sections, we want to place them after symbol assignments
631 // so that an orphan after
632 //   begin_foo = .;
633 //   foo : { *(foo) }
634 //   end_foo = .;
635 // doesn't break the intended meaning of the begin/end symbols.
636 // We don't want to go over sections since Writer<ELFT>::sortSections is the
637 // one in charge of deciding the order of the sections.
638 // We don't want to go over alignments, since doing so in
639 //  rx_sec : { *(rx_sec) }
640 //  . = ALIGN(0x1000);
641 //  /* The RW PT_LOAD starts here*/
642 //  rw_sec : { *(rw_sec) }
643 // would mean that the RW PT_LOAD would become unaligned.
644 static bool shouldSkip(const BaseCommand &Cmd) {
645   if (isa<OutputSectionCommand>(Cmd))
646     return false;
647   const auto *Assign = dyn_cast<SymbolAssignment>(&Cmd);
648   if (!Assign)
649     return true;
650   return Assign->Name != ".";
651 }
652 
653 // Orphan sections are sections present in the input files which are not
654 // explicitly placed into the output file by the linker script. This just
655 // places them in the order already decided in OutputSections.
656 template <class ELFT>
657 void LinkerScript<ELFT>::placeOrphanSections() {
658   // The OutputSections are already in the correct order.
659   // This loops creates or moves commands as needed so that they are in the
660   // correct order.
661   int CmdIndex = 0;
662 
663   // As a horrible special case, skip the first . assignment if it is before any
664   // section. We do this because it is common to set a load address by starting
665   // the script with ". = 0xabcd" and the expectation is that every section is
666   // after that.
667   auto FirstSectionOrDotAssignment =
668       std::find_if(Opt.Commands.begin(), Opt.Commands.end(),
669                    [](const std::unique_ptr<BaseCommand> &Cmd) {
670                      if (isa<OutputSectionCommand>(*Cmd))
671                        return true;
672                      const auto *Assign = dyn_cast<SymbolAssignment>(Cmd.get());
673                      if (!Assign)
674                        return false;
675                      return Assign->Name == ".";
676                    });
677   if (FirstSectionOrDotAssignment != Opt.Commands.end()) {
678     CmdIndex = FirstSectionOrDotAssignment - Opt.Commands.begin();
679     if (isa<SymbolAssignment>(**FirstSectionOrDotAssignment))
680       ++CmdIndex;
681   }
682 
683   for (OutputSectionBase *Sec : *OutputSections) {
684     StringRef Name = Sec->getName();
685 
686     // Find the last spot where we can insert a command and still get the
687     // correct result.
688     auto CmdIter = Opt.Commands.begin() + CmdIndex;
689     auto E = Opt.Commands.end();
690     while (CmdIter != E && shouldSkip(**CmdIter)) {
691       ++CmdIter;
692       ++CmdIndex;
693     }
694 
695     auto Pos =
696         std::find_if(CmdIter, E, [&](const std::unique_ptr<BaseCommand> &Base) {
697           auto *Cmd = dyn_cast<OutputSectionCommand>(Base.get());
698           return Cmd && Cmd->Name == Name;
699         });
700     if (Pos == E) {
701       Opt.Commands.insert(CmdIter,
702                           llvm::make_unique<OutputSectionCommand>(Name));
703       ++CmdIndex;
704       continue;
705     }
706 
707     // Continue from where we found it.
708     CmdIndex = (Pos - Opt.Commands.begin()) + 1;
709   }
710 }
711 
712 template <class ELFT>
713 void LinkerScript<ELFT>::assignAddresses(std::vector<PhdrEntry<ELFT>> &Phdrs) {
714   // Assign addresses as instructed by linker script SECTIONS sub-commands.
715   Dot = 0;
716 
717   for (const std::unique_ptr<BaseCommand> &Base : Opt.Commands) {
718     if (auto *Cmd = dyn_cast<SymbolAssignment>(Base.get())) {
719       if (Cmd->Name == ".") {
720         Dot = Cmd->Expression(Dot);
721       } else if (Cmd->Sym) {
722         assignSectionSymbol<ELFT>(Cmd, Dot);
723       }
724       continue;
725     }
726 
727     if (auto *Cmd = dyn_cast<AssertCommand>(Base.get())) {
728       Cmd->Expression(Dot);
729       continue;
730     }
731 
732     auto *Cmd = cast<OutputSectionCommand>(Base.get());
733     if (Cmd->AddrExpr)
734       Dot = Cmd->AddrExpr(Dot);
735     assignOffsets(Cmd);
736   }
737 
738   uintX_t MinVA = std::numeric_limits<uintX_t>::max();
739   for (OutputSectionBase *Sec : *OutputSections) {
740     if (Sec->Flags & SHF_ALLOC)
741       MinVA = std::min<uint64_t>(MinVA, Sec->Addr);
742     else
743       Sec->Addr = 0;
744   }
745 
746   uintX_t HeaderSize = getHeaderSize();
747   auto FirstPTLoad =
748       std::find_if(Phdrs.begin(), Phdrs.end(), [](const PhdrEntry<ELFT> &E) {
749         return E.H.p_type == PT_LOAD;
750       });
751   if (FirstPTLoad == Phdrs.end())
752     return;
753 
754   // If the linker script doesn't have PHDRS, add ElfHeader and ProgramHeaders
755   // now that we know we have space.
756   if (HeaderSize <= MinVA && !hasPhdrsCommands()) {
757     FirstPTLoad->First = Out<ELFT>::ElfHeader;
758     if (!FirstPTLoad->Last)
759       FirstPTLoad->Last = Out<ELFT>::ProgramHeaders;
760   }
761 
762   // ELF and Program headers need to be right before the first section in
763   // memory. Set their addresses accordingly.
764   MinVA = alignDown(MinVA - HeaderSize, Config->MaxPageSize);
765   Out<ELFT>::ElfHeader->Addr = MinVA;
766   Out<ELFT>::ProgramHeaders->Addr = Out<ELFT>::ElfHeader->Size + MinVA;
767 
768   if (!FirstPTLoad->First) {
769     // Sometimes the very first PT_LOAD segment can be empty.
770     // This happens if (all conditions met):
771     //  - Linker script is used
772     //  - First section in ELF image is not RO
773     //  - Not enough space for program headers.
774     // The code below removes empty PT_LOAD segment and updates
775     // program headers size.
776     Phdrs.erase(FirstPTLoad);
777     Out<ELFT>::ProgramHeaders->Size =
778         sizeof(typename ELFT::Phdr) * Phdrs.size();
779   }
780 }
781 
782 // Creates program headers as instructed by PHDRS linker script command.
783 template <class ELFT>
784 std::vector<PhdrEntry<ELFT>> LinkerScript<ELFT>::createPhdrs() {
785   std::vector<PhdrEntry<ELFT>> Ret;
786 
787   // Process PHDRS and FILEHDR keywords because they are not
788   // real output sections and cannot be added in the following loop.
789   for (const PhdrsCommand &Cmd : Opt.PhdrsCommands) {
790     Ret.emplace_back(Cmd.Type, Cmd.Flags == UINT_MAX ? PF_R : Cmd.Flags);
791     PhdrEntry<ELFT> &Phdr = Ret.back();
792 
793     if (Cmd.HasFilehdr)
794       Phdr.add(Out<ELFT>::ElfHeader);
795     if (Cmd.HasPhdrs)
796       Phdr.add(Out<ELFT>::ProgramHeaders);
797 
798     if (Cmd.LMAExpr) {
799       Phdr.H.p_paddr = Cmd.LMAExpr(0);
800       Phdr.HasLMA = true;
801     }
802   }
803 
804   // Add output sections to program headers.
805   for (OutputSectionBase *Sec : *OutputSections) {
806     if (!(Sec->Flags & SHF_ALLOC))
807       break;
808 
809     // Assign headers specified by linker script
810     for (size_t Id : getPhdrIndices(Sec->getName())) {
811       Ret[Id].add(Sec);
812       if (Opt.PhdrsCommands[Id].Flags == UINT_MAX)
813         Ret[Id].H.p_flags |= Sec->getPhdrFlags();
814     }
815   }
816   return Ret;
817 }
818 
819 template <class ELFT> bool LinkerScript<ELFT>::ignoreInterpSection() {
820   // Ignore .interp section in case we have PHDRS specification
821   // and PT_INTERP isn't listed.
822   return !Opt.PhdrsCommands.empty() &&
823          llvm::find_if(Opt.PhdrsCommands, [](const PhdrsCommand &Cmd) {
824            return Cmd.Type == PT_INTERP;
825          }) == Opt.PhdrsCommands.end();
826 }
827 
828 template <class ELFT>
829 uint32_t LinkerScript<ELFT>::getFiller(StringRef Name) {
830   for (const std::unique_ptr<BaseCommand> &Base : Opt.Commands)
831     if (auto *Cmd = dyn_cast<OutputSectionCommand>(Base.get()))
832       if (Cmd->Name == Name)
833         return Cmd->Filler;
834   return 0;
835 }
836 
837 template <class ELFT>
838 static void writeInt(uint8_t *Buf, uint64_t Data, uint64_t Size) {
839   const endianness E = ELFT::TargetEndianness;
840 
841   switch (Size) {
842   case 1:
843     *Buf = (uint8_t)Data;
844     break;
845   case 2:
846     write16<E>(Buf, Data);
847     break;
848   case 4:
849     write32<E>(Buf, Data);
850     break;
851   case 8:
852     write64<E>(Buf, Data);
853     break;
854   default:
855     llvm_unreachable("unsupported Size argument");
856   }
857 }
858 
859 template <class ELFT>
860 void LinkerScript<ELFT>::writeDataBytes(StringRef Name, uint8_t *Buf) {
861   int I = getSectionIndex(Name);
862   if (I == INT_MAX)
863     return;
864 
865   auto *Cmd = dyn_cast<OutputSectionCommand>(Opt.Commands[I].get());
866   for (const std::unique_ptr<BaseCommand> &Base : Cmd->Commands)
867     if (auto *Data = dyn_cast<BytesDataCommand>(Base.get()))
868       writeInt<ELFT>(Buf + Data->Offset, Data->Data, Data->Size);
869 }
870 
871 template <class ELFT> bool LinkerScript<ELFT>::hasLMA(StringRef Name) {
872   for (const std::unique_ptr<BaseCommand> &Base : Opt.Commands)
873     if (auto *Cmd = dyn_cast<OutputSectionCommand>(Base.get()))
874       if (Cmd->LMAExpr && Cmd->Name == Name)
875         return true;
876   return false;
877 }
878 
879 // Returns the index of the given section name in linker script
880 // SECTIONS commands. Sections are laid out as the same order as they
881 // were in the script. If a given name did not appear in the script,
882 // it returns INT_MAX, so that it will be laid out at end of file.
883 template <class ELFT> int LinkerScript<ELFT>::getSectionIndex(StringRef Name) {
884   for (int I = 0, E = Opt.Commands.size(); I != E; ++I)
885     if (auto *Cmd = dyn_cast<OutputSectionCommand>(Opt.Commands[I].get()))
886       if (Cmd->Name == Name)
887         return I;
888   return INT_MAX;
889 }
890 
891 template <class ELFT> bool LinkerScript<ELFT>::hasPhdrsCommands() {
892   return !Opt.PhdrsCommands.empty();
893 }
894 
895 template <class ELFT>
896 const OutputSectionBase *LinkerScript<ELFT>::getOutputSection(const Twine &Loc,
897                                                               StringRef Name) {
898   static OutputSectionBase FakeSec("", 0, 0);
899 
900   for (OutputSectionBase *Sec : *OutputSections)
901     if (Sec->getName() == Name)
902       return Sec;
903 
904   error(Loc + ": undefined section " + Name);
905   return &FakeSec;
906 }
907 
908 // This function is essentially the same as getOutputSection(Name)->Size,
909 // but it won't print out an error message if a given section is not found.
910 //
911 // Linker script does not create an output section if its content is empty.
912 // We want to allow SIZEOF(.foo) where .foo is a section which happened to
913 // be empty. That is why this function is different from getOutputSection().
914 template <class ELFT>
915 uint64_t LinkerScript<ELFT>::getOutputSectionSize(StringRef Name) {
916   for (OutputSectionBase *Sec : *OutputSections)
917     if (Sec->getName() == Name)
918       return Sec->Size;
919   return 0;
920 }
921 
922 template <class ELFT> uint64_t LinkerScript<ELFT>::getHeaderSize() {
923   return elf::getHeaderSize<ELFT>();
924 }
925 
926 template <class ELFT> uint64_t LinkerScript<ELFT>::getSymbolValue(StringRef S) {
927   if (SymbolBody *B = Symtab<ELFT>::X->find(S))
928     return B->getVA<ELFT>();
929   error("symbol not found: " + S);
930   return 0;
931 }
932 
933 template <class ELFT> bool LinkerScript<ELFT>::isDefined(StringRef S) {
934   return Symtab<ELFT>::X->find(S) != nullptr;
935 }
936 
937 template <class ELFT> bool LinkerScript<ELFT>::isAbsolute(StringRef S) {
938   SymbolBody *Sym = Symtab<ELFT>::X->find(S);
939   auto *DR = dyn_cast_or_null<DefinedRegular<ELFT>>(Sym);
940   return DR && !DR->Section;
941 }
942 
943 // Gets section symbol belongs to. Symbol "." doesn't belong to any
944 // specific section but isn't absolute at the same time, so we try
945 // to find suitable section for it as well.
946 template <class ELFT>
947 const OutputSectionBase *LinkerScript<ELFT>::getSymbolSection(StringRef S) {
948   SymbolBody *Sym = Symtab<ELFT>::X->find(S);
949   if (!Sym) {
950     if (OutputSections->empty())
951       return nullptr;
952     return CurOutSec ? CurOutSec : (*OutputSections)[0];
953   }
954 
955   if (auto *DR = dyn_cast_or_null<DefinedRegular<ELFT>>(Sym))
956     return DR->Section ? DR->Section->OutSec : nullptr;
957   if (auto *DS = dyn_cast_or_null<DefinedSynthetic<ELFT>>(Sym))
958     return DS->Section;
959 
960   return nullptr;
961 }
962 
963 // Returns indices of ELF headers containing specific section, identified
964 // by Name. Each index is a zero based number of ELF header listed within
965 // PHDRS {} script block.
966 template <class ELFT>
967 std::vector<size_t> LinkerScript<ELFT>::getPhdrIndices(StringRef SectionName) {
968   for (const std::unique_ptr<BaseCommand> &Base : Opt.Commands) {
969     auto *Cmd = dyn_cast<OutputSectionCommand>(Base.get());
970     if (!Cmd || Cmd->Name != SectionName)
971       continue;
972 
973     std::vector<size_t> Ret;
974     for (StringRef PhdrName : Cmd->Phdrs)
975       Ret.push_back(getPhdrIndex(Cmd->Location, PhdrName));
976     return Ret;
977   }
978   return {};
979 }
980 
981 template <class ELFT>
982 size_t LinkerScript<ELFT>::getPhdrIndex(const Twine &Loc, StringRef PhdrName) {
983   size_t I = 0;
984   for (PhdrsCommand &Cmd : Opt.PhdrsCommands) {
985     if (Cmd.Name == PhdrName)
986       return I;
987     ++I;
988   }
989   error(Loc + ": section header '" + PhdrName + "' is not listed in PHDRS");
990   return 0;
991 }
992 
993 class elf::ScriptParser final : public ScriptParserBase {
994   typedef void (ScriptParser::*Handler)();
995 
996 public:
997   ScriptParser(MemoryBufferRef MB)
998       : ScriptParserBase(MB),
999         IsUnderSysroot(isUnderSysroot(MB.getBufferIdentifier())) {}
1000 
1001   void readLinkerScript();
1002   void readVersionScript();
1003 
1004 private:
1005   void addFile(StringRef Path);
1006 
1007   void readAsNeeded();
1008   void readEntry();
1009   void readExtern();
1010   void readGroup();
1011   void readInclude();
1012   void readOutput();
1013   void readOutputArch();
1014   void readOutputFormat();
1015   void readPhdrs();
1016   void readSearchDir();
1017   void readSections();
1018   void readVersion();
1019   void readVersionScriptCommand();
1020 
1021   SymbolAssignment *readAssignment(StringRef Name);
1022   BytesDataCommand *readBytesDataCommand(StringRef Tok);
1023   uint32_t readFill();
1024   OutputSectionCommand *readOutputSectionDescription(StringRef OutSec);
1025   uint32_t readOutputSectionFiller(StringRef Tok);
1026   std::vector<StringRef> readOutputSectionPhdrs();
1027   InputSectionDescription *readInputSectionDescription(StringRef Tok);
1028   StringMatcher readFilePatterns();
1029   std::vector<SectionPattern> readInputSectionsList();
1030   InputSectionDescription *readInputSectionRules(StringRef FilePattern);
1031   unsigned readPhdrType();
1032   SortSectionPolicy readSortKind();
1033   SymbolAssignment *readProvideHidden(bool Provide, bool Hidden);
1034   SymbolAssignment *readProvideOrAssignment(StringRef Tok);
1035   void readSort();
1036   Expr readAssert();
1037 
1038   Expr readExpr();
1039   Expr readExpr1(Expr Lhs, int MinPrec);
1040   StringRef readParenLiteral();
1041   Expr readPrimary();
1042   Expr readTernary(Expr Cond);
1043   Expr readParenExpr();
1044 
1045   // For parsing version script.
1046   std::vector<SymbolVersion> readVersionExtern();
1047   void readAnonymousDeclaration();
1048   void readVersionDeclaration(StringRef VerStr);
1049   std::vector<SymbolVersion> readSymbols();
1050 
1051   ScriptConfiguration &Opt = *ScriptConfig;
1052   bool IsUnderSysroot;
1053   std::vector<std::unique_ptr<MemoryBuffer>> OwningMBs;
1054 };
1055 
1056 void ScriptParser::readVersionScript() {
1057   readVersionScriptCommand();
1058   if (!atEOF())
1059     setError("EOF expected, but got " + next());
1060 }
1061 
1062 void ScriptParser::readVersionScriptCommand() {
1063   if (consume("{")) {
1064     readAnonymousDeclaration();
1065     return;
1066   }
1067 
1068   while (!atEOF() && !Error && peek() != "}") {
1069     StringRef VerStr = next();
1070     if (VerStr == "{") {
1071       setError("anonymous version definition is used in "
1072                "combination with other version definitions");
1073       return;
1074     }
1075     expect("{");
1076     readVersionDeclaration(VerStr);
1077   }
1078 }
1079 
1080 void ScriptParser::readVersion() {
1081   expect("{");
1082   readVersionScriptCommand();
1083   expect("}");
1084 }
1085 
1086 void ScriptParser::readLinkerScript() {
1087   while (!atEOF()) {
1088     StringRef Tok = next();
1089     if (Tok == ";")
1090       continue;
1091 
1092     if (Tok == "ASSERT") {
1093       Opt.Commands.emplace_back(new AssertCommand(readAssert()));
1094     } else if (Tok == "ENTRY") {
1095       readEntry();
1096     } else if (Tok == "EXTERN") {
1097       readExtern();
1098     } else if (Tok == "GROUP" || Tok == "INPUT") {
1099       readGroup();
1100     } else if (Tok == "INCLUDE") {
1101       readInclude();
1102     } else if (Tok == "OUTPUT") {
1103       readOutput();
1104     } else if (Tok == "OUTPUT_ARCH") {
1105       readOutputArch();
1106     } else if (Tok == "OUTPUT_FORMAT") {
1107       readOutputFormat();
1108     } else if (Tok == "PHDRS") {
1109       readPhdrs();
1110     } else if (Tok == "SEARCH_DIR") {
1111       readSearchDir();
1112     } else if (Tok == "SECTIONS") {
1113       readSections();
1114     } else if (Tok == "VERSION") {
1115       readVersion();
1116     } else if (SymbolAssignment *Cmd = readProvideOrAssignment(Tok)) {
1117       Opt.Commands.emplace_back(Cmd);
1118     } else {
1119       setError("unknown directive: " + Tok);
1120     }
1121   }
1122 }
1123 
1124 void ScriptParser::addFile(StringRef S) {
1125   if (IsUnderSysroot && S.startswith("/")) {
1126     SmallString<128> PathData;
1127     StringRef Path = (Config->Sysroot + S).toStringRef(PathData);
1128     if (sys::fs::exists(Path)) {
1129       Driver->addFile(Saver.save(Path));
1130       return;
1131     }
1132   }
1133 
1134   if (sys::path::is_absolute(S)) {
1135     Driver->addFile(S);
1136   } else if (S.startswith("=")) {
1137     if (Config->Sysroot.empty())
1138       Driver->addFile(S.substr(1));
1139     else
1140       Driver->addFile(Saver.save(Config->Sysroot + "/" + S.substr(1)));
1141   } else if (S.startswith("-l")) {
1142     Driver->addLibrary(S.substr(2));
1143   } else if (sys::fs::exists(S)) {
1144     Driver->addFile(S);
1145   } else {
1146     if (Optional<std::string> Path = findFromSearchPaths(S))
1147       Driver->addFile(Saver.save(*Path));
1148     else
1149       setError("unable to find " + S);
1150   }
1151 }
1152 
1153 void ScriptParser::readAsNeeded() {
1154   expect("(");
1155   bool Orig = Config->AsNeeded;
1156   Config->AsNeeded = true;
1157   while (!Error && !consume(")"))
1158     addFile(unquote(next()));
1159   Config->AsNeeded = Orig;
1160 }
1161 
1162 void ScriptParser::readEntry() {
1163   // -e <symbol> takes predecence over ENTRY(<symbol>).
1164   expect("(");
1165   StringRef Tok = next();
1166   if (Config->Entry.empty())
1167     Config->Entry = Tok;
1168   expect(")");
1169 }
1170 
1171 void ScriptParser::readExtern() {
1172   expect("(");
1173   while (!Error && !consume(")"))
1174     Config->Undefined.push_back(next());
1175 }
1176 
1177 void ScriptParser::readGroup() {
1178   expect("(");
1179   while (!Error && !consume(")")) {
1180     StringRef Tok = next();
1181     if (Tok == "AS_NEEDED")
1182       readAsNeeded();
1183     else
1184       addFile(unquote(Tok));
1185   }
1186 }
1187 
1188 void ScriptParser::readInclude() {
1189   StringRef Tok = next();
1190   auto MBOrErr = MemoryBuffer::getFile(unquote(Tok));
1191   if (!MBOrErr) {
1192     setError("cannot open " + Tok);
1193     return;
1194   }
1195   std::unique_ptr<MemoryBuffer> &MB = *MBOrErr;
1196   tokenize(MB->getMemBufferRef());
1197   OwningMBs.push_back(std::move(MB));
1198 }
1199 
1200 void ScriptParser::readOutput() {
1201   // -o <file> takes predecence over OUTPUT(<file>).
1202   expect("(");
1203   StringRef Tok = next();
1204   if (Config->OutputFile.empty())
1205     Config->OutputFile = unquote(Tok);
1206   expect(")");
1207 }
1208 
1209 void ScriptParser::readOutputArch() {
1210   // Error checking only for now.
1211   expect("(");
1212   skip();
1213   expect(")");
1214 }
1215 
1216 void ScriptParser::readOutputFormat() {
1217   // Error checking only for now.
1218   expect("(");
1219   skip();
1220   StringRef Tok = next();
1221   if (Tok == ")")
1222     return;
1223   if (Tok != ",") {
1224     setError("unexpected token: " + Tok);
1225     return;
1226   }
1227   skip();
1228   expect(",");
1229   skip();
1230   expect(")");
1231 }
1232 
1233 void ScriptParser::readPhdrs() {
1234   expect("{");
1235   while (!Error && !consume("}")) {
1236     StringRef Tok = next();
1237     Opt.PhdrsCommands.push_back(
1238         {Tok, PT_NULL, false, false, UINT_MAX, nullptr});
1239     PhdrsCommand &PhdrCmd = Opt.PhdrsCommands.back();
1240 
1241     PhdrCmd.Type = readPhdrType();
1242     do {
1243       Tok = next();
1244       if (Tok == ";")
1245         break;
1246       if (Tok == "FILEHDR")
1247         PhdrCmd.HasFilehdr = true;
1248       else if (Tok == "PHDRS")
1249         PhdrCmd.HasPhdrs = true;
1250       else if (Tok == "AT")
1251         PhdrCmd.LMAExpr = readParenExpr();
1252       else if (Tok == "FLAGS") {
1253         expect("(");
1254         // Passing 0 for the value of dot is a bit of a hack. It means that
1255         // we accept expressions like ".|1".
1256         PhdrCmd.Flags = readExpr()(0);
1257         expect(")");
1258       } else
1259         setError("unexpected header attribute: " + Tok);
1260     } while (!Error);
1261   }
1262 }
1263 
1264 void ScriptParser::readSearchDir() {
1265   expect("(");
1266   StringRef Tok = next();
1267   if (!Config->Nostdlib)
1268     Config->SearchPaths.push_back(unquote(Tok));
1269   expect(")");
1270 }
1271 
1272 void ScriptParser::readSections() {
1273   Opt.HasSections = true;
1274   // -no-rosegment is used to avoid placing read only non-executable sections in
1275   // their own segment. We do the same if SECTIONS command is present in linker
1276   // script. See comment for computeFlags().
1277   Config->SingleRoRx = true;
1278 
1279   expect("{");
1280   while (!Error && !consume("}")) {
1281     StringRef Tok = next();
1282     BaseCommand *Cmd = readProvideOrAssignment(Tok);
1283     if (!Cmd) {
1284       if (Tok == "ASSERT")
1285         Cmd = new AssertCommand(readAssert());
1286       else
1287         Cmd = readOutputSectionDescription(Tok);
1288     }
1289     Opt.Commands.emplace_back(Cmd);
1290   }
1291 }
1292 
1293 static int precedence(StringRef Op) {
1294   return StringSwitch<int>(Op)
1295       .Cases("*", "/", 5)
1296       .Cases("+", "-", 4)
1297       .Cases("<<", ">>", 3)
1298       .Cases("<", "<=", ">", ">=", "==", "!=", 2)
1299       .Cases("&", "|", 1)
1300       .Default(-1);
1301 }
1302 
1303 StringMatcher ScriptParser::readFilePatterns() {
1304   std::vector<StringRef> V;
1305   while (!Error && !consume(")"))
1306     V.push_back(next());
1307   return StringMatcher(V);
1308 }
1309 
1310 SortSectionPolicy ScriptParser::readSortKind() {
1311   if (consume("SORT") || consume("SORT_BY_NAME"))
1312     return SortSectionPolicy::Name;
1313   if (consume("SORT_BY_ALIGNMENT"))
1314     return SortSectionPolicy::Alignment;
1315   if (consume("SORT_BY_INIT_PRIORITY"))
1316     return SortSectionPolicy::Priority;
1317   if (consume("SORT_NONE"))
1318     return SortSectionPolicy::None;
1319   return SortSectionPolicy::Default;
1320 }
1321 
1322 // Method reads a list of sequence of excluded files and section globs given in
1323 // a following form: ((EXCLUDE_FILE(file_pattern+))? section_pattern+)+
1324 // Example: *(.foo.1 EXCLUDE_FILE (*a.o) .foo.2 EXCLUDE_FILE (*b.o) .foo.3)
1325 // The semantics of that is next:
1326 // * Include .foo.1 from every file.
1327 // * Include .foo.2 from every file but a.o
1328 // * Include .foo.3 from every file but b.o
1329 std::vector<SectionPattern> ScriptParser::readInputSectionsList() {
1330   std::vector<SectionPattern> Ret;
1331   while (!Error && peek() != ")") {
1332     StringMatcher ExcludeFilePat;
1333     if (consume("EXCLUDE_FILE")) {
1334       expect("(");
1335       ExcludeFilePat = readFilePatterns();
1336     }
1337 
1338     std::vector<StringRef> V;
1339     while (!Error && peek() != ")" && peek() != "EXCLUDE_FILE")
1340       V.push_back(next());
1341 
1342     if (!V.empty())
1343       Ret.push_back({std::move(ExcludeFilePat), StringMatcher(V)});
1344     else
1345       setError("section pattern is expected");
1346   }
1347   return Ret;
1348 }
1349 
1350 // Reads contents of "SECTIONS" directive. That directive contains a
1351 // list of glob patterns for input sections. The grammar is as follows.
1352 //
1353 // <patterns> ::= <section-list>
1354 //              | <sort> "(" <section-list> ")"
1355 //              | <sort> "(" <sort> "(" <section-list> ")" ")"
1356 //
1357 // <sort>     ::= "SORT" | "SORT_BY_NAME" | "SORT_BY_ALIGNMENT"
1358 //              | "SORT_BY_INIT_PRIORITY" | "SORT_NONE"
1359 //
1360 // <section-list> is parsed by readInputSectionsList().
1361 InputSectionDescription *
1362 ScriptParser::readInputSectionRules(StringRef FilePattern) {
1363   auto *Cmd = new InputSectionDescription(FilePattern);
1364   expect("(");
1365   while (!Error && !consume(")")) {
1366     SortSectionPolicy Outer = readSortKind();
1367     SortSectionPolicy Inner = SortSectionPolicy::Default;
1368     std::vector<SectionPattern> V;
1369     if (Outer != SortSectionPolicy::Default) {
1370       expect("(");
1371       Inner = readSortKind();
1372       if (Inner != SortSectionPolicy::Default) {
1373         expect("(");
1374         V = readInputSectionsList();
1375         expect(")");
1376       } else {
1377         V = readInputSectionsList();
1378       }
1379       expect(")");
1380     } else {
1381       V = readInputSectionsList();
1382     }
1383 
1384     for (SectionPattern &Pat : V) {
1385       Pat.SortInner = Inner;
1386       Pat.SortOuter = Outer;
1387     }
1388 
1389     std::move(V.begin(), V.end(), std::back_inserter(Cmd->SectionPatterns));
1390   }
1391   return Cmd;
1392 }
1393 
1394 InputSectionDescription *
1395 ScriptParser::readInputSectionDescription(StringRef Tok) {
1396   // Input section wildcard can be surrounded by KEEP.
1397   // https://sourceware.org/binutils/docs/ld/Input-Section-Keep.html#Input-Section-Keep
1398   if (Tok == "KEEP") {
1399     expect("(");
1400     StringRef FilePattern = next();
1401     InputSectionDescription *Cmd = readInputSectionRules(FilePattern);
1402     expect(")");
1403     Opt.KeptSections.push_back(Cmd);
1404     return Cmd;
1405   }
1406   return readInputSectionRules(Tok);
1407 }
1408 
1409 void ScriptParser::readSort() {
1410   expect("(");
1411   expect("CONSTRUCTORS");
1412   expect(")");
1413 }
1414 
1415 Expr ScriptParser::readAssert() {
1416   expect("(");
1417   Expr E = readExpr();
1418   expect(",");
1419   StringRef Msg = unquote(next());
1420   expect(")");
1421   return [=](uint64_t Dot) {
1422     uint64_t V = E(Dot);
1423     if (!V)
1424       error(Msg);
1425     return V;
1426   };
1427 }
1428 
1429 // Reads a FILL(expr) command. We handle the FILL command as an
1430 // alias for =fillexp section attribute, which is different from
1431 // what GNU linkers do.
1432 // https://sourceware.org/binutils/docs/ld/Output-Section-Data.html
1433 uint32_t ScriptParser::readFill() {
1434   expect("(");
1435   uint32_t V = readOutputSectionFiller(next());
1436   expect(")");
1437   expect(";");
1438   return V;
1439 }
1440 
1441 OutputSectionCommand *
1442 ScriptParser::readOutputSectionDescription(StringRef OutSec) {
1443   OutputSectionCommand *Cmd = new OutputSectionCommand(OutSec);
1444   Cmd->Location = getCurrentLocation();
1445 
1446   // Read an address expression.
1447   // https://sourceware.org/binutils/docs/ld/Output-Section-Address.html#Output-Section-Address
1448   if (peek() != ":")
1449     Cmd->AddrExpr = readExpr();
1450 
1451   expect(":");
1452 
1453   if (consume("AT"))
1454     Cmd->LMAExpr = readParenExpr();
1455   if (consume("ALIGN"))
1456     Cmd->AlignExpr = readParenExpr();
1457   if (consume("SUBALIGN"))
1458     Cmd->SubalignExpr = readParenExpr();
1459 
1460   // Parse constraints.
1461   if (consume("ONLY_IF_RO"))
1462     Cmd->Constraint = ConstraintKind::ReadOnly;
1463   if (consume("ONLY_IF_RW"))
1464     Cmd->Constraint = ConstraintKind::ReadWrite;
1465   expect("{");
1466 
1467   while (!Error && !consume("}")) {
1468     StringRef Tok = next();
1469     if (SymbolAssignment *Assignment = readProvideOrAssignment(Tok)) {
1470       Cmd->Commands.emplace_back(Assignment);
1471     } else if (BytesDataCommand *Data = readBytesDataCommand(Tok)) {
1472       Cmd->Commands.emplace_back(Data);
1473     } else if (Tok == "ASSERT") {
1474       Cmd->Commands.emplace_back(new AssertCommand(readAssert()));
1475       expect(";");
1476     } else if (Tok == "FILL") {
1477       Cmd->Filler = readFill();
1478     } else if (Tok == "SORT") {
1479       readSort();
1480     } else if (peek() == "(") {
1481       Cmd->Commands.emplace_back(readInputSectionDescription(Tok));
1482     } else {
1483       setError("unknown command " + Tok);
1484     }
1485   }
1486   Cmd->Phdrs = readOutputSectionPhdrs();
1487 
1488   if (consume("="))
1489     Cmd->Filler = readOutputSectionFiller(next());
1490   else if (peek().startswith("="))
1491     Cmd->Filler = readOutputSectionFiller(next().drop_front());
1492 
1493   return Cmd;
1494 }
1495 
1496 // Read "=<number>" where <number> is an octal/decimal/hexadecimal number.
1497 // https://sourceware.org/binutils/docs/ld/Output-Section-Fill.html
1498 //
1499 // ld.gold is not fully compatible with ld.bfd. ld.bfd handles
1500 // hexstrings as blobs of arbitrary sizes, while ld.gold handles them
1501 // as 32-bit big-endian values. We will do the same as ld.gold does
1502 // because it's simpler than what ld.bfd does.
1503 uint32_t ScriptParser::readOutputSectionFiller(StringRef Tok) {
1504   uint32_t V;
1505   if (!Tok.getAsInteger(0, V))
1506     return V;
1507   setError("invalid filler expression: " + Tok);
1508   return 0;
1509 }
1510 
1511 SymbolAssignment *ScriptParser::readProvideHidden(bool Provide, bool Hidden) {
1512   expect("(");
1513   SymbolAssignment *Cmd = readAssignment(next());
1514   Cmd->Provide = Provide;
1515   Cmd->Hidden = Hidden;
1516   expect(")");
1517   expect(";");
1518   return Cmd;
1519 }
1520 
1521 SymbolAssignment *ScriptParser::readProvideOrAssignment(StringRef Tok) {
1522   SymbolAssignment *Cmd = nullptr;
1523   if (peek() == "=" || peek() == "+=") {
1524     Cmd = readAssignment(Tok);
1525     expect(";");
1526   } else if (Tok == "PROVIDE") {
1527     Cmd = readProvideHidden(true, false);
1528   } else if (Tok == "HIDDEN") {
1529     Cmd = readProvideHidden(false, true);
1530   } else if (Tok == "PROVIDE_HIDDEN") {
1531     Cmd = readProvideHidden(true, true);
1532   }
1533   return Cmd;
1534 }
1535 
1536 static uint64_t getSymbolValue(StringRef S, uint64_t Dot) {
1537   if (S == ".")
1538     return Dot;
1539   return ScriptBase->getSymbolValue(S);
1540 }
1541 
1542 static bool isAbsolute(StringRef S) {
1543   if (S == ".")
1544     return false;
1545   return ScriptBase->isAbsolute(S);
1546 }
1547 
1548 SymbolAssignment *ScriptParser::readAssignment(StringRef Name) {
1549   StringRef Op = next();
1550   Expr E;
1551   assert(Op == "=" || Op == "+=");
1552   if (consume("ABSOLUTE")) {
1553     // The RHS may be something like "ABSOLUTE(.) & 0xff".
1554     // Call readExpr1 to read the whole expression.
1555     E = readExpr1(readParenExpr(), 0);
1556     E.IsAbsolute = [] { return true; };
1557   } else {
1558     E = readExpr();
1559   }
1560   if (Op == "+=")
1561     E = [=](uint64_t Dot) { return getSymbolValue(Name, Dot) + E(Dot); };
1562   return new SymbolAssignment(Name, E);
1563 }
1564 
1565 // This is an operator-precedence parser to parse a linker
1566 // script expression.
1567 Expr ScriptParser::readExpr() { return readExpr1(readPrimary(), 0); }
1568 
1569 static Expr combine(StringRef Op, Expr L, Expr R) {
1570   if (Op == "*")
1571     return [=](uint64_t Dot) { return L(Dot) * R(Dot); };
1572   if (Op == "/") {
1573     return [=](uint64_t Dot) -> uint64_t {
1574       uint64_t RHS = R(Dot);
1575       if (RHS == 0) {
1576         error("division by zero");
1577         return 0;
1578       }
1579       return L(Dot) / RHS;
1580     };
1581   }
1582   if (Op == "+")
1583     return {[=](uint64_t Dot) { return L(Dot) + R(Dot); },
1584             [=] { return L.IsAbsolute() && R.IsAbsolute(); },
1585             [=] {
1586               const OutputSectionBase *S = L.Section();
1587               return S ? S : R.Section();
1588             }};
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   if (Op == "&")
1608     return [=](uint64_t Dot) { return L(Dot) & R(Dot); };
1609   if (Op == "|")
1610     return [=](uint64_t Dot) { return L(Dot) | R(Dot); };
1611   llvm_unreachable("invalid operator");
1612 }
1613 
1614 // This is a part of the operator-precedence parser. This function
1615 // assumes that the remaining token stream starts with an operator.
1616 Expr ScriptParser::readExpr1(Expr Lhs, int MinPrec) {
1617   while (!atEOF() && !Error) {
1618     // Read an operator and an expression.
1619     if (consume("?"))
1620       return readTernary(Lhs);
1621     StringRef Op1 = peek();
1622     if (precedence(Op1) < MinPrec)
1623       break;
1624     skip();
1625     Expr Rhs = readPrimary();
1626 
1627     // Evaluate the remaining part of the expression first if the
1628     // next operator has greater precedence than the previous one.
1629     // For example, if we have read "+" and "3", and if the next
1630     // operator is "*", then we'll evaluate 3 * ... part first.
1631     while (!atEOF()) {
1632       StringRef Op2 = peek();
1633       if (precedence(Op2) <= precedence(Op1))
1634         break;
1635       Rhs = readExpr1(Rhs, precedence(Op2));
1636     }
1637 
1638     Lhs = combine(Op1, Lhs, Rhs);
1639   }
1640   return Lhs;
1641 }
1642 
1643 uint64_t static getConstant(StringRef S) {
1644   if (S == "COMMONPAGESIZE")
1645     return Target->PageSize;
1646   if (S == "MAXPAGESIZE")
1647     return Config->MaxPageSize;
1648   error("unknown constant: " + S);
1649   return 0;
1650 }
1651 
1652 // Parses Tok as an integer. Returns true if successful.
1653 // It recognizes hexadecimal (prefixed with "0x" or suffixed with "H")
1654 // and decimal numbers. Decimal numbers may have "K" (kilo) or
1655 // "M" (mega) prefixes.
1656 static bool readInteger(StringRef Tok, uint64_t &Result) {
1657   // Negative number
1658   if (Tok.startswith("-")) {
1659     if (!readInteger(Tok.substr(1), Result))
1660       return false;
1661     Result = -Result;
1662     return true;
1663   }
1664 
1665   // Hexadecimal
1666   if (Tok.startswith_lower("0x"))
1667     return !Tok.substr(2).getAsInteger(16, Result);
1668   if (Tok.endswith_lower("H"))
1669     return !Tok.drop_back().getAsInteger(16, Result);
1670 
1671   // Decimal
1672   int Suffix = 1;
1673   if (Tok.endswith_lower("K")) {
1674     Suffix = 1024;
1675     Tok = Tok.drop_back();
1676   } else if (Tok.endswith_lower("M")) {
1677     Suffix = 1024 * 1024;
1678     Tok = Tok.drop_back();
1679   }
1680   if (Tok.getAsInteger(10, Result))
1681     return false;
1682   Result *= Suffix;
1683   return true;
1684 }
1685 
1686 BytesDataCommand *ScriptParser::readBytesDataCommand(StringRef Tok) {
1687   int Size = StringSwitch<unsigned>(Tok)
1688                  .Case("BYTE", 1)
1689                  .Case("SHORT", 2)
1690                  .Case("LONG", 4)
1691                  .Case("QUAD", 8)
1692                  .Default(-1);
1693   if (Size == -1)
1694     return nullptr;
1695 
1696   expect("(");
1697   uint64_t Val = 0;
1698   StringRef S = next();
1699   if (!readInteger(S, Val))
1700     setError("unexpected value: " + S);
1701   expect(")");
1702   return new BytesDataCommand(Val, Size);
1703 }
1704 
1705 StringRef ScriptParser::readParenLiteral() {
1706   expect("(");
1707   StringRef Tok = next();
1708   expect(")");
1709   return Tok;
1710 }
1711 
1712 Expr ScriptParser::readPrimary() {
1713   if (peek() == "(")
1714     return readParenExpr();
1715 
1716   StringRef Tok = next();
1717   std::string Location = getCurrentLocation();
1718 
1719   if (Tok == "~") {
1720     Expr E = readPrimary();
1721     return [=](uint64_t Dot) { return ~E(Dot); };
1722   }
1723   if (Tok == "-") {
1724     Expr E = readPrimary();
1725     return [=](uint64_t Dot) { return -E(Dot); };
1726   }
1727 
1728   // Built-in functions are parsed here.
1729   // https://sourceware.org/binutils/docs/ld/Builtin-Functions.html.
1730   if (Tok == "ADDR") {
1731     StringRef Name = readParenLiteral();
1732     return {[=](uint64_t Dot) {
1733               return ScriptBase->getOutputSection(Location, Name)->Addr;
1734             },
1735             [=] { return false; },
1736             [=] { return ScriptBase->getOutputSection(Location, Name); }};
1737   }
1738   if (Tok == "LOADADDR") {
1739     StringRef Name = readParenLiteral();
1740     return [=](uint64_t Dot) {
1741       return ScriptBase->getOutputSection(Location, Name)->getLMA();
1742     };
1743   }
1744   if (Tok == "ASSERT")
1745     return readAssert();
1746   if (Tok == "ALIGN") {
1747     Expr E = readParenExpr();
1748     return [=](uint64_t Dot) { return alignTo(Dot, E(Dot)); };
1749   }
1750   if (Tok == "CONSTANT") {
1751     StringRef Name = readParenLiteral();
1752     return [=](uint64_t Dot) { return getConstant(Name); };
1753   }
1754   if (Tok == "DEFINED") {
1755     StringRef Name = readParenLiteral();
1756     return [=](uint64_t Dot) { return ScriptBase->isDefined(Name) ? 1 : 0; };
1757   }
1758   if (Tok == "SEGMENT_START") {
1759     expect("(");
1760     skip();
1761     expect(",");
1762     Expr E = readExpr();
1763     expect(")");
1764     return [=](uint64_t Dot) { return E(Dot); };
1765   }
1766   if (Tok == "DATA_SEGMENT_ALIGN") {
1767     expect("(");
1768     Expr E = readExpr();
1769     expect(",");
1770     readExpr();
1771     expect(")");
1772     return [=](uint64_t Dot) { return alignTo(Dot, E(Dot)); };
1773   }
1774   if (Tok == "DATA_SEGMENT_END") {
1775     expect("(");
1776     expect(".");
1777     expect(")");
1778     return [](uint64_t Dot) { return Dot; };
1779   }
1780   // GNU linkers implements more complicated logic to handle
1781   // DATA_SEGMENT_RELRO_END. We instead ignore the arguments and just align to
1782   // the next page boundary for simplicity.
1783   if (Tok == "DATA_SEGMENT_RELRO_END") {
1784     expect("(");
1785     readExpr();
1786     expect(",");
1787     readExpr();
1788     expect(")");
1789     return [](uint64_t Dot) { return alignTo(Dot, Target->PageSize); };
1790   }
1791   if (Tok == "SIZEOF") {
1792     StringRef Name = readParenLiteral();
1793     return [=](uint64_t Dot) { return ScriptBase->getOutputSectionSize(Name); };
1794   }
1795   if (Tok == "ALIGNOF") {
1796     StringRef Name = readParenLiteral();
1797     return [=](uint64_t Dot) {
1798       return ScriptBase->getOutputSection(Location, Name)->Addralign;
1799     };
1800   }
1801   if (Tok == "SIZEOF_HEADERS")
1802     return [=](uint64_t Dot) { return ScriptBase->getHeaderSize(); };
1803 
1804   // Tok is a literal number.
1805   uint64_t V;
1806   if (readInteger(Tok, V))
1807     return [=](uint64_t Dot) { return V; };
1808 
1809   // Tok is a symbol name.
1810   if (Tok != "." && !isValidCIdentifier(Tok))
1811     setError("malformed number: " + Tok);
1812   return {[=](uint64_t Dot) { return getSymbolValue(Tok, Dot); },
1813           [=] { return isAbsolute(Tok); },
1814           [=] { return ScriptBase->getSymbolSection(Tok); }};
1815 }
1816 
1817 Expr ScriptParser::readTernary(Expr Cond) {
1818   Expr L = readExpr();
1819   expect(":");
1820   Expr R = readExpr();
1821   return [=](uint64_t Dot) { return Cond(Dot) ? L(Dot) : R(Dot); };
1822 }
1823 
1824 Expr ScriptParser::readParenExpr() {
1825   expect("(");
1826   Expr E = readExpr();
1827   expect(")");
1828   return E;
1829 }
1830 
1831 std::vector<StringRef> ScriptParser::readOutputSectionPhdrs() {
1832   std::vector<StringRef> Phdrs;
1833   while (!Error && peek().startswith(":")) {
1834     StringRef Tok = next();
1835     Phdrs.push_back((Tok.size() == 1) ? next() : Tok.substr(1));
1836   }
1837   return Phdrs;
1838 }
1839 
1840 // Read a program header type name. The next token must be a
1841 // name of a program header type or a constant (e.g. "0x3").
1842 unsigned ScriptParser::readPhdrType() {
1843   StringRef Tok = next();
1844   uint64_t Val;
1845   if (readInteger(Tok, Val))
1846     return Val;
1847 
1848   unsigned Ret = StringSwitch<unsigned>(Tok)
1849                      .Case("PT_NULL", PT_NULL)
1850                      .Case("PT_LOAD", PT_LOAD)
1851                      .Case("PT_DYNAMIC", PT_DYNAMIC)
1852                      .Case("PT_INTERP", PT_INTERP)
1853                      .Case("PT_NOTE", PT_NOTE)
1854                      .Case("PT_SHLIB", PT_SHLIB)
1855                      .Case("PT_PHDR", PT_PHDR)
1856                      .Case("PT_TLS", PT_TLS)
1857                      .Case("PT_GNU_EH_FRAME", PT_GNU_EH_FRAME)
1858                      .Case("PT_GNU_STACK", PT_GNU_STACK)
1859                      .Case("PT_GNU_RELRO", PT_GNU_RELRO)
1860                      .Case("PT_OPENBSD_RANDOMIZE", PT_OPENBSD_RANDOMIZE)
1861                      .Case("PT_OPENBSD_WXNEEDED", PT_OPENBSD_WXNEEDED)
1862                      .Default(-1);
1863 
1864   if (Ret == (unsigned)-1) {
1865     setError("invalid program header type: " + Tok);
1866     return PT_NULL;
1867   }
1868   return Ret;
1869 }
1870 
1871 // Reads a list of symbols, e.g. "{ global: foo; bar; local: *; };".
1872 void ScriptParser::readAnonymousDeclaration() {
1873   // Read global symbols first. "global:" is default, so if there's
1874   // no label, we assume global symbols.
1875   if (consume("global:") || peek() != "local:")
1876     Config->VersionScriptGlobals = readSymbols();
1877 
1878   // Next, read local symbols.
1879   if (consume("local:")) {
1880     if (consume("*")) {
1881       Config->DefaultSymbolVersion = VER_NDX_LOCAL;
1882       expect(";");
1883     } else {
1884       setError("local symbol list for anonymous version is not supported");
1885     }
1886   }
1887   expect("}");
1888   expect(";");
1889 }
1890 
1891 // Reads a list of symbols, e.g. "VerStr { global: foo; bar; local: *; };".
1892 void ScriptParser::readVersionDeclaration(StringRef VerStr) {
1893   // Identifiers start at 2 because 0 and 1 are reserved
1894   // for VER_NDX_LOCAL and VER_NDX_GLOBAL constants.
1895   uint16_t VersionId = Config->VersionDefinitions.size() + 2;
1896   Config->VersionDefinitions.push_back({VerStr, VersionId});
1897 
1898   // Read global symbols.
1899   if (consume("global:") || peek() != "local:")
1900     Config->VersionDefinitions.back().Globals = readSymbols();
1901 
1902   // Read local symbols.
1903   if (consume("local:")) {
1904     if (consume("*")) {
1905       Config->DefaultSymbolVersion = VER_NDX_LOCAL;
1906       expect(";");
1907     } else {
1908       for (SymbolVersion V : readSymbols())
1909         Config->VersionScriptLocals.push_back(V);
1910     }
1911   }
1912   expect("}");
1913 
1914   // Each version may have a parent version. For example, "Ver2"
1915   // defined as "Ver2 { global: foo; local: *; } Ver1;" has "Ver1"
1916   // as a parent. This version hierarchy is, probably against your
1917   // instinct, purely for hint; the runtime doesn't care about it
1918   // at all. In LLD, we simply ignore it.
1919   if (peek() != ";")
1920     skip();
1921   expect(";");
1922 }
1923 
1924 // Reads a list of symbols for a versions cript.
1925 std::vector<SymbolVersion> ScriptParser::readSymbols() {
1926   std::vector<SymbolVersion> Ret;
1927   for (;;) {
1928     if (consume("extern"))
1929       for (SymbolVersion V : readVersionExtern())
1930         Ret.push_back(V);
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   expect("\"C++\"");
1945   expect("{");
1946 
1947   std::vector<SymbolVersion> Ret;
1948   while (!Error && peek() != "}") {
1949     StringRef Tok = next();
1950     bool HasWildcard = !Tok.startswith("\"") && hasWildcard(Tok);
1951     Ret.push_back({unquote(Tok), true, HasWildcard});
1952     expect(";");
1953   }
1954 
1955   expect("}");
1956   expect(";");
1957   return Ret;
1958 }
1959 
1960 void elf::readLinkerScript(MemoryBufferRef MB) {
1961   ScriptParser(MB).readLinkerScript();
1962 }
1963 
1964 void elf::readVersionScript(MemoryBufferRef MB) {
1965   ScriptParser(MB).readVersionScript();
1966 }
1967 
1968 template class elf::LinkerScript<ELF32LE>;
1969 template class elf::LinkerScript<ELF32BE>;
1970 template class elf::LinkerScript<ELF64LE>;
1971 template class elf::LinkerScript<ELF64BE>;
1972