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"
151ebc8ed7SRui Ueyama #include "InputSection.h"
16652852c5SGeorge Rimar #include "OutputSections.h"
17f7c5fbb1SRui Ueyama #include "SymbolTable.h"
1855518e7dSRui Ueyama #include "Symbols.h"
193fb5a6dcSGeorge Rimar #include "SyntheticSections.h"
2055b169bfSRafael Espindola #include "Target.h"
21bbe38602SEugene Leviant #include "Writer.h"
222017d52bSRui Ueyama #include "lld/Common/Memory.h"
23ee173718SRui Ueyama #include "lld/Common/Strings.h"
244f5c8c29SBob Haarman #include "lld/Common/Threads.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"
31f7c5fbb1SRui Ueyama #include "llvm/Support/FileSystem.h"
32f03f3cc1SRui Ueyama #include "llvm/Support/Path.h"
3322886a28SEugene Zelenko #include <algorithm>
3422886a28SEugene Zelenko #include <cassert>
3522886a28SEugene Zelenko #include <cstddef>
3622886a28SEugene Zelenko #include <cstdint>
3722886a28SEugene Zelenko #include <iterator>
3822886a28SEugene Zelenko #include <limits>
3922886a28SEugene Zelenko #include <string>
4022886a28SEugene Zelenko #include <vector>
41f7c5fbb1SRui Ueyama 
42f7c5fbb1SRui Ueyama using namespace llvm;
43652852c5SGeorge Rimar using namespace llvm::ELF;
441ebc8ed7SRui Ueyama using namespace llvm::object;
45e38cbab5SGeorge Rimar using namespace llvm::support::endian;
46f7c5fbb1SRui Ueyama 
47bd8cfe65SFangrui Song namespace lld {
48bd8cfe65SFangrui Song namespace elf {
49bd8cfe65SFangrui Song LinkerScript *script;
50a34da938SRui Ueyama 
515d9f419aSFangrui Song static uint64_t getOutputSectionVA(SectionBase *sec) {
525d9f419aSFangrui Song   OutputSection *os = sec->getOutputSection();
535d9f419aSFangrui Song   assert(os && "input section has no output section assigned");
545d9f419aSFangrui Song   return os ? os->addr : 0;
55608cf670SGeorge Rimar }
565d0ea70aSGeorge Rimar 
575d0ea70aSGeorge Rimar uint64_t ExprValue::getValue() const {
583837f427SRui Ueyama   if (sec)
595d9f419aSFangrui Song     return alignTo(sec->getOffset(val) + getOutputSectionVA(sec),
603837f427SRui Ueyama                    alignment);
613837f427SRui Ueyama   return alignTo(val, alignment);
6272dc195dSRafael Espindola }
6372dc195dSRafael Espindola 
647ba5f47eSRafael Espindola uint64_t ExprValue::getSecAddr() const {
653837f427SRui Ueyama   if (sec)
665d9f419aSFangrui Song     return sec->getOffset(0) + getOutputSectionVA(sec);
677ba5f47eSRafael Espindola   return 0;
687ba5f47eSRafael Espindola }
697ba5f47eSRafael Espindola 
70a6acd23cSRafael Espindola uint64_t ExprValue::getSectionOffset() const {
718b250344SRafael Espindola   // If the alignment is trivial, we don't have to compute the full
728b250344SRafael Espindola   // value to know the offset. This allows this function to succeed in
738b250344SRafael Espindola   // cases where the output section is not yet known.
745d9f419aSFangrui Song   if (alignment == 1 && !sec)
753837f427SRui Ueyama     return val;
76a6acd23cSRafael Espindola   return getValue() - getSecAddr();
77a6acd23cSRafael Espindola }
78a6acd23cSRafael Espindola 
793837f427SRui Ueyama OutputSection *LinkerScript::createOutputSection(StringRef name,
803837f427SRui Ueyama                                                  StringRef location) {
813837f427SRui Ueyama   OutputSection *&secRef = nameToOutputSection[name];
823837f427SRui Ueyama   OutputSection *sec;
833837f427SRui Ueyama   if (secRef && secRef->location.empty()) {
8405c4f67cSRafael Espindola     // There was a forward reference.
853837f427SRui Ueyama     sec = secRef;
8605c4f67cSRafael Espindola   } else {
873837f427SRui Ueyama     sec = make<OutputSection>(name, SHT_PROGBITS, 0);
883837f427SRui Ueyama     if (!secRef)
893837f427SRui Ueyama       secRef = sec;
9005c4f67cSRafael Espindola   }
91adcd0268SBenjamin Kramer   sec->location = std::string(location);
923837f427SRui Ueyama   return sec;
93851dc1e8SGeorge Rimar }
94851dc1e8SGeorge Rimar 
953837f427SRui Ueyama OutputSection *LinkerScript::getOrCreateOutputSection(StringRef name) {
963837f427SRui Ueyama   OutputSection *&cmdRef = nameToOutputSection[name];
973837f427SRui Ueyama   if (!cmdRef)
983837f427SRui Ueyama     cmdRef = make<OutputSection>(name, SHT_PROGBITS, 0);
993837f427SRui Ueyama   return cmdRef;
100d83ce1b4SGeorge Rimar }
101d83ce1b4SGeorge Rimar 
102162d436cSGeorge Rimar // Expands the memory region by the specified size.
1033837f427SRui Ueyama static void expandMemoryRegion(MemoryRegion *memRegion, uint64_t size,
1043837f427SRui Ueyama                                StringRef regionName, StringRef secName) {
1053837f427SRui Ueyama   memRegion->curPos += size;
10692b5b980SFangrui Song   uint64_t newSize = memRegion->curPos - (memRegion->origin)().getValue();
10792b5b980SFangrui Song   uint64_t length = (memRegion->length)().getValue();
10892b5b980SFangrui Song   if (newSize > length)
1093837f427SRui Ueyama     error("section '" + secName + "' will not fit in region '" + regionName +
11092b5b980SFangrui Song           "': overflowed by " + Twine(newSize - length) + " bytes");
111162d436cSGeorge Rimar }
112162d436cSGeorge Rimar 
1133837f427SRui Ueyama void LinkerScript::expandMemoryRegions(uint64_t size) {
1143837f427SRui Ueyama   if (ctx->memRegion)
1153837f427SRui Ueyama     expandMemoryRegion(ctx->memRegion, size, ctx->memRegion->name,
1163837f427SRui Ueyama                        ctx->outSec->name);
11747cfe8f3SFangrui Song   // Only expand the LMARegion if it is different from memRegion.
1183837f427SRui Ueyama   if (ctx->lmaRegion && ctx->memRegion != ctx->lmaRegion)
1193837f427SRui Ueyama     expandMemoryRegion(ctx->lmaRegion, size, ctx->lmaRegion->name,
1203837f427SRui Ueyama                        ctx->outSec->name);
121162d436cSGeorge Rimar }
122162d436cSGeorge Rimar 
1233837f427SRui Ueyama void LinkerScript::expandOutputSection(uint64_t size) {
1243837f427SRui Ueyama   ctx->outSec->size += size;
1253837f427SRui Ueyama   expandMemoryRegions(size);
126a6ce78ecSGeorge Rimar }
127a6ce78ecSGeorge Rimar 
1283837f427SRui Ueyama void LinkerScript::setDot(Expr e, const Twine &loc, bool inSec) {
1293837f427SRui Ueyama   uint64_t val = e().getValue();
1303837f427SRui Ueyama   if (val < dot && inSec)
1313837f427SRui Ueyama     error(loc + ": unable to move location counter backward for: " +
1323837f427SRui Ueyama           ctx->outSec->name);
13318d19687SRui Ueyama 
1344cd7352cSRafael Espindola   // Update to location counter means update to section size.
1353837f427SRui Ueyama   if (inSec)
1363837f427SRui Ueyama     expandOutputSection(val - dot);
1379b99abcfSGeorge Rimar 
1383837f427SRui Ueyama   dot = val;
139679828ffSRafael Espindola }
140679828ffSRafael Espindola 
141c4ccfb5dSGeorge Rimar // Used for handling linker symbol assignments, for both finalizing
142c4ccfb5dSGeorge Rimar // their values and doing early declarations. Returns true if symbol
143c4ccfb5dSGeorge Rimar // should be defined from linker script.
1443837f427SRui Ueyama static bool shouldDefineSym(SymbolAssignment *cmd) {
1453837f427SRui Ueyama   if (cmd->name == ".")
146c4ccfb5dSGeorge Rimar     return false;
147c4ccfb5dSGeorge Rimar 
1483837f427SRui Ueyama   if (!cmd->provide)
149c4ccfb5dSGeorge Rimar     return true;
150c4ccfb5dSGeorge Rimar 
151c4ccfb5dSGeorge Rimar   // If a symbol was in PROVIDE(), we need to define it only
152c4ccfb5dSGeorge Rimar   // when it is a referenced undefined symbol.
1533837f427SRui Ueyama   Symbol *b = symtab->find(cmd->name);
1543837f427SRui Ueyama   if (b && !b->isDefined())
155c4ccfb5dSGeorge Rimar     return true;
1563345c9acSIgor Kudrin   return false;
157c4ccfb5dSGeorge Rimar }
158c4ccfb5dSGeorge Rimar 
1595d9f419aSFangrui Song // Called by processSymbolAssignments() to assign definitions to
1605d9f419aSFangrui Song // linker-script-defined symbols.
1613837f427SRui Ueyama void LinkerScript::addSymbol(SymbolAssignment *cmd) {
1623837f427SRui Ueyama   if (!shouldDefineSym(cmd))
1638f1f3c40SMeador Inge     return;
1648f1f3c40SMeador Inge 
16518d19687SRui Ueyama   // Define a symbol.
1663837f427SRui Ueyama   ExprValue value = cmd->expression();
1673837f427SRui Ueyama   SectionBase *sec = value.isAbsolute() ? nullptr : value.sec;
1683837f427SRui Ueyama   uint8_t visibility = cmd->hidden ? STV_HIDDEN : STV_DEFAULT;
16918d19687SRui Ueyama 
17018d19687SRui Ueyama   // When this function is called, section addresses have not been
17118d19687SRui Ueyama   // fixed yet. So, we may or may not know the value of the RHS
17218d19687SRui Ueyama   // expression.
17318d19687SRui Ueyama   //
17418d19687SRui Ueyama   // For example, if an expression is `x = 42`, we know x is always 42.
17518d19687SRui Ueyama   // However, if an expression is `x = .`, there's no way to know its
17618d19687SRui Ueyama   // value at the moment.
17718d19687SRui Ueyama   //
17818d19687SRui Ueyama   // We want to set symbol values early if we can. This allows us to
17918d19687SRui Ueyama   // use symbols as variables in linker scripts. Doing so allows us to
18018d19687SRui Ueyama   // write expressions like this: `alignment = 16; . = ALIGN(., alignment)`.
1813837f427SRui Ueyama   uint64_t symValue = value.sec ? 0 : value.getValue();
18218d19687SRui Ueyama 
183ab04ad6aSFangrui Song   Defined newSym(nullptr, cmd->name, STB_GLOBAL, visibility, STT_NOTYPE,
184ab04ad6aSFangrui Song                  symValue, 0, sec);
1857d476192SRui Ueyama 
1863837f427SRui Ueyama   Symbol *sym = symtab->insert(cmd->name);
187ab04ad6aSFangrui Song   sym->mergeProperties(newSym);
188ab04ad6aSFangrui Song   sym->replace(newSym);
1893837f427SRui Ueyama   cmd->sym = cast<Defined>(sym);
19018d19687SRui Ueyama }
19118d19687SRui Ueyama 
192c844524eSIgor Kudrin // This function is called from LinkerScript::declareSymbols.
193c844524eSIgor Kudrin // It creates a placeholder symbol if needed.
1943837f427SRui Ueyama static void declareSymbol(SymbolAssignment *cmd) {
1953837f427SRui Ueyama   if (!shouldDefineSym(cmd))
196c844524eSIgor Kudrin     return;
197c4ccfb5dSGeorge Rimar 
1983837f427SRui Ueyama   uint8_t visibility = cmd->hidden ? STV_HIDDEN : STV_DEFAULT;
199ab04ad6aSFangrui Song   Defined newSym(nullptr, cmd->name, STB_GLOBAL, visibility, STT_NOTYPE, 0, 0,
2007d476192SRui Ueyama                  nullptr);
2017d476192SRui Ueyama 
202c4ccfb5dSGeorge Rimar   // We can't calculate final value right now.
2033837f427SRui Ueyama   Symbol *sym = symtab->insert(cmd->name);
204ab04ad6aSFangrui Song   sym->mergeProperties(newSym);
205ab04ad6aSFangrui Song   sym->replace(newSym);
2067d476192SRui Ueyama 
2073837f427SRui Ueyama   cmd->sym = cast<Defined>(sym);
2083837f427SRui Ueyama   cmd->provide = false;
2093837f427SRui Ueyama   sym->scriptDefined = true;
210c4ccfb5dSGeorge Rimar }
211c844524eSIgor Kudrin 
212debcac9fSFangrui Song using SymbolAssignmentMap =
213debcac9fSFangrui Song     DenseMap<const Defined *, std::pair<SectionBase *, uint64_t>>;
214debcac9fSFangrui Song 
215debcac9fSFangrui Song // Collect section/value pairs of linker-script-defined symbols. This is used to
216debcac9fSFangrui Song // check whether symbol values converge.
217debcac9fSFangrui Song static SymbolAssignmentMap
218debcac9fSFangrui Song getSymbolAssignmentValues(const std::vector<BaseCommand *> &sectionCommands) {
219debcac9fSFangrui Song   SymbolAssignmentMap ret;
220debcac9fSFangrui Song   for (BaseCommand *base : sectionCommands) {
221debcac9fSFangrui Song     if (auto *cmd = dyn_cast<SymbolAssignment>(base)) {
222debcac9fSFangrui Song       if (cmd->sym) // sym is nullptr for dot.
223debcac9fSFangrui Song         ret.try_emplace(cmd->sym,
224debcac9fSFangrui Song                         std::make_pair(cmd->sym->section, cmd->sym->value));
225debcac9fSFangrui Song       continue;
226debcac9fSFangrui Song     }
227debcac9fSFangrui Song     for (BaseCommand *sub_base : cast<OutputSection>(base)->sectionCommands)
228debcac9fSFangrui Song       if (auto *cmd = dyn_cast<SymbolAssignment>(sub_base))
229debcac9fSFangrui Song         if (cmd->sym)
230debcac9fSFangrui Song           ret.try_emplace(cmd->sym,
231debcac9fSFangrui Song                           std::make_pair(cmd->sym->section, cmd->sym->value));
232debcac9fSFangrui Song   }
233debcac9fSFangrui Song   return ret;
234debcac9fSFangrui Song }
235debcac9fSFangrui Song 
236debcac9fSFangrui Song // Returns the lexicographical smallest (for determinism) Defined whose
237debcac9fSFangrui Song // section/value has changed.
238debcac9fSFangrui Song static const Defined *
239debcac9fSFangrui Song getChangedSymbolAssignment(const SymbolAssignmentMap &oldValues) {
240debcac9fSFangrui Song   const Defined *changed = nullptr;
241debcac9fSFangrui Song   for (auto &it : oldValues) {
242debcac9fSFangrui Song     const Defined *sym = it.first;
243debcac9fSFangrui Song     if (std::make_pair(sym->section, sym->value) != it.second &&
244debcac9fSFangrui Song         (!changed || sym->getName() < changed->getName()))
245debcac9fSFangrui Song       changed = sym;
246debcac9fSFangrui Song   }
247debcac9fSFangrui Song   return changed;
248debcac9fSFangrui Song }
249debcac9fSFangrui Song 
2507c426fb1SFangrui Song // Process INSERT [AFTER|BEFORE] commands. For each command, we move the
2517c426fb1SFangrui Song // specified output section to the designated place.
2529e2c8a9dSGeorge Rimar void LinkerScript::processInsertCommands() {
2537c426fb1SFangrui Song   for (const InsertCommand &cmd : insertCommands) {
2547c426fb1SFangrui Song     // If cmd.os is empty, it may have been discarded by
2557c426fb1SFangrui Song     // adjustSectionsBeforeSorting(). We do not handle such output sections.
2567c426fb1SFangrui Song     auto from = llvm::find(sectionCommands, cmd.os);
2577c426fb1SFangrui Song     if (from == sectionCommands.end())
258796684b4SGeorge Rimar       continue;
2597c426fb1SFangrui Song     sectionCommands.erase(from);
260796684b4SGeorge Rimar 
2617c426fb1SFangrui Song     auto insertPos = llvm::find_if(sectionCommands, [&cmd](BaseCommand *base) {
2627c426fb1SFangrui Song       auto *to = dyn_cast<OutputSection>(base);
2637c426fb1SFangrui Song       return to != nullptr && to->name == cmd.where;
2647c426fb1SFangrui Song     });
2657c426fb1SFangrui Song     if (insertPos == sectionCommands.end()) {
2667c426fb1SFangrui Song       error("unable to insert " + cmd.os->name +
2677c426fb1SFangrui Song             (cmd.isAfter ? " after " : " before ") + cmd.where);
2687c426fb1SFangrui Song     } else {
2697c426fb1SFangrui Song       if (cmd.isAfter)
2707c426fb1SFangrui Song         ++insertPos;
2717c426fb1SFangrui Song       sectionCommands.insert(insertPos, cmd.os);
2727c426fb1SFangrui Song     }
2737c426fb1SFangrui Song   }
2749e2c8a9dSGeorge Rimar }
2759e2c8a9dSGeorge Rimar 
276c844524eSIgor Kudrin // Symbols defined in script should not be inlined by LTO. At the same time
277c844524eSIgor Kudrin // we don't know their final values until late stages of link. Here we scan
278c844524eSIgor Kudrin // over symbol assignment commands and create placeholder symbols if needed.
279c844524eSIgor Kudrin void LinkerScript::declareSymbols() {
2803837f427SRui Ueyama   assert(!ctx);
2813837f427SRui Ueyama   for (BaseCommand *base : sectionCommands) {
2823837f427SRui Ueyama     if (auto *cmd = dyn_cast<SymbolAssignment>(base)) {
2833837f427SRui Ueyama       declareSymbol(cmd);
284c844524eSIgor Kudrin       continue;
285c844524eSIgor Kudrin     }
286ccf0db99SGeorge Rimar 
287c844524eSIgor Kudrin     // If the output section directive has constraints,
288c844524eSIgor Kudrin     // we can't say for sure if it is going to be included or not.
289c844524eSIgor Kudrin     // Skip such sections for now. Improve the checks if we ever
290c844524eSIgor Kudrin     // need symbols from that sections to be declared early.
2913837f427SRui Ueyama     auto *sec = cast<OutputSection>(base);
2923837f427SRui Ueyama     if (sec->constraint != ConstraintKind::NoConstraint)
293c844524eSIgor Kudrin       continue;
2943837f427SRui Ueyama     for (BaseCommand *base2 : sec->sectionCommands)
2953837f427SRui Ueyama       if (auto *cmd = dyn_cast<SymbolAssignment>(base2))
2963837f427SRui Ueyama         declareSymbol(cmd);
297c844524eSIgor Kudrin   }
298c4ccfb5dSGeorge Rimar }
299c4ccfb5dSGeorge Rimar 
30018d19687SRui Ueyama // This function is called from assignAddresses, while we are
30118d19687SRui Ueyama // fixing the output section addresses. This function is supposed
30218d19687SRui Ueyama // to set the final value for a given symbol assignment.
3033837f427SRui Ueyama void LinkerScript::assignSymbol(SymbolAssignment *cmd, bool inSec) {
3043837f427SRui Ueyama   if (cmd->name == ".") {
3053837f427SRui Ueyama     setDot(cmd->expression, cmd->location, inSec);
30618d19687SRui Ueyama     return;
30718d19687SRui Ueyama   }
30818d19687SRui Ueyama 
3093837f427SRui Ueyama   if (!cmd->sym)
31018d19687SRui Ueyama     return;
31118d19687SRui Ueyama 
3123837f427SRui Ueyama   ExprValue v = cmd->expression();
3133837f427SRui Ueyama   if (v.isAbsolute()) {
3143837f427SRui Ueyama     cmd->sym->section = nullptr;
3153837f427SRui Ueyama     cmd->sym->value = v.getValue();
31618d19687SRui Ueyama   } else {
3173837f427SRui Ueyama     cmd->sym->section = v.sec;
3183837f427SRui Ueyama     cmd->sym->value = v.getSectionOffset();
31918d19687SRui Ueyama   }
320ceabe80eSEugene Leviant }
321ceabe80eSEugene Leviant 
3223837f427SRui Ueyama static std::string getFilename(InputFile *file) {
3233837f427SRui Ueyama   if (!file)
324e0be2901SRui Ueyama     return "";
3253837f427SRui Ueyama   if (file->archiveName.empty())
326adcd0268SBenjamin Kramer     return std::string(file->getName());
32793331a17SFangrui Song   return (file->archiveName + ':' + file->getName()).str();
328e0be2901SRui Ueyama }
329e0be2901SRui Ueyama 
3303837f427SRui Ueyama bool LinkerScript::shouldKeep(InputSectionBase *s) {
3313837f427SRui Ueyama   if (keptSections.empty())
3320ab9d8b6SRafael Espindola     return false;
3333837f427SRui Ueyama   std::string filename = getFilename(s->file);
3343837f427SRui Ueyama   for (InputSectionDescription *id : keptSections)
3353837f427SRui Ueyama     if (id->filePat.match(filename))
3363837f427SRui Ueyama       for (SectionPattern &p : id->sectionPatterns)
337dbd0ad33SPeter Smith         if (p.sectionPat.match(s->name) &&
338dbd0ad33SPeter Smith             (s->flags & id->withFlags) == id->withFlags &&
339dbd0ad33SPeter Smith             (s->flags & id->withoutFlags) == 0)
340eea3114fSGeorge Rimar           return true;
341eea3114fSGeorge Rimar   return false;
342eea3114fSGeorge Rimar }
343eea3114fSGeorge Rimar 
344ea93fe00SRui Ueyama // A helper function for the SORT() command.
345e47bbd28SFangrui Song static bool matchConstraints(ArrayRef<InputSectionBase *> sections,
3463837f427SRui Ueyama                              ConstraintKind kind) {
3473837f427SRui Ueyama   if (kind == ConstraintKind::NoConstraint)
3488f66df92SGeorge Rimar     return true;
3492c7171bfSRui Ueyama 
3503837f427SRui Ueyama   bool isRW = llvm::any_of(
351e47bbd28SFangrui Song       sections, [](InputSectionBase *sec) { return sec->flags & SHF_WRITE; });
3522c7171bfSRui Ueyama 
3533837f427SRui Ueyama   return (isRW && kind == ConstraintKind::ReadWrite) ||
3543837f427SRui Ueyama          (!isRW && kind == ConstraintKind::ReadOnly);
35506ae6836SGeorge Rimar }
35606ae6836SGeorge Rimar 
357e47bbd28SFangrui Song static void sortSections(MutableArrayRef<InputSectionBase *> vec,
3583837f427SRui Ueyama                          SortSectionPolicy k) {
3597a210d65SJF Bastien   auto alignmentComparator = [](InputSectionBase *a, InputSectionBase *b) {
3607a210d65SJF Bastien     // ">" is not a mistake. Sections with larger alignments are placed
3617a210d65SJF Bastien     // before sections with smaller alignments in order to reduce the
3627a210d65SJF Bastien     // amount of padding necessary. This is compatible with GNU.
3637a210d65SJF Bastien     return a->alignment > b->alignment;
3647a210d65SJF Bastien   };
3657a210d65SJF Bastien   auto nameComparator = [](InputSectionBase *a, InputSectionBase *b) {
3667a210d65SJF Bastien     return a->name < b->name;
3677a210d65SJF Bastien   };
3687a210d65SJF Bastien   auto priorityComparator = [](InputSectionBase *a, InputSectionBase *b) {
3697a210d65SJF Bastien     return getPriority(a->name) < getPriority(b->name);
3707a210d65SJF Bastien   };
3717a210d65SJF Bastien 
3727a210d65SJF Bastien   switch (k) {
3737a210d65SJF Bastien   case SortSectionPolicy::Default:
3747a210d65SJF Bastien   case SortSectionPolicy::None:
3757a210d65SJF Bastien     return;
3767a210d65SJF Bastien   case SortSectionPolicy::Alignment:
3777a210d65SJF Bastien     return llvm::stable_sort(vec, alignmentComparator);
3787a210d65SJF Bastien   case SortSectionPolicy::Name:
3797a210d65SJF Bastien     return llvm::stable_sort(vec, nameComparator);
3807a210d65SJF Bastien   case SortSectionPolicy::Priority:
3817a210d65SJF Bastien     return llvm::stable_sort(vec, priorityComparator);
3827a210d65SJF Bastien   }
383ee924709SRui Ueyama }
384ee924709SRui Ueyama 
3857ad1e310SRui Ueyama // Sort sections as instructed by SORT-family commands and --sort-section
3867ad1e310SRui Ueyama // option. Because SORT-family commands can be nested at most two depth
3877ad1e310SRui Ueyama // (e.g. SORT_BY_NAME(SORT_BY_ALIGNMENT(.text.*))) and because the command
3887ad1e310SRui Ueyama // line option is respected even if a SORT command is given, the exact
3897ad1e310SRui Ueyama // behavior we have here is a bit complicated. Here are the rules.
3907ad1e310SRui Ueyama //
3917ad1e310SRui Ueyama // 1. If two SORT commands are given, --sort-section is ignored.
3927ad1e310SRui Ueyama // 2. If one SORT command is given, and if it is not SORT_NONE,
3937ad1e310SRui Ueyama //    --sort-section is handled as an inner SORT command.
3947ad1e310SRui Ueyama // 3. If one SORT command is given, and if it is SORT_NONE, don't sort.
3957ad1e310SRui Ueyama // 4. If no SORT command is given, sort according to --sort-section.
396e47bbd28SFangrui Song static void sortInputSections(MutableArrayRef<InputSectionBase *> vec,
3973837f427SRui Ueyama                               const SectionPattern &pat) {
3983837f427SRui Ueyama   if (pat.sortOuter == SortSectionPolicy::None)
3997ad1e310SRui Ueyama     return;
4007ad1e310SRui Ueyama 
4013837f427SRui Ueyama   if (pat.sortInner == SortSectionPolicy::Default)
4023837f427SRui Ueyama     sortSections(vec, config->sortSection);
4037ad1e310SRui Ueyama   else
4043837f427SRui Ueyama     sortSections(vec, pat.sortInner);
4053837f427SRui Ueyama   sortSections(vec, pat.sortOuter);
4067ad1e310SRui Ueyama }
4077ad1e310SRui Ueyama 
408d3190795SRafael Espindola // Compute and remember which sections the InputSectionDescription matches.
409e47bbd28SFangrui Song std::vector<InputSectionBase *>
4103837f427SRui Ueyama LinkerScript::computeInputSections(const InputSectionDescription *cmd) {
411e47bbd28SFangrui Song   std::vector<InputSectionBase *> ret;
4128c6a5aafSRui Ueyama 
41372e107f3SRui Ueyama   // Collects all sections that satisfy constraints of Cmd.
4143837f427SRui Ueyama   for (const SectionPattern &pat : cmd->sectionPatterns) {
4153837f427SRui Ueyama     size_t sizeBefore = ret.size();
41672e107f3SRui Ueyama 
4173837f427SRui Ueyama     for (InputSectionBase *sec : inputSections) {
418e447d5afSFangrui Song       if (!sec->isLive() || sec->parent)
4198c6a5aafSRui Ueyama         continue;
42072e107f3SRui Ueyama 
421908a3d34SRafael Espindola       // For -emit-relocs we have to ignore entries like
422908a3d34SRafael Espindola       //   .rela.dyn : { *(.rela.data) }
423908a3d34SRafael Espindola       // which are common because they are in the default bfd script.
424d8281379SGeorge Rimar       // We do not ignore SHT_REL[A] linker-synthesized sections here because
425d8281379SGeorge Rimar       // want to support scripts that do custom layout for them.
426e47bbd28SFangrui Song       if (isa<InputSection>(sec) &&
427e47bbd28SFangrui Song           cast<InputSection>(sec)->getRelocatedSection())
428908a3d34SRafael Espindola         continue;
4298c6a5aafSRui Ueyama 
430d36b2649SAndrew Ng       // Check the name early to improve performance in the common case.
431d36b2649SAndrew Ng       if (!pat.sectionPat.match(sec->name))
432d36b2649SAndrew Ng         continue;
433d36b2649SAndrew Ng 
4343837f427SRui Ueyama       std::string filename = getFilename(sec->file);
435dbd0ad33SPeter Smith       if (!cmd->filePat.match(filename) ||
436dbd0ad33SPeter Smith           pat.excludedFilePat.match(filename) ||
437dbd0ad33SPeter Smith           (sec->flags & cmd->withFlags) != cmd->withFlags ||
438dbd0ad33SPeter Smith           (sec->flags & cmd->withoutFlags) != 0)
439e0be2901SRui Ueyama         continue;
44072e107f3SRui Ueyama 
441e47bbd28SFangrui Song       ret.push_back(sec);
442f94efdddSRui Ueyama     }
443d3190795SRafael Espindola 
444e47bbd28SFangrui Song     sortInputSections(
445e47bbd28SFangrui Song         MutableArrayRef<InputSectionBase *>(ret).slice(sizeBefore), pat);
446ee924709SRui Ueyama   }
4473837f427SRui Ueyama   return ret;
448be94e1b6SRafael Espindola }
449be94e1b6SRafael Espindola 
450e47bbd28SFangrui Song void LinkerScript::discard(InputSectionBase *s) {
451a71c1e2aSFangrui Song   if (s == in.shStrTab || s == mainPart->relrDyn)
4523837f427SRui Ueyama     error("discarding " + s->name + " section is not allowed");
4538e38ea8bSRui Ueyama 
45454baa5f4SGeorge Rimar   // You can discard .hash and .gnu.hash sections by linker scripts. Since
45554baa5f4SGeorge Rimar   // they are synthesized sections, we need to handle them differently than
45654baa5f4SGeorge Rimar   // other regular sections.
4573837f427SRui Ueyama   if (s == mainPart->gnuHashTab)
4583837f427SRui Ueyama     mainPart->gnuHashTab = nullptr;
4593837f427SRui Ueyama   if (s == mainPart->hashTab)
4603837f427SRui Ueyama     mainPart->hashTab = nullptr;
46154baa5f4SGeorge Rimar 
4623837f427SRui Ueyama   s->markDead();
463e447d5afSFangrui Song   s->parent = nullptr;
464e47bbd28SFangrui Song   for (InputSection *ds : s->dependentSections)
465e47bbd28SFangrui Song     discard(ds);
466be94e1b6SRafael Espindola }
467be94e1b6SRafael Espindola 
468e47bbd28SFangrui Song std::vector<InputSectionBase *>
4693837f427SRui Ueyama LinkerScript::createInputSectionList(OutputSection &outCmd) {
470e47bbd28SFangrui Song   std::vector<InputSectionBase *> ret;
471e7f912cdSRui Ueyama 
4723837f427SRui Ueyama   for (BaseCommand *base : outCmd.sectionCommands) {
4733837f427SRui Ueyama     if (auto *cmd = dyn_cast<InputSectionDescription>(base)) {
474e47bbd28SFangrui Song       cmd->sectionBases = computeInputSections(cmd);
475e447d5afSFangrui Song       for (InputSectionBase *s : cmd->sectionBases)
476e447d5afSFangrui Song         s->parent = &outCmd;
477e47bbd28SFangrui Song       ret.insert(ret.end(), cmd->sectionBases.begin(), cmd->sectionBases.end());
4780b9ce6a4SRui Ueyama     }
47905433431SRui Ueyama   }
4803837f427SRui Ueyama   return ret;
4810b9ce6a4SRui Ueyama }
4820b9ce6a4SRui Ueyama 
4835d9f419aSFangrui Song // Create output sections described by SECTIONS commands.
4843558e24aSRafael Espindola void LinkerScript::processSectionCommands() {
4853837f427SRui Ueyama   size_t i = 0;
4863837f427SRui Ueyama   for (BaseCommand *base : sectionCommands) {
4873837f427SRui Ueyama     if (auto *sec = dyn_cast<OutputSection>(base)) {
488e47bbd28SFangrui Song       std::vector<InputSectionBase *> v = createInputSectionList(*sec);
4897bd37870SRafael Espindola 
4900b1b695aSRui Ueyama       // The output section name `/DISCARD/' is special.
4910b1b695aSRui Ueyama       // Any input section assigned to it is discarded.
4923837f427SRui Ueyama       if (sec->name == "/DISCARD/") {
493e47bbd28SFangrui Song         for (InputSectionBase *s : v)
494e47bbd28SFangrui Song           discard(s);
4953837f427SRui Ueyama         sec->sectionCommands.clear();
49648c3f1ceSRui Ueyama         continue;
49748c3f1ceSRui Ueyama       }
4980b9ce6a4SRui Ueyama 
4990b1b695aSRui Ueyama       // This is for ONLY_IF_RO and ONLY_IF_RW. An output section directive
5000b1b695aSRui Ueyama       // ".foo : ONLY_IF_R[OW] { ... }" is handled only if all member input
5010b1b695aSRui Ueyama       // sections satisfy a given constraint. If not, a directive is handled
50207d7c42cSGeorge Rimar       // as if it wasn't present from the beginning.
5030b1b695aSRui Ueyama       //
5041b45d377SGeorge Rimar       // Because we'll iterate over SectionCommands many more times, the easy
5051b45d377SGeorge Rimar       // way to "make it as if it wasn't present" is to make it empty.
5063837f427SRui Ueyama       if (!matchConstraints(v, sec->constraint)) {
5073837f427SRui Ueyama         for (InputSectionBase *s : v)
508e447d5afSFangrui Song           s->parent = nullptr;
5093837f427SRui Ueyama         sec->sectionCommands.clear();
5107c3ff2ebSRafael Espindola         continue;
5117c3ff2ebSRafael Espindola       }
5127c3ff2ebSRafael Espindola 
5130b1b695aSRui Ueyama       // Handle subalign (e.g. ".foo : SUBALIGN(32) { ... }"). If subalign
5140b1b695aSRui Ueyama       // is given, input sections are aligned to that value, whether the
5150b1b695aSRui Ueyama       // given value is larger or smaller than the original section alignment.
5163837f427SRui Ueyama       if (sec->subalignExpr) {
5173837f427SRui Ueyama         uint32_t subalign = sec->subalignExpr().getValue();
5183837f427SRui Ueyama         for (InputSectionBase *s : v)
5193837f427SRui Ueyama           s->alignment = subalign;
52020d03194SEugene Leviant       }
5210b1b695aSRui Ueyama 
522f1e14519SFangrui Song       // Set the partition field the same way OutputSection::recordSection()
523f1e14519SFangrui Song       // does. Partitions cannot be used with the SECTIONS command, so this is
524f1e14519SFangrui Song       // always 1.
525f1e14519SFangrui Song       sec->partition = 1;
526f1e14519SFangrui Song 
5273837f427SRui Ueyama       sec->sectionIndex = i++;
52848c3f1ceSRui Ueyama     }
5294aa2ef5bSRafael Espindola   }
5305d9f419aSFangrui Song }
5315d9f419aSFangrui Song 
5325d9f419aSFangrui Song void LinkerScript::processSymbolAssignments() {
5335d9f419aSFangrui Song   // Dot outside an output section still represents a relative address, whose
5345d9f419aSFangrui Song   // sh_shndx should not be SHN_UNDEF or SHN_ABS. Create a dummy aether section
5355d9f419aSFangrui Song   // that fills the void outside a section. It has an index of one, which is
5365d9f419aSFangrui Song   // indistinguishable from any other regular section index.
5375d9f419aSFangrui Song   aether = make<OutputSection>("", 0, SHF_ALLOC);
5385d9f419aSFangrui Song   aether->sectionIndex = 1;
5395d9f419aSFangrui Song 
5405d9f419aSFangrui Song   // ctx captures the local AddressState and makes it accessible deliberately.
5415d9f419aSFangrui Song   // This is needed as there are some cases where we cannot just thread the
5425d9f419aSFangrui Song   // current state through to a lambda function created by the script parser.
5435d9f419aSFangrui Song   AddressState state;
5445d9f419aSFangrui Song   ctx = &state;
5455d9f419aSFangrui Song   ctx->outSec = aether;
5465d9f419aSFangrui Song 
5475d9f419aSFangrui Song   for (BaseCommand *base : sectionCommands) {
5485d9f419aSFangrui Song     if (auto *cmd = dyn_cast<SymbolAssignment>(base))
5495d9f419aSFangrui Song       addSymbol(cmd);
5505d9f419aSFangrui Song     else
5515d9f419aSFangrui Song       for (BaseCommand *sub_base : cast<OutputSection>(base)->sectionCommands)
5525d9f419aSFangrui Song         if (auto *cmd = dyn_cast<SymbolAssignment>(sub_base))
5535d9f419aSFangrui Song           addSymbol(cmd);
5545d9f419aSFangrui Song   }
5555d9f419aSFangrui Song 
5563837f427SRui Ueyama   ctx = nullptr;
5571b45d377SGeorge Rimar }
558e63d81bdSEugene Leviant 
5593837f427SRui Ueyama static OutputSection *findByName(ArrayRef<BaseCommand *> vec,
5603837f427SRui Ueyama                                  StringRef name) {
5613837f427SRui Ueyama   for (BaseCommand *base : vec)
5623837f427SRui Ueyama     if (auto *sec = dyn_cast<OutputSection>(base))
5633837f427SRui Ueyama       if (sec->name == name)
5643837f427SRui Ueyama         return sec;
565d2f225fdSRui Ueyama   return nullptr;
566d2f225fdSRui Ueyama }
567d2f225fdSRui Ueyama 
5683837f427SRui Ueyama static OutputSection *createSection(InputSectionBase *isec,
5693837f427SRui Ueyama                                     StringRef outsecName) {
5703837f427SRui Ueyama   OutputSection *sec = script->createOutputSection(outsecName, "<internal>");
571e47bbd28SFangrui Song   sec->recordSection(isec);
5723837f427SRui Ueyama   return sec;
573aa8523e4SRui Ueyama }
574aa8523e4SRui Ueyama 
575ba2816beSPeter Collingbourne static OutputSection *
5763837f427SRui Ueyama addInputSec(StringMap<TinyPtrVector<OutputSection *>> &map,
5773837f427SRui Ueyama             InputSectionBase *isec, StringRef outsecName) {
578aa8523e4SRui Ueyama   // Sections with SHT_GROUP or SHF_GROUP attributes reach here only when the -r
579aa8523e4SRui Ueyama   // option is given. A section with SHT_GROUP defines a "section group", and
580aa8523e4SRui Ueyama   // its members have SHF_GROUP attribute. Usually these flags have already been
581aa8523e4SRui Ueyama   // stripped by InputFiles.cpp as section groups are processed and uniquified.
582aa8523e4SRui Ueyama   // However, for the -r option, we want to pass through all section groups
583aa8523e4SRui Ueyama   // as-is because adding/removing members or merging them with other groups
584aa8523e4SRui Ueyama   // change their semantics.
5853837f427SRui Ueyama   if (isec->type == SHT_GROUP || (isec->flags & SHF_GROUP))
5863837f427SRui Ueyama     return createSection(isec, outsecName);
587aa8523e4SRui Ueyama 
588aa8523e4SRui Ueyama   // Imagine .zed : { *(.foo) *(.bar) } script. Both foo and bar may have
589aa8523e4SRui Ueyama   // relocation sections .rela.foo and .rela.bar for example. Most tools do
590aa8523e4SRui Ueyama   // not allow multiple REL[A] sections for output section. Hence we
591aa8523e4SRui Ueyama   // should combine these relocation sections into single output.
592aa8523e4SRui Ueyama   // We skip synthetic sections because it can be .rela.dyn/.rela.plt or any
593aa8523e4SRui Ueyama   // other REL[A] sections created by linker itself.
5943837f427SRui Ueyama   if (!isa<SyntheticSection>(isec) &&
5953837f427SRui Ueyama       (isec->type == SHT_REL || isec->type == SHT_RELA)) {
5963837f427SRui Ueyama     auto *sec = cast<InputSection>(isec);
5973837f427SRui Ueyama     OutputSection *out = sec->getRelocatedSection()->getOutputSection();
598aa8523e4SRui Ueyama 
5993837f427SRui Ueyama     if (out->relocationSection) {
600e47bbd28SFangrui Song       out->relocationSection->recordSection(sec);
601aa8523e4SRui Ueyama       return nullptr;
602aa8523e4SRui Ueyama     }
603aa8523e4SRui Ueyama 
6043837f427SRui Ueyama     out->relocationSection = createSection(isec, outsecName);
6053837f427SRui Ueyama     return out->relocationSection;
606aa8523e4SRui Ueyama   }
607aa8523e4SRui Ueyama 
608aa8523e4SRui Ueyama   //  The ELF spec just says
609aa8523e4SRui Ueyama   // ----------------------------------------------------------------
610aa8523e4SRui Ueyama   // In the first phase, input sections that match in name, type and
611aa8523e4SRui Ueyama   // attribute flags should be concatenated into single sections.
612aa8523e4SRui Ueyama   // ----------------------------------------------------------------
613aa8523e4SRui Ueyama   //
614aa8523e4SRui Ueyama   // However, it is clear that at least some flags have to be ignored for
615aa8523e4SRui Ueyama   // section merging. At the very least SHF_GROUP and SHF_COMPRESSED have to be
616aa8523e4SRui Ueyama   // ignored. We should not have two output .text sections just because one was
617aa8523e4SRui Ueyama   // in a group and another was not for example.
618aa8523e4SRui Ueyama   //
6199cd5df0eSFangrui Song   // It also seems that wording was a late addition and didn't get the
620aa8523e4SRui Ueyama   // necessary scrutiny.
621aa8523e4SRui Ueyama   //
622aa8523e4SRui Ueyama   // Merging sections with different flags is expected by some users. One
623aa8523e4SRui Ueyama   // reason is that if one file has
624aa8523e4SRui Ueyama   //
625aa8523e4SRui Ueyama   // int *const bar __attribute__((section(".foo"))) = (int *)0;
626aa8523e4SRui Ueyama   //
627aa8523e4SRui Ueyama   // gcc with -fPIC will produce a read only .foo section. But if another
628aa8523e4SRui Ueyama   // file has
629aa8523e4SRui Ueyama   //
630aa8523e4SRui Ueyama   // int zed;
631aa8523e4SRui Ueyama   // int *const bar __attribute__((section(".foo"))) = (int *)&zed;
632aa8523e4SRui Ueyama   //
633aa8523e4SRui Ueyama   // gcc with -fPIC will produce a read write section.
634aa8523e4SRui Ueyama   //
635aa8523e4SRui Ueyama   // Last but not least, when using linker script the merge rules are forced by
636aa8523e4SRui Ueyama   // the script. Unfortunately, linker scripts are name based. This means that
637aa8523e4SRui Ueyama   // expressions like *(.foo*) can refer to multiple input sections with
638aa8523e4SRui Ueyama   // different flags. We cannot put them in different output sections or we
639aa8523e4SRui Ueyama   // would produce wrong results for
640aa8523e4SRui Ueyama   //
641aa8523e4SRui Ueyama   // start = .; *(.foo.*) end = .; *(.bar)
642aa8523e4SRui Ueyama   //
643aa8523e4SRui Ueyama   // and a mapping of .foo1 and .bar1 to one section and .foo2 and .bar2 to
644aa8523e4SRui Ueyama   // another. The problem is that there is no way to layout those output
645aa8523e4SRui Ueyama   // sections such that the .foo sections are the only thing between the start
646aa8523e4SRui Ueyama   // and end symbols.
647aa8523e4SRui Ueyama   //
648aa8523e4SRui Ueyama   // Given the above issues, we instead merge sections by name and error on
649aa8523e4SRui Ueyama   // incompatible types and flags.
6503837f427SRui Ueyama   TinyPtrVector<OutputSection *> &v = map[outsecName];
6513837f427SRui Ueyama   for (OutputSection *sec : v) {
6523837f427SRui Ueyama     if (sec->partition != isec->partition)
653ba2816beSPeter Collingbourne       continue;
65497e251e0SPeter Collingbourne 
65597e251e0SPeter Collingbourne     if (config->relocatable && (isec->flags & SHF_LINK_ORDER)) {
65697e251e0SPeter Collingbourne       // Merging two SHF_LINK_ORDER sections with different sh_link fields will
65797e251e0SPeter Collingbourne       // change their semantics, so we only merge them in -r links if they will
65897e251e0SPeter Collingbourne       // end up being linked to the same output section. The casts are fine
65997e251e0SPeter Collingbourne       // because everything in the map was created by the orphan placement code.
66097e251e0SPeter Collingbourne       auto *firstIsec = cast<InputSectionBase>(
66197e251e0SPeter Collingbourne           cast<InputSectionDescription>(sec->sectionCommands[0])
66297e251e0SPeter Collingbourne               ->sectionBases[0]);
66397e251e0SPeter Collingbourne       if (firstIsec->getLinkOrderDep()->getOutputSection() !=
66497e251e0SPeter Collingbourne           isec->getLinkOrderDep()->getOutputSection())
66597e251e0SPeter Collingbourne         continue;
66697e251e0SPeter Collingbourne     }
66797e251e0SPeter Collingbourne 
668e47bbd28SFangrui Song     sec->recordSection(isec);
669aa8523e4SRui Ueyama     return nullptr;
670aa8523e4SRui Ueyama   }
671aa8523e4SRui Ueyama 
6723837f427SRui Ueyama   OutputSection *sec = createSection(isec, outsecName);
6733837f427SRui Ueyama   v.push_back(sec);
6743837f427SRui Ueyama   return sec;
675aa8523e4SRui Ueyama }
676aa8523e4SRui Ueyama 
6770b1b695aSRui Ueyama // Add sections that didn't match any sections command.
678aa8523e4SRui Ueyama void LinkerScript::addOrphanSections() {
6793837f427SRui Ueyama   StringMap<TinyPtrVector<OutputSection *>> map;
6803837f427SRui Ueyama   std::vector<OutputSection *> v;
68154634f19SGeorge Rimar 
68297e251e0SPeter Collingbourne   std::function<void(InputSectionBase *)> add;
68397e251e0SPeter Collingbourne   add = [&](InputSectionBase *s) {
68497e251e0SPeter Collingbourne     if (s->isLive() && !s->parent) {
68542319409SFangrui Song       orphanSections.push_back(s);
68642319409SFangrui Song 
6873837f427SRui Ueyama       StringRef name = getOutputSectionName(s);
6886e2804ceSDavid Bozier       if (config->unique) {
6896e2804ceSDavid Bozier         v.push_back(createSection(s, name));
6906e2804ceSDavid Bozier       } else if (OutputSection *sec = findByName(sectionCommands, name)) {
691e47bbd28SFangrui Song         sec->recordSection(s);
69297e251e0SPeter Collingbourne       } else {
6933837f427SRui Ueyama         if (OutputSection *os = addInputSec(map, s, name))
6943837f427SRui Ueyama           v.push_back(os);
695e47bbd28SFangrui Song         assert(isa<MergeInputSection>(s) ||
696e47bbd28SFangrui Song                s->getOutputSection()->sectionIndex == UINT32_MAX);
69797e251e0SPeter Collingbourne       }
69897e251e0SPeter Collingbourne     }
69997e251e0SPeter Collingbourne 
70097e251e0SPeter Collingbourne     if (config->relocatable)
70197e251e0SPeter Collingbourne       for (InputSectionBase *depSec : s->dependentSections)
70297e251e0SPeter Collingbourne         if (depSec->flags & SHF_LINK_ORDER)
70397e251e0SPeter Collingbourne           add(depSec);
70454634f19SGeorge Rimar   };
70554634f19SGeorge Rimar 
70654634f19SGeorge Rimar   // For futher --emit-reloc handling code we need target output section
70754634f19SGeorge Rimar   // to be created before we create relocation output section, so we want
70854634f19SGeorge Rimar   // to create target sections first. We do not want priority handling
70954634f19SGeorge Rimar   // for synthetic sections because them are special.
7103837f427SRui Ueyama   for (InputSectionBase *isec : inputSections) {
71197e251e0SPeter Collingbourne     // In -r links, SHF_LINK_ORDER sections are added while adding their parent
71297e251e0SPeter Collingbourne     // sections because we need to know the parent's output section before we
71397e251e0SPeter Collingbourne     // can select an output section for the SHF_LINK_ORDER section.
71497e251e0SPeter Collingbourne     if (config->relocatable && (isec->flags & SHF_LINK_ORDER))
71597e251e0SPeter Collingbourne       continue;
71697e251e0SPeter Collingbourne 
7173837f427SRui Ueyama     if (auto *sec = dyn_cast<InputSection>(isec))
7183837f427SRui Ueyama       if (InputSectionBase *rel = sec->getRelocatedSection())
7193837f427SRui Ueyama         if (auto *relIS = dyn_cast_or_null<InputSectionBase>(rel->parent))
7203837f427SRui Ueyama           add(relIS);
7213837f427SRui Ueyama     add(isec);
722d7faa916SRafael Espindola   }
723f9b04fd9SGeorge Rimar 
724f9b04fd9SGeorge Rimar   // If no SECTIONS command was given, we should insert sections commands
725f9b04fd9SGeorge Rimar   // before others, so that we can handle scripts which refers them,
726f9b04fd9SGeorge Rimar   // for example: "foo = ABSOLUTE(ADDR(.text)));".
727f9b04fd9SGeorge Rimar   // When SECTIONS command is present we just add all orphans to the end.
7283837f427SRui Ueyama   if (hasSectionsCommand)
7293837f427SRui Ueyama     sectionCommands.insert(sectionCommands.end(), v.begin(), v.end());
730f9b04fd9SGeorge Rimar   else
7313837f427SRui Ueyama     sectionCommands.insert(sectionCommands.begin(), v.begin(), v.end());
7324f013bb3SRafael Espindola }
733e63d81bdSEugene Leviant 
73442319409SFangrui Song void LinkerScript::diagnoseOrphanHandling() const {
73542319409SFangrui Song   for (const InputSectionBase *sec : orphanSections) {
73637c7f0d9SFangrui Song     // Input SHT_REL[A] retained by --emit-relocs are ignored by
73737c7f0d9SFangrui Song     // computeInputSections(). Don't warn/error.
73837c7f0d9SFangrui Song     if (isa<InputSection>(sec) &&
73937c7f0d9SFangrui Song         cast<InputSection>(sec)->getRelocatedSection())
74037c7f0d9SFangrui Song       continue;
74137c7f0d9SFangrui Song 
74242319409SFangrui Song     StringRef name = getOutputSectionName(sec);
74342319409SFangrui Song     if (config->orphanHandling == OrphanHandlingPolicy::Error)
74442319409SFangrui Song       error(toString(sec) + " is being placed in '" + name + "'");
74542319409SFangrui Song     else if (config->orphanHandling == OrphanHandlingPolicy::Warn)
74642319409SFangrui Song       warn(toString(sec) + " is being placed in '" + name + "'");
74742319409SFangrui Song   }
74842319409SFangrui Song }
74942319409SFangrui Song 
7503837f427SRui Ueyama uint64_t LinkerScript::advance(uint64_t size, unsigned alignment) {
7513837f427SRui Ueyama   bool isTbss =
7523837f427SRui Ueyama       (ctx->outSec->flags & SHF_TLS) && ctx->outSec->type == SHT_NOBITS;
7533837f427SRui Ueyama   uint64_t start = isTbss ? dot + ctx->threadBssOffset : dot;
7543837f427SRui Ueyama   start = alignTo(start, alignment);
7553837f427SRui Ueyama   uint64_t end = start + size;
7567c4eafa3SRafael Espindola 
7573837f427SRui Ueyama   if (isTbss)
7583837f427SRui Ueyama     ctx->threadBssOffset = end - dot;
7597c4eafa3SRafael Espindola   else
7603837f427SRui Ueyama     dot = end;
7613837f427SRui Ueyama   return end;
762a940e539SRafael Espindola }
763a940e539SRafael Espindola 
7643837f427SRui Ueyama void LinkerScript::output(InputSection *s) {
7653837f427SRui Ueyama   assert(ctx->outSec == s->getParent());
7663837f427SRui Ueyama   uint64_t before = advance(0, 1);
7673837f427SRui Ueyama   uint64_t pos = advance(s->getSize(), s->alignment);
7683837f427SRui Ueyama   s->outSecOff = pos - s->getSize() - ctx->outSec->addr;
769d3190795SRafael Espindola 
770d3190795SRafael Espindola   // Update output section size after adding each section. This is so that
771d3190795SRafael Espindola   // SIZEOF works correctly in the case below:
772d3190795SRafael Espindola   // .foo { *(.aaa) a = SIZEOF(.foo); *(.bbb) }
7733837f427SRui Ueyama   expandOutputSection(pos - before);
7742de509c3SRui Ueyama }
775ceabe80eSEugene Leviant 
7763837f427SRui Ueyama void LinkerScript::switchTo(OutputSection *sec) {
7773837f427SRui Ueyama   ctx->outSec = sec;
778a6ce78ecSGeorge Rimar 
7796ed8e201SFangrui Song   uint64_t pos = advance(0, 1);
780fbf41b52SFangrui Song   if (sec->addrExpr && script->hasSectionsCommand) {
7816ed8e201SFangrui Song     // The alignment is ignored.
7826ed8e201SFangrui Song     ctx->outSec->addr = pos;
7836ed8e201SFangrui Song   } else {
784fbf41b52SFangrui Song     // ctx->outSec->alignment is the max of ALIGN and the maximum of input
785fbf41b52SFangrui Song     // section alignments.
786fbf41b52SFangrui Song     ctx->outSec->addr = advance(0, ctx->outSec->alignment);
7876ed8e201SFangrui Song     expandMemoryRegions(ctx->outSec->addr - pos);
7886ed8e201SFangrui Song   }
789d3190795SRafael Espindola }
790d3190795SRafael Espindola 
791b889744eSMeador Inge // This function searches for a memory region to place the given output
792b889744eSMeador Inge // section in. If found, a pointer to the appropriate memory region is
793b889744eSMeador Inge // returned. Otherwise, a nullptr is returned.
7943837f427SRui Ueyama MemoryRegion *LinkerScript::findMemoryRegion(OutputSection *sec) {
795b889744eSMeador Inge   // If a memory region name was specified in the output section command,
796b889744eSMeador Inge   // then try to find that region first.
7973837f427SRui Ueyama   if (!sec->memoryRegionName.empty()) {
7983837f427SRui Ueyama     if (MemoryRegion *m = memoryRegions.lookup(sec->memoryRegionName))
7993837f427SRui Ueyama       return m;
8003837f427SRui Ueyama     error("memory region '" + sec->memoryRegionName + "' not declared");
801b889744eSMeador Inge     return nullptr;
802b889744eSMeador Inge   }
803b889744eSMeador Inge 
804d7c5400fSRui Ueyama   // If at least one memory region is defined, all sections must
805d7c5400fSRui Ueyama   // belong to some memory region. Otherwise, we don't need to do
806d7c5400fSRui Ueyama   // anything for memory regions.
8073837f427SRui Ueyama   if (memoryRegions.empty())
808b889744eSMeador Inge     return nullptr;
809b889744eSMeador Inge 
810b889744eSMeador Inge   // See if a region can be found by matching section flags.
8113837f427SRui Ueyama   for (auto &pair : memoryRegions) {
8123837f427SRui Ueyama     MemoryRegion *m = pair.second;
8133837f427SRui Ueyama     if ((m->flags & sec->flags) && (m->negFlags & sec->flags) == 0)
8143837f427SRui Ueyama       return m;
815b889744eSMeador Inge   }
816b889744eSMeador Inge 
817b889744eSMeador Inge   // Otherwise, no suitable region was found.
8183837f427SRui Ueyama   if (sec->flags & SHF_ALLOC)
8193837f427SRui Ueyama     error("no memory region specified for section '" + sec->name + "'");
820b889744eSMeador Inge   return nullptr;
821b889744eSMeador Inge }
822b889744eSMeador Inge 
8233837f427SRui Ueyama static OutputSection *findFirstSection(PhdrEntry *load) {
8243837f427SRui Ueyama   for (OutputSection *sec : outputSections)
8253837f427SRui Ueyama     if (sec->ptLoad == load)
8263837f427SRui Ueyama       return sec;
827acf8cef8SGeorge Rimar   return nullptr;
828acf8cef8SGeorge Rimar }
829acf8cef8SGeorge Rimar 
8300b1b695aSRui Ueyama // This function assigns offsets to input sections and an output section
8310b1b695aSRui Ueyama // for a single sections command (e.g. ".text { *(.text); }").
8323837f427SRui Ueyama void LinkerScript::assignOffsets(OutputSection *sec) {
8333837f427SRui Ueyama   if (!(sec->flags & SHF_ALLOC))
8343837f427SRui Ueyama     dot = 0;
835679828ffSRafael Espindola 
836*bb4a36eaSFangrui Song   bool prevLMARegionIsDefault = ctx->lmaRegion == nullptr;
8373837f427SRui Ueyama   ctx->memRegion = sec->memRegion;
8383837f427SRui Ueyama   ctx->lmaRegion = sec->lmaRegion;
8393837f427SRui Ueyama   if (ctx->memRegion)
8403837f427SRui Ueyama     dot = ctx->memRegion->curPos;
841c2dffe3aSGeorge Rimar 
8423837f427SRui Ueyama   if ((sec->flags & SHF_ALLOC) && sec->addrExpr)
8433837f427SRui Ueyama     setDot(sec->addrExpr, sec->location, false);
84414ef9b30SRui Ueyama 
845179dc276SFangrui Song   // If the address of the section has been moved forward by an explicit
846179dc276SFangrui Song   // expression so that it now starts past the current curPos of the enclosing
847179dc276SFangrui Song   // region, we need to expand the current region to account for the space
848179dc276SFangrui Song   // between the previous section, if any, and the start of this section.
849179dc276SFangrui Song   if (ctx->memRegion && ctx->memRegion->curPos < dot)
850179dc276SFangrui Song     expandMemoryRegion(ctx->memRegion, dot - ctx->memRegion->curPos,
851179dc276SFangrui Song                        ctx->memRegion->name, sec->name);
852179dc276SFangrui Song 
8533837f427SRui Ueyama   switchTo(sec);
85475702389SRafael Espindola 
855*bb4a36eaSFangrui Song   // ctx->lmaOffset is LMA minus VMA. If LMA is explicitly specified via AT() or
856*bb4a36eaSFangrui Song   // AT>, recompute ctx->lmaOffset; otherwise, if both previous/current LMA
857*bb4a36eaSFangrui Song   // region is the default, reuse previous lmaOffset; otherwise, reset lmaOffset
858*bb4a36eaSFangrui Song   // to 0. This emulates heuristics described in
859*bb4a36eaSFangrui Song   // https://sourceware.org/binutils/docs/ld/Output-Section-LMA.html
8603837f427SRui Ueyama   if (sec->lmaExpr)
8613837f427SRui Ueyama     ctx->lmaOffset = sec->lmaExpr().getValue() - dot;
862*bb4a36eaSFangrui Song   else if (MemoryRegion *mr = sec->lmaRegion)
863e21b9ca7SFangrui Song     ctx->lmaOffset = alignTo(mr->curPos, sec->alignment) - dot;
864*bb4a36eaSFangrui Song   else if (!prevLMARegionIsDefault)
865*bb4a36eaSFangrui Song     ctx->lmaOffset = 0;
8665d01a8beSGeorge Rimar 
867*bb4a36eaSFangrui Song   // Propagate ctx->lmaOffset to the first "non-header" section.
8683837f427SRui Ueyama   if (PhdrEntry *l = ctx->outSec->ptLoad)
8693837f427SRui Ueyama     if (sec == findFirstSection(l))
8703837f427SRui Ueyama       l->lmaOffset = ctx->lmaOffset;
8710b1b695aSRui Ueyama 
8720029f214SGeorge Rimar   // We can call this method multiple times during the creation of
8730029f214SGeorge Rimar   // thunks and want to start over calculation each time.
8743837f427SRui Ueyama   sec->size = 0;
8758d0efdd5SJames Henderson 
8762f4c121dSRui Ueyama   // We visited SectionsCommands from processSectionCommands to
8772f4c121dSRui Ueyama   // layout sections. Now, we visit SectionsCommands again to fix
8782f4c121dSRui Ueyama   // section offsets.
8793837f427SRui Ueyama   for (BaseCommand *base : sec->sectionCommands) {
8802f4c121dSRui Ueyama     // This handles the assignments to symbol or to the dot.
8813837f427SRui Ueyama     if (auto *cmd = dyn_cast<SymbolAssignment>(base)) {
8823837f427SRui Ueyama       cmd->addr = dot;
8833837f427SRui Ueyama       assignSymbol(cmd, true);
8843837f427SRui Ueyama       cmd->size = dot - cmd->addr;
8852f4c121dSRui Ueyama       continue;
8862f4c121dSRui Ueyama     }
8872f4c121dSRui Ueyama 
8882f4c121dSRui Ueyama     // Handle BYTE(), SHORT(), LONG(), or QUAD().
8893837f427SRui Ueyama     if (auto *cmd = dyn_cast<ByteCommand>(base)) {
8903837f427SRui Ueyama       cmd->offset = dot - ctx->outSec->addr;
8913837f427SRui Ueyama       dot += cmd->size;
8923837f427SRui Ueyama       expandOutputSection(cmd->size);
8932f4c121dSRui Ueyama       continue;
8942f4c121dSRui Ueyama     }
8952f4c121dSRui Ueyama 
8962f4c121dSRui Ueyama     // Handle a single input section description command.
8972f4c121dSRui Ueyama     // It calculates and assigns the offsets for each section and also
8982f4c121dSRui Ueyama     // updates the output section size.
8993837f427SRui Ueyama     for (InputSection *sec : cast<InputSectionDescription>(base)->sections)
9003837f427SRui Ueyama       output(sec);
9012f4c121dSRui Ueyama   }
9022f4c121dSRui Ueyama }
903d3190795SRafael Espindola 
9043837f427SRui Ueyama static bool isDiscardable(OutputSection &sec) {
9053837f427SRui Ueyama   if (sec.name == "/DISCARD/")
906abb7484cSFangrui Song     return true;
907abb7484cSFangrui Song 
908852bd5c0SRafael Espindola   // We do not remove empty sections that are explicitly
909852bd5c0SRafael Espindola   // assigned to any segment.
9103837f427SRui Ueyama   if (!sec.phdrs.empty())
911852bd5c0SRafael Espindola     return false;
912852bd5c0SRafael Espindola 
913dee900aeSGeorge Rimar   // We do not want to remove OutputSections with expressions that reference
914dee900aeSGeorge Rimar   // symbols even if the OutputSection is empty. We want to ensure that the
915dee900aeSGeorge Rimar   // expressions can be evaluated and report an error if they cannot.
9163837f427SRui Ueyama   if (sec.expressionsUseSymbols)
917852bd5c0SRafael Espindola     return false;
918852bd5c0SRafael Espindola 
919dee900aeSGeorge Rimar   // OutputSections may be referenced by name in ADDR and LOADADDR expressions,
920dee900aeSGeorge Rimar   // as an empty Section can has a valid VMA and LMA we keep the OutputSection
921dee900aeSGeorge Rimar   // to maintain the integrity of the other Expression.
9223837f427SRui Ueyama   if (sec.usedInExpression)
923dee900aeSGeorge Rimar     return false;
924dee900aeSGeorge Rimar 
9253837f427SRui Ueyama   for (BaseCommand *base : sec.sectionCommands) {
9263837f427SRui Ueyama     if (auto cmd = dyn_cast<SymbolAssignment>(base))
927b427d4ecSJames Henderson       // Don't create empty output sections just for unreferenced PROVIDE
928b427d4ecSJames Henderson       // symbols.
9293837f427SRui Ueyama       if (cmd->name != "." && !cmd->sym)
930b427d4ecSJames Henderson         continue;
931b427d4ecSJames Henderson 
9323837f427SRui Ueyama     if (!isa<InputSectionDescription>(*base))
933852bd5c0SRafael Espindola       return false;
934b427d4ecSJames Henderson   }
935afbf90aeSGeorge Rimar   return true;
936852bd5c0SRafael Espindola }
937852bd5c0SRafael Espindola 
938b8dd23f5SRui Ueyama void LinkerScript::adjustSectionsBeforeSorting() {
9399546fffbSRafael Espindola   // If the output section contains only symbol assignments, create a
94026fa916dSGeorge Rimar   // corresponding output section. The issue is what to do with linker script
94126fa916dSGeorge Rimar   // like ".foo : { symbol = 42; }". One option would be to convert it to
94226fa916dSGeorge Rimar   // "symbol = 42;". That is, move the symbol out of the empty section
94326fa916dSGeorge Rimar   // description. That seems to be what bfd does for this simple case. The
94426fa916dSGeorge Rimar   // problem is that this is not completely general. bfd will give up and
94526fa916dSGeorge Rimar   // create a dummy section too if there is a ". = . + 1" inside the section
94626fa916dSGeorge Rimar   // for example.
94726fa916dSGeorge Rimar   // Given that we want to create the section, we have to worry what impact
94826fa916dSGeorge Rimar   // it will have on the link. For example, if we just create a section with
94926fa916dSGeorge Rimar   // 0 for flags, it would change which PT_LOADs are created.
950ffac3ed3SFangrui Song   // We could remember that particular section is dummy and ignore it in
95126fa916dSGeorge Rimar   // other parts of the linker, but unfortunately there are quite a few places
95226fa916dSGeorge Rimar   // that would need to change:
95326fa916dSGeorge Rimar   //   * The program header creation.
95426fa916dSGeorge Rimar   //   * The orphan section placement.
95526fa916dSGeorge Rimar   //   * The address assignment.
95626fa916dSGeorge Rimar   // The other option is to pick flags that minimize the impact the section
95726fa916dSGeorge Rimar   // will have on the rest of the linker. That is why we copy the flags from
95826fa916dSGeorge Rimar   // the previous sections. Only a few flags are needed to keep the impact low.
9593837f427SRui Ueyama   uint64_t flags = SHF_ALLOC;
960660c9ab9SRafael Espindola 
9613837f427SRui Ueyama   for (BaseCommand *&cmd : sectionCommands) {
9623837f427SRui Ueyama     auto *sec = dyn_cast<OutputSection>(cmd);
9633837f427SRui Ueyama     if (!sec)
9649546fffbSRafael Espindola       continue;
9659546fffbSRafael Espindola 
966b75d19c3SRui Ueyama     // Handle align (e.g. ".foo : ALIGN(16) { ... }").
9673837f427SRui Ueyama     if (sec->alignExpr)
9683837f427SRui Ueyama       sec->alignment =
9693837f427SRui Ueyama           std::max<uint32_t>(sec->alignment, sec->alignExpr().getValue());
970b75d19c3SRui Ueyama 
97106f3b094SPeter Collingbourne     // The input section might have been removed (if it was an empty synthetic
97206f3b094SPeter Collingbourne     // section), but we at least know the flags.
9733837f427SRui Ueyama     if (sec->hasInputSections)
9743837f427SRui Ueyama       flags = sec->flags;
975852bd5c0SRafael Espindola 
976afbf90aeSGeorge Rimar     // We do not want to keep any special flags for output section
977afbf90aeSGeorge Rimar     // in case it is empty.
9784e8116f4SAndrew Ng     bool isEmpty = (getFirstInputSection(sec) == nullptr);
9793837f427SRui Ueyama     if (isEmpty)
9803837f427SRui Ueyama       sec->flags = flags & ((sec->nonAlloc ? 0 : (uint64_t)SHF_ALLOC) |
98174780852SFangrui Song                             SHF_WRITE | SHF_EXECINSTR);
982afbf90aeSGeorge Rimar 
9833837f427SRui Ueyama     if (isEmpty && isDiscardable(*sec)) {
9843837f427SRui Ueyama       sec->markDead();
9853837f427SRui Ueyama       cmd = nullptr;
9869546fffbSRafael Espindola     }
987852bd5c0SRafael Espindola   }
988db1a0624SGeorge Rimar 
989db1a0624SGeorge Rimar   // It is common practice to use very generic linker scripts. So for any
990db1a0624SGeorge Rimar   // given run some of the output sections in the script will be empty.
991db1a0624SGeorge Rimar   // We could create corresponding empty output sections, but that would
992db1a0624SGeorge Rimar   // clutter the output.
993db1a0624SGeorge Rimar   // We instead remove trivially empty sections. The bfd linker seems even
994db1a0624SGeorge Rimar   // more aggressive at removing them.
9953837f427SRui Ueyama   llvm::erase_if(sectionCommands, [&](BaseCommand *base) { return !base; });
996f7a17448SRafael Espindola }
997f7a17448SRafael Espindola 
998b8dd23f5SRui Ueyama void LinkerScript::adjustSectionsAfterSorting() {
999feed7506SRafael Espindola   // Try and find an appropriate memory region to assign offsets in.
10003837f427SRui Ueyama   for (BaseCommand *base : sectionCommands) {
10013837f427SRui Ueyama     if (auto *sec = dyn_cast<OutputSection>(base)) {
10023837f427SRui Ueyama       if (!sec->lmaRegionName.empty()) {
10033837f427SRui Ueyama         if (MemoryRegion *m = memoryRegions.lookup(sec->lmaRegionName))
10043837f427SRui Ueyama           sec->lmaRegion = m;
1005567175f3SRafael Espindola         else
10063837f427SRui Ueyama           error("memory region '" + sec->lmaRegionName + "' not declared");
1007567175f3SRafael Espindola       }
10083837f427SRui Ueyama       sec->memRegion = findMemoryRegion(sec);
1009d1960dc0SRafael Espindola     }
1010d1960dc0SRafael Espindola   }
1011feed7506SRafael Espindola 
1012f7a17448SRafael Espindola   // If output section command doesn't specify any segments,
1013f7a17448SRafael Espindola   // and we haven't previously assigned any section to segment,
1014f7a17448SRafael Espindola   // then we simply assign section to the very first load segment.
1015f7a17448SRafael Espindola   // Below is an example of such linker script:
1016f7a17448SRafael Espindola   // PHDRS { seg PT_LOAD; }
1017f7a17448SRafael Espindola   // SECTIONS { .aaa : { *(.aaa) } }
10183837f427SRui Ueyama   std::vector<StringRef> defPhdrs;
10193837f427SRui Ueyama   auto firstPtLoad = llvm::find_if(phdrsCommands, [](const PhdrsCommand &cmd) {
10203837f427SRui Ueyama     return cmd.type == PT_LOAD;
1021367bfce6SRafael Espindola   });
10223837f427SRui Ueyama   if (firstPtLoad != phdrsCommands.end())
10233837f427SRui Ueyama     defPhdrs.push_back(firstPtLoad->name);
1024f7a17448SRafael Espindola 
1025f7a17448SRafael Espindola   // Walk the commands and propagate the program headers to commands that don't
1026f7a17448SRafael Espindola   // explicitly specify them.
10273837f427SRui Ueyama   for (BaseCommand *base : sectionCommands) {
10283837f427SRui Ueyama     auto *sec = dyn_cast<OutputSection>(base);
10293837f427SRui Ueyama     if (!sec)
1030f7a17448SRafael Espindola       continue;
10318f99f73cSRui Ueyama 
10323837f427SRui Ueyama     if (sec->phdrs.empty()) {
1033a020d348SAndrew Ng       // To match the bfd linker script behaviour, only propagate program
1034a020d348SAndrew Ng       // headers to sections that are allocated.
10353837f427SRui Ueyama       if (sec->flags & SHF_ALLOC)
10363837f427SRui Ueyama         sec->phdrs = defPhdrs;
1037a020d348SAndrew Ng     } else {
10383837f427SRui Ueyama       defPhdrs = sec->phdrs;
1039f7a17448SRafael Espindola     }
1040a020d348SAndrew Ng   }
10419546fffbSRafael Espindola }
10429546fffbSRafael Espindola 
10433837f427SRui Ueyama static uint64_t computeBase(uint64_t min, bool allocateHeaders) {
1044e75b42eeSRafael Espindola   // If there is no SECTIONS or if the linkerscript is explicit about program
1045e75b42eeSRafael Espindola   // headers, do our best to allocate them.
10463837f427SRui Ueyama   if (!script->hasSectionsCommand || allocateHeaders)
1047e75b42eeSRafael Espindola     return 0;
1048e75b42eeSRafael Espindola   // Otherwise only allocate program headers if that would not add a page.
10493837f427SRui Ueyama   return alignDown(min, config->maxPageSize);
1050e75b42eeSRafael Espindola }
1051e75b42eeSRafael Espindola 
105206bb7dfbSFangrui Song // When the SECTIONS command is used, try to find an address for the file and
105306bb7dfbSFangrui Song // program headers output sections, which can be added to the first PT_LOAD
105406bb7dfbSFangrui Song // segment when program headers are created.
1055b93c5b9fSPetr Hosek //
105606bb7dfbSFangrui Song // We check if the headers fit below the first allocated section. If there isn't
105706bb7dfbSFangrui Song // enough space for these sections, we'll remove them from the PT_LOAD segment,
105806bb7dfbSFangrui Song // and we'll also remove the PT_PHDR segment.
10593837f427SRui Ueyama void LinkerScript::allocateHeaders(std::vector<PhdrEntry *> &phdrs) {
10603837f427SRui Ueyama   uint64_t min = std::numeric_limits<uint64_t>::max();
10613837f427SRui Ueyama   for (OutputSection *sec : outputSections)
10623837f427SRui Ueyama     if (sec->flags & SHF_ALLOC)
10633837f427SRui Ueyama       min = std::min<uint64_t>(min, sec->addr);
10645aedebffSPeter Smith 
10653837f427SRui Ueyama   auto it = llvm::find_if(
10663837f427SRui Ueyama       phdrs, [](const PhdrEntry *e) { return e->p_type == PT_LOAD; });
10673837f427SRui Ueyama   if (it == phdrs.end())
1068d971e703SGeorge Rimar     return;
10693837f427SRui Ueyama   PhdrEntry *firstPTLoad = *it;
107002ed7575SRafael Espindola 
10713837f427SRui Ueyama   bool hasExplicitHeaders =
10723837f427SRui Ueyama       llvm::any_of(phdrsCommands, [](const PhdrsCommand &cmd) {
10733837f427SRui Ueyama         return cmd.hasPhdrs || cmd.hasFilehdr;
1074e75b42eeSRafael Espindola       });
10753837f427SRui Ueyama   bool paged = !config->omagic && !config->nmagic;
10763837f427SRui Ueyama   uint64_t headerSize = getHeaderSize();
10773837f427SRui Ueyama   if ((paged || hasExplicitHeaders) &&
10783837f427SRui Ueyama       headerSize <= min - computeBase(min, hasExplicitHeaders)) {
10793837f427SRui Ueyama     min = alignDown(min - headerSize, config->maxPageSize);
10803837f427SRui Ueyama     Out::elfHeader->addr = min;
10813837f427SRui Ueyama     Out::programHeaders->addr = min + Out::elfHeader->size;
1082d971e703SGeorge Rimar     return;
108302ed7575SRafael Espindola   }
108402ed7575SRafael Espindola 
1085e75b42eeSRafael Espindola   // Error if we were explicitly asked to allocate headers.
10863837f427SRui Ueyama   if (hasExplicitHeaders)
1087e75b42eeSRafael Espindola     error("could not allocate headers");
1088e75b42eeSRafael Espindola 
10893837f427SRui Ueyama   Out::elfHeader->ptLoad = nullptr;
10903837f427SRui Ueyama   Out::programHeaders->ptLoad = nullptr;
10913837f427SRui Ueyama   firstPTLoad->firstSec = findFirstSection(firstPTLoad);
109202ed7575SRafael Espindola 
10933837f427SRui Ueyama   llvm::erase_if(phdrs,
10943837f427SRui Ueyama                  [](const PhdrEntry *e) { return e->p_type == PT_PHDR; });
109502ed7575SRafael Espindola }
109602ed7575SRafael Espindola 
1097ac27de9dSRui Ueyama LinkerScript::AddressState::AddressState() {
10983837f427SRui Ueyama   for (auto &mri : script->memoryRegions) {
10993837f427SRui Ueyama     MemoryRegion *mr = mri.second;
110092b5b980SFangrui Song     mr->curPos = (mr->origin)().getValue();
1101906e9a18SPeter Smith   }
1102906e9a18SPeter Smith }
1103906e9a18SPeter Smith 
1104f9b04fd9SGeorge Rimar // Here we assign addresses as instructed by linker script SECTIONS
1105f9b04fd9SGeorge Rimar // sub-commands. Doing that allows us to use final VA values, so here
1106f9b04fd9SGeorge Rimar // we also handle rest commands like symbol assignments and ASSERTs.
1107debcac9fSFangrui Song // Returns a symbol that has changed its section or value, or nullptr if no
1108debcac9fSFangrui Song // symbol has changed.
1109debcac9fSFangrui Song const Defined *LinkerScript::assignAddresses() {
111006bb7dfbSFangrui Song   if (script->hasSectionsCommand) {
111106bb7dfbSFangrui Song     // With a linker script, assignment of addresses to headers is covered by
111206bb7dfbSFangrui Song     // allocateHeaders().
111306bb7dfbSFangrui Song     dot = config->imageBase.getValueOr(0);
111406bb7dfbSFangrui Song   } else {
111506bb7dfbSFangrui Song     // Assign addresses to headers right now.
111606bb7dfbSFangrui Song     dot = target->getImageBase();
111706bb7dfbSFangrui Song     Out::elfHeader->addr = dot;
111806bb7dfbSFangrui Song     Out::programHeaders->addr = dot + Out::elfHeader->size;
111906bb7dfbSFangrui Song     dot += getHeaderSize();
112006bb7dfbSFangrui Song   }
112118d19687SRui Ueyama 
11226ba79920SJonas Devlieghere   auto deleter = std::make_unique<AddressState>();
11233837f427SRui Ueyama   ctx = deleter.get();
11243837f427SRui Ueyama   errorOnMissingSection = true;
11253837f427SRui Ueyama   switchTo(aether);
112606f4743aSRafael Espindola 
1127debcac9fSFangrui Song   SymbolAssignmentMap oldValues = getSymbolAssignmentValues(sectionCommands);
11283837f427SRui Ueyama   for (BaseCommand *base : sectionCommands) {
11293837f427SRui Ueyama     if (auto *cmd = dyn_cast<SymbolAssignment>(base)) {
11303837f427SRui Ueyama       cmd->addr = dot;
11313837f427SRui Ueyama       assignSymbol(cmd, false);
11323837f427SRui Ueyama       cmd->size = dot - cmd->addr;
113305ef4cffSRui Ueyama       continue;
1134652852c5SGeorge Rimar     }
11353837f427SRui Ueyama     assignOffsets(cast<OutputSection>(base));
1136a14b13d8SGeorge Rimar   }
1137debcac9fSFangrui Song 
11383837f427SRui Ueyama   ctx = nullptr;
1139debcac9fSFangrui Song   return getChangedSymbolAssignment(oldValues);
1140fb8978fcSDima Stepanov }
1141652852c5SGeorge Rimar 
1142464daadcSRui Ueyama // Creates program headers as instructed by PHDRS linker script command.
1143aa354187SGeorge Rimar std::vector<PhdrEntry *> LinkerScript::createPhdrs() {
11443837f427SRui Ueyama   std::vector<PhdrEntry *> ret;
1145bbe38602SEugene Leviant 
1146464daadcSRui Ueyama   // Process PHDRS and FILEHDR keywords because they are not
1147464daadcSRui Ueyama   // real output sections and cannot be added in the following loop.
11483837f427SRui Ueyama   for (const PhdrsCommand &cmd : phdrsCommands) {
11493837f427SRui Ueyama     PhdrEntry *phdr = make<PhdrEntry>(cmd.type, cmd.flags ? *cmd.flags : PF_R);
1150bbe38602SEugene Leviant 
11513837f427SRui Ueyama     if (cmd.hasFilehdr)
11523837f427SRui Ueyama       phdr->add(Out::elfHeader);
11533837f427SRui Ueyama     if (cmd.hasPhdrs)
11543837f427SRui Ueyama       phdr->add(Out::programHeaders);
115556b21c86SEugene Leviant 
11563837f427SRui Ueyama     if (cmd.lmaExpr) {
11573837f427SRui Ueyama       phdr->p_paddr = cmd.lmaExpr().getValue();
11583837f427SRui Ueyama       phdr->hasLMA = true;
115956b21c86SEugene Leviant     }
11603837f427SRui Ueyama     ret.push_back(phdr);
1161bbe38602SEugene Leviant   }
1162bbe38602SEugene Leviant 
1163464daadcSRui Ueyama   // Add output sections to program headers.
11643837f427SRui Ueyama   for (OutputSection *sec : outputSections) {
1165bbe38602SEugene Leviant     // Assign headers specified by linker script
11663837f427SRui Ueyama     for (size_t id : getPhdrIndices(sec)) {
11673837f427SRui Ueyama       ret[id]->add(sec);
11683837f427SRui Ueyama       if (!phdrsCommands[id].flags.hasValue())
11693837f427SRui Ueyama         ret[id]->p_flags |= sec->getPhdrFlags();
1170bbe38602SEugene Leviant     }
1171bbe38602SEugene Leviant   }
11723837f427SRui Ueyama   return ret;
1173bbe38602SEugene Leviant }
1174bbe38602SEugene Leviant 
1175e03ba023SRui Ueyama // Returns true if we should emit an .interp section.
1176e03ba023SRui Ueyama //
1177e03ba023SRui Ueyama // We usually do. But if PHDRS commands are given, and
1178e03ba023SRui Ueyama // no PT_INTERP is there, there's no place to emit an
1179e03ba023SRui Ueyama // .interp, so we don't do that in that case.
1180e03ba023SRui Ueyama bool LinkerScript::needsInterpSection() {
11813837f427SRui Ueyama   if (phdrsCommands.empty())
1182e03ba023SRui Ueyama     return true;
11833837f427SRui Ueyama   for (PhdrsCommand &cmd : phdrsCommands)
11843837f427SRui Ueyama     if (cmd.type == PT_INTERP)
1185e31d9886SRui Ueyama       return true;
1186e03ba023SRui Ueyama   return false;
1187f9bc3bd2SEugene Leviant }
1188f9bc3bd2SEugene Leviant 
11893837f427SRui Ueyama ExprValue LinkerScript::getSymbolValue(StringRef name, const Twine &loc) {
11903837f427SRui Ueyama   if (name == ".") {
11913837f427SRui Ueyama     if (ctx)
11923837f427SRui Ueyama       return {ctx->outSec, false, dot - ctx->outSec->addr, loc};
11933837f427SRui Ueyama     error(loc + ": unable to get location counter value");
11947e5b0a59SGeorge Rimar     return 0;
11957e5b0a59SGeorge Rimar   }
1196f5733500SRui Ueyama 
11973837f427SRui Ueyama   if (Symbol *sym = symtab->find(name)) {
11983837f427SRui Ueyama     if (auto *ds = dyn_cast<Defined>(sym))
11993837f427SRui Ueyama       return {ds->section, false, ds->value, loc};
12003837f427SRui Ueyama     if (isa<SharedSymbol>(sym))
12013837f427SRui Ueyama       if (!errorOnMissingSection)
12023837f427SRui Ueyama         return {nullptr, false, 0, loc};
1203de38b3d2SRafael Espindola   }
1204f5733500SRui Ueyama 
12053837f427SRui Ueyama   error(loc + ": symbol not found: " + name);
1206884e786dSGeorge Rimar   return 0;
1207884e786dSGeorge Rimar }
1208884e786dSGeorge Rimar 
1209656be311SRui Ueyama // Returns the index of the segment named Name.
12103837f427SRui Ueyama static Optional<size_t> getPhdrIndex(ArrayRef<PhdrsCommand> vec,
12113837f427SRui Ueyama                                      StringRef name) {
12123837f427SRui Ueyama   for (size_t i = 0; i < vec.size(); ++i)
12133837f427SRui Ueyama     if (vec[i].name == name)
12143837f427SRui Ueyama       return i;
1215656be311SRui Ueyama   return None;
1216656be311SRui Ueyama }
1217656be311SRui Ueyama 
12182c923c2cSRafael Espindola // Returns indices of ELF headers containing specific section. Each index is a
12192c923c2cSRafael Espindola // zero based number of ELF header listed within PHDRS {} script block.
12203837f427SRui Ueyama std::vector<size_t> LinkerScript::getPhdrIndices(OutputSection *cmd) {
12213837f427SRui Ueyama   std::vector<size_t> ret;
1222656be311SRui Ueyama 
12233837f427SRui Ueyama   for (StringRef s : cmd->phdrs) {
12243837f427SRui Ueyama     if (Optional<size_t> idx = getPhdrIndex(phdrsCommands, s))
12253837f427SRui Ueyama       ret.push_back(*idx);
12263837f427SRui Ueyama     else if (s != "NONE")
12273ff3c698SJames Henderson       error(cmd->location + ": program header '" + s +
1228656be311SRui Ueyama             "' is not listed in PHDRS");
1229bbe38602SEugene Leviant   }
12303837f427SRui Ueyama   return ret;
123129c5a2a9SRui Ueyama }
1232bd8cfe65SFangrui Song 
1233bd8cfe65SFangrui Song } // namespace elf
1234bd8cfe65SFangrui Song } // namespace lld
1235