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 70eea3114fSGeorge Rimar static bool match(StringRef Pattern, ArrayRef<StringRef> Arr) { 71eea3114fSGeorge Rimar for (StringRef S : Arr) 72eea3114fSGeorge Rimar if (globMatch(S, Pattern)) 73eea3114fSGeorge Rimar return true; 74eea3114fSGeorge Rimar return false; 75eea3114fSGeorge Rimar } 76eea3114fSGeorge Rimar 77652852c5SGeorge Rimar template <class ELFT> 78a7f7884dSRui Ueyama std::vector<OutputSectionBase<ELFT> *> 79e63d81bdSEugene Leviant LinkerScript<ELFT>::createSections(OutputSectionFactory<ELFT> &Factory) { 80eea3114fSGeorge Rimar typedef const std::unique_ptr<ObjectFile<ELFT>> ObjectFile; 81a7f7884dSRui Ueyama std::vector<OutputSectionBase<ELFT> *> Result; 828a9bb7baSRui Ueyama DenseSet<OutputSectionBase<ELFT> *> Removed; 83a7f7884dSRui Ueyama 84e63d81bdSEugene Leviant // Add input section to output section. If there is no output section yet, 85e63d81bdSEugene Leviant // then create it and add to output section list. 868a9bb7baSRui Ueyama auto AddInputSec = [&](InputSectionBase<ELFT> *C, StringRef Name, 878a9bb7baSRui Ueyama ConstraintKind Constraint) { 88e63d81bdSEugene Leviant OutputSectionBase<ELFT> *Sec; 89e63d81bdSEugene Leviant bool IsNew; 90e63d81bdSEugene Leviant std::tie(Sec, IsNew) = Factory.create(C, Name); 91e63d81bdSEugene Leviant if (IsNew) 92a7f7884dSRui Ueyama Result.push_back(Sec); 938a9bb7baSRui Ueyama if ((!(C->getSectionHdr()->sh_flags & SHF_WRITE)) && 948a9bb7baSRui Ueyama Constraint == ReadWrite) { 958a9bb7baSRui Ueyama Removed.insert(Sec); 968a9bb7baSRui Ueyama return; 978a9bb7baSRui Ueyama } 988a9bb7baSRui Ueyama if ((C->getSectionHdr()->sh_flags & SHF_WRITE) && Constraint == ReadOnly) { 998a9bb7baSRui Ueyama Removed.insert(Sec); 1008a9bb7baSRui Ueyama return; 1018a9bb7baSRui Ueyama } 102e63d81bdSEugene Leviant Sec->addSection(C); 103e63d81bdSEugene Leviant }; 104e63d81bdSEugene Leviant 105e63d81bdSEugene Leviant // Select input sections matching rule and add them to corresponding 106e63d81bdSEugene Leviant // output section. Section rules are processed in order they're listed 107e63d81bdSEugene Leviant // in script, so correct input section order is maintained by design. 108eea3114fSGeorge Rimar for (const std::unique_ptr<BaseCommand> &Base : Opt.Commands) { 109eea3114fSGeorge Rimar auto *OutCmd = dyn_cast<OutputSectionCommand>(Base.get()); 110eea3114fSGeorge Rimar if (!OutCmd) 111eea3114fSGeorge Rimar continue; 112eea3114fSGeorge Rimar 113eea3114fSGeorge Rimar for (const std::unique_ptr<BaseCommand> &Cmd : OutCmd->Commands) { 114eea3114fSGeorge Rimar auto *InCmd = dyn_cast<InputSectionDescription>(Cmd.get()); 115eea3114fSGeorge Rimar if (!InCmd) 116eea3114fSGeorge Rimar continue; 117eea3114fSGeorge Rimar 118eea3114fSGeorge Rimar for (ObjectFile &F : Symtab<ELFT>::X->getObjectFiles()) { 119eea3114fSGeorge Rimar for (InputSectionBase<ELFT> *S : F->getSections()) { 120eea3114fSGeorge Rimar if (isDiscarded(S) || S->OutSec) 121eea3114fSGeorge Rimar continue; 122eea3114fSGeorge Rimar 123eea3114fSGeorge Rimar if (match(S->getSectionName(), InCmd->Patterns)) { 124eea3114fSGeorge Rimar if (OutCmd->Name == "/DISCARD/") 125eea3114fSGeorge Rimar S->Live = false; 126eea3114fSGeorge Rimar else 1278a9bb7baSRui Ueyama AddInputSec(S, OutCmd->Name, OutCmd->Constraint); 128eea3114fSGeorge Rimar } 129eea3114fSGeorge Rimar } 130eea3114fSGeorge Rimar } 131eea3114fSGeorge Rimar } 132eea3114fSGeorge Rimar } 133e63d81bdSEugene Leviant 134e63d81bdSEugene Leviant // Add all other input sections, which are not listed in script. 135eea3114fSGeorge Rimar for (ObjectFile &F : Symtab<ELFT>::X->getObjectFiles()) 1363c944ec8SReid Kleckner for (InputSectionBase<ELFT> *S : F->getSections()) { 137e63d81bdSEugene Leviant if (!isDiscarded(S)) { 138e63d81bdSEugene Leviant if (!S->OutSec) 1398a9bb7baSRui Ueyama AddInputSec(S, getOutputSectionName(S), NoConstraint); 140e63d81bdSEugene Leviant } else 141e63d81bdSEugene Leviant reportDiscarded(S, F); 1423c944ec8SReid Kleckner } 143e63d81bdSEugene Leviant 1448a9bb7baSRui Ueyama // Remove from the output all the sections which did not met the constraints. 1458a9bb7baSRui Ueyama Result.erase(std::remove_if(Result.begin(), Result.end(), 1468a9bb7baSRui Ueyama [&](OutputSectionBase<ELFT> *Sec) { 1478a9bb7baSRui Ueyama return Removed.count(Sec); 1488a9bb7baSRui Ueyama }), 1498a9bb7baSRui Ueyama Result.end()); 1508a9bb7baSRui Ueyama return Result; 151e63d81bdSEugene Leviant } 152e63d81bdSEugene Leviant 153e63d81bdSEugene Leviant template <class ELFT> 15410e576e1SGeorge Rimar void LinkerScript<ELFT>::dispatchAssignment(SymbolAssignment *Cmd) { 155708019c4SRui Ueyama uint64_t Val = Cmd->Expression(Dot); 15610e576e1SGeorge Rimar if (Cmd->Name == ".") { 15710e576e1SGeorge Rimar Dot = Val; 158a31c91b1SEugene Leviant } else if (!Cmd->Ignore) { 15910e576e1SGeorge Rimar auto *D = cast<DefinedRegular<ELFT>>(Symtab<ELFT>::X->find(Cmd->Name)); 16010e576e1SGeorge Rimar D->Value = Val; 16110e576e1SGeorge Rimar } 16210e576e1SGeorge Rimar } 16310e576e1SGeorge Rimar 16410e576e1SGeorge Rimar template <class ELFT> 16507320e40SRui Ueyama void LinkerScript<ELFT>::assignAddresses( 166dbbd8b15SGeorge Rimar ArrayRef<OutputSectionBase<ELFT> *> Sections) { 167652852c5SGeorge Rimar // Orphan sections are sections present in the input files which 1687c18c28cSRui Ueyama // are not explicitly placed into the output file by the linker script. 1697c18c28cSRui Ueyama // We place orphan sections at end of file. 1707c18c28cSRui Ueyama // Other linkers places them using some heuristics as described in 171652852c5SGeorge Rimar // https://sourceware.org/binutils/docs/ld/Orphan-Sections.html#Orphan-Sections. 1727c18c28cSRui Ueyama for (OutputSectionBase<ELFT> *Sec : Sections) { 173652852c5SGeorge Rimar StringRef Name = Sec->getName(); 174c3e2a4b0SRui Ueyama if (getSectionIndex(Name) == INT_MAX) 175076fe157SGeorge Rimar Opt.Commands.push_back(llvm::make_unique<OutputSectionCommand>(Name)); 176652852c5SGeorge Rimar } 177652852c5SGeorge Rimar 1787c18c28cSRui Ueyama // Assign addresses as instructed by linker script SECTIONS sub-commands. 179c998a8c0SRui Ueyama Dot = Out<ELFT>::ElfHeader->getSize() + Out<ELFT>::ProgramHeaders->getSize(); 180467c4d55SEugene Leviant uintX_t MinVA = std::numeric_limits<uintX_t>::max(); 181652852c5SGeorge Rimar uintX_t ThreadBssOffset = 0; 182652852c5SGeorge Rimar 183076fe157SGeorge Rimar for (const std::unique_ptr<BaseCommand> &Base : Opt.Commands) { 184076fe157SGeorge Rimar if (auto *Cmd = dyn_cast<SymbolAssignment>(Base.get())) { 18510e576e1SGeorge Rimar dispatchAssignment(Cmd); 18605ef4cffSRui Ueyama continue; 187652852c5SGeorge Rimar } 188652852c5SGeorge Rimar 189fb8978fcSDima Stepanov // Find all the sections with required name. There can be more than 1906ad330acSGeorge Rimar // one section with such name, if the alignment, flags or type 191fb8978fcSDima Stepanov // attribute differs. 192076fe157SGeorge Rimar auto *Cmd = cast<OutputSectionCommand>(Base.get()); 193fb8978fcSDima Stepanov for (OutputSectionBase<ELFT> *Sec : Sections) { 194076fe157SGeorge Rimar if (Sec->getName() != Cmd->Name) 195652852c5SGeorge Rimar continue; 196652852c5SGeorge Rimar 197652852c5SGeorge Rimar if ((Sec->getFlags() & SHF_TLS) && Sec->getType() == SHT_NOBITS) { 198c998a8c0SRui Ueyama uintX_t TVA = Dot + ThreadBssOffset; 199424b4081SRui Ueyama TVA = alignTo(TVA, Sec->getAlignment()); 200652852c5SGeorge Rimar Sec->setVA(TVA); 201c998a8c0SRui Ueyama ThreadBssOffset = TVA - Dot + Sec->getSize(); 202652852c5SGeorge Rimar continue; 203652852c5SGeorge Rimar } 204652852c5SGeorge Rimar 205652852c5SGeorge Rimar if (Sec->getFlags() & SHF_ALLOC) { 206424b4081SRui Ueyama Dot = alignTo(Dot, Sec->getAlignment()); 207c998a8c0SRui Ueyama Sec->setVA(Dot); 208467c4d55SEugene Leviant MinVA = std::min(MinVA, Dot); 209c998a8c0SRui Ueyama Dot += Sec->getSize(); 210652852c5SGeorge Rimar continue; 211652852c5SGeorge Rimar } 212652852c5SGeorge Rimar } 213652852c5SGeorge Rimar } 214467c4d55SEugene Leviant 21564c32d6fSRafael Espindola // ELF and Program headers need to be right before the first section in 216b91e7118SGeorge Rimar // memory. Set their addresses accordingly. 217467c4d55SEugene Leviant MinVA = alignDown(MinVA - Out<ELFT>::ElfHeader->getSize() - 218467c4d55SEugene Leviant Out<ELFT>::ProgramHeaders->getSize(), 219467c4d55SEugene Leviant Target->PageSize); 220467c4d55SEugene Leviant Out<ELFT>::ElfHeader->setVA(MinVA); 221467c4d55SEugene Leviant Out<ELFT>::ProgramHeaders->setVA(Out<ELFT>::ElfHeader->getSize() + MinVA); 222fb8978fcSDima Stepanov } 223652852c5SGeorge Rimar 22407320e40SRui Ueyama template <class ELFT> 22574df5c7eSRafael Espindola std::vector<PhdrEntry<ELFT>> 226bbe38602SEugene Leviant LinkerScript<ELFT>::createPhdrs(ArrayRef<OutputSectionBase<ELFT> *> Sections) { 227*edebbdf1SRui Ueyama std::vector<PhdrEntry<ELFT>> Ret; 228*edebbdf1SRui Ueyama PhdrEntry<ELFT> *TlsPhdr = nullptr; 229*edebbdf1SRui Ueyama PhdrEntry<ELFT> *NotePhdr = nullptr; 230*edebbdf1SRui Ueyama PhdrEntry<ELFT> *RelroPhdr = nullptr; 231bbe38602SEugene Leviant 232bbe38602SEugene Leviant for (const PhdrsCommand &Cmd : Opt.PhdrsCommands) { 233*edebbdf1SRui Ueyama Ret.emplace_back(Cmd.Type, Cmd.Flags == UINT_MAX ? PF_R : Cmd.Flags); 234*edebbdf1SRui Ueyama PhdrEntry<ELFT> &Phdr = Ret.back(); 235bbe38602SEugene Leviant 236bbe38602SEugene Leviant if (Cmd.HasFilehdr) 237adca245fSRui Ueyama Phdr.add(Out<ELFT>::ElfHeader); 238bbe38602SEugene Leviant if (Cmd.HasPhdrs) 239adca245fSRui Ueyama Phdr.add(Out<ELFT>::ProgramHeaders); 240bbe38602SEugene Leviant 241bbe38602SEugene Leviant switch (Cmd.Type) { 242bbe38602SEugene Leviant case PT_INTERP: 243fd03cfd2SRui Ueyama if (Out<ELFT>::Interp) 244adca245fSRui Ueyama Phdr.add(Out<ELFT>::Interp); 245bbe38602SEugene Leviant break; 246bbe38602SEugene Leviant case PT_DYNAMIC: 247bbe38602SEugene Leviant if (isOutputDynamic<ELFT>()) { 248adca245fSRui Ueyama Phdr.H.p_flags = toPhdrFlags(Out<ELFT>::Dynamic->getFlags()); 249adca245fSRui Ueyama Phdr.add(Out<ELFT>::Dynamic); 250bbe38602SEugene Leviant } 251bbe38602SEugene Leviant break; 252bbe38602SEugene Leviant case PT_TLS: 253*edebbdf1SRui Ueyama TlsPhdr = &Phdr; 254bbe38602SEugene Leviant break; 255bbe38602SEugene Leviant case PT_NOTE: 256*edebbdf1SRui Ueyama NotePhdr = &Phdr; 257bbe38602SEugene Leviant break; 258bbe38602SEugene Leviant case PT_GNU_RELRO: 259*edebbdf1SRui Ueyama RelroPhdr = &Phdr; 260bbe38602SEugene Leviant break; 261bbe38602SEugene Leviant case PT_GNU_EH_FRAME: 262bbe38602SEugene Leviant if (!Out<ELFT>::EhFrame->empty() && Out<ELFT>::EhFrameHdr) { 263adca245fSRui Ueyama Phdr.H.p_flags = toPhdrFlags(Out<ELFT>::EhFrameHdr->getFlags()); 264adca245fSRui Ueyama Phdr.add(Out<ELFT>::EhFrameHdr); 265bbe38602SEugene Leviant } 266bbe38602SEugene Leviant break; 267bbe38602SEugene Leviant } 268bbe38602SEugene Leviant } 269bbe38602SEugene Leviant 270*edebbdf1SRui Ueyama PhdrEntry<ELFT> *Load = nullptr; 271*edebbdf1SRui Ueyama uintX_t Flags = PF_R; 272bbe38602SEugene Leviant for (OutputSectionBase<ELFT> *Sec : Sections) { 273bbe38602SEugene Leviant if (!(Sec->getFlags() & SHF_ALLOC)) 274bbe38602SEugene Leviant break; 275bbe38602SEugene Leviant 276*edebbdf1SRui Ueyama if (TlsPhdr && (Sec->getFlags() & SHF_TLS)) 277*edebbdf1SRui Ueyama TlsPhdr->add(Sec); 278bbe38602SEugene Leviant 279bbe38602SEugene Leviant if (!needsPtLoad<ELFT>(Sec)) 280bbe38602SEugene Leviant continue; 281bbe38602SEugene Leviant 282*edebbdf1SRui Ueyama std::vector<size_t> PhdrIds = getPhdrIndices(Sec->getName()); 283bbe38602SEugene Leviant if (!PhdrIds.empty()) { 284bbe38602SEugene Leviant // Assign headers specified by linker script 285bbe38602SEugene Leviant for (size_t Id : PhdrIds) { 286*edebbdf1SRui Ueyama Ret[Id].add(Sec); 287865bf863SEugene Leviant if (Opt.PhdrsCommands[Id].Flags == UINT_MAX) 288*edebbdf1SRui Ueyama Ret[Id].H.p_flags |= toPhdrFlags(Sec->getFlags()); 289bbe38602SEugene Leviant } 290bbe38602SEugene Leviant } else { 291bbe38602SEugene Leviant // If we have no load segment or flags've changed then we want new load 292bbe38602SEugene Leviant // segment. 293bbe38602SEugene Leviant uintX_t NewFlags = toPhdrFlags(Sec->getFlags()); 294bbe38602SEugene Leviant if (Load == nullptr || Flags != NewFlags) { 295*edebbdf1SRui Ueyama Load = &*Ret.emplace(Ret.end(), PT_LOAD, NewFlags); 296bbe38602SEugene Leviant Flags = NewFlags; 297bbe38602SEugene Leviant } 29818f084ffSRui Ueyama Load->add(Sec); 299bbe38602SEugene Leviant } 300bbe38602SEugene Leviant 301*edebbdf1SRui Ueyama if (RelroPhdr && isRelroSection(Sec)) 302*edebbdf1SRui Ueyama RelroPhdr->add(Sec); 303*edebbdf1SRui Ueyama if (NotePhdr && Sec->getType() == SHT_NOTE) 304*edebbdf1SRui Ueyama NotePhdr->add(Sec); 305bbe38602SEugene Leviant } 306*edebbdf1SRui Ueyama return Ret; 307bbe38602SEugene Leviant } 308bbe38602SEugene Leviant 309bbe38602SEugene Leviant template <class ELFT> 31007320e40SRui Ueyama ArrayRef<uint8_t> LinkerScript<ELFT>::getFiller(StringRef Name) { 311f6c3ccefSGeorge Rimar for (const std::unique_ptr<BaseCommand> &Base : Opt.Commands) 312f6c3ccefSGeorge Rimar if (auto *Cmd = dyn_cast<OutputSectionCommand>(Base.get())) 313f6c3ccefSGeorge Rimar if (Cmd->Name == Name) 314f6c3ccefSGeorge Rimar return Cmd->Filler; 315e2ee72b5SGeorge Rimar return {}; 316e2ee72b5SGeorge Rimar } 317e2ee72b5SGeorge Rimar 318c3e2a4b0SRui Ueyama // Returns the index of the given section name in linker script 319c3e2a4b0SRui Ueyama // SECTIONS commands. Sections are laid out as the same order as they 320c3e2a4b0SRui Ueyama // were in the script. If a given name did not appear in the script, 321c3e2a4b0SRui Ueyama // it returns INT_MAX, so that it will be laid out at end of file. 322076fe157SGeorge Rimar template <class ELFT> int LinkerScript<ELFT>::getSectionIndex(StringRef Name) { 32371b26e94SGeorge Rimar auto Begin = Opt.Commands.begin(); 32471b26e94SGeorge Rimar auto End = Opt.Commands.end(); 325076fe157SGeorge Rimar auto I = 326076fe157SGeorge Rimar std::find_if(Begin, End, [&](const std::unique_ptr<BaseCommand> &Base) { 327076fe157SGeorge Rimar if (auto *Cmd = dyn_cast<OutputSectionCommand>(Base.get())) 328076fe157SGeorge Rimar if (Cmd->Name == Name) 329076fe157SGeorge Rimar return true; 330076fe157SGeorge Rimar return false; 33171b26e94SGeorge Rimar }); 332c3e2a4b0SRui Ueyama return I == End ? INT_MAX : (I - Begin); 33371b26e94SGeorge Rimar } 33471b26e94SGeorge Rimar 33571b26e94SGeorge Rimar // A compartor to sort output sections. Returns -1 or 1 if 33671b26e94SGeorge Rimar // A or B are mentioned in linker script. Otherwise, returns 0. 33707320e40SRui Ueyama template <class ELFT> 33807320e40SRui Ueyama int LinkerScript<ELFT>::compareSections(StringRef A, StringRef B) { 339c3e2a4b0SRui Ueyama int I = getSectionIndex(A); 340c3e2a4b0SRui Ueyama int J = getSectionIndex(B); 341c3e2a4b0SRui Ueyama if (I == INT_MAX && J == INT_MAX) 342717677afSRui Ueyama return 0; 343717677afSRui Ueyama return I < J ? -1 : 1; 344717677afSRui Ueyama } 345717677afSRui Ueyama 346076fe157SGeorge Rimar template <class ELFT> void LinkerScript<ELFT>::addScriptedSymbols() { 347a31c91b1SEugene Leviant for (const std::unique_ptr<BaseCommand> &Base : Opt.Commands) { 348a31c91b1SEugene Leviant auto *Cmd = dyn_cast<SymbolAssignment>(Base.get()); 349a31c91b1SEugene Leviant if (!Cmd || Cmd->Name == ".") 350a31c91b1SEugene Leviant continue; 351a31c91b1SEugene Leviant 3528ab4108dSDavide Italiano SymbolBody *B = Symtab<ELFT>::X->find(Cmd->Name); 3538ab4108dSDavide Italiano if (!B || B->isUndefined()) 354a31c91b1SEugene Leviant Symtab<ELFT>::X->addAbsolute(Cmd->Name, 355a31c91b1SEugene Leviant Cmd->Hidden ? STV_HIDDEN : STV_DEFAULT); 356a31c91b1SEugene Leviant else 357a31c91b1SEugene Leviant // Symbol already exists in symbol table. If it is provided 358a31c91b1SEugene Leviant // then we can't override its value. 359a31c91b1SEugene Leviant Cmd->Ignore = Cmd->Provide; 360a31c91b1SEugene Leviant } 361eda81a1bSEugene Leviant } 362eda81a1bSEugene Leviant 363bbe38602SEugene Leviant template <class ELFT> bool LinkerScript<ELFT>::hasPhdrsCommands() { 364bbe38602SEugene Leviant return !Opt.PhdrsCommands.empty(); 365bbe38602SEugene Leviant } 366bbe38602SEugene Leviant 367bbe38602SEugene Leviant // Returns indices of ELF headers containing specific section, identified 368bbe38602SEugene Leviant // by Name. Each index is a zero based number of ELF header listed within 369bbe38602SEugene Leviant // PHDRS {} script block. 370bbe38602SEugene Leviant template <class ELFT> 371*edebbdf1SRui Ueyama std::vector<size_t> LinkerScript<ELFT>::getPhdrIndices(StringRef SectionName) { 372076fe157SGeorge Rimar for (const std::unique_ptr<BaseCommand> &Base : Opt.Commands) { 373076fe157SGeorge Rimar auto *Cmd = dyn_cast<OutputSectionCommand>(Base.get()); 374*edebbdf1SRui Ueyama if (!Cmd || Cmd->Name != SectionName) 37531d842f5SGeorge Rimar continue; 37631d842f5SGeorge Rimar 377bbe38602SEugene Leviant std::vector<size_t> Indices; 378076fe157SGeorge Rimar for (StringRef PhdrName : Cmd->Phdrs) { 37931d842f5SGeorge Rimar auto ItPhdr = 38031d842f5SGeorge Rimar std::find_if(Opt.PhdrsCommands.rbegin(), Opt.PhdrsCommands.rend(), 381076fe157SGeorge Rimar [&](PhdrsCommand &P) { return P.Name == PhdrName; }); 382bbe38602SEugene Leviant if (ItPhdr == Opt.PhdrsCommands.rend()) 383bbe38602SEugene Leviant error("section header '" + PhdrName + "' is not listed in PHDRS"); 384bbe38602SEugene Leviant else 385bbe38602SEugene Leviant Indices.push_back(std::distance(ItPhdr, Opt.PhdrsCommands.rend()) - 1); 386bbe38602SEugene Leviant } 387bbe38602SEugene Leviant return Indices; 388bbe38602SEugene Leviant } 38931d842f5SGeorge Rimar return {}; 39031d842f5SGeorge Rimar } 391bbe38602SEugene Leviant 39207320e40SRui Ueyama class elf::ScriptParser : public ScriptParserBase { 393c3794e58SGeorge Rimar typedef void (ScriptParser::*Handler)(); 394c3794e58SGeorge Rimar 395f7c5fbb1SRui Ueyama public: 39607320e40SRui Ueyama ScriptParser(StringRef S, bool B) : ScriptParserBase(S), IsUnderSysroot(B) {} 397f23b2320SGeorge Rimar 3984a46539cSRui Ueyama void run(); 399f7c5fbb1SRui Ueyama 400f7c5fbb1SRui Ueyama private: 40152a1509eSRui Ueyama void addFile(StringRef Path); 40252a1509eSRui Ueyama 403f7c5fbb1SRui Ueyama void readAsNeeded(); 40490c5099eSDenis Protivensky void readEntry(); 40583f406cfSGeorge Rimar void readExtern(); 406f7c5fbb1SRui Ueyama void readGroup(); 40731aa1f83SRui Ueyama void readInclude(); 408c3794e58SGeorge Rimar void readNothing() {} 409ee59282bSRui Ueyama void readOutput(); 4109159ce93SDavide Italiano void readOutputArch(); 411f7c5fbb1SRui Ueyama void readOutputFormat(); 412bbe38602SEugene Leviant void readPhdrs(); 41368a39a65SDavide Italiano void readSearchDir(); 4148e3b38abSDenis Protivensky void readSections(); 4158e3b38abSDenis Protivensky 416113cdec9SRui Ueyama SymbolAssignment *readAssignment(StringRef Name); 417eda81a1bSEugene Leviant void readOutputSectionDescription(StringRef OutSec); 418bbe38602SEugene Leviant std::vector<StringRef> readOutputSectionPhdrs(); 419bbe38602SEugene Leviant unsigned readPhdrType(); 420a31c91b1SEugene Leviant void readProvide(bool Hidden); 421708019c4SRui Ueyama 422708019c4SRui Ueyama Expr readExpr(); 423708019c4SRui Ueyama Expr readExpr1(Expr Lhs, int MinPrec); 424708019c4SRui Ueyama Expr readPrimary(); 425708019c4SRui Ueyama Expr readTernary(Expr Cond); 426708019c4SRui Ueyama Expr combine(StringRef Op, Expr Lhs, Expr Rhs); 427f7c5fbb1SRui Ueyama 428c3794e58SGeorge Rimar const static StringMap<Handler> Cmd; 42907320e40SRui Ueyama ScriptConfiguration &Opt = *ScriptConfig; 43007320e40SRui Ueyama StringSaver Saver = {ScriptConfig->Alloc}; 43116b0cc9eSSimon Atanasyan bool IsUnderSysroot; 432f7c5fbb1SRui Ueyama }; 433f7c5fbb1SRui Ueyama 434e0df00b9SRafael Espindola const StringMap<elf::ScriptParser::Handler> elf::ScriptParser::Cmd = { 435c3794e58SGeorge Rimar {"ENTRY", &ScriptParser::readEntry}, 436c3794e58SGeorge Rimar {"EXTERN", &ScriptParser::readExtern}, 437c3794e58SGeorge Rimar {"GROUP", &ScriptParser::readGroup}, 438c3794e58SGeorge Rimar {"INCLUDE", &ScriptParser::readInclude}, 439c3794e58SGeorge Rimar {"INPUT", &ScriptParser::readGroup}, 440c3794e58SGeorge Rimar {"OUTPUT", &ScriptParser::readOutput}, 441c3794e58SGeorge Rimar {"OUTPUT_ARCH", &ScriptParser::readOutputArch}, 442c3794e58SGeorge Rimar {"OUTPUT_FORMAT", &ScriptParser::readOutputFormat}, 443bbe38602SEugene Leviant {"PHDRS", &ScriptParser::readPhdrs}, 444c3794e58SGeorge Rimar {"SEARCH_DIR", &ScriptParser::readSearchDir}, 445c3794e58SGeorge Rimar {"SECTIONS", &ScriptParser::readSections}, 446c3794e58SGeorge Rimar {";", &ScriptParser::readNothing}}; 447c3794e58SGeorge Rimar 448717677afSRui Ueyama void ScriptParser::run() { 449f7c5fbb1SRui Ueyama while (!atEOF()) { 450f7c5fbb1SRui Ueyama StringRef Tok = next(); 451c3794e58SGeorge Rimar if (Handler Fn = Cmd.lookup(Tok)) 452c3794e58SGeorge Rimar (this->*Fn)(); 453c3794e58SGeorge Rimar else 4545761042dSGeorge Rimar setError("unknown directive: " + Tok); 455f7c5fbb1SRui Ueyama } 456f7c5fbb1SRui Ueyama } 457f7c5fbb1SRui Ueyama 458717677afSRui Ueyama void ScriptParser::addFile(StringRef S) { 45916b0cc9eSSimon Atanasyan if (IsUnderSysroot && S.startswith("/")) { 46016b0cc9eSSimon Atanasyan SmallString<128> Path; 46116b0cc9eSSimon Atanasyan (Config->Sysroot + S).toStringRef(Path); 46216b0cc9eSSimon Atanasyan if (sys::fs::exists(Path)) { 46316b0cc9eSSimon Atanasyan Driver->addFile(Saver.save(Path.str())); 46416b0cc9eSSimon Atanasyan return; 46516b0cc9eSSimon Atanasyan } 46616b0cc9eSSimon Atanasyan } 46716b0cc9eSSimon Atanasyan 468f03f3cc1SRui Ueyama if (sys::path::is_absolute(S)) { 46952a1509eSRui Ueyama Driver->addFile(S); 47052a1509eSRui Ueyama } else if (S.startswith("=")) { 47152a1509eSRui Ueyama if (Config->Sysroot.empty()) 47252a1509eSRui Ueyama Driver->addFile(S.substr(1)); 47352a1509eSRui Ueyama else 47452a1509eSRui Ueyama Driver->addFile(Saver.save(Config->Sysroot + "/" + S.substr(1))); 47552a1509eSRui Ueyama } else if (S.startswith("-l")) { 47621eecb4fSRui Ueyama Driver->addLibrary(S.substr(2)); 477a1b8fc3bSSimon Atanasyan } else if (sys::fs::exists(S)) { 478a1b8fc3bSSimon Atanasyan Driver->addFile(S); 47952a1509eSRui Ueyama } else { 48052a1509eSRui Ueyama std::string Path = findFromSearchPaths(S); 48152a1509eSRui Ueyama if (Path.empty()) 482777f9630SGeorge Rimar setError("unable to find " + S); 483025d59b1SRui Ueyama else 48452a1509eSRui Ueyama Driver->addFile(Saver.save(Path)); 48552a1509eSRui Ueyama } 48652a1509eSRui Ueyama } 48752a1509eSRui Ueyama 488717677afSRui Ueyama void ScriptParser::readAsNeeded() { 489f7c5fbb1SRui Ueyama expect("("); 49035da9b6eSRui Ueyama bool Orig = Config->AsNeeded; 49135da9b6eSRui Ueyama Config->AsNeeded = true; 492025d59b1SRui Ueyama while (!Error) { 493f7c5fbb1SRui Ueyama StringRef Tok = next(); 494f7c5fbb1SRui Ueyama if (Tok == ")") 49535da9b6eSRui Ueyama break; 49652a1509eSRui Ueyama addFile(Tok); 497f7c5fbb1SRui Ueyama } 49835da9b6eSRui Ueyama Config->AsNeeded = Orig; 499f7c5fbb1SRui Ueyama } 500f7c5fbb1SRui Ueyama 501717677afSRui Ueyama void ScriptParser::readEntry() { 50290c5099eSDenis Protivensky // -e <symbol> takes predecence over ENTRY(<symbol>). 50390c5099eSDenis Protivensky expect("("); 50490c5099eSDenis Protivensky StringRef Tok = next(); 50590c5099eSDenis Protivensky if (Config->Entry.empty()) 50690c5099eSDenis Protivensky Config->Entry = Tok; 50790c5099eSDenis Protivensky expect(")"); 50890c5099eSDenis Protivensky } 50990c5099eSDenis Protivensky 510717677afSRui Ueyama void ScriptParser::readExtern() { 51183f406cfSGeorge Rimar expect("("); 512025d59b1SRui Ueyama while (!Error) { 51383f406cfSGeorge Rimar StringRef Tok = next(); 51483f406cfSGeorge Rimar if (Tok == ")") 51583f406cfSGeorge Rimar return; 51683f406cfSGeorge Rimar Config->Undefined.push_back(Tok); 51783f406cfSGeorge Rimar } 51883f406cfSGeorge Rimar } 51983f406cfSGeorge Rimar 520717677afSRui Ueyama void ScriptParser::readGroup() { 521f7c5fbb1SRui Ueyama expect("("); 522025d59b1SRui Ueyama while (!Error) { 523f7c5fbb1SRui Ueyama StringRef Tok = next(); 524f7c5fbb1SRui Ueyama if (Tok == ")") 525f7c5fbb1SRui Ueyama return; 526f7c5fbb1SRui Ueyama if (Tok == "AS_NEEDED") { 527f7c5fbb1SRui Ueyama readAsNeeded(); 528f7c5fbb1SRui Ueyama continue; 529f7c5fbb1SRui Ueyama } 53052a1509eSRui Ueyama addFile(Tok); 531f7c5fbb1SRui Ueyama } 532f7c5fbb1SRui Ueyama } 533f7c5fbb1SRui Ueyama 534717677afSRui Ueyama void ScriptParser::readInclude() { 53531aa1f83SRui Ueyama StringRef Tok = next(); 53631aa1f83SRui Ueyama auto MBOrErr = MemoryBuffer::getFile(Tok); 537025d59b1SRui Ueyama if (!MBOrErr) { 5385761042dSGeorge Rimar setError("cannot open " + Tok); 539025d59b1SRui Ueyama return; 540025d59b1SRui Ueyama } 54131aa1f83SRui Ueyama std::unique_ptr<MemoryBuffer> &MB = *MBOrErr; 542a47ee68dSRui Ueyama StringRef S = Saver.save(MB->getMemBufferRef().getBuffer()); 543a47ee68dSRui Ueyama std::vector<StringRef> V = tokenize(S); 54431aa1f83SRui Ueyama Tokens.insert(Tokens.begin() + Pos, V.begin(), V.end()); 54531aa1f83SRui Ueyama } 54631aa1f83SRui Ueyama 547717677afSRui Ueyama void ScriptParser::readOutput() { 548ee59282bSRui Ueyama // -o <file> takes predecence over OUTPUT(<file>). 549ee59282bSRui Ueyama expect("("); 550ee59282bSRui Ueyama StringRef Tok = next(); 551ee59282bSRui Ueyama if (Config->OutputFile.empty()) 552ee59282bSRui Ueyama Config->OutputFile = Tok; 553ee59282bSRui Ueyama expect(")"); 554ee59282bSRui Ueyama } 555ee59282bSRui Ueyama 556717677afSRui Ueyama void ScriptParser::readOutputArch() { 5579159ce93SDavide Italiano // Error checking only for now. 5589159ce93SDavide Italiano expect("("); 5599159ce93SDavide Italiano next(); 5609159ce93SDavide Italiano expect(")"); 5619159ce93SDavide Italiano } 5629159ce93SDavide Italiano 563717677afSRui Ueyama void ScriptParser::readOutputFormat() { 564f7c5fbb1SRui Ueyama // Error checking only for now. 565f7c5fbb1SRui Ueyama expect("("); 566f7c5fbb1SRui Ueyama next(); 5676836c618SDavide Italiano StringRef Tok = next(); 5686836c618SDavide Italiano if (Tok == ")") 5696836c618SDavide Italiano return; 570025d59b1SRui Ueyama if (Tok != ",") { 5715761042dSGeorge Rimar setError("unexpected token: " + Tok); 572025d59b1SRui Ueyama return; 573025d59b1SRui Ueyama } 5746836c618SDavide Italiano next(); 5756836c618SDavide Italiano expect(","); 5766836c618SDavide Italiano next(); 577f7c5fbb1SRui Ueyama expect(")"); 578f7c5fbb1SRui Ueyama } 579f7c5fbb1SRui Ueyama 580bbe38602SEugene Leviant void ScriptParser::readPhdrs() { 581bbe38602SEugene Leviant expect("{"); 582bbe38602SEugene Leviant while (!Error && !skip("}")) { 583bbe38602SEugene Leviant StringRef Tok = next(); 584865bf863SEugene Leviant Opt.PhdrsCommands.push_back({Tok, PT_NULL, false, false, UINT_MAX}); 585bbe38602SEugene Leviant PhdrsCommand &PhdrCmd = Opt.PhdrsCommands.back(); 586bbe38602SEugene Leviant 587bbe38602SEugene Leviant PhdrCmd.Type = readPhdrType(); 588bbe38602SEugene Leviant do { 589bbe38602SEugene Leviant Tok = next(); 590bbe38602SEugene Leviant if (Tok == ";") 591bbe38602SEugene Leviant break; 592bbe38602SEugene Leviant if (Tok == "FILEHDR") 593bbe38602SEugene Leviant PhdrCmd.HasFilehdr = true; 594bbe38602SEugene Leviant else if (Tok == "PHDRS") 595bbe38602SEugene Leviant PhdrCmd.HasPhdrs = true; 596865bf863SEugene Leviant else if (Tok == "FLAGS") { 597865bf863SEugene Leviant expect("("); 598865bf863SEugene Leviant next().getAsInteger(0, PhdrCmd.Flags); 599865bf863SEugene Leviant expect(")"); 600865bf863SEugene Leviant } else 601bbe38602SEugene Leviant setError("unexpected header attribute: " + Tok); 602bbe38602SEugene Leviant } while (!Error); 603bbe38602SEugene Leviant } 604bbe38602SEugene Leviant } 605bbe38602SEugene Leviant 606717677afSRui Ueyama void ScriptParser::readSearchDir() { 60768a39a65SDavide Italiano expect("("); 60806501920SRafael Espindola Config->SearchPaths.push_back(next()); 60968a39a65SDavide Italiano expect(")"); 61068a39a65SDavide Italiano } 61168a39a65SDavide Italiano 612717677afSRui Ueyama void ScriptParser::readSections() { 61307320e40SRui Ueyama Opt.DoLayout = true; 6148e3b38abSDenis Protivensky expect("{"); 615652852c5SGeorge Rimar while (!Error && !skip("}")) { 616113cdec9SRui Ueyama StringRef Tok = next(); 617113cdec9SRui Ueyama if (peek() == "=") { 618113cdec9SRui Ueyama readAssignment(Tok); 619113cdec9SRui Ueyama expect(";"); 620113cdec9SRui Ueyama } else if (Tok == "PROVIDE") { 621a31c91b1SEugene Leviant readProvide(false); 622708019c4SRui Ueyama } else if (Tok == "PROVIDE_HIDDEN") { 623a31c91b1SEugene Leviant readProvide(true); 624708019c4SRui Ueyama } else { 625eda81a1bSEugene Leviant readOutputSectionDescription(Tok); 6268e3b38abSDenis Protivensky } 627652852c5SGeorge Rimar } 628708019c4SRui Ueyama } 6298e3b38abSDenis Protivensky 630708019c4SRui Ueyama static int precedence(StringRef Op) { 631708019c4SRui Ueyama return StringSwitch<int>(Op) 632708019c4SRui Ueyama .Case("*", 4) 633708019c4SRui Ueyama .Case("/", 4) 634708019c4SRui Ueyama .Case("+", 3) 635708019c4SRui Ueyama .Case("-", 3) 636708019c4SRui Ueyama .Case("<", 2) 637708019c4SRui Ueyama .Case(">", 2) 638708019c4SRui Ueyama .Case(">=", 2) 639708019c4SRui Ueyama .Case("<=", 2) 640708019c4SRui Ueyama .Case("==", 2) 641708019c4SRui Ueyama .Case("!=", 2) 642708019c4SRui Ueyama .Case("&", 1) 643708019c4SRui Ueyama .Default(-1); 644708019c4SRui Ueyama } 645708019c4SRui Ueyama 646eda81a1bSEugene Leviant void ScriptParser::readOutputSectionDescription(StringRef OutSec) { 647076fe157SGeorge Rimar OutputSectionCommand *Cmd = new OutputSectionCommand(OutSec); 648076fe157SGeorge Rimar Opt.Commands.emplace_back(Cmd); 6498e3b38abSDenis Protivensky expect(":"); 650246f681eSDavide Italiano 651246f681eSDavide Italiano // Parse constraints. 652246f681eSDavide Italiano if (skip("ONLY_IF_RO")) 653246f681eSDavide Italiano Cmd->Constraint = ReadOnly; 654246f681eSDavide Italiano if (skip("ONLY_IF_RW")) 655246f681eSDavide Italiano Cmd->Constraint = ReadWrite; 6568e3b38abSDenis Protivensky expect("{"); 6578ec77e64SRui Ueyama 658025d59b1SRui Ueyama while (!Error && !skip("}")) { 659481c2ce6SGeorge Rimar StringRef Tok = next(); 660481c2ce6SGeorge Rimar if (Tok == "*") { 661eea3114fSGeorge Rimar auto *InCmd = new InputSectionDescription(); 662eea3114fSGeorge Rimar Cmd->Commands.emplace_back(InCmd); 6638ec77e64SRui Ueyama expect("("); 6648ec77e64SRui Ueyama while (!Error && !skip(")")) 665eea3114fSGeorge Rimar InCmd->Patterns.push_back(next()); 666481c2ce6SGeorge Rimar } else if (Tok == "KEEP") { 6678e3b38abSDenis Protivensky expect("("); 6688ec77e64SRui Ueyama expect("*"); 6698ec77e64SRui Ueyama expect("("); 670eea3114fSGeorge Rimar auto *InCmd = new InputSectionDescription(); 671eea3114fSGeorge Rimar Cmd->Commands.emplace_back(InCmd); 6728ec77e64SRui Ueyama while (!Error && !skip(")")) { 673eea3114fSGeorge Rimar Opt.KeptSections.push_back(peek()); 674eea3114fSGeorge Rimar InCmd->Patterns.push_back(next()); 6758ec77e64SRui Ueyama } 676481c2ce6SGeorge Rimar expect(")"); 677054a6796SDavide Italiano } else if (Tok == "PROVIDE") { 678054a6796SDavide Italiano readProvide(false); 679054a6796SDavide Italiano } else if (Tok == "PROVIDE_HIDDEN") { 680054a6796SDavide Italiano readProvide(true); 681481c2ce6SGeorge Rimar } else { 682777f9630SGeorge Rimar setError("unknown command " + Tok); 683481c2ce6SGeorge Rimar } 6848e3b38abSDenis Protivensky } 685076fe157SGeorge Rimar Cmd->Phdrs = readOutputSectionPhdrs(); 6868ec77e64SRui Ueyama 687e2ee72b5SGeorge Rimar StringRef Tok = peek(); 688e2ee72b5SGeorge Rimar if (Tok.startswith("=")) { 689e2ee72b5SGeorge Rimar if (!Tok.startswith("=0x")) { 6903ed2f069SRui Ueyama setError("filler should be a hexadecimal value"); 691e2ee72b5SGeorge Rimar return; 692e2ee72b5SGeorge Rimar } 6933e808976SRui Ueyama Tok = Tok.substr(3); 694f6c3ccefSGeorge Rimar Cmd->Filler = parseHex(Tok); 695e2ee72b5SGeorge Rimar next(); 696e2ee72b5SGeorge Rimar } 6978e3b38abSDenis Protivensky } 6988e3b38abSDenis Protivensky 699a31c91b1SEugene Leviant void ScriptParser::readProvide(bool Hidden) { 700a31c91b1SEugene Leviant expect("("); 701113cdec9SRui Ueyama if (SymbolAssignment *Assignment = readAssignment(next())) { 702a31c91b1SEugene Leviant Assignment->Provide = true; 703a31c91b1SEugene Leviant Assignment->Hidden = Hidden; 704a31c91b1SEugene Leviant } 705a31c91b1SEugene Leviant expect(")"); 706a31c91b1SEugene Leviant expect(";"); 707eda81a1bSEugene Leviant } 708eda81a1bSEugene Leviant 709113cdec9SRui Ueyama SymbolAssignment *ScriptParser::readAssignment(StringRef Name) { 710a31c91b1SEugene Leviant expect("="); 711708019c4SRui Ueyama Expr E = readExpr(); 712113cdec9SRui Ueyama auto *Cmd = new SymbolAssignment(Name, E); 713113cdec9SRui Ueyama Opt.Commands.emplace_back(Cmd); 714113cdec9SRui Ueyama return Cmd; 715a31c91b1SEugene Leviant } 716a31c91b1SEugene Leviant 717708019c4SRui Ueyama // This is an operator-precedence parser to parse a linker 718708019c4SRui Ueyama // script expression. 719708019c4SRui Ueyama Expr ScriptParser::readExpr() { return readExpr1(readPrimary(), 0); } 720a31c91b1SEugene Leviant 721708019c4SRui Ueyama // This is a part of the operator-precedence parser. This function 722708019c4SRui Ueyama // assumes that the remaining token stream starts with an operator. 723708019c4SRui Ueyama Expr ScriptParser::readExpr1(Expr Lhs, int MinPrec) { 724708019c4SRui Ueyama while (!atEOF() && !Error) { 725708019c4SRui Ueyama // Read an operator and an expression. 726708019c4SRui Ueyama StringRef Op1 = peek(); 727708019c4SRui Ueyama if (Op1 == "?") 728708019c4SRui Ueyama return readTernary(Lhs); 729708019c4SRui Ueyama if (precedence(Op1) < MinPrec) 730a31c91b1SEugene Leviant break; 731a31c91b1SEugene Leviant next(); 732708019c4SRui Ueyama Expr Rhs = readPrimary(); 733708019c4SRui Ueyama 734708019c4SRui Ueyama // Evaluate the remaining part of the expression first if the 735708019c4SRui Ueyama // next operator has greater precedence than the previous one. 736708019c4SRui Ueyama // For example, if we have read "+" and "3", and if the next 737708019c4SRui Ueyama // operator is "*", then we'll evaluate 3 * ... part first. 738708019c4SRui Ueyama while (!atEOF()) { 739708019c4SRui Ueyama StringRef Op2 = peek(); 740708019c4SRui Ueyama if (precedence(Op2) <= precedence(Op1)) 741eda81a1bSEugene Leviant break; 742708019c4SRui Ueyama Rhs = readExpr1(Rhs, precedence(Op2)); 743eda81a1bSEugene Leviant } 744708019c4SRui Ueyama 745708019c4SRui Ueyama Lhs = combine(Op1, Lhs, Rhs); 746708019c4SRui Ueyama } 747708019c4SRui Ueyama return Lhs; 748708019c4SRui Ueyama } 749708019c4SRui Ueyama 750708019c4SRui Ueyama uint64_t static getConstant(StringRef S) { 751708019c4SRui Ueyama if (S == "COMMONPAGESIZE" || S == "MAXPAGESIZE") 752708019c4SRui Ueyama return Target->PageSize; 753708019c4SRui Ueyama error("unknown constant: " + S); 754708019c4SRui Ueyama return 0; 755708019c4SRui Ueyama } 756708019c4SRui Ueyama 757708019c4SRui Ueyama Expr ScriptParser::readPrimary() { 758708019c4SRui Ueyama StringRef Tok = next(); 759708019c4SRui Ueyama 760708019c4SRui Ueyama if (Tok == ".") 761708019c4SRui Ueyama return [](uint64_t Dot) { return Dot; }; 762708019c4SRui Ueyama 763708019c4SRui Ueyama if (Tok == "(") { 764708019c4SRui Ueyama Expr E = readExpr(); 765708019c4SRui Ueyama expect(")"); 766708019c4SRui Ueyama return E; 767708019c4SRui Ueyama } 768708019c4SRui Ueyama 769708019c4SRui Ueyama // Built-in functions are parsed here. 770708019c4SRui Ueyama // https://sourceware.org/binutils/docs/ld/Builtin-Functions.html. 771708019c4SRui Ueyama if (Tok == "ALIGN") { 772708019c4SRui Ueyama expect("("); 773708019c4SRui Ueyama Expr E = readExpr(); 774708019c4SRui Ueyama expect(")"); 775708019c4SRui Ueyama return [=](uint64_t Dot) { return alignTo(Dot, E(Dot)); }; 776708019c4SRui Ueyama } 777708019c4SRui Ueyama if (Tok == "CONSTANT") { 778708019c4SRui Ueyama expect("("); 779708019c4SRui Ueyama StringRef Tok = next(); 780708019c4SRui Ueyama expect(")"); 781708019c4SRui Ueyama return [=](uint64_t Dot) { return getConstant(Tok); }; 782708019c4SRui Ueyama } 783708019c4SRui Ueyama if (Tok == "DATA_SEGMENT_ALIGN") { 784708019c4SRui Ueyama expect("("); 785708019c4SRui Ueyama Expr E = readExpr(); 786708019c4SRui Ueyama expect(","); 787708019c4SRui Ueyama readExpr(); 788708019c4SRui Ueyama expect(")"); 789708019c4SRui Ueyama return [=](uint64_t Dot) -> uint64_t { 790708019c4SRui Ueyama uint64_t Val = E(Dot); 791708019c4SRui Ueyama return alignTo(Dot, Val) + (Dot & (Val - 1)); 792708019c4SRui Ueyama }; 793708019c4SRui Ueyama } 794708019c4SRui Ueyama if (Tok == "DATA_SEGMENT_END") { 795708019c4SRui Ueyama expect("("); 796708019c4SRui Ueyama expect("."); 797708019c4SRui Ueyama expect(")"); 798708019c4SRui Ueyama return [](uint64_t Dot) { return Dot; }; 799708019c4SRui Ueyama } 800708019c4SRui Ueyama 801708019c4SRui Ueyama // Parse a number literal 802708019c4SRui Ueyama uint64_t V = 0; 803708019c4SRui Ueyama if (Tok.getAsInteger(0, V)) 804708019c4SRui Ueyama setError("malformed number: " + Tok); 805708019c4SRui Ueyama return [=](uint64_t Dot) { return V; }; 806708019c4SRui Ueyama } 807708019c4SRui Ueyama 808708019c4SRui Ueyama Expr ScriptParser::readTernary(Expr Cond) { 809708019c4SRui Ueyama next(); 810708019c4SRui Ueyama Expr L = readExpr(); 811708019c4SRui Ueyama expect(":"); 812708019c4SRui Ueyama Expr R = readExpr(); 813708019c4SRui Ueyama return [=](uint64_t Dot) { return Cond(Dot) ? L(Dot) : R(Dot); }; 814708019c4SRui Ueyama } 815708019c4SRui Ueyama 816708019c4SRui Ueyama Expr ScriptParser::combine(StringRef Op, Expr L, Expr R) { 817708019c4SRui Ueyama if (Op == "*") 818708019c4SRui Ueyama return [=](uint64_t Dot) { return L(Dot) * R(Dot); }; 819708019c4SRui Ueyama if (Op == "/") { 820708019c4SRui Ueyama return [=](uint64_t Dot) -> uint64_t { 821708019c4SRui Ueyama uint64_t RHS = R(Dot); 822708019c4SRui Ueyama if (RHS == 0) { 823708019c4SRui Ueyama error("division by zero"); 824708019c4SRui Ueyama return 0; 825708019c4SRui Ueyama } 826708019c4SRui Ueyama return L(Dot) / RHS; 827708019c4SRui Ueyama }; 828708019c4SRui Ueyama } 829708019c4SRui Ueyama if (Op == "+") 830708019c4SRui Ueyama return [=](uint64_t Dot) { return L(Dot) + R(Dot); }; 831708019c4SRui Ueyama if (Op == "-") 832708019c4SRui Ueyama return [=](uint64_t Dot) { return L(Dot) - R(Dot); }; 833708019c4SRui Ueyama if (Op == "<") 834708019c4SRui Ueyama return [=](uint64_t Dot) { return L(Dot) < R(Dot); }; 835708019c4SRui Ueyama if (Op == ">") 836708019c4SRui Ueyama return [=](uint64_t Dot) { return L(Dot) > R(Dot); }; 837708019c4SRui Ueyama if (Op == ">=") 838708019c4SRui Ueyama return [=](uint64_t Dot) { return L(Dot) >= R(Dot); }; 839708019c4SRui Ueyama if (Op == "<=") 840708019c4SRui Ueyama return [=](uint64_t Dot) { return L(Dot) <= R(Dot); }; 841708019c4SRui Ueyama if (Op == "==") 842708019c4SRui Ueyama return [=](uint64_t Dot) { return L(Dot) == R(Dot); }; 843708019c4SRui Ueyama if (Op == "!=") 844708019c4SRui Ueyama return [=](uint64_t Dot) { return L(Dot) != R(Dot); }; 845708019c4SRui Ueyama if (Op == "&") 846708019c4SRui Ueyama return [=](uint64_t Dot) { return L(Dot) & R(Dot); }; 847708019c4SRui Ueyama llvm_unreachable("invalid operator"); 848eda81a1bSEugene Leviant } 849eda81a1bSEugene Leviant 850bbe38602SEugene Leviant std::vector<StringRef> ScriptParser::readOutputSectionPhdrs() { 851bbe38602SEugene Leviant std::vector<StringRef> Phdrs; 852bbe38602SEugene Leviant while (!Error && peek().startswith(":")) { 853bbe38602SEugene Leviant StringRef Tok = next(); 854bbe38602SEugene Leviant Tok = (Tok.size() == 1) ? next() : Tok.substr(1); 855bbe38602SEugene Leviant if (Tok.empty()) { 856bbe38602SEugene Leviant setError("section header name is empty"); 857bbe38602SEugene Leviant break; 858bbe38602SEugene Leviant } 859bbe38602SEugene Leviant Phdrs.push_back(Tok); 860bbe38602SEugene Leviant } 861bbe38602SEugene Leviant return Phdrs; 862bbe38602SEugene Leviant } 863bbe38602SEugene Leviant 864bbe38602SEugene Leviant unsigned ScriptParser::readPhdrType() { 865bbe38602SEugene Leviant StringRef Tok = next(); 866b0f6c590SRui Ueyama unsigned Ret = StringSwitch<unsigned>(Tok) 867b0f6c590SRui Ueyama .Case("PT_NULL", PT_NULL) 868b0f6c590SRui Ueyama .Case("PT_LOAD", PT_LOAD) 869b0f6c590SRui Ueyama .Case("PT_DYNAMIC", PT_DYNAMIC) 870b0f6c590SRui Ueyama .Case("PT_INTERP", PT_INTERP) 871b0f6c590SRui Ueyama .Case("PT_NOTE", PT_NOTE) 872b0f6c590SRui Ueyama .Case("PT_SHLIB", PT_SHLIB) 873b0f6c590SRui Ueyama .Case("PT_PHDR", PT_PHDR) 874b0f6c590SRui Ueyama .Case("PT_TLS", PT_TLS) 875b0f6c590SRui Ueyama .Case("PT_GNU_EH_FRAME", PT_GNU_EH_FRAME) 876b0f6c590SRui Ueyama .Case("PT_GNU_STACK", PT_GNU_STACK) 877b0f6c590SRui Ueyama .Case("PT_GNU_RELRO", PT_GNU_RELRO) 878b0f6c590SRui Ueyama .Default(-1); 879bbe38602SEugene Leviant 880b0f6c590SRui Ueyama if (Ret == (unsigned)-1) { 881b0f6c590SRui Ueyama setError("invalid program header type: " + Tok); 882b0f6c590SRui Ueyama return PT_NULL; 883b0f6c590SRui Ueyama } 884b0f6c590SRui Ueyama return Ret; 885bbe38602SEugene Leviant } 886bbe38602SEugene Leviant 88716b0cc9eSSimon Atanasyan static bool isUnderSysroot(StringRef Path) { 88816b0cc9eSSimon Atanasyan if (Config->Sysroot == "") 88916b0cc9eSSimon Atanasyan return false; 89016b0cc9eSSimon Atanasyan for (; !Path.empty(); Path = sys::path::parent_path(Path)) 89116b0cc9eSSimon Atanasyan if (sys::fs::equivalent(Config->Sysroot, Path)) 89216b0cc9eSSimon Atanasyan return true; 89316b0cc9eSSimon Atanasyan return false; 89416b0cc9eSSimon Atanasyan } 89516b0cc9eSSimon Atanasyan 89607320e40SRui Ueyama // Entry point. 89707320e40SRui Ueyama void elf::readLinkerScript(MemoryBufferRef MB) { 89816b0cc9eSSimon Atanasyan StringRef Path = MB.getBufferIdentifier(); 89907320e40SRui Ueyama ScriptParser(MB.getBuffer(), isUnderSysroot(Path)).run(); 900f7c5fbb1SRui Ueyama } 9011ebc8ed7SRui Ueyama 90207320e40SRui Ueyama template class elf::LinkerScript<ELF32LE>; 90307320e40SRui Ueyama template class elf::LinkerScript<ELF32BE>; 90407320e40SRui Ueyama template class elf::LinkerScript<ELF64LE>; 90507320e40SRui Ueyama template class elf::LinkerScript<ELF64BE>; 906