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 ScriptConfiguration *elf::ScriptConfig;
45 
46 template <class ELFT>
47 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 
53 template <class ELFT> static void addSynthetic(SymbolAssignment *Cmd) {
54   Symbol *Sym = Symtab<ELFT>::X->addSynthetic(
55       Cmd->Name, nullptr, 0, Cmd->Hidden ? STV_HIDDEN : STV_DEFAULT);
56   Cmd->Sym = Sym->body();
57 }
58 
59 // If a symbol was in PROVIDE(), we need to define it only when
60 // it is an undefined symbol.
61 template <class ELFT> static bool shouldDefine(SymbolAssignment *Cmd) {
62   if (Cmd->Name == ".")
63     return false;
64   if (!Cmd->Provide)
65     return true;
66   SymbolBody *B = Symtab<ELFT>::X->find(Cmd->Name);
67   return B && B->isUndefined();
68 }
69 
70 bool SymbolAssignment::classof(const BaseCommand *C) {
71   return C->Kind == AssignmentKind;
72 }
73 
74 bool OutputSectionCommand::classof(const BaseCommand *C) {
75   return C->Kind == OutputSectionKind;
76 }
77 
78 bool InputSectionDescription::classof(const BaseCommand *C) {
79   return C->Kind == InputSectionKind;
80 }
81 
82 bool AssertCommand::classof(const BaseCommand *C) {
83   return C->Kind == AssertKind;
84 }
85 
86 template <class ELFT> static bool isDiscarded(InputSectionBase<ELFT> *S) {
87   return !S || !S->Live;
88 }
89 
90 template <class ELFT> LinkerScript<ELFT>::LinkerScript() {}
91 template <class ELFT> LinkerScript<ELFT>::~LinkerScript() {}
92 
93 template <class ELFT>
94 bool LinkerScript<ELFT>::shouldKeep(InputSectionBase<ELFT> *S) {
95   for (StringRef Pat : Opt.KeptSections)
96     if (globMatch(Pat, S->getSectionName()))
97       return true;
98   return false;
99 }
100 
101 static bool match(ArrayRef<StringRef> Patterns, StringRef S) {
102   for (StringRef Pat : Patterns)
103     if (globMatch(Pat, S))
104       return true;
105   return false;
106 }
107 
108 static bool fileMatches(const InputSectionDescription *Desc,
109                         StringRef Filename) {
110   if (!globMatch(Desc->FilePattern, Filename))
111     return false;
112   return Desc->ExcludedFiles.empty() || !match(Desc->ExcludedFiles, Filename);
113 }
114 
115 // Returns input sections filtered by given glob patterns.
116 template <class ELFT>
117 std::vector<InputSectionBase<ELFT> *>
118 LinkerScript<ELFT>::getInputSections(const InputSectionDescription *I) {
119   ArrayRef<StringRef> Patterns = I->SectionPatterns;
120   std::vector<InputSectionBase<ELFT> *> Ret;
121   for (const std::unique_ptr<ObjectFile<ELFT>> &F :
122        Symtab<ELFT>::X->getObjectFiles()) {
123     if (fileMatches(I, sys::path::filename(F->getName())))
124       for (InputSectionBase<ELFT> *S : F->getSections())
125         if (!isDiscarded(S) && !S->OutSec &&
126             match(Patterns, S->getSectionName()))
127           Ret.push_back(S);
128   }
129 
130   if (llvm::find(Patterns, "COMMON") != Patterns.end())
131     Ret.push_back(CommonInputSection<ELFT>::X);
132 
133   return Ret;
134 }
135 
136 // You can define new symbols using linker scripts. For example,
137 // ".text { abc.o(.text); foo = .; def.o(.text); }" defines symbol
138 // foo just after abc.o's text section contents. This class is to
139 // handle such symbol definitions.
140 //
141 // In order to handle scripts like the above one, we want to
142 // keep symbol definitions in output sections. Because output sections
143 // can contain only input sections, we wrap symbol definitions
144 // with dummy input sections. This class serves that purpose.
145 template <class ELFT>
146 class elf::LayoutInputSection : public InputSectionBase<ELFT> {
147 public:
148   explicit LayoutInputSection(SymbolAssignment *Cmd);
149   static bool classof(const InputSectionBase<ELFT> *S);
150   SymbolAssignment *Cmd;
151 
152 private:
153   typename ELFT::Shdr Hdr;
154 };
155 
156 template <class ELFT>
157 static InputSectionBase<ELFT> *
158 getNonLayoutSection(std::vector<InputSectionBase<ELFT> *> &Vec) {
159   for (InputSectionBase<ELFT> *S : Vec)
160     if (!isa<LayoutInputSection<ELFT>>(S))
161       return S;
162   return nullptr;
163 }
164 
165 template <class T> static T *zero(T *Val) {
166   memset(Val, 0, sizeof(*Val));
167   return Val;
168 }
169 
170 template <class ELFT>
171 LayoutInputSection<ELFT>::LayoutInputSection(SymbolAssignment *Cmd)
172     : InputSectionBase<ELFT>(nullptr, zero(&Hdr),
173                              InputSectionBase<ELFT>::Layout),
174       Cmd(Cmd) {
175   this->Live = true;
176   Hdr.sh_type = SHT_NOBITS;
177 }
178 
179 template <class ELFT>
180 bool LayoutInputSection<ELFT>::classof(const InputSectionBase<ELFT> *S) {
181   return S->SectionKind == InputSectionBase<ELFT>::Layout;
182 }
183 
184 template <class ELFT>
185 static bool compareName(InputSectionBase<ELFT> *A, InputSectionBase<ELFT> *B) {
186   return A->getSectionName() < B->getSectionName();
187 }
188 
189 template <class ELFT>
190 static bool compareAlignment(InputSectionBase<ELFT> *A,
191                              InputSectionBase<ELFT> *B) {
192   // ">" is not a mistake. Larger alignments are placed before smaller
193   // alignments in order to reduce the amount of padding necessary.
194   // This is compatible with GNU.
195   return A->Alignment > B->Alignment;
196 }
197 
198 template <class ELFT>
199 static std::function<bool(InputSectionBase<ELFT> *, InputSectionBase<ELFT> *)>
200 getComparator(SortKind K) {
201   if (K == SortByName)
202     return compareName<ELFT>;
203   return compareAlignment<ELFT>;
204 }
205 
206 template <class ELFT>
207 void LinkerScript<ELFT>::discard(OutputSectionCommand &Cmd) {
208   for (const std::unique_ptr<BaseCommand> &Base : Cmd.Commands) {
209     if (auto *Cmd = dyn_cast<InputSectionDescription>(Base.get())) {
210       for (InputSectionBase<ELFT> *S : getInputSections(Cmd)) {
211         S->Live = false;
212         reportDiscarded(S);
213       }
214     }
215   }
216 }
217 
218 static bool checkConstraint(uint64_t Flags, ConstraintKind Kind) {
219   bool RO = (Kind == ConstraintKind::ReadOnly);
220   bool RW = (Kind == ConstraintKind::ReadWrite);
221   bool Writable = Flags & SHF_WRITE;
222   return !((RO && Writable) || (RW && !Writable));
223 }
224 
225 template <class ELFT>
226 static bool matchConstraints(ArrayRef<InputSectionBase<ELFT> *> Sections,
227                              ConstraintKind Kind) {
228   if (Kind == ConstraintKind::NoConstraint)
229     return true;
230   return llvm::all_of(Sections, [=](InputSectionBase<ELFT> *Sec) {
231     return checkConstraint(Sec->getSectionHdr()->sh_flags, Kind);
232   });
233 }
234 
235 template <class ELFT>
236 std::vector<InputSectionBase<ELFT> *>
237 LinkerScript<ELFT>::createInputSectionList(OutputSectionCommand &OutCmd) {
238   std::vector<InputSectionBase<ELFT> *> Ret;
239 
240   for (const std::unique_ptr<BaseCommand> &Base : OutCmd.Commands) {
241     if (auto *OutCmd = dyn_cast<SymbolAssignment>(Base.get())) {
242       if (shouldDefine<ELFT>(OutCmd))
243         addSynthetic<ELFT>(OutCmd);
244       Ret.push_back(new (LAlloc.Allocate()) LayoutInputSection<ELFT>(OutCmd));
245       continue;
246     }
247 
248     auto *Cmd = cast<InputSectionDescription>(Base.get());
249     std::vector<InputSectionBase<ELFT> *> V = getInputSections(Cmd);
250     if (!matchConstraints<ELFT>(V, OutCmd.Constraint))
251       continue;
252     if (Cmd->SortInner)
253       std::stable_sort(V.begin(), V.end(), getComparator<ELFT>(Cmd->SortInner));
254     if (Cmd->SortOuter)
255       std::stable_sort(V.begin(), V.end(), getComparator<ELFT>(Cmd->SortOuter));
256     Ret.insert(Ret.end(), V.begin(), V.end());
257   }
258   return Ret;
259 }
260 
261 template <class ELFT>
262 void LinkerScript<ELFT>::createSections(OutputSectionFactory<ELFT> &Factory) {
263   for (const std::unique_ptr<BaseCommand> &Base1 : Opt.Commands) {
264     if (auto *Cmd = dyn_cast<SymbolAssignment>(Base1.get())) {
265       if (shouldDefine<ELFT>(Cmd))
266         addRegular<ELFT>(Cmd);
267       continue;
268     }
269 
270     if (auto *Cmd = dyn_cast<OutputSectionCommand>(Base1.get())) {
271       if (Cmd->Name == "/DISCARD/") {
272         discard(*Cmd);
273         continue;
274       }
275 
276       std::vector<InputSectionBase<ELFT> *> V = createInputSectionList(*Cmd);
277       InputSectionBase<ELFT> *Head = getNonLayoutSection<ELFT>(V);
278       if (!Head)
279         continue;
280 
281       OutputSectionBase<ELFT> *OutSec;
282       bool IsNew;
283       std::tie(OutSec, IsNew) = Factory.create(Head, Cmd->Name);
284       if (IsNew)
285         OutputSections->push_back(OutSec);
286 
287       uint32_t Subalign = Cmd->SubalignExpr ? Cmd->SubalignExpr(0) : 0;
288       for (InputSectionBase<ELFT> *Sec : V) {
289         if (Subalign)
290           Sec->Alignment = Subalign;
291         if (!Sec->OutSec)
292           OutSec->addSection(Sec);
293       }
294     }
295   }
296 
297   // Add orphan sections.
298   for (const std::unique_ptr<ObjectFile<ELFT>> &F :
299        Symtab<ELFT>::X->getObjectFiles()) {
300     for (InputSectionBase<ELFT> *S : F->getSections()) {
301       if (isDiscarded(S) || S->OutSec)
302         continue;
303       OutputSectionBase<ELFT> *OutSec;
304       bool IsNew;
305       std::tie(OutSec, IsNew) = Factory.create(S, getOutputSectionName(S));
306       if (IsNew)
307         OutputSections->push_back(OutSec);
308       OutSec->addSection(S);
309     }
310   }
311 }
312 
313 template <class ELFT> void assignOffsets(OutputSectionBase<ELFT> *Sec) {
314   auto *OutSec = dyn_cast<OutputSection<ELFT>>(Sec);
315   if (!OutSec) {
316     Sec->assignOffsets();
317     return;
318   }
319 
320   typedef typename ELFT::uint uintX_t;
321   uintX_t Off = 0;
322 
323   for (InputSection<ELFT> *I : OutSec->Sections) {
324     if (auto *L = dyn_cast<LayoutInputSection<ELFT>>(I)) {
325       uintX_t Value = L->Cmd->Expression(Sec->getVA() + Off) - Sec->getVA();
326       if (L->Cmd->Name == ".") {
327         Off = Value;
328       } else if (auto *Sym =
329                      cast_or_null<DefinedSynthetic<ELFT>>(L->Cmd->Sym)) {
330         // shouldDefine could have returned false, so we need to check Sym,
331         // for non-null value.
332         Sym->Section = OutSec;
333         Sym->Value = Value;
334       }
335     } else {
336       Off = alignTo(Off, I->Alignment);
337       I->OutSecOff = Off;
338       Off += I->getSize();
339     }
340     // Update section size inside for-loop, so that SIZEOF
341     // works correctly in the case below:
342     // .foo { *(.aaa) a = SIZEOF(.foo); *(.bbb) }
343     Sec->setSize(Off);
344   }
345 }
346 
347 template <class ELFT>
348 static OutputSectionBase<ELFT> *
349 findSection(OutputSectionCommand &Cmd,
350             ArrayRef<OutputSectionBase<ELFT> *> Sections) {
351   for (OutputSectionBase<ELFT> *Sec : Sections) {
352     if (Sec->getName() != Cmd.Name)
353       continue;
354     if (checkConstraint(Sec->getFlags(), Cmd.Constraint))
355       return Sec;
356   }
357   return nullptr;
358 }
359 
360 template <class ELFT> void LinkerScript<ELFT>::assignAddresses() {
361   // Orphan sections are sections present in the input files which
362   // are not explicitly placed into the output file by the linker script.
363   // We place orphan sections at end of file.
364   // Other linkers places them using some heuristics as described in
365   // https://sourceware.org/binutils/docs/ld/Orphan-Sections.html#Orphan-Sections.
366   for (OutputSectionBase<ELFT> *Sec : *OutputSections) {
367     StringRef Name = Sec->getName();
368     if (getSectionIndex(Name) == INT_MAX)
369       Opt.Commands.push_back(llvm::make_unique<OutputSectionCommand>(Name));
370   }
371 
372   // Assign addresses as instructed by linker script SECTIONS sub-commands.
373   Dot = getHeaderSize();
374   uintX_t MinVA = std::numeric_limits<uintX_t>::max();
375   uintX_t ThreadBssOffset = 0;
376 
377   for (const std::unique_ptr<BaseCommand> &Base : Opt.Commands) {
378     if (auto *Cmd = dyn_cast<SymbolAssignment>(Base.get())) {
379       if (Cmd->Name == ".") {
380         Dot = Cmd->Expression(Dot);
381       } else if (Cmd->Sym) {
382         cast<DefinedRegular<ELFT>>(Cmd->Sym)->Value = Cmd->Expression(Dot);
383       }
384       continue;
385     }
386 
387     if (auto *Cmd = dyn_cast<AssertCommand>(Base.get())) {
388       Cmd->Expression(Dot);
389       continue;
390     }
391 
392     auto *Cmd = cast<OutputSectionCommand>(Base.get());
393     OutputSectionBase<ELFT> *Sec = findSection<ELFT>(*Cmd, *OutputSections);
394     if (!Sec)
395       continue;
396 
397     if (Cmd->AddrExpr)
398       Dot = Cmd->AddrExpr(Dot);
399 
400     if (Cmd->AlignExpr)
401       Sec->updateAlignment(Cmd->AlignExpr(Dot));
402 
403     if ((Sec->getFlags() & SHF_TLS) && Sec->getType() == SHT_NOBITS) {
404       uintX_t TVA = Dot + ThreadBssOffset;
405       TVA = alignTo(TVA, Sec->getAlignment());
406       Sec->setVA(TVA);
407       assignOffsets(Sec);
408       ThreadBssOffset = TVA - Dot + Sec->getSize();
409       continue;
410     }
411 
412     if (!(Sec->getFlags() & SHF_ALLOC)) {
413       Sec->assignOffsets();
414       continue;
415     }
416 
417     Dot = alignTo(Dot, Sec->getAlignment());
418     Sec->setVA(Dot);
419     assignOffsets(Sec);
420     MinVA = std::min(MinVA, Dot);
421     Dot += Sec->getSize();
422   }
423 
424   // ELF and Program headers need to be right before the first section in
425   // memory. Set their addresses accordingly.
426   MinVA = alignDown(MinVA - Out<ELFT>::ElfHeader->getSize() -
427                         Out<ELFT>::ProgramHeaders->getSize(),
428                     Target->PageSize);
429   Out<ELFT>::ElfHeader->setVA(MinVA);
430   Out<ELFT>::ProgramHeaders->setVA(Out<ELFT>::ElfHeader->getSize() + MinVA);
431 }
432 
433 // Creates program headers as instructed by PHDRS linker script command.
434 template <class ELFT>
435 std::vector<PhdrEntry<ELFT>> LinkerScript<ELFT>::createPhdrs() {
436   std::vector<PhdrEntry<ELFT>> Ret;
437 
438   // Process PHDRS and FILEHDR keywords because they are not
439   // real output sections and cannot be added in the following loop.
440   for (const PhdrsCommand &Cmd : Opt.PhdrsCommands) {
441     Ret.emplace_back(Cmd.Type, Cmd.Flags == UINT_MAX ? PF_R : Cmd.Flags);
442     PhdrEntry<ELFT> &Phdr = Ret.back();
443 
444     if (Cmd.HasFilehdr)
445       Phdr.add(Out<ELFT>::ElfHeader);
446     if (Cmd.HasPhdrs)
447       Phdr.add(Out<ELFT>::ProgramHeaders);
448   }
449 
450   // Add output sections to program headers.
451   PhdrEntry<ELFT> *Load = nullptr;
452   uintX_t Flags = PF_R;
453   for (OutputSectionBase<ELFT> *Sec : *OutputSections) {
454     if (!(Sec->getFlags() & SHF_ALLOC))
455       break;
456 
457     std::vector<size_t> PhdrIds = getPhdrIndices(Sec->getName());
458     if (!PhdrIds.empty()) {
459       // Assign headers specified by linker script
460       for (size_t Id : PhdrIds) {
461         Ret[Id].add(Sec);
462         if (Opt.PhdrsCommands[Id].Flags == UINT_MAX)
463           Ret[Id].H.p_flags |= Sec->getPhdrFlags();
464       }
465     } else {
466       // If we have no load segment or flags've changed then we want new load
467       // segment.
468       uintX_t NewFlags = Sec->getPhdrFlags();
469       if (Load == nullptr || Flags != NewFlags) {
470         Load = &*Ret.emplace(Ret.end(), PT_LOAD, NewFlags);
471         Flags = NewFlags;
472       }
473       Load->add(Sec);
474     }
475   }
476   return Ret;
477 }
478 
479 template <class ELFT> bool LinkerScript<ELFT>::ignoreInterpSection() {
480   // Ignore .interp section in case we have PHDRS specification
481   // and PT_INTERP isn't listed.
482   return !Opt.PhdrsCommands.empty() &&
483          llvm::find_if(Opt.PhdrsCommands, [](const PhdrsCommand &Cmd) {
484            return Cmd.Type == PT_INTERP;
485          }) == Opt.PhdrsCommands.end();
486 }
487 
488 template <class ELFT>
489 ArrayRef<uint8_t> LinkerScript<ELFT>::getFiller(StringRef Name) {
490   for (const std::unique_ptr<BaseCommand> &Base : Opt.Commands)
491     if (auto *Cmd = dyn_cast<OutputSectionCommand>(Base.get()))
492       if (Cmd->Name == Name)
493         return Cmd->Filler;
494   return {};
495 }
496 
497 template <class ELFT> Expr LinkerScript<ELFT>::getLma(StringRef Name) {
498   for (const std::unique_ptr<BaseCommand> &Base : Opt.Commands)
499     if (auto *Cmd = dyn_cast<OutputSectionCommand>(Base.get()))
500       if (Cmd->LmaExpr && Cmd->Name == Name)
501         return Cmd->LmaExpr;
502   return {};
503 }
504 
505 // Returns the index of the given section name in linker script
506 // SECTIONS commands. Sections are laid out as the same order as they
507 // were in the script. If a given name did not appear in the script,
508 // it returns INT_MAX, so that it will be laid out at end of file.
509 template <class ELFT> int LinkerScript<ELFT>::getSectionIndex(StringRef Name) {
510   int I = 0;
511   for (std::unique_ptr<BaseCommand> &Base : Opt.Commands) {
512     if (auto *Cmd = dyn_cast<OutputSectionCommand>(Base.get()))
513       if (Cmd->Name == Name)
514         return I;
515     ++I;
516   }
517   return INT_MAX;
518 }
519 
520 // A compartor to sort output sections. Returns -1 or 1 if
521 // A or B are mentioned in linker script. Otherwise, returns 0.
522 template <class ELFT>
523 int LinkerScript<ELFT>::compareSections(StringRef A, StringRef B) {
524   int I = getSectionIndex(A);
525   int J = getSectionIndex(B);
526   if (I == INT_MAX && J == INT_MAX)
527     return 0;
528   return I < J ? -1 : 1;
529 }
530 
531 template <class ELFT> bool LinkerScript<ELFT>::hasPhdrsCommands() {
532   return !Opt.PhdrsCommands.empty();
533 }
534 
535 template <class ELFT>
536 typename ELFT::uint LinkerScript<ELFT>::getOutputSectionSize(StringRef Name) {
537   for (OutputSectionBase<ELFT> *Sec : *OutputSections)
538     if (Sec->getName() == Name)
539       return Sec->getSize();
540   error("undefined section " + Name);
541   return 0;
542 }
543 
544 template <class ELFT>
545 typename ELFT::uint LinkerScript<ELFT>::getHeaderSize() {
546   return Out<ELFT>::ElfHeader->getSize() + Out<ELFT>::ProgramHeaders->getSize();
547 }
548 
549 // Returns indices of ELF headers containing specific section, identified
550 // by Name. Each index is a zero based number of ELF header listed within
551 // PHDRS {} script block.
552 template <class ELFT>
553 std::vector<size_t> LinkerScript<ELFT>::getPhdrIndices(StringRef SectionName) {
554   for (const std::unique_ptr<BaseCommand> &Base : Opt.Commands) {
555     auto *Cmd = dyn_cast<OutputSectionCommand>(Base.get());
556     if (!Cmd || Cmd->Name != SectionName)
557       continue;
558 
559     std::vector<size_t> Ret;
560     for (StringRef PhdrName : Cmd->Phdrs)
561       Ret.push_back(getPhdrIndex(PhdrName));
562     return Ret;
563   }
564   return {};
565 }
566 
567 template <class ELFT>
568 size_t LinkerScript<ELFT>::getPhdrIndex(StringRef PhdrName) {
569   size_t I = 0;
570   for (PhdrsCommand &Cmd : Opt.PhdrsCommands) {
571     if (Cmd.Name == PhdrName)
572       return I;
573     ++I;
574   }
575   error("section header '" + PhdrName + "' is not listed in PHDRS");
576   return 0;
577 }
578 
579 class elf::ScriptParser : public ScriptParserBase {
580   typedef void (ScriptParser::*Handler)();
581 
582 public:
583   ScriptParser(StringRef S, bool B) : ScriptParserBase(S), IsUnderSysroot(B) {}
584 
585   void run();
586 
587 private:
588   void addFile(StringRef Path);
589 
590   void readAsNeeded();
591   void readEntry();
592   void readExtern();
593   void readGroup();
594   void readInclude();
595   void readNothing() {}
596   void readOutput();
597   void readOutputArch();
598   void readOutputFormat();
599   void readPhdrs();
600   void readSearchDir();
601   void readSections();
602 
603   SymbolAssignment *readAssignment(StringRef Name);
604   OutputSectionCommand *readOutputSectionDescription(StringRef OutSec);
605   std::vector<uint8_t> readOutputSectionFiller();
606   std::vector<StringRef> readOutputSectionPhdrs();
607   InputSectionDescription *readInputSectionDescription();
608   std::vector<StringRef> readInputFilePatterns();
609   InputSectionDescription *readInputSectionRules();
610   unsigned readPhdrType();
611   SortKind readSortKind();
612   SymbolAssignment *readProvideHidden(bool Provide, bool Hidden);
613   SymbolAssignment *readProvideOrAssignment(StringRef Tok);
614   void readSort();
615   Expr readAssert();
616 
617   Expr readExpr();
618   Expr readExpr1(Expr Lhs, int MinPrec);
619   Expr readPrimary();
620   Expr readTernary(Expr Cond);
621   Expr readParenExpr();
622 
623   const static StringMap<Handler> Cmd;
624   ScriptConfiguration &Opt = *ScriptConfig;
625   StringSaver Saver = {ScriptConfig->Alloc};
626   bool IsUnderSysroot;
627 };
628 
629 const StringMap<elf::ScriptParser::Handler> elf::ScriptParser::Cmd = {
630     {"ENTRY", &ScriptParser::readEntry},
631     {"EXTERN", &ScriptParser::readExtern},
632     {"GROUP", &ScriptParser::readGroup},
633     {"INCLUDE", &ScriptParser::readInclude},
634     {"INPUT", &ScriptParser::readGroup},
635     {"OUTPUT", &ScriptParser::readOutput},
636     {"OUTPUT_ARCH", &ScriptParser::readOutputArch},
637     {"OUTPUT_FORMAT", &ScriptParser::readOutputFormat},
638     {"PHDRS", &ScriptParser::readPhdrs},
639     {"SEARCH_DIR", &ScriptParser::readSearchDir},
640     {"SECTIONS", &ScriptParser::readSections},
641     {";", &ScriptParser::readNothing}};
642 
643 void ScriptParser::run() {
644   while (!atEOF()) {
645     StringRef Tok = next();
646     if (Handler Fn = Cmd.lookup(Tok))
647       (this->*Fn)();
648     else if (SymbolAssignment *Cmd = readProvideOrAssignment(Tok))
649       Opt.Commands.emplace_back(Cmd);
650     else
651       setError("unknown directive: " + Tok);
652   }
653 }
654 
655 void ScriptParser::addFile(StringRef S) {
656   if (IsUnderSysroot && S.startswith("/")) {
657     SmallString<128> Path;
658     (Config->Sysroot + S).toStringRef(Path);
659     if (sys::fs::exists(Path)) {
660       Driver->addFile(Saver.save(Path.str()));
661       return;
662     }
663   }
664 
665   if (sys::path::is_absolute(S)) {
666     Driver->addFile(S);
667   } else if (S.startswith("=")) {
668     if (Config->Sysroot.empty())
669       Driver->addFile(S.substr(1));
670     else
671       Driver->addFile(Saver.save(Config->Sysroot + "/" + S.substr(1)));
672   } else if (S.startswith("-l")) {
673     Driver->addLibrary(S.substr(2));
674   } else if (sys::fs::exists(S)) {
675     Driver->addFile(S);
676   } else {
677     std::string Path = findFromSearchPaths(S);
678     if (Path.empty())
679       setError("unable to find " + S);
680     else
681       Driver->addFile(Saver.save(Path));
682   }
683 }
684 
685 void ScriptParser::readAsNeeded() {
686   expect("(");
687   bool Orig = Config->AsNeeded;
688   Config->AsNeeded = true;
689   while (!Error && !skip(")"))
690     addFile(next());
691   Config->AsNeeded = Orig;
692 }
693 
694 void ScriptParser::readEntry() {
695   // -e <symbol> takes predecence over ENTRY(<symbol>).
696   expect("(");
697   StringRef Tok = next();
698   if (Config->Entry.empty())
699     Config->Entry = Tok;
700   expect(")");
701 }
702 
703 void ScriptParser::readExtern() {
704   expect("(");
705   while (!Error && !skip(")"))
706     Config->Undefined.push_back(next());
707 }
708 
709 void ScriptParser::readGroup() {
710   expect("(");
711   while (!Error && !skip(")")) {
712     StringRef Tok = next();
713     if (Tok == "AS_NEEDED")
714       readAsNeeded();
715     else
716       addFile(Tok);
717   }
718 }
719 
720 void ScriptParser::readInclude() {
721   StringRef Tok = next();
722   auto MBOrErr = MemoryBuffer::getFile(Tok);
723   if (!MBOrErr) {
724     setError("cannot open " + Tok);
725     return;
726   }
727   std::unique_ptr<MemoryBuffer> &MB = *MBOrErr;
728   StringRef S = Saver.save(MB->getMemBufferRef().getBuffer());
729   std::vector<StringRef> V = tokenize(S);
730   Tokens.insert(Tokens.begin() + Pos, V.begin(), V.end());
731 }
732 
733 void ScriptParser::readOutput() {
734   // -o <file> takes predecence over OUTPUT(<file>).
735   expect("(");
736   StringRef Tok = next();
737   if (Config->OutputFile.empty())
738     Config->OutputFile = Tok;
739   expect(")");
740 }
741 
742 void ScriptParser::readOutputArch() {
743   // Error checking only for now.
744   expect("(");
745   next();
746   expect(")");
747 }
748 
749 void ScriptParser::readOutputFormat() {
750   // Error checking only for now.
751   expect("(");
752   next();
753   StringRef Tok = next();
754   if (Tok == ")")
755    return;
756   if (Tok != ",") {
757     setError("unexpected token: " + Tok);
758     return;
759   }
760   next();
761   expect(",");
762   next();
763   expect(")");
764 }
765 
766 void ScriptParser::readPhdrs() {
767   expect("{");
768   while (!Error && !skip("}")) {
769     StringRef Tok = next();
770     Opt.PhdrsCommands.push_back({Tok, PT_NULL, false, false, UINT_MAX});
771     PhdrsCommand &PhdrCmd = Opt.PhdrsCommands.back();
772 
773     PhdrCmd.Type = readPhdrType();
774     do {
775       Tok = next();
776       if (Tok == ";")
777         break;
778       if (Tok == "FILEHDR")
779         PhdrCmd.HasFilehdr = true;
780       else if (Tok == "PHDRS")
781         PhdrCmd.HasPhdrs = true;
782       else if (Tok == "FLAGS") {
783         expect("(");
784         // Passing 0 for the value of dot is a bit of a hack. It means that
785         // we accept expressions like ".|1".
786         PhdrCmd.Flags = readExpr()(0);
787         expect(")");
788       } else
789         setError("unexpected header attribute: " + Tok);
790     } while (!Error);
791   }
792 }
793 
794 void ScriptParser::readSearchDir() {
795   expect("(");
796   Config->SearchPaths.push_back(next());
797   expect(")");
798 }
799 
800 void ScriptParser::readSections() {
801   Opt.HasContents = true;
802   expect("{");
803   while (!Error && !skip("}")) {
804     StringRef Tok = next();
805     BaseCommand *Cmd = readProvideOrAssignment(Tok);
806     if (!Cmd) {
807       if (Tok == "ASSERT")
808         Cmd = new AssertCommand(readAssert());
809       else
810         Cmd = readOutputSectionDescription(Tok);
811     }
812     Opt.Commands.emplace_back(Cmd);
813   }
814 }
815 
816 static int precedence(StringRef Op) {
817   return StringSwitch<int>(Op)
818       .Case("*", 4)
819       .Case("/", 4)
820       .Case("+", 3)
821       .Case("-", 3)
822       .Case("<", 2)
823       .Case(">", 2)
824       .Case(">=", 2)
825       .Case("<=", 2)
826       .Case("==", 2)
827       .Case("!=", 2)
828       .Case("&", 1)
829       .Case("|", 1)
830       .Default(-1);
831 }
832 
833 std::vector<StringRef> ScriptParser::readInputFilePatterns() {
834   std::vector<StringRef> V;
835   while (!Error && !skip(")"))
836     V.push_back(next());
837   return V;
838 }
839 
840 SortKind ScriptParser::readSortKind() {
841   if (skip("SORT") || skip("SORT_BY_NAME"))
842     return SortByName;
843   if (skip("SORT_BY_ALIGNMENT"))
844     return SortByAlignment;
845   return SortNone;
846 }
847 
848 InputSectionDescription *ScriptParser::readInputSectionRules() {
849   auto *Cmd = new InputSectionDescription;
850   Cmd->FilePattern = next();
851   expect("(");
852 
853   // Read EXCLUDE_FILE().
854   if (skip("EXCLUDE_FILE")) {
855     expect("(");
856     while (!Error && !skip(")"))
857       Cmd->ExcludedFiles.push_back(next());
858   }
859 
860   // Read SORT().
861   if (SortKind K1 = readSortKind()) {
862     Cmd->SortOuter = K1;
863     expect("(");
864     if (SortKind K2 = readSortKind()) {
865       Cmd->SortInner = K2;
866       expect("(");
867       Cmd->SectionPatterns = readInputFilePatterns();
868       expect(")");
869     } else {
870       Cmd->SectionPatterns = readInputFilePatterns();
871     }
872     expect(")");
873     return Cmd;
874   }
875 
876   Cmd->SectionPatterns = readInputFilePatterns();
877   return Cmd;
878 }
879 
880 InputSectionDescription *ScriptParser::readInputSectionDescription() {
881   // Input section wildcard can be surrounded by KEEP.
882   // https://sourceware.org/binutils/docs/ld/Input-Section-Keep.html#Input-Section-Keep
883   if (skip("KEEP")) {
884     expect("(");
885     InputSectionDescription *Cmd = readInputSectionRules();
886     expect(")");
887     Opt.KeptSections.insert(Opt.KeptSections.end(),
888                             Cmd->SectionPatterns.begin(),
889                             Cmd->SectionPatterns.end());
890     return Cmd;
891   }
892   return readInputSectionRules();
893 }
894 
895 void ScriptParser::readSort() {
896   expect("(");
897   expect("CONSTRUCTORS");
898   expect(")");
899 }
900 
901 Expr ScriptParser::readAssert() {
902   expect("(");
903   Expr E = readExpr();
904   expect(",");
905   StringRef Msg = next();
906   expect(")");
907   return [=](uint64_t Dot) {
908     uint64_t V = E(Dot);
909     if (!V)
910       error(Msg);
911     return V;
912   };
913 }
914 
915 OutputSectionCommand *
916 ScriptParser::readOutputSectionDescription(StringRef OutSec) {
917   OutputSectionCommand *Cmd = new OutputSectionCommand(OutSec);
918 
919   // Read an address expression.
920   // https://sourceware.org/binutils/docs/ld/Output-Section-Address.html#Output-Section-Address
921   if (peek() != ":")
922     Cmd->AddrExpr = readExpr();
923 
924   expect(":");
925 
926   if (skip("AT"))
927     Cmd->LmaExpr = readParenExpr();
928   if (skip("ALIGN"))
929     Cmd->AlignExpr = readParenExpr();
930   if (skip("SUBALIGN"))
931     Cmd->SubalignExpr = readParenExpr();
932 
933   // Parse constraints.
934   if (skip("ONLY_IF_RO"))
935     Cmd->Constraint = ConstraintKind::ReadOnly;
936   if (skip("ONLY_IF_RW"))
937     Cmd->Constraint = ConstraintKind::ReadWrite;
938   expect("{");
939 
940   while (!Error && !skip("}")) {
941     if (peek().startswith("*") || peek() == "KEEP") {
942       Cmd->Commands.emplace_back(readInputSectionDescription());
943       continue;
944     }
945 
946     StringRef Tok = next();
947     if (SymbolAssignment *Assignment = readProvideOrAssignment(Tok))
948       Cmd->Commands.emplace_back(Assignment);
949     else if (Tok == "SORT")
950       readSort();
951     else
952       setError("unknown command " + Tok);
953   }
954   Cmd->Phdrs = readOutputSectionPhdrs();
955   Cmd->Filler = readOutputSectionFiller();
956   return Cmd;
957 }
958 
959 std::vector<uint8_t> ScriptParser::readOutputSectionFiller() {
960   StringRef Tok = peek();
961   if (!Tok.startswith("="))
962     return {};
963   next();
964 
965   // Read a hexstring of arbitrary length.
966   if (Tok.startswith("=0x"))
967     return parseHex(Tok.substr(3));
968 
969   // Read a decimal or octal value as a big-endian 32 bit value.
970   // Why do this? I don't know, but that's what gold does.
971   uint32_t V;
972   if (Tok.substr(1).getAsInteger(0, V)) {
973     setError("invalid filler expression: " + Tok);
974     return {};
975   }
976   return { uint8_t(V >> 24), uint8_t(V >> 16), uint8_t(V >> 8), uint8_t(V) };
977 }
978 
979 SymbolAssignment *ScriptParser::readProvideHidden(bool Provide, bool Hidden) {
980   expect("(");
981   SymbolAssignment *Cmd = readAssignment(next());
982   Cmd->Provide = Provide;
983   Cmd->Hidden = Hidden;
984   expect(")");
985   expect(";");
986   return Cmd;
987 }
988 
989 SymbolAssignment *ScriptParser::readProvideOrAssignment(StringRef Tok) {
990   SymbolAssignment *Cmd = nullptr;
991   if (peek() == "=" || peek() == "+=") {
992     Cmd = readAssignment(Tok);
993     expect(";");
994   } else if (Tok == "PROVIDE") {
995     Cmd = readProvideHidden(true, false);
996   } else if (Tok == "HIDDEN") {
997     Cmd = readProvideHidden(false, true);
998   } else if (Tok == "PROVIDE_HIDDEN") {
999     Cmd = readProvideHidden(true, true);
1000   }
1001   return Cmd;
1002 }
1003 
1004 static uint64_t getSymbolValue(StringRef S, uint64_t Dot) {
1005   if (S == ".")
1006     return Dot;
1007 
1008   switch (Config->EKind) {
1009   case ELF32LEKind:
1010     if (SymbolBody *B = Symtab<ELF32LE>::X->find(S))
1011       return B->getVA<ELF32LE>();
1012     break;
1013   case ELF32BEKind:
1014     if (SymbolBody *B = Symtab<ELF32BE>::X->find(S))
1015       return B->getVA<ELF32BE>();
1016     break;
1017   case ELF64LEKind:
1018     if (SymbolBody *B = Symtab<ELF64LE>::X->find(S))
1019       return B->getVA<ELF64LE>();
1020     break;
1021   case ELF64BEKind:
1022     if (SymbolBody *B = Symtab<ELF64BE>::X->find(S))
1023       return B->getVA<ELF64BE>();
1024     break;
1025   default:
1026     llvm_unreachable("unsupported target");
1027   }
1028   error("symbol not found: " + S);
1029   return 0;
1030 }
1031 
1032 static uint64_t getSectionSize(StringRef Name) {
1033   switch (Config->EKind) {
1034   case ELF32LEKind:
1035     return Script<ELF32LE>::X->getOutputSectionSize(Name);
1036   case ELF32BEKind:
1037     return Script<ELF32BE>::X->getOutputSectionSize(Name);
1038   case ELF64LEKind:
1039     return Script<ELF64LE>::X->getOutputSectionSize(Name);
1040   case ELF64BEKind:
1041     return Script<ELF64BE>::X->getOutputSectionSize(Name);
1042   default:
1043     llvm_unreachable("unsupported target");
1044   }
1045 }
1046 
1047 static uint64_t getHeaderSize() {
1048   switch (Config->EKind) {
1049   case ELF32LEKind:
1050     return Script<ELF32LE>::X->getHeaderSize();
1051   case ELF32BEKind:
1052     return Script<ELF32BE>::X->getHeaderSize();
1053   case ELF64LEKind:
1054     return Script<ELF64LE>::X->getHeaderSize();
1055   case ELF64BEKind:
1056     return Script<ELF64BE>::X->getHeaderSize();
1057   default:
1058     llvm_unreachable("unsupported target");
1059   }
1060 }
1061 
1062 SymbolAssignment *ScriptParser::readAssignment(StringRef Name) {
1063   StringRef Op = next();
1064   assert(Op == "=" || Op == "+=");
1065   Expr E = readExpr();
1066   if (Op == "+=")
1067     E = [=](uint64_t Dot) { return getSymbolValue(Name, Dot) + E(Dot); };
1068   return new SymbolAssignment(Name, E);
1069 }
1070 
1071 // This is an operator-precedence parser to parse a linker
1072 // script expression.
1073 Expr ScriptParser::readExpr() { return readExpr1(readPrimary(), 0); }
1074 
1075 static Expr combine(StringRef Op, Expr L, Expr R) {
1076   if (Op == "*")
1077     return [=](uint64_t Dot) { return L(Dot) * R(Dot); };
1078   if (Op == "/") {
1079     return [=](uint64_t Dot) -> uint64_t {
1080       uint64_t RHS = R(Dot);
1081       if (RHS == 0) {
1082         error("division by zero");
1083         return 0;
1084       }
1085       return L(Dot) / RHS;
1086     };
1087   }
1088   if (Op == "+")
1089     return [=](uint64_t Dot) { return L(Dot) + R(Dot); };
1090   if (Op == "-")
1091     return [=](uint64_t Dot) { return L(Dot) - R(Dot); };
1092   if (Op == "<")
1093     return [=](uint64_t Dot) { return L(Dot) < R(Dot); };
1094   if (Op == ">")
1095     return [=](uint64_t Dot) { return L(Dot) > R(Dot); };
1096   if (Op == ">=")
1097     return [=](uint64_t Dot) { return L(Dot) >= R(Dot); };
1098   if (Op == "<=")
1099     return [=](uint64_t Dot) { return L(Dot) <= R(Dot); };
1100   if (Op == "==")
1101     return [=](uint64_t Dot) { return L(Dot) == R(Dot); };
1102   if (Op == "!=")
1103     return [=](uint64_t Dot) { return L(Dot) != R(Dot); };
1104   if (Op == "&")
1105     return [=](uint64_t Dot) { return L(Dot) & R(Dot); };
1106   if (Op == "|")
1107     return [=](uint64_t Dot) { return L(Dot) | R(Dot); };
1108   llvm_unreachable("invalid operator");
1109 }
1110 
1111 // This is a part of the operator-precedence parser. This function
1112 // assumes that the remaining token stream starts with an operator.
1113 Expr ScriptParser::readExpr1(Expr Lhs, int MinPrec) {
1114   while (!atEOF() && !Error) {
1115     // Read an operator and an expression.
1116     StringRef Op1 = peek();
1117     if (Op1 == "?")
1118       return readTernary(Lhs);
1119     if (precedence(Op1) < MinPrec)
1120       break;
1121     next();
1122     Expr Rhs = readPrimary();
1123 
1124     // Evaluate the remaining part of the expression first if the
1125     // next operator has greater precedence than the previous one.
1126     // For example, if we have read "+" and "3", and if the next
1127     // operator is "*", then we'll evaluate 3 * ... part first.
1128     while (!atEOF()) {
1129       StringRef Op2 = peek();
1130       if (precedence(Op2) <= precedence(Op1))
1131         break;
1132       Rhs = readExpr1(Rhs, precedence(Op2));
1133     }
1134 
1135     Lhs = combine(Op1, Lhs, Rhs);
1136   }
1137   return Lhs;
1138 }
1139 
1140 uint64_t static getConstant(StringRef S) {
1141   if (S == "COMMONPAGESIZE")
1142     return Target->PageSize;
1143   if (S == "MAXPAGESIZE")
1144     return Target->MaxPageSize;
1145   error("unknown constant: " + S);
1146   return 0;
1147 }
1148 
1149 Expr ScriptParser::readPrimary() {
1150   if (peek() == "(")
1151     return readParenExpr();
1152 
1153   StringRef Tok = next();
1154 
1155   // Built-in functions are parsed here.
1156   // https://sourceware.org/binutils/docs/ld/Builtin-Functions.html.
1157   if (Tok == "ASSERT")
1158     return readAssert();
1159   if (Tok == "ALIGN") {
1160     Expr E = readParenExpr();
1161     return [=](uint64_t Dot) { return alignTo(Dot, E(Dot)); };
1162   }
1163   if (Tok == "CONSTANT") {
1164     expect("(");
1165     StringRef Tok = next();
1166     expect(")");
1167     return [=](uint64_t Dot) { return getConstant(Tok); };
1168   }
1169   if (Tok == "SEGMENT_START") {
1170     expect("(");
1171     next();
1172     expect(",");
1173     uint64_t Val;
1174     next().getAsInteger(0, Val);
1175     expect(")");
1176     return [=](uint64_t Dot) { return Val; };
1177   }
1178   if (Tok == "DATA_SEGMENT_ALIGN") {
1179     expect("(");
1180     Expr E = readExpr();
1181     expect(",");
1182     readExpr();
1183     expect(")");
1184     return [=](uint64_t Dot) { return alignTo(Dot, E(Dot)); };
1185   }
1186   if (Tok == "DATA_SEGMENT_END") {
1187     expect("(");
1188     expect(".");
1189     expect(")");
1190     return [](uint64_t Dot) { return Dot; };
1191   }
1192   // GNU linkers implements more complicated logic to handle
1193   // DATA_SEGMENT_RELRO_END. We instead ignore the arguments and just align to
1194   // the next page boundary for simplicity.
1195   if (Tok == "DATA_SEGMENT_RELRO_END") {
1196     expect("(");
1197     next();
1198     expect(",");
1199     readExpr();
1200     expect(")");
1201     return [](uint64_t Dot) { return alignTo(Dot, Target->PageSize); };
1202   }
1203   if (Tok == "SIZEOF") {
1204     expect("(");
1205     StringRef Name = next();
1206     expect(")");
1207     return [=](uint64_t Dot) { return getSectionSize(Name); };
1208   }
1209   if (Tok == "SIZEOF_HEADERS")
1210     return [=](uint64_t Dot) { return getHeaderSize(); };
1211 
1212   // Parse a symbol name or a number literal.
1213   uint64_t V = 0;
1214   if (Tok.getAsInteger(0, V)) {
1215     if (Tok != "." && !isValidCIdentifier(Tok))
1216       setError("malformed number: " + Tok);
1217     return [=](uint64_t Dot) { return getSymbolValue(Tok, Dot); };
1218   }
1219   return [=](uint64_t Dot) { return V; };
1220 }
1221 
1222 Expr ScriptParser::readTernary(Expr Cond) {
1223   next();
1224   Expr L = readExpr();
1225   expect(":");
1226   Expr R = readExpr();
1227   return [=](uint64_t Dot) { return Cond(Dot) ? L(Dot) : R(Dot); };
1228 }
1229 
1230 Expr ScriptParser::readParenExpr() {
1231   expect("(");
1232   Expr E = readExpr();
1233   expect(")");
1234   return E;
1235 }
1236 
1237 std::vector<StringRef> ScriptParser::readOutputSectionPhdrs() {
1238   std::vector<StringRef> Phdrs;
1239   while (!Error && peek().startswith(":")) {
1240     StringRef Tok = next();
1241     Tok = (Tok.size() == 1) ? next() : Tok.substr(1);
1242     if (Tok.empty()) {
1243       setError("section header name is empty");
1244       break;
1245     }
1246     Phdrs.push_back(Tok);
1247   }
1248   return Phdrs;
1249 }
1250 
1251 unsigned ScriptParser::readPhdrType() {
1252   StringRef Tok = next();
1253   unsigned Ret = StringSwitch<unsigned>(Tok)
1254       .Case("PT_NULL", PT_NULL)
1255       .Case("PT_LOAD", PT_LOAD)
1256       .Case("PT_DYNAMIC", PT_DYNAMIC)
1257       .Case("PT_INTERP", PT_INTERP)
1258       .Case("PT_NOTE", PT_NOTE)
1259       .Case("PT_SHLIB", PT_SHLIB)
1260       .Case("PT_PHDR", PT_PHDR)
1261       .Case("PT_TLS", PT_TLS)
1262       .Case("PT_GNU_EH_FRAME", PT_GNU_EH_FRAME)
1263       .Case("PT_GNU_STACK", PT_GNU_STACK)
1264       .Case("PT_GNU_RELRO", PT_GNU_RELRO)
1265       .Default(-1);
1266 
1267   if (Ret == (unsigned)-1) {
1268     setError("invalid program header type: " + Tok);
1269     return PT_NULL;
1270   }
1271   return Ret;
1272 }
1273 
1274 static bool isUnderSysroot(StringRef Path) {
1275   if (Config->Sysroot == "")
1276     return false;
1277   for (; !Path.empty(); Path = sys::path::parent_path(Path))
1278     if (sys::fs::equivalent(Config->Sysroot, Path))
1279       return true;
1280   return false;
1281 }
1282 
1283 // Entry point.
1284 void elf::readLinkerScript(MemoryBufferRef MB) {
1285   StringRef Path = MB.getBufferIdentifier();
1286   ScriptParser(MB.getBuffer(), isUnderSysroot(Path)).run();
1287 }
1288 
1289 template class elf::LinkerScript<ELF32LE>;
1290 template class elf::LinkerScript<ELF32BE>;
1291 template class elf::LinkerScript<ELF64LE>;
1292 template class elf::LinkerScript<ELF64BE>;
1293