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. 11629e0aa5SRui Ueyama // It parses a linker script and write the result to Config or ScriptConfig 12629e0aa5SRui Ueyama // objects. 13629e0aa5SRui Ueyama // 14629e0aa5SRui Ueyama // If SECTIONS command is used, a ScriptConfig contains an AST 15629e0aa5SRui Ueyama // of the command which will later be consumed by createSections() and 16629e0aa5SRui Ueyama // assignAddresses(). 17f7c5fbb1SRui Ueyama // 18f7c5fbb1SRui Ueyama //===----------------------------------------------------------------------===// 19f7c5fbb1SRui Ueyama 20717677afSRui Ueyama #include "LinkerScript.h" 21f7c5fbb1SRui Ueyama #include "Config.h" 22f7c5fbb1SRui Ueyama #include "Driver.h" 231ebc8ed7SRui Ueyama #include "InputSection.h" 24652852c5SGeorge Rimar #include "OutputSections.h" 25e77b5bf6SAdhemerval Zanella #include "ScriptParser.h" 2693c9af42SRui Ueyama #include "Strings.h" 27eda81a1bSEugene Leviant #include "Symbols.h" 28f7c5fbb1SRui Ueyama #include "SymbolTable.h" 29467c4d55SEugene Leviant #include "Target.h" 30bbe38602SEugene Leviant #include "Writer.h" 31960504b9SRui Ueyama #include "llvm/ADT/StringSwitch.h" 32652852c5SGeorge Rimar #include "llvm/Support/ELF.h" 33f7c5fbb1SRui Ueyama #include "llvm/Support/FileSystem.h" 34f7c5fbb1SRui Ueyama #include "llvm/Support/MemoryBuffer.h" 35f03f3cc1SRui Ueyama #include "llvm/Support/Path.h" 36a47ee68dSRui Ueyama #include "llvm/Support/StringSaver.h" 37f7c5fbb1SRui Ueyama 38f7c5fbb1SRui Ueyama using namespace llvm; 39652852c5SGeorge Rimar using namespace llvm::ELF; 401ebc8ed7SRui Ueyama using namespace llvm::object; 41f7c5fbb1SRui Ueyama using namespace lld; 42e0df00b9SRafael Espindola using namespace lld::elf; 43f7c5fbb1SRui Ueyama 44884e786dSGeorge Rimar LinkerScriptBase *elf::ScriptBase; 4507320e40SRui Ueyama ScriptConfiguration *elf::ScriptConfig; 46717677afSRui Ueyama 476c55f0e3SGeorge Rimar template <class ELFT> static void addRegular(SymbolAssignment *Cmd) { 481602421cSRui Ueyama Symbol *Sym = Symtab<ELFT>::X->addRegular(Cmd->Name, STB_GLOBAL, STV_DEFAULT); 491602421cSRui Ueyama Sym->Visibility = Cmd->Hidden ? STV_HIDDEN : STV_DEFAULT; 501602421cSRui Ueyama Cmd->Sym = Sym->body(); 5120d03194SEugene Leviant 5220d03194SEugene Leviant // If we have no SECTIONS then we don't have '.' and don't call 5320d03194SEugene Leviant // assignAddresses(). We calculate symbol value immediately in this case. 5420d03194SEugene Leviant if (!ScriptConfig->HasSections) 5520d03194SEugene Leviant cast<DefinedRegular<ELFT>>(Cmd->Sym)->Value = Cmd->Expression(0); 56ceabe80eSEugene Leviant } 57ceabe80eSEugene Leviant 580c70d3ccSRui Ueyama template <class ELFT> static void addSynthetic(SymbolAssignment *Cmd) { 59e1937bb5SGeorge Rimar Symbol *Sym = Symtab<ELFT>::X->addSynthetic( 60e1937bb5SGeorge Rimar Cmd->Name, nullptr, 0, Cmd->Hidden ? STV_HIDDEN : STV_DEFAULT); 611602421cSRui Ueyama Cmd->Sym = Sym->body(); 62ceabe80eSEugene Leviant } 63ceabe80eSEugene Leviant 64db741e72SEugene Leviant template <class ELFT> static void addSymbol(SymbolAssignment *Cmd) { 65db741e72SEugene Leviant if (Cmd->IsAbsolute) 66db741e72SEugene Leviant addRegular<ELFT>(Cmd); 67db741e72SEugene Leviant else 68db741e72SEugene Leviant addSynthetic<ELFT>(Cmd); 69db741e72SEugene Leviant } 701602421cSRui Ueyama // If a symbol was in PROVIDE(), we need to define it only when 711602421cSRui Ueyama // it is an undefined symbol. 721602421cSRui Ueyama template <class ELFT> static bool shouldDefine(SymbolAssignment *Cmd) { 731602421cSRui Ueyama if (Cmd->Name == ".") 74ceabe80eSEugene Leviant return false; 751602421cSRui Ueyama if (!Cmd->Provide) 76ceabe80eSEugene Leviant return true; 771602421cSRui Ueyama SymbolBody *B = Symtab<ELFT>::X->find(Cmd->Name); 781602421cSRui Ueyama return B && B->isUndefined(); 79ceabe80eSEugene Leviant } 80ceabe80eSEugene Leviant 81076fe157SGeorge Rimar bool SymbolAssignment::classof(const BaseCommand *C) { 82076fe157SGeorge Rimar return C->Kind == AssignmentKind; 83076fe157SGeorge Rimar } 84076fe157SGeorge Rimar 85076fe157SGeorge Rimar bool OutputSectionCommand::classof(const BaseCommand *C) { 86076fe157SGeorge Rimar return C->Kind == OutputSectionKind; 87076fe157SGeorge Rimar } 88076fe157SGeorge Rimar 89eea3114fSGeorge Rimar bool InputSectionDescription::classof(const BaseCommand *C) { 90eea3114fSGeorge Rimar return C->Kind == InputSectionKind; 91eea3114fSGeorge Rimar } 92eea3114fSGeorge Rimar 93eefa758eSGeorge Rimar bool AssertCommand::classof(const BaseCommand *C) { 94eefa758eSGeorge Rimar return C->Kind == AssertKind; 95eefa758eSGeorge Rimar } 96eefa758eSGeorge Rimar 9736a153cdSRui Ueyama template <class ELFT> static bool isDiscarded(InputSectionBase<ELFT> *S) { 98eea3114fSGeorge Rimar return !S || !S->Live; 99717677afSRui Ueyama } 100717677afSRui Ueyama 101f34d0e08SRui Ueyama template <class ELFT> LinkerScript<ELFT>::LinkerScript() {} 102f34d0e08SRui Ueyama template <class ELFT> LinkerScript<ELFT>::~LinkerScript() {} 103f34d0e08SRui Ueyama 10407320e40SRui Ueyama template <class ELFT> 10507320e40SRui Ueyama bool LinkerScript<ELFT>::shouldKeep(InputSectionBase<ELFT> *S) { 106c91930a1SGeorge Rimar for (Regex *Re : Opt.KeptSections) 107042a3f20SRafael Espindola if (Re->match(S->Name)) 108eea3114fSGeorge Rimar return true; 109eea3114fSGeorge Rimar return false; 110eea3114fSGeorge Rimar } 111eea3114fSGeorge Rimar 112395281cfSGeorge Rimar static bool fileMatches(const llvm::Regex &FileRe, 113395281cfSGeorge Rimar const llvm::Regex &ExcludedFileRe, StringRef Filename) { 114395281cfSGeorge Rimar return const_cast<Regex &>(FileRe).match(Filename) && 115395281cfSGeorge Rimar !const_cast<Regex &>(ExcludedFileRe).match(Filename); 1160659800eSGeorge Rimar } 1170659800eSGeorge Rimar 118575208caSGeorge Rimar static bool comparePriority(InputSectionData *A, InputSectionData *B) { 119575208caSGeorge Rimar return getPriority(A->Name) < getPriority(B->Name); 120575208caSGeorge Rimar } 121575208caSGeorge Rimar 122c0028d3dSRafael Espindola static bool compareName(InputSectionData *A, InputSectionData *B) { 123042a3f20SRafael Espindola return A->Name < B->Name; 1240702c4e8SGeorge Rimar } 125742c3836SRui Ueyama 126c0028d3dSRafael Espindola static bool compareAlignment(InputSectionData *A, InputSectionData *B) { 127742c3836SRui Ueyama // ">" is not a mistake. Larger alignments are placed before smaller 128742c3836SRui Ueyama // alignments in order to reduce the amount of padding necessary. 129742c3836SRui Ueyama // This is compatible with GNU. 130742c3836SRui Ueyama return A->Alignment > B->Alignment; 131742c3836SRui Ueyama } 132742c3836SRui Ueyama 133c0028d3dSRafael Espindola static std::function<bool(InputSectionData *, InputSectionData *)> 134be394db3SGeorge Rimar getComparator(SortSectionPolicy K) { 135be394db3SGeorge Rimar switch (K) { 136be394db3SGeorge Rimar case SortSectionPolicy::Alignment: 137c0028d3dSRafael Espindola return compareAlignment; 138be394db3SGeorge Rimar case SortSectionPolicy::Name: 139be394db3SGeorge Rimar return compareName; 140be394db3SGeorge Rimar case SortSectionPolicy::Priority: 141be394db3SGeorge Rimar return comparePriority; 142be394db3SGeorge Rimar default: 143be394db3SGeorge Rimar llvm_unreachable("unknown sort policy"); 144be394db3SGeorge Rimar } 145742c3836SRui Ueyama } 1460702c4e8SGeorge Rimar 1478f66df92SGeorge Rimar static bool checkConstraint(uint64_t Flags, ConstraintKind Kind) { 1488f66df92SGeorge Rimar bool RO = (Kind == ConstraintKind::ReadOnly); 1498f66df92SGeorge Rimar bool RW = (Kind == ConstraintKind::ReadWrite); 1508f66df92SGeorge Rimar bool Writable = Flags & SHF_WRITE; 151adcdb664SRui Ueyama return !(RO && Writable) && !(RW && !Writable); 1528f66df92SGeorge Rimar } 1538f66df92SGeorge Rimar 15448c3f1ceSRui Ueyama template <class ELFT> 155*e71a3f8aSRafael Espindola static bool matchConstraints(ArrayRef<InputSectionBase<ELFT> *> Sections, 15606ae6836SGeorge Rimar ConstraintKind Kind) { 1578f66df92SGeorge Rimar if (Kind == ConstraintKind::NoConstraint) 1588f66df92SGeorge Rimar return true; 159d3190795SRafael Espindola return llvm::all_of(Sections, [=](InputSectionData *Sec2) { 160d3190795SRafael Espindola auto *Sec = static_cast<InputSectionBase<ELFT> *>(Sec2); 1618f66df92SGeorge Rimar return checkConstraint(Sec->getSectionHdr()->sh_flags, Kind); 16206ae6836SGeorge Rimar }); 16306ae6836SGeorge Rimar } 16406ae6836SGeorge Rimar 165d3190795SRafael Espindola // Compute and remember which sections the InputSectionDescription matches. 166be94e1b6SRafael Espindola template <class ELFT> 167*e71a3f8aSRafael Espindola void LinkerScript<ELFT>::computeInputSections(InputSectionDescription *I) { 168395281cfSGeorge Rimar for (const std::pair<llvm::Regex, llvm::Regex> &V : I->SectionsVec) { 169395281cfSGeorge Rimar for (ObjectFile<ELFT> *F : Symtab<ELFT>::X->getObjectFiles()) { 170395281cfSGeorge Rimar if (fileMatches(I->FileRe, V.first, sys::path::filename(F->getName()))) { 171395281cfSGeorge Rimar Regex &Re = const_cast<Regex &>(V.second); 172be94e1b6SRafael Espindola for (InputSectionBase<ELFT> *S : F->getSections()) 173395281cfSGeorge Rimar if (!isDiscarded(S) && !S->OutSec && Re.match(S->Name)) 174d3190795SRafael Espindola I->Sections.push_back(S); 175be94e1b6SRafael Espindola 176395281cfSGeorge Rimar if (Re.match("COMMON")) 177d3190795SRafael Espindola I->Sections.push_back(CommonInputSection<ELFT>::X); 178395281cfSGeorge Rimar } 179395281cfSGeorge Rimar } 180395281cfSGeorge Rimar } 181d3190795SRafael Espindola 182be394db3SGeorge Rimar if (I->SortInner != SortSectionPolicy::None) 183d3190795SRafael Espindola std::stable_sort(I->Sections.begin(), I->Sections.end(), 184d3190795SRafael Espindola getComparator(I->SortInner)); 185be394db3SGeorge Rimar if (I->SortOuter != SortSectionPolicy::None) 186d3190795SRafael Espindola std::stable_sort(I->Sections.begin(), I->Sections.end(), 187d3190795SRafael Espindola getComparator(I->SortOuter)); 188d3190795SRafael Espindola 189d3190795SRafael Espindola // We do not add duplicate input sections, so mark them with a dummy output 190d3190795SRafael Espindola // section for now. 191d3190795SRafael Espindola for (InputSectionData *S : I->Sections) { 192d3190795SRafael Espindola auto *S2 = static_cast<InputSectionBase<ELFT> *>(S); 193d3190795SRafael Espindola S2->OutSec = (OutputSectionBase<ELFT> *)-1; 194d3190795SRafael Espindola } 195be94e1b6SRafael Espindola } 196be94e1b6SRafael Espindola 197be94e1b6SRafael Espindola template <class ELFT> 198be94e1b6SRafael Espindola void LinkerScript<ELFT>::discard(ArrayRef<InputSectionBase<ELFT> *> V) { 199be94e1b6SRafael Espindola for (InputSectionBase<ELFT> *S : V) { 200be94e1b6SRafael Espindola S->Live = false; 201be94e1b6SRafael Espindola reportDiscarded(S); 202be94e1b6SRafael Espindola } 203be94e1b6SRafael Espindola } 204be94e1b6SRafael Espindola 20506ae6836SGeorge Rimar template <class ELFT> 2060b9ce6a4SRui Ueyama std::vector<InputSectionBase<ELFT> *> 20706ae6836SGeorge Rimar LinkerScript<ELFT>::createInputSectionList(OutputSectionCommand &OutCmd) { 2080b9ce6a4SRui Ueyama std::vector<InputSectionBase<ELFT> *> Ret; 209e7f912cdSRui Ueyama 21006ae6836SGeorge Rimar for (const std::unique_ptr<BaseCommand> &Base : OutCmd.Commands) { 21106ae6836SGeorge Rimar if (auto *OutCmd = dyn_cast<SymbolAssignment>(Base.get())) { 21206ae6836SGeorge Rimar if (shouldDefine<ELFT>(OutCmd)) 213db741e72SEugene Leviant addSymbol<ELFT>(OutCmd); 2140b9ce6a4SRui Ueyama continue; 2150b9ce6a4SRui Ueyama } 2160b9ce6a4SRui Ueyama 2170b9ce6a4SRui Ueyama auto *Cmd = cast<InputSectionDescription>(Base.get()); 218*e71a3f8aSRafael Espindola computeInputSections(Cmd); 219d3190795SRafael Espindola for (InputSectionData *S : Cmd->Sections) 220d3190795SRafael Espindola Ret.push_back(static_cast<InputSectionBase<ELFT> *>(S)); 2210b9ce6a4SRui Ueyama } 222*e71a3f8aSRafael Espindola 223*e71a3f8aSRafael Espindola if (!matchConstraints<ELFT>(Ret, OutCmd.Constraint)) { 224*e71a3f8aSRafael Espindola for (InputSectionBase<ELFT> *S : Ret) 225*e71a3f8aSRafael Espindola S->OutSec = nullptr; 226*e71a3f8aSRafael Espindola Ret.clear(); 227*e71a3f8aSRafael Espindola } 228*e71a3f8aSRafael Espindola 2290b9ce6a4SRui Ueyama return Ret; 2300b9ce6a4SRui Ueyama } 2310b9ce6a4SRui Ueyama 232e5d3ca50SPetr Hosek template <class ELFT> 23310897f18SRafael Espindola static SectionKey<ELFT::Is64Bits> createKey(InputSectionBase<ELFT> *C, 23410897f18SRafael Espindola StringRef OutsecName) { 23510897f18SRafael Espindola // When using linker script the merge rules are different. 23610897f18SRafael Espindola // Unfortunately, linker scripts are name based. This means that expressions 23710897f18SRafael Espindola // like *(.foo*) can refer to multiple input sections that would normally be 23810897f18SRafael Espindola // placed in different output sections. We cannot put them in different 23910897f18SRafael Espindola // output sections or we would produce wrong results for 24010897f18SRafael Espindola // start = .; *(.foo.*) end = .; *(.bar) 24110897f18SRafael Espindola // and a mapping of .foo1 and .bar1 to one section and .foo2 and .bar2 to 24210897f18SRafael Espindola // another. The problem is that there is no way to layout those output 24310897f18SRafael Espindola // sections such that the .foo sections are the only thing between the 24410897f18SRafael Espindola // start and end symbols. 24510897f18SRafael Espindola 24610897f18SRafael Espindola // An extra annoyance is that we cannot simply disable merging of the contents 24710897f18SRafael Espindola // of SHF_MERGE sections, but our implementation requires one output section 24810897f18SRafael Espindola // per "kind" (string or not, which size/aligment). 24910897f18SRafael Espindola // Fortunately, creating symbols in the middle of a merge section is not 25010897f18SRafael Espindola // supported by bfd or gold, so we can just create multiple section in that 25110897f18SRafael Espindola // case. 25210897f18SRafael Espindola const typename ELFT::Shdr *H = C->getSectionHdr(); 25310897f18SRafael Espindola typedef typename ELFT::uint uintX_t; 25410897f18SRafael Espindola uintX_t Flags = H->sh_flags & (SHF_MERGE | SHF_STRINGS); 25510897f18SRafael Espindola 25610897f18SRafael Espindola uintX_t Alignment = 0; 25710897f18SRafael Espindola if (isa<MergeInputSection<ELFT>>(C)) 25810897f18SRafael Espindola Alignment = std::max(H->sh_addralign, H->sh_entsize); 25910897f18SRafael Espindola 26010897f18SRafael Espindola return SectionKey<ELFT::Is64Bits>{OutsecName, /*Type*/ 0, Flags, Alignment}; 26110897f18SRafael Espindola } 26210897f18SRafael Espindola 26310897f18SRafael Espindola template <class ELFT> 26420d03194SEugene Leviant void LinkerScript<ELFT>::addSection(OutputSectionFactory<ELFT> &Factory, 26520d03194SEugene Leviant InputSectionBase<ELFT> *Sec, 26620d03194SEugene Leviant StringRef Name) { 26728c1597aSRafael Espindola OutputSectionBase<ELFT> *OutSec; 26828c1597aSRafael Espindola bool IsNew; 26910897f18SRafael Espindola std::tie(OutSec, IsNew) = Factory.create(createKey(Sec, Name), Sec); 27028c1597aSRafael Espindola if (IsNew) 27128c1597aSRafael Espindola OutputSections->push_back(OutSec); 27220d03194SEugene Leviant OutSec->addSection(Sec); 27320d03194SEugene Leviant } 27420d03194SEugene Leviant 27520d03194SEugene Leviant template <class ELFT> 27620d03194SEugene Leviant void LinkerScript<ELFT>::processCommands(OutputSectionFactory<ELFT> &Factory) { 27728c1597aSRafael Espindola 27848c3f1ceSRui Ueyama for (const std::unique_ptr<BaseCommand> &Base1 : Opt.Commands) { 2792ab5f73dSRui Ueyama if (auto *Cmd = dyn_cast<SymbolAssignment>(Base1.get())) { 2802ab5f73dSRui Ueyama if (shouldDefine<ELFT>(Cmd)) 2812ab5f73dSRui Ueyama addRegular<ELFT>(Cmd); 2822ab5f73dSRui Ueyama continue; 2832ab5f73dSRui Ueyama } 28420d03194SEugene Leviant if (auto *Cmd = dyn_cast<AssertCommand>(Base1.get())) { 28520d03194SEugene Leviant // If we don't have SECTIONS then output sections have already been 28620d03194SEugene Leviant // created by Writer<EFLT>. The LinkerScript<ELFT>::assignAddresses 28720d03194SEugene Leviant // will not be called, so ASSERT should be evaluated now. 28820d03194SEugene Leviant if (!Opt.HasSections) 28920d03194SEugene Leviant Cmd->Expression(0); 29020d03194SEugene Leviant continue; 29120d03194SEugene Leviant } 2922ab5f73dSRui Ueyama 293ceabe80eSEugene Leviant if (auto *Cmd = dyn_cast<OutputSectionCommand>(Base1.get())) { 2947bd37870SRafael Espindola std::vector<InputSectionBase<ELFT> *> V = createInputSectionList(*Cmd); 2957bd37870SRafael Espindola 29648c3f1ceSRui Ueyama if (Cmd->Name == "/DISCARD/") { 2977bd37870SRafael Espindola discard(V); 29848c3f1ceSRui Ueyama continue; 29948c3f1ceSRui Ueyama } 3000b9ce6a4SRui Ueyama 30197403d15SEugene Leviant if (V.empty()) 3020b9ce6a4SRui Ueyama continue; 3030b9ce6a4SRui Ueyama 304a14b13d8SGeorge Rimar for (InputSectionBase<ELFT> *Sec : V) { 30520d03194SEugene Leviant addSection(Factory, Sec, Cmd->Name); 30620d03194SEugene Leviant if (uint32_t Subalign = Cmd->SubalignExpr ? Cmd->SubalignExpr(0) : 0) 307db24d9c3SGeorge Rimar Sec->Alignment = Subalign; 30820d03194SEugene Leviant } 309eea3114fSGeorge Rimar } 31048c3f1ceSRui Ueyama } 311db24d9c3SGeorge Rimar } 312e63d81bdSEugene Leviant 31320d03194SEugene Leviant template <class ELFT> 31420d03194SEugene Leviant void LinkerScript<ELFT>::createSections(OutputSectionFactory<ELFT> &Factory) { 31520d03194SEugene Leviant processCommands(Factory); 3160b9ce6a4SRui Ueyama // Add orphan sections. 31720d03194SEugene Leviant for (ObjectFile<ELFT> *F : Symtab<ELFT>::X->getObjectFiles()) 31820d03194SEugene Leviant for (InputSectionBase<ELFT> *S : F->getSections()) 31920d03194SEugene Leviant if (!isDiscarded(S) && !S->OutSec) 32020d03194SEugene Leviant addSection(Factory, S, getOutputSectionName(S)); 321e63d81bdSEugene Leviant } 322e63d81bdSEugene Leviant 323db741e72SEugene Leviant // Sets value of a section-defined symbol. Two kinds of 324db741e72SEugene Leviant // symbols are processed: synthetic symbols, whose value 325db741e72SEugene Leviant // is an offset from beginning of section and regular 326db741e72SEugene Leviant // symbols whose value is absolute. 327db741e72SEugene Leviant template <class ELFT> 328db741e72SEugene Leviant static void assignSectionSymbol(SymbolAssignment *Cmd, 329db741e72SEugene Leviant OutputSectionBase<ELFT> *Sec, 330db741e72SEugene Leviant typename ELFT::uint Off) { 331db741e72SEugene Leviant if (!Cmd->Sym) 332db741e72SEugene Leviant return; 333db741e72SEugene Leviant 334db741e72SEugene Leviant if (auto *Body = dyn_cast<DefinedSynthetic<ELFT>>(Cmd->Sym)) { 335db741e72SEugene Leviant Body->Section = Sec; 336db741e72SEugene Leviant Body->Value = Cmd->Expression(Sec->getVA() + Off) - Sec->getVA(); 337db741e72SEugene Leviant return; 338db741e72SEugene Leviant } 339db741e72SEugene Leviant auto *Body = cast<DefinedRegular<ELFT>>(Cmd->Sym); 340db741e72SEugene Leviant Body->Value = Cmd->Expression(Sec->getVA() + Off); 341db741e72SEugene Leviant } 342db741e72SEugene Leviant 343d3190795SRafael Espindola template <class ELFT> void LinkerScript<ELFT>::output(InputSection<ELFT> *S) { 344d3190795SRafael Espindola if (!AlreadyOutputIS.insert(S).second) 345ceabe80eSEugene Leviant return; 346d3190795SRafael Espindola bool IsTbss = 347d3190795SRafael Espindola (CurOutSec->getFlags() & SHF_TLS) && CurOutSec->getType() == SHT_NOBITS; 348d3190795SRafael Espindola 349d3190795SRafael Espindola uintX_t Pos = IsTbss ? Dot + ThreadBssOffset : Dot; 350d3190795SRafael Espindola Pos = alignTo(Pos, S->Alignment); 351d3190795SRafael Espindola S->OutSecOff = Pos - CurOutSec->getVA(); 352d3190795SRafael Espindola Pos += S->getSize(); 353d3190795SRafael Espindola 354d3190795SRafael Espindola // Update output section size after adding each section. This is so that 355d3190795SRafael Espindola // SIZEOF works correctly in the case below: 356d3190795SRafael Espindola // .foo { *(.aaa) a = SIZEOF(.foo); *(.bbb) } 357d3190795SRafael Espindola CurOutSec->setSize(Pos - CurOutSec->getVA()); 358d3190795SRafael Espindola 359d3190795SRafael Espindola if (!IsTbss) 360d3190795SRafael Espindola Dot = Pos; 3612de509c3SRui Ueyama } 362ceabe80eSEugene Leviant 363d3190795SRafael Espindola template <class ELFT> void LinkerScript<ELFT>::flush() { 364d3190795SRafael Espindola if (auto *OutSec = dyn_cast_or_null<OutputSection<ELFT>>(CurOutSec)) { 365d3190795SRafael Espindola for (InputSection<ELFT> *I : OutSec->Sections) 366d3190795SRafael Espindola output(I); 367d3190795SRafael Espindola AlreadyOutputOS.insert(CurOutSec); 368d3190795SRafael Espindola } 369d3190795SRafael Espindola } 37097403d15SEugene Leviant 371d3190795SRafael Espindola template <class ELFT> 372d3190795SRafael Espindola void LinkerScript<ELFT>::switchTo(OutputSectionBase<ELFT> *Sec) { 373d3190795SRafael Espindola if (CurOutSec == Sec) 374d3190795SRafael Espindola return; 375d3190795SRafael Espindola if (AlreadyOutputOS.count(Sec)) 376d3190795SRafael Espindola return; 377d3190795SRafael Espindola 378d3190795SRafael Espindola flush(); 379d3190795SRafael Espindola CurOutSec = Sec; 380d3190795SRafael Espindola 381d3190795SRafael Espindola Dot = alignTo(Dot, CurOutSec->getAlignment()); 382d3190795SRafael Espindola CurOutSec->setVA(Dot); 383d3190795SRafael Espindola } 384d3190795SRafael Espindola 385d3190795SRafael Espindola template <class ELFT> void LinkerScript<ELFT>::process(BaseCommand &Base) { 386d3190795SRafael Espindola if (auto *AssignCmd = dyn_cast<SymbolAssignment>(&Base)) { 38797403d15SEugene Leviant if (AssignCmd->Name == ".") { 38897403d15SEugene Leviant // Update to location counter means update to section size. 389d3190795SRafael Espindola Dot = AssignCmd->Expression(Dot); 390d3190795SRafael Espindola CurOutSec->setSize(Dot - CurOutSec->getVA()); 391d3190795SRafael Espindola return; 39297403d15SEugene Leviant } 393d3190795SRafael Espindola assignSectionSymbol<ELFT>(AssignCmd, CurOutSec, Dot - CurOutSec->getVA()); 394d3190795SRafael Espindola return; 39597403d15SEugene Leviant } 396d3190795SRafael Espindola auto &ICmd = cast<InputSectionDescription>(Base); 397d3190795SRafael Espindola for (InputSectionData *ID : ICmd.Sections) { 398d3190795SRafael Espindola auto *IB = static_cast<InputSectionBase<ELFT> *>(ID); 399d3190795SRafael Espindola switchTo(IB->OutSec); 400d3190795SRafael Espindola if (auto *I = dyn_cast<InputSection<ELFT>>(IB)) 401d3190795SRafael Espindola output(I); 402d3190795SRafael Espindola else if (AlreadyOutputOS.insert(CurOutSec).second) 403d3190795SRafael Espindola Dot += CurOutSec->getSize(); 404ceabe80eSEugene Leviant } 405ceabe80eSEugene Leviant } 406ceabe80eSEugene Leviant 4078f66df92SGeorge Rimar template <class ELFT> 408a14b13d8SGeorge Rimar static std::vector<OutputSectionBase<ELFT> *> 409a14b13d8SGeorge Rimar findSections(OutputSectionCommand &Cmd, 410d3190795SRafael Espindola const std::vector<OutputSectionBase<ELFT> *> &Sections) { 411a14b13d8SGeorge Rimar std::vector<OutputSectionBase<ELFT> *> Ret; 412a14b13d8SGeorge Rimar for (OutputSectionBase<ELFT> *Sec : Sections) 413a14b13d8SGeorge Rimar if (Sec->getName() == Cmd.Name && 414a14b13d8SGeorge Rimar checkConstraint(Sec->getFlags(), Cmd.Constraint)) 415a14b13d8SGeorge Rimar Ret.push_back(Sec); 416a14b13d8SGeorge Rimar return Ret; 4178f66df92SGeorge Rimar } 4188f66df92SGeorge Rimar 419d3190795SRafael Espindola template <class ELFT> 420d3190795SRafael Espindola void LinkerScript<ELFT>::assignOffsets(OutputSectionCommand *Cmd) { 421d3190795SRafael Espindola std::vector<OutputSectionBase<ELFT> *> Sections = 422d3190795SRafael Espindola findSections(*Cmd, *OutputSections); 423d3190795SRafael Espindola if (Sections.empty()) 424d3190795SRafael Espindola return; 425d3190795SRafael Espindola switchTo(Sections[0]); 426d3190795SRafael Espindola 427d3190795SRafael Espindola // Find the last section output location. We will output orphan sections 428d3190795SRafael Espindola // there so that end symbols point to the correct location. 429d3190795SRafael Espindola auto E = std::find_if(Cmd->Commands.rbegin(), Cmd->Commands.rend(), 430d3190795SRafael Espindola [](const std::unique_ptr<BaseCommand> &Cmd) { 431d3190795SRafael Espindola return !isa<SymbolAssignment>(*Cmd); 432d3190795SRafael Espindola }) 433d3190795SRafael Espindola .base(); 434d3190795SRafael Espindola for (auto I = Cmd->Commands.begin(); I != E; ++I) 435d3190795SRafael Espindola process(**I); 436d3190795SRafael Espindola flush(); 437d3190795SRafael Espindola for (OutputSectionBase<ELFT> *Base : Sections) { 438d3190795SRafael Espindola if (!AlreadyOutputOS.insert(Base).second) 439d3190795SRafael Espindola continue; 440d3190795SRafael Espindola switchTo(Base); 441d3190795SRafael Espindola Dot += CurOutSec->getSize(); 442d3190795SRafael Espindola } 443d3190795SRafael Espindola for (auto I = E, E = Cmd->Commands.end(); I != E; ++I) 444d3190795SRafael Espindola process(**I); 445d3190795SRafael Espindola } 446d3190795SRafael Espindola 447a4b41dcaSRafael Espindola template <class ELFT> void LinkerScript<ELFT>::assignAddresses() { 448652852c5SGeorge Rimar // Orphan sections are sections present in the input files which 4497c18c28cSRui Ueyama // are not explicitly placed into the output file by the linker script. 4507c18c28cSRui Ueyama // We place orphan sections at end of file. 4517c18c28cSRui Ueyama // Other linkers places them using some heuristics as described in 452652852c5SGeorge Rimar // https://sourceware.org/binutils/docs/ld/Orphan-Sections.html#Orphan-Sections. 453e5cc668eSRui Ueyama for (OutputSectionBase<ELFT> *Sec : *OutputSections) { 454652852c5SGeorge Rimar StringRef Name = Sec->getName(); 455c3e2a4b0SRui Ueyama if (getSectionIndex(Name) == INT_MAX) 456076fe157SGeorge Rimar Opt.Commands.push_back(llvm::make_unique<OutputSectionCommand>(Name)); 457652852c5SGeorge Rimar } 458652852c5SGeorge Rimar 4597c18c28cSRui Ueyama // Assign addresses as instructed by linker script SECTIONS sub-commands. 4604f7500bfSRui Ueyama Dot = getHeaderSize(); 461467c4d55SEugene Leviant uintX_t MinVA = std::numeric_limits<uintX_t>::max(); 462652852c5SGeorge Rimar 463076fe157SGeorge Rimar for (const std::unique_ptr<BaseCommand> &Base : Opt.Commands) { 464076fe157SGeorge Rimar if (auto *Cmd = dyn_cast<SymbolAssignment>(Base.get())) { 4658d083e6aSRui Ueyama if (Cmd->Name == ".") { 4668d083e6aSRui Ueyama Dot = Cmd->Expression(Dot); 4678d083e6aSRui Ueyama } else if (Cmd->Sym) { 4688d083e6aSRui Ueyama cast<DefinedRegular<ELFT>>(Cmd->Sym)->Value = Cmd->Expression(Dot); 4698d083e6aSRui Ueyama } 47005ef4cffSRui Ueyama continue; 471652852c5SGeorge Rimar } 472652852c5SGeorge Rimar 473eefa758eSGeorge Rimar if (auto *Cmd = dyn_cast<AssertCommand>(Base.get())) { 474eefa758eSGeorge Rimar Cmd->Expression(Dot); 475eefa758eSGeorge Rimar continue; 476eefa758eSGeorge Rimar } 477eefa758eSGeorge Rimar 478076fe157SGeorge Rimar auto *Cmd = cast<OutputSectionCommand>(Base.get()); 479652852c5SGeorge Rimar 48058e5c4dcSGeorge Rimar if (Cmd->AddrExpr) 48158e5c4dcSGeorge Rimar Dot = Cmd->AddrExpr(Dot); 48258e5c4dcSGeorge Rimar 483467c4d55SEugene Leviant MinVA = std::min(MinVA, Dot); 484d3190795SRafael Espindola assignOffsets(Cmd); 485a14b13d8SGeorge Rimar } 486467c4d55SEugene Leviant 487d3190795SRafael Espindola for (OutputSectionBase<ELFT> *Sec : *OutputSections) 488d3190795SRafael Espindola if (!(Sec->getFlags() & SHF_ALLOC)) 489d3190795SRafael Espindola Sec->setVA(0); 4904ec013acSRafael Espindola uintX_t HeaderSize = 4914ec013acSRafael Espindola Out<ELFT>::ElfHeader->getSize() + Out<ELFT>::ProgramHeaders->getSize(); 4924ec013acSRafael Espindola if (HeaderSize > MinVA) 4934ec013acSRafael Espindola fatal("Not enough space for ELF and program headers"); 4944ec013acSRafael Espindola 49564c32d6fSRafael Espindola // ELF and Program headers need to be right before the first section in 496b91e7118SGeorge Rimar // memory. Set their addresses accordingly. 4974ec013acSRafael Espindola MinVA = alignDown(MinVA - HeaderSize, Target->PageSize); 498467c4d55SEugene Leviant Out<ELFT>::ElfHeader->setVA(MinVA); 499467c4d55SEugene Leviant Out<ELFT>::ProgramHeaders->setVA(Out<ELFT>::ElfHeader->getSize() + MinVA); 500fb8978fcSDima Stepanov } 501652852c5SGeorge Rimar 502464daadcSRui Ueyama // Creates program headers as instructed by PHDRS linker script command. 50307320e40SRui Ueyama template <class ELFT> 504a4b41dcaSRafael Espindola std::vector<PhdrEntry<ELFT>> LinkerScript<ELFT>::createPhdrs() { 505edebbdf1SRui Ueyama std::vector<PhdrEntry<ELFT>> Ret; 506bbe38602SEugene Leviant 507464daadcSRui Ueyama // Process PHDRS and FILEHDR keywords because they are not 508464daadcSRui Ueyama // real output sections and cannot be added in the following loop. 509bbe38602SEugene Leviant for (const PhdrsCommand &Cmd : Opt.PhdrsCommands) { 510edebbdf1SRui Ueyama Ret.emplace_back(Cmd.Type, Cmd.Flags == UINT_MAX ? PF_R : Cmd.Flags); 511edebbdf1SRui Ueyama PhdrEntry<ELFT> &Phdr = Ret.back(); 512bbe38602SEugene Leviant 513bbe38602SEugene Leviant if (Cmd.HasFilehdr) 514adca245fSRui Ueyama Phdr.add(Out<ELFT>::ElfHeader); 515bbe38602SEugene Leviant if (Cmd.HasPhdrs) 516adca245fSRui Ueyama Phdr.add(Out<ELFT>::ProgramHeaders); 51756b21c86SEugene Leviant 51856b21c86SEugene Leviant if (Cmd.LMAExpr) { 51956b21c86SEugene Leviant Phdr.H.p_paddr = Cmd.LMAExpr(0); 52056b21c86SEugene Leviant Phdr.HasLMA = true; 52156b21c86SEugene Leviant } 522bbe38602SEugene Leviant } 523bbe38602SEugene Leviant 524464daadcSRui Ueyama // Add output sections to program headers. 525edebbdf1SRui Ueyama PhdrEntry<ELFT> *Load = nullptr; 526edebbdf1SRui Ueyama uintX_t Flags = PF_R; 527464daadcSRui Ueyama for (OutputSectionBase<ELFT> *Sec : *OutputSections) { 528bbe38602SEugene Leviant if (!(Sec->getFlags() & SHF_ALLOC)) 529bbe38602SEugene Leviant break; 530bbe38602SEugene Leviant 531edebbdf1SRui Ueyama std::vector<size_t> PhdrIds = getPhdrIndices(Sec->getName()); 532bbe38602SEugene Leviant if (!PhdrIds.empty()) { 533bbe38602SEugene Leviant // Assign headers specified by linker script 534bbe38602SEugene Leviant for (size_t Id : PhdrIds) { 535edebbdf1SRui Ueyama Ret[Id].add(Sec); 536865bf863SEugene Leviant if (Opt.PhdrsCommands[Id].Flags == UINT_MAX) 5370b113671SRafael Espindola Ret[Id].H.p_flags |= Sec->getPhdrFlags(); 538bbe38602SEugene Leviant } 539bbe38602SEugene Leviant } else { 540bbe38602SEugene Leviant // If we have no load segment or flags've changed then we want new load 541bbe38602SEugene Leviant // segment. 5420b113671SRafael Espindola uintX_t NewFlags = Sec->getPhdrFlags(); 543bbe38602SEugene Leviant if (Load == nullptr || Flags != NewFlags) { 544edebbdf1SRui Ueyama Load = &*Ret.emplace(Ret.end(), PT_LOAD, NewFlags); 545bbe38602SEugene Leviant Flags = NewFlags; 546bbe38602SEugene Leviant } 54718f084ffSRui Ueyama Load->add(Sec); 548bbe38602SEugene Leviant } 549bbe38602SEugene Leviant } 550edebbdf1SRui Ueyama return Ret; 551bbe38602SEugene Leviant } 552bbe38602SEugene Leviant 553f9bc3bd2SEugene Leviant template <class ELFT> bool LinkerScript<ELFT>::ignoreInterpSection() { 554f9bc3bd2SEugene Leviant // Ignore .interp section in case we have PHDRS specification 555f9bc3bd2SEugene Leviant // and PT_INTERP isn't listed. 556f9bc3bd2SEugene Leviant return !Opt.PhdrsCommands.empty() && 557f9bc3bd2SEugene Leviant llvm::find_if(Opt.PhdrsCommands, [](const PhdrsCommand &Cmd) { 558f9bc3bd2SEugene Leviant return Cmd.Type == PT_INTERP; 559f9bc3bd2SEugene Leviant }) == Opt.PhdrsCommands.end(); 560f9bc3bd2SEugene Leviant } 561f9bc3bd2SEugene Leviant 562bbe38602SEugene Leviant template <class ELFT> 56307320e40SRui Ueyama ArrayRef<uint8_t> LinkerScript<ELFT>::getFiller(StringRef Name) { 564f6c3ccefSGeorge Rimar for (const std::unique_ptr<BaseCommand> &Base : Opt.Commands) 565f6c3ccefSGeorge Rimar if (auto *Cmd = dyn_cast<OutputSectionCommand>(Base.get())) 566f6c3ccefSGeorge Rimar if (Cmd->Name == Name) 567f6c3ccefSGeorge Rimar return Cmd->Filler; 568e2ee72b5SGeorge Rimar return {}; 569e2ee72b5SGeorge Rimar } 570e2ee72b5SGeorge Rimar 571206fffa1SGeorge Rimar template <class ELFT> Expr LinkerScript<ELFT>::getLma(StringRef Name) { 5728ceadb38SGeorge Rimar for (const std::unique_ptr<BaseCommand> &Base : Opt.Commands) 5738ceadb38SGeorge Rimar if (auto *Cmd = dyn_cast<OutputSectionCommand>(Base.get())) 5748ceadb38SGeorge Rimar if (Cmd->LmaExpr && Cmd->Name == Name) 5758ceadb38SGeorge Rimar return Cmd->LmaExpr; 5768ceadb38SGeorge Rimar return {}; 5778ceadb38SGeorge Rimar } 5788ceadb38SGeorge Rimar 579c3e2a4b0SRui Ueyama // Returns the index of the given section name in linker script 580c3e2a4b0SRui Ueyama // SECTIONS commands. Sections are laid out as the same order as they 581c3e2a4b0SRui Ueyama // were in the script. If a given name did not appear in the script, 582c3e2a4b0SRui Ueyama // it returns INT_MAX, so that it will be laid out at end of file. 583076fe157SGeorge Rimar template <class ELFT> int LinkerScript<ELFT>::getSectionIndex(StringRef Name) { 584f510fa6bSRui Ueyama int I = 0; 585f510fa6bSRui Ueyama for (std::unique_ptr<BaseCommand> &Base : Opt.Commands) { 586076fe157SGeorge Rimar if (auto *Cmd = dyn_cast<OutputSectionCommand>(Base.get())) 587076fe157SGeorge Rimar if (Cmd->Name == Name) 588f510fa6bSRui Ueyama return I; 589f510fa6bSRui Ueyama ++I; 590f510fa6bSRui Ueyama } 591f510fa6bSRui Ueyama return INT_MAX; 59271b26e94SGeorge Rimar } 59371b26e94SGeorge Rimar 59471b26e94SGeorge Rimar // A compartor to sort output sections. Returns -1 or 1 if 59571b26e94SGeorge Rimar // A or B are mentioned in linker script. Otherwise, returns 0. 59607320e40SRui Ueyama template <class ELFT> 59707320e40SRui Ueyama int LinkerScript<ELFT>::compareSections(StringRef A, StringRef B) { 598c3e2a4b0SRui Ueyama int I = getSectionIndex(A); 599c3e2a4b0SRui Ueyama int J = getSectionIndex(B); 600c3e2a4b0SRui Ueyama if (I == INT_MAX && J == INT_MAX) 601717677afSRui Ueyama return 0; 602717677afSRui Ueyama return I < J ? -1 : 1; 603717677afSRui Ueyama } 604717677afSRui Ueyama 605bbe38602SEugene Leviant template <class ELFT> bool LinkerScript<ELFT>::hasPhdrsCommands() { 606bbe38602SEugene Leviant return !Opt.PhdrsCommands.empty(); 607bbe38602SEugene Leviant } 608bbe38602SEugene Leviant 6099e69450eSGeorge Rimar template <class ELFT> 610884e786dSGeorge Rimar uint64_t LinkerScript<ELFT>::getOutputSectionAddress(StringRef Name) { 61196659df0SGeorge Rimar for (OutputSectionBase<ELFT> *Sec : *OutputSections) 61296659df0SGeorge Rimar if (Sec->getName() == Name) 61396659df0SGeorge Rimar return Sec->getVA(); 61496659df0SGeorge Rimar error("undefined section " + Name); 61596659df0SGeorge Rimar return 0; 61696659df0SGeorge Rimar } 61796659df0SGeorge Rimar 61896659df0SGeorge Rimar template <class ELFT> 619884e786dSGeorge Rimar uint64_t LinkerScript<ELFT>::getOutputSectionSize(StringRef Name) { 6209e69450eSGeorge Rimar for (OutputSectionBase<ELFT> *Sec : *OutputSections) 6219e69450eSGeorge Rimar if (Sec->getName() == Name) 6229e69450eSGeorge Rimar return Sec->getSize(); 6239e69450eSGeorge Rimar error("undefined section " + Name); 6249e69450eSGeorge Rimar return 0; 6259e69450eSGeorge Rimar } 6269e69450eSGeorge Rimar 62736fac7f0SEugene Leviant template <class ELFT> 62836fac7f0SEugene Leviant uint64_t LinkerScript<ELFT>::getOutputSectionAlign(StringRef Name) { 62936fac7f0SEugene Leviant for (OutputSectionBase<ELFT> *Sec : *OutputSections) 63036fac7f0SEugene Leviant if (Sec->getName() == Name) 63136fac7f0SEugene Leviant return Sec->getAlignment(); 63236fac7f0SEugene Leviant error("undefined section " + Name); 63336fac7f0SEugene Leviant return 0; 63436fac7f0SEugene Leviant } 63536fac7f0SEugene Leviant 636884e786dSGeorge Rimar template <class ELFT> uint64_t LinkerScript<ELFT>::getHeaderSize() { 637e32a3598SGeorge Rimar return Out<ELFT>::ElfHeader->getSize() + Out<ELFT>::ProgramHeaders->getSize(); 638e32a3598SGeorge Rimar } 639e32a3598SGeorge Rimar 640884e786dSGeorge Rimar template <class ELFT> uint64_t LinkerScript<ELFT>::getSymbolValue(StringRef S) { 641884e786dSGeorge Rimar if (SymbolBody *B = Symtab<ELFT>::X->find(S)) 642884e786dSGeorge Rimar return B->getVA<ELFT>(); 643884e786dSGeorge Rimar error("symbol not found: " + S); 644884e786dSGeorge Rimar return 0; 645884e786dSGeorge Rimar } 646884e786dSGeorge Rimar 647bbe38602SEugene Leviant // Returns indices of ELF headers containing specific section, identified 648bbe38602SEugene Leviant // by Name. Each index is a zero based number of ELF header listed within 649bbe38602SEugene Leviant // PHDRS {} script block. 650bbe38602SEugene Leviant template <class ELFT> 651edebbdf1SRui Ueyama std::vector<size_t> LinkerScript<ELFT>::getPhdrIndices(StringRef SectionName) { 652076fe157SGeorge Rimar for (const std::unique_ptr<BaseCommand> &Base : Opt.Commands) { 653076fe157SGeorge Rimar auto *Cmd = dyn_cast<OutputSectionCommand>(Base.get()); 654edebbdf1SRui Ueyama if (!Cmd || Cmd->Name != SectionName) 65531d842f5SGeorge Rimar continue; 65631d842f5SGeorge Rimar 65729c5a2a9SRui Ueyama std::vector<size_t> Ret; 65829c5a2a9SRui Ueyama for (StringRef PhdrName : Cmd->Phdrs) 65929c5a2a9SRui Ueyama Ret.push_back(getPhdrIndex(PhdrName)); 66029c5a2a9SRui Ueyama return Ret; 661bbe38602SEugene Leviant } 66231d842f5SGeorge Rimar return {}; 66331d842f5SGeorge Rimar } 664bbe38602SEugene Leviant 66529c5a2a9SRui Ueyama template <class ELFT> 66629c5a2a9SRui Ueyama size_t LinkerScript<ELFT>::getPhdrIndex(StringRef PhdrName) { 66729c5a2a9SRui Ueyama size_t I = 0; 66829c5a2a9SRui Ueyama for (PhdrsCommand &Cmd : Opt.PhdrsCommands) { 66929c5a2a9SRui Ueyama if (Cmd.Name == PhdrName) 67029c5a2a9SRui Ueyama return I; 67129c5a2a9SRui Ueyama ++I; 67229c5a2a9SRui Ueyama } 67329c5a2a9SRui Ueyama error("section header '" + PhdrName + "' is not listed in PHDRS"); 67429c5a2a9SRui Ueyama return 0; 67529c5a2a9SRui Ueyama } 67629c5a2a9SRui Ueyama 67707320e40SRui Ueyama class elf::ScriptParser : public ScriptParserBase { 678c3794e58SGeorge Rimar typedef void (ScriptParser::*Handler)(); 679c3794e58SGeorge Rimar 680f7c5fbb1SRui Ueyama public: 68107320e40SRui Ueyama ScriptParser(StringRef S, bool B) : ScriptParserBase(S), IsUnderSysroot(B) {} 682f23b2320SGeorge Rimar 68320b6598cSGeorge Rimar void readLinkerScript(); 68420b6598cSGeorge Rimar void readVersionScript(); 685f7c5fbb1SRui Ueyama 686f7c5fbb1SRui Ueyama private: 68752a1509eSRui Ueyama void addFile(StringRef Path); 68852a1509eSRui Ueyama 689f7c5fbb1SRui Ueyama void readAsNeeded(); 69090c5099eSDenis Protivensky void readEntry(); 69183f406cfSGeorge Rimar void readExtern(); 692f7c5fbb1SRui Ueyama void readGroup(); 69331aa1f83SRui Ueyama void readInclude(); 694ee59282bSRui Ueyama void readOutput(); 6959159ce93SDavide Italiano void readOutputArch(); 696f7c5fbb1SRui Ueyama void readOutputFormat(); 697bbe38602SEugene Leviant void readPhdrs(); 69868a39a65SDavide Italiano void readSearchDir(); 6998e3b38abSDenis Protivensky void readSections(); 70095769b4aSRui Ueyama void readVersion(); 70195769b4aSRui Ueyama void readVersionScriptCommand(); 7028e3b38abSDenis Protivensky 703113cdec9SRui Ueyama SymbolAssignment *readAssignment(StringRef Name); 704ff1f29e0SGeorge Rimar std::vector<uint8_t> readFill(); 70510416564SRui Ueyama OutputSectionCommand *readOutputSectionDescription(StringRef OutSec); 706ff1f29e0SGeorge Rimar std::vector<uint8_t> readOutputSectionFiller(StringRef Tok); 707bbe38602SEugene Leviant std::vector<StringRef> readOutputSectionPhdrs(); 708a2496cbeSGeorge Rimar InputSectionDescription *readInputSectionDescription(StringRef Tok); 709c91930a1SGeorge Rimar Regex readFilePatterns(); 710395281cfSGeorge Rimar void readSectionExcludes(InputSectionDescription *Cmd); 711a2496cbeSGeorge Rimar InputSectionDescription *readInputSectionRules(StringRef FilePattern); 712bbe38602SEugene Leviant unsigned readPhdrType(); 713be394db3SGeorge Rimar SortSectionPolicy readSortKind(); 714a35e39caSPetr Hosek SymbolAssignment *readProvideHidden(bool Provide, bool Hidden); 715db741e72SEugene Leviant SymbolAssignment *readProvideOrAssignment(StringRef Tok, bool MakeAbsolute); 71603fc010eSGeorge Rimar void readSort(); 717eefa758eSGeorge Rimar Expr readAssert(); 718708019c4SRui Ueyama 719708019c4SRui Ueyama Expr readExpr(); 720708019c4SRui Ueyama Expr readExpr1(Expr Lhs, int MinPrec); 721708019c4SRui Ueyama Expr readPrimary(); 722708019c4SRui Ueyama Expr readTernary(Expr Cond); 7236ad7dfccSRui Ueyama Expr readParenExpr(); 724f7c5fbb1SRui Ueyama 72520b6598cSGeorge Rimar // For parsing version script. 72620b6598cSGeorge Rimar void readExtern(std::vector<SymbolVersion> *Globals); 72795769b4aSRui Ueyama void readVersionDeclaration(StringRef VerStr); 72820b6598cSGeorge Rimar void readGlobal(StringRef VerStr); 72920b6598cSGeorge Rimar void readLocal(); 73020b6598cSGeorge Rimar 73107320e40SRui Ueyama ScriptConfiguration &Opt = *ScriptConfig; 73207320e40SRui Ueyama StringSaver Saver = {ScriptConfig->Alloc}; 73316b0cc9eSSimon Atanasyan bool IsUnderSysroot; 734f7c5fbb1SRui Ueyama }; 735f7c5fbb1SRui Ueyama 73620b6598cSGeorge Rimar void ScriptParser::readVersionScript() { 73795769b4aSRui Ueyama readVersionScriptCommand(); 73820b6598cSGeorge Rimar if (!atEOF()) 73995769b4aSRui Ueyama setError("EOF expected, but got " + next()); 74095769b4aSRui Ueyama } 74195769b4aSRui Ueyama 74295769b4aSRui Ueyama void ScriptParser::readVersionScriptCommand() { 74395769b4aSRui Ueyama if (skip("{")) { 74495769b4aSRui Ueyama readVersionDeclaration(""); 74520b6598cSGeorge Rimar return; 74620b6598cSGeorge Rimar } 74720b6598cSGeorge Rimar 74895769b4aSRui Ueyama while (!atEOF() && !Error && peek() != "}") { 74920b6598cSGeorge Rimar StringRef VerStr = next(); 75020b6598cSGeorge Rimar if (VerStr == "{") { 75195769b4aSRui Ueyama setError("anonymous version definition is used in " 75295769b4aSRui Ueyama "combination with other version definitions"); 75320b6598cSGeorge Rimar return; 75420b6598cSGeorge Rimar } 75520b6598cSGeorge Rimar expect("{"); 75695769b4aSRui Ueyama readVersionDeclaration(VerStr); 75720b6598cSGeorge Rimar } 75820b6598cSGeorge Rimar } 75920b6598cSGeorge Rimar 76095769b4aSRui Ueyama void ScriptParser::readVersion() { 76195769b4aSRui Ueyama expect("{"); 76295769b4aSRui Ueyama readVersionScriptCommand(); 76395769b4aSRui Ueyama expect("}"); 76495769b4aSRui Ueyama } 76595769b4aSRui Ueyama 76620b6598cSGeorge Rimar void ScriptParser::readLinkerScript() { 767f7c5fbb1SRui Ueyama while (!atEOF()) { 768f7c5fbb1SRui Ueyama StringRef Tok = next(); 769a27eeccaSRui Ueyama if (Tok == ";") 770a27eeccaSRui Ueyama continue; 771a27eeccaSRui Ueyama 77220d03194SEugene Leviant if (Tok == "ASSERT") { 77320d03194SEugene Leviant Opt.Commands.emplace_back(new AssertCommand(readAssert())); 77420d03194SEugene Leviant } else if (Tok == "ENTRY") { 775a27eeccaSRui Ueyama readEntry(); 776a27eeccaSRui Ueyama } else if (Tok == "EXTERN") { 777a27eeccaSRui Ueyama readExtern(); 778a27eeccaSRui Ueyama } else if (Tok == "GROUP" || Tok == "INPUT") { 779a27eeccaSRui Ueyama readGroup(); 780a27eeccaSRui Ueyama } else if (Tok == "INCLUDE") { 781a27eeccaSRui Ueyama readInclude(); 782a27eeccaSRui Ueyama } else if (Tok == "OUTPUT") { 783a27eeccaSRui Ueyama readOutput(); 784a27eeccaSRui Ueyama } else if (Tok == "OUTPUT_ARCH") { 785a27eeccaSRui Ueyama readOutputArch(); 786a27eeccaSRui Ueyama } else if (Tok == "OUTPUT_FORMAT") { 787a27eeccaSRui Ueyama readOutputFormat(); 788a27eeccaSRui Ueyama } else if (Tok == "PHDRS") { 789a27eeccaSRui Ueyama readPhdrs(); 790a27eeccaSRui Ueyama } else if (Tok == "SEARCH_DIR") { 791a27eeccaSRui Ueyama readSearchDir(); 792a27eeccaSRui Ueyama } else if (Tok == "SECTIONS") { 793a27eeccaSRui Ueyama readSections(); 794a27eeccaSRui Ueyama } else if (Tok == "VERSION") { 795a27eeccaSRui Ueyama readVersion(); 796db741e72SEugene Leviant } else if (SymbolAssignment *Cmd = readProvideOrAssignment(Tok, true)) { 7970df80befSPetr Hosek Opt.Commands.emplace_back(Cmd); 798e5d3ca50SPetr Hosek } else { 7995761042dSGeorge Rimar setError("unknown directive: " + Tok); 800f7c5fbb1SRui Ueyama } 801f7c5fbb1SRui Ueyama } 802e5d3ca50SPetr Hosek } 803f7c5fbb1SRui Ueyama 804717677afSRui Ueyama void ScriptParser::addFile(StringRef S) { 80516b0cc9eSSimon Atanasyan if (IsUnderSysroot && S.startswith("/")) { 80616b0cc9eSSimon Atanasyan SmallString<128> Path; 80716b0cc9eSSimon Atanasyan (Config->Sysroot + S).toStringRef(Path); 80816b0cc9eSSimon Atanasyan if (sys::fs::exists(Path)) { 80916b0cc9eSSimon Atanasyan Driver->addFile(Saver.save(Path.str())); 81016b0cc9eSSimon Atanasyan return; 81116b0cc9eSSimon Atanasyan } 81216b0cc9eSSimon Atanasyan } 81316b0cc9eSSimon Atanasyan 814f03f3cc1SRui Ueyama if (sys::path::is_absolute(S)) { 81552a1509eSRui Ueyama Driver->addFile(S); 81652a1509eSRui Ueyama } else if (S.startswith("=")) { 81752a1509eSRui Ueyama if (Config->Sysroot.empty()) 81852a1509eSRui Ueyama Driver->addFile(S.substr(1)); 81952a1509eSRui Ueyama else 82052a1509eSRui Ueyama Driver->addFile(Saver.save(Config->Sysroot + "/" + S.substr(1))); 82152a1509eSRui Ueyama } else if (S.startswith("-l")) { 82221eecb4fSRui Ueyama Driver->addLibrary(S.substr(2)); 823a1b8fc3bSSimon Atanasyan } else if (sys::fs::exists(S)) { 824a1b8fc3bSSimon Atanasyan Driver->addFile(S); 82552a1509eSRui Ueyama } else { 82652a1509eSRui Ueyama std::string Path = findFromSearchPaths(S); 82752a1509eSRui Ueyama if (Path.empty()) 828777f9630SGeorge Rimar setError("unable to find " + S); 829025d59b1SRui Ueyama else 83052a1509eSRui Ueyama Driver->addFile(Saver.save(Path)); 83152a1509eSRui Ueyama } 83252a1509eSRui Ueyama } 83352a1509eSRui Ueyama 834717677afSRui Ueyama void ScriptParser::readAsNeeded() { 835f7c5fbb1SRui Ueyama expect("("); 83635da9b6eSRui Ueyama bool Orig = Config->AsNeeded; 83735da9b6eSRui Ueyama Config->AsNeeded = true; 838a2acc931SRui Ueyama while (!Error && !skip(")")) 839cd574a5eSGeorge Rimar addFile(unquote(next())); 84035da9b6eSRui Ueyama Config->AsNeeded = Orig; 841f7c5fbb1SRui Ueyama } 842f7c5fbb1SRui Ueyama 843717677afSRui Ueyama void ScriptParser::readEntry() { 84490c5099eSDenis Protivensky // -e <symbol> takes predecence over ENTRY(<symbol>). 84590c5099eSDenis Protivensky expect("("); 84690c5099eSDenis Protivensky StringRef Tok = next(); 84790c5099eSDenis Protivensky if (Config->Entry.empty()) 84890c5099eSDenis Protivensky Config->Entry = Tok; 84990c5099eSDenis Protivensky expect(")"); 85090c5099eSDenis Protivensky } 85190c5099eSDenis Protivensky 852717677afSRui Ueyama void ScriptParser::readExtern() { 85383f406cfSGeorge Rimar expect("("); 854a2acc931SRui Ueyama while (!Error && !skip(")")) 855a2acc931SRui Ueyama Config->Undefined.push_back(next()); 85683f406cfSGeorge Rimar } 85783f406cfSGeorge Rimar 858717677afSRui Ueyama void ScriptParser::readGroup() { 859f7c5fbb1SRui Ueyama expect("("); 860a2acc931SRui Ueyama while (!Error && !skip(")")) { 861f7c5fbb1SRui Ueyama StringRef Tok = next(); 862a2acc931SRui Ueyama if (Tok == "AS_NEEDED") 863f7c5fbb1SRui Ueyama readAsNeeded(); 864a2acc931SRui Ueyama else 865cd574a5eSGeorge Rimar addFile(unquote(Tok)); 866f7c5fbb1SRui Ueyama } 867f7c5fbb1SRui Ueyama } 868f7c5fbb1SRui Ueyama 869717677afSRui Ueyama void ScriptParser::readInclude() { 87031aa1f83SRui Ueyama StringRef Tok = next(); 871cd574a5eSGeorge Rimar auto MBOrErr = MemoryBuffer::getFile(unquote(Tok)); 872025d59b1SRui Ueyama if (!MBOrErr) { 8735761042dSGeorge Rimar setError("cannot open " + Tok); 874025d59b1SRui Ueyama return; 875025d59b1SRui Ueyama } 87631aa1f83SRui Ueyama std::unique_ptr<MemoryBuffer> &MB = *MBOrErr; 877a47ee68dSRui Ueyama StringRef S = Saver.save(MB->getMemBufferRef().getBuffer()); 878a47ee68dSRui Ueyama std::vector<StringRef> V = tokenize(S); 87931aa1f83SRui Ueyama Tokens.insert(Tokens.begin() + Pos, V.begin(), V.end()); 88031aa1f83SRui Ueyama } 88131aa1f83SRui Ueyama 882717677afSRui Ueyama void ScriptParser::readOutput() { 883ee59282bSRui Ueyama // -o <file> takes predecence over OUTPUT(<file>). 884ee59282bSRui Ueyama expect("("); 885ee59282bSRui Ueyama StringRef Tok = next(); 886ee59282bSRui Ueyama if (Config->OutputFile.empty()) 887cd574a5eSGeorge Rimar Config->OutputFile = unquote(Tok); 888ee59282bSRui Ueyama expect(")"); 889ee59282bSRui Ueyama } 890ee59282bSRui Ueyama 891717677afSRui Ueyama void ScriptParser::readOutputArch() { 8929159ce93SDavide Italiano // Error checking only for now. 8939159ce93SDavide Italiano expect("("); 8949159ce93SDavide Italiano next(); 8959159ce93SDavide Italiano expect(")"); 8969159ce93SDavide Italiano } 8979159ce93SDavide Italiano 898717677afSRui Ueyama void ScriptParser::readOutputFormat() { 899f7c5fbb1SRui Ueyama // Error checking only for now. 900f7c5fbb1SRui Ueyama expect("("); 901f7c5fbb1SRui Ueyama next(); 9026836c618SDavide Italiano StringRef Tok = next(); 9036836c618SDavide Italiano if (Tok == ")") 9046836c618SDavide Italiano return; 905025d59b1SRui Ueyama if (Tok != ",") { 9065761042dSGeorge Rimar setError("unexpected token: " + Tok); 907025d59b1SRui Ueyama return; 908025d59b1SRui Ueyama } 9096836c618SDavide Italiano next(); 9106836c618SDavide Italiano expect(","); 9116836c618SDavide Italiano next(); 912f7c5fbb1SRui Ueyama expect(")"); 913f7c5fbb1SRui Ueyama } 914f7c5fbb1SRui Ueyama 915bbe38602SEugene Leviant void ScriptParser::readPhdrs() { 916bbe38602SEugene Leviant expect("{"); 917bbe38602SEugene Leviant while (!Error && !skip("}")) { 918bbe38602SEugene Leviant StringRef Tok = next(); 91956b21c86SEugene Leviant Opt.PhdrsCommands.push_back( 92056b21c86SEugene Leviant {Tok, PT_NULL, false, false, UINT_MAX, nullptr}); 921bbe38602SEugene Leviant PhdrsCommand &PhdrCmd = Opt.PhdrsCommands.back(); 922bbe38602SEugene Leviant 923bbe38602SEugene Leviant PhdrCmd.Type = readPhdrType(); 924bbe38602SEugene Leviant do { 925bbe38602SEugene Leviant Tok = next(); 926bbe38602SEugene Leviant if (Tok == ";") 927bbe38602SEugene Leviant break; 928bbe38602SEugene Leviant if (Tok == "FILEHDR") 929bbe38602SEugene Leviant PhdrCmd.HasFilehdr = true; 930bbe38602SEugene Leviant else if (Tok == "PHDRS") 931bbe38602SEugene Leviant PhdrCmd.HasPhdrs = true; 93256b21c86SEugene Leviant else if (Tok == "AT") 93356b21c86SEugene Leviant PhdrCmd.LMAExpr = readParenExpr(); 934865bf863SEugene Leviant else if (Tok == "FLAGS") { 935865bf863SEugene Leviant expect("("); 936eb685cd7SRafael Espindola // Passing 0 for the value of dot is a bit of a hack. It means that 937eb685cd7SRafael Espindola // we accept expressions like ".|1". 938eb685cd7SRafael Espindola PhdrCmd.Flags = readExpr()(0); 939865bf863SEugene Leviant expect(")"); 940865bf863SEugene Leviant } else 941bbe38602SEugene Leviant setError("unexpected header attribute: " + Tok); 942bbe38602SEugene Leviant } while (!Error); 943bbe38602SEugene Leviant } 944bbe38602SEugene Leviant } 945bbe38602SEugene Leviant 946717677afSRui Ueyama void ScriptParser::readSearchDir() { 94768a39a65SDavide Italiano expect("("); 94886c5fb82SRui Ueyama StringRef Tok = next(); 9496c7ad13fSRui Ueyama if (!Config->Nostdlib) 950cd574a5eSGeorge Rimar Config->SearchPaths.push_back(unquote(Tok)); 95168a39a65SDavide Italiano expect(")"); 95268a39a65SDavide Italiano } 95368a39a65SDavide Italiano 954717677afSRui Ueyama void ScriptParser::readSections() { 955e05336ffSEugene Leviant Opt.HasSections = true; 9568e3b38abSDenis Protivensky expect("{"); 957652852c5SGeorge Rimar while (!Error && !skip("}")) { 958113cdec9SRui Ueyama StringRef Tok = next(); 959db741e72SEugene Leviant BaseCommand *Cmd = readProvideOrAssignment(Tok, true); 960ceabe80eSEugene Leviant if (!Cmd) { 961ceabe80eSEugene Leviant if (Tok == "ASSERT") 962eefa758eSGeorge Rimar Cmd = new AssertCommand(readAssert()); 963ceabe80eSEugene Leviant else 96410416564SRui Ueyama Cmd = readOutputSectionDescription(Tok); 9658e3b38abSDenis Protivensky } 96610416564SRui Ueyama Opt.Commands.emplace_back(Cmd); 967652852c5SGeorge Rimar } 968708019c4SRui Ueyama } 9698e3b38abSDenis Protivensky 970708019c4SRui Ueyama static int precedence(StringRef Op) { 971708019c4SRui Ueyama return StringSwitch<int>(Op) 972708019c4SRui Ueyama .Case("*", 4) 973708019c4SRui Ueyama .Case("/", 4) 974708019c4SRui Ueyama .Case("+", 3) 975708019c4SRui Ueyama .Case("-", 3) 976708019c4SRui Ueyama .Case("<", 2) 977708019c4SRui Ueyama .Case(">", 2) 978708019c4SRui Ueyama .Case(">=", 2) 979708019c4SRui Ueyama .Case("<=", 2) 980708019c4SRui Ueyama .Case("==", 2) 981708019c4SRui Ueyama .Case("!=", 2) 982708019c4SRui Ueyama .Case("&", 1) 983cc3dd629SRafael Espindola .Case("|", 1) 984708019c4SRui Ueyama .Default(-1); 985708019c4SRui Ueyama } 986708019c4SRui Ueyama 987c91930a1SGeorge Rimar Regex ScriptParser::readFilePatterns() { 98810416564SRui Ueyama std::vector<StringRef> V; 98910416564SRui Ueyama while (!Error && !skip(")")) 99010416564SRui Ueyama V.push_back(next()); 991c91930a1SGeorge Rimar return compileGlobPatterns(V); 9920702c4e8SGeorge Rimar } 9930702c4e8SGeorge Rimar 994be394db3SGeorge Rimar SortSectionPolicy ScriptParser::readSortKind() { 995742c3836SRui Ueyama if (skip("SORT") || skip("SORT_BY_NAME")) 996be394db3SGeorge Rimar return SortSectionPolicy::Name; 997742c3836SRui Ueyama if (skip("SORT_BY_ALIGNMENT")) 998be394db3SGeorge Rimar return SortSectionPolicy::Alignment; 999575208caSGeorge Rimar if (skip("SORT_BY_INIT_PRIORITY")) 1000be394db3SGeorge Rimar return SortSectionPolicy::Priority; 1001be394db3SGeorge Rimar // `SORT_NONE' disables section sorting by ignoring the command line 1002be394db3SGeorge Rimar // section sorting option. 1003be394db3SGeorge Rimar if (skip("SORT_NONE")) 1004be394db3SGeorge Rimar return SortSectionPolicy::IgnoreConfig; 1005be394db3SGeorge Rimar return SortSectionPolicy::None; 1006be394db3SGeorge Rimar } 1007be394db3SGeorge Rimar 1008be394db3SGeorge Rimar static void selectSortKind(InputSectionDescription *Cmd) { 1009be394db3SGeorge Rimar if (Cmd->SortOuter == SortSectionPolicy::IgnoreConfig) { 1010be394db3SGeorge Rimar Cmd->SortOuter = SortSectionPolicy::None; 1011be394db3SGeorge Rimar return; 1012be394db3SGeorge Rimar } 1013be394db3SGeorge Rimar 1014be394db3SGeorge Rimar if (Cmd->SortOuter != SortSectionPolicy::None) { 1015be394db3SGeorge Rimar // If the section sorting command in linker script is nested, the command 1016be394db3SGeorge Rimar // line option will be ignored. 1017be394db3SGeorge Rimar if (Cmd->SortInner != SortSectionPolicy::None) 1018be394db3SGeorge Rimar return; 1019be394db3SGeorge Rimar // If the section sorting command in linker script isn't nested, the 1020be394db3SGeorge Rimar // command line option will make the section sorting command to be treated 1021be394db3SGeorge Rimar // as nested sorting command. 1022be394db3SGeorge Rimar Cmd->SortInner = Config->SortSection; 1023be394db3SGeorge Rimar return; 1024be394db3SGeorge Rimar } 1025be394db3SGeorge Rimar // If sorting rule not specified, use command line option. 1026be394db3SGeorge Rimar Cmd->SortOuter = Config->SortSection; 1027742c3836SRui Ueyama } 1028742c3836SRui Ueyama 1029395281cfSGeorge Rimar // Method reads a list of sequence of excluded files and section globs given in 1030395281cfSGeorge Rimar // a following form: ((EXCLUDE_FILE(file_pattern+))? section_pattern+)+ 1031395281cfSGeorge Rimar // Example: *(.foo.1 EXCLUDE_FILE (*a.o) .foo.2 EXCLUDE_FILE (*b.o) .foo.3) 1032395281cfSGeorge Rimar void ScriptParser::readSectionExcludes(InputSectionDescription *Cmd) { 1033395281cfSGeorge Rimar llvm::Regex ExcludeFileRe; 1034395281cfSGeorge Rimar std::vector<StringRef> V; 1035395281cfSGeorge Rimar 1036395281cfSGeorge Rimar while (!Error) { 1037395281cfSGeorge Rimar if (skip(")")) { 1038395281cfSGeorge Rimar Cmd->SectionsVec.push_back( 1039395281cfSGeorge Rimar {std::move(ExcludeFileRe), compileGlobPatterns(V)}); 1040395281cfSGeorge Rimar return; 1041395281cfSGeorge Rimar } 1042395281cfSGeorge Rimar 1043395281cfSGeorge Rimar if (skip("EXCLUDE_FILE")) { 1044395281cfSGeorge Rimar if (!V.empty()) { 1045395281cfSGeorge Rimar Cmd->SectionsVec.push_back( 1046395281cfSGeorge Rimar {std::move(ExcludeFileRe), compileGlobPatterns(V)}); 1047395281cfSGeorge Rimar V.clear(); 1048395281cfSGeorge Rimar } 1049395281cfSGeorge Rimar 1050395281cfSGeorge Rimar expect("("); 1051395281cfSGeorge Rimar ExcludeFileRe = readFilePatterns(); 1052395281cfSGeorge Rimar continue; 1053395281cfSGeorge Rimar } 1054395281cfSGeorge Rimar 1055395281cfSGeorge Rimar V.push_back(next()); 1056395281cfSGeorge Rimar } 1057395281cfSGeorge Rimar } 1058395281cfSGeorge Rimar 1059a2496cbeSGeorge Rimar InputSectionDescription * 1060a2496cbeSGeorge Rimar ScriptParser::readInputSectionRules(StringRef FilePattern) { 1061c91930a1SGeorge Rimar auto *Cmd = new InputSectionDescription(FilePattern); 10620ed42b0cSDavide Italiano expect("("); 1063e7282797SDavide Italiano 1064742c3836SRui Ueyama // Read SORT(). 1065be394db3SGeorge Rimar SortSectionPolicy K1 = readSortKind(); 1066be394db3SGeorge Rimar if (K1 != SortSectionPolicy::None) { 1067742c3836SRui Ueyama Cmd->SortOuter = K1; 10680702c4e8SGeorge Rimar expect("("); 1069be394db3SGeorge Rimar SortSectionPolicy K2 = readSortKind(); 1070be394db3SGeorge Rimar if (K2 != SortSectionPolicy::None) { 1071742c3836SRui Ueyama Cmd->SortInner = K2; 1072350ece4eSGeorge Rimar expect("("); 1073395281cfSGeorge Rimar Cmd->SectionsVec.push_back({llvm::Regex(), readFilePatterns()}); 10740702c4e8SGeorge Rimar expect(")"); 1075350ece4eSGeorge Rimar } else { 1076395281cfSGeorge Rimar Cmd->SectionsVec.push_back({llvm::Regex(), readFilePatterns()}); 1077350ece4eSGeorge Rimar } 1078350ece4eSGeorge Rimar expect(")"); 1079be394db3SGeorge Rimar selectSortKind(Cmd); 108010416564SRui Ueyama return Cmd; 10810659800eSGeorge Rimar } 10820702c4e8SGeorge Rimar 1083be394db3SGeorge Rimar selectSortKind(Cmd); 1084395281cfSGeorge Rimar readSectionExcludes(Cmd); 108510416564SRui Ueyama return Cmd; 10860659800eSGeorge Rimar } 10870659800eSGeorge Rimar 1088a2496cbeSGeorge Rimar InputSectionDescription * 1089a2496cbeSGeorge Rimar ScriptParser::readInputSectionDescription(StringRef Tok) { 10900659800eSGeorge Rimar // Input section wildcard can be surrounded by KEEP. 10910659800eSGeorge Rimar // https://sourceware.org/binutils/docs/ld/Input-Section-Keep.html#Input-Section-Keep 1092a2496cbeSGeorge Rimar if (Tok == "KEEP") { 1093e7282797SDavide Italiano expect("("); 1094a2496cbeSGeorge Rimar StringRef FilePattern = next(); 1095a2496cbeSGeorge Rimar InputSectionDescription *Cmd = readInputSectionRules(FilePattern); 10960ed42b0cSDavide Italiano expect(")"); 1097395281cfSGeorge Rimar for (std::pair<llvm::Regex, llvm::Regex> &Regex : Cmd->SectionsVec) 1098395281cfSGeorge Rimar Opt.KeptSections.push_back(&Regex.second); 109910416564SRui Ueyama return Cmd; 110010416564SRui Ueyama } 1101a2496cbeSGeorge Rimar return readInputSectionRules(Tok); 11020659800eSGeorge Rimar } 11030659800eSGeorge Rimar 110403fc010eSGeorge Rimar void ScriptParser::readSort() { 110503fc010eSGeorge Rimar expect("("); 110603fc010eSGeorge Rimar expect("CONSTRUCTORS"); 110703fc010eSGeorge Rimar expect(")"); 110803fc010eSGeorge Rimar } 110903fc010eSGeorge Rimar 1110eefa758eSGeorge Rimar Expr ScriptParser::readAssert() { 1111eefa758eSGeorge Rimar expect("("); 1112eefa758eSGeorge Rimar Expr E = readExpr(); 1113eefa758eSGeorge Rimar expect(","); 1114cd574a5eSGeorge Rimar StringRef Msg = unquote(next()); 1115eefa758eSGeorge Rimar expect(")"); 1116eefa758eSGeorge Rimar return [=](uint64_t Dot) { 1117eefa758eSGeorge Rimar uint64_t V = E(Dot); 1118eefa758eSGeorge Rimar if (!V) 1119eefa758eSGeorge Rimar error(Msg); 1120eefa758eSGeorge Rimar return V; 1121eefa758eSGeorge Rimar }; 1122eefa758eSGeorge Rimar } 1123eefa758eSGeorge Rimar 112425150e8bSRui Ueyama // Reads a FILL(expr) command. We handle the FILL command as an 112525150e8bSRui Ueyama // alias for =fillexp section attribute, which is different from 112625150e8bSRui Ueyama // what GNU linkers do. 112725150e8bSRui Ueyama // https://sourceware.org/binutils/docs/ld/Output-Section-Data.html 1128ff1f29e0SGeorge Rimar std::vector<uint8_t> ScriptParser::readFill() { 1129ff1f29e0SGeorge Rimar expect("("); 1130ff1f29e0SGeorge Rimar std::vector<uint8_t> V = readOutputSectionFiller(next()); 1131ff1f29e0SGeorge Rimar expect(")"); 1132ff1f29e0SGeorge Rimar expect(";"); 1133ff1f29e0SGeorge Rimar return V; 1134ff1f29e0SGeorge Rimar } 1135ff1f29e0SGeorge Rimar 113610416564SRui Ueyama OutputSectionCommand * 113710416564SRui Ueyama ScriptParser::readOutputSectionDescription(StringRef OutSec) { 1138076fe157SGeorge Rimar OutputSectionCommand *Cmd = new OutputSectionCommand(OutSec); 113958e5c4dcSGeorge Rimar 114058e5c4dcSGeorge Rimar // Read an address expression. 114158e5c4dcSGeorge Rimar // https://sourceware.org/binutils/docs/ld/Output-Section-Address.html#Output-Section-Address 114258e5c4dcSGeorge Rimar if (peek() != ":") 114358e5c4dcSGeorge Rimar Cmd->AddrExpr = readExpr(); 114458e5c4dcSGeorge Rimar 11458e3b38abSDenis Protivensky expect(":"); 1146246f681eSDavide Italiano 11478ceadb38SGeorge Rimar if (skip("AT")) 11486ad7dfccSRui Ueyama Cmd->LmaExpr = readParenExpr(); 1149630c6179SGeorge Rimar if (skip("ALIGN")) 11506ad7dfccSRui Ueyama Cmd->AlignExpr = readParenExpr(); 1151db24d9c3SGeorge Rimar if (skip("SUBALIGN")) 1152db24d9c3SGeorge Rimar Cmd->SubalignExpr = readParenExpr(); 1153630c6179SGeorge Rimar 1154246f681eSDavide Italiano // Parse constraints. 1155246f681eSDavide Italiano if (skip("ONLY_IF_RO")) 1156efc4066bSRui Ueyama Cmd->Constraint = ConstraintKind::ReadOnly; 1157246f681eSDavide Italiano if (skip("ONLY_IF_RW")) 1158efc4066bSRui Ueyama Cmd->Constraint = ConstraintKind::ReadWrite; 11598e3b38abSDenis Protivensky expect("{"); 11608ec77e64SRui Ueyama 1161025d59b1SRui Ueyama while (!Error && !skip("}")) { 1162ceabe80eSEugene Leviant StringRef Tok = next(); 1163db741e72SEugene Leviant if (SymbolAssignment *Assignment = readProvideOrAssignment(Tok, false)) 1164ceabe80eSEugene Leviant Cmd->Commands.emplace_back(Assignment); 1165ff1f29e0SGeorge Rimar else if (Tok == "FILL") 1166ff1f29e0SGeorge Rimar Cmd->Filler = readFill(); 1167ceabe80eSEugene Leviant else if (Tok == "SORT") 116803fc010eSGeorge Rimar readSort(); 1169a2496cbeSGeorge Rimar else if (peek() == "(") 1170a2496cbeSGeorge Rimar Cmd->Commands.emplace_back(readInputSectionDescription(Tok)); 1171ceabe80eSEugene Leviant else 1172ceabe80eSEugene Leviant setError("unknown command " + Tok); 11738e3b38abSDenis Protivensky } 1174076fe157SGeorge Rimar Cmd->Phdrs = readOutputSectionPhdrs(); 1175ff1f29e0SGeorge Rimar if (peek().startswith("=")) 1176ff1f29e0SGeorge Rimar Cmd->Filler = readOutputSectionFiller(next().drop_front()); 117710416564SRui Ueyama return Cmd; 1178f71caa2bSRui Ueyama } 11798ec77e64SRui Ueyama 11802c8f1f04SRui Ueyama // Read "=<number>" where <number> is an octal/decimal/hexadecimal number. 11812c8f1f04SRui Ueyama // https://sourceware.org/binutils/docs/ld/Output-Section-Fill.html 11822c8f1f04SRui Ueyama // 11832c8f1f04SRui Ueyama // ld.gold is not fully compatible with ld.bfd. ld.bfd handles 11842c8f1f04SRui Ueyama // hexstrings as blobs of arbitrary sizes, while ld.gold handles them 11852c8f1f04SRui Ueyama // as 32-bit big-endian values. We will do the same as ld.gold does 11862c8f1f04SRui Ueyama // because it's simpler than what ld.bfd does. 1187ff1f29e0SGeorge Rimar std::vector<uint8_t> ScriptParser::readOutputSectionFiller(StringRef Tok) { 1188965827d6SRui Ueyama uint32_t V; 1189ff1f29e0SGeorge Rimar if (Tok.getAsInteger(0, V)) { 1190965827d6SRui Ueyama setError("invalid filler expression: " + Tok); 1191f71caa2bSRui Ueyama return {}; 1192e2ee72b5SGeorge Rimar } 1193965827d6SRui Ueyama return {uint8_t(V >> 24), uint8_t(V >> 16), uint8_t(V >> 8), uint8_t(V)}; 11948e3b38abSDenis Protivensky } 11958e3b38abSDenis Protivensky 1196a35e39caSPetr Hosek SymbolAssignment *ScriptParser::readProvideHidden(bool Provide, bool Hidden) { 1197a31c91b1SEugene Leviant expect("("); 1198174e0a16SRui Ueyama SymbolAssignment *Cmd = readAssignment(next()); 1199a35e39caSPetr Hosek Cmd->Provide = Provide; 1200174e0a16SRui Ueyama Cmd->Hidden = Hidden; 1201a31c91b1SEugene Leviant expect(")"); 1202a31c91b1SEugene Leviant expect(";"); 120310416564SRui Ueyama return Cmd; 1204eda81a1bSEugene Leviant } 1205eda81a1bSEugene Leviant 1206db741e72SEugene Leviant SymbolAssignment *ScriptParser::readProvideOrAssignment(StringRef Tok, 1207db741e72SEugene Leviant bool MakeAbsolute) { 1208ceabe80eSEugene Leviant SymbolAssignment *Cmd = nullptr; 1209ceabe80eSEugene Leviant if (peek() == "=" || peek() == "+=") { 1210ceabe80eSEugene Leviant Cmd = readAssignment(Tok); 1211ceabe80eSEugene Leviant expect(";"); 1212ceabe80eSEugene Leviant } else if (Tok == "PROVIDE") { 1213a35e39caSPetr Hosek Cmd = readProvideHidden(true, false); 1214a35e39caSPetr Hosek } else if (Tok == "HIDDEN") { 1215a35e39caSPetr Hosek Cmd = readProvideHidden(false, true); 1216ceabe80eSEugene Leviant } else if (Tok == "PROVIDE_HIDDEN") { 1217a35e39caSPetr Hosek Cmd = readProvideHidden(true, true); 1218ceabe80eSEugene Leviant } 1219db741e72SEugene Leviant if (Cmd && MakeAbsolute) 1220db741e72SEugene Leviant Cmd->IsAbsolute = true; 1221ceabe80eSEugene Leviant return Cmd; 1222ceabe80eSEugene Leviant } 1223ceabe80eSEugene Leviant 122430835ea4SGeorge Rimar static uint64_t getSymbolValue(StringRef S, uint64_t Dot) { 122530835ea4SGeorge Rimar if (S == ".") 122630835ea4SGeorge Rimar return Dot; 1227884e786dSGeorge Rimar return ScriptBase->getSymbolValue(S); 1228e32a3598SGeorge Rimar } 1229e32a3598SGeorge Rimar 123030835ea4SGeorge Rimar SymbolAssignment *ScriptParser::readAssignment(StringRef Name) { 123130835ea4SGeorge Rimar StringRef Op = next(); 1232db741e72SEugene Leviant bool IsAbsolute = false; 1233db741e72SEugene Leviant Expr E; 123430835ea4SGeorge Rimar assert(Op == "=" || Op == "+="); 1235db741e72SEugene Leviant if (skip("ABSOLUTE")) { 1236db741e72SEugene Leviant E = readParenExpr(); 1237db741e72SEugene Leviant IsAbsolute = true; 1238db741e72SEugene Leviant } else { 1239db741e72SEugene Leviant E = readExpr(); 1240db741e72SEugene Leviant } 124130835ea4SGeorge Rimar if (Op == "+=") 124230835ea4SGeorge Rimar E = [=](uint64_t Dot) { return getSymbolValue(Name, Dot) + E(Dot); }; 1243db741e72SEugene Leviant return new SymbolAssignment(Name, E, IsAbsolute); 124430835ea4SGeorge Rimar } 124530835ea4SGeorge Rimar 124630835ea4SGeorge Rimar // This is an operator-precedence parser to parse a linker 124730835ea4SGeorge Rimar // script expression. 124830835ea4SGeorge Rimar Expr ScriptParser::readExpr() { return readExpr1(readPrimary(), 0); } 124930835ea4SGeorge Rimar 125036c1cd23SRui Ueyama static Expr combine(StringRef Op, Expr L, Expr R) { 125136c1cd23SRui Ueyama if (Op == "*") 125236c1cd23SRui Ueyama return [=](uint64_t Dot) { return L(Dot) * R(Dot); }; 125336c1cd23SRui Ueyama if (Op == "/") { 125436c1cd23SRui Ueyama return [=](uint64_t Dot) -> uint64_t { 125536c1cd23SRui Ueyama uint64_t RHS = R(Dot); 125636c1cd23SRui Ueyama if (RHS == 0) { 125736c1cd23SRui Ueyama error("division by zero"); 125836c1cd23SRui Ueyama return 0; 125936c1cd23SRui Ueyama } 126036c1cd23SRui Ueyama return L(Dot) / RHS; 126136c1cd23SRui Ueyama }; 126236c1cd23SRui Ueyama } 126336c1cd23SRui Ueyama if (Op == "+") 126436c1cd23SRui Ueyama return [=](uint64_t Dot) { return L(Dot) + R(Dot); }; 126536c1cd23SRui Ueyama if (Op == "-") 126636c1cd23SRui Ueyama return [=](uint64_t Dot) { return L(Dot) - R(Dot); }; 126736c1cd23SRui Ueyama if (Op == "<") 126836c1cd23SRui Ueyama return [=](uint64_t Dot) { return L(Dot) < R(Dot); }; 126936c1cd23SRui Ueyama if (Op == ">") 127036c1cd23SRui Ueyama return [=](uint64_t Dot) { return L(Dot) > R(Dot); }; 127136c1cd23SRui Ueyama if (Op == ">=") 127236c1cd23SRui Ueyama return [=](uint64_t Dot) { return L(Dot) >= R(Dot); }; 127336c1cd23SRui Ueyama if (Op == "<=") 127436c1cd23SRui Ueyama return [=](uint64_t Dot) { return L(Dot) <= R(Dot); }; 127536c1cd23SRui Ueyama if (Op == "==") 127636c1cd23SRui Ueyama return [=](uint64_t Dot) { return L(Dot) == R(Dot); }; 127736c1cd23SRui Ueyama if (Op == "!=") 127836c1cd23SRui Ueyama return [=](uint64_t Dot) { return L(Dot) != R(Dot); }; 127936c1cd23SRui Ueyama if (Op == "&") 128036c1cd23SRui Ueyama return [=](uint64_t Dot) { return L(Dot) & R(Dot); }; 1281cc3dd629SRafael Espindola if (Op == "|") 1282cc3dd629SRafael Espindola return [=](uint64_t Dot) { return L(Dot) | R(Dot); }; 128336c1cd23SRui Ueyama llvm_unreachable("invalid operator"); 128436c1cd23SRui Ueyama } 128536c1cd23SRui Ueyama 1286708019c4SRui Ueyama // This is a part of the operator-precedence parser. This function 1287708019c4SRui Ueyama // assumes that the remaining token stream starts with an operator. 1288708019c4SRui Ueyama Expr ScriptParser::readExpr1(Expr Lhs, int MinPrec) { 1289708019c4SRui Ueyama while (!atEOF() && !Error) { 1290708019c4SRui Ueyama // Read an operator and an expression. 1291708019c4SRui Ueyama StringRef Op1 = peek(); 1292708019c4SRui Ueyama if (Op1 == "?") 1293708019c4SRui Ueyama return readTernary(Lhs); 1294708019c4SRui Ueyama if (precedence(Op1) < MinPrec) 1295a31c91b1SEugene Leviant break; 1296a31c91b1SEugene Leviant next(); 1297708019c4SRui Ueyama Expr Rhs = readPrimary(); 1298708019c4SRui Ueyama 1299708019c4SRui Ueyama // Evaluate the remaining part of the expression first if the 1300708019c4SRui Ueyama // next operator has greater precedence than the previous one. 1301708019c4SRui Ueyama // For example, if we have read "+" and "3", and if the next 1302708019c4SRui Ueyama // operator is "*", then we'll evaluate 3 * ... part first. 1303708019c4SRui Ueyama while (!atEOF()) { 1304708019c4SRui Ueyama StringRef Op2 = peek(); 1305708019c4SRui Ueyama if (precedence(Op2) <= precedence(Op1)) 1306eda81a1bSEugene Leviant break; 1307708019c4SRui Ueyama Rhs = readExpr1(Rhs, precedence(Op2)); 1308eda81a1bSEugene Leviant } 1309708019c4SRui Ueyama 1310708019c4SRui Ueyama Lhs = combine(Op1, Lhs, Rhs); 1311708019c4SRui Ueyama } 1312708019c4SRui Ueyama return Lhs; 1313708019c4SRui Ueyama } 1314708019c4SRui Ueyama 1315708019c4SRui Ueyama uint64_t static getConstant(StringRef S) { 1316e2cc07bcSMichael J. Spencer if (S == "COMMONPAGESIZE") 1317708019c4SRui Ueyama return Target->PageSize; 1318e2cc07bcSMichael J. Spencer if (S == "MAXPAGESIZE") 1319e2cc07bcSMichael J. Spencer return Target->MaxPageSize; 1320708019c4SRui Ueyama error("unknown constant: " + S); 1321708019c4SRui Ueyama return 0; 1322708019c4SRui Ueyama } 1323708019c4SRui Ueyama 1324626e0b08SRui Ueyama // Parses Tok as an integer. Returns true if successful. 1325626e0b08SRui Ueyama // It recognizes hexadecimal (prefixed with "0x" or suffixed with "H") 1326626e0b08SRui Ueyama // and decimal numbers. Decimal numbers may have "K" (kilo) or 1327626e0b08SRui Ueyama // "M" (mega) prefixes. 13289f2f7ad9SGeorge Rimar static bool readInteger(StringRef Tok, uint64_t &Result) { 1329eaeafb2bSSimon Atanasyan if (Tok.startswith("-")) { 1330eaeafb2bSSimon Atanasyan if (!readInteger(Tok.substr(1), Result)) 1331eaeafb2bSSimon Atanasyan return false; 1332eaeafb2bSSimon Atanasyan Result = -Result; 1333eaeafb2bSSimon Atanasyan return true; 1334eaeafb2bSSimon Atanasyan } 13359f2f7ad9SGeorge Rimar if (Tok.startswith_lower("0x")) 13369f2f7ad9SGeorge Rimar return !Tok.substr(2).getAsInteger(16, Result); 13379f2f7ad9SGeorge Rimar if (Tok.endswith_lower("H")) 13389f2f7ad9SGeorge Rimar return !Tok.drop_back().getAsInteger(16, Result); 13399f2f7ad9SGeorge Rimar 13409f2f7ad9SGeorge Rimar int Suffix = 1; 13419f2f7ad9SGeorge Rimar if (Tok.endswith_lower("K")) { 13429f2f7ad9SGeorge Rimar Suffix = 1024; 13439f2f7ad9SGeorge Rimar Tok = Tok.drop_back(); 13449f2f7ad9SGeorge Rimar } else if (Tok.endswith_lower("M")) { 13459f2f7ad9SGeorge Rimar Suffix = 1024 * 1024; 13469f2f7ad9SGeorge Rimar Tok = Tok.drop_back(); 13479f2f7ad9SGeorge Rimar } 13489f2f7ad9SGeorge Rimar if (Tok.getAsInteger(10, Result)) 13499f2f7ad9SGeorge Rimar return false; 13509f2f7ad9SGeorge Rimar Result *= Suffix; 13519f2f7ad9SGeorge Rimar return true; 13529f2f7ad9SGeorge Rimar } 13539f2f7ad9SGeorge Rimar 1354708019c4SRui Ueyama Expr ScriptParser::readPrimary() { 13556ad7dfccSRui Ueyama if (peek() == "(") 13566ad7dfccSRui Ueyama return readParenExpr(); 1357708019c4SRui Ueyama 13586ad7dfccSRui Ueyama StringRef Tok = next(); 1359708019c4SRui Ueyama 1360eaeafb2bSSimon Atanasyan if (Tok == "~") { 1361eaeafb2bSSimon Atanasyan Expr E = readPrimary(); 1362eaeafb2bSSimon Atanasyan return [=](uint64_t Dot) { return ~E(Dot); }; 1363eaeafb2bSSimon Atanasyan } 1364eaeafb2bSSimon Atanasyan if (Tok == "-") { 1365eaeafb2bSSimon Atanasyan Expr E = readPrimary(); 1366eaeafb2bSSimon Atanasyan return [=](uint64_t Dot) { return -E(Dot); }; 1367eaeafb2bSSimon Atanasyan } 1368eaeafb2bSSimon Atanasyan 1369708019c4SRui Ueyama // Built-in functions are parsed here. 1370708019c4SRui Ueyama // https://sourceware.org/binutils/docs/ld/Builtin-Functions.html. 137196659df0SGeorge Rimar if (Tok == "ADDR") { 137296659df0SGeorge Rimar expect("("); 137396659df0SGeorge Rimar StringRef Name = next(); 137496659df0SGeorge Rimar expect(")"); 1375884e786dSGeorge Rimar return 1376884e786dSGeorge Rimar [=](uint64_t Dot) { return ScriptBase->getOutputSectionAddress(Name); }; 137796659df0SGeorge Rimar } 1378eefa758eSGeorge Rimar if (Tok == "ASSERT") 1379eefa758eSGeorge Rimar return readAssert(); 1380708019c4SRui Ueyama if (Tok == "ALIGN") { 13816ad7dfccSRui Ueyama Expr E = readParenExpr(); 1382708019c4SRui Ueyama return [=](uint64_t Dot) { return alignTo(Dot, E(Dot)); }; 1383708019c4SRui Ueyama } 1384708019c4SRui Ueyama if (Tok == "CONSTANT") { 1385708019c4SRui Ueyama expect("("); 1386708019c4SRui Ueyama StringRef Tok = next(); 1387708019c4SRui Ueyama expect(")"); 1388708019c4SRui Ueyama return [=](uint64_t Dot) { return getConstant(Tok); }; 1389708019c4SRui Ueyama } 139054c145ceSRafael Espindola if (Tok == "SEGMENT_START") { 139154c145ceSRafael Espindola expect("("); 139254c145ceSRafael Espindola next(); 139354c145ceSRafael Espindola expect(","); 139454c145ceSRafael Espindola uint64_t Val; 13953adbbc38SRafael Espindola if (next().getAsInteger(0, Val)) 13963adbbc38SRafael Espindola setError("integer expected"); 139754c145ceSRafael Espindola expect(")"); 139854c145ceSRafael Espindola return [=](uint64_t Dot) { return Val; }; 139954c145ceSRafael Espindola } 1400708019c4SRui Ueyama if (Tok == "DATA_SEGMENT_ALIGN") { 1401708019c4SRui Ueyama expect("("); 1402708019c4SRui Ueyama Expr E = readExpr(); 1403708019c4SRui Ueyama expect(","); 1404708019c4SRui Ueyama readExpr(); 1405708019c4SRui Ueyama expect(")"); 1406f7791bb9SRui Ueyama return [=](uint64_t Dot) { return alignTo(Dot, E(Dot)); }; 1407708019c4SRui Ueyama } 1408708019c4SRui Ueyama if (Tok == "DATA_SEGMENT_END") { 1409708019c4SRui Ueyama expect("("); 1410708019c4SRui Ueyama expect("."); 1411708019c4SRui Ueyama expect(")"); 1412708019c4SRui Ueyama return [](uint64_t Dot) { return Dot; }; 1413708019c4SRui Ueyama } 1414276b4e64SGeorge Rimar // GNU linkers implements more complicated logic to handle 1415276b4e64SGeorge Rimar // DATA_SEGMENT_RELRO_END. We instead ignore the arguments and just align to 1416276b4e64SGeorge Rimar // the next page boundary for simplicity. 1417276b4e64SGeorge Rimar if (Tok == "DATA_SEGMENT_RELRO_END") { 1418276b4e64SGeorge Rimar expect("("); 141997bdc722SRafael Espindola readExpr(); 1420276b4e64SGeorge Rimar expect(","); 1421276b4e64SGeorge Rimar readExpr(); 1422276b4e64SGeorge Rimar expect(")"); 1423276b4e64SGeorge Rimar return [](uint64_t Dot) { return alignTo(Dot, Target->PageSize); }; 1424276b4e64SGeorge Rimar } 14259e69450eSGeorge Rimar if (Tok == "SIZEOF") { 14269e69450eSGeorge Rimar expect("("); 14279e69450eSGeorge Rimar StringRef Name = next(); 14289e69450eSGeorge Rimar expect(")"); 1429884e786dSGeorge Rimar return [=](uint64_t Dot) { return ScriptBase->getOutputSectionSize(Name); }; 14309e69450eSGeorge Rimar } 143136fac7f0SEugene Leviant if (Tok == "ALIGNOF") { 143236fac7f0SEugene Leviant expect("("); 143336fac7f0SEugene Leviant StringRef Name = next(); 143436fac7f0SEugene Leviant expect(")"); 143536fac7f0SEugene Leviant return 143636fac7f0SEugene Leviant [=](uint64_t Dot) { return ScriptBase->getOutputSectionAlign(Name); }; 143736fac7f0SEugene Leviant } 1438e32a3598SGeorge Rimar if (Tok == "SIZEOF_HEADERS") 1439884e786dSGeorge Rimar return [=](uint64_t Dot) { return ScriptBase->getHeaderSize(); }; 1440708019c4SRui Ueyama 14419f2f7ad9SGeorge Rimar // Tok is a literal number. 14429f2f7ad9SGeorge Rimar uint64_t V; 14439f2f7ad9SGeorge Rimar if (readInteger(Tok, V)) 14449f2f7ad9SGeorge Rimar return [=](uint64_t Dot) { return V; }; 14459f2f7ad9SGeorge Rimar 14469f2f7ad9SGeorge Rimar // Tok is a symbol name. 144730835ea4SGeorge Rimar if (Tok != "." && !isValidCIdentifier(Tok)) 1448708019c4SRui Ueyama setError("malformed number: " + Tok); 144930835ea4SGeorge Rimar return [=](uint64_t Dot) { return getSymbolValue(Tok, Dot); }; 1450a9c5a528SGeorge Rimar } 1451708019c4SRui Ueyama 1452708019c4SRui Ueyama Expr ScriptParser::readTernary(Expr Cond) { 1453708019c4SRui Ueyama next(); 1454708019c4SRui Ueyama Expr L = readExpr(); 1455708019c4SRui Ueyama expect(":"); 1456708019c4SRui Ueyama Expr R = readExpr(); 1457708019c4SRui Ueyama return [=](uint64_t Dot) { return Cond(Dot) ? L(Dot) : R(Dot); }; 1458708019c4SRui Ueyama } 1459708019c4SRui Ueyama 14606ad7dfccSRui Ueyama Expr ScriptParser::readParenExpr() { 14616ad7dfccSRui Ueyama expect("("); 14626ad7dfccSRui Ueyama Expr E = readExpr(); 14636ad7dfccSRui Ueyama expect(")"); 14646ad7dfccSRui Ueyama return E; 14656ad7dfccSRui Ueyama } 14666ad7dfccSRui Ueyama 1467bbe38602SEugene Leviant std::vector<StringRef> ScriptParser::readOutputSectionPhdrs() { 1468bbe38602SEugene Leviant std::vector<StringRef> Phdrs; 1469bbe38602SEugene Leviant while (!Error && peek().startswith(":")) { 1470bbe38602SEugene Leviant StringRef Tok = next(); 1471bbe38602SEugene Leviant Tok = (Tok.size() == 1) ? next() : Tok.substr(1); 1472bbe38602SEugene Leviant if (Tok.empty()) { 1473bbe38602SEugene Leviant setError("section header name is empty"); 1474bbe38602SEugene Leviant break; 1475bbe38602SEugene Leviant } 1476bbe38602SEugene Leviant Phdrs.push_back(Tok); 1477bbe38602SEugene Leviant } 1478bbe38602SEugene Leviant return Phdrs; 1479bbe38602SEugene Leviant } 1480bbe38602SEugene Leviant 1481bbe38602SEugene Leviant unsigned ScriptParser::readPhdrType() { 1482bbe38602SEugene Leviant StringRef Tok = next(); 1483b0f6c590SRui Ueyama unsigned Ret = StringSwitch<unsigned>(Tok) 1484b0f6c590SRui Ueyama .Case("PT_NULL", PT_NULL) 1485b0f6c590SRui Ueyama .Case("PT_LOAD", PT_LOAD) 1486b0f6c590SRui Ueyama .Case("PT_DYNAMIC", PT_DYNAMIC) 1487b0f6c590SRui Ueyama .Case("PT_INTERP", PT_INTERP) 1488b0f6c590SRui Ueyama .Case("PT_NOTE", PT_NOTE) 1489b0f6c590SRui Ueyama .Case("PT_SHLIB", PT_SHLIB) 1490b0f6c590SRui Ueyama .Case("PT_PHDR", PT_PHDR) 1491b0f6c590SRui Ueyama .Case("PT_TLS", PT_TLS) 1492b0f6c590SRui Ueyama .Case("PT_GNU_EH_FRAME", PT_GNU_EH_FRAME) 1493b0f6c590SRui Ueyama .Case("PT_GNU_STACK", PT_GNU_STACK) 1494b0f6c590SRui Ueyama .Case("PT_GNU_RELRO", PT_GNU_RELRO) 1495b0f6c590SRui Ueyama .Default(-1); 1496bbe38602SEugene Leviant 1497b0f6c590SRui Ueyama if (Ret == (unsigned)-1) { 1498b0f6c590SRui Ueyama setError("invalid program header type: " + Tok); 1499b0f6c590SRui Ueyama return PT_NULL; 1500b0f6c590SRui Ueyama } 1501b0f6c590SRui Ueyama return Ret; 1502bbe38602SEugene Leviant } 1503bbe38602SEugene Leviant 150495769b4aSRui Ueyama void ScriptParser::readVersionDeclaration(StringRef VerStr) { 150520b6598cSGeorge Rimar // Identifiers start at 2 because 0 and 1 are reserved 150620b6598cSGeorge Rimar // for VER_NDX_LOCAL and VER_NDX_GLOBAL constants. 150720b6598cSGeorge Rimar size_t VersionId = Config->VersionDefinitions.size() + 2; 150820b6598cSGeorge Rimar Config->VersionDefinitions.push_back({VerStr, VersionId}); 150920b6598cSGeorge Rimar 151020b6598cSGeorge Rimar if (skip("global:") || peek() != "local:") 151120b6598cSGeorge Rimar readGlobal(VerStr); 151220b6598cSGeorge Rimar if (skip("local:")) 151320b6598cSGeorge Rimar readLocal(); 151420b6598cSGeorge Rimar expect("}"); 151520b6598cSGeorge Rimar 151620b6598cSGeorge Rimar // Each version may have a parent version. For example, "Ver2" defined as 151720b6598cSGeorge Rimar // "Ver2 { global: foo; local: *; } Ver1;" has "Ver1" as a parent. This 151820b6598cSGeorge Rimar // version hierarchy is, probably against your instinct, purely for human; the 151920b6598cSGeorge Rimar // runtime doesn't care about them at all. In LLD, we simply skip the token. 152020b6598cSGeorge Rimar if (!VerStr.empty() && peek() != ";") 152120b6598cSGeorge Rimar next(); 152220b6598cSGeorge Rimar expect(";"); 152320b6598cSGeorge Rimar } 152420b6598cSGeorge Rimar 152520b6598cSGeorge Rimar void ScriptParser::readLocal() { 152620b6598cSGeorge Rimar Config->DefaultSymbolVersion = VER_NDX_LOCAL; 152720b6598cSGeorge Rimar expect("*"); 152820b6598cSGeorge Rimar expect(";"); 152920b6598cSGeorge Rimar } 153020b6598cSGeorge Rimar 153120b6598cSGeorge Rimar void ScriptParser::readExtern(std::vector<SymbolVersion> *Globals) { 1532cd574a5eSGeorge Rimar expect("\"C++\""); 153320b6598cSGeorge Rimar expect("{"); 153420b6598cSGeorge Rimar 153520b6598cSGeorge Rimar for (;;) { 153620b6598cSGeorge Rimar if (peek() == "}" || Error) 153720b6598cSGeorge Rimar break; 1538cd574a5eSGeorge Rimar bool HasWildcard = !peek().startswith("\"") && hasWildcard(peek()); 1539cd574a5eSGeorge Rimar Globals->push_back({unquote(next()), true, HasWildcard}); 154020b6598cSGeorge Rimar expect(";"); 154120b6598cSGeorge Rimar } 154220b6598cSGeorge Rimar 154320b6598cSGeorge Rimar expect("}"); 154420b6598cSGeorge Rimar expect(";"); 154520b6598cSGeorge Rimar } 154620b6598cSGeorge Rimar 154720b6598cSGeorge Rimar void ScriptParser::readGlobal(StringRef VerStr) { 154820b6598cSGeorge Rimar std::vector<SymbolVersion> *Globals; 154920b6598cSGeorge Rimar if (VerStr.empty()) 155020b6598cSGeorge Rimar Globals = &Config->VersionScriptGlobals; 155120b6598cSGeorge Rimar else 155220b6598cSGeorge Rimar Globals = &Config->VersionDefinitions.back().Globals; 155320b6598cSGeorge Rimar 155420b6598cSGeorge Rimar for (;;) { 155520b6598cSGeorge Rimar if (skip("extern")) 155620b6598cSGeorge Rimar readExtern(Globals); 155720b6598cSGeorge Rimar 155820b6598cSGeorge Rimar StringRef Cur = peek(); 155920b6598cSGeorge Rimar if (Cur == "}" || Cur == "local:" || Error) 156020b6598cSGeorge Rimar return; 156120b6598cSGeorge Rimar next(); 1562cd574a5eSGeorge Rimar Globals->push_back({unquote(Cur), false, hasWildcard(Cur)}); 156320b6598cSGeorge Rimar expect(";"); 156420b6598cSGeorge Rimar } 156520b6598cSGeorge Rimar } 156620b6598cSGeorge Rimar 156716b0cc9eSSimon Atanasyan static bool isUnderSysroot(StringRef Path) { 156816b0cc9eSSimon Atanasyan if (Config->Sysroot == "") 156916b0cc9eSSimon Atanasyan return false; 157016b0cc9eSSimon Atanasyan for (; !Path.empty(); Path = sys::path::parent_path(Path)) 157116b0cc9eSSimon Atanasyan if (sys::fs::equivalent(Config->Sysroot, Path)) 157216b0cc9eSSimon Atanasyan return true; 157316b0cc9eSSimon Atanasyan return false; 157416b0cc9eSSimon Atanasyan } 157516b0cc9eSSimon Atanasyan 157607320e40SRui Ueyama void elf::readLinkerScript(MemoryBufferRef MB) { 157716b0cc9eSSimon Atanasyan StringRef Path = MB.getBufferIdentifier(); 157820b6598cSGeorge Rimar ScriptParser(MB.getBuffer(), isUnderSysroot(Path)).readLinkerScript(); 157920b6598cSGeorge Rimar } 158020b6598cSGeorge Rimar 158120b6598cSGeorge Rimar void elf::readVersionScript(MemoryBufferRef MB) { 158220b6598cSGeorge Rimar ScriptParser(MB.getBuffer(), false).readVersionScript(); 1583f7c5fbb1SRui Ueyama } 15841ebc8ed7SRui Ueyama 158507320e40SRui Ueyama template class elf::LinkerScript<ELF32LE>; 158607320e40SRui Ueyama template class elf::LinkerScript<ELF32BE>; 158707320e40SRui Ueyama template class elf::LinkerScript<ELF64LE>; 158807320e40SRui Ueyama template class elf::LinkerScript<ELF64BE>; 1589