1 //===- LinkerScript.cpp ---------------------------------------------------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 // This file contains the parser/evaluator of the linker script.
10 //
11 //===----------------------------------------------------------------------===//
12 
13 #include "LinkerScript.h"
14 #include "Config.h"
15 #include "InputFiles.h"
16 #include "InputSection.h"
17 #include "OutputSections.h"
18 #include "SymbolTable.h"
19 #include "Symbols.h"
20 #include "SyntheticSections.h"
21 #include "Target.h"
22 #include "Writer.h"
23 #include "lld/Common/CommonLinkerContext.h"
24 #include "lld/Common/Strings.h"
25 #include "llvm/ADT/STLExtras.h"
26 #include "llvm/ADT/StringRef.h"
27 #include "llvm/BinaryFormat/ELF.h"
28 #include "llvm/Support/Casting.h"
29 #include "llvm/Support/Endian.h"
30 #include "llvm/Support/ErrorHandling.h"
31 #include "llvm/Support/TimeProfiler.h"
32 #include <algorithm>
33 #include <cassert>
34 #include <cstddef>
35 #include <cstdint>
36 #include <limits>
37 #include <string>
38 #include <vector>
39 
40 using namespace llvm;
41 using namespace llvm::ELF;
42 using namespace llvm::object;
43 using namespace llvm::support::endian;
44 using namespace lld;
45 using namespace lld::elf;
46 
47 std::unique_ptr<LinkerScript> elf::script;
48 
49 static bool isSectionPrefix(StringRef prefix, StringRef name) {
50   return name.consume_front(prefix) && (name.empty() || name[0] == '.');
51 }
52 
53 static StringRef getOutputSectionName(const InputSectionBase *s) {
54   if (config->relocatable)
55     return s->name;
56 
57   // This is for --emit-relocs. If .text.foo is emitted as .text.bar, we want
58   // to emit .rela.text.foo as .rela.text.bar for consistency (this is not
59   // technically required, but not doing it is odd). This code guarantees that.
60   if (auto *isec = dyn_cast<InputSection>(s)) {
61     if (InputSectionBase *rel = isec->getRelocatedSection()) {
62       OutputSection *out = rel->getOutputSection();
63       if (s->type == SHT_RELA)
64         return saver().save(".rela" + out->name);
65       return saver().save(".rel" + out->name);
66     }
67   }
68 
69   // A BssSection created for a common symbol is identified as "COMMON" in
70   // linker scripts. It should go to .bss section.
71   if (s->name == "COMMON")
72     return ".bss";
73 
74   if (script->hasSectionsCommand)
75     return s->name;
76 
77   // When no SECTIONS is specified, emulate GNU ld's internal linker scripts
78   // by grouping sections with certain prefixes.
79 
80   // GNU ld places text sections with prefix ".text.hot.", ".text.unknown.",
81   // ".text.unlikely.", ".text.startup." or ".text.exit." before others.
82   // We provide an option -z keep-text-section-prefix to group such sections
83   // into separate output sections. This is more flexible. See also
84   // sortISDBySectionOrder().
85   // ".text.unknown" means the hotness of the section is unknown. When
86   // SampleFDO is used, if a function doesn't have sample, it could be very
87   // cold or it could be a new function never being sampled. Those functions
88   // will be kept in the ".text.unknown" section.
89   // ".text.split." holds symbols which are split out from functions in other
90   // input sections. For example, with -fsplit-machine-functions, placing the
91   // cold parts in .text.split instead of .text.unlikely mitigates against poor
92   // profile inaccuracy. Techniques such as hugepage remapping can make
93   // conservative decisions at the section granularity.
94   if (isSectionPrefix(".text", s->name)) {
95     if (config->zKeepTextSectionPrefix)
96       for (StringRef v : {".text.hot", ".text.unknown", ".text.unlikely",
97                           ".text.startup", ".text.exit", ".text.split"})
98         if (isSectionPrefix(v.substr(5), s->name.substr(5)))
99           return v;
100     return ".text";
101   }
102 
103   for (StringRef v :
104        {".data.rel.ro", ".data", ".rodata", ".bss.rel.ro", ".bss",
105         ".gcc_except_table", ".init_array", ".fini_array", ".tbss", ".tdata",
106         ".ARM.exidx", ".ARM.extab", ".ctors", ".dtors"})
107     if (isSectionPrefix(v, s->name))
108       return v;
109 
110   return s->name;
111 }
112 
113 uint64_t ExprValue::getValue() const {
114   if (sec)
115     return alignTo(sec->getOutputSection()->addr + sec->getOffset(val),
116                    alignment);
117   return alignTo(val, alignment);
118 }
119 
120 uint64_t ExprValue::getSecAddr() const {
121   return sec ? sec->getOutputSection()->addr + sec->getOffset(0) : 0;
122 }
123 
124 uint64_t ExprValue::getSectionOffset() const {
125   // If the alignment is trivial, we don't have to compute the full
126   // value to know the offset. This allows this function to succeed in
127   // cases where the output section is not yet known.
128   if (alignment == 1 && !sec)
129     return val;
130   return getValue() - getSecAddr();
131 }
132 
133 OutputSection *LinkerScript::createOutputSection(StringRef name,
134                                                  StringRef location) {
135   OutputSection *&secRef = nameToOutputSection[CachedHashStringRef(name)];
136   OutputSection *sec;
137   if (secRef && secRef->location.empty()) {
138     // There was a forward reference.
139     sec = secRef;
140   } else {
141     sec = make<OutputSection>(name, SHT_PROGBITS, 0);
142     if (!secRef)
143       secRef = sec;
144   }
145   sec->location = std::string(location);
146   return sec;
147 }
148 
149 OutputSection *LinkerScript::getOrCreateOutputSection(StringRef name) {
150   OutputSection *&cmdRef = nameToOutputSection[CachedHashStringRef(name)];
151   if (!cmdRef)
152     cmdRef = make<OutputSection>(name, SHT_PROGBITS, 0);
153   return cmdRef;
154 }
155 
156 // Expands the memory region by the specified size.
157 static void expandMemoryRegion(MemoryRegion *memRegion, uint64_t size,
158                                StringRef secName) {
159   memRegion->curPos += size;
160   uint64_t newSize = memRegion->curPos - (memRegion->origin)().getValue();
161   uint64_t length = (memRegion->length)().getValue();
162   if (newSize > length)
163     error("section '" + secName + "' will not fit in region '" +
164           memRegion->name + "': overflowed by " + Twine(newSize - length) +
165           " bytes");
166 }
167 
168 void LinkerScript::expandMemoryRegions(uint64_t size) {
169   if (ctx->memRegion)
170     expandMemoryRegion(ctx->memRegion, size, ctx->outSec->name);
171   // Only expand the LMARegion if it is different from memRegion.
172   if (ctx->lmaRegion && ctx->memRegion != ctx->lmaRegion)
173     expandMemoryRegion(ctx->lmaRegion, size, ctx->outSec->name);
174 }
175 
176 void LinkerScript::expandOutputSection(uint64_t size) {
177   ctx->outSec->size += size;
178   expandMemoryRegions(size);
179 }
180 
181 void LinkerScript::setDot(Expr e, const Twine &loc, bool inSec) {
182   uint64_t val = e().getValue();
183   if (val < dot && inSec)
184     error(loc + ": unable to move location counter backward for: " +
185           ctx->outSec->name);
186 
187   // Update to location counter means update to section size.
188   if (inSec)
189     expandOutputSection(val - dot);
190 
191   dot = val;
192 }
193 
194 // Used for handling linker symbol assignments, for both finalizing
195 // their values and doing early declarations. Returns true if symbol
196 // should be defined from linker script.
197 static bool shouldDefineSym(SymbolAssignment *cmd) {
198   if (cmd->name == ".")
199     return false;
200 
201   if (!cmd->provide)
202     return true;
203 
204   // If a symbol was in PROVIDE(), we need to define it only
205   // when it is a referenced undefined symbol.
206   Symbol *b = symtab->find(cmd->name);
207   if (b && !b->isDefined() && !b->isCommon())
208     return true;
209   return false;
210 }
211 
212 // Called by processSymbolAssignments() to assign definitions to
213 // linker-script-defined symbols.
214 void LinkerScript::addSymbol(SymbolAssignment *cmd) {
215   if (!shouldDefineSym(cmd))
216     return;
217 
218   // Define a symbol.
219   ExprValue value = cmd->expression();
220   SectionBase *sec = value.isAbsolute() ? nullptr : value.sec;
221   uint8_t visibility = cmd->hidden ? STV_HIDDEN : STV_DEFAULT;
222 
223   // When this function is called, section addresses have not been
224   // fixed yet. So, we may or may not know the value of the RHS
225   // expression.
226   //
227   // For example, if an expression is `x = 42`, we know x is always 42.
228   // However, if an expression is `x = .`, there's no way to know its
229   // value at the moment.
230   //
231   // We want to set symbol values early if we can. This allows us to
232   // use symbols as variables in linker scripts. Doing so allows us to
233   // write expressions like this: `alignment = 16; . = ALIGN(., alignment)`.
234   uint64_t symValue = value.sec ? 0 : value.getValue();
235 
236   Defined newSym(nullptr, cmd->name, STB_GLOBAL, visibility, value.type,
237                  symValue, 0, sec);
238 
239   Symbol *sym = symtab->insert(cmd->name);
240   sym->mergeProperties(newSym);
241   sym->replace(newSym);
242   sym->isUsedInRegularObj = true;
243   cmd->sym = cast<Defined>(sym);
244 }
245 
246 // This function is called from LinkerScript::declareSymbols.
247 // It creates a placeholder symbol if needed.
248 static void declareSymbol(SymbolAssignment *cmd) {
249   if (!shouldDefineSym(cmd))
250     return;
251 
252   uint8_t visibility = cmd->hidden ? STV_HIDDEN : STV_DEFAULT;
253   Defined newSym(nullptr, cmd->name, STB_GLOBAL, visibility, STT_NOTYPE, 0, 0,
254                  nullptr);
255 
256   // We can't calculate final value right now.
257   Symbol *sym = symtab->insert(cmd->name);
258   sym->mergeProperties(newSym);
259   sym->replace(newSym);
260 
261   cmd->sym = cast<Defined>(sym);
262   cmd->provide = false;
263   sym->isUsedInRegularObj = true;
264   sym->scriptDefined = true;
265 }
266 
267 using SymbolAssignmentMap =
268     DenseMap<const Defined *, std::pair<SectionBase *, uint64_t>>;
269 
270 // Collect section/value pairs of linker-script-defined symbols. This is used to
271 // check whether symbol values converge.
272 static SymbolAssignmentMap
273 getSymbolAssignmentValues(ArrayRef<SectionCommand *> sectionCommands) {
274   SymbolAssignmentMap ret;
275   for (SectionCommand *cmd : sectionCommands) {
276     if (auto *assign = dyn_cast<SymbolAssignment>(cmd)) {
277       if (assign->sym) // sym is nullptr for dot.
278         ret.try_emplace(assign->sym, std::make_pair(assign->sym->section,
279                                                     assign->sym->value));
280       continue;
281     }
282     for (SectionCommand *subCmd : cast<OutputSection>(cmd)->commands)
283       if (auto *assign = dyn_cast<SymbolAssignment>(subCmd))
284         if (assign->sym)
285           ret.try_emplace(assign->sym, std::make_pair(assign->sym->section,
286                                                       assign->sym->value));
287   }
288   return ret;
289 }
290 
291 // Returns the lexicographical smallest (for determinism) Defined whose
292 // section/value has changed.
293 static const Defined *
294 getChangedSymbolAssignment(const SymbolAssignmentMap &oldValues) {
295   const Defined *changed = nullptr;
296   for (auto &it : oldValues) {
297     const Defined *sym = it.first;
298     if (std::make_pair(sym->section, sym->value) != it.second &&
299         (!changed || sym->getName() < changed->getName()))
300       changed = sym;
301   }
302   return changed;
303 }
304 
305 // Process INSERT [AFTER|BEFORE] commands. For each command, we move the
306 // specified output section to the designated place.
307 void LinkerScript::processInsertCommands() {
308   SmallVector<OutputSection *, 0> moves;
309   for (const InsertCommand &cmd : insertCommands) {
310     for (StringRef name : cmd.names) {
311       // If base is empty, it may have been discarded by
312       // adjustOutputSections(). We do not handle such output sections.
313       auto from = llvm::find_if(sectionCommands, [&](SectionCommand *subCmd) {
314         return isa<OutputSection>(subCmd) &&
315                cast<OutputSection>(subCmd)->name == name;
316       });
317       if (from == sectionCommands.end())
318         continue;
319       moves.push_back(cast<OutputSection>(*from));
320       sectionCommands.erase(from);
321     }
322 
323     auto insertPos =
324         llvm::find_if(sectionCommands, [&cmd](SectionCommand *subCmd) {
325           auto *to = dyn_cast<OutputSection>(subCmd);
326           return to != nullptr && to->name == cmd.where;
327         });
328     if (insertPos == sectionCommands.end()) {
329       error("unable to insert " + cmd.names[0] +
330             (cmd.isAfter ? " after " : " before ") + cmd.where);
331     } else {
332       if (cmd.isAfter)
333         ++insertPos;
334       sectionCommands.insert(insertPos, moves.begin(), moves.end());
335     }
336     moves.clear();
337   }
338 }
339 
340 // Symbols defined in script should not be inlined by LTO. At the same time
341 // we don't know their final values until late stages of link. Here we scan
342 // over symbol assignment commands and create placeholder symbols if needed.
343 void LinkerScript::declareSymbols() {
344   assert(!ctx);
345   for (SectionCommand *cmd : sectionCommands) {
346     if (auto *assign = dyn_cast<SymbolAssignment>(cmd)) {
347       declareSymbol(assign);
348       continue;
349     }
350 
351     // If the output section directive has constraints,
352     // we can't say for sure if it is going to be included or not.
353     // Skip such sections for now. Improve the checks if we ever
354     // need symbols from that sections to be declared early.
355     auto *sec = cast<OutputSection>(cmd);
356     if (sec->constraint != ConstraintKind::NoConstraint)
357       continue;
358     for (SectionCommand *cmd : sec->commands)
359       if (auto *assign = dyn_cast<SymbolAssignment>(cmd))
360         declareSymbol(assign);
361   }
362 }
363 
364 // This function is called from assignAddresses, while we are
365 // fixing the output section addresses. This function is supposed
366 // to set the final value for a given symbol assignment.
367 void LinkerScript::assignSymbol(SymbolAssignment *cmd, bool inSec) {
368   if (cmd->name == ".") {
369     setDot(cmd->expression, cmd->location, inSec);
370     return;
371   }
372 
373   if (!cmd->sym)
374     return;
375 
376   ExprValue v = cmd->expression();
377   if (v.isAbsolute()) {
378     cmd->sym->section = nullptr;
379     cmd->sym->value = v.getValue();
380   } else {
381     cmd->sym->section = v.sec;
382     cmd->sym->value = v.getSectionOffset();
383   }
384   cmd->sym->type = v.type;
385 }
386 
387 static inline StringRef getFilename(const InputFile *file) {
388   return file ? file->getNameForScript() : StringRef();
389 }
390 
391 bool InputSectionDescription::matchesFile(const InputFile *file) const {
392   if (filePat.isTrivialMatchAll())
393     return true;
394 
395   if (!matchesFileCache || matchesFileCache->first != file)
396     matchesFileCache.emplace(file, filePat.match(getFilename(file)));
397 
398   return matchesFileCache->second;
399 }
400 
401 bool SectionPattern::excludesFile(const InputFile *file) const {
402   if (excludedFilePat.empty())
403     return false;
404 
405   if (!excludesFileCache || excludesFileCache->first != file)
406     excludesFileCache.emplace(file, excludedFilePat.match(getFilename(file)));
407 
408   return excludesFileCache->second;
409 }
410 
411 bool LinkerScript::shouldKeep(InputSectionBase *s) {
412   for (InputSectionDescription *id : keptSections)
413     if (id->matchesFile(s->file))
414       for (SectionPattern &p : id->sectionPatterns)
415         if (p.sectionPat.match(s->name) &&
416             (s->flags & id->withFlags) == id->withFlags &&
417             (s->flags & id->withoutFlags) == 0)
418           return true;
419   return false;
420 }
421 
422 // A helper function for the SORT() command.
423 static bool matchConstraints(ArrayRef<InputSectionBase *> sections,
424                              ConstraintKind kind) {
425   if (kind == ConstraintKind::NoConstraint)
426     return true;
427 
428   bool isRW = llvm::any_of(
429       sections, [](InputSectionBase *sec) { return sec->flags & SHF_WRITE; });
430 
431   return (isRW && kind == ConstraintKind::ReadWrite) ||
432          (!isRW && kind == ConstraintKind::ReadOnly);
433 }
434 
435 static void sortSections(MutableArrayRef<InputSectionBase *> vec,
436                          SortSectionPolicy k) {
437   auto alignmentComparator = [](InputSectionBase *a, InputSectionBase *b) {
438     // ">" is not a mistake. Sections with larger alignments are placed
439     // before sections with smaller alignments in order to reduce the
440     // amount of padding necessary. This is compatible with GNU.
441     return a->alignment > b->alignment;
442   };
443   auto nameComparator = [](InputSectionBase *a, InputSectionBase *b) {
444     return a->name < b->name;
445   };
446   auto priorityComparator = [](InputSectionBase *a, InputSectionBase *b) {
447     return getPriority(a->name) < getPriority(b->name);
448   };
449 
450   switch (k) {
451   case SortSectionPolicy::Default:
452   case SortSectionPolicy::None:
453     return;
454   case SortSectionPolicy::Alignment:
455     return llvm::stable_sort(vec, alignmentComparator);
456   case SortSectionPolicy::Name:
457     return llvm::stable_sort(vec, nameComparator);
458   case SortSectionPolicy::Priority:
459     return llvm::stable_sort(vec, priorityComparator);
460   }
461 }
462 
463 // Sort sections as instructed by SORT-family commands and --sort-section
464 // option. Because SORT-family commands can be nested at most two depth
465 // (e.g. SORT_BY_NAME(SORT_BY_ALIGNMENT(.text.*))) and because the command
466 // line option is respected even if a SORT command is given, the exact
467 // behavior we have here is a bit complicated. Here are the rules.
468 //
469 // 1. If two SORT commands are given, --sort-section is ignored.
470 // 2. If one SORT command is given, and if it is not SORT_NONE,
471 //    --sort-section is handled as an inner SORT command.
472 // 3. If one SORT command is given, and if it is SORT_NONE, don't sort.
473 // 4. If no SORT command is given, sort according to --sort-section.
474 static void sortInputSections(MutableArrayRef<InputSectionBase *> vec,
475                               SortSectionPolicy outer,
476                               SortSectionPolicy inner) {
477   if (outer == SortSectionPolicy::None)
478     return;
479 
480   if (inner == SortSectionPolicy::Default)
481     sortSections(vec, config->sortSection);
482   else
483     sortSections(vec, inner);
484   sortSections(vec, outer);
485 }
486 
487 // Compute and remember which sections the InputSectionDescription matches.
488 SmallVector<InputSectionBase *, 0>
489 LinkerScript::computeInputSections(const InputSectionDescription *cmd,
490                                    ArrayRef<InputSectionBase *> sections) {
491   SmallVector<InputSectionBase *, 0> ret;
492   SmallVector<size_t, 0> indexes;
493   DenseSet<size_t> seen;
494   auto sortByPositionThenCommandLine = [&](size_t begin, size_t end) {
495     llvm::sort(MutableArrayRef<size_t>(indexes).slice(begin, end - begin));
496     for (size_t i = begin; i != end; ++i)
497       ret[i] = sections[indexes[i]];
498     sortInputSections(
499         MutableArrayRef<InputSectionBase *>(ret).slice(begin, end - begin),
500         config->sortSection, SortSectionPolicy::None);
501   };
502 
503   // Collects all sections that satisfy constraints of Cmd.
504   size_t sizeAfterPrevSort = 0;
505   for (const SectionPattern &pat : cmd->sectionPatterns) {
506     size_t sizeBeforeCurrPat = ret.size();
507 
508     for (size_t i = 0, e = sections.size(); i != e; ++i) {
509       // Skip if the section is dead or has been matched by a previous input
510       // section description or a previous pattern.
511       InputSectionBase *sec = sections[i];
512       if (!sec->isLive() || sec->parent || seen.contains(i))
513         continue;
514 
515       // For --emit-relocs we have to ignore entries like
516       //   .rela.dyn : { *(.rela.data) }
517       // which are common because they are in the default bfd script.
518       // We do not ignore SHT_REL[A] linker-synthesized sections here because
519       // want to support scripts that do custom layout for them.
520       if (isa<InputSection>(sec) &&
521           cast<InputSection>(sec)->getRelocatedSection())
522         continue;
523 
524       // Check the name early to improve performance in the common case.
525       if (!pat.sectionPat.match(sec->name))
526         continue;
527 
528       if (!cmd->matchesFile(sec->file) || pat.excludesFile(sec->file) ||
529           (sec->flags & cmd->withFlags) != cmd->withFlags ||
530           (sec->flags & cmd->withoutFlags) != 0)
531         continue;
532 
533       ret.push_back(sec);
534       indexes.push_back(i);
535       seen.insert(i);
536     }
537 
538     if (pat.sortOuter == SortSectionPolicy::Default)
539       continue;
540 
541     // Matched sections are ordered by radix sort with the keys being (SORT*,
542     // --sort-section, input order), where SORT* (if present) is most
543     // significant.
544     //
545     // Matched sections between the previous SORT* and this SORT* are sorted by
546     // (--sort-alignment, input order).
547     sortByPositionThenCommandLine(sizeAfterPrevSort, sizeBeforeCurrPat);
548     // Matched sections by this SORT* pattern are sorted using all 3 keys.
549     // ret[sizeBeforeCurrPat,ret.size()) are already in the input order, so we
550     // just sort by sortOuter and sortInner.
551     sortInputSections(
552         MutableArrayRef<InputSectionBase *>(ret).slice(sizeBeforeCurrPat),
553         pat.sortOuter, pat.sortInner);
554     sizeAfterPrevSort = ret.size();
555   }
556   // Matched sections after the last SORT* are sorted by (--sort-alignment,
557   // input order).
558   sortByPositionThenCommandLine(sizeAfterPrevSort, ret.size());
559   return ret;
560 }
561 
562 void LinkerScript::discard(InputSectionBase &s) {
563   if (&s == in.shStrTab.get())
564     error("discarding " + s.name + " section is not allowed");
565 
566   s.markDead();
567   s.parent = nullptr;
568   for (InputSection *sec : s.dependentSections)
569     discard(*sec);
570 }
571 
572 void LinkerScript::discardSynthetic(OutputSection &outCmd) {
573   for (Partition &part : partitions) {
574     if (!part.armExidx || !part.armExidx->isLive())
575       continue;
576     SmallVector<InputSectionBase *, 0> secs(
577         part.armExidx->exidxSections.begin(),
578         part.armExidx->exidxSections.end());
579     for (SectionCommand *cmd : outCmd.commands)
580       if (auto *isd = dyn_cast<InputSectionDescription>(cmd))
581         for (InputSectionBase *s : computeInputSections(isd, secs))
582           discard(*s);
583   }
584 }
585 
586 SmallVector<InputSectionBase *, 0>
587 LinkerScript::createInputSectionList(OutputSection &outCmd) {
588   SmallVector<InputSectionBase *, 0> ret;
589 
590   for (SectionCommand *cmd : outCmd.commands) {
591     if (auto *isd = dyn_cast<InputSectionDescription>(cmd)) {
592       isd->sectionBases = computeInputSections(isd, inputSections);
593       for (InputSectionBase *s : isd->sectionBases)
594         s->parent = &outCmd;
595       ret.insert(ret.end(), isd->sectionBases.begin(), isd->sectionBases.end());
596     }
597   }
598   return ret;
599 }
600 
601 // Create output sections described by SECTIONS commands.
602 void LinkerScript::processSectionCommands() {
603   auto process = [this](OutputSection *osec) {
604     SmallVector<InputSectionBase *, 0> v = createInputSectionList(*osec);
605 
606     // The output section name `/DISCARD/' is special.
607     // Any input section assigned to it is discarded.
608     if (osec->name == "/DISCARD/") {
609       for (InputSectionBase *s : v)
610         discard(*s);
611       discardSynthetic(*osec);
612       osec->commands.clear();
613       return false;
614     }
615 
616     // This is for ONLY_IF_RO and ONLY_IF_RW. An output section directive
617     // ".foo : ONLY_IF_R[OW] { ... }" is handled only if all member input
618     // sections satisfy a given constraint. If not, a directive is handled
619     // as if it wasn't present from the beginning.
620     //
621     // Because we'll iterate over SectionCommands many more times, the easy
622     // way to "make it as if it wasn't present" is to make it empty.
623     if (!matchConstraints(v, osec->constraint)) {
624       for (InputSectionBase *s : v)
625         s->parent = nullptr;
626       osec->commands.clear();
627       return false;
628     }
629 
630     // Handle subalign (e.g. ".foo : SUBALIGN(32) { ... }"). If subalign
631     // is given, input sections are aligned to that value, whether the
632     // given value is larger or smaller than the original section alignment.
633     if (osec->subalignExpr) {
634       uint32_t subalign = osec->subalignExpr().getValue();
635       for (InputSectionBase *s : v)
636         s->alignment = subalign;
637     }
638 
639     // Set the partition field the same way OutputSection::recordSection()
640     // does. Partitions cannot be used with the SECTIONS command, so this is
641     // always 1.
642     osec->partition = 1;
643     return true;
644   };
645 
646   // Process OVERWRITE_SECTIONS first so that it can overwrite the main script
647   // or orphans.
648   DenseMap<CachedHashStringRef, OutputSection *> map;
649   size_t i = 0;
650   for (OutputSection *osec : overwriteSections)
651     if (process(osec) &&
652         !map.try_emplace(CachedHashStringRef(osec->name), osec).second)
653       warn("OVERWRITE_SECTIONS specifies duplicate " + osec->name);
654   for (SectionCommand *&base : sectionCommands)
655     if (auto *osec = dyn_cast<OutputSection>(base)) {
656       if (OutputSection *overwrite =
657               map.lookup(CachedHashStringRef(osec->name))) {
658         log(overwrite->location + " overwrites " + osec->name);
659         overwrite->sectionIndex = i++;
660         base = overwrite;
661       } else if (process(osec)) {
662         osec->sectionIndex = i++;
663       }
664     }
665 
666   // If an OVERWRITE_SECTIONS specified output section is not in
667   // sectionCommands, append it to the end. The section will be inserted by
668   // orphan placement.
669   for (OutputSection *osec : overwriteSections)
670     if (osec->partition == 1 && osec->sectionIndex == UINT32_MAX)
671       sectionCommands.push_back(osec);
672 }
673 
674 void LinkerScript::processSymbolAssignments() {
675   // Dot outside an output section still represents a relative address, whose
676   // sh_shndx should not be SHN_UNDEF or SHN_ABS. Create a dummy aether section
677   // that fills the void outside a section. It has an index of one, which is
678   // indistinguishable from any other regular section index.
679   aether = make<OutputSection>("", 0, SHF_ALLOC);
680   aether->sectionIndex = 1;
681 
682   // ctx captures the local AddressState and makes it accessible deliberately.
683   // This is needed as there are some cases where we cannot just thread the
684   // current state through to a lambda function created by the script parser.
685   AddressState state;
686   ctx = &state;
687   ctx->outSec = aether;
688 
689   for (SectionCommand *cmd : sectionCommands) {
690     if (auto *assign = dyn_cast<SymbolAssignment>(cmd))
691       addSymbol(assign);
692     else
693       for (SectionCommand *subCmd : cast<OutputSection>(cmd)->commands)
694         if (auto *assign = dyn_cast<SymbolAssignment>(subCmd))
695           addSymbol(assign);
696   }
697 
698   ctx = nullptr;
699 }
700 
701 static OutputSection *findByName(ArrayRef<SectionCommand *> vec,
702                                  StringRef name) {
703   for (SectionCommand *cmd : vec)
704     if (auto *sec = dyn_cast<OutputSection>(cmd))
705       if (sec->name == name)
706         return sec;
707   return nullptr;
708 }
709 
710 static OutputSection *createSection(InputSectionBase *isec,
711                                     StringRef outsecName) {
712   OutputSection *sec = script->createOutputSection(outsecName, "<internal>");
713   sec->recordSection(isec);
714   return sec;
715 }
716 
717 static OutputSection *
718 addInputSec(StringMap<TinyPtrVector<OutputSection *>> &map,
719             InputSectionBase *isec, StringRef outsecName) {
720   // Sections with SHT_GROUP or SHF_GROUP attributes reach here only when the -r
721   // option is given. A section with SHT_GROUP defines a "section group", and
722   // its members have SHF_GROUP attribute. Usually these flags have already been
723   // stripped by InputFiles.cpp as section groups are processed and uniquified.
724   // However, for the -r option, we want to pass through all section groups
725   // as-is because adding/removing members or merging them with other groups
726   // change their semantics.
727   if (isec->type == SHT_GROUP || (isec->flags & SHF_GROUP))
728     return createSection(isec, outsecName);
729 
730   // Imagine .zed : { *(.foo) *(.bar) } script. Both foo and bar may have
731   // relocation sections .rela.foo and .rela.bar for example. Most tools do
732   // not allow multiple REL[A] sections for output section. Hence we
733   // should combine these relocation sections into single output.
734   // We skip synthetic sections because it can be .rela.dyn/.rela.plt or any
735   // other REL[A] sections created by linker itself.
736   if (!isa<SyntheticSection>(isec) &&
737       (isec->type == SHT_REL || isec->type == SHT_RELA)) {
738     auto *sec = cast<InputSection>(isec);
739     OutputSection *out = sec->getRelocatedSection()->getOutputSection();
740 
741     if (out->relocationSection) {
742       out->relocationSection->recordSection(sec);
743       return nullptr;
744     }
745 
746     out->relocationSection = createSection(isec, outsecName);
747     return out->relocationSection;
748   }
749 
750   //  The ELF spec just says
751   // ----------------------------------------------------------------
752   // In the first phase, input sections that match in name, type and
753   // attribute flags should be concatenated into single sections.
754   // ----------------------------------------------------------------
755   //
756   // However, it is clear that at least some flags have to be ignored for
757   // section merging. At the very least SHF_GROUP and SHF_COMPRESSED have to be
758   // ignored. We should not have two output .text sections just because one was
759   // in a group and another was not for example.
760   //
761   // It also seems that wording was a late addition and didn't get the
762   // necessary scrutiny.
763   //
764   // Merging sections with different flags is expected by some users. One
765   // reason is that if one file has
766   //
767   // int *const bar __attribute__((section(".foo"))) = (int *)0;
768   //
769   // gcc with -fPIC will produce a read only .foo section. But if another
770   // file has
771   //
772   // int zed;
773   // int *const bar __attribute__((section(".foo"))) = (int *)&zed;
774   //
775   // gcc with -fPIC will produce a read write section.
776   //
777   // Last but not least, when using linker script the merge rules are forced by
778   // the script. Unfortunately, linker scripts are name based. This means that
779   // expressions like *(.foo*) can refer to multiple input sections with
780   // different flags. We cannot put them in different output sections or we
781   // would produce wrong results for
782   //
783   // start = .; *(.foo.*) end = .; *(.bar)
784   //
785   // and a mapping of .foo1 and .bar1 to one section and .foo2 and .bar2 to
786   // another. The problem is that there is no way to layout those output
787   // sections such that the .foo sections are the only thing between the start
788   // and end symbols.
789   //
790   // Given the above issues, we instead merge sections by name and error on
791   // incompatible types and flags.
792   TinyPtrVector<OutputSection *> &v = map[outsecName];
793   for (OutputSection *sec : v) {
794     if (sec->partition != isec->partition)
795       continue;
796 
797     if (config->relocatable && (isec->flags & SHF_LINK_ORDER)) {
798       // Merging two SHF_LINK_ORDER sections with different sh_link fields will
799       // change their semantics, so we only merge them in -r links if they will
800       // end up being linked to the same output section. The casts are fine
801       // because everything in the map was created by the orphan placement code.
802       auto *firstIsec = cast<InputSectionBase>(
803           cast<InputSectionDescription>(sec->commands[0])->sectionBases[0]);
804       OutputSection *firstIsecOut =
805           firstIsec->flags & SHF_LINK_ORDER
806               ? firstIsec->getLinkOrderDep()->getOutputSection()
807               : nullptr;
808       if (firstIsecOut != isec->getLinkOrderDep()->getOutputSection())
809         continue;
810     }
811 
812     sec->recordSection(isec);
813     return nullptr;
814   }
815 
816   OutputSection *sec = createSection(isec, outsecName);
817   v.push_back(sec);
818   return sec;
819 }
820 
821 // Add sections that didn't match any sections command.
822 void LinkerScript::addOrphanSections() {
823   StringMap<TinyPtrVector<OutputSection *>> map;
824   SmallVector<OutputSection *, 0> v;
825 
826   auto add = [&](InputSectionBase *s) {
827     if (s->isLive() && !s->parent) {
828       orphanSections.push_back(s);
829 
830       StringRef name = getOutputSectionName(s);
831       if (config->unique) {
832         v.push_back(createSection(s, name));
833       } else if (OutputSection *sec = findByName(sectionCommands, name)) {
834         sec->recordSection(s);
835       } else {
836         if (OutputSection *os = addInputSec(map, s, name))
837           v.push_back(os);
838         assert(isa<MergeInputSection>(s) ||
839                s->getOutputSection()->sectionIndex == UINT32_MAX);
840       }
841     }
842   };
843 
844   // For further --emit-reloc handling code we need target output section
845   // to be created before we create relocation output section, so we want
846   // to create target sections first. We do not want priority handling
847   // for synthetic sections because them are special.
848   for (InputSectionBase *isec : inputSections) {
849     // In -r links, SHF_LINK_ORDER sections are added while adding their parent
850     // sections because we need to know the parent's output section before we
851     // can select an output section for the SHF_LINK_ORDER section.
852     if (config->relocatable && (isec->flags & SHF_LINK_ORDER))
853       continue;
854 
855     if (auto *sec = dyn_cast<InputSection>(isec))
856       if (InputSectionBase *rel = sec->getRelocatedSection())
857         if (auto *relIS = dyn_cast_or_null<InputSectionBase>(rel->parent))
858           add(relIS);
859     add(isec);
860     if (config->relocatable)
861       for (InputSectionBase *depSec : isec->dependentSections)
862         if (depSec->flags & SHF_LINK_ORDER)
863           add(depSec);
864   }
865 
866   // If no SECTIONS command was given, we should insert sections commands
867   // before others, so that we can handle scripts which refers them,
868   // for example: "foo = ABSOLUTE(ADDR(.text)));".
869   // When SECTIONS command is present we just add all orphans to the end.
870   if (hasSectionsCommand)
871     sectionCommands.insert(sectionCommands.end(), v.begin(), v.end());
872   else
873     sectionCommands.insert(sectionCommands.begin(), v.begin(), v.end());
874 }
875 
876 void LinkerScript::diagnoseOrphanHandling() const {
877   llvm::TimeTraceScope timeScope("Diagnose orphan sections");
878   if (config->orphanHandling == OrphanHandlingPolicy::Place)
879     return;
880   for (const InputSectionBase *sec : orphanSections) {
881     // Input SHT_REL[A] retained by --emit-relocs are ignored by
882     // computeInputSections(). Don't warn/error.
883     if (isa<InputSection>(sec) &&
884         cast<InputSection>(sec)->getRelocatedSection())
885       continue;
886 
887     StringRef name = getOutputSectionName(sec);
888     if (config->orphanHandling == OrphanHandlingPolicy::Error)
889       error(toString(sec) + " is being placed in '" + name + "'");
890     else
891       warn(toString(sec) + " is being placed in '" + name + "'");
892   }
893 }
894 
895 // This function searches for a memory region to place the given output
896 // section in. If found, a pointer to the appropriate memory region is
897 // returned in the first member of the pair. Otherwise, a nullptr is returned.
898 // The second member of the pair is a hint that should be passed to the
899 // subsequent call of this method.
900 std::pair<MemoryRegion *, MemoryRegion *>
901 LinkerScript::findMemoryRegion(OutputSection *sec, MemoryRegion *hint) {
902   // Non-allocatable sections are not part of the process image.
903   if (!(sec->flags & SHF_ALLOC)) {
904     if (!sec->memoryRegionName.empty())
905       warn("ignoring memory region assignment for non-allocatable section '" +
906            sec->name + "'");
907     return {nullptr, nullptr};
908   }
909 
910   // If a memory region name was specified in the output section command,
911   // then try to find that region first.
912   if (!sec->memoryRegionName.empty()) {
913     if (MemoryRegion *m = memoryRegions.lookup(sec->memoryRegionName))
914       return {m, m};
915     error("memory region '" + sec->memoryRegionName + "' not declared");
916     return {nullptr, nullptr};
917   }
918 
919   // If at least one memory region is defined, all sections must
920   // belong to some memory region. Otherwise, we don't need to do
921   // anything for memory regions.
922   if (memoryRegions.empty())
923     return {nullptr, nullptr};
924 
925   // An orphan section should continue the previous memory region.
926   if (sec->sectionIndex == UINT32_MAX && hint)
927     return {hint, hint};
928 
929   // See if a region can be found by matching section flags.
930   for (auto &pair : memoryRegions) {
931     MemoryRegion *m = pair.second;
932     if (m->compatibleWith(sec->flags))
933       return {m, nullptr};
934   }
935 
936   // Otherwise, no suitable region was found.
937   error("no memory region specified for section '" + sec->name + "'");
938   return {nullptr, nullptr};
939 }
940 
941 static OutputSection *findFirstSection(PhdrEntry *load) {
942   for (OutputSection *sec : outputSections)
943     if (sec->ptLoad == load)
944       return sec;
945   return nullptr;
946 }
947 
948 // This function assigns offsets to input sections and an output section
949 // for a single sections command (e.g. ".text { *(.text); }").
950 void LinkerScript::assignOffsets(OutputSection *sec) {
951   const bool isTbss = (sec->flags & SHF_TLS) && sec->type == SHT_NOBITS;
952   const bool sameMemRegion = ctx->memRegion == sec->memRegion;
953   const bool prevLMARegionIsDefault = ctx->lmaRegion == nullptr;
954   const uint64_t savedDot = dot;
955   ctx->memRegion = sec->memRegion;
956   ctx->lmaRegion = sec->lmaRegion;
957 
958   if (!(sec->flags & SHF_ALLOC)) {
959     // Non-SHF_ALLOC sections have zero addresses.
960     dot = 0;
961   } else if (isTbss) {
962     // Allow consecutive SHF_TLS SHT_NOBITS output sections. The address range
963     // starts from the end address of the previous tbss section.
964     if (ctx->tbssAddr == 0)
965       ctx->tbssAddr = dot;
966     else
967       dot = ctx->tbssAddr;
968   } else {
969     if (ctx->memRegion)
970       dot = ctx->memRegion->curPos;
971     if (sec->addrExpr)
972       setDot(sec->addrExpr, sec->location, false);
973 
974     // If the address of the section has been moved forward by an explicit
975     // expression so that it now starts past the current curPos of the enclosing
976     // region, we need to expand the current region to account for the space
977     // between the previous section, if any, and the start of this section.
978     if (ctx->memRegion && ctx->memRegion->curPos < dot)
979       expandMemoryRegion(ctx->memRegion, dot - ctx->memRegion->curPos,
980                          sec->name);
981   }
982 
983   ctx->outSec = sec;
984   if (sec->addrExpr && script->hasSectionsCommand) {
985     // The alignment is ignored.
986     sec->addr = dot;
987   } else {
988     // sec->alignment is the max of ALIGN and the maximum of input
989     // section alignments.
990     const uint64_t pos = dot;
991     dot = alignTo(dot, sec->alignment);
992     sec->addr = dot;
993     expandMemoryRegions(dot - pos);
994   }
995 
996   // ctx->lmaOffset is LMA minus VMA. If LMA is explicitly specified via AT() or
997   // AT>, recompute ctx->lmaOffset; otherwise, if both previous/current LMA
998   // region is the default, and the two sections are in the same memory region,
999   // reuse previous lmaOffset; otherwise, reset lmaOffset to 0. This emulates
1000   // heuristics described in
1001   // https://sourceware.org/binutils/docs/ld/Output-Section-LMA.html
1002   if (sec->lmaExpr) {
1003     ctx->lmaOffset = sec->lmaExpr().getValue() - dot;
1004   } else if (MemoryRegion *mr = sec->lmaRegion) {
1005     uint64_t lmaStart = alignTo(mr->curPos, sec->alignment);
1006     if (mr->curPos < lmaStart)
1007       expandMemoryRegion(mr, lmaStart - mr->curPos, sec->name);
1008     ctx->lmaOffset = lmaStart - dot;
1009   } else if (!sameMemRegion || !prevLMARegionIsDefault) {
1010     ctx->lmaOffset = 0;
1011   }
1012 
1013   // Propagate ctx->lmaOffset to the first "non-header" section.
1014   if (PhdrEntry *l = sec->ptLoad)
1015     if (sec == findFirstSection(l))
1016       l->lmaOffset = ctx->lmaOffset;
1017 
1018   // We can call this method multiple times during the creation of
1019   // thunks and want to start over calculation each time.
1020   sec->size = 0;
1021 
1022   // We visited SectionsCommands from processSectionCommands to
1023   // layout sections. Now, we visit SectionsCommands again to fix
1024   // section offsets.
1025   for (SectionCommand *cmd : sec->commands) {
1026     // This handles the assignments to symbol or to the dot.
1027     if (auto *assign = dyn_cast<SymbolAssignment>(cmd)) {
1028       assign->addr = dot;
1029       assignSymbol(assign, true);
1030       assign->size = dot - assign->addr;
1031       continue;
1032     }
1033 
1034     // Handle BYTE(), SHORT(), LONG(), or QUAD().
1035     if (auto *data = dyn_cast<ByteCommand>(cmd)) {
1036       data->offset = dot - sec->addr;
1037       dot += data->size;
1038       expandOutputSection(data->size);
1039       continue;
1040     }
1041 
1042     // Handle a single input section description command.
1043     // It calculates and assigns the offsets for each section and also
1044     // updates the output section size.
1045     for (InputSection *isec : cast<InputSectionDescription>(cmd)->sections) {
1046       assert(isec->getParent() == sec);
1047       const uint64_t pos = dot;
1048       dot = alignTo(dot, isec->alignment);
1049       isec->outSecOff = dot - sec->addr;
1050       dot += isec->getSize();
1051 
1052       // Update output section size after adding each section. This is so that
1053       // SIZEOF works correctly in the case below:
1054       // .foo { *(.aaa) a = SIZEOF(.foo); *(.bbb) }
1055       expandOutputSection(dot - pos);
1056     }
1057   }
1058 
1059   // Non-SHF_ALLOC sections do not affect the addresses of other OutputSections
1060   // as they are not part of the process image.
1061   if (!(sec->flags & SHF_ALLOC)) {
1062     dot = savedDot;
1063   } else if (isTbss) {
1064     // NOBITS TLS sections are similar. Additionally save the end address.
1065     ctx->tbssAddr = dot;
1066     dot = savedDot;
1067   }
1068 }
1069 
1070 static bool isDiscardable(const OutputSection &sec) {
1071   if (sec.name == "/DISCARD/")
1072     return true;
1073 
1074   // We do not want to remove OutputSections with expressions that reference
1075   // symbols even if the OutputSection is empty. We want to ensure that the
1076   // expressions can be evaluated and report an error if they cannot.
1077   if (sec.expressionsUseSymbols)
1078     return false;
1079 
1080   // OutputSections may be referenced by name in ADDR and LOADADDR expressions,
1081   // as an empty Section can has a valid VMA and LMA we keep the OutputSection
1082   // to maintain the integrity of the other Expression.
1083   if (sec.usedInExpression)
1084     return false;
1085 
1086   for (SectionCommand *cmd : sec.commands) {
1087     if (auto assign = dyn_cast<SymbolAssignment>(cmd))
1088       // Don't create empty output sections just for unreferenced PROVIDE
1089       // symbols.
1090       if (assign->name != "." && !assign->sym)
1091         continue;
1092 
1093     if (!isa<InputSectionDescription>(*cmd))
1094       return false;
1095   }
1096   return true;
1097 }
1098 
1099 bool LinkerScript::isDiscarded(const OutputSection *sec) const {
1100   return hasSectionsCommand && (getFirstInputSection(sec) == nullptr) &&
1101          isDiscardable(*sec);
1102 }
1103 
1104 static void maybePropagatePhdrs(OutputSection &sec,
1105                                 SmallVector<StringRef, 0> &phdrs) {
1106   if (sec.phdrs.empty()) {
1107     // To match the bfd linker script behaviour, only propagate program
1108     // headers to sections that are allocated.
1109     if (sec.flags & SHF_ALLOC)
1110       sec.phdrs = phdrs;
1111   } else {
1112     phdrs = sec.phdrs;
1113   }
1114 }
1115 
1116 void LinkerScript::adjustOutputSections() {
1117   // If the output section contains only symbol assignments, create a
1118   // corresponding output section. The issue is what to do with linker script
1119   // like ".foo : { symbol = 42; }". One option would be to convert it to
1120   // "symbol = 42;". That is, move the symbol out of the empty section
1121   // description. That seems to be what bfd does for this simple case. The
1122   // problem is that this is not completely general. bfd will give up and
1123   // create a dummy section too if there is a ". = . + 1" inside the section
1124   // for example.
1125   // Given that we want to create the section, we have to worry what impact
1126   // it will have on the link. For example, if we just create a section with
1127   // 0 for flags, it would change which PT_LOADs are created.
1128   // We could remember that particular section is dummy and ignore it in
1129   // other parts of the linker, but unfortunately there are quite a few places
1130   // that would need to change:
1131   //   * The program header creation.
1132   //   * The orphan section placement.
1133   //   * The address assignment.
1134   // The other option is to pick flags that minimize the impact the section
1135   // will have on the rest of the linker. That is why we copy the flags from
1136   // the previous sections. Only a few flags are needed to keep the impact low.
1137   uint64_t flags = SHF_ALLOC;
1138 
1139   SmallVector<StringRef, 0> defPhdrs;
1140   for (SectionCommand *&cmd : sectionCommands) {
1141     auto *sec = dyn_cast<OutputSection>(cmd);
1142     if (!sec)
1143       continue;
1144 
1145     // Handle align (e.g. ".foo : ALIGN(16) { ... }").
1146     if (sec->alignExpr)
1147       sec->alignment =
1148           std::max<uint32_t>(sec->alignment, sec->alignExpr().getValue());
1149 
1150     bool isEmpty = (getFirstInputSection(sec) == nullptr);
1151     bool discardable = isEmpty && isDiscardable(*sec);
1152     // If sec has at least one input section and not discarded, remember its
1153     // flags to be inherited by subsequent output sections. (sec may contain
1154     // just one empty synthetic section.)
1155     if (sec->hasInputSections && !discardable)
1156       flags = sec->flags;
1157 
1158     // We do not want to keep any special flags for output section
1159     // in case it is empty.
1160     if (isEmpty)
1161       sec->flags = flags & ((sec->nonAlloc ? 0 : (uint64_t)SHF_ALLOC) |
1162                             SHF_WRITE | SHF_EXECINSTR);
1163 
1164     // The code below may remove empty output sections. We should save the
1165     // specified program headers (if exist) and propagate them to subsequent
1166     // sections which do not specify program headers.
1167     // An example of such a linker script is:
1168     // SECTIONS { .empty : { *(.empty) } :rw
1169     //            .foo : { *(.foo) } }
1170     // Note: at this point the order of output sections has not been finalized,
1171     // because orphans have not been inserted into their expected positions. We
1172     // will handle them in adjustSectionsAfterSorting().
1173     if (sec->sectionIndex != UINT32_MAX)
1174       maybePropagatePhdrs(*sec, defPhdrs);
1175 
1176     if (discardable) {
1177       sec->markDead();
1178       cmd = nullptr;
1179     }
1180   }
1181 
1182   // It is common practice to use very generic linker scripts. So for any
1183   // given run some of the output sections in the script will be empty.
1184   // We could create corresponding empty output sections, but that would
1185   // clutter the output.
1186   // We instead remove trivially empty sections. The bfd linker seems even
1187   // more aggressive at removing them.
1188   llvm::erase_if(sectionCommands, [&](SectionCommand *cmd) { return !cmd; });
1189 }
1190 
1191 void LinkerScript::adjustSectionsAfterSorting() {
1192   // Try and find an appropriate memory region to assign offsets in.
1193   MemoryRegion *hint = nullptr;
1194   for (SectionCommand *cmd : sectionCommands) {
1195     if (auto *sec = dyn_cast<OutputSection>(cmd)) {
1196       if (!sec->lmaRegionName.empty()) {
1197         if (MemoryRegion *m = memoryRegions.lookup(sec->lmaRegionName))
1198           sec->lmaRegion = m;
1199         else
1200           error("memory region '" + sec->lmaRegionName + "' not declared");
1201       }
1202       std::tie(sec->memRegion, hint) = findMemoryRegion(sec, hint);
1203     }
1204   }
1205 
1206   // If output section command doesn't specify any segments,
1207   // and we haven't previously assigned any section to segment,
1208   // then we simply assign section to the very first load segment.
1209   // Below is an example of such linker script:
1210   // PHDRS { seg PT_LOAD; }
1211   // SECTIONS { .aaa : { *(.aaa) } }
1212   SmallVector<StringRef, 0> defPhdrs;
1213   auto firstPtLoad = llvm::find_if(phdrsCommands, [](const PhdrsCommand &cmd) {
1214     return cmd.type == PT_LOAD;
1215   });
1216   if (firstPtLoad != phdrsCommands.end())
1217     defPhdrs.push_back(firstPtLoad->name);
1218 
1219   // Walk the commands and propagate the program headers to commands that don't
1220   // explicitly specify them.
1221   for (SectionCommand *cmd : sectionCommands)
1222     if (auto *sec = dyn_cast<OutputSection>(cmd))
1223       maybePropagatePhdrs(*sec, defPhdrs);
1224 }
1225 
1226 static uint64_t computeBase(uint64_t min, bool allocateHeaders) {
1227   // If there is no SECTIONS or if the linkerscript is explicit about program
1228   // headers, do our best to allocate them.
1229   if (!script->hasSectionsCommand || allocateHeaders)
1230     return 0;
1231   // Otherwise only allocate program headers if that would not add a page.
1232   return alignDown(min, config->maxPageSize);
1233 }
1234 
1235 // When the SECTIONS command is used, try to find an address for the file and
1236 // program headers output sections, which can be added to the first PT_LOAD
1237 // segment when program headers are created.
1238 //
1239 // We check if the headers fit below the first allocated section. If there isn't
1240 // enough space for these sections, we'll remove them from the PT_LOAD segment,
1241 // and we'll also remove the PT_PHDR segment.
1242 void LinkerScript::allocateHeaders(SmallVector<PhdrEntry *, 0> &phdrs) {
1243   uint64_t min = std::numeric_limits<uint64_t>::max();
1244   for (OutputSection *sec : outputSections)
1245     if (sec->flags & SHF_ALLOC)
1246       min = std::min<uint64_t>(min, sec->addr);
1247 
1248   auto it = llvm::find_if(
1249       phdrs, [](const PhdrEntry *e) { return e->p_type == PT_LOAD; });
1250   if (it == phdrs.end())
1251     return;
1252   PhdrEntry *firstPTLoad = *it;
1253 
1254   bool hasExplicitHeaders =
1255       llvm::any_of(phdrsCommands, [](const PhdrsCommand &cmd) {
1256         return cmd.hasPhdrs || cmd.hasFilehdr;
1257       });
1258   bool paged = !config->omagic && !config->nmagic;
1259   uint64_t headerSize = getHeaderSize();
1260   if ((paged || hasExplicitHeaders) &&
1261       headerSize <= min - computeBase(min, hasExplicitHeaders)) {
1262     min = alignDown(min - headerSize, config->maxPageSize);
1263     Out::elfHeader->addr = min;
1264     Out::programHeaders->addr = min + Out::elfHeader->size;
1265     return;
1266   }
1267 
1268   // Error if we were explicitly asked to allocate headers.
1269   if (hasExplicitHeaders)
1270     error("could not allocate headers");
1271 
1272   Out::elfHeader->ptLoad = nullptr;
1273   Out::programHeaders->ptLoad = nullptr;
1274   firstPTLoad->firstSec = findFirstSection(firstPTLoad);
1275 
1276   llvm::erase_if(phdrs,
1277                  [](const PhdrEntry *e) { return e->p_type == PT_PHDR; });
1278 }
1279 
1280 LinkerScript::AddressState::AddressState() {
1281   for (auto &mri : script->memoryRegions) {
1282     MemoryRegion *mr = mri.second;
1283     mr->curPos = (mr->origin)().getValue();
1284   }
1285 }
1286 
1287 // Here we assign addresses as instructed by linker script SECTIONS
1288 // sub-commands. Doing that allows us to use final VA values, so here
1289 // we also handle rest commands like symbol assignments and ASSERTs.
1290 // Returns a symbol that has changed its section or value, or nullptr if no
1291 // symbol has changed.
1292 const Defined *LinkerScript::assignAddresses() {
1293   if (script->hasSectionsCommand) {
1294     // With a linker script, assignment of addresses to headers is covered by
1295     // allocateHeaders().
1296     dot = config->imageBase.getValueOr(0);
1297   } else {
1298     // Assign addresses to headers right now.
1299     dot = target->getImageBase();
1300     Out::elfHeader->addr = dot;
1301     Out::programHeaders->addr = dot + Out::elfHeader->size;
1302     dot += getHeaderSize();
1303   }
1304 
1305   AddressState state;
1306   ctx = &state;
1307   errorOnMissingSection = true;
1308   ctx->outSec = aether;
1309 
1310   SymbolAssignmentMap oldValues = getSymbolAssignmentValues(sectionCommands);
1311   for (SectionCommand *cmd : sectionCommands) {
1312     if (auto *assign = dyn_cast<SymbolAssignment>(cmd)) {
1313       assign->addr = dot;
1314       assignSymbol(assign, false);
1315       assign->size = dot - assign->addr;
1316       continue;
1317     }
1318     assignOffsets(cast<OutputSection>(cmd));
1319   }
1320 
1321   ctx = nullptr;
1322   return getChangedSymbolAssignment(oldValues);
1323 }
1324 
1325 // Creates program headers as instructed by PHDRS linker script command.
1326 SmallVector<PhdrEntry *, 0> LinkerScript::createPhdrs() {
1327   SmallVector<PhdrEntry *, 0> ret;
1328 
1329   // Process PHDRS and FILEHDR keywords because they are not
1330   // real output sections and cannot be added in the following loop.
1331   for (const PhdrsCommand &cmd : phdrsCommands) {
1332     PhdrEntry *phdr = make<PhdrEntry>(cmd.type, cmd.flags.getValueOr(PF_R));
1333 
1334     if (cmd.hasFilehdr)
1335       phdr->add(Out::elfHeader);
1336     if (cmd.hasPhdrs)
1337       phdr->add(Out::programHeaders);
1338 
1339     if (cmd.lmaExpr) {
1340       phdr->p_paddr = cmd.lmaExpr().getValue();
1341       phdr->hasLMA = true;
1342     }
1343     ret.push_back(phdr);
1344   }
1345 
1346   // Add output sections to program headers.
1347   for (OutputSection *sec : outputSections) {
1348     // Assign headers specified by linker script
1349     for (size_t id : getPhdrIndices(sec)) {
1350       ret[id]->add(sec);
1351       if (!phdrsCommands[id].flags.hasValue())
1352         ret[id]->p_flags |= sec->getPhdrFlags();
1353     }
1354   }
1355   return ret;
1356 }
1357 
1358 // Returns true if we should emit an .interp section.
1359 //
1360 // We usually do. But if PHDRS commands are given, and
1361 // no PT_INTERP is there, there's no place to emit an
1362 // .interp, so we don't do that in that case.
1363 bool LinkerScript::needsInterpSection() {
1364   if (phdrsCommands.empty())
1365     return true;
1366   for (PhdrsCommand &cmd : phdrsCommands)
1367     if (cmd.type == PT_INTERP)
1368       return true;
1369   return false;
1370 }
1371 
1372 ExprValue LinkerScript::getSymbolValue(StringRef name, const Twine &loc) {
1373   if (name == ".") {
1374     if (ctx)
1375       return {ctx->outSec, false, dot - ctx->outSec->addr, loc};
1376     error(loc + ": unable to get location counter value");
1377     return 0;
1378   }
1379 
1380   if (Symbol *sym = symtab->find(name)) {
1381     if (auto *ds = dyn_cast<Defined>(sym)) {
1382       ExprValue v{ds->section, false, ds->value, loc};
1383       // Retain the original st_type, so that the alias will get the same
1384       // behavior in relocation processing. Any operation will reset st_type to
1385       // STT_NOTYPE.
1386       v.type = ds->type;
1387       return v;
1388     }
1389     if (isa<SharedSymbol>(sym))
1390       if (!errorOnMissingSection)
1391         return {nullptr, false, 0, loc};
1392   }
1393 
1394   error(loc + ": symbol not found: " + name);
1395   return 0;
1396 }
1397 
1398 // Returns the index of the segment named Name.
1399 static Optional<size_t> getPhdrIndex(ArrayRef<PhdrsCommand> vec,
1400                                      StringRef name) {
1401   for (size_t i = 0; i < vec.size(); ++i)
1402     if (vec[i].name == name)
1403       return i;
1404   return None;
1405 }
1406 
1407 // Returns indices of ELF headers containing specific section. Each index is a
1408 // zero based number of ELF header listed within PHDRS {} script block.
1409 SmallVector<size_t, 0> LinkerScript::getPhdrIndices(OutputSection *cmd) {
1410   SmallVector<size_t, 0> ret;
1411 
1412   for (StringRef s : cmd->phdrs) {
1413     if (Optional<size_t> idx = getPhdrIndex(phdrsCommands, s))
1414       ret.push_back(*idx);
1415     else if (s != "NONE")
1416       error(cmd->location + ": program header '" + s +
1417             "' is not listed in PHDRS");
1418   }
1419   return ret;
1420 }
1421