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" 16f7c5fbb1SRui Ueyama #include "Driver.h" 171ebc8ed7SRui Ueyama #include "InputSection.h" 189381eb10SRui Ueyama #include "Memory.h" 19652852c5SGeorge Rimar #include "OutputSections.h" 20794366a2SRui Ueyama #include "ScriptLexer.h" 2193c9af42SRui Ueyama #include "Strings.h" 22f7c5fbb1SRui Ueyama #include "SymbolTable.h" 2355518e7dSRui Ueyama #include "Symbols.h" 243fb5a6dcSGeorge Rimar #include "SyntheticSections.h" 25467c4d55SEugene Leviant #include "Target.h" 26bbe38602SEugene Leviant #include "Writer.h" 2722886a28SEugene Zelenko #include "llvm/ADT/STLExtras.h" 288c6a5aafSRui Ueyama #include "llvm/ADT/SmallString.h" 2922886a28SEugene Zelenko #include "llvm/ADT/StringRef.h" 30960504b9SRui Ueyama #include "llvm/ADT/StringSwitch.h" 3122886a28SEugene Zelenko #include "llvm/Support/Casting.h" 32652852c5SGeorge Rimar #include "llvm/Support/ELF.h" 3322886a28SEugene Zelenko #include "llvm/Support/Endian.h" 3422886a28SEugene Zelenko #include "llvm/Support/ErrorHandling.h" 35f7c5fbb1SRui Ueyama #include "llvm/Support/FileSystem.h" 3622886a28SEugene Zelenko #include "llvm/Support/MathExtras.h" 37f03f3cc1SRui Ueyama #include "llvm/Support/Path.h" 3822886a28SEugene Zelenko #include <algorithm> 3922886a28SEugene Zelenko #include <cassert> 4022886a28SEugene Zelenko #include <cstddef> 4122886a28SEugene Zelenko #include <cstdint> 4222886a28SEugene Zelenko #include <iterator> 4322886a28SEugene Zelenko #include <limits> 4422886a28SEugene Zelenko #include <memory> 4522886a28SEugene Zelenko #include <string> 4622886a28SEugene Zelenko #include <tuple> 4722886a28SEugene Zelenko #include <vector> 48f7c5fbb1SRui Ueyama 49f7c5fbb1SRui Ueyama using namespace llvm; 50652852c5SGeorge Rimar using namespace llvm::ELF; 511ebc8ed7SRui Ueyama using namespace llvm::object; 52e38cbab5SGeorge Rimar using namespace llvm::support::endian; 53f7c5fbb1SRui Ueyama using namespace lld; 54e0df00b9SRafael Espindola using namespace lld::elf; 55f7c5fbb1SRui Ueyama 56884e786dSGeorge Rimar LinkerScriptBase *elf::ScriptBase; 5707320e40SRui Ueyama ScriptConfiguration *elf::ScriptConfig; 58717677afSRui Ueyama 598f1f3c40SMeador Inge template <class ELFT> static SymbolBody *addRegular(SymbolAssignment *Cmd) { 603dabfc6bSRafael Espindola uint8_t Visibility = Cmd->Hidden ? STV_HIDDEN : STV_DEFAULT; 618f1f3c40SMeador Inge Symbol *Sym = Symtab<ELFT>::X->addUndefined( 628f1f3c40SMeador Inge Cmd->Name, /*IsLocal=*/false, STB_GLOBAL, Visibility, 638f1f3c40SMeador Inge /*Type*/ 0, 648f1f3c40SMeador Inge /*CanOmitFromDynSym*/ false, /*File*/ nullptr); 6520d03194SEugene Leviant 668f1f3c40SMeador Inge replaceBody<DefinedRegular<ELFT>>(Sym, Cmd->Name, /*IsLocal=*/false, 678f1f3c40SMeador Inge Visibility, STT_NOTYPE, 0, 0, nullptr, 688f1f3c40SMeador Inge nullptr); 698f1f3c40SMeador Inge return Sym->body(); 70ceabe80eSEugene Leviant } 71ceabe80eSEugene Leviant 728f1f3c40SMeador Inge template <class ELFT> static SymbolBody *addSynthetic(SymbolAssignment *Cmd) { 738f1f3c40SMeador Inge uint8_t Visibility = Cmd->Hidden ? STV_HIDDEN : STV_DEFAULT; 74afaa9343SEugene Leviant const OutputSectionBase *Sec = 75afaa9343SEugene Leviant ScriptConfig->HasSections ? nullptr : Cmd->Expression.Section(); 768f1f3c40SMeador Inge Symbol *Sym = Symtab<ELFT>::X->addUndefined( 778f1f3c40SMeador Inge Cmd->Name, /*IsLocal=*/false, STB_GLOBAL, Visibility, 788f1f3c40SMeador Inge /*Type*/ 0, 798f1f3c40SMeador Inge /*CanOmitFromDynSym*/ false, /*File*/ nullptr); 80afaa9343SEugene Leviant 818f1f3c40SMeador Inge replaceBody<DefinedSynthetic>(Sym, Cmd->Name, 0, Sec); 828f1f3c40SMeador Inge return Sym->body(); 83ceabe80eSEugene Leviant } 84ceabe80eSEugene Leviant 8522375f24SRui Ueyama static bool isUnderSysroot(StringRef Path) { 8622375f24SRui Ueyama if (Config->Sysroot == "") 8722375f24SRui Ueyama return false; 8822375f24SRui Ueyama for (; !Path.empty(); Path = sys::path::parent_path(Path)) 8922375f24SRui Ueyama if (sys::fs::equivalent(Config->Sysroot, Path)) 9022375f24SRui Ueyama return true; 9122375f24SRui Ueyama return false; 9222375f24SRui Ueyama } 9322375f24SRui Ueyama 94*2ee2d2dcSGeorge Rimar template <class ELFT> 95*2ee2d2dcSGeorge Rimar void LinkerScript<ELFT>::setDot(Expr E, const Twine &Loc, bool InSec) { 96679828ffSRafael Espindola uintX_t Val = E(Dot); 974cd7352cSRafael Espindola if (Val < Dot) { 984cd7352cSRafael Espindola if (InSec) 99*2ee2d2dcSGeorge Rimar error(Loc + ": unable to move location counter backward for: " + 100*2ee2d2dcSGeorge Rimar CurOutSec->Name); 1014cd7352cSRafael Espindola else 102*2ee2d2dcSGeorge Rimar error(Loc + ": unable to move location counter backward"); 1034cd7352cSRafael Espindola } 1044cd7352cSRafael Espindola Dot = Val; 1054cd7352cSRafael Espindola // Update to location counter means update to section size. 1064cd7352cSRafael Espindola if (InSec) 1074cd7352cSRafael Espindola CurOutSec->Size = Dot - CurOutSec->Addr; 108679828ffSRafael Espindola } 109679828ffSRafael Espindola 110679828ffSRafael Espindola // Sets value of a symbol. Two kinds of symbols are processed: synthetic 111679828ffSRafael Espindola // symbols, whose value is an offset from beginning of section and regular 112679828ffSRafael Espindola // symbols whose value is absolute. 113679828ffSRafael Espindola template <class ELFT> 114679828ffSRafael Espindola void LinkerScript<ELFT>::assignSymbol(SymbolAssignment *Cmd, bool InSec) { 115679828ffSRafael Espindola if (Cmd->Name == ".") { 116*2ee2d2dcSGeorge Rimar setDot(Cmd->Expression, Cmd->Location, InSec); 1174cd7352cSRafael Espindola return; 1184cd7352cSRafael Espindola } 1194cd7352cSRafael Espindola 120b2b70975SGeorge Rimar if (!Cmd->Sym) 1218f1f3c40SMeador Inge return; 1228f1f3c40SMeador Inge 123b2b70975SGeorge Rimar if (auto *Body = dyn_cast<DefinedSynthetic>(Cmd->Sym)) { 124b2b70975SGeorge Rimar Body->Section = Cmd->Expression.Section(); 125ea590d91SRafael Espindola if (Body->Section) { 126ea590d91SRafael Espindola uint64_t VA = 0; 127ea590d91SRafael Espindola if (Body->Section->Flags & SHF_ALLOC) 128ea590d91SRafael Espindola VA = Body->Section->Addr; 129ea590d91SRafael Espindola Body->Value = Cmd->Expression(Dot) - VA; 130ea590d91SRafael Espindola } 131b2b70975SGeorge Rimar return; 132db741e72SEugene Leviant } 133b2b70975SGeorge Rimar 134b2b70975SGeorge Rimar cast<DefinedRegular<ELFT>>(Cmd->Sym)->Value = Cmd->Expression(Dot); 1358f1f3c40SMeador Inge } 1368f1f3c40SMeador Inge 1374cd7352cSRafael Espindola template <class ELFT> 1384cd7352cSRafael Espindola void LinkerScript<ELFT>::addSymbol(SymbolAssignment *Cmd) { 1391602421cSRui Ueyama if (Cmd->Name == ".") 1408f1f3c40SMeador Inge return; 1418f1f3c40SMeador Inge 1428f1f3c40SMeador Inge // If a symbol was in PROVIDE(), we need to define it only when 1438f1f3c40SMeador Inge // it is a referenced undefined symbol. 1441602421cSRui Ueyama SymbolBody *B = Symtab<ELFT>::X->find(Cmd->Name); 1458f1f3c40SMeador Inge if (Cmd->Provide && (!B || B->isDefined())) 1468f1f3c40SMeador Inge return; 1478f1f3c40SMeador Inge 1488f1f3c40SMeador Inge // Otherwise, create a new symbol if one does not exist or an 1498f1f3c40SMeador Inge // undefined one does exist. 1508f1f3c40SMeador Inge if (Cmd->Expression.IsAbsolute()) 1518f1f3c40SMeador Inge Cmd->Sym = addRegular<ELFT>(Cmd); 1528f1f3c40SMeador Inge else 1538f1f3c40SMeador Inge Cmd->Sym = addSynthetic<ELFT>(Cmd); 154b2b70975SGeorge Rimar 155b2b70975SGeorge Rimar // If there are sections, then let the value be assigned later in 156b2b70975SGeorge Rimar // `assignAddresses`. 157b2b70975SGeorge Rimar if (!ScriptConfig->HasSections) 1584cd7352cSRafael Espindola assignSymbol(Cmd); 159ceabe80eSEugene Leviant } 160ceabe80eSEugene Leviant 161076fe157SGeorge Rimar bool SymbolAssignment::classof(const BaseCommand *C) { 162076fe157SGeorge Rimar return C->Kind == AssignmentKind; 163076fe157SGeorge Rimar } 164076fe157SGeorge Rimar 165076fe157SGeorge Rimar bool OutputSectionCommand::classof(const BaseCommand *C) { 166076fe157SGeorge Rimar return C->Kind == OutputSectionKind; 167076fe157SGeorge Rimar } 168076fe157SGeorge Rimar 169eea3114fSGeorge Rimar bool InputSectionDescription::classof(const BaseCommand *C) { 170eea3114fSGeorge Rimar return C->Kind == InputSectionKind; 171eea3114fSGeorge Rimar } 172eea3114fSGeorge Rimar 173eefa758eSGeorge Rimar bool AssertCommand::classof(const BaseCommand *C) { 174eefa758eSGeorge Rimar return C->Kind == AssertKind; 175eefa758eSGeorge Rimar } 176eefa758eSGeorge Rimar 177e38cbab5SGeorge Rimar bool BytesDataCommand::classof(const BaseCommand *C) { 178e38cbab5SGeorge Rimar return C->Kind == BytesDataKind; 179e38cbab5SGeorge Rimar } 180e38cbab5SGeorge Rimar 18122886a28SEugene Zelenko template <class ELFT> LinkerScript<ELFT>::LinkerScript() = default; 18222886a28SEugene Zelenko template <class ELFT> LinkerScript<ELFT>::~LinkerScript() = default; 183f34d0e08SRui Ueyama 184e0be2901SRui Ueyama template <class ELFT> static StringRef basename(InputSectionBase<ELFT> *S) { 185e0be2901SRui Ueyama if (S->getFile()) 186e0be2901SRui Ueyama return sys::path::filename(S->getFile()->getName()); 187e0be2901SRui Ueyama return ""; 188e0be2901SRui Ueyama } 189e0be2901SRui Ueyama 19007320e40SRui Ueyama template <class ELFT> 19107320e40SRui Ueyama bool LinkerScript<ELFT>::shouldKeep(InputSectionBase<ELFT> *S) { 192e0be2901SRui Ueyama for (InputSectionDescription *ID : Opt.KeptSections) 193e0be2901SRui Ueyama if (ID->FilePat.match(basename(S))) 194cf43f179SEugene Leviant for (SectionPattern &P : ID->SectionPatterns) 195f91282e1SRui Ueyama if (P.SectionPat.match(S->Name)) 196eea3114fSGeorge Rimar return true; 197eea3114fSGeorge Rimar return false; 198eea3114fSGeorge Rimar } 199eea3114fSGeorge Rimar 200575208caSGeorge Rimar static bool comparePriority(InputSectionData *A, InputSectionData *B) { 201575208caSGeorge Rimar return getPriority(A->Name) < getPriority(B->Name); 202575208caSGeorge Rimar } 203575208caSGeorge Rimar 204c0028d3dSRafael Espindola static bool compareName(InputSectionData *A, InputSectionData *B) { 205042a3f20SRafael Espindola return A->Name < B->Name; 2060702c4e8SGeorge Rimar } 207742c3836SRui Ueyama 208c0028d3dSRafael Espindola static bool compareAlignment(InputSectionData *A, InputSectionData *B) { 209742c3836SRui Ueyama // ">" is not a mistake. Larger alignments are placed before smaller 210742c3836SRui Ueyama // alignments in order to reduce the amount of padding necessary. 211742c3836SRui Ueyama // This is compatible with GNU. 212742c3836SRui Ueyama return A->Alignment > B->Alignment; 213742c3836SRui Ueyama } 214742c3836SRui Ueyama 215c0028d3dSRafael Espindola static std::function<bool(InputSectionData *, InputSectionData *)> 216be394db3SGeorge Rimar getComparator(SortSectionPolicy K) { 217be394db3SGeorge Rimar switch (K) { 218be394db3SGeorge Rimar case SortSectionPolicy::Alignment: 219c0028d3dSRafael Espindola return compareAlignment; 220be394db3SGeorge Rimar case SortSectionPolicy::Name: 221be394db3SGeorge Rimar return compareName; 222be394db3SGeorge Rimar case SortSectionPolicy::Priority: 223be394db3SGeorge Rimar return comparePriority; 224be394db3SGeorge Rimar default: 225be394db3SGeorge Rimar llvm_unreachable("unknown sort policy"); 226be394db3SGeorge Rimar } 227742c3836SRui Ueyama } 2280702c4e8SGeorge Rimar 22948c3f1ceSRui Ueyama template <class ELFT> 230e71a3f8aSRafael Espindola static bool matchConstraints(ArrayRef<InputSectionBase<ELFT> *> Sections, 23106ae6836SGeorge Rimar ConstraintKind Kind) { 2328f66df92SGeorge Rimar if (Kind == ConstraintKind::NoConstraint) 2338f66df92SGeorge Rimar return true; 234e746e52cSRafael Espindola bool IsRW = llvm::any_of(Sections, [=](InputSectionData *Sec2) { 235d3190795SRafael Espindola auto *Sec = static_cast<InputSectionBase<ELFT> *>(Sec2); 2361854a8ebSRafael Espindola return Sec->Flags & SHF_WRITE; 23706ae6836SGeorge Rimar }); 238e746e52cSRafael Espindola return (IsRW && Kind == ConstraintKind::ReadWrite) || 239e746e52cSRafael Espindola (!IsRW && Kind == ConstraintKind::ReadOnly); 24006ae6836SGeorge Rimar } 24106ae6836SGeorge Rimar 24207171f21SGeorge Rimar static void sortSections(InputSectionData **Begin, InputSectionData **End, 243ee924709SRui Ueyama SortSectionPolicy K) { 244ee924709SRui Ueyama if (K != SortSectionPolicy::Default && K != SortSectionPolicy::None) 24507171f21SGeorge Rimar std::stable_sort(Begin, End, getComparator(K)); 246ee924709SRui Ueyama } 247ee924709SRui Ueyama 248d3190795SRafael Espindola // Compute and remember which sections the InputSectionDescription matches. 249be94e1b6SRafael Espindola template <class ELFT> 250e71a3f8aSRafael Espindola void LinkerScript<ELFT>::computeInputSections(InputSectionDescription *I) { 2514dc07becSRui Ueyama // Collects all sections that satisfy constraints of I 2524dc07becSRui Ueyama // and attach them to I. 2534dc07becSRui Ueyama for (SectionPattern &Pat : I->SectionPatterns) { 25407171f21SGeorge Rimar size_t SizeBefore = I->Sections.size(); 2558c6a5aafSRui Ueyama 2568c6a5aafSRui Ueyama for (InputSectionBase<ELFT> *S : Symtab<ELFT>::X->Sections) { 2573773bcacSRafael Espindola if (S->Assigned) 2588c6a5aafSRui Ueyama continue; 259908a3d34SRafael Espindola // For -emit-relocs we have to ignore entries like 260908a3d34SRafael Espindola // .rela.dyn : { *(.rela.data) } 261908a3d34SRafael Espindola // which are common because they are in the default bfd script. 262908a3d34SRafael Espindola if (S->Type == SHT_REL || S->Type == SHT_RELA) 263908a3d34SRafael Espindola continue; 2648c6a5aafSRui Ueyama 265e0be2901SRui Ueyama StringRef Filename = basename(S); 266e0be2901SRui Ueyama if (!I->FilePat.match(Filename) || Pat.ExcludedFilePat.match(Filename)) 267e0be2901SRui Ueyama continue; 268e0be2901SRui Ueyama if (!Pat.SectionPat.match(S->Name)) 269e0be2901SRui Ueyama continue; 270d3190795SRafael Espindola I->Sections.push_back(S); 271f94efdddSRui Ueyama S->Assigned = true; 272f94efdddSRui Ueyama } 273d3190795SRafael Espindola 274ee924709SRui Ueyama // Sort sections as instructed by SORT-family commands and --sort-section 275ee924709SRui Ueyama // option. Because SORT-family commands can be nested at most two depth 276ee924709SRui Ueyama // (e.g. SORT_BY_NAME(SORT_BY_ALIGNMENT(.text.*))) and because the command 277ee924709SRui Ueyama // line option is respected even if a SORT command is given, the exact 278ee924709SRui Ueyama // behavior we have here is a bit complicated. Here are the rules. 279ee924709SRui Ueyama // 280ee924709SRui Ueyama // 1. If two SORT commands are given, --sort-section is ignored. 281ee924709SRui Ueyama // 2. If one SORT command is given, and if it is not SORT_NONE, 282ee924709SRui Ueyama // --sort-section is handled as an inner SORT command. 283ee924709SRui Ueyama // 3. If one SORT command is given, and if it is SORT_NONE, don't sort. 284ee924709SRui Ueyama // 4. If no SORT command is given, sort according to --sort-section. 28507171f21SGeorge Rimar InputSectionData **Begin = I->Sections.data() + SizeBefore; 28607171f21SGeorge Rimar InputSectionData **End = I->Sections.data() + I->Sections.size(); 28707171f21SGeorge Rimar if (Pat.SortOuter != SortSectionPolicy::None) { 28807171f21SGeorge Rimar if (Pat.SortInner == SortSectionPolicy::Default) 28907171f21SGeorge Rimar sortSections(Begin, End, Config->SortSection); 290ee924709SRui Ueyama else 29107171f21SGeorge Rimar sortSections(Begin, End, Pat.SortInner); 29207171f21SGeorge Rimar sortSections(Begin, End, Pat.SortOuter); 29307171f21SGeorge Rimar } 294ee924709SRui Ueyama } 295be94e1b6SRafael Espindola } 296be94e1b6SRafael Espindola 297be94e1b6SRafael Espindola template <class ELFT> 298be94e1b6SRafael Espindola void LinkerScript<ELFT>::discard(ArrayRef<InputSectionBase<ELFT> *> V) { 299be94e1b6SRafael Espindola for (InputSectionBase<ELFT> *S : V) { 300be94e1b6SRafael Espindola S->Live = false; 301ecbfd871SRafael Espindola if (S == In<ELFT>::ShStrTab) 302ecbfd871SRafael Espindola error("discarding .shstrtab section is not allowed"); 303647c1685SGeorge Rimar discard(S->DependentSections); 304be94e1b6SRafael Espindola } 305be94e1b6SRafael Espindola } 306be94e1b6SRafael Espindola 30706ae6836SGeorge Rimar template <class ELFT> 3080b9ce6a4SRui Ueyama std::vector<InputSectionBase<ELFT> *> 30906ae6836SGeorge Rimar LinkerScript<ELFT>::createInputSectionList(OutputSectionCommand &OutCmd) { 3100b9ce6a4SRui Ueyama std::vector<InputSectionBase<ELFT> *> Ret; 311e7f912cdSRui Ueyama 31206ae6836SGeorge Rimar for (const std::unique_ptr<BaseCommand> &Base : OutCmd.Commands) { 3137c3ff2ebSRafael Espindola auto *Cmd = dyn_cast<InputSectionDescription>(Base.get()); 3147c3ff2ebSRafael Espindola if (!Cmd) 3150b9ce6a4SRui Ueyama continue; 316e71a3f8aSRafael Espindola computeInputSections(Cmd); 317d3190795SRafael Espindola for (InputSectionData *S : Cmd->Sections) 318d3190795SRafael Espindola Ret.push_back(static_cast<InputSectionBase<ELFT> *>(S)); 3190b9ce6a4SRui Ueyama } 320e71a3f8aSRafael Espindola 3210b9ce6a4SRui Ueyama return Ret; 3220b9ce6a4SRui Ueyama } 3230b9ce6a4SRui Ueyama 324e5d3ca50SPetr Hosek template <class ELFT> 32520d03194SEugene Leviant void LinkerScript<ELFT>::processCommands(OutputSectionFactory<ELFT> &Factory) { 3267c3ff2ebSRafael Espindola for (unsigned I = 0; I < Opt.Commands.size(); ++I) { 3277c3ff2ebSRafael Espindola auto Iter = Opt.Commands.begin() + I; 3287c3ff2ebSRafael Espindola const std::unique_ptr<BaseCommand> &Base1 = *Iter; 3290b1b695aSRui Ueyama 3300b1b695aSRui Ueyama // Handle symbol assignments outside of any output section. 3312ab5f73dSRui Ueyama if (auto *Cmd = dyn_cast<SymbolAssignment>(Base1.get())) { 3324cd7352cSRafael Espindola addSymbol(Cmd); 3332ab5f73dSRui Ueyama continue; 3342ab5f73dSRui Ueyama } 3350b1b695aSRui Ueyama 33620d03194SEugene Leviant if (auto *Cmd = dyn_cast<AssertCommand>(Base1.get())) { 33720d03194SEugene Leviant // If we don't have SECTIONS then output sections have already been 338194470cdSGeorge Rimar // created by Writer<ELFT>. The LinkerScript<ELFT>::assignAddresses 33920d03194SEugene Leviant // will not be called, so ASSERT should be evaluated now. 34020d03194SEugene Leviant if (!Opt.HasSections) 34120d03194SEugene Leviant Cmd->Expression(0); 34220d03194SEugene Leviant continue; 34320d03194SEugene Leviant } 3442ab5f73dSRui Ueyama 345ceabe80eSEugene Leviant if (auto *Cmd = dyn_cast<OutputSectionCommand>(Base1.get())) { 3467bd37870SRafael Espindola std::vector<InputSectionBase<ELFT> *> V = createInputSectionList(*Cmd); 3477bd37870SRafael Espindola 3480b1b695aSRui Ueyama // The output section name `/DISCARD/' is special. 3490b1b695aSRui Ueyama // Any input section assigned to it is discarded. 35048c3f1ceSRui Ueyama if (Cmd->Name == "/DISCARD/") { 3517bd37870SRafael Espindola discard(V); 35248c3f1ceSRui Ueyama continue; 35348c3f1ceSRui Ueyama } 3540b9ce6a4SRui Ueyama 3550b1b695aSRui Ueyama // This is for ONLY_IF_RO and ONLY_IF_RW. An output section directive 3560b1b695aSRui Ueyama // ".foo : ONLY_IF_R[OW] { ... }" is handled only if all member input 3570b1b695aSRui Ueyama // sections satisfy a given constraint. If not, a directive is handled 3580b1b695aSRui Ueyama // as if it wasn't present from the beginning. 3590b1b695aSRui Ueyama // 3600b1b695aSRui Ueyama // Because we'll iterate over Commands many more times, the easiest 3610b1b695aSRui Ueyama // way to "make it as if it wasn't present" is to just remove it. 3627c3ff2ebSRafael Espindola if (!matchConstraints<ELFT>(V, Cmd->Constraint)) { 3637c3ff2ebSRafael Espindola for (InputSectionBase<ELFT> *S : V) 364f94efdddSRui Ueyama S->Assigned = false; 3657c3ff2ebSRafael Espindola Opt.Commands.erase(Iter); 366dfbbbc86SGeorge Rimar --I; 3677c3ff2ebSRafael Espindola continue; 3687c3ff2ebSRafael Espindola } 3697c3ff2ebSRafael Espindola 3700b1b695aSRui Ueyama // A directive may contain symbol definitions like this: 3710b1b695aSRui Ueyama // ".foo : { ...; bar = .; }". Handle them. 3727c3ff2ebSRafael Espindola for (const std::unique_ptr<BaseCommand> &Base : Cmd->Commands) 3737c3ff2ebSRafael Espindola if (auto *OutCmd = dyn_cast<SymbolAssignment>(Base.get())) 3744cd7352cSRafael Espindola addSymbol(OutCmd); 3757c3ff2ebSRafael Espindola 3760b1b695aSRui Ueyama // Handle subalign (e.g. ".foo : SUBALIGN(32) { ... }"). If subalign 3770b1b695aSRui Ueyama // is given, input sections are aligned to that value, whether the 3780b1b695aSRui Ueyama // given value is larger or smaller than the original section alignment. 3790b1b695aSRui Ueyama if (Cmd->SubalignExpr) { 3800b1b695aSRui Ueyama uint32_t Subalign = Cmd->SubalignExpr(0); 3810b1b695aSRui Ueyama for (InputSectionBase<ELFT> *S : V) 3820b1b695aSRui Ueyama S->Alignment = Subalign; 38320d03194SEugene Leviant } 3840b1b695aSRui Ueyama 3850b1b695aSRui Ueyama // Add input sections to an output section. 3860b1b695aSRui Ueyama for (InputSectionBase<ELFT> *S : V) 3878290274cSRafael Espindola Factory.addInputSec(S, Cmd->Name); 388eea3114fSGeorge Rimar } 38948c3f1ceSRui Ueyama } 390db24d9c3SGeorge Rimar } 391e63d81bdSEugene Leviant 3920b1b695aSRui Ueyama // Add sections that didn't match any sections command. 39320d03194SEugene Leviant template <class ELFT> 39493c64025SGeorge Rimar void LinkerScript<ELFT>::addOrphanSections( 39593c64025SGeorge Rimar OutputSectionFactory<ELFT> &Factory) { 3968c6a5aafSRui Ueyama for (InputSectionBase<ELFT> *S : Symtab<ELFT>::X->Sections) 3978f9026baSRafael Espindola if (S->Live && !S->OutSec) 3988290274cSRafael Espindola Factory.addInputSec(S, getOutputSectionName(S->Name)); 399e63d81bdSEugene Leviant } 400e63d81bdSEugene Leviant 401e08e78dfSRafael Espindola template <class ELFT> static bool isTbss(OutputSectionBase *Sec) { 40204a2e348SRafael Espindola return (Sec->Flags & SHF_TLS) && Sec->Type == SHT_NOBITS; 403a940e539SRafael Espindola } 404a940e539SRafael Espindola 405d3190795SRafael Espindola template <class ELFT> void LinkerScript<ELFT>::output(InputSection<ELFT> *S) { 406d3190795SRafael Espindola if (!AlreadyOutputIS.insert(S).second) 407ceabe80eSEugene Leviant return; 408e08e78dfSRafael Espindola bool IsTbss = isTbss<ELFT>(CurOutSec); 409d3190795SRafael Espindola 410d3190795SRafael Espindola uintX_t Pos = IsTbss ? Dot + ThreadBssOffset : Dot; 411d3190795SRafael Espindola Pos = alignTo(Pos, S->Alignment); 41204a2e348SRafael Espindola S->OutSecOff = Pos - CurOutSec->Addr; 413d3190795SRafael Espindola Pos += S->getSize(); 414d3190795SRafael Espindola 415d3190795SRafael Espindola // Update output section size after adding each section. This is so that 416d3190795SRafael Espindola // SIZEOF works correctly in the case below: 417d3190795SRafael Espindola // .foo { *(.aaa) a = SIZEOF(.foo); *(.bbb) } 41804a2e348SRafael Espindola CurOutSec->Size = Pos - CurOutSec->Addr; 419d3190795SRafael Espindola 420b889744eSMeador Inge // If there is a memory region associated with this input section, then 421b889744eSMeador Inge // place the section in that region and update the region index. 422b889744eSMeador Inge if (CurMemRegion) { 423b889744eSMeador Inge CurMemRegion->Offset += CurOutSec->Size; 424b889744eSMeador Inge uint64_t CurSize = CurMemRegion->Offset - CurMemRegion->Origin; 425b889744eSMeador Inge if (CurSize > CurMemRegion->Length) { 426b889744eSMeador Inge uint64_t OverflowAmt = CurSize - CurMemRegion->Length; 427b889744eSMeador Inge error("section '" + CurOutSec->Name + "' will not fit in region '" + 428b889744eSMeador Inge CurMemRegion->Name + "': overflowed by " + Twine(OverflowAmt) + 429b889744eSMeador Inge " bytes"); 430b889744eSMeador Inge } 431b889744eSMeador Inge } 432b889744eSMeador Inge 4337252ae52SRafael Espindola if (IsTbss) 4347252ae52SRafael Espindola ThreadBssOffset = Pos - Dot; 4357252ae52SRafael Espindola else 436d3190795SRafael Espindola Dot = Pos; 4372de509c3SRui Ueyama } 438ceabe80eSEugene Leviant 439d3190795SRafael Espindola template <class ELFT> void LinkerScript<ELFT>::flush() { 44065499b90SRafael Espindola if (!CurOutSec || !AlreadyOutputOS.insert(CurOutSec).second) 44165499b90SRafael Espindola return; 44265499b90SRafael Espindola if (auto *OutSec = dyn_cast<OutputSection<ELFT>>(CurOutSec)) { 443d3190795SRafael Espindola for (InputSection<ELFT> *I : OutSec->Sections) 444d3190795SRafael Espindola output(I); 44565499b90SRafael Espindola } else { 44604a2e348SRafael Espindola Dot += CurOutSec->Size; 447d3190795SRafael Espindola } 448d3190795SRafael Espindola } 44997403d15SEugene Leviant 450d3190795SRafael Espindola template <class ELFT> 451e08e78dfSRafael Espindola void LinkerScript<ELFT>::switchTo(OutputSectionBase *Sec) { 452d3190795SRafael Espindola if (CurOutSec == Sec) 453d3190795SRafael Espindola return; 454d3190795SRafael Espindola if (AlreadyOutputOS.count(Sec)) 455d3190795SRafael Espindola return; 456d3190795SRafael Espindola 457d3190795SRafael Espindola flush(); 458d3190795SRafael Espindola CurOutSec = Sec; 459d3190795SRafael Espindola 46004a2e348SRafael Espindola Dot = alignTo(Dot, CurOutSec->Addralign); 461e08e78dfSRafael Espindola CurOutSec->Addr = isTbss<ELFT>(CurOutSec) ? Dot + ThreadBssOffset : Dot; 462b71d6f7aSEugene Leviant 463b71d6f7aSEugene Leviant // If neither AT nor AT> is specified for an allocatable section, the linker 464b71d6f7aSEugene Leviant // will set the LMA such that the difference between VMA and LMA for the 465b71d6f7aSEugene Leviant // section is the same as the preceding output section in the same region 466b71d6f7aSEugene Leviant // https://sourceware.org/binutils/docs-2.20/ld/Output-Section-LMA.html 467b71d6f7aSEugene Leviant CurOutSec->setLMAOffset(LMAOffset); 468d3190795SRafael Espindola } 469d3190795SRafael Espindola 470d3190795SRafael Espindola template <class ELFT> void LinkerScript<ELFT>::process(BaseCommand &Base) { 471e38cbab5SGeorge Rimar // This handles the assignments to symbol or to a location counter (.) 472d3190795SRafael Espindola if (auto *AssignCmd = dyn_cast<SymbolAssignment>(&Base)) { 4734cd7352cSRafael Espindola assignSymbol(AssignCmd, true); 474d3190795SRafael Espindola return; 47597403d15SEugene Leviant } 476e38cbab5SGeorge Rimar 477e38cbab5SGeorge Rimar // Handle BYTE(), SHORT(), LONG(), or QUAD(). 478e38cbab5SGeorge Rimar if (auto *DataCmd = dyn_cast<BytesDataCommand>(&Base)) { 47904a2e348SRafael Espindola DataCmd->Offset = Dot - CurOutSec->Addr; 480e38cbab5SGeorge Rimar Dot += DataCmd->Size; 48104a2e348SRafael Espindola CurOutSec->Size = Dot - CurOutSec->Addr; 482e38cbab5SGeorge Rimar return; 483e38cbab5SGeorge Rimar } 484e38cbab5SGeorge Rimar 485b2d99d6aSMeador Inge if (auto *AssertCmd = dyn_cast<AssertCommand>(&Base)) { 486b2d99d6aSMeador Inge AssertCmd->Expression(Dot); 487b2d99d6aSMeador Inge return; 488b2d99d6aSMeador Inge } 489b2d99d6aSMeador Inge 490e38cbab5SGeorge Rimar // It handles single input section description command, 491e38cbab5SGeorge Rimar // calculates and assigns the offsets for each section and also 492e38cbab5SGeorge Rimar // updates the output section size. 493d3190795SRafael Espindola auto &ICmd = cast<InputSectionDescription>(Base); 494d3190795SRafael Espindola for (InputSectionData *ID : ICmd.Sections) { 4953fb5a6dcSGeorge Rimar // We tentatively added all synthetic sections at the beginning and removed 4963fb5a6dcSGeorge Rimar // empty ones afterwards (because there is no way to know whether they were 4973fb5a6dcSGeorge Rimar // going be empty or not other than actually running linker scripts.) 4983fb5a6dcSGeorge Rimar // We need to ignore remains of empty sections. 4993fb5a6dcSGeorge Rimar if (auto *Sec = dyn_cast<SyntheticSection<ELFT>>(ID)) 5003fb5a6dcSGeorge Rimar if (Sec->empty()) 5013fb5a6dcSGeorge Rimar continue; 5023fb5a6dcSGeorge Rimar 503d3190795SRafael Espindola auto *IB = static_cast<InputSectionBase<ELFT> *>(ID); 504d3190795SRafael Espindola switchTo(IB->OutSec); 505d3190795SRafael Espindola if (auto *I = dyn_cast<InputSection<ELFT>>(IB)) 506d3190795SRafael Espindola output(I); 50765499b90SRafael Espindola else 50865499b90SRafael Espindola flush(); 509ceabe80eSEugene Leviant } 510ceabe80eSEugene Leviant } 511ceabe80eSEugene Leviant 5128f66df92SGeorge Rimar template <class ELFT> 5132b074553SRafael Espindola static OutputSectionBase * 5142b074553SRafael Espindola findSection(StringRef Name, const std::vector<OutputSectionBase *> &Sections) { 5152b074553SRafael Espindola auto End = Sections.end(); 5162b074553SRafael Espindola auto HasName = [=](OutputSectionBase *Sec) { return Sec->getName() == Name; }; 5172b074553SRafael Espindola auto I = std::find_if(Sections.begin(), End, HasName); 518e08e78dfSRafael Espindola std::vector<OutputSectionBase *> Ret; 5192b074553SRafael Espindola if (I == End) 5202b074553SRafael Espindola return nullptr; 5212b074553SRafael Espindola assert(std::find_if(I + 1, End, HasName) == End); 5222b074553SRafael Espindola return *I; 5238f66df92SGeorge Rimar } 5248f66df92SGeorge Rimar 525b889744eSMeador Inge // This function searches for a memory region to place the given output 526b889744eSMeador Inge // section in. If found, a pointer to the appropriate memory region is 527b889744eSMeador Inge // returned. Otherwise, a nullptr is returned. 528b889744eSMeador Inge template <class ELFT> 529b889744eSMeador Inge MemoryRegion *LinkerScript<ELFT>::findMemoryRegion(OutputSectionCommand *Cmd, 530b889744eSMeador Inge OutputSectionBase *Sec) { 531b889744eSMeador Inge // If a memory region name was specified in the output section command, 532b889744eSMeador Inge // then try to find that region first. 533b889744eSMeador Inge if (!Cmd->MemoryRegionName.empty()) { 534b889744eSMeador Inge auto It = Opt.MemoryRegions.find(Cmd->MemoryRegionName); 535b889744eSMeador Inge if (It != Opt.MemoryRegions.end()) 536b889744eSMeador Inge return &It->second; 537b889744eSMeador Inge error("memory region '" + Cmd->MemoryRegionName + "' not declared"); 538b889744eSMeador Inge return nullptr; 539b889744eSMeador Inge } 540b889744eSMeador Inge 541b889744eSMeador Inge // The memory region name is empty, thus a suitable region must be 542b889744eSMeador Inge // searched for in the region map. If the region map is empty, just 543b889744eSMeador Inge // return. Note that this check doesn't happen at the very beginning 544b889744eSMeador Inge // so that uses of undeclared regions can be caught. 545b889744eSMeador Inge if (!Opt.MemoryRegions.size()) 546b889744eSMeador Inge return nullptr; 547b889744eSMeador Inge 548b889744eSMeador Inge // See if a region can be found by matching section flags. 549b889744eSMeador Inge for (auto &MRI : Opt.MemoryRegions) { 550b889744eSMeador Inge MemoryRegion &MR = MRI.second; 5518a8a953eSRui Ueyama if ((MR.Flags & Sec->Flags) != 0 && (MR.NegFlags & Sec->Flags) == 0) 552b889744eSMeador Inge return &MR; 553b889744eSMeador Inge } 554b889744eSMeador Inge 555b889744eSMeador Inge // Otherwise, no suitable region was found. 556b889744eSMeador Inge if (Sec->Flags & SHF_ALLOC) 557b889744eSMeador Inge error("no memory region specified for section '" + Sec->Name + "'"); 558b889744eSMeador Inge return nullptr; 559b889744eSMeador Inge } 560b889744eSMeador Inge 5610b1b695aSRui Ueyama // This function assigns offsets to input sections and an output section 5620b1b695aSRui Ueyama // for a single sections command (e.g. ".text { *(.text); }"). 563d3190795SRafael Espindola template <class ELFT> 564d3190795SRafael Espindola void LinkerScript<ELFT>::assignOffsets(OutputSectionCommand *Cmd) { 565b71d6f7aSEugene Leviant if (Cmd->LMAExpr) 566b71d6f7aSEugene Leviant LMAOffset = Cmd->LMAExpr(Dot) - Dot; 5672b074553SRafael Espindola OutputSectionBase *Sec = findSection<ELFT>(Cmd->Name, *OutputSections); 5682b074553SRafael Espindola if (!Sec) 569d3190795SRafael Espindola return; 570b889744eSMeador Inge 571679828ffSRafael Espindola if (Cmd->AddrExpr && Sec->Flags & SHF_ALLOC) 572*2ee2d2dcSGeorge Rimar setDot(Cmd->AddrExpr, Cmd->Location); 573679828ffSRafael Espindola 574165088aaSPetr Hosek // Handle align (e.g. ".foo : ALIGN(16) { ... }"). 575165088aaSPetr Hosek if (Cmd->AlignExpr) 576165088aaSPetr Hosek Sec->updateAlignment(Cmd->AlignExpr(0)); 577165088aaSPetr Hosek 578b889744eSMeador Inge // Try and find an appropriate memory region to assign offsets in. 579b889744eSMeador Inge CurMemRegion = findMemoryRegion(Cmd, Sec); 580b889744eSMeador Inge if (CurMemRegion) 581b889744eSMeador Inge Dot = CurMemRegion->Offset; 582b889744eSMeador Inge switchTo(Sec); 5830b1b695aSRui Ueyama 584d3190795SRafael Espindola // Find the last section output location. We will output orphan sections 585d3190795SRafael Espindola // there so that end symbols point to the correct location. 586d3190795SRafael Espindola auto E = std::find_if(Cmd->Commands.rbegin(), Cmd->Commands.rend(), 587d3190795SRafael Espindola [](const std::unique_ptr<BaseCommand> &Cmd) { 588d3190795SRafael Espindola return !isa<SymbolAssignment>(*Cmd); 589d3190795SRafael Espindola }) 590d3190795SRafael Espindola .base(); 591d3190795SRafael Espindola for (auto I = Cmd->Commands.begin(); I != E; ++I) 592d3190795SRafael Espindola process(**I); 5932506cb4dSEugene Leviant flush(); 594b31dd370SGeorge Rimar std::for_each(E, Cmd->Commands.end(), 595b31dd370SGeorge Rimar [this](std::unique_ptr<BaseCommand> &B) { process(*B.get()); }); 596d3190795SRafael Espindola } 597d3190795SRafael Espindola 59807fe6129SRafael Espindola template <class ELFT> void LinkerScript<ELFT>::removeEmptyCommands() { 5996d38e4dbSRafael Espindola // It is common practice to use very generic linker scripts. So for any 6006d38e4dbSRafael Espindola // given run some of the output sections in the script will be empty. 6016d38e4dbSRafael Espindola // We could create corresponding empty output sections, but that would 6026d38e4dbSRafael Espindola // clutter the output. 6036d38e4dbSRafael Espindola // We instead remove trivially empty sections. The bfd linker seems even 6046d38e4dbSRafael Espindola // more aggressive at removing them. 6056d38e4dbSRafael Espindola auto Pos = std::remove_if( 6066d38e4dbSRafael Espindola Opt.Commands.begin(), Opt.Commands.end(), 6076d38e4dbSRafael Espindola [&](const std::unique_ptr<BaseCommand> &Base) { 6080b1b695aSRui Ueyama if (auto *Cmd = dyn_cast<OutputSectionCommand>(Base.get())) 6092b074553SRafael Espindola return !findSection<ELFT>(Cmd->Name, *OutputSections); 6100b1b695aSRui Ueyama return false; 6116d38e4dbSRafael Espindola }); 6126d38e4dbSRafael Espindola Opt.Commands.erase(Pos, Opt.Commands.end()); 61307fe6129SRafael Espindola } 61407fe6129SRafael Espindola 6156a53737cSRafael Espindola static bool isAllSectionDescription(const OutputSectionCommand &Cmd) { 6166a53737cSRafael Espindola for (const std::unique_ptr<BaseCommand> &I : Cmd.Commands) 6176a53737cSRafael Espindola if (!isa<InputSectionDescription>(*I)) 6186a53737cSRafael Espindola return false; 6196a53737cSRafael Espindola return true; 6206a53737cSRafael Espindola } 6216d38e4dbSRafael Espindola 6226a53737cSRafael Espindola template <class ELFT> void LinkerScript<ELFT>::adjustSectionsBeforeSorting() { 6239546fffbSRafael Espindola // If the output section contains only symbol assignments, create a 6249546fffbSRafael Espindola // corresponding output section. The bfd linker seems to only create them if 6259546fffbSRafael Espindola // '.' is assigned to, but creating these section should not have any bad 6269546fffbSRafael Espindola // consequeces and gives us a section to put the symbol in. 6279546fffbSRafael Espindola uintX_t Flags = SHF_ALLOC; 628f93b8c29SRafael Espindola uint32_t Type = SHT_NOBITS; 6299546fffbSRafael Espindola for (const std::unique_ptr<BaseCommand> &Base : Opt.Commands) { 6309546fffbSRafael Espindola auto *Cmd = dyn_cast<OutputSectionCommand>(Base.get()); 6319546fffbSRafael Espindola if (!Cmd) 6329546fffbSRafael Espindola continue; 6332b074553SRafael Espindola if (OutputSectionBase *Sec = 6342b074553SRafael Espindola findSection<ELFT>(Cmd->Name, *OutputSections)) { 6352b074553SRafael Espindola Flags = Sec->Flags; 6362b074553SRafael Espindola Type = Sec->Type; 6379546fffbSRafael Espindola continue; 6389546fffbSRafael Espindola } 6399546fffbSRafael Espindola 6406a53737cSRafael Espindola if (isAllSectionDescription(*Cmd)) 6416a53737cSRafael Espindola continue; 6426a53737cSRafael Espindola 64395642b95SRui Ueyama auto *OutSec = make<OutputSection<ELFT>>(Cmd->Name, Type, Flags); 6449546fffbSRafael Espindola OutputSections->push_back(OutSec); 6459546fffbSRafael Espindola } 646f7a17448SRafael Espindola } 647f7a17448SRafael Espindola 648f7a17448SRafael Espindola template <class ELFT> void LinkerScript<ELFT>::adjustSectionsAfterSorting() { 649f7a17448SRafael Espindola placeOrphanSections(); 650f7a17448SRafael Espindola 651f7a17448SRafael Espindola // If output section command doesn't specify any segments, 652f7a17448SRafael Espindola // and we haven't previously assigned any section to segment, 653f7a17448SRafael Espindola // then we simply assign section to the very first load segment. 654f7a17448SRafael Espindola // Below is an example of such linker script: 655f7a17448SRafael Espindola // PHDRS { seg PT_LOAD; } 656f7a17448SRafael Espindola // SECTIONS { .aaa : { *(.aaa) } } 657f7a17448SRafael Espindola std::vector<StringRef> DefPhdrs; 658f7a17448SRafael Espindola auto FirstPtLoad = 659f7a17448SRafael Espindola std::find_if(Opt.PhdrsCommands.begin(), Opt.PhdrsCommands.end(), 660f7a17448SRafael Espindola [](const PhdrsCommand &Cmd) { return Cmd.Type == PT_LOAD; }); 661f7a17448SRafael Espindola if (FirstPtLoad != Opt.PhdrsCommands.end()) 662f7a17448SRafael Espindola DefPhdrs.push_back(FirstPtLoad->Name); 663f7a17448SRafael Espindola 664f7a17448SRafael Espindola // Walk the commands and propagate the program headers to commands that don't 665f7a17448SRafael Espindola // explicitly specify them. 666f7a17448SRafael Espindola for (const std::unique_ptr<BaseCommand> &Base : Opt.Commands) { 667f7a17448SRafael Espindola auto *Cmd = dyn_cast<OutputSectionCommand>(Base.get()); 668f7a17448SRafael Espindola if (!Cmd) 669f7a17448SRafael Espindola continue; 670f7a17448SRafael Espindola if (Cmd->Phdrs.empty()) 671f7a17448SRafael Espindola Cmd->Phdrs = DefPhdrs; 672f7a17448SRafael Espindola else 673f7a17448SRafael Espindola DefPhdrs = Cmd->Phdrs; 674f7a17448SRafael Espindola } 6756a53737cSRafael Espindola 6766a53737cSRafael Espindola removeEmptyCommands(); 6779546fffbSRafael Espindola } 6789546fffbSRafael Espindola 67915c57951SRafael Espindola // When placing orphan sections, we want to place them after symbol assignments 68015c57951SRafael Espindola // so that an orphan after 68115c57951SRafael Espindola // begin_foo = .; 68215c57951SRafael Espindola // foo : { *(foo) } 68315c57951SRafael Espindola // end_foo = .; 68415c57951SRafael Espindola // doesn't break the intended meaning of the begin/end symbols. 68515c57951SRafael Espindola // We don't want to go over sections since Writer<ELFT>::sortSections is the 68615c57951SRafael Espindola // one in charge of deciding the order of the sections. 68715c57951SRafael Espindola // We don't want to go over alignments, since doing so in 68815c57951SRafael Espindola // rx_sec : { *(rx_sec) } 68915c57951SRafael Espindola // . = ALIGN(0x1000); 69015c57951SRafael Espindola // /* The RW PT_LOAD starts here*/ 69115c57951SRafael Espindola // rw_sec : { *(rw_sec) } 69215c57951SRafael Espindola // would mean that the RW PT_LOAD would become unaligned. 6935fcc99c2SRafael Espindola static bool shouldSkip(const BaseCommand &Cmd) { 69415c57951SRafael Espindola if (isa<OutputSectionCommand>(Cmd)) 69515c57951SRafael Espindola return false; 69615c57951SRafael Espindola const auto *Assign = dyn_cast<SymbolAssignment>(&Cmd); 69715c57951SRafael Espindola if (!Assign) 69815c57951SRafael Espindola return true; 6995fcc99c2SRafael Espindola return Assign->Name != "."; 70015c57951SRafael Espindola } 70115c57951SRafael Espindola 7026697ec29SRui Ueyama // Orphan sections are sections present in the input files which are 7036697ec29SRui Ueyama // not explicitly placed into the output file by the linker script. 7046697ec29SRui Ueyama // 7056697ec29SRui Ueyama // When the control reaches this function, Opt.Commands contains 7066697ec29SRui Ueyama // output section commands for non-orphan sections only. This function 7076697ec29SRui Ueyama // adds new elements for orphan sections to Opt.Commands so that all 7086697ec29SRui Ueyama // sections are explicitly handled by Opt.Commands. 7096697ec29SRui Ueyama // 7106697ec29SRui Ueyama // Writer<ELFT>::sortSections has already sorted output sections. 7116697ec29SRui Ueyama // What we need to do is to scan OutputSections vector and 7126697ec29SRui Ueyama // Opt.Commands in parallel to find orphan sections. If there is an 7136697ec29SRui Ueyama // output section that doesn't have a corresponding entry in 7146697ec29SRui Ueyama // Opt.Commands, we will insert a new entry to Opt.Commands. 7156697ec29SRui Ueyama // 7166697ec29SRui Ueyama // There is some ambiguity as to where exactly a new entry should be 7176697ec29SRui Ueyama // inserted, because Opt.Commands contains not only output section 7186697ec29SRui Ueyama // commands but other types of commands such as symbol assignment 7196697ec29SRui Ueyama // expressions. There's no correct answer here due to the lack of the 7206697ec29SRui Ueyama // formal specification of the linker script. We use heuristics to 7216697ec29SRui Ueyama // determine whether a new output command should be added before or 7226697ec29SRui Ueyama // after another commands. For the details, look at shouldSkip 7236697ec29SRui Ueyama // function. 72493c64025SGeorge Rimar template <class ELFT> void LinkerScript<ELFT>::placeOrphanSections() { 725aab6d5c5SRafael Espindola // The OutputSections are already in the correct order. 726aab6d5c5SRafael Espindola // This loops creates or moves commands as needed so that they are in the 727aab6d5c5SRafael Espindola // correct order. 728aab6d5c5SRafael Espindola int CmdIndex = 0; 7295fcc99c2SRafael Espindola 7305fcc99c2SRafael Espindola // As a horrible special case, skip the first . assignment if it is before any 7315fcc99c2SRafael Espindola // section. We do this because it is common to set a load address by starting 7325fcc99c2SRafael Espindola // the script with ". = 0xabcd" and the expectation is that every section is 7335fcc99c2SRafael Espindola // after that. 7345fcc99c2SRafael Espindola auto FirstSectionOrDotAssignment = 7355fcc99c2SRafael Espindola std::find_if(Opt.Commands.begin(), Opt.Commands.end(), 7365fcc99c2SRafael Espindola [](const std::unique_ptr<BaseCommand> &Cmd) { 7375fcc99c2SRafael Espindola if (isa<OutputSectionCommand>(*Cmd)) 7385fcc99c2SRafael Espindola return true; 7395fcc99c2SRafael Espindola const auto *Assign = dyn_cast<SymbolAssignment>(Cmd.get()); 7405fcc99c2SRafael Espindola if (!Assign) 7415fcc99c2SRafael Espindola return false; 7425fcc99c2SRafael Espindola return Assign->Name == "."; 7435fcc99c2SRafael Espindola }); 7445fcc99c2SRafael Espindola if (FirstSectionOrDotAssignment != Opt.Commands.end()) { 7455fcc99c2SRafael Espindola CmdIndex = FirstSectionOrDotAssignment - Opt.Commands.begin(); 7465fcc99c2SRafael Espindola if (isa<SymbolAssignment>(**FirstSectionOrDotAssignment)) 7475fcc99c2SRafael Espindola ++CmdIndex; 7485fcc99c2SRafael Espindola } 7495fcc99c2SRafael Espindola 750e08e78dfSRafael Espindola for (OutputSectionBase *Sec : *OutputSections) { 751652852c5SGeorge Rimar StringRef Name = Sec->getName(); 752aab6d5c5SRafael Espindola 753aab6d5c5SRafael Espindola // Find the last spot where we can insert a command and still get the 75415c57951SRafael Espindola // correct result. 755aab6d5c5SRafael Espindola auto CmdIter = Opt.Commands.begin() + CmdIndex; 756aab6d5c5SRafael Espindola auto E = Opt.Commands.end(); 7575fcc99c2SRafael Espindola while (CmdIter != E && shouldSkip(**CmdIter)) { 758aab6d5c5SRafael Espindola ++CmdIter; 759aab6d5c5SRafael Espindola ++CmdIndex; 760aab6d5c5SRafael Espindola } 761aab6d5c5SRafael Espindola 762aab6d5c5SRafael Espindola auto Pos = 763aab6d5c5SRafael Espindola std::find_if(CmdIter, E, [&](const std::unique_ptr<BaseCommand> &Base) { 764aab6d5c5SRafael Espindola auto *Cmd = dyn_cast<OutputSectionCommand>(Base.get()); 765aab6d5c5SRafael Espindola return Cmd && Cmd->Name == Name; 766aab6d5c5SRafael Espindola }); 767aab6d5c5SRafael Espindola if (Pos == E) { 768aab6d5c5SRafael Espindola Opt.Commands.insert(CmdIter, 769aab6d5c5SRafael Espindola llvm::make_unique<OutputSectionCommand>(Name)); 770aab6d5c5SRafael Espindola ++CmdIndex; 77115c57951SRafael Espindola continue; 77215c57951SRafael Espindola } 77315c57951SRafael Espindola 77415c57951SRafael Espindola // Continue from where we found it. 77515c57951SRafael Espindola CmdIndex = (Pos - Opt.Commands.begin()) + 1; 776652852c5SGeorge Rimar } 777337f903cSRafael Espindola } 778337f903cSRafael Espindola 779337f903cSRafael Espindola template <class ELFT> 78017cb7c0aSRafael Espindola void LinkerScript<ELFT>::assignAddresses(std::vector<PhdrEntry> &Phdrs) { 7817c18c28cSRui Ueyama // Assign addresses as instructed by linker script SECTIONS sub-commands. 782be607334SRafael Espindola Dot = 0; 783652852c5SGeorge Rimar 78406f4743aSRafael Espindola // A symbol can be assigned before any section is mentioned in the linker 78506f4743aSRafael Espindola // script. In an DSO, the symbol values are addresses, so the only important 78606f4743aSRafael Espindola // section values are: 78706f4743aSRafael Espindola // * SHN_UNDEF 78806f4743aSRafael Espindola // * SHN_ABS 78906f4743aSRafael Espindola // * Any value meaning a regular section. 79006f4743aSRafael Espindola // To handle that, create a dummy aether section that fills the void before 79106f4743aSRafael Espindola // the linker scripts switches to another section. It has an index of one 79206f4743aSRafael Espindola // which will map to whatever the first actual section is. 79306f4743aSRafael Espindola auto *Aether = make<OutputSectionBase>("", 0, SHF_ALLOC); 79406f4743aSRafael Espindola Aether->SectionIndex = 1; 79506f4743aSRafael Espindola switchTo(Aether); 79606f4743aSRafael Espindola 797076fe157SGeorge Rimar for (const std::unique_ptr<BaseCommand> &Base : Opt.Commands) { 798076fe157SGeorge Rimar if (auto *Cmd = dyn_cast<SymbolAssignment>(Base.get())) { 7994cd7352cSRafael Espindola assignSymbol(Cmd); 80005ef4cffSRui Ueyama continue; 801652852c5SGeorge Rimar } 802652852c5SGeorge Rimar 803eefa758eSGeorge Rimar if (auto *Cmd = dyn_cast<AssertCommand>(Base.get())) { 804eefa758eSGeorge Rimar Cmd->Expression(Dot); 805eefa758eSGeorge Rimar continue; 806eefa758eSGeorge Rimar } 807eefa758eSGeorge Rimar 808076fe157SGeorge Rimar auto *Cmd = cast<OutputSectionCommand>(Base.get()); 809d3190795SRafael Espindola assignOffsets(Cmd); 810a14b13d8SGeorge Rimar } 811467c4d55SEugene Leviant 812aab6d5c5SRafael Espindola uintX_t MinVA = std::numeric_limits<uintX_t>::max(); 813ea590d91SRafael Espindola for (OutputSectionBase *Sec : *OutputSections) { 81404a2e348SRafael Espindola if (Sec->Flags & SHF_ALLOC) 815e08e78dfSRafael Espindola MinVA = std::min<uint64_t>(MinVA, Sec->Addr); 816ea590d91SRafael Espindola else 817ea590d91SRafael Espindola Sec->Addr = 0; 818ea590d91SRafael Espindola } 819aab6d5c5SRafael Espindola 8208c495e20SRafael Espindola allocateHeaders<ELFT>(Phdrs, *OutputSections, MinVA); 821fb8978fcSDima Stepanov } 822652852c5SGeorge Rimar 823464daadcSRui Ueyama // Creates program headers as instructed by PHDRS linker script command. 82417cb7c0aSRafael Espindola template <class ELFT> std::vector<PhdrEntry> LinkerScript<ELFT>::createPhdrs() { 82517cb7c0aSRafael Espindola std::vector<PhdrEntry> Ret; 826bbe38602SEugene Leviant 827464daadcSRui Ueyama // Process PHDRS and FILEHDR keywords because they are not 828464daadcSRui Ueyama // real output sections and cannot be added in the following loop. 829bbe38602SEugene Leviant for (const PhdrsCommand &Cmd : Opt.PhdrsCommands) { 830edebbdf1SRui Ueyama Ret.emplace_back(Cmd.Type, Cmd.Flags == UINT_MAX ? PF_R : Cmd.Flags); 83117cb7c0aSRafael Espindola PhdrEntry &Phdr = Ret.back(); 832bbe38602SEugene Leviant 833bbe38602SEugene Leviant if (Cmd.HasFilehdr) 834adca245fSRui Ueyama Phdr.add(Out<ELFT>::ElfHeader); 835bbe38602SEugene Leviant if (Cmd.HasPhdrs) 836adca245fSRui Ueyama Phdr.add(Out<ELFT>::ProgramHeaders); 83756b21c86SEugene Leviant 83856b21c86SEugene Leviant if (Cmd.LMAExpr) { 83917cb7c0aSRafael Espindola Phdr.p_paddr = Cmd.LMAExpr(0); 84056b21c86SEugene Leviant Phdr.HasLMA = true; 84156b21c86SEugene Leviant } 842bbe38602SEugene Leviant } 843bbe38602SEugene Leviant 844464daadcSRui Ueyama // Add output sections to program headers. 845e08e78dfSRafael Espindola for (OutputSectionBase *Sec : *OutputSections) { 84604a2e348SRafael Espindola if (!(Sec->Flags & SHF_ALLOC)) 847bbe38602SEugene Leviant break; 848bbe38602SEugene Leviant 849bbe38602SEugene Leviant // Assign headers specified by linker script 850f7a17448SRafael Espindola for (size_t Id : getPhdrIndices(Sec->getName())) { 851edebbdf1SRui Ueyama Ret[Id].add(Sec); 852865bf863SEugene Leviant if (Opt.PhdrsCommands[Id].Flags == UINT_MAX) 85317cb7c0aSRafael Espindola Ret[Id].p_flags |= Sec->getPhdrFlags(); 854bbe38602SEugene Leviant } 855bbe38602SEugene Leviant } 856edebbdf1SRui Ueyama return Ret; 857bbe38602SEugene Leviant } 858bbe38602SEugene Leviant 859f9bc3bd2SEugene Leviant template <class ELFT> bool LinkerScript<ELFT>::ignoreInterpSection() { 860f9bc3bd2SEugene Leviant // Ignore .interp section in case we have PHDRS specification 861f9bc3bd2SEugene Leviant // and PT_INTERP isn't listed. 862f9bc3bd2SEugene Leviant return !Opt.PhdrsCommands.empty() && 863f9bc3bd2SEugene Leviant llvm::find_if(Opt.PhdrsCommands, [](const PhdrsCommand &Cmd) { 864f9bc3bd2SEugene Leviant return Cmd.Type == PT_INTERP; 865f9bc3bd2SEugene Leviant }) == Opt.PhdrsCommands.end(); 866f9bc3bd2SEugene Leviant } 867f9bc3bd2SEugene Leviant 86893c64025SGeorge Rimar template <class ELFT> uint32_t LinkerScript<ELFT>::getFiller(StringRef Name) { 869f6c3ccefSGeorge Rimar for (const std::unique_ptr<BaseCommand> &Base : Opt.Commands) 870f6c3ccefSGeorge Rimar if (auto *Cmd = dyn_cast<OutputSectionCommand>(Base.get())) 871f6c3ccefSGeorge Rimar if (Cmd->Name == Name) 872f6c3ccefSGeorge Rimar return Cmd->Filler; 87316068aebSRui Ueyama return 0; 874e2ee72b5SGeorge Rimar } 875e2ee72b5SGeorge Rimar 876e38cbab5SGeorge Rimar template <class ELFT> 877e38cbab5SGeorge Rimar static void writeInt(uint8_t *Buf, uint64_t Data, uint64_t Size) { 878e38cbab5SGeorge Rimar const endianness E = ELFT::TargetEndianness; 879e38cbab5SGeorge Rimar 880e38cbab5SGeorge Rimar switch (Size) { 881e38cbab5SGeorge Rimar case 1: 882e38cbab5SGeorge Rimar *Buf = (uint8_t)Data; 883e38cbab5SGeorge Rimar break; 884e38cbab5SGeorge Rimar case 2: 885e38cbab5SGeorge Rimar write16<E>(Buf, Data); 886e38cbab5SGeorge Rimar break; 887e38cbab5SGeorge Rimar case 4: 888e38cbab5SGeorge Rimar write32<E>(Buf, Data); 889e38cbab5SGeorge Rimar break; 890e38cbab5SGeorge Rimar case 8: 891e38cbab5SGeorge Rimar write64<E>(Buf, Data); 892e38cbab5SGeorge Rimar break; 893e38cbab5SGeorge Rimar default: 894e38cbab5SGeorge Rimar llvm_unreachable("unsupported Size argument"); 895e38cbab5SGeorge Rimar } 896e38cbab5SGeorge Rimar } 897e38cbab5SGeorge Rimar 898e38cbab5SGeorge Rimar template <class ELFT> 899e38cbab5SGeorge Rimar void LinkerScript<ELFT>::writeDataBytes(StringRef Name, uint8_t *Buf) { 900e38cbab5SGeorge Rimar int I = getSectionIndex(Name); 901e38cbab5SGeorge Rimar if (I == INT_MAX) 902e38cbab5SGeorge Rimar return; 903e38cbab5SGeorge Rimar 9046e68c5e5SRui Ueyama auto *Cmd = dyn_cast<OutputSectionCommand>(Opt.Commands[I].get()); 9056e68c5e5SRui Ueyama for (const std::unique_ptr<BaseCommand> &Base : Cmd->Commands) 9066e68c5e5SRui Ueyama if (auto *Data = dyn_cast<BytesDataCommand>(Base.get())) 90795c7d8d2SMeador Inge writeInt<ELFT>(Buf + Data->Offset, Data->Expression(0), Data->Size); 908e38cbab5SGeorge Rimar } 909e38cbab5SGeorge Rimar 910b71d6f7aSEugene Leviant template <class ELFT> bool LinkerScript<ELFT>::hasLMA(StringRef Name) { 9118ceadb38SGeorge Rimar for (const std::unique_ptr<BaseCommand> &Base : Opt.Commands) 9128ceadb38SGeorge Rimar if (auto *Cmd = dyn_cast<OutputSectionCommand>(Base.get())) 913b71d6f7aSEugene Leviant if (Cmd->LMAExpr && Cmd->Name == Name) 914b71d6f7aSEugene Leviant return true; 915b71d6f7aSEugene Leviant return false; 9168ceadb38SGeorge Rimar } 9178ceadb38SGeorge Rimar 918c3e2a4b0SRui Ueyama // Returns the index of the given section name in linker script 919c3e2a4b0SRui Ueyama // SECTIONS commands. Sections are laid out as the same order as they 920c3e2a4b0SRui Ueyama // were in the script. If a given name did not appear in the script, 921c3e2a4b0SRui Ueyama // it returns INT_MAX, so that it will be laid out at end of file. 922076fe157SGeorge Rimar template <class ELFT> int LinkerScript<ELFT>::getSectionIndex(StringRef Name) { 9236e68c5e5SRui Ueyama for (int I = 0, E = Opt.Commands.size(); I != E; ++I) 9246e68c5e5SRui Ueyama if (auto *Cmd = dyn_cast<OutputSectionCommand>(Opt.Commands[I].get())) 925076fe157SGeorge Rimar if (Cmd->Name == Name) 926f510fa6bSRui Ueyama return I; 927f510fa6bSRui Ueyama return INT_MAX; 92871b26e94SGeorge Rimar } 92971b26e94SGeorge Rimar 930bbe38602SEugene Leviant template <class ELFT> bool LinkerScript<ELFT>::hasPhdrsCommands() { 931bbe38602SEugene Leviant return !Opt.PhdrsCommands.empty(); 932bbe38602SEugene Leviant } 933bbe38602SEugene Leviant 9349e69450eSGeorge Rimar template <class ELFT> 935ed30ce7aSEugene Leviant const OutputSectionBase *LinkerScript<ELFT>::getOutputSection(const Twine &Loc, 936ed30ce7aSEugene Leviant StringRef Name) { 937afaa9343SEugene Leviant static OutputSectionBase FakeSec("", 0, 0); 93896659df0SGeorge Rimar 939e08e78dfSRafael Espindola for (OutputSectionBase *Sec : *OutputSections) 940b71d6f7aSEugene Leviant if (Sec->getName() == Name) 941afaa9343SEugene Leviant return Sec; 942ed30ce7aSEugene Leviant 943ed30ce7aSEugene Leviant error(Loc + ": undefined section " + Name); 944afaa9343SEugene Leviant return &FakeSec; 94536fac7f0SEugene Leviant } 94636fac7f0SEugene Leviant 947edf75e79SRui Ueyama // This function is essentially the same as getOutputSection(Name)->Size, 948edf75e79SRui Ueyama // but it won't print out an error message if a given section is not found. 949edf75e79SRui Ueyama // 950edf75e79SRui Ueyama // Linker script does not create an output section if its content is empty. 951edf75e79SRui Ueyama // We want to allow SIZEOF(.foo) where .foo is a section which happened to 952edf75e79SRui Ueyama // be empty. That is why this function is different from getOutputSection(). 953edf75e79SRui Ueyama template <class ELFT> 954edf75e79SRui Ueyama uint64_t LinkerScript<ELFT>::getOutputSectionSize(StringRef Name) { 955edf75e79SRui Ueyama for (OutputSectionBase *Sec : *OutputSections) 956edf75e79SRui Ueyama if (Sec->getName() == Name) 957edf75e79SRui Ueyama return Sec->Size; 958edf75e79SRui Ueyama return 0; 959edf75e79SRui Ueyama } 960edf75e79SRui Ueyama 961884e786dSGeorge Rimar template <class ELFT> uint64_t LinkerScript<ELFT>::getHeaderSize() { 9620d4b6d5cSRafael Espindola return elf::getHeaderSize<ELFT>(); 963e32a3598SGeorge Rimar } 964e32a3598SGeorge Rimar 965f6aeed36SEugene Leviant template <class ELFT> 966f6aeed36SEugene Leviant uint64_t LinkerScript<ELFT>::getSymbolValue(const Twine &Loc, StringRef S) { 967884e786dSGeorge Rimar if (SymbolBody *B = Symtab<ELFT>::X->find(S)) 968884e786dSGeorge Rimar return B->getVA<ELFT>(); 969f6aeed36SEugene Leviant error(Loc + ": symbol not found: " + S); 970884e786dSGeorge Rimar return 0; 971884e786dSGeorge Rimar } 972884e786dSGeorge Rimar 973f34f45fdSGeorge Rimar template <class ELFT> bool LinkerScript<ELFT>::isDefined(StringRef S) { 974f34f45fdSGeorge Rimar return Symtab<ELFT>::X->find(S) != nullptr; 975f34f45fdSGeorge Rimar } 976f34f45fdSGeorge Rimar 9772f831dcaSRafael Espindola template <class ELFT> bool LinkerScript<ELFT>::isAbsolute(StringRef S) { 9782f831dcaSRafael Espindola SymbolBody *Sym = Symtab<ELFT>::X->find(S); 9792f831dcaSRafael Espindola auto *DR = dyn_cast_or_null<DefinedRegular<ELFT>>(Sym); 9802f831dcaSRafael Espindola return DR && !DR->Section; 9812f831dcaSRafael Espindola } 9822f831dcaSRafael Espindola 983afaa9343SEugene Leviant // Gets section symbol belongs to. Symbol "." doesn't belong to any 984afaa9343SEugene Leviant // specific section but isn't absolute at the same time, so we try 985afaa9343SEugene Leviant // to find suitable section for it as well. 986afaa9343SEugene Leviant template <class ELFT> 987afaa9343SEugene Leviant const OutputSectionBase *LinkerScript<ELFT>::getSymbolSection(StringRef S) { 98806f4743aSRafael Espindola if (SymbolBody *Sym = Symtab<ELFT>::X->find(S)) 98960aed443SGeorge Rimar return SymbolTableSection<ELFT>::getOutputSection(Sym); 99006f4743aSRafael Espindola return CurOutSec; 991afaa9343SEugene Leviant } 992afaa9343SEugene Leviant 993bbe38602SEugene Leviant // Returns indices of ELF headers containing specific section, identified 994bbe38602SEugene Leviant // by Name. Each index is a zero based number of ELF header listed within 995bbe38602SEugene Leviant // PHDRS {} script block. 996bbe38602SEugene Leviant template <class ELFT> 997edebbdf1SRui Ueyama std::vector<size_t> LinkerScript<ELFT>::getPhdrIndices(StringRef SectionName) { 998076fe157SGeorge Rimar for (const std::unique_ptr<BaseCommand> &Base : Opt.Commands) { 999076fe157SGeorge Rimar auto *Cmd = dyn_cast<OutputSectionCommand>(Base.get()); 1000edebbdf1SRui Ueyama if (!Cmd || Cmd->Name != SectionName) 100131d842f5SGeorge Rimar continue; 100231d842f5SGeorge Rimar 100329c5a2a9SRui Ueyama std::vector<size_t> Ret; 100429c5a2a9SRui Ueyama for (StringRef PhdrName : Cmd->Phdrs) 10052a942c4bSEugene Leviant Ret.push_back(getPhdrIndex(Cmd->Location, PhdrName)); 100629c5a2a9SRui Ueyama return Ret; 1007bbe38602SEugene Leviant } 100831d842f5SGeorge Rimar return {}; 100931d842f5SGeorge Rimar } 1010bbe38602SEugene Leviant 101129c5a2a9SRui Ueyama template <class ELFT> 10122a942c4bSEugene Leviant size_t LinkerScript<ELFT>::getPhdrIndex(const Twine &Loc, StringRef PhdrName) { 101329c5a2a9SRui Ueyama size_t I = 0; 101429c5a2a9SRui Ueyama for (PhdrsCommand &Cmd : Opt.PhdrsCommands) { 101529c5a2a9SRui Ueyama if (Cmd.Name == PhdrName) 101629c5a2a9SRui Ueyama return I; 101729c5a2a9SRui Ueyama ++I; 101829c5a2a9SRui Ueyama } 10192a942c4bSEugene Leviant error(Loc + ": section header '" + PhdrName + "' is not listed in PHDRS"); 102029c5a2a9SRui Ueyama return 0; 102129c5a2a9SRui Ueyama } 102229c5a2a9SRui Ueyama 1023794366a2SRui Ueyama class elf::ScriptParser final : public ScriptLexer { 1024c3794e58SGeorge Rimar typedef void (ScriptParser::*Handler)(); 1025c3794e58SGeorge Rimar 1026f7c5fbb1SRui Ueyama public: 102722375f24SRui Ueyama ScriptParser(MemoryBufferRef MB) 1028794366a2SRui Ueyama : ScriptLexer(MB), 102922375f24SRui Ueyama IsUnderSysroot(isUnderSysroot(MB.getBufferIdentifier())) {} 1030f23b2320SGeorge Rimar 103120b6598cSGeorge Rimar void readLinkerScript(); 103220b6598cSGeorge Rimar void readVersionScript(); 1033d0ebd84cSRafael Espindola void readDynamicList(); 1034f7c5fbb1SRui Ueyama 1035f7c5fbb1SRui Ueyama private: 103652a1509eSRui Ueyama void addFile(StringRef Path); 103752a1509eSRui Ueyama 1038f7c5fbb1SRui Ueyama void readAsNeeded(); 103990c5099eSDenis Protivensky void readEntry(); 104083f406cfSGeorge Rimar void readExtern(); 1041f7c5fbb1SRui Ueyama void readGroup(); 104231aa1f83SRui Ueyama void readInclude(); 1043b889744eSMeador Inge void readMemory(); 1044ee59282bSRui Ueyama void readOutput(); 10459159ce93SDavide Italiano void readOutputArch(); 1046f7c5fbb1SRui Ueyama void readOutputFormat(); 1047bbe38602SEugene Leviant void readPhdrs(); 104868a39a65SDavide Italiano void readSearchDir(); 10498e3b38abSDenis Protivensky void readSections(); 105095769b4aSRui Ueyama void readVersion(); 105195769b4aSRui Ueyama void readVersionScriptCommand(); 10528e3b38abSDenis Protivensky 1053113cdec9SRui Ueyama SymbolAssignment *readAssignment(StringRef Name); 1054e38cbab5SGeorge Rimar BytesDataCommand *readBytesDataCommand(StringRef Tok); 105516068aebSRui Ueyama uint32_t readFill(); 105610416564SRui Ueyama OutputSectionCommand *readOutputSectionDescription(StringRef OutSec); 105716068aebSRui Ueyama uint32_t readOutputSectionFiller(StringRef Tok); 1058bbe38602SEugene Leviant std::vector<StringRef> readOutputSectionPhdrs(); 1059a2496cbeSGeorge Rimar InputSectionDescription *readInputSectionDescription(StringRef Tok); 1060db688454SEugene Leviant StringMatcher readFilePatterns(); 106107171f21SGeorge Rimar std::vector<SectionPattern> readInputSectionsList(); 1062a2496cbeSGeorge Rimar InputSectionDescription *readInputSectionRules(StringRef FilePattern); 1063bbe38602SEugene Leviant unsigned readPhdrType(); 1064be394db3SGeorge Rimar SortSectionPolicy readSortKind(); 1065a35e39caSPetr Hosek SymbolAssignment *readProvideHidden(bool Provide, bool Hidden); 1066c96da110SRafael Espindola SymbolAssignment *readProvideOrAssignment(StringRef Tok); 106703fc010eSGeorge Rimar void readSort(); 1068eefa758eSGeorge Rimar Expr readAssert(); 1069708019c4SRui Ueyama 107024e626ccSRui Ueyama uint64_t readMemoryAssignment(StringRef, StringRef, StringRef); 107124e626ccSRui Ueyama std::pair<uint32_t, uint32_t> readMemoryAttributes(); 107224e626ccSRui Ueyama 1073708019c4SRui Ueyama Expr readExpr(); 1074708019c4SRui Ueyama Expr readExpr1(Expr Lhs, int MinPrec); 1075b71d6f7aSEugene Leviant StringRef readParenLiteral(); 1076708019c4SRui Ueyama Expr readPrimary(); 1077708019c4SRui Ueyama Expr readTernary(Expr Cond); 10786ad7dfccSRui Ueyama Expr readParenExpr(); 1079f7c5fbb1SRui Ueyama 108020b6598cSGeorge Rimar // For parsing version script. 108112450b20SRui Ueyama std::vector<SymbolVersion> readVersionExtern(); 108212450b20SRui Ueyama void readAnonymousDeclaration(); 108395769b4aSRui Ueyama void readVersionDeclaration(StringRef VerStr); 108412450b20SRui Ueyama std::vector<SymbolVersion> readSymbols(); 1085e999ddb8SRafael Espindola void readLocals(); 108620b6598cSGeorge Rimar 108707320e40SRui Ueyama ScriptConfiguration &Opt = *ScriptConfig; 108816b0cc9eSSimon Atanasyan bool IsUnderSysroot; 1089f7c5fbb1SRui Ueyama }; 1090f7c5fbb1SRui Ueyama 1091d0ebd84cSRafael Espindola void ScriptParser::readDynamicList() { 1092d0ebd84cSRafael Espindola expect("{"); 1093d0ebd84cSRafael Espindola readAnonymousDeclaration(); 1094d0ebd84cSRafael Espindola if (!atEOF()) 1095d0ebd84cSRafael Espindola setError("EOF expected, but got " + next()); 1096d0ebd84cSRafael Espindola } 1097d0ebd84cSRafael Espindola 109820b6598cSGeorge Rimar void ScriptParser::readVersionScript() { 109995769b4aSRui Ueyama readVersionScriptCommand(); 110020b6598cSGeorge Rimar if (!atEOF()) 110195769b4aSRui Ueyama setError("EOF expected, but got " + next()); 110295769b4aSRui Ueyama } 110395769b4aSRui Ueyama 110495769b4aSRui Ueyama void ScriptParser::readVersionScriptCommand() { 110583043f23SRui Ueyama if (consume("{")) { 110612450b20SRui Ueyama readAnonymousDeclaration(); 110720b6598cSGeorge Rimar return; 110820b6598cSGeorge Rimar } 110920b6598cSGeorge Rimar 111095769b4aSRui Ueyama while (!atEOF() && !Error && peek() != "}") { 111120b6598cSGeorge Rimar StringRef VerStr = next(); 111220b6598cSGeorge Rimar if (VerStr == "{") { 111395769b4aSRui Ueyama setError("anonymous version definition is used in " 111495769b4aSRui Ueyama "combination with other version definitions"); 111520b6598cSGeorge Rimar return; 111620b6598cSGeorge Rimar } 111720b6598cSGeorge Rimar expect("{"); 111895769b4aSRui Ueyama readVersionDeclaration(VerStr); 111920b6598cSGeorge Rimar } 112020b6598cSGeorge Rimar } 112120b6598cSGeorge Rimar 112295769b4aSRui Ueyama void ScriptParser::readVersion() { 112395769b4aSRui Ueyama expect("{"); 112495769b4aSRui Ueyama readVersionScriptCommand(); 112595769b4aSRui Ueyama expect("}"); 112695769b4aSRui Ueyama } 112795769b4aSRui Ueyama 112820b6598cSGeorge Rimar void ScriptParser::readLinkerScript() { 1129f7c5fbb1SRui Ueyama while (!atEOF()) { 1130f7c5fbb1SRui Ueyama StringRef Tok = next(); 1131a27eeccaSRui Ueyama if (Tok == ";") 1132a27eeccaSRui Ueyama continue; 1133a27eeccaSRui Ueyama 113420d03194SEugene Leviant if (Tok == "ASSERT") { 113520d03194SEugene Leviant Opt.Commands.emplace_back(new AssertCommand(readAssert())); 113620d03194SEugene Leviant } else if (Tok == "ENTRY") { 1137a27eeccaSRui Ueyama readEntry(); 1138a27eeccaSRui Ueyama } else if (Tok == "EXTERN") { 1139a27eeccaSRui Ueyama readExtern(); 1140a27eeccaSRui Ueyama } else if (Tok == "GROUP" || Tok == "INPUT") { 1141a27eeccaSRui Ueyama readGroup(); 1142a27eeccaSRui Ueyama } else if (Tok == "INCLUDE") { 1143a27eeccaSRui Ueyama readInclude(); 1144b889744eSMeador Inge } else if (Tok == "MEMORY") { 1145b889744eSMeador Inge readMemory(); 1146a27eeccaSRui Ueyama } else if (Tok == "OUTPUT") { 1147a27eeccaSRui Ueyama readOutput(); 1148a27eeccaSRui Ueyama } else if (Tok == "OUTPUT_ARCH") { 1149a27eeccaSRui Ueyama readOutputArch(); 1150a27eeccaSRui Ueyama } else if (Tok == "OUTPUT_FORMAT") { 1151a27eeccaSRui Ueyama readOutputFormat(); 1152a27eeccaSRui Ueyama } else if (Tok == "PHDRS") { 1153a27eeccaSRui Ueyama readPhdrs(); 1154a27eeccaSRui Ueyama } else if (Tok == "SEARCH_DIR") { 1155a27eeccaSRui Ueyama readSearchDir(); 1156a27eeccaSRui Ueyama } else if (Tok == "SECTIONS") { 1157a27eeccaSRui Ueyama readSections(); 1158a27eeccaSRui Ueyama } else if (Tok == "VERSION") { 1159a27eeccaSRui Ueyama readVersion(); 1160c96da110SRafael Espindola } else if (SymbolAssignment *Cmd = readProvideOrAssignment(Tok)) { 11610df80befSPetr Hosek Opt.Commands.emplace_back(Cmd); 1162e5d3ca50SPetr Hosek } else { 11635761042dSGeorge Rimar setError("unknown directive: " + Tok); 1164f7c5fbb1SRui Ueyama } 1165f7c5fbb1SRui Ueyama } 1166e5d3ca50SPetr Hosek } 1167f7c5fbb1SRui Ueyama 1168717677afSRui Ueyama void ScriptParser::addFile(StringRef S) { 116916b0cc9eSSimon Atanasyan if (IsUnderSysroot && S.startswith("/")) { 11705af1687fSJustin Bogner SmallString<128> PathData; 11715af1687fSJustin Bogner StringRef Path = (Config->Sysroot + S).toStringRef(PathData); 117216b0cc9eSSimon Atanasyan if (sys::fs::exists(Path)) { 11735af1687fSJustin Bogner Driver->addFile(Saver.save(Path)); 117416b0cc9eSSimon Atanasyan return; 117516b0cc9eSSimon Atanasyan } 117616b0cc9eSSimon Atanasyan } 117716b0cc9eSSimon Atanasyan 1178f03f3cc1SRui Ueyama if (sys::path::is_absolute(S)) { 117952a1509eSRui Ueyama Driver->addFile(S); 118052a1509eSRui Ueyama } else if (S.startswith("=")) { 118152a1509eSRui Ueyama if (Config->Sysroot.empty()) 118252a1509eSRui Ueyama Driver->addFile(S.substr(1)); 118352a1509eSRui Ueyama else 118452a1509eSRui Ueyama Driver->addFile(Saver.save(Config->Sysroot + "/" + S.substr(1))); 118552a1509eSRui Ueyama } else if (S.startswith("-l")) { 118621eecb4fSRui Ueyama Driver->addLibrary(S.substr(2)); 1187a1b8fc3bSSimon Atanasyan } else if (sys::fs::exists(S)) { 1188a1b8fc3bSSimon Atanasyan Driver->addFile(S); 118952a1509eSRui Ueyama } else { 1190061f9286SRui Ueyama if (Optional<std::string> Path = findFromSearchPaths(S)) 1191061f9286SRui Ueyama Driver->addFile(Saver.save(*Path)); 1192025d59b1SRui Ueyama else 1193061f9286SRui Ueyama setError("unable to find " + S); 119452a1509eSRui Ueyama } 119552a1509eSRui Ueyama } 119652a1509eSRui Ueyama 1197717677afSRui Ueyama void ScriptParser::readAsNeeded() { 1198f7c5fbb1SRui Ueyama expect("("); 119935da9b6eSRui Ueyama bool Orig = Config->AsNeeded; 120035da9b6eSRui Ueyama Config->AsNeeded = true; 120183043f23SRui Ueyama while (!Error && !consume(")")) 1202cd574a5eSGeorge Rimar addFile(unquote(next())); 120335da9b6eSRui Ueyama Config->AsNeeded = Orig; 1204f7c5fbb1SRui Ueyama } 1205f7c5fbb1SRui Ueyama 1206717677afSRui Ueyama void ScriptParser::readEntry() { 120790c5099eSDenis Protivensky // -e <symbol> takes predecence over ENTRY(<symbol>). 120890c5099eSDenis Protivensky expect("("); 120990c5099eSDenis Protivensky StringRef Tok = next(); 121090c5099eSDenis Protivensky if (Config->Entry.empty()) 121190c5099eSDenis Protivensky Config->Entry = Tok; 121290c5099eSDenis Protivensky expect(")"); 121390c5099eSDenis Protivensky } 121490c5099eSDenis Protivensky 1215717677afSRui Ueyama void ScriptParser::readExtern() { 121683f406cfSGeorge Rimar expect("("); 121783043f23SRui Ueyama while (!Error && !consume(")")) 1218a2acc931SRui Ueyama Config->Undefined.push_back(next()); 121983f406cfSGeorge Rimar } 122083f406cfSGeorge Rimar 1221717677afSRui Ueyama void ScriptParser::readGroup() { 1222f7c5fbb1SRui Ueyama expect("("); 122383043f23SRui Ueyama while (!Error && !consume(")")) { 1224f7c5fbb1SRui Ueyama StringRef Tok = next(); 1225a2acc931SRui Ueyama if (Tok == "AS_NEEDED") 1226f7c5fbb1SRui Ueyama readAsNeeded(); 1227a2acc931SRui Ueyama else 1228cd574a5eSGeorge Rimar addFile(unquote(Tok)); 1229f7c5fbb1SRui Ueyama } 1230f7c5fbb1SRui Ueyama } 1231f7c5fbb1SRui Ueyama 1232717677afSRui Ueyama void ScriptParser::readInclude() { 1233d4500653SGeorge Rimar StringRef Tok = unquote(next()); 1234ec1c75e0SRui Ueyama 1235d4500653SGeorge Rimar // https://sourceware.org/binutils/docs/ld/File-Commands.html: 1236d4500653SGeorge Rimar // The file will be searched for in the current directory, and in any 1237d4500653SGeorge Rimar // directory specified with the -L option. 1238ec1c75e0SRui Ueyama if (sys::fs::exists(Tok)) { 1239ec1c75e0SRui Ueyama if (Optional<MemoryBufferRef> MB = readFile(Tok)) 1240ec1c75e0SRui Ueyama tokenize(*MB); 1241025d59b1SRui Ueyama return; 1242025d59b1SRui Ueyama } 1243ec1c75e0SRui Ueyama if (Optional<std::string> Path = findFromSearchPaths(Tok)) { 1244ec1c75e0SRui Ueyama if (Optional<MemoryBufferRef> MB = readFile(*Path)) 1245ec1c75e0SRui Ueyama tokenize(*MB); 1246ec1c75e0SRui Ueyama return; 1247ec1c75e0SRui Ueyama } 1248ec1c75e0SRui Ueyama setError("cannot open " + Tok); 124931aa1f83SRui Ueyama } 125031aa1f83SRui Ueyama 1251717677afSRui Ueyama void ScriptParser::readOutput() { 1252ee59282bSRui Ueyama // -o <file> takes predecence over OUTPUT(<file>). 1253ee59282bSRui Ueyama expect("("); 1254ee59282bSRui Ueyama StringRef Tok = next(); 1255ee59282bSRui Ueyama if (Config->OutputFile.empty()) 1256cd574a5eSGeorge Rimar Config->OutputFile = unquote(Tok); 1257ee59282bSRui Ueyama expect(")"); 1258ee59282bSRui Ueyama } 1259ee59282bSRui Ueyama 1260717677afSRui Ueyama void ScriptParser::readOutputArch() { 12614e01c3e8SGeorge Rimar // OUTPUT_ARCH is ignored for now. 12629159ce93SDavide Italiano expect("("); 12634e01c3e8SGeorge Rimar while (!Error && !consume(")")) 12645424e7c7SJustin Bogner skip(); 12659159ce93SDavide Italiano } 12669159ce93SDavide Italiano 1267717677afSRui Ueyama void ScriptParser::readOutputFormat() { 1268f7c5fbb1SRui Ueyama // Error checking only for now. 1269f7c5fbb1SRui Ueyama expect("("); 12705424e7c7SJustin Bogner skip(); 12716836c618SDavide Italiano StringRef Tok = next(); 12726836c618SDavide Italiano if (Tok == ")") 12736836c618SDavide Italiano return; 1274025d59b1SRui Ueyama if (Tok != ",") { 12755761042dSGeorge Rimar setError("unexpected token: " + Tok); 1276025d59b1SRui Ueyama return; 1277025d59b1SRui Ueyama } 12785424e7c7SJustin Bogner skip(); 12796836c618SDavide Italiano expect(","); 12805424e7c7SJustin Bogner skip(); 1281f7c5fbb1SRui Ueyama expect(")"); 1282f7c5fbb1SRui Ueyama } 1283f7c5fbb1SRui Ueyama 1284bbe38602SEugene Leviant void ScriptParser::readPhdrs() { 1285bbe38602SEugene Leviant expect("{"); 128683043f23SRui Ueyama while (!Error && !consume("}")) { 1287bbe38602SEugene Leviant StringRef Tok = next(); 128856b21c86SEugene Leviant Opt.PhdrsCommands.push_back( 128956b21c86SEugene Leviant {Tok, PT_NULL, false, false, UINT_MAX, nullptr}); 1290bbe38602SEugene Leviant PhdrsCommand &PhdrCmd = Opt.PhdrsCommands.back(); 1291bbe38602SEugene Leviant 1292bbe38602SEugene Leviant PhdrCmd.Type = readPhdrType(); 1293bbe38602SEugene Leviant do { 1294bbe38602SEugene Leviant Tok = next(); 1295bbe38602SEugene Leviant if (Tok == ";") 1296bbe38602SEugene Leviant break; 1297bbe38602SEugene Leviant if (Tok == "FILEHDR") 1298bbe38602SEugene Leviant PhdrCmd.HasFilehdr = true; 1299bbe38602SEugene Leviant else if (Tok == "PHDRS") 1300bbe38602SEugene Leviant PhdrCmd.HasPhdrs = true; 130156b21c86SEugene Leviant else if (Tok == "AT") 130256b21c86SEugene Leviant PhdrCmd.LMAExpr = readParenExpr(); 1303865bf863SEugene Leviant else if (Tok == "FLAGS") { 1304865bf863SEugene Leviant expect("("); 1305eb685cd7SRafael Espindola // Passing 0 for the value of dot is a bit of a hack. It means that 1306eb685cd7SRafael Espindola // we accept expressions like ".|1". 1307eb685cd7SRafael Espindola PhdrCmd.Flags = readExpr()(0); 1308865bf863SEugene Leviant expect(")"); 1309865bf863SEugene Leviant } else 1310bbe38602SEugene Leviant setError("unexpected header attribute: " + Tok); 1311bbe38602SEugene Leviant } while (!Error); 1312bbe38602SEugene Leviant } 1313bbe38602SEugene Leviant } 1314bbe38602SEugene Leviant 1315717677afSRui Ueyama void ScriptParser::readSearchDir() { 131668a39a65SDavide Italiano expect("("); 131786c5fb82SRui Ueyama StringRef Tok = next(); 13186c7ad13fSRui Ueyama if (!Config->Nostdlib) 1319cd574a5eSGeorge Rimar Config->SearchPaths.push_back(unquote(Tok)); 132068a39a65SDavide Italiano expect(")"); 132168a39a65SDavide Italiano } 132268a39a65SDavide Italiano 1323717677afSRui Ueyama void ScriptParser::readSections() { 1324e05336ffSEugene Leviant Opt.HasSections = true; 132518a30962SGeorge Rimar // -no-rosegment is used to avoid placing read only non-executable sections in 132618a30962SGeorge Rimar // their own segment. We do the same if SECTIONS command is present in linker 132718a30962SGeorge Rimar // script. See comment for computeFlags(). 132818a30962SGeorge Rimar Config->SingleRoRx = true; 132918a30962SGeorge Rimar 13308e3b38abSDenis Protivensky expect("{"); 133183043f23SRui Ueyama while (!Error && !consume("}")) { 1332113cdec9SRui Ueyama StringRef Tok = next(); 1333c96da110SRafael Espindola BaseCommand *Cmd = readProvideOrAssignment(Tok); 1334ceabe80eSEugene Leviant if (!Cmd) { 1335ceabe80eSEugene Leviant if (Tok == "ASSERT") 1336eefa758eSGeorge Rimar Cmd = new AssertCommand(readAssert()); 1337ceabe80eSEugene Leviant else 133810416564SRui Ueyama Cmd = readOutputSectionDescription(Tok); 13398e3b38abSDenis Protivensky } 134010416564SRui Ueyama Opt.Commands.emplace_back(Cmd); 1341652852c5SGeorge Rimar } 1342708019c4SRui Ueyama } 13438e3b38abSDenis Protivensky 1344708019c4SRui Ueyama static int precedence(StringRef Op) { 1345708019c4SRui Ueyama return StringSwitch<int>(Op) 13460120e3f2SRui Ueyama .Cases("*", "/", 5) 13470120e3f2SRui Ueyama .Cases("+", "-", 4) 13480120e3f2SRui Ueyama .Cases("<<", ">>", 3) 13499c4ac5f2SRui Ueyama .Cases("<", "<=", ">", ">=", "==", "!=", 2) 13500120e3f2SRui Ueyama .Cases("&", "|", 1) 1351708019c4SRui Ueyama .Default(-1); 1352708019c4SRui Ueyama } 1353708019c4SRui Ueyama 1354db688454SEugene Leviant StringMatcher ScriptParser::readFilePatterns() { 135510416564SRui Ueyama std::vector<StringRef> V; 135683043f23SRui Ueyama while (!Error && !consume(")")) 135710416564SRui Ueyama V.push_back(next()); 1358f91282e1SRui Ueyama return StringMatcher(V); 13590702c4e8SGeorge Rimar } 13600702c4e8SGeorge Rimar 1361be394db3SGeorge Rimar SortSectionPolicy ScriptParser::readSortKind() { 136283043f23SRui Ueyama if (consume("SORT") || consume("SORT_BY_NAME")) 1363be394db3SGeorge Rimar return SortSectionPolicy::Name; 136483043f23SRui Ueyama if (consume("SORT_BY_ALIGNMENT")) 1365be394db3SGeorge Rimar return SortSectionPolicy::Alignment; 136683043f23SRui Ueyama if (consume("SORT_BY_INIT_PRIORITY")) 1367be394db3SGeorge Rimar return SortSectionPolicy::Priority; 136883043f23SRui Ueyama if (consume("SORT_NONE")) 1369be394db3SGeorge Rimar return SortSectionPolicy::None; 1370b2a0abdfSRui Ueyama return SortSectionPolicy::Default; 1371be394db3SGeorge Rimar } 1372be394db3SGeorge Rimar 1373395281cfSGeorge Rimar // Method reads a list of sequence of excluded files and section globs given in 1374395281cfSGeorge Rimar // a following form: ((EXCLUDE_FILE(file_pattern+))? section_pattern+)+ 1375395281cfSGeorge Rimar // Example: *(.foo.1 EXCLUDE_FILE (*a.o) .foo.2 EXCLUDE_FILE (*b.o) .foo.3) 1376af03be19SGeorge Rimar // The semantics of that is next: 1377af03be19SGeorge Rimar // * Include .foo.1 from every file. 1378af03be19SGeorge Rimar // * Include .foo.2 from every file but a.o 1379af03be19SGeorge Rimar // * Include .foo.3 from every file but b.o 138007171f21SGeorge Rimar std::vector<SectionPattern> ScriptParser::readInputSectionsList() { 138107171f21SGeorge Rimar std::vector<SectionPattern> Ret; 1382601e9898SGeorge Rimar while (!Error && peek() != ")") { 1383f91282e1SRui Ueyama StringMatcher ExcludeFilePat; 138483043f23SRui Ueyama if (consume("EXCLUDE_FILE")) { 1385395281cfSGeorge Rimar expect("("); 1386f91282e1SRui Ueyama ExcludeFilePat = readFilePatterns(); 1387395281cfSGeorge Rimar } 1388395281cfSGeorge Rimar 1389601e9898SGeorge Rimar std::vector<StringRef> V; 1390601e9898SGeorge Rimar while (!Error && peek() != ")" && peek() != "EXCLUDE_FILE") 1391395281cfSGeorge Rimar V.push_back(next()); 1392601e9898SGeorge Rimar 1393601e9898SGeorge Rimar if (!V.empty()) 1394f91282e1SRui Ueyama Ret.push_back({std::move(ExcludeFilePat), StringMatcher(V)}); 1395601e9898SGeorge Rimar else 1396601e9898SGeorge Rimar setError("section pattern is expected"); 1397395281cfSGeorge Rimar } 139807171f21SGeorge Rimar return Ret; 1399395281cfSGeorge Rimar } 1400395281cfSGeorge Rimar 1401f8f6f1e7SRui Ueyama // Reads contents of "SECTIONS" directive. That directive contains a 1402f8f6f1e7SRui Ueyama // list of glob patterns for input sections. The grammar is as follows. 1403f8f6f1e7SRui Ueyama // 1404f8f6f1e7SRui Ueyama // <patterns> ::= <section-list> 1405f8f6f1e7SRui Ueyama // | <sort> "(" <section-list> ")" 1406f8f6f1e7SRui Ueyama // | <sort> "(" <sort> "(" <section-list> ")" ")" 1407f8f6f1e7SRui Ueyama // 1408f8f6f1e7SRui Ueyama // <sort> ::= "SORT" | "SORT_BY_NAME" | "SORT_BY_ALIGNMENT" 1409f8f6f1e7SRui Ueyama // | "SORT_BY_INIT_PRIORITY" | "SORT_NONE" 1410f8f6f1e7SRui Ueyama // 1411f8f6f1e7SRui Ueyama // <section-list> is parsed by readInputSectionsList(). 1412a2496cbeSGeorge Rimar InputSectionDescription * 1413a2496cbeSGeorge Rimar ScriptParser::readInputSectionRules(StringRef FilePattern) { 1414c91930a1SGeorge Rimar auto *Cmd = new InputSectionDescription(FilePattern); 14150ed42b0cSDavide Italiano expect("("); 1416f373dd76SRui Ueyama while (!Error && !consume(")")) { 141707171f21SGeorge Rimar SortSectionPolicy Outer = readSortKind(); 141807171f21SGeorge Rimar SortSectionPolicy Inner = SortSectionPolicy::Default; 141907171f21SGeorge Rimar std::vector<SectionPattern> V; 142007171f21SGeorge Rimar if (Outer != SortSectionPolicy::Default) { 14210702c4e8SGeorge Rimar expect("("); 142207171f21SGeorge Rimar Inner = readSortKind(); 142307171f21SGeorge Rimar if (Inner != SortSectionPolicy::Default) { 1424350ece4eSGeorge Rimar expect("("); 142507171f21SGeorge Rimar V = readInputSectionsList(); 14260702c4e8SGeorge Rimar expect(")"); 1427350ece4eSGeorge Rimar } else { 142807171f21SGeorge Rimar V = readInputSectionsList(); 1429350ece4eSGeorge Rimar } 1430350ece4eSGeorge Rimar expect(")"); 143107171f21SGeorge Rimar } else { 143207171f21SGeorge Rimar V = readInputSectionsList(); 14330659800eSGeorge Rimar } 14340702c4e8SGeorge Rimar 143507171f21SGeorge Rimar for (SectionPattern &Pat : V) { 143607171f21SGeorge Rimar Pat.SortInner = Inner; 143707171f21SGeorge Rimar Pat.SortOuter = Outer; 143807171f21SGeorge Rimar } 143907171f21SGeorge Rimar 144007171f21SGeorge Rimar std::move(V.begin(), V.end(), std::back_inserter(Cmd->SectionPatterns)); 144107171f21SGeorge Rimar } 144210416564SRui Ueyama return Cmd; 14430659800eSGeorge Rimar } 14440659800eSGeorge Rimar 1445a2496cbeSGeorge Rimar InputSectionDescription * 1446a2496cbeSGeorge Rimar ScriptParser::readInputSectionDescription(StringRef Tok) { 14470659800eSGeorge Rimar // Input section wildcard can be surrounded by KEEP. 14480659800eSGeorge Rimar // https://sourceware.org/binutils/docs/ld/Input-Section-Keep.html#Input-Section-Keep 1449a2496cbeSGeorge Rimar if (Tok == "KEEP") { 1450e7282797SDavide Italiano expect("("); 1451a2496cbeSGeorge Rimar StringRef FilePattern = next(); 1452a2496cbeSGeorge Rimar InputSectionDescription *Cmd = readInputSectionRules(FilePattern); 14530ed42b0cSDavide Italiano expect(")"); 1454cf43f179SEugene Leviant Opt.KeptSections.push_back(Cmd); 145510416564SRui Ueyama return Cmd; 145610416564SRui Ueyama } 1457a2496cbeSGeorge Rimar return readInputSectionRules(Tok); 14580659800eSGeorge Rimar } 14590659800eSGeorge Rimar 146003fc010eSGeorge Rimar void ScriptParser::readSort() { 146103fc010eSGeorge Rimar expect("("); 146203fc010eSGeorge Rimar expect("CONSTRUCTORS"); 146303fc010eSGeorge Rimar expect(")"); 146403fc010eSGeorge Rimar } 146503fc010eSGeorge Rimar 1466eefa758eSGeorge Rimar Expr ScriptParser::readAssert() { 1467eefa758eSGeorge Rimar expect("("); 1468eefa758eSGeorge Rimar Expr E = readExpr(); 1469eefa758eSGeorge Rimar expect(","); 1470cd574a5eSGeorge Rimar StringRef Msg = unquote(next()); 1471eefa758eSGeorge Rimar expect(")"); 1472eefa758eSGeorge Rimar return [=](uint64_t Dot) { 147360f1fe84SGeorge Rimar if (!E(Dot)) 1474eefa758eSGeorge Rimar error(Msg); 147560f1fe84SGeorge Rimar return Dot; 1476eefa758eSGeorge Rimar }; 1477eefa758eSGeorge Rimar } 1478eefa758eSGeorge Rimar 147925150e8bSRui Ueyama // Reads a FILL(expr) command. We handle the FILL command as an 148025150e8bSRui Ueyama // alias for =fillexp section attribute, which is different from 148125150e8bSRui Ueyama // what GNU linkers do. 148225150e8bSRui Ueyama // https://sourceware.org/binutils/docs/ld/Output-Section-Data.html 148316068aebSRui Ueyama uint32_t ScriptParser::readFill() { 1484ff1f29e0SGeorge Rimar expect("("); 148516068aebSRui Ueyama uint32_t V = readOutputSectionFiller(next()); 1486ff1f29e0SGeorge Rimar expect(")"); 1487ff1f29e0SGeorge Rimar expect(";"); 1488ff1f29e0SGeorge Rimar return V; 1489ff1f29e0SGeorge Rimar } 1490ff1f29e0SGeorge Rimar 149110416564SRui Ueyama OutputSectionCommand * 149210416564SRui Ueyama ScriptParser::readOutputSectionDescription(StringRef OutSec) { 1493076fe157SGeorge Rimar OutputSectionCommand *Cmd = new OutputSectionCommand(OutSec); 14942a942c4bSEugene Leviant Cmd->Location = getCurrentLocation(); 149558e5c4dcSGeorge Rimar 149658e5c4dcSGeorge Rimar // Read an address expression. 149758e5c4dcSGeorge Rimar // https://sourceware.org/binutils/docs/ld/Output-Section-Address.html#Output-Section-Address 149858e5c4dcSGeorge Rimar if (peek() != ":") 149958e5c4dcSGeorge Rimar Cmd->AddrExpr = readExpr(); 150058e5c4dcSGeorge Rimar 15018e3b38abSDenis Protivensky expect(":"); 1502246f681eSDavide Italiano 150383043f23SRui Ueyama if (consume("AT")) 1504b71d6f7aSEugene Leviant Cmd->LMAExpr = readParenExpr(); 150583043f23SRui Ueyama if (consume("ALIGN")) 15066ad7dfccSRui Ueyama Cmd->AlignExpr = readParenExpr(); 150783043f23SRui Ueyama if (consume("SUBALIGN")) 1508db24d9c3SGeorge Rimar Cmd->SubalignExpr = readParenExpr(); 1509630c6179SGeorge Rimar 1510246f681eSDavide Italiano // Parse constraints. 151183043f23SRui Ueyama if (consume("ONLY_IF_RO")) 1512efc4066bSRui Ueyama Cmd->Constraint = ConstraintKind::ReadOnly; 151383043f23SRui Ueyama if (consume("ONLY_IF_RW")) 1514efc4066bSRui Ueyama Cmd->Constraint = ConstraintKind::ReadWrite; 15158e3b38abSDenis Protivensky expect("{"); 15168ec77e64SRui Ueyama 151783043f23SRui Ueyama while (!Error && !consume("}")) { 1518ceabe80eSEugene Leviant StringRef Tok = next(); 15192fe07923SGeorge Rimar if (Tok == ";") { 152069750755SGeorge Rimar // Empty commands are allowed. Do nothing here. 15212fe07923SGeorge Rimar } else if (SymbolAssignment *Assignment = readProvideOrAssignment(Tok)) { 1522ceabe80eSEugene Leviant Cmd->Commands.emplace_back(Assignment); 1523b2d99d6aSMeador Inge } else if (BytesDataCommand *Data = readBytesDataCommand(Tok)) { 1524e38cbab5SGeorge Rimar Cmd->Commands.emplace_back(Data); 1525b2d99d6aSMeador Inge } else if (Tok == "ASSERT") { 1526b2d99d6aSMeador Inge Cmd->Commands.emplace_back(new AssertCommand(readAssert())); 1527b2d99d6aSMeador Inge expect(";"); 15288e2eca22SGeorge Rimar } else if (Tok == "CONSTRUCTORS") { 15298e2eca22SGeorge Rimar // CONSTRUCTORS is a keyword to make the linker recognize C++ ctors/dtors 15308e2eca22SGeorge Rimar // by name. This is for very old file formats such as ECOFF/XCOFF. 15318e2eca22SGeorge Rimar // For ELF, we should ignore. 1532b2d99d6aSMeador Inge } else if (Tok == "FILL") { 1533ff1f29e0SGeorge Rimar Cmd->Filler = readFill(); 1534b2d99d6aSMeador Inge } else if (Tok == "SORT") { 153503fc010eSGeorge Rimar readSort(); 1536b2d99d6aSMeador Inge } else if (peek() == "(") { 1537a2496cbeSGeorge Rimar Cmd->Commands.emplace_back(readInputSectionDescription(Tok)); 1538b2d99d6aSMeador Inge } else { 1539ceabe80eSEugene Leviant setError("unknown command " + Tok); 15408e3b38abSDenis Protivensky } 1541b2d99d6aSMeador Inge } 1542b889744eSMeador Inge 1543b889744eSMeador Inge if (consume(">")) 1544b889744eSMeador Inge Cmd->MemoryRegionName = next(); 1545b889744eSMeador Inge 1546076fe157SGeorge Rimar Cmd->Phdrs = readOutputSectionPhdrs(); 15474ebc5620SGeorge Rimar 154883043f23SRui Ueyama if (consume("=")) 15494ebc5620SGeorge Rimar Cmd->Filler = readOutputSectionFiller(next()); 15504ebc5620SGeorge Rimar else if (peek().startswith("=")) 1551ff1f29e0SGeorge Rimar Cmd->Filler = readOutputSectionFiller(next().drop_front()); 15524ebc5620SGeorge Rimar 15537185a1acSGeorge Rimar // Consume optional comma following output section command. 15547185a1acSGeorge Rimar consume(","); 15557185a1acSGeorge Rimar 155610416564SRui Ueyama return Cmd; 1557f71caa2bSRui Ueyama } 15588ec77e64SRui Ueyama 15592c8f1f04SRui Ueyama // Read "=<number>" where <number> is an octal/decimal/hexadecimal number. 15602c8f1f04SRui Ueyama // https://sourceware.org/binutils/docs/ld/Output-Section-Fill.html 15612c8f1f04SRui Ueyama // 15622c8f1f04SRui Ueyama // ld.gold is not fully compatible with ld.bfd. ld.bfd handles 15632c8f1f04SRui Ueyama // hexstrings as blobs of arbitrary sizes, while ld.gold handles them 15642c8f1f04SRui Ueyama // as 32-bit big-endian values. We will do the same as ld.gold does 15652c8f1f04SRui Ueyama // because it's simpler than what ld.bfd does. 156616068aebSRui Ueyama uint32_t ScriptParser::readOutputSectionFiller(StringRef Tok) { 1567965827d6SRui Ueyama uint32_t V; 156816068aebSRui Ueyama if (!Tok.getAsInteger(0, V)) 156916068aebSRui Ueyama return V; 1570965827d6SRui Ueyama setError("invalid filler expression: " + Tok); 157116068aebSRui Ueyama return 0; 15728e3b38abSDenis Protivensky } 15738e3b38abSDenis Protivensky 1574a35e39caSPetr Hosek SymbolAssignment *ScriptParser::readProvideHidden(bool Provide, bool Hidden) { 1575a31c91b1SEugene Leviant expect("("); 1576174e0a16SRui Ueyama SymbolAssignment *Cmd = readAssignment(next()); 1577a35e39caSPetr Hosek Cmd->Provide = Provide; 1578174e0a16SRui Ueyama Cmd->Hidden = Hidden; 1579a31c91b1SEugene Leviant expect(")"); 1580a31c91b1SEugene Leviant expect(";"); 158110416564SRui Ueyama return Cmd; 1582eda81a1bSEugene Leviant } 1583eda81a1bSEugene Leviant 1584c96da110SRafael Espindola SymbolAssignment *ScriptParser::readProvideOrAssignment(StringRef Tok) { 1585ceabe80eSEugene Leviant SymbolAssignment *Cmd = nullptr; 1586ceabe80eSEugene Leviant if (peek() == "=" || peek() == "+=") { 1587ceabe80eSEugene Leviant Cmd = readAssignment(Tok); 1588ceabe80eSEugene Leviant expect(";"); 1589ceabe80eSEugene Leviant } else if (Tok == "PROVIDE") { 1590a35e39caSPetr Hosek Cmd = readProvideHidden(true, false); 1591a35e39caSPetr Hosek } else if (Tok == "HIDDEN") { 1592a35e39caSPetr Hosek Cmd = readProvideHidden(false, true); 1593ceabe80eSEugene Leviant } else if (Tok == "PROVIDE_HIDDEN") { 1594a35e39caSPetr Hosek Cmd = readProvideHidden(true, true); 1595ceabe80eSEugene Leviant } 1596ceabe80eSEugene Leviant return Cmd; 1597ceabe80eSEugene Leviant } 1598ceabe80eSEugene Leviant 1599f6aeed36SEugene Leviant static uint64_t getSymbolValue(const Twine &Loc, StringRef S, uint64_t Dot) { 160030835ea4SGeorge Rimar if (S == ".") 160130835ea4SGeorge Rimar return Dot; 1602f6aeed36SEugene Leviant return ScriptBase->getSymbolValue(Loc, S); 1603e32a3598SGeorge Rimar } 1604e32a3598SGeorge Rimar 16052f831dcaSRafael Espindola static bool isAbsolute(StringRef S) { 16062f831dcaSRafael Espindola if (S == ".") 16072f831dcaSRafael Espindola return false; 16082f831dcaSRafael Espindola return ScriptBase->isAbsolute(S); 16092f831dcaSRafael Espindola } 16102f831dcaSRafael Espindola 161130835ea4SGeorge Rimar SymbolAssignment *ScriptParser::readAssignment(StringRef Name) { 161230835ea4SGeorge Rimar StringRef Op = next(); 1613db741e72SEugene Leviant Expr E; 161430835ea4SGeorge Rimar assert(Op == "=" || Op == "+="); 161583043f23SRui Ueyama if (consume("ABSOLUTE")) { 1616731a66aeSRui Ueyama E = readExpr(); 1617009d1742SRui Ueyama E.IsAbsolute = [] { return true; }; 1618db741e72SEugene Leviant } else { 1619db741e72SEugene Leviant E = readExpr(); 1620db741e72SEugene Leviant } 1621f6aeed36SEugene Leviant if (Op == "+=") { 1622f6aeed36SEugene Leviant std::string Loc = getCurrentLocation(); 1623f6aeed36SEugene Leviant E = [=](uint64_t Dot) { 1624f6aeed36SEugene Leviant return getSymbolValue(Loc, Name, Dot) + E(Dot); 1625f6aeed36SEugene Leviant }; 1626f6aeed36SEugene Leviant } 1627*2ee2d2dcSGeorge Rimar return new SymbolAssignment(Name, E, getCurrentLocation()); 162830835ea4SGeorge Rimar } 162930835ea4SGeorge Rimar 163030835ea4SGeorge Rimar // This is an operator-precedence parser to parse a linker 163130835ea4SGeorge Rimar // script expression. 1632731a66aeSRui Ueyama Expr ScriptParser::readExpr() { 1633731a66aeSRui Ueyama // Our lexer is context-aware. Set the in-expression bit so that 1634731a66aeSRui Ueyama // they apply different tokenization rules. 1635731a66aeSRui Ueyama bool Orig = InExpr; 1636731a66aeSRui Ueyama InExpr = true; 1637731a66aeSRui Ueyama Expr E = readExpr1(readPrimary(), 0); 1638731a66aeSRui Ueyama InExpr = Orig; 1639731a66aeSRui Ueyama return E; 1640731a66aeSRui Ueyama } 164130835ea4SGeorge Rimar 164236c1cd23SRui Ueyama static Expr combine(StringRef Op, Expr L, Expr R) { 1643cc4d3e57SGeorge Rimar auto IsAbs = [=] { return L.IsAbsolute() && R.IsAbsolute(); }; 1644cc4d3e57SGeorge Rimar auto GetOutSec = [=] { 1645cc4d3e57SGeorge Rimar const OutputSectionBase *S = L.Section(); 1646cc4d3e57SGeorge Rimar return S ? S : R.Section(); 1647cc4d3e57SGeorge Rimar }; 1648cc4d3e57SGeorge Rimar 164936c1cd23SRui Ueyama if (Op == "*") 165036c1cd23SRui Ueyama return [=](uint64_t Dot) { return L(Dot) * R(Dot); }; 165136c1cd23SRui Ueyama if (Op == "/") { 165236c1cd23SRui Ueyama return [=](uint64_t Dot) -> uint64_t { 165336c1cd23SRui Ueyama uint64_t RHS = R(Dot); 165436c1cd23SRui Ueyama if (RHS == 0) { 165536c1cd23SRui Ueyama error("division by zero"); 165636c1cd23SRui Ueyama return 0; 165736c1cd23SRui Ueyama } 165836c1cd23SRui Ueyama return L(Dot) / RHS; 165936c1cd23SRui Ueyama }; 166036c1cd23SRui Ueyama } 166136c1cd23SRui Ueyama if (Op == "+") 1662cc4d3e57SGeorge Rimar return {[=](uint64_t Dot) { return L(Dot) + R(Dot); }, IsAbs, GetOutSec}; 166336c1cd23SRui Ueyama if (Op == "-") 1664cc4d3e57SGeorge Rimar return {[=](uint64_t Dot) { return L(Dot) - R(Dot); }, IsAbs, GetOutSec}; 1665c8ccd1f1SGeorge Rimar if (Op == "<<") 1666c8ccd1f1SGeorge Rimar return [=](uint64_t Dot) { return L(Dot) << R(Dot); }; 1667c8ccd1f1SGeorge Rimar if (Op == ">>") 1668c8ccd1f1SGeorge Rimar return [=](uint64_t Dot) { return L(Dot) >> R(Dot); }; 166936c1cd23SRui Ueyama if (Op == "<") 167036c1cd23SRui Ueyama return [=](uint64_t Dot) { return L(Dot) < R(Dot); }; 167136c1cd23SRui Ueyama if (Op == ">") 167236c1cd23SRui Ueyama return [=](uint64_t Dot) { return L(Dot) > R(Dot); }; 167336c1cd23SRui Ueyama if (Op == ">=") 167436c1cd23SRui Ueyama return [=](uint64_t Dot) { return L(Dot) >= R(Dot); }; 167536c1cd23SRui Ueyama if (Op == "<=") 167636c1cd23SRui Ueyama return [=](uint64_t Dot) { return L(Dot) <= R(Dot); }; 167736c1cd23SRui Ueyama if (Op == "==") 167836c1cd23SRui Ueyama return [=](uint64_t Dot) { return L(Dot) == R(Dot); }; 167936c1cd23SRui Ueyama if (Op == "!=") 168036c1cd23SRui Ueyama return [=](uint64_t Dot) { return L(Dot) != R(Dot); }; 168136c1cd23SRui Ueyama if (Op == "&") 168236c1cd23SRui Ueyama return [=](uint64_t Dot) { return L(Dot) & R(Dot); }; 1683cc3dd629SRafael Espindola if (Op == "|") 1684cc3dd629SRafael Espindola return [=](uint64_t Dot) { return L(Dot) | R(Dot); }; 168536c1cd23SRui Ueyama llvm_unreachable("invalid operator"); 168636c1cd23SRui Ueyama } 168736c1cd23SRui Ueyama 1688708019c4SRui Ueyama // This is a part of the operator-precedence parser. This function 1689708019c4SRui Ueyama // assumes that the remaining token stream starts with an operator. 1690708019c4SRui Ueyama Expr ScriptParser::readExpr1(Expr Lhs, int MinPrec) { 1691708019c4SRui Ueyama while (!atEOF() && !Error) { 1692708019c4SRui Ueyama // Read an operator and an expression. 169346247b85SRui Ueyama if (consume("?")) 1694708019c4SRui Ueyama return readTernary(Lhs); 169546247b85SRui Ueyama StringRef Op1 = peek(); 1696708019c4SRui Ueyama if (precedence(Op1) < MinPrec) 1697a31c91b1SEugene Leviant break; 16985424e7c7SJustin Bogner skip(); 1699708019c4SRui Ueyama Expr Rhs = readPrimary(); 1700708019c4SRui Ueyama 1701708019c4SRui Ueyama // Evaluate the remaining part of the expression first if the 1702708019c4SRui Ueyama // next operator has greater precedence than the previous one. 1703708019c4SRui Ueyama // For example, if we have read "+" and "3", and if the next 1704708019c4SRui Ueyama // operator is "*", then we'll evaluate 3 * ... part first. 1705708019c4SRui Ueyama while (!atEOF()) { 1706708019c4SRui Ueyama StringRef Op2 = peek(); 1707708019c4SRui Ueyama if (precedence(Op2) <= precedence(Op1)) 1708eda81a1bSEugene Leviant break; 1709708019c4SRui Ueyama Rhs = readExpr1(Rhs, precedence(Op2)); 1710eda81a1bSEugene Leviant } 1711708019c4SRui Ueyama 1712708019c4SRui Ueyama Lhs = combine(Op1, Lhs, Rhs); 1713708019c4SRui Ueyama } 1714708019c4SRui Ueyama return Lhs; 1715708019c4SRui Ueyama } 1716708019c4SRui Ueyama 1717708019c4SRui Ueyama uint64_t static getConstant(StringRef S) { 1718e2cc07bcSMichael J. Spencer if (S == "COMMONPAGESIZE") 1719708019c4SRui Ueyama return Target->PageSize; 1720e2cc07bcSMichael J. Spencer if (S == "MAXPAGESIZE") 1721997f8838SPetr Hosek return Config->MaxPageSize; 1722708019c4SRui Ueyama error("unknown constant: " + S); 1723708019c4SRui Ueyama return 0; 1724708019c4SRui Ueyama } 1725708019c4SRui Ueyama 1726626e0b08SRui Ueyama // Parses Tok as an integer. Returns true if successful. 1727626e0b08SRui Ueyama // It recognizes hexadecimal (prefixed with "0x" or suffixed with "H") 1728626e0b08SRui Ueyama // and decimal numbers. Decimal numbers may have "K" (kilo) or 1729626e0b08SRui Ueyama // "M" (mega) prefixes. 17309f2f7ad9SGeorge Rimar static bool readInteger(StringRef Tok, uint64_t &Result) { 173146247b85SRui Ueyama // Negative number 1732eaeafb2bSSimon Atanasyan if (Tok.startswith("-")) { 1733eaeafb2bSSimon Atanasyan if (!readInteger(Tok.substr(1), Result)) 1734eaeafb2bSSimon Atanasyan return false; 1735eaeafb2bSSimon Atanasyan Result = -Result; 1736eaeafb2bSSimon Atanasyan return true; 1737eaeafb2bSSimon Atanasyan } 173846247b85SRui Ueyama 173946247b85SRui Ueyama // Hexadecimal 17409f2f7ad9SGeorge Rimar if (Tok.startswith_lower("0x")) 17419f2f7ad9SGeorge Rimar return !Tok.substr(2).getAsInteger(16, Result); 17429f2f7ad9SGeorge Rimar if (Tok.endswith_lower("H")) 17439f2f7ad9SGeorge Rimar return !Tok.drop_back().getAsInteger(16, Result); 17449f2f7ad9SGeorge Rimar 174546247b85SRui Ueyama // Decimal 17469f2f7ad9SGeorge Rimar int Suffix = 1; 17479f2f7ad9SGeorge Rimar if (Tok.endswith_lower("K")) { 17489f2f7ad9SGeorge Rimar Suffix = 1024; 17499f2f7ad9SGeorge Rimar Tok = Tok.drop_back(); 17509f2f7ad9SGeorge Rimar } else if (Tok.endswith_lower("M")) { 17519f2f7ad9SGeorge Rimar Suffix = 1024 * 1024; 17529f2f7ad9SGeorge Rimar Tok = Tok.drop_back(); 17539f2f7ad9SGeorge Rimar } 17549f2f7ad9SGeorge Rimar if (Tok.getAsInteger(10, Result)) 17559f2f7ad9SGeorge Rimar return false; 17569f2f7ad9SGeorge Rimar Result *= Suffix; 17579f2f7ad9SGeorge Rimar return true; 17589f2f7ad9SGeorge Rimar } 17599f2f7ad9SGeorge Rimar 1760e38cbab5SGeorge Rimar BytesDataCommand *ScriptParser::readBytesDataCommand(StringRef Tok) { 1761e38cbab5SGeorge Rimar int Size = StringSwitch<unsigned>(Tok) 1762e38cbab5SGeorge Rimar .Case("BYTE", 1) 1763e38cbab5SGeorge Rimar .Case("SHORT", 2) 1764e38cbab5SGeorge Rimar .Case("LONG", 4) 1765e38cbab5SGeorge Rimar .Case("QUAD", 8) 1766e38cbab5SGeorge Rimar .Default(-1); 1767e38cbab5SGeorge Rimar if (Size == -1) 1768e38cbab5SGeorge Rimar return nullptr; 1769e38cbab5SGeorge Rimar 177095c7d8d2SMeador Inge return new BytesDataCommand(readParenExpr(), Size); 1771e38cbab5SGeorge Rimar } 1772e38cbab5SGeorge Rimar 1773b71d6f7aSEugene Leviant StringRef ScriptParser::readParenLiteral() { 1774b71d6f7aSEugene Leviant expect("("); 1775b71d6f7aSEugene Leviant StringRef Tok = next(); 1776b71d6f7aSEugene Leviant expect(")"); 1777b71d6f7aSEugene Leviant return Tok; 1778b71d6f7aSEugene Leviant } 1779b71d6f7aSEugene Leviant 1780708019c4SRui Ueyama Expr ScriptParser::readPrimary() { 17816ad7dfccSRui Ueyama if (peek() == "(") 17826ad7dfccSRui Ueyama return readParenExpr(); 1783708019c4SRui Ueyama 17846ad7dfccSRui Ueyama StringRef Tok = next(); 1785b5f1c3ecSRui Ueyama std::string Location = getCurrentLocation(); 1786708019c4SRui Ueyama 1787eaeafb2bSSimon Atanasyan if (Tok == "~") { 1788eaeafb2bSSimon Atanasyan Expr E = readPrimary(); 1789eaeafb2bSSimon Atanasyan return [=](uint64_t Dot) { return ~E(Dot); }; 1790eaeafb2bSSimon Atanasyan } 1791eaeafb2bSSimon Atanasyan if (Tok == "-") { 1792eaeafb2bSSimon Atanasyan Expr E = readPrimary(); 1793eaeafb2bSSimon Atanasyan return [=](uint64_t Dot) { return -E(Dot); }; 1794eaeafb2bSSimon Atanasyan } 1795eaeafb2bSSimon Atanasyan 1796708019c4SRui Ueyama // Built-in functions are parsed here. 1797708019c4SRui Ueyama // https://sourceware.org/binutils/docs/ld/Builtin-Functions.html. 179896659df0SGeorge Rimar if (Tok == "ADDR") { 1799b71d6f7aSEugene Leviant StringRef Name = readParenLiteral(); 1800ed30ce7aSEugene Leviant return {[=](uint64_t Dot) { 1801ed30ce7aSEugene Leviant return ScriptBase->getOutputSection(Location, Name)->Addr; 1802ed30ce7aSEugene Leviant }, 1803009d1742SRui Ueyama [=] { return false; }, 1804ed30ce7aSEugene Leviant [=] { return ScriptBase->getOutputSection(Location, Name); }}; 180596659df0SGeorge Rimar } 1806b71d6f7aSEugene Leviant if (Tok == "LOADADDR") { 1807b71d6f7aSEugene Leviant StringRef Name = readParenLiteral(); 1808afaa9343SEugene Leviant return [=](uint64_t Dot) { 1809ed30ce7aSEugene Leviant return ScriptBase->getOutputSection(Location, Name)->getLMA(); 1810afaa9343SEugene Leviant }; 1811b71d6f7aSEugene Leviant } 1812eefa758eSGeorge Rimar if (Tok == "ASSERT") 1813eefa758eSGeorge Rimar return readAssert(); 1814708019c4SRui Ueyama if (Tok == "ALIGN") { 18155d804dc8SRui Ueyama expect("("); 18165d804dc8SRui Ueyama Expr E = readExpr(); 18175d804dc8SRui Ueyama if (consume(",")) { 18185d804dc8SRui Ueyama Expr E2 = readExpr(); 18195d804dc8SRui Ueyama expect(")"); 18205d804dc8SRui Ueyama return [=](uint64_t Dot) { return alignTo(E(Dot), E2(Dot)); }; 18215d804dc8SRui Ueyama } 18225d804dc8SRui Ueyama expect(")"); 1823708019c4SRui Ueyama return [=](uint64_t Dot) { return alignTo(Dot, E(Dot)); }; 1824708019c4SRui Ueyama } 1825708019c4SRui Ueyama if (Tok == "CONSTANT") { 1826b71d6f7aSEugene Leviant StringRef Name = readParenLiteral(); 1827b0de56b5SRafael Espindola return [=](uint64_t Dot) { return getConstant(Name); }; 1828708019c4SRui Ueyama } 1829f34f45fdSGeorge Rimar if (Tok == "DEFINED") { 18300ee25a69SRui Ueyama StringRef Name = readParenLiteral(); 18310ee25a69SRui Ueyama return [=](uint64_t Dot) { return ScriptBase->isDefined(Name) ? 1 : 0; }; 1832f34f45fdSGeorge Rimar } 183354c145ceSRafael Espindola if (Tok == "SEGMENT_START") { 183454c145ceSRafael Espindola expect("("); 18355424e7c7SJustin Bogner skip(); 183654c145ceSRafael Espindola expect(","); 18378c658bf8SGeorge Rimar Expr E = readExpr(); 183854c145ceSRafael Espindola expect(")"); 18398c658bf8SGeorge Rimar return [=](uint64_t Dot) { return E(Dot); }; 184054c145ceSRafael Espindola } 1841708019c4SRui Ueyama if (Tok == "DATA_SEGMENT_ALIGN") { 1842708019c4SRui Ueyama expect("("); 1843708019c4SRui Ueyama Expr E = readExpr(); 1844708019c4SRui Ueyama expect(","); 1845708019c4SRui Ueyama readExpr(); 1846708019c4SRui Ueyama expect(")"); 1847f7791bb9SRui Ueyama return [=](uint64_t Dot) { return alignTo(Dot, E(Dot)); }; 1848708019c4SRui Ueyama } 1849708019c4SRui Ueyama if (Tok == "DATA_SEGMENT_END") { 1850708019c4SRui Ueyama expect("("); 1851708019c4SRui Ueyama expect("."); 1852708019c4SRui Ueyama expect(")"); 1853708019c4SRui Ueyama return [](uint64_t Dot) { return Dot; }; 1854708019c4SRui Ueyama } 1855276b4e64SGeorge Rimar // GNU linkers implements more complicated logic to handle 1856276b4e64SGeorge Rimar // DATA_SEGMENT_RELRO_END. We instead ignore the arguments and just align to 1857276b4e64SGeorge Rimar // the next page boundary for simplicity. 1858276b4e64SGeorge Rimar if (Tok == "DATA_SEGMENT_RELRO_END") { 1859276b4e64SGeorge Rimar expect("("); 186097bdc722SRafael Espindola readExpr(); 1861276b4e64SGeorge Rimar expect(","); 1862276b4e64SGeorge Rimar readExpr(); 1863276b4e64SGeorge Rimar expect(")"); 1864276b4e64SGeorge Rimar return [](uint64_t Dot) { return alignTo(Dot, Target->PageSize); }; 1865276b4e64SGeorge Rimar } 18669e69450eSGeorge Rimar if (Tok == "SIZEOF") { 1867b71d6f7aSEugene Leviant StringRef Name = readParenLiteral(); 1868edf75e79SRui Ueyama return [=](uint64_t Dot) { return ScriptBase->getOutputSectionSize(Name); }; 18699e69450eSGeorge Rimar } 187036fac7f0SEugene Leviant if (Tok == "ALIGNOF") { 1871b71d6f7aSEugene Leviant StringRef Name = readParenLiteral(); 1872afaa9343SEugene Leviant return [=](uint64_t Dot) { 1873ed30ce7aSEugene Leviant return ScriptBase->getOutputSection(Location, Name)->Addralign; 1874afaa9343SEugene Leviant }; 187536fac7f0SEugene Leviant } 1876e32a3598SGeorge Rimar if (Tok == "SIZEOF_HEADERS") 1877b0de56b5SRafael Espindola return [=](uint64_t Dot) { return ScriptBase->getHeaderSize(); }; 1878708019c4SRui Ueyama 18799f2f7ad9SGeorge Rimar // Tok is a literal number. 18809f2f7ad9SGeorge Rimar uint64_t V; 18819f2f7ad9SGeorge Rimar if (readInteger(Tok, V)) 1882b0de56b5SRafael Espindola return [=](uint64_t Dot) { return V; }; 18839f2f7ad9SGeorge Rimar 18849f2f7ad9SGeorge Rimar // Tok is a symbol name. 188530835ea4SGeorge Rimar if (Tok != "." && !isValidCIdentifier(Tok)) 1886708019c4SRui Ueyama setError("malformed number: " + Tok); 1887f6aeed36SEugene Leviant return {[=](uint64_t Dot) { return getSymbolValue(Location, Tok, Dot); }, 1888009d1742SRui Ueyama [=] { return isAbsolute(Tok); }, 1889009d1742SRui Ueyama [=] { return ScriptBase->getSymbolSection(Tok); }}; 1890a9c5a528SGeorge Rimar } 1891708019c4SRui Ueyama 1892708019c4SRui Ueyama Expr ScriptParser::readTernary(Expr Cond) { 1893708019c4SRui Ueyama Expr L = readExpr(); 1894708019c4SRui Ueyama expect(":"); 1895708019c4SRui Ueyama Expr R = readExpr(); 1896708019c4SRui Ueyama return [=](uint64_t Dot) { return Cond(Dot) ? L(Dot) : R(Dot); }; 1897708019c4SRui Ueyama } 1898708019c4SRui Ueyama 18996ad7dfccSRui Ueyama Expr ScriptParser::readParenExpr() { 19006ad7dfccSRui Ueyama expect("("); 19016ad7dfccSRui Ueyama Expr E = readExpr(); 19026ad7dfccSRui Ueyama expect(")"); 19036ad7dfccSRui Ueyama return E; 19046ad7dfccSRui Ueyama } 19056ad7dfccSRui Ueyama 1906bbe38602SEugene Leviant std::vector<StringRef> ScriptParser::readOutputSectionPhdrs() { 1907bbe38602SEugene Leviant std::vector<StringRef> Phdrs; 1908bbe38602SEugene Leviant while (!Error && peek().startswith(":")) { 1909bbe38602SEugene Leviant StringRef Tok = next(); 1910da841c16SGeorge Rimar Phdrs.push_back((Tok.size() == 1) ? next() : Tok.substr(1)); 1911bbe38602SEugene Leviant } 1912bbe38602SEugene Leviant return Phdrs; 1913bbe38602SEugene Leviant } 1914bbe38602SEugene Leviant 191595dd718cSGeorge Rimar // Read a program header type name. The next token must be a 191695dd718cSGeorge Rimar // name of a program header type or a constant (e.g. "0x3"). 1917bbe38602SEugene Leviant unsigned ScriptParser::readPhdrType() { 1918bbe38602SEugene Leviant StringRef Tok = next(); 191995dd718cSGeorge Rimar uint64_t Val; 192095dd718cSGeorge Rimar if (readInteger(Tok, Val)) 192195dd718cSGeorge Rimar return Val; 192295dd718cSGeorge Rimar 1923b0f6c590SRui Ueyama unsigned Ret = StringSwitch<unsigned>(Tok) 1924b0f6c590SRui Ueyama .Case("PT_NULL", PT_NULL) 1925b0f6c590SRui Ueyama .Case("PT_LOAD", PT_LOAD) 1926b0f6c590SRui Ueyama .Case("PT_DYNAMIC", PT_DYNAMIC) 1927b0f6c590SRui Ueyama .Case("PT_INTERP", PT_INTERP) 1928b0f6c590SRui Ueyama .Case("PT_NOTE", PT_NOTE) 1929b0f6c590SRui Ueyama .Case("PT_SHLIB", PT_SHLIB) 1930b0f6c590SRui Ueyama .Case("PT_PHDR", PT_PHDR) 1931b0f6c590SRui Ueyama .Case("PT_TLS", PT_TLS) 1932b0f6c590SRui Ueyama .Case("PT_GNU_EH_FRAME", PT_GNU_EH_FRAME) 1933b0f6c590SRui Ueyama .Case("PT_GNU_STACK", PT_GNU_STACK) 1934b0f6c590SRui Ueyama .Case("PT_GNU_RELRO", PT_GNU_RELRO) 1935270173f2SGeorge Rimar .Case("PT_OPENBSD_RANDOMIZE", PT_OPENBSD_RANDOMIZE) 1936cc6e567cSGeorge Rimar .Case("PT_OPENBSD_WXNEEDED", PT_OPENBSD_WXNEEDED) 1937a2a32c2cSGeorge Rimar .Case("PT_OPENBSD_BOOTDATA", PT_OPENBSD_BOOTDATA) 1938b0f6c590SRui Ueyama .Default(-1); 1939bbe38602SEugene Leviant 1940b0f6c590SRui Ueyama if (Ret == (unsigned)-1) { 1941b0f6c590SRui Ueyama setError("invalid program header type: " + Tok); 1942b0f6c590SRui Ueyama return PT_NULL; 1943b0f6c590SRui Ueyama } 1944b0f6c590SRui Ueyama return Ret; 1945bbe38602SEugene Leviant } 1946bbe38602SEugene Leviant 194712450b20SRui Ueyama // Reads a list of symbols, e.g. "{ global: foo; bar; local: *; };". 194812450b20SRui Ueyama void ScriptParser::readAnonymousDeclaration() { 194912450b20SRui Ueyama // Read global symbols first. "global:" is default, so if there's 195012450b20SRui Ueyama // no label, we assume global symbols. 19514524268cSRafael Espindola if (peek() != "local") { 19524524268cSRafael Espindola if (consume("global")) 19534524268cSRafael Espindola expect(":"); 1954904c5ed5SPeter Collingbourne for (SymbolVersion V : readSymbols()) 1955904c5ed5SPeter Collingbourne Config->VersionScriptGlobals.push_back(V); 19564524268cSRafael Espindola } 1957e999ddb8SRafael Espindola readLocals(); 195812450b20SRui Ueyama expect("}"); 195912450b20SRui Ueyama expect(";"); 196012450b20SRui Ueyama } 196112450b20SRui Ueyama 1962e999ddb8SRafael Espindola void ScriptParser::readLocals() { 19634524268cSRafael Espindola if (!consume("local")) 1964e999ddb8SRafael Espindola return; 19654524268cSRafael Espindola expect(":"); 1966e999ddb8SRafael Espindola std::vector<SymbolVersion> Locals = readSymbols(); 1967e999ddb8SRafael Espindola for (SymbolVersion V : Locals) { 1968e999ddb8SRafael Espindola if (V.Name == "*") { 1969e999ddb8SRafael Espindola Config->DefaultSymbolVersion = VER_NDX_LOCAL; 1970e999ddb8SRafael Espindola continue; 1971e999ddb8SRafael Espindola } 1972e999ddb8SRafael Espindola Config->VersionScriptLocals.push_back(V); 1973e999ddb8SRafael Espindola } 1974e999ddb8SRafael Espindola } 1975e999ddb8SRafael Espindola 197612450b20SRui Ueyama // Reads a list of symbols, e.g. "VerStr { global: foo; bar; local: *; };". 197795769b4aSRui Ueyama void ScriptParser::readVersionDeclaration(StringRef VerStr) { 197820b6598cSGeorge Rimar // Identifiers start at 2 because 0 and 1 are reserved 197920b6598cSGeorge Rimar // for VER_NDX_LOCAL and VER_NDX_GLOBAL constants. 1980da805c48SRui Ueyama uint16_t VersionId = Config->VersionDefinitions.size() + 2; 198120b6598cSGeorge Rimar Config->VersionDefinitions.push_back({VerStr, VersionId}); 198220b6598cSGeorge Rimar 198312450b20SRui Ueyama // Read global symbols. 19844524268cSRafael Espindola if (peek() != "local") { 19854524268cSRafael Espindola if (consume("global")) 19864524268cSRafael Espindola expect(":"); 198712450b20SRui Ueyama Config->VersionDefinitions.back().Globals = readSymbols(); 19884524268cSRafael Espindola } 1989e999ddb8SRafael Espindola readLocals(); 199020b6598cSGeorge Rimar expect("}"); 199120b6598cSGeorge Rimar 199212450b20SRui Ueyama // Each version may have a parent version. For example, "Ver2" 199312450b20SRui Ueyama // defined as "Ver2 { global: foo; local: *; } Ver1;" has "Ver1" 199412450b20SRui Ueyama // as a parent. This version hierarchy is, probably against your 199512450b20SRui Ueyama // instinct, purely for hint; the runtime doesn't care about it 199612450b20SRui Ueyama // at all. In LLD, we simply ignore it. 199712450b20SRui Ueyama if (peek() != ";") 19985424e7c7SJustin Bogner skip(); 199920b6598cSGeorge Rimar expect(";"); 200020b6598cSGeorge Rimar } 200120b6598cSGeorge Rimar 200212450b20SRui Ueyama // Reads a list of symbols for a versions cript. 200312450b20SRui Ueyama std::vector<SymbolVersion> ScriptParser::readSymbols() { 200412450b20SRui Ueyama std::vector<SymbolVersion> Ret; 2005e0fc2421SGeorge Rimar for (;;) { 20061ef90d2fSRafael Espindola if (consume("extern")) { 200712450b20SRui Ueyama for (SymbolVersion V : readVersionExtern()) 200812450b20SRui Ueyama Ret.push_back(V); 20091ef90d2fSRafael Espindola continue; 20101ef90d2fSRafael Espindola } 2011e0fc2421SGeorge Rimar 2012f3965c02SDmitry Mikulin if (peek() == "}" || (peek() == "local" && peek(1) == ":") || Error) 201312450b20SRui Ueyama break; 20140ee25a69SRui Ueyama StringRef Tok = next(); 201512450b20SRui Ueyama Ret.push_back({unquote(Tok), false, hasWildcard(Tok)}); 2016e0fc2421SGeorge Rimar expect(";"); 2017e0fc2421SGeorge Rimar } 201812450b20SRui Ueyama return Ret; 2019e0fc2421SGeorge Rimar } 2020e0fc2421SGeorge Rimar 202112450b20SRui Ueyama // Reads an "extern C++" directive, e.g., 202212450b20SRui Ueyama // "extern "C++" { ns::*; "f(int, double)"; };" 202312450b20SRui Ueyama std::vector<SymbolVersion> ScriptParser::readVersionExtern() { 20247e71415cSRafael Espindola StringRef Tok = next(); 20257e71415cSRafael Espindola bool IsCXX = Tok == "\"C++\""; 20267e71415cSRafael Espindola if (!IsCXX && Tok != "\"C\"") 2027d0ebd84cSRafael Espindola setError("Unknown language"); 202820b6598cSGeorge Rimar expect("{"); 202920b6598cSGeorge Rimar 203012450b20SRui Ueyama std::vector<SymbolVersion> Ret; 20310ee25a69SRui Ueyama while (!Error && peek() != "}") { 20320ee25a69SRui Ueyama StringRef Tok = next(); 20330ee25a69SRui Ueyama bool HasWildcard = !Tok.startswith("\"") && hasWildcard(Tok); 20347e71415cSRafael Espindola Ret.push_back({unquote(Tok), IsCXX, HasWildcard}); 203520b6598cSGeorge Rimar expect(";"); 203620b6598cSGeorge Rimar } 203720b6598cSGeorge Rimar 203820b6598cSGeorge Rimar expect("}"); 203920b6598cSGeorge Rimar expect(";"); 204012450b20SRui Ueyama return Ret; 204120b6598cSGeorge Rimar } 204220b6598cSGeorge Rimar 204324e626ccSRui Ueyama uint64_t ScriptParser::readMemoryAssignment( 204424e626ccSRui Ueyama StringRef S1, StringRef S2, StringRef S3) { 204524e626ccSRui Ueyama if (!(consume(S1) || consume(S2) || consume(S3))) { 204624e626ccSRui Ueyama setError("expected one of: " + S1 + ", " + S2 + ", or " + S3); 204724e626ccSRui Ueyama return 0; 204824e626ccSRui Ueyama } 204924e626ccSRui Ueyama expect("="); 205024e626ccSRui Ueyama 205124e626ccSRui Ueyama // TODO: Fully support constant expressions. 205224e626ccSRui Ueyama uint64_t Val; 205324e626ccSRui Ueyama if (!readInteger(next(), Val)) 205424e626ccSRui Ueyama setError("nonconstant expression for "+ S1); 205524e626ccSRui Ueyama return Val; 205624e626ccSRui Ueyama } 205724e626ccSRui Ueyama 205824e626ccSRui Ueyama // Parse the MEMORY command as specified in: 205924e626ccSRui Ueyama // https://sourceware.org/binutils/docs/ld/MEMORY.html 206024e626ccSRui Ueyama // 206124e626ccSRui Ueyama // MEMORY { name [(attr)] : ORIGIN = origin, LENGTH = len ... } 2062b889744eSMeador Inge void ScriptParser::readMemory() { 2063b889744eSMeador Inge expect("{"); 2064b889744eSMeador Inge while (!Error && !consume("}")) { 2065b889744eSMeador Inge StringRef Name = next(); 206624e626ccSRui Ueyama 2067b889744eSMeador Inge uint32_t Flags = 0; 20688a8a953eSRui Ueyama uint32_t NegFlags = 0; 2069b889744eSMeador Inge if (consume("(")) { 20708a8a953eSRui Ueyama std::tie(Flags, NegFlags) = readMemoryAttributes(); 2071b889744eSMeador Inge expect(")"); 2072b889744eSMeador Inge } 2073b889744eSMeador Inge expect(":"); 2074b889744eSMeador Inge 207524e626ccSRui Ueyama uint64_t Origin = readMemoryAssignment("ORIGIN", "org", "o"); 2076b889744eSMeador Inge expect(","); 207724e626ccSRui Ueyama uint64_t Length = readMemoryAssignment("LENGTH", "len", "l"); 2078b889744eSMeador Inge 2079b889744eSMeador Inge // Add the memory region to the region map (if it doesn't already exist). 2080b889744eSMeador Inge auto It = Opt.MemoryRegions.find(Name); 2081b889744eSMeador Inge if (It != Opt.MemoryRegions.end()) 2082b889744eSMeador Inge setError("region '" + Name + "' already defined"); 2083b889744eSMeador Inge else 20848a8a953eSRui Ueyama Opt.MemoryRegions[Name] = {Name, Origin, Length, Origin, Flags, NegFlags}; 2085b889744eSMeador Inge } 2086b889744eSMeador Inge } 2087b889744eSMeador Inge 2088b889744eSMeador Inge // This function parses the attributes used to match against section 2089b889744eSMeador Inge // flags when placing output sections in a memory region. These flags 2090b889744eSMeador Inge // are only used when an explicit memory region name is not used. 2091b889744eSMeador Inge std::pair<uint32_t, uint32_t> ScriptParser::readMemoryAttributes() { 2092b889744eSMeador Inge uint32_t Flags = 0; 20938a8a953eSRui Ueyama uint32_t NegFlags = 0; 2094b889744eSMeador Inge bool Invert = false; 2095481ac996SRui Ueyama 2096481ac996SRui Ueyama for (char C : next().lower()) { 2097b889744eSMeador Inge uint32_t Flag = 0; 2098b889744eSMeador Inge if (C == '!') 2099b889744eSMeador Inge Invert = !Invert; 2100481ac996SRui Ueyama else if (C == 'w') 2101b889744eSMeador Inge Flag = SHF_WRITE; 2102481ac996SRui Ueyama else if (C == 'x') 2103b889744eSMeador Inge Flag = SHF_EXECINSTR; 2104481ac996SRui Ueyama else if (C == 'a') 2105b889744eSMeador Inge Flag = SHF_ALLOC; 2106481ac996SRui Ueyama else if (C != 'r') 2107b889744eSMeador Inge setError("invalid memory region attribute"); 2108481ac996SRui Ueyama 2109b889744eSMeador Inge if (Invert) 21108a8a953eSRui Ueyama NegFlags |= Flag; 2111b889744eSMeador Inge else 2112b889744eSMeador Inge Flags |= Flag; 2113b889744eSMeador Inge } 21148a8a953eSRui Ueyama return {Flags, NegFlags}; 2115b889744eSMeador Inge } 2116b889744eSMeador Inge 211707320e40SRui Ueyama void elf::readLinkerScript(MemoryBufferRef MB) { 211822375f24SRui Ueyama ScriptParser(MB).readLinkerScript(); 211920b6598cSGeorge Rimar } 212020b6598cSGeorge Rimar 212120b6598cSGeorge Rimar void elf::readVersionScript(MemoryBufferRef MB) { 212222375f24SRui Ueyama ScriptParser(MB).readVersionScript(); 2123f7c5fbb1SRui Ueyama } 21241ebc8ed7SRui Ueyama 2125d0ebd84cSRafael Espindola void elf::readDynamicList(MemoryBufferRef MB) { 2126d0ebd84cSRafael Espindola ScriptParser(MB).readDynamicList(); 2127d0ebd84cSRafael Espindola } 2128d0ebd84cSRafael Espindola 212907320e40SRui Ueyama template class elf::LinkerScript<ELF32LE>; 213007320e40SRui Ueyama template class elf::LinkerScript<ELF32BE>; 213107320e40SRui Ueyama template class elf::LinkerScript<ELF64LE>; 213207320e40SRui Ueyama template class elf::LinkerScript<ELF64BE>; 2133