]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - contrib/llvm/tools/lld/ELF/LinkerScript.cpp
Merge llvm, clang, lld, lldb, compiler-rt and libc++ r308421, and update
[FreeBSD/FreeBSD.git] / contrib / llvm / tools / lld / ELF / LinkerScript.cpp
1 //===- LinkerScript.cpp ---------------------------------------------------===//
2 //
3 //                             The LLVM Linker
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // This file contains the parser/evaluator of the linker script.
11 //
12 //===----------------------------------------------------------------------===//
13
14 #include "LinkerScript.h"
15 #include "Config.h"
16 #include "InputSection.h"
17 #include "Memory.h"
18 #include "OutputSections.h"
19 #include "Strings.h"
20 #include "SymbolTable.h"
21 #include "Symbols.h"
22 #include "SyntheticSections.h"
23 #include "Target.h"
24 #include "Threads.h"
25 #include "Writer.h"
26 #include "llvm/ADT/STLExtras.h"
27 #include "llvm/ADT/StringRef.h"
28 #include "llvm/BinaryFormat/ELF.h"
29 #include "llvm/Support/Casting.h"
30 #include "llvm/Support/Compression.h"
31 #include "llvm/Support/Endian.h"
32 #include "llvm/Support/ErrorHandling.h"
33 #include "llvm/Support/FileSystem.h"
34 #include "llvm/Support/Path.h"
35 #include <algorithm>
36 #include <cassert>
37 #include <cstddef>
38 #include <cstdint>
39 #include <iterator>
40 #include <limits>
41 #include <string>
42 #include <vector>
43
44 using namespace llvm;
45 using namespace llvm::ELF;
46 using namespace llvm::object;
47 using namespace llvm::support::endian;
48 using namespace lld;
49 using namespace lld::elf;
50
51 LinkerScript *elf::Script;
52
53 uint64_t ExprValue::getValue() const {
54   if (Sec) {
55     if (OutputSection *OS = Sec->getOutputSection())
56       return alignTo(Sec->getOffset(Val) + OS->Addr, Alignment);
57     error(Loc + ": unable to evaluate expression: input section " + Sec->Name +
58           " has no output section assigned");
59   }
60   return alignTo(Val, Alignment);
61 }
62
63 uint64_t ExprValue::getSecAddr() const {
64   if (Sec)
65     return Sec->getOffset(0) + Sec->getOutputSection()->Addr;
66   return 0;
67 }
68
69 template <class ELFT> static SymbolBody *addRegular(SymbolAssignment *Cmd) {
70   Symbol *Sym;
71   uint8_t Visibility = Cmd->Hidden ? STV_HIDDEN : STV_DEFAULT;
72   std::tie(Sym, std::ignore) = Symtab<ELFT>::X->insert(
73       Cmd->Name, /*Type*/ 0, Visibility, /*CanOmitFromDynSym*/ false,
74       /*File*/ nullptr);
75   Sym->Binding = STB_GLOBAL;
76   ExprValue Value = Cmd->Expression();
77   SectionBase *Sec = Value.isAbsolute() ? nullptr : Value.Sec;
78
79   // We want to set symbol values early if we can. This allows us to use symbols
80   // as variables in linker scripts. Doing so allows us to write expressions
81   // like this: `alignment = 16; . = ALIGN(., alignment)`
82   uint64_t SymValue = Value.isAbsolute() ? Value.getValue() : 0;
83   replaceBody<DefinedRegular>(Sym, Cmd->Name, /*IsLocal=*/false, Visibility,
84                               STT_NOTYPE, SymValue, 0, Sec, nullptr);
85   return Sym->body();
86 }
87
88 OutputSectionCommand *
89 LinkerScript::createOutputSectionCommand(StringRef Name, StringRef Location) {
90   OutputSectionCommand *&CmdRef = NameToOutputSectionCommand[Name];
91   OutputSectionCommand *Cmd;
92   if (CmdRef && CmdRef->Location.empty()) {
93     // There was a forward reference.
94     Cmd = CmdRef;
95   } else {
96     Cmd = make<OutputSectionCommand>(Name);
97     if (!CmdRef)
98       CmdRef = Cmd;
99   }
100   Cmd->Location = Location;
101   return Cmd;
102 }
103
104 OutputSectionCommand *
105 LinkerScript::getOrCreateOutputSectionCommand(StringRef Name) {
106   OutputSectionCommand *&CmdRef = NameToOutputSectionCommand[Name];
107   if (!CmdRef)
108     CmdRef = make<OutputSectionCommand>(Name);
109   return CmdRef;
110 }
111
112 void LinkerScript::setDot(Expr E, const Twine &Loc, bool InSec) {
113   uint64_t Val = E().getValue();
114   if (Val < Dot && InSec)
115     error(Loc + ": unable to move location counter backward for: " +
116           CurAddressState->OutSec->Name);
117   Dot = Val;
118   // Update to location counter means update to section size.
119   if (InSec)
120     CurAddressState->OutSec->Size = Dot - CurAddressState->OutSec->Addr;
121 }
122
123 // Sets value of a symbol. Two kinds of symbols are processed: synthetic
124 // symbols, whose value is an offset from beginning of section and regular
125 // symbols whose value is absolute.
126 void LinkerScript::assignSymbol(SymbolAssignment *Cmd, bool InSec) {
127   if (Cmd->Name == ".") {
128     setDot(Cmd->Expression, Cmd->Location, InSec);
129     return;
130   }
131
132   if (!Cmd->Sym)
133     return;
134
135   auto *Sym = cast<DefinedRegular>(Cmd->Sym);
136   ExprValue V = Cmd->Expression();
137   if (V.isAbsolute()) {
138     Sym->Value = V.getValue();
139   } else {
140     Sym->Section = V.Sec;
141     Sym->Value = alignTo(V.Val, V.Alignment);
142   }
143 }
144
145 static SymbolBody *findSymbol(StringRef S) {
146   switch (Config->EKind) {
147   case ELF32LEKind:
148     return Symtab<ELF32LE>::X->find(S);
149   case ELF32BEKind:
150     return Symtab<ELF32BE>::X->find(S);
151   case ELF64LEKind:
152     return Symtab<ELF64LE>::X->find(S);
153   case ELF64BEKind:
154     return Symtab<ELF64BE>::X->find(S);
155   default:
156     llvm_unreachable("unknown Config->EKind");
157   }
158 }
159
160 static SymbolBody *addRegularSymbol(SymbolAssignment *Cmd) {
161   switch (Config->EKind) {
162   case ELF32LEKind:
163     return addRegular<ELF32LE>(Cmd);
164   case ELF32BEKind:
165     return addRegular<ELF32BE>(Cmd);
166   case ELF64LEKind:
167     return addRegular<ELF64LE>(Cmd);
168   case ELF64BEKind:
169     return addRegular<ELF64BE>(Cmd);
170   default:
171     llvm_unreachable("unknown Config->EKind");
172   }
173 }
174
175 void LinkerScript::addSymbol(SymbolAssignment *Cmd) {
176   if (Cmd->Name == ".")
177     return;
178
179   // If a symbol was in PROVIDE(), we need to define it only when
180   // it is a referenced undefined symbol.
181   SymbolBody *B = findSymbol(Cmd->Name);
182   if (Cmd->Provide && (!B || B->isDefined()))
183     return;
184
185   Cmd->Sym = addRegularSymbol(Cmd);
186 }
187
188 bool SymbolAssignment::classof(const BaseCommand *C) {
189   return C->Kind == AssignmentKind;
190 }
191
192 bool OutputSectionCommand::classof(const BaseCommand *C) {
193   return C->Kind == OutputSectionKind;
194 }
195
196 // Fill [Buf, Buf + Size) with Filler.
197 // This is used for linker script "=fillexp" command.
198 static void fill(uint8_t *Buf, size_t Size, uint32_t Filler) {
199   size_t I = 0;
200   for (; I + 4 < Size; I += 4)
201     memcpy(Buf + I, &Filler, 4);
202   memcpy(Buf + I, &Filler, Size - I);
203 }
204
205 bool InputSectionDescription::classof(const BaseCommand *C) {
206   return C->Kind == InputSectionKind;
207 }
208
209 bool AssertCommand::classof(const BaseCommand *C) {
210   return C->Kind == AssertKind;
211 }
212
213 bool BytesDataCommand::classof(const BaseCommand *C) {
214   return C->Kind == BytesDataKind;
215 }
216
217 static StringRef basename(InputSectionBase *S) {
218   if (S->File)
219     return sys::path::filename(S->File->getName());
220   return "";
221 }
222
223 bool LinkerScript::shouldKeep(InputSectionBase *S) {
224   for (InputSectionDescription *ID : Opt.KeptSections)
225     if (ID->FilePat.match(basename(S)))
226       for (SectionPattern &P : ID->SectionPatterns)
227         if (P.SectionPat.match(S->Name))
228           return true;
229   return false;
230 }
231
232 // If an input string is in the form of "foo.N" where N is a number,
233 // return N. Otherwise, returns 65536, which is one greater than the
234 // lowest priority.
235 static int getPriority(StringRef S) {
236   size_t Pos = S.rfind('.');
237   if (Pos == StringRef::npos)
238     return 65536;
239   int V;
240   if (!to_integer(S.substr(Pos + 1), V, 10))
241     return 65536;
242   return V;
243 }
244
245 // A helper function for the SORT() command.
246 static std::function<bool(InputSectionBase *, InputSectionBase *)>
247 getComparator(SortSectionPolicy K) {
248   switch (K) {
249   case SortSectionPolicy::Alignment:
250     return [](InputSectionBase *A, InputSectionBase *B) {
251       // ">" is not a mistake. Sections with larger alignments are placed
252       // before sections with smaller alignments in order to reduce the
253       // amount of padding necessary. This is compatible with GNU.
254       return A->Alignment > B->Alignment;
255     };
256   case SortSectionPolicy::Name:
257     return [](InputSectionBase *A, InputSectionBase *B) {
258       return A->Name < B->Name;
259     };
260   case SortSectionPolicy::Priority:
261     return [](InputSectionBase *A, InputSectionBase *B) {
262       return getPriority(A->Name) < getPriority(B->Name);
263     };
264   default:
265     llvm_unreachable("unknown sort policy");
266   }
267 }
268
269 // A helper function for the SORT() command.
270 static bool matchConstraints(ArrayRef<InputSectionBase *> Sections,
271                              ConstraintKind Kind) {
272   if (Kind == ConstraintKind::NoConstraint)
273     return true;
274
275   bool IsRW = llvm::any_of(Sections, [](InputSectionBase *Sec) {
276     return static_cast<InputSectionBase *>(Sec)->Flags & SHF_WRITE;
277   });
278
279   return (IsRW && Kind == ConstraintKind::ReadWrite) ||
280          (!IsRW && Kind == ConstraintKind::ReadOnly);
281 }
282
283 static void sortSections(InputSection **Begin, InputSection **End,
284                          SortSectionPolicy K) {
285   if (K != SortSectionPolicy::Default && K != SortSectionPolicy::None)
286     std::stable_sort(Begin, End, getComparator(K));
287 }
288
289 // Compute and remember which sections the InputSectionDescription matches.
290 std::vector<InputSection *>
291 LinkerScript::computeInputSections(const InputSectionDescription *Cmd) {
292   std::vector<InputSection *> Ret;
293
294   // Collects all sections that satisfy constraints of Cmd.
295   for (const SectionPattern &Pat : Cmd->SectionPatterns) {
296     size_t SizeBefore = Ret.size();
297
298     for (InputSectionBase *Sec : InputSections) {
299       if (Sec->Assigned)
300         continue;
301
302       if (!Sec->Live) {
303         reportDiscarded(Sec);
304         continue;
305       }
306
307       // For -emit-relocs we have to ignore entries like
308       //   .rela.dyn : { *(.rela.data) }
309       // which are common because they are in the default bfd script.
310       if (Sec->Type == SHT_REL || Sec->Type == SHT_RELA)
311         continue;
312
313       StringRef Filename = basename(Sec);
314       if (!Cmd->FilePat.match(Filename) ||
315           Pat.ExcludedFilePat.match(Filename) ||
316           !Pat.SectionPat.match(Sec->Name))
317         continue;
318
319       Ret.push_back(cast<InputSection>(Sec));
320       Sec->Assigned = true;
321     }
322
323     // Sort sections as instructed by SORT-family commands and --sort-section
324     // option. Because SORT-family commands can be nested at most two depth
325     // (e.g. SORT_BY_NAME(SORT_BY_ALIGNMENT(.text.*))) and because the command
326     // line option is respected even if a SORT command is given, the exact
327     // behavior we have here is a bit complicated. Here are the rules.
328     //
329     // 1. If two SORT commands are given, --sort-section is ignored.
330     // 2. If one SORT command is given, and if it is not SORT_NONE,
331     //    --sort-section is handled as an inner SORT command.
332     // 3. If one SORT command is given, and if it is SORT_NONE, don't sort.
333     // 4. If no SORT command is given, sort according to --sort-section.
334     InputSection **Begin = Ret.data() + SizeBefore;
335     InputSection **End = Ret.data() + Ret.size();
336     if (Pat.SortOuter != SortSectionPolicy::None) {
337       if (Pat.SortInner == SortSectionPolicy::Default)
338         sortSections(Begin, End, Config->SortSection);
339       else
340         sortSections(Begin, End, Pat.SortInner);
341       sortSections(Begin, End, Pat.SortOuter);
342     }
343   }
344   return Ret;
345 }
346
347 void LinkerScript::discard(ArrayRef<InputSectionBase *> V) {
348   for (InputSectionBase *S : V) {
349     S->Live = false;
350     if (S == InX::ShStrTab || S == InX::Dynamic || S == InX::DynSymTab ||
351         S == InX::DynStrTab)
352       error("discarding " + S->Name + " section is not allowed");
353     discard(S->DependentSections);
354   }
355 }
356
357 std::vector<InputSectionBase *>
358 LinkerScript::createInputSectionList(OutputSectionCommand &OutCmd) {
359   std::vector<InputSectionBase *> Ret;
360
361   for (BaseCommand *Base : OutCmd.Commands) {
362     auto *Cmd = dyn_cast<InputSectionDescription>(Base);
363     if (!Cmd)
364       continue;
365
366     Cmd->Sections = computeInputSections(Cmd);
367     Ret.insert(Ret.end(), Cmd->Sections.begin(), Cmd->Sections.end());
368   }
369
370   return Ret;
371 }
372
373 void LinkerScript::processCommands(OutputSectionFactory &Factory) {
374   // A symbol can be assigned before any section is mentioned in the linker
375   // script. In an DSO, the symbol values are addresses, so the only important
376   // section values are:
377   // * SHN_UNDEF
378   // * SHN_ABS
379   // * Any value meaning a regular section.
380   // To handle that, create a dummy aether section that fills the void before
381   // the linker scripts switches to another section. It has an index of one
382   // which will map to whatever the first actual section is.
383   Aether = make<OutputSection>("", 0, SHF_ALLOC);
384   Aether->SectionIndex = 1;
385   auto State = make_unique<AddressState>(Opt);
386   // CurAddressState captures the local AddressState and makes it accessible
387   // deliberately. This is needed as there are some cases where we cannot just
388   // thread the current state through to a lambda function created by the
389   // script parser.
390   CurAddressState = State.get();
391   CurAddressState->OutSec = Aether;
392   Dot = 0;
393
394   for (size_t I = 0; I < Opt.Commands.size(); ++I) {
395     // Handle symbol assignments outside of any output section.
396     if (auto *Cmd = dyn_cast<SymbolAssignment>(Opt.Commands[I])) {
397       addSymbol(Cmd);
398       continue;
399     }
400
401     if (auto *Cmd = dyn_cast<OutputSectionCommand>(Opt.Commands[I])) {
402       std::vector<InputSectionBase *> V = createInputSectionList(*Cmd);
403
404       // The output section name `/DISCARD/' is special.
405       // Any input section assigned to it is discarded.
406       if (Cmd->Name == "/DISCARD/") {
407         discard(V);
408         continue;
409       }
410
411       // This is for ONLY_IF_RO and ONLY_IF_RW. An output section directive
412       // ".foo : ONLY_IF_R[OW] { ... }" is handled only if all member input
413       // sections satisfy a given constraint. If not, a directive is handled
414       // as if it wasn't present from the beginning.
415       //
416       // Because we'll iterate over Commands many more times, the easiest
417       // way to "make it as if it wasn't present" is to just remove it.
418       if (!matchConstraints(V, Cmd->Constraint)) {
419         for (InputSectionBase *S : V)
420           S->Assigned = false;
421         Opt.Commands.erase(Opt.Commands.begin() + I);
422         --I;
423         continue;
424       }
425
426       // A directive may contain symbol definitions like this:
427       // ".foo : { ...; bar = .; }". Handle them.
428       for (BaseCommand *Base : Cmd->Commands)
429         if (auto *OutCmd = dyn_cast<SymbolAssignment>(Base))
430           addSymbol(OutCmd);
431
432       // Handle subalign (e.g. ".foo : SUBALIGN(32) { ... }"). If subalign
433       // is given, input sections are aligned to that value, whether the
434       // given value is larger or smaller than the original section alignment.
435       if (Cmd->SubalignExpr) {
436         uint32_t Subalign = Cmd->SubalignExpr().getValue();
437         for (InputSectionBase *S : V)
438           S->Alignment = Subalign;
439       }
440
441       // Add input sections to an output section.
442       for (InputSectionBase *S : V)
443         Factory.addInputSec(S, Cmd->Name, Cmd->Sec);
444       if (OutputSection *Sec = Cmd->Sec) {
445         assert(Sec->SectionIndex == INT_MAX);
446         Sec->SectionIndex = I;
447         if (Cmd->Noload)
448           Sec->Type = SHT_NOBITS;
449         SecToCommand[Sec] = Cmd;
450       }
451     }
452   }
453   CurAddressState = nullptr;
454 }
455
456 void LinkerScript::fabricateDefaultCommands() {
457   std::vector<BaseCommand *> Commands;
458
459   // Define start address
460   uint64_t StartAddr = -1;
461
462   // The Sections with -T<section> have been sorted in order of ascending
463   // address. We must lower StartAddr if the lowest -T<section address> as
464   // calls to setDot() must be monotonically increasing.
465   for (auto &KV : Config->SectionStartMap)
466     StartAddr = std::min(StartAddr, KV.second);
467
468   Commands.push_back(make<SymbolAssignment>(
469       ".",
470       [=] {
471         return std::min(StartAddr, Config->ImageBase + elf::getHeaderSize());
472       },
473       ""));
474
475   // For each OutputSection that needs a VA fabricate an OutputSectionCommand
476   // with an InputSectionDescription describing the InputSections
477   for (OutputSection *Sec : OutputSections) {
478     auto *OSCmd = createOutputSectionCommand(Sec->Name, "<internal>");
479     OSCmd->Sec = Sec;
480     SecToCommand[Sec] = OSCmd;
481
482     Commands.push_back(OSCmd);
483     if (Sec->Sections.size()) {
484       auto *ISD = make<InputSectionDescription>("");
485       OSCmd->Commands.push_back(ISD);
486       for (InputSection *ISec : Sec->Sections) {
487         ISD->Sections.push_back(ISec);
488         ISec->Assigned = true;
489       }
490     }
491   }
492   // SECTIONS commands run before other non SECTIONS commands
493   Commands.insert(Commands.end(), Opt.Commands.begin(), Opt.Commands.end());
494   Opt.Commands = std::move(Commands);
495 }
496
497 // Add sections that didn't match any sections command.
498 void LinkerScript::addOrphanSections(OutputSectionFactory &Factory) {
499   unsigned NumCommands = Opt.Commands.size();
500   for (InputSectionBase *S : InputSections) {
501     if (!S->Live || S->Parent)
502       continue;
503     StringRef Name = getOutputSectionName(S->Name);
504     auto End = Opt.Commands.begin() + NumCommands;
505     auto I = std::find_if(Opt.Commands.begin(), End, [&](BaseCommand *Base) {
506       if (auto *Cmd = dyn_cast<OutputSectionCommand>(Base))
507         return Cmd->Name == Name;
508       return false;
509     });
510     OutputSectionCommand *Cmd;
511     if (I == End) {
512       Factory.addInputSec(S, Name);
513       OutputSection *Sec = S->getOutputSection();
514       assert(Sec->SectionIndex == INT_MAX);
515       OutputSectionCommand *&CmdRef = SecToCommand[Sec];
516       if (!CmdRef) {
517         CmdRef = createOutputSectionCommand(Sec->Name, "<internal>");
518         CmdRef->Sec = Sec;
519         Opt.Commands.push_back(CmdRef);
520       }
521       Cmd = CmdRef;
522     } else {
523       Cmd = cast<OutputSectionCommand>(*I);
524       Factory.addInputSec(S, Name, Cmd->Sec);
525       if (OutputSection *Sec = Cmd->Sec) {
526         SecToCommand[Sec] = Cmd;
527         unsigned Index = std::distance(Opt.Commands.begin(), I);
528         assert(Sec->SectionIndex == INT_MAX || Sec->SectionIndex == Index);
529         Sec->SectionIndex = Index;
530       }
531     }
532     auto *ISD = make<InputSectionDescription>("");
533     ISD->Sections.push_back(cast<InputSection>(S));
534     Cmd->Commands.push_back(ISD);
535   }
536 }
537
538 uint64_t LinkerScript::advance(uint64_t Size, unsigned Align) {
539   bool IsTbss = (CurAddressState->OutSec->Flags & SHF_TLS) &&
540                 CurAddressState->OutSec->Type == SHT_NOBITS;
541   uint64_t Start = IsTbss ? Dot + CurAddressState->ThreadBssOffset : Dot;
542   Start = alignTo(Start, Align);
543   uint64_t End = Start + Size;
544
545   if (IsTbss)
546     CurAddressState->ThreadBssOffset = End - Dot;
547   else
548     Dot = End;
549   return End;
550 }
551
552 void LinkerScript::output(InputSection *S) {
553   uint64_t Pos = advance(S->getSize(), S->Alignment);
554   S->OutSecOff = Pos - S->getSize() - CurAddressState->OutSec->Addr;
555
556   // Update output section size after adding each section. This is so that
557   // SIZEOF works correctly in the case below:
558   // .foo { *(.aaa) a = SIZEOF(.foo); *(.bbb) }
559   CurAddressState->OutSec->Size = Pos - CurAddressState->OutSec->Addr;
560
561   // If there is a memory region associated with this input section, then
562   // place the section in that region and update the region index.
563   if (CurAddressState->MemRegion) {
564     uint64_t &CurOffset =
565         CurAddressState->MemRegionOffset[CurAddressState->MemRegion];
566     CurOffset += CurAddressState->OutSec->Size;
567     uint64_t CurSize = CurOffset - CurAddressState->MemRegion->Origin;
568     if (CurSize > CurAddressState->MemRegion->Length) {
569       uint64_t OverflowAmt = CurSize - CurAddressState->MemRegion->Length;
570       error("section '" + CurAddressState->OutSec->Name +
571             "' will not fit in region '" + CurAddressState->MemRegion->Name +
572             "': overflowed by " + Twine(OverflowAmt) + " bytes");
573     }
574   }
575 }
576
577 void LinkerScript::switchTo(OutputSection *Sec) {
578   if (CurAddressState->OutSec == Sec)
579     return;
580
581   CurAddressState->OutSec = Sec;
582   CurAddressState->OutSec->Addr =
583       advance(0, CurAddressState->OutSec->Alignment);
584
585   // If neither AT nor AT> is specified for an allocatable section, the linker
586   // will set the LMA such that the difference between VMA and LMA for the
587   // section is the same as the preceding output section in the same region
588   // https://sourceware.org/binutils/docs-2.20/ld/Output-Section-LMA.html
589   if (CurAddressState->LMAOffset)
590     CurAddressState->OutSec->LMAOffset = CurAddressState->LMAOffset();
591 }
592
593 void LinkerScript::process(BaseCommand &Base) {
594   // This handles the assignments to symbol or to the dot.
595   if (auto *Cmd = dyn_cast<SymbolAssignment>(&Base)) {
596     assignSymbol(Cmd, true);
597     return;
598   }
599
600   // Handle BYTE(), SHORT(), LONG(), or QUAD().
601   if (auto *Cmd = dyn_cast<BytesDataCommand>(&Base)) {
602     Cmd->Offset = Dot - CurAddressState->OutSec->Addr;
603     Dot += Cmd->Size;
604     CurAddressState->OutSec->Size = Dot - CurAddressState->OutSec->Addr;
605     return;
606   }
607
608   // Handle ASSERT().
609   if (auto *Cmd = dyn_cast<AssertCommand>(&Base)) {
610     Cmd->Expression();
611     return;
612   }
613
614   // Handle a single input section description command.
615   // It calculates and assigns the offsets for each section and also
616   // updates the output section size.
617   auto &Cmd = cast<InputSectionDescription>(Base);
618   for (InputSection *Sec : Cmd.Sections) {
619     // We tentatively added all synthetic sections at the beginning and removed
620     // empty ones afterwards (because there is no way to know whether they were
621     // going be empty or not other than actually running linker scripts.)
622     // We need to ignore remains of empty sections.
623     if (auto *S = dyn_cast<SyntheticSection>(Sec))
624       if (S->empty())
625         continue;
626
627     if (!Sec->Live)
628       continue;
629     assert(CurAddressState->OutSec == Sec->getParent());
630     output(Sec);
631   }
632 }
633
634 // This function searches for a memory region to place the given output
635 // section in. If found, a pointer to the appropriate memory region is
636 // returned. Otherwise, a nullptr is returned.
637 MemoryRegion *LinkerScript::findMemoryRegion(OutputSectionCommand *Cmd) {
638   // If a memory region name was specified in the output section command,
639   // then try to find that region first.
640   if (!Cmd->MemoryRegionName.empty()) {
641     auto It = Opt.MemoryRegions.find(Cmd->MemoryRegionName);
642     if (It != Opt.MemoryRegions.end())
643       return &It->second;
644     error("memory region '" + Cmd->MemoryRegionName + "' not declared");
645     return nullptr;
646   }
647
648   // If at least one memory region is defined, all sections must
649   // belong to some memory region. Otherwise, we don't need to do
650   // anything for memory regions.
651   if (Opt.MemoryRegions.empty())
652     return nullptr;
653
654   OutputSection *Sec = Cmd->Sec;
655   // See if a region can be found by matching section flags.
656   for (auto &Pair : Opt.MemoryRegions) {
657     MemoryRegion &M = Pair.second;
658     if ((M.Flags & Sec->Flags) && (M.NegFlags & Sec->Flags) == 0)
659       return &M;
660   }
661
662   // Otherwise, no suitable region was found.
663   if (Sec->Flags & SHF_ALLOC)
664     error("no memory region specified for section '" + Sec->Name + "'");
665   return nullptr;
666 }
667
668 // This function assigns offsets to input sections and an output section
669 // for a single sections command (e.g. ".text { *(.text); }").
670 void LinkerScript::assignOffsets(OutputSectionCommand *Cmd) {
671   OutputSection *Sec = Cmd->Sec;
672   if (!Sec)
673     return;
674
675   if (!(Sec->Flags & SHF_ALLOC))
676     Dot = 0;
677   else if (Cmd->AddrExpr)
678     setDot(Cmd->AddrExpr, Cmd->Location, false);
679
680   if (Cmd->LMAExpr) {
681     uint64_t D = Dot;
682     CurAddressState->LMAOffset = [=] { return Cmd->LMAExpr().getValue() - D; };
683   }
684
685   CurAddressState->MemRegion = Cmd->MemRegion;
686   if (CurAddressState->MemRegion)
687     Dot = CurAddressState->MemRegionOffset[CurAddressState->MemRegion];
688   switchTo(Sec);
689
690   // We do not support custom layout for compressed debug sectons.
691   // At this point we already know their size and have compressed content.
692   if (CurAddressState->OutSec->Flags & SHF_COMPRESSED)
693     return;
694
695   for (BaseCommand *C : Cmd->Commands)
696     process(*C);
697 }
698
699 void LinkerScript::removeEmptyCommands() {
700   // It is common practice to use very generic linker scripts. So for any
701   // given run some of the output sections in the script will be empty.
702   // We could create corresponding empty output sections, but that would
703   // clutter the output.
704   // We instead remove trivially empty sections. The bfd linker seems even
705   // more aggressive at removing them.
706   auto Pos = std::remove_if(
707       Opt.Commands.begin(), Opt.Commands.end(), [&](BaseCommand *Base) {
708         if (auto *Cmd = dyn_cast<OutputSectionCommand>(Base))
709           return Cmd->Sec == nullptr;
710         return false;
711       });
712   Opt.Commands.erase(Pos, Opt.Commands.end());
713 }
714
715 static bool isAllSectionDescription(const OutputSectionCommand &Cmd) {
716   for (BaseCommand *Base : Cmd.Commands)
717     if (!isa<InputSectionDescription>(*Base))
718       return false;
719   return true;
720 }
721
722 void LinkerScript::adjustSectionsBeforeSorting() {
723   // If the output section contains only symbol assignments, create a
724   // corresponding output section. The bfd linker seems to only create them if
725   // '.' is assigned to, but creating these section should not have any bad
726   // consequeces and gives us a section to put the symbol in.
727   uint64_t Flags = SHF_ALLOC;
728
729   for (int I = 0, E = Opt.Commands.size(); I != E; ++I) {
730     auto *Cmd = dyn_cast<OutputSectionCommand>(Opt.Commands[I]);
731     if (!Cmd)
732       continue;
733     if (OutputSection *Sec = Cmd->Sec) {
734       Flags = Sec->Flags;
735       continue;
736     }
737
738     if (isAllSectionDescription(*Cmd))
739       continue;
740
741     auto *OutSec = make<OutputSection>(Cmd->Name, SHT_PROGBITS, Flags);
742     OutSec->SectionIndex = I;
743     Cmd->Sec = OutSec;
744     SecToCommand[OutSec] = Cmd;
745   }
746 }
747
748 void LinkerScript::adjustSectionsAfterSorting() {
749   // Try and find an appropriate memory region to assign offsets in.
750   for (BaseCommand *Base : Opt.Commands) {
751     if (auto *Cmd = dyn_cast<OutputSectionCommand>(Base)) {
752       Cmd->MemRegion = findMemoryRegion(Cmd);
753       // Handle align (e.g. ".foo : ALIGN(16) { ... }").
754       if (Cmd->AlignExpr)
755         Cmd->Sec->updateAlignment(Cmd->AlignExpr().getValue());
756     }
757   }
758
759   // If output section command doesn't specify any segments,
760   // and we haven't previously assigned any section to segment,
761   // then we simply assign section to the very first load segment.
762   // Below is an example of such linker script:
763   // PHDRS { seg PT_LOAD; }
764   // SECTIONS { .aaa : { *(.aaa) } }
765   std::vector<StringRef> DefPhdrs;
766   auto FirstPtLoad =
767       std::find_if(Opt.PhdrsCommands.begin(), Opt.PhdrsCommands.end(),
768                    [](const PhdrsCommand &Cmd) { return Cmd.Type == PT_LOAD; });
769   if (FirstPtLoad != Opt.PhdrsCommands.end())
770     DefPhdrs.push_back(FirstPtLoad->Name);
771
772   // Walk the commands and propagate the program headers to commands that don't
773   // explicitly specify them.
774   for (BaseCommand *Base : Opt.Commands) {
775     auto *Cmd = dyn_cast<OutputSectionCommand>(Base);
776     if (!Cmd)
777       continue;
778
779     if (Cmd->Phdrs.empty()) {
780       OutputSection *Sec = Cmd->Sec;
781       // To match the bfd linker script behaviour, only propagate program
782       // headers to sections that are allocated.
783       if (Sec && (Sec->Flags & SHF_ALLOC))
784         Cmd->Phdrs = DefPhdrs;
785     } else {
786       DefPhdrs = Cmd->Phdrs;
787     }
788   }
789
790   removeEmptyCommands();
791 }
792
793 void LinkerScript::processNonSectionCommands() {
794   for (BaseCommand *Base : Opt.Commands) {
795     if (auto *Cmd = dyn_cast<SymbolAssignment>(Base))
796       assignSymbol(Cmd, false);
797     else if (auto *Cmd = dyn_cast<AssertCommand>(Base))
798       Cmd->Expression();
799   }
800 }
801
802 void LinkerScript::allocateHeaders(std::vector<PhdrEntry> &Phdrs) {
803   uint64_t Min = std::numeric_limits<uint64_t>::max();
804   for (OutputSectionCommand *Cmd : OutputSectionCommands) {
805     OutputSection *Sec = Cmd->Sec;
806     if (Sec->Flags & SHF_ALLOC)
807       Min = std::min<uint64_t>(Min, Sec->Addr);
808   }
809
810   auto FirstPTLoad = llvm::find_if(
811       Phdrs, [](const PhdrEntry &E) { return E.p_type == PT_LOAD; });
812   if (FirstPTLoad == Phdrs.end())
813     return;
814
815   uint64_t HeaderSize = getHeaderSize();
816   if (HeaderSize <= Min || Script->hasPhdrsCommands()) {
817     Min = alignDown(Min - HeaderSize, Config->MaxPageSize);
818     Out::ElfHeader->Addr = Min;
819     Out::ProgramHeaders->Addr = Min + Out::ElfHeader->Size;
820     return;
821   }
822
823   assert(FirstPTLoad->First == Out::ElfHeader);
824   OutputSection *ActualFirst = nullptr;
825   for (OutputSectionCommand *Cmd : OutputSectionCommands) {
826     OutputSection *Sec = Cmd->Sec;
827     if (Sec->FirstInPtLoad == Out::ElfHeader) {
828       ActualFirst = Sec;
829       break;
830     }
831   }
832   if (ActualFirst) {
833     for (OutputSectionCommand *Cmd : OutputSectionCommands) {
834       OutputSection *Sec = Cmd->Sec;
835       if (Sec->FirstInPtLoad == Out::ElfHeader)
836         Sec->FirstInPtLoad = ActualFirst;
837     }
838     FirstPTLoad->First = ActualFirst;
839   } else {
840     Phdrs.erase(FirstPTLoad);
841   }
842
843   auto PhdrI = llvm::find_if(
844       Phdrs, [](const PhdrEntry &E) { return E.p_type == PT_PHDR; });
845   if (PhdrI != Phdrs.end())
846     Phdrs.erase(PhdrI);
847 }
848
849 LinkerScript::AddressState::AddressState(const ScriptConfiguration &Opt) {
850   for (auto &MRI : Opt.MemoryRegions) {
851     const MemoryRegion *MR = &MRI.second;
852     MemRegionOffset[MR] = MR->Origin;
853   }
854 }
855
856 void LinkerScript::assignAddresses() {
857   // Assign addresses as instructed by linker script SECTIONS sub-commands.
858   Dot = 0;
859   auto State = make_unique<AddressState>(Opt);
860   // CurAddressState captures the local AddressState and makes it accessible
861   // deliberately. This is needed as there are some cases where we cannot just
862   // thread the current state through to a lambda function created by the
863   // script parser.
864   CurAddressState = State.get();
865   ErrorOnMissingSection = true;
866   switchTo(Aether);
867
868   for (BaseCommand *Base : Opt.Commands) {
869     if (auto *Cmd = dyn_cast<SymbolAssignment>(Base)) {
870       assignSymbol(Cmd, false);
871       continue;
872     }
873
874     if (auto *Cmd = dyn_cast<AssertCommand>(Base)) {
875       Cmd->Expression();
876       continue;
877     }
878
879     auto *Cmd = cast<OutputSectionCommand>(Base);
880     assignOffsets(Cmd);
881   }
882   CurAddressState = nullptr;
883 }
884
885 // Creates program headers as instructed by PHDRS linker script command.
886 std::vector<PhdrEntry> LinkerScript::createPhdrs() {
887   std::vector<PhdrEntry> Ret;
888
889   // Process PHDRS and FILEHDR keywords because they are not
890   // real output sections and cannot be added in the following loop.
891   for (const PhdrsCommand &Cmd : Opt.PhdrsCommands) {
892     Ret.emplace_back(Cmd.Type, Cmd.Flags == UINT_MAX ? PF_R : Cmd.Flags);
893     PhdrEntry &Phdr = Ret.back();
894
895     if (Cmd.HasFilehdr)
896       Phdr.add(Out::ElfHeader);
897     if (Cmd.HasPhdrs)
898       Phdr.add(Out::ProgramHeaders);
899
900     if (Cmd.LMAExpr) {
901       Phdr.p_paddr = Cmd.LMAExpr().getValue();
902       Phdr.HasLMA = true;
903     }
904   }
905
906   // Add output sections to program headers.
907   for (OutputSectionCommand *Cmd : OutputSectionCommands) {
908     // Assign headers specified by linker script
909     for (size_t Id : getPhdrIndices(Cmd)) {
910       OutputSection *Sec = Cmd->Sec;
911       Ret[Id].add(Sec);
912       if (Opt.PhdrsCommands[Id].Flags == UINT_MAX)
913         Ret[Id].p_flags |= Sec->getPhdrFlags();
914     }
915   }
916   return Ret;
917 }
918
919 bool LinkerScript::ignoreInterpSection() {
920   // Ignore .interp section in case we have PHDRS specification
921   // and PT_INTERP isn't listed.
922   if (Opt.PhdrsCommands.empty())
923     return false;
924   for (PhdrsCommand &Cmd : Opt.PhdrsCommands)
925     if (Cmd.Type == PT_INTERP)
926       return false;
927   return true;
928 }
929
930 OutputSectionCommand *LinkerScript::getCmd(OutputSection *Sec) const {
931   auto I = SecToCommand.find(Sec);
932   if (I == SecToCommand.end())
933     return nullptr;
934   return I->second;
935 }
936
937 void OutputSectionCommand::sort(std::function<int(InputSectionBase *S)> Order) {
938   typedef std::pair<unsigned, InputSection *> Pair;
939   auto Comp = [](const Pair &A, const Pair &B) { return A.first < B.first; };
940
941   std::vector<Pair> V;
942   assert(Commands.size() == 1);
943   auto *ISD = cast<InputSectionDescription>(Commands[0]);
944   for (InputSection *S : ISD->Sections)
945     V.push_back({Order(S), S});
946   std::stable_sort(V.begin(), V.end(), Comp);
947   ISD->Sections.clear();
948   for (Pair &P : V)
949     ISD->Sections.push_back(P.second);
950 }
951
952 // Returns true if S matches /Filename.?\.o$/.
953 static bool isCrtBeginEnd(StringRef S, StringRef Filename) {
954   if (!S.endswith(".o"))
955     return false;
956   S = S.drop_back(2);
957   if (S.endswith(Filename))
958     return true;
959   return !S.empty() && S.drop_back().endswith(Filename);
960 }
961
962 static bool isCrtbegin(StringRef S) { return isCrtBeginEnd(S, "crtbegin"); }
963 static bool isCrtend(StringRef S) { return isCrtBeginEnd(S, "crtend"); }
964
965 // .ctors and .dtors are sorted by this priority from highest to lowest.
966 //
967 //  1. The section was contained in crtbegin (crtbegin contains
968 //     some sentinel value in its .ctors and .dtors so that the runtime
969 //     can find the beginning of the sections.)
970 //
971 //  2. The section has an optional priority value in the form of ".ctors.N"
972 //     or ".dtors.N" where N is a number. Unlike .{init,fini}_array,
973 //     they are compared as string rather than number.
974 //
975 //  3. The section is just ".ctors" or ".dtors".
976 //
977 //  4. The section was contained in crtend, which contains an end marker.
978 //
979 // In an ideal world, we don't need this function because .init_array and
980 // .ctors are duplicate features (and .init_array is newer.) However, there
981 // are too many real-world use cases of .ctors, so we had no choice to
982 // support that with this rather ad-hoc semantics.
983 static bool compCtors(const InputSection *A, const InputSection *B) {
984   bool BeginA = isCrtbegin(A->File->getName());
985   bool BeginB = isCrtbegin(B->File->getName());
986   if (BeginA != BeginB)
987     return BeginA;
988   bool EndA = isCrtend(A->File->getName());
989   bool EndB = isCrtend(B->File->getName());
990   if (EndA != EndB)
991     return EndB;
992   StringRef X = A->Name;
993   StringRef Y = B->Name;
994   assert(X.startswith(".ctors") || X.startswith(".dtors"));
995   assert(Y.startswith(".ctors") || Y.startswith(".dtors"));
996   X = X.substr(6);
997   Y = Y.substr(6);
998   if (X.empty() && Y.empty())
999     return false;
1000   return X < Y;
1001 }
1002
1003 // Sorts input sections by the special rules for .ctors and .dtors.
1004 // Unfortunately, the rules are different from the one for .{init,fini}_array.
1005 // Read the comment above.
1006 void OutputSectionCommand::sortCtorsDtors() {
1007   assert(Commands.size() == 1);
1008   auto *ISD = cast<InputSectionDescription>(Commands[0]);
1009   std::stable_sort(ISD->Sections.begin(), ISD->Sections.end(), compCtors);
1010 }
1011
1012 // Sorts input sections by section name suffixes, so that .foo.N comes
1013 // before .foo.M if N < M. Used to sort .{init,fini}_array.N sections.
1014 // We want to keep the original order if the priorities are the same
1015 // because the compiler keeps the original initialization order in a
1016 // translation unit and we need to respect that.
1017 // For more detail, read the section of the GCC's manual about init_priority.
1018 void OutputSectionCommand::sortInitFini() {
1019   // Sort sections by priority.
1020   sort([](InputSectionBase *S) { return getPriority(S->Name); });
1021 }
1022
1023 uint32_t OutputSectionCommand::getFiller() {
1024   if (Filler)
1025     return *Filler;
1026   if (Sec->Flags & SHF_EXECINSTR)
1027     return Target->TrapInstr;
1028   return 0;
1029 }
1030
1031 static void writeInt(uint8_t *Buf, uint64_t Data, uint64_t Size) {
1032   if (Size == 1)
1033     *Buf = Data;
1034   else if (Size == 2)
1035     write16(Buf, Data, Config->Endianness);
1036   else if (Size == 4)
1037     write32(Buf, Data, Config->Endianness);
1038   else if (Size == 8)
1039     write64(Buf, Data, Config->Endianness);
1040   else
1041     llvm_unreachable("unsupported Size argument");
1042 }
1043
1044 static bool compareByFilePosition(InputSection *A, InputSection *B) {
1045   // Synthetic doesn't have link order dependecy, stable_sort will keep it last
1046   if (A->kind() == InputSectionBase::Synthetic ||
1047       B->kind() == InputSectionBase::Synthetic)
1048     return false;
1049   InputSection *LA = A->getLinkOrderDep();
1050   InputSection *LB = B->getLinkOrderDep();
1051   OutputSection *AOut = LA->getParent();
1052   OutputSection *BOut = LB->getParent();
1053   if (AOut != BOut)
1054     return AOut->SectionIndex < BOut->SectionIndex;
1055   return LA->OutSecOff < LB->OutSecOff;
1056 }
1057
1058 template <class ELFT>
1059 static void finalizeShtGroup(OutputSection *OS,
1060                              ArrayRef<InputSection *> Sections) {
1061   assert(Config->Relocatable && Sections.size() == 1);
1062
1063   // sh_link field for SHT_GROUP sections should contain the section index of
1064   // the symbol table.
1065   OS->Link = InX::SymTab->getParent()->SectionIndex;
1066
1067   // sh_info then contain index of an entry in symbol table section which
1068   // provides signature of the section group.
1069   elf::ObjectFile<ELFT> *Obj = Sections[0]->getFile<ELFT>();
1070   ArrayRef<SymbolBody *> Symbols = Obj->getSymbols();
1071   OS->Info = InX::SymTab->getSymbolIndex(Symbols[Sections[0]->Info - 1]);
1072 }
1073
1074 template <class ELFT> void OutputSectionCommand::finalize() {
1075   // Link order may be distributed across several InputSectionDescriptions
1076   // but sort must consider them all at once.
1077   std::vector<InputSection **> ScriptSections;
1078   std::vector<InputSection *> Sections;
1079   for (BaseCommand *Base : Commands)
1080     if (auto *ISD = dyn_cast<InputSectionDescription>(Base))
1081       for (InputSection *&IS : ISD->Sections) {
1082         ScriptSections.push_back(&IS);
1083         Sections.push_back(IS);
1084       }
1085
1086   if ((Sec->Flags & SHF_LINK_ORDER)) {
1087     std::stable_sort(Sections.begin(), Sections.end(), compareByFilePosition);
1088     for (int I = 0, N = Sections.size(); I < N; ++I)
1089       *ScriptSections[I] = Sections[I];
1090
1091     // We must preserve the link order dependency of sections with the
1092     // SHF_LINK_ORDER flag. The dependency is indicated by the sh_link field. We
1093     // need to translate the InputSection sh_link to the OutputSection sh_link,
1094     // all InputSections in the OutputSection have the same dependency.
1095     if (auto *D = Sections.front()->getLinkOrderDep())
1096       Sec->Link = D->getParent()->SectionIndex;
1097   }
1098
1099   uint32_t Type = Sec->Type;
1100   if (Type == SHT_GROUP) {
1101     finalizeShtGroup<ELFT>(Sec, Sections);
1102     return;
1103   }
1104
1105   if (!Config->CopyRelocs || (Type != SHT_RELA && Type != SHT_REL))
1106     return;
1107
1108   InputSection *First = Sections[0];
1109   if (isa<SyntheticSection>(First))
1110     return;
1111
1112   Sec->Link = InX::SymTab->getParent()->SectionIndex;
1113   // sh_info for SHT_REL[A] sections should contain the section header index of
1114   // the section to which the relocation applies.
1115   InputSectionBase *S = First->getRelocatedSection();
1116   Sec->Info = S->getOutputSection()->SectionIndex;
1117   Sec->Flags |= SHF_INFO_LINK;
1118 }
1119
1120 // Compress section contents if this section contains debug info.
1121 template <class ELFT> void OutputSectionCommand::maybeCompress() {
1122   typedef typename ELFT::Chdr Elf_Chdr;
1123
1124   // Compress only DWARF debug sections.
1125   if (!Config->CompressDebugSections || (Sec->Flags & SHF_ALLOC) ||
1126       !Name.startswith(".debug_"))
1127     return;
1128
1129   // Create a section header.
1130   Sec->ZDebugHeader.resize(sizeof(Elf_Chdr));
1131   auto *Hdr = reinterpret_cast<Elf_Chdr *>(Sec->ZDebugHeader.data());
1132   Hdr->ch_type = ELFCOMPRESS_ZLIB;
1133   Hdr->ch_size = Sec->Size;
1134   Hdr->ch_addralign = Sec->Alignment;
1135
1136   // Write section contents to a temporary buffer and compress it.
1137   std::vector<uint8_t> Buf(Sec->Size);
1138   writeTo<ELFT>(Buf.data());
1139   if (Error E = zlib::compress(toStringRef(Buf), Sec->CompressedData))
1140     fatal("compress failed: " + llvm::toString(std::move(E)));
1141
1142   // Update section headers.
1143   Sec->Size = sizeof(Elf_Chdr) + Sec->CompressedData.size();
1144   Sec->Flags |= SHF_COMPRESSED;
1145 }
1146
1147 template <class ELFT> void OutputSectionCommand::writeTo(uint8_t *Buf) {
1148   if (Sec->Type == SHT_NOBITS)
1149     return;
1150
1151   Sec->Loc = Buf;
1152
1153   // If -compress-debug-section is specified and if this is a debug seciton,
1154   // we've already compressed section contents. If that's the case,
1155   // just write it down.
1156   if (!Sec->CompressedData.empty()) {
1157     memcpy(Buf, Sec->ZDebugHeader.data(), Sec->ZDebugHeader.size());
1158     memcpy(Buf + Sec->ZDebugHeader.size(), Sec->CompressedData.data(),
1159            Sec->CompressedData.size());
1160     return;
1161   }
1162
1163   // Write leading padding.
1164   std::vector<InputSection *> Sections;
1165   for (BaseCommand *Cmd : Commands)
1166     if (auto *ISD = dyn_cast<InputSectionDescription>(Cmd))
1167       for (InputSection *IS : ISD->Sections)
1168         if (IS->Live)
1169           Sections.push_back(IS);
1170   uint32_t Filler = getFiller();
1171   if (Filler)
1172     fill(Buf, Sections.empty() ? Sec->Size : Sections[0]->OutSecOff, Filler);
1173
1174   parallelForEachN(0, Sections.size(), [=](size_t I) {
1175     InputSection *IS = Sections[I];
1176     IS->writeTo<ELFT>(Buf);
1177
1178     // Fill gaps between sections.
1179     if (Filler) {
1180       uint8_t *Start = Buf + IS->OutSecOff + IS->getSize();
1181       uint8_t *End;
1182       if (I + 1 == Sections.size())
1183         End = Buf + Sec->Size;
1184       else
1185         End = Buf + Sections[I + 1]->OutSecOff;
1186       fill(Start, End - Start, Filler);
1187     }
1188   });
1189
1190   // Linker scripts may have BYTE()-family commands with which you
1191   // can write arbitrary bytes to the output. Process them if any.
1192   for (BaseCommand *Base : Commands)
1193     if (auto *Data = dyn_cast<BytesDataCommand>(Base))
1194       writeInt(Buf + Data->Offset, Data->Expression().getValue(), Data->Size);
1195 }
1196
1197 ExprValue LinkerScript::getSymbolValue(const Twine &Loc, StringRef S) {
1198   if (S == ".")
1199     return {CurAddressState->OutSec, Dot - CurAddressState->OutSec->Addr, Loc};
1200   if (SymbolBody *B = findSymbol(S)) {
1201     if (auto *D = dyn_cast<DefinedRegular>(B))
1202       return {D->Section, D->Value, Loc};
1203     if (auto *C = dyn_cast<DefinedCommon>(B))
1204       return {InX::Common, C->Offset, Loc};
1205   }
1206   error(Loc + ": symbol not found: " + S);
1207   return 0;
1208 }
1209
1210 bool LinkerScript::isDefined(StringRef S) { return findSymbol(S) != nullptr; }
1211
1212 static const size_t NoPhdr = -1;
1213
1214 // Returns indices of ELF headers containing specific section. Each index is a
1215 // zero based number of ELF header listed within PHDRS {} script block.
1216 std::vector<size_t> LinkerScript::getPhdrIndices(OutputSectionCommand *Cmd) {
1217   std::vector<size_t> Ret;
1218   for (StringRef PhdrName : Cmd->Phdrs) {
1219     size_t Index = getPhdrIndex(Cmd->Location, PhdrName);
1220     if (Index != NoPhdr)
1221       Ret.push_back(Index);
1222   }
1223   return Ret;
1224 }
1225
1226 // Returns the index of the segment named PhdrName if found otherwise
1227 // NoPhdr. When not found, if PhdrName is not the special case value 'NONE'
1228 // (which can be used to explicitly specify that a section isn't assigned to a
1229 // segment) then error.
1230 size_t LinkerScript::getPhdrIndex(const Twine &Loc, StringRef PhdrName) {
1231   size_t I = 0;
1232   for (PhdrsCommand &Cmd : Opt.PhdrsCommands) {
1233     if (Cmd.Name == PhdrName)
1234       return I;
1235     ++I;
1236   }
1237   if (PhdrName != "NONE")
1238     error(Loc + ": section header '" + PhdrName + "' is not listed in PHDRS");
1239   return NoPhdr;
1240 }
1241
1242 template void OutputSectionCommand::writeTo<ELF32LE>(uint8_t *Buf);
1243 template void OutputSectionCommand::writeTo<ELF32BE>(uint8_t *Buf);
1244 template void OutputSectionCommand::writeTo<ELF64LE>(uint8_t *Buf);
1245 template void OutputSectionCommand::writeTo<ELF64BE>(uint8_t *Buf);
1246
1247 template void OutputSectionCommand::maybeCompress<ELF32LE>();
1248 template void OutputSectionCommand::maybeCompress<ELF32BE>();
1249 template void OutputSectionCommand::maybeCompress<ELF64LE>();
1250 template void OutputSectionCommand::maybeCompress<ELF64BE>();
1251
1252 template void OutputSectionCommand::finalize<ELF32LE>();
1253 template void OutputSectionCommand::finalize<ELF32BE>();
1254 template void OutputSectionCommand::finalize<ELF64LE>();
1255 template void OutputSectionCommand::finalize<ELF64BE>();