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/Endian.h"
31 #include "llvm/Support/ErrorHandling.h"
32 #include "llvm/Support/FileSystem.h"
33 #include "llvm/Support/Path.h"
34 #include <algorithm>
35 #include <cassert>
36 #include <cstddef>
37 #include <cstdint>
38 #include <iterator>
39 #include <limits>
40 #include <string>
41 #include <vector>
42 
43 using namespace llvm;
44 using namespace llvm::ELF;
45 using namespace llvm::object;
46 using namespace llvm::support::endian;
47 using namespace lld;
48 using namespace lld::elf;
49 
50 LinkerScript *elf::Script;
51 
52 uint64_t ExprValue::getValue() const {
53   if (Sec) {
54     if (OutputSection *OS = Sec->getOutputSection())
55       return alignTo(Sec->getOffset(Val) + OS->Addr, Alignment);
56     error(Loc + ": unable to evaluate expression: input section " + Sec->Name +
57           " has no output section assigned");
58   }
59   return alignTo(Val, Alignment);
60 }
61 
62 uint64_t ExprValue::getSecAddr() const {
63   if (Sec)
64     return Sec->getOffset(0) + Sec->getOutputSection()->Addr;
65   return 0;
66 }
67 
68 static SymbolBody *addRegular(SymbolAssignment *Cmd) {
69   Symbol *Sym;
70   uint8_t Visibility = Cmd->Hidden ? STV_HIDDEN : STV_DEFAULT;
71   std::tie(Sym, std::ignore) = Symtab->insert(Cmd->Name, /*Type*/ 0, Visibility,
72                                               /*CanOmitFromDynSym*/ false,
73                                               /*File*/ nullptr);
74   Sym->Binding = STB_GLOBAL;
75   ExprValue Value = Cmd->Expression();
76   SectionBase *Sec = Value.isAbsolute() ? nullptr : Value.Sec;
77 
78   // We want to set symbol values early if we can. This allows us to use symbols
79   // as variables in linker scripts. Doing so allows us to write expressions
80   // like this: `alignment = 16; . = ALIGN(., alignment)`
81   uint64_t SymValue = Value.isAbsolute() ? Value.getValue() : 0;
82   replaceBody<DefinedRegular>(Sym, nullptr, Cmd->Name, /*IsLocal=*/false,
83                               Visibility, STT_NOTYPE, SymValue, 0, Sec);
84   return Sym->body();
85 }
86 
87 OutputSection *LinkerScript::createOutputSection(StringRef Name,
88                                                  StringRef Location) {
89   OutputSection *&SecRef = NameToOutputSection[Name];
90   OutputSection *Sec;
91   if (SecRef && SecRef->Location.empty()) {
92     // There was a forward reference.
93     Sec = SecRef;
94   } else {
95     Sec = make<OutputSection>(Name, SHT_PROGBITS, 0);
96     if (!SecRef)
97       SecRef = Sec;
98   }
99   Sec->Location = Location;
100   return Sec;
101 }
102 
103 OutputSection *LinkerScript::getOrCreateOutputSection(StringRef Name) {
104   OutputSection *&CmdRef = NameToOutputSection[Name];
105   if (!CmdRef)
106     CmdRef = make<OutputSection>(Name, SHT_PROGBITS, 0);
107   return CmdRef;
108 }
109 
110 void LinkerScript::setDot(Expr E, const Twine &Loc, bool InSec) {
111   uint64_t Val = E().getValue();
112   if (Val < Dot && InSec)
113     error(Loc + ": unable to move location counter backward for: " +
114           CurAddressState->OutSec->Name);
115   Dot = Val;
116   // Update to location counter means update to section size.
117   if (InSec)
118     CurAddressState->OutSec->Size = Dot - CurAddressState->OutSec->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     Sym->Value = alignTo(V.Val, V.Alignment);
140   }
141 }
142 
143 void LinkerScript::addSymbol(SymbolAssignment *Cmd) {
144   if (Cmd->Name == ".")
145     return;
146 
147   // If a symbol was in PROVIDE(), we need to define it only when
148   // it is a referenced undefined symbol.
149   SymbolBody *B = Symtab->find(Cmd->Name);
150   if (Cmd->Provide && (!B || B->isDefined()))
151     return;
152 
153   Cmd->Sym = addRegular(Cmd);
154 }
155 
156 bool SymbolAssignment::classof(const BaseCommand *C) {
157   return C->Kind == AssignmentKind;
158 }
159 
160 bool InputSectionDescription::classof(const BaseCommand *C) {
161   return C->Kind == InputSectionKind;
162 }
163 
164 bool AssertCommand::classof(const BaseCommand *C) {
165   return C->Kind == AssertKind;
166 }
167 
168 bool BytesDataCommand::classof(const BaseCommand *C) {
169   return C->Kind == BytesDataKind;
170 }
171 
172 static StringRef basename(InputSectionBase *S) {
173   if (S->File)
174     return sys::path::filename(S->File->getName());
175   return "";
176 }
177 
178 bool LinkerScript::shouldKeep(InputSectionBase *S) {
179   for (InputSectionDescription *ID : Opt.KeptSections)
180     if (ID->FilePat.match(basename(S)))
181       for (SectionPattern &P : ID->SectionPatterns)
182         if (P.SectionPat.match(S->Name))
183           return true;
184   return false;
185 }
186 
187 // A helper function for the SORT() command.
188 static std::function<bool(InputSectionBase *, InputSectionBase *)>
189 getComparator(SortSectionPolicy K) {
190   switch (K) {
191   case SortSectionPolicy::Alignment:
192     return [](InputSectionBase *A, InputSectionBase *B) {
193       // ">" is not a mistake. Sections with larger alignments are placed
194       // before sections with smaller alignments in order to reduce the
195       // amount of padding necessary. This is compatible with GNU.
196       return A->Alignment > B->Alignment;
197     };
198   case SortSectionPolicy::Name:
199     return [](InputSectionBase *A, InputSectionBase *B) {
200       return A->Name < B->Name;
201     };
202   case SortSectionPolicy::Priority:
203     return [](InputSectionBase *A, InputSectionBase *B) {
204       return getPriority(A->Name) < getPriority(B->Name);
205     };
206   default:
207     llvm_unreachable("unknown sort policy");
208   }
209 }
210 
211 // A helper function for the SORT() command.
212 static bool matchConstraints(ArrayRef<InputSectionBase *> Sections,
213                              ConstraintKind Kind) {
214   if (Kind == ConstraintKind::NoConstraint)
215     return true;
216 
217   bool IsRW = llvm::any_of(Sections, [](InputSectionBase *Sec) {
218     return static_cast<InputSectionBase *>(Sec)->Flags & SHF_WRITE;
219   });
220 
221   return (IsRW && Kind == ConstraintKind::ReadWrite) ||
222          (!IsRW && Kind == ConstraintKind::ReadOnly);
223 }
224 
225 static void sortSections(InputSection **Begin, InputSection **End,
226                          SortSectionPolicy K) {
227   if (K != SortSectionPolicy::Default && K != SortSectionPolicy::None)
228     std::stable_sort(Begin, End, getComparator(K));
229 }
230 
231 static llvm::DenseMap<SectionBase *, int> getSectionOrder() {
232   switch (Config->EKind) {
233   case ELF32LEKind:
234     return buildSectionOrder<ELF32LE>();
235   case ELF32BEKind:
236     return buildSectionOrder<ELF32BE>();
237   case ELF64LEKind:
238     return buildSectionOrder<ELF64LE>();
239   case ELF64BEKind:
240     return buildSectionOrder<ELF64BE>();
241   default:
242     llvm_unreachable("unknown ELF type");
243   }
244 }
245 
246 static void sortBySymbolOrder(InputSection **Begin, InputSection **End) {
247   if (Config->SymbolOrderingFile.empty())
248     return;
249   static llvm::DenseMap<SectionBase *, int> Order = getSectionOrder();
250   MutableArrayRef<InputSection *> In(Begin, End - Begin);
251   sortByOrder(In, [&](InputSectionBase *S) { return Order.lookup(S); });
252 }
253 
254 // Compute and remember which sections the InputSectionDescription matches.
255 std::vector<InputSection *>
256 LinkerScript::computeInputSections(const InputSectionDescription *Cmd) {
257   std::vector<InputSection *> Ret;
258 
259   // Collects all sections that satisfy constraints of Cmd.
260   for (const SectionPattern &Pat : Cmd->SectionPatterns) {
261     size_t SizeBefore = Ret.size();
262 
263     for (InputSectionBase *Sec : InputSections) {
264       if (Sec->Assigned)
265         continue;
266 
267       if (!Sec->Live) {
268         reportDiscarded(Sec);
269         continue;
270       }
271 
272       // For -emit-relocs we have to ignore entries like
273       //   .rela.dyn : { *(.rela.data) }
274       // which are common because they are in the default bfd script.
275       if (Sec->Type == SHT_REL || Sec->Type == SHT_RELA)
276         continue;
277 
278       StringRef Filename = basename(Sec);
279       if (!Cmd->FilePat.match(Filename) ||
280           Pat.ExcludedFilePat.match(Filename) ||
281           !Pat.SectionPat.match(Sec->Name))
282         continue;
283 
284       Ret.push_back(cast<InputSection>(Sec));
285       Sec->Assigned = true;
286     }
287 
288     // Sort sections as instructed by SORT-family commands and --sort-section
289     // option. Because SORT-family commands can be nested at most two depth
290     // (e.g. SORT_BY_NAME(SORT_BY_ALIGNMENT(.text.*))) and because the command
291     // line option is respected even if a SORT command is given, the exact
292     // behavior we have here is a bit complicated. Here are the rules.
293     //
294     // 1. If two SORT commands are given, --sort-section is ignored.
295     // 2. If one SORT command is given, and if it is not SORT_NONE,
296     //    --sort-section is handled as an inner SORT command.
297     // 3. If one SORT command is given, and if it is SORT_NONE, don't sort.
298     // 4. If no SORT command is given, sort according to --sort-section.
299     // 5. If no SORT commands are given and --sort-section is not specified,
300     //    apply sorting provided by --symbol-ordering-file if any exist.
301     InputSection **Begin = Ret.data() + SizeBefore;
302     InputSection **End = Ret.data() + Ret.size();
303     if (Pat.SortOuter == SortSectionPolicy::Default &&
304         Config->SortSection == SortSectionPolicy::Default) {
305       sortBySymbolOrder(Begin, End);
306       continue;
307     }
308     if (Pat.SortOuter != SortSectionPolicy::None) {
309       if (Pat.SortInner == SortSectionPolicy::Default)
310         sortSections(Begin, End, Config->SortSection);
311       else
312         sortSections(Begin, End, Pat.SortInner);
313       sortSections(Begin, End, Pat.SortOuter);
314     }
315   }
316   return Ret;
317 }
318 
319 void LinkerScript::discard(ArrayRef<InputSectionBase *> V) {
320   for (InputSectionBase *S : V) {
321     S->Live = false;
322     if (S == InX::ShStrTab || S == InX::Common || S == InX::Dynamic ||
323         S == InX::DynSymTab || S == InX::DynStrTab)
324       error("discarding " + S->Name + " section is not allowed");
325     discard(S->DependentSections);
326   }
327 }
328 
329 std::vector<InputSectionBase *>
330 LinkerScript::createInputSectionList(OutputSection &OutCmd) {
331   std::vector<InputSectionBase *> Ret;
332 
333   for (BaseCommand *Base : OutCmd.Commands) {
334     auto *Cmd = dyn_cast<InputSectionDescription>(Base);
335     if (!Cmd)
336       continue;
337 
338     Cmd->Sections = computeInputSections(Cmd);
339     Ret.insert(Ret.end(), Cmd->Sections.begin(), Cmd->Sections.end());
340   }
341 
342   return Ret;
343 }
344 
345 void LinkerScript::processCommands(OutputSectionFactory &Factory) {
346   // A symbol can be assigned before any section is mentioned in the linker
347   // script. In an DSO, the symbol values are addresses, so the only important
348   // section values are:
349   // * SHN_UNDEF
350   // * SHN_ABS
351   // * Any value meaning a regular section.
352   // To handle that, create a dummy aether section that fills the void before
353   // the linker scripts switches to another section. It has an index of one
354   // which will map to whatever the first actual section is.
355   Aether = make<OutputSection>("", 0, SHF_ALLOC);
356   Aether->SectionIndex = 1;
357   auto State = make_unique<AddressState>(Opt);
358   // CurAddressState captures the local AddressState and makes it accessible
359   // deliberately. This is needed as there are some cases where we cannot just
360   // thread the current state through to a lambda function created by the
361   // script parser.
362   CurAddressState = State.get();
363   CurAddressState->OutSec = Aether;
364   Dot = 0;
365 
366   for (size_t I = 0; I < Opt.Commands.size(); ++I) {
367     // Handle symbol assignments outside of any output section.
368     if (auto *Cmd = dyn_cast<SymbolAssignment>(Opt.Commands[I])) {
369       addSymbol(Cmd);
370       continue;
371     }
372 
373     if (auto *Sec = dyn_cast<OutputSection>(Opt.Commands[I])) {
374       std::vector<InputSectionBase *> V = createInputSectionList(*Sec);
375 
376       // The output section name `/DISCARD/' is special.
377       // Any input section assigned to it is discarded.
378       if (Sec->Name == "/DISCARD/") {
379         discard(V);
380         continue;
381       }
382 
383       // This is for ONLY_IF_RO and ONLY_IF_RW. An output section directive
384       // ".foo : ONLY_IF_R[OW] { ... }" is handled only if all member input
385       // sections satisfy a given constraint. If not, a directive is handled
386       // as if it wasn't present from the beginning.
387       //
388       // Because we'll iterate over Commands many more times, the easiest
389       // way to "make it as if it wasn't present" is to just remove it.
390       if (!matchConstraints(V, Sec->Constraint)) {
391         for (InputSectionBase *S : V)
392           S->Assigned = false;
393         Opt.Commands.erase(Opt.Commands.begin() + I);
394         --I;
395         continue;
396       }
397 
398       // A directive may contain symbol definitions like this:
399       // ".foo : { ...; bar = .; }". Handle them.
400       for (BaseCommand *Base : Sec->Commands)
401         if (auto *OutCmd = dyn_cast<SymbolAssignment>(Base))
402           addSymbol(OutCmd);
403 
404       // Handle subalign (e.g. ".foo : SUBALIGN(32) { ... }"). If subalign
405       // is given, input sections are aligned to that value, whether the
406       // given value is larger or smaller than the original section alignment.
407       if (Sec->SubalignExpr) {
408         uint32_t Subalign = Sec->SubalignExpr().getValue();
409         for (InputSectionBase *S : V)
410           S->Alignment = Subalign;
411       }
412 
413       // Add input sections to an output section.
414       for (InputSectionBase *S : V)
415         Factory.addInputSec(S, Sec->Name, Sec);
416       assert(Sec->SectionIndex == INT_MAX);
417       Sec->SectionIndex = I;
418       if (Sec->Noload)
419         Sec->Type = SHT_NOBITS;
420     }
421   }
422   CurAddressState = nullptr;
423 }
424 
425 void LinkerScript::fabricateDefaultCommands() {
426   // Define start address
427   uint64_t StartAddr = -1;
428 
429   // The Sections with -T<section> have been sorted in order of ascending
430   // address. We must lower StartAddr if the lowest -T<section address> as
431   // calls to setDot() must be monotonically increasing.
432   for (auto &KV : Config->SectionStartMap)
433     StartAddr = std::min(StartAddr, KV.second);
434 
435   Opt.Commands.insert(Opt.Commands.begin(),
436                       make<SymbolAssignment>(".",
437                                              [=] {
438                                                return std::min(
439                                                    StartAddr,
440                                                    Config->ImageBase +
441                                                        elf::getHeaderSize());
442                                              },
443                                              ""));
444 }
445 
446 // Add sections that didn't match any sections command.
447 void LinkerScript::addOrphanSections(OutputSectionFactory &Factory) {
448   unsigned NumCommands = Opt.Commands.size();
449   for (InputSectionBase *S : InputSections) {
450     if (!S->Live || S->Parent)
451       continue;
452     StringRef Name = getOutputSectionName(S->Name);
453     auto End = Opt.Commands.begin() + NumCommands;
454     auto I = std::find_if(Opt.Commands.begin(), End, [&](BaseCommand *Base) {
455       if (auto *Sec = dyn_cast<OutputSection>(Base))
456         return Sec->Name == Name;
457       return false;
458     });
459     if (I == End) {
460       Factory.addInputSec(S, Name);
461       assert(S->getOutputSection()->SectionIndex == INT_MAX);
462     } else {
463       OutputSection *Sec = cast<OutputSection>(*I);
464       Factory.addInputSec(S, Name, Sec);
465       unsigned Index = std::distance(Opt.Commands.begin(), I);
466       assert(Sec->SectionIndex == INT_MAX || Sec->SectionIndex == Index);
467       Sec->SectionIndex = Index;
468     }
469   }
470 }
471 
472 uint64_t LinkerScript::advance(uint64_t Size, unsigned Align) {
473   bool IsTbss = (CurAddressState->OutSec->Flags & SHF_TLS) &&
474                 CurAddressState->OutSec->Type == SHT_NOBITS;
475   uint64_t Start = IsTbss ? Dot + CurAddressState->ThreadBssOffset : Dot;
476   Start = alignTo(Start, Align);
477   uint64_t End = Start + Size;
478 
479   if (IsTbss)
480     CurAddressState->ThreadBssOffset = End - Dot;
481   else
482     Dot = End;
483   return End;
484 }
485 
486 void LinkerScript::output(InputSection *S) {
487   uint64_t Before = advance(0, 1);
488   uint64_t Pos = advance(S->getSize(), S->Alignment);
489   S->OutSecOff = Pos - S->getSize() - CurAddressState->OutSec->Addr;
490 
491   // Update output section size after adding each section. This is so that
492   // SIZEOF works correctly in the case below:
493   // .foo { *(.aaa) a = SIZEOF(.foo); *(.bbb) }
494   CurAddressState->OutSec->Size = Pos - CurAddressState->OutSec->Addr;
495 
496   // If there is a memory region associated with this input section, then
497   // place the section in that region and update the region index.
498   if (CurAddressState->MemRegion) {
499     uint64_t &CurOffset =
500         CurAddressState->MemRegionOffset[CurAddressState->MemRegion];
501     CurOffset += Pos - Before;
502     uint64_t CurSize = CurOffset - CurAddressState->MemRegion->Origin;
503     if (CurSize > CurAddressState->MemRegion->Length) {
504       uint64_t OverflowAmt = CurSize - CurAddressState->MemRegion->Length;
505       error("section '" + CurAddressState->OutSec->Name +
506             "' will not fit in region '" + CurAddressState->MemRegion->Name +
507             "': overflowed by " + Twine(OverflowAmt) + " bytes");
508     }
509   }
510 }
511 
512 void LinkerScript::switchTo(OutputSection *Sec) {
513   if (CurAddressState->OutSec == Sec)
514     return;
515 
516   CurAddressState->OutSec = Sec;
517   CurAddressState->OutSec->Addr =
518       advance(0, CurAddressState->OutSec->Alignment);
519 
520   // If neither AT nor AT> is specified for an allocatable section, the linker
521   // will set the LMA such that the difference between VMA and LMA for the
522   // section is the same as the preceding output section in the same region
523   // https://sourceware.org/binutils/docs-2.20/ld/Output-Section-LMA.html
524   if (CurAddressState->LMAOffset)
525     CurAddressState->OutSec->LMAOffset = CurAddressState->LMAOffset();
526 }
527 
528 void LinkerScript::process(BaseCommand &Base) {
529   // This handles the assignments to symbol or to the dot.
530   if (auto *Cmd = dyn_cast<SymbolAssignment>(&Base)) {
531     assignSymbol(Cmd, true);
532     return;
533   }
534 
535   // Handle BYTE(), SHORT(), LONG(), or QUAD().
536   if (auto *Cmd = dyn_cast<BytesDataCommand>(&Base)) {
537     Cmd->Offset = Dot - CurAddressState->OutSec->Addr;
538     Dot += Cmd->Size;
539     CurAddressState->OutSec->Size = Dot - CurAddressState->OutSec->Addr;
540     return;
541   }
542 
543   // Handle ASSERT().
544   if (auto *Cmd = dyn_cast<AssertCommand>(&Base)) {
545     Cmd->Expression();
546     return;
547   }
548 
549   // Handle a single input section description command.
550   // It calculates and assigns the offsets for each section and also
551   // updates the output section size.
552   auto &Cmd = cast<InputSectionDescription>(Base);
553   for (InputSection *Sec : Cmd.Sections) {
554     // We tentatively added all synthetic sections at the beginning and removed
555     // empty ones afterwards (because there is no way to know whether they were
556     // going be empty or not other than actually running linker scripts.)
557     // We need to ignore remains of empty sections.
558     if (auto *S = dyn_cast<SyntheticSection>(Sec))
559       if (S->empty())
560         continue;
561 
562     if (!Sec->Live)
563       continue;
564     assert(CurAddressState->OutSec == Sec->getParent());
565     output(Sec);
566   }
567 }
568 
569 // This function searches for a memory region to place the given output
570 // section in. If found, a pointer to the appropriate memory region is
571 // returned. Otherwise, a nullptr is returned.
572 MemoryRegion *LinkerScript::findMemoryRegion(OutputSection *Sec) {
573   // If a memory region name was specified in the output section command,
574   // then try to find that region first.
575   if (!Sec->MemoryRegionName.empty()) {
576     auto It = Opt.MemoryRegions.find(Sec->MemoryRegionName);
577     if (It != Opt.MemoryRegions.end())
578       return &It->second;
579     error("memory region '" + Sec->MemoryRegionName + "' not declared");
580     return nullptr;
581   }
582 
583   // If at least one memory region is defined, all sections must
584   // belong to some memory region. Otherwise, we don't need to do
585   // anything for memory regions.
586   if (Opt.MemoryRegions.empty())
587     return nullptr;
588 
589   // See if a region can be found by matching section flags.
590   for (auto &Pair : Opt.MemoryRegions) {
591     MemoryRegion &M = Pair.second;
592     if ((M.Flags & Sec->Flags) && (M.NegFlags & Sec->Flags) == 0)
593       return &M;
594   }
595 
596   // Otherwise, no suitable region was found.
597   if (Sec->Flags & SHF_ALLOC)
598     error("no memory region specified for section '" + Sec->Name + "'");
599   return nullptr;
600 }
601 
602 // This function assigns offsets to input sections and an output section
603 // for a single sections command (e.g. ".text { *(.text); }").
604 void LinkerScript::assignOffsets(OutputSection *Sec) {
605   if (!(Sec->Flags & SHF_ALLOC))
606     Dot = 0;
607   else if (Sec->AddrExpr)
608     setDot(Sec->AddrExpr, Sec->Location, false);
609 
610   if (Sec->LMAExpr) {
611     uint64_t D = Dot;
612     CurAddressState->LMAOffset = [=] { return Sec->LMAExpr().getValue() - D; };
613   }
614 
615   CurAddressState->MemRegion = Sec->MemRegion;
616   if (CurAddressState->MemRegion)
617     Dot = CurAddressState->MemRegionOffset[CurAddressState->MemRegion];
618   switchTo(Sec);
619 
620   // We do not support custom layout for compressed debug sectons.
621   // At this point we already know their size and have compressed content.
622   if (CurAddressState->OutSec->Flags & SHF_COMPRESSED)
623     return;
624 
625   for (BaseCommand *C : Sec->Commands)
626     process(*C);
627 }
628 
629 void LinkerScript::removeEmptyCommands() {
630   // It is common practice to use very generic linker scripts. So for any
631   // given run some of the output sections in the script will be empty.
632   // We could create corresponding empty output sections, but that would
633   // clutter the output.
634   // We instead remove trivially empty sections. The bfd linker seems even
635   // more aggressive at removing them.
636   auto Pos = std::remove_if(Opt.Commands.begin(), Opt.Commands.end(),
637                             [&](BaseCommand *Base) {
638                               if (auto *Sec = dyn_cast<OutputSection>(Base))
639                                 return !Sec->Live;
640                               return false;
641                             });
642   Opt.Commands.erase(Pos, Opt.Commands.end());
643 }
644 
645 static bool isAllSectionDescription(const OutputSection &Cmd) {
646   for (BaseCommand *Base : Cmd.Commands)
647     if (!isa<InputSectionDescription>(*Base))
648       return false;
649   return true;
650 }
651 
652 void LinkerScript::adjustSectionsBeforeSorting() {
653   // If the output section contains only symbol assignments, create a
654   // corresponding output section. The bfd linker seems to only create them if
655   // '.' is assigned to, but creating these section should not have any bad
656   // consequeces and gives us a section to put the symbol in.
657   uint64_t Flags = SHF_ALLOC;
658 
659   for (int I = 0, E = Opt.Commands.size(); I != E; ++I) {
660     auto *Sec = dyn_cast<OutputSection>(Opt.Commands[I]);
661     if (!Sec)
662       continue;
663     if (Sec->Live) {
664       Flags = Sec->Flags;
665       continue;
666     }
667 
668     if (isAllSectionDescription(*Sec))
669       continue;
670 
671     Sec->Live = true;
672     Sec->SectionIndex = I;
673     Sec->Flags = Flags;
674   }
675 }
676 
677 void LinkerScript::adjustSectionsAfterSorting() {
678   // Try and find an appropriate memory region to assign offsets in.
679   for (BaseCommand *Base : Opt.Commands) {
680     if (auto *Sec = dyn_cast<OutputSection>(Base)) {
681       Sec->MemRegion = findMemoryRegion(Sec);
682       // Handle align (e.g. ".foo : ALIGN(16) { ... }").
683       if (Sec->AlignExpr)
684         Sec->updateAlignment(Sec->AlignExpr().getValue());
685     }
686   }
687 
688   // If output section command doesn't specify any segments,
689   // and we haven't previously assigned any section to segment,
690   // then we simply assign section to the very first load segment.
691   // Below is an example of such linker script:
692   // PHDRS { seg PT_LOAD; }
693   // SECTIONS { .aaa : { *(.aaa) } }
694   std::vector<StringRef> DefPhdrs;
695   auto FirstPtLoad =
696       std::find_if(Opt.PhdrsCommands.begin(), Opt.PhdrsCommands.end(),
697                    [](const PhdrsCommand &Cmd) { return Cmd.Type == PT_LOAD; });
698   if (FirstPtLoad != Opt.PhdrsCommands.end())
699     DefPhdrs.push_back(FirstPtLoad->Name);
700 
701   // Walk the commands and propagate the program headers to commands that don't
702   // explicitly specify them.
703   for (BaseCommand *Base : Opt.Commands) {
704     auto *Sec = dyn_cast<OutputSection>(Base);
705     if (!Sec)
706       continue;
707 
708     if (Sec->Phdrs.empty()) {
709       // To match the bfd linker script behaviour, only propagate program
710       // headers to sections that are allocated.
711       if (Sec->Flags & SHF_ALLOC)
712         Sec->Phdrs = DefPhdrs;
713     } else {
714       DefPhdrs = Sec->Phdrs;
715     }
716   }
717 
718   removeEmptyCommands();
719 }
720 
721 void LinkerScript::allocateHeaders(std::vector<PhdrEntry *> &Phdrs) {
722   uint64_t Min = std::numeric_limits<uint64_t>::max();
723   for (OutputSection *Sec : OutputSections)
724     if (Sec->Flags & SHF_ALLOC)
725       Min = std::min<uint64_t>(Min, Sec->Addr);
726 
727   auto It = llvm::find_if(
728       Phdrs, [](const PhdrEntry *E) { return E->p_type == PT_LOAD; });
729   if (It == Phdrs.end())
730     return;
731   PhdrEntry *FirstPTLoad = *It;
732 
733   uint64_t HeaderSize = getHeaderSize();
734   if (HeaderSize <= Min || Script->hasPhdrsCommands()) {
735     Min = alignDown(Min - HeaderSize, Config->MaxPageSize);
736     Out::ElfHeader->Addr = Min;
737     Out::ProgramHeaders->Addr = Min + Out::ElfHeader->Size;
738     return;
739   }
740 
741   assert(FirstPTLoad->First == Out::ElfHeader);
742   OutputSection *ActualFirst = nullptr;
743   for (OutputSection *Sec : OutputSections) {
744     if (Sec->FirstInPtLoad == Out::ElfHeader) {
745       ActualFirst = Sec;
746       break;
747     }
748   }
749   if (ActualFirst) {
750     for (OutputSection *Sec : OutputSections)
751       if (Sec->FirstInPtLoad == Out::ElfHeader)
752         Sec->FirstInPtLoad = ActualFirst;
753     FirstPTLoad->First = ActualFirst;
754   } else {
755     Phdrs.erase(It);
756   }
757 
758   auto PhdrI = llvm::find_if(
759       Phdrs, [](const PhdrEntry *E) { return E->p_type == PT_PHDR; });
760   if (PhdrI != Phdrs.end())
761     Phdrs.erase(PhdrI);
762 }
763 
764 LinkerScript::AddressState::AddressState(const ScriptConfiguration &Opt) {
765   for (auto &MRI : Opt.MemoryRegions) {
766     const MemoryRegion *MR = &MRI.second;
767     MemRegionOffset[MR] = MR->Origin;
768   }
769 }
770 
771 void LinkerScript::assignAddresses() {
772   // Assign addresses as instructed by linker script SECTIONS sub-commands.
773   Dot = 0;
774   auto State = make_unique<AddressState>(Opt);
775   // CurAddressState captures the local AddressState and makes it accessible
776   // deliberately. This is needed as there are some cases where we cannot just
777   // thread the current state through to a lambda function created by the
778   // script parser.
779   CurAddressState = State.get();
780   ErrorOnMissingSection = true;
781   switchTo(Aether);
782 
783   for (BaseCommand *Base : Opt.Commands) {
784     if (auto *Cmd = dyn_cast<SymbolAssignment>(Base)) {
785       assignSymbol(Cmd, false);
786       continue;
787     }
788 
789     if (auto *Cmd = dyn_cast<AssertCommand>(Base)) {
790       Cmd->Expression();
791       continue;
792     }
793 
794     assignOffsets(cast<OutputSection>(Base));
795   }
796   CurAddressState = nullptr;
797 }
798 
799 // Creates program headers as instructed by PHDRS linker script command.
800 std::vector<PhdrEntry *> LinkerScript::createPhdrs() {
801   std::vector<PhdrEntry *> Ret;
802 
803   // Process PHDRS and FILEHDR keywords because they are not
804   // real output sections and cannot be added in the following loop.
805   for (const PhdrsCommand &Cmd : Opt.PhdrsCommands) {
806     PhdrEntry *Phdr =
807         make<PhdrEntry>(Cmd.Type, Cmd.Flags == UINT_MAX ? PF_R : Cmd.Flags);
808 
809     if (Cmd.HasFilehdr)
810       Phdr->add(Out::ElfHeader);
811     if (Cmd.HasPhdrs)
812       Phdr->add(Out::ProgramHeaders);
813 
814     if (Cmd.LMAExpr) {
815       Phdr->p_paddr = Cmd.LMAExpr().getValue();
816       Phdr->HasLMA = true;
817     }
818     Ret.push_back(Phdr);
819   }
820 
821   // Add output sections to program headers.
822   for (OutputSection *Sec : OutputSections) {
823     // Assign headers specified by linker script
824     for (size_t Id : getPhdrIndices(Sec)) {
825       Ret[Id]->add(Sec);
826       if (Opt.PhdrsCommands[Id].Flags == UINT_MAX)
827         Ret[Id]->p_flags |= Sec->getPhdrFlags();
828     }
829   }
830   return Ret;
831 }
832 
833 bool LinkerScript::ignoreInterpSection() {
834   // Ignore .interp section in case we have PHDRS specification
835   // and PT_INTERP isn't listed.
836   if (Opt.PhdrsCommands.empty())
837     return false;
838   for (PhdrsCommand &Cmd : Opt.PhdrsCommands)
839     if (Cmd.Type == PT_INTERP)
840       return false;
841   return true;
842 }
843 
844 ExprValue LinkerScript::getSymbolValue(const Twine &Loc, StringRef S) {
845   if (S == ".")
846     return {CurAddressState->OutSec, Dot - CurAddressState->OutSec->Addr, Loc};
847   if (SymbolBody *B = Symtab->find(S)) {
848     if (auto *D = dyn_cast<DefinedRegular>(B))
849       return {D->Section, D->Value, Loc};
850     if (auto *C = dyn_cast<DefinedCommon>(B))
851       return {InX::Common, C->Offset, Loc};
852   }
853   error(Loc + ": symbol not found: " + S);
854   return 0;
855 }
856 
857 bool LinkerScript::isDefined(StringRef S) { return Symtab->find(S) != nullptr; }
858 
859 static const size_t NoPhdr = -1;
860 
861 // Returns indices of ELF headers containing specific section. Each index is a
862 // zero based number of ELF header listed within PHDRS {} script block.
863 std::vector<size_t> LinkerScript::getPhdrIndices(OutputSection *Cmd) {
864   std::vector<size_t> Ret;
865   for (StringRef PhdrName : Cmd->Phdrs) {
866     size_t Index = getPhdrIndex(Cmd->Location, PhdrName);
867     if (Index != NoPhdr)
868       Ret.push_back(Index);
869   }
870   return Ret;
871 }
872 
873 // Returns the index of the segment named PhdrName if found otherwise
874 // NoPhdr. When not found, if PhdrName is not the special case value 'NONE'
875 // (which can be used to explicitly specify that a section isn't assigned to a
876 // segment) then error.
877 size_t LinkerScript::getPhdrIndex(const Twine &Loc, StringRef PhdrName) {
878   size_t I = 0;
879   for (PhdrsCommand &Cmd : Opt.PhdrsCommands) {
880     if (Cmd.Name == PhdrName)
881       return I;
882     ++I;
883   }
884   if (PhdrName != "NONE")
885     error(Loc + ": section header '" + PhdrName + "' is not listed in PHDRS");
886   return NoPhdr;
887 }
888