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 
776b274810SRui Ueyama // Create a vector of (<output section name>, <input section name patterns>).
786b274810SRui Ueyama // For example, if a returned vector contains (".text" (".foo.*" ".bar.*")),
796b274810SRui Ueyama // input sections start with ".foo." or ".bar." should be added to
806b274810SRui Ueyama // ".text" section.
816b274810SRui Ueyama template <class ELFT>
82e7282797SDavide Italiano std::vector<std::pair<StringRef, const InputSectionDescription *>>
836b274810SRui Ueyama LinkerScript<ELFT>::getSectionMap() {
84e7282797SDavide Italiano   std::vector<std::pair<StringRef, const InputSectionDescription *>> Ret;
856b274810SRui Ueyama 
866b274810SRui Ueyama   for (const std::unique_ptr<BaseCommand> &Base1 : Opt.Commands)
876b274810SRui Ueyama     if (auto *Cmd1 = dyn_cast<OutputSectionCommand>(Base1.get()))
886b274810SRui Ueyama       for (const std::unique_ptr<BaseCommand> &Base2 : Cmd1->Commands)
896b274810SRui Ueyama         if (auto *Cmd2 = dyn_cast<InputSectionDescription>(Base2.get()))
90e7282797SDavide Italiano           Ret.emplace_back(Cmd1->Name, Cmd2);
916b274810SRui Ueyama 
926b274810SRui Ueyama   return Ret;
936b274810SRui Ueyama }
946b274810SRui Ueyama 
950659800eSGeorge Rimar static bool fileMatches(const InputSectionDescription *Desc,
960659800eSGeorge Rimar                         StringRef Filename) {
970659800eSGeorge Rimar   if (!globMatch(Desc->FilePattern, Filename))
980659800eSGeorge Rimar     return false;
990659800eSGeorge Rimar   return Desc->ExcludedFiles.empty() || !match(Desc->ExcludedFiles, Filename);
1000659800eSGeorge Rimar }
1010659800eSGeorge Rimar 
1026b274810SRui Ueyama // Returns input sections filtered by given glob patterns.
1036b274810SRui Ueyama template <class ELFT>
1046b274810SRui Ueyama std::vector<InputSectionBase<ELFT> *>
105ad10c3d8SRui Ueyama LinkerScript<ELFT>::getInputSections(const InputSectionDescription *I) {
1060659800eSGeorge Rimar   ArrayRef<StringRef> Patterns = I->SectionPatterns;
1076b274810SRui Ueyama   std::vector<InputSectionBase<ELFT> *> Ret;
1086b274810SRui Ueyama   for (const std::unique_ptr<ObjectFile<ELFT>> &F :
1090659800eSGeorge Rimar        Symtab<ELFT>::X->getObjectFiles()) {
1100659800eSGeorge Rimar     if (fileMatches(I, sys::path::filename(F->getName())))
1116b274810SRui Ueyama       for (InputSectionBase<ELFT> *S : F->getSections())
1120659800eSGeorge Rimar         if (!isDiscarded(S) && !S->OutSec &&
1130659800eSGeorge Rimar             match(Patterns, S->getSectionName()))
1146b274810SRui Ueyama           Ret.push_back(S);
1150659800eSGeorge Rimar   }
1163e6b0277SEugene Leviant 
1173e6b0277SEugene Leviant   if ((llvm::find(Patterns, "COMMON") != Patterns.end()))
118ad10c3d8SRui Ueyama     Ret.push_back(CommonInputSection<ELFT>::X);
1193e6b0277SEugene Leviant 
1206b274810SRui Ueyama   return Ret;
1216b274810SRui Ueyama }
1226b274810SRui Ueyama 
123652852c5SGeorge Rimar template <class ELFT>
124a7f7884dSRui Ueyama std::vector<OutputSectionBase<ELFT> *>
125ad10c3d8SRui Ueyama LinkerScript<ELFT>::createSections(OutputSectionFactory<ELFT> &Factory) {
1266b274810SRui Ueyama   std::vector<OutputSectionBase<ELFT> *> Ret;
127a7f7884dSRui Ueyama 
128e63d81bdSEugene Leviant   // Add input section to output section. If there is no output section yet,
129e63d81bdSEugene Leviant   // then create it and add to output section list.
1306b274810SRui Ueyama   auto Add = [&](InputSectionBase<ELFT> *C, StringRef Name) {
131e63d81bdSEugene Leviant     OutputSectionBase<ELFT> *Sec;
132e63d81bdSEugene Leviant     bool IsNew;
133e63d81bdSEugene Leviant     std::tie(Sec, IsNew) = Factory.create(C, Name);
134e63d81bdSEugene Leviant     if (IsNew)
1356b274810SRui Ueyama       Ret.push_back(Sec);
136e63d81bdSEugene Leviant     Sec->addSection(C);
137e63d81bdSEugene Leviant   };
138e63d81bdSEugene Leviant 
1396b274810SRui Ueyama   for (auto &P : getSectionMap()) {
1406b274810SRui Ueyama     StringRef OutputName = P.first;
141e7282797SDavide Italiano     const InputSectionDescription *I = P.second;
142ad10c3d8SRui Ueyama     for (InputSectionBase<ELFT> *S : getInputSections(I)) {
1436b274810SRui Ueyama       if (OutputName == "/DISCARD/") {
144eea3114fSGeorge Rimar         S->Live = false;
1456b274810SRui Ueyama         reportDiscarded(S);
1466b274810SRui Ueyama         continue;
147eea3114fSGeorge Rimar       }
1486b274810SRui Ueyama       Add(S, OutputName);
149eea3114fSGeorge Rimar     }
150eea3114fSGeorge Rimar   }
151e63d81bdSEugene Leviant 
152e63d81bdSEugene Leviant   // Add all other input sections, which are not listed in script.
1536b274810SRui Ueyama   for (const std::unique_ptr<ObjectFile<ELFT>> &F :
1546b274810SRui Ueyama        Symtab<ELFT>::X->getObjectFiles())
1556b274810SRui Ueyama     for (InputSectionBase<ELFT> *S : F->getSections())
1566b274810SRui Ueyama       if (!isDiscarded(S) && !S->OutSec)
1576b274810SRui Ueyama         Add(S, getOutputSectionName(S));
158e63d81bdSEugene Leviant 
1593c291e1aSRui Ueyama   // Remove from the output all the sections which did not meet
1603c291e1aSRui Ueyama   // the optional constraints.
1616b274810SRui Ueyama   return filter(Ret);
1623c291e1aSRui Ueyama }
1633c291e1aSRui Ueyama 
1643c291e1aSRui Ueyama // Process ONLY_IF_RO and ONLY_IF_RW.
1653c291e1aSRui Ueyama template <class ELFT>
1663c291e1aSRui Ueyama std::vector<OutputSectionBase<ELFT> *>
1673c291e1aSRui Ueyama LinkerScript<ELFT>::filter(std::vector<OutputSectionBase<ELFT> *> &Sections) {
1683c291e1aSRui Ueyama   // In this loop, we remove output sections if they don't satisfy
1693c291e1aSRui Ueyama   // requested properties.
1703c291e1aSRui Ueyama   for (const std::unique_ptr<BaseCommand> &Base : Opt.Commands) {
1713c291e1aSRui Ueyama     auto *Cmd = dyn_cast<OutputSectionCommand>(Base.get());
1723c291e1aSRui Ueyama     if (!Cmd || Cmd->Name == "/DISCARD/")
1733c291e1aSRui Ueyama       continue;
1743c291e1aSRui Ueyama 
175bfc4a4b7SGeorge Rimar     if (Cmd->Constraint == ConstraintKind::NoConstraint)
1763c291e1aSRui Ueyama       continue;
177bfc4a4b7SGeorge Rimar 
178bfc4a4b7SGeorge Rimar     auto It = llvm::find_if(Sections, [&](OutputSectionBase<ELFT> *S) {
179bfc4a4b7SGeorge Rimar       return S->getName() == Cmd->Name;
180bfc4a4b7SGeorge Rimar     });
181bfc4a4b7SGeorge Rimar     if (It == Sections.end())
182bfc4a4b7SGeorge Rimar       continue;
1833c291e1aSRui Ueyama 
1843c291e1aSRui Ueyama     OutputSectionBase<ELFT> *Sec = *It;
1853c291e1aSRui Ueyama     bool Writable = (Sec->getFlags() & SHF_WRITE);
1863c291e1aSRui Ueyama     bool RO = (Cmd->Constraint == ConstraintKind::ReadOnly);
1873c291e1aSRui Ueyama     bool RW = (Cmd->Constraint == ConstraintKind::ReadWrite);
1883c291e1aSRui Ueyama 
1893c291e1aSRui Ueyama     if ((RO && Writable) || (RW && !Writable)) {
1903c291e1aSRui Ueyama       Sections.erase(It);
1913c291e1aSRui Ueyama       continue;
1923c291e1aSRui Ueyama     }
1933c291e1aSRui Ueyama   }
1943c291e1aSRui Ueyama   return Sections;
195e63d81bdSEugene Leviant }
196e63d81bdSEugene Leviant 
197e63d81bdSEugene Leviant template <class ELFT>
19807320e40SRui Ueyama void LinkerScript<ELFT>::assignAddresses(
199dbbd8b15SGeorge Rimar     ArrayRef<OutputSectionBase<ELFT> *> Sections) {
200652852c5SGeorge Rimar   // Orphan sections are sections present in the input files which
2017c18c28cSRui Ueyama   // are not explicitly placed into the output file by the linker script.
2027c18c28cSRui Ueyama   // We place orphan sections at end of file.
2037c18c28cSRui Ueyama   // Other linkers places them using some heuristics as described in
204652852c5SGeorge Rimar   // https://sourceware.org/binutils/docs/ld/Orphan-Sections.html#Orphan-Sections.
2057c18c28cSRui Ueyama   for (OutputSectionBase<ELFT> *Sec : Sections) {
206652852c5SGeorge Rimar     StringRef Name = Sec->getName();
207c3e2a4b0SRui Ueyama     if (getSectionIndex(Name) == INT_MAX)
208076fe157SGeorge Rimar       Opt.Commands.push_back(llvm::make_unique<OutputSectionCommand>(Name));
209652852c5SGeorge Rimar   }
210652852c5SGeorge Rimar 
2117c18c28cSRui Ueyama   // Assign addresses as instructed by linker script SECTIONS sub-commands.
212c998a8c0SRui Ueyama   Dot = Out<ELFT>::ElfHeader->getSize() + Out<ELFT>::ProgramHeaders->getSize();
213467c4d55SEugene Leviant   uintX_t MinVA = std::numeric_limits<uintX_t>::max();
214652852c5SGeorge Rimar   uintX_t ThreadBssOffset = 0;
215652852c5SGeorge Rimar 
216076fe157SGeorge Rimar   for (const std::unique_ptr<BaseCommand> &Base : Opt.Commands) {
217076fe157SGeorge Rimar     if (auto *Cmd = dyn_cast<SymbolAssignment>(Base.get())) {
2188d083e6aSRui Ueyama       if (Cmd->Name == ".") {
2198d083e6aSRui Ueyama         Dot = Cmd->Expression(Dot);
2208d083e6aSRui Ueyama       } else if (Cmd->Sym) {
2218d083e6aSRui Ueyama         cast<DefinedRegular<ELFT>>(Cmd->Sym)->Value = Cmd->Expression(Dot);
2228d083e6aSRui Ueyama       }
22305ef4cffSRui Ueyama       continue;
224652852c5SGeorge Rimar     }
225652852c5SGeorge Rimar 
226fb8978fcSDima Stepanov     // Find all the sections with required name. There can be more than
2276ad330acSGeorge Rimar     // one section with such name, if the alignment, flags or type
228fb8978fcSDima Stepanov     // attribute differs.
229076fe157SGeorge Rimar     auto *Cmd = cast<OutputSectionCommand>(Base.get());
230fb8978fcSDima Stepanov     for (OutputSectionBase<ELFT> *Sec : Sections) {
231076fe157SGeorge Rimar       if (Sec->getName() != Cmd->Name)
232652852c5SGeorge Rimar         continue;
233652852c5SGeorge Rimar 
23458e5c4dcSGeorge Rimar       if (Cmd->AddrExpr)
23558e5c4dcSGeorge Rimar         Dot = Cmd->AddrExpr(Dot);
23658e5c4dcSGeorge Rimar 
237630c6179SGeorge Rimar       if (Cmd->AlignExpr)
238630c6179SGeorge Rimar         Sec->updateAlignment(Cmd->AlignExpr(Dot));
239630c6179SGeorge Rimar 
240652852c5SGeorge Rimar       if ((Sec->getFlags() & SHF_TLS) && Sec->getType() == SHT_NOBITS) {
241c998a8c0SRui Ueyama         uintX_t TVA = Dot + ThreadBssOffset;
242424b4081SRui Ueyama         TVA = alignTo(TVA, Sec->getAlignment());
243652852c5SGeorge Rimar         Sec->setVA(TVA);
244c998a8c0SRui Ueyama         ThreadBssOffset = TVA - Dot + Sec->getSize();
245652852c5SGeorge Rimar         continue;
246652852c5SGeorge Rimar       }
247652852c5SGeorge Rimar 
248652852c5SGeorge Rimar       if (Sec->getFlags() & SHF_ALLOC) {
249424b4081SRui Ueyama         Dot = alignTo(Dot, Sec->getAlignment());
250c998a8c0SRui Ueyama         Sec->setVA(Dot);
251467c4d55SEugene Leviant         MinVA = std::min(MinVA, Dot);
252c998a8c0SRui Ueyama         Dot += Sec->getSize();
253652852c5SGeorge Rimar         continue;
254652852c5SGeorge Rimar       }
255652852c5SGeorge Rimar     }
256652852c5SGeorge Rimar   }
257467c4d55SEugene Leviant 
25864c32d6fSRafael Espindola   // ELF and Program headers need to be right before the first section in
259b91e7118SGeorge Rimar   // memory. Set their addresses accordingly.
260467c4d55SEugene Leviant   MinVA = alignDown(MinVA - Out<ELFT>::ElfHeader->getSize() -
261467c4d55SEugene Leviant                         Out<ELFT>::ProgramHeaders->getSize(),
262467c4d55SEugene Leviant                     Target->PageSize);
263467c4d55SEugene Leviant   Out<ELFT>::ElfHeader->setVA(MinVA);
264467c4d55SEugene Leviant   Out<ELFT>::ProgramHeaders->setVA(Out<ELFT>::ElfHeader->getSize() + MinVA);
265fb8978fcSDima Stepanov }
266652852c5SGeorge Rimar 
26707320e40SRui Ueyama template <class ELFT>
26874df5c7eSRafael Espindola std::vector<PhdrEntry<ELFT>>
269bbe38602SEugene Leviant LinkerScript<ELFT>::createPhdrs(ArrayRef<OutputSectionBase<ELFT> *> Sections) {
270edebbdf1SRui Ueyama   std::vector<PhdrEntry<ELFT>> Ret;
271bbe38602SEugene Leviant 
272bbe38602SEugene Leviant   for (const PhdrsCommand &Cmd : Opt.PhdrsCommands) {
273edebbdf1SRui Ueyama     Ret.emplace_back(Cmd.Type, Cmd.Flags == UINT_MAX ? PF_R : Cmd.Flags);
274edebbdf1SRui Ueyama     PhdrEntry<ELFT> &Phdr = Ret.back();
275bbe38602SEugene Leviant 
276bbe38602SEugene Leviant     if (Cmd.HasFilehdr)
277adca245fSRui Ueyama       Phdr.add(Out<ELFT>::ElfHeader);
278bbe38602SEugene Leviant     if (Cmd.HasPhdrs)
279adca245fSRui Ueyama       Phdr.add(Out<ELFT>::ProgramHeaders);
280bbe38602SEugene Leviant 
281bbe38602SEugene Leviant     switch (Cmd.Type) {
282bbe38602SEugene Leviant     case PT_INTERP:
283fd03cfd2SRui Ueyama       if (Out<ELFT>::Interp)
284adca245fSRui Ueyama         Phdr.add(Out<ELFT>::Interp);
285bbe38602SEugene Leviant       break;
286bbe38602SEugene Leviant     case PT_DYNAMIC:
287bbe38602SEugene Leviant       if (isOutputDynamic<ELFT>()) {
2880b113671SRafael Espindola         Phdr.H.p_flags = Out<ELFT>::Dynamic->getPhdrFlags();
289adca245fSRui Ueyama         Phdr.add(Out<ELFT>::Dynamic);
290bbe38602SEugene Leviant       }
291bbe38602SEugene Leviant       break;
292bbe38602SEugene Leviant     case PT_GNU_EH_FRAME:
293bbe38602SEugene Leviant       if (!Out<ELFT>::EhFrame->empty() && Out<ELFT>::EhFrameHdr) {
2940b113671SRafael Espindola         Phdr.H.p_flags = Out<ELFT>::EhFrameHdr->getPhdrFlags();
295adca245fSRui Ueyama         Phdr.add(Out<ELFT>::EhFrameHdr);
296bbe38602SEugene Leviant       }
297bbe38602SEugene Leviant       break;
298bbe38602SEugene Leviant     }
299bbe38602SEugene Leviant   }
300bbe38602SEugene Leviant 
301edebbdf1SRui Ueyama   PhdrEntry<ELFT> *Load = nullptr;
302edebbdf1SRui Ueyama   uintX_t Flags = PF_R;
303bbe38602SEugene Leviant   for (OutputSectionBase<ELFT> *Sec : Sections) {
304bbe38602SEugene Leviant     if (!(Sec->getFlags() & SHF_ALLOC))
305bbe38602SEugene Leviant       break;
306bbe38602SEugene Leviant 
307edebbdf1SRui Ueyama     std::vector<size_t> PhdrIds = getPhdrIndices(Sec->getName());
308bbe38602SEugene Leviant     if (!PhdrIds.empty()) {
309bbe38602SEugene Leviant       // Assign headers specified by linker script
310bbe38602SEugene Leviant       for (size_t Id : PhdrIds) {
311edebbdf1SRui Ueyama         Ret[Id].add(Sec);
312865bf863SEugene Leviant         if (Opt.PhdrsCommands[Id].Flags == UINT_MAX)
3130b113671SRafael Espindola           Ret[Id].H.p_flags |= Sec->getPhdrFlags();
314bbe38602SEugene Leviant       }
315bbe38602SEugene Leviant     } else {
316bbe38602SEugene Leviant       // If we have no load segment or flags've changed then we want new load
317bbe38602SEugene Leviant       // segment.
3180b113671SRafael Espindola       uintX_t NewFlags = Sec->getPhdrFlags();
319bbe38602SEugene Leviant       if (Load == nullptr || Flags != NewFlags) {
320edebbdf1SRui Ueyama         Load = &*Ret.emplace(Ret.end(), PT_LOAD, NewFlags);
321bbe38602SEugene Leviant         Flags = NewFlags;
322bbe38602SEugene Leviant       }
32318f084ffSRui Ueyama       Load->add(Sec);
324bbe38602SEugene Leviant     }
325bbe38602SEugene Leviant   }
326edebbdf1SRui Ueyama   return Ret;
327bbe38602SEugene Leviant }
328bbe38602SEugene Leviant 
329bbe38602SEugene Leviant template <class ELFT>
33007320e40SRui Ueyama ArrayRef<uint8_t> LinkerScript<ELFT>::getFiller(StringRef Name) {
331f6c3ccefSGeorge Rimar   for (const std::unique_ptr<BaseCommand> &Base : Opt.Commands)
332f6c3ccefSGeorge Rimar     if (auto *Cmd = dyn_cast<OutputSectionCommand>(Base.get()))
333f6c3ccefSGeorge Rimar       if (Cmd->Name == Name)
334f6c3ccefSGeorge Rimar         return Cmd->Filler;
335e2ee72b5SGeorge Rimar   return {};
336e2ee72b5SGeorge Rimar }
337e2ee72b5SGeorge Rimar 
338c3e2a4b0SRui Ueyama // Returns the index of the given section name in linker script
339c3e2a4b0SRui Ueyama // SECTIONS commands. Sections are laid out as the same order as they
340c3e2a4b0SRui Ueyama // were in the script. If a given name did not appear in the script,
341c3e2a4b0SRui Ueyama // it returns INT_MAX, so that it will be laid out at end of file.
342076fe157SGeorge Rimar template <class ELFT> int LinkerScript<ELFT>::getSectionIndex(StringRef Name) {
343f510fa6bSRui Ueyama   int I = 0;
344f510fa6bSRui Ueyama   for (std::unique_ptr<BaseCommand> &Base : Opt.Commands) {
345076fe157SGeorge Rimar     if (auto *Cmd = dyn_cast<OutputSectionCommand>(Base.get()))
346076fe157SGeorge Rimar       if (Cmd->Name == Name)
347f510fa6bSRui Ueyama         return I;
348f510fa6bSRui Ueyama     ++I;
349f510fa6bSRui Ueyama   }
350f510fa6bSRui Ueyama   return INT_MAX;
35171b26e94SGeorge Rimar }
35271b26e94SGeorge Rimar 
35371b26e94SGeorge Rimar // A compartor to sort output sections. Returns -1 or 1 if
35471b26e94SGeorge Rimar // A or B are mentioned in linker script. Otherwise, returns 0.
35507320e40SRui Ueyama template <class ELFT>
35607320e40SRui Ueyama int LinkerScript<ELFT>::compareSections(StringRef A, StringRef B) {
357c3e2a4b0SRui Ueyama   int I = getSectionIndex(A);
358c3e2a4b0SRui Ueyama   int J = getSectionIndex(B);
359c3e2a4b0SRui Ueyama   if (I == INT_MAX && J == INT_MAX)
360717677afSRui Ueyama     return 0;
361717677afSRui Ueyama   return I < J ? -1 : 1;
362717677afSRui Ueyama }
363717677afSRui Ueyama 
3648d083e6aSRui Ueyama // Add symbols defined by linker scripts.
365076fe157SGeorge Rimar template <class ELFT> void LinkerScript<ELFT>::addScriptedSymbols() {
366a31c91b1SEugene Leviant   for (const std::unique_ptr<BaseCommand> &Base : Opt.Commands) {
367a31c91b1SEugene Leviant     auto *Cmd = dyn_cast<SymbolAssignment>(Base.get());
368a31c91b1SEugene Leviant     if (!Cmd || Cmd->Name == ".")
369a31c91b1SEugene Leviant       continue;
370a31c91b1SEugene Leviant 
3718d083e6aSRui Ueyama     // If a symbol was in PROVIDE(), define it only when it is an
3728d083e6aSRui Ueyama     // undefined symbol.
3738ab4108dSDavide Italiano     SymbolBody *B = Symtab<ELFT>::X->find(Cmd->Name);
3748d083e6aSRui Ueyama     if (Cmd->Provide && !(B && B->isUndefined()))
3758d083e6aSRui Ueyama       continue;
3768d083e6aSRui Ueyama 
3778d083e6aSRui Ueyama     // Define an absolute symbol. The symbol value will be assigned later.
3788d083e6aSRui Ueyama     // (At this point, we don't know the final address yet.)
3798d083e6aSRui Ueyama     Symbol *Sym = Symtab<ELFT>::X->addUndefined(Cmd->Name);
3808d083e6aSRui Ueyama     replaceBody<DefinedRegular<ELFT>>(Sym, Cmd->Name, STV_DEFAULT);
3818d083e6aSRui Ueyama     Sym->Visibility = Cmd->Hidden ? STV_HIDDEN : STV_DEFAULT;
3828d083e6aSRui Ueyama     Cmd->Sym = Sym->body();
383a31c91b1SEugene Leviant   }
384eda81a1bSEugene Leviant }
385eda81a1bSEugene Leviant 
386bbe38602SEugene Leviant template <class ELFT> bool LinkerScript<ELFT>::hasPhdrsCommands() {
387bbe38602SEugene Leviant   return !Opt.PhdrsCommands.empty();
388bbe38602SEugene Leviant }
389bbe38602SEugene Leviant 
390bbe38602SEugene Leviant // Returns indices of ELF headers containing specific section, identified
391bbe38602SEugene Leviant // by Name. Each index is a zero based number of ELF header listed within
392bbe38602SEugene Leviant // PHDRS {} script block.
393bbe38602SEugene Leviant template <class ELFT>
394edebbdf1SRui Ueyama std::vector<size_t> LinkerScript<ELFT>::getPhdrIndices(StringRef SectionName) {
395076fe157SGeorge Rimar   for (const std::unique_ptr<BaseCommand> &Base : Opt.Commands) {
396076fe157SGeorge Rimar     auto *Cmd = dyn_cast<OutputSectionCommand>(Base.get());
397edebbdf1SRui Ueyama     if (!Cmd || Cmd->Name != SectionName)
39831d842f5SGeorge Rimar       continue;
39931d842f5SGeorge Rimar 
40029c5a2a9SRui Ueyama     std::vector<size_t> Ret;
40129c5a2a9SRui Ueyama     for (StringRef PhdrName : Cmd->Phdrs)
40229c5a2a9SRui Ueyama       Ret.push_back(getPhdrIndex(PhdrName));
40329c5a2a9SRui Ueyama     return Ret;
404bbe38602SEugene Leviant   }
40531d842f5SGeorge Rimar   return {};
40631d842f5SGeorge Rimar }
407bbe38602SEugene Leviant 
40829c5a2a9SRui Ueyama template <class ELFT>
40929c5a2a9SRui Ueyama size_t LinkerScript<ELFT>::getPhdrIndex(StringRef PhdrName) {
41029c5a2a9SRui Ueyama   size_t I = 0;
41129c5a2a9SRui Ueyama   for (PhdrsCommand &Cmd : Opt.PhdrsCommands) {
41229c5a2a9SRui Ueyama     if (Cmd.Name == PhdrName)
41329c5a2a9SRui Ueyama       return I;
41429c5a2a9SRui Ueyama     ++I;
41529c5a2a9SRui Ueyama   }
41629c5a2a9SRui Ueyama   error("section header '" + PhdrName + "' is not listed in PHDRS");
41729c5a2a9SRui Ueyama   return 0;
41829c5a2a9SRui Ueyama }
41929c5a2a9SRui Ueyama 
42007320e40SRui Ueyama class elf::ScriptParser : public ScriptParserBase {
421c3794e58SGeorge Rimar   typedef void (ScriptParser::*Handler)();
422c3794e58SGeorge Rimar 
423f7c5fbb1SRui Ueyama public:
42407320e40SRui Ueyama   ScriptParser(StringRef S, bool B) : ScriptParserBase(S), IsUnderSysroot(B) {}
425f23b2320SGeorge Rimar 
4264a46539cSRui Ueyama   void run();
427f7c5fbb1SRui Ueyama 
428f7c5fbb1SRui Ueyama private:
42952a1509eSRui Ueyama   void addFile(StringRef Path);
43052a1509eSRui Ueyama 
431f7c5fbb1SRui Ueyama   void readAsNeeded();
43290c5099eSDenis Protivensky   void readEntry();
43383f406cfSGeorge Rimar   void readExtern();
434f7c5fbb1SRui Ueyama   void readGroup();
43531aa1f83SRui Ueyama   void readInclude();
436c3794e58SGeorge Rimar   void readNothing() {}
437ee59282bSRui Ueyama   void readOutput();
4389159ce93SDavide Italiano   void readOutputArch();
439f7c5fbb1SRui Ueyama   void readOutputFormat();
440bbe38602SEugene Leviant   void readPhdrs();
44168a39a65SDavide Italiano   void readSearchDir();
4428e3b38abSDenis Protivensky   void readSections();
4438e3b38abSDenis Protivensky 
444113cdec9SRui Ueyama   SymbolAssignment *readAssignment(StringRef Name);
445eda81a1bSEugene Leviant   void readOutputSectionDescription(StringRef OutSec);
446*f71caa2bSRui Ueyama   std::vector<uint8_t> readOutputSectionFiller();
447bbe38602SEugene Leviant   std::vector<StringRef> readOutputSectionPhdrs();
4480659800eSGeorge Rimar   std::unique_ptr<InputSectionDescription> readInputSectionDescription();
4490659800eSGeorge Rimar   void readInputSectionRules(InputSectionDescription *InCmd, bool Keep);
450bbe38602SEugene Leviant   unsigned readPhdrType();
451a31c91b1SEugene Leviant   void readProvide(bool Hidden);
452630c6179SGeorge Rimar   void readAlign(OutputSectionCommand *Cmd);
45303fc010eSGeorge Rimar   void readSort();
454708019c4SRui Ueyama 
455708019c4SRui Ueyama   Expr readExpr();
456708019c4SRui Ueyama   Expr readExpr1(Expr Lhs, int MinPrec);
457708019c4SRui Ueyama   Expr readPrimary();
458708019c4SRui Ueyama   Expr readTernary(Expr Cond);
459708019c4SRui Ueyama   Expr combine(StringRef Op, Expr Lhs, Expr Rhs);
460f7c5fbb1SRui Ueyama 
461c3794e58SGeorge Rimar   const static StringMap<Handler> Cmd;
46207320e40SRui Ueyama   ScriptConfiguration &Opt = *ScriptConfig;
46307320e40SRui Ueyama   StringSaver Saver = {ScriptConfig->Alloc};
46416b0cc9eSSimon Atanasyan   bool IsUnderSysroot;
465f7c5fbb1SRui Ueyama };
466f7c5fbb1SRui Ueyama 
467e0df00b9SRafael Espindola const StringMap<elf::ScriptParser::Handler> elf::ScriptParser::Cmd = {
468c3794e58SGeorge Rimar     {"ENTRY", &ScriptParser::readEntry},
469c3794e58SGeorge Rimar     {"EXTERN", &ScriptParser::readExtern},
470c3794e58SGeorge Rimar     {"GROUP", &ScriptParser::readGroup},
471c3794e58SGeorge Rimar     {"INCLUDE", &ScriptParser::readInclude},
472c3794e58SGeorge Rimar     {"INPUT", &ScriptParser::readGroup},
473c3794e58SGeorge Rimar     {"OUTPUT", &ScriptParser::readOutput},
474c3794e58SGeorge Rimar     {"OUTPUT_ARCH", &ScriptParser::readOutputArch},
475c3794e58SGeorge Rimar     {"OUTPUT_FORMAT", &ScriptParser::readOutputFormat},
476bbe38602SEugene Leviant     {"PHDRS", &ScriptParser::readPhdrs},
477c3794e58SGeorge Rimar     {"SEARCH_DIR", &ScriptParser::readSearchDir},
478c3794e58SGeorge Rimar     {"SECTIONS", &ScriptParser::readSections},
479c3794e58SGeorge Rimar     {";", &ScriptParser::readNothing}};
480c3794e58SGeorge Rimar 
481717677afSRui Ueyama void ScriptParser::run() {
482f7c5fbb1SRui Ueyama   while (!atEOF()) {
483f7c5fbb1SRui Ueyama     StringRef Tok = next();
484c3794e58SGeorge Rimar     if (Handler Fn = Cmd.lookup(Tok))
485c3794e58SGeorge Rimar       (this->*Fn)();
486c3794e58SGeorge Rimar     else
4875761042dSGeorge Rimar       setError("unknown directive: " + Tok);
488f7c5fbb1SRui Ueyama   }
489f7c5fbb1SRui Ueyama }
490f7c5fbb1SRui Ueyama 
491717677afSRui Ueyama void ScriptParser::addFile(StringRef S) {
49216b0cc9eSSimon Atanasyan   if (IsUnderSysroot && S.startswith("/")) {
49316b0cc9eSSimon Atanasyan     SmallString<128> Path;
49416b0cc9eSSimon Atanasyan     (Config->Sysroot + S).toStringRef(Path);
49516b0cc9eSSimon Atanasyan     if (sys::fs::exists(Path)) {
49616b0cc9eSSimon Atanasyan       Driver->addFile(Saver.save(Path.str()));
49716b0cc9eSSimon Atanasyan       return;
49816b0cc9eSSimon Atanasyan     }
49916b0cc9eSSimon Atanasyan   }
50016b0cc9eSSimon Atanasyan 
501f03f3cc1SRui Ueyama   if (sys::path::is_absolute(S)) {
50252a1509eSRui Ueyama     Driver->addFile(S);
50352a1509eSRui Ueyama   } else if (S.startswith("=")) {
50452a1509eSRui Ueyama     if (Config->Sysroot.empty())
50552a1509eSRui Ueyama       Driver->addFile(S.substr(1));
50652a1509eSRui Ueyama     else
50752a1509eSRui Ueyama       Driver->addFile(Saver.save(Config->Sysroot + "/" + S.substr(1)));
50852a1509eSRui Ueyama   } else if (S.startswith("-l")) {
50921eecb4fSRui Ueyama     Driver->addLibrary(S.substr(2));
510a1b8fc3bSSimon Atanasyan   } else if (sys::fs::exists(S)) {
511a1b8fc3bSSimon Atanasyan     Driver->addFile(S);
51252a1509eSRui Ueyama   } else {
51352a1509eSRui Ueyama     std::string Path = findFromSearchPaths(S);
51452a1509eSRui Ueyama     if (Path.empty())
515777f9630SGeorge Rimar       setError("unable to find " + S);
516025d59b1SRui Ueyama     else
51752a1509eSRui Ueyama       Driver->addFile(Saver.save(Path));
51852a1509eSRui Ueyama   }
51952a1509eSRui Ueyama }
52052a1509eSRui Ueyama 
521717677afSRui Ueyama void ScriptParser::readAsNeeded() {
522f7c5fbb1SRui Ueyama   expect("(");
52335da9b6eSRui Ueyama   bool Orig = Config->AsNeeded;
52435da9b6eSRui Ueyama   Config->AsNeeded = true;
525025d59b1SRui Ueyama   while (!Error) {
526f7c5fbb1SRui Ueyama     StringRef Tok = next();
527f7c5fbb1SRui Ueyama     if (Tok == ")")
52835da9b6eSRui Ueyama       break;
52952a1509eSRui Ueyama     addFile(Tok);
530f7c5fbb1SRui Ueyama   }
53135da9b6eSRui Ueyama   Config->AsNeeded = Orig;
532f7c5fbb1SRui Ueyama }
533f7c5fbb1SRui Ueyama 
534717677afSRui Ueyama void ScriptParser::readEntry() {
53590c5099eSDenis Protivensky   // -e <symbol> takes predecence over ENTRY(<symbol>).
53690c5099eSDenis Protivensky   expect("(");
53790c5099eSDenis Protivensky   StringRef Tok = next();
53890c5099eSDenis Protivensky   if (Config->Entry.empty())
53990c5099eSDenis Protivensky     Config->Entry = Tok;
54090c5099eSDenis Protivensky   expect(")");
54190c5099eSDenis Protivensky }
54290c5099eSDenis Protivensky 
543717677afSRui Ueyama void ScriptParser::readExtern() {
54483f406cfSGeorge Rimar   expect("(");
545025d59b1SRui Ueyama   while (!Error) {
54683f406cfSGeorge Rimar     StringRef Tok = next();
54783f406cfSGeorge Rimar     if (Tok == ")")
54883f406cfSGeorge Rimar       return;
54983f406cfSGeorge Rimar     Config->Undefined.push_back(Tok);
55083f406cfSGeorge Rimar   }
55183f406cfSGeorge Rimar }
55283f406cfSGeorge Rimar 
553717677afSRui Ueyama void ScriptParser::readGroup() {
554f7c5fbb1SRui Ueyama   expect("(");
555025d59b1SRui Ueyama   while (!Error) {
556f7c5fbb1SRui Ueyama     StringRef Tok = next();
557f7c5fbb1SRui Ueyama     if (Tok == ")")
558f7c5fbb1SRui Ueyama       return;
559f7c5fbb1SRui Ueyama     if (Tok == "AS_NEEDED") {
560f7c5fbb1SRui Ueyama       readAsNeeded();
561f7c5fbb1SRui Ueyama       continue;
562f7c5fbb1SRui Ueyama     }
56352a1509eSRui Ueyama     addFile(Tok);
564f7c5fbb1SRui Ueyama   }
565f7c5fbb1SRui Ueyama }
566f7c5fbb1SRui Ueyama 
567717677afSRui Ueyama void ScriptParser::readInclude() {
56831aa1f83SRui Ueyama   StringRef Tok = next();
56931aa1f83SRui Ueyama   auto MBOrErr = MemoryBuffer::getFile(Tok);
570025d59b1SRui Ueyama   if (!MBOrErr) {
5715761042dSGeorge Rimar     setError("cannot open " + Tok);
572025d59b1SRui Ueyama     return;
573025d59b1SRui Ueyama   }
57431aa1f83SRui Ueyama   std::unique_ptr<MemoryBuffer> &MB = *MBOrErr;
575a47ee68dSRui Ueyama   StringRef S = Saver.save(MB->getMemBufferRef().getBuffer());
576a47ee68dSRui Ueyama   std::vector<StringRef> V = tokenize(S);
57731aa1f83SRui Ueyama   Tokens.insert(Tokens.begin() + Pos, V.begin(), V.end());
57831aa1f83SRui Ueyama }
57931aa1f83SRui Ueyama 
580717677afSRui Ueyama void ScriptParser::readOutput() {
581ee59282bSRui Ueyama   // -o <file> takes predecence over OUTPUT(<file>).
582ee59282bSRui Ueyama   expect("(");
583ee59282bSRui Ueyama   StringRef Tok = next();
584ee59282bSRui Ueyama   if (Config->OutputFile.empty())
585ee59282bSRui Ueyama     Config->OutputFile = Tok;
586ee59282bSRui Ueyama   expect(")");
587ee59282bSRui Ueyama }
588ee59282bSRui Ueyama 
589717677afSRui Ueyama void ScriptParser::readOutputArch() {
5909159ce93SDavide Italiano   // Error checking only for now.
5919159ce93SDavide Italiano   expect("(");
5929159ce93SDavide Italiano   next();
5939159ce93SDavide Italiano   expect(")");
5949159ce93SDavide Italiano }
5959159ce93SDavide Italiano 
596717677afSRui Ueyama void ScriptParser::readOutputFormat() {
597f7c5fbb1SRui Ueyama   // Error checking only for now.
598f7c5fbb1SRui Ueyama   expect("(");
599f7c5fbb1SRui Ueyama   next();
6006836c618SDavide Italiano   StringRef Tok = next();
6016836c618SDavide Italiano   if (Tok == ")")
6026836c618SDavide Italiano    return;
603025d59b1SRui Ueyama   if (Tok != ",") {
6045761042dSGeorge Rimar     setError("unexpected token: " + Tok);
605025d59b1SRui Ueyama     return;
606025d59b1SRui Ueyama   }
6076836c618SDavide Italiano   next();
6086836c618SDavide Italiano   expect(",");
6096836c618SDavide Italiano   next();
610f7c5fbb1SRui Ueyama   expect(")");
611f7c5fbb1SRui Ueyama }
612f7c5fbb1SRui Ueyama 
613bbe38602SEugene Leviant void ScriptParser::readPhdrs() {
614bbe38602SEugene Leviant   expect("{");
615bbe38602SEugene Leviant   while (!Error && !skip("}")) {
616bbe38602SEugene Leviant     StringRef Tok = next();
617865bf863SEugene Leviant     Opt.PhdrsCommands.push_back({Tok, PT_NULL, false, false, UINT_MAX});
618bbe38602SEugene Leviant     PhdrsCommand &PhdrCmd = Opt.PhdrsCommands.back();
619bbe38602SEugene Leviant 
620bbe38602SEugene Leviant     PhdrCmd.Type = readPhdrType();
621bbe38602SEugene Leviant     do {
622bbe38602SEugene Leviant       Tok = next();
623bbe38602SEugene Leviant       if (Tok == ";")
624bbe38602SEugene Leviant         break;
625bbe38602SEugene Leviant       if (Tok == "FILEHDR")
626bbe38602SEugene Leviant         PhdrCmd.HasFilehdr = true;
627bbe38602SEugene Leviant       else if (Tok == "PHDRS")
628bbe38602SEugene Leviant         PhdrCmd.HasPhdrs = true;
629865bf863SEugene Leviant       else if (Tok == "FLAGS") {
630865bf863SEugene Leviant         expect("(");
631865bf863SEugene Leviant         next().getAsInteger(0, PhdrCmd.Flags);
632865bf863SEugene Leviant         expect(")");
633865bf863SEugene Leviant       } else
634bbe38602SEugene Leviant         setError("unexpected header attribute: " + Tok);
635bbe38602SEugene Leviant     } while (!Error);
636bbe38602SEugene Leviant   }
637bbe38602SEugene Leviant }
638bbe38602SEugene Leviant 
639717677afSRui Ueyama void ScriptParser::readSearchDir() {
64068a39a65SDavide Italiano   expect("(");
64106501920SRafael Espindola   Config->SearchPaths.push_back(next());
64268a39a65SDavide Italiano   expect(")");
64368a39a65SDavide Italiano }
64468a39a65SDavide Italiano 
645717677afSRui Ueyama void ScriptParser::readSections() {
6463de0a330SRui Ueyama   Opt.HasContents = true;
6478e3b38abSDenis Protivensky   expect("{");
648652852c5SGeorge Rimar   while (!Error && !skip("}")) {
649113cdec9SRui Ueyama     StringRef Tok = next();
65030835ea4SGeorge Rimar     if (peek() == "=" || peek() == "+=") {
651113cdec9SRui Ueyama       readAssignment(Tok);
652113cdec9SRui Ueyama       expect(";");
653113cdec9SRui Ueyama     } else if (Tok == "PROVIDE") {
654a31c91b1SEugene Leviant       readProvide(false);
655708019c4SRui Ueyama     } else if (Tok == "PROVIDE_HIDDEN") {
656a31c91b1SEugene Leviant       readProvide(true);
657708019c4SRui Ueyama     } else {
658eda81a1bSEugene Leviant       readOutputSectionDescription(Tok);
6598e3b38abSDenis Protivensky     }
660652852c5SGeorge Rimar   }
661708019c4SRui Ueyama }
6628e3b38abSDenis Protivensky 
663708019c4SRui Ueyama static int precedence(StringRef Op) {
664708019c4SRui Ueyama   return StringSwitch<int>(Op)
665708019c4SRui Ueyama       .Case("*", 4)
666708019c4SRui Ueyama       .Case("/", 4)
667708019c4SRui Ueyama       .Case("+", 3)
668708019c4SRui Ueyama       .Case("-", 3)
669708019c4SRui Ueyama       .Case("<", 2)
670708019c4SRui Ueyama       .Case(">", 2)
671708019c4SRui Ueyama       .Case(">=", 2)
672708019c4SRui Ueyama       .Case("<=", 2)
673708019c4SRui Ueyama       .Case("==", 2)
674708019c4SRui Ueyama       .Case("!=", 2)
675708019c4SRui Ueyama       .Case("&", 1)
676708019c4SRui Ueyama       .Default(-1);
677708019c4SRui Ueyama }
678708019c4SRui Ueyama 
6790659800eSGeorge Rimar void ScriptParser::readInputSectionRules(InputSectionDescription *InCmd, bool Keep) {
6800659800eSGeorge Rimar   InCmd->FilePattern = next();
6810ed42b0cSDavide Italiano   expect("(");
682e7282797SDavide Italiano 
683e7282797SDavide Italiano   if (skip("EXCLUDE_FILE")) {
684e7282797SDavide Italiano     expect("(");
685e7282797SDavide Italiano     while (!Error && !skip(")"))
686e7282797SDavide Italiano       InCmd->ExcludedFiles.push_back(next());
687e7282797SDavide Italiano   }
688e7282797SDavide Italiano 
6890659800eSGeorge Rimar   while (!Error && !skip(")")) {
6900659800eSGeorge Rimar     if (Keep)
6910659800eSGeorge Rimar       Opt.KeptSections.push_back(peek());
6920659800eSGeorge Rimar     InCmd->SectionPatterns.push_back(next());
6930659800eSGeorge Rimar   }
6940659800eSGeorge Rimar }
6950659800eSGeorge Rimar 
6960659800eSGeorge Rimar std::unique_ptr<InputSectionDescription>
6970659800eSGeorge Rimar ScriptParser::readInputSectionDescription() {
698352eac37SGeorge Rimar   auto InCmd = llvm::make_unique<InputSectionDescription>();
6990659800eSGeorge Rimar 
7000659800eSGeorge Rimar   // Input section wildcard can be surrounded by KEEP.
7010659800eSGeorge Rimar   // https://sourceware.org/binutils/docs/ld/Input-Section-Keep.html#Input-Section-Keep
7020659800eSGeorge Rimar   if (skip("KEEP")) {
703e7282797SDavide Italiano     expect("(");
7040659800eSGeorge Rimar     readInputSectionRules(InCmd.get(), true);
7050ed42b0cSDavide Italiano     expect(")");
7060659800eSGeorge Rimar   } else {
7070659800eSGeorge Rimar     readInputSectionRules(InCmd.get(), false);
7080659800eSGeorge Rimar   }
7090659800eSGeorge Rimar 
7100659800eSGeorge Rimar   return InCmd;
7110ed42b0cSDavide Italiano }
7120ed42b0cSDavide Italiano 
713630c6179SGeorge Rimar void ScriptParser::readAlign(OutputSectionCommand *Cmd) {
714630c6179SGeorge Rimar   expect("(");
715630c6179SGeorge Rimar   Cmd->AlignExpr = readExpr();
716630c6179SGeorge Rimar   expect(")");
717630c6179SGeorge Rimar }
718630c6179SGeorge Rimar 
71903fc010eSGeorge Rimar void ScriptParser::readSort() {
72003fc010eSGeorge Rimar   expect("(");
72103fc010eSGeorge Rimar   expect("CONSTRUCTORS");
72203fc010eSGeorge Rimar   expect(")");
72303fc010eSGeorge Rimar }
72403fc010eSGeorge Rimar 
725eda81a1bSEugene Leviant void ScriptParser::readOutputSectionDescription(StringRef OutSec) {
726076fe157SGeorge Rimar   OutputSectionCommand *Cmd = new OutputSectionCommand(OutSec);
727076fe157SGeorge Rimar   Opt.Commands.emplace_back(Cmd);
72858e5c4dcSGeorge Rimar 
72958e5c4dcSGeorge Rimar   // Read an address expression.
73058e5c4dcSGeorge Rimar   // https://sourceware.org/binutils/docs/ld/Output-Section-Address.html#Output-Section-Address
73158e5c4dcSGeorge Rimar   if (peek() != ":")
73258e5c4dcSGeorge Rimar     Cmd->AddrExpr = readExpr();
73358e5c4dcSGeorge Rimar 
7348e3b38abSDenis Protivensky   expect(":");
735246f681eSDavide Italiano 
736630c6179SGeorge Rimar   if (skip("ALIGN"))
737630c6179SGeorge Rimar     readAlign(Cmd);
738630c6179SGeorge Rimar 
739246f681eSDavide Italiano   // Parse constraints.
740246f681eSDavide Italiano   if (skip("ONLY_IF_RO"))
741efc4066bSRui Ueyama     Cmd->Constraint = ConstraintKind::ReadOnly;
742246f681eSDavide Italiano   if (skip("ONLY_IF_RW"))
743efc4066bSRui Ueyama     Cmd->Constraint = ConstraintKind::ReadWrite;
7448e3b38abSDenis Protivensky   expect("{");
7458ec77e64SRui Ueyama 
746025d59b1SRui Ueyama   while (!Error && !skip("}")) {
747f586ff7eSGeorge Rimar     if (peek().startswith("*") || peek() == "KEEP") {
7480659800eSGeorge Rimar       Cmd->Commands.push_back(readInputSectionDescription());
7490659800eSGeorge Rimar       continue;
7500659800eSGeorge Rimar     }
7510659800eSGeorge Rimar 
752481c2ce6SGeorge Rimar     StringRef Tok = next();
7530659800eSGeorge Rimar     if (Tok == "PROVIDE") {
754054a6796SDavide Italiano       readProvide(false);
755054a6796SDavide Italiano     } else if (Tok == "PROVIDE_HIDDEN") {
756054a6796SDavide Italiano       readProvide(true);
75703fc010eSGeorge Rimar     } else if (Tok == "SORT") {
75803fc010eSGeorge Rimar       readSort();
759481c2ce6SGeorge Rimar     } else {
760777f9630SGeorge Rimar       setError("unknown command " + Tok);
761481c2ce6SGeorge Rimar     }
7628e3b38abSDenis Protivensky   }
763076fe157SGeorge Rimar   Cmd->Phdrs = readOutputSectionPhdrs();
764*f71caa2bSRui Ueyama   Cmd->Filler = readOutputSectionFiller();
765*f71caa2bSRui Ueyama }
7668ec77e64SRui Ueyama 
767*f71caa2bSRui Ueyama std::vector<uint8_t> ScriptParser::readOutputSectionFiller() {
768e2ee72b5SGeorge Rimar   StringRef Tok = peek();
769*f71caa2bSRui Ueyama   if (!Tok.startswith("="))
770*f71caa2bSRui Ueyama     return {};
771e2ee72b5SGeorge Rimar   if (!Tok.startswith("=0x")) {
7723ed2f069SRui Ueyama     setError("filler should be a hexadecimal value");
773*f71caa2bSRui Ueyama     return {};
774e2ee72b5SGeorge Rimar   }
7753e808976SRui Ueyama   Tok = Tok.substr(3);
776e2ee72b5SGeorge Rimar   next();
777*f71caa2bSRui Ueyama   return parseHex(Tok);
7788e3b38abSDenis Protivensky }
7798e3b38abSDenis Protivensky 
780a31c91b1SEugene Leviant void ScriptParser::readProvide(bool Hidden) {
781a31c91b1SEugene Leviant   expect("(");
782174e0a16SRui Ueyama   SymbolAssignment *Cmd = readAssignment(next());
783174e0a16SRui Ueyama   Cmd->Provide = true;
784174e0a16SRui Ueyama   Cmd->Hidden = Hidden;
785a31c91b1SEugene Leviant   expect(")");
786a31c91b1SEugene Leviant   expect(";");
787eda81a1bSEugene Leviant }
788eda81a1bSEugene Leviant 
78930835ea4SGeorge Rimar static uint64_t getSymbolValue(StringRef S, uint64_t Dot) {
79030835ea4SGeorge Rimar   if (S == ".")
79130835ea4SGeorge Rimar     return Dot;
792a31c91b1SEugene Leviant 
793a9c5a528SGeorge Rimar   switch (Config->EKind) {
794a9c5a528SGeorge Rimar   case ELF32LEKind:
795a9c5a528SGeorge Rimar     if (SymbolBody *B = Symtab<ELF32LE>::X->find(S))
796a9c5a528SGeorge Rimar       return B->getVA<ELF32LE>();
797a9c5a528SGeorge Rimar     break;
798a9c5a528SGeorge Rimar   case ELF32BEKind:
799a9c5a528SGeorge Rimar     if (SymbolBody *B = Symtab<ELF32BE>::X->find(S))
800a9c5a528SGeorge Rimar       return B->getVA<ELF32BE>();
801a9c5a528SGeorge Rimar     break;
802a9c5a528SGeorge Rimar   case ELF64LEKind:
803a9c5a528SGeorge Rimar     if (SymbolBody *B = Symtab<ELF64LE>::X->find(S))
804a9c5a528SGeorge Rimar       return B->getVA<ELF64LE>();
805a9c5a528SGeorge Rimar     break;
806a9c5a528SGeorge Rimar   case ELF64BEKind:
807a9c5a528SGeorge Rimar     if (SymbolBody *B = Symtab<ELF64BE>::X->find(S))
808a9c5a528SGeorge Rimar       return B->getVA<ELF64BE>();
809a9c5a528SGeorge Rimar     break;
8106930a6dcSGeorge Rimar   default:
811b567b628SGeorge Rimar     llvm_unreachable("unsupported target");
812a9c5a528SGeorge Rimar   }
813a9c5a528SGeorge Rimar   error("symbol not found: " + S);
814a9c5a528SGeorge Rimar   return 0;
815a9c5a528SGeorge Rimar }
816a9c5a528SGeorge Rimar 
81730835ea4SGeorge Rimar SymbolAssignment *ScriptParser::readAssignment(StringRef Name) {
81830835ea4SGeorge Rimar   StringRef Op = next();
81930835ea4SGeorge Rimar   assert(Op == "=" || Op == "+=");
82030835ea4SGeorge Rimar   Expr E = readExpr();
82130835ea4SGeorge Rimar   if (Op == "+=")
82230835ea4SGeorge Rimar     E = [=](uint64_t Dot) { return getSymbolValue(Name, Dot) + E(Dot); };
82330835ea4SGeorge Rimar   auto *Cmd = new SymbolAssignment(Name, E);
82430835ea4SGeorge Rimar   Opt.Commands.emplace_back(Cmd);
82530835ea4SGeorge Rimar   return Cmd;
82630835ea4SGeorge Rimar }
82730835ea4SGeorge Rimar 
82830835ea4SGeorge Rimar // This is an operator-precedence parser to parse a linker
82930835ea4SGeorge Rimar // script expression.
83030835ea4SGeorge Rimar Expr ScriptParser::readExpr() { return readExpr1(readPrimary(), 0); }
83130835ea4SGeorge Rimar 
832708019c4SRui Ueyama // This is a part of the operator-precedence parser. This function
833708019c4SRui Ueyama // assumes that the remaining token stream starts with an operator.
834708019c4SRui Ueyama Expr ScriptParser::readExpr1(Expr Lhs, int MinPrec) {
835708019c4SRui Ueyama   while (!atEOF() && !Error) {
836708019c4SRui Ueyama     // Read an operator and an expression.
837708019c4SRui Ueyama     StringRef Op1 = peek();
838708019c4SRui Ueyama     if (Op1 == "?")
839708019c4SRui Ueyama       return readTernary(Lhs);
840708019c4SRui Ueyama     if (precedence(Op1) < MinPrec)
841a31c91b1SEugene Leviant       break;
842a31c91b1SEugene Leviant     next();
843708019c4SRui Ueyama     Expr Rhs = readPrimary();
844708019c4SRui Ueyama 
845708019c4SRui Ueyama     // Evaluate the remaining part of the expression first if the
846708019c4SRui Ueyama     // next operator has greater precedence than the previous one.
847708019c4SRui Ueyama     // For example, if we have read "+" and "3", and if the next
848708019c4SRui Ueyama     // operator is "*", then we'll evaluate 3 * ... part first.
849708019c4SRui Ueyama     while (!atEOF()) {
850708019c4SRui Ueyama       StringRef Op2 = peek();
851708019c4SRui Ueyama       if (precedence(Op2) <= precedence(Op1))
852eda81a1bSEugene Leviant         break;
853708019c4SRui Ueyama       Rhs = readExpr1(Rhs, precedence(Op2));
854eda81a1bSEugene Leviant     }
855708019c4SRui Ueyama 
856708019c4SRui Ueyama     Lhs = combine(Op1, Lhs, Rhs);
857708019c4SRui Ueyama   }
858708019c4SRui Ueyama   return Lhs;
859708019c4SRui Ueyama }
860708019c4SRui Ueyama 
861708019c4SRui Ueyama uint64_t static getConstant(StringRef S) {
862708019c4SRui Ueyama   if (S == "COMMONPAGESIZE" || S == "MAXPAGESIZE")
863708019c4SRui Ueyama     return Target->PageSize;
864708019c4SRui Ueyama   error("unknown constant: " + S);
865708019c4SRui Ueyama   return 0;
866708019c4SRui Ueyama }
867708019c4SRui Ueyama 
868708019c4SRui Ueyama Expr ScriptParser::readPrimary() {
869708019c4SRui Ueyama   StringRef Tok = next();
870708019c4SRui Ueyama 
871708019c4SRui Ueyama   if (Tok == "(") {
872708019c4SRui Ueyama     Expr E = readExpr();
873708019c4SRui Ueyama     expect(")");
874708019c4SRui Ueyama     return E;
875708019c4SRui Ueyama   }
876708019c4SRui Ueyama 
877708019c4SRui Ueyama   // Built-in functions are parsed here.
878708019c4SRui Ueyama   // https://sourceware.org/binutils/docs/ld/Builtin-Functions.html.
879708019c4SRui Ueyama   if (Tok == "ALIGN") {
880708019c4SRui Ueyama     expect("(");
881708019c4SRui Ueyama     Expr E = readExpr();
882708019c4SRui Ueyama     expect(")");
883708019c4SRui Ueyama     return [=](uint64_t Dot) { return alignTo(Dot, E(Dot)); };
884708019c4SRui Ueyama   }
885708019c4SRui Ueyama   if (Tok == "CONSTANT") {
886708019c4SRui Ueyama     expect("(");
887708019c4SRui Ueyama     StringRef Tok = next();
888708019c4SRui Ueyama     expect(")");
889708019c4SRui Ueyama     return [=](uint64_t Dot) { return getConstant(Tok); };
890708019c4SRui Ueyama   }
89154c145ceSRafael Espindola   if (Tok == "SEGMENT_START") {
89254c145ceSRafael Espindola     expect("(");
89354c145ceSRafael Espindola     next();
89454c145ceSRafael Espindola     expect(",");
89554c145ceSRafael Espindola     uint64_t Val;
89654c145ceSRafael Espindola     next().getAsInteger(0, Val);
89754c145ceSRafael Espindola     expect(")");
89854c145ceSRafael Espindola     return [=](uint64_t Dot) { return Val; };
89954c145ceSRafael Espindola   }
900708019c4SRui Ueyama   if (Tok == "DATA_SEGMENT_ALIGN") {
901708019c4SRui Ueyama     expect("(");
902708019c4SRui Ueyama     Expr E = readExpr();
903708019c4SRui Ueyama     expect(",");
904708019c4SRui Ueyama     readExpr();
905708019c4SRui Ueyama     expect(")");
906f7791bb9SRui Ueyama     return [=](uint64_t Dot) { return alignTo(Dot, E(Dot)); };
907708019c4SRui Ueyama   }
908708019c4SRui Ueyama   if (Tok == "DATA_SEGMENT_END") {
909708019c4SRui Ueyama     expect("(");
910708019c4SRui Ueyama     expect(".");
911708019c4SRui Ueyama     expect(")");
912708019c4SRui Ueyama     return [](uint64_t Dot) { return Dot; };
913708019c4SRui Ueyama   }
914276b4e64SGeorge Rimar   // GNU linkers implements more complicated logic to handle
915276b4e64SGeorge Rimar   // DATA_SEGMENT_RELRO_END. We instead ignore the arguments and just align to
916276b4e64SGeorge Rimar   // the next page boundary for simplicity.
917276b4e64SGeorge Rimar   if (Tok == "DATA_SEGMENT_RELRO_END") {
918276b4e64SGeorge Rimar     expect("(");
919276b4e64SGeorge Rimar     next();
920276b4e64SGeorge Rimar     expect(",");
921276b4e64SGeorge Rimar     readExpr();
922276b4e64SGeorge Rimar     expect(")");
923276b4e64SGeorge Rimar     return [](uint64_t Dot) { return alignTo(Dot, Target->PageSize); };
924276b4e64SGeorge Rimar   }
925708019c4SRui Ueyama 
926a9c5a528SGeorge Rimar   // Parse a symbol name or a number literal.
927708019c4SRui Ueyama   uint64_t V = 0;
928a9c5a528SGeorge Rimar   if (Tok.getAsInteger(0, V)) {
92930835ea4SGeorge Rimar     if (Tok != "." && !isValidCIdentifier(Tok))
930708019c4SRui Ueyama       setError("malformed number: " + Tok);
93130835ea4SGeorge Rimar     return [=](uint64_t Dot) { return getSymbolValue(Tok, Dot); };
932a9c5a528SGeorge Rimar   }
933708019c4SRui Ueyama   return [=](uint64_t Dot) { return V; };
934708019c4SRui Ueyama }
935708019c4SRui Ueyama 
936708019c4SRui Ueyama Expr ScriptParser::readTernary(Expr Cond) {
937708019c4SRui Ueyama   next();
938708019c4SRui Ueyama   Expr L = readExpr();
939708019c4SRui Ueyama   expect(":");
940708019c4SRui Ueyama   Expr R = readExpr();
941708019c4SRui Ueyama   return [=](uint64_t Dot) { return Cond(Dot) ? L(Dot) : R(Dot); };
942708019c4SRui Ueyama }
943708019c4SRui Ueyama 
944708019c4SRui Ueyama Expr ScriptParser::combine(StringRef Op, Expr L, Expr R) {
945708019c4SRui Ueyama   if (Op == "*")
946708019c4SRui Ueyama     return [=](uint64_t Dot) { return L(Dot) * R(Dot); };
947708019c4SRui Ueyama   if (Op == "/") {
948708019c4SRui Ueyama     return [=](uint64_t Dot) -> uint64_t {
949708019c4SRui Ueyama       uint64_t RHS = R(Dot);
950708019c4SRui Ueyama       if (RHS == 0) {
951708019c4SRui Ueyama         error("division by zero");
952708019c4SRui Ueyama         return 0;
953708019c4SRui Ueyama       }
954708019c4SRui Ueyama       return L(Dot) / RHS;
955708019c4SRui Ueyama     };
956708019c4SRui Ueyama   }
957708019c4SRui Ueyama   if (Op == "+")
958708019c4SRui Ueyama     return [=](uint64_t Dot) { return L(Dot) + R(Dot); };
959708019c4SRui Ueyama   if (Op == "-")
960708019c4SRui Ueyama     return [=](uint64_t Dot) { return L(Dot) - R(Dot); };
961708019c4SRui Ueyama   if (Op == "<")
962708019c4SRui Ueyama     return [=](uint64_t Dot) { return L(Dot) < R(Dot); };
963708019c4SRui Ueyama   if (Op == ">")
964708019c4SRui Ueyama     return [=](uint64_t Dot) { return L(Dot) > R(Dot); };
965708019c4SRui Ueyama   if (Op == ">=")
966708019c4SRui Ueyama     return [=](uint64_t Dot) { return L(Dot) >= R(Dot); };
967708019c4SRui Ueyama   if (Op == "<=")
968708019c4SRui Ueyama     return [=](uint64_t Dot) { return L(Dot) <= R(Dot); };
969708019c4SRui Ueyama   if (Op == "==")
970708019c4SRui Ueyama     return [=](uint64_t Dot) { return L(Dot) == R(Dot); };
971708019c4SRui Ueyama   if (Op == "!=")
972708019c4SRui Ueyama     return [=](uint64_t Dot) { return L(Dot) != R(Dot); };
973708019c4SRui Ueyama   if (Op == "&")
974708019c4SRui Ueyama     return [=](uint64_t Dot) { return L(Dot) & R(Dot); };
975708019c4SRui Ueyama   llvm_unreachable("invalid operator");
976eda81a1bSEugene Leviant }
977eda81a1bSEugene Leviant 
978bbe38602SEugene Leviant std::vector<StringRef> ScriptParser::readOutputSectionPhdrs() {
979bbe38602SEugene Leviant   std::vector<StringRef> Phdrs;
980bbe38602SEugene Leviant   while (!Error && peek().startswith(":")) {
981bbe38602SEugene Leviant     StringRef Tok = next();
982bbe38602SEugene Leviant     Tok = (Tok.size() == 1) ? next() : Tok.substr(1);
983bbe38602SEugene Leviant     if (Tok.empty()) {
984bbe38602SEugene Leviant       setError("section header name is empty");
985bbe38602SEugene Leviant       break;
986bbe38602SEugene Leviant     }
987bbe38602SEugene Leviant     Phdrs.push_back(Tok);
988bbe38602SEugene Leviant   }
989bbe38602SEugene Leviant   return Phdrs;
990bbe38602SEugene Leviant }
991bbe38602SEugene Leviant 
992bbe38602SEugene Leviant unsigned ScriptParser::readPhdrType() {
993bbe38602SEugene Leviant   StringRef Tok = next();
994b0f6c590SRui Ueyama   unsigned Ret = StringSwitch<unsigned>(Tok)
995b0f6c590SRui Ueyama       .Case("PT_NULL", PT_NULL)
996b0f6c590SRui Ueyama       .Case("PT_LOAD", PT_LOAD)
997b0f6c590SRui Ueyama       .Case("PT_DYNAMIC", PT_DYNAMIC)
998b0f6c590SRui Ueyama       .Case("PT_INTERP", PT_INTERP)
999b0f6c590SRui Ueyama       .Case("PT_NOTE", PT_NOTE)
1000b0f6c590SRui Ueyama       .Case("PT_SHLIB", PT_SHLIB)
1001b0f6c590SRui Ueyama       .Case("PT_PHDR", PT_PHDR)
1002b0f6c590SRui Ueyama       .Case("PT_TLS", PT_TLS)
1003b0f6c590SRui Ueyama       .Case("PT_GNU_EH_FRAME", PT_GNU_EH_FRAME)
1004b0f6c590SRui Ueyama       .Case("PT_GNU_STACK", PT_GNU_STACK)
1005b0f6c590SRui Ueyama       .Case("PT_GNU_RELRO", PT_GNU_RELRO)
1006b0f6c590SRui Ueyama       .Default(-1);
1007bbe38602SEugene Leviant 
1008b0f6c590SRui Ueyama   if (Ret == (unsigned)-1) {
1009b0f6c590SRui Ueyama     setError("invalid program header type: " + Tok);
1010b0f6c590SRui Ueyama     return PT_NULL;
1011b0f6c590SRui Ueyama   }
1012b0f6c590SRui Ueyama   return Ret;
1013bbe38602SEugene Leviant }
1014bbe38602SEugene Leviant 
101516b0cc9eSSimon Atanasyan static bool isUnderSysroot(StringRef Path) {
101616b0cc9eSSimon Atanasyan   if (Config->Sysroot == "")
101716b0cc9eSSimon Atanasyan     return false;
101816b0cc9eSSimon Atanasyan   for (; !Path.empty(); Path = sys::path::parent_path(Path))
101916b0cc9eSSimon Atanasyan     if (sys::fs::equivalent(Config->Sysroot, Path))
102016b0cc9eSSimon Atanasyan       return true;
102116b0cc9eSSimon Atanasyan   return false;
102216b0cc9eSSimon Atanasyan }
102316b0cc9eSSimon Atanasyan 
102407320e40SRui Ueyama // Entry point.
102507320e40SRui Ueyama void elf::readLinkerScript(MemoryBufferRef MB) {
102616b0cc9eSSimon Atanasyan   StringRef Path = MB.getBufferIdentifier();
102707320e40SRui Ueyama   ScriptParser(MB.getBuffer(), isUnderSysroot(Path)).run();
1028f7c5fbb1SRui Ueyama }
10291ebc8ed7SRui Ueyama 
103007320e40SRui Ueyama template class elf::LinkerScript<ELF32LE>;
103107320e40SRui Ueyama template class elf::LinkerScript<ELF32BE>;
103207320e40SRui Ueyama template class elf::LinkerScript<ELF64LE>;
103307320e40SRui Ueyama template class elf::LinkerScript<ELF64BE>;
1034