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