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