1f7c5fbb1SRui Ueyama //===- LinkerScript.cpp ---------------------------------------------------===//
2f7c5fbb1SRui Ueyama //
32946cd70SChandler Carruth // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
42946cd70SChandler Carruth // See https://llvm.org/LICENSE.txt for license information.
52946cd70SChandler Carruth // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6f7c5fbb1SRui Ueyama //
7f7c5fbb1SRui Ueyama //===----------------------------------------------------------------------===//
8f7c5fbb1SRui Ueyama //
9f7c5fbb1SRui Ueyama // This file contains the parser/evaluator of the linker script.
10f7c5fbb1SRui Ueyama //
11f7c5fbb1SRui Ueyama //===----------------------------------------------------------------------===//
12f7c5fbb1SRui Ueyama 
13717677afSRui Ueyama #include "LinkerScript.h"
14f7c5fbb1SRui Ueyama #include "Config.h"
15b01430a0SFangrui Song #include "InputFiles.h"
161ebc8ed7SRui Ueyama #include "InputSection.h"
17652852c5SGeorge Rimar #include "OutputSections.h"
18f7c5fbb1SRui Ueyama #include "SymbolTable.h"
1955518e7dSRui Ueyama #include "Symbols.h"
203fb5a6dcSGeorge Rimar #include "SyntheticSections.h"
2155b169bfSRafael Espindola #include "Target.h"
22bbe38602SEugene Leviant #include "Writer.h"
2383d59e05SAlexandre Ganea #include "lld/Common/CommonLinkerContext.h"
24ee173718SRui Ueyama #include "lld/Common/Strings.h"
2522886a28SEugene Zelenko #include "llvm/ADT/STLExtras.h"
2622886a28SEugene Zelenko #include "llvm/ADT/StringRef.h"
27264b5d9eSZachary Turner #include "llvm/BinaryFormat/ELF.h"
2822886a28SEugene Zelenko #include "llvm/Support/Casting.h"
2922886a28SEugene Zelenko #include "llvm/Support/Endian.h"
3022886a28SEugene Zelenko #include "llvm/Support/ErrorHandling.h"
31439341b9SJames Henderson #include "llvm/Support/TimeProfiler.h"
3222886a28SEugene Zelenko #include <algorithm>
3322886a28SEugene Zelenko #include <cassert>
3422886a28SEugene Zelenko #include <cstddef>
3522886a28SEugene Zelenko #include <cstdint>
3622886a28SEugene Zelenko #include <limits>
3722886a28SEugene Zelenko #include <string>
3822886a28SEugene Zelenko #include <vector>
39f7c5fbb1SRui Ueyama 
40f7c5fbb1SRui Ueyama using namespace llvm;
41652852c5SGeorge Rimar using namespace llvm::ELF;
421ebc8ed7SRui Ueyama using namespace llvm::object;
43e38cbab5SGeorge Rimar using namespace llvm::support::endian;
4407837b8fSFangrui Song using namespace lld;
4507837b8fSFangrui Song using namespace lld::elf;
46f7c5fbb1SRui Ueyama 
475fc4323eSFangrui Song std::unique_ptr<LinkerScript> elf::script;
48a34da938SRui Ueyama 
isSectionPrefix(StringRef prefix,StringRef name)49648157b0SFangrui Song static bool isSectionPrefix(StringRef prefix, StringRef name) {
505816f185SFangrui Song   return name.consume_front(prefix) && (name.empty() || name[0] == '.');
51648157b0SFangrui Song }
52648157b0SFangrui Song 
getOutputSectionName(const InputSectionBase * s)53648157b0SFangrui Song static StringRef getOutputSectionName(const InputSectionBase *s) {
54648157b0SFangrui Song   if (config->relocatable)
55648157b0SFangrui Song     return s->name;
56648157b0SFangrui Song 
57648157b0SFangrui Song   // This is for --emit-relocs. If .text.foo is emitted as .text.bar, we want
58648157b0SFangrui Song   // to emit .rela.text.foo as .rela.text.bar for consistency (this is not
59648157b0SFangrui Song   // technically required, but not doing it is odd). This code guarantees that.
60648157b0SFangrui Song   if (auto *isec = dyn_cast<InputSection>(s)) {
61648157b0SFangrui Song     if (InputSectionBase *rel = isec->getRelocatedSection()) {
62648157b0SFangrui Song       OutputSection *out = rel->getOutputSection();
63648157b0SFangrui Song       if (s->type == SHT_RELA)
6483d59e05SAlexandre Ganea         return saver().save(".rela" + out->name);
6583d59e05SAlexandre Ganea       return saver().save(".rel" + out->name);
66648157b0SFangrui Song     }
67648157b0SFangrui Song   }
68648157b0SFangrui Song 
69648157b0SFangrui Song   // A BssSection created for a common symbol is identified as "COMMON" in
70648157b0SFangrui Song   // linker scripts. It should go to .bss section.
71648157b0SFangrui Song   if (s->name == "COMMON")
72648157b0SFangrui Song     return ".bss";
73648157b0SFangrui Song 
74648157b0SFangrui Song   if (script->hasSectionsCommand)
75648157b0SFangrui Song     return s->name;
76648157b0SFangrui Song 
77648157b0SFangrui Song   // When no SECTIONS is specified, emulate GNU ld's internal linker scripts
78648157b0SFangrui Song   // by grouping sections with certain prefixes.
79648157b0SFangrui Song 
80648157b0SFangrui Song   // GNU ld places text sections with prefix ".text.hot.", ".text.unknown.",
81648157b0SFangrui Song   // ".text.unlikely.", ".text.startup." or ".text.exit." before others.
82648157b0SFangrui Song   // We provide an option -z keep-text-section-prefix to group such sections
83648157b0SFangrui Song   // into separate output sections. This is more flexible. See also
84648157b0SFangrui Song   // sortISDBySectionOrder().
85648157b0SFangrui Song   // ".text.unknown" means the hotness of the section is unknown. When
86648157b0SFangrui Song   // SampleFDO is used, if a function doesn't have sample, it could be very
87648157b0SFangrui Song   // cold or it could be a new function never being sampled. Those functions
88648157b0SFangrui Song   // will be kept in the ".text.unknown" section.
89648157b0SFangrui Song   // ".text.split." holds symbols which are split out from functions in other
90648157b0SFangrui Song   // input sections. For example, with -fsplit-machine-functions, placing the
91648157b0SFangrui Song   // cold parts in .text.split instead of .text.unlikely mitigates against poor
92648157b0SFangrui Song   // profile inaccuracy. Techniques such as hugepage remapping can make
93648157b0SFangrui Song   // conservative decisions at the section granularity.
943534d26cSFangrui Song   if (isSectionPrefix(".text", s->name)) {
953534d26cSFangrui Song     if (config->zKeepTextSectionPrefix)
965816f185SFangrui Song       for (StringRef v : {".text.hot", ".text.unknown", ".text.unlikely",
975816f185SFangrui Song                           ".text.startup", ".text.exit", ".text.split"})
983534d26cSFangrui Song         if (isSectionPrefix(v.substr(5), s->name.substr(5)))
995816f185SFangrui Song           return v;
1003534d26cSFangrui Song     return ".text";
1013534d26cSFangrui Song   }
102648157b0SFangrui Song 
103648157b0SFangrui Song   for (StringRef v :
1043534d26cSFangrui Song        {".data.rel.ro", ".data", ".rodata", ".bss.rel.ro", ".bss",
1053deb82cdSFangrui Song         ".gcc_except_table", ".init_array", ".fini_array", ".tbss", ".tdata",
1063deb82cdSFangrui Song         ".ARM.exidx", ".ARM.extab", ".ctors", ".dtors"})
107648157b0SFangrui Song     if (isSectionPrefix(v, s->name))
1085816f185SFangrui Song       return v;
109648157b0SFangrui Song 
110648157b0SFangrui Song   return s->name;
111648157b0SFangrui Song }
112648157b0SFangrui Song 
getValue() const1135d0ea70aSGeorge Rimar uint64_t ExprValue::getValue() const {
1143837f427SRui Ueyama   if (sec)
115*85cfd917SFangrui Song     return alignToPowerOf2(sec->getOutputSection()->addr + sec->getOffset(val),
1163837f427SRui Ueyama                            alignment);
117*85cfd917SFangrui Song   return alignToPowerOf2(val, alignment);
11872dc195dSRafael Espindola }
11972dc195dSRafael Espindola 
getSecAddr() const1207ba5f47eSRafael Espindola uint64_t ExprValue::getSecAddr() const {
121e80a0b35SFangrui Song   return sec ? sec->getOutputSection()->addr + sec->getOffset(0) : 0;
1227ba5f47eSRafael Espindola }
1237ba5f47eSRafael Espindola 
getSectionOffset() const124a6acd23cSRafael Espindola uint64_t ExprValue::getSectionOffset() const {
1258b250344SRafael Espindola   // If the alignment is trivial, we don't have to compute the full
1268b250344SRafael Espindola   // value to know the offset. This allows this function to succeed in
1278b250344SRafael Espindola   // cases where the output section is not yet known.
1285d9f419aSFangrui Song   if (alignment == 1 && !sec)
1293837f427SRui Ueyama     return val;
130a6acd23cSRafael Espindola   return getValue() - getSecAddr();
131a6acd23cSRafael Espindola }
132a6acd23cSRafael Espindola 
createOutputSection(StringRef name,StringRef location)1336c814931SFangrui Song OutputDesc *LinkerScript::createOutputSection(StringRef name,
1343837f427SRui Ueyama                                               StringRef location) {
1356c814931SFangrui Song   OutputDesc *&secRef = nameToOutputSection[CachedHashStringRef(name)];
1366c814931SFangrui Song   OutputDesc *sec;
1376c814931SFangrui Song   if (secRef && secRef->osec.location.empty()) {
13805c4f67cSRafael Espindola     // There was a forward reference.
1393837f427SRui Ueyama     sec = secRef;
14005c4f67cSRafael Espindola   } else {
1416c814931SFangrui Song     sec = make<OutputDesc>(name, SHT_PROGBITS, 0);
1423837f427SRui Ueyama     if (!secRef)
1433837f427SRui Ueyama       secRef = sec;
14405c4f67cSRafael Espindola   }
1456c814931SFangrui Song   sec->osec.location = std::string(location);
1463837f427SRui Ueyama   return sec;
147851dc1e8SGeorge Rimar }
148851dc1e8SGeorge Rimar 
getOrCreateOutputSection(StringRef name)1496c814931SFangrui Song OutputDesc *LinkerScript::getOrCreateOutputSection(StringRef name) {
1506c814931SFangrui Song   OutputDesc *&cmdRef = nameToOutputSection[CachedHashStringRef(name)];
1513837f427SRui Ueyama   if (!cmdRef)
1526c814931SFangrui Song     cmdRef = make<OutputDesc>(name, SHT_PROGBITS, 0);
1533837f427SRui Ueyama   return cmdRef;
154d83ce1b4SGeorge Rimar }
155d83ce1b4SGeorge Rimar 
156162d436cSGeorge Rimar // Expands the memory region by the specified size.
expandMemoryRegion(MemoryRegion * memRegion,uint64_t size,StringRef secName)1573837f427SRui Ueyama static void expandMemoryRegion(MemoryRegion *memRegion, uint64_t size,
158a05b694bSIgor Kudrin                                StringRef secName) {
1593837f427SRui Ueyama   memRegion->curPos += size;
16092b5b980SFangrui Song   uint64_t newSize = memRegion->curPos - (memRegion->origin)().getValue();
16192b5b980SFangrui Song   uint64_t length = (memRegion->length)().getValue();
16292b5b980SFangrui Song   if (newSize > length)
163a05b694bSIgor Kudrin     error("section '" + secName + "' will not fit in region '" +
164a05b694bSIgor Kudrin           memRegion->name + "': overflowed by " + Twine(newSize - length) +
165a05b694bSIgor Kudrin           " bytes");
166162d436cSGeorge Rimar }
167162d436cSGeorge Rimar 
expandMemoryRegions(uint64_t size)1683837f427SRui Ueyama void LinkerScript::expandMemoryRegions(uint64_t size) {
1693837f427SRui Ueyama   if (ctx->memRegion)
170a05b694bSIgor Kudrin     expandMemoryRegion(ctx->memRegion, size, ctx->outSec->name);
17147cfe8f3SFangrui Song   // Only expand the LMARegion if it is different from memRegion.
1723837f427SRui Ueyama   if (ctx->lmaRegion && ctx->memRegion != ctx->lmaRegion)
173a05b694bSIgor Kudrin     expandMemoryRegion(ctx->lmaRegion, size, ctx->outSec->name);
174162d436cSGeorge Rimar }
175162d436cSGeorge Rimar 
expandOutputSection(uint64_t size)1763837f427SRui Ueyama void LinkerScript::expandOutputSection(uint64_t size) {
1773837f427SRui Ueyama   ctx->outSec->size += size;
1783837f427SRui Ueyama   expandMemoryRegions(size);
179a6ce78ecSGeorge Rimar }
180a6ce78ecSGeorge Rimar 
setDot(Expr e,const Twine & loc,bool inSec)1813837f427SRui Ueyama void LinkerScript::setDot(Expr e, const Twine &loc, bool inSec) {
1823837f427SRui Ueyama   uint64_t val = e().getValue();
1833837f427SRui Ueyama   if (val < dot && inSec)
1843837f427SRui Ueyama     error(loc + ": unable to move location counter backward for: " +
1853837f427SRui Ueyama           ctx->outSec->name);
18618d19687SRui Ueyama 
1874cd7352cSRafael Espindola   // Update to location counter means update to section size.
1883837f427SRui Ueyama   if (inSec)
1893837f427SRui Ueyama     expandOutputSection(val - dot);
1909b99abcfSGeorge Rimar 
1913837f427SRui Ueyama   dot = val;
192679828ffSRafael Espindola }
193679828ffSRafael Espindola 
194c4ccfb5dSGeorge Rimar // Used for handling linker symbol assignments, for both finalizing
195c4ccfb5dSGeorge Rimar // their values and doing early declarations. Returns true if symbol
196c4ccfb5dSGeorge Rimar // should be defined from linker script.
shouldDefineSym(SymbolAssignment * cmd)1973837f427SRui Ueyama static bool shouldDefineSym(SymbolAssignment *cmd) {
1983837f427SRui Ueyama   if (cmd->name == ".")
199c4ccfb5dSGeorge Rimar     return false;
200c4ccfb5dSGeorge Rimar 
2013837f427SRui Ueyama   if (!cmd->provide)
202c4ccfb5dSGeorge Rimar     return true;
203c4ccfb5dSGeorge Rimar 
204c4ccfb5dSGeorge Rimar   // If a symbol was in PROVIDE(), we need to define it only
205c4ccfb5dSGeorge Rimar   // when it is a referenced undefined symbol.
2063837f427SRui Ueyama   Symbol *b = symtab->find(cmd->name);
207fc0aa842SFangrui Song   if (b && !b->isDefined() && !b->isCommon())
208c4ccfb5dSGeorge Rimar     return true;
2093345c9acSIgor Kudrin   return false;
210c4ccfb5dSGeorge Rimar }
211c4ccfb5dSGeorge Rimar 
2125d9f419aSFangrui Song // Called by processSymbolAssignments() to assign definitions to
2135d9f419aSFangrui Song // linker-script-defined symbols.
addSymbol(SymbolAssignment * cmd)2143837f427SRui Ueyama void LinkerScript::addSymbol(SymbolAssignment *cmd) {
2153837f427SRui Ueyama   if (!shouldDefineSym(cmd))
2168f1f3c40SMeador Inge     return;
2178f1f3c40SMeador Inge 
21818d19687SRui Ueyama   // Define a symbol.
2193837f427SRui Ueyama   ExprValue value = cmd->expression();
2203837f427SRui Ueyama   SectionBase *sec = value.isAbsolute() ? nullptr : value.sec;
2213837f427SRui Ueyama   uint8_t visibility = cmd->hidden ? STV_HIDDEN : STV_DEFAULT;
22218d19687SRui Ueyama 
22318d19687SRui Ueyama   // When this function is called, section addresses have not been
22418d19687SRui Ueyama   // fixed yet. So, we may or may not know the value of the RHS
22518d19687SRui Ueyama   // expression.
22618d19687SRui Ueyama   //
22718d19687SRui Ueyama   // For example, if an expression is `x = 42`, we know x is always 42.
22818d19687SRui Ueyama   // However, if an expression is `x = .`, there's no way to know its
22918d19687SRui Ueyama   // value at the moment.
23018d19687SRui Ueyama   //
23118d19687SRui Ueyama   // We want to set symbol values early if we can. This allows us to
23218d19687SRui Ueyama   // use symbols as variables in linker scripts. Doing so allows us to
23318d19687SRui Ueyama   // write expressions like this: `alignment = 16; . = ALIGN(., alignment)`.
2343837f427SRui Ueyama   uint64_t symValue = value.sec ? 0 : value.getValue();
23518d19687SRui Ueyama 
2369670029bSFangrui Song   Defined newSym(nullptr, cmd->name, STB_GLOBAL, visibility, value.type,
237ab04ad6aSFangrui Song                  symValue, 0, sec);
2387d476192SRui Ueyama 
2393837f427SRui Ueyama   Symbol *sym = symtab->insert(cmd->name);
240ab04ad6aSFangrui Song   sym->mergeProperties(newSym);
241ab04ad6aSFangrui Song   sym->replace(newSym);
2428ca46bbaSFangrui Song   sym->isUsedInRegularObj = true;
2433837f427SRui Ueyama   cmd->sym = cast<Defined>(sym);
24418d19687SRui Ueyama }
24518d19687SRui Ueyama 
246c844524eSIgor Kudrin // This function is called from LinkerScript::declareSymbols.
247c844524eSIgor Kudrin // It creates a placeholder symbol if needed.
declareSymbol(SymbolAssignment * cmd)2483837f427SRui Ueyama static void declareSymbol(SymbolAssignment *cmd) {
2493837f427SRui Ueyama   if (!shouldDefineSym(cmd))
250c844524eSIgor Kudrin     return;
251c4ccfb5dSGeorge Rimar 
2523837f427SRui Ueyama   uint8_t visibility = cmd->hidden ? STV_HIDDEN : STV_DEFAULT;
253ab04ad6aSFangrui Song   Defined newSym(nullptr, cmd->name, STB_GLOBAL, visibility, STT_NOTYPE, 0, 0,
2547d476192SRui Ueyama                  nullptr);
2557d476192SRui Ueyama 
256c4ccfb5dSGeorge Rimar   // We can't calculate final value right now.
2573837f427SRui Ueyama   Symbol *sym = symtab->insert(cmd->name);
258ab04ad6aSFangrui Song   sym->mergeProperties(newSym);
259ab04ad6aSFangrui Song   sym->replace(newSym);
2607d476192SRui Ueyama 
2613837f427SRui Ueyama   cmd->sym = cast<Defined>(sym);
2623837f427SRui Ueyama   cmd->provide = false;
2638ca46bbaSFangrui Song   sym->isUsedInRegularObj = true;
2643837f427SRui Ueyama   sym->scriptDefined = true;
265c4ccfb5dSGeorge Rimar }
266c844524eSIgor Kudrin 
267debcac9fSFangrui Song using SymbolAssignmentMap =
268debcac9fSFangrui Song     DenseMap<const Defined *, std::pair<SectionBase *, uint64_t>>;
269debcac9fSFangrui Song 
270debcac9fSFangrui Song // Collect section/value pairs of linker-script-defined symbols. This is used to
271debcac9fSFangrui Song // check whether symbol values converge.
27210316a6fSFangrui Song static SymbolAssignmentMap
getSymbolAssignmentValues(ArrayRef<SectionCommand * > sectionCommands)27310316a6fSFangrui Song getSymbolAssignmentValues(ArrayRef<SectionCommand *> sectionCommands) {
274debcac9fSFangrui Song   SymbolAssignmentMap ret;
2757051aeefSFangrui Song   for (SectionCommand *cmd : sectionCommands) {
2767051aeefSFangrui Song     if (auto *assign = dyn_cast<SymbolAssignment>(cmd)) {
2777051aeefSFangrui Song       if (assign->sym) // sym is nullptr for dot.
2787051aeefSFangrui Song         ret.try_emplace(assign->sym, std::make_pair(assign->sym->section,
2797051aeefSFangrui Song                                                     assign->sym->value));
280debcac9fSFangrui Song       continue;
281debcac9fSFangrui Song     }
2826c814931SFangrui Song     for (SectionCommand *subCmd : cast<OutputDesc>(cmd)->osec.commands)
2837051aeefSFangrui Song       if (auto *assign = dyn_cast<SymbolAssignment>(subCmd))
2847051aeefSFangrui Song         if (assign->sym)
2857051aeefSFangrui Song           ret.try_emplace(assign->sym, std::make_pair(assign->sym->section,
2867051aeefSFangrui Song                                                       assign->sym->value));
287debcac9fSFangrui Song   }
288debcac9fSFangrui Song   return ret;
289debcac9fSFangrui Song }
290debcac9fSFangrui Song 
291debcac9fSFangrui Song // Returns the lexicographical smallest (for determinism) Defined whose
292debcac9fSFangrui Song // section/value has changed.
293debcac9fSFangrui Song static const Defined *
getChangedSymbolAssignment(const SymbolAssignmentMap & oldValues)294debcac9fSFangrui Song getChangedSymbolAssignment(const SymbolAssignmentMap &oldValues) {
295debcac9fSFangrui Song   const Defined *changed = nullptr;
296debcac9fSFangrui Song   for (auto &it : oldValues) {
297debcac9fSFangrui Song     const Defined *sym = it.first;
298debcac9fSFangrui Song     if (std::make_pair(sym->section, sym->value) != it.second &&
299debcac9fSFangrui Song         (!changed || sym->getName() < changed->getName()))
300debcac9fSFangrui Song       changed = sym;
301debcac9fSFangrui Song   }
302debcac9fSFangrui Song   return changed;
303debcac9fSFangrui Song }
304debcac9fSFangrui Song 
3057c426fb1SFangrui Song // Process INSERT [AFTER|BEFORE] commands. For each command, we move the
3067c426fb1SFangrui Song // specified output section to the designated place.
processInsertCommands()3079e2c8a9dSGeorge Rimar void LinkerScript::processInsertCommands() {
3086c814931SFangrui Song   SmallVector<OutputDesc *, 0> moves;
3097c426fb1SFangrui Song   for (const InsertCommand &cmd : insertCommands) {
31003051f7aSFangrui Song     for (StringRef name : cmd.names) {
31103051f7aSFangrui Song       // If base is empty, it may have been discarded by
312a0318711SFangrui Song       // adjustOutputSections(). We do not handle such output sections.
3137051aeefSFangrui Song       auto from = llvm::find_if(sectionCommands, [&](SectionCommand *subCmd) {
3146c814931SFangrui Song         return isa<OutputDesc>(subCmd) &&
3156c814931SFangrui Song                cast<OutputDesc>(subCmd)->osec.name == name;
316899fdf54SFangrui Song       });
3177c426fb1SFangrui Song       if (from == sectionCommands.end())
318796684b4SGeorge Rimar         continue;
3196c814931SFangrui Song       moves.push_back(cast<OutputDesc>(*from));
3207c426fb1SFangrui Song       sectionCommands.erase(from);
32103051f7aSFangrui Song     }
322796684b4SGeorge Rimar 
3237051aeefSFangrui Song     auto insertPos =
3247051aeefSFangrui Song         llvm::find_if(sectionCommands, [&cmd](SectionCommand *subCmd) {
3256c814931SFangrui Song           auto *to = dyn_cast<OutputDesc>(subCmd);
3266c814931SFangrui Song           return to != nullptr && to->osec.name == cmd.where;
3277c426fb1SFangrui Song         });
3287c426fb1SFangrui Song     if (insertPos == sectionCommands.end()) {
32903051f7aSFangrui Song       error("unable to insert " + cmd.names[0] +
3307c426fb1SFangrui Song             (cmd.isAfter ? " after " : " before ") + cmd.where);
3317c426fb1SFangrui Song     } else {
3327c426fb1SFangrui Song       if (cmd.isAfter)
3337c426fb1SFangrui Song         ++insertPos;
33403051f7aSFangrui Song       sectionCommands.insert(insertPos, moves.begin(), moves.end());
3357c426fb1SFangrui Song     }
33603051f7aSFangrui Song     moves.clear();
3377c426fb1SFangrui Song   }
3389e2c8a9dSGeorge Rimar }
3399e2c8a9dSGeorge Rimar 
340c844524eSIgor Kudrin // Symbols defined in script should not be inlined by LTO. At the same time
341c844524eSIgor Kudrin // we don't know their final values until late stages of link. Here we scan
342c844524eSIgor Kudrin // over symbol assignment commands and create placeholder symbols if needed.
declareSymbols()343c844524eSIgor Kudrin void LinkerScript::declareSymbols() {
3443837f427SRui Ueyama   assert(!ctx);
3457051aeefSFangrui Song   for (SectionCommand *cmd : sectionCommands) {
3467051aeefSFangrui Song     if (auto *assign = dyn_cast<SymbolAssignment>(cmd)) {
3477051aeefSFangrui Song       declareSymbol(assign);
348c844524eSIgor Kudrin       continue;
349c844524eSIgor Kudrin     }
350ccf0db99SGeorge Rimar 
351c844524eSIgor Kudrin     // If the output section directive has constraints,
352c844524eSIgor Kudrin     // we can't say for sure if it is going to be included or not.
353c844524eSIgor Kudrin     // Skip such sections for now. Improve the checks if we ever
354c844524eSIgor Kudrin     // need symbols from that sections to be declared early.
3556c814931SFangrui Song     const OutputSection &sec = cast<OutputDesc>(cmd)->osec;
3566c814931SFangrui Song     if (sec.constraint != ConstraintKind::NoConstraint)
357c844524eSIgor Kudrin       continue;
3586c814931SFangrui Song     for (SectionCommand *cmd : sec.commands)
3597051aeefSFangrui Song       if (auto *assign = dyn_cast<SymbolAssignment>(cmd))
3607051aeefSFangrui Song         declareSymbol(assign);
361c844524eSIgor Kudrin   }
362c4ccfb5dSGeorge Rimar }
363c4ccfb5dSGeorge Rimar 
36418d19687SRui Ueyama // This function is called from assignAddresses, while we are
36518d19687SRui Ueyama // fixing the output section addresses. This function is supposed
36618d19687SRui Ueyama // to set the final value for a given symbol assignment.
assignSymbol(SymbolAssignment * cmd,bool inSec)3673837f427SRui Ueyama void LinkerScript::assignSymbol(SymbolAssignment *cmd, bool inSec) {
3683837f427SRui Ueyama   if (cmd->name == ".") {
3693837f427SRui Ueyama     setDot(cmd->expression, cmd->location, inSec);
37018d19687SRui Ueyama     return;
37118d19687SRui Ueyama   }
37218d19687SRui Ueyama 
3733837f427SRui Ueyama   if (!cmd->sym)
37418d19687SRui Ueyama     return;
37518d19687SRui Ueyama 
3763837f427SRui Ueyama   ExprValue v = cmd->expression();
3773837f427SRui Ueyama   if (v.isAbsolute()) {
3783837f427SRui Ueyama     cmd->sym->section = nullptr;
3793837f427SRui Ueyama     cmd->sym->value = v.getValue();
38018d19687SRui Ueyama   } else {
3813837f427SRui Ueyama     cmd->sym->section = v.sec;
3823837f427SRui Ueyama     cmd->sym->value = v.getSectionOffset();
38318d19687SRui Ueyama   }
3849670029bSFangrui Song   cmd->sym->type = v.type;
385ceabe80eSEugene Leviant }
386ceabe80eSEugene Leviant 
getFilename(const InputFile * file)38777152a6bSAndrew Ng static inline StringRef getFilename(const InputFile *file) {
38877152a6bSAndrew Ng   return file ? file->getNameForScript() : StringRef();
38977152a6bSAndrew Ng }
39077152a6bSAndrew Ng 
matchesFile(const InputFile * file) const39177152a6bSAndrew Ng bool InputSectionDescription::matchesFile(const InputFile *file) const {
39277152a6bSAndrew Ng   if (filePat.isTrivialMatchAll())
39377152a6bSAndrew Ng     return true;
39477152a6bSAndrew Ng 
39577152a6bSAndrew Ng   if (!matchesFileCache || matchesFileCache->first != file)
39677152a6bSAndrew Ng     matchesFileCache.emplace(file, filePat.match(getFilename(file)));
39777152a6bSAndrew Ng 
39877152a6bSAndrew Ng   return matchesFileCache->second;
39977152a6bSAndrew Ng }
40077152a6bSAndrew Ng 
excludesFile(const InputFile * file) const40177152a6bSAndrew Ng bool SectionPattern::excludesFile(const InputFile *file) const {
40277152a6bSAndrew Ng   if (excludedFilePat.empty())
40377152a6bSAndrew Ng     return false;
40477152a6bSAndrew Ng 
40577152a6bSAndrew Ng   if (!excludesFileCache || excludesFileCache->first != file)
40677152a6bSAndrew Ng     excludesFileCache.emplace(file, excludedFilePat.match(getFilename(file)));
40777152a6bSAndrew Ng 
40877152a6bSAndrew Ng   return excludesFileCache->second;
409e0be2901SRui Ueyama }
410e0be2901SRui Ueyama 
shouldKeep(InputSectionBase * s)4113837f427SRui Ueyama bool LinkerScript::shouldKeep(InputSectionBase *s) {
4123837f427SRui Ueyama   for (InputSectionDescription *id : keptSections)
41377152a6bSAndrew Ng     if (id->matchesFile(s->file))
4143837f427SRui Ueyama       for (SectionPattern &p : id->sectionPatterns)
415dbd0ad33SPeter Smith         if (p.sectionPat.match(s->name) &&
416dbd0ad33SPeter Smith             (s->flags & id->withFlags) == id->withFlags &&
417dbd0ad33SPeter Smith             (s->flags & id->withoutFlags) == 0)
418eea3114fSGeorge Rimar           return true;
419eea3114fSGeorge Rimar   return false;
420eea3114fSGeorge Rimar }
421eea3114fSGeorge Rimar 
422ea93fe00SRui Ueyama // A helper function for the SORT() command.
matchConstraints(ArrayRef<InputSectionBase * > sections,ConstraintKind kind)423e47bbd28SFangrui Song static bool matchConstraints(ArrayRef<InputSectionBase *> sections,
4243837f427SRui Ueyama                              ConstraintKind kind) {
4253837f427SRui Ueyama   if (kind == ConstraintKind::NoConstraint)
4268f66df92SGeorge Rimar     return true;
4272c7171bfSRui Ueyama 
4283837f427SRui Ueyama   bool isRW = llvm::any_of(
429e47bbd28SFangrui Song       sections, [](InputSectionBase *sec) { return sec->flags & SHF_WRITE; });
4302c7171bfSRui Ueyama 
4313837f427SRui Ueyama   return (isRW && kind == ConstraintKind::ReadWrite) ||
4323837f427SRui Ueyama          (!isRW && kind == ConstraintKind::ReadOnly);
43306ae6836SGeorge Rimar }
43406ae6836SGeorge Rimar 
sortSections(MutableArrayRef<InputSectionBase * > vec,SortSectionPolicy k)435e47bbd28SFangrui Song static void sortSections(MutableArrayRef<InputSectionBase *> vec,
4363837f427SRui Ueyama                          SortSectionPolicy k) {
4377a210d65SJF Bastien   auto alignmentComparator = [](InputSectionBase *a, InputSectionBase *b) {
4387a210d65SJF Bastien     // ">" is not a mistake. Sections with larger alignments are placed
4397a210d65SJF Bastien     // before sections with smaller alignments in order to reduce the
4407a210d65SJF Bastien     // amount of padding necessary. This is compatible with GNU.
4417a210d65SJF Bastien     return a->alignment > b->alignment;
4427a210d65SJF Bastien   };
4437a210d65SJF Bastien   auto nameComparator = [](InputSectionBase *a, InputSectionBase *b) {
4447a210d65SJF Bastien     return a->name < b->name;
4457a210d65SJF Bastien   };
4467a210d65SJF Bastien   auto priorityComparator = [](InputSectionBase *a, InputSectionBase *b) {
4477a210d65SJF Bastien     return getPriority(a->name) < getPriority(b->name);
4487a210d65SJF Bastien   };
4497a210d65SJF Bastien 
4507a210d65SJF Bastien   switch (k) {
4517a210d65SJF Bastien   case SortSectionPolicy::Default:
4527a210d65SJF Bastien   case SortSectionPolicy::None:
4537a210d65SJF Bastien     return;
4547a210d65SJF Bastien   case SortSectionPolicy::Alignment:
4557a210d65SJF Bastien     return llvm::stable_sort(vec, alignmentComparator);
4567a210d65SJF Bastien   case SortSectionPolicy::Name:
4577a210d65SJF Bastien     return llvm::stable_sort(vec, nameComparator);
4587a210d65SJF Bastien   case SortSectionPolicy::Priority:
4597a210d65SJF Bastien     return llvm::stable_sort(vec, priorityComparator);
4607a210d65SJF Bastien   }
461ee924709SRui Ueyama }
462ee924709SRui Ueyama 
4637ad1e310SRui Ueyama // Sort sections as instructed by SORT-family commands and --sort-section
4647ad1e310SRui Ueyama // option. Because SORT-family commands can be nested at most two depth
4657ad1e310SRui Ueyama // (e.g. SORT_BY_NAME(SORT_BY_ALIGNMENT(.text.*))) and because the command
4667ad1e310SRui Ueyama // line option is respected even if a SORT command is given, the exact
4677ad1e310SRui Ueyama // behavior we have here is a bit complicated. Here are the rules.
4687ad1e310SRui Ueyama //
4697ad1e310SRui Ueyama // 1. If two SORT commands are given, --sort-section is ignored.
4707ad1e310SRui Ueyama // 2. If one SORT command is given, and if it is not SORT_NONE,
4717ad1e310SRui Ueyama //    --sort-section is handled as an inner SORT command.
4727ad1e310SRui Ueyama // 3. If one SORT command is given, and if it is SORT_NONE, don't sort.
4737ad1e310SRui Ueyama // 4. If no SORT command is given, sort according to --sort-section.
sortInputSections(MutableArrayRef<InputSectionBase * > vec,SortSectionPolicy outer,SortSectionPolicy inner)474e47bbd28SFangrui Song static void sortInputSections(MutableArrayRef<InputSectionBase *> vec,
47573d01a80SFangrui Song                               SortSectionPolicy outer,
47673d01a80SFangrui Song                               SortSectionPolicy inner) {
47773d01a80SFangrui Song   if (outer == SortSectionPolicy::None)
4787ad1e310SRui Ueyama     return;
4797ad1e310SRui Ueyama 
48073d01a80SFangrui Song   if (inner == SortSectionPolicy::Default)
4813837f427SRui Ueyama     sortSections(vec, config->sortSection);
4827ad1e310SRui Ueyama   else
48373d01a80SFangrui Song     sortSections(vec, inner);
48473d01a80SFangrui Song   sortSections(vec, outer);
4857ad1e310SRui Ueyama }
4867ad1e310SRui Ueyama 
487d3190795SRafael Espindola // Compute and remember which sections the InputSectionDescription matches.
48810316a6fSFangrui Song SmallVector<InputSectionBase *, 0>
computeInputSections(const InputSectionDescription * cmd,ArrayRef<InputSectionBase * > sections)4890ae7990bSPeter Smith LinkerScript::computeInputSections(const InputSectionDescription *cmd,
4900ae7990bSPeter Smith                                    ArrayRef<InputSectionBase *> sections) {
49110316a6fSFangrui Song   SmallVector<InputSectionBase *, 0> ret;
492a1c2ee01SFangrui Song   SmallVector<size_t, 0> indexes;
49373d01a80SFangrui Song   DenseSet<size_t> seen;
49473d01a80SFangrui Song   auto sortByPositionThenCommandLine = [&](size_t begin, size_t end) {
49573d01a80SFangrui Song     llvm::sort(MutableArrayRef<size_t>(indexes).slice(begin, end - begin));
49673d01a80SFangrui Song     for (size_t i = begin; i != end; ++i)
49773d01a80SFangrui Song       ret[i] = sections[indexes[i]];
49873d01a80SFangrui Song     sortInputSections(
49973d01a80SFangrui Song         MutableArrayRef<InputSectionBase *>(ret).slice(begin, end - begin),
50073d01a80SFangrui Song         config->sortSection, SortSectionPolicy::None);
50173d01a80SFangrui Song   };
5028c6a5aafSRui Ueyama 
50372e107f3SRui Ueyama   // Collects all sections that satisfy constraints of Cmd.
50473d01a80SFangrui Song   size_t sizeAfterPrevSort = 0;
5053837f427SRui Ueyama   for (const SectionPattern &pat : cmd->sectionPatterns) {
50673d01a80SFangrui Song     size_t sizeBeforeCurrPat = ret.size();
50772e107f3SRui Ueyama 
50873d01a80SFangrui Song     for (size_t i = 0, e = sections.size(); i != e; ++i) {
50973d01a80SFangrui Song       // Skip if the section is dead or has been matched by a previous input
51073d01a80SFangrui Song       // section description or a previous pattern.
51173d01a80SFangrui Song       InputSectionBase *sec = sections[i];
51273d01a80SFangrui Song       if (!sec->isLive() || sec->parent || seen.contains(i))
5138c6a5aafSRui Ueyama         continue;
51472e107f3SRui Ueyama 
515bf6e259bSFangrui Song       // For --emit-relocs we have to ignore entries like
516908a3d34SRafael Espindola       //   .rela.dyn : { *(.rela.data) }
517908a3d34SRafael Espindola       // which are common because they are in the default bfd script.
518d8281379SGeorge Rimar       // We do not ignore SHT_REL[A] linker-synthesized sections here because
519d8281379SGeorge Rimar       // want to support scripts that do custom layout for them.
520e47bbd28SFangrui Song       if (isa<InputSection>(sec) &&
521e47bbd28SFangrui Song           cast<InputSection>(sec)->getRelocatedSection())
522908a3d34SRafael Espindola         continue;
5238c6a5aafSRui Ueyama 
524d36b2649SAndrew Ng       // Check the name early to improve performance in the common case.
525d36b2649SAndrew Ng       if (!pat.sectionPat.match(sec->name))
526d36b2649SAndrew Ng         continue;
527d36b2649SAndrew Ng 
52877152a6bSAndrew Ng       if (!cmd->matchesFile(sec->file) || pat.excludesFile(sec->file) ||
529dbd0ad33SPeter Smith           (sec->flags & cmd->withFlags) != cmd->withFlags ||
530dbd0ad33SPeter Smith           (sec->flags & cmd->withoutFlags) != 0)
531e0be2901SRui Ueyama         continue;
53272e107f3SRui Ueyama 
533e47bbd28SFangrui Song       ret.push_back(sec);
53473d01a80SFangrui Song       indexes.push_back(i);
53573d01a80SFangrui Song       seen.insert(i);
536f94efdddSRui Ueyama     }
537d3190795SRafael Espindola 
53873d01a80SFangrui Song     if (pat.sortOuter == SortSectionPolicy::Default)
53973d01a80SFangrui Song       continue;
54073d01a80SFangrui Song 
54173d01a80SFangrui Song     // Matched sections are ordered by radix sort with the keys being (SORT*,
54273d01a80SFangrui Song     // --sort-section, input order), where SORT* (if present) is most
54373d01a80SFangrui Song     // significant.
54473d01a80SFangrui Song     //
54573d01a80SFangrui Song     // Matched sections between the previous SORT* and this SORT* are sorted by
54673d01a80SFangrui Song     // (--sort-alignment, input order).
54773d01a80SFangrui Song     sortByPositionThenCommandLine(sizeAfterPrevSort, sizeBeforeCurrPat);
54873d01a80SFangrui Song     // Matched sections by this SORT* pattern are sorted using all 3 keys.
54973d01a80SFangrui Song     // ret[sizeBeforeCurrPat,ret.size()) are already in the input order, so we
55073d01a80SFangrui Song     // just sort by sortOuter and sortInner.
551e47bbd28SFangrui Song     sortInputSections(
55273d01a80SFangrui Song         MutableArrayRef<InputSectionBase *>(ret).slice(sizeBeforeCurrPat),
55373d01a80SFangrui Song         pat.sortOuter, pat.sortInner);
55473d01a80SFangrui Song     sizeAfterPrevSort = ret.size();
555ee924709SRui Ueyama   }
55673d01a80SFangrui Song   // Matched sections after the last SORT* are sorted by (--sort-alignment,
55773d01a80SFangrui Song   // input order).
55873d01a80SFangrui Song   sortByPositionThenCommandLine(sizeAfterPrevSort, ret.size());
5593837f427SRui Ueyama   return ret;
560be94e1b6SRafael Espindola }
561be94e1b6SRafael Espindola 
discard(InputSectionBase & s)562baa3eb0dSFangrui Song void LinkerScript::discard(InputSectionBase &s) {
563db08df05SFangrui Song   if (&s == in.shStrTab.get())
564baa3eb0dSFangrui Song     error("discarding " + s.name + " section is not allowed");
5658e38ea8bSRui Ueyama 
566baa3eb0dSFangrui Song   s.markDead();
567baa3eb0dSFangrui Song   s.parent = nullptr;
568baa3eb0dSFangrui Song   for (InputSection *sec : s.dependentSections)
569baa3eb0dSFangrui Song     discard(*sec);
570be94e1b6SRafael Espindola }
571be94e1b6SRafael Espindola 
discardSynthetic(OutputSection & outCmd)5720ae7990bSPeter Smith void LinkerScript::discardSynthetic(OutputSection &outCmd) {
5730ae7990bSPeter Smith   for (Partition &part : partitions) {
5740ae7990bSPeter Smith     if (!part.armExidx || !part.armExidx->isLive())
5750ae7990bSPeter Smith       continue;
576ac0986f8SFangrui Song     SmallVector<InputSectionBase *, 0> secs(
577ac0986f8SFangrui Song         part.armExidx->exidxSections.begin(),
5780ae7990bSPeter Smith         part.armExidx->exidxSections.end());
5797051aeefSFangrui Song     for (SectionCommand *cmd : outCmd.commands)
58010316a6fSFangrui Song       if (auto *isd = dyn_cast<InputSectionDescription>(cmd))
58110316a6fSFangrui Song         for (InputSectionBase *s : computeInputSections(isd, secs))
582baa3eb0dSFangrui Song           discard(*s);
5830ae7990bSPeter Smith   }
5840ae7990bSPeter Smith }
5850ae7990bSPeter Smith 
58610316a6fSFangrui Song SmallVector<InputSectionBase *, 0>
createInputSectionList(OutputSection & outCmd)5873837f427SRui Ueyama LinkerScript::createInputSectionList(OutputSection &outCmd) {
58810316a6fSFangrui Song   SmallVector<InputSectionBase *, 0> ret;
589e7f912cdSRui Ueyama 
5907051aeefSFangrui Song   for (SectionCommand *cmd : outCmd.commands) {
5917051aeefSFangrui Song     if (auto *isd = dyn_cast<InputSectionDescription>(cmd)) {
5927051aeefSFangrui Song       isd->sectionBases = computeInputSections(isd, inputSections);
5937051aeefSFangrui Song       for (InputSectionBase *s : isd->sectionBases)
594e447d5afSFangrui Song         s->parent = &outCmd;
5957051aeefSFangrui Song       ret.insert(ret.end(), isd->sectionBases.begin(), isd->sectionBases.end());
5960b9ce6a4SRui Ueyama     }
59705433431SRui Ueyama   }
5983837f427SRui Ueyama   return ret;
5990b9ce6a4SRui Ueyama }
6000b9ce6a4SRui Ueyama 
6015d9f419aSFangrui Song // Create output sections described by SECTIONS commands.
processSectionCommands()6023558e24aSRafael Espindola void LinkerScript::processSectionCommands() {
603899fdf54SFangrui Song   auto process = [this](OutputSection *osec) {
60410316a6fSFangrui Song     SmallVector<InputSectionBase *, 0> v = createInputSectionList(*osec);
6057bd37870SRafael Espindola 
6060b1b695aSRui Ueyama     // The output section name `/DISCARD/' is special.
6070b1b695aSRui Ueyama     // Any input section assigned to it is discarded.
608899fdf54SFangrui Song     if (osec->name == "/DISCARD/") {
609e47bbd28SFangrui Song       for (InputSectionBase *s : v)
610baa3eb0dSFangrui Song         discard(*s);
611899fdf54SFangrui Song       discardSynthetic(*osec);
6126188fd49SFangrui Song       osec->commands.clear();
613899fdf54SFangrui Song       return false;
61448c3f1ceSRui Ueyama     }
6150b9ce6a4SRui Ueyama 
6160b1b695aSRui Ueyama     // This is for ONLY_IF_RO and ONLY_IF_RW. An output section directive
6170b1b695aSRui Ueyama     // ".foo : ONLY_IF_R[OW] { ... }" is handled only if all member input
6180b1b695aSRui Ueyama     // sections satisfy a given constraint. If not, a directive is handled
61907d7c42cSGeorge Rimar     // as if it wasn't present from the beginning.
6200b1b695aSRui Ueyama     //
6211b45d377SGeorge Rimar     // Because we'll iterate over SectionCommands many more times, the easy
6221b45d377SGeorge Rimar     // way to "make it as if it wasn't present" is to make it empty.
623899fdf54SFangrui Song     if (!matchConstraints(v, osec->constraint)) {
6243837f427SRui Ueyama       for (InputSectionBase *s : v)
625e447d5afSFangrui Song         s->parent = nullptr;
6266188fd49SFangrui Song       osec->commands.clear();
627899fdf54SFangrui Song       return false;
6287c3ff2ebSRafael Espindola     }
6297c3ff2ebSRafael Espindola 
6300b1b695aSRui Ueyama     // Handle subalign (e.g. ".foo : SUBALIGN(32) { ... }"). If subalign
6310b1b695aSRui Ueyama     // is given, input sections are aligned to that value, whether the
6320b1b695aSRui Ueyama     // given value is larger or smaller than the original section alignment.
633899fdf54SFangrui Song     if (osec->subalignExpr) {
634899fdf54SFangrui Song       uint32_t subalign = osec->subalignExpr().getValue();
6353837f427SRui Ueyama       for (InputSectionBase *s : v)
6363837f427SRui Ueyama         s->alignment = subalign;
63720d03194SEugene Leviant     }
6380b1b695aSRui Ueyama 
639f1e14519SFangrui Song     // Set the partition field the same way OutputSection::recordSection()
640f1e14519SFangrui Song     // does. Partitions cannot be used with the SECTIONS command, so this is
641f1e14519SFangrui Song     // always 1.
642899fdf54SFangrui Song     osec->partition = 1;
643899fdf54SFangrui Song     return true;
644899fdf54SFangrui Song   };
645f1e14519SFangrui Song 
646899fdf54SFangrui Song   // Process OVERWRITE_SECTIONS first so that it can overwrite the main script
647899fdf54SFangrui Song   // or orphans.
6486c814931SFangrui Song   DenseMap<CachedHashStringRef, OutputDesc *> map;
649899fdf54SFangrui Song   size_t i = 0;
6506c814931SFangrui Song   for (OutputDesc *osd : overwriteSections) {
6516c814931SFangrui Song     OutputSection *osec = &osd->osec;
652769057a5SFangrui Song     if (process(osec) &&
6536c814931SFangrui Song         !map.try_emplace(CachedHashStringRef(osec->name), osd).second)
654899fdf54SFangrui Song       warn("OVERWRITE_SECTIONS specifies duplicate " + osec->name);
6556c814931SFangrui Song   }
6567051aeefSFangrui Song   for (SectionCommand *&base : sectionCommands)
6576c814931SFangrui Song     if (auto *osd = dyn_cast<OutputDesc>(base)) {
6586c814931SFangrui Song       OutputSection *osec = &osd->osec;
6596c814931SFangrui Song       if (OutputDesc *overwrite = map.lookup(CachedHashStringRef(osec->name))) {
6606c814931SFangrui Song         log(overwrite->osec.location + " overwrites " + osec->name);
6616c814931SFangrui Song         overwrite->osec.sectionIndex = i++;
662899fdf54SFangrui Song         base = overwrite;
663899fdf54SFangrui Song       } else if (process(osec)) {
664899fdf54SFangrui Song         osec->sectionIndex = i++;
66548c3f1ceSRui Ueyama       }
6664aa2ef5bSRafael Espindola     }
667899fdf54SFangrui Song 
668899fdf54SFangrui Song   // If an OVERWRITE_SECTIONS specified output section is not in
669899fdf54SFangrui Song   // sectionCommands, append it to the end. The section will be inserted by
670899fdf54SFangrui Song   // orphan placement.
6716c814931SFangrui Song   for (OutputDesc *osd : overwriteSections)
6726c814931SFangrui Song     if (osd->osec.partition == 1 && osd->osec.sectionIndex == UINT32_MAX)
6736c814931SFangrui Song       sectionCommands.push_back(osd);
6745d9f419aSFangrui Song }
6755d9f419aSFangrui Song 
processSymbolAssignments()6765d9f419aSFangrui Song void LinkerScript::processSymbolAssignments() {
6775d9f419aSFangrui Song   // Dot outside an output section still represents a relative address, whose
6785d9f419aSFangrui Song   // sh_shndx should not be SHN_UNDEF or SHN_ABS. Create a dummy aether section
6795d9f419aSFangrui Song   // that fills the void outside a section. It has an index of one, which is
6805d9f419aSFangrui Song   // indistinguishable from any other regular section index.
6815d9f419aSFangrui Song   aether = make<OutputSection>("", 0, SHF_ALLOC);
6825d9f419aSFangrui Song   aether->sectionIndex = 1;
6835d9f419aSFangrui Song 
6845d9f419aSFangrui Song   // ctx captures the local AddressState and makes it accessible deliberately.
6855d9f419aSFangrui Song   // This is needed as there are some cases where we cannot just thread the
6865d9f419aSFangrui Song   // current state through to a lambda function created by the script parser.
6875d9f419aSFangrui Song   AddressState state;
6885d9f419aSFangrui Song   ctx = &state;
6895d9f419aSFangrui Song   ctx->outSec = aether;
6905d9f419aSFangrui Song 
6917051aeefSFangrui Song   for (SectionCommand *cmd : sectionCommands) {
6927051aeefSFangrui Song     if (auto *assign = dyn_cast<SymbolAssignment>(cmd))
6937051aeefSFangrui Song       addSymbol(assign);
6945d9f419aSFangrui Song     else
6956c814931SFangrui Song       for (SectionCommand *subCmd : cast<OutputDesc>(cmd)->osec.commands)
6967051aeefSFangrui Song         if (auto *assign = dyn_cast<SymbolAssignment>(subCmd))
6977051aeefSFangrui Song           addSymbol(assign);
6985d9f419aSFangrui Song   }
6995d9f419aSFangrui Song 
7003837f427SRui Ueyama   ctx = nullptr;
7011b45d377SGeorge Rimar }
702e63d81bdSEugene Leviant 
findByName(ArrayRef<SectionCommand * > vec,StringRef name)7037051aeefSFangrui Song static OutputSection *findByName(ArrayRef<SectionCommand *> vec,
7043837f427SRui Ueyama                                  StringRef name) {
7057051aeefSFangrui Song   for (SectionCommand *cmd : vec)
7066c814931SFangrui Song     if (auto *osd = dyn_cast<OutputDesc>(cmd))
7076c814931SFangrui Song       if (osd->osec.name == name)
7086c814931SFangrui Song         return &osd->osec;
709d2f225fdSRui Ueyama   return nullptr;
710d2f225fdSRui Ueyama }
711d2f225fdSRui Ueyama 
createSection(InputSectionBase * isec,StringRef outsecName)7126c814931SFangrui Song static OutputDesc *createSection(InputSectionBase *isec, StringRef outsecName) {
7136c814931SFangrui Song   OutputDesc *osd = script->createOutputSection(outsecName, "<internal>");
7146c814931SFangrui Song   osd->osec.recordSection(isec);
7156c814931SFangrui Song   return osd;
716aa8523e4SRui Ueyama }
717aa8523e4SRui Ueyama 
addInputSec(StringMap<TinyPtrVector<OutputSection * >> & map,InputSectionBase * isec,StringRef outsecName)7186c814931SFangrui Song static OutputDesc *addInputSec(StringMap<TinyPtrVector<OutputSection *>> &map,
7193837f427SRui Ueyama                                InputSectionBase *isec, StringRef outsecName) {
720aa8523e4SRui Ueyama   // Sections with SHT_GROUP or SHF_GROUP attributes reach here only when the -r
721aa8523e4SRui Ueyama   // option is given. A section with SHT_GROUP defines a "section group", and
722aa8523e4SRui Ueyama   // its members have SHF_GROUP attribute. Usually these flags have already been
723aa8523e4SRui Ueyama   // stripped by InputFiles.cpp as section groups are processed and uniquified.
724aa8523e4SRui Ueyama   // However, for the -r option, we want to pass through all section groups
725aa8523e4SRui Ueyama   // as-is because adding/removing members or merging them with other groups
726aa8523e4SRui Ueyama   // change their semantics.
7273837f427SRui Ueyama   if (isec->type == SHT_GROUP || (isec->flags & SHF_GROUP))
7283837f427SRui Ueyama     return createSection(isec, outsecName);
729aa8523e4SRui Ueyama 
730aa8523e4SRui Ueyama   // Imagine .zed : { *(.foo) *(.bar) } script. Both foo and bar may have
731aa8523e4SRui Ueyama   // relocation sections .rela.foo and .rela.bar for example. Most tools do
732aa8523e4SRui Ueyama   // not allow multiple REL[A] sections for output section. Hence we
733aa8523e4SRui Ueyama   // should combine these relocation sections into single output.
734aa8523e4SRui Ueyama   // We skip synthetic sections because it can be .rela.dyn/.rela.plt or any
735aa8523e4SRui Ueyama   // other REL[A] sections created by linker itself.
7363837f427SRui Ueyama   if (!isa<SyntheticSection>(isec) &&
7373837f427SRui Ueyama       (isec->type == SHT_REL || isec->type == SHT_RELA)) {
7383837f427SRui Ueyama     auto *sec = cast<InputSection>(isec);
7393837f427SRui Ueyama     OutputSection *out = sec->getRelocatedSection()->getOutputSection();
740aa8523e4SRui Ueyama 
7413837f427SRui Ueyama     if (out->relocationSection) {
742e47bbd28SFangrui Song       out->relocationSection->recordSection(sec);
743aa8523e4SRui Ueyama       return nullptr;
744aa8523e4SRui Ueyama     }
745aa8523e4SRui Ueyama 
7466c814931SFangrui Song     OutputDesc *osd = createSection(isec, outsecName);
7476c814931SFangrui Song     out->relocationSection = &osd->osec;
7486c814931SFangrui Song     return osd;
749aa8523e4SRui Ueyama   }
750aa8523e4SRui Ueyama 
751aa8523e4SRui Ueyama   //  The ELF spec just says
752aa8523e4SRui Ueyama   // ----------------------------------------------------------------
753aa8523e4SRui Ueyama   // In the first phase, input sections that match in name, type and
754aa8523e4SRui Ueyama   // attribute flags should be concatenated into single sections.
755aa8523e4SRui Ueyama   // ----------------------------------------------------------------
756aa8523e4SRui Ueyama   //
757aa8523e4SRui Ueyama   // However, it is clear that at least some flags have to be ignored for
758aa8523e4SRui Ueyama   // section merging. At the very least SHF_GROUP and SHF_COMPRESSED have to be
759aa8523e4SRui Ueyama   // ignored. We should not have two output .text sections just because one was
760aa8523e4SRui Ueyama   // in a group and another was not for example.
761aa8523e4SRui Ueyama   //
7629cd5df0eSFangrui Song   // It also seems that wording was a late addition and didn't get the
763aa8523e4SRui Ueyama   // necessary scrutiny.
764aa8523e4SRui Ueyama   //
765aa8523e4SRui Ueyama   // Merging sections with different flags is expected by some users. One
766aa8523e4SRui Ueyama   // reason is that if one file has
767aa8523e4SRui Ueyama   //
768aa8523e4SRui Ueyama   // int *const bar __attribute__((section(".foo"))) = (int *)0;
769aa8523e4SRui Ueyama   //
770aa8523e4SRui Ueyama   // gcc with -fPIC will produce a read only .foo section. But if another
771aa8523e4SRui Ueyama   // file has
772aa8523e4SRui Ueyama   //
773aa8523e4SRui Ueyama   // int zed;
774aa8523e4SRui Ueyama   // int *const bar __attribute__((section(".foo"))) = (int *)&zed;
775aa8523e4SRui Ueyama   //
776aa8523e4SRui Ueyama   // gcc with -fPIC will produce a read write section.
777aa8523e4SRui Ueyama   //
778aa8523e4SRui Ueyama   // Last but not least, when using linker script the merge rules are forced by
779aa8523e4SRui Ueyama   // the script. Unfortunately, linker scripts are name based. This means that
780aa8523e4SRui Ueyama   // expressions like *(.foo*) can refer to multiple input sections with
781aa8523e4SRui Ueyama   // different flags. We cannot put them in different output sections or we
782aa8523e4SRui Ueyama   // would produce wrong results for
783aa8523e4SRui Ueyama   //
784aa8523e4SRui Ueyama   // start = .; *(.foo.*) end = .; *(.bar)
785aa8523e4SRui Ueyama   //
786aa8523e4SRui Ueyama   // and a mapping of .foo1 and .bar1 to one section and .foo2 and .bar2 to
787aa8523e4SRui Ueyama   // another. The problem is that there is no way to layout those output
788aa8523e4SRui Ueyama   // sections such that the .foo sections are the only thing between the start
789aa8523e4SRui Ueyama   // and end symbols.
790aa8523e4SRui Ueyama   //
791aa8523e4SRui Ueyama   // Given the above issues, we instead merge sections by name and error on
792aa8523e4SRui Ueyama   // incompatible types and flags.
7933837f427SRui Ueyama   TinyPtrVector<OutputSection *> &v = map[outsecName];
7943837f427SRui Ueyama   for (OutputSection *sec : v) {
7953837f427SRui Ueyama     if (sec->partition != isec->partition)
796ba2816beSPeter Collingbourne       continue;
79797e251e0SPeter Collingbourne 
79897e251e0SPeter Collingbourne     if (config->relocatable && (isec->flags & SHF_LINK_ORDER)) {
79997e251e0SPeter Collingbourne       // Merging two SHF_LINK_ORDER sections with different sh_link fields will
80097e251e0SPeter Collingbourne       // change their semantics, so we only merge them in -r links if they will
80197e251e0SPeter Collingbourne       // end up being linked to the same output section. The casts are fine
80297e251e0SPeter Collingbourne       // because everything in the map was created by the orphan placement code.
80397e251e0SPeter Collingbourne       auto *firstIsec = cast<InputSectionBase>(
8046188fd49SFangrui Song           cast<InputSectionDescription>(sec->commands[0])->sectionBases[0]);
805ae73091fSFangrui Song       OutputSection *firstIsecOut =
806ae73091fSFangrui Song           firstIsec->flags & SHF_LINK_ORDER
807ae73091fSFangrui Song               ? firstIsec->getLinkOrderDep()->getOutputSection()
808ae73091fSFangrui Song               : nullptr;
809ae73091fSFangrui Song       if (firstIsecOut != isec->getLinkOrderDep()->getOutputSection())
81097e251e0SPeter Collingbourne         continue;
81197e251e0SPeter Collingbourne     }
81297e251e0SPeter Collingbourne 
813e47bbd28SFangrui Song     sec->recordSection(isec);
814aa8523e4SRui Ueyama     return nullptr;
815aa8523e4SRui Ueyama   }
816aa8523e4SRui Ueyama 
8176c814931SFangrui Song   OutputDesc *osd = createSection(isec, outsecName);
8186c814931SFangrui Song   v.push_back(&osd->osec);
8196c814931SFangrui Song   return osd;
820aa8523e4SRui Ueyama }
821aa8523e4SRui Ueyama 
8220b1b695aSRui Ueyama // Add sections that didn't match any sections command.
addOrphanSections()823aa8523e4SRui Ueyama void LinkerScript::addOrphanSections() {
8243837f427SRui Ueyama   StringMap<TinyPtrVector<OutputSection *>> map;
8256c814931SFangrui Song   SmallVector<OutputDesc *, 0> v;
82654634f19SGeorge Rimar 
8273c94d5d9SFangrui Song   auto add = [&](InputSectionBase *s) {
82897e251e0SPeter Collingbourne     if (s->isLive() && !s->parent) {
82942319409SFangrui Song       orphanSections.push_back(s);
83042319409SFangrui Song 
8313837f427SRui Ueyama       StringRef name = getOutputSectionName(s);
8326e2804ceSDavid Bozier       if (config->unique) {
8336e2804ceSDavid Bozier         v.push_back(createSection(s, name));
8346e2804ceSDavid Bozier       } else if (OutputSection *sec = findByName(sectionCommands, name)) {
835e47bbd28SFangrui Song         sec->recordSection(s);
83697e251e0SPeter Collingbourne       } else {
8376c814931SFangrui Song         if (OutputDesc *osd = addInputSec(map, s, name))
8386c814931SFangrui Song           v.push_back(osd);
839e47bbd28SFangrui Song         assert(isa<MergeInputSection>(s) ||
840e47bbd28SFangrui Song                s->getOutputSection()->sectionIndex == UINT32_MAX);
84197e251e0SPeter Collingbourne       }
84297e251e0SPeter Collingbourne     }
84354634f19SGeorge Rimar   };
84454634f19SGeorge Rimar 
8451e89f08fSNico Weber   // For further --emit-reloc handling code we need target output section
84654634f19SGeorge Rimar   // to be created before we create relocation output section, so we want
84754634f19SGeorge Rimar   // to create target sections first. We do not want priority handling
84854634f19SGeorge Rimar   // for synthetic sections because them are special.
8493837f427SRui Ueyama   for (InputSectionBase *isec : inputSections) {
85097e251e0SPeter Collingbourne     // In -r links, SHF_LINK_ORDER sections are added while adding their parent
85197e251e0SPeter Collingbourne     // sections because we need to know the parent's output section before we
85297e251e0SPeter Collingbourne     // can select an output section for the SHF_LINK_ORDER section.
85397e251e0SPeter Collingbourne     if (config->relocatable && (isec->flags & SHF_LINK_ORDER))
85497e251e0SPeter Collingbourne       continue;
85597e251e0SPeter Collingbourne 
8563837f427SRui Ueyama     if (auto *sec = dyn_cast<InputSection>(isec))
8573837f427SRui Ueyama       if (InputSectionBase *rel = sec->getRelocatedSection())
8583837f427SRui Ueyama         if (auto *relIS = dyn_cast_or_null<InputSectionBase>(rel->parent))
8593837f427SRui Ueyama           add(relIS);
8603837f427SRui Ueyama     add(isec);
8613c94d5d9SFangrui Song     if (config->relocatable)
8623c94d5d9SFangrui Song       for (InputSectionBase *depSec : isec->dependentSections)
8633c94d5d9SFangrui Song         if (depSec->flags & SHF_LINK_ORDER)
8643c94d5d9SFangrui Song           add(depSec);
865d7faa916SRafael Espindola   }
866f9b04fd9SGeorge Rimar 
867f9b04fd9SGeorge Rimar   // If no SECTIONS command was given, we should insert sections commands
868f9b04fd9SGeorge Rimar   // before others, so that we can handle scripts which refers them,
869f9b04fd9SGeorge Rimar   // for example: "foo = ABSOLUTE(ADDR(.text)));".
870f9b04fd9SGeorge Rimar   // When SECTIONS command is present we just add all orphans to the end.
8713837f427SRui Ueyama   if (hasSectionsCommand)
8723837f427SRui Ueyama     sectionCommands.insert(sectionCommands.end(), v.begin(), v.end());
873f9b04fd9SGeorge Rimar   else
8743837f427SRui Ueyama     sectionCommands.insert(sectionCommands.begin(), v.begin(), v.end());
8754f013bb3SRafael Espindola }
876e63d81bdSEugene Leviant 
diagnoseOrphanHandling() const87742319409SFangrui Song void LinkerScript::diagnoseOrphanHandling() const {
878439341b9SJames Henderson   llvm::TimeTraceScope timeScope("Diagnose orphan sections");
879048b16f7SFangrui Song   if (config->orphanHandling == OrphanHandlingPolicy::Place)
880048b16f7SFangrui Song     return;
88142319409SFangrui Song   for (const InputSectionBase *sec : orphanSections) {
88237c7f0d9SFangrui Song     // Input SHT_REL[A] retained by --emit-relocs are ignored by
88337c7f0d9SFangrui Song     // computeInputSections(). Don't warn/error.
88437c7f0d9SFangrui Song     if (isa<InputSection>(sec) &&
88537c7f0d9SFangrui Song         cast<InputSection>(sec)->getRelocatedSection())
88637c7f0d9SFangrui Song       continue;
88737c7f0d9SFangrui Song 
88842319409SFangrui Song     StringRef name = getOutputSectionName(sec);
88942319409SFangrui Song     if (config->orphanHandling == OrphanHandlingPolicy::Error)
89042319409SFangrui Song       error(toString(sec) + " is being placed in '" + name + "'");
891048b16f7SFangrui Song     else
89242319409SFangrui Song       warn(toString(sec) + " is being placed in '" + name + "'");
89342319409SFangrui Song   }
89442319409SFangrui Song }
89542319409SFangrui Song 
896b889744eSMeador Inge // This function searches for a memory region to place the given output
897b889744eSMeador Inge // section in. If found, a pointer to the appropriate memory region is
898d2dd36bbSIgor Kudrin // returned in the first member of the pair. Otherwise, a nullptr is returned.
899d2dd36bbSIgor Kudrin // The second member of the pair is a hint that should be passed to the
900d2dd36bbSIgor Kudrin // subsequent call of this method.
901d2dd36bbSIgor Kudrin std::pair<MemoryRegion *, MemoryRegion *>
findMemoryRegion(OutputSection * sec,MemoryRegion * hint)902d2dd36bbSIgor Kudrin LinkerScript::findMemoryRegion(OutputSection *sec, MemoryRegion *hint) {
90366691de9SIgor Kudrin   // Non-allocatable sections are not part of the process image.
90466691de9SIgor Kudrin   if (!(sec->flags & SHF_ALLOC)) {
90566691de9SIgor Kudrin     if (!sec->memoryRegionName.empty())
90666691de9SIgor Kudrin       warn("ignoring memory region assignment for non-allocatable section '" +
90766691de9SIgor Kudrin            sec->name + "'");
90866691de9SIgor Kudrin     return {nullptr, nullptr};
90966691de9SIgor Kudrin   }
91066691de9SIgor Kudrin 
911b889744eSMeador Inge   // If a memory region name was specified in the output section command,
912b889744eSMeador Inge   // then try to find that region first.
9133837f427SRui Ueyama   if (!sec->memoryRegionName.empty()) {
9143837f427SRui Ueyama     if (MemoryRegion *m = memoryRegions.lookup(sec->memoryRegionName))
915d2dd36bbSIgor Kudrin       return {m, m};
9163837f427SRui Ueyama     error("memory region '" + sec->memoryRegionName + "' not declared");
917d2dd36bbSIgor Kudrin     return {nullptr, nullptr};
918b889744eSMeador Inge   }
919b889744eSMeador Inge 
920d7c5400fSRui Ueyama   // If at least one memory region is defined, all sections must
921d7c5400fSRui Ueyama   // belong to some memory region. Otherwise, we don't need to do
922d7c5400fSRui Ueyama   // anything for memory regions.
9233837f427SRui Ueyama   if (memoryRegions.empty())
924d2dd36bbSIgor Kudrin     return {nullptr, nullptr};
925d2dd36bbSIgor Kudrin 
92666691de9SIgor Kudrin   // An orphan section should continue the previous memory region.
92766691de9SIgor Kudrin   if (sec->sectionIndex == UINT32_MAX && hint)
928d2dd36bbSIgor Kudrin     return {hint, hint};
929b889744eSMeador Inge 
930b889744eSMeador Inge   // See if a region can be found by matching section flags.
9313837f427SRui Ueyama   for (auto &pair : memoryRegions) {
9323837f427SRui Ueyama     MemoryRegion *m = pair.second;
9338cdf1c1eSIgor Kudrin     if (m->compatibleWith(sec->flags))
934d2dd36bbSIgor Kudrin       return {m, nullptr};
935b889744eSMeador Inge   }
936b889744eSMeador Inge 
937b889744eSMeador Inge   // Otherwise, no suitable region was found.
9383837f427SRui Ueyama   error("no memory region specified for section '" + sec->name + "'");
939d2dd36bbSIgor Kudrin   return {nullptr, nullptr};
940b889744eSMeador Inge }
941b889744eSMeador Inge 
findFirstSection(PhdrEntry * load)9423837f427SRui Ueyama static OutputSection *findFirstSection(PhdrEntry *load) {
9433837f427SRui Ueyama   for (OutputSection *sec : outputSections)
9443837f427SRui Ueyama     if (sec->ptLoad == load)
9453837f427SRui Ueyama       return sec;
946acf8cef8SGeorge Rimar   return nullptr;
947acf8cef8SGeorge Rimar }
948acf8cef8SGeorge Rimar 
9490b1b695aSRui Ueyama // This function assigns offsets to input sections and an output section
9500b1b695aSRui Ueyama // for a single sections command (e.g. ".text { *(.text); }").
assignOffsets(OutputSection * sec)9513837f427SRui Ueyama void LinkerScript::assignOffsets(OutputSection *sec) {
9529bd29a73SFangrui Song   const bool isTbss = (sec->flags & SHF_TLS) && sec->type == SHT_NOBITS;
9538ffb2097SFangrui Song   const bool sameMemRegion = ctx->memRegion == sec->memRegion;
9548ffb2097SFangrui Song   const bool prevLMARegionIsDefault = ctx->lmaRegion == nullptr;
955ec29538aSFangrui Song   const uint64_t savedDot = dot;
9563837f427SRui Ueyama   ctx->memRegion = sec->memRegion;
9573837f427SRui Ueyama   ctx->lmaRegion = sec->lmaRegion;
958ec29538aSFangrui Song 
9599bd29a73SFangrui Song   if (!(sec->flags & SHF_ALLOC)) {
9609bd29a73SFangrui Song     // Non-SHF_ALLOC sections have zero addresses.
9619bd29a73SFangrui Song     dot = 0;
9629bd29a73SFangrui Song   } else if (isTbss) {
9639bd29a73SFangrui Song     // Allow consecutive SHF_TLS SHT_NOBITS output sections. The address range
9649bd29a73SFangrui Song     // starts from the end address of the previous tbss section.
9659bd29a73SFangrui Song     if (ctx->tbssAddr == 0)
9669bd29a73SFangrui Song       ctx->tbssAddr = dot;
9679bd29a73SFangrui Song     else
9689bd29a73SFangrui Song       dot = ctx->tbssAddr;
9699bd29a73SFangrui Song   } else {
9703837f427SRui Ueyama     if (ctx->memRegion)
9713837f427SRui Ueyama       dot = ctx->memRegion->curPos;
972a6db64efSFangrui Song     if (sec->addrExpr)
9733837f427SRui Ueyama       setDot(sec->addrExpr, sec->location, false);
97414ef9b30SRui Ueyama 
975179dc276SFangrui Song     // If the address of the section has been moved forward by an explicit
976179dc276SFangrui Song     // expression so that it now starts past the current curPos of the enclosing
977179dc276SFangrui Song     // region, we need to expand the current region to account for the space
978179dc276SFangrui Song     // between the previous section, if any, and the start of this section.
979179dc276SFangrui Song     if (ctx->memRegion && ctx->memRegion->curPos < dot)
980179dc276SFangrui Song       expandMemoryRegion(ctx->memRegion, dot - ctx->memRegion->curPos,
981a05b694bSIgor Kudrin                          sec->name);
982ec29538aSFangrui Song   }
983179dc276SFangrui Song 
98489c0f455SFangrui Song   ctx->outSec = sec;
98589c0f455SFangrui Song   if (sec->addrExpr && script->hasSectionsCommand) {
98689c0f455SFangrui Song     // The alignment is ignored.
987e652f3f0SFangrui Song     sec->addr = dot;
98889c0f455SFangrui Song   } else {
989e652f3f0SFangrui Song     // sec->alignment is the max of ALIGN and the maximum of input
99089c0f455SFangrui Song     // section alignments.
99189c0f455SFangrui Song     const uint64_t pos = dot;
992*85cfd917SFangrui Song     dot = alignToPowerOf2(dot, sec->alignment);
993e652f3f0SFangrui Song     sec->addr = dot;
99489c0f455SFangrui Song     expandMemoryRegions(dot - pos);
99589c0f455SFangrui Song   }
99675702389SRafael Espindola 
997bb4a36eaSFangrui Song   // ctx->lmaOffset is LMA minus VMA. If LMA is explicitly specified via AT() or
998bb4a36eaSFangrui Song   // AT>, recompute ctx->lmaOffset; otherwise, if both previous/current LMA
9998ffb2097SFangrui Song   // region is the default, and the two sections are in the same memory region,
10008ffb2097SFangrui Song   // reuse previous lmaOffset; otherwise, reset lmaOffset to 0. This emulates
10018ffb2097SFangrui Song   // heuristics described in
1002bb4a36eaSFangrui Song   // https://sourceware.org/binutils/docs/ld/Output-Section-LMA.html
10038c18719bSKonstantin Schwarz   if (sec->lmaExpr) {
10043837f427SRui Ueyama     ctx->lmaOffset = sec->lmaExpr().getValue() - dot;
10058c18719bSKonstantin Schwarz   } else if (MemoryRegion *mr = sec->lmaRegion) {
1006*85cfd917SFangrui Song     uint64_t lmaStart = alignToPowerOf2(mr->curPos, sec->alignment);
10078c18719bSKonstantin Schwarz     if (mr->curPos < lmaStart)
1008a05b694bSIgor Kudrin       expandMemoryRegion(mr, lmaStart - mr->curPos, sec->name);
10098c18719bSKonstantin Schwarz     ctx->lmaOffset = lmaStart - dot;
10108c18719bSKonstantin Schwarz   } else if (!sameMemRegion || !prevLMARegionIsDefault) {
1011bb4a36eaSFangrui Song     ctx->lmaOffset = 0;
10128c18719bSKonstantin Schwarz   }
10135d01a8beSGeorge Rimar 
1014bb4a36eaSFangrui Song   // Propagate ctx->lmaOffset to the first "non-header" section.
1015e652f3f0SFangrui Song   if (PhdrEntry *l = sec->ptLoad)
10163837f427SRui Ueyama     if (sec == findFirstSection(l))
10173837f427SRui Ueyama       l->lmaOffset = ctx->lmaOffset;
10180b1b695aSRui Ueyama 
10190029f214SGeorge Rimar   // We can call this method multiple times during the creation of
10200029f214SGeorge Rimar   // thunks and want to start over calculation each time.
10213837f427SRui Ueyama   sec->size = 0;
10228d0efdd5SJames Henderson 
10232f4c121dSRui Ueyama   // We visited SectionsCommands from processSectionCommands to
10242f4c121dSRui Ueyama   // layout sections. Now, we visit SectionsCommands again to fix
10252f4c121dSRui Ueyama   // section offsets.
10267051aeefSFangrui Song   for (SectionCommand *cmd : sec->commands) {
10272f4c121dSRui Ueyama     // This handles the assignments to symbol or to the dot.
10287051aeefSFangrui Song     if (auto *assign = dyn_cast<SymbolAssignment>(cmd)) {
10297051aeefSFangrui Song       assign->addr = dot;
10307051aeefSFangrui Song       assignSymbol(assign, true);
10317051aeefSFangrui Song       assign->size = dot - assign->addr;
10322f4c121dSRui Ueyama       continue;
10332f4c121dSRui Ueyama     }
10342f4c121dSRui Ueyama 
10352f4c121dSRui Ueyama     // Handle BYTE(), SHORT(), LONG(), or QUAD().
10367051aeefSFangrui Song     if (auto *data = dyn_cast<ByteCommand>(cmd)) {
1037e652f3f0SFangrui Song       data->offset = dot - sec->addr;
10387051aeefSFangrui Song       dot += data->size;
10397051aeefSFangrui Song       expandOutputSection(data->size);
10402f4c121dSRui Ueyama       continue;
10412f4c121dSRui Ueyama     }
10422f4c121dSRui Ueyama 
10432f4c121dSRui Ueyama     // Handle a single input section description command.
10442f4c121dSRui Ueyama     // It calculates and assigns the offsets for each section and also
10452f4c121dSRui Ueyama     // updates the output section size.
10461164c4b3SFangrui Song     for (InputSection *isec : cast<InputSectionDescription>(cmd)->sections) {
1047e652f3f0SFangrui Song       assert(isec->getParent() == sec);
10481164c4b3SFangrui Song       const uint64_t pos = dot;
1049*85cfd917SFangrui Song       dot = alignToPowerOf2(dot, isec->alignment);
1050e652f3f0SFangrui Song       isec->outSecOff = dot - sec->addr;
10511164c4b3SFangrui Song       dot += isec->getSize();
10521164c4b3SFangrui Song 
10531164c4b3SFangrui Song       // Update output section size after adding each section. This is so that
10541164c4b3SFangrui Song       // SIZEOF works correctly in the case below:
10551164c4b3SFangrui Song       // .foo { *(.aaa) a = SIZEOF(.foo); *(.bbb) }
10561164c4b3SFangrui Song       expandOutputSection(dot - pos);
10571164c4b3SFangrui Song     }
10582f4c121dSRui Ueyama   }
1059ec29538aSFangrui Song 
1060ec29538aSFangrui Song   // Non-SHF_ALLOC sections do not affect the addresses of other OutputSections
1061ec29538aSFangrui Song   // as they are not part of the process image.
10629bd29a73SFangrui Song   if (!(sec->flags & SHF_ALLOC)) {
1063ec29538aSFangrui Song     dot = savedDot;
10649bd29a73SFangrui Song   } else if (isTbss) {
10659bd29a73SFangrui Song     // NOBITS TLS sections are similar. Additionally save the end address.
10669bd29a73SFangrui Song     ctx->tbssAddr = dot;
10679bd29a73SFangrui Song     dot = savedDot;
10689bd29a73SFangrui Song   }
10692f4c121dSRui Ueyama }
1070d3190795SRafael Espindola 
isDiscardable(const OutputSection & sec)107147eb3f15SAndrew Ng static bool isDiscardable(const OutputSection &sec) {
10723837f427SRui Ueyama   if (sec.name == "/DISCARD/")
1073abb7484cSFangrui Song     return true;
1074abb7484cSFangrui Song 
1075dee900aeSGeorge Rimar   // We do not want to remove OutputSections with expressions that reference
1076dee900aeSGeorge Rimar   // symbols even if the OutputSection is empty. We want to ensure that the
1077dee900aeSGeorge Rimar   // expressions can be evaluated and report an error if they cannot.
10783837f427SRui Ueyama   if (sec.expressionsUseSymbols)
1079852bd5c0SRafael Espindola     return false;
1080852bd5c0SRafael Espindola 
1081dee900aeSGeorge Rimar   // OutputSections may be referenced by name in ADDR and LOADADDR expressions,
1082dee900aeSGeorge Rimar   // as an empty Section can has a valid VMA and LMA we keep the OutputSection
1083dee900aeSGeorge Rimar   // to maintain the integrity of the other Expression.
10843837f427SRui Ueyama   if (sec.usedInExpression)
1085dee900aeSGeorge Rimar     return false;
1086dee900aeSGeorge Rimar 
10877051aeefSFangrui Song   for (SectionCommand *cmd : sec.commands) {
10887051aeefSFangrui Song     if (auto assign = dyn_cast<SymbolAssignment>(cmd))
1089b427d4ecSJames Henderson       // Don't create empty output sections just for unreferenced PROVIDE
1090b427d4ecSJames Henderson       // symbols.
10917051aeefSFangrui Song       if (assign->name != "." && !assign->sym)
1092b427d4ecSJames Henderson         continue;
1093b427d4ecSJames Henderson 
10947051aeefSFangrui Song     if (!isa<InputSectionDescription>(*cmd))
1095852bd5c0SRafael Espindola       return false;
1096b427d4ecSJames Henderson   }
1097afbf90aeSGeorge Rimar   return true;
1098852bd5c0SRafael Espindola }
1099852bd5c0SRafael Espindola 
isDiscarded(const OutputSection * sec) const110047eb3f15SAndrew Ng bool LinkerScript::isDiscarded(const OutputSection *sec) const {
110147eb3f15SAndrew Ng   return hasSectionsCommand && (getFirstInputSection(sec) == nullptr) &&
110247eb3f15SAndrew Ng          isDiscardable(*sec);
110347eb3f15SAndrew Ng }
110447eb3f15SAndrew Ng 
maybePropagatePhdrs(OutputSection & sec,SmallVector<StringRef,0> & phdrs)11053f5dc57fSGeorgii Rymar static void maybePropagatePhdrs(OutputSection &sec,
1106a1c2ee01SFangrui Song                                 SmallVector<StringRef, 0> &phdrs) {
11073f5dc57fSGeorgii Rymar   if (sec.phdrs.empty()) {
11083f5dc57fSGeorgii Rymar     // To match the bfd linker script behaviour, only propagate program
11093f5dc57fSGeorgii Rymar     // headers to sections that are allocated.
11103f5dc57fSGeorgii Rymar     if (sec.flags & SHF_ALLOC)
11113f5dc57fSGeorgii Rymar       sec.phdrs = phdrs;
11123f5dc57fSGeorgii Rymar   } else {
11133f5dc57fSGeorgii Rymar     phdrs = sec.phdrs;
11143f5dc57fSGeorgii Rymar   }
11153f5dc57fSGeorgii Rymar }
11163f5dc57fSGeorgii Rymar 
adjustOutputSections()1117a0318711SFangrui Song void LinkerScript::adjustOutputSections() {
11189546fffbSRafael Espindola   // If the output section contains only symbol assignments, create a
111926fa916dSGeorge Rimar   // corresponding output section. The issue is what to do with linker script
112026fa916dSGeorge Rimar   // like ".foo : { symbol = 42; }". One option would be to convert it to
112126fa916dSGeorge Rimar   // "symbol = 42;". That is, move the symbol out of the empty section
112226fa916dSGeorge Rimar   // description. That seems to be what bfd does for this simple case. The
112326fa916dSGeorge Rimar   // problem is that this is not completely general. bfd will give up and
112426fa916dSGeorge Rimar   // create a dummy section too if there is a ". = . + 1" inside the section
112526fa916dSGeorge Rimar   // for example.
112626fa916dSGeorge Rimar   // Given that we want to create the section, we have to worry what impact
112726fa916dSGeorge Rimar   // it will have on the link. For example, if we just create a section with
112826fa916dSGeorge Rimar   // 0 for flags, it would change which PT_LOADs are created.
1129ffac3ed3SFangrui Song   // We could remember that particular section is dummy and ignore it in
113026fa916dSGeorge Rimar   // other parts of the linker, but unfortunately there are quite a few places
113126fa916dSGeorge Rimar   // that would need to change:
113226fa916dSGeorge Rimar   //   * The program header creation.
113326fa916dSGeorge Rimar   //   * The orphan section placement.
113426fa916dSGeorge Rimar   //   * The address assignment.
113526fa916dSGeorge Rimar   // The other option is to pick flags that minimize the impact the section
113626fa916dSGeorge Rimar   // will have on the rest of the linker. That is why we copy the flags from
113726fa916dSGeorge Rimar   // the previous sections. Only a few flags are needed to keep the impact low.
11383837f427SRui Ueyama   uint64_t flags = SHF_ALLOC;
1139660c9ab9SRafael Espindola 
1140a1c2ee01SFangrui Song   SmallVector<StringRef, 0> defPhdrs;
11417051aeefSFangrui Song   for (SectionCommand *&cmd : sectionCommands) {
11426c814931SFangrui Song     if (!isa<OutputDesc>(cmd))
11439546fffbSRafael Espindola       continue;
11446c814931SFangrui Song     auto *sec = &cast<OutputDesc>(cmd)->osec;
11459546fffbSRafael Espindola 
1146b75d19c3SRui Ueyama     // Handle align (e.g. ".foo : ALIGN(16) { ... }").
11473837f427SRui Ueyama     if (sec->alignExpr)
11483837f427SRui Ueyama       sec->alignment =
11493837f427SRui Ueyama           std::max<uint32_t>(sec->alignment, sec->alignExpr().getValue());
1150b75d19c3SRui Ueyama 
1151fbf2f664SFangrui Song     bool isEmpty = (getFirstInputSection(sec) == nullptr);
1152fbf2f664SFangrui Song     bool discardable = isEmpty && isDiscardable(*sec);
1153fbf2f664SFangrui Song     // If sec has at least one input section and not discarded, remember its
1154fbf2f664SFangrui Song     // flags to be inherited by subsequent output sections. (sec may contain
1155fbf2f664SFangrui Song     // just one empty synthetic section.)
1156fbf2f664SFangrui Song     if (sec->hasInputSections && !discardable)
11573837f427SRui Ueyama       flags = sec->flags;
1158852bd5c0SRafael Espindola 
1159afbf90aeSGeorge Rimar     // We do not want to keep any special flags for output section
1160afbf90aeSGeorge Rimar     // in case it is empty.
11613837f427SRui Ueyama     if (isEmpty)
11623837f427SRui Ueyama       sec->flags = flags & ((sec->nonAlloc ? 0 : (uint64_t)SHF_ALLOC) |
116374780852SFangrui Song                             SHF_WRITE | SHF_EXECINSTR);
1164afbf90aeSGeorge Rimar 
11653f5dc57fSGeorgii Rymar     // The code below may remove empty output sections. We should save the
11663f5dc57fSGeorgii Rymar     // specified program headers (if exist) and propagate them to subsequent
11673f5dc57fSGeorgii Rymar     // sections which do not specify program headers.
11683f5dc57fSGeorgii Rymar     // An example of such a linker script is:
11693f5dc57fSGeorgii Rymar     // SECTIONS { .empty : { *(.empty) } :rw
11703f5dc57fSGeorgii Rymar     //            .foo : { *(.foo) } }
11713f5dc57fSGeorgii Rymar     // Note: at this point the order of output sections has not been finalized,
11723f5dc57fSGeorgii Rymar     // because orphans have not been inserted into their expected positions. We
11733f5dc57fSGeorgii Rymar     // will handle them in adjustSectionsAfterSorting().
11743f5dc57fSGeorgii Rymar     if (sec->sectionIndex != UINT32_MAX)
11753f5dc57fSGeorgii Rymar       maybePropagatePhdrs(*sec, defPhdrs);
11763f5dc57fSGeorgii Rymar 
1177fbf2f664SFangrui Song     if (discardable) {
11783837f427SRui Ueyama       sec->markDead();
11793837f427SRui Ueyama       cmd = nullptr;
11809546fffbSRafael Espindola     }
1181852bd5c0SRafael Espindola   }
1182db1a0624SGeorge Rimar 
1183db1a0624SGeorge Rimar   // It is common practice to use very generic linker scripts. So for any
1184db1a0624SGeorge Rimar   // given run some of the output sections in the script will be empty.
1185db1a0624SGeorge Rimar   // We could create corresponding empty output sections, but that would
1186db1a0624SGeorge Rimar   // clutter the output.
1187db1a0624SGeorge Rimar   // We instead remove trivially empty sections. The bfd linker seems even
1188db1a0624SGeorge Rimar   // more aggressive at removing them.
11897051aeefSFangrui Song   llvm::erase_if(sectionCommands, [&](SectionCommand *cmd) { return !cmd; });
1190f7a17448SRafael Espindola }
1191f7a17448SRafael Espindola 
adjustSectionsAfterSorting()1192b8dd23f5SRui Ueyama void LinkerScript::adjustSectionsAfterSorting() {
1193feed7506SRafael Espindola   // Try and find an appropriate memory region to assign offsets in.
1194d2dd36bbSIgor Kudrin   MemoryRegion *hint = nullptr;
11957051aeefSFangrui Song   for (SectionCommand *cmd : sectionCommands) {
11966c814931SFangrui Song     if (auto *osd = dyn_cast<OutputDesc>(cmd)) {
11976c814931SFangrui Song       OutputSection *sec = &osd->osec;
11983837f427SRui Ueyama       if (!sec->lmaRegionName.empty()) {
11993837f427SRui Ueyama         if (MemoryRegion *m = memoryRegions.lookup(sec->lmaRegionName))
12003837f427SRui Ueyama           sec->lmaRegion = m;
1201567175f3SRafael Espindola         else
12023837f427SRui Ueyama           error("memory region '" + sec->lmaRegionName + "' not declared");
1203567175f3SRafael Espindola       }
1204d2dd36bbSIgor Kudrin       std::tie(sec->memRegion, hint) = findMemoryRegion(sec, hint);
1205d1960dc0SRafael Espindola     }
1206d1960dc0SRafael Espindola   }
1207feed7506SRafael Espindola 
1208f7a17448SRafael Espindola   // If output section command doesn't specify any segments,
1209f7a17448SRafael Espindola   // and we haven't previously assigned any section to segment,
1210f7a17448SRafael Espindola   // then we simply assign section to the very first load segment.
1211f7a17448SRafael Espindola   // Below is an example of such linker script:
1212f7a17448SRafael Espindola   // PHDRS { seg PT_LOAD; }
1213f7a17448SRafael Espindola   // SECTIONS { .aaa : { *(.aaa) } }
1214a1c2ee01SFangrui Song   SmallVector<StringRef, 0> defPhdrs;
12153837f427SRui Ueyama   auto firstPtLoad = llvm::find_if(phdrsCommands, [](const PhdrsCommand &cmd) {
12163837f427SRui Ueyama     return cmd.type == PT_LOAD;
1217367bfce6SRafael Espindola   });
12183837f427SRui Ueyama   if (firstPtLoad != phdrsCommands.end())
12193837f427SRui Ueyama     defPhdrs.push_back(firstPtLoad->name);
1220f7a17448SRafael Espindola 
1221f7a17448SRafael Espindola   // Walk the commands and propagate the program headers to commands that don't
1222f7a17448SRafael Espindola   // explicitly specify them.
12237051aeefSFangrui Song   for (SectionCommand *cmd : sectionCommands)
12246c814931SFangrui Song     if (auto *osd = dyn_cast<OutputDesc>(cmd))
12256c814931SFangrui Song       maybePropagatePhdrs(osd->osec, defPhdrs);
12269546fffbSRafael Espindola }
12279546fffbSRafael Espindola 
computeBase(uint64_t min,bool allocateHeaders)12283837f427SRui Ueyama static uint64_t computeBase(uint64_t min, bool allocateHeaders) {
1229e75b42eeSRafael Espindola   // If there is no SECTIONS or if the linkerscript is explicit about program
1230e75b42eeSRafael Espindola   // headers, do our best to allocate them.
12313837f427SRui Ueyama   if (!script->hasSectionsCommand || allocateHeaders)
1232e75b42eeSRafael Espindola     return 0;
1233e75b42eeSRafael Espindola   // Otherwise only allocate program headers if that would not add a page.
12343837f427SRui Ueyama   return alignDown(min, config->maxPageSize);
1235e75b42eeSRafael Espindola }
1236e75b42eeSRafael Espindola 
123706bb7dfbSFangrui Song // When the SECTIONS command is used, try to find an address for the file and
123806bb7dfbSFangrui Song // program headers output sections, which can be added to the first PT_LOAD
123906bb7dfbSFangrui Song // segment when program headers are created.
1240b93c5b9fSPetr Hosek //
124106bb7dfbSFangrui Song // We check if the headers fit below the first allocated section. If there isn't
124206bb7dfbSFangrui Song // enough space for these sections, we'll remove them from the PT_LOAD segment,
124306bb7dfbSFangrui Song // and we'll also remove the PT_PHDR segment.
allocateHeaders(SmallVector<PhdrEntry *,0> & phdrs)1244a1c2ee01SFangrui Song void LinkerScript::allocateHeaders(SmallVector<PhdrEntry *, 0> &phdrs) {
12453837f427SRui Ueyama   uint64_t min = std::numeric_limits<uint64_t>::max();
12463837f427SRui Ueyama   for (OutputSection *sec : outputSections)
12473837f427SRui Ueyama     if (sec->flags & SHF_ALLOC)
12483837f427SRui Ueyama       min = std::min<uint64_t>(min, sec->addr);
12495aedebffSPeter Smith 
12503837f427SRui Ueyama   auto it = llvm::find_if(
12513837f427SRui Ueyama       phdrs, [](const PhdrEntry *e) { return e->p_type == PT_LOAD; });
12523837f427SRui Ueyama   if (it == phdrs.end())
1253d971e703SGeorge Rimar     return;
12543837f427SRui Ueyama   PhdrEntry *firstPTLoad = *it;
125502ed7575SRafael Espindola 
12563837f427SRui Ueyama   bool hasExplicitHeaders =
12573837f427SRui Ueyama       llvm::any_of(phdrsCommands, [](const PhdrsCommand &cmd) {
12583837f427SRui Ueyama         return cmd.hasPhdrs || cmd.hasFilehdr;
1259e75b42eeSRafael Espindola       });
12603837f427SRui Ueyama   bool paged = !config->omagic && !config->nmagic;
12613837f427SRui Ueyama   uint64_t headerSize = getHeaderSize();
12623837f427SRui Ueyama   if ((paged || hasExplicitHeaders) &&
12633837f427SRui Ueyama       headerSize <= min - computeBase(min, hasExplicitHeaders)) {
12643837f427SRui Ueyama     min = alignDown(min - headerSize, config->maxPageSize);
12653837f427SRui Ueyama     Out::elfHeader->addr = min;
12663837f427SRui Ueyama     Out::programHeaders->addr = min + Out::elfHeader->size;
1267d971e703SGeorge Rimar     return;
126802ed7575SRafael Espindola   }
126902ed7575SRafael Espindola 
1270e75b42eeSRafael Espindola   // Error if we were explicitly asked to allocate headers.
12713837f427SRui Ueyama   if (hasExplicitHeaders)
1272e75b42eeSRafael Espindola     error("could not allocate headers");
1273e75b42eeSRafael Espindola 
12743837f427SRui Ueyama   Out::elfHeader->ptLoad = nullptr;
12753837f427SRui Ueyama   Out::programHeaders->ptLoad = nullptr;
12763837f427SRui Ueyama   firstPTLoad->firstSec = findFirstSection(firstPTLoad);
127702ed7575SRafael Espindola 
12783837f427SRui Ueyama   llvm::erase_if(phdrs,
12793837f427SRui Ueyama                  [](const PhdrEntry *e) { return e->p_type == PT_PHDR; });
128002ed7575SRafael Espindola }
128102ed7575SRafael Espindola 
AddressState()1282ac27de9dSRui Ueyama LinkerScript::AddressState::AddressState() {
12833837f427SRui Ueyama   for (auto &mri : script->memoryRegions) {
12843837f427SRui Ueyama     MemoryRegion *mr = mri.second;
128592b5b980SFangrui Song     mr->curPos = (mr->origin)().getValue();
1286906e9a18SPeter Smith   }
1287906e9a18SPeter Smith }
1288906e9a18SPeter Smith 
1289f9b04fd9SGeorge Rimar // Here we assign addresses as instructed by linker script SECTIONS
1290f9b04fd9SGeorge Rimar // sub-commands. Doing that allows us to use final VA values, so here
1291f9b04fd9SGeorge Rimar // we also handle rest commands like symbol assignments and ASSERTs.
1292debcac9fSFangrui Song // Returns a symbol that has changed its section or value, or nullptr if no
1293debcac9fSFangrui Song // symbol has changed.
assignAddresses()1294debcac9fSFangrui Song const Defined *LinkerScript::assignAddresses() {
129506bb7dfbSFangrui Song   if (script->hasSectionsCommand) {
129606bb7dfbSFangrui Song     // With a linker script, assignment of addresses to headers is covered by
129706bb7dfbSFangrui Song     // allocateHeaders().
1298757d9d22SKazu Hirata     dot = config->imageBase.value_or(0);
129906bb7dfbSFangrui Song   } else {
130006bb7dfbSFangrui Song     // Assign addresses to headers right now.
130106bb7dfbSFangrui Song     dot = target->getImageBase();
130206bb7dfbSFangrui Song     Out::elfHeader->addr = dot;
130306bb7dfbSFangrui Song     Out::programHeaders->addr = dot + Out::elfHeader->size;
130406bb7dfbSFangrui Song     dot += getHeaderSize();
130506bb7dfbSFangrui Song   }
130618d19687SRui Ueyama 
13077ea662e2SFangrui Song   AddressState state;
13087ea662e2SFangrui Song   ctx = &state;
13093837f427SRui Ueyama   errorOnMissingSection = true;
131089c0f455SFangrui Song   ctx->outSec = aether;
131106f4743aSRafael Espindola 
1312debcac9fSFangrui Song   SymbolAssignmentMap oldValues = getSymbolAssignmentValues(sectionCommands);
13137051aeefSFangrui Song   for (SectionCommand *cmd : sectionCommands) {
13147051aeefSFangrui Song     if (auto *assign = dyn_cast<SymbolAssignment>(cmd)) {
13157051aeefSFangrui Song       assign->addr = dot;
13167051aeefSFangrui Song       assignSymbol(assign, false);
13177051aeefSFangrui Song       assign->size = dot - assign->addr;
131805ef4cffSRui Ueyama       continue;
1319652852c5SGeorge Rimar     }
13206c814931SFangrui Song     assignOffsets(&cast<OutputDesc>(cmd)->osec);
1321a14b13d8SGeorge Rimar   }
1322debcac9fSFangrui Song 
13233837f427SRui Ueyama   ctx = nullptr;
1324debcac9fSFangrui Song   return getChangedSymbolAssignment(oldValues);
1325fb8978fcSDima Stepanov }
1326652852c5SGeorge Rimar 
1327464daadcSRui Ueyama // Creates program headers as instructed by PHDRS linker script command.
createPhdrs()1328a1c2ee01SFangrui Song SmallVector<PhdrEntry *, 0> LinkerScript::createPhdrs() {
1329a1c2ee01SFangrui Song   SmallVector<PhdrEntry *, 0> ret;
1330bbe38602SEugene Leviant 
1331464daadcSRui Ueyama   // Process PHDRS and FILEHDR keywords because they are not
1332464daadcSRui Ueyama   // real output sections and cannot be added in the following loop.
13333837f427SRui Ueyama   for (const PhdrsCommand &cmd : phdrsCommands) {
1334757d9d22SKazu Hirata     PhdrEntry *phdr = make<PhdrEntry>(cmd.type, cmd.flags.value_or(PF_R));
1335bbe38602SEugene Leviant 
13363837f427SRui Ueyama     if (cmd.hasFilehdr)
13373837f427SRui Ueyama       phdr->add(Out::elfHeader);
13383837f427SRui Ueyama     if (cmd.hasPhdrs)
13393837f427SRui Ueyama       phdr->add(Out::programHeaders);
134056b21c86SEugene Leviant 
13413837f427SRui Ueyama     if (cmd.lmaExpr) {
13423837f427SRui Ueyama       phdr->p_paddr = cmd.lmaExpr().getValue();
13433837f427SRui Ueyama       phdr->hasLMA = true;
134456b21c86SEugene Leviant     }
13453837f427SRui Ueyama     ret.push_back(phdr);
1346bbe38602SEugene Leviant   }
1347bbe38602SEugene Leviant 
1348464daadcSRui Ueyama   // Add output sections to program headers.
13493837f427SRui Ueyama   for (OutputSection *sec : outputSections) {
1350bbe38602SEugene Leviant     // Assign headers specified by linker script
13513837f427SRui Ueyama     for (size_t id : getPhdrIndices(sec)) {
13523837f427SRui Ueyama       ret[id]->add(sec);
13535413bf1bSKazu Hirata       if (!phdrsCommands[id].flags)
13543837f427SRui Ueyama         ret[id]->p_flags |= sec->getPhdrFlags();
1355bbe38602SEugene Leviant     }
1356bbe38602SEugene Leviant   }
13573837f427SRui Ueyama   return ret;
1358bbe38602SEugene Leviant }
1359bbe38602SEugene Leviant 
1360e03ba023SRui Ueyama // Returns true if we should emit an .interp section.
1361e03ba023SRui Ueyama //
1362e03ba023SRui Ueyama // We usually do. But if PHDRS commands are given, and
1363e03ba023SRui Ueyama // no PT_INTERP is there, there's no place to emit an
1364e03ba023SRui Ueyama // .interp, so we don't do that in that case.
needsInterpSection()1365e03ba023SRui Ueyama bool LinkerScript::needsInterpSection() {
13663837f427SRui Ueyama   if (phdrsCommands.empty())
1367e03ba023SRui Ueyama     return true;
13683837f427SRui Ueyama   for (PhdrsCommand &cmd : phdrsCommands)
13693837f427SRui Ueyama     if (cmd.type == PT_INTERP)
1370e31d9886SRui Ueyama       return true;
1371e03ba023SRui Ueyama   return false;
1372f9bc3bd2SEugene Leviant }
1373f9bc3bd2SEugene Leviant 
getSymbolValue(StringRef name,const Twine & loc)13743837f427SRui Ueyama ExprValue LinkerScript::getSymbolValue(StringRef name, const Twine &loc) {
13753837f427SRui Ueyama   if (name == ".") {
13763837f427SRui Ueyama     if (ctx)
13773837f427SRui Ueyama       return {ctx->outSec, false, dot - ctx->outSec->addr, loc};
13783837f427SRui Ueyama     error(loc + ": unable to get location counter value");
13797e5b0a59SGeorge Rimar     return 0;
13807e5b0a59SGeorge Rimar   }
1381f5733500SRui Ueyama 
13823837f427SRui Ueyama   if (Symbol *sym = symtab->find(name)) {
13839670029bSFangrui Song     if (auto *ds = dyn_cast<Defined>(sym)) {
13849670029bSFangrui Song       ExprValue v{ds->section, false, ds->value, loc};
13859670029bSFangrui Song       // Retain the original st_type, so that the alias will get the same
13869670029bSFangrui Song       // behavior in relocation processing. Any operation will reset st_type to
13879670029bSFangrui Song       // STT_NOTYPE.
13889670029bSFangrui Song       v.type = ds->type;
13899670029bSFangrui Song       return v;
13909670029bSFangrui Song     }
13913837f427SRui Ueyama     if (isa<SharedSymbol>(sym))
13923837f427SRui Ueyama       if (!errorOnMissingSection)
13933837f427SRui Ueyama         return {nullptr, false, 0, loc};
1394de38b3d2SRafael Espindola   }
1395f5733500SRui Ueyama 
13963837f427SRui Ueyama   error(loc + ": symbol not found: " + name);
1397884e786dSGeorge Rimar   return 0;
1398884e786dSGeorge Rimar }
1399884e786dSGeorge Rimar 
1400656be311SRui Ueyama // Returns the index of the segment named Name.
getPhdrIndex(ArrayRef<PhdrsCommand> vec,StringRef name)14013837f427SRui Ueyama static Optional<size_t> getPhdrIndex(ArrayRef<PhdrsCommand> vec,
14023837f427SRui Ueyama                                      StringRef name) {
14033837f427SRui Ueyama   for (size_t i = 0; i < vec.size(); ++i)
14043837f427SRui Ueyama     if (vec[i].name == name)
14053837f427SRui Ueyama       return i;
1406656be311SRui Ueyama   return None;
1407656be311SRui Ueyama }
1408656be311SRui Ueyama 
14092c923c2cSRafael Espindola // Returns indices of ELF headers containing specific section. Each index is a
14102c923c2cSRafael Espindola // zero based number of ELF header listed within PHDRS {} script block.
getPhdrIndices(OutputSection * cmd)1411a1c2ee01SFangrui Song SmallVector<size_t, 0> LinkerScript::getPhdrIndices(OutputSection *cmd) {
1412a1c2ee01SFangrui Song   SmallVector<size_t, 0> ret;
1413656be311SRui Ueyama 
14143837f427SRui Ueyama   for (StringRef s : cmd->phdrs) {
14153837f427SRui Ueyama     if (Optional<size_t> idx = getPhdrIndex(phdrsCommands, s))
14163837f427SRui Ueyama       ret.push_back(*idx);
14173837f427SRui Ueyama     else if (s != "NONE")
14183ff3c698SJames Henderson       error(cmd->location + ": program header '" + s +
1419656be311SRui Ueyama             "' is not listed in PHDRS");
1420bbe38602SEugene Leviant   }
14213837f427SRui Ueyama   return ret;
142229c5a2a9SRui Ueyama }
1423