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