xref: /openbsd-src/gnu/llvm/lld/ELF/LinkerScript.cpp (revision c8cb675144102a1a5c89aa59e87e7efd3698a8ea)
1ece8a530Spatrick //===- LinkerScript.cpp ---------------------------------------------------===//
2ece8a530Spatrick //
3ece8a530Spatrick // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4ece8a530Spatrick // See https://llvm.org/LICENSE.txt for license information.
5ece8a530Spatrick // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6ece8a530Spatrick //
7ece8a530Spatrick //===----------------------------------------------------------------------===//
8ece8a530Spatrick //
9ece8a530Spatrick // This file contains the parser/evaluator of the linker script.
10ece8a530Spatrick //
11ece8a530Spatrick //===----------------------------------------------------------------------===//
12ece8a530Spatrick 
13ece8a530Spatrick #include "LinkerScript.h"
14ece8a530Spatrick #include "Config.h"
1505edf1c1Srobert #include "InputFiles.h"
16ece8a530Spatrick #include "InputSection.h"
17ece8a530Spatrick #include "OutputSections.h"
18ece8a530Spatrick #include "SymbolTable.h"
19ece8a530Spatrick #include "Symbols.h"
20ece8a530Spatrick #include "SyntheticSections.h"
21ece8a530Spatrick #include "Target.h"
22ece8a530Spatrick #include "Writer.h"
2305edf1c1Srobert #include "lld/Common/CommonLinkerContext.h"
24ece8a530Spatrick #include "lld/Common/Strings.h"
25ece8a530Spatrick #include "llvm/ADT/STLExtras.h"
26ece8a530Spatrick #include "llvm/ADT/StringRef.h"
27ece8a530Spatrick #include "llvm/BinaryFormat/ELF.h"
28ece8a530Spatrick #include "llvm/Support/Casting.h"
29ece8a530Spatrick #include "llvm/Support/Endian.h"
30ece8a530Spatrick #include "llvm/Support/ErrorHandling.h"
31a0747c9fSpatrick #include "llvm/Support/TimeProfiler.h"
32ece8a530Spatrick #include <algorithm>
33ece8a530Spatrick #include <cassert>
34ece8a530Spatrick #include <cstddef>
35ece8a530Spatrick #include <cstdint>
36ece8a530Spatrick #include <limits>
37ece8a530Spatrick #include <string>
38ece8a530Spatrick #include <vector>
39ece8a530Spatrick 
40ece8a530Spatrick using namespace llvm;
41ece8a530Spatrick using namespace llvm::ELF;
42ece8a530Spatrick using namespace llvm::object;
43ece8a530Spatrick using namespace llvm::support::endian;
44bb684c34Spatrick using namespace lld;
45bb684c34Spatrick using namespace lld::elf;
46ece8a530Spatrick 
4705edf1c1Srobert std::unique_ptr<LinkerScript> elf::script;
48ece8a530Spatrick 
isSectionPrefix(StringRef prefix,StringRef name)4905edf1c1Srobert static bool isSectionPrefix(StringRef prefix, StringRef name) {
5005edf1c1Srobert   return name.consume_front(prefix) && (name.empty() || name[0] == '.');
5105edf1c1Srobert }
5205edf1c1Srobert 
getOutputSectionName(const InputSectionBase * s)5305edf1c1Srobert static StringRef getOutputSectionName(const InputSectionBase *s) {
5405edf1c1Srobert   if (config->relocatable)
5505edf1c1Srobert     return s->name;
5605edf1c1Srobert 
5705edf1c1Srobert   // This is for --emit-relocs. If .text.foo is emitted as .text.bar, we want
5805edf1c1Srobert   // to emit .rela.text.foo as .rela.text.bar for consistency (this is not
5905edf1c1Srobert   // technically required, but not doing it is odd). This code guarantees that.
6005edf1c1Srobert   if (auto *isec = dyn_cast<InputSection>(s)) {
6105edf1c1Srobert     if (InputSectionBase *rel = isec->getRelocatedSection()) {
6205edf1c1Srobert       OutputSection *out = rel->getOutputSection();
6305edf1c1Srobert       if (s->type == SHT_RELA)
6405edf1c1Srobert         return saver().save(".rela" + out->name);
6505edf1c1Srobert       return saver().save(".rel" + out->name);
6605edf1c1Srobert     }
6705edf1c1Srobert   }
6805edf1c1Srobert 
6905edf1c1Srobert   // A BssSection created for a common symbol is identified as "COMMON" in
7005edf1c1Srobert   // linker scripts. It should go to .bss section.
7105edf1c1Srobert   if (s->name == "COMMON")
7205edf1c1Srobert     return ".bss";
7305edf1c1Srobert 
7405edf1c1Srobert   if (script->hasSectionsCommand)
7505edf1c1Srobert     return s->name;
7605edf1c1Srobert 
7705edf1c1Srobert   // When no SECTIONS is specified, emulate GNU ld's internal linker scripts
7805edf1c1Srobert   // by grouping sections with certain prefixes.
7905edf1c1Srobert 
8005edf1c1Srobert   // GNU ld places text sections with prefix ".text.hot.", ".text.unknown.",
8105edf1c1Srobert   // ".text.unlikely.", ".text.startup." or ".text.exit." before others.
8205edf1c1Srobert   // We provide an option -z keep-text-section-prefix to group such sections
8305edf1c1Srobert   // into separate output sections. This is more flexible. See also
8405edf1c1Srobert   // sortISDBySectionOrder().
8505edf1c1Srobert   // ".text.unknown" means the hotness of the section is unknown. When
8605edf1c1Srobert   // SampleFDO is used, if a function doesn't have sample, it could be very
8705edf1c1Srobert   // cold or it could be a new function never being sampled. Those functions
8805edf1c1Srobert   // will be kept in the ".text.unknown" section.
8905edf1c1Srobert   // ".text.split." holds symbols which are split out from functions in other
9005edf1c1Srobert   // input sections. For example, with -fsplit-machine-functions, placing the
9105edf1c1Srobert   // cold parts in .text.split instead of .text.unlikely mitigates against poor
9205edf1c1Srobert   // profile inaccuracy. Techniques such as hugepage remapping can make
9305edf1c1Srobert   // conservative decisions at the section granularity.
9405edf1c1Srobert   if (isSectionPrefix(".text", s->name)) {
9505edf1c1Srobert     if (config->zKeepTextSectionPrefix)
9605edf1c1Srobert       for (StringRef v : {".text.hot", ".text.unknown", ".text.unlikely",
9705edf1c1Srobert                           ".text.startup", ".text.exit", ".text.split"})
9805edf1c1Srobert         if (isSectionPrefix(v.substr(5), s->name.substr(5)))
9905edf1c1Srobert           return v;
10005edf1c1Srobert     return ".text";
10105edf1c1Srobert   }
10205edf1c1Srobert 
10305edf1c1Srobert   for (StringRef v :
10405edf1c1Srobert        {".data.rel.ro", ".data", ".rodata", ".bss.rel.ro", ".bss",
10505edf1c1Srobert         ".gcc_except_table", ".init_array", ".fini_array", ".tbss", ".tdata",
10605edf1c1Srobert         ".ARM.exidx", ".ARM.extab", ".ctors", ".dtors",
107*c8cb6751Skettenis         ".openbsd.randomdata", ".openbsd.mutable"})
10805edf1c1Srobert     if (isSectionPrefix(v, s->name))
10905edf1c1Srobert       return v;
11005edf1c1Srobert 
11105edf1c1Srobert   return s->name;
112ece8a530Spatrick }
113ece8a530Spatrick 
getValue() const114ece8a530Spatrick uint64_t ExprValue::getValue() const {
115ece8a530Spatrick   if (sec)
11605edf1c1Srobert     return alignToPowerOf2(sec->getOutputSection()->addr + sec->getOffset(val),
117ece8a530Spatrick                            alignment);
11805edf1c1Srobert   return alignToPowerOf2(val, alignment);
119ece8a530Spatrick }
120ece8a530Spatrick 
getSecAddr() const121ece8a530Spatrick uint64_t ExprValue::getSecAddr() const {
12205edf1c1Srobert   return sec ? sec->getOutputSection()->addr + sec->getOffset(0) : 0;
123ece8a530Spatrick }
124ece8a530Spatrick 
getSectionOffset() const125ece8a530Spatrick uint64_t ExprValue::getSectionOffset() const {
126ece8a530Spatrick   // If the alignment is trivial, we don't have to compute the full
127ece8a530Spatrick   // value to know the offset. This allows this function to succeed in
128ece8a530Spatrick   // cases where the output section is not yet known.
129ece8a530Spatrick   if (alignment == 1 && !sec)
130ece8a530Spatrick     return val;
131ece8a530Spatrick   return getValue() - getSecAddr();
132ece8a530Spatrick }
133ece8a530Spatrick 
createOutputSection(StringRef name,StringRef location)13405edf1c1Srobert OutputDesc *LinkerScript::createOutputSection(StringRef name,
135ece8a530Spatrick                                               StringRef location) {
13605edf1c1Srobert   OutputDesc *&secRef = nameToOutputSection[CachedHashStringRef(name)];
13705edf1c1Srobert   OutputDesc *sec;
13805edf1c1Srobert   if (secRef && secRef->osec.location.empty()) {
139ece8a530Spatrick     // There was a forward reference.
140ece8a530Spatrick     sec = secRef;
141ece8a530Spatrick   } else {
14205edf1c1Srobert     sec = make<OutputDesc>(name, SHT_PROGBITS, 0);
143ece8a530Spatrick     if (!secRef)
144ece8a530Spatrick       secRef = sec;
145ece8a530Spatrick   }
14605edf1c1Srobert   sec->osec.location = std::string(location);
147ece8a530Spatrick   return sec;
148ece8a530Spatrick }
149ece8a530Spatrick 
getOrCreateOutputSection(StringRef name)15005edf1c1Srobert OutputDesc *LinkerScript::getOrCreateOutputSection(StringRef name) {
15105edf1c1Srobert   OutputDesc *&cmdRef = nameToOutputSection[CachedHashStringRef(name)];
152ece8a530Spatrick   if (!cmdRef)
15305edf1c1Srobert     cmdRef = make<OutputDesc>(name, SHT_PROGBITS, 0);
154ece8a530Spatrick   return cmdRef;
155ece8a530Spatrick }
156ece8a530Spatrick 
157ece8a530Spatrick // Expands the memory region by the specified size.
expandMemoryRegion(MemoryRegion * memRegion,uint64_t size,StringRef secName)158ece8a530Spatrick static void expandMemoryRegion(MemoryRegion *memRegion, uint64_t size,
15905edf1c1Srobert                                StringRef secName) {
160ece8a530Spatrick   memRegion->curPos += size;
161bb684c34Spatrick   uint64_t newSize = memRegion->curPos - (memRegion->origin)().getValue();
162bb684c34Spatrick   uint64_t length = (memRegion->length)().getValue();
163bb684c34Spatrick   if (newSize > length)
16405edf1c1Srobert     error("section '" + secName + "' will not fit in region '" +
16505edf1c1Srobert           memRegion->name + "': overflowed by " + Twine(newSize - length) +
16605edf1c1Srobert           " bytes");
167ece8a530Spatrick }
168ece8a530Spatrick 
expandMemoryRegions(uint64_t size)169ece8a530Spatrick void LinkerScript::expandMemoryRegions(uint64_t size) {
17005edf1c1Srobert   if (state->memRegion)
17105edf1c1Srobert     expandMemoryRegion(state->memRegion, size, state->outSec->name);
172ece8a530Spatrick   // Only expand the LMARegion if it is different from memRegion.
17305edf1c1Srobert   if (state->lmaRegion && state->memRegion != state->lmaRegion)
17405edf1c1Srobert     expandMemoryRegion(state->lmaRegion, size, state->outSec->name);
175ece8a530Spatrick }
176ece8a530Spatrick 
expandOutputSection(uint64_t size)177ece8a530Spatrick void LinkerScript::expandOutputSection(uint64_t size) {
17805edf1c1Srobert   state->outSec->size += size;
179ece8a530Spatrick   expandMemoryRegions(size);
180ece8a530Spatrick }
181ece8a530Spatrick 
setDot(Expr e,const Twine & loc,bool inSec)182ece8a530Spatrick void LinkerScript::setDot(Expr e, const Twine &loc, bool inSec) {
183ece8a530Spatrick   uint64_t val = e().getValue();
184ece8a530Spatrick   if (val < dot && inSec)
185ece8a530Spatrick     error(loc + ": unable to move location counter backward for: " +
18605edf1c1Srobert           state->outSec->name);
187ece8a530Spatrick 
188ece8a530Spatrick   // Update to location counter means update to section size.
189ece8a530Spatrick   if (inSec)
190ece8a530Spatrick     expandOutputSection(val - dot);
191ece8a530Spatrick 
192ece8a530Spatrick   dot = val;
193ece8a530Spatrick }
194ece8a530Spatrick 
195ece8a530Spatrick // Used for handling linker symbol assignments, for both finalizing
196ece8a530Spatrick // their values and doing early declarations. Returns true if symbol
197ece8a530Spatrick // should be defined from linker script.
shouldDefineSym(SymbolAssignment * cmd)198ece8a530Spatrick static bool shouldDefineSym(SymbolAssignment *cmd) {
199ece8a530Spatrick   if (cmd->name == ".")
200ece8a530Spatrick     return false;
201ece8a530Spatrick 
202ece8a530Spatrick   if (!cmd->provide)
203ece8a530Spatrick     return true;
204ece8a530Spatrick 
205ece8a530Spatrick   // If a symbol was in PROVIDE(), we need to define it only
206ece8a530Spatrick   // when it is a referenced undefined symbol.
20705edf1c1Srobert   Symbol *b = symtab.find(cmd->name);
20805edf1c1Srobert   if (b && !b->isDefined() && !b->isCommon())
209ece8a530Spatrick     return true;
210ece8a530Spatrick   return false;
211ece8a530Spatrick }
212ece8a530Spatrick 
213ece8a530Spatrick // Called by processSymbolAssignments() to assign definitions to
214ece8a530Spatrick // linker-script-defined symbols.
addSymbol(SymbolAssignment * cmd)215ece8a530Spatrick void LinkerScript::addSymbol(SymbolAssignment *cmd) {
216ece8a530Spatrick   if (!shouldDefineSym(cmd))
217ece8a530Spatrick     return;
218ece8a530Spatrick 
219ece8a530Spatrick   // Define a symbol.
220ece8a530Spatrick   ExprValue value = cmd->expression();
221ece8a530Spatrick   SectionBase *sec = value.isAbsolute() ? nullptr : value.sec;
222ece8a530Spatrick   uint8_t visibility = cmd->hidden ? STV_HIDDEN : STV_DEFAULT;
223ece8a530Spatrick 
224ece8a530Spatrick   // When this function is called, section addresses have not been
225ece8a530Spatrick   // fixed yet. So, we may or may not know the value of the RHS
226ece8a530Spatrick   // expression.
227ece8a530Spatrick   //
228ece8a530Spatrick   // For example, if an expression is `x = 42`, we know x is always 42.
229ece8a530Spatrick   // However, if an expression is `x = .`, there's no way to know its
230ece8a530Spatrick   // value at the moment.
231ece8a530Spatrick   //
232ece8a530Spatrick   // We want to set symbol values early if we can. This allows us to
233ece8a530Spatrick   // use symbols as variables in linker scripts. Doing so allows us to
234ece8a530Spatrick   // write expressions like this: `alignment = 16; . = ALIGN(., alignment)`.
235ece8a530Spatrick   uint64_t symValue = value.sec ? 0 : value.getValue();
236ece8a530Spatrick 
237bb684c34Spatrick   Defined newSym(nullptr, cmd->name, STB_GLOBAL, visibility, value.type,
238ece8a530Spatrick                  symValue, 0, sec);
239ece8a530Spatrick 
24005edf1c1Srobert   Symbol *sym = symtab.insert(cmd->name);
241ece8a530Spatrick   sym->mergeProperties(newSym);
24205edf1c1Srobert   newSym.overwrite(*sym);
24305edf1c1Srobert   sym->isUsedInRegularObj = true;
244ece8a530Spatrick   cmd->sym = cast<Defined>(sym);
245ece8a530Spatrick }
246ece8a530Spatrick 
247ece8a530Spatrick // This function is called from LinkerScript::declareSymbols.
248ece8a530Spatrick // It creates a placeholder symbol if needed.
declareSymbol(SymbolAssignment * cmd)249ece8a530Spatrick static void declareSymbol(SymbolAssignment *cmd) {
250ece8a530Spatrick   if (!shouldDefineSym(cmd))
251ece8a530Spatrick     return;
252ece8a530Spatrick 
253ece8a530Spatrick   uint8_t visibility = cmd->hidden ? STV_HIDDEN : STV_DEFAULT;
254ece8a530Spatrick   Defined newSym(nullptr, cmd->name, STB_GLOBAL, visibility, STT_NOTYPE, 0, 0,
255ece8a530Spatrick                  nullptr);
256ece8a530Spatrick 
257ece8a530Spatrick   // We can't calculate final value right now.
25805edf1c1Srobert   Symbol *sym = symtab.insert(cmd->name);
259ece8a530Spatrick   sym->mergeProperties(newSym);
26005edf1c1Srobert   newSym.overwrite(*sym);
261ece8a530Spatrick 
262ece8a530Spatrick   cmd->sym = cast<Defined>(sym);
263ece8a530Spatrick   cmd->provide = false;
26405edf1c1Srobert   sym->isUsedInRegularObj = true;
265ece8a530Spatrick   sym->scriptDefined = true;
266ece8a530Spatrick }
267ece8a530Spatrick 
268ece8a530Spatrick using SymbolAssignmentMap =
269ece8a530Spatrick     DenseMap<const Defined *, std::pair<SectionBase *, uint64_t>>;
270ece8a530Spatrick 
271ece8a530Spatrick // Collect section/value pairs of linker-script-defined symbols. This is used to
272ece8a530Spatrick // check whether symbol values converge.
273ece8a530Spatrick static SymbolAssignmentMap
getSymbolAssignmentValues(ArrayRef<SectionCommand * > sectionCommands)27405edf1c1Srobert getSymbolAssignmentValues(ArrayRef<SectionCommand *> sectionCommands) {
275ece8a530Spatrick   SymbolAssignmentMap ret;
27605edf1c1Srobert   for (SectionCommand *cmd : sectionCommands) {
27705edf1c1Srobert     if (auto *assign = dyn_cast<SymbolAssignment>(cmd)) {
27805edf1c1Srobert       if (assign->sym) // sym is nullptr for dot.
27905edf1c1Srobert         ret.try_emplace(assign->sym, std::make_pair(assign->sym->section,
28005edf1c1Srobert                                                     assign->sym->value));
281ece8a530Spatrick       continue;
282ece8a530Spatrick     }
28305edf1c1Srobert     for (SectionCommand *subCmd : cast<OutputDesc>(cmd)->osec.commands)
28405edf1c1Srobert       if (auto *assign = dyn_cast<SymbolAssignment>(subCmd))
28505edf1c1Srobert         if (assign->sym)
28605edf1c1Srobert           ret.try_emplace(assign->sym, std::make_pair(assign->sym->section,
28705edf1c1Srobert                                                       assign->sym->value));
288ece8a530Spatrick   }
289ece8a530Spatrick   return ret;
290ece8a530Spatrick }
291ece8a530Spatrick 
292ece8a530Spatrick // Returns the lexicographical smallest (for determinism) Defined whose
293ece8a530Spatrick // section/value has changed.
294ece8a530Spatrick static const Defined *
getChangedSymbolAssignment(const SymbolAssignmentMap & oldValues)295ece8a530Spatrick getChangedSymbolAssignment(const SymbolAssignmentMap &oldValues) {
296ece8a530Spatrick   const Defined *changed = nullptr;
297ece8a530Spatrick   for (auto &it : oldValues) {
298ece8a530Spatrick     const Defined *sym = it.first;
299ece8a530Spatrick     if (std::make_pair(sym->section, sym->value) != it.second &&
300ece8a530Spatrick         (!changed || sym->getName() < changed->getName()))
301ece8a530Spatrick       changed = sym;
302ece8a530Spatrick   }
303ece8a530Spatrick   return changed;
304ece8a530Spatrick }
305ece8a530Spatrick 
306bb684c34Spatrick // Process INSERT [AFTER|BEFORE] commands. For each command, we move the
307bb684c34Spatrick // specified output section to the designated place.
processInsertCommands()308ece8a530Spatrick void LinkerScript::processInsertCommands() {
30905edf1c1Srobert   SmallVector<OutputDesc *, 0> moves;
310bb684c34Spatrick   for (const InsertCommand &cmd : insertCommands) {
311a0747c9fSpatrick     for (StringRef name : cmd.names) {
312a0747c9fSpatrick       // If base is empty, it may have been discarded by
31305edf1c1Srobert       // adjustOutputSections(). We do not handle such output sections.
31405edf1c1Srobert       auto from = llvm::find_if(sectionCommands, [&](SectionCommand *subCmd) {
31505edf1c1Srobert         return isa<OutputDesc>(subCmd) &&
31605edf1c1Srobert                cast<OutputDesc>(subCmd)->osec.name == name;
317a0747c9fSpatrick       });
318bb684c34Spatrick       if (from == sectionCommands.end())
319ece8a530Spatrick         continue;
32005edf1c1Srobert       moves.push_back(cast<OutputDesc>(*from));
321bb684c34Spatrick       sectionCommands.erase(from);
322a0747c9fSpatrick     }
323ece8a530Spatrick 
32405edf1c1Srobert     auto insertPos =
32505edf1c1Srobert         llvm::find_if(sectionCommands, [&cmd](SectionCommand *subCmd) {
32605edf1c1Srobert           auto *to = dyn_cast<OutputDesc>(subCmd);
32705edf1c1Srobert           return to != nullptr && to->osec.name == cmd.where;
328bb684c34Spatrick         });
329bb684c34Spatrick     if (insertPos == sectionCommands.end()) {
330a0747c9fSpatrick       error("unable to insert " + cmd.names[0] +
331bb684c34Spatrick             (cmd.isAfter ? " after " : " before ") + cmd.where);
332bb684c34Spatrick     } else {
333bb684c34Spatrick       if (cmd.isAfter)
334bb684c34Spatrick         ++insertPos;
335a0747c9fSpatrick       sectionCommands.insert(insertPos, moves.begin(), moves.end());
336bb684c34Spatrick     }
337a0747c9fSpatrick     moves.clear();
338bb684c34Spatrick   }
339ece8a530Spatrick }
340ece8a530Spatrick 
341ece8a530Spatrick // Symbols defined in script should not be inlined by LTO. At the same time
342ece8a530Spatrick // we don't know their final values until late stages of link. Here we scan
343ece8a530Spatrick // over symbol assignment commands and create placeholder symbols if needed.
declareSymbols()344ece8a530Spatrick void LinkerScript::declareSymbols() {
34505edf1c1Srobert   assert(!state);
34605edf1c1Srobert   for (SectionCommand *cmd : sectionCommands) {
34705edf1c1Srobert     if (auto *assign = dyn_cast<SymbolAssignment>(cmd)) {
34805edf1c1Srobert       declareSymbol(assign);
349ece8a530Spatrick       continue;
350ece8a530Spatrick     }
351ece8a530Spatrick 
352ece8a530Spatrick     // If the output section directive has constraints,
353ece8a530Spatrick     // we can't say for sure if it is going to be included or not.
354ece8a530Spatrick     // Skip such sections for now. Improve the checks if we ever
355ece8a530Spatrick     // need symbols from that sections to be declared early.
35605edf1c1Srobert     const OutputSection &sec = cast<OutputDesc>(cmd)->osec;
35705edf1c1Srobert     if (sec.constraint != ConstraintKind::NoConstraint)
358ece8a530Spatrick       continue;
35905edf1c1Srobert     for (SectionCommand *cmd : sec.commands)
36005edf1c1Srobert       if (auto *assign = dyn_cast<SymbolAssignment>(cmd))
36105edf1c1Srobert         declareSymbol(assign);
362ece8a530Spatrick   }
363ece8a530Spatrick }
364ece8a530Spatrick 
365ece8a530Spatrick // This function is called from assignAddresses, while we are
366ece8a530Spatrick // fixing the output section addresses. This function is supposed
367ece8a530Spatrick // to set the final value for a given symbol assignment.
assignSymbol(SymbolAssignment * cmd,bool inSec)368ece8a530Spatrick void LinkerScript::assignSymbol(SymbolAssignment *cmd, bool inSec) {
369ece8a530Spatrick   if (cmd->name == ".") {
370ece8a530Spatrick     setDot(cmd->expression, cmd->location, inSec);
371ece8a530Spatrick     return;
372ece8a530Spatrick   }
373ece8a530Spatrick 
374ece8a530Spatrick   if (!cmd->sym)
375ece8a530Spatrick     return;
376ece8a530Spatrick 
377ece8a530Spatrick   ExprValue v = cmd->expression();
378ece8a530Spatrick   if (v.isAbsolute()) {
379ece8a530Spatrick     cmd->sym->section = nullptr;
380ece8a530Spatrick     cmd->sym->value = v.getValue();
381ece8a530Spatrick   } else {
382ece8a530Spatrick     cmd->sym->section = v.sec;
383ece8a530Spatrick     cmd->sym->value = v.getSectionOffset();
384ece8a530Spatrick   }
385bb684c34Spatrick   cmd->sym->type = v.type;
386ece8a530Spatrick }
387ece8a530Spatrick 
getFilename(const InputFile * file)388a0747c9fSpatrick static inline StringRef getFilename(const InputFile *file) {
389a0747c9fSpatrick   return file ? file->getNameForScript() : StringRef();
390a0747c9fSpatrick }
391a0747c9fSpatrick 
matchesFile(const InputFile * file) const392a0747c9fSpatrick bool InputSectionDescription::matchesFile(const InputFile *file) const {
393a0747c9fSpatrick   if (filePat.isTrivialMatchAll())
394a0747c9fSpatrick     return true;
395a0747c9fSpatrick 
396a0747c9fSpatrick   if (!matchesFileCache || matchesFileCache->first != file)
397a0747c9fSpatrick     matchesFileCache.emplace(file, filePat.match(getFilename(file)));
398a0747c9fSpatrick 
399a0747c9fSpatrick   return matchesFileCache->second;
400a0747c9fSpatrick }
401a0747c9fSpatrick 
excludesFile(const InputFile * file) const402a0747c9fSpatrick bool SectionPattern::excludesFile(const InputFile *file) const {
403a0747c9fSpatrick   if (excludedFilePat.empty())
404a0747c9fSpatrick     return false;
405a0747c9fSpatrick 
406a0747c9fSpatrick   if (!excludesFileCache || excludesFileCache->first != file)
407a0747c9fSpatrick     excludesFileCache.emplace(file, excludedFilePat.match(getFilename(file)));
408a0747c9fSpatrick 
409a0747c9fSpatrick   return excludesFileCache->second;
410ece8a530Spatrick }
411ece8a530Spatrick 
shouldKeep(InputSectionBase * s)412ece8a530Spatrick bool LinkerScript::shouldKeep(InputSectionBase *s) {
413ece8a530Spatrick   for (InputSectionDescription *id : keptSections)
414a0747c9fSpatrick     if (id->matchesFile(s->file))
415ece8a530Spatrick       for (SectionPattern &p : id->sectionPatterns)
416bb684c34Spatrick         if (p.sectionPat.match(s->name) &&
417bb684c34Spatrick             (s->flags & id->withFlags) == id->withFlags &&
418bb684c34Spatrick             (s->flags & id->withoutFlags) == 0)
419ece8a530Spatrick           return true;
420ece8a530Spatrick   return false;
421ece8a530Spatrick }
422ece8a530Spatrick 
423ece8a530Spatrick // A helper function for the SORT() command.
matchConstraints(ArrayRef<InputSectionBase * > sections,ConstraintKind kind)424ece8a530Spatrick static bool matchConstraints(ArrayRef<InputSectionBase *> sections,
425ece8a530Spatrick                              ConstraintKind kind) {
426ece8a530Spatrick   if (kind == ConstraintKind::NoConstraint)
427ece8a530Spatrick     return true;
428ece8a530Spatrick 
429ece8a530Spatrick   bool isRW = llvm::any_of(
430ece8a530Spatrick       sections, [](InputSectionBase *sec) { return sec->flags & SHF_WRITE; });
431ece8a530Spatrick 
432ece8a530Spatrick   return (isRW && kind == ConstraintKind::ReadWrite) ||
433ece8a530Spatrick          (!isRW && kind == ConstraintKind::ReadOnly);
434ece8a530Spatrick }
435ece8a530Spatrick 
sortSections(MutableArrayRef<InputSectionBase * > vec,SortSectionPolicy k)436ece8a530Spatrick static void sortSections(MutableArrayRef<InputSectionBase *> vec,
437ece8a530Spatrick                          SortSectionPolicy k) {
438ece8a530Spatrick   auto alignmentComparator = [](InputSectionBase *a, InputSectionBase *b) {
439ece8a530Spatrick     // ">" is not a mistake. Sections with larger alignments are placed
440ece8a530Spatrick     // before sections with smaller alignments in order to reduce the
441ece8a530Spatrick     // amount of padding necessary. This is compatible with GNU.
44205edf1c1Srobert     return a->addralign > b->addralign;
443ece8a530Spatrick   };
444ece8a530Spatrick   auto nameComparator = [](InputSectionBase *a, InputSectionBase *b) {
445ece8a530Spatrick     return a->name < b->name;
446ece8a530Spatrick   };
447ece8a530Spatrick   auto priorityComparator = [](InputSectionBase *a, InputSectionBase *b) {
448ece8a530Spatrick     return getPriority(a->name) < getPriority(b->name);
449ece8a530Spatrick   };
450ece8a530Spatrick 
451ece8a530Spatrick   switch (k) {
452ece8a530Spatrick   case SortSectionPolicy::Default:
453ece8a530Spatrick   case SortSectionPolicy::None:
454ece8a530Spatrick     return;
455ece8a530Spatrick   case SortSectionPolicy::Alignment:
456ece8a530Spatrick     return llvm::stable_sort(vec, alignmentComparator);
457ece8a530Spatrick   case SortSectionPolicy::Name:
458ece8a530Spatrick     return llvm::stable_sort(vec, nameComparator);
459ece8a530Spatrick   case SortSectionPolicy::Priority:
460ece8a530Spatrick     return llvm::stable_sort(vec, priorityComparator);
461ece8a530Spatrick   }
462ece8a530Spatrick }
463ece8a530Spatrick 
464ece8a530Spatrick // Sort sections as instructed by SORT-family commands and --sort-section
465ece8a530Spatrick // option. Because SORT-family commands can be nested at most two depth
466ece8a530Spatrick // (e.g. SORT_BY_NAME(SORT_BY_ALIGNMENT(.text.*))) and because the command
467ece8a530Spatrick // line option is respected even if a SORT command is given, the exact
468ece8a530Spatrick // behavior we have here is a bit complicated. Here are the rules.
469ece8a530Spatrick //
470ece8a530Spatrick // 1. If two SORT commands are given, --sort-section is ignored.
471ece8a530Spatrick // 2. If one SORT command is given, and if it is not SORT_NONE,
472ece8a530Spatrick //    --sort-section is handled as an inner SORT command.
473ece8a530Spatrick // 3. If one SORT command is given, and if it is SORT_NONE, don't sort.
474ece8a530Spatrick // 4. If no SORT command is given, sort according to --sort-section.
sortInputSections(MutableArrayRef<InputSectionBase * > vec,SortSectionPolicy outer,SortSectionPolicy inner)475ece8a530Spatrick static void sortInputSections(MutableArrayRef<InputSectionBase *> vec,
476a0747c9fSpatrick                               SortSectionPolicy outer,
477a0747c9fSpatrick                               SortSectionPolicy inner) {
478a0747c9fSpatrick   if (outer == SortSectionPolicy::None)
479ece8a530Spatrick     return;
480ece8a530Spatrick 
481a0747c9fSpatrick   if (inner == SortSectionPolicy::Default)
482ece8a530Spatrick     sortSections(vec, config->sortSection);
483ece8a530Spatrick   else
484a0747c9fSpatrick     sortSections(vec, inner);
485a0747c9fSpatrick   sortSections(vec, outer);
486ece8a530Spatrick }
487ece8a530Spatrick 
488ece8a530Spatrick // Compute and remember which sections the InputSectionDescription matches.
48905edf1c1Srobert SmallVector<InputSectionBase *, 0>
computeInputSections(const InputSectionDescription * cmd,ArrayRef<InputSectionBase * > sections)490bb684c34Spatrick LinkerScript::computeInputSections(const InputSectionDescription *cmd,
491bb684c34Spatrick                                    ArrayRef<InputSectionBase *> sections) {
49205edf1c1Srobert   SmallVector<InputSectionBase *, 0> ret;
49305edf1c1Srobert   SmallVector<size_t, 0> indexes;
494a0747c9fSpatrick   DenseSet<size_t> seen;
495a0747c9fSpatrick   auto sortByPositionThenCommandLine = [&](size_t begin, size_t end) {
496a0747c9fSpatrick     llvm::sort(MutableArrayRef<size_t>(indexes).slice(begin, end - begin));
497a0747c9fSpatrick     for (size_t i = begin; i != end; ++i)
498a0747c9fSpatrick       ret[i] = sections[indexes[i]];
499a0747c9fSpatrick     sortInputSections(
500a0747c9fSpatrick         MutableArrayRef<InputSectionBase *>(ret).slice(begin, end - begin),
501a0747c9fSpatrick         config->sortSection, SortSectionPolicy::None);
502a0747c9fSpatrick   };
503ece8a530Spatrick 
504ece8a530Spatrick   // Collects all sections that satisfy constraints of Cmd.
505a0747c9fSpatrick   size_t sizeAfterPrevSort = 0;
506ece8a530Spatrick   for (const SectionPattern &pat : cmd->sectionPatterns) {
507a0747c9fSpatrick     size_t sizeBeforeCurrPat = ret.size();
508ece8a530Spatrick 
509a0747c9fSpatrick     for (size_t i = 0, e = sections.size(); i != e; ++i) {
510a0747c9fSpatrick       // Skip if the section is dead or has been matched by a previous input
511a0747c9fSpatrick       // section description or a previous pattern.
512a0747c9fSpatrick       InputSectionBase *sec = sections[i];
513a0747c9fSpatrick       if (!sec->isLive() || sec->parent || seen.contains(i))
514ece8a530Spatrick         continue;
515ece8a530Spatrick 
51605edf1c1Srobert       // For --emit-relocs we have to ignore entries like
517ece8a530Spatrick       //   .rela.dyn : { *(.rela.data) }
518ece8a530Spatrick       // which are common because they are in the default bfd script.
519ece8a530Spatrick       // We do not ignore SHT_REL[A] linker-synthesized sections here because
520ece8a530Spatrick       // want to support scripts that do custom layout for them.
521ece8a530Spatrick       if (isa<InputSection>(sec) &&
522ece8a530Spatrick           cast<InputSection>(sec)->getRelocatedSection())
523ece8a530Spatrick         continue;
524ece8a530Spatrick 
525bb684c34Spatrick       // Check the name early to improve performance in the common case.
526bb684c34Spatrick       if (!pat.sectionPat.match(sec->name))
527bb684c34Spatrick         continue;
528bb684c34Spatrick 
529a0747c9fSpatrick       if (!cmd->matchesFile(sec->file) || pat.excludesFile(sec->file) ||
530bb684c34Spatrick           (sec->flags & cmd->withFlags) != cmd->withFlags ||
531bb684c34Spatrick           (sec->flags & cmd->withoutFlags) != 0)
532ece8a530Spatrick         continue;
533ece8a530Spatrick 
534ece8a530Spatrick       ret.push_back(sec);
535a0747c9fSpatrick       indexes.push_back(i);
536a0747c9fSpatrick       seen.insert(i);
537ece8a530Spatrick     }
538ece8a530Spatrick 
539a0747c9fSpatrick     if (pat.sortOuter == SortSectionPolicy::Default)
540a0747c9fSpatrick       continue;
541a0747c9fSpatrick 
542a0747c9fSpatrick     // Matched sections are ordered by radix sort with the keys being (SORT*,
543a0747c9fSpatrick     // --sort-section, input order), where SORT* (if present) is most
544a0747c9fSpatrick     // significant.
545a0747c9fSpatrick     //
546a0747c9fSpatrick     // Matched sections between the previous SORT* and this SORT* are sorted by
547a0747c9fSpatrick     // (--sort-alignment, input order).
548a0747c9fSpatrick     sortByPositionThenCommandLine(sizeAfterPrevSort, sizeBeforeCurrPat);
549a0747c9fSpatrick     // Matched sections by this SORT* pattern are sorted using all 3 keys.
550a0747c9fSpatrick     // ret[sizeBeforeCurrPat,ret.size()) are already in the input order, so we
551a0747c9fSpatrick     // just sort by sortOuter and sortInner.
552ece8a530Spatrick     sortInputSections(
553a0747c9fSpatrick         MutableArrayRef<InputSectionBase *>(ret).slice(sizeBeforeCurrPat),
554a0747c9fSpatrick         pat.sortOuter, pat.sortInner);
555a0747c9fSpatrick     sizeAfterPrevSort = ret.size();
556ece8a530Spatrick   }
557a0747c9fSpatrick   // Matched sections after the last SORT* are sorted by (--sort-alignment,
558a0747c9fSpatrick   // input order).
559a0747c9fSpatrick   sortByPositionThenCommandLine(sizeAfterPrevSort, ret.size());
560ece8a530Spatrick   return ret;
561ece8a530Spatrick }
562ece8a530Spatrick 
discard(InputSectionBase & s)56305edf1c1Srobert void LinkerScript::discard(InputSectionBase &s) {
56405edf1c1Srobert   if (&s == in.shStrTab.get())
56505edf1c1Srobert     error("discarding " + s.name + " section is not allowed");
566ece8a530Spatrick 
56705edf1c1Srobert   s.markDead();
56805edf1c1Srobert   s.parent = nullptr;
56905edf1c1Srobert   for (InputSection *sec : s.dependentSections)
57005edf1c1Srobert     discard(*sec);
571ece8a530Spatrick }
572ece8a530Spatrick 
discardSynthetic(OutputSection & outCmd)573bb684c34Spatrick void LinkerScript::discardSynthetic(OutputSection &outCmd) {
574bb684c34Spatrick   for (Partition &part : partitions) {
575bb684c34Spatrick     if (!part.armExidx || !part.armExidx->isLive())
576bb684c34Spatrick       continue;
57705edf1c1Srobert     SmallVector<InputSectionBase *, 0> secs(
57805edf1c1Srobert         part.armExidx->exidxSections.begin(),
579bb684c34Spatrick         part.armExidx->exidxSections.end());
58005edf1c1Srobert     for (SectionCommand *cmd : outCmd.commands)
58105edf1c1Srobert       if (auto *isd = dyn_cast<InputSectionDescription>(cmd))
58205edf1c1Srobert         for (InputSectionBase *s : computeInputSections(isd, secs))
58305edf1c1Srobert           discard(*s);
584bb684c34Spatrick   }
585bb684c34Spatrick }
586bb684c34Spatrick 
58705edf1c1Srobert SmallVector<InputSectionBase *, 0>
createInputSectionList(OutputSection & outCmd)588ece8a530Spatrick LinkerScript::createInputSectionList(OutputSection &outCmd) {
58905edf1c1Srobert   SmallVector<InputSectionBase *, 0> ret;
590ece8a530Spatrick 
59105edf1c1Srobert   for (SectionCommand *cmd : outCmd.commands) {
59205edf1c1Srobert     if (auto *isd = dyn_cast<InputSectionDescription>(cmd)) {
59305edf1c1Srobert       isd->sectionBases = computeInputSections(isd, ctx.inputSections);
59405edf1c1Srobert       for (InputSectionBase *s : isd->sectionBases)
595ece8a530Spatrick         s->parent = &outCmd;
59605edf1c1Srobert       ret.insert(ret.end(), isd->sectionBases.begin(), isd->sectionBases.end());
597ece8a530Spatrick     }
598ece8a530Spatrick   }
599ece8a530Spatrick   return ret;
600ece8a530Spatrick }
601ece8a530Spatrick 
602ece8a530Spatrick // Create output sections described by SECTIONS commands.
processSectionCommands()603ece8a530Spatrick void LinkerScript::processSectionCommands() {
604a0747c9fSpatrick   auto process = [this](OutputSection *osec) {
60505edf1c1Srobert     SmallVector<InputSectionBase *, 0> v = createInputSectionList(*osec);
606ece8a530Spatrick 
607ece8a530Spatrick     // The output section name `/DISCARD/' is special.
608ece8a530Spatrick     // Any input section assigned to it is discarded.
609a0747c9fSpatrick     if (osec->name == "/DISCARD/") {
610ece8a530Spatrick       for (InputSectionBase *s : v)
61105edf1c1Srobert         discard(*s);
612a0747c9fSpatrick       discardSynthetic(*osec);
61305edf1c1Srobert       osec->commands.clear();
614a0747c9fSpatrick       return false;
615ece8a530Spatrick     }
616ece8a530Spatrick 
617ece8a530Spatrick     // This is for ONLY_IF_RO and ONLY_IF_RW. An output section directive
618ece8a530Spatrick     // ".foo : ONLY_IF_R[OW] { ... }" is handled only if all member input
619ece8a530Spatrick     // sections satisfy a given constraint. If not, a directive is handled
620ece8a530Spatrick     // as if it wasn't present from the beginning.
621ece8a530Spatrick     //
622ece8a530Spatrick     // Because we'll iterate over SectionCommands many more times, the easy
623ece8a530Spatrick     // way to "make it as if it wasn't present" is to make it empty.
624a0747c9fSpatrick     if (!matchConstraints(v, osec->constraint)) {
625ece8a530Spatrick       for (InputSectionBase *s : v)
626ece8a530Spatrick         s->parent = nullptr;
62705edf1c1Srobert       osec->commands.clear();
628a0747c9fSpatrick       return false;
629ece8a530Spatrick     }
630ece8a530Spatrick 
631ece8a530Spatrick     // Handle subalign (e.g. ".foo : SUBALIGN(32) { ... }"). If subalign
632ece8a530Spatrick     // is given, input sections are aligned to that value, whether the
633ece8a530Spatrick     // given value is larger or smaller than the original section alignment.
634a0747c9fSpatrick     if (osec->subalignExpr) {
635a0747c9fSpatrick       uint32_t subalign = osec->subalignExpr().getValue();
636ece8a530Spatrick       for (InputSectionBase *s : v)
63705edf1c1Srobert         s->addralign = subalign;
638ece8a530Spatrick     }
639ece8a530Spatrick 
640ece8a530Spatrick     // Set the partition field the same way OutputSection::recordSection()
641ece8a530Spatrick     // does. Partitions cannot be used with the SECTIONS command, so this is
642ece8a530Spatrick     // always 1.
643a0747c9fSpatrick     osec->partition = 1;
644a0747c9fSpatrick     return true;
645a0747c9fSpatrick   };
646ece8a530Spatrick 
647a0747c9fSpatrick   // Process OVERWRITE_SECTIONS first so that it can overwrite the main script
648a0747c9fSpatrick   // or orphans.
64905edf1c1Srobert   DenseMap<CachedHashStringRef, OutputDesc *> map;
650a0747c9fSpatrick   size_t i = 0;
65105edf1c1Srobert   for (OutputDesc *osd : overwriteSections) {
65205edf1c1Srobert     OutputSection *osec = &osd->osec;
65305edf1c1Srobert     if (process(osec) &&
65405edf1c1Srobert         !map.try_emplace(CachedHashStringRef(osec->name), osd).second)
655a0747c9fSpatrick       warn("OVERWRITE_SECTIONS specifies duplicate " + osec->name);
65605edf1c1Srobert   }
65705edf1c1Srobert   for (SectionCommand *&base : sectionCommands)
65805edf1c1Srobert     if (auto *osd = dyn_cast<OutputDesc>(base)) {
65905edf1c1Srobert       OutputSection *osec = &osd->osec;
66005edf1c1Srobert       if (OutputDesc *overwrite = map.lookup(CachedHashStringRef(osec->name))) {
66105edf1c1Srobert         log(overwrite->osec.location + " overwrites " + osec->name);
66205edf1c1Srobert         overwrite->osec.sectionIndex = i++;
663a0747c9fSpatrick         base = overwrite;
664a0747c9fSpatrick       } else if (process(osec)) {
665a0747c9fSpatrick         osec->sectionIndex = i++;
666ece8a530Spatrick       }
667ece8a530Spatrick     }
668a0747c9fSpatrick 
669a0747c9fSpatrick   // If an OVERWRITE_SECTIONS specified output section is not in
670a0747c9fSpatrick   // sectionCommands, append it to the end. The section will be inserted by
671a0747c9fSpatrick   // orphan placement.
67205edf1c1Srobert   for (OutputDesc *osd : overwriteSections)
67305edf1c1Srobert     if (osd->osec.partition == 1 && osd->osec.sectionIndex == UINT32_MAX)
67405edf1c1Srobert       sectionCommands.push_back(osd);
675ece8a530Spatrick }
676ece8a530Spatrick 
processSymbolAssignments()677ece8a530Spatrick void LinkerScript::processSymbolAssignments() {
678ece8a530Spatrick   // Dot outside an output section still represents a relative address, whose
679ece8a530Spatrick   // sh_shndx should not be SHN_UNDEF or SHN_ABS. Create a dummy aether section
680ece8a530Spatrick   // that fills the void outside a section. It has an index of one, which is
681ece8a530Spatrick   // indistinguishable from any other regular section index.
682ece8a530Spatrick   aether = make<OutputSection>("", 0, SHF_ALLOC);
683ece8a530Spatrick   aether->sectionIndex = 1;
684ece8a530Spatrick 
68505edf1c1Srobert   // `st` captures the local AddressState and makes it accessible deliberately.
686ece8a530Spatrick   // This is needed as there are some cases where we cannot just thread the
687ece8a530Spatrick   // current state through to a lambda function created by the script parser.
68805edf1c1Srobert   AddressState st;
68905edf1c1Srobert   state = &st;
69005edf1c1Srobert   st.outSec = aether;
691ece8a530Spatrick 
69205edf1c1Srobert   for (SectionCommand *cmd : sectionCommands) {
69305edf1c1Srobert     if (auto *assign = dyn_cast<SymbolAssignment>(cmd))
69405edf1c1Srobert       addSymbol(assign);
695ece8a530Spatrick     else
69605edf1c1Srobert       for (SectionCommand *subCmd : cast<OutputDesc>(cmd)->osec.commands)
69705edf1c1Srobert         if (auto *assign = dyn_cast<SymbolAssignment>(subCmd))
69805edf1c1Srobert           addSymbol(assign);
699ece8a530Spatrick   }
700ece8a530Spatrick 
70105edf1c1Srobert   state = nullptr;
702ece8a530Spatrick }
703ece8a530Spatrick 
findByName(ArrayRef<SectionCommand * > vec,StringRef name)70405edf1c1Srobert static OutputSection *findByName(ArrayRef<SectionCommand *> vec,
705ece8a530Spatrick                                  StringRef name) {
70605edf1c1Srobert   for (SectionCommand *cmd : vec)
70705edf1c1Srobert     if (auto *osd = dyn_cast<OutputDesc>(cmd))
70805edf1c1Srobert       if (osd->osec.name == name)
70905edf1c1Srobert         return &osd->osec;
710ece8a530Spatrick   return nullptr;
711ece8a530Spatrick }
712ece8a530Spatrick 
createSection(InputSectionBase * isec,StringRef outsecName)71305edf1c1Srobert static OutputDesc *createSection(InputSectionBase *isec, StringRef outsecName) {
71405edf1c1Srobert   OutputDesc *osd = script->createOutputSection(outsecName, "<internal>");
71505edf1c1Srobert   osd->osec.recordSection(isec);
71605edf1c1Srobert   return osd;
717ece8a530Spatrick }
718ece8a530Spatrick 
addInputSec(StringMap<TinyPtrVector<OutputSection * >> & map,InputSectionBase * isec,StringRef outsecName)71905edf1c1Srobert static OutputDesc *addInputSec(StringMap<TinyPtrVector<OutputSection *>> &map,
720ece8a530Spatrick                                InputSectionBase *isec, StringRef outsecName) {
721ece8a530Spatrick   // Sections with SHT_GROUP or SHF_GROUP attributes reach here only when the -r
722ece8a530Spatrick   // option is given. A section with SHT_GROUP defines a "section group", and
723ece8a530Spatrick   // its members have SHF_GROUP attribute. Usually these flags have already been
724ece8a530Spatrick   // stripped by InputFiles.cpp as section groups are processed and uniquified.
725ece8a530Spatrick   // However, for the -r option, we want to pass through all section groups
726ece8a530Spatrick   // as-is because adding/removing members or merging them with other groups
727ece8a530Spatrick   // change their semantics.
728ece8a530Spatrick   if (isec->type == SHT_GROUP || (isec->flags & SHF_GROUP))
729ece8a530Spatrick     return createSection(isec, outsecName);
730ece8a530Spatrick 
731ece8a530Spatrick   // Imagine .zed : { *(.foo) *(.bar) } script. Both foo and bar may have
732ece8a530Spatrick   // relocation sections .rela.foo and .rela.bar for example. Most tools do
733ece8a530Spatrick   // not allow multiple REL[A] sections for output section. Hence we
734ece8a530Spatrick   // should combine these relocation sections into single output.
735ece8a530Spatrick   // We skip synthetic sections because it can be .rela.dyn/.rela.plt or any
736ece8a530Spatrick   // other REL[A] sections created by linker itself.
737ece8a530Spatrick   if (!isa<SyntheticSection>(isec) &&
738ece8a530Spatrick       (isec->type == SHT_REL || isec->type == SHT_RELA)) {
739ece8a530Spatrick     auto *sec = cast<InputSection>(isec);
740ece8a530Spatrick     OutputSection *out = sec->getRelocatedSection()->getOutputSection();
741ece8a530Spatrick 
742ece8a530Spatrick     if (out->relocationSection) {
743ece8a530Spatrick       out->relocationSection->recordSection(sec);
744ece8a530Spatrick       return nullptr;
745ece8a530Spatrick     }
746ece8a530Spatrick 
74705edf1c1Srobert     OutputDesc *osd = createSection(isec, outsecName);
74805edf1c1Srobert     out->relocationSection = &osd->osec;
74905edf1c1Srobert     return osd;
750ece8a530Spatrick   }
751ece8a530Spatrick 
752ece8a530Spatrick   //  The ELF spec just says
753ece8a530Spatrick   // ----------------------------------------------------------------
754ece8a530Spatrick   // In the first phase, input sections that match in name, type and
755ece8a530Spatrick   // attribute flags should be concatenated into single sections.
756ece8a530Spatrick   // ----------------------------------------------------------------
757ece8a530Spatrick   //
758ece8a530Spatrick   // However, it is clear that at least some flags have to be ignored for
759ece8a530Spatrick   // section merging. At the very least SHF_GROUP and SHF_COMPRESSED have to be
760ece8a530Spatrick   // ignored. We should not have two output .text sections just because one was
761ece8a530Spatrick   // in a group and another was not for example.
762ece8a530Spatrick   //
763ece8a530Spatrick   // It also seems that wording was a late addition and didn't get the
764ece8a530Spatrick   // necessary scrutiny.
765ece8a530Spatrick   //
766ece8a530Spatrick   // Merging sections with different flags is expected by some users. One
767ece8a530Spatrick   // reason is that if one file has
768ece8a530Spatrick   //
769ece8a530Spatrick   // int *const bar __attribute__((section(".foo"))) = (int *)0;
770ece8a530Spatrick   //
771ece8a530Spatrick   // gcc with -fPIC will produce a read only .foo section. But if another
772ece8a530Spatrick   // file has
773ece8a530Spatrick   //
774ece8a530Spatrick   // int zed;
775ece8a530Spatrick   // int *const bar __attribute__((section(".foo"))) = (int *)&zed;
776ece8a530Spatrick   //
777ece8a530Spatrick   // gcc with -fPIC will produce a read write section.
778ece8a530Spatrick   //
779ece8a530Spatrick   // Last but not least, when using linker script the merge rules are forced by
780ece8a530Spatrick   // the script. Unfortunately, linker scripts are name based. This means that
781ece8a530Spatrick   // expressions like *(.foo*) can refer to multiple input sections with
782ece8a530Spatrick   // different flags. We cannot put them in different output sections or we
783ece8a530Spatrick   // would produce wrong results for
784ece8a530Spatrick   //
785ece8a530Spatrick   // start = .; *(.foo.*) end = .; *(.bar)
786ece8a530Spatrick   //
787ece8a530Spatrick   // and a mapping of .foo1 and .bar1 to one section and .foo2 and .bar2 to
788ece8a530Spatrick   // another. The problem is that there is no way to layout those output
789ece8a530Spatrick   // sections such that the .foo sections are the only thing between the start
790ece8a530Spatrick   // and end symbols.
791ece8a530Spatrick   //
792ece8a530Spatrick   // Given the above issues, we instead merge sections by name and error on
793ece8a530Spatrick   // incompatible types and flags.
794ece8a530Spatrick   TinyPtrVector<OutputSection *> &v = map[outsecName];
795ece8a530Spatrick   for (OutputSection *sec : v) {
796ece8a530Spatrick     if (sec->partition != isec->partition)
797ece8a530Spatrick       continue;
798ece8a530Spatrick 
799ece8a530Spatrick     if (config->relocatable && (isec->flags & SHF_LINK_ORDER)) {
800ece8a530Spatrick       // Merging two SHF_LINK_ORDER sections with different sh_link fields will
801ece8a530Spatrick       // change their semantics, so we only merge them in -r links if they will
802ece8a530Spatrick       // end up being linked to the same output section. The casts are fine
803ece8a530Spatrick       // because everything in the map was created by the orphan placement code.
804ece8a530Spatrick       auto *firstIsec = cast<InputSectionBase>(
80505edf1c1Srobert           cast<InputSectionDescription>(sec->commands[0])->sectionBases[0]);
806bb684c34Spatrick       OutputSection *firstIsecOut =
807bb684c34Spatrick           firstIsec->flags & SHF_LINK_ORDER
808bb684c34Spatrick               ? firstIsec->getLinkOrderDep()->getOutputSection()
809bb684c34Spatrick               : nullptr;
810bb684c34Spatrick       if (firstIsecOut != isec->getLinkOrderDep()->getOutputSection())
811ece8a530Spatrick         continue;
812ece8a530Spatrick     }
813ece8a530Spatrick 
814ece8a530Spatrick     sec->recordSection(isec);
815ece8a530Spatrick     return nullptr;
816ece8a530Spatrick   }
817ece8a530Spatrick 
81805edf1c1Srobert   OutputDesc *osd = createSection(isec, outsecName);
81905edf1c1Srobert   v.push_back(&osd->osec);
82005edf1c1Srobert   return osd;
821ece8a530Spatrick }
822ece8a530Spatrick 
823ece8a530Spatrick // Add sections that didn't match any sections command.
addOrphanSections()824ece8a530Spatrick void LinkerScript::addOrphanSections() {
825ece8a530Spatrick   StringMap<TinyPtrVector<OutputSection *>> map;
82605edf1c1Srobert   SmallVector<OutputDesc *, 0> v;
827ece8a530Spatrick 
82805edf1c1Srobert   auto add = [&](InputSectionBase *s) {
829ece8a530Spatrick     if (s->isLive() && !s->parent) {
830bb684c34Spatrick       orphanSections.push_back(s);
831bb684c34Spatrick 
832ece8a530Spatrick       StringRef name = getOutputSectionName(s);
833bb684c34Spatrick       if (config->unique) {
834bb684c34Spatrick         v.push_back(createSection(s, name));
835bb684c34Spatrick       } else if (OutputSection *sec = findByName(sectionCommands, name)) {
836ece8a530Spatrick         sec->recordSection(s);
837ece8a530Spatrick       } else {
83805edf1c1Srobert         if (OutputDesc *osd = addInputSec(map, s, name))
83905edf1c1Srobert           v.push_back(osd);
840ece8a530Spatrick         assert(isa<MergeInputSection>(s) ||
841ece8a530Spatrick                s->getOutputSection()->sectionIndex == UINT32_MAX);
842ece8a530Spatrick       }
843ece8a530Spatrick     }
844ece8a530Spatrick   };
845ece8a530Spatrick 
846a0747c9fSpatrick   // For further --emit-reloc handling code we need target output section
847ece8a530Spatrick   // to be created before we create relocation output section, so we want
848ece8a530Spatrick   // to create target sections first. We do not want priority handling
849ece8a530Spatrick   // for synthetic sections because them are special.
85005edf1c1Srobert   size_t n = 0;
85105edf1c1Srobert   for (InputSectionBase *isec : ctx.inputSections) {
85205edf1c1Srobert     // Process InputSection and MergeInputSection.
85305edf1c1Srobert     if (LLVM_LIKELY(isa<InputSection>(isec)))
85405edf1c1Srobert       ctx.inputSections[n++] = isec;
85505edf1c1Srobert 
856ece8a530Spatrick     // In -r links, SHF_LINK_ORDER sections are added while adding their parent
857ece8a530Spatrick     // sections because we need to know the parent's output section before we
858ece8a530Spatrick     // can select an output section for the SHF_LINK_ORDER section.
859ece8a530Spatrick     if (config->relocatable && (isec->flags & SHF_LINK_ORDER))
860ece8a530Spatrick       continue;
861ece8a530Spatrick 
862ece8a530Spatrick     if (auto *sec = dyn_cast<InputSection>(isec))
863ece8a530Spatrick       if (InputSectionBase *rel = sec->getRelocatedSection())
864ece8a530Spatrick         if (auto *relIS = dyn_cast_or_null<InputSectionBase>(rel->parent))
865ece8a530Spatrick           add(relIS);
866ece8a530Spatrick     add(isec);
86705edf1c1Srobert     if (config->relocatable)
86805edf1c1Srobert       for (InputSectionBase *depSec : isec->dependentSections)
86905edf1c1Srobert         if (depSec->flags & SHF_LINK_ORDER)
87005edf1c1Srobert           add(depSec);
871ece8a530Spatrick   }
87205edf1c1Srobert   // Keep just InputSection.
87305edf1c1Srobert   ctx.inputSections.resize(n);
874ece8a530Spatrick 
875ece8a530Spatrick   // If no SECTIONS command was given, we should insert sections commands
876ece8a530Spatrick   // before others, so that we can handle scripts which refers them,
877ece8a530Spatrick   // for example: "foo = ABSOLUTE(ADDR(.text)));".
878ece8a530Spatrick   // When SECTIONS command is present we just add all orphans to the end.
879ece8a530Spatrick   if (hasSectionsCommand)
880ece8a530Spatrick     sectionCommands.insert(sectionCommands.end(), v.begin(), v.end());
881ece8a530Spatrick   else
882ece8a530Spatrick     sectionCommands.insert(sectionCommands.begin(), v.begin(), v.end());
883ece8a530Spatrick }
884ece8a530Spatrick 
diagnoseOrphanHandling() const885bb684c34Spatrick void LinkerScript::diagnoseOrphanHandling() const {
886a0747c9fSpatrick   llvm::TimeTraceScope timeScope("Diagnose orphan sections");
887a0747c9fSpatrick   if (config->orphanHandling == OrphanHandlingPolicy::Place)
888a0747c9fSpatrick     return;
889bb684c34Spatrick   for (const InputSectionBase *sec : orphanSections) {
890bb684c34Spatrick     // Input SHT_REL[A] retained by --emit-relocs are ignored by
891bb684c34Spatrick     // computeInputSections(). Don't warn/error.
892bb684c34Spatrick     if (isa<InputSection>(sec) &&
893bb684c34Spatrick         cast<InputSection>(sec)->getRelocatedSection())
894bb684c34Spatrick       continue;
895bb684c34Spatrick 
896bb684c34Spatrick     StringRef name = getOutputSectionName(sec);
897bb684c34Spatrick     if (config->orphanHandling == OrphanHandlingPolicy::Error)
898bb684c34Spatrick       error(toString(sec) + " is being placed in '" + name + "'");
899a0747c9fSpatrick     else
900bb684c34Spatrick       warn(toString(sec) + " is being placed in '" + name + "'");
901bb684c34Spatrick   }
902bb684c34Spatrick }
903bb684c34Spatrick 
904ece8a530Spatrick // This function searches for a memory region to place the given output
905ece8a530Spatrick // section in. If found, a pointer to the appropriate memory region is
90605edf1c1Srobert // returned in the first member of the pair. Otherwise, a nullptr is returned.
90705edf1c1Srobert // The second member of the pair is a hint that should be passed to the
90805edf1c1Srobert // subsequent call of this method.
90905edf1c1Srobert std::pair<MemoryRegion *, MemoryRegion *>
findMemoryRegion(OutputSection * sec,MemoryRegion * hint)91005edf1c1Srobert LinkerScript::findMemoryRegion(OutputSection *sec, MemoryRegion *hint) {
91105edf1c1Srobert   // Non-allocatable sections are not part of the process image.
91205edf1c1Srobert   if (!(sec->flags & SHF_ALLOC)) {
91305edf1c1Srobert     if (!sec->memoryRegionName.empty())
91405edf1c1Srobert       warn("ignoring memory region assignment for non-allocatable section '" +
91505edf1c1Srobert            sec->name + "'");
91605edf1c1Srobert     return {nullptr, nullptr};
91705edf1c1Srobert   }
91805edf1c1Srobert 
919ece8a530Spatrick   // If a memory region name was specified in the output section command,
920ece8a530Spatrick   // then try to find that region first.
921ece8a530Spatrick   if (!sec->memoryRegionName.empty()) {
922ece8a530Spatrick     if (MemoryRegion *m = memoryRegions.lookup(sec->memoryRegionName))
92305edf1c1Srobert       return {m, m};
924ece8a530Spatrick     error("memory region '" + sec->memoryRegionName + "' not declared");
92505edf1c1Srobert     return {nullptr, nullptr};
926ece8a530Spatrick   }
927ece8a530Spatrick 
928ece8a530Spatrick   // If at least one memory region is defined, all sections must
929ece8a530Spatrick   // belong to some memory region. Otherwise, we don't need to do
930ece8a530Spatrick   // anything for memory regions.
931ece8a530Spatrick   if (memoryRegions.empty())
93205edf1c1Srobert     return {nullptr, nullptr};
93305edf1c1Srobert 
93405edf1c1Srobert   // An orphan section should continue the previous memory region.
93505edf1c1Srobert   if (sec->sectionIndex == UINT32_MAX && hint)
93605edf1c1Srobert     return {hint, hint};
937ece8a530Spatrick 
938ece8a530Spatrick   // See if a region can be found by matching section flags.
939ece8a530Spatrick   for (auto &pair : memoryRegions) {
940ece8a530Spatrick     MemoryRegion *m = pair.second;
94105edf1c1Srobert     if (m->compatibleWith(sec->flags))
94205edf1c1Srobert       return {m, nullptr};
943ece8a530Spatrick   }
944ece8a530Spatrick 
945ece8a530Spatrick   // Otherwise, no suitable region was found.
946ece8a530Spatrick   error("no memory region specified for section '" + sec->name + "'");
94705edf1c1Srobert   return {nullptr, nullptr};
948ece8a530Spatrick }
949ece8a530Spatrick 
findFirstSection(PhdrEntry * load)950ece8a530Spatrick static OutputSection *findFirstSection(PhdrEntry *load) {
951ece8a530Spatrick   for (OutputSection *sec : outputSections)
952ece8a530Spatrick     if (sec->ptLoad == load)
953ece8a530Spatrick       return sec;
954ece8a530Spatrick   return nullptr;
955ece8a530Spatrick }
956ece8a530Spatrick 
957ece8a530Spatrick // This function assigns offsets to input sections and an output section
958ece8a530Spatrick // for a single sections command (e.g. ".text { *(.text); }").
assignOffsets(OutputSection * sec)959ece8a530Spatrick void LinkerScript::assignOffsets(OutputSection *sec) {
960a0747c9fSpatrick   const bool isTbss = (sec->flags & SHF_TLS) && sec->type == SHT_NOBITS;
96105edf1c1Srobert   const bool sameMemRegion = state->memRegion == sec->memRegion;
96205edf1c1Srobert   const bool prevLMARegionIsDefault = state->lmaRegion == nullptr;
963a0747c9fSpatrick   const uint64_t savedDot = dot;
96405edf1c1Srobert   state->memRegion = sec->memRegion;
96505edf1c1Srobert   state->lmaRegion = sec->lmaRegion;
966a0747c9fSpatrick 
967a0747c9fSpatrick   if (!(sec->flags & SHF_ALLOC)) {
968a0747c9fSpatrick     // Non-SHF_ALLOC sections have zero addresses.
969a0747c9fSpatrick     dot = 0;
970a0747c9fSpatrick   } else if (isTbss) {
971a0747c9fSpatrick     // Allow consecutive SHF_TLS SHT_NOBITS output sections. The address range
972a0747c9fSpatrick     // starts from the end address of the previous tbss section.
97305edf1c1Srobert     if (state->tbssAddr == 0)
97405edf1c1Srobert       state->tbssAddr = dot;
975a0747c9fSpatrick     else
97605edf1c1Srobert       dot = state->tbssAddr;
977a0747c9fSpatrick   } else {
97805edf1c1Srobert     if (state->memRegion)
97905edf1c1Srobert       dot = state->memRegion->curPos;
980a0747c9fSpatrick     if (sec->addrExpr)
981ece8a530Spatrick       setDot(sec->addrExpr, sec->location, false);
982ece8a530Spatrick 
983ece8a530Spatrick     // If the address of the section has been moved forward by an explicit
984ece8a530Spatrick     // expression so that it now starts past the current curPos of the enclosing
985ece8a530Spatrick     // region, we need to expand the current region to account for the space
986ece8a530Spatrick     // between the previous section, if any, and the start of this section.
98705edf1c1Srobert     if (state->memRegion && state->memRegion->curPos < dot)
98805edf1c1Srobert       expandMemoryRegion(state->memRegion, dot - state->memRegion->curPos,
98905edf1c1Srobert                          sec->name);
990a0747c9fSpatrick   }
991ece8a530Spatrick 
99205edf1c1Srobert   // This section was previously a call to switchTo(), but switchTo()
99305edf1c1Srobert   // was unrolled here.
99405edf1c1Srobert   // On OpenBSD, we had consistently moved the call to switchTo()
99505edf1c1Srobert   // below the next section.
99605edf1c1Srobert   state->outSec = sec;
99705edf1c1Srobert   if (sec->addrExpr && script->hasSectionsCommand) {
99805edf1c1Srobert     // The alignment is ignored.
99905edf1c1Srobert     sec->addr = dot;
100005edf1c1Srobert   } else {
100105edf1c1Srobert     // sec->alignment is the max of ALIGN and the maximum of input
100205edf1c1Srobert     // section alignments.
100305edf1c1Srobert     const uint64_t pos = dot;
100405edf1c1Srobert     dot = alignToPowerOf2(dot, sec->addralign);
100505edf1c1Srobert     sec->addr = dot;
100605edf1c1Srobert     expandMemoryRegions(dot - pos);
100705edf1c1Srobert   }
1008ece8a530Spatrick 
100905edf1c1Srobert   // state->lmaOffset is LMA minus VMA. If LMA is explicitly specified via AT()
101005edf1c1Srobert   // or AT>, recompute state->lmaOffset; otherwise, if both previous/current LMA
1011bb684c34Spatrick   // region is the default, and the two sections are in the same memory region,
1012bb684c34Spatrick   // reuse previous lmaOffset; otherwise, reset lmaOffset to 0. This emulates
1013bb684c34Spatrick   // heuristics described in
1014bb684c34Spatrick   // https://sourceware.org/binutils/docs/ld/Output-Section-LMA.html
101505edf1c1Srobert   if (sec->lmaExpr) {
101605edf1c1Srobert     state->lmaOffset = sec->lmaExpr().getValue() - dot;
101705edf1c1Srobert   } else if (MemoryRegion *mr = sec->lmaRegion) {
101805edf1c1Srobert     uint64_t lmaStart = alignToPowerOf2(mr->curPos, sec->addralign);
101905edf1c1Srobert     if (mr->curPos < lmaStart)
102005edf1c1Srobert       expandMemoryRegion(mr, lmaStart - mr->curPos, sec->name);
102105edf1c1Srobert     state->lmaOffset = lmaStart - dot;
102205edf1c1Srobert   } else if (!sameMemRegion || !prevLMARegionIsDefault) {
102305edf1c1Srobert     state->lmaOffset = 0;
102405edf1c1Srobert   }
1025bb684c34Spatrick 
102605edf1c1Srobert   // On OpenBSD, the switchTo() call was here.
1027adae0cfdSpatrick 
102805edf1c1Srobert   // Propagate state->lmaOffset to the first "non-header" section.
102905edf1c1Srobert   if (PhdrEntry *l = sec->ptLoad)
1030ece8a530Spatrick     if (sec == findFirstSection(l))
103105edf1c1Srobert       l->lmaOffset = state->lmaOffset;
1032ece8a530Spatrick 
1033ece8a530Spatrick   // We can call this method multiple times during the creation of
1034ece8a530Spatrick   // thunks and want to start over calculation each time.
1035ece8a530Spatrick   sec->size = 0;
1036ece8a530Spatrick 
1037ece8a530Spatrick   // We visited SectionsCommands from processSectionCommands to
1038ece8a530Spatrick   // layout sections. Now, we visit SectionsCommands again to fix
1039ece8a530Spatrick   // section offsets.
104005edf1c1Srobert   for (SectionCommand *cmd : sec->commands) {
1041ece8a530Spatrick     // This handles the assignments to symbol or to the dot.
104205edf1c1Srobert     if (auto *assign = dyn_cast<SymbolAssignment>(cmd)) {
104305edf1c1Srobert       assign->addr = dot;
104405edf1c1Srobert       assignSymbol(assign, true);
104505edf1c1Srobert       assign->size = dot - assign->addr;
1046ece8a530Spatrick       continue;
1047ece8a530Spatrick     }
1048ece8a530Spatrick 
1049ece8a530Spatrick     // Handle BYTE(), SHORT(), LONG(), or QUAD().
105005edf1c1Srobert     if (auto *data = dyn_cast<ByteCommand>(cmd)) {
105105edf1c1Srobert       data->offset = dot - sec->addr;
105205edf1c1Srobert       dot += data->size;
105305edf1c1Srobert       expandOutputSection(data->size);
1054ece8a530Spatrick       continue;
1055ece8a530Spatrick     }
1056ece8a530Spatrick 
1057ece8a530Spatrick     // Handle a single input section description command.
1058ece8a530Spatrick     // It calculates and assigns the offsets for each section and also
1059ece8a530Spatrick     // updates the output section size.
106005edf1c1Srobert     for (InputSection *isec : cast<InputSectionDescription>(cmd)->sections) {
106105edf1c1Srobert       assert(isec->getParent() == sec);
106205edf1c1Srobert       const uint64_t pos = dot;
106305edf1c1Srobert       dot = alignToPowerOf2(dot, isec->addralign);
106405edf1c1Srobert       isec->outSecOff = dot - sec->addr;
106505edf1c1Srobert       dot += isec->getSize();
106605edf1c1Srobert 
106705edf1c1Srobert       // Update output section size after adding each section. This is so that
106805edf1c1Srobert       // SIZEOF works correctly in the case below:
106905edf1c1Srobert       // .foo { *(.aaa) a = SIZEOF(.foo); *(.bbb) }
107005edf1c1Srobert       expandOutputSection(dot - pos);
107105edf1c1Srobert     }
1072ece8a530Spatrick   }
1073a0747c9fSpatrick 
1074a0747c9fSpatrick   // Non-SHF_ALLOC sections do not affect the addresses of other OutputSections
1075a0747c9fSpatrick   // as they are not part of the process image.
1076a0747c9fSpatrick   if (!(sec->flags & SHF_ALLOC)) {
1077a0747c9fSpatrick     dot = savedDot;
1078a0747c9fSpatrick   } else if (isTbss) {
1079a0747c9fSpatrick     // NOBITS TLS sections are similar. Additionally save the end address.
108005edf1c1Srobert     state->tbssAddr = dot;
1081a0747c9fSpatrick     dot = savedDot;
1082a0747c9fSpatrick   }
1083ece8a530Spatrick }
1084ece8a530Spatrick 
isDiscardable(const OutputSection & sec)108505edf1c1Srobert static bool isDiscardable(const OutputSection &sec) {
1086ece8a530Spatrick   if (sec.name == "/DISCARD/")
1087ece8a530Spatrick     return true;
1088ece8a530Spatrick 
1089ece8a530Spatrick   // We do not want to remove OutputSections with expressions that reference
1090ece8a530Spatrick   // symbols even if the OutputSection is empty. We want to ensure that the
1091ece8a530Spatrick   // expressions can be evaluated and report an error if they cannot.
1092ece8a530Spatrick   if (sec.expressionsUseSymbols)
1093ece8a530Spatrick     return false;
1094ece8a530Spatrick 
1095ece8a530Spatrick   // OutputSections may be referenced by name in ADDR and LOADADDR expressions,
1096ece8a530Spatrick   // as an empty Section can has a valid VMA and LMA we keep the OutputSection
1097ece8a530Spatrick   // to maintain the integrity of the other Expression.
1098ece8a530Spatrick   if (sec.usedInExpression)
1099ece8a530Spatrick     return false;
1100ece8a530Spatrick 
110105edf1c1Srobert   for (SectionCommand *cmd : sec.commands) {
110205edf1c1Srobert     if (auto assign = dyn_cast<SymbolAssignment>(cmd))
1103ece8a530Spatrick       // Don't create empty output sections just for unreferenced PROVIDE
1104ece8a530Spatrick       // symbols.
110505edf1c1Srobert       if (assign->name != "." && !assign->sym)
1106ece8a530Spatrick         continue;
1107ece8a530Spatrick 
110805edf1c1Srobert     if (!isa<InputSectionDescription>(*cmd))
1109ece8a530Spatrick       return false;
1110ece8a530Spatrick   }
1111ece8a530Spatrick   return true;
1112ece8a530Spatrick }
1113ece8a530Spatrick 
isDiscarded(const OutputSection * sec) const111405edf1c1Srobert bool LinkerScript::isDiscarded(const OutputSection *sec) const {
111505edf1c1Srobert   return hasSectionsCommand && (getFirstInputSection(sec) == nullptr) &&
111605edf1c1Srobert          isDiscardable(*sec);
111705edf1c1Srobert }
111805edf1c1Srobert 
maybePropagatePhdrs(OutputSection & sec,SmallVector<StringRef,0> & phdrs)1119a0747c9fSpatrick static void maybePropagatePhdrs(OutputSection &sec,
112005edf1c1Srobert                                 SmallVector<StringRef, 0> &phdrs) {
1121a0747c9fSpatrick   if (sec.phdrs.empty()) {
1122a0747c9fSpatrick     // To match the bfd linker script behaviour, only propagate program
1123a0747c9fSpatrick     // headers to sections that are allocated.
1124a0747c9fSpatrick     if (sec.flags & SHF_ALLOC)
1125a0747c9fSpatrick       sec.phdrs = phdrs;
1126a0747c9fSpatrick   } else {
1127a0747c9fSpatrick     phdrs = sec.phdrs;
1128a0747c9fSpatrick   }
1129a0747c9fSpatrick }
1130a0747c9fSpatrick 
adjustOutputSections()113105edf1c1Srobert void LinkerScript::adjustOutputSections() {
1132ece8a530Spatrick   // If the output section contains only symbol assignments, create a
1133ece8a530Spatrick   // corresponding output section. The issue is what to do with linker script
1134ece8a530Spatrick   // like ".foo : { symbol = 42; }". One option would be to convert it to
1135ece8a530Spatrick   // "symbol = 42;". That is, move the symbol out of the empty section
1136ece8a530Spatrick   // description. That seems to be what bfd does for this simple case. The
1137ece8a530Spatrick   // problem is that this is not completely general. bfd will give up and
1138ece8a530Spatrick   // create a dummy section too if there is a ". = . + 1" inside the section
1139ece8a530Spatrick   // for example.
1140ece8a530Spatrick   // Given that we want to create the section, we have to worry what impact
1141ece8a530Spatrick   // it will have on the link. For example, if we just create a section with
1142ece8a530Spatrick   // 0 for flags, it would change which PT_LOADs are created.
1143ece8a530Spatrick   // We could remember that particular section is dummy and ignore it in
1144ece8a530Spatrick   // other parts of the linker, but unfortunately there are quite a few places
1145ece8a530Spatrick   // that would need to change:
1146ece8a530Spatrick   //   * The program header creation.
1147ece8a530Spatrick   //   * The orphan section placement.
1148ece8a530Spatrick   //   * The address assignment.
1149ece8a530Spatrick   // The other option is to pick flags that minimize the impact the section
1150ece8a530Spatrick   // will have on the rest of the linker. That is why we copy the flags from
1151ece8a530Spatrick   // the previous sections. Only a few flags are needed to keep the impact low.
1152ece8a530Spatrick   uint64_t flags = SHF_ALLOC;
1153ece8a530Spatrick 
115405edf1c1Srobert   SmallVector<StringRef, 0> defPhdrs;
115505edf1c1Srobert   for (SectionCommand *&cmd : sectionCommands) {
115605edf1c1Srobert     if (!isa<OutputDesc>(cmd))
1157ece8a530Spatrick       continue;
115805edf1c1Srobert     auto *sec = &cast<OutputDesc>(cmd)->osec;
1159ece8a530Spatrick 
1160ece8a530Spatrick     // Handle align (e.g. ".foo : ALIGN(16) { ... }").
1161ece8a530Spatrick     if (sec->alignExpr)
116205edf1c1Srobert       sec->addralign =
116305edf1c1Srobert           std::max<uint32_t>(sec->addralign, sec->alignExpr().getValue());
1164ece8a530Spatrick 
116505edf1c1Srobert     bool isEmpty = (getFirstInputSection(sec) == nullptr);
116605edf1c1Srobert     bool discardable = isEmpty && isDiscardable(*sec);
116705edf1c1Srobert     // If sec has at least one input section and not discarded, remember its
116805edf1c1Srobert     // flags to be inherited by subsequent output sections. (sec may contain
116905edf1c1Srobert     // just one empty synthetic section.)
117005edf1c1Srobert     if (sec->hasInputSections && !discardable)
1171ece8a530Spatrick       flags = sec->flags;
1172ece8a530Spatrick 
1173ece8a530Spatrick     // We do not want to keep any special flags for output section
1174ece8a530Spatrick     // in case it is empty.
1175ece8a530Spatrick     if (isEmpty)
1176ece8a530Spatrick       sec->flags = flags & ((sec->nonAlloc ? 0 : (uint64_t)SHF_ALLOC) |
1177ece8a530Spatrick                             SHF_WRITE | SHF_EXECINSTR);
1178ece8a530Spatrick 
1179a0747c9fSpatrick     // The code below may remove empty output sections. We should save the
1180a0747c9fSpatrick     // specified program headers (if exist) and propagate them to subsequent
1181a0747c9fSpatrick     // sections which do not specify program headers.
1182a0747c9fSpatrick     // An example of such a linker script is:
1183a0747c9fSpatrick     // SECTIONS { .empty : { *(.empty) } :rw
1184a0747c9fSpatrick     //            .foo : { *(.foo) } }
1185a0747c9fSpatrick     // Note: at this point the order of output sections has not been finalized,
1186a0747c9fSpatrick     // because orphans have not been inserted into their expected positions. We
1187a0747c9fSpatrick     // will handle them in adjustSectionsAfterSorting().
1188a0747c9fSpatrick     if (sec->sectionIndex != UINT32_MAX)
1189a0747c9fSpatrick       maybePropagatePhdrs(*sec, defPhdrs);
1190a0747c9fSpatrick 
119105edf1c1Srobert     if (discardable) {
1192ece8a530Spatrick       sec->markDead();
1193ece8a530Spatrick       cmd = nullptr;
1194ece8a530Spatrick     }
1195ece8a530Spatrick   }
1196ece8a530Spatrick 
1197ece8a530Spatrick   // It is common practice to use very generic linker scripts. So for any
1198ece8a530Spatrick   // given run some of the output sections in the script will be empty.
1199ece8a530Spatrick   // We could create corresponding empty output sections, but that would
1200ece8a530Spatrick   // clutter the output.
1201ece8a530Spatrick   // We instead remove trivially empty sections. The bfd linker seems even
1202ece8a530Spatrick   // more aggressive at removing them.
120305edf1c1Srobert   llvm::erase_if(sectionCommands, [&](SectionCommand *cmd) { return !cmd; });
1204ece8a530Spatrick }
1205ece8a530Spatrick 
adjustSectionsAfterSorting()1206ece8a530Spatrick void LinkerScript::adjustSectionsAfterSorting() {
1207ece8a530Spatrick   // Try and find an appropriate memory region to assign offsets in.
120805edf1c1Srobert   MemoryRegion *hint = nullptr;
120905edf1c1Srobert   for (SectionCommand *cmd : sectionCommands) {
121005edf1c1Srobert     if (auto *osd = dyn_cast<OutputDesc>(cmd)) {
121105edf1c1Srobert       OutputSection *sec = &osd->osec;
1212ece8a530Spatrick       if (!sec->lmaRegionName.empty()) {
1213ece8a530Spatrick         if (MemoryRegion *m = memoryRegions.lookup(sec->lmaRegionName))
1214ece8a530Spatrick           sec->lmaRegion = m;
1215ece8a530Spatrick         else
1216ece8a530Spatrick           error("memory region '" + sec->lmaRegionName + "' not declared");
1217ece8a530Spatrick       }
121805edf1c1Srobert       std::tie(sec->memRegion, hint) = findMemoryRegion(sec, hint);
1219ece8a530Spatrick     }
1220ece8a530Spatrick   }
1221ece8a530Spatrick 
1222ece8a530Spatrick   // If output section command doesn't specify any segments,
1223ece8a530Spatrick   // and we haven't previously assigned any section to segment,
1224ece8a530Spatrick   // then we simply assign section to the very first load segment.
1225ece8a530Spatrick   // Below is an example of such linker script:
1226ece8a530Spatrick   // PHDRS { seg PT_LOAD; }
1227ece8a530Spatrick   // SECTIONS { .aaa : { *(.aaa) } }
122805edf1c1Srobert   SmallVector<StringRef, 0> defPhdrs;
1229ece8a530Spatrick   auto firstPtLoad = llvm::find_if(phdrsCommands, [](const PhdrsCommand &cmd) {
1230ece8a530Spatrick     return cmd.type == PT_LOAD;
1231ece8a530Spatrick   });
1232ece8a530Spatrick   if (firstPtLoad != phdrsCommands.end())
1233ece8a530Spatrick     defPhdrs.push_back(firstPtLoad->name);
1234ece8a530Spatrick 
1235ece8a530Spatrick   // Walk the commands and propagate the program headers to commands that don't
1236ece8a530Spatrick   // explicitly specify them.
123705edf1c1Srobert   for (SectionCommand *cmd : sectionCommands)
123805edf1c1Srobert     if (auto *osd = dyn_cast<OutputDesc>(cmd))
123905edf1c1Srobert       maybePropagatePhdrs(osd->osec, defPhdrs);
1240ece8a530Spatrick }
1241ece8a530Spatrick 
computeBase(uint64_t min,bool allocateHeaders)1242ece8a530Spatrick static uint64_t computeBase(uint64_t min, bool allocateHeaders) {
1243ece8a530Spatrick   // If there is no SECTIONS or if the linkerscript is explicit about program
1244ece8a530Spatrick   // headers, do our best to allocate them.
1245ece8a530Spatrick   if (!script->hasSectionsCommand || allocateHeaders)
1246ece8a530Spatrick     return 0;
1247ece8a530Spatrick   // Otherwise only allocate program headers if that would not add a page.
1248ece8a530Spatrick   return alignDown(min, config->maxPageSize);
1249ece8a530Spatrick }
1250ece8a530Spatrick 
1251ece8a530Spatrick // When the SECTIONS command is used, try to find an address for the file and
1252ece8a530Spatrick // program headers output sections, which can be added to the first PT_LOAD
1253ece8a530Spatrick // segment when program headers are created.
1254ece8a530Spatrick //
1255ece8a530Spatrick // We check if the headers fit below the first allocated section. If there isn't
1256ece8a530Spatrick // enough space for these sections, we'll remove them from the PT_LOAD segment,
1257ece8a530Spatrick // and we'll also remove the PT_PHDR segment.
allocateHeaders(SmallVector<PhdrEntry *,0> & phdrs)125805edf1c1Srobert void LinkerScript::allocateHeaders(SmallVector<PhdrEntry *, 0> &phdrs) {
1259ece8a530Spatrick   uint64_t min = std::numeric_limits<uint64_t>::max();
1260ece8a530Spatrick   for (OutputSection *sec : outputSections)
1261ece8a530Spatrick     if (sec->flags & SHF_ALLOC)
1262ece8a530Spatrick       min = std::min<uint64_t>(min, sec->addr);
1263ece8a530Spatrick 
1264ece8a530Spatrick   auto it = llvm::find_if(
1265ece8a530Spatrick       phdrs, [](const PhdrEntry *e) { return e->p_type == PT_LOAD; });
1266ece8a530Spatrick   if (it == phdrs.end())
1267ece8a530Spatrick     return;
1268ece8a530Spatrick   PhdrEntry *firstPTLoad = *it;
1269ece8a530Spatrick 
1270ece8a530Spatrick   bool hasExplicitHeaders =
1271ece8a530Spatrick       llvm::any_of(phdrsCommands, [](const PhdrsCommand &cmd) {
1272ece8a530Spatrick         return cmd.hasPhdrs || cmd.hasFilehdr;
1273ece8a530Spatrick       });
1274ece8a530Spatrick   bool paged = !config->omagic && !config->nmagic;
1275ece8a530Spatrick   uint64_t headerSize = getHeaderSize();
1276ece8a530Spatrick   if ((paged || hasExplicitHeaders) &&
1277ece8a530Spatrick       headerSize <= min - computeBase(min, hasExplicitHeaders)) {
1278ece8a530Spatrick     min = alignDown(min - headerSize, config->maxPageSize);
1279ece8a530Spatrick     Out::elfHeader->addr = min;
1280ece8a530Spatrick     Out::programHeaders->addr = min + Out::elfHeader->size;
1281ece8a530Spatrick     return;
1282ece8a530Spatrick   }
1283ece8a530Spatrick 
1284ece8a530Spatrick   // Error if we were explicitly asked to allocate headers.
1285ece8a530Spatrick   if (hasExplicitHeaders)
1286ece8a530Spatrick     error("could not allocate headers");
1287ece8a530Spatrick 
1288ece8a530Spatrick   Out::elfHeader->ptLoad = nullptr;
1289ece8a530Spatrick   Out::programHeaders->ptLoad = nullptr;
1290ece8a530Spatrick   firstPTLoad->firstSec = findFirstSection(firstPTLoad);
1291ece8a530Spatrick 
1292ece8a530Spatrick   llvm::erase_if(phdrs,
1293ece8a530Spatrick                  [](const PhdrEntry *e) { return e->p_type == PT_PHDR; });
1294ece8a530Spatrick }
1295ece8a530Spatrick 
AddressState()1296ece8a530Spatrick LinkerScript::AddressState::AddressState() {
1297ece8a530Spatrick   for (auto &mri : script->memoryRegions) {
1298ece8a530Spatrick     MemoryRegion *mr = mri.second;
1299bb684c34Spatrick     mr->curPos = (mr->origin)().getValue();
1300ece8a530Spatrick   }
1301ece8a530Spatrick }
1302ece8a530Spatrick 
1303ece8a530Spatrick // Here we assign addresses as instructed by linker script SECTIONS
1304ece8a530Spatrick // sub-commands. Doing that allows us to use final VA values, so here
1305ece8a530Spatrick // we also handle rest commands like symbol assignments and ASSERTs.
1306ece8a530Spatrick // Returns a symbol that has changed its section or value, or nullptr if no
1307ece8a530Spatrick // symbol has changed.
assignAddresses()1308ece8a530Spatrick const Defined *LinkerScript::assignAddresses() {
1309ece8a530Spatrick   if (script->hasSectionsCommand) {
1310ece8a530Spatrick     // With a linker script, assignment of addresses to headers is covered by
1311ece8a530Spatrick     // allocateHeaders().
131205edf1c1Srobert     dot = config->imageBase.value_or(0);
1313ece8a530Spatrick   } else {
1314ece8a530Spatrick     // Assign addresses to headers right now.
1315ece8a530Spatrick     dot = target->getImageBase();
1316ece8a530Spatrick     Out::elfHeader->addr = dot;
1317ece8a530Spatrick     Out::programHeaders->addr = dot + Out::elfHeader->size;
1318ece8a530Spatrick     dot += getHeaderSize();
1319ece8a530Spatrick   }
1320ece8a530Spatrick 
132105edf1c1Srobert   AddressState st;
132205edf1c1Srobert   state = &st;
1323ece8a530Spatrick   errorOnMissingSection = true;
132405edf1c1Srobert   st.outSec = aether;
1325ece8a530Spatrick 
1326ece8a530Spatrick   SymbolAssignmentMap oldValues = getSymbolAssignmentValues(sectionCommands);
132705edf1c1Srobert   for (SectionCommand *cmd : sectionCommands) {
132805edf1c1Srobert     if (auto *assign = dyn_cast<SymbolAssignment>(cmd)) {
132905edf1c1Srobert       assign->addr = dot;
133005edf1c1Srobert       assignSymbol(assign, false);
133105edf1c1Srobert       assign->size = dot - assign->addr;
1332ece8a530Spatrick       continue;
1333ece8a530Spatrick     }
133405edf1c1Srobert     assignOffsets(&cast<OutputDesc>(cmd)->osec);
1335ece8a530Spatrick   }
1336ece8a530Spatrick 
133705edf1c1Srobert   state = nullptr;
1338ece8a530Spatrick   return getChangedSymbolAssignment(oldValues);
1339ece8a530Spatrick }
1340ece8a530Spatrick 
1341ece8a530Spatrick // Creates program headers as instructed by PHDRS linker script command.
createPhdrs()134205edf1c1Srobert SmallVector<PhdrEntry *, 0> LinkerScript::createPhdrs() {
134305edf1c1Srobert   SmallVector<PhdrEntry *, 0> ret;
1344ece8a530Spatrick 
1345ece8a530Spatrick   // Process PHDRS and FILEHDR keywords because they are not
1346ece8a530Spatrick   // real output sections and cannot be added in the following loop.
1347ece8a530Spatrick   for (const PhdrsCommand &cmd : phdrsCommands) {
134805edf1c1Srobert     PhdrEntry *phdr = make<PhdrEntry>(cmd.type, cmd.flags.value_or(PF_R));
1349ece8a530Spatrick 
1350ece8a530Spatrick     if (cmd.hasFilehdr)
1351ece8a530Spatrick       phdr->add(Out::elfHeader);
1352ece8a530Spatrick     if (cmd.hasPhdrs)
1353ece8a530Spatrick       phdr->add(Out::programHeaders);
1354ece8a530Spatrick 
1355ece8a530Spatrick     if (cmd.lmaExpr) {
1356ece8a530Spatrick       phdr->p_paddr = cmd.lmaExpr().getValue();
1357ece8a530Spatrick       phdr->hasLMA = true;
1358ece8a530Spatrick     }
1359ece8a530Spatrick     ret.push_back(phdr);
1360ece8a530Spatrick   }
1361ece8a530Spatrick 
1362ece8a530Spatrick   // Add output sections to program headers.
1363ece8a530Spatrick   for (OutputSection *sec : outputSections) {
1364ece8a530Spatrick     // Assign headers specified by linker script
1365ece8a530Spatrick     for (size_t id : getPhdrIndices(sec)) {
1366ece8a530Spatrick       ret[id]->add(sec);
136705edf1c1Srobert       if (!phdrsCommands[id].flags)
1368ece8a530Spatrick         ret[id]->p_flags |= sec->getPhdrFlags();
1369ece8a530Spatrick     }
1370ece8a530Spatrick   }
1371ece8a530Spatrick   return ret;
1372ece8a530Spatrick }
1373ece8a530Spatrick 
1374ece8a530Spatrick // Returns true if we should emit an .interp section.
1375ece8a530Spatrick //
1376ece8a530Spatrick // We usually do. But if PHDRS commands are given, and
1377ece8a530Spatrick // no PT_INTERP is there, there's no place to emit an
1378ece8a530Spatrick // .interp, so we don't do that in that case.
needsInterpSection()1379ece8a530Spatrick bool LinkerScript::needsInterpSection() {
1380ece8a530Spatrick   if (phdrsCommands.empty())
1381ece8a530Spatrick     return true;
1382ece8a530Spatrick   for (PhdrsCommand &cmd : phdrsCommands)
1383ece8a530Spatrick     if (cmd.type == PT_INTERP)
1384ece8a530Spatrick       return true;
1385ece8a530Spatrick   return false;
1386ece8a530Spatrick }
1387ece8a530Spatrick 
getSymbolValue(StringRef name,const Twine & loc)1388ece8a530Spatrick ExprValue LinkerScript::getSymbolValue(StringRef name, const Twine &loc) {
1389ece8a530Spatrick   if (name == ".") {
139005edf1c1Srobert     if (state)
139105edf1c1Srobert       return {state->outSec, false, dot - state->outSec->addr, loc};
1392ece8a530Spatrick     error(loc + ": unable to get location counter value");
1393ece8a530Spatrick     return 0;
1394ece8a530Spatrick   }
1395ece8a530Spatrick 
139605edf1c1Srobert   if (Symbol *sym = symtab.find(name)) {
1397bb684c34Spatrick     if (auto *ds = dyn_cast<Defined>(sym)) {
1398bb684c34Spatrick       ExprValue v{ds->section, false, ds->value, loc};
1399bb684c34Spatrick       // Retain the original st_type, so that the alias will get the same
1400bb684c34Spatrick       // behavior in relocation processing. Any operation will reset st_type to
1401bb684c34Spatrick       // STT_NOTYPE.
1402bb684c34Spatrick       v.type = ds->type;
1403bb684c34Spatrick       return v;
1404bb684c34Spatrick     }
1405ece8a530Spatrick     if (isa<SharedSymbol>(sym))
1406ece8a530Spatrick       if (!errorOnMissingSection)
1407ece8a530Spatrick         return {nullptr, false, 0, loc};
1408ece8a530Spatrick   }
1409ece8a530Spatrick 
1410ece8a530Spatrick   error(loc + ": symbol not found: " + name);
1411ece8a530Spatrick   return 0;
1412ece8a530Spatrick }
1413ece8a530Spatrick 
1414ece8a530Spatrick // Returns the index of the segment named Name.
getPhdrIndex(ArrayRef<PhdrsCommand> vec,StringRef name)141505edf1c1Srobert static std::optional<size_t> getPhdrIndex(ArrayRef<PhdrsCommand> vec,
1416ece8a530Spatrick                                           StringRef name) {
1417ece8a530Spatrick   for (size_t i = 0; i < vec.size(); ++i)
1418ece8a530Spatrick     if (vec[i].name == name)
1419ece8a530Spatrick       return i;
142005edf1c1Srobert   return std::nullopt;
1421ece8a530Spatrick }
1422ece8a530Spatrick 
1423ece8a530Spatrick // Returns indices of ELF headers containing specific section. Each index is a
1424ece8a530Spatrick // zero based number of ELF header listed within PHDRS {} script block.
getPhdrIndices(OutputSection * cmd)142505edf1c1Srobert SmallVector<size_t, 0> LinkerScript::getPhdrIndices(OutputSection *cmd) {
142605edf1c1Srobert   SmallVector<size_t, 0> ret;
1427ece8a530Spatrick 
1428ece8a530Spatrick   for (StringRef s : cmd->phdrs) {
142905edf1c1Srobert     if (std::optional<size_t> idx = getPhdrIndex(phdrsCommands, s))
1430ece8a530Spatrick       ret.push_back(*idx);
1431ece8a530Spatrick     else if (s != "NONE")
1432bb684c34Spatrick       error(cmd->location + ": program header '" + s +
1433ece8a530Spatrick             "' is not listed in PHDRS");
1434ece8a530Spatrick   }
1435ece8a530Spatrick   return ret;
1436ece8a530Spatrick }
1437