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