197c3811cSEd Maste //===- LinkerScript.cpp ---------------------------------------------------===//
297c3811cSEd Maste //
397c3811cSEd Maste //                             The LLVM Linker
497c3811cSEd Maste //
597c3811cSEd Maste // This file is distributed under the University of Illinois Open Source
697c3811cSEd Maste // License. See LICENSE.TXT for details.
797c3811cSEd Maste //
897c3811cSEd Maste //===----------------------------------------------------------------------===//
997c3811cSEd Maste //
1097c3811cSEd Maste // This file contains the parser/evaluator of the linker script.
1197c3811cSEd Maste //
1297c3811cSEd Maste //===----------------------------------------------------------------------===//
1397c3811cSEd Maste 
1497c3811cSEd Maste #include "LinkerScript.h"
1597c3811cSEd Maste #include "Config.h"
1697c3811cSEd Maste #include "InputSection.h"
1797c3811cSEd Maste #include "OutputSections.h"
1897c3811cSEd Maste #include "SymbolTable.h"
191189dbaaSDimitry Andric #include "Symbols.h"
201189dbaaSDimitry Andric #include "SyntheticSections.h"
21302affcbSDimitry Andric #include "Target.h"
221189dbaaSDimitry Andric #include "Writer.h"
2310dc89a5SDimitry Andric #include "lld/Common/Memory.h"
244ba319b5SDimitry Andric #include "lld/Common/Strings.h"
2510dc89a5SDimitry Andric #include "lld/Common/Threads.h"
261189dbaaSDimitry Andric #include "llvm/ADT/STLExtras.h"
271189dbaaSDimitry Andric #include "llvm/ADT/StringRef.h"
28db17bf38SDimitry Andric #include "llvm/BinaryFormat/ELF.h"
291189dbaaSDimitry Andric #include "llvm/Support/Casting.h"
301189dbaaSDimitry Andric #include "llvm/Support/Endian.h"
311189dbaaSDimitry Andric #include "llvm/Support/ErrorHandling.h"
3297c3811cSEd Maste #include "llvm/Support/FileSystem.h"
3397c3811cSEd Maste #include "llvm/Support/Path.h"
341189dbaaSDimitry Andric #include <algorithm>
351189dbaaSDimitry Andric #include <cassert>
361189dbaaSDimitry Andric #include <cstddef>
371189dbaaSDimitry Andric #include <cstdint>
381189dbaaSDimitry Andric #include <iterator>
391189dbaaSDimitry Andric #include <limits>
401189dbaaSDimitry Andric #include <string>
411189dbaaSDimitry Andric #include <vector>
4297c3811cSEd Maste 
4397c3811cSEd Maste using namespace llvm;
4497c3811cSEd Maste using namespace llvm::ELF;
4597c3811cSEd Maste using namespace llvm::object;
461189dbaaSDimitry Andric using namespace llvm::support::endian;
4797c3811cSEd Maste using namespace lld;
4897c3811cSEd Maste using namespace lld::elf;
4997c3811cSEd Maste 
5046b69c69SDimitry Andric LinkerScript *elf::Script;
5146b69c69SDimitry Andric 
getOutputSectionVA(SectionBase * InputSec,StringRef Loc)5210dc89a5SDimitry Andric static uint64_t getOutputSectionVA(SectionBase *InputSec, StringRef Loc) {
5310dc89a5SDimitry Andric   if (OutputSection *OS = InputSec->getOutputSection())
5410dc89a5SDimitry Andric     return OS->Addr;
5510dc89a5SDimitry Andric   error(Loc + ": unable to evaluate expression: input section " +
5610dc89a5SDimitry Andric         InputSec->Name + " has no output section assigned");
5710dc89a5SDimitry Andric   return 0;
585517e702SDimitry Andric }
5910dc89a5SDimitry Andric 
getValue() const6010dc89a5SDimitry Andric uint64_t ExprValue::getValue() const {
6110dc89a5SDimitry Andric   if (Sec)
6210dc89a5SDimitry Andric     return alignTo(Sec->getOffset(Val) + getOutputSectionVA(Sec, Loc),
6310dc89a5SDimitry Andric                    Alignment);
6489cb50c9SDimitry Andric   return alignTo(Val, Alignment);
6546b69c69SDimitry Andric }
6646b69c69SDimitry Andric 
getSecAddr() const6746b69c69SDimitry Andric uint64_t ExprValue::getSecAddr() const {
6846b69c69SDimitry Andric   if (Sec)
6910dc89a5SDimitry Andric     return Sec->getOffset(0) + getOutputSectionVA(Sec, Loc);
7046b69c69SDimitry Andric   return 0;
7146b69c69SDimitry Andric }
7297c3811cSEd Maste 
getSectionOffset() const7310dc89a5SDimitry Andric uint64_t ExprValue::getSectionOffset() const {
7410dc89a5SDimitry Andric   // If the alignment is trivial, we don't have to compute the full
7510dc89a5SDimitry Andric   // value to know the offset. This allows this function to succeed in
7610dc89a5SDimitry Andric   // cases where the output section is not yet known.
774ba319b5SDimitry Andric   if (Alignment == 1 && (!Sec || !Sec->getOutputSection()))
7810dc89a5SDimitry Andric     return Val;
7910dc89a5SDimitry Andric   return getValue() - getSecAddr();
8097c3811cSEd Maste }
8197c3811cSEd Maste 
createOutputSection(StringRef Name,StringRef Location)8210dc89a5SDimitry Andric OutputSection *LinkerScript::createOutputSection(StringRef Name,
8310dc89a5SDimitry Andric                                                  StringRef Location) {
8410dc89a5SDimitry Andric   OutputSection *&SecRef = NameToOutputSection[Name];
8510dc89a5SDimitry Andric   OutputSection *Sec;
8610dc89a5SDimitry Andric   if (SecRef && SecRef->Location.empty()) {
87f9448bf3SDimitry Andric     // There was a forward reference.
8810dc89a5SDimitry Andric     Sec = SecRef;
89f9448bf3SDimitry Andric   } else {
9010dc89a5SDimitry Andric     Sec = make<OutputSection>(Name, SHT_NOBITS, 0);
9110dc89a5SDimitry Andric     if (!SecRef)
9210dc89a5SDimitry Andric       SecRef = Sec;
93f9448bf3SDimitry Andric   }
9410dc89a5SDimitry Andric   Sec->Location = Location;
9510dc89a5SDimitry Andric   return Sec;
9697c3811cSEd Maste }
9797c3811cSEd Maste 
getOrCreateOutputSection(StringRef Name)9810dc89a5SDimitry Andric OutputSection *LinkerScript::getOrCreateOutputSection(StringRef Name) {
9910dc89a5SDimitry Andric   OutputSection *&CmdRef = NameToOutputSection[Name];
100f9448bf3SDimitry Andric   if (!CmdRef)
10110dc89a5SDimitry Andric     CmdRef = make<OutputSection>(Name, SHT_PROGBITS, 0);
102f9448bf3SDimitry Andric   return CmdRef;
10397c3811cSEd Maste }
10497c3811cSEd Maste 
1054ba319b5SDimitry Andric // Expands the memory region by the specified size.
expandMemoryRegion(MemoryRegion * MemRegion,uint64_t Size,StringRef RegionName,StringRef SecName)1064ba319b5SDimitry Andric static void expandMemoryRegion(MemoryRegion *MemRegion, uint64_t Size,
1074ba319b5SDimitry Andric                                StringRef RegionName, StringRef SecName) {
1084ba319b5SDimitry Andric   MemRegion->CurPos += Size;
1094ba319b5SDimitry Andric   uint64_t NewSize = MemRegion->CurPos - MemRegion->Origin;
1104ba319b5SDimitry Andric   if (NewSize > MemRegion->Length)
1114ba319b5SDimitry Andric     error("section '" + SecName + "' will not fit in region '" + RegionName +
1124ba319b5SDimitry Andric           "': overflowed by " + Twine(NewSize - MemRegion->Length) + " bytes");
1134ba319b5SDimitry Andric }
1144ba319b5SDimitry Andric 
expandMemoryRegions(uint64_t Size)1154ba319b5SDimitry Andric void LinkerScript::expandMemoryRegions(uint64_t Size) {
1164ba319b5SDimitry Andric   if (Ctx->MemRegion)
1174ba319b5SDimitry Andric     expandMemoryRegion(Ctx->MemRegion, Size, Ctx->MemRegion->Name,
1184ba319b5SDimitry Andric                        Ctx->OutSec->Name);
1194ba319b5SDimitry Andric   // Only expand the LMARegion if it is different from MemRegion.
1204ba319b5SDimitry Andric   if (Ctx->LMARegion && Ctx->MemRegion != Ctx->LMARegion)
1214ba319b5SDimitry Andric     expandMemoryRegion(Ctx->LMARegion, Size, Ctx->LMARegion->Name,
1224ba319b5SDimitry Andric                        Ctx->OutSec->Name);
1234ba319b5SDimitry Andric }
1244ba319b5SDimitry Andric 
expandOutputSection(uint64_t Size)1254ba319b5SDimitry Andric void LinkerScript::expandOutputSection(uint64_t Size) {
1264ba319b5SDimitry Andric   Ctx->OutSec->Size += Size;
1274ba319b5SDimitry Andric   expandMemoryRegions(Size);
1284ba319b5SDimitry Andric }
1294ba319b5SDimitry Andric 
setDot(Expr E,const Twine & Loc,bool InSec)13046b69c69SDimitry Andric void LinkerScript::setDot(Expr E, const Twine &Loc, bool InSec) {
13146b69c69SDimitry Andric   uint64_t Val = E().getValue();
132c4394386SDimitry Andric   if (Val < Dot && InSec)
13346b69c69SDimitry Andric     error(Loc + ": unable to move location counter backward for: " +
13410dc89a5SDimitry Andric           Ctx->OutSec->Name);
13510dc89a5SDimitry Andric 
13646b69c69SDimitry Andric   // Update to location counter means update to section size.
13746b69c69SDimitry Andric   if (InSec)
1384ba319b5SDimitry Andric     expandOutputSection(Val - Dot);
1394ba319b5SDimitry Andric   else
1404ba319b5SDimitry Andric     expandMemoryRegions(Val - Dot);
1414ba319b5SDimitry Andric 
1424ba319b5SDimitry Andric   Dot = Val;
1434ba319b5SDimitry Andric }
1444ba319b5SDimitry Andric 
1454ba319b5SDimitry Andric // Used for handling linker symbol assignments, for both finalizing
1464ba319b5SDimitry Andric // their values and doing early declarations. Returns true if symbol
1474ba319b5SDimitry Andric // should be defined from linker script.
shouldDefineSym(SymbolAssignment * Cmd)1484ba319b5SDimitry Andric static bool shouldDefineSym(SymbolAssignment *Cmd) {
1494ba319b5SDimitry Andric   if (Cmd->Name == ".")
1504ba319b5SDimitry Andric     return false;
1514ba319b5SDimitry Andric 
1524ba319b5SDimitry Andric   if (!Cmd->Provide)
1534ba319b5SDimitry Andric     return true;
1544ba319b5SDimitry Andric 
1554ba319b5SDimitry Andric   // If a symbol was in PROVIDE(), we need to define it only
1564ba319b5SDimitry Andric   // when it is a referenced undefined symbol.
1574ba319b5SDimitry Andric   Symbol *B = Symtab->find(Cmd->Name);
1584ba319b5SDimitry Andric   if (B && !B->isDefined())
1594ba319b5SDimitry Andric     return true;
1604ba319b5SDimitry Andric   return false;
16146b69c69SDimitry Andric }
16246b69c69SDimitry Andric 
16310dc89a5SDimitry Andric // This function is called from processSectionCommands,
16410dc89a5SDimitry Andric // while we are fixing the output section layout.
addSymbol(SymbolAssignment * Cmd)16510dc89a5SDimitry Andric void LinkerScript::addSymbol(SymbolAssignment *Cmd) {
1664ba319b5SDimitry Andric   if (!shouldDefineSym(Cmd))
16710dc89a5SDimitry Andric     return;
16810dc89a5SDimitry Andric 
16910dc89a5SDimitry Andric   // Define a symbol.
17010dc89a5SDimitry Andric   Symbol *Sym;
17110dc89a5SDimitry Andric   uint8_t Visibility = Cmd->Hidden ? STV_HIDDEN : STV_DEFAULT;
172*b5893f02SDimitry Andric   std::tie(Sym, std::ignore) = Symtab->insert(Cmd->Name, Visibility,
17310dc89a5SDimitry Andric                                               /*CanOmitFromDynSym*/ false,
17410dc89a5SDimitry Andric                                               /*File*/ nullptr);
17510dc89a5SDimitry Andric   ExprValue Value = Cmd->Expression();
17610dc89a5SDimitry Andric   SectionBase *Sec = Value.isAbsolute() ? nullptr : Value.Sec;
17710dc89a5SDimitry Andric 
17810dc89a5SDimitry Andric   // When this function is called, section addresses have not been
17910dc89a5SDimitry Andric   // fixed yet. So, we may or may not know the value of the RHS
18010dc89a5SDimitry Andric   // expression.
18110dc89a5SDimitry Andric   //
18210dc89a5SDimitry Andric   // For example, if an expression is `x = 42`, we know x is always 42.
18310dc89a5SDimitry Andric   // However, if an expression is `x = .`, there's no way to know its
18410dc89a5SDimitry Andric   // value at the moment.
18510dc89a5SDimitry Andric   //
18610dc89a5SDimitry Andric   // We want to set symbol values early if we can. This allows us to
18710dc89a5SDimitry Andric   // use symbols as variables in linker scripts. Doing so allows us to
18810dc89a5SDimitry Andric   // write expressions like this: `alignment = 16; . = ALIGN(., alignment)`.
18910dc89a5SDimitry Andric   uint64_t SymValue = Value.Sec ? 0 : Value.getValue();
19010dc89a5SDimitry Andric 
19110dc89a5SDimitry Andric   replaceSymbol<Defined>(Sym, nullptr, Cmd->Name, STB_GLOBAL, Visibility,
19210dc89a5SDimitry Andric                          STT_NOTYPE, SymValue, 0, Sec);
19310dc89a5SDimitry Andric   Cmd->Sym = cast<Defined>(Sym);
19410dc89a5SDimitry Andric }
19510dc89a5SDimitry Andric 
1964ba319b5SDimitry Andric // This function is called from LinkerScript::declareSymbols.
1974ba319b5SDimitry Andric // It creates a placeholder symbol if needed.
declareSymbol(SymbolAssignment * Cmd)1984ba319b5SDimitry Andric static void declareSymbol(SymbolAssignment *Cmd) {
1994ba319b5SDimitry Andric   if (!shouldDefineSym(Cmd))
2004ba319b5SDimitry Andric     return;
2014ba319b5SDimitry Andric 
2024ba319b5SDimitry Andric   // We can't calculate final value right now.
2034ba319b5SDimitry Andric   Symbol *Sym;
2044ba319b5SDimitry Andric   uint8_t Visibility = Cmd->Hidden ? STV_HIDDEN : STV_DEFAULT;
205*b5893f02SDimitry Andric   std::tie(Sym, std::ignore) = Symtab->insert(Cmd->Name, Visibility,
2064ba319b5SDimitry Andric                                               /*CanOmitFromDynSym*/ false,
2074ba319b5SDimitry Andric                                               /*File*/ nullptr);
2084ba319b5SDimitry Andric   replaceSymbol<Defined>(Sym, nullptr, Cmd->Name, STB_GLOBAL, Visibility,
2094ba319b5SDimitry Andric                          STT_NOTYPE, 0, 0, nullptr);
2104ba319b5SDimitry Andric   Cmd->Sym = cast<Defined>(Sym);
2114ba319b5SDimitry Andric   Cmd->Provide = false;
212*b5893f02SDimitry Andric   Sym->ScriptDefined = true;
2134ba319b5SDimitry Andric }
2144ba319b5SDimitry Andric 
2154ba319b5SDimitry Andric // This method is used to handle INSERT AFTER statement. Here we rebuild
2164ba319b5SDimitry Andric // the list of script commands to mix sections inserted into.
processInsertCommands()2174ba319b5SDimitry Andric void LinkerScript::processInsertCommands() {
2184ba319b5SDimitry Andric   std::vector<BaseCommand *> V;
2194ba319b5SDimitry Andric   auto Insert = [&](std::vector<BaseCommand *> &From) {
2204ba319b5SDimitry Andric     V.insert(V.end(), From.begin(), From.end());
2214ba319b5SDimitry Andric     From.clear();
2224ba319b5SDimitry Andric   };
2234ba319b5SDimitry Andric 
2244ba319b5SDimitry Andric   for (BaseCommand *Base : SectionCommands) {
2254ba319b5SDimitry Andric     if (auto *OS = dyn_cast<OutputSection>(Base)) {
2264ba319b5SDimitry Andric       Insert(InsertBeforeCommands[OS->Name]);
2274ba319b5SDimitry Andric       V.push_back(Base);
2284ba319b5SDimitry Andric       Insert(InsertAfterCommands[OS->Name]);
2294ba319b5SDimitry Andric       continue;
2304ba319b5SDimitry Andric     }
2314ba319b5SDimitry Andric     V.push_back(Base);
2324ba319b5SDimitry Andric   }
2334ba319b5SDimitry Andric 
2344ba319b5SDimitry Andric   for (auto &Cmds : {InsertBeforeCommands, InsertAfterCommands})
2354ba319b5SDimitry Andric     for (const std::pair<StringRef, std::vector<BaseCommand *>> &P : Cmds)
2364ba319b5SDimitry Andric       if (!P.second.empty())
2374ba319b5SDimitry Andric         error("unable to INSERT AFTER/BEFORE " + P.first +
2384ba319b5SDimitry Andric               ": section not defined");
2394ba319b5SDimitry Andric 
2404ba319b5SDimitry Andric   SectionCommands = std::move(V);
2414ba319b5SDimitry Andric }
2424ba319b5SDimitry Andric 
2434ba319b5SDimitry Andric // Symbols defined in script should not be inlined by LTO. At the same time
2444ba319b5SDimitry Andric // we don't know their final values until late stages of link. Here we scan
2454ba319b5SDimitry Andric // over symbol assignment commands and create placeholder symbols if needed.
declareSymbols()2464ba319b5SDimitry Andric void LinkerScript::declareSymbols() {
2474ba319b5SDimitry Andric   assert(!Ctx);
2484ba319b5SDimitry Andric   for (BaseCommand *Base : SectionCommands) {
2494ba319b5SDimitry Andric     if (auto *Cmd = dyn_cast<SymbolAssignment>(Base)) {
2504ba319b5SDimitry Andric       declareSymbol(Cmd);
2514ba319b5SDimitry Andric       continue;
2524ba319b5SDimitry Andric     }
2534ba319b5SDimitry Andric 
2544ba319b5SDimitry Andric     // If the output section directive has constraints,
2554ba319b5SDimitry Andric     // we can't say for sure if it is going to be included or not.
2564ba319b5SDimitry Andric     // Skip such sections for now. Improve the checks if we ever
2574ba319b5SDimitry Andric     // need symbols from that sections to be declared early.
2584ba319b5SDimitry Andric     auto *Sec = cast<OutputSection>(Base);
2594ba319b5SDimitry Andric     if (Sec->Constraint != ConstraintKind::NoConstraint)
2604ba319b5SDimitry Andric       continue;
2614ba319b5SDimitry Andric     for (BaseCommand *Base2 : Sec->SectionCommands)
2624ba319b5SDimitry Andric       if (auto *Cmd = dyn_cast<SymbolAssignment>(Base2))
2634ba319b5SDimitry Andric         declareSymbol(Cmd);
2644ba319b5SDimitry Andric   }
2654ba319b5SDimitry Andric }
2664ba319b5SDimitry Andric 
26710dc89a5SDimitry Andric // This function is called from assignAddresses, while we are
26810dc89a5SDimitry Andric // fixing the output section addresses. This function is supposed
26910dc89a5SDimitry Andric // to set the final value for a given symbol assignment.
assignSymbol(SymbolAssignment * Cmd,bool InSec)27046b69c69SDimitry Andric void LinkerScript::assignSymbol(SymbolAssignment *Cmd, bool InSec) {
27146b69c69SDimitry Andric   if (Cmd->Name == ".") {
27246b69c69SDimitry Andric     setDot(Cmd->Expression, Cmd->Location, InSec);
27346b69c69SDimitry Andric     return;
27446b69c69SDimitry Andric   }
27546b69c69SDimitry Andric 
27646b69c69SDimitry Andric   if (!Cmd->Sym)
27724e2fe98SDimitry Andric     return;
27824e2fe98SDimitry Andric 
27946b69c69SDimitry Andric   ExprValue V = Cmd->Expression();
28046b69c69SDimitry Andric   if (V.isAbsolute()) {
28110dc89a5SDimitry Andric     Cmd->Sym->Section = nullptr;
28210dc89a5SDimitry Andric     Cmd->Sym->Value = V.getValue();
28324e2fe98SDimitry Andric   } else {
28410dc89a5SDimitry Andric     Cmd->Sym->Section = V.Sec;
28510dc89a5SDimitry Andric     Cmd->Sym->Value = V.getSectionOffset();
2861189dbaaSDimitry Andric   }
28724e2fe98SDimitry Andric }
28824e2fe98SDimitry Andric 
getFilename(InputFile * File)28910dc89a5SDimitry Andric static std::string getFilename(InputFile *File) {
29010dc89a5SDimitry Andric   if (!File)
2911189dbaaSDimitry Andric     return "";
29210dc89a5SDimitry Andric   if (File->ArchiveName.empty())
29310dc89a5SDimitry Andric     return File->getName();
29410dc89a5SDimitry Andric   return (File->ArchiveName + "(" + File->getName() + ")").str();
2951189dbaaSDimitry Andric }
2961189dbaaSDimitry Andric 
shouldKeep(InputSectionBase * S)29746b69c69SDimitry Andric bool LinkerScript::shouldKeep(InputSectionBase *S) {
29810dc89a5SDimitry Andric   if (KeptSections.empty())
29910dc89a5SDimitry Andric     return false;
30010dc89a5SDimitry Andric   std::string Filename = getFilename(S->File);
30110dc89a5SDimitry Andric   for (InputSectionDescription *ID : KeptSections)
30210dc89a5SDimitry Andric     if (ID->FilePat.match(Filename))
3031189dbaaSDimitry Andric       for (SectionPattern &P : ID->SectionPatterns)
3041189dbaaSDimitry Andric         if (P.SectionPat.match(S->Name))
3051189dbaaSDimitry Andric           return true;
3061189dbaaSDimitry Andric   return false;
30797c3811cSEd Maste }
30897c3811cSEd Maste 
30946b69c69SDimitry Andric // A helper function for the SORT() command.
31046b69c69SDimitry Andric static std::function<bool(InputSectionBase *, InputSectionBase *)>
getComparator(SortSectionPolicy K)3111189dbaaSDimitry Andric getComparator(SortSectionPolicy K) {
3121189dbaaSDimitry Andric   switch (K) {
3131189dbaaSDimitry Andric   case SortSectionPolicy::Alignment:
31446b69c69SDimitry Andric     return [](InputSectionBase *A, InputSectionBase *B) {
31546b69c69SDimitry Andric       // ">" is not a mistake. Sections with larger alignments are placed
31646b69c69SDimitry Andric       // before sections with smaller alignments in order to reduce the
31746b69c69SDimitry Andric       // amount of padding necessary. This is compatible with GNU.
31846b69c69SDimitry Andric       return A->Alignment > B->Alignment;
31946b69c69SDimitry Andric     };
3201189dbaaSDimitry Andric   case SortSectionPolicy::Name:
32146b69c69SDimitry Andric     return [](InputSectionBase *A, InputSectionBase *B) {
32246b69c69SDimitry Andric       return A->Name < B->Name;
32346b69c69SDimitry Andric     };
3241189dbaaSDimitry Andric   case SortSectionPolicy::Priority:
32546b69c69SDimitry Andric     return [](InputSectionBase *A, InputSectionBase *B) {
32646b69c69SDimitry Andric       return getPriority(A->Name) < getPriority(B->Name);
32746b69c69SDimitry Andric     };
3281189dbaaSDimitry Andric   default:
3291189dbaaSDimitry Andric     llvm_unreachable("unknown sort policy");
3301189dbaaSDimitry Andric   }
3311189dbaaSDimitry Andric }
3321189dbaaSDimitry Andric 
33346b69c69SDimitry Andric // A helper function for the SORT() command.
matchConstraints(ArrayRef<InputSection * > Sections,ConstraintKind Kind)33410dc89a5SDimitry Andric static bool matchConstraints(ArrayRef<InputSection *> Sections,
3351189dbaaSDimitry Andric                              ConstraintKind Kind) {
3361189dbaaSDimitry Andric   if (Kind == ConstraintKind::NoConstraint)
3371189dbaaSDimitry Andric     return true;
33846b69c69SDimitry Andric 
33910dc89a5SDimitry Andric   bool IsRW = llvm::any_of(
34010dc89a5SDimitry Andric       Sections, [](InputSection *Sec) { return Sec->Flags & SHF_WRITE; });
34146b69c69SDimitry Andric 
3421189dbaaSDimitry Andric   return (IsRW && Kind == ConstraintKind::ReadWrite) ||
3431189dbaaSDimitry Andric          (!IsRW && Kind == ConstraintKind::ReadOnly);
3441189dbaaSDimitry Andric }
3451189dbaaSDimitry Andric 
sortSections(MutableArrayRef<InputSection * > Vec,SortSectionPolicy K)34610dc89a5SDimitry Andric static void sortSections(MutableArrayRef<InputSection *> Vec,
3471189dbaaSDimitry Andric                          SortSectionPolicy K) {
3481189dbaaSDimitry Andric   if (K != SortSectionPolicy::Default && K != SortSectionPolicy::None)
34910dc89a5SDimitry Andric     std::stable_sort(Vec.begin(), Vec.end(), getComparator(K));
3501189dbaaSDimitry Andric }
3511189dbaaSDimitry Andric 
3521189dbaaSDimitry Andric // Sort sections as instructed by SORT-family commands and --sort-section
3531189dbaaSDimitry Andric // option. Because SORT-family commands can be nested at most two depth
3541189dbaaSDimitry Andric // (e.g. SORT_BY_NAME(SORT_BY_ALIGNMENT(.text.*))) and because the command
3551189dbaaSDimitry Andric // line option is respected even if a SORT command is given, the exact
3561189dbaaSDimitry Andric // behavior we have here is a bit complicated. Here are the rules.
3571189dbaaSDimitry Andric //
3581189dbaaSDimitry Andric // 1. If two SORT commands are given, --sort-section is ignored.
3591189dbaaSDimitry Andric // 2. If one SORT command is given, and if it is not SORT_NONE,
3601189dbaaSDimitry Andric //    --sort-section is handled as an inner SORT command.
3611189dbaaSDimitry Andric // 3. If one SORT command is given, and if it is SORT_NONE, don't sort.
3621189dbaaSDimitry Andric // 4. If no SORT command is given, sort according to --sort-section.
sortInputSections(MutableArrayRef<InputSection * > Vec,const SectionPattern & Pat)3634ba319b5SDimitry Andric static void sortInputSections(MutableArrayRef<InputSection *> Vec,
3644ba319b5SDimitry Andric                               const SectionPattern &Pat) {
36510dc89a5SDimitry Andric   if (Pat.SortOuter == SortSectionPolicy::None)
36610dc89a5SDimitry Andric     return;
36710dc89a5SDimitry Andric 
36810dc89a5SDimitry Andric   if (Pat.SortInner == SortSectionPolicy::Default)
36910dc89a5SDimitry Andric     sortSections(Vec, Config->SortSection);
37010dc89a5SDimitry Andric   else
37110dc89a5SDimitry Andric     sortSections(Vec, Pat.SortInner);
37210dc89a5SDimitry Andric   sortSections(Vec, Pat.SortOuter);
37310dc89a5SDimitry Andric }
37410dc89a5SDimitry Andric 
37510dc89a5SDimitry Andric // Compute and remember which sections the InputSectionDescription matches.
37610dc89a5SDimitry Andric std::vector<InputSection *>
computeInputSections(const InputSectionDescription * Cmd)3774ba319b5SDimitry Andric LinkerScript::computeInputSections(const InputSectionDescription *Cmd) {
37810dc89a5SDimitry Andric   std::vector<InputSection *> Ret;
37910dc89a5SDimitry Andric 
38010dc89a5SDimitry Andric   // Collects all sections that satisfy constraints of Cmd.
38110dc89a5SDimitry Andric   for (const SectionPattern &Pat : Cmd->SectionPatterns) {
38210dc89a5SDimitry Andric     size_t SizeBefore = Ret.size();
38310dc89a5SDimitry Andric 
38410dc89a5SDimitry Andric     for (InputSectionBase *Sec : InputSections) {
38510dc89a5SDimitry Andric       if (!Sec->Live || Sec->Assigned)
38610dc89a5SDimitry Andric         continue;
38710dc89a5SDimitry Andric 
38810dc89a5SDimitry Andric       // For -emit-relocs we have to ignore entries like
38910dc89a5SDimitry Andric       //   .rela.dyn : { *(.rela.data) }
39010dc89a5SDimitry Andric       // which are common because they are in the default bfd script.
3914ba319b5SDimitry Andric       // We do not ignore SHT_REL[A] linker-synthesized sections here because
3924ba319b5SDimitry Andric       // want to support scripts that do custom layout for them.
3934ba319b5SDimitry Andric       if (auto *IS = dyn_cast<InputSection>(Sec))
3944ba319b5SDimitry Andric         if (IS->getRelocatedSection())
39510dc89a5SDimitry Andric           continue;
39610dc89a5SDimitry Andric 
39710dc89a5SDimitry Andric       std::string Filename = getFilename(Sec->File);
39810dc89a5SDimitry Andric       if (!Cmd->FilePat.match(Filename) ||
39910dc89a5SDimitry Andric           Pat.ExcludedFilePat.match(Filename) ||
40010dc89a5SDimitry Andric           !Pat.SectionPat.match(Sec->Name))
40110dc89a5SDimitry Andric         continue;
40210dc89a5SDimitry Andric 
40310dc89a5SDimitry Andric       // It is safe to assume that Sec is an InputSection
40410dc89a5SDimitry Andric       // because mergeable or EH input sections have already been
40510dc89a5SDimitry Andric       // handled and eliminated.
40610dc89a5SDimitry Andric       Ret.push_back(cast<InputSection>(Sec));
40710dc89a5SDimitry Andric       Sec->Assigned = true;
40810dc89a5SDimitry Andric     }
40910dc89a5SDimitry Andric 
41010dc89a5SDimitry Andric     sortInputSections(MutableArrayRef<InputSection *>(Ret).slice(SizeBefore),
4114ba319b5SDimitry Andric                       Pat);
4121189dbaaSDimitry Andric   }
41346b69c69SDimitry Andric   return Ret;
4141189dbaaSDimitry Andric }
4151189dbaaSDimitry Andric 
discard(ArrayRef<InputSection * > V)41610dc89a5SDimitry Andric void LinkerScript::discard(ArrayRef<InputSection *> V) {
41710dc89a5SDimitry Andric   for (InputSection *S : V) {
418*b5893f02SDimitry Andric     if (S == In.ShStrTab || S == In.RelaDyn || S == In.RelrDyn)
419edd7eaddSDimitry Andric       error("discarding " + S->Name + " section is not allowed");
42010dc89a5SDimitry Andric 
4214ba319b5SDimitry Andric     // You can discard .hash and .gnu.hash sections by linker scripts. Since
4224ba319b5SDimitry Andric     // they are synthesized sections, we need to handle them differently than
4234ba319b5SDimitry Andric     // other regular sections.
424*b5893f02SDimitry Andric     if (S == In.GnuHashTab)
425*b5893f02SDimitry Andric       In.GnuHashTab = nullptr;
426*b5893f02SDimitry Andric     if (S == In.HashTab)
427*b5893f02SDimitry Andric       In.HashTab = nullptr;
4284ba319b5SDimitry Andric 
42910dc89a5SDimitry Andric     S->Assigned = false;
43010dc89a5SDimitry Andric     S->Live = false;
43146b69c69SDimitry Andric     discard(S->DependentSections);
4321189dbaaSDimitry Andric   }
4331189dbaaSDimitry Andric }
4341189dbaaSDimitry Andric 
4354ba319b5SDimitry Andric std::vector<InputSection *>
createInputSectionList(OutputSection & OutCmd)4364ba319b5SDimitry Andric LinkerScript::createInputSectionList(OutputSection &OutCmd) {
43710dc89a5SDimitry Andric   std::vector<InputSection *> Ret;
4381189dbaaSDimitry Andric 
43910dc89a5SDimitry Andric   for (BaseCommand *Base : OutCmd.SectionCommands) {
44010dc89a5SDimitry Andric     if (auto *Cmd = dyn_cast<InputSectionDescription>(Base)) {
4414ba319b5SDimitry Andric       Cmd->Sections = computeInputSections(Cmd);
44246b69c69SDimitry Andric       Ret.insert(Ret.end(), Cmd->Sections.begin(), Cmd->Sections.end());
4431189dbaaSDimitry Andric     }
44410dc89a5SDimitry Andric   }
4451189dbaaSDimitry Andric   return Ret;
4461189dbaaSDimitry Andric }
4471189dbaaSDimitry Andric 
processSectionCommands()44810dc89a5SDimitry Andric void LinkerScript::processSectionCommands() {
44946b69c69SDimitry Andric   // A symbol can be assigned before any section is mentioned in the linker
45046b69c69SDimitry Andric   // script. In an DSO, the symbol values are addresses, so the only important
45146b69c69SDimitry Andric   // section values are:
45246b69c69SDimitry Andric   // * SHN_UNDEF
45346b69c69SDimitry Andric   // * SHN_ABS
45446b69c69SDimitry Andric   // * Any value meaning a regular section.
45546b69c69SDimitry Andric   // To handle that, create a dummy aether section that fills the void before
45646b69c69SDimitry Andric   // the linker scripts switches to another section. It has an index of one
45746b69c69SDimitry Andric   // which will map to whatever the first actual section is.
45846b69c69SDimitry Andric   Aether = make<OutputSection>("", 0, SHF_ALLOC);
45946b69c69SDimitry Andric   Aether->SectionIndex = 1;
46010dc89a5SDimitry Andric 
46110dc89a5SDimitry Andric   // Ctx captures the local AddressState and makes it accessible deliberately.
46210dc89a5SDimitry Andric   // This is needed as there are some cases where we cannot just
463c4394386SDimitry Andric   // thread the current state through to a lambda function created by the
464c4394386SDimitry Andric   // script parser.
46510dc89a5SDimitry Andric   auto Deleter = make_unique<AddressState>();
46610dc89a5SDimitry Andric   Ctx = Deleter.get();
46710dc89a5SDimitry Andric   Ctx->OutSec = Aether;
4681189dbaaSDimitry Andric 
46910dc89a5SDimitry Andric   size_t I = 0;
47010dc89a5SDimitry Andric   // Add input sections to output sections.
47110dc89a5SDimitry Andric   for (BaseCommand *Base : SectionCommands) {
4721189dbaaSDimitry Andric     // Handle symbol assignments outside of any output section.
47310dc89a5SDimitry Andric     if (auto *Cmd = dyn_cast<SymbolAssignment>(Base)) {
47446b69c69SDimitry Andric       addSymbol(Cmd);
4751189dbaaSDimitry Andric       continue;
4761189dbaaSDimitry Andric     }
4771189dbaaSDimitry Andric 
47810dc89a5SDimitry Andric     if (auto *Sec = dyn_cast<OutputSection>(Base)) {
4794ba319b5SDimitry Andric       std::vector<InputSection *> V = createInputSectionList(*Sec);
4801189dbaaSDimitry Andric 
4811189dbaaSDimitry Andric       // The output section name `/DISCARD/' is special.
4821189dbaaSDimitry Andric       // Any input section assigned to it is discarded.
48310dc89a5SDimitry Andric       if (Sec->Name == "/DISCARD/") {
4841189dbaaSDimitry Andric         discard(V);
4854ba319b5SDimitry Andric         Sec->SectionCommands.clear();
4861189dbaaSDimitry Andric         continue;
4871189dbaaSDimitry Andric       }
4881189dbaaSDimitry Andric 
4891189dbaaSDimitry Andric       // This is for ONLY_IF_RO and ONLY_IF_RW. An output section directive
4901189dbaaSDimitry Andric       // ".foo : ONLY_IF_R[OW] { ... }" is handled only if all member input
4911189dbaaSDimitry Andric       // sections satisfy a given constraint. If not, a directive is handled
4921189dbaaSDimitry Andric       // as if it wasn't present from the beginning.
4931189dbaaSDimitry Andric       //
49410dc89a5SDimitry Andric       // Because we'll iterate over SectionCommands many more times, the easy
49510dc89a5SDimitry Andric       // way to "make it as if it wasn't present" is to make it empty.
49610dc89a5SDimitry Andric       if (!matchConstraints(V, Sec->Constraint)) {
49746b69c69SDimitry Andric         for (InputSectionBase *S : V)
4981189dbaaSDimitry Andric           S->Assigned = false;
49910dc89a5SDimitry Andric         Sec->SectionCommands.clear();
5001189dbaaSDimitry Andric         continue;
5011189dbaaSDimitry Andric       }
5021189dbaaSDimitry Andric 
5031189dbaaSDimitry Andric       // A directive may contain symbol definitions like this:
5041189dbaaSDimitry Andric       // ".foo : { ...; bar = .; }". Handle them.
50510dc89a5SDimitry Andric       for (BaseCommand *Base : Sec->SectionCommands)
50646b69c69SDimitry Andric         if (auto *OutCmd = dyn_cast<SymbolAssignment>(Base))
50746b69c69SDimitry Andric           addSymbol(OutCmd);
5081189dbaaSDimitry Andric 
5091189dbaaSDimitry Andric       // Handle subalign (e.g. ".foo : SUBALIGN(32) { ... }"). If subalign
5101189dbaaSDimitry Andric       // is given, input sections are aligned to that value, whether the
5111189dbaaSDimitry Andric       // given value is larger or smaller than the original section alignment.
51210dc89a5SDimitry Andric       if (Sec->SubalignExpr) {
51310dc89a5SDimitry Andric         uint32_t Subalign = Sec->SubalignExpr().getValue();
51446b69c69SDimitry Andric         for (InputSectionBase *S : V)
5151189dbaaSDimitry Andric           S->Alignment = Subalign;
5161189dbaaSDimitry Andric       }
5171189dbaaSDimitry Andric 
5181189dbaaSDimitry Andric       // Add input sections to an output section.
51910dc89a5SDimitry Andric       for (InputSection *S : V)
52010dc89a5SDimitry Andric         Sec->addSection(S);
52110dc89a5SDimitry Andric 
52210dc89a5SDimitry Andric       Sec->SectionIndex = I++;
52310dc89a5SDimitry Andric       if (Sec->Noload)
524db17bf38SDimitry Andric         Sec->Type = SHT_NOBITS;
5254ba319b5SDimitry Andric       if (Sec->NonAlloc)
5264ba319b5SDimitry Andric         Sec->Flags &= ~(uint64_t)SHF_ALLOC;
527f37b6182SDimitry Andric     }
5281189dbaaSDimitry Andric   }
52910dc89a5SDimitry Andric   Ctx = nullptr;
5301189dbaaSDimitry Andric }
5311189dbaaSDimitry Andric 
findByName(ArrayRef<BaseCommand * > Vec,StringRef Name)53210dc89a5SDimitry Andric static OutputSection *findByName(ArrayRef<BaseCommand *> Vec,
53310dc89a5SDimitry Andric                                  StringRef Name) {
53410dc89a5SDimitry Andric   for (BaseCommand *Base : Vec)
53510dc89a5SDimitry Andric     if (auto *Sec = dyn_cast<OutputSection>(Base))
53610dc89a5SDimitry Andric       if (Sec->Name == Name)
53710dc89a5SDimitry Andric         return Sec;
53810dc89a5SDimitry Andric   return nullptr;
5396bc11b14SDimitry Andric }
54010dc89a5SDimitry Andric 
createSection(InputSectionBase * IS,StringRef OutsecName)54110dc89a5SDimitry Andric static OutputSection *createSection(InputSectionBase *IS,
54210dc89a5SDimitry Andric                                     StringRef OutsecName) {
54310dc89a5SDimitry Andric   OutputSection *Sec = Script->createOutputSection(OutsecName, "<internal>");
54410dc89a5SDimitry Andric   Sec->addSection(cast<InputSection>(IS));
54510dc89a5SDimitry Andric   return Sec;
5466bc11b14SDimitry Andric }
54710dc89a5SDimitry Andric 
addInputSec(StringMap<OutputSection * > & Map,InputSectionBase * IS,StringRef OutsecName)54810dc89a5SDimitry Andric static OutputSection *addInputSec(StringMap<OutputSection *> &Map,
54910dc89a5SDimitry Andric                                   InputSectionBase *IS, StringRef OutsecName) {
55010dc89a5SDimitry Andric   // Sections with SHT_GROUP or SHF_GROUP attributes reach here only when the -r
55110dc89a5SDimitry Andric   // option is given. A section with SHT_GROUP defines a "section group", and
55210dc89a5SDimitry Andric   // its members have SHF_GROUP attribute. Usually these flags have already been
55310dc89a5SDimitry Andric   // stripped by InputFiles.cpp as section groups are processed and uniquified.
55410dc89a5SDimitry Andric   // However, for the -r option, we want to pass through all section groups
55510dc89a5SDimitry Andric   // as-is because adding/removing members or merging them with other groups
55610dc89a5SDimitry Andric   // change their semantics.
55710dc89a5SDimitry Andric   if (IS->Type == SHT_GROUP || (IS->Flags & SHF_GROUP))
55810dc89a5SDimitry Andric     return createSection(IS, OutsecName);
55910dc89a5SDimitry Andric 
56010dc89a5SDimitry Andric   // Imagine .zed : { *(.foo) *(.bar) } script. Both foo and bar may have
56110dc89a5SDimitry Andric   // relocation sections .rela.foo and .rela.bar for example. Most tools do
56210dc89a5SDimitry Andric   // not allow multiple REL[A] sections for output section. Hence we
56310dc89a5SDimitry Andric   // should combine these relocation sections into single output.
56410dc89a5SDimitry Andric   // We skip synthetic sections because it can be .rela.dyn/.rela.plt or any
56510dc89a5SDimitry Andric   // other REL[A] sections created by linker itself.
56610dc89a5SDimitry Andric   if (!isa<SyntheticSection>(IS) &&
56710dc89a5SDimitry Andric       (IS->Type == SHT_REL || IS->Type == SHT_RELA)) {
56810dc89a5SDimitry Andric     auto *Sec = cast<InputSection>(IS);
56910dc89a5SDimitry Andric     OutputSection *Out = Sec->getRelocatedSection()->getOutputSection();
57010dc89a5SDimitry Andric 
57110dc89a5SDimitry Andric     if (Out->RelocationSection) {
57210dc89a5SDimitry Andric       Out->RelocationSection->addSection(Sec);
57310dc89a5SDimitry Andric       return nullptr;
5746bc11b14SDimitry Andric     }
57510dc89a5SDimitry Andric 
57610dc89a5SDimitry Andric     Out->RelocationSection = createSection(IS, OutsecName);
57710dc89a5SDimitry Andric     return Out->RelocationSection;
57810dc89a5SDimitry Andric   }
57910dc89a5SDimitry Andric 
58010dc89a5SDimitry Andric   // When control reaches here, mergeable sections have already been merged into
58110dc89a5SDimitry Andric   // synthetic sections. For relocatable case we want to create one output
58210dc89a5SDimitry Andric   // section per syntetic section so that they have a valid sh_entsize.
58310dc89a5SDimitry Andric   if (Config->Relocatable && (IS->Flags & SHF_MERGE))
58410dc89a5SDimitry Andric     return createSection(IS, OutsecName);
58510dc89a5SDimitry Andric 
58610dc89a5SDimitry Andric   //  The ELF spec just says
58710dc89a5SDimitry Andric   // ----------------------------------------------------------------
58810dc89a5SDimitry Andric   // In the first phase, input sections that match in name, type and
58910dc89a5SDimitry Andric   // attribute flags should be concatenated into single sections.
59010dc89a5SDimitry Andric   // ----------------------------------------------------------------
59110dc89a5SDimitry Andric   //
59210dc89a5SDimitry Andric   // However, it is clear that at least some flags have to be ignored for
59310dc89a5SDimitry Andric   // section merging. At the very least SHF_GROUP and SHF_COMPRESSED have to be
59410dc89a5SDimitry Andric   // ignored. We should not have two output .text sections just because one was
59510dc89a5SDimitry Andric   // in a group and another was not for example.
59610dc89a5SDimitry Andric   //
5974ba319b5SDimitry Andric   // It also seems that wording was a late addition and didn't get the
59810dc89a5SDimitry Andric   // necessary scrutiny.
59910dc89a5SDimitry Andric   //
60010dc89a5SDimitry Andric   // Merging sections with different flags is expected by some users. One
60110dc89a5SDimitry Andric   // reason is that if one file has
60210dc89a5SDimitry Andric   //
60310dc89a5SDimitry Andric   // int *const bar __attribute__((section(".foo"))) = (int *)0;
60410dc89a5SDimitry Andric   //
60510dc89a5SDimitry Andric   // gcc with -fPIC will produce a read only .foo section. But if another
60610dc89a5SDimitry Andric   // file has
60710dc89a5SDimitry Andric   //
60810dc89a5SDimitry Andric   // int zed;
60910dc89a5SDimitry Andric   // int *const bar __attribute__((section(".foo"))) = (int *)&zed;
61010dc89a5SDimitry Andric   //
61110dc89a5SDimitry Andric   // gcc with -fPIC will produce a read write section.
61210dc89a5SDimitry Andric   //
61310dc89a5SDimitry Andric   // Last but not least, when using linker script the merge rules are forced by
61410dc89a5SDimitry Andric   // the script. Unfortunately, linker scripts are name based. This means that
61510dc89a5SDimitry Andric   // expressions like *(.foo*) can refer to multiple input sections with
61610dc89a5SDimitry Andric   // different flags. We cannot put them in different output sections or we
61710dc89a5SDimitry Andric   // would produce wrong results for
61810dc89a5SDimitry Andric   //
61910dc89a5SDimitry Andric   // start = .; *(.foo.*) end = .; *(.bar)
62010dc89a5SDimitry Andric   //
62110dc89a5SDimitry Andric   // and a mapping of .foo1 and .bar1 to one section and .foo2 and .bar2 to
62210dc89a5SDimitry Andric   // another. The problem is that there is no way to layout those output
62310dc89a5SDimitry Andric   // sections such that the .foo sections are the only thing between the start
62410dc89a5SDimitry Andric   // and end symbols.
62510dc89a5SDimitry Andric   //
62610dc89a5SDimitry Andric   // Given the above issues, we instead merge sections by name and error on
62710dc89a5SDimitry Andric   // incompatible types and flags.
62810dc89a5SDimitry Andric   OutputSection *&Sec = Map[OutsecName];
62910dc89a5SDimitry Andric   if (Sec) {
63010dc89a5SDimitry Andric     Sec->addSection(cast<InputSection>(IS));
63110dc89a5SDimitry Andric     return nullptr;
63210dc89a5SDimitry Andric   }
63310dc89a5SDimitry Andric 
63410dc89a5SDimitry Andric   Sec = createSection(IS, OutsecName);
63510dc89a5SDimitry Andric   return Sec;
6366bc11b14SDimitry Andric }
6376bc11b14SDimitry Andric 
6381189dbaaSDimitry Andric // Add sections that didn't match any sections command.
addOrphanSections()63910dc89a5SDimitry Andric void LinkerScript::addOrphanSections() {
64010dc89a5SDimitry Andric   unsigned End = SectionCommands.size();
64110dc89a5SDimitry Andric   StringMap<OutputSection *> Map;
64210dc89a5SDimitry Andric   std::vector<OutputSection *> V;
6434ba319b5SDimitry Andric 
6444ba319b5SDimitry Andric   auto Add = [&](InputSectionBase *S) {
645f9448bf3SDimitry Andric     if (!S->Live || S->Parent)
6464ba319b5SDimitry Andric       return;
64710dc89a5SDimitry Andric 
64810dc89a5SDimitry Andric     StringRef Name = getOutputSectionName(S);
64910dc89a5SDimitry Andric 
65010dc89a5SDimitry Andric     if (Config->OrphanHandling == OrphanHandlingPolicy::Error)
65110dc89a5SDimitry Andric       error(toString(S) + " is being placed in '" + Name + "'");
65210dc89a5SDimitry Andric     else if (Config->OrphanHandling == OrphanHandlingPolicy::Warn)
65310dc89a5SDimitry Andric       warn(toString(S) + " is being placed in '" + Name + "'");
65410dc89a5SDimitry Andric 
65510dc89a5SDimitry Andric     if (OutputSection *Sec =
65610dc89a5SDimitry Andric             findByName(makeArrayRef(SectionCommands).slice(0, End), Name)) {
65710dc89a5SDimitry Andric       Sec->addSection(cast<InputSection>(S));
6584ba319b5SDimitry Andric       return;
659f37b6182SDimitry Andric     }
6601189dbaaSDimitry Andric 
66110dc89a5SDimitry Andric     if (OutputSection *OS = addInputSec(Map, S, Name))
66210dc89a5SDimitry Andric       V.push_back(OS);
6634ba319b5SDimitry Andric     assert(S->getOutputSection()->SectionIndex == UINT32_MAX);
6644ba319b5SDimitry Andric   };
6654ba319b5SDimitry Andric 
6664ba319b5SDimitry Andric   // For futher --emit-reloc handling code we need target output section
6674ba319b5SDimitry Andric   // to be created before we create relocation output section, so we want
6684ba319b5SDimitry Andric   // to create target sections first. We do not want priority handling
6694ba319b5SDimitry Andric   // for synthetic sections because them are special.
6704ba319b5SDimitry Andric   for (InputSectionBase *IS : InputSections) {
6714ba319b5SDimitry Andric     if (auto *Sec = dyn_cast<InputSection>(IS))
6724ba319b5SDimitry Andric       if (InputSectionBase *Rel = Sec->getRelocatedSection())
6734ba319b5SDimitry Andric         if (auto *RelIS = dyn_cast_or_null<InputSectionBase>(Rel->Parent))
6744ba319b5SDimitry Andric           Add(RelIS);
6754ba319b5SDimitry Andric     Add(IS);
67610dc89a5SDimitry Andric   }
67710dc89a5SDimitry Andric 
67810dc89a5SDimitry Andric   // If no SECTIONS command was given, we should insert sections commands
67910dc89a5SDimitry Andric   // before others, so that we can handle scripts which refers them,
68010dc89a5SDimitry Andric   // for example: "foo = ABSOLUTE(ADDR(.text)));".
68110dc89a5SDimitry Andric   // When SECTIONS command is present we just add all orphans to the end.
68210dc89a5SDimitry Andric   if (HasSectionsCommand)
68310dc89a5SDimitry Andric     SectionCommands.insert(SectionCommands.end(), V.begin(), V.end());
68410dc89a5SDimitry Andric   else
68510dc89a5SDimitry Andric     SectionCommands.insert(SectionCommands.begin(), V.begin(), V.end());
68610dc89a5SDimitry Andric }
68710dc89a5SDimitry Andric 
advance(uint64_t Size,unsigned Alignment)68810dc89a5SDimitry Andric uint64_t LinkerScript::advance(uint64_t Size, unsigned Alignment) {
68910dc89a5SDimitry Andric   bool IsTbss =
69010dc89a5SDimitry Andric       (Ctx->OutSec->Flags & SHF_TLS) && Ctx->OutSec->Type == SHT_NOBITS;
69110dc89a5SDimitry Andric   uint64_t Start = IsTbss ? Dot + Ctx->ThreadBssOffset : Dot;
69210dc89a5SDimitry Andric   Start = alignTo(Start, Alignment);
6930f5676f4SDimitry Andric   uint64_t End = Start + Size;
6940f5676f4SDimitry Andric 
6950f5676f4SDimitry Andric   if (IsTbss)
69610dc89a5SDimitry Andric     Ctx->ThreadBssOffset = End - Dot;
6970f5676f4SDimitry Andric   else
6980f5676f4SDimitry Andric     Dot = End;
6990f5676f4SDimitry Andric   return End;
7001189dbaaSDimitry Andric }
7011189dbaaSDimitry Andric 
output(InputSection * S)70246b69c69SDimitry Andric void LinkerScript::output(InputSection *S) {
703*b5893f02SDimitry Andric   assert(Ctx->OutSec == S->getParent());
70410dc89a5SDimitry Andric   uint64_t Before = advance(0, 1);
7050f5676f4SDimitry Andric   uint64_t Pos = advance(S->getSize(), S->Alignment);
70610dc89a5SDimitry Andric   S->OutSecOff = Pos - S->getSize() - Ctx->OutSec->Addr;
7071189dbaaSDimitry Andric 
7081189dbaaSDimitry Andric   // Update output section size after adding each section. This is so that
7091189dbaaSDimitry Andric   // SIZEOF works correctly in the case below:
7101189dbaaSDimitry Andric   // .foo { *(.aaa) a = SIZEOF(.foo); *(.bbb) }
7114ba319b5SDimitry Andric   expandOutputSection(Pos - Before);
7121189dbaaSDimitry Andric }
7131189dbaaSDimitry Andric 
switchTo(OutputSection * Sec)71446b69c69SDimitry Andric void LinkerScript::switchTo(OutputSection *Sec) {
71510dc89a5SDimitry Andric   Ctx->OutSec = Sec;
7164ba319b5SDimitry Andric 
7174ba319b5SDimitry Andric   uint64_t Before = advance(0, 1);
71810dc89a5SDimitry Andric   Ctx->OutSec->Addr = advance(0, Ctx->OutSec->Alignment);
7194ba319b5SDimitry Andric   expandMemoryRegions(Ctx->OutSec->Addr - Before);
7201189dbaaSDimitry Andric }
7211189dbaaSDimitry Andric 
72246b69c69SDimitry Andric // This function searches for a memory region to place the given output
72346b69c69SDimitry Andric // section in. If found, a pointer to the appropriate memory region is
72446b69c69SDimitry Andric // returned. Otherwise, a nullptr is returned.
findMemoryRegion(OutputSection * Sec)72510dc89a5SDimitry Andric MemoryRegion *LinkerScript::findMemoryRegion(OutputSection *Sec) {
72646b69c69SDimitry Andric   // If a memory region name was specified in the output section command,
72746b69c69SDimitry Andric   // then try to find that region first.
72810dc89a5SDimitry Andric   if (!Sec->MemoryRegionName.empty()) {
729032c24abSEd Maste     if (MemoryRegion *M = MemoryRegions.lookup(Sec->MemoryRegionName))
730032c24abSEd Maste       return M;
73110dc89a5SDimitry Andric     error("memory region '" + Sec->MemoryRegionName + "' not declared");
73246b69c69SDimitry Andric     return nullptr;
73346b69c69SDimitry Andric   }
73446b69c69SDimitry Andric 
73546b69c69SDimitry Andric   // If at least one memory region is defined, all sections must
73646b69c69SDimitry Andric   // belong to some memory region. Otherwise, we don't need to do
73746b69c69SDimitry Andric   // anything for memory regions.
73810dc89a5SDimitry Andric   if (MemoryRegions.empty())
73946b69c69SDimitry Andric     return nullptr;
74046b69c69SDimitry Andric 
74146b69c69SDimitry Andric   // See if a region can be found by matching section flags.
74210dc89a5SDimitry Andric   for (auto &Pair : MemoryRegions) {
74310dc89a5SDimitry Andric     MemoryRegion *M = Pair.second;
74410dc89a5SDimitry Andric     if ((M->Flags & Sec->Flags) && (M->NegFlags & Sec->Flags) == 0)
74510dc89a5SDimitry Andric       return M;
74646b69c69SDimitry Andric   }
74746b69c69SDimitry Andric 
74846b69c69SDimitry Andric   // Otherwise, no suitable region was found.
74946b69c69SDimitry Andric   if (Sec->Flags & SHF_ALLOC)
75046b69c69SDimitry Andric     error("no memory region specified for section '" + Sec->Name + "'");
75146b69c69SDimitry Andric   return nullptr;
7521189dbaaSDimitry Andric }
7531189dbaaSDimitry Andric 
findFirstSection(PhdrEntry * Load)7544ba319b5SDimitry Andric static OutputSection *findFirstSection(PhdrEntry *Load) {
7554ba319b5SDimitry Andric   for (OutputSection *Sec : OutputSections)
7564ba319b5SDimitry Andric     if (Sec->PtLoad == Load)
7574ba319b5SDimitry Andric       return Sec;
7584ba319b5SDimitry Andric   return nullptr;
7594ba319b5SDimitry Andric }
7604ba319b5SDimitry Andric 
7611189dbaaSDimitry Andric // This function assigns offsets to input sections and an output section
7621189dbaaSDimitry Andric // for a single sections command (e.g. ".text { *(.text); }").
assignOffsets(OutputSection * Sec)76310dc89a5SDimitry Andric void LinkerScript::assignOffsets(OutputSection *Sec) {
76424d58133SDimitry Andric   if (!(Sec->Flags & SHF_ALLOC))
76524d58133SDimitry Andric     Dot = 0;
76610dc89a5SDimitry Andric   else if (Sec->AddrExpr)
76710dc89a5SDimitry Andric     setDot(Sec->AddrExpr, Sec->Location, false);
76846b69c69SDimitry Andric 
76910dc89a5SDimitry Andric   Ctx->MemRegion = Sec->MemRegion;
7702b8c81f1SEd Maste   Ctx->LMARegion = Sec->LMARegion;
77110dc89a5SDimitry Andric   if (Ctx->MemRegion)
7726fcb8605SEd Maste     Dot = Ctx->MemRegion->CurPos;
77310dc89a5SDimitry Andric 
77462de8f4aSEd Maste   switchTo(Sec);
77562de8f4aSEd Maste 
7765a2ea378SEd Maste   if (Sec->LMAExpr)
7775a2ea378SEd Maste     Ctx->LMAOffset = Sec->LMAExpr().getValue() - Dot;
7781189dbaaSDimitry Andric 
7795a2ea378SEd Maste   if (MemoryRegion *MR = Sec->LMARegion)
7802b8c81f1SEd Maste     Ctx->LMAOffset = MR->CurPos - Dot;
781d3f08be7SEd Maste 
78262de8f4aSEd Maste   // If neither AT nor AT> is specified for an allocatable section, the linker
78362de8f4aSEd Maste   // will set the LMA such that the difference between VMA and LMA for the
78462de8f4aSEd Maste   // section is the same as the preceding output section in the same region
78562de8f4aSEd Maste   // https://sourceware.org/binutils/docs-2.20/ld/Output-Section-LMA.html
7864ba319b5SDimitry Andric   // This, however, should only be done by the first "non-header" section
7874ba319b5SDimitry Andric   // in the segment.
7881da03555SEd Maste   if (PhdrEntry *L = Ctx->OutSec->PtLoad)
7894ba319b5SDimitry Andric     if (Sec == findFirstSection(L))
7901da03555SEd Maste       L->LMAOffset = Ctx->LMAOffset;
79146b69c69SDimitry Andric 
7924ba319b5SDimitry Andric   // We can call this method multiple times during the creation of
7934ba319b5SDimitry Andric   // thunks and want to start over calculation each time.
79410dc89a5SDimitry Andric   Sec->Size = 0;
79510dc89a5SDimitry Andric 
79610dc89a5SDimitry Andric   // We visited SectionsCommands from processSectionCommands to
79710dc89a5SDimitry Andric   // layout sections. Now, we visit SectionsCommands again to fix
79810dc89a5SDimitry Andric   // section offsets.
79910dc89a5SDimitry Andric   for (BaseCommand *Base : Sec->SectionCommands) {
80010dc89a5SDimitry Andric     // This handles the assignments to symbol or to the dot.
80110dc89a5SDimitry Andric     if (auto *Cmd = dyn_cast<SymbolAssignment>(Base)) {
8024ba319b5SDimitry Andric       Cmd->Addr = Dot;
80310dc89a5SDimitry Andric       assignSymbol(Cmd, true);
8044ba319b5SDimitry Andric       Cmd->Size = Dot - Cmd->Addr;
80510dc89a5SDimitry Andric       continue;
80610dc89a5SDimitry Andric     }
80710dc89a5SDimitry Andric 
80810dc89a5SDimitry Andric     // Handle BYTE(), SHORT(), LONG(), or QUAD().
80910dc89a5SDimitry Andric     if (auto *Cmd = dyn_cast<ByteCommand>(Base)) {
81010dc89a5SDimitry Andric       Cmd->Offset = Dot - Ctx->OutSec->Addr;
81110dc89a5SDimitry Andric       Dot += Cmd->Size;
8124ba319b5SDimitry Andric       expandOutputSection(Cmd->Size);
81310dc89a5SDimitry Andric       continue;
81410dc89a5SDimitry Andric     }
81510dc89a5SDimitry Andric 
81610dc89a5SDimitry Andric     // Handle a single input section description command.
81710dc89a5SDimitry Andric     // It calculates and assigns the offsets for each section and also
81810dc89a5SDimitry Andric     // updates the output section size.
819*b5893f02SDimitry Andric     for (InputSection *Sec : cast<InputSectionDescription>(Base)->Sections)
82010dc89a5SDimitry Andric       output(Sec);
82110dc89a5SDimitry Andric   }
82210dc89a5SDimitry Andric }
82346b69c69SDimitry Andric 
isDiscardable(OutputSection & Sec)8244ba319b5SDimitry Andric static bool isDiscardable(OutputSection &Sec) {
8254ba319b5SDimitry Andric   // We do not remove empty sections that are explicitly
8264ba319b5SDimitry Andric   // assigned to any segment.
8274ba319b5SDimitry Andric   if (!Sec.Phdrs.empty())
8281189dbaaSDimitry Andric     return false;
8291189dbaaSDimitry Andric 
8304ba319b5SDimitry Andric   // We do not want to remove sections that reference symbols in address and
8314ba319b5SDimitry Andric   // other expressions. We add script symbols as undefined, and want to ensure
8324ba319b5SDimitry Andric   // all of them are defined in the output, hence have to keep them.
8334ba319b5SDimitry Andric   if (Sec.ExpressionsUseSymbols)
8344ba319b5SDimitry Andric     return false;
8354ba319b5SDimitry Andric 
8364ba319b5SDimitry Andric   for (BaseCommand *Base : Sec.SectionCommands) {
8374ba319b5SDimitry Andric     if (auto Cmd = dyn_cast<SymbolAssignment>(Base))
8384ba319b5SDimitry Andric       // Don't create empty output sections just for unreferenced PROVIDE
8394ba319b5SDimitry Andric       // symbols.
8404ba319b5SDimitry Andric       if (Cmd->Name != "." && !Cmd->Sym)
8414ba319b5SDimitry Andric         continue;
8424ba319b5SDimitry Andric 
84346b69c69SDimitry Andric     if (!isa<InputSectionDescription>(*Base))
8441189dbaaSDimitry Andric       return false;
8454ba319b5SDimitry Andric   }
8461189dbaaSDimitry Andric   return true;
8471189dbaaSDimitry Andric }
8481189dbaaSDimitry Andric 
adjustSectionsBeforeSorting()84946b69c69SDimitry Andric void LinkerScript::adjustSectionsBeforeSorting() {
8501189dbaaSDimitry Andric   // If the output section contains only symbol assignments, create a
85110dc89a5SDimitry Andric   // corresponding output section. The issue is what to do with linker script
85210dc89a5SDimitry Andric   // like ".foo : { symbol = 42; }". One option would be to convert it to
85310dc89a5SDimitry Andric   // "symbol = 42;". That is, move the symbol out of the empty section
85410dc89a5SDimitry Andric   // description. That seems to be what bfd does for this simple case. The
85510dc89a5SDimitry Andric   // problem is that this is not completely general. bfd will give up and
85610dc89a5SDimitry Andric   // create a dummy section too if there is a ". = . + 1" inside the section
85710dc89a5SDimitry Andric   // for example.
85810dc89a5SDimitry Andric   // Given that we want to create the section, we have to worry what impact
85910dc89a5SDimitry Andric   // it will have on the link. For example, if we just create a section with
86010dc89a5SDimitry Andric   // 0 for flags, it would change which PT_LOADs are created.
8614ba319b5SDimitry Andric   // We could remember that particular section is dummy and ignore it in
86210dc89a5SDimitry Andric   // other parts of the linker, but unfortunately there are quite a few places
86310dc89a5SDimitry Andric   // that would need to change:
86410dc89a5SDimitry Andric   //   * The program header creation.
86510dc89a5SDimitry Andric   //   * The orphan section placement.
86610dc89a5SDimitry Andric   //   * The address assignment.
86710dc89a5SDimitry Andric   // The other option is to pick flags that minimize the impact the section
86810dc89a5SDimitry Andric   // will have on the rest of the linker. That is why we copy the flags from
86910dc89a5SDimitry Andric   // the previous sections. Only a few flags are needed to keep the impact low.
87046b69c69SDimitry Andric   uint64_t Flags = SHF_ALLOC;
8710f5676f4SDimitry Andric 
8724ba319b5SDimitry Andric   for (BaseCommand *&Cmd : SectionCommands) {
87310dc89a5SDimitry Andric     auto *Sec = dyn_cast<OutputSection>(Cmd);
87410dc89a5SDimitry Andric     if (!Sec)
8751189dbaaSDimitry Andric       continue;
8764ba319b5SDimitry Andric 
8774ba319b5SDimitry Andric     // Handle align (e.g. ".foo : ALIGN(16) { ... }").
8784ba319b5SDimitry Andric     if (Sec->AlignExpr)
8794ba319b5SDimitry Andric       Sec->Alignment =
8804ba319b5SDimitry Andric           std::max<uint32_t>(Sec->Alignment, Sec->AlignExpr().getValue());
8814ba319b5SDimitry Andric 
8824ba319b5SDimitry Andric     // A live output section means that some input section was added to it. It
8834ba319b5SDimitry Andric     // might have been removed (if it was empty synthetic section), but we at
8844ba319b5SDimitry Andric     // least know the flags.
8854ba319b5SDimitry Andric     if (Sec->Live)
8864ba319b5SDimitry Andric       Flags = Sec->Flags;
8874ba319b5SDimitry Andric 
8884ba319b5SDimitry Andric     // We do not want to keep any special flags for output section
8894ba319b5SDimitry Andric     // in case it is empty.
8904ba319b5SDimitry Andric     bool IsEmpty = getInputSections(Sec).empty();
8914ba319b5SDimitry Andric     if (IsEmpty)
8924ba319b5SDimitry Andric       Sec->Flags = Flags & (SHF_ALLOC | SHF_WRITE | SHF_EXECINSTR);
8934ba319b5SDimitry Andric 
8944ba319b5SDimitry Andric     if (IsEmpty && isDiscardable(*Sec)) {
8954ba319b5SDimitry Andric       Sec->Live = false;
8964ba319b5SDimitry Andric       Cmd = nullptr;
8974ba319b5SDimitry Andric     }
8981189dbaaSDimitry Andric   }
8991189dbaaSDimitry Andric 
9004ba319b5SDimitry Andric   // It is common practice to use very generic linker scripts. So for any
9014ba319b5SDimitry Andric   // given run some of the output sections in the script will be empty.
9024ba319b5SDimitry Andric   // We could create corresponding empty output sections, but that would
9034ba319b5SDimitry Andric   // clutter the output.
9044ba319b5SDimitry Andric   // We instead remove trivially empty sections. The bfd linker seems even
9054ba319b5SDimitry Andric   // more aggressive at removing them.
9064ba319b5SDimitry Andric   llvm::erase_if(SectionCommands, [&](BaseCommand *Base) { return !Base; });
9071189dbaaSDimitry Andric }
9081189dbaaSDimitry Andric 
adjustSectionsAfterSorting()90946b69c69SDimitry Andric void LinkerScript::adjustSectionsAfterSorting() {
91046b69c69SDimitry Andric   // Try and find an appropriate memory region to assign offsets in.
91110dc89a5SDimitry Andric   for (BaseCommand *Base : SectionCommands) {
91210dc89a5SDimitry Andric     if (auto *Sec = dyn_cast<OutputSection>(Base)) {
91370bad665SEd Maste       if (!Sec->LMARegionName.empty()) {
91470bad665SEd Maste         if (MemoryRegion *M = MemoryRegions.lookup(Sec->LMARegionName))
91570bad665SEd Maste           Sec->LMARegion = M;
91670bad665SEd Maste         else
91770bad665SEd Maste           error("memory region '" + Sec->LMARegionName + "' not declared");
91870bad665SEd Maste       }
91910dc89a5SDimitry Andric       Sec->MemRegion = findMemoryRegion(Sec);
92046b69c69SDimitry Andric     }
92146b69c69SDimitry Andric   }
92246b69c69SDimitry Andric 
9231189dbaaSDimitry Andric   // If output section command doesn't specify any segments,
9241189dbaaSDimitry Andric   // and we haven't previously assigned any section to segment,
9251189dbaaSDimitry Andric   // then we simply assign section to the very first load segment.
9261189dbaaSDimitry Andric   // Below is an example of such linker script:
9271189dbaaSDimitry Andric   // PHDRS { seg PT_LOAD; }
9281189dbaaSDimitry Andric   // SECTIONS { .aaa : { *(.aaa) } }
9291189dbaaSDimitry Andric   std::vector<StringRef> DefPhdrs;
9304ba319b5SDimitry Andric   auto FirstPtLoad = llvm::find_if(PhdrsCommands, [](const PhdrsCommand &Cmd) {
9314ba319b5SDimitry Andric     return Cmd.Type == PT_LOAD;
9324ba319b5SDimitry Andric   });
93310dc89a5SDimitry Andric   if (FirstPtLoad != PhdrsCommands.end())
9341189dbaaSDimitry Andric     DefPhdrs.push_back(FirstPtLoad->Name);
9351189dbaaSDimitry Andric 
9361189dbaaSDimitry Andric   // Walk the commands and propagate the program headers to commands that don't
9371189dbaaSDimitry Andric   // explicitly specify them.
93810dc89a5SDimitry Andric   for (BaseCommand *Base : SectionCommands) {
93910dc89a5SDimitry Andric     auto *Sec = dyn_cast<OutputSection>(Base);
94010dc89a5SDimitry Andric     if (!Sec)
9411189dbaaSDimitry Andric       continue;
94246b69c69SDimitry Andric 
94310dc89a5SDimitry Andric     if (Sec->Phdrs.empty()) {
944c4394386SDimitry Andric       // To match the bfd linker script behaviour, only propagate program
945c4394386SDimitry Andric       // headers to sections that are allocated.
94610dc89a5SDimitry Andric       if (Sec->Flags & SHF_ALLOC)
94710dc89a5SDimitry Andric         Sec->Phdrs = DefPhdrs;
948c4394386SDimitry Andric     } else {
94910dc89a5SDimitry Andric       DefPhdrs = Sec->Phdrs;
95010dc89a5SDimitry Andric     }
9511189dbaaSDimitry Andric   }
952c4394386SDimitry Andric }
9531189dbaaSDimitry Andric 
computeBase(uint64_t Min,bool AllocateHeaders)9544ba319b5SDimitry Andric static uint64_t computeBase(uint64_t Min, bool AllocateHeaders) {
9554ba319b5SDimitry Andric   // If there is no SECTIONS or if the linkerscript is explicit about program
9564ba319b5SDimitry Andric   // headers, do our best to allocate them.
9574ba319b5SDimitry Andric   if (!Script->HasSectionsCommand || AllocateHeaders)
9584ba319b5SDimitry Andric     return 0;
9594ba319b5SDimitry Andric   // Otherwise only allocate program headers if that would not add a page.
9604ba319b5SDimitry Andric   return alignDown(Min, Config->MaxPageSize);
9611189dbaaSDimitry Andric }
9621189dbaaSDimitry Andric 
96310dc89a5SDimitry Andric // Try to find an address for the file and program headers output sections,
96410dc89a5SDimitry Andric // which were unconditionally added to the first PT_LOAD segment earlier.
96510dc89a5SDimitry Andric //
96610dc89a5SDimitry Andric // When using the default layout, we check if the headers fit below the first
96710dc89a5SDimitry Andric // allocated section. When using a linker script, we also check if the headers
96810dc89a5SDimitry Andric // are covered by the output section. This allows omitting the headers by not
96910dc89a5SDimitry Andric // leaving enough space for them in the linker script; this pattern is common
97010dc89a5SDimitry Andric // in embedded systems.
97110dc89a5SDimitry Andric //
97210dc89a5SDimitry Andric // If there isn't enough space for these sections, we'll remove them from the
97310dc89a5SDimitry Andric // PT_LOAD segment, and we'll also remove the PT_PHDR segment.
allocateHeaders(std::vector<PhdrEntry * > & Phdrs)97410dc89a5SDimitry Andric void LinkerScript::allocateHeaders(std::vector<PhdrEntry *> &Phdrs) {
975c4394386SDimitry Andric   uint64_t Min = std::numeric_limits<uint64_t>::max();
97610dc89a5SDimitry Andric   for (OutputSection *Sec : OutputSections)
977c4394386SDimitry Andric     if (Sec->Flags & SHF_ALLOC)
978c4394386SDimitry Andric       Min = std::min<uint64_t>(Min, Sec->Addr);
979c4394386SDimitry Andric 
98010dc89a5SDimitry Andric   auto It = llvm::find_if(
98110dc89a5SDimitry Andric       Phdrs, [](const PhdrEntry *E) { return E->p_type == PT_LOAD; });
98210dc89a5SDimitry Andric   if (It == Phdrs.end())
983c4394386SDimitry Andric     return;
98410dc89a5SDimitry Andric   PhdrEntry *FirstPTLoad = *It;
9850f5676f4SDimitry Andric 
9864ba319b5SDimitry Andric   bool HasExplicitHeaders =
9874ba319b5SDimitry Andric       llvm::any_of(PhdrsCommands, [](const PhdrsCommand &Cmd) {
9884ba319b5SDimitry Andric         return Cmd.HasPhdrs || Cmd.HasFilehdr;
9894ba319b5SDimitry Andric       });
9900f5676f4SDimitry Andric   uint64_t HeaderSize = getHeaderSize();
9914ba319b5SDimitry Andric   if (HeaderSize <= Min - computeBase(Min, HasExplicitHeaders)) {
9920f5676f4SDimitry Andric     Min = alignDown(Min - HeaderSize, Config->MaxPageSize);
9930f5676f4SDimitry Andric     Out::ElfHeader->Addr = Min;
9940f5676f4SDimitry Andric     Out::ProgramHeaders->Addr = Min + Out::ElfHeader->Size;
995c4394386SDimitry Andric     return;
9960f5676f4SDimitry Andric   }
9970f5676f4SDimitry Andric 
9984ba319b5SDimitry Andric   // Error if we were explicitly asked to allocate headers.
9994ba319b5SDimitry Andric   if (HasExplicitHeaders)
10004ba319b5SDimitry Andric     error("could not allocate headers");
10014ba319b5SDimitry Andric 
100210dc89a5SDimitry Andric   Out::ElfHeader->PtLoad = nullptr;
100310dc89a5SDimitry Andric   Out::ProgramHeaders->PtLoad = nullptr;
100410dc89a5SDimitry Andric   FirstPTLoad->FirstSec = findFirstSection(FirstPTLoad);
100510dc89a5SDimitry Andric 
100610dc89a5SDimitry Andric   llvm::erase_if(Phdrs,
100710dc89a5SDimitry Andric                  [](const PhdrEntry *E) { return E->p_type == PT_PHDR; });
10080f5676f4SDimitry Andric }
10090f5676f4SDimitry Andric 
AddressState()101010dc89a5SDimitry Andric LinkerScript::AddressState::AddressState() {
101110dc89a5SDimitry Andric   for (auto &MRI : Script->MemoryRegions) {
10126fcb8605SEd Maste     MemoryRegion *MR = MRI.second;
10136fcb8605SEd Maste     MR->CurPos = MR->Origin;
1014c4394386SDimitry Andric   }
1015c4394386SDimitry Andric }
1016c4394386SDimitry Andric 
getInitialDot()101710dc89a5SDimitry Andric static uint64_t getInitialDot() {
101810dc89a5SDimitry Andric   // By default linker scripts use an initial value of 0 for '.',
101910dc89a5SDimitry Andric   // but prefer -image-base if set.
102010dc89a5SDimitry Andric   if (Script->HasSectionsCommand)
102110dc89a5SDimitry Andric     return Config->ImageBase ? *Config->ImageBase : 0;
102210dc89a5SDimitry Andric 
102310dc89a5SDimitry Andric   uint64_t StartAddr = UINT64_MAX;
102410dc89a5SDimitry Andric   // The Sections with -T<section> have been sorted in order of ascending
102510dc89a5SDimitry Andric   // address. We must lower StartAddr if the lowest -T<section address> as
102610dc89a5SDimitry Andric   // calls to setDot() must be monotonically increasing.
102710dc89a5SDimitry Andric   for (auto &KV : Config->SectionStartMap)
102810dc89a5SDimitry Andric     StartAddr = std::min(StartAddr, KV.second);
102910dc89a5SDimitry Andric   return std::min(StartAddr, Target->getImageBase() + elf::getHeaderSize());
103010dc89a5SDimitry Andric }
103110dc89a5SDimitry Andric 
103210dc89a5SDimitry Andric // Here we assign addresses as instructed by linker script SECTIONS
103310dc89a5SDimitry Andric // sub-commands. Doing that allows us to use final VA values, so here
103410dc89a5SDimitry Andric // we also handle rest commands like symbol assignments and ASSERTs.
assignAddresses()1035c4394386SDimitry Andric void LinkerScript::assignAddresses() {
103610dc89a5SDimitry Andric   Dot = getInitialDot();
103710dc89a5SDimitry Andric 
103810dc89a5SDimitry Andric   auto Deleter = make_unique<AddressState>();
103910dc89a5SDimitry Andric   Ctx = Deleter.get();
104046b69c69SDimitry Andric   ErrorOnMissingSection = true;
104146b69c69SDimitry Andric   switchTo(Aether);
10421189dbaaSDimitry Andric 
104310dc89a5SDimitry Andric   for (BaseCommand *Base : SectionCommands) {
104446b69c69SDimitry Andric     if (auto *Cmd = dyn_cast<SymbolAssignment>(Base)) {
10454ba319b5SDimitry Andric       Cmd->Addr = Dot;
104646b69c69SDimitry Andric       assignSymbol(Cmd, false);
10474ba319b5SDimitry Andric       Cmd->Size = Dot - Cmd->Addr;
104897c3811cSEd Maste       continue;
104997c3811cSEd Maste     }
105010dc89a5SDimitry Andric     assignOffsets(cast<OutputSection>(Base));
105197c3811cSEd Maste   }
105210dc89a5SDimitry Andric   Ctx = nullptr;
10531189dbaaSDimitry Andric }
10541189dbaaSDimitry Andric 
10551189dbaaSDimitry Andric // Creates program headers as instructed by PHDRS linker script command.
createPhdrs()105610dc89a5SDimitry Andric std::vector<PhdrEntry *> LinkerScript::createPhdrs() {
105710dc89a5SDimitry Andric   std::vector<PhdrEntry *> Ret;
10581189dbaaSDimitry Andric 
10591189dbaaSDimitry Andric   // Process PHDRS and FILEHDR keywords because they are not
10601189dbaaSDimitry Andric   // real output sections and cannot be added in the following loop.
106110dc89a5SDimitry Andric   for (const PhdrsCommand &Cmd : PhdrsCommands) {
106210dc89a5SDimitry Andric     PhdrEntry *Phdr = make<PhdrEntry>(Cmd.Type, Cmd.Flags ? *Cmd.Flags : PF_R);
10631189dbaaSDimitry Andric 
10641189dbaaSDimitry Andric     if (Cmd.HasFilehdr)
106510dc89a5SDimitry Andric       Phdr->add(Out::ElfHeader);
10661189dbaaSDimitry Andric     if (Cmd.HasPhdrs)
106710dc89a5SDimitry Andric       Phdr->add(Out::ProgramHeaders);
10681189dbaaSDimitry Andric 
10691189dbaaSDimitry Andric     if (Cmd.LMAExpr) {
107010dc89a5SDimitry Andric       Phdr->p_paddr = Cmd.LMAExpr().getValue();
107110dc89a5SDimitry Andric       Phdr->HasLMA = true;
10721189dbaaSDimitry Andric     }
107310dc89a5SDimitry Andric     Ret.push_back(Phdr);
10741189dbaaSDimitry Andric   }
10751189dbaaSDimitry Andric 
10761189dbaaSDimitry Andric   // Add output sections to program headers.
107710dc89a5SDimitry Andric   for (OutputSection *Sec : OutputSections) {
10781189dbaaSDimitry Andric     // Assign headers specified by linker script
107910dc89a5SDimitry Andric     for (size_t Id : getPhdrIndices(Sec)) {
108010dc89a5SDimitry Andric       Ret[Id]->add(Sec);
108110dc89a5SDimitry Andric       if (!PhdrsCommands[Id].Flags.hasValue())
108210dc89a5SDimitry Andric         Ret[Id]->p_flags |= Sec->getPhdrFlags();
10831189dbaaSDimitry Andric     }
10841189dbaaSDimitry Andric   }
10851189dbaaSDimitry Andric   return Ret;
10861189dbaaSDimitry Andric }
10871189dbaaSDimitry Andric 
108810dc89a5SDimitry Andric // Returns true if we should emit an .interp section.
108910dc89a5SDimitry Andric //
109010dc89a5SDimitry Andric // We usually do. But if PHDRS commands are given, and
109110dc89a5SDimitry Andric // no PT_INTERP is there, there's no place to emit an
109210dc89a5SDimitry Andric // .interp, so we don't do that in that case.
needsInterpSection()109310dc89a5SDimitry Andric bool LinkerScript::needsInterpSection() {
109410dc89a5SDimitry Andric   if (PhdrsCommands.empty())
109510dc89a5SDimitry Andric     return true;
109610dc89a5SDimitry Andric   for (PhdrsCommand &Cmd : PhdrsCommands)
109746b69c69SDimitry Andric     if (Cmd.Type == PT_INTERP)
109846b69c69SDimitry Andric       return true;
1099c4394386SDimitry Andric   return false;
1100c4394386SDimitry Andric }
1101c4394386SDimitry Andric 
getSymbolValue(StringRef Name,const Twine & Loc)110210dc89a5SDimitry Andric ExprValue LinkerScript::getSymbolValue(StringRef Name, const Twine &Loc) {
110310dc89a5SDimitry Andric   if (Name == ".") {
110410dc89a5SDimitry Andric     if (Ctx)
110510dc89a5SDimitry Andric       return {Ctx->OutSec, false, Dot - Ctx->OutSec->Addr, Loc};
110610dc89a5SDimitry Andric     error(Loc + ": unable to get location counter value");
1107302affcbSDimitry Andric     return 0;
110897c3811cSEd Maste   }
110997c3811cSEd Maste 
111010dc89a5SDimitry Andric   if (Symbol *Sym = Symtab->find(Name)) {
111110dc89a5SDimitry Andric     if (auto *DS = dyn_cast<Defined>(Sym))
111210dc89a5SDimitry Andric       return {DS->Section, false, DS->Value, Loc};
11134ba319b5SDimitry Andric     if (isa<SharedSymbol>(Sym))
11144ba319b5SDimitry Andric       if (!ErrorOnMissingSection)
11154ba319b5SDimitry Andric         return {nullptr, false, 0, Loc};
11161189dbaaSDimitry Andric   }
11171189dbaaSDimitry Andric 
111810dc89a5SDimitry Andric   error(Loc + ": symbol not found: " + Name);
11191189dbaaSDimitry Andric   return 0;
112097c3811cSEd Maste }
112197c3811cSEd Maste 
112210dc89a5SDimitry Andric // Returns the index of the segment named Name.
getPhdrIndex(ArrayRef<PhdrsCommand> Vec,StringRef Name)112310dc89a5SDimitry Andric static Optional<size_t> getPhdrIndex(ArrayRef<PhdrsCommand> Vec,
112410dc89a5SDimitry Andric                                      StringRef Name) {
112510dc89a5SDimitry Andric   for (size_t I = 0; I < Vec.size(); ++I)
112610dc89a5SDimitry Andric     if (Vec[I].Name == Name)
112710dc89a5SDimitry Andric       return I;
112810dc89a5SDimitry Andric   return None;
112910dc89a5SDimitry Andric }
1130edd7eaddSDimitry Andric 
11315517e702SDimitry Andric // Returns indices of ELF headers containing specific section. Each index is a
11325517e702SDimitry Andric // zero based number of ELF header listed within PHDRS {} script block.
getPhdrIndices(OutputSection * Cmd)113310dc89a5SDimitry Andric std::vector<size_t> LinkerScript::getPhdrIndices(OutputSection *Cmd) {
11341189dbaaSDimitry Andric   std::vector<size_t> Ret;
113510dc89a5SDimitry Andric 
113610dc89a5SDimitry Andric   for (StringRef S : Cmd->Phdrs) {
113710dc89a5SDimitry Andric     if (Optional<size_t> Idx = getPhdrIndex(PhdrsCommands, S))
113810dc89a5SDimitry Andric       Ret.push_back(*Idx);
113910dc89a5SDimitry Andric     else if (S != "NONE")
114010dc89a5SDimitry Andric       error(Cmd->Location + ": section header '" + S +
114110dc89a5SDimitry Andric             "' is not listed in PHDRS");
1142edd7eaddSDimitry Andric   }
11431189dbaaSDimitry Andric   return Ret;
11441189dbaaSDimitry Andric }
1145