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