1f7c5fbb1SRui Ueyama //===- LinkerScript.cpp ---------------------------------------------------===// 2f7c5fbb1SRui Ueyama // 3f7c5fbb1SRui Ueyama // The LLVM Linker 4f7c5fbb1SRui Ueyama // 5f7c5fbb1SRui Ueyama // This file is distributed under the University of Illinois Open Source 6f7c5fbb1SRui Ueyama // License. See LICENSE.TXT for details. 7f7c5fbb1SRui Ueyama // 8f7c5fbb1SRui Ueyama //===----------------------------------------------------------------------===// 9f7c5fbb1SRui Ueyama // 10f7c5fbb1SRui Ueyama // This file contains the parser/evaluator of the linker script. 11f7c5fbb1SRui Ueyama // 12f7c5fbb1SRui Ueyama //===----------------------------------------------------------------------===// 13f7c5fbb1SRui Ueyama 14717677afSRui Ueyama #include "LinkerScript.h" 15f7c5fbb1SRui Ueyama #include "Config.h" 161ebc8ed7SRui Ueyama #include "InputSection.h" 179381eb10SRui Ueyama #include "Memory.h" 18652852c5SGeorge Rimar #include "OutputSections.h" 1993c9af42SRui Ueyama #include "Strings.h" 20f7c5fbb1SRui Ueyama #include "SymbolTable.h" 2155518e7dSRui Ueyama #include "Symbols.h" 223fb5a6dcSGeorge Rimar #include "SyntheticSections.h" 23bbe38602SEugene Leviant #include "Writer.h" 2422886a28SEugene Zelenko #include "llvm/ADT/STLExtras.h" 2522886a28SEugene Zelenko #include "llvm/ADT/StringRef.h" 2622886a28SEugene Zelenko #include "llvm/Support/Casting.h" 27652852c5SGeorge Rimar #include "llvm/Support/ELF.h" 2822886a28SEugene Zelenko #include "llvm/Support/Endian.h" 2922886a28SEugene Zelenko #include "llvm/Support/ErrorHandling.h" 30f7c5fbb1SRui Ueyama #include "llvm/Support/FileSystem.h" 31f03f3cc1SRui Ueyama #include "llvm/Support/Path.h" 3222886a28SEugene Zelenko #include <algorithm> 3322886a28SEugene Zelenko #include <cassert> 3422886a28SEugene Zelenko #include <cstddef> 3522886a28SEugene Zelenko #include <cstdint> 3622886a28SEugene Zelenko #include <iterator> 3722886a28SEugene Zelenko #include <limits> 3822886a28SEugene Zelenko #include <string> 3922886a28SEugene Zelenko #include <vector> 40f7c5fbb1SRui Ueyama 41f7c5fbb1SRui Ueyama using namespace llvm; 42652852c5SGeorge Rimar using namespace llvm::ELF; 431ebc8ed7SRui Ueyama using namespace llvm::object; 44e38cbab5SGeorge Rimar using namespace llvm::support::endian; 45f7c5fbb1SRui Ueyama using namespace lld; 46e0df00b9SRafael Espindola using namespace lld::elf; 47f7c5fbb1SRui Ueyama 48a34da938SRui Ueyama LinkerScript *elf::Script; 49a34da938SRui Ueyama 5072dc195dSRafael Espindola uint64_t ExprValue::getValue() const { 5172dc195dSRafael Espindola if (Sec) 5272dc195dSRafael Espindola return Sec->getOffset(Val) + Sec->getOutputSection()->Addr; 5372dc195dSRafael Espindola return Val; 5472dc195dSRafael Espindola } 5572dc195dSRafael Espindola 567ba5f47eSRafael Espindola uint64_t ExprValue::getSecAddr() const { 577ba5f47eSRafael Espindola if (Sec) 587ba5f47eSRafael Espindola return Sec->getOffset(0) + Sec->getOutputSection()->Addr; 597ba5f47eSRafael Espindola return 0; 607ba5f47eSRafael Espindola } 617ba5f47eSRafael Espindola 628f1f3c40SMeador Inge template <class ELFT> static SymbolBody *addRegular(SymbolAssignment *Cmd) { 635e51f7d2SPetr Hosek Symbol *Sym; 643dabfc6bSRafael Espindola uint8_t Visibility = Cmd->Hidden ? STV_HIDDEN : STV_DEFAULT; 655e51f7d2SPetr Hosek std::tie(Sym, std::ignore) = Symtab<ELFT>::X->insert( 665e51f7d2SPetr Hosek Cmd->Name, /*Type*/ 0, Visibility, /*CanOmitFromDynSym*/ false, 675e51f7d2SPetr Hosek /*File*/ nullptr); 685e51f7d2SPetr Hosek Sym->Binding = STB_GLOBAL; 6972dc195dSRafael Espindola ExprValue Value = Cmd->Expression(); 7072dc195dSRafael Espindola SectionBase *Sec = Value.isAbsolute() ? nullptr : Value.Sec; 7101aa795fSGeorge Rimar 7201aa795fSGeorge Rimar // We want to set symbol values early if we can. This allows us to use symbols 7301aa795fSGeorge Rimar // as variables in linker scripts. Doing so allows us to write expressions 7401aa795fSGeorge Rimar // like this: `alignment = 16; . = ALIGN(., alignment)` 7501aa795fSGeorge Rimar uint64_t SymValue = Value.isAbsolute() ? Value.getValue() : 0; 7680474a26SRui Ueyama replaceBody<DefinedRegular>(Sym, Cmd->Name, /*IsLocal=*/false, Visibility, 7701aa795fSGeorge Rimar STT_NOTYPE, SymValue, 0, Sec, nullptr); 788f1f3c40SMeador Inge return Sym->body(); 79ceabe80eSEugene Leviant } 80ceabe80eSEugene Leviant 81b8dd23f5SRui Ueyama OutputSection *LinkerScript::getOutputSection(const Twine &Loc, 82851dc1e8SGeorge Rimar StringRef Name) { 83851dc1e8SGeorge Rimar for (OutputSection *Sec : *OutputSections) 84851dc1e8SGeorge Rimar if (Sec->Name == Name) 85851dc1e8SGeorge Rimar return Sec; 86851dc1e8SGeorge Rimar 87a08fa2ecSRui Ueyama static OutputSection Dummy("", 0, 0); 8872dc195dSRafael Espindola if (ErrorOnMissingSection) 89851dc1e8SGeorge Rimar error(Loc + ": undefined section " + Name); 90a08fa2ecSRui Ueyama return &Dummy; 91851dc1e8SGeorge Rimar } 92851dc1e8SGeorge Rimar 93d83ce1b4SGeorge Rimar // This function is essentially the same as getOutputSection(Name)->Size, 94d83ce1b4SGeorge Rimar // but it won't print out an error message if a given section is not found. 95d83ce1b4SGeorge Rimar // 96d83ce1b4SGeorge Rimar // Linker script does not create an output section if its content is empty. 97d83ce1b4SGeorge Rimar // We want to allow SIZEOF(.foo) where .foo is a section which happened to 98d83ce1b4SGeorge Rimar // be empty. That is why this function is different from getOutputSection(). 99b8dd23f5SRui Ueyama uint64_t LinkerScript::getOutputSectionSize(StringRef Name) { 100d83ce1b4SGeorge Rimar for (OutputSection *Sec : *OutputSections) 101d83ce1b4SGeorge Rimar if (Sec->Name == Name) 102d83ce1b4SGeorge Rimar return Sec->Size; 103d83ce1b4SGeorge Rimar return 0; 104d83ce1b4SGeorge Rimar } 105d83ce1b4SGeorge Rimar 106b8dd23f5SRui Ueyama void LinkerScript::setDot(Expr E, const Twine &Loc, bool InSec) { 10772dc195dSRafael Espindola uint64_t Val = E().getValue(); 1084cd7352cSRafael Espindola if (Val < Dot) { 1094cd7352cSRafael Espindola if (InSec) 1102ee2d2dcSGeorge Rimar error(Loc + ": unable to move location counter backward for: " + 1112ee2d2dcSGeorge Rimar CurOutSec->Name); 1124cd7352cSRafael Espindola else 1132ee2d2dcSGeorge Rimar error(Loc + ": unable to move location counter backward"); 1144cd7352cSRafael Espindola } 1154cd7352cSRafael Espindola Dot = Val; 1164cd7352cSRafael Espindola // Update to location counter means update to section size. 1174cd7352cSRafael Espindola if (InSec) 1184cd7352cSRafael Espindola CurOutSec->Size = Dot - CurOutSec->Addr; 119679828ffSRafael Espindola } 120679828ffSRafael Espindola 121679828ffSRafael Espindola // Sets value of a symbol. Two kinds of symbols are processed: synthetic 122679828ffSRafael Espindola // symbols, whose value is an offset from beginning of section and regular 123679828ffSRafael Espindola // symbols whose value is absolute. 124b8dd23f5SRui Ueyama void LinkerScript::assignSymbol(SymbolAssignment *Cmd, bool InSec) { 125679828ffSRafael Espindola if (Cmd->Name == ".") { 1262ee2d2dcSGeorge Rimar setDot(Cmd->Expression, Cmd->Location, InSec); 1274cd7352cSRafael Espindola return; 1284cd7352cSRafael Espindola } 1294cd7352cSRafael Espindola 130b2b70975SGeorge Rimar if (!Cmd->Sym) 1318f1f3c40SMeador Inge return; 1328f1f3c40SMeador Inge 1335616adf6SRafael Espindola auto *Sym = cast<DefinedRegular>(Cmd->Sym); 13472dc195dSRafael Espindola ExprValue V = Cmd->Expression(); 13572dc195dSRafael Espindola if (V.isAbsolute()) { 13672dc195dSRafael Espindola Sym->Value = V.getValue(); 13772dc195dSRafael Espindola } else { 13872dc195dSRafael Espindola Sym->Section = V.Sec; 13972dc195dSRafael Espindola if (Sym->Section->Flags & SHF_ALLOC) 14072dc195dSRafael Espindola Sym->Value = V.Val; 14172dc195dSRafael Espindola else 14272dc195dSRafael Espindola Sym->Value = V.getValue(); 143ea590d91SRafael Espindola } 1448f1f3c40SMeador Inge } 1458f1f3c40SMeador Inge 146a8dba487SGeorge Rimar static SymbolBody *findSymbol(StringRef S) { 147a8dba487SGeorge Rimar switch (Config->EKind) { 148a8dba487SGeorge Rimar case ELF32LEKind: 149a8dba487SGeorge Rimar return Symtab<ELF32LE>::X->find(S); 150a8dba487SGeorge Rimar case ELF32BEKind: 151a8dba487SGeorge Rimar return Symtab<ELF32BE>::X->find(S); 152a8dba487SGeorge Rimar case ELF64LEKind: 153a8dba487SGeorge Rimar return Symtab<ELF64LE>::X->find(S); 154a8dba487SGeorge Rimar case ELF64BEKind: 155a8dba487SGeorge Rimar return Symtab<ELF64BE>::X->find(S); 156a8dba487SGeorge Rimar default: 157a8dba487SGeorge Rimar llvm_unreachable("unknown Config->EKind"); 158a8dba487SGeorge Rimar } 159a8dba487SGeorge Rimar } 160a8dba487SGeorge Rimar 161a8dba487SGeorge Rimar static SymbolBody *addRegularSymbol(SymbolAssignment *Cmd) { 162a8dba487SGeorge Rimar switch (Config->EKind) { 163a8dba487SGeorge Rimar case ELF32LEKind: 164a8dba487SGeorge Rimar return addRegular<ELF32LE>(Cmd); 165a8dba487SGeorge Rimar case ELF32BEKind: 166a8dba487SGeorge Rimar return addRegular<ELF32BE>(Cmd); 167a8dba487SGeorge Rimar case ELF64LEKind: 168a8dba487SGeorge Rimar return addRegular<ELF64LE>(Cmd); 169a8dba487SGeorge Rimar case ELF64BEKind: 170a8dba487SGeorge Rimar return addRegular<ELF64BE>(Cmd); 171a8dba487SGeorge Rimar default: 172a8dba487SGeorge Rimar llvm_unreachable("unknown Config->EKind"); 173a8dba487SGeorge Rimar } 174a8dba487SGeorge Rimar } 175a8dba487SGeorge Rimar 176b8dd23f5SRui Ueyama void LinkerScript::addSymbol(SymbolAssignment *Cmd) { 1771602421cSRui Ueyama if (Cmd->Name == ".") 1788f1f3c40SMeador Inge return; 1798f1f3c40SMeador Inge 1808f1f3c40SMeador Inge // If a symbol was in PROVIDE(), we need to define it only when 1818f1f3c40SMeador Inge // it is a referenced undefined symbol. 182a8dba487SGeorge Rimar SymbolBody *B = findSymbol(Cmd->Name); 1838f1f3c40SMeador Inge if (Cmd->Provide && (!B || B->isDefined())) 1848f1f3c40SMeador Inge return; 1858f1f3c40SMeador Inge 186a8dba487SGeorge Rimar Cmd->Sym = addRegularSymbol(Cmd); 187ceabe80eSEugene Leviant } 188ceabe80eSEugene Leviant 189076fe157SGeorge Rimar bool SymbolAssignment::classof(const BaseCommand *C) { 190076fe157SGeorge Rimar return C->Kind == AssignmentKind; 191076fe157SGeorge Rimar } 192076fe157SGeorge Rimar 193076fe157SGeorge Rimar bool OutputSectionCommand::classof(const BaseCommand *C) { 194076fe157SGeorge Rimar return C->Kind == OutputSectionKind; 195076fe157SGeorge Rimar } 196076fe157SGeorge Rimar 197eea3114fSGeorge Rimar bool InputSectionDescription::classof(const BaseCommand *C) { 198eea3114fSGeorge Rimar return C->Kind == InputSectionKind; 199eea3114fSGeorge Rimar } 200eea3114fSGeorge Rimar 201eefa758eSGeorge Rimar bool AssertCommand::classof(const BaseCommand *C) { 202eefa758eSGeorge Rimar return C->Kind == AssertKind; 203eefa758eSGeorge Rimar } 204eefa758eSGeorge Rimar 205e38cbab5SGeorge Rimar bool BytesDataCommand::classof(const BaseCommand *C) { 206e38cbab5SGeorge Rimar return C->Kind == BytesDataKind; 207e38cbab5SGeorge Rimar } 208e38cbab5SGeorge Rimar 209b4c9b81aSRafael Espindola static StringRef basename(InputSectionBase *S) { 210b4c9b81aSRafael Espindola if (S->File) 211b4c9b81aSRafael Espindola return sys::path::filename(S->File->getName()); 212e0be2901SRui Ueyama return ""; 213e0be2901SRui Ueyama } 214e0be2901SRui Ueyama 215b8dd23f5SRui Ueyama bool LinkerScript::shouldKeep(InputSectionBase *S) { 216e0be2901SRui Ueyama for (InputSectionDescription *ID : Opt.KeptSections) 217e0be2901SRui Ueyama if (ID->FilePat.match(basename(S))) 218cf43f179SEugene Leviant for (SectionPattern &P : ID->SectionPatterns) 219f91282e1SRui Ueyama if (P.SectionPat.match(S->Name)) 220eea3114fSGeorge Rimar return true; 221eea3114fSGeorge Rimar return false; 222eea3114fSGeorge Rimar } 223eea3114fSGeorge Rimar 224ea93fe00SRui Ueyama // A helper function for the SORT() command. 225c404d50dSRafael Espindola static std::function<bool(InputSectionBase *, InputSectionBase *)> 226be394db3SGeorge Rimar getComparator(SortSectionPolicy K) { 227be394db3SGeorge Rimar switch (K) { 228be394db3SGeorge Rimar case SortSectionPolicy::Alignment: 229ea93fe00SRui Ueyama return [](InputSectionBase *A, InputSectionBase *B) { 230ea93fe00SRui Ueyama // ">" is not a mistake. Sections with larger alignments are placed 231ea93fe00SRui Ueyama // before sections with smaller alignments in order to reduce the 232ea93fe00SRui Ueyama // amount of padding necessary. This is compatible with GNU. 233ea93fe00SRui Ueyama return A->Alignment > B->Alignment; 234ea93fe00SRui Ueyama }; 235be394db3SGeorge Rimar case SortSectionPolicy::Name: 236ea93fe00SRui Ueyama return [](InputSectionBase *A, InputSectionBase *B) { 237ea93fe00SRui Ueyama return A->Name < B->Name; 238ea93fe00SRui Ueyama }; 239be394db3SGeorge Rimar case SortSectionPolicy::Priority: 240ea93fe00SRui Ueyama return [](InputSectionBase *A, InputSectionBase *B) { 241ea93fe00SRui Ueyama return getPriority(A->Name) < getPriority(B->Name); 242ea93fe00SRui Ueyama }; 243be394db3SGeorge Rimar default: 244be394db3SGeorge Rimar llvm_unreachable("unknown sort policy"); 245be394db3SGeorge Rimar } 246742c3836SRui Ueyama } 2470702c4e8SGeorge Rimar 248ea93fe00SRui Ueyama // A helper function for the SORT() command. 249b4c9b81aSRafael Espindola static bool matchConstraints(ArrayRef<InputSectionBase *> Sections, 25006ae6836SGeorge Rimar ConstraintKind Kind) { 2518f66df92SGeorge Rimar if (Kind == ConstraintKind::NoConstraint) 2528f66df92SGeorge Rimar return true; 2532c7171bfSRui Ueyama 2542c7171bfSRui Ueyama bool IsRW = llvm::any_of(Sections, [](InputSectionBase *Sec) { 2552c7171bfSRui Ueyama return static_cast<InputSectionBase *>(Sec)->Flags & SHF_WRITE; 25606ae6836SGeorge Rimar }); 2572c7171bfSRui Ueyama 258e746e52cSRafael Espindola return (IsRW && Kind == ConstraintKind::ReadWrite) || 259e746e52cSRafael Espindola (!IsRW && Kind == ConstraintKind::ReadOnly); 26006ae6836SGeorge Rimar } 26106ae6836SGeorge Rimar 262c404d50dSRafael Espindola static void sortSections(InputSectionBase **Begin, InputSectionBase **End, 263ee924709SRui Ueyama SortSectionPolicy K) { 264ee924709SRui Ueyama if (K != SortSectionPolicy::Default && K != SortSectionPolicy::None) 26507171f21SGeorge Rimar std::stable_sort(Begin, End, getComparator(K)); 266ee924709SRui Ueyama } 267ee924709SRui Ueyama 268d3190795SRafael Espindola // Compute and remember which sections the InputSectionDescription matches. 26972e107f3SRui Ueyama std::vector<InputSectionBase *> 27072e107f3SRui Ueyama LinkerScript::computeInputSections(const InputSectionDescription *Cmd) { 27172e107f3SRui Ueyama std::vector<InputSectionBase *> Ret; 2728c6a5aafSRui Ueyama 27372e107f3SRui Ueyama // Collects all sections that satisfy constraints of Cmd. 27472e107f3SRui Ueyama for (const SectionPattern &Pat : Cmd->SectionPatterns) { 27572e107f3SRui Ueyama size_t SizeBefore = Ret.size(); 27672e107f3SRui Ueyama 27772e107f3SRui Ueyama for (InputSectionBase *Sec : InputSections) { 27872e107f3SRui Ueyama if (Sec->Assigned) 2798c6a5aafSRui Ueyama continue; 28072e107f3SRui Ueyama 281908a3d34SRafael Espindola // For -emit-relocs we have to ignore entries like 282908a3d34SRafael Espindola // .rela.dyn : { *(.rela.data) } 283908a3d34SRafael Espindola // which are common because they are in the default bfd script. 28472e107f3SRui Ueyama if (Sec->Type == SHT_REL || Sec->Type == SHT_RELA) 285908a3d34SRafael Espindola continue; 2868c6a5aafSRui Ueyama 28772e107f3SRui Ueyama StringRef Filename = basename(Sec); 28872e107f3SRui Ueyama if (!Cmd->FilePat.match(Filename) || 28972e107f3SRui Ueyama Pat.ExcludedFilePat.match(Filename) || 29072e107f3SRui Ueyama !Pat.SectionPat.match(Sec->Name)) 291e0be2901SRui Ueyama continue; 29272e107f3SRui Ueyama 29372e107f3SRui Ueyama Ret.push_back(Sec); 29472e107f3SRui Ueyama Sec->Assigned = true; 295f94efdddSRui Ueyama } 296d3190795SRafael Espindola 297ee924709SRui Ueyama // Sort sections as instructed by SORT-family commands and --sort-section 298ee924709SRui Ueyama // option. Because SORT-family commands can be nested at most two depth 299ee924709SRui Ueyama // (e.g. SORT_BY_NAME(SORT_BY_ALIGNMENT(.text.*))) and because the command 300ee924709SRui Ueyama // line option is respected even if a SORT command is given, the exact 301ee924709SRui Ueyama // behavior we have here is a bit complicated. Here are the rules. 302ee924709SRui Ueyama // 303ee924709SRui Ueyama // 1. If two SORT commands are given, --sort-section is ignored. 304ee924709SRui Ueyama // 2. If one SORT command is given, and if it is not SORT_NONE, 305ee924709SRui Ueyama // --sort-section is handled as an inner SORT command. 306ee924709SRui Ueyama // 3. If one SORT command is given, and if it is SORT_NONE, don't sort. 307ee924709SRui Ueyama // 4. If no SORT command is given, sort according to --sort-section. 30872e107f3SRui Ueyama InputSectionBase **Begin = Ret.data() + SizeBefore; 30972e107f3SRui Ueyama InputSectionBase **End = Ret.data() + Ret.size(); 31007171f21SGeorge Rimar if (Pat.SortOuter != SortSectionPolicy::None) { 31107171f21SGeorge Rimar if (Pat.SortInner == SortSectionPolicy::Default) 31207171f21SGeorge Rimar sortSections(Begin, End, Config->SortSection); 313ee924709SRui Ueyama else 31407171f21SGeorge Rimar sortSections(Begin, End, Pat.SortInner); 31507171f21SGeorge Rimar sortSections(Begin, End, Pat.SortOuter); 31607171f21SGeorge Rimar } 317ee924709SRui Ueyama } 31872e107f3SRui Ueyama return Ret; 319be94e1b6SRafael Espindola } 320be94e1b6SRafael Espindola 321b8dd23f5SRui Ueyama void LinkerScript::discard(ArrayRef<InputSectionBase *> V) { 322b4c9b81aSRafael Espindola for (InputSectionBase *S : V) { 323be94e1b6SRafael Espindola S->Live = false; 324503206c5SGeorge Rimar if (S == InX::ShStrTab) 325ecbfd871SRafael Espindola error("discarding .shstrtab section is not allowed"); 326647c1685SGeorge Rimar discard(S->DependentSections); 327be94e1b6SRafael Espindola } 328be94e1b6SRafael Espindola } 329be94e1b6SRafael Espindola 330b4c9b81aSRafael Espindola std::vector<InputSectionBase *> 331b8dd23f5SRui Ueyama LinkerScript::createInputSectionList(OutputSectionCommand &OutCmd) { 332b4c9b81aSRafael Espindola std::vector<InputSectionBase *> Ret; 333e7f912cdSRui Ueyama 3348f99f73cSRui Ueyama for (BaseCommand *Base : OutCmd.Commands) { 3358f99f73cSRui Ueyama auto *Cmd = dyn_cast<InputSectionDescription>(Base); 3367c3ff2ebSRafael Espindola if (!Cmd) 3370b9ce6a4SRui Ueyama continue; 33872e107f3SRui Ueyama 33972e107f3SRui Ueyama Cmd->Sections = computeInputSections(Cmd); 340e4c8b9b7SRafael Espindola Ret.insert(Ret.end(), Cmd->Sections.begin(), Cmd->Sections.end()); 3410b9ce6a4SRui Ueyama } 342e71a3f8aSRafael Espindola 3430b9ce6a4SRui Ueyama return Ret; 3440b9ce6a4SRui Ueyama } 3450b9ce6a4SRui Ueyama 346b8dd23f5SRui Ueyama void LinkerScript::processCommands(OutputSectionFactory &Factory) { 3475616adf6SRafael Espindola // A symbol can be assigned before any section is mentioned in the linker 3485616adf6SRafael Espindola // script. In an DSO, the symbol values are addresses, so the only important 3495616adf6SRafael Espindola // section values are: 3505616adf6SRafael Espindola // * SHN_UNDEF 3515616adf6SRafael Espindola // * SHN_ABS 3525616adf6SRafael Espindola // * Any value meaning a regular section. 3535616adf6SRafael Espindola // To handle that, create a dummy aether section that fills the void before 3545616adf6SRafael Espindola // the linker scripts switches to another section. It has an index of one 3555616adf6SRafael Espindola // which will map to whatever the first actual section is. 3565616adf6SRafael Espindola Aether = make<OutputSection>("", 0, SHF_ALLOC); 3575616adf6SRafael Espindola Aether->SectionIndex = 1; 3585616adf6SRafael Espindola CurOutSec = Aether; 35949592cf6SRafael Espindola Dot = 0; 3605616adf6SRafael Espindola 36192a5ba6dSRui Ueyama for (size_t I = 0; I < Opt.Commands.size(); ++I) { 3620b1b695aSRui Ueyama // Handle symbol assignments outside of any output section. 36392a5ba6dSRui Ueyama if (auto *Cmd = dyn_cast<SymbolAssignment>(Opt.Commands[I])) { 3644cd7352cSRafael Espindola addSymbol(Cmd); 3652ab5f73dSRui Ueyama continue; 3662ab5f73dSRui Ueyama } 3670b1b695aSRui Ueyama 36892a5ba6dSRui Ueyama if (auto *Cmd = dyn_cast<OutputSectionCommand>(Opt.Commands[I])) { 369b4c9b81aSRafael Espindola std::vector<InputSectionBase *> V = createInputSectionList(*Cmd); 3707bd37870SRafael Espindola 3710b1b695aSRui Ueyama // The output section name `/DISCARD/' is special. 3720b1b695aSRui Ueyama // Any input section assigned to it is discarded. 37348c3f1ceSRui Ueyama if (Cmd->Name == "/DISCARD/") { 3747bd37870SRafael Espindola discard(V); 37548c3f1ceSRui Ueyama continue; 37648c3f1ceSRui Ueyama } 3770b9ce6a4SRui Ueyama 3780b1b695aSRui Ueyama // This is for ONLY_IF_RO and ONLY_IF_RW. An output section directive 3790b1b695aSRui Ueyama // ".foo : ONLY_IF_R[OW] { ... }" is handled only if all member input 3800b1b695aSRui Ueyama // sections satisfy a given constraint. If not, a directive is handled 38107d7c42cSGeorge Rimar // as if it wasn't present from the beginning. 3820b1b695aSRui Ueyama // 3830b1b695aSRui Ueyama // Because we'll iterate over Commands many more times, the easiest 38407d7c42cSGeorge Rimar // way to "make it as if it wasn't present" is to just remove it. 385f7f0d088SGeorge Rimar if (!matchConstraints(V, Cmd->Constraint)) { 386b4c9b81aSRafael Espindola for (InputSectionBase *S : V) 387f94efdddSRui Ueyama S->Assigned = false; 38892a5ba6dSRui Ueyama Opt.Commands.erase(Opt.Commands.begin() + I); 38907d7c42cSGeorge Rimar --I; 3907c3ff2ebSRafael Espindola continue; 3917c3ff2ebSRafael Espindola } 3927c3ff2ebSRafael Espindola 3930b1b695aSRui Ueyama // A directive may contain symbol definitions like this: 3940b1b695aSRui Ueyama // ".foo : { ...; bar = .; }". Handle them. 3958f99f73cSRui Ueyama for (BaseCommand *Base : Cmd->Commands) 3968f99f73cSRui Ueyama if (auto *OutCmd = dyn_cast<SymbolAssignment>(Base)) 3974cd7352cSRafael Espindola addSymbol(OutCmd); 3987c3ff2ebSRafael Espindola 3990b1b695aSRui Ueyama // Handle subalign (e.g. ".foo : SUBALIGN(32) { ... }"). If subalign 4000b1b695aSRui Ueyama // is given, input sections are aligned to that value, whether the 4010b1b695aSRui Ueyama // given value is larger or smaller than the original section alignment. 4020b1b695aSRui Ueyama if (Cmd->SubalignExpr) { 40372dc195dSRafael Espindola uint32_t Subalign = Cmd->SubalignExpr().getValue(); 404b4c9b81aSRafael Espindola for (InputSectionBase *S : V) 4050b1b695aSRui Ueyama S->Alignment = Subalign; 40620d03194SEugene Leviant } 4070b1b695aSRui Ueyama 4080b1b695aSRui Ueyama // Add input sections to an output section. 4094aa2ef5bSRafael Espindola unsigned Pos = 0; 4104aa2ef5bSRafael Espindola for (InputSectionBase *S : V) { 4114aa2ef5bSRafael Espindola // The actual offset will be computed during 4124aa2ef5bSRafael Espindola // assignAddresses. For now, use the index as a very crude 4134aa2ef5bSRafael Espindola // approximation so that it is at least easy for other code to 4144aa2ef5bSRafael Espindola // know the section order. 4154aa2ef5bSRafael Espindola cast<InputSection>(S)->OutSecOff = Pos++; 4164f013bb3SRafael Espindola Factory.addInputSec(S, Cmd->Name, Cmd->Sec); 417eea3114fSGeorge Rimar } 41848c3f1ceSRui Ueyama } 4194aa2ef5bSRafael Espindola } 4205616adf6SRafael Espindola CurOutSec = nullptr; 421db24d9c3SGeorge Rimar } 422e63d81bdSEugene Leviant 423cbfe9e94SPeter Smith void LinkerScript::fabricateDefaultCommands(bool AllocateHeader) { 424cbfe9e94SPeter Smith std::vector<BaseCommand *> Commands; 425cbfe9e94SPeter Smith 426cbfe9e94SPeter Smith // Define start address 427cbfe9e94SPeter Smith uint64_t StartAddr = Config->ImageBase; 428cbfe9e94SPeter Smith if (AllocateHeader) 429cbfe9e94SPeter Smith StartAddr += elf::getHeaderSize(); 430cbfe9e94SPeter Smith 431c60b4510SPeter Smith // The Sections with -T<section> have been sorted in order of ascending 432c60b4510SPeter Smith // address. We must lower StartAddr if the lowest -T<section address> as 433c60b4510SPeter Smith // calls to setDot() must be monotonically increasing. 434c60b4510SPeter Smith for (auto& KV : Config->SectionStartMap) 435c60b4510SPeter Smith StartAddr = std::min(StartAddr, KV.second); 436c60b4510SPeter Smith 437cbfe9e94SPeter Smith Commands.push_back( 438cbfe9e94SPeter Smith make<SymbolAssignment>(".", [=] { return StartAddr; }, "")); 439cbfe9e94SPeter Smith 440cbfe9e94SPeter Smith // For each OutputSection that needs a VA fabricate an OutputSectionCommand 441cbfe9e94SPeter Smith // with an InputSectionDescription describing the InputSections 442cbfe9e94SPeter Smith for (OutputSection *Sec : *OutputSections) { 443cbfe9e94SPeter Smith if (!(Sec->Flags & SHF_ALLOC)) 444cbfe9e94SPeter Smith continue; 445cbfe9e94SPeter Smith 446c60b4510SPeter Smith auto *OSCmd = make<OutputSectionCommand>(Sec->Name); 447c60b4510SPeter Smith OSCmd->Sec = Sec; 448c60b4510SPeter Smith 449c60b4510SPeter Smith // Prefer user supplied address over additional alignment constraint 450cbfe9e94SPeter Smith auto I = Config->SectionStartMap.find(Sec->Name); 451cbfe9e94SPeter Smith if (I != Config->SectionStartMap.end()) 452cbfe9e94SPeter Smith Commands.push_back( 453cbfe9e94SPeter Smith make<SymbolAssignment>(".", [=] { return I->second; }, "")); 454c60b4510SPeter Smith else if (Sec->PageAlign) 455cbfe9e94SPeter Smith OSCmd->AddrExpr = [=] { 456cbfe9e94SPeter Smith return alignTo(Script->getDot(), Config->MaxPageSize); 457cbfe9e94SPeter Smith }; 458c60b4510SPeter Smith 459cbfe9e94SPeter Smith Commands.push_back(OSCmd); 460cbfe9e94SPeter Smith if (Sec->Sections.size()) { 461cbfe9e94SPeter Smith auto *ISD = make<InputSectionDescription>(""); 462cbfe9e94SPeter Smith OSCmd->Commands.push_back(ISD); 463cbfe9e94SPeter Smith for (InputSection *ISec : Sec->Sections) { 464cbfe9e94SPeter Smith ISD->Sections.push_back(ISec); 465cbfe9e94SPeter Smith ISec->Assigned = true; 466cbfe9e94SPeter Smith } 467cbfe9e94SPeter Smith } 468cbfe9e94SPeter Smith } 469cbfe9e94SPeter Smith // SECTIONS commands run before other non SECTIONS commands 470cbfe9e94SPeter Smith Commands.insert(Commands.end(), Opt.Commands.begin(), Opt.Commands.end()); 471cbfe9e94SPeter Smith Opt.Commands = std::move(Commands); 472cbfe9e94SPeter Smith } 473cbfe9e94SPeter Smith 4740b1b695aSRui Ueyama // Add sections that didn't match any sections command. 475b8dd23f5SRui Ueyama void LinkerScript::addOrphanSections(OutputSectionFactory &Factory) { 4764f013bb3SRafael Espindola for (InputSectionBase *S : InputSections) { 4774f013bb3SRafael Espindola if (!S->Live || S->OutSec) 4784f013bb3SRafael Espindola continue; 4794f013bb3SRafael Espindola StringRef Name = getOutputSectionName(S->Name); 4804f013bb3SRafael Espindola auto I = std::find_if( 4814f013bb3SRafael Espindola Opt.Commands.begin(), Opt.Commands.end(), [&](BaseCommand *Base) { 4824f013bb3SRafael Espindola if (auto *Cmd = dyn_cast<OutputSectionCommand>(Base)) 4834f013bb3SRafael Espindola return Cmd->Name == Name; 4844f013bb3SRafael Espindola return false; 4854f013bb3SRafael Espindola }); 486de8d9897SRafael Espindola if (I == Opt.Commands.end()) { 4874f013bb3SRafael Espindola Factory.addInputSec(S, Name); 488de8d9897SRafael Espindola } else { 489de8d9897SRafael Espindola auto *Cmd = cast<OutputSectionCommand>(*I); 490de8d9897SRafael Espindola Factory.addInputSec(S, Name, Cmd->Sec); 491de8d9897SRafael Espindola auto *ISD = make<InputSectionDescription>(""); 492de8d9897SRafael Espindola ISD->Sections.push_back(S); 493de8d9897SRafael Espindola Cmd->Commands.push_back(ISD); 494de8d9897SRafael Espindola } 4954f013bb3SRafael Espindola } 496e63d81bdSEugene Leviant } 497e63d81bdSEugene Leviant 498*7c4eafa3SRafael Espindola uint64_t LinkerScript::advance(uint64_t Size, unsigned Align) { 499*7c4eafa3SRafael Espindola bool IsTbss = (CurOutSec->Flags & SHF_TLS) && CurOutSec->Type == SHT_NOBITS; 500*7c4eafa3SRafael Espindola uint64_t Start = IsTbss ? Dot + ThreadBssOffset : Dot; 501*7c4eafa3SRafael Espindola Start = alignTo(Start, Align); 502*7c4eafa3SRafael Espindola uint64_t End = Start + Size; 503*7c4eafa3SRafael Espindola 504*7c4eafa3SRafael Espindola if (IsTbss) 505*7c4eafa3SRafael Espindola ThreadBssOffset = End - Dot; 506*7c4eafa3SRafael Espindola else 507*7c4eafa3SRafael Espindola Dot = End; 508*7c4eafa3SRafael Espindola return End; 509a940e539SRafael Espindola } 510a940e539SRafael Espindola 511b8dd23f5SRui Ueyama void LinkerScript::output(InputSection *S) { 512*7c4eafa3SRafael Espindola uint64_t Pos = advance(S->getSize(), S->Alignment); 513*7c4eafa3SRafael Espindola S->OutSecOff = Pos - S->getSize() - CurOutSec->Addr; 514d3190795SRafael Espindola 515d3190795SRafael Espindola // Update output section size after adding each section. This is so that 516d3190795SRafael Espindola // SIZEOF works correctly in the case below: 517d3190795SRafael Espindola // .foo { *(.aaa) a = SIZEOF(.foo); *(.bbb) } 51804a2e348SRafael Espindola CurOutSec->Size = Pos - CurOutSec->Addr; 519d3190795SRafael Espindola 520b889744eSMeador Inge // If there is a memory region associated with this input section, then 521b889744eSMeador Inge // place the section in that region and update the region index. 522b889744eSMeador Inge if (CurMemRegion) { 523b889744eSMeador Inge CurMemRegion->Offset += CurOutSec->Size; 524b889744eSMeador Inge uint64_t CurSize = CurMemRegion->Offset - CurMemRegion->Origin; 525b889744eSMeador Inge if (CurSize > CurMemRegion->Length) { 526b889744eSMeador Inge uint64_t OverflowAmt = CurSize - CurMemRegion->Length; 527b889744eSMeador Inge error("section '" + CurOutSec->Name + "' will not fit in region '" + 528b889744eSMeador Inge CurMemRegion->Name + "': overflowed by " + Twine(OverflowAmt) + 529b889744eSMeador Inge " bytes"); 530b889744eSMeador Inge } 531b889744eSMeador Inge } 5322de509c3SRui Ueyama } 533ceabe80eSEugene Leviant 534b8dd23f5SRui Ueyama void LinkerScript::switchTo(OutputSection *Sec) { 535d3190795SRafael Espindola if (CurOutSec == Sec) 536d3190795SRafael Espindola return; 537d3190795SRafael Espindola 538d3190795SRafael Espindola CurOutSec = Sec; 539*7c4eafa3SRafael Espindola CurOutSec->Addr = advance(0, CurOutSec->Alignment); 540b71d6f7aSEugene Leviant 541b71d6f7aSEugene Leviant // If neither AT nor AT> is specified for an allocatable section, the linker 542b71d6f7aSEugene Leviant // will set the LMA such that the difference between VMA and LMA for the 543b71d6f7aSEugene Leviant // section is the same as the preceding output section in the same region 544b71d6f7aSEugene Leviant // https://sourceware.org/binutils/docs-2.20/ld/Output-Section-LMA.html 54521467876SGeorge Rimar if (LMAOffset) 54629c1afb8SRafael Espindola CurOutSec->LMAOffset = LMAOffset(); 547d3190795SRafael Espindola } 548d3190795SRafael Espindola 549b8dd23f5SRui Ueyama void LinkerScript::process(BaseCommand &Base) { 5502e081a4fSRui Ueyama // This handles the assignments to symbol or to the dot. 5512e081a4fSRui Ueyama if (auto *Cmd = dyn_cast<SymbolAssignment>(&Base)) { 5522e081a4fSRui Ueyama assignSymbol(Cmd, true); 553d3190795SRafael Espindola return; 55497403d15SEugene Leviant } 555e38cbab5SGeorge Rimar 556e38cbab5SGeorge Rimar // Handle BYTE(), SHORT(), LONG(), or QUAD(). 5572e081a4fSRui Ueyama if (auto *Cmd = dyn_cast<BytesDataCommand>(&Base)) { 5582e081a4fSRui Ueyama Cmd->Offset = Dot - CurOutSec->Addr; 5592e081a4fSRui Ueyama Dot += Cmd->Size; 56004a2e348SRafael Espindola CurOutSec->Size = Dot - CurOutSec->Addr; 561e38cbab5SGeorge Rimar return; 562e38cbab5SGeorge Rimar } 563e38cbab5SGeorge Rimar 5642e081a4fSRui Ueyama // Handle ASSERT(). 5652e081a4fSRui Ueyama if (auto *Cmd = dyn_cast<AssertCommand>(&Base)) { 5662e081a4fSRui Ueyama Cmd->Expression(); 567b2d99d6aSMeador Inge return; 568b2d99d6aSMeador Inge } 569b2d99d6aSMeador Inge 5702e081a4fSRui Ueyama // Handle a single input section description command. 5712e081a4fSRui Ueyama // It calculates and assigns the offsets for each section and also 572e38cbab5SGeorge Rimar // updates the output section size. 5732e081a4fSRui Ueyama auto &Cmd = cast<InputSectionDescription>(Base); 5742e081a4fSRui Ueyama for (InputSectionBase *Sec : Cmd.Sections) { 5753fb5a6dcSGeorge Rimar // We tentatively added all synthetic sections at the beginning and removed 5763fb5a6dcSGeorge Rimar // empty ones afterwards (because there is no way to know whether they were 5773fb5a6dcSGeorge Rimar // going be empty or not other than actually running linker scripts.) 5783fb5a6dcSGeorge Rimar // We need to ignore remains of empty sections. 5792e081a4fSRui Ueyama if (auto *S = dyn_cast<SyntheticSection>(Sec)) 5802e081a4fSRui Ueyama if (S->empty()) 5813fb5a6dcSGeorge Rimar continue; 5823fb5a6dcSGeorge Rimar 5832e081a4fSRui Ueyama if (!Sec->Live) 58478ef645fSGeorge Rimar continue; 585de8d9897SRafael Espindola assert(CurOutSec == Sec->OutSec); 5862e081a4fSRui Ueyama output(cast<InputSection>(Sec)); 587ceabe80eSEugene Leviant } 588ceabe80eSEugene Leviant } 589ceabe80eSEugene Leviant 590b889744eSMeador Inge // This function searches for a memory region to place the given output 591b889744eSMeador Inge // section in. If found, a pointer to the appropriate memory region is 592b889744eSMeador Inge // returned. Otherwise, a nullptr is returned. 5931902b337SRafael Espindola MemoryRegion *LinkerScript::findMemoryRegion(OutputSectionCommand *Cmd) { 594b889744eSMeador Inge // If a memory region name was specified in the output section command, 595b889744eSMeador Inge // then try to find that region first. 596b889744eSMeador Inge if (!Cmd->MemoryRegionName.empty()) { 597b889744eSMeador Inge auto It = Opt.MemoryRegions.find(Cmd->MemoryRegionName); 598b889744eSMeador Inge if (It != Opt.MemoryRegions.end()) 599b889744eSMeador Inge return &It->second; 600b889744eSMeador Inge error("memory region '" + Cmd->MemoryRegionName + "' not declared"); 601b889744eSMeador Inge return nullptr; 602b889744eSMeador Inge } 603b889744eSMeador Inge 604d7c5400fSRui Ueyama // If at least one memory region is defined, all sections must 605d7c5400fSRui Ueyama // belong to some memory region. Otherwise, we don't need to do 606d7c5400fSRui Ueyama // anything for memory regions. 607cc400cc8SRui Ueyama if (Opt.MemoryRegions.empty()) 608b889744eSMeador Inge return nullptr; 609b889744eSMeador Inge 6101902b337SRafael Espindola OutputSection *Sec = Cmd->Sec; 611b889744eSMeador Inge // See if a region can be found by matching section flags. 6122e081a4fSRui Ueyama for (auto &Pair : Opt.MemoryRegions) { 6132e081a4fSRui Ueyama MemoryRegion &M = Pair.second; 6142e081a4fSRui Ueyama if ((M.Flags & Sec->Flags) && (M.NegFlags & Sec->Flags) == 0) 6152e081a4fSRui Ueyama return &M; 616b889744eSMeador Inge } 617b889744eSMeador Inge 618b889744eSMeador Inge // Otherwise, no suitable region was found. 619b889744eSMeador Inge if (Sec->Flags & SHF_ALLOC) 620b889744eSMeador Inge error("no memory region specified for section '" + Sec->Name + "'"); 621b889744eSMeador Inge return nullptr; 622b889744eSMeador Inge } 623b889744eSMeador Inge 6240b1b695aSRui Ueyama // This function assigns offsets to input sections and an output section 6250b1b695aSRui Ueyama // for a single sections command (e.g. ".text { *(.text); }"). 626b8dd23f5SRui Ueyama void LinkerScript::assignOffsets(OutputSectionCommand *Cmd) { 6279b980095SRafael Espindola OutputSection *Sec = Cmd->Sec; 6282b074553SRafael Espindola if (!Sec) 629d3190795SRafael Espindola return; 630b889744eSMeador Inge 631cba41013SRui Ueyama if (Cmd->AddrExpr && (Sec->Flags & SHF_ALLOC)) 632d379f735SRui Ueyama setDot(Cmd->AddrExpr, Cmd->Location, false); 633679828ffSRafael Espindola 6345784e96fSEugene Leviant if (Cmd->LMAExpr) { 6350c1c8085SGeorge Rimar uint64_t D = Dot; 63672dc195dSRafael Espindola LMAOffset = [=] { return Cmd->LMAExpr().getValue() - D; }; 6375784e96fSEugene Leviant } 6385784e96fSEugene Leviant 639feed7506SRafael Espindola CurMemRegion = Cmd->MemRegion; 640b889744eSMeador Inge if (CurMemRegion) 641b889744eSMeador Inge Dot = CurMemRegion->Offset; 642b889744eSMeador Inge switchTo(Sec); 6430b1b695aSRui Ueyama 644de8d9897SRafael Espindola for (BaseCommand *C : Cmd->Commands) 645de8d9897SRafael Espindola process(*C); 646d3190795SRafael Espindola } 647d3190795SRafael Espindola 648b8dd23f5SRui Ueyama void LinkerScript::removeEmptyCommands() { 6496d38e4dbSRafael Espindola // It is common practice to use very generic linker scripts. So for any 6506d38e4dbSRafael Espindola // given run some of the output sections in the script will be empty. 6516d38e4dbSRafael Espindola // We could create corresponding empty output sections, but that would 6526d38e4dbSRafael Espindola // clutter the output. 6536d38e4dbSRafael Espindola // We instead remove trivially empty sections. The bfd linker seems even 6546d38e4dbSRafael Espindola // more aggressive at removing them. 6556d38e4dbSRafael Espindola auto Pos = std::remove_if( 6568f99f73cSRui Ueyama Opt.Commands.begin(), Opt.Commands.end(), [&](BaseCommand *Base) { 6578f99f73cSRui Ueyama if (auto *Cmd = dyn_cast<OutputSectionCommand>(Base)) 6584f013bb3SRafael Espindola return std::find(OutputSections->begin(), OutputSections->end(), 6594f013bb3SRafael Espindola Cmd->Sec) == OutputSections->end(); 6600b1b695aSRui Ueyama return false; 6616d38e4dbSRafael Espindola }); 6626d38e4dbSRafael Espindola Opt.Commands.erase(Pos, Opt.Commands.end()); 66307fe6129SRafael Espindola } 66407fe6129SRafael Espindola 6656a53737cSRafael Espindola static bool isAllSectionDescription(const OutputSectionCommand &Cmd) { 6668f99f73cSRui Ueyama for (BaseCommand *Base : Cmd.Commands) 6678f99f73cSRui Ueyama if (!isa<InputSectionDescription>(*Base)) 6686a53737cSRafael Espindola return false; 6696a53737cSRafael Espindola return true; 6706a53737cSRafael Espindola } 6716d38e4dbSRafael Espindola 672b8dd23f5SRui Ueyama void LinkerScript::adjustSectionsBeforeSorting() { 6739546fffbSRafael Espindola // If the output section contains only symbol assignments, create a 6749546fffbSRafael Espindola // corresponding output section. The bfd linker seems to only create them if 6759546fffbSRafael Espindola // '.' is assigned to, but creating these section should not have any bad 6769546fffbSRafael Espindola // consequeces and gives us a section to put the symbol in. 6770c1c8085SGeorge Rimar uint64_t Flags = SHF_ALLOC; 6781022112dSGeorge Rimar uint32_t Type = SHT_PROGBITS; 6798f99f73cSRui Ueyama for (BaseCommand *Base : Opt.Commands) { 6808f99f73cSRui Ueyama auto *Cmd = dyn_cast<OutputSectionCommand>(Base); 6819546fffbSRafael Espindola if (!Cmd) 6829546fffbSRafael Espindola continue; 6834f013bb3SRafael Espindola if (OutputSection *Sec = Cmd->Sec) { 6842b074553SRafael Espindola Flags = Sec->Flags; 6852b074553SRafael Espindola Type = Sec->Type; 6869546fffbSRafael Espindola continue; 6879546fffbSRafael Espindola } 6889546fffbSRafael Espindola 6896a53737cSRafael Espindola if (isAllSectionDescription(*Cmd)) 6906a53737cSRafael Espindola continue; 6916a53737cSRafael Espindola 69224e6f363SRafael Espindola auto *OutSec = make<OutputSection>(Cmd->Name, Type, Flags); 6939546fffbSRafael Espindola OutputSections->push_back(OutSec); 6949b980095SRafael Espindola Cmd->Sec = OutSec; 6959546fffbSRafael Espindola } 696f7a17448SRafael Espindola } 697f7a17448SRafael Espindola 698b8dd23f5SRui Ueyama void LinkerScript::adjustSectionsAfterSorting() { 699f7a17448SRafael Espindola placeOrphanSections(); 700f7a17448SRafael Espindola 701feed7506SRafael Espindola // Try and find an appropriate memory region to assign offsets in. 702d1960dc0SRafael Espindola for (BaseCommand *Base : Opt.Commands) { 703d1960dc0SRafael Espindola if (auto *Cmd = dyn_cast<OutputSectionCommand>(Base)) { 704feed7506SRafael Espindola Cmd->MemRegion = findMemoryRegion(Cmd); 705d1960dc0SRafael Espindola // Handle align (e.g. ".foo : ALIGN(16) { ... }"). 706d1960dc0SRafael Espindola if (Cmd->AlignExpr) 707d1960dc0SRafael Espindola Cmd->Sec->updateAlignment(Cmd->AlignExpr().getValue()); 708d1960dc0SRafael Espindola } 709d1960dc0SRafael Espindola } 710feed7506SRafael Espindola 711f7a17448SRafael Espindola // If output section command doesn't specify any segments, 712f7a17448SRafael Espindola // and we haven't previously assigned any section to segment, 713f7a17448SRafael Espindola // then we simply assign section to the very first load segment. 714f7a17448SRafael Espindola // Below is an example of such linker script: 715f7a17448SRafael Espindola // PHDRS { seg PT_LOAD; } 716f7a17448SRafael Espindola // SECTIONS { .aaa : { *(.aaa) } } 717f7a17448SRafael Espindola std::vector<StringRef> DefPhdrs; 718f7a17448SRafael Espindola auto FirstPtLoad = 719f7a17448SRafael Espindola std::find_if(Opt.PhdrsCommands.begin(), Opt.PhdrsCommands.end(), 720f7a17448SRafael Espindola [](const PhdrsCommand &Cmd) { return Cmd.Type == PT_LOAD; }); 721f7a17448SRafael Espindola if (FirstPtLoad != Opt.PhdrsCommands.end()) 722f7a17448SRafael Espindola DefPhdrs.push_back(FirstPtLoad->Name); 723f7a17448SRafael Espindola 724f7a17448SRafael Espindola // Walk the commands and propagate the program headers to commands that don't 725f7a17448SRafael Espindola // explicitly specify them. 7268f99f73cSRui Ueyama for (BaseCommand *Base : Opt.Commands) { 7278f99f73cSRui Ueyama auto *Cmd = dyn_cast<OutputSectionCommand>(Base); 728f7a17448SRafael Espindola if (!Cmd) 729f7a17448SRafael Espindola continue; 7308f99f73cSRui Ueyama 731f7a17448SRafael Espindola if (Cmd->Phdrs.empty()) 732f7a17448SRafael Espindola Cmd->Phdrs = DefPhdrs; 733f7a17448SRafael Espindola else 734f7a17448SRafael Espindola DefPhdrs = Cmd->Phdrs; 735f7a17448SRafael Espindola } 7366a53737cSRafael Espindola 7376a53737cSRafael Espindola removeEmptyCommands(); 7389546fffbSRafael Espindola } 7399546fffbSRafael Espindola 74015c57951SRafael Espindola // When placing orphan sections, we want to place them after symbol assignments 74115c57951SRafael Espindola // so that an orphan after 74215c57951SRafael Espindola // begin_foo = .; 74315c57951SRafael Espindola // foo : { *(foo) } 74415c57951SRafael Espindola // end_foo = .; 74515c57951SRafael Espindola // doesn't break the intended meaning of the begin/end symbols. 74615c57951SRafael Espindola // We don't want to go over sections since Writer<ELFT>::sortSections is the 74715c57951SRafael Espindola // one in charge of deciding the order of the sections. 74815c57951SRafael Espindola // We don't want to go over alignments, since doing so in 74915c57951SRafael Espindola // rx_sec : { *(rx_sec) } 75015c57951SRafael Espindola // . = ALIGN(0x1000); 75115c57951SRafael Espindola // /* The RW PT_LOAD starts here*/ 75215c57951SRafael Espindola // rw_sec : { *(rw_sec) } 75315c57951SRafael Espindola // would mean that the RW PT_LOAD would become unaligned. 7544e1e88e3SRui Ueyama static bool shouldSkip(BaseCommand *Cmd) { 75515c57951SRafael Espindola if (isa<OutputSectionCommand>(Cmd)) 75615c57951SRafael Espindola return false; 7574e1e88e3SRui Ueyama if (auto *Assign = dyn_cast<SymbolAssignment>(Cmd)) 7585fcc99c2SRafael Espindola return Assign->Name != "."; 7594e1e88e3SRui Ueyama return true; 76015c57951SRafael Espindola } 76115c57951SRafael Espindola 7626697ec29SRui Ueyama // Orphan sections are sections present in the input files which are 7636697ec29SRui Ueyama // not explicitly placed into the output file by the linker script. 7646697ec29SRui Ueyama // 7656697ec29SRui Ueyama // When the control reaches this function, Opt.Commands contains 7666697ec29SRui Ueyama // output section commands for non-orphan sections only. This function 76781cb7107SRui Ueyama // adds new elements for orphan sections so that all sections are 76881cb7107SRui Ueyama // explicitly handled by Opt.Commands. 7696697ec29SRui Ueyama // 7706697ec29SRui Ueyama // Writer<ELFT>::sortSections has already sorted output sections. 7716697ec29SRui Ueyama // What we need to do is to scan OutputSections vector and 7726697ec29SRui Ueyama // Opt.Commands in parallel to find orphan sections. If there is an 7736697ec29SRui Ueyama // output section that doesn't have a corresponding entry in 7746697ec29SRui Ueyama // Opt.Commands, we will insert a new entry to Opt.Commands. 7756697ec29SRui Ueyama // 7766697ec29SRui Ueyama // There is some ambiguity as to where exactly a new entry should be 7776697ec29SRui Ueyama // inserted, because Opt.Commands contains not only output section 77881cb7107SRui Ueyama // commands but also other types of commands such as symbol assignment 7796697ec29SRui Ueyama // expressions. There's no correct answer here due to the lack of the 7806697ec29SRui Ueyama // formal specification of the linker script. We use heuristics to 7816697ec29SRui Ueyama // determine whether a new output command should be added before or 7826697ec29SRui Ueyama // after another commands. For the details, look at shouldSkip 7836697ec29SRui Ueyama // function. 784b8dd23f5SRui Ueyama void LinkerScript::placeOrphanSections() { 785aab6d5c5SRafael Espindola // The OutputSections are already in the correct order. 786aab6d5c5SRafael Espindola // This loops creates or moves commands as needed so that they are in the 787aab6d5c5SRafael Espindola // correct order. 788aab6d5c5SRafael Espindola int CmdIndex = 0; 7895fcc99c2SRafael Espindola 7905fcc99c2SRafael Espindola // As a horrible special case, skip the first . assignment if it is before any 7915fcc99c2SRafael Espindola // section. We do this because it is common to set a load address by starting 7925fcc99c2SRafael Espindola // the script with ". = 0xabcd" and the expectation is that every section is 7935fcc99c2SRafael Espindola // after that. 7944e1e88e3SRui Ueyama auto FirstSectionOrDotAssignment = 7954e1e88e3SRui Ueyama std::find_if(Opt.Commands.begin(), Opt.Commands.end(), 7964e1e88e3SRui Ueyama [](BaseCommand *Cmd) { return !shouldSkip(Cmd); }); 7975fcc99c2SRafael Espindola if (FirstSectionOrDotAssignment != Opt.Commands.end()) { 7985fcc99c2SRafael Espindola CmdIndex = FirstSectionOrDotAssignment - Opt.Commands.begin(); 7995fcc99c2SRafael Espindola if (isa<SymbolAssignment>(**FirstSectionOrDotAssignment)) 8005fcc99c2SRafael Espindola ++CmdIndex; 8015fcc99c2SRafael Espindola } 8025fcc99c2SRafael Espindola 80324e6f363SRafael Espindola for (OutputSection *Sec : *OutputSections) { 80440849419SRafael Espindola StringRef Name = Sec->Name; 805aab6d5c5SRafael Espindola 806aab6d5c5SRafael Espindola // Find the last spot where we can insert a command and still get the 80715c57951SRafael Espindola // correct result. 808aab6d5c5SRafael Espindola auto CmdIter = Opt.Commands.begin() + CmdIndex; 809aab6d5c5SRafael Espindola auto E = Opt.Commands.end(); 8104e1e88e3SRui Ueyama while (CmdIter != E && shouldSkip(*CmdIter)) { 811aab6d5c5SRafael Espindola ++CmdIter; 812aab6d5c5SRafael Espindola ++CmdIndex; 813aab6d5c5SRafael Espindola } 814aab6d5c5SRafael Espindola 815de8d9897SRafael Espindola // If there is no command corresponding to this output section, 816de8d9897SRafael Espindola // create one and put a InputSectionDescription in it so that both 817de8d9897SRafael Espindola // representations agree on which input sections to use. 8188f99f73cSRui Ueyama auto Pos = std::find_if(CmdIter, E, [&](BaseCommand *Base) { 8198f99f73cSRui Ueyama auto *Cmd = dyn_cast<OutputSectionCommand>(Base); 820aab6d5c5SRafael Espindola return Cmd && Cmd->Name == Name; 821aab6d5c5SRafael Espindola }); 822aab6d5c5SRafael Espindola if (Pos == E) { 8239b980095SRafael Espindola auto *Cmd = make<OutputSectionCommand>(Name); 8249b980095SRafael Espindola Opt.Commands.insert(CmdIter, Cmd); 825aab6d5c5SRafael Espindola ++CmdIndex; 826de8d9897SRafael Espindola 827de8d9897SRafael Espindola Cmd->Sec = Sec; 828de8d9897SRafael Espindola auto *ISD = make<InputSectionDescription>(""); 829de8d9897SRafael Espindola for (InputSection *IS : Sec->Sections) 830de8d9897SRafael Espindola ISD->Sections.push_back(IS); 831de8d9897SRafael Espindola Cmd->Commands.push_back(ISD); 832de8d9897SRafael Espindola 83315c57951SRafael Espindola continue; 83415c57951SRafael Espindola } 83515c57951SRafael Espindola 83615c57951SRafael Espindola // Continue from where we found it. 83715c57951SRafael Espindola CmdIndex = (Pos - Opt.Commands.begin()) + 1; 838652852c5SGeorge Rimar } 839337f903cSRafael Espindola } 840337f903cSRafael Espindola 841b8dd23f5SRui Ueyama void LinkerScript::processNonSectionCommands() { 8428f99f73cSRui Ueyama for (BaseCommand *Base : Opt.Commands) { 8438f99f73cSRui Ueyama if (auto *Cmd = dyn_cast<SymbolAssignment>(Base)) 844d379f735SRui Ueyama assignSymbol(Cmd, false); 8458f99f73cSRui Ueyama else if (auto *Cmd = dyn_cast<AssertCommand>(Base)) 84602ad516bSPetr Hosek Cmd->Expression(); 84702ad516bSPetr Hosek } 84802ad516bSPetr Hosek } 84902ad516bSPetr Hosek 850de8d9897SRafael Espindola // Do a last effort at synchronizing the linker script "AST" and the section 851de8d9897SRafael Espindola // list. This is needed to account for last minute changes, like adding a 852de8d9897SRafael Espindola // .ARM.exidx terminator and sorting SHF_LINK_ORDER sections. 853de8d9897SRafael Espindola // 854de8d9897SRafael Espindola // FIXME: We should instead create the "AST" earlier and the above changes would 855de8d9897SRafael Espindola // be done directly in the "AST". 856de8d9897SRafael Espindola // 857de8d9897SRafael Espindola // This can only handle new sections being added and sections being reordered. 858de8d9897SRafael Espindola void LinkerScript::synchronize() { 859de8d9897SRafael Espindola for (BaseCommand *Base : Opt.Commands) { 860de8d9897SRafael Espindola auto *Cmd = dyn_cast<OutputSectionCommand>(Base); 861de8d9897SRafael Espindola if (!Cmd) 862de8d9897SRafael Espindola continue; 863de8d9897SRafael Espindola ArrayRef<InputSection *> Sections = Cmd->Sec->Sections; 864de8d9897SRafael Espindola std::vector<InputSectionBase **> ScriptSections; 865de8d9897SRafael Espindola DenseSet<InputSectionBase *> ScriptSectionsSet; 866de8d9897SRafael Espindola for (BaseCommand *Base : Cmd->Commands) { 867de8d9897SRafael Espindola auto *ISD = dyn_cast<InputSectionDescription>(Base); 868de8d9897SRafael Espindola if (!ISD) 869de8d9897SRafael Espindola continue; 870de8d9897SRafael Espindola for (InputSectionBase *&IS : ISD->Sections) { 871de8d9897SRafael Espindola if (IS->Live) { 872de8d9897SRafael Espindola ScriptSections.push_back(&IS); 873de8d9897SRafael Espindola ScriptSectionsSet.insert(IS); 874de8d9897SRafael Espindola } 875de8d9897SRafael Espindola } 876de8d9897SRafael Espindola } 877de8d9897SRafael Espindola std::vector<InputSectionBase *> Missing; 878de8d9897SRafael Espindola for (InputSection *IS : Sections) 879de8d9897SRafael Espindola if (!ScriptSectionsSet.count(IS)) 880de8d9897SRafael Espindola Missing.push_back(IS); 881de8d9897SRafael Espindola if (!Missing.empty()) { 882de8d9897SRafael Espindola auto ISD = make<InputSectionDescription>(""); 883de8d9897SRafael Espindola ISD->Sections = Missing; 884de8d9897SRafael Espindola Cmd->Commands.push_back(ISD); 885de8d9897SRafael Espindola for (InputSectionBase *&IS : ISD->Sections) 886de8d9897SRafael Espindola if (IS->Live) 887de8d9897SRafael Espindola ScriptSections.push_back(&IS); 888de8d9897SRafael Espindola } 889de8d9897SRafael Espindola assert(ScriptSections.size() == Sections.size()); 890de8d9897SRafael Espindola for (int I = 0, N = Sections.size(); I < N; ++I) 891de8d9897SRafael Espindola *ScriptSections[I] = Sections[I]; 892de8d9897SRafael Espindola } 893de8d9897SRafael Espindola } 894de8d9897SRafael Espindola 895b8dd23f5SRui Ueyama void LinkerScript::assignAddresses(std::vector<PhdrEntry> &Phdrs) { 8967c18c28cSRui Ueyama // Assign addresses as instructed by linker script SECTIONS sub-commands. 897be607334SRafael Espindola Dot = 0; 89872dc195dSRafael Espindola ErrorOnMissingSection = true; 89906f4743aSRafael Espindola switchTo(Aether); 90006f4743aSRafael Espindola 9018f99f73cSRui Ueyama for (BaseCommand *Base : Opt.Commands) { 9028f99f73cSRui Ueyama if (auto *Cmd = dyn_cast<SymbolAssignment>(Base)) { 903d379f735SRui Ueyama assignSymbol(Cmd, false); 90405ef4cffSRui Ueyama continue; 905652852c5SGeorge Rimar } 906652852c5SGeorge Rimar 9078f99f73cSRui Ueyama if (auto *Cmd = dyn_cast<AssertCommand>(Base)) { 9084595df94SRafael Espindola Cmd->Expression(); 909eefa758eSGeorge Rimar continue; 910eefa758eSGeorge Rimar } 911eefa758eSGeorge Rimar 9128f99f73cSRui Ueyama auto *Cmd = cast<OutputSectionCommand>(Base); 913d3190795SRafael Espindola assignOffsets(Cmd); 914a14b13d8SGeorge Rimar } 915467c4d55SEugene Leviant 9160c1c8085SGeorge Rimar uint64_t MinVA = std::numeric_limits<uint64_t>::max(); 91724e6f363SRafael Espindola for (OutputSection *Sec : *OutputSections) { 91804a2e348SRafael Espindola if (Sec->Flags & SHF_ALLOC) 919e08e78dfSRafael Espindola MinVA = std::min<uint64_t>(MinVA, Sec->Addr); 920ea590d91SRafael Espindola else 921ea590d91SRafael Espindola Sec->Addr = 0; 922ea590d91SRafael Espindola } 923aab6d5c5SRafael Espindola 9242d262109SGeorge Rimar allocateHeaders(Phdrs, *OutputSections, MinVA); 925fb8978fcSDima Stepanov } 926652852c5SGeorge Rimar 927464daadcSRui Ueyama // Creates program headers as instructed by PHDRS linker script command. 928b8dd23f5SRui Ueyama std::vector<PhdrEntry> LinkerScript::createPhdrs() { 92917cb7c0aSRafael Espindola std::vector<PhdrEntry> Ret; 930bbe38602SEugene Leviant 931464daadcSRui Ueyama // Process PHDRS and FILEHDR keywords because they are not 932464daadcSRui Ueyama // real output sections and cannot be added in the following loop. 933bbe38602SEugene Leviant for (const PhdrsCommand &Cmd : Opt.PhdrsCommands) { 934edebbdf1SRui Ueyama Ret.emplace_back(Cmd.Type, Cmd.Flags == UINT_MAX ? PF_R : Cmd.Flags); 93517cb7c0aSRafael Espindola PhdrEntry &Phdr = Ret.back(); 936bbe38602SEugene Leviant 937bbe38602SEugene Leviant if (Cmd.HasFilehdr) 9389d1bacb1SRui Ueyama Phdr.add(Out::ElfHeader); 939bbe38602SEugene Leviant if (Cmd.HasPhdrs) 9409d1bacb1SRui Ueyama Phdr.add(Out::ProgramHeaders); 94156b21c86SEugene Leviant 94256b21c86SEugene Leviant if (Cmd.LMAExpr) { 94372dc195dSRafael Espindola Phdr.p_paddr = Cmd.LMAExpr().getValue(); 94456b21c86SEugene Leviant Phdr.HasLMA = true; 94556b21c86SEugene Leviant } 946bbe38602SEugene Leviant } 947bbe38602SEugene Leviant 948464daadcSRui Ueyama // Add output sections to program headers. 94924e6f363SRafael Espindola for (OutputSection *Sec : *OutputSections) { 95004a2e348SRafael Espindola if (!(Sec->Flags & SHF_ALLOC)) 951bbe38602SEugene Leviant break; 952bbe38602SEugene Leviant 953bbe38602SEugene Leviant // Assign headers specified by linker script 95440849419SRafael Espindola for (size_t Id : getPhdrIndices(Sec->Name)) { 955edebbdf1SRui Ueyama Ret[Id].add(Sec); 956865bf863SEugene Leviant if (Opt.PhdrsCommands[Id].Flags == UINT_MAX) 95717cb7c0aSRafael Espindola Ret[Id].p_flags |= Sec->getPhdrFlags(); 958bbe38602SEugene Leviant } 959bbe38602SEugene Leviant } 960edebbdf1SRui Ueyama return Ret; 961bbe38602SEugene Leviant } 962bbe38602SEugene Leviant 963b8dd23f5SRui Ueyama bool LinkerScript::ignoreInterpSection() { 964f9bc3bd2SEugene Leviant // Ignore .interp section in case we have PHDRS specification 965f9bc3bd2SEugene Leviant // and PT_INTERP isn't listed. 966e31d9886SRui Ueyama if (Opt.PhdrsCommands.empty()) 967e31d9886SRui Ueyama return false; 968e31d9886SRui Ueyama for (PhdrsCommand &Cmd : Opt.PhdrsCommands) 969e31d9886SRui Ueyama if (Cmd.Type == PT_INTERP) 970e31d9886SRui Ueyama return false; 971e31d9886SRui Ueyama return true; 972f9bc3bd2SEugene Leviant } 973f9bc3bd2SEugene Leviant 9749d9a6637SJames Henderson Optional<uint32_t> LinkerScript::getFiller(StringRef Name) { 9758f99f73cSRui Ueyama for (BaseCommand *Base : Opt.Commands) 9768f99f73cSRui Ueyama if (auto *Cmd = dyn_cast<OutputSectionCommand>(Base)) 977f6c3ccefSGeorge Rimar if (Cmd->Name == Name) 978f6c3ccefSGeorge Rimar return Cmd->Filler; 9799d9a6637SJames Henderson return None; 980e2ee72b5SGeorge Rimar } 981e2ee72b5SGeorge Rimar 982e38cbab5SGeorge Rimar static void writeInt(uint8_t *Buf, uint64_t Data, uint64_t Size) { 983f62d2607SRui Ueyama if (Size == 1) 984f62d2607SRui Ueyama *Buf = Data; 985f62d2607SRui Ueyama else if (Size == 2) 986f93ed4deSRui Ueyama write16(Buf, Data, Config->Endianness); 987f62d2607SRui Ueyama else if (Size == 4) 988f93ed4deSRui Ueyama write32(Buf, Data, Config->Endianness); 989f62d2607SRui Ueyama else if (Size == 8) 990f93ed4deSRui Ueyama write64(Buf, Data, Config->Endianness); 991f62d2607SRui Ueyama else 992e38cbab5SGeorge Rimar llvm_unreachable("unsupported Size argument"); 993e38cbab5SGeorge Rimar } 994e38cbab5SGeorge Rimar 995b8dd23f5SRui Ueyama void LinkerScript::writeDataBytes(StringRef Name, uint8_t *Buf) { 996e38cbab5SGeorge Rimar int I = getSectionIndex(Name); 997e38cbab5SGeorge Rimar if (I == INT_MAX) 998e38cbab5SGeorge Rimar return; 999e38cbab5SGeorge Rimar 10008f99f73cSRui Ueyama auto *Cmd = dyn_cast<OutputSectionCommand>(Opt.Commands[I]); 10018f99f73cSRui Ueyama for (BaseCommand *Base : Cmd->Commands) 10028f99f73cSRui Ueyama if (auto *Data = dyn_cast<BytesDataCommand>(Base)) 1003a8dba487SGeorge Rimar writeInt(Buf + Data->Offset, Data->Expression().getValue(), Data->Size); 1004e38cbab5SGeorge Rimar } 1005e38cbab5SGeorge Rimar 1006b8dd23f5SRui Ueyama bool LinkerScript::hasLMA(StringRef Name) { 10078f99f73cSRui Ueyama for (BaseCommand *Base : Opt.Commands) 10088f99f73cSRui Ueyama if (auto *Cmd = dyn_cast<OutputSectionCommand>(Base)) 1009b71d6f7aSEugene Leviant if (Cmd->LMAExpr && Cmd->Name == Name) 1010b71d6f7aSEugene Leviant return true; 1011b71d6f7aSEugene Leviant return false; 10128ceadb38SGeorge Rimar } 10138ceadb38SGeorge Rimar 1014c3e2a4b0SRui Ueyama // Returns the index of the given section name in linker script 1015c3e2a4b0SRui Ueyama // SECTIONS commands. Sections are laid out as the same order as they 1016c3e2a4b0SRui Ueyama // were in the script. If a given name did not appear in the script, 1017c3e2a4b0SRui Ueyama // it returns INT_MAX, so that it will be laid out at end of file. 1018b8dd23f5SRui Ueyama int LinkerScript::getSectionIndex(StringRef Name) { 10196e68c5e5SRui Ueyama for (int I = 0, E = Opt.Commands.size(); I != E; ++I) 10208f99f73cSRui Ueyama if (auto *Cmd = dyn_cast<OutputSectionCommand>(Opt.Commands[I])) 1021076fe157SGeorge Rimar if (Cmd->Name == Name) 1022f510fa6bSRui Ueyama return I; 1023f510fa6bSRui Ueyama return INT_MAX; 102471b26e94SGeorge Rimar } 102571b26e94SGeorge Rimar 1026b8dd23f5SRui Ueyama ExprValue LinkerScript::getSymbolValue(const Twine &Loc, StringRef S) { 10274595df94SRafael Espindola if (S == ".") 102872dc195dSRafael Espindola return {CurOutSec, Dot - CurOutSec->Addr}; 1029a8dba487SGeorge Rimar if (SymbolBody *B = findSymbol(S)) { 103072dc195dSRafael Espindola if (auto *D = dyn_cast<DefinedRegular>(B)) 103172dc195dSRafael Espindola return {D->Section, D->Value}; 103230f16b23SPetr Hosek if (auto *C = dyn_cast<DefinedCommon>(B)) 1033a8dba487SGeorge Rimar return {InX::Common, C->Offset}; 103472dc195dSRafael Espindola } 1035f6aeed36SEugene Leviant error(Loc + ": symbol not found: " + S); 1036884e786dSGeorge Rimar return 0; 1037884e786dSGeorge Rimar } 1038884e786dSGeorge Rimar 1039b8dd23f5SRui Ueyama bool LinkerScript::isDefined(StringRef S) { return findSymbol(S) != nullptr; } 1040f34f45fdSGeorge Rimar 1041bbe38602SEugene Leviant // Returns indices of ELF headers containing specific section, identified 1042bbe38602SEugene Leviant // by Name. Each index is a zero based number of ELF header listed within 1043bbe38602SEugene Leviant // PHDRS {} script block. 1044b8dd23f5SRui Ueyama std::vector<size_t> LinkerScript::getPhdrIndices(StringRef SectionName) { 10458f99f73cSRui Ueyama for (BaseCommand *Base : Opt.Commands) { 10468f99f73cSRui Ueyama auto *Cmd = dyn_cast<OutputSectionCommand>(Base); 1047edebbdf1SRui Ueyama if (!Cmd || Cmd->Name != SectionName) 104831d842f5SGeorge Rimar continue; 104931d842f5SGeorge Rimar 105029c5a2a9SRui Ueyama std::vector<size_t> Ret; 105129c5a2a9SRui Ueyama for (StringRef PhdrName : Cmd->Phdrs) 10522a942c4bSEugene Leviant Ret.push_back(getPhdrIndex(Cmd->Location, PhdrName)); 105329c5a2a9SRui Ueyama return Ret; 1054bbe38602SEugene Leviant } 105531d842f5SGeorge Rimar return {}; 105631d842f5SGeorge Rimar } 1057bbe38602SEugene Leviant 1058b8dd23f5SRui Ueyama size_t LinkerScript::getPhdrIndex(const Twine &Loc, StringRef PhdrName) { 105929c5a2a9SRui Ueyama size_t I = 0; 106029c5a2a9SRui Ueyama for (PhdrsCommand &Cmd : Opt.PhdrsCommands) { 106129c5a2a9SRui Ueyama if (Cmd.Name == PhdrName) 106229c5a2a9SRui Ueyama return I; 106329c5a2a9SRui Ueyama ++I; 106429c5a2a9SRui Ueyama } 10652a942c4bSEugene Leviant error(Loc + ": section header '" + PhdrName + "' is not listed in PHDRS"); 106629c5a2a9SRui Ueyama return 0; 106729c5a2a9SRui Ueyama } 1068