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 4407320e40SRui Ueyama ScriptConfiguration *elf::ScriptConfig; 45717677afSRui Ueyama 46076fe157SGeorge Rimar bool SymbolAssignment::classof(const BaseCommand *C) { 47076fe157SGeorge Rimar return C->Kind == AssignmentKind; 48076fe157SGeorge Rimar } 49076fe157SGeorge Rimar 50076fe157SGeorge Rimar bool OutputSectionCommand::classof(const BaseCommand *C) { 51076fe157SGeorge Rimar return C->Kind == OutputSectionKind; 52076fe157SGeorge Rimar } 53076fe157SGeorge Rimar 54eea3114fSGeorge Rimar bool InputSectionDescription::classof(const BaseCommand *C) { 55eea3114fSGeorge Rimar return C->Kind == InputSectionKind; 56eea3114fSGeorge Rimar } 57eea3114fSGeorge Rimar 5836a153cdSRui Ueyama template <class ELFT> static bool isDiscarded(InputSectionBase<ELFT> *S) { 59eea3114fSGeorge Rimar return !S || !S->Live; 60717677afSRui Ueyama } 61717677afSRui Ueyama 6207320e40SRui Ueyama template <class ELFT> 6307320e40SRui Ueyama bool LinkerScript<ELFT>::shouldKeep(InputSectionBase<ELFT> *S) { 648ec77e64SRui Ueyama for (StringRef Pat : Opt.KeptSections) 65722830a5SRui Ueyama if (globMatch(Pat, S->getSectionName())) 668ec77e64SRui Ueyama return true; 678ec77e64SRui Ueyama return false; 68481c2ce6SGeorge Rimar } 69481c2ce6SGeorge Rimar 7063dc6509SRui Ueyama static bool match(ArrayRef<StringRef> Patterns, StringRef S) { 7163dc6509SRui Ueyama for (StringRef Pat : Patterns) 7263dc6509SRui Ueyama if (globMatch(Pat, S)) 73eea3114fSGeorge Rimar return true; 74eea3114fSGeorge Rimar return false; 75eea3114fSGeorge Rimar } 76eea3114fSGeorge Rimar 77eaee2af5SGeorge Rimar // Create a vector of (<output section name>, <input section description>). 786b274810SRui Ueyama template <class ELFT> 79e7282797SDavide Italiano std::vector<std::pair<StringRef, const InputSectionDescription *>> 806b274810SRui Ueyama LinkerScript<ELFT>::getSectionMap() { 81e7282797SDavide Italiano std::vector<std::pair<StringRef, const InputSectionDescription *>> Ret; 826b274810SRui Ueyama 836b274810SRui Ueyama for (const std::unique_ptr<BaseCommand> &Base1 : Opt.Commands) 846b274810SRui Ueyama if (auto *Cmd1 = dyn_cast<OutputSectionCommand>(Base1.get())) 856b274810SRui Ueyama for (const std::unique_ptr<BaseCommand> &Base2 : Cmd1->Commands) 866b274810SRui Ueyama if (auto *Cmd2 = dyn_cast<InputSectionDescription>(Base2.get())) 87e7282797SDavide Italiano Ret.emplace_back(Cmd1->Name, Cmd2); 886b274810SRui Ueyama 896b274810SRui Ueyama return Ret; 906b274810SRui Ueyama } 916b274810SRui Ueyama 920659800eSGeorge Rimar static bool fileMatches(const InputSectionDescription *Desc, 930659800eSGeorge Rimar StringRef Filename) { 940659800eSGeorge Rimar if (!globMatch(Desc->FilePattern, Filename)) 950659800eSGeorge Rimar return false; 960659800eSGeorge Rimar return Desc->ExcludedFiles.empty() || !match(Desc->ExcludedFiles, Filename); 970659800eSGeorge Rimar } 980659800eSGeorge Rimar 996b274810SRui Ueyama // Returns input sections filtered by given glob patterns. 1006b274810SRui Ueyama template <class ELFT> 1016b274810SRui Ueyama std::vector<InputSectionBase<ELFT> *> 102ad10c3d8SRui Ueyama LinkerScript<ELFT>::getInputSections(const InputSectionDescription *I) { 1030659800eSGeorge Rimar ArrayRef<StringRef> Patterns = I->SectionPatterns; 1046b274810SRui Ueyama std::vector<InputSectionBase<ELFT> *> Ret; 1056b274810SRui Ueyama for (const std::unique_ptr<ObjectFile<ELFT>> &F : 1060659800eSGeorge Rimar Symtab<ELFT>::X->getObjectFiles()) { 1070659800eSGeorge Rimar if (fileMatches(I, sys::path::filename(F->getName()))) 1086b274810SRui Ueyama for (InputSectionBase<ELFT> *S : F->getSections()) 1090659800eSGeorge Rimar if (!isDiscarded(S) && !S->OutSec && 1100659800eSGeorge Rimar match(Patterns, S->getSectionName())) 1116b274810SRui Ueyama Ret.push_back(S); 1120659800eSGeorge Rimar } 1133e6b0277SEugene Leviant 1143e6b0277SEugene Leviant if ((llvm::find(Patterns, "COMMON") != Patterns.end())) 115ad10c3d8SRui Ueyama Ret.push_back(CommonInputSection<ELFT>::X); 1163e6b0277SEugene Leviant 1176b274810SRui Ueyama return Ret; 1186b274810SRui Ueyama } 1196b274810SRui Ueyama 120e63d81bdSEugene Leviant // Add input section to output section. If there is no output section yet, 121e63d81bdSEugene Leviant // then create it and add to output section list. 122c3cb884cSGeorge Rimar template <class ELFT> 123c3cb884cSGeorge Rimar static void addSection(OutputSectionFactory<ELFT> &Factory, 124c3cb884cSGeorge Rimar std::vector<OutputSectionBase<ELFT> *> &Out, 125c3cb884cSGeorge Rimar InputSectionBase<ELFT> *C, StringRef Name) { 126e63d81bdSEugene Leviant OutputSectionBase<ELFT> *Sec; 127e63d81bdSEugene Leviant bool IsNew; 128e63d81bdSEugene Leviant std::tie(Sec, IsNew) = Factory.create(C, Name); 129e63d81bdSEugene Leviant if (IsNew) 130c3cb884cSGeorge Rimar Out.push_back(Sec); 131e63d81bdSEugene Leviant Sec->addSection(C); 132c3cb884cSGeorge Rimar } 133c3cb884cSGeorge Rimar 134*350ece4eSGeorge Rimar template <class ELFT> struct SectionsSorter { 135*350ece4eSGeorge Rimar SectionsSorter(SortKind Kind) : Kind(Kind) {} 136*350ece4eSGeorge Rimar bool operator()(InputSectionBase<ELFT> *A, InputSectionBase<ELFT> *B) { 137*350ece4eSGeorge Rimar int AlignmentCmp = A->Alignment - B->Alignment; 138*350ece4eSGeorge Rimar if (Kind == SortKind::Align || (Kind == SortKind::AlignName && AlignmentCmp != 0)) 139*350ece4eSGeorge Rimar return AlignmentCmp < 0; 140*350ece4eSGeorge Rimar 141*350ece4eSGeorge Rimar int NameCmp = A->getSectionName().compare(B->getSectionName()); 142*350ece4eSGeorge Rimar if (Kind == SortKind::Name || (Kind == SortKind::NameAlign && NameCmp != 0)) 143*350ece4eSGeorge Rimar return NameCmp < 0; 144*350ece4eSGeorge Rimar 145*350ece4eSGeorge Rimar if (Kind == SortKind::NameAlign) 146*350ece4eSGeorge Rimar return AlignmentCmp < 0; 147*350ece4eSGeorge Rimar if (Kind == SortKind::AlignName) 148*350ece4eSGeorge Rimar return NameCmp < 0; 149*350ece4eSGeorge Rimar 150*350ece4eSGeorge Rimar llvm_unreachable("unknown section sort kind in predicate"); 151*350ece4eSGeorge Rimar return false; 1520702c4e8SGeorge Rimar } 153*350ece4eSGeorge Rimar SortKind Kind; 154*350ece4eSGeorge Rimar }; 1550702c4e8SGeorge Rimar 1560702c4e8SGeorge Rimar template <class ELFT> 1579e69450eSGeorge Rimar void LinkerScript<ELFT>::createSections( 1589e69450eSGeorge Rimar std::vector<OutputSectionBase<ELFT> *> *Out, 1599e69450eSGeorge Rimar OutputSectionFactory<ELFT> &Factory) { 1609e69450eSGeorge Rimar OutputSections = Out; 161e63d81bdSEugene Leviant 1626b274810SRui Ueyama for (auto &P : getSectionMap()) { 1630702c4e8SGeorge Rimar std::vector<InputSectionBase<ELFT> *> Sections; 1646b274810SRui Ueyama StringRef OutputName = P.first; 165e7282797SDavide Italiano const InputSectionDescription *I = P.second; 166ad10c3d8SRui Ueyama for (InputSectionBase<ELFT> *S : getInputSections(I)) { 1676b274810SRui Ueyama if (OutputName == "/DISCARD/") { 168eea3114fSGeorge Rimar S->Live = false; 1696b274810SRui Ueyama reportDiscarded(S); 1706b274810SRui Ueyama continue; 171eea3114fSGeorge Rimar } 1720702c4e8SGeorge Rimar Sections.push_back(S); 173eea3114fSGeorge Rimar } 174*350ece4eSGeorge Rimar if (I->Sort != SortKind::None) 175*350ece4eSGeorge Rimar std::stable_sort(Sections.begin(), Sections.end(), 176*350ece4eSGeorge Rimar SectionsSorter<ELFT>(I->Sort)); 1770702c4e8SGeorge Rimar for (InputSectionBase<ELFT> *S : Sections) 1789e69450eSGeorge Rimar addSection(Factory, *Out, S, OutputName); 179eea3114fSGeorge Rimar } 180e63d81bdSEugene Leviant 181e63d81bdSEugene Leviant // Add all other input sections, which are not listed in script. 1826b274810SRui Ueyama for (const std::unique_ptr<ObjectFile<ELFT>> &F : 1836b274810SRui Ueyama Symtab<ELFT>::X->getObjectFiles()) 1846b274810SRui Ueyama for (InputSectionBase<ELFT> *S : F->getSections()) 1856b274810SRui Ueyama if (!isDiscarded(S) && !S->OutSec) 1869e69450eSGeorge Rimar addSection(Factory, *Out, S, getOutputSectionName(S)); 187e63d81bdSEugene Leviant 1883c291e1aSRui Ueyama // Remove from the output all the sections which did not meet 1893c291e1aSRui Ueyama // the optional constraints. 1909e69450eSGeorge Rimar filter(); 1913c291e1aSRui Ueyama } 1923c291e1aSRui Ueyama 1933c291e1aSRui Ueyama // Process ONLY_IF_RO and ONLY_IF_RW. 1949e69450eSGeorge Rimar template <class ELFT> void LinkerScript<ELFT>::filter() { 1953c291e1aSRui Ueyama // In this loop, we remove output sections if they don't satisfy 1963c291e1aSRui Ueyama // requested properties. 1973c291e1aSRui Ueyama for (const std::unique_ptr<BaseCommand> &Base : Opt.Commands) { 1983c291e1aSRui Ueyama auto *Cmd = dyn_cast<OutputSectionCommand>(Base.get()); 1993c291e1aSRui Ueyama if (!Cmd || Cmd->Name == "/DISCARD/") 2003c291e1aSRui Ueyama continue; 2013c291e1aSRui Ueyama 202bfc4a4b7SGeorge Rimar if (Cmd->Constraint == ConstraintKind::NoConstraint) 2033c291e1aSRui Ueyama continue; 204bfc4a4b7SGeorge Rimar 2059e69450eSGeorge Rimar auto It = llvm::find_if(*OutputSections, [&](OutputSectionBase<ELFT> *S) { 206bfc4a4b7SGeorge Rimar return S->getName() == Cmd->Name; 207bfc4a4b7SGeorge Rimar }); 2089e69450eSGeorge Rimar if (It == OutputSections->end()) 209bfc4a4b7SGeorge Rimar continue; 2103c291e1aSRui Ueyama 2113c291e1aSRui Ueyama OutputSectionBase<ELFT> *Sec = *It; 2123c291e1aSRui Ueyama bool Writable = (Sec->getFlags() & SHF_WRITE); 2133c291e1aSRui Ueyama bool RO = (Cmd->Constraint == ConstraintKind::ReadOnly); 2143c291e1aSRui Ueyama bool RW = (Cmd->Constraint == ConstraintKind::ReadWrite); 2153c291e1aSRui Ueyama 216ed942713SRui Ueyama if ((RO && Writable) || (RW && !Writable)) 2179e69450eSGeorge Rimar OutputSections->erase(It); 2183c291e1aSRui Ueyama } 219e63d81bdSEugene Leviant } 220e63d81bdSEugene Leviant 221e63d81bdSEugene Leviant template <class ELFT> 22207320e40SRui Ueyama void LinkerScript<ELFT>::assignAddresses( 223dbbd8b15SGeorge Rimar ArrayRef<OutputSectionBase<ELFT> *> Sections) { 224652852c5SGeorge Rimar // Orphan sections are sections present in the input files which 2257c18c28cSRui Ueyama // are not explicitly placed into the output file by the linker script. 2267c18c28cSRui Ueyama // We place orphan sections at end of file. 2277c18c28cSRui Ueyama // Other linkers places them using some heuristics as described in 228652852c5SGeorge Rimar // https://sourceware.org/binutils/docs/ld/Orphan-Sections.html#Orphan-Sections. 2297c18c28cSRui Ueyama for (OutputSectionBase<ELFT> *Sec : Sections) { 230652852c5SGeorge Rimar StringRef Name = Sec->getName(); 231c3e2a4b0SRui Ueyama if (getSectionIndex(Name) == INT_MAX) 232076fe157SGeorge Rimar Opt.Commands.push_back(llvm::make_unique<OutputSectionCommand>(Name)); 233652852c5SGeorge Rimar } 234652852c5SGeorge Rimar 2357c18c28cSRui Ueyama // Assign addresses as instructed by linker script SECTIONS sub-commands. 236c998a8c0SRui Ueyama Dot = Out<ELFT>::ElfHeader->getSize() + Out<ELFT>::ProgramHeaders->getSize(); 237467c4d55SEugene Leviant uintX_t MinVA = std::numeric_limits<uintX_t>::max(); 238652852c5SGeorge Rimar uintX_t ThreadBssOffset = 0; 239652852c5SGeorge Rimar 240076fe157SGeorge Rimar for (const std::unique_ptr<BaseCommand> &Base : Opt.Commands) { 241076fe157SGeorge Rimar if (auto *Cmd = dyn_cast<SymbolAssignment>(Base.get())) { 2428d083e6aSRui Ueyama if (Cmd->Name == ".") { 2438d083e6aSRui Ueyama Dot = Cmd->Expression(Dot); 2448d083e6aSRui Ueyama } else if (Cmd->Sym) { 2458d083e6aSRui Ueyama cast<DefinedRegular<ELFT>>(Cmd->Sym)->Value = Cmd->Expression(Dot); 2468d083e6aSRui Ueyama } 24705ef4cffSRui Ueyama continue; 248652852c5SGeorge Rimar } 249652852c5SGeorge Rimar 250fb8978fcSDima Stepanov // Find all the sections with required name. There can be more than 2516ad330acSGeorge Rimar // one section with such name, if the alignment, flags or type 252fb8978fcSDima Stepanov // attribute differs. 253076fe157SGeorge Rimar auto *Cmd = cast<OutputSectionCommand>(Base.get()); 254fb8978fcSDima Stepanov for (OutputSectionBase<ELFT> *Sec : Sections) { 255076fe157SGeorge Rimar if (Sec->getName() != Cmd->Name) 256652852c5SGeorge Rimar continue; 257652852c5SGeorge Rimar 25858e5c4dcSGeorge Rimar if (Cmd->AddrExpr) 25958e5c4dcSGeorge Rimar Dot = Cmd->AddrExpr(Dot); 26058e5c4dcSGeorge Rimar 261630c6179SGeorge Rimar if (Cmd->AlignExpr) 262630c6179SGeorge Rimar Sec->updateAlignment(Cmd->AlignExpr(Dot)); 263630c6179SGeorge Rimar 264652852c5SGeorge Rimar if ((Sec->getFlags() & SHF_TLS) && Sec->getType() == SHT_NOBITS) { 265c998a8c0SRui Ueyama uintX_t TVA = Dot + ThreadBssOffset; 266424b4081SRui Ueyama TVA = alignTo(TVA, Sec->getAlignment()); 267652852c5SGeorge Rimar Sec->setVA(TVA); 268c998a8c0SRui Ueyama ThreadBssOffset = TVA - Dot + Sec->getSize(); 269652852c5SGeorge Rimar continue; 270652852c5SGeorge Rimar } 271652852c5SGeorge Rimar 272652852c5SGeorge Rimar if (Sec->getFlags() & SHF_ALLOC) { 273424b4081SRui Ueyama Dot = alignTo(Dot, Sec->getAlignment()); 274c998a8c0SRui Ueyama Sec->setVA(Dot); 275467c4d55SEugene Leviant MinVA = std::min(MinVA, Dot); 276c998a8c0SRui Ueyama Dot += Sec->getSize(); 277652852c5SGeorge Rimar continue; 278652852c5SGeorge Rimar } 279652852c5SGeorge Rimar } 280652852c5SGeorge Rimar } 281467c4d55SEugene Leviant 28264c32d6fSRafael Espindola // ELF and Program headers need to be right before the first section in 283b91e7118SGeorge Rimar // memory. Set their addresses accordingly. 284467c4d55SEugene Leviant MinVA = alignDown(MinVA - Out<ELFT>::ElfHeader->getSize() - 285467c4d55SEugene Leviant Out<ELFT>::ProgramHeaders->getSize(), 286467c4d55SEugene Leviant Target->PageSize); 287467c4d55SEugene Leviant Out<ELFT>::ElfHeader->setVA(MinVA); 288467c4d55SEugene Leviant Out<ELFT>::ProgramHeaders->setVA(Out<ELFT>::ElfHeader->getSize() + MinVA); 289fb8978fcSDima Stepanov } 290652852c5SGeorge Rimar 29107320e40SRui Ueyama template <class ELFT> 29274df5c7eSRafael Espindola std::vector<PhdrEntry<ELFT>> 293bbe38602SEugene Leviant LinkerScript<ELFT>::createPhdrs(ArrayRef<OutputSectionBase<ELFT> *> Sections) { 294edebbdf1SRui Ueyama std::vector<PhdrEntry<ELFT>> Ret; 295bbe38602SEugene Leviant 296bbe38602SEugene Leviant for (const PhdrsCommand &Cmd : Opt.PhdrsCommands) { 297edebbdf1SRui Ueyama Ret.emplace_back(Cmd.Type, Cmd.Flags == UINT_MAX ? PF_R : Cmd.Flags); 298edebbdf1SRui Ueyama PhdrEntry<ELFT> &Phdr = Ret.back(); 299bbe38602SEugene Leviant 300bbe38602SEugene Leviant if (Cmd.HasFilehdr) 301adca245fSRui Ueyama Phdr.add(Out<ELFT>::ElfHeader); 302bbe38602SEugene Leviant if (Cmd.HasPhdrs) 303adca245fSRui Ueyama Phdr.add(Out<ELFT>::ProgramHeaders); 304bbe38602SEugene Leviant 305bbe38602SEugene Leviant switch (Cmd.Type) { 306bbe38602SEugene Leviant case PT_INTERP: 307fd03cfd2SRui Ueyama if (Out<ELFT>::Interp) 308adca245fSRui Ueyama Phdr.add(Out<ELFT>::Interp); 309bbe38602SEugene Leviant break; 310bbe38602SEugene Leviant case PT_DYNAMIC: 311bbe38602SEugene Leviant if (isOutputDynamic<ELFT>()) { 3120b113671SRafael Espindola Phdr.H.p_flags = Out<ELFT>::Dynamic->getPhdrFlags(); 313adca245fSRui Ueyama Phdr.add(Out<ELFT>::Dynamic); 314bbe38602SEugene Leviant } 315bbe38602SEugene Leviant break; 316bbe38602SEugene Leviant case PT_GNU_EH_FRAME: 317bbe38602SEugene Leviant if (!Out<ELFT>::EhFrame->empty() && Out<ELFT>::EhFrameHdr) { 3180b113671SRafael Espindola Phdr.H.p_flags = Out<ELFT>::EhFrameHdr->getPhdrFlags(); 319adca245fSRui Ueyama Phdr.add(Out<ELFT>::EhFrameHdr); 320bbe38602SEugene Leviant } 321bbe38602SEugene Leviant break; 322bbe38602SEugene Leviant } 323bbe38602SEugene Leviant } 324bbe38602SEugene Leviant 325edebbdf1SRui Ueyama PhdrEntry<ELFT> *Load = nullptr; 326edebbdf1SRui Ueyama uintX_t Flags = PF_R; 327bbe38602SEugene Leviant for (OutputSectionBase<ELFT> *Sec : Sections) { 328bbe38602SEugene Leviant if (!(Sec->getFlags() & SHF_ALLOC)) 329bbe38602SEugene Leviant break; 330bbe38602SEugene Leviant 331edebbdf1SRui Ueyama std::vector<size_t> PhdrIds = getPhdrIndices(Sec->getName()); 332bbe38602SEugene Leviant if (!PhdrIds.empty()) { 333bbe38602SEugene Leviant // Assign headers specified by linker script 334bbe38602SEugene Leviant for (size_t Id : PhdrIds) { 335edebbdf1SRui Ueyama Ret[Id].add(Sec); 336865bf863SEugene Leviant if (Opt.PhdrsCommands[Id].Flags == UINT_MAX) 3370b113671SRafael Espindola Ret[Id].H.p_flags |= Sec->getPhdrFlags(); 338bbe38602SEugene Leviant } 339bbe38602SEugene Leviant } else { 340bbe38602SEugene Leviant // If we have no load segment or flags've changed then we want new load 341bbe38602SEugene Leviant // segment. 3420b113671SRafael Espindola uintX_t NewFlags = Sec->getPhdrFlags(); 343bbe38602SEugene Leviant if (Load == nullptr || Flags != NewFlags) { 344edebbdf1SRui Ueyama Load = &*Ret.emplace(Ret.end(), PT_LOAD, NewFlags); 345bbe38602SEugene Leviant Flags = NewFlags; 346bbe38602SEugene Leviant } 34718f084ffSRui Ueyama Load->add(Sec); 348bbe38602SEugene Leviant } 349bbe38602SEugene Leviant } 350edebbdf1SRui Ueyama return Ret; 351bbe38602SEugene Leviant } 352bbe38602SEugene Leviant 353bbe38602SEugene Leviant template <class ELFT> 35407320e40SRui Ueyama ArrayRef<uint8_t> LinkerScript<ELFT>::getFiller(StringRef Name) { 355f6c3ccefSGeorge Rimar for (const std::unique_ptr<BaseCommand> &Base : Opt.Commands) 356f6c3ccefSGeorge Rimar if (auto *Cmd = dyn_cast<OutputSectionCommand>(Base.get())) 357f6c3ccefSGeorge Rimar if (Cmd->Name == Name) 358f6c3ccefSGeorge Rimar return Cmd->Filler; 359e2ee72b5SGeorge Rimar return {}; 360e2ee72b5SGeorge Rimar } 361e2ee72b5SGeorge Rimar 362c3e2a4b0SRui Ueyama // Returns the index of the given section name in linker script 363c3e2a4b0SRui Ueyama // SECTIONS commands. Sections are laid out as the same order as they 364c3e2a4b0SRui Ueyama // were in the script. If a given name did not appear in the script, 365c3e2a4b0SRui Ueyama // it returns INT_MAX, so that it will be laid out at end of file. 366076fe157SGeorge Rimar template <class ELFT> int LinkerScript<ELFT>::getSectionIndex(StringRef Name) { 367f510fa6bSRui Ueyama int I = 0; 368f510fa6bSRui Ueyama for (std::unique_ptr<BaseCommand> &Base : Opt.Commands) { 369076fe157SGeorge Rimar if (auto *Cmd = dyn_cast<OutputSectionCommand>(Base.get())) 370076fe157SGeorge Rimar if (Cmd->Name == Name) 371f510fa6bSRui Ueyama return I; 372f510fa6bSRui Ueyama ++I; 373f510fa6bSRui Ueyama } 374f510fa6bSRui Ueyama return INT_MAX; 37571b26e94SGeorge Rimar } 37671b26e94SGeorge Rimar 37771b26e94SGeorge Rimar // A compartor to sort output sections. Returns -1 or 1 if 37871b26e94SGeorge Rimar // A or B are mentioned in linker script. Otherwise, returns 0. 37907320e40SRui Ueyama template <class ELFT> 38007320e40SRui Ueyama int LinkerScript<ELFT>::compareSections(StringRef A, StringRef B) { 381c3e2a4b0SRui Ueyama int I = getSectionIndex(A); 382c3e2a4b0SRui Ueyama int J = getSectionIndex(B); 383c3e2a4b0SRui Ueyama if (I == INT_MAX && J == INT_MAX) 384717677afSRui Ueyama return 0; 385717677afSRui Ueyama return I < J ? -1 : 1; 386717677afSRui Ueyama } 387717677afSRui Ueyama 3888d083e6aSRui Ueyama // Add symbols defined by linker scripts. 389076fe157SGeorge Rimar template <class ELFT> void LinkerScript<ELFT>::addScriptedSymbols() { 390a31c91b1SEugene Leviant for (const std::unique_ptr<BaseCommand> &Base : Opt.Commands) { 391a31c91b1SEugene Leviant auto *Cmd = dyn_cast<SymbolAssignment>(Base.get()); 392a31c91b1SEugene Leviant if (!Cmd || Cmd->Name == ".") 393a31c91b1SEugene Leviant continue; 394a31c91b1SEugene Leviant 3958d083e6aSRui Ueyama // If a symbol was in PROVIDE(), define it only when it is an 3968d083e6aSRui Ueyama // undefined symbol. 3978ab4108dSDavide Italiano SymbolBody *B = Symtab<ELFT>::X->find(Cmd->Name); 3988d083e6aSRui Ueyama if (Cmd->Provide && !(B && B->isUndefined())) 3998d083e6aSRui Ueyama continue; 4008d083e6aSRui Ueyama 4018d083e6aSRui Ueyama // Define an absolute symbol. The symbol value will be assigned later. 4028d083e6aSRui Ueyama // (At this point, we don't know the final address yet.) 4038d083e6aSRui Ueyama Symbol *Sym = Symtab<ELFT>::X->addUndefined(Cmd->Name); 4048d083e6aSRui Ueyama replaceBody<DefinedRegular<ELFT>>(Sym, Cmd->Name, STV_DEFAULT); 4058d083e6aSRui Ueyama Sym->Visibility = Cmd->Hidden ? STV_HIDDEN : STV_DEFAULT; 4068d083e6aSRui Ueyama Cmd->Sym = Sym->body(); 407a31c91b1SEugene Leviant } 408eda81a1bSEugene Leviant } 409eda81a1bSEugene Leviant 410bbe38602SEugene Leviant template <class ELFT> bool LinkerScript<ELFT>::hasPhdrsCommands() { 411bbe38602SEugene Leviant return !Opt.PhdrsCommands.empty(); 412bbe38602SEugene Leviant } 413bbe38602SEugene Leviant 4149e69450eSGeorge Rimar template <class ELFT> 4159e69450eSGeorge Rimar typename ELFT::uint LinkerScript<ELFT>::getOutputSectionSize(StringRef Name) { 4169e69450eSGeorge Rimar for (OutputSectionBase<ELFT> *Sec : *OutputSections) 4179e69450eSGeorge Rimar if (Sec->getName() == Name) 4189e69450eSGeorge Rimar return Sec->getSize(); 4199e69450eSGeorge Rimar error("undefined section " + Name); 4209e69450eSGeorge Rimar return 0; 4219e69450eSGeorge Rimar } 4229e69450eSGeorge Rimar 423bbe38602SEugene Leviant // Returns indices of ELF headers containing specific section, identified 424bbe38602SEugene Leviant // by Name. Each index is a zero based number of ELF header listed within 425bbe38602SEugene Leviant // PHDRS {} script block. 426bbe38602SEugene Leviant template <class ELFT> 427edebbdf1SRui Ueyama std::vector<size_t> LinkerScript<ELFT>::getPhdrIndices(StringRef SectionName) { 428076fe157SGeorge Rimar for (const std::unique_ptr<BaseCommand> &Base : Opt.Commands) { 429076fe157SGeorge Rimar auto *Cmd = dyn_cast<OutputSectionCommand>(Base.get()); 430edebbdf1SRui Ueyama if (!Cmd || Cmd->Name != SectionName) 43131d842f5SGeorge Rimar continue; 43231d842f5SGeorge Rimar 43329c5a2a9SRui Ueyama std::vector<size_t> Ret; 43429c5a2a9SRui Ueyama for (StringRef PhdrName : Cmd->Phdrs) 43529c5a2a9SRui Ueyama Ret.push_back(getPhdrIndex(PhdrName)); 43629c5a2a9SRui Ueyama return Ret; 437bbe38602SEugene Leviant } 43831d842f5SGeorge Rimar return {}; 43931d842f5SGeorge Rimar } 440bbe38602SEugene Leviant 44129c5a2a9SRui Ueyama template <class ELFT> 44229c5a2a9SRui Ueyama size_t LinkerScript<ELFT>::getPhdrIndex(StringRef PhdrName) { 44329c5a2a9SRui Ueyama size_t I = 0; 44429c5a2a9SRui Ueyama for (PhdrsCommand &Cmd : Opt.PhdrsCommands) { 44529c5a2a9SRui Ueyama if (Cmd.Name == PhdrName) 44629c5a2a9SRui Ueyama return I; 44729c5a2a9SRui Ueyama ++I; 44829c5a2a9SRui Ueyama } 44929c5a2a9SRui Ueyama error("section header '" + PhdrName + "' is not listed in PHDRS"); 45029c5a2a9SRui Ueyama return 0; 45129c5a2a9SRui Ueyama } 45229c5a2a9SRui Ueyama 45307320e40SRui Ueyama class elf::ScriptParser : public ScriptParserBase { 454c3794e58SGeorge Rimar typedef void (ScriptParser::*Handler)(); 455c3794e58SGeorge Rimar 456f7c5fbb1SRui Ueyama public: 45707320e40SRui Ueyama ScriptParser(StringRef S, bool B) : ScriptParserBase(S), IsUnderSysroot(B) {} 458f23b2320SGeorge Rimar 4594a46539cSRui Ueyama void run(); 460f7c5fbb1SRui Ueyama 461f7c5fbb1SRui Ueyama private: 46252a1509eSRui Ueyama void addFile(StringRef Path); 46352a1509eSRui Ueyama 464f7c5fbb1SRui Ueyama void readAsNeeded(); 46590c5099eSDenis Protivensky void readEntry(); 46683f406cfSGeorge Rimar void readExtern(); 467f7c5fbb1SRui Ueyama void readGroup(); 46831aa1f83SRui Ueyama void readInclude(); 469c3794e58SGeorge Rimar void readNothing() {} 470ee59282bSRui Ueyama void readOutput(); 4719159ce93SDavide Italiano void readOutputArch(); 472f7c5fbb1SRui Ueyama void readOutputFormat(); 473bbe38602SEugene Leviant void readPhdrs(); 47468a39a65SDavide Italiano void readSearchDir(); 4758e3b38abSDenis Protivensky void readSections(); 4768e3b38abSDenis Protivensky 477113cdec9SRui Ueyama SymbolAssignment *readAssignment(StringRef Name); 478eda81a1bSEugene Leviant void readOutputSectionDescription(StringRef OutSec); 479f71caa2bSRui Ueyama std::vector<uint8_t> readOutputSectionFiller(); 480bbe38602SEugene Leviant std::vector<StringRef> readOutputSectionPhdrs(); 4810659800eSGeorge Rimar std::unique_ptr<InputSectionDescription> readInputSectionDescription(); 4820702c4e8SGeorge Rimar void readInputFilePattern(InputSectionDescription *InCmd, bool Keep); 4830659800eSGeorge Rimar void readInputSectionRules(InputSectionDescription *InCmd, bool Keep); 484bbe38602SEugene Leviant unsigned readPhdrType(); 485a31c91b1SEugene Leviant void readProvide(bool Hidden); 486630c6179SGeorge Rimar void readAlign(OutputSectionCommand *Cmd); 48703fc010eSGeorge Rimar void readSort(); 488708019c4SRui Ueyama 489708019c4SRui Ueyama Expr readExpr(); 490708019c4SRui Ueyama Expr readExpr1(Expr Lhs, int MinPrec); 491708019c4SRui Ueyama Expr readPrimary(); 492708019c4SRui Ueyama Expr readTernary(Expr Cond); 493708019c4SRui Ueyama Expr combine(StringRef Op, Expr Lhs, Expr Rhs); 494f7c5fbb1SRui Ueyama 495c3794e58SGeorge Rimar const static StringMap<Handler> Cmd; 49607320e40SRui Ueyama ScriptConfiguration &Opt = *ScriptConfig; 49707320e40SRui Ueyama StringSaver Saver = {ScriptConfig->Alloc}; 49816b0cc9eSSimon Atanasyan bool IsUnderSysroot; 499f7c5fbb1SRui Ueyama }; 500f7c5fbb1SRui Ueyama 501e0df00b9SRafael Espindola const StringMap<elf::ScriptParser::Handler> elf::ScriptParser::Cmd = { 502c3794e58SGeorge Rimar {"ENTRY", &ScriptParser::readEntry}, 503c3794e58SGeorge Rimar {"EXTERN", &ScriptParser::readExtern}, 504c3794e58SGeorge Rimar {"GROUP", &ScriptParser::readGroup}, 505c3794e58SGeorge Rimar {"INCLUDE", &ScriptParser::readInclude}, 506c3794e58SGeorge Rimar {"INPUT", &ScriptParser::readGroup}, 507c3794e58SGeorge Rimar {"OUTPUT", &ScriptParser::readOutput}, 508c3794e58SGeorge Rimar {"OUTPUT_ARCH", &ScriptParser::readOutputArch}, 509c3794e58SGeorge Rimar {"OUTPUT_FORMAT", &ScriptParser::readOutputFormat}, 510bbe38602SEugene Leviant {"PHDRS", &ScriptParser::readPhdrs}, 511c3794e58SGeorge Rimar {"SEARCH_DIR", &ScriptParser::readSearchDir}, 512c3794e58SGeorge Rimar {"SECTIONS", &ScriptParser::readSections}, 513c3794e58SGeorge Rimar {";", &ScriptParser::readNothing}}; 514c3794e58SGeorge Rimar 515717677afSRui Ueyama void ScriptParser::run() { 516f7c5fbb1SRui Ueyama while (!atEOF()) { 517f7c5fbb1SRui Ueyama StringRef Tok = next(); 518c3794e58SGeorge Rimar if (Handler Fn = Cmd.lookup(Tok)) 519c3794e58SGeorge Rimar (this->*Fn)(); 520c3794e58SGeorge Rimar else 5215761042dSGeorge Rimar setError("unknown directive: " + Tok); 522f7c5fbb1SRui Ueyama } 523f7c5fbb1SRui Ueyama } 524f7c5fbb1SRui Ueyama 525717677afSRui Ueyama void ScriptParser::addFile(StringRef S) { 52616b0cc9eSSimon Atanasyan if (IsUnderSysroot && S.startswith("/")) { 52716b0cc9eSSimon Atanasyan SmallString<128> Path; 52816b0cc9eSSimon Atanasyan (Config->Sysroot + S).toStringRef(Path); 52916b0cc9eSSimon Atanasyan if (sys::fs::exists(Path)) { 53016b0cc9eSSimon Atanasyan Driver->addFile(Saver.save(Path.str())); 53116b0cc9eSSimon Atanasyan return; 53216b0cc9eSSimon Atanasyan } 53316b0cc9eSSimon Atanasyan } 53416b0cc9eSSimon Atanasyan 535f03f3cc1SRui Ueyama if (sys::path::is_absolute(S)) { 53652a1509eSRui Ueyama Driver->addFile(S); 53752a1509eSRui Ueyama } else if (S.startswith("=")) { 53852a1509eSRui Ueyama if (Config->Sysroot.empty()) 53952a1509eSRui Ueyama Driver->addFile(S.substr(1)); 54052a1509eSRui Ueyama else 54152a1509eSRui Ueyama Driver->addFile(Saver.save(Config->Sysroot + "/" + S.substr(1))); 54252a1509eSRui Ueyama } else if (S.startswith("-l")) { 54321eecb4fSRui Ueyama Driver->addLibrary(S.substr(2)); 544a1b8fc3bSSimon Atanasyan } else if (sys::fs::exists(S)) { 545a1b8fc3bSSimon Atanasyan Driver->addFile(S); 54652a1509eSRui Ueyama } else { 54752a1509eSRui Ueyama std::string Path = findFromSearchPaths(S); 54852a1509eSRui Ueyama if (Path.empty()) 549777f9630SGeorge Rimar setError("unable to find " + S); 550025d59b1SRui Ueyama else 55152a1509eSRui Ueyama Driver->addFile(Saver.save(Path)); 55252a1509eSRui Ueyama } 55352a1509eSRui Ueyama } 55452a1509eSRui Ueyama 555717677afSRui Ueyama void ScriptParser::readAsNeeded() { 556f7c5fbb1SRui Ueyama expect("("); 55735da9b6eSRui Ueyama bool Orig = Config->AsNeeded; 55835da9b6eSRui Ueyama Config->AsNeeded = true; 559025d59b1SRui Ueyama while (!Error) { 560f7c5fbb1SRui Ueyama StringRef Tok = next(); 561f7c5fbb1SRui Ueyama if (Tok == ")") 56235da9b6eSRui Ueyama break; 56352a1509eSRui Ueyama addFile(Tok); 564f7c5fbb1SRui Ueyama } 56535da9b6eSRui Ueyama Config->AsNeeded = Orig; 566f7c5fbb1SRui Ueyama } 567f7c5fbb1SRui Ueyama 568717677afSRui Ueyama void ScriptParser::readEntry() { 56990c5099eSDenis Protivensky // -e <symbol> takes predecence over ENTRY(<symbol>). 57090c5099eSDenis Protivensky expect("("); 57190c5099eSDenis Protivensky StringRef Tok = next(); 57290c5099eSDenis Protivensky if (Config->Entry.empty()) 57390c5099eSDenis Protivensky Config->Entry = Tok; 57490c5099eSDenis Protivensky expect(")"); 57590c5099eSDenis Protivensky } 57690c5099eSDenis Protivensky 577717677afSRui Ueyama void ScriptParser::readExtern() { 57883f406cfSGeorge Rimar expect("("); 579025d59b1SRui Ueyama while (!Error) { 58083f406cfSGeorge Rimar StringRef Tok = next(); 58183f406cfSGeorge Rimar if (Tok == ")") 58283f406cfSGeorge Rimar return; 58383f406cfSGeorge Rimar Config->Undefined.push_back(Tok); 58483f406cfSGeorge Rimar } 58583f406cfSGeorge Rimar } 58683f406cfSGeorge Rimar 587717677afSRui Ueyama void ScriptParser::readGroup() { 588f7c5fbb1SRui Ueyama expect("("); 589025d59b1SRui Ueyama while (!Error) { 590f7c5fbb1SRui Ueyama StringRef Tok = next(); 591f7c5fbb1SRui Ueyama if (Tok == ")") 592f7c5fbb1SRui Ueyama return; 593f7c5fbb1SRui Ueyama if (Tok == "AS_NEEDED") { 594f7c5fbb1SRui Ueyama readAsNeeded(); 595f7c5fbb1SRui Ueyama continue; 596f7c5fbb1SRui Ueyama } 59752a1509eSRui Ueyama addFile(Tok); 598f7c5fbb1SRui Ueyama } 599f7c5fbb1SRui Ueyama } 600f7c5fbb1SRui Ueyama 601717677afSRui Ueyama void ScriptParser::readInclude() { 60231aa1f83SRui Ueyama StringRef Tok = next(); 60331aa1f83SRui Ueyama auto MBOrErr = MemoryBuffer::getFile(Tok); 604025d59b1SRui Ueyama if (!MBOrErr) { 6055761042dSGeorge Rimar setError("cannot open " + Tok); 606025d59b1SRui Ueyama return; 607025d59b1SRui Ueyama } 60831aa1f83SRui Ueyama std::unique_ptr<MemoryBuffer> &MB = *MBOrErr; 609a47ee68dSRui Ueyama StringRef S = Saver.save(MB->getMemBufferRef().getBuffer()); 610a47ee68dSRui Ueyama std::vector<StringRef> V = tokenize(S); 61131aa1f83SRui Ueyama Tokens.insert(Tokens.begin() + Pos, V.begin(), V.end()); 61231aa1f83SRui Ueyama } 61331aa1f83SRui Ueyama 614717677afSRui Ueyama void ScriptParser::readOutput() { 615ee59282bSRui Ueyama // -o <file> takes predecence over OUTPUT(<file>). 616ee59282bSRui Ueyama expect("("); 617ee59282bSRui Ueyama StringRef Tok = next(); 618ee59282bSRui Ueyama if (Config->OutputFile.empty()) 619ee59282bSRui Ueyama Config->OutputFile = Tok; 620ee59282bSRui Ueyama expect(")"); 621ee59282bSRui Ueyama } 622ee59282bSRui Ueyama 623717677afSRui Ueyama void ScriptParser::readOutputArch() { 6249159ce93SDavide Italiano // Error checking only for now. 6259159ce93SDavide Italiano expect("("); 6269159ce93SDavide Italiano next(); 6279159ce93SDavide Italiano expect(")"); 6289159ce93SDavide Italiano } 6299159ce93SDavide Italiano 630717677afSRui Ueyama void ScriptParser::readOutputFormat() { 631f7c5fbb1SRui Ueyama // Error checking only for now. 632f7c5fbb1SRui Ueyama expect("("); 633f7c5fbb1SRui Ueyama next(); 6346836c618SDavide Italiano StringRef Tok = next(); 6356836c618SDavide Italiano if (Tok == ")") 6366836c618SDavide Italiano return; 637025d59b1SRui Ueyama if (Tok != ",") { 6385761042dSGeorge Rimar setError("unexpected token: " + Tok); 639025d59b1SRui Ueyama return; 640025d59b1SRui Ueyama } 6416836c618SDavide Italiano next(); 6426836c618SDavide Italiano expect(","); 6436836c618SDavide Italiano next(); 644f7c5fbb1SRui Ueyama expect(")"); 645f7c5fbb1SRui Ueyama } 646f7c5fbb1SRui Ueyama 647bbe38602SEugene Leviant void ScriptParser::readPhdrs() { 648bbe38602SEugene Leviant expect("{"); 649bbe38602SEugene Leviant while (!Error && !skip("}")) { 650bbe38602SEugene Leviant StringRef Tok = next(); 651865bf863SEugene Leviant Opt.PhdrsCommands.push_back({Tok, PT_NULL, false, false, UINT_MAX}); 652bbe38602SEugene Leviant PhdrsCommand &PhdrCmd = Opt.PhdrsCommands.back(); 653bbe38602SEugene Leviant 654bbe38602SEugene Leviant PhdrCmd.Type = readPhdrType(); 655bbe38602SEugene Leviant do { 656bbe38602SEugene Leviant Tok = next(); 657bbe38602SEugene Leviant if (Tok == ";") 658bbe38602SEugene Leviant break; 659bbe38602SEugene Leviant if (Tok == "FILEHDR") 660bbe38602SEugene Leviant PhdrCmd.HasFilehdr = true; 661bbe38602SEugene Leviant else if (Tok == "PHDRS") 662bbe38602SEugene Leviant PhdrCmd.HasPhdrs = true; 663865bf863SEugene Leviant else if (Tok == "FLAGS") { 664865bf863SEugene Leviant expect("("); 665eb685cd7SRafael Espindola // Passing 0 for the value of dot is a bit of a hack. It means that 666eb685cd7SRafael Espindola // we accept expressions like ".|1". 667eb685cd7SRafael Espindola PhdrCmd.Flags = readExpr()(0); 668865bf863SEugene Leviant expect(")"); 669865bf863SEugene Leviant } else 670bbe38602SEugene Leviant setError("unexpected header attribute: " + Tok); 671bbe38602SEugene Leviant } while (!Error); 672bbe38602SEugene Leviant } 673bbe38602SEugene Leviant } 674bbe38602SEugene Leviant 675717677afSRui Ueyama void ScriptParser::readSearchDir() { 67668a39a65SDavide Italiano expect("("); 67706501920SRafael Espindola Config->SearchPaths.push_back(next()); 67868a39a65SDavide Italiano expect(")"); 67968a39a65SDavide Italiano } 68068a39a65SDavide Italiano 681717677afSRui Ueyama void ScriptParser::readSections() { 6823de0a330SRui Ueyama Opt.HasContents = true; 6838e3b38abSDenis Protivensky expect("{"); 684652852c5SGeorge Rimar while (!Error && !skip("}")) { 685113cdec9SRui Ueyama StringRef Tok = next(); 68630835ea4SGeorge Rimar if (peek() == "=" || peek() == "+=") { 687113cdec9SRui Ueyama readAssignment(Tok); 688113cdec9SRui Ueyama expect(";"); 689113cdec9SRui Ueyama } else if (Tok == "PROVIDE") { 690a31c91b1SEugene Leviant readProvide(false); 691708019c4SRui Ueyama } else if (Tok == "PROVIDE_HIDDEN") { 692a31c91b1SEugene Leviant readProvide(true); 693708019c4SRui Ueyama } else { 694eda81a1bSEugene Leviant readOutputSectionDescription(Tok); 6958e3b38abSDenis Protivensky } 696652852c5SGeorge Rimar } 697708019c4SRui Ueyama } 6988e3b38abSDenis Protivensky 699708019c4SRui Ueyama static int precedence(StringRef Op) { 700708019c4SRui Ueyama return StringSwitch<int>(Op) 701708019c4SRui Ueyama .Case("*", 4) 702708019c4SRui Ueyama .Case("/", 4) 703708019c4SRui Ueyama .Case("+", 3) 704708019c4SRui Ueyama .Case("-", 3) 705708019c4SRui Ueyama .Case("<", 2) 706708019c4SRui Ueyama .Case(">", 2) 707708019c4SRui Ueyama .Case(">=", 2) 708708019c4SRui Ueyama .Case("<=", 2) 709708019c4SRui Ueyama .Case("==", 2) 710708019c4SRui Ueyama .Case("!=", 2) 711708019c4SRui Ueyama .Case("&", 1) 712708019c4SRui Ueyama .Default(-1); 713708019c4SRui Ueyama } 714708019c4SRui Ueyama 7150702c4e8SGeorge Rimar void ScriptParser::readInputFilePattern(InputSectionDescription *InCmd, 7160702c4e8SGeorge Rimar bool Keep) { 7170702c4e8SGeorge Rimar while (!Error && !skip(")")) { 7180702c4e8SGeorge Rimar if (Keep) 7190702c4e8SGeorge Rimar Opt.KeptSections.push_back(peek()); 7200702c4e8SGeorge Rimar InCmd->SectionPatterns.push_back(next()); 7210702c4e8SGeorge Rimar } 7220702c4e8SGeorge Rimar } 7230702c4e8SGeorge Rimar 7240702c4e8SGeorge Rimar void ScriptParser::readInputSectionRules(InputSectionDescription *InCmd, 7250702c4e8SGeorge Rimar bool Keep) { 7260659800eSGeorge Rimar InCmd->FilePattern = next(); 7270ed42b0cSDavide Italiano expect("("); 728e7282797SDavide Italiano 729e7282797SDavide Italiano if (skip("EXCLUDE_FILE")) { 730e7282797SDavide Italiano expect("("); 731e7282797SDavide Italiano while (!Error && !skip(")")) 732e7282797SDavide Italiano InCmd->ExcludedFiles.push_back(next()); 733e7282797SDavide Italiano } 734e7282797SDavide Italiano 735*350ece4eSGeorge Rimar if (skip("SORT") || skip("SORT_BY_NAME")) { 7360702c4e8SGeorge Rimar expect("("); 737*350ece4eSGeorge Rimar if (skip("SORT_BY_ALIGNMENT")) { 738*350ece4eSGeorge Rimar InCmd->Sort = SortKind::NameAlign; 739*350ece4eSGeorge Rimar expect("("); 7400702c4e8SGeorge Rimar readInputFilePattern(InCmd, Keep); 7410702c4e8SGeorge Rimar expect(")"); 742*350ece4eSGeorge Rimar } else { 743*350ece4eSGeorge Rimar InCmd->Sort = SortKind::Name; 744*350ece4eSGeorge Rimar readInputFilePattern(InCmd, Keep); 745*350ece4eSGeorge Rimar } 746*350ece4eSGeorge Rimar expect(")"); 747*350ece4eSGeorge Rimar return; 748*350ece4eSGeorge Rimar } 749*350ece4eSGeorge Rimar 750*350ece4eSGeorge Rimar if (skip("SORT_BY_ALIGNMENT")) { 751*350ece4eSGeorge Rimar expect("("); 752*350ece4eSGeorge Rimar if (skip("SORT") || skip("SORT_BY_NAME")) { 753*350ece4eSGeorge Rimar InCmd->Sort = SortKind::AlignName; 754*350ece4eSGeorge Rimar expect("("); 755*350ece4eSGeorge Rimar readInputFilePattern(InCmd, Keep); 756*350ece4eSGeorge Rimar expect(")"); 757*350ece4eSGeorge Rimar } else { 758*350ece4eSGeorge Rimar InCmd->Sort = SortKind::Align; 759*350ece4eSGeorge Rimar readInputFilePattern(InCmd, Keep); 760*350ece4eSGeorge Rimar } 761*350ece4eSGeorge Rimar expect(")"); 7620702c4e8SGeorge Rimar return; 7630659800eSGeorge Rimar } 7640702c4e8SGeorge Rimar 7650702c4e8SGeorge Rimar readInputFilePattern(InCmd, Keep); 7660659800eSGeorge Rimar } 7670659800eSGeorge Rimar 7680659800eSGeorge Rimar std::unique_ptr<InputSectionDescription> 7690659800eSGeorge Rimar ScriptParser::readInputSectionDescription() { 770352eac37SGeorge Rimar auto InCmd = llvm::make_unique<InputSectionDescription>(); 7710659800eSGeorge Rimar 7720659800eSGeorge Rimar // Input section wildcard can be surrounded by KEEP. 7730659800eSGeorge Rimar // https://sourceware.org/binutils/docs/ld/Input-Section-Keep.html#Input-Section-Keep 7740659800eSGeorge Rimar if (skip("KEEP")) { 775e7282797SDavide Italiano expect("("); 7760659800eSGeorge Rimar readInputSectionRules(InCmd.get(), true); 7770ed42b0cSDavide Italiano expect(")"); 7780659800eSGeorge Rimar } else { 7790659800eSGeorge Rimar readInputSectionRules(InCmd.get(), false); 7800659800eSGeorge Rimar } 7810659800eSGeorge Rimar 7820659800eSGeorge Rimar return InCmd; 7830ed42b0cSDavide Italiano } 7840ed42b0cSDavide Italiano 785630c6179SGeorge Rimar void ScriptParser::readAlign(OutputSectionCommand *Cmd) { 786630c6179SGeorge Rimar expect("("); 787630c6179SGeorge Rimar Cmd->AlignExpr = readExpr(); 788630c6179SGeorge Rimar expect(")"); 789630c6179SGeorge Rimar } 790630c6179SGeorge Rimar 79103fc010eSGeorge Rimar void ScriptParser::readSort() { 79203fc010eSGeorge Rimar expect("("); 79303fc010eSGeorge Rimar expect("CONSTRUCTORS"); 79403fc010eSGeorge Rimar expect(")"); 79503fc010eSGeorge Rimar } 79603fc010eSGeorge Rimar 797eda81a1bSEugene Leviant void ScriptParser::readOutputSectionDescription(StringRef OutSec) { 798076fe157SGeorge Rimar OutputSectionCommand *Cmd = new OutputSectionCommand(OutSec); 799076fe157SGeorge Rimar Opt.Commands.emplace_back(Cmd); 80058e5c4dcSGeorge Rimar 80158e5c4dcSGeorge Rimar // Read an address expression. 80258e5c4dcSGeorge Rimar // https://sourceware.org/binutils/docs/ld/Output-Section-Address.html#Output-Section-Address 80358e5c4dcSGeorge Rimar if (peek() != ":") 80458e5c4dcSGeorge Rimar Cmd->AddrExpr = readExpr(); 80558e5c4dcSGeorge Rimar 8068e3b38abSDenis Protivensky expect(":"); 807246f681eSDavide Italiano 808630c6179SGeorge Rimar if (skip("ALIGN")) 809630c6179SGeorge Rimar readAlign(Cmd); 810630c6179SGeorge Rimar 811246f681eSDavide Italiano // Parse constraints. 812246f681eSDavide Italiano if (skip("ONLY_IF_RO")) 813efc4066bSRui Ueyama Cmd->Constraint = ConstraintKind::ReadOnly; 814246f681eSDavide Italiano if (skip("ONLY_IF_RW")) 815efc4066bSRui Ueyama Cmd->Constraint = ConstraintKind::ReadWrite; 8168e3b38abSDenis Protivensky expect("{"); 8178ec77e64SRui Ueyama 818025d59b1SRui Ueyama while (!Error && !skip("}")) { 819f586ff7eSGeorge Rimar if (peek().startswith("*") || peek() == "KEEP") { 8200659800eSGeorge Rimar Cmd->Commands.push_back(readInputSectionDescription()); 8210659800eSGeorge Rimar continue; 8220659800eSGeorge Rimar } 8230659800eSGeorge Rimar 824481c2ce6SGeorge Rimar StringRef Tok = next(); 8250659800eSGeorge Rimar if (Tok == "PROVIDE") { 826054a6796SDavide Italiano readProvide(false); 827054a6796SDavide Italiano } else if (Tok == "PROVIDE_HIDDEN") { 828054a6796SDavide Italiano readProvide(true); 82903fc010eSGeorge Rimar } else if (Tok == "SORT") { 83003fc010eSGeorge Rimar readSort(); 831481c2ce6SGeorge Rimar } else { 832777f9630SGeorge Rimar setError("unknown command " + Tok); 833481c2ce6SGeorge Rimar } 8348e3b38abSDenis Protivensky } 835076fe157SGeorge Rimar Cmd->Phdrs = readOutputSectionPhdrs(); 836f71caa2bSRui Ueyama Cmd->Filler = readOutputSectionFiller(); 837f71caa2bSRui Ueyama } 8388ec77e64SRui Ueyama 839f71caa2bSRui Ueyama std::vector<uint8_t> ScriptParser::readOutputSectionFiller() { 840e2ee72b5SGeorge Rimar StringRef Tok = peek(); 841f71caa2bSRui Ueyama if (!Tok.startswith("=")) 842f71caa2bSRui Ueyama return {}; 8435ac0d7c5SDavide Italiano next(); 8445ac0d7c5SDavide Italiano if (Tok.startswith("=0x")) 8455ac0d7c5SDavide Italiano return parseHex(Tok.substr(3)); 8465ac0d7c5SDavide Italiano 8475ac0d7c5SDavide Italiano // This must be a decimal. 8485ac0d7c5SDavide Italiano unsigned int Value; 8495ac0d7c5SDavide Italiano if (Tok.substr(1).getAsInteger(10, Value)) { 8505ac0d7c5SDavide Italiano setError("filler should be a decimal/hexadecimal value"); 851f71caa2bSRui Ueyama return {}; 852e2ee72b5SGeorge Rimar } 8535ac0d7c5SDavide Italiano if (Value > 255) 8545ac0d7c5SDavide Italiano setError("only single bytes decimal are supported for the filler now"); 8555ac0d7c5SDavide Italiano return {static_cast<unsigned char>(Value)}; 8568e3b38abSDenis Protivensky } 8578e3b38abSDenis Protivensky 858a31c91b1SEugene Leviant void ScriptParser::readProvide(bool Hidden) { 859a31c91b1SEugene Leviant expect("("); 860174e0a16SRui Ueyama SymbolAssignment *Cmd = readAssignment(next()); 861174e0a16SRui Ueyama Cmd->Provide = true; 862174e0a16SRui Ueyama Cmd->Hidden = Hidden; 863a31c91b1SEugene Leviant expect(")"); 864a31c91b1SEugene Leviant expect(";"); 865eda81a1bSEugene Leviant } 866eda81a1bSEugene Leviant 86730835ea4SGeorge Rimar static uint64_t getSymbolValue(StringRef S, uint64_t Dot) { 86830835ea4SGeorge Rimar if (S == ".") 86930835ea4SGeorge Rimar return Dot; 870a31c91b1SEugene Leviant 871a9c5a528SGeorge Rimar switch (Config->EKind) { 872a9c5a528SGeorge Rimar case ELF32LEKind: 873a9c5a528SGeorge Rimar if (SymbolBody *B = Symtab<ELF32LE>::X->find(S)) 874a9c5a528SGeorge Rimar return B->getVA<ELF32LE>(); 875a9c5a528SGeorge Rimar break; 876a9c5a528SGeorge Rimar case ELF32BEKind: 877a9c5a528SGeorge Rimar if (SymbolBody *B = Symtab<ELF32BE>::X->find(S)) 878a9c5a528SGeorge Rimar return B->getVA<ELF32BE>(); 879a9c5a528SGeorge Rimar break; 880a9c5a528SGeorge Rimar case ELF64LEKind: 881a9c5a528SGeorge Rimar if (SymbolBody *B = Symtab<ELF64LE>::X->find(S)) 882a9c5a528SGeorge Rimar return B->getVA<ELF64LE>(); 883a9c5a528SGeorge Rimar break; 884a9c5a528SGeorge Rimar case ELF64BEKind: 885a9c5a528SGeorge Rimar if (SymbolBody *B = Symtab<ELF64BE>::X->find(S)) 886a9c5a528SGeorge Rimar return B->getVA<ELF64BE>(); 887a9c5a528SGeorge Rimar break; 8886930a6dcSGeorge Rimar default: 889b567b628SGeorge Rimar llvm_unreachable("unsupported target"); 890a9c5a528SGeorge Rimar } 891a9c5a528SGeorge Rimar error("symbol not found: " + S); 892a9c5a528SGeorge Rimar return 0; 893a9c5a528SGeorge Rimar } 894a9c5a528SGeorge Rimar 8959e69450eSGeorge Rimar static uint64_t getSectionSize(StringRef Name) { 8969e69450eSGeorge Rimar switch (Config->EKind) { 8979e69450eSGeorge Rimar case ELF32LEKind: 8989e69450eSGeorge Rimar return Script<ELF32LE>::X->getOutputSectionSize(Name); 8999e69450eSGeorge Rimar case ELF32BEKind: 9009e69450eSGeorge Rimar return Script<ELF32BE>::X->getOutputSectionSize(Name); 9019e69450eSGeorge Rimar case ELF64LEKind: 9029e69450eSGeorge Rimar return Script<ELF64LE>::X->getOutputSectionSize(Name); 9039e69450eSGeorge Rimar case ELF64BEKind: 9049e69450eSGeorge Rimar return Script<ELF64BE>::X->getOutputSectionSize(Name); 9059e69450eSGeorge Rimar default: 9069e69450eSGeorge Rimar llvm_unreachable("unsupported target"); 9079e69450eSGeorge Rimar } 9089e69450eSGeorge Rimar return 0; 9099e69450eSGeorge Rimar } 9109e69450eSGeorge Rimar 91130835ea4SGeorge Rimar SymbolAssignment *ScriptParser::readAssignment(StringRef Name) { 91230835ea4SGeorge Rimar StringRef Op = next(); 91330835ea4SGeorge Rimar assert(Op == "=" || Op == "+="); 91430835ea4SGeorge Rimar Expr E = readExpr(); 91530835ea4SGeorge Rimar if (Op == "+=") 91630835ea4SGeorge Rimar E = [=](uint64_t Dot) { return getSymbolValue(Name, Dot) + E(Dot); }; 91730835ea4SGeorge Rimar auto *Cmd = new SymbolAssignment(Name, E); 91830835ea4SGeorge Rimar Opt.Commands.emplace_back(Cmd); 91930835ea4SGeorge Rimar return Cmd; 92030835ea4SGeorge Rimar } 92130835ea4SGeorge Rimar 92230835ea4SGeorge Rimar // This is an operator-precedence parser to parse a linker 92330835ea4SGeorge Rimar // script expression. 92430835ea4SGeorge Rimar Expr ScriptParser::readExpr() { return readExpr1(readPrimary(), 0); } 92530835ea4SGeorge Rimar 926708019c4SRui Ueyama // This is a part of the operator-precedence parser. This function 927708019c4SRui Ueyama // assumes that the remaining token stream starts with an operator. 928708019c4SRui Ueyama Expr ScriptParser::readExpr1(Expr Lhs, int MinPrec) { 929708019c4SRui Ueyama while (!atEOF() && !Error) { 930708019c4SRui Ueyama // Read an operator and an expression. 931708019c4SRui Ueyama StringRef Op1 = peek(); 932708019c4SRui Ueyama if (Op1 == "?") 933708019c4SRui Ueyama return readTernary(Lhs); 934708019c4SRui Ueyama if (precedence(Op1) < MinPrec) 935a31c91b1SEugene Leviant break; 936a31c91b1SEugene Leviant next(); 937708019c4SRui Ueyama Expr Rhs = readPrimary(); 938708019c4SRui Ueyama 939708019c4SRui Ueyama // Evaluate the remaining part of the expression first if the 940708019c4SRui Ueyama // next operator has greater precedence than the previous one. 941708019c4SRui Ueyama // For example, if we have read "+" and "3", and if the next 942708019c4SRui Ueyama // operator is "*", then we'll evaluate 3 * ... part first. 943708019c4SRui Ueyama while (!atEOF()) { 944708019c4SRui Ueyama StringRef Op2 = peek(); 945708019c4SRui Ueyama if (precedence(Op2) <= precedence(Op1)) 946eda81a1bSEugene Leviant break; 947708019c4SRui Ueyama Rhs = readExpr1(Rhs, precedence(Op2)); 948eda81a1bSEugene Leviant } 949708019c4SRui Ueyama 950708019c4SRui Ueyama Lhs = combine(Op1, Lhs, Rhs); 951708019c4SRui Ueyama } 952708019c4SRui Ueyama return Lhs; 953708019c4SRui Ueyama } 954708019c4SRui Ueyama 955708019c4SRui Ueyama uint64_t static getConstant(StringRef S) { 956708019c4SRui Ueyama if (S == "COMMONPAGESIZE" || S == "MAXPAGESIZE") 957708019c4SRui Ueyama return Target->PageSize; 958708019c4SRui Ueyama error("unknown constant: " + S); 959708019c4SRui Ueyama return 0; 960708019c4SRui Ueyama } 961708019c4SRui Ueyama 962708019c4SRui Ueyama Expr ScriptParser::readPrimary() { 963708019c4SRui Ueyama StringRef Tok = next(); 964708019c4SRui Ueyama 965708019c4SRui Ueyama if (Tok == "(") { 966708019c4SRui Ueyama Expr E = readExpr(); 967708019c4SRui Ueyama expect(")"); 968708019c4SRui Ueyama return E; 969708019c4SRui Ueyama } 970708019c4SRui Ueyama 971708019c4SRui Ueyama // Built-in functions are parsed here. 972708019c4SRui Ueyama // https://sourceware.org/binutils/docs/ld/Builtin-Functions.html. 973708019c4SRui Ueyama if (Tok == "ALIGN") { 974708019c4SRui Ueyama expect("("); 975708019c4SRui Ueyama Expr E = readExpr(); 976708019c4SRui Ueyama expect(")"); 977708019c4SRui Ueyama return [=](uint64_t Dot) { return alignTo(Dot, E(Dot)); }; 978708019c4SRui Ueyama } 979708019c4SRui Ueyama if (Tok == "CONSTANT") { 980708019c4SRui Ueyama expect("("); 981708019c4SRui Ueyama StringRef Tok = next(); 982708019c4SRui Ueyama expect(")"); 983708019c4SRui Ueyama return [=](uint64_t Dot) { return getConstant(Tok); }; 984708019c4SRui Ueyama } 98554c145ceSRafael Espindola if (Tok == "SEGMENT_START") { 98654c145ceSRafael Espindola expect("("); 98754c145ceSRafael Espindola next(); 98854c145ceSRafael Espindola expect(","); 98954c145ceSRafael Espindola uint64_t Val; 99054c145ceSRafael Espindola next().getAsInteger(0, Val); 99154c145ceSRafael Espindola expect(")"); 99254c145ceSRafael Espindola return [=](uint64_t Dot) { return Val; }; 99354c145ceSRafael Espindola } 994708019c4SRui Ueyama if (Tok == "DATA_SEGMENT_ALIGN") { 995708019c4SRui Ueyama expect("("); 996708019c4SRui Ueyama Expr E = readExpr(); 997708019c4SRui Ueyama expect(","); 998708019c4SRui Ueyama readExpr(); 999708019c4SRui Ueyama expect(")"); 1000f7791bb9SRui Ueyama return [=](uint64_t Dot) { return alignTo(Dot, E(Dot)); }; 1001708019c4SRui Ueyama } 1002708019c4SRui Ueyama if (Tok == "DATA_SEGMENT_END") { 1003708019c4SRui Ueyama expect("("); 1004708019c4SRui Ueyama expect("."); 1005708019c4SRui Ueyama expect(")"); 1006708019c4SRui Ueyama return [](uint64_t Dot) { return Dot; }; 1007708019c4SRui Ueyama } 1008276b4e64SGeorge Rimar // GNU linkers implements more complicated logic to handle 1009276b4e64SGeorge Rimar // DATA_SEGMENT_RELRO_END. We instead ignore the arguments and just align to 1010276b4e64SGeorge Rimar // the next page boundary for simplicity. 1011276b4e64SGeorge Rimar if (Tok == "DATA_SEGMENT_RELRO_END") { 1012276b4e64SGeorge Rimar expect("("); 1013276b4e64SGeorge Rimar next(); 1014276b4e64SGeorge Rimar expect(","); 1015276b4e64SGeorge Rimar readExpr(); 1016276b4e64SGeorge Rimar expect(")"); 1017276b4e64SGeorge Rimar return [](uint64_t Dot) { return alignTo(Dot, Target->PageSize); }; 1018276b4e64SGeorge Rimar } 10199e69450eSGeorge Rimar if (Tok == "SIZEOF") { 10209e69450eSGeorge Rimar expect("("); 10219e69450eSGeorge Rimar StringRef Name = next(); 10229e69450eSGeorge Rimar expect(")"); 10239e69450eSGeorge Rimar return [=](uint64_t Dot) { return getSectionSize(Name); }; 10249e69450eSGeorge Rimar } 1025708019c4SRui Ueyama 1026a9c5a528SGeorge Rimar // Parse a symbol name or a number literal. 1027708019c4SRui Ueyama uint64_t V = 0; 1028a9c5a528SGeorge Rimar if (Tok.getAsInteger(0, V)) { 102930835ea4SGeorge Rimar if (Tok != "." && !isValidCIdentifier(Tok)) 1030708019c4SRui Ueyama setError("malformed number: " + Tok); 103130835ea4SGeorge Rimar return [=](uint64_t Dot) { return getSymbolValue(Tok, Dot); }; 1032a9c5a528SGeorge Rimar } 1033708019c4SRui Ueyama return [=](uint64_t Dot) { return V; }; 1034708019c4SRui Ueyama } 1035708019c4SRui Ueyama 1036708019c4SRui Ueyama Expr ScriptParser::readTernary(Expr Cond) { 1037708019c4SRui Ueyama next(); 1038708019c4SRui Ueyama Expr L = readExpr(); 1039708019c4SRui Ueyama expect(":"); 1040708019c4SRui Ueyama Expr R = readExpr(); 1041708019c4SRui Ueyama return [=](uint64_t Dot) { return Cond(Dot) ? L(Dot) : R(Dot); }; 1042708019c4SRui Ueyama } 1043708019c4SRui Ueyama 1044708019c4SRui Ueyama Expr ScriptParser::combine(StringRef Op, Expr L, Expr R) { 1045708019c4SRui Ueyama if (Op == "*") 1046708019c4SRui Ueyama return [=](uint64_t Dot) { return L(Dot) * R(Dot); }; 1047708019c4SRui Ueyama if (Op == "/") { 1048708019c4SRui Ueyama return [=](uint64_t Dot) -> uint64_t { 1049708019c4SRui Ueyama uint64_t RHS = R(Dot); 1050708019c4SRui Ueyama if (RHS == 0) { 1051708019c4SRui Ueyama error("division by zero"); 1052708019c4SRui Ueyama return 0; 1053708019c4SRui Ueyama } 1054708019c4SRui Ueyama return L(Dot) / RHS; 1055708019c4SRui Ueyama }; 1056708019c4SRui Ueyama } 1057708019c4SRui Ueyama if (Op == "+") 1058708019c4SRui Ueyama return [=](uint64_t Dot) { return L(Dot) + R(Dot); }; 1059708019c4SRui Ueyama if (Op == "-") 1060708019c4SRui Ueyama return [=](uint64_t Dot) { return L(Dot) - R(Dot); }; 1061708019c4SRui Ueyama if (Op == "<") 1062708019c4SRui Ueyama return [=](uint64_t Dot) { return L(Dot) < R(Dot); }; 1063708019c4SRui Ueyama if (Op == ">") 1064708019c4SRui Ueyama return [=](uint64_t Dot) { return L(Dot) > R(Dot); }; 1065708019c4SRui Ueyama if (Op == ">=") 1066708019c4SRui Ueyama return [=](uint64_t Dot) { return L(Dot) >= R(Dot); }; 1067708019c4SRui Ueyama if (Op == "<=") 1068708019c4SRui Ueyama return [=](uint64_t Dot) { return L(Dot) <= R(Dot); }; 1069708019c4SRui Ueyama if (Op == "==") 1070708019c4SRui Ueyama return [=](uint64_t Dot) { return L(Dot) == R(Dot); }; 1071708019c4SRui Ueyama if (Op == "!=") 1072708019c4SRui Ueyama return [=](uint64_t Dot) { return L(Dot) != R(Dot); }; 1073708019c4SRui Ueyama if (Op == "&") 1074708019c4SRui Ueyama return [=](uint64_t Dot) { return L(Dot) & R(Dot); }; 1075708019c4SRui Ueyama llvm_unreachable("invalid operator"); 1076eda81a1bSEugene Leviant } 1077eda81a1bSEugene Leviant 1078bbe38602SEugene Leviant std::vector<StringRef> ScriptParser::readOutputSectionPhdrs() { 1079bbe38602SEugene Leviant std::vector<StringRef> Phdrs; 1080bbe38602SEugene Leviant while (!Error && peek().startswith(":")) { 1081bbe38602SEugene Leviant StringRef Tok = next(); 1082bbe38602SEugene Leviant Tok = (Tok.size() == 1) ? next() : Tok.substr(1); 1083bbe38602SEugene Leviant if (Tok.empty()) { 1084bbe38602SEugene Leviant setError("section header name is empty"); 1085bbe38602SEugene Leviant break; 1086bbe38602SEugene Leviant } 1087bbe38602SEugene Leviant Phdrs.push_back(Tok); 1088bbe38602SEugene Leviant } 1089bbe38602SEugene Leviant return Phdrs; 1090bbe38602SEugene Leviant } 1091bbe38602SEugene Leviant 1092bbe38602SEugene Leviant unsigned ScriptParser::readPhdrType() { 1093bbe38602SEugene Leviant StringRef Tok = next(); 1094b0f6c590SRui Ueyama unsigned Ret = StringSwitch<unsigned>(Tok) 1095b0f6c590SRui Ueyama .Case("PT_NULL", PT_NULL) 1096b0f6c590SRui Ueyama .Case("PT_LOAD", PT_LOAD) 1097b0f6c590SRui Ueyama .Case("PT_DYNAMIC", PT_DYNAMIC) 1098b0f6c590SRui Ueyama .Case("PT_INTERP", PT_INTERP) 1099b0f6c590SRui Ueyama .Case("PT_NOTE", PT_NOTE) 1100b0f6c590SRui Ueyama .Case("PT_SHLIB", PT_SHLIB) 1101b0f6c590SRui Ueyama .Case("PT_PHDR", PT_PHDR) 1102b0f6c590SRui Ueyama .Case("PT_TLS", PT_TLS) 1103b0f6c590SRui Ueyama .Case("PT_GNU_EH_FRAME", PT_GNU_EH_FRAME) 1104b0f6c590SRui Ueyama .Case("PT_GNU_STACK", PT_GNU_STACK) 1105b0f6c590SRui Ueyama .Case("PT_GNU_RELRO", PT_GNU_RELRO) 1106b0f6c590SRui Ueyama .Default(-1); 1107bbe38602SEugene Leviant 1108b0f6c590SRui Ueyama if (Ret == (unsigned)-1) { 1109b0f6c590SRui Ueyama setError("invalid program header type: " + Tok); 1110b0f6c590SRui Ueyama return PT_NULL; 1111b0f6c590SRui Ueyama } 1112b0f6c590SRui Ueyama return Ret; 1113bbe38602SEugene Leviant } 1114bbe38602SEugene Leviant 111516b0cc9eSSimon Atanasyan static bool isUnderSysroot(StringRef Path) { 111616b0cc9eSSimon Atanasyan if (Config->Sysroot == "") 111716b0cc9eSSimon Atanasyan return false; 111816b0cc9eSSimon Atanasyan for (; !Path.empty(); Path = sys::path::parent_path(Path)) 111916b0cc9eSSimon Atanasyan if (sys::fs::equivalent(Config->Sysroot, Path)) 112016b0cc9eSSimon Atanasyan return true; 112116b0cc9eSSimon Atanasyan return false; 112216b0cc9eSSimon Atanasyan } 112316b0cc9eSSimon Atanasyan 112407320e40SRui Ueyama // Entry point. 112507320e40SRui Ueyama void elf::readLinkerScript(MemoryBufferRef MB) { 112616b0cc9eSSimon Atanasyan StringRef Path = MB.getBufferIdentifier(); 112707320e40SRui Ueyama ScriptParser(MB.getBuffer(), isUnderSysroot(Path)).run(); 1128f7c5fbb1SRui Ueyama } 11291ebc8ed7SRui Ueyama 113007320e40SRui Ueyama template class elf::LinkerScript<ELF32LE>; 113107320e40SRui Ueyama template class elf::LinkerScript<ELF32BE>; 113207320e40SRui Ueyama template class elf::LinkerScript<ELF64LE>; 113307320e40SRui Ueyama template class elf::LinkerScript<ELF64BE>; 1134