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 "Writer.h"
24 #include "llvm/ADT/STLExtras.h"
25 #include "llvm/ADT/StringRef.h"
26 #include "llvm/Support/Casting.h"
27 #include "llvm/Support/ELF.h"
28 #include "llvm/Support/Endian.h"
29 #include "llvm/Support/ErrorHandling.h"
30 #include "llvm/Support/FileSystem.h"
31 #include "llvm/Support/Path.h"
32 #include <algorithm>
33 #include <cassert>
34 #include <cstddef>
35 #include <cstdint>
36 #include <iterator>
37 #include <limits>
38 #include <string>
39 #include <vector>
40 
41 using namespace llvm;
42 using namespace llvm::ELF;
43 using namespace llvm::object;
44 using namespace llvm::support::endian;
45 using namespace lld;
46 using namespace lld::elf;
47 
48 LinkerScript *elf::Script;
49 
50 uint64_t ExprValue::getValue() const {
51   if (Sec)
52     return Sec->getOffset(Val) + Sec->getOutputSection()->Addr;
53   return Val;
54 }
55 
56 uint64_t ExprValue::getSecAddr() const {
57   if (Sec)
58     return Sec->getOffset(0) + Sec->getOutputSection()->Addr;
59   return 0;
60 }
61 
62 template <class ELFT> static SymbolBody *addRegular(SymbolAssignment *Cmd) {
63   Symbol *Sym;
64   uint8_t Visibility = Cmd->Hidden ? STV_HIDDEN : STV_DEFAULT;
65   std::tie(Sym, std::ignore) = Symtab<ELFT>::X->insert(
66       Cmd->Name, /*Type*/ 0, Visibility, /*CanOmitFromDynSym*/ false,
67       /*File*/ nullptr);
68   Sym->Binding = STB_GLOBAL;
69   ExprValue Value = Cmd->Expression();
70   SectionBase *Sec = Value.isAbsolute() ? nullptr : Value.Sec;
71 
72   // We want to set symbol values early if we can. This allows us to use symbols
73   // as variables in linker scripts. Doing so allows us to write expressions
74   // like this: `alignment = 16; . = ALIGN(., alignment)`
75   uint64_t SymValue = Value.isAbsolute() ? Value.getValue() : 0;
76   replaceBody<DefinedRegular>(Sym, Cmd->Name, /*IsLocal=*/false, Visibility,
77                               STT_NOTYPE, SymValue, 0, Sec, nullptr);
78   return Sym->body();
79 }
80 
81 OutputSection *LinkerScript::getOutputSection(const Twine &Loc,
82                                               StringRef Name) {
83   for (OutputSection *Sec : *OutputSections)
84     if (Sec->Name == Name)
85       return Sec;
86 
87   static OutputSection Dummy("", 0, 0);
88   if (ErrorOnMissingSection)
89     error(Loc + ": undefined section " + Name);
90   return &Dummy;
91 }
92 
93 // This function is essentially the same as getOutputSection(Name)->Size,
94 // but it won't print out an error message if a given section is not found.
95 //
96 // Linker script does not create an output section if its content is empty.
97 // We want to allow SIZEOF(.foo) where .foo is a section which happened to
98 // be empty. That is why this function is different from getOutputSection().
99 uint64_t LinkerScript::getOutputSectionSize(StringRef Name) {
100   for (OutputSection *Sec : *OutputSections)
101     if (Sec->Name == Name)
102       return Sec->Size;
103   return 0;
104 }
105 
106 void LinkerScript::setDot(Expr E, const Twine &Loc, bool InSec) {
107   uint64_t Val = E().getValue();
108   if (Val < Dot) {
109     if (InSec)
110       error(Loc + ": unable to move location counter backward for: " +
111             CurOutSec->Name);
112     else
113       error(Loc + ": unable to move location counter backward");
114   }
115   Dot = Val;
116   // Update to location counter means update to section size.
117   if (InSec)
118     CurOutSec->Size = Dot - CurOutSec->Addr;
119 }
120 
121 // Sets value of a symbol. Two kinds of symbols are processed: synthetic
122 // symbols, whose value is an offset from beginning of section and regular
123 // symbols whose value is absolute.
124 void LinkerScript::assignSymbol(SymbolAssignment *Cmd, bool InSec) {
125   if (Cmd->Name == ".") {
126     setDot(Cmd->Expression, Cmd->Location, InSec);
127     return;
128   }
129 
130   if (!Cmd->Sym)
131     return;
132 
133   auto *Sym = cast<DefinedRegular>(Cmd->Sym);
134   ExprValue V = Cmd->Expression();
135   if (V.isAbsolute()) {
136     Sym->Value = V.getValue();
137   } else {
138     Sym->Section = V.Sec;
139     if (Sym->Section->Flags & SHF_ALLOC)
140       Sym->Value = V.Val;
141     else
142       Sym->Value = V.getValue();
143   }
144 }
145 
146 static SymbolBody *findSymbol(StringRef S) {
147   switch (Config->EKind) {
148   case ELF32LEKind:
149     return Symtab<ELF32LE>::X->find(S);
150   case ELF32BEKind:
151     return Symtab<ELF32BE>::X->find(S);
152   case ELF64LEKind:
153     return Symtab<ELF64LE>::X->find(S);
154   case ELF64BEKind:
155     return Symtab<ELF64BE>::X->find(S);
156   default:
157     llvm_unreachable("unknown Config->EKind");
158   }
159 }
160 
161 static SymbolBody *addRegularSymbol(SymbolAssignment *Cmd) {
162   switch (Config->EKind) {
163   case ELF32LEKind:
164     return addRegular<ELF32LE>(Cmd);
165   case ELF32BEKind:
166     return addRegular<ELF32BE>(Cmd);
167   case ELF64LEKind:
168     return addRegular<ELF64LE>(Cmd);
169   case ELF64BEKind:
170     return addRegular<ELF64BE>(Cmd);
171   default:
172     llvm_unreachable("unknown Config->EKind");
173   }
174 }
175 
176 void LinkerScript::addSymbol(SymbolAssignment *Cmd) {
177   if (Cmd->Name == ".")
178     return;
179 
180   // If a symbol was in PROVIDE(), we need to define it only when
181   // it is a referenced undefined symbol.
182   SymbolBody *B = findSymbol(Cmd->Name);
183   if (Cmd->Provide && (!B || B->isDefined()))
184     return;
185 
186   Cmd->Sym = addRegularSymbol(Cmd);
187 }
188 
189 bool SymbolAssignment::classof(const BaseCommand *C) {
190   return C->Kind == AssignmentKind;
191 }
192 
193 bool OutputSectionCommand::classof(const BaseCommand *C) {
194   return C->Kind == OutputSectionKind;
195 }
196 
197 bool InputSectionDescription::classof(const BaseCommand *C) {
198   return C->Kind == InputSectionKind;
199 }
200 
201 bool AssertCommand::classof(const BaseCommand *C) {
202   return C->Kind == AssertKind;
203 }
204 
205 bool BytesDataCommand::classof(const BaseCommand *C) {
206   return C->Kind == BytesDataKind;
207 }
208 
209 static StringRef basename(InputSectionBase *S) {
210   if (S->File)
211     return sys::path::filename(S->File->getName());
212   return "";
213 }
214 
215 bool LinkerScript::shouldKeep(InputSectionBase *S) {
216   for (InputSectionDescription *ID : Opt.KeptSections)
217     if (ID->FilePat.match(basename(S)))
218       for (SectionPattern &P : ID->SectionPatterns)
219         if (P.SectionPat.match(S->Name))
220           return true;
221   return false;
222 }
223 
224 // A helper function for the SORT() command.
225 static std::function<bool(InputSectionBase *, InputSectionBase *)>
226 getComparator(SortSectionPolicy K) {
227   switch (K) {
228   case SortSectionPolicy::Alignment:
229     return [](InputSectionBase *A, InputSectionBase *B) {
230       // ">" is not a mistake. Sections with larger alignments are placed
231       // before sections with smaller alignments in order to reduce the
232       // amount of padding necessary. This is compatible with GNU.
233       return A->Alignment > B->Alignment;
234     };
235   case SortSectionPolicy::Name:
236     return [](InputSectionBase *A, InputSectionBase *B) {
237       return A->Name < B->Name;
238     };
239   case SortSectionPolicy::Priority:
240     return [](InputSectionBase *A, InputSectionBase *B) {
241       return getPriority(A->Name) < getPriority(B->Name);
242     };
243   default:
244     llvm_unreachable("unknown sort policy");
245   }
246 }
247 
248 // A helper function for the SORT() command.
249 static bool matchConstraints(ArrayRef<InputSectionBase *> Sections,
250                              ConstraintKind Kind) {
251   if (Kind == ConstraintKind::NoConstraint)
252     return true;
253 
254   bool IsRW = llvm::any_of(Sections, [](InputSectionBase *Sec) {
255     return static_cast<InputSectionBase *>(Sec)->Flags & SHF_WRITE;
256   });
257 
258   return (IsRW && Kind == ConstraintKind::ReadWrite) ||
259          (!IsRW && Kind == ConstraintKind::ReadOnly);
260 }
261 
262 static void sortSections(InputSectionBase **Begin, InputSectionBase **End,
263                          SortSectionPolicy K) {
264   if (K != SortSectionPolicy::Default && K != SortSectionPolicy::None)
265     std::stable_sort(Begin, End, getComparator(K));
266 }
267 
268 // Compute and remember which sections the InputSectionDescription matches.
269 std::vector<InputSectionBase *>
270 LinkerScript::computeInputSections(const InputSectionDescription *Cmd) {
271   std::vector<InputSectionBase *> Ret;
272 
273   // Collects all sections that satisfy constraints of Cmd.
274   for (const SectionPattern &Pat : Cmd->SectionPatterns) {
275     size_t SizeBefore = Ret.size();
276 
277     for (InputSectionBase *Sec : InputSections) {
278       if (Sec->Assigned)
279         continue;
280 
281       // For -emit-relocs we have to ignore entries like
282       //   .rela.dyn : { *(.rela.data) }
283       // which are common because they are in the default bfd script.
284       if (Sec->Type == SHT_REL || Sec->Type == SHT_RELA)
285         continue;
286 
287       StringRef Filename = basename(Sec);
288       if (!Cmd->FilePat.match(Filename) ||
289           Pat.ExcludedFilePat.match(Filename) ||
290           !Pat.SectionPat.match(Sec->Name))
291         continue;
292 
293       Ret.push_back(Sec);
294       Sec->Assigned = true;
295     }
296 
297     // Sort sections as instructed by SORT-family commands and --sort-section
298     // option. Because SORT-family commands can be nested at most two depth
299     // (e.g. SORT_BY_NAME(SORT_BY_ALIGNMENT(.text.*))) and because the command
300     // line option is respected even if a SORT command is given, the exact
301     // behavior we have here is a bit complicated. Here are the rules.
302     //
303     // 1. If two SORT commands are given, --sort-section is ignored.
304     // 2. If one SORT command is given, and if it is not SORT_NONE,
305     //    --sort-section is handled as an inner SORT command.
306     // 3. If one SORT command is given, and if it is SORT_NONE, don't sort.
307     // 4. If no SORT command is given, sort according to --sort-section.
308     InputSectionBase **Begin = Ret.data() + SizeBefore;
309     InputSectionBase **End = Ret.data() + Ret.size();
310     if (Pat.SortOuter != SortSectionPolicy::None) {
311       if (Pat.SortInner == SortSectionPolicy::Default)
312         sortSections(Begin, End, Config->SortSection);
313       else
314         sortSections(Begin, End, Pat.SortInner);
315       sortSections(Begin, End, Pat.SortOuter);
316     }
317   }
318   return Ret;
319 }
320 
321 void LinkerScript::discard(ArrayRef<InputSectionBase *> V) {
322   for (InputSectionBase *S : V) {
323     S->Live = false;
324     if (S == InX::ShStrTab)
325       error("discarding .shstrtab section is not allowed");
326     discard(S->DependentSections);
327   }
328 }
329 
330 std::vector<InputSectionBase *>
331 LinkerScript::createInputSectionList(OutputSectionCommand &OutCmd) {
332   std::vector<InputSectionBase *> Ret;
333 
334   for (BaseCommand *Base : OutCmd.Commands) {
335     auto *Cmd = dyn_cast<InputSectionDescription>(Base);
336     if (!Cmd)
337       continue;
338 
339     Cmd->Sections = computeInputSections(Cmd);
340     Ret.insert(Ret.end(), Cmd->Sections.begin(), Cmd->Sections.end());
341   }
342 
343   return Ret;
344 }
345 
346 void LinkerScript::processCommands(OutputSectionFactory &Factory) {
347   // A symbol can be assigned before any section is mentioned in the linker
348   // script. In an DSO, the symbol values are addresses, so the only important
349   // section values are:
350   // * SHN_UNDEF
351   // * SHN_ABS
352   // * Any value meaning a regular section.
353   // To handle that, create a dummy aether section that fills the void before
354   // the linker scripts switches to another section. It has an index of one
355   // which will map to whatever the first actual section is.
356   Aether = make<OutputSection>("", 0, SHF_ALLOC);
357   Aether->SectionIndex = 1;
358   CurOutSec = Aether;
359   Dot = 0;
360 
361   for (size_t I = 0; I < Opt.Commands.size(); ++I) {
362     // Handle symbol assignments outside of any output section.
363     if (auto *Cmd = dyn_cast<SymbolAssignment>(Opt.Commands[I])) {
364       addSymbol(Cmd);
365       continue;
366     }
367 
368     if (auto *Cmd = dyn_cast<OutputSectionCommand>(Opt.Commands[I])) {
369       std::vector<InputSectionBase *> V = createInputSectionList(*Cmd);
370 
371       // The output section name `/DISCARD/' is special.
372       // Any input section assigned to it is discarded.
373       if (Cmd->Name == "/DISCARD/") {
374         discard(V);
375         continue;
376       }
377 
378       // This is for ONLY_IF_RO and ONLY_IF_RW. An output section directive
379       // ".foo : ONLY_IF_R[OW] { ... }" is handled only if all member input
380       // sections satisfy a given constraint. If not, a directive is handled
381       // as if it wasn't present from the beginning.
382       //
383       // Because we'll iterate over Commands many more times, the easiest
384       // way to "make it as if it wasn't present" is to just remove it.
385       if (!matchConstraints(V, Cmd->Constraint)) {
386         for (InputSectionBase *S : V)
387           S->Assigned = false;
388         Opt.Commands.erase(Opt.Commands.begin() + I);
389         --I;
390         continue;
391       }
392 
393       // A directive may contain symbol definitions like this:
394       // ".foo : { ...; bar = .; }". Handle them.
395       for (BaseCommand *Base : Cmd->Commands)
396         if (auto *OutCmd = dyn_cast<SymbolAssignment>(Base))
397           addSymbol(OutCmd);
398 
399       // Handle subalign (e.g. ".foo : SUBALIGN(32) { ... }"). If subalign
400       // is given, input sections are aligned to that value, whether the
401       // given value is larger or smaller than the original section alignment.
402       if (Cmd->SubalignExpr) {
403         uint32_t Subalign = Cmd->SubalignExpr().getValue();
404         for (InputSectionBase *S : V)
405           S->Alignment = Subalign;
406       }
407 
408       // Add input sections to an output section.
409       for (InputSectionBase *S : V)
410         Factory.addInputSec(S, Cmd->Name);
411     }
412   }
413   CurOutSec = nullptr;
414 }
415 
416 void LinkerScript::fabricateDefaultCommands(bool AllocateHeader) {
417   std::vector<BaseCommand *> Commands;
418 
419   // Define start address
420   uint64_t StartAddr = Config->ImageBase;
421   if (AllocateHeader)
422     StartAddr += elf::getHeaderSize();
423 
424   // The Sections with -T<section> are sorted in order of ascending address
425   // we must use this if it is lower than StartAddr as calls to setDot() must
426   // be monotonically increasing
427   if (!Config->SectionStartMap.empty()) {
428     uint64_t LowestSecStart = Config->SectionStartMap.begin()->second;
429     StartAddr = std::min(StartAddr, LowestSecStart);
430   }
431   Commands.push_back(
432       make<SymbolAssignment>(".", [=] { return StartAddr; }, ""));
433 
434   // For each OutputSection that needs a VA fabricate an OutputSectionCommand
435   // with an InputSectionDescription describing the InputSections
436   for (OutputSection *Sec : *OutputSections) {
437     if (!(Sec->Flags & SHF_ALLOC))
438       continue;
439 
440     auto I = Config->SectionStartMap.find(Sec->Name);
441     if (I != Config->SectionStartMap.end())
442       Commands.push_back(
443           make<SymbolAssignment>(".", [=] { return I->second; }, ""));
444 
445     auto *OSCmd = make<OutputSectionCommand>(Sec->Name);
446     OSCmd->Sec = Sec;
447     if (Sec->PageAlign)
448       OSCmd->AddrExpr = [=] {
449         return alignTo(Script->getDot(), Config->MaxPageSize);
450       };
451     Commands.push_back(OSCmd);
452     if (Sec->Sections.size()) {
453       auto *ISD = make<InputSectionDescription>("");
454       OSCmd->Commands.push_back(ISD);
455       for (InputSection *ISec : Sec->Sections) {
456         ISD->Sections.push_back(ISec);
457         ISec->Assigned = true;
458       }
459     }
460   }
461   // SECTIONS commands run before other non SECTIONS commands
462   Commands.insert(Commands.end(), Opt.Commands.begin(), Opt.Commands.end());
463   Opt.Commands = std::move(Commands);
464 }
465 
466 // Add sections that didn't match any sections command.
467 void LinkerScript::addOrphanSections(OutputSectionFactory &Factory) {
468   for (InputSectionBase *S : InputSections)
469     if (S->Live && !S->OutSec)
470       Factory.addInputSec(S, getOutputSectionName(S->Name));
471 }
472 
473 static bool isTbss(OutputSection *Sec) {
474   return (Sec->Flags & SHF_TLS) && Sec->Type == SHT_NOBITS;
475 }
476 
477 void LinkerScript::output(InputSection *S) {
478   if (!AlreadyOutputIS.insert(S).second)
479     return;
480   bool IsTbss = isTbss(CurOutSec);
481 
482   uint64_t Pos = IsTbss ? Dot + ThreadBssOffset : Dot;
483   Pos = alignTo(Pos, S->Alignment);
484   S->OutSecOff = Pos - CurOutSec->Addr;
485   Pos += S->getSize();
486 
487   // Update output section size after adding each section. This is so that
488   // SIZEOF works correctly in the case below:
489   // .foo { *(.aaa) a = SIZEOF(.foo); *(.bbb) }
490   CurOutSec->Size = Pos - CurOutSec->Addr;
491 
492   // If there is a memory region associated with this input section, then
493   // place the section in that region and update the region index.
494   if (CurMemRegion) {
495     CurMemRegion->Offset += CurOutSec->Size;
496     uint64_t CurSize = CurMemRegion->Offset - CurMemRegion->Origin;
497     if (CurSize > CurMemRegion->Length) {
498       uint64_t OverflowAmt = CurSize - CurMemRegion->Length;
499       error("section '" + CurOutSec->Name + "' will not fit in region '" +
500             CurMemRegion->Name + "': overflowed by " + Twine(OverflowAmt) +
501             " bytes");
502     }
503   }
504 
505   if (IsTbss)
506     ThreadBssOffset = Pos - Dot;
507   else
508     Dot = Pos;
509 }
510 
511 void LinkerScript::flush() {
512   assert(CurOutSec);
513   if (!AlreadyOutputOS.insert(CurOutSec).second)
514     return;
515   for (InputSection *I : CurOutSec->Sections)
516     output(I);
517 }
518 
519 void LinkerScript::switchTo(OutputSection *Sec) {
520   if (CurOutSec == Sec)
521     return;
522   if (AlreadyOutputOS.count(Sec))
523     return;
524 
525   CurOutSec = Sec;
526 
527   Dot = alignTo(Dot, CurOutSec->Alignment);
528   CurOutSec->Addr = isTbss(CurOutSec) ? Dot + ThreadBssOffset : Dot;
529 
530   // If neither AT nor AT> is specified for an allocatable section, the linker
531   // will set the LMA such that the difference between VMA and LMA for the
532   // section is the same as the preceding output section in the same region
533   // https://sourceware.org/binutils/docs-2.20/ld/Output-Section-LMA.html
534   if (LMAOffset)
535     CurOutSec->LMAOffset = LMAOffset();
536 }
537 
538 void LinkerScript::process(BaseCommand &Base) {
539   // This handles the assignments to symbol or to the dot.
540   if (auto *Cmd = dyn_cast<SymbolAssignment>(&Base)) {
541     assignSymbol(Cmd, true);
542     return;
543   }
544 
545   // Handle BYTE(), SHORT(), LONG(), or QUAD().
546   if (auto *Cmd = dyn_cast<BytesDataCommand>(&Base)) {
547     Cmd->Offset = Dot - CurOutSec->Addr;
548     Dot += Cmd->Size;
549     CurOutSec->Size = Dot - CurOutSec->Addr;
550     return;
551   }
552 
553   // Handle ASSERT().
554   if (auto *Cmd = dyn_cast<AssertCommand>(&Base)) {
555     Cmd->Expression();
556     return;
557   }
558 
559   // Handle a single input section description command.
560   // It calculates and assigns the offsets for each section and also
561   // updates the output section size.
562   auto &Cmd = cast<InputSectionDescription>(Base);
563   for (InputSectionBase *Sec : Cmd.Sections) {
564     // We tentatively added all synthetic sections at the beginning and removed
565     // empty ones afterwards (because there is no way to know whether they were
566     // going be empty or not other than actually running linker scripts.)
567     // We need to ignore remains of empty sections.
568     if (auto *S = dyn_cast<SyntheticSection>(Sec))
569       if (S->empty())
570         continue;
571 
572     if (!Sec->Live)
573       continue;
574     assert(CurOutSec == Sec->OutSec || AlreadyOutputOS.count(Sec->OutSec));
575     output(cast<InputSection>(Sec));
576   }
577 }
578 
579 static OutputSection *
580 findSection(StringRef Name, const std::vector<OutputSection *> &Sections) {
581   for (OutputSection *Sec : Sections)
582     if (Sec->Name == Name)
583       return Sec;
584   return nullptr;
585 }
586 
587 // This function searches for a memory region to place the given output
588 // section in. If found, a pointer to the appropriate memory region is
589 // returned. Otherwise, a nullptr is returned.
590 MemoryRegion *LinkerScript::findMemoryRegion(OutputSectionCommand *Cmd) {
591   // If a memory region name was specified in the output section command,
592   // then try to find that region first.
593   if (!Cmd->MemoryRegionName.empty()) {
594     auto It = Opt.MemoryRegions.find(Cmd->MemoryRegionName);
595     if (It != Opt.MemoryRegions.end())
596       return &It->second;
597     error("memory region '" + Cmd->MemoryRegionName + "' not declared");
598     return nullptr;
599   }
600 
601   // If at least one memory region is defined, all sections must
602   // belong to some memory region. Otherwise, we don't need to do
603   // anything for memory regions.
604   if (Opt.MemoryRegions.empty())
605     return nullptr;
606 
607   OutputSection *Sec = Cmd->Sec;
608   // See if a region can be found by matching section flags.
609   for (auto &Pair : Opt.MemoryRegions) {
610     MemoryRegion &M = Pair.second;
611     if ((M.Flags & Sec->Flags) && (M.NegFlags & Sec->Flags) == 0)
612       return &M;
613   }
614 
615   // Otherwise, no suitable region was found.
616   if (Sec->Flags & SHF_ALLOC)
617     error("no memory region specified for section '" + Sec->Name + "'");
618   return nullptr;
619 }
620 
621 // This function assigns offsets to input sections and an output section
622 // for a single sections command (e.g. ".text { *(.text); }").
623 void LinkerScript::assignOffsets(OutputSectionCommand *Cmd) {
624   OutputSection *Sec = Cmd->Sec;
625   if (!Sec)
626     return;
627 
628   if (Cmd->AddrExpr && (Sec->Flags & SHF_ALLOC))
629     setDot(Cmd->AddrExpr, Cmd->Location, false);
630 
631   if (Cmd->LMAExpr) {
632     uint64_t D = Dot;
633     LMAOffset = [=] { return Cmd->LMAExpr().getValue() - D; };
634   }
635 
636   CurMemRegion = Cmd->MemRegion;
637   if (CurMemRegion)
638     Dot = CurMemRegion->Offset;
639   switchTo(Sec);
640 
641   // flush() may add orphan sections, so the order of flush() and
642   // symbol assignments is important. We want to call flush() first so
643   // that symbols pointing the end of the current section points to
644   // the location after orphan sections.
645   auto Mid =
646       std::find_if(Cmd->Commands.rbegin(), Cmd->Commands.rend(),
647                    [](BaseCommand *Cmd) { return !isa<SymbolAssignment>(Cmd); })
648           .base();
649   for (auto I = Cmd->Commands.begin(); I != Mid; ++I)
650     process(**I);
651   flush();
652   for (auto I = Mid, E = Cmd->Commands.end(); I != E; ++I)
653     process(**I);
654 }
655 
656 void LinkerScript::removeEmptyCommands() {
657   // It is common practice to use very generic linker scripts. So for any
658   // given run some of the output sections in the script will be empty.
659   // We could create corresponding empty output sections, but that would
660   // clutter the output.
661   // We instead remove trivially empty sections. The bfd linker seems even
662   // more aggressive at removing them.
663   auto Pos = std::remove_if(
664       Opt.Commands.begin(), Opt.Commands.end(), [&](BaseCommand *Base) {
665         if (auto *Cmd = dyn_cast<OutputSectionCommand>(Base))
666           return !Cmd->Sec;
667         return false;
668       });
669   Opt.Commands.erase(Pos, Opt.Commands.end());
670 }
671 
672 static bool isAllSectionDescription(const OutputSectionCommand &Cmd) {
673   for (BaseCommand *Base : Cmd.Commands)
674     if (!isa<InputSectionDescription>(*Base))
675       return false;
676   return true;
677 }
678 
679 void LinkerScript::adjustSectionsBeforeSorting() {
680   // If the output section contains only symbol assignments, create a
681   // corresponding output section. The bfd linker seems to only create them if
682   // '.' is assigned to, but creating these section should not have any bad
683   // consequeces and gives us a section to put the symbol in.
684   uint64_t Flags = SHF_ALLOC;
685   uint32_t Type = SHT_PROGBITS;
686   for (BaseCommand *Base : Opt.Commands) {
687     auto *Cmd = dyn_cast<OutputSectionCommand>(Base);
688     if (!Cmd)
689       continue;
690     if (OutputSection *Sec = findSection(Cmd->Name, *OutputSections)) {
691       Cmd->Sec = Sec;
692       Flags = Sec->Flags;
693       Type = Sec->Type;
694       continue;
695     }
696 
697     if (isAllSectionDescription(*Cmd))
698       continue;
699 
700     auto *OutSec = make<OutputSection>(Cmd->Name, Type, Flags);
701     OutputSections->push_back(OutSec);
702     Cmd->Sec = OutSec;
703   }
704 }
705 
706 void LinkerScript::adjustSectionsAfterSorting() {
707   placeOrphanSections();
708 
709   // Try and find an appropriate memory region to assign offsets in.
710   for (BaseCommand *Base : Opt.Commands) {
711     if (auto *Cmd = dyn_cast<OutputSectionCommand>(Base)) {
712       Cmd->MemRegion = findMemoryRegion(Cmd);
713       // Handle align (e.g. ".foo : ALIGN(16) { ... }").
714       if (Cmd->AlignExpr)
715 	Cmd->Sec->updateAlignment(Cmd->AlignExpr().getValue());
716     }
717   }
718 
719   // If output section command doesn't specify any segments,
720   // and we haven't previously assigned any section to segment,
721   // then we simply assign section to the very first load segment.
722   // Below is an example of such linker script:
723   // PHDRS { seg PT_LOAD; }
724   // SECTIONS { .aaa : { *(.aaa) } }
725   std::vector<StringRef> DefPhdrs;
726   auto FirstPtLoad =
727       std::find_if(Opt.PhdrsCommands.begin(), Opt.PhdrsCommands.end(),
728                    [](const PhdrsCommand &Cmd) { return Cmd.Type == PT_LOAD; });
729   if (FirstPtLoad != Opt.PhdrsCommands.end())
730     DefPhdrs.push_back(FirstPtLoad->Name);
731 
732   // Walk the commands and propagate the program headers to commands that don't
733   // explicitly specify them.
734   for (BaseCommand *Base : Opt.Commands) {
735     auto *Cmd = dyn_cast<OutputSectionCommand>(Base);
736     if (!Cmd)
737       continue;
738 
739     if (Cmd->Phdrs.empty())
740       Cmd->Phdrs = DefPhdrs;
741     else
742       DefPhdrs = Cmd->Phdrs;
743   }
744 
745   removeEmptyCommands();
746 }
747 
748 // When placing orphan sections, we want to place them after symbol assignments
749 // so that an orphan after
750 //   begin_foo = .;
751 //   foo : { *(foo) }
752 //   end_foo = .;
753 // doesn't break the intended meaning of the begin/end symbols.
754 // We don't want to go over sections since Writer<ELFT>::sortSections is the
755 // one in charge of deciding the order of the sections.
756 // We don't want to go over alignments, since doing so in
757 //  rx_sec : { *(rx_sec) }
758 //  . = ALIGN(0x1000);
759 //  /* The RW PT_LOAD starts here*/
760 //  rw_sec : { *(rw_sec) }
761 // would mean that the RW PT_LOAD would become unaligned.
762 static bool shouldSkip(BaseCommand *Cmd) {
763   if (isa<OutputSectionCommand>(Cmd))
764     return false;
765   if (auto *Assign = dyn_cast<SymbolAssignment>(Cmd))
766     return Assign->Name != ".";
767   return true;
768 }
769 
770 // Orphan sections are sections present in the input files which are
771 // not explicitly placed into the output file by the linker script.
772 //
773 // When the control reaches this function, Opt.Commands contains
774 // output section commands for non-orphan sections only. This function
775 // adds new elements for orphan sections so that all sections are
776 // explicitly handled by Opt.Commands.
777 //
778 // Writer<ELFT>::sortSections has already sorted output sections.
779 // What we need to do is to scan OutputSections vector and
780 // Opt.Commands in parallel to find orphan sections. If there is an
781 // output section that doesn't have a corresponding entry in
782 // Opt.Commands, we will insert a new entry to Opt.Commands.
783 //
784 // There is some ambiguity as to where exactly a new entry should be
785 // inserted, because Opt.Commands contains not only output section
786 // commands but also other types of commands such as symbol assignment
787 // expressions. There's no correct answer here due to the lack of the
788 // formal specification of the linker script. We use heuristics to
789 // determine whether a new output command should be added before or
790 // after another commands. For the details, look at shouldSkip
791 // function.
792 void LinkerScript::placeOrphanSections() {
793   // The OutputSections are already in the correct order.
794   // This loops creates or moves commands as needed so that they are in the
795   // correct order.
796   int CmdIndex = 0;
797 
798   // As a horrible special case, skip the first . assignment if it is before any
799   // section. We do this because it is common to set a load address by starting
800   // the script with ". = 0xabcd" and the expectation is that every section is
801   // after that.
802   auto FirstSectionOrDotAssignment =
803       std::find_if(Opt.Commands.begin(), Opt.Commands.end(),
804                    [](BaseCommand *Cmd) { return !shouldSkip(Cmd); });
805   if (FirstSectionOrDotAssignment != Opt.Commands.end()) {
806     CmdIndex = FirstSectionOrDotAssignment - Opt.Commands.begin();
807     if (isa<SymbolAssignment>(**FirstSectionOrDotAssignment))
808       ++CmdIndex;
809   }
810 
811   for (OutputSection *Sec : *OutputSections) {
812     StringRef Name = Sec->Name;
813 
814     // Find the last spot where we can insert a command and still get the
815     // correct result.
816     auto CmdIter = Opt.Commands.begin() + CmdIndex;
817     auto E = Opt.Commands.end();
818     while (CmdIter != E && shouldSkip(*CmdIter)) {
819       ++CmdIter;
820       ++CmdIndex;
821     }
822 
823     auto Pos = std::find_if(CmdIter, E, [&](BaseCommand *Base) {
824       auto *Cmd = dyn_cast<OutputSectionCommand>(Base);
825       return Cmd && Cmd->Name == Name;
826     });
827     if (Pos == E) {
828       auto *Cmd = make<OutputSectionCommand>(Name);
829       Cmd->Sec = Sec;
830       Opt.Commands.insert(CmdIter, Cmd);
831       ++CmdIndex;
832       continue;
833     }
834 
835     // Continue from where we found it.
836     CmdIndex = (Pos - Opt.Commands.begin()) + 1;
837   }
838 }
839 
840 void LinkerScript::processNonSectionCommands() {
841   for (BaseCommand *Base : Opt.Commands) {
842     if (auto *Cmd = dyn_cast<SymbolAssignment>(Base))
843       assignSymbol(Cmd, false);
844     else if (auto *Cmd = dyn_cast<AssertCommand>(Base))
845       Cmd->Expression();
846   }
847 }
848 
849 void LinkerScript::assignAddresses(std::vector<PhdrEntry> &Phdrs) {
850   // Assign addresses as instructed by linker script SECTIONS sub-commands.
851   Dot = 0;
852   ErrorOnMissingSection = true;
853   switchTo(Aether);
854 
855   for (BaseCommand *Base : Opt.Commands) {
856     if (auto *Cmd = dyn_cast<SymbolAssignment>(Base)) {
857       assignSymbol(Cmd, false);
858       continue;
859     }
860 
861     if (auto *Cmd = dyn_cast<AssertCommand>(Base)) {
862       Cmd->Expression();
863       continue;
864     }
865 
866     auto *Cmd = cast<OutputSectionCommand>(Base);
867     assignOffsets(Cmd);
868   }
869 
870   uint64_t MinVA = std::numeric_limits<uint64_t>::max();
871   for (OutputSection *Sec : *OutputSections) {
872     if (Sec->Flags & SHF_ALLOC)
873       MinVA = std::min<uint64_t>(MinVA, Sec->Addr);
874     else
875       Sec->Addr = 0;
876   }
877 
878   allocateHeaders(Phdrs, *OutputSections, MinVA);
879 }
880 
881 // Creates program headers as instructed by PHDRS linker script command.
882 std::vector<PhdrEntry> LinkerScript::createPhdrs() {
883   std::vector<PhdrEntry> Ret;
884 
885   // Process PHDRS and FILEHDR keywords because they are not
886   // real output sections and cannot be added in the following loop.
887   for (const PhdrsCommand &Cmd : Opt.PhdrsCommands) {
888     Ret.emplace_back(Cmd.Type, Cmd.Flags == UINT_MAX ? PF_R : Cmd.Flags);
889     PhdrEntry &Phdr = Ret.back();
890 
891     if (Cmd.HasFilehdr)
892       Phdr.add(Out::ElfHeader);
893     if (Cmd.HasPhdrs)
894       Phdr.add(Out::ProgramHeaders);
895 
896     if (Cmd.LMAExpr) {
897       Phdr.p_paddr = Cmd.LMAExpr().getValue();
898       Phdr.HasLMA = true;
899     }
900   }
901 
902   // Add output sections to program headers.
903   for (OutputSection *Sec : *OutputSections) {
904     if (!(Sec->Flags & SHF_ALLOC))
905       break;
906 
907     // Assign headers specified by linker script
908     for (size_t Id : getPhdrIndices(Sec->Name)) {
909       Ret[Id].add(Sec);
910       if (Opt.PhdrsCommands[Id].Flags == UINT_MAX)
911         Ret[Id].p_flags |= Sec->getPhdrFlags();
912     }
913   }
914   return Ret;
915 }
916 
917 bool LinkerScript::ignoreInterpSection() {
918   // Ignore .interp section in case we have PHDRS specification
919   // and PT_INTERP isn't listed.
920   if (Opt.PhdrsCommands.empty())
921     return false;
922   for (PhdrsCommand &Cmd : Opt.PhdrsCommands)
923     if (Cmd.Type == PT_INTERP)
924       return false;
925   return true;
926 }
927 
928 Optional<uint32_t> LinkerScript::getFiller(StringRef Name) {
929   for (BaseCommand *Base : Opt.Commands)
930     if (auto *Cmd = dyn_cast<OutputSectionCommand>(Base))
931       if (Cmd->Name == Name)
932         return Cmd->Filler;
933   return None;
934 }
935 
936 static void writeInt(uint8_t *Buf, uint64_t Data, uint64_t Size) {
937   if (Size == 1)
938     *Buf = Data;
939   else if (Size == 2)
940     write16(Buf, Data, Config->Endianness);
941   else if (Size == 4)
942     write32(Buf, Data, Config->Endianness);
943   else if (Size == 8)
944     write64(Buf, Data, Config->Endianness);
945   else
946     llvm_unreachable("unsupported Size argument");
947 }
948 
949 void LinkerScript::writeDataBytes(StringRef Name, uint8_t *Buf) {
950   int I = getSectionIndex(Name);
951   if (I == INT_MAX)
952     return;
953 
954   auto *Cmd = dyn_cast<OutputSectionCommand>(Opt.Commands[I]);
955   for (BaseCommand *Base : Cmd->Commands)
956     if (auto *Data = dyn_cast<BytesDataCommand>(Base))
957       writeInt(Buf + Data->Offset, Data->Expression().getValue(), Data->Size);
958 }
959 
960 bool LinkerScript::hasLMA(StringRef Name) {
961   for (BaseCommand *Base : Opt.Commands)
962     if (auto *Cmd = dyn_cast<OutputSectionCommand>(Base))
963       if (Cmd->LMAExpr && Cmd->Name == Name)
964         return true;
965   return false;
966 }
967 
968 // Returns the index of the given section name in linker script
969 // SECTIONS commands. Sections are laid out as the same order as they
970 // were in the script. If a given name did not appear in the script,
971 // it returns INT_MAX, so that it will be laid out at end of file.
972 int LinkerScript::getSectionIndex(StringRef Name) {
973   for (int I = 0, E = Opt.Commands.size(); I != E; ++I)
974     if (auto *Cmd = dyn_cast<OutputSectionCommand>(Opt.Commands[I]))
975       if (Cmd->Name == Name)
976         return I;
977   return INT_MAX;
978 }
979 
980 ExprValue LinkerScript::getSymbolValue(const Twine &Loc, StringRef S) {
981   if (S == ".")
982     return {CurOutSec, Dot - CurOutSec->Addr};
983   if (SymbolBody *B = findSymbol(S)) {
984     if (auto *D = dyn_cast<DefinedRegular>(B))
985       return {D->Section, D->Value};
986     if (auto *C = dyn_cast<DefinedCommon>(B))
987       return {InX::Common, C->Offset};
988   }
989   error(Loc + ": symbol not found: " + S);
990   return 0;
991 }
992 
993 bool LinkerScript::isDefined(StringRef S) { return findSymbol(S) != nullptr; }
994 
995 // Returns indices of ELF headers containing specific section, identified
996 // by Name. Each index is a zero based number of ELF header listed within
997 // PHDRS {} script block.
998 std::vector<size_t> LinkerScript::getPhdrIndices(StringRef SectionName) {
999   for (BaseCommand *Base : Opt.Commands) {
1000     auto *Cmd = dyn_cast<OutputSectionCommand>(Base);
1001     if (!Cmd || Cmd->Name != SectionName)
1002       continue;
1003 
1004     std::vector<size_t> Ret;
1005     for (StringRef PhdrName : Cmd->Phdrs)
1006       Ret.push_back(getPhdrIndex(Cmd->Location, PhdrName));
1007     return Ret;
1008   }
1009   return {};
1010 }
1011 
1012 size_t LinkerScript::getPhdrIndex(const Twine &Loc, StringRef PhdrName) {
1013   size_t I = 0;
1014   for (PhdrsCommand &Cmd : Opt.PhdrsCommands) {
1015     if (Cmd.Name == PhdrName)
1016       return I;
1017     ++I;
1018   }
1019   error(Loc + ": section header '" + PhdrName + "' is not listed in PHDRS");
1020   return 0;
1021 }
1022