1f7c5fbb1SRui Ueyama //===- LinkerScript.cpp ---------------------------------------------------===//
2f7c5fbb1SRui Ueyama //
3f7c5fbb1SRui Ueyama //                             The LLVM Linker
4f7c5fbb1SRui Ueyama //
5f7c5fbb1SRui Ueyama // This file is distributed under the University of Illinois Open Source
6f7c5fbb1SRui Ueyama // License. See LICENSE.TXT for details.
7f7c5fbb1SRui Ueyama //
8f7c5fbb1SRui Ueyama //===----------------------------------------------------------------------===//
9f7c5fbb1SRui Ueyama //
10f7c5fbb1SRui Ueyama // This file contains the parser/evaluator of the linker script.
11629e0aa5SRui Ueyama // It parses a linker script and write the result to Config or ScriptConfig
12629e0aa5SRui Ueyama // objects.
13629e0aa5SRui Ueyama //
14629e0aa5SRui Ueyama // If SECTIONS command is used, a ScriptConfig contains an AST
15629e0aa5SRui Ueyama // of the command which will later be consumed by createSections() and
16629e0aa5SRui Ueyama // assignAddresses().
17f7c5fbb1SRui Ueyama //
18f7c5fbb1SRui Ueyama //===----------------------------------------------------------------------===//
19f7c5fbb1SRui Ueyama 
20717677afSRui Ueyama #include "LinkerScript.h"
21f7c5fbb1SRui Ueyama #include "Config.h"
22f7c5fbb1SRui Ueyama #include "Driver.h"
231ebc8ed7SRui Ueyama #include "InputSection.h"
24652852c5SGeorge Rimar #include "OutputSections.h"
25e77b5bf6SAdhemerval Zanella #include "ScriptParser.h"
2693c9af42SRui Ueyama #include "Strings.h"
27eda81a1bSEugene Leviant #include "Symbols.h"
28f7c5fbb1SRui Ueyama #include "SymbolTable.h"
29467c4d55SEugene Leviant #include "Target.h"
30bbe38602SEugene Leviant #include "Writer.h"
31960504b9SRui Ueyama #include "llvm/ADT/StringSwitch.h"
32652852c5SGeorge Rimar #include "llvm/Support/ELF.h"
33f7c5fbb1SRui Ueyama #include "llvm/Support/FileSystem.h"
34f7c5fbb1SRui Ueyama #include "llvm/Support/MemoryBuffer.h"
35f03f3cc1SRui Ueyama #include "llvm/Support/Path.h"
36a47ee68dSRui Ueyama #include "llvm/Support/StringSaver.h"
37f7c5fbb1SRui Ueyama 
38f7c5fbb1SRui Ueyama using namespace llvm;
39652852c5SGeorge Rimar using namespace llvm::ELF;
401ebc8ed7SRui Ueyama using namespace llvm::object;
41f7c5fbb1SRui Ueyama using namespace lld;
42e0df00b9SRafael Espindola using namespace lld::elf;
43f7c5fbb1SRui Ueyama 
44884e786dSGeorge Rimar LinkerScriptBase *elf::ScriptBase;
4507320e40SRui Ueyama ScriptConfiguration *elf::ScriptConfig;
46717677afSRui Ueyama 
476c55f0e3SGeorge Rimar template <class ELFT> static void addRegular(SymbolAssignment *Cmd) {
481602421cSRui Ueyama   Symbol *Sym = Symtab<ELFT>::X->addRegular(Cmd->Name, STB_GLOBAL, STV_DEFAULT);
491602421cSRui Ueyama   Sym->Visibility = Cmd->Hidden ? STV_HIDDEN : STV_DEFAULT;
501602421cSRui Ueyama   Cmd->Sym = Sym->body();
5120d03194SEugene Leviant 
5220d03194SEugene Leviant   // If we have no SECTIONS then we don't have '.' and don't call
5320d03194SEugene Leviant   // assignAddresses(). We calculate symbol value immediately in this case.
5420d03194SEugene Leviant   if (!ScriptConfig->HasSections)
5520d03194SEugene Leviant     cast<DefinedRegular<ELFT>>(Cmd->Sym)->Value = Cmd->Expression(0);
56ceabe80eSEugene Leviant }
57ceabe80eSEugene Leviant 
580c70d3ccSRui Ueyama template <class ELFT> static void addSynthetic(SymbolAssignment *Cmd) {
59e1937bb5SGeorge Rimar   Symbol *Sym = Symtab<ELFT>::X->addSynthetic(
60e1937bb5SGeorge Rimar       Cmd->Name, nullptr, 0, Cmd->Hidden ? STV_HIDDEN : STV_DEFAULT);
611602421cSRui Ueyama   Cmd->Sym = Sym->body();
62ceabe80eSEugene Leviant }
63ceabe80eSEugene Leviant 
64db741e72SEugene Leviant template <class ELFT> static void addSymbol(SymbolAssignment *Cmd) {
65db741e72SEugene Leviant   if (Cmd->IsAbsolute)
66db741e72SEugene Leviant     addRegular<ELFT>(Cmd);
67db741e72SEugene Leviant   else
68db741e72SEugene Leviant     addSynthetic<ELFT>(Cmd);
69db741e72SEugene Leviant }
701602421cSRui Ueyama // If a symbol was in PROVIDE(), we need to define it only when
711602421cSRui Ueyama // it is an undefined symbol.
721602421cSRui Ueyama template <class ELFT> static bool shouldDefine(SymbolAssignment *Cmd) {
731602421cSRui Ueyama   if (Cmd->Name == ".")
74ceabe80eSEugene Leviant     return false;
751602421cSRui Ueyama   if (!Cmd->Provide)
76ceabe80eSEugene Leviant     return true;
771602421cSRui Ueyama   SymbolBody *B = Symtab<ELFT>::X->find(Cmd->Name);
781602421cSRui Ueyama   return B && B->isUndefined();
79ceabe80eSEugene Leviant }
80ceabe80eSEugene Leviant 
81076fe157SGeorge Rimar bool SymbolAssignment::classof(const BaseCommand *C) {
82076fe157SGeorge Rimar   return C->Kind == AssignmentKind;
83076fe157SGeorge Rimar }
84076fe157SGeorge Rimar 
85076fe157SGeorge Rimar bool OutputSectionCommand::classof(const BaseCommand *C) {
86076fe157SGeorge Rimar   return C->Kind == OutputSectionKind;
87076fe157SGeorge Rimar }
88076fe157SGeorge Rimar 
89eea3114fSGeorge Rimar bool InputSectionDescription::classof(const BaseCommand *C) {
90eea3114fSGeorge Rimar   return C->Kind == InputSectionKind;
91eea3114fSGeorge Rimar }
92eea3114fSGeorge Rimar 
93eefa758eSGeorge Rimar bool AssertCommand::classof(const BaseCommand *C) {
94eefa758eSGeorge Rimar   return C->Kind == AssertKind;
95eefa758eSGeorge Rimar }
96eefa758eSGeorge Rimar 
9736a153cdSRui Ueyama template <class ELFT> static bool isDiscarded(InputSectionBase<ELFT> *S) {
98eea3114fSGeorge Rimar   return !S || !S->Live;
99717677afSRui Ueyama }
100717677afSRui Ueyama 
101f34d0e08SRui Ueyama template <class ELFT> LinkerScript<ELFT>::LinkerScript() {}
102f34d0e08SRui Ueyama template <class ELFT> LinkerScript<ELFT>::~LinkerScript() {}
103f34d0e08SRui Ueyama 
10407320e40SRui Ueyama template <class ELFT>
10507320e40SRui Ueyama bool LinkerScript<ELFT>::shouldKeep(InputSectionBase<ELFT> *S) {
106c91930a1SGeorge Rimar   for (Regex *Re : Opt.KeptSections)
107042a3f20SRafael Espindola     if (Re->match(S->Name))
108eea3114fSGeorge Rimar       return true;
109eea3114fSGeorge Rimar   return false;
110eea3114fSGeorge Rimar }
111eea3114fSGeorge Rimar 
1123ff27f49SRui Ueyama // We need to use const_cast because match() is not a const function.
1133ff27f49SRui Ueyama // This function encapsulates that ugliness.
1143ff27f49SRui Ueyama static bool match(const Regex &Re, StringRef S) {
1153ff27f49SRui Ueyama   return const_cast<Regex &>(Re).match(S);
1160659800eSGeorge Rimar }
1170659800eSGeorge Rimar 
118575208caSGeorge Rimar static bool comparePriority(InputSectionData *A, InputSectionData *B) {
119575208caSGeorge Rimar   return getPriority(A->Name) < getPriority(B->Name);
120575208caSGeorge Rimar }
121575208caSGeorge Rimar 
122c0028d3dSRafael Espindola static bool compareName(InputSectionData *A, InputSectionData *B) {
123042a3f20SRafael Espindola   return A->Name < B->Name;
1240702c4e8SGeorge Rimar }
125742c3836SRui Ueyama 
126c0028d3dSRafael Espindola static bool compareAlignment(InputSectionData *A, InputSectionData *B) {
127742c3836SRui Ueyama   // ">" is not a mistake. Larger alignments are placed before smaller
128742c3836SRui Ueyama   // alignments in order to reduce the amount of padding necessary.
129742c3836SRui Ueyama   // This is compatible with GNU.
130742c3836SRui Ueyama   return A->Alignment > B->Alignment;
131742c3836SRui Ueyama }
132742c3836SRui Ueyama 
133c0028d3dSRafael Espindola static std::function<bool(InputSectionData *, InputSectionData *)>
134be394db3SGeorge Rimar getComparator(SortSectionPolicy K) {
135be394db3SGeorge Rimar   switch (K) {
136be394db3SGeorge Rimar   case SortSectionPolicy::Alignment:
137c0028d3dSRafael Espindola     return compareAlignment;
138be394db3SGeorge Rimar   case SortSectionPolicy::Name:
139be394db3SGeorge Rimar     return compareName;
140be394db3SGeorge Rimar   case SortSectionPolicy::Priority:
141be394db3SGeorge Rimar     return comparePriority;
142be394db3SGeorge Rimar   default:
143be394db3SGeorge Rimar     llvm_unreachable("unknown sort policy");
144be394db3SGeorge Rimar   }
145742c3836SRui Ueyama }
1460702c4e8SGeorge Rimar 
1478f66df92SGeorge Rimar static bool checkConstraint(uint64_t Flags, ConstraintKind Kind) {
1488f66df92SGeorge Rimar   bool RO = (Kind == ConstraintKind::ReadOnly);
1498f66df92SGeorge Rimar   bool RW = (Kind == ConstraintKind::ReadWrite);
1508f66df92SGeorge Rimar   bool Writable = Flags & SHF_WRITE;
151adcdb664SRui Ueyama   return !(RO && Writable) && !(RW && !Writable);
1528f66df92SGeorge Rimar }
1538f66df92SGeorge Rimar 
15448c3f1ceSRui Ueyama template <class ELFT>
155e71a3f8aSRafael Espindola static bool matchConstraints(ArrayRef<InputSectionBase<ELFT> *> Sections,
15606ae6836SGeorge Rimar                              ConstraintKind Kind) {
1578f66df92SGeorge Rimar   if (Kind == ConstraintKind::NoConstraint)
1588f66df92SGeorge Rimar     return true;
159d3190795SRafael Espindola   return llvm::all_of(Sections, [=](InputSectionData *Sec2) {
160d3190795SRafael Espindola     auto *Sec = static_cast<InputSectionBase<ELFT> *>(Sec2);
1618f66df92SGeorge Rimar     return checkConstraint(Sec->getSectionHdr()->sh_flags, Kind);
16206ae6836SGeorge Rimar   });
16306ae6836SGeorge Rimar }
16406ae6836SGeorge Rimar 
165d3190795SRafael Espindola // Compute and remember which sections the InputSectionDescription matches.
166be94e1b6SRafael Espindola template <class ELFT>
167e71a3f8aSRafael Espindola void LinkerScript<ELFT>::computeInputSections(InputSectionDescription *I) {
1684dc07becSRui Ueyama   // Collects all sections that satisfy constraints of I
1694dc07becSRui Ueyama   // and attach them to I.
1704dc07becSRui Ueyama   for (SectionPattern &Pat : I->SectionPatterns) {
171395281cfSGeorge Rimar     for (ObjectFile<ELFT> *F : Symtab<ELFT>::X->getObjectFiles()) {
1723ff27f49SRui Ueyama       StringRef Filename = sys::path::filename(F->getName());
1734dc07becSRui Ueyama       if (!match(I->FileRe, Filename) || match(Pat.ExcludedFileRe, Filename))
1743ff27f49SRui Ueyama         continue;
1754dc07becSRui Ueyama 
176be94e1b6SRafael Espindola       for (InputSectionBase<ELFT> *S : F->getSections())
1774dc07becSRui Ueyama         if (!isDiscarded(S) && !S->OutSec && match(Pat.SectionRe, S->Name))
178d3190795SRafael Espindola           I->Sections.push_back(S);
1794dc07becSRui Ueyama       if (match(Pat.SectionRe, "COMMON"))
180d3190795SRafael Espindola         I->Sections.push_back(CommonInputSection<ELFT>::X);
181395281cfSGeorge Rimar     }
182395281cfSGeorge Rimar   }
183d3190795SRafael Espindola 
1844dc07becSRui Ueyama   // Sort for SORT() commands.
185b2a0abdfSRui Ueyama   if (I->SortInner != SortSectionPolicy::Default)
186d3190795SRafael Espindola     std::stable_sort(I->Sections.begin(), I->Sections.end(),
187d3190795SRafael Espindola                      getComparator(I->SortInner));
188b2a0abdfSRui Ueyama   if (I->SortOuter != SortSectionPolicy::Default)
189d3190795SRafael Espindola     std::stable_sort(I->Sections.begin(), I->Sections.end(),
190d3190795SRafael Espindola                      getComparator(I->SortOuter));
191d3190795SRafael Espindola 
192d3190795SRafael Espindola   // We do not add duplicate input sections, so mark them with a dummy output
193d3190795SRafael Espindola   // section for now.
194d3190795SRafael Espindola   for (InputSectionData *S : I->Sections) {
195d3190795SRafael Espindola     auto *S2 = static_cast<InputSectionBase<ELFT> *>(S);
196d3190795SRafael Espindola     S2->OutSec = (OutputSectionBase<ELFT> *)-1;
197d3190795SRafael Espindola   }
198be94e1b6SRafael Espindola }
199be94e1b6SRafael Espindola 
200be94e1b6SRafael Espindola template <class ELFT>
201be94e1b6SRafael Espindola void LinkerScript<ELFT>::discard(ArrayRef<InputSectionBase<ELFT> *> V) {
202be94e1b6SRafael Espindola   for (InputSectionBase<ELFT> *S : V) {
203be94e1b6SRafael Espindola     S->Live = false;
204be94e1b6SRafael Espindola     reportDiscarded(S);
205be94e1b6SRafael Espindola   }
206be94e1b6SRafael Espindola }
207be94e1b6SRafael Espindola 
20806ae6836SGeorge Rimar template <class ELFT>
2090b9ce6a4SRui Ueyama std::vector<InputSectionBase<ELFT> *>
21006ae6836SGeorge Rimar LinkerScript<ELFT>::createInputSectionList(OutputSectionCommand &OutCmd) {
2110b9ce6a4SRui Ueyama   std::vector<InputSectionBase<ELFT> *> Ret;
212e7f912cdSRui Ueyama 
21306ae6836SGeorge Rimar   for (const std::unique_ptr<BaseCommand> &Base : OutCmd.Commands) {
2147c3ff2ebSRafael Espindola     auto *Cmd = dyn_cast<InputSectionDescription>(Base.get());
2157c3ff2ebSRafael Espindola     if (!Cmd)
2160b9ce6a4SRui Ueyama       continue;
217e71a3f8aSRafael Espindola     computeInputSections(Cmd);
218d3190795SRafael Espindola     for (InputSectionData *S : Cmd->Sections)
219d3190795SRafael Espindola       Ret.push_back(static_cast<InputSectionBase<ELFT> *>(S));
2200b9ce6a4SRui Ueyama   }
221e71a3f8aSRafael Espindola 
2220b9ce6a4SRui Ueyama   return Ret;
2230b9ce6a4SRui Ueyama }
2240b9ce6a4SRui Ueyama 
225e5d3ca50SPetr Hosek template <class ELFT>
22610897f18SRafael Espindola static SectionKey<ELFT::Is64Bits> createKey(InputSectionBase<ELFT> *C,
22710897f18SRafael Espindola                                             StringRef OutsecName) {
22810897f18SRafael Espindola   // When using linker script the merge rules are different.
22910897f18SRafael Espindola   // Unfortunately, linker scripts are name based. This means that expressions
23010897f18SRafael Espindola   // like *(.foo*) can refer to multiple input sections that would normally be
23110897f18SRafael Espindola   // placed in different output sections. We cannot put them in different
23210897f18SRafael Espindola   // output sections or we would produce wrong results for
23310897f18SRafael Espindola   // start = .; *(.foo.*) end = .; *(.bar)
23410897f18SRafael Espindola   // and a mapping of .foo1 and .bar1 to one section and .foo2 and .bar2 to
23510897f18SRafael Espindola   // another. The problem is that there is no way to layout those output
23610897f18SRafael Espindola   // sections such that the .foo sections are the only thing between the
23710897f18SRafael Espindola   // start and end symbols.
23810897f18SRafael Espindola 
23910897f18SRafael Espindola   // An extra annoyance is that we cannot simply disable merging of the contents
24010897f18SRafael Espindola   // of SHF_MERGE sections, but our implementation requires one output section
24110897f18SRafael Espindola   // per "kind" (string or not, which size/aligment).
24210897f18SRafael Espindola   // Fortunately, creating symbols in the middle of a merge section is not
24310897f18SRafael Espindola   // supported by bfd or gold, so we can just create multiple section in that
24410897f18SRafael Espindola   // case.
24510897f18SRafael Espindola   const typename ELFT::Shdr *H = C->getSectionHdr();
24610897f18SRafael Espindola   typedef typename ELFT::uint uintX_t;
24710897f18SRafael Espindola   uintX_t Flags = H->sh_flags & (SHF_MERGE | SHF_STRINGS);
24810897f18SRafael Espindola 
24910897f18SRafael Espindola   uintX_t Alignment = 0;
25010897f18SRafael Espindola   if (isa<MergeInputSection<ELFT>>(C))
25110897f18SRafael Espindola     Alignment = std::max(H->sh_addralign, H->sh_entsize);
25210897f18SRafael Espindola 
25310897f18SRafael Espindola   return SectionKey<ELFT::Is64Bits>{OutsecName, /*Type*/ 0, Flags, Alignment};
25410897f18SRafael Espindola }
25510897f18SRafael Espindola 
25610897f18SRafael Espindola template <class ELFT>
25720d03194SEugene Leviant void LinkerScript<ELFT>::addSection(OutputSectionFactory<ELFT> &Factory,
25820d03194SEugene Leviant                                     InputSectionBase<ELFT> *Sec,
25920d03194SEugene Leviant                                     StringRef Name) {
26028c1597aSRafael Espindola   OutputSectionBase<ELFT> *OutSec;
26128c1597aSRafael Espindola   bool IsNew;
26210897f18SRafael Espindola   std::tie(OutSec, IsNew) = Factory.create(createKey(Sec, Name), Sec);
26328c1597aSRafael Espindola   if (IsNew)
26428c1597aSRafael Espindola     OutputSections->push_back(OutSec);
26520d03194SEugene Leviant   OutSec->addSection(Sec);
26620d03194SEugene Leviant }
26720d03194SEugene Leviant 
26820d03194SEugene Leviant template <class ELFT>
26920d03194SEugene Leviant void LinkerScript<ELFT>::processCommands(OutputSectionFactory<ELFT> &Factory) {
27028c1597aSRafael Espindola 
2717c3ff2ebSRafael Espindola   for (unsigned I = 0; I < Opt.Commands.size(); ++I) {
2727c3ff2ebSRafael Espindola     auto Iter = Opt.Commands.begin() + I;
2737c3ff2ebSRafael Espindola     const std::unique_ptr<BaseCommand> &Base1 = *Iter;
2742ab5f73dSRui Ueyama     if (auto *Cmd = dyn_cast<SymbolAssignment>(Base1.get())) {
2752ab5f73dSRui Ueyama       if (shouldDefine<ELFT>(Cmd))
2762ab5f73dSRui Ueyama         addRegular<ELFT>(Cmd);
2772ab5f73dSRui Ueyama       continue;
2782ab5f73dSRui Ueyama     }
27920d03194SEugene Leviant     if (auto *Cmd = dyn_cast<AssertCommand>(Base1.get())) {
28020d03194SEugene Leviant       // If we don't have SECTIONS then output sections have already been
28120d03194SEugene Leviant       // created by Writer<EFLT>. The LinkerScript<ELFT>::assignAddresses
28220d03194SEugene Leviant       // will not be called, so ASSERT should be evaluated now.
28320d03194SEugene Leviant       if (!Opt.HasSections)
28420d03194SEugene Leviant         Cmd->Expression(0);
28520d03194SEugene Leviant       continue;
28620d03194SEugene Leviant     }
2872ab5f73dSRui Ueyama 
288ceabe80eSEugene Leviant     if (auto *Cmd = dyn_cast<OutputSectionCommand>(Base1.get())) {
2897bd37870SRafael Espindola       std::vector<InputSectionBase<ELFT> *> V = createInputSectionList(*Cmd);
2907bd37870SRafael Espindola 
29148c3f1ceSRui Ueyama       if (Cmd->Name == "/DISCARD/") {
2927bd37870SRafael Espindola         discard(V);
29348c3f1ceSRui Ueyama         continue;
29448c3f1ceSRui Ueyama       }
2950b9ce6a4SRui Ueyama 
2967c3ff2ebSRafael Espindola       if (!matchConstraints<ELFT>(V, Cmd->Constraint)) {
2977c3ff2ebSRafael Espindola         for (InputSectionBase<ELFT> *S : V)
2987c3ff2ebSRafael Espindola           S->OutSec = nullptr;
2997c3ff2ebSRafael Espindola         Opt.Commands.erase(Iter);
300*dfbbbc86SGeorge Rimar         --I;
3017c3ff2ebSRafael Espindola         continue;
3027c3ff2ebSRafael Espindola       }
3037c3ff2ebSRafael Espindola 
3047c3ff2ebSRafael Espindola       for (const std::unique_ptr<BaseCommand> &Base : Cmd->Commands)
3057c3ff2ebSRafael Espindola         if (auto *OutCmd = dyn_cast<SymbolAssignment>(Base.get()))
3067c3ff2ebSRafael Espindola           if (shouldDefine<ELFT>(OutCmd))
3077c3ff2ebSRafael Espindola             addSymbol<ELFT>(OutCmd);
3087c3ff2ebSRafael Espindola 
30997403d15SEugene Leviant       if (V.empty())
3100b9ce6a4SRui Ueyama         continue;
3110b9ce6a4SRui Ueyama 
312a14b13d8SGeorge Rimar       for (InputSectionBase<ELFT> *Sec : V) {
31320d03194SEugene Leviant         addSection(Factory, Sec, Cmd->Name);
31420d03194SEugene Leviant         if (uint32_t Subalign = Cmd->SubalignExpr ? Cmd->SubalignExpr(0) : 0)
315db24d9c3SGeorge Rimar           Sec->Alignment = Subalign;
31620d03194SEugene Leviant       }
317eea3114fSGeorge Rimar     }
31848c3f1ceSRui Ueyama   }
319db24d9c3SGeorge Rimar }
320e63d81bdSEugene Leviant 
32120d03194SEugene Leviant template <class ELFT>
32220d03194SEugene Leviant void LinkerScript<ELFT>::createSections(OutputSectionFactory<ELFT> &Factory) {
32320d03194SEugene Leviant   processCommands(Factory);
3240b9ce6a4SRui Ueyama   // Add orphan sections.
32520d03194SEugene Leviant   for (ObjectFile<ELFT> *F : Symtab<ELFT>::X->getObjectFiles())
32620d03194SEugene Leviant     for (InputSectionBase<ELFT> *S : F->getSections())
32720d03194SEugene Leviant       if (!isDiscarded(S) && !S->OutSec)
32820d03194SEugene Leviant         addSection(Factory, S, getOutputSectionName(S));
329e63d81bdSEugene Leviant }
330e63d81bdSEugene Leviant 
331db741e72SEugene Leviant // Sets value of a section-defined symbol. Two kinds of
332db741e72SEugene Leviant // symbols are processed: synthetic symbols, whose value
333db741e72SEugene Leviant // is an offset from beginning of section and regular
334db741e72SEugene Leviant // symbols whose value is absolute.
335db741e72SEugene Leviant template <class ELFT>
336db741e72SEugene Leviant static void assignSectionSymbol(SymbolAssignment *Cmd,
337db741e72SEugene Leviant                                 OutputSectionBase<ELFT> *Sec,
338db741e72SEugene Leviant                                 typename ELFT::uint Off) {
339db741e72SEugene Leviant   if (!Cmd->Sym)
340db741e72SEugene Leviant     return;
341db741e72SEugene Leviant 
342db741e72SEugene Leviant   if (auto *Body = dyn_cast<DefinedSynthetic<ELFT>>(Cmd->Sym)) {
343db741e72SEugene Leviant     Body->Section = Sec;
344db741e72SEugene Leviant     Body->Value = Cmd->Expression(Sec->getVA() + Off) - Sec->getVA();
345db741e72SEugene Leviant     return;
346db741e72SEugene Leviant   }
347db741e72SEugene Leviant   auto *Body = cast<DefinedRegular<ELFT>>(Cmd->Sym);
348db741e72SEugene Leviant   Body->Value = Cmd->Expression(Sec->getVA() + Off);
349db741e72SEugene Leviant }
350db741e72SEugene Leviant 
351d3190795SRafael Espindola template <class ELFT> void LinkerScript<ELFT>::output(InputSection<ELFT> *S) {
352d3190795SRafael Espindola   if (!AlreadyOutputIS.insert(S).second)
353ceabe80eSEugene Leviant     return;
354d3190795SRafael Espindola   bool IsTbss =
355d3190795SRafael Espindola       (CurOutSec->getFlags() & SHF_TLS) && CurOutSec->getType() == SHT_NOBITS;
356d3190795SRafael Espindola 
357d3190795SRafael Espindola   uintX_t Pos = IsTbss ? Dot + ThreadBssOffset : Dot;
358d3190795SRafael Espindola   Pos = alignTo(Pos, S->Alignment);
359d3190795SRafael Espindola   S->OutSecOff = Pos - CurOutSec->getVA();
360d3190795SRafael Espindola   Pos += S->getSize();
361d3190795SRafael Espindola 
362d3190795SRafael Espindola   // Update output section size after adding each section. This is so that
363d3190795SRafael Espindola   // SIZEOF works correctly in the case below:
364d3190795SRafael Espindola   // .foo { *(.aaa) a = SIZEOF(.foo); *(.bbb) }
365d3190795SRafael Espindola   CurOutSec->setSize(Pos - CurOutSec->getVA());
366d3190795SRafael Espindola 
367d3190795SRafael Espindola   if (!IsTbss)
368d3190795SRafael Espindola     Dot = Pos;
3692de509c3SRui Ueyama }
370ceabe80eSEugene Leviant 
371d3190795SRafael Espindola template <class ELFT> void LinkerScript<ELFT>::flush() {
372d3190795SRafael Espindola   if (auto *OutSec = dyn_cast_or_null<OutputSection<ELFT>>(CurOutSec)) {
373d3190795SRafael Espindola     for (InputSection<ELFT> *I : OutSec->Sections)
374d3190795SRafael Espindola       output(I);
375d3190795SRafael Espindola     AlreadyOutputOS.insert(CurOutSec);
376d3190795SRafael Espindola   }
377d3190795SRafael Espindola }
37897403d15SEugene Leviant 
379d3190795SRafael Espindola template <class ELFT>
380d3190795SRafael Espindola void LinkerScript<ELFT>::switchTo(OutputSectionBase<ELFT> *Sec) {
381d3190795SRafael Espindola   if (CurOutSec == Sec)
382d3190795SRafael Espindola     return;
383d3190795SRafael Espindola   if (AlreadyOutputOS.count(Sec))
384d3190795SRafael Espindola     return;
385d3190795SRafael Espindola 
386d3190795SRafael Espindola   flush();
387d3190795SRafael Espindola   CurOutSec = Sec;
388d3190795SRafael Espindola 
389d3190795SRafael Espindola   Dot = alignTo(Dot, CurOutSec->getAlignment());
390d3190795SRafael Espindola   CurOutSec->setVA(Dot);
391d3190795SRafael Espindola }
392d3190795SRafael Espindola 
393d3190795SRafael Espindola template <class ELFT> void LinkerScript<ELFT>::process(BaseCommand &Base) {
394d3190795SRafael Espindola   if (auto *AssignCmd = dyn_cast<SymbolAssignment>(&Base)) {
39597403d15SEugene Leviant     if (AssignCmd->Name == ".") {
39697403d15SEugene Leviant       // Update to location counter means update to section size.
397d3190795SRafael Espindola       Dot = AssignCmd->Expression(Dot);
398d3190795SRafael Espindola       CurOutSec->setSize(Dot - CurOutSec->getVA());
399d3190795SRafael Espindola       return;
40097403d15SEugene Leviant     }
401d3190795SRafael Espindola     assignSectionSymbol<ELFT>(AssignCmd, CurOutSec, Dot - CurOutSec->getVA());
402d3190795SRafael Espindola     return;
40397403d15SEugene Leviant   }
404d3190795SRafael Espindola   auto &ICmd = cast<InputSectionDescription>(Base);
405d3190795SRafael Espindola   for (InputSectionData *ID : ICmd.Sections) {
406d3190795SRafael Espindola     auto *IB = static_cast<InputSectionBase<ELFT> *>(ID);
407d3190795SRafael Espindola     switchTo(IB->OutSec);
408d3190795SRafael Espindola     if (auto *I = dyn_cast<InputSection<ELFT>>(IB))
409d3190795SRafael Espindola       output(I);
410d3190795SRafael Espindola     else if (AlreadyOutputOS.insert(CurOutSec).second)
411d3190795SRafael Espindola       Dot += CurOutSec->getSize();
412ceabe80eSEugene Leviant   }
413ceabe80eSEugene Leviant }
414ceabe80eSEugene Leviant 
4158f66df92SGeorge Rimar template <class ELFT>
416a14b13d8SGeorge Rimar static std::vector<OutputSectionBase<ELFT> *>
417a14b13d8SGeorge Rimar findSections(OutputSectionCommand &Cmd,
418d3190795SRafael Espindola              const std::vector<OutputSectionBase<ELFT> *> &Sections) {
419a14b13d8SGeorge Rimar   std::vector<OutputSectionBase<ELFT> *> Ret;
420a14b13d8SGeorge Rimar   for (OutputSectionBase<ELFT> *Sec : Sections)
4217c3ff2ebSRafael Espindola     if (Sec->getName() == Cmd.Name)
422a14b13d8SGeorge Rimar       Ret.push_back(Sec);
423a14b13d8SGeorge Rimar   return Ret;
4248f66df92SGeorge Rimar }
4258f66df92SGeorge Rimar 
426d3190795SRafael Espindola template <class ELFT>
427d3190795SRafael Espindola void LinkerScript<ELFT>::assignOffsets(OutputSectionCommand *Cmd) {
428d3190795SRafael Espindola   std::vector<OutputSectionBase<ELFT> *> Sections =
429d3190795SRafael Espindola       findSections(*Cmd, *OutputSections);
430d3190795SRafael Espindola   if (Sections.empty())
431d3190795SRafael Espindola     return;
432d3190795SRafael Espindola   switchTo(Sections[0]);
433d3190795SRafael Espindola 
434d3190795SRafael Espindola   // Find the last section output location. We will output orphan sections
435d3190795SRafael Espindola   // there so that end symbols point to the correct location.
436d3190795SRafael Espindola   auto E = std::find_if(Cmd->Commands.rbegin(), Cmd->Commands.rend(),
437d3190795SRafael Espindola                         [](const std::unique_ptr<BaseCommand> &Cmd) {
438d3190795SRafael Espindola                           return !isa<SymbolAssignment>(*Cmd);
439d3190795SRafael Espindola                         })
440d3190795SRafael Espindola                .base();
441d3190795SRafael Espindola   for (auto I = Cmd->Commands.begin(); I != E; ++I)
442d3190795SRafael Espindola     process(**I);
443d3190795SRafael Espindola   flush();
444d3190795SRafael Espindola   for (OutputSectionBase<ELFT> *Base : Sections) {
445d3190795SRafael Espindola     if (!AlreadyOutputOS.insert(Base).second)
446d3190795SRafael Espindola       continue;
447d3190795SRafael Espindola     switchTo(Base);
448d3190795SRafael Espindola     Dot += CurOutSec->getSize();
449d3190795SRafael Espindola   }
450d3190795SRafael Espindola   for (auto I = E, E = Cmd->Commands.end(); I != E; ++I)
451d3190795SRafael Espindola     process(**I);
452d3190795SRafael Espindola }
453d3190795SRafael Espindola 
454a4b41dcaSRafael Espindola template <class ELFT> void LinkerScript<ELFT>::assignAddresses() {
455652852c5SGeorge Rimar   // Orphan sections are sections present in the input files which
4567c18c28cSRui Ueyama   // are not explicitly placed into the output file by the linker script.
4577c18c28cSRui Ueyama   // We place orphan sections at end of file.
4587c18c28cSRui Ueyama   // Other linkers places them using some heuristics as described in
459652852c5SGeorge Rimar   // https://sourceware.org/binutils/docs/ld/Orphan-Sections.html#Orphan-Sections.
460aab6d5c5SRafael Espindola 
461aab6d5c5SRafael Espindola   // The OutputSections are already in the correct order.
462aab6d5c5SRafael Espindola   // This loops creates or moves commands as needed so that they are in the
463aab6d5c5SRafael Espindola   // correct order.
464aab6d5c5SRafael Espindola   int CmdIndex = 0;
465e5cc668eSRui Ueyama   for (OutputSectionBase<ELFT> *Sec : *OutputSections) {
466652852c5SGeorge Rimar     StringRef Name = Sec->getName();
467aab6d5c5SRafael Espindola 
468aab6d5c5SRafael Espindola     // Find the last spot where we can insert a command and still get the
469aab6d5c5SRafael Espindola     // correct order.
470aab6d5c5SRafael Espindola     auto CmdIter = Opt.Commands.begin() + CmdIndex;
471aab6d5c5SRafael Espindola     auto E = Opt.Commands.end();
472aab6d5c5SRafael Espindola     while (CmdIter != E && !isa<OutputSectionCommand>(**CmdIter)) {
473aab6d5c5SRafael Espindola       ++CmdIter;
474aab6d5c5SRafael Espindola       ++CmdIndex;
475aab6d5c5SRafael Espindola     }
476aab6d5c5SRafael Espindola 
477aab6d5c5SRafael Espindola     auto Pos =
478aab6d5c5SRafael Espindola         std::find_if(CmdIter, E, [&](const std::unique_ptr<BaseCommand> &Base) {
479aab6d5c5SRafael Espindola           auto *Cmd = dyn_cast<OutputSectionCommand>(Base.get());
480aab6d5c5SRafael Espindola           return Cmd && Cmd->Name == Name;
481aab6d5c5SRafael Espindola         });
482aab6d5c5SRafael Espindola     if (Pos == E) {
483aab6d5c5SRafael Espindola       Opt.Commands.insert(CmdIter,
484aab6d5c5SRafael Espindola                           llvm::make_unique<OutputSectionCommand>(Name));
485aab6d5c5SRafael Espindola     } else {
486aab6d5c5SRafael Espindola       // If linker script lists alloc/non-alloc sections is the wrong order,
487aab6d5c5SRafael Espindola       // this does a right rotate to bring the desired command in place.
488373343bdSRafael Espindola       auto RPos = llvm::make_reverse_iterator(Pos + 1);
489373343bdSRafael Espindola       std::rotate(RPos, RPos + 1, llvm::make_reverse_iterator(CmdIter));
490aab6d5c5SRafael Espindola     }
491aab6d5c5SRafael Espindola     ++CmdIndex;
492652852c5SGeorge Rimar   }
493652852c5SGeorge Rimar 
4947c18c28cSRui Ueyama   // Assign addresses as instructed by linker script SECTIONS sub-commands.
4954f7500bfSRui Ueyama   Dot = getHeaderSize();
496652852c5SGeorge Rimar 
497076fe157SGeorge Rimar   for (const std::unique_ptr<BaseCommand> &Base : Opt.Commands) {
498076fe157SGeorge Rimar     if (auto *Cmd = dyn_cast<SymbolAssignment>(Base.get())) {
4998d083e6aSRui Ueyama       if (Cmd->Name == ".") {
5008d083e6aSRui Ueyama         Dot = Cmd->Expression(Dot);
5018d083e6aSRui Ueyama       } else if (Cmd->Sym) {
5028d083e6aSRui Ueyama         cast<DefinedRegular<ELFT>>(Cmd->Sym)->Value = Cmd->Expression(Dot);
5038d083e6aSRui Ueyama       }
50405ef4cffSRui Ueyama       continue;
505652852c5SGeorge Rimar     }
506652852c5SGeorge Rimar 
507eefa758eSGeorge Rimar     if (auto *Cmd = dyn_cast<AssertCommand>(Base.get())) {
508eefa758eSGeorge Rimar       Cmd->Expression(Dot);
509eefa758eSGeorge Rimar       continue;
510eefa758eSGeorge Rimar     }
511eefa758eSGeorge Rimar 
512076fe157SGeorge Rimar     auto *Cmd = cast<OutputSectionCommand>(Base.get());
513652852c5SGeorge Rimar 
51458e5c4dcSGeorge Rimar     if (Cmd->AddrExpr)
51558e5c4dcSGeorge Rimar       Dot = Cmd->AddrExpr(Dot);
51658e5c4dcSGeorge Rimar 
517d3190795SRafael Espindola     assignOffsets(Cmd);
518a14b13d8SGeorge Rimar   }
519467c4d55SEugene Leviant 
520aab6d5c5SRafael Espindola   uintX_t MinVA = std::numeric_limits<uintX_t>::max();
521aab6d5c5SRafael Espindola   for (OutputSectionBase<ELFT> *Sec : *OutputSections) {
522aab6d5c5SRafael Espindola     if (Sec->getFlags() & SHF_ALLOC)
523aab6d5c5SRafael Espindola       MinVA = std::min(MinVA, Sec->getVA());
524aab6d5c5SRafael Espindola     else
525d3190795SRafael Espindola       Sec->setVA(0);
526aab6d5c5SRafael Espindola   }
527aab6d5c5SRafael Espindola 
5284ec013acSRafael Espindola   uintX_t HeaderSize =
5294ec013acSRafael Espindola       Out<ELFT>::ElfHeader->getSize() + Out<ELFT>::ProgramHeaders->getSize();
5304ec013acSRafael Espindola   if (HeaderSize > MinVA)
5314ec013acSRafael Espindola     fatal("Not enough space for ELF and program headers");
5324ec013acSRafael Espindola 
53364c32d6fSRafael Espindola   // ELF and Program headers need to be right before the first section in
534b91e7118SGeorge Rimar   // memory. Set their addresses accordingly.
5354ec013acSRafael Espindola   MinVA = alignDown(MinVA - HeaderSize, Target->PageSize);
536467c4d55SEugene Leviant   Out<ELFT>::ElfHeader->setVA(MinVA);
537467c4d55SEugene Leviant   Out<ELFT>::ProgramHeaders->setVA(Out<ELFT>::ElfHeader->getSize() + MinVA);
538fb8978fcSDima Stepanov }
539652852c5SGeorge Rimar 
540464daadcSRui Ueyama // Creates program headers as instructed by PHDRS linker script command.
54107320e40SRui Ueyama template <class ELFT>
542a4b41dcaSRafael Espindola std::vector<PhdrEntry<ELFT>> LinkerScript<ELFT>::createPhdrs() {
543edebbdf1SRui Ueyama   std::vector<PhdrEntry<ELFT>> Ret;
544bbe38602SEugene Leviant 
545464daadcSRui Ueyama   // Process PHDRS and FILEHDR keywords because they are not
546464daadcSRui Ueyama   // real output sections and cannot be added in the following loop.
547bbe38602SEugene Leviant   for (const PhdrsCommand &Cmd : Opt.PhdrsCommands) {
548edebbdf1SRui Ueyama     Ret.emplace_back(Cmd.Type, Cmd.Flags == UINT_MAX ? PF_R : Cmd.Flags);
549edebbdf1SRui Ueyama     PhdrEntry<ELFT> &Phdr = Ret.back();
550bbe38602SEugene Leviant 
551bbe38602SEugene Leviant     if (Cmd.HasFilehdr)
552adca245fSRui Ueyama       Phdr.add(Out<ELFT>::ElfHeader);
553bbe38602SEugene Leviant     if (Cmd.HasPhdrs)
554adca245fSRui Ueyama       Phdr.add(Out<ELFT>::ProgramHeaders);
55556b21c86SEugene Leviant 
55656b21c86SEugene Leviant     if (Cmd.LMAExpr) {
55756b21c86SEugene Leviant       Phdr.H.p_paddr = Cmd.LMAExpr(0);
55856b21c86SEugene Leviant       Phdr.HasLMA = true;
55956b21c86SEugene Leviant     }
560bbe38602SEugene Leviant   }
561bbe38602SEugene Leviant 
562464daadcSRui Ueyama   // Add output sections to program headers.
563edebbdf1SRui Ueyama   PhdrEntry<ELFT> *Load = nullptr;
564edebbdf1SRui Ueyama   uintX_t Flags = PF_R;
565464daadcSRui Ueyama   for (OutputSectionBase<ELFT> *Sec : *OutputSections) {
566bbe38602SEugene Leviant     if (!(Sec->getFlags() & SHF_ALLOC))
567bbe38602SEugene Leviant       break;
568bbe38602SEugene Leviant 
569edebbdf1SRui Ueyama     std::vector<size_t> PhdrIds = getPhdrIndices(Sec->getName());
570bbe38602SEugene Leviant     if (!PhdrIds.empty()) {
571bbe38602SEugene Leviant       // Assign headers specified by linker script
572bbe38602SEugene Leviant       for (size_t Id : PhdrIds) {
573edebbdf1SRui Ueyama         Ret[Id].add(Sec);
574865bf863SEugene Leviant         if (Opt.PhdrsCommands[Id].Flags == UINT_MAX)
5750b113671SRafael Espindola           Ret[Id].H.p_flags |= Sec->getPhdrFlags();
576bbe38602SEugene Leviant       }
577bbe38602SEugene Leviant     } else {
578bbe38602SEugene Leviant       // If we have no load segment or flags've changed then we want new load
579bbe38602SEugene Leviant       // segment.
5800b113671SRafael Espindola       uintX_t NewFlags = Sec->getPhdrFlags();
581bbe38602SEugene Leviant       if (Load == nullptr || Flags != NewFlags) {
582edebbdf1SRui Ueyama         Load = &*Ret.emplace(Ret.end(), PT_LOAD, NewFlags);
583bbe38602SEugene Leviant         Flags = NewFlags;
584bbe38602SEugene Leviant       }
58518f084ffSRui Ueyama       Load->add(Sec);
586bbe38602SEugene Leviant     }
587bbe38602SEugene Leviant   }
588edebbdf1SRui Ueyama   return Ret;
589bbe38602SEugene Leviant }
590bbe38602SEugene Leviant 
591f9bc3bd2SEugene Leviant template <class ELFT> bool LinkerScript<ELFT>::ignoreInterpSection() {
592f9bc3bd2SEugene Leviant   // Ignore .interp section in case we have PHDRS specification
593f9bc3bd2SEugene Leviant   // and PT_INTERP isn't listed.
594f9bc3bd2SEugene Leviant   return !Opt.PhdrsCommands.empty() &&
595f9bc3bd2SEugene Leviant          llvm::find_if(Opt.PhdrsCommands, [](const PhdrsCommand &Cmd) {
596f9bc3bd2SEugene Leviant            return Cmd.Type == PT_INTERP;
597f9bc3bd2SEugene Leviant          }) == Opt.PhdrsCommands.end();
598f9bc3bd2SEugene Leviant }
599f9bc3bd2SEugene Leviant 
600bbe38602SEugene Leviant template <class ELFT>
60107320e40SRui Ueyama ArrayRef<uint8_t> LinkerScript<ELFT>::getFiller(StringRef Name) {
602f6c3ccefSGeorge Rimar   for (const std::unique_ptr<BaseCommand> &Base : Opt.Commands)
603f6c3ccefSGeorge Rimar     if (auto *Cmd = dyn_cast<OutputSectionCommand>(Base.get()))
604f6c3ccefSGeorge Rimar       if (Cmd->Name == Name)
605f6c3ccefSGeorge Rimar         return Cmd->Filler;
606e2ee72b5SGeorge Rimar   return {};
607e2ee72b5SGeorge Rimar }
608e2ee72b5SGeorge Rimar 
609206fffa1SGeorge Rimar template <class ELFT> Expr LinkerScript<ELFT>::getLma(StringRef Name) {
6108ceadb38SGeorge Rimar   for (const std::unique_ptr<BaseCommand> &Base : Opt.Commands)
6118ceadb38SGeorge Rimar     if (auto *Cmd = dyn_cast<OutputSectionCommand>(Base.get()))
6128ceadb38SGeorge Rimar       if (Cmd->LmaExpr && Cmd->Name == Name)
6138ceadb38SGeorge Rimar         return Cmd->LmaExpr;
6148ceadb38SGeorge Rimar   return {};
6158ceadb38SGeorge Rimar }
6168ceadb38SGeorge Rimar 
617c3e2a4b0SRui Ueyama // Returns the index of the given section name in linker script
618c3e2a4b0SRui Ueyama // SECTIONS commands. Sections are laid out as the same order as they
619c3e2a4b0SRui Ueyama // were in the script. If a given name did not appear in the script,
620c3e2a4b0SRui Ueyama // it returns INT_MAX, so that it will be laid out at end of file.
621076fe157SGeorge Rimar template <class ELFT> int LinkerScript<ELFT>::getSectionIndex(StringRef Name) {
622f510fa6bSRui Ueyama   int I = 0;
623f510fa6bSRui Ueyama   for (std::unique_ptr<BaseCommand> &Base : Opt.Commands) {
624076fe157SGeorge Rimar     if (auto *Cmd = dyn_cast<OutputSectionCommand>(Base.get()))
625076fe157SGeorge Rimar       if (Cmd->Name == Name)
626f510fa6bSRui Ueyama         return I;
627f510fa6bSRui Ueyama     ++I;
628f510fa6bSRui Ueyama   }
629f510fa6bSRui Ueyama   return INT_MAX;
63071b26e94SGeorge Rimar }
63171b26e94SGeorge Rimar 
63271b26e94SGeorge Rimar // A compartor to sort output sections. Returns -1 or 1 if
63371b26e94SGeorge Rimar // A or B are mentioned in linker script. Otherwise, returns 0.
63407320e40SRui Ueyama template <class ELFT>
63507320e40SRui Ueyama int LinkerScript<ELFT>::compareSections(StringRef A, StringRef B) {
636c3e2a4b0SRui Ueyama   int I = getSectionIndex(A);
637c3e2a4b0SRui Ueyama   int J = getSectionIndex(B);
638c3e2a4b0SRui Ueyama   if (I == INT_MAX && J == INT_MAX)
639717677afSRui Ueyama     return 0;
640717677afSRui Ueyama   return I < J ? -1 : 1;
641717677afSRui Ueyama }
642717677afSRui Ueyama 
643bbe38602SEugene Leviant template <class ELFT> bool LinkerScript<ELFT>::hasPhdrsCommands() {
644bbe38602SEugene Leviant   return !Opt.PhdrsCommands.empty();
645bbe38602SEugene Leviant }
646bbe38602SEugene Leviant 
6479e69450eSGeorge Rimar template <class ELFT>
648884e786dSGeorge Rimar uint64_t LinkerScript<ELFT>::getOutputSectionAddress(StringRef Name) {
64996659df0SGeorge Rimar   for (OutputSectionBase<ELFT> *Sec : *OutputSections)
65096659df0SGeorge Rimar     if (Sec->getName() == Name)
65196659df0SGeorge Rimar       return Sec->getVA();
65296659df0SGeorge Rimar   error("undefined section " + Name);
65396659df0SGeorge Rimar   return 0;
65496659df0SGeorge Rimar }
65596659df0SGeorge Rimar 
65696659df0SGeorge Rimar template <class ELFT>
657884e786dSGeorge Rimar uint64_t LinkerScript<ELFT>::getOutputSectionSize(StringRef Name) {
6589e69450eSGeorge Rimar   for (OutputSectionBase<ELFT> *Sec : *OutputSections)
6599e69450eSGeorge Rimar     if (Sec->getName() == Name)
6609e69450eSGeorge Rimar       return Sec->getSize();
6619e69450eSGeorge Rimar   error("undefined section " + Name);
6629e69450eSGeorge Rimar   return 0;
6639e69450eSGeorge Rimar }
6649e69450eSGeorge Rimar 
66536fac7f0SEugene Leviant template <class ELFT>
66636fac7f0SEugene Leviant uint64_t LinkerScript<ELFT>::getOutputSectionAlign(StringRef Name) {
66736fac7f0SEugene Leviant   for (OutputSectionBase<ELFT> *Sec : *OutputSections)
66836fac7f0SEugene Leviant     if (Sec->getName() == Name)
66936fac7f0SEugene Leviant       return Sec->getAlignment();
67036fac7f0SEugene Leviant   error("undefined section " + Name);
67136fac7f0SEugene Leviant   return 0;
67236fac7f0SEugene Leviant }
67336fac7f0SEugene Leviant 
674884e786dSGeorge Rimar template <class ELFT> uint64_t LinkerScript<ELFT>::getHeaderSize() {
675e32a3598SGeorge Rimar   return Out<ELFT>::ElfHeader->getSize() + Out<ELFT>::ProgramHeaders->getSize();
676e32a3598SGeorge Rimar }
677e32a3598SGeorge Rimar 
678884e786dSGeorge Rimar template <class ELFT> uint64_t LinkerScript<ELFT>::getSymbolValue(StringRef S) {
679884e786dSGeorge Rimar   if (SymbolBody *B = Symtab<ELFT>::X->find(S))
680884e786dSGeorge Rimar     return B->getVA<ELFT>();
681884e786dSGeorge Rimar   error("symbol not found: " + S);
682884e786dSGeorge Rimar   return 0;
683884e786dSGeorge Rimar }
684884e786dSGeorge Rimar 
685bbe38602SEugene Leviant // Returns indices of ELF headers containing specific section, identified
686bbe38602SEugene Leviant // by Name. Each index is a zero based number of ELF header listed within
687bbe38602SEugene Leviant // PHDRS {} script block.
688bbe38602SEugene Leviant template <class ELFT>
689edebbdf1SRui Ueyama std::vector<size_t> LinkerScript<ELFT>::getPhdrIndices(StringRef SectionName) {
690076fe157SGeorge Rimar   for (const std::unique_ptr<BaseCommand> &Base : Opt.Commands) {
691076fe157SGeorge Rimar     auto *Cmd = dyn_cast<OutputSectionCommand>(Base.get());
692edebbdf1SRui Ueyama     if (!Cmd || Cmd->Name != SectionName)
69331d842f5SGeorge Rimar       continue;
69431d842f5SGeorge Rimar 
69529c5a2a9SRui Ueyama     std::vector<size_t> Ret;
69629c5a2a9SRui Ueyama     for (StringRef PhdrName : Cmd->Phdrs)
69729c5a2a9SRui Ueyama       Ret.push_back(getPhdrIndex(PhdrName));
69829c5a2a9SRui Ueyama     return Ret;
699bbe38602SEugene Leviant   }
70031d842f5SGeorge Rimar   return {};
70131d842f5SGeorge Rimar }
702bbe38602SEugene Leviant 
70329c5a2a9SRui Ueyama template <class ELFT>
70429c5a2a9SRui Ueyama size_t LinkerScript<ELFT>::getPhdrIndex(StringRef PhdrName) {
70529c5a2a9SRui Ueyama   size_t I = 0;
70629c5a2a9SRui Ueyama   for (PhdrsCommand &Cmd : Opt.PhdrsCommands) {
70729c5a2a9SRui Ueyama     if (Cmd.Name == PhdrName)
70829c5a2a9SRui Ueyama       return I;
70929c5a2a9SRui Ueyama     ++I;
71029c5a2a9SRui Ueyama   }
71129c5a2a9SRui Ueyama   error("section header '" + PhdrName + "' is not listed in PHDRS");
71229c5a2a9SRui Ueyama   return 0;
71329c5a2a9SRui Ueyama }
71429c5a2a9SRui Ueyama 
71507320e40SRui Ueyama class elf::ScriptParser : public ScriptParserBase {
716c3794e58SGeorge Rimar   typedef void (ScriptParser::*Handler)();
717c3794e58SGeorge Rimar 
718f7c5fbb1SRui Ueyama public:
71907320e40SRui Ueyama   ScriptParser(StringRef S, bool B) : ScriptParserBase(S), IsUnderSysroot(B) {}
720f23b2320SGeorge Rimar 
72120b6598cSGeorge Rimar   void readLinkerScript();
72220b6598cSGeorge Rimar   void readVersionScript();
723f7c5fbb1SRui Ueyama 
724f7c5fbb1SRui Ueyama private:
72552a1509eSRui Ueyama   void addFile(StringRef Path);
72652a1509eSRui Ueyama 
727f7c5fbb1SRui Ueyama   void readAsNeeded();
72890c5099eSDenis Protivensky   void readEntry();
72983f406cfSGeorge Rimar   void readExtern();
730f7c5fbb1SRui Ueyama   void readGroup();
73131aa1f83SRui Ueyama   void readInclude();
732ee59282bSRui Ueyama   void readOutput();
7339159ce93SDavide Italiano   void readOutputArch();
734f7c5fbb1SRui Ueyama   void readOutputFormat();
735bbe38602SEugene Leviant   void readPhdrs();
73668a39a65SDavide Italiano   void readSearchDir();
7378e3b38abSDenis Protivensky   void readSections();
73895769b4aSRui Ueyama   void readVersion();
73995769b4aSRui Ueyama   void readVersionScriptCommand();
7408e3b38abSDenis Protivensky 
741113cdec9SRui Ueyama   SymbolAssignment *readAssignment(StringRef Name);
742ff1f29e0SGeorge Rimar   std::vector<uint8_t> readFill();
74310416564SRui Ueyama   OutputSectionCommand *readOutputSectionDescription(StringRef OutSec);
744ff1f29e0SGeorge Rimar   std::vector<uint8_t> readOutputSectionFiller(StringRef Tok);
745bbe38602SEugene Leviant   std::vector<StringRef> readOutputSectionPhdrs();
746a2496cbeSGeorge Rimar   InputSectionDescription *readInputSectionDescription(StringRef Tok);
747c91930a1SGeorge Rimar   Regex readFilePatterns();
748395281cfSGeorge Rimar   void readSectionExcludes(InputSectionDescription *Cmd);
749a2496cbeSGeorge Rimar   InputSectionDescription *readInputSectionRules(StringRef FilePattern);
750bbe38602SEugene Leviant   unsigned readPhdrType();
751be394db3SGeorge Rimar   SortSectionPolicy readSortKind();
752a35e39caSPetr Hosek   SymbolAssignment *readProvideHidden(bool Provide, bool Hidden);
753db741e72SEugene Leviant   SymbolAssignment *readProvideOrAssignment(StringRef Tok, bool MakeAbsolute);
75403fc010eSGeorge Rimar   void readSort();
755eefa758eSGeorge Rimar   Expr readAssert();
756708019c4SRui Ueyama 
757708019c4SRui Ueyama   Expr readExpr();
758708019c4SRui Ueyama   Expr readExpr1(Expr Lhs, int MinPrec);
759708019c4SRui Ueyama   Expr readPrimary();
760708019c4SRui Ueyama   Expr readTernary(Expr Cond);
7616ad7dfccSRui Ueyama   Expr readParenExpr();
762f7c5fbb1SRui Ueyama 
76320b6598cSGeorge Rimar   // For parsing version script.
76420b6598cSGeorge Rimar   void readExtern(std::vector<SymbolVersion> *Globals);
76595769b4aSRui Ueyama   void readVersionDeclaration(StringRef VerStr);
76620b6598cSGeorge Rimar   void readGlobal(StringRef VerStr);
76720b6598cSGeorge Rimar   void readLocal();
76820b6598cSGeorge Rimar 
76907320e40SRui Ueyama   ScriptConfiguration &Opt = *ScriptConfig;
77007320e40SRui Ueyama   StringSaver Saver = {ScriptConfig->Alloc};
77116b0cc9eSSimon Atanasyan   bool IsUnderSysroot;
772f7c5fbb1SRui Ueyama };
773f7c5fbb1SRui Ueyama 
77420b6598cSGeorge Rimar void ScriptParser::readVersionScript() {
77595769b4aSRui Ueyama   readVersionScriptCommand();
77620b6598cSGeorge Rimar   if (!atEOF())
77795769b4aSRui Ueyama     setError("EOF expected, but got " + next());
77895769b4aSRui Ueyama }
77995769b4aSRui Ueyama 
78095769b4aSRui Ueyama void ScriptParser::readVersionScriptCommand() {
78195769b4aSRui Ueyama   if (skip("{")) {
78295769b4aSRui Ueyama     readVersionDeclaration("");
78320b6598cSGeorge Rimar     return;
78420b6598cSGeorge Rimar   }
78520b6598cSGeorge Rimar 
78695769b4aSRui Ueyama   while (!atEOF() && !Error && peek() != "}") {
78720b6598cSGeorge Rimar     StringRef VerStr = next();
78820b6598cSGeorge Rimar     if (VerStr == "{") {
78995769b4aSRui Ueyama       setError("anonymous version definition is used in "
79095769b4aSRui Ueyama                "combination with other version definitions");
79120b6598cSGeorge Rimar       return;
79220b6598cSGeorge Rimar     }
79320b6598cSGeorge Rimar     expect("{");
79495769b4aSRui Ueyama     readVersionDeclaration(VerStr);
79520b6598cSGeorge Rimar   }
79620b6598cSGeorge Rimar }
79720b6598cSGeorge Rimar 
79895769b4aSRui Ueyama void ScriptParser::readVersion() {
79995769b4aSRui Ueyama   expect("{");
80095769b4aSRui Ueyama   readVersionScriptCommand();
80195769b4aSRui Ueyama   expect("}");
80295769b4aSRui Ueyama }
80395769b4aSRui Ueyama 
80420b6598cSGeorge Rimar void ScriptParser::readLinkerScript() {
805f7c5fbb1SRui Ueyama   while (!atEOF()) {
806f7c5fbb1SRui Ueyama     StringRef Tok = next();
807a27eeccaSRui Ueyama     if (Tok == ";")
808a27eeccaSRui Ueyama       continue;
809a27eeccaSRui Ueyama 
81020d03194SEugene Leviant     if (Tok == "ASSERT") {
81120d03194SEugene Leviant       Opt.Commands.emplace_back(new AssertCommand(readAssert()));
81220d03194SEugene Leviant     } else if (Tok == "ENTRY") {
813a27eeccaSRui Ueyama       readEntry();
814a27eeccaSRui Ueyama     } else if (Tok == "EXTERN") {
815a27eeccaSRui Ueyama       readExtern();
816a27eeccaSRui Ueyama     } else if (Tok == "GROUP" || Tok == "INPUT") {
817a27eeccaSRui Ueyama       readGroup();
818a27eeccaSRui Ueyama     } else if (Tok == "INCLUDE") {
819a27eeccaSRui Ueyama       readInclude();
820a27eeccaSRui Ueyama     } else if (Tok == "OUTPUT") {
821a27eeccaSRui Ueyama       readOutput();
822a27eeccaSRui Ueyama     } else if (Tok == "OUTPUT_ARCH") {
823a27eeccaSRui Ueyama       readOutputArch();
824a27eeccaSRui Ueyama     } else if (Tok == "OUTPUT_FORMAT") {
825a27eeccaSRui Ueyama       readOutputFormat();
826a27eeccaSRui Ueyama     } else if (Tok == "PHDRS") {
827a27eeccaSRui Ueyama       readPhdrs();
828a27eeccaSRui Ueyama     } else if (Tok == "SEARCH_DIR") {
829a27eeccaSRui Ueyama       readSearchDir();
830a27eeccaSRui Ueyama     } else if (Tok == "SECTIONS") {
831a27eeccaSRui Ueyama       readSections();
832a27eeccaSRui Ueyama     } else if (Tok == "VERSION") {
833a27eeccaSRui Ueyama       readVersion();
834db741e72SEugene Leviant     } else if (SymbolAssignment *Cmd = readProvideOrAssignment(Tok, true)) {
8350df80befSPetr Hosek       Opt.Commands.emplace_back(Cmd);
836e5d3ca50SPetr Hosek     } else {
8375761042dSGeorge Rimar       setError("unknown directive: " + Tok);
838f7c5fbb1SRui Ueyama     }
839f7c5fbb1SRui Ueyama   }
840e5d3ca50SPetr Hosek }
841f7c5fbb1SRui Ueyama 
842717677afSRui Ueyama void ScriptParser::addFile(StringRef S) {
84316b0cc9eSSimon Atanasyan   if (IsUnderSysroot && S.startswith("/")) {
84416b0cc9eSSimon Atanasyan     SmallString<128> Path;
84516b0cc9eSSimon Atanasyan     (Config->Sysroot + S).toStringRef(Path);
84616b0cc9eSSimon Atanasyan     if (sys::fs::exists(Path)) {
84716b0cc9eSSimon Atanasyan       Driver->addFile(Saver.save(Path.str()));
84816b0cc9eSSimon Atanasyan       return;
84916b0cc9eSSimon Atanasyan     }
85016b0cc9eSSimon Atanasyan   }
85116b0cc9eSSimon Atanasyan 
852f03f3cc1SRui Ueyama   if (sys::path::is_absolute(S)) {
85352a1509eSRui Ueyama     Driver->addFile(S);
85452a1509eSRui Ueyama   } else if (S.startswith("=")) {
85552a1509eSRui Ueyama     if (Config->Sysroot.empty())
85652a1509eSRui Ueyama       Driver->addFile(S.substr(1));
85752a1509eSRui Ueyama     else
85852a1509eSRui Ueyama       Driver->addFile(Saver.save(Config->Sysroot + "/" + S.substr(1)));
85952a1509eSRui Ueyama   } else if (S.startswith("-l")) {
86021eecb4fSRui Ueyama     Driver->addLibrary(S.substr(2));
861a1b8fc3bSSimon Atanasyan   } else if (sys::fs::exists(S)) {
862a1b8fc3bSSimon Atanasyan     Driver->addFile(S);
86352a1509eSRui Ueyama   } else {
86452a1509eSRui Ueyama     std::string Path = findFromSearchPaths(S);
86552a1509eSRui Ueyama     if (Path.empty())
866777f9630SGeorge Rimar       setError("unable to find " + S);
867025d59b1SRui Ueyama     else
86852a1509eSRui Ueyama       Driver->addFile(Saver.save(Path));
86952a1509eSRui Ueyama   }
87052a1509eSRui Ueyama }
87152a1509eSRui Ueyama 
872717677afSRui Ueyama void ScriptParser::readAsNeeded() {
873f7c5fbb1SRui Ueyama   expect("(");
87435da9b6eSRui Ueyama   bool Orig = Config->AsNeeded;
87535da9b6eSRui Ueyama   Config->AsNeeded = true;
876a2acc931SRui Ueyama   while (!Error && !skip(")"))
877cd574a5eSGeorge Rimar     addFile(unquote(next()));
87835da9b6eSRui Ueyama   Config->AsNeeded = Orig;
879f7c5fbb1SRui Ueyama }
880f7c5fbb1SRui Ueyama 
881717677afSRui Ueyama void ScriptParser::readEntry() {
88290c5099eSDenis Protivensky   // -e <symbol> takes predecence over ENTRY(<symbol>).
88390c5099eSDenis Protivensky   expect("(");
88490c5099eSDenis Protivensky   StringRef Tok = next();
88590c5099eSDenis Protivensky   if (Config->Entry.empty())
88690c5099eSDenis Protivensky     Config->Entry = Tok;
88790c5099eSDenis Protivensky   expect(")");
88890c5099eSDenis Protivensky }
88990c5099eSDenis Protivensky 
890717677afSRui Ueyama void ScriptParser::readExtern() {
89183f406cfSGeorge Rimar   expect("(");
892a2acc931SRui Ueyama   while (!Error && !skip(")"))
893a2acc931SRui Ueyama     Config->Undefined.push_back(next());
89483f406cfSGeorge Rimar }
89583f406cfSGeorge Rimar 
896717677afSRui Ueyama void ScriptParser::readGroup() {
897f7c5fbb1SRui Ueyama   expect("(");
898a2acc931SRui Ueyama   while (!Error && !skip(")")) {
899f7c5fbb1SRui Ueyama     StringRef Tok = next();
900a2acc931SRui Ueyama     if (Tok == "AS_NEEDED")
901f7c5fbb1SRui Ueyama       readAsNeeded();
902a2acc931SRui Ueyama     else
903cd574a5eSGeorge Rimar       addFile(unquote(Tok));
904f7c5fbb1SRui Ueyama   }
905f7c5fbb1SRui Ueyama }
906f7c5fbb1SRui Ueyama 
907717677afSRui Ueyama void ScriptParser::readInclude() {
90831aa1f83SRui Ueyama   StringRef Tok = next();
909cd574a5eSGeorge Rimar   auto MBOrErr = MemoryBuffer::getFile(unquote(Tok));
910025d59b1SRui Ueyama   if (!MBOrErr) {
9115761042dSGeorge Rimar     setError("cannot open " + Tok);
912025d59b1SRui Ueyama     return;
913025d59b1SRui Ueyama   }
91431aa1f83SRui Ueyama   std::unique_ptr<MemoryBuffer> &MB = *MBOrErr;
915a47ee68dSRui Ueyama   StringRef S = Saver.save(MB->getMemBufferRef().getBuffer());
916a47ee68dSRui Ueyama   std::vector<StringRef> V = tokenize(S);
91731aa1f83SRui Ueyama   Tokens.insert(Tokens.begin() + Pos, V.begin(), V.end());
91831aa1f83SRui Ueyama }
91931aa1f83SRui Ueyama 
920717677afSRui Ueyama void ScriptParser::readOutput() {
921ee59282bSRui Ueyama   // -o <file> takes predecence over OUTPUT(<file>).
922ee59282bSRui Ueyama   expect("(");
923ee59282bSRui Ueyama   StringRef Tok = next();
924ee59282bSRui Ueyama   if (Config->OutputFile.empty())
925cd574a5eSGeorge Rimar     Config->OutputFile = unquote(Tok);
926ee59282bSRui Ueyama   expect(")");
927ee59282bSRui Ueyama }
928ee59282bSRui Ueyama 
929717677afSRui Ueyama void ScriptParser::readOutputArch() {
9309159ce93SDavide Italiano   // Error checking only for now.
9319159ce93SDavide Italiano   expect("(");
9329159ce93SDavide Italiano   next();
9339159ce93SDavide Italiano   expect(")");
9349159ce93SDavide Italiano }
9359159ce93SDavide Italiano 
936717677afSRui Ueyama void ScriptParser::readOutputFormat() {
937f7c5fbb1SRui Ueyama   // Error checking only for now.
938f7c5fbb1SRui Ueyama   expect("(");
939f7c5fbb1SRui Ueyama   next();
9406836c618SDavide Italiano   StringRef Tok = next();
9416836c618SDavide Italiano   if (Tok == ")")
9426836c618SDavide Italiano     return;
943025d59b1SRui Ueyama   if (Tok != ",") {
9445761042dSGeorge Rimar     setError("unexpected token: " + Tok);
945025d59b1SRui Ueyama     return;
946025d59b1SRui Ueyama   }
9476836c618SDavide Italiano   next();
9486836c618SDavide Italiano   expect(",");
9496836c618SDavide Italiano   next();
950f7c5fbb1SRui Ueyama   expect(")");
951f7c5fbb1SRui Ueyama }
952f7c5fbb1SRui Ueyama 
953bbe38602SEugene Leviant void ScriptParser::readPhdrs() {
954bbe38602SEugene Leviant   expect("{");
955bbe38602SEugene Leviant   while (!Error && !skip("}")) {
956bbe38602SEugene Leviant     StringRef Tok = next();
95756b21c86SEugene Leviant     Opt.PhdrsCommands.push_back(
95856b21c86SEugene Leviant         {Tok, PT_NULL, false, false, UINT_MAX, nullptr});
959bbe38602SEugene Leviant     PhdrsCommand &PhdrCmd = Opt.PhdrsCommands.back();
960bbe38602SEugene Leviant 
961bbe38602SEugene Leviant     PhdrCmd.Type = readPhdrType();
962bbe38602SEugene Leviant     do {
963bbe38602SEugene Leviant       Tok = next();
964bbe38602SEugene Leviant       if (Tok == ";")
965bbe38602SEugene Leviant         break;
966bbe38602SEugene Leviant       if (Tok == "FILEHDR")
967bbe38602SEugene Leviant         PhdrCmd.HasFilehdr = true;
968bbe38602SEugene Leviant       else if (Tok == "PHDRS")
969bbe38602SEugene Leviant         PhdrCmd.HasPhdrs = true;
97056b21c86SEugene Leviant       else if (Tok == "AT")
97156b21c86SEugene Leviant         PhdrCmd.LMAExpr = readParenExpr();
972865bf863SEugene Leviant       else if (Tok == "FLAGS") {
973865bf863SEugene Leviant         expect("(");
974eb685cd7SRafael Espindola         // Passing 0 for the value of dot is a bit of a hack. It means that
975eb685cd7SRafael Espindola         // we accept expressions like ".|1".
976eb685cd7SRafael Espindola         PhdrCmd.Flags = readExpr()(0);
977865bf863SEugene Leviant         expect(")");
978865bf863SEugene Leviant       } else
979bbe38602SEugene Leviant         setError("unexpected header attribute: " + Tok);
980bbe38602SEugene Leviant     } while (!Error);
981bbe38602SEugene Leviant   }
982bbe38602SEugene Leviant }
983bbe38602SEugene Leviant 
984717677afSRui Ueyama void ScriptParser::readSearchDir() {
98568a39a65SDavide Italiano   expect("(");
98686c5fb82SRui Ueyama   StringRef Tok = next();
9876c7ad13fSRui Ueyama   if (!Config->Nostdlib)
988cd574a5eSGeorge Rimar     Config->SearchPaths.push_back(unquote(Tok));
98968a39a65SDavide Italiano   expect(")");
99068a39a65SDavide Italiano }
99168a39a65SDavide Italiano 
992717677afSRui Ueyama void ScriptParser::readSections() {
993e05336ffSEugene Leviant   Opt.HasSections = true;
9948e3b38abSDenis Protivensky   expect("{");
995652852c5SGeorge Rimar   while (!Error && !skip("}")) {
996113cdec9SRui Ueyama     StringRef Tok = next();
997db741e72SEugene Leviant     BaseCommand *Cmd = readProvideOrAssignment(Tok, true);
998ceabe80eSEugene Leviant     if (!Cmd) {
999ceabe80eSEugene Leviant       if (Tok == "ASSERT")
1000eefa758eSGeorge Rimar         Cmd = new AssertCommand(readAssert());
1001ceabe80eSEugene Leviant       else
100210416564SRui Ueyama         Cmd = readOutputSectionDescription(Tok);
10038e3b38abSDenis Protivensky     }
100410416564SRui Ueyama     Opt.Commands.emplace_back(Cmd);
1005652852c5SGeorge Rimar   }
1006708019c4SRui Ueyama }
10078e3b38abSDenis Protivensky 
1008708019c4SRui Ueyama static int precedence(StringRef Op) {
1009708019c4SRui Ueyama   return StringSwitch<int>(Op)
1010708019c4SRui Ueyama       .Case("*", 4)
1011708019c4SRui Ueyama       .Case("/", 4)
1012708019c4SRui Ueyama       .Case("+", 3)
1013708019c4SRui Ueyama       .Case("-", 3)
1014708019c4SRui Ueyama       .Case("<", 2)
1015708019c4SRui Ueyama       .Case(">", 2)
1016708019c4SRui Ueyama       .Case(">=", 2)
1017708019c4SRui Ueyama       .Case("<=", 2)
1018708019c4SRui Ueyama       .Case("==", 2)
1019708019c4SRui Ueyama       .Case("!=", 2)
1020708019c4SRui Ueyama       .Case("&", 1)
1021cc3dd629SRafael Espindola       .Case("|", 1)
1022708019c4SRui Ueyama       .Default(-1);
1023708019c4SRui Ueyama }
1024708019c4SRui Ueyama 
1025c91930a1SGeorge Rimar Regex ScriptParser::readFilePatterns() {
102610416564SRui Ueyama   std::vector<StringRef> V;
102710416564SRui Ueyama   while (!Error && !skip(")"))
102810416564SRui Ueyama     V.push_back(next());
1029c91930a1SGeorge Rimar   return compileGlobPatterns(V);
10300702c4e8SGeorge Rimar }
10310702c4e8SGeorge Rimar 
1032be394db3SGeorge Rimar SortSectionPolicy ScriptParser::readSortKind() {
1033742c3836SRui Ueyama   if (skip("SORT") || skip("SORT_BY_NAME"))
1034be394db3SGeorge Rimar     return SortSectionPolicy::Name;
1035742c3836SRui Ueyama   if (skip("SORT_BY_ALIGNMENT"))
1036be394db3SGeorge Rimar     return SortSectionPolicy::Alignment;
1037575208caSGeorge Rimar   if (skip("SORT_BY_INIT_PRIORITY"))
1038be394db3SGeorge Rimar     return SortSectionPolicy::Priority;
1039be394db3SGeorge Rimar   if (skip("SORT_NONE"))
1040be394db3SGeorge Rimar     return SortSectionPolicy::None;
1041b2a0abdfSRui Ueyama   return SortSectionPolicy::Default;
1042be394db3SGeorge Rimar }
1043be394db3SGeorge Rimar 
1044be394db3SGeorge Rimar static void selectSortKind(InputSectionDescription *Cmd) {
1045b2a0abdfSRui Ueyama   if (Cmd->SortOuter == SortSectionPolicy::None) {
1046b2a0abdfSRui Ueyama     Cmd->SortOuter = SortSectionPolicy::Default;
1047be394db3SGeorge Rimar     return;
1048be394db3SGeorge Rimar   }
1049be394db3SGeorge Rimar 
1050b2a0abdfSRui Ueyama   if (Cmd->SortOuter != SortSectionPolicy::Default) {
1051be394db3SGeorge Rimar     // If the section sorting command in linker script is nested, the command
1052be394db3SGeorge Rimar     // line option will be ignored.
1053b2a0abdfSRui Ueyama     if (Cmd->SortInner != SortSectionPolicy::Default)
1054be394db3SGeorge Rimar       return;
1055be394db3SGeorge Rimar     // If the section sorting command in linker script isn't nested, the
1056be394db3SGeorge Rimar     // command line option will make the section sorting command to be treated
1057be394db3SGeorge Rimar     // as nested sorting command.
1058be394db3SGeorge Rimar     Cmd->SortInner = Config->SortSection;
1059be394db3SGeorge Rimar     return;
1060be394db3SGeorge Rimar   }
1061be394db3SGeorge Rimar   // If sorting rule not specified, use command line option.
1062be394db3SGeorge Rimar   Cmd->SortOuter = Config->SortSection;
1063742c3836SRui Ueyama }
1064742c3836SRui Ueyama 
1065395281cfSGeorge Rimar // Method reads a list of sequence of excluded files and section globs given in
1066395281cfSGeorge Rimar // a following form: ((EXCLUDE_FILE(file_pattern+))? section_pattern+)+
1067395281cfSGeorge Rimar // Example: *(.foo.1 EXCLUDE_FILE (*a.o) .foo.2 EXCLUDE_FILE (*b.o) .foo.3)
1068395281cfSGeorge Rimar void ScriptParser::readSectionExcludes(InputSectionDescription *Cmd) {
1069027a9e87SRui Ueyama   Regex ExcludeFileRe;
1070395281cfSGeorge Rimar   std::vector<StringRef> V;
1071395281cfSGeorge Rimar 
1072395281cfSGeorge Rimar   while (!Error) {
1073395281cfSGeorge Rimar     if (skip(")")) {
10744dc07becSRui Ueyama       Cmd->SectionPatterns.push_back(
1075395281cfSGeorge Rimar           {std::move(ExcludeFileRe), compileGlobPatterns(V)});
1076395281cfSGeorge Rimar       return;
1077395281cfSGeorge Rimar     }
1078395281cfSGeorge Rimar 
1079395281cfSGeorge Rimar     if (skip("EXCLUDE_FILE")) {
1080395281cfSGeorge Rimar       if (!V.empty()) {
10814dc07becSRui Ueyama         Cmd->SectionPatterns.push_back(
1082395281cfSGeorge Rimar             {std::move(ExcludeFileRe), compileGlobPatterns(V)});
1083395281cfSGeorge Rimar         V.clear();
1084395281cfSGeorge Rimar       }
1085395281cfSGeorge Rimar 
1086395281cfSGeorge Rimar       expect("(");
1087395281cfSGeorge Rimar       ExcludeFileRe = readFilePatterns();
1088395281cfSGeorge Rimar       continue;
1089395281cfSGeorge Rimar     }
1090395281cfSGeorge Rimar 
1091395281cfSGeorge Rimar     V.push_back(next());
1092395281cfSGeorge Rimar   }
1093395281cfSGeorge Rimar }
1094395281cfSGeorge Rimar 
1095a2496cbeSGeorge Rimar InputSectionDescription *
1096a2496cbeSGeorge Rimar ScriptParser::readInputSectionRules(StringRef FilePattern) {
1097c91930a1SGeorge Rimar   auto *Cmd = new InputSectionDescription(FilePattern);
10980ed42b0cSDavide Italiano   expect("(");
1099e7282797SDavide Italiano 
1100742c3836SRui Ueyama   // Read SORT().
1101be394db3SGeorge Rimar   SortSectionPolicy K1 = readSortKind();
1102b2a0abdfSRui Ueyama   if (K1 != SortSectionPolicy::Default) {
1103742c3836SRui Ueyama     Cmd->SortOuter = K1;
11040702c4e8SGeorge Rimar     expect("(");
1105be394db3SGeorge Rimar     SortSectionPolicy K2 = readSortKind();
1106b2a0abdfSRui Ueyama     if (K2 != SortSectionPolicy::Default) {
1107742c3836SRui Ueyama       Cmd->SortInner = K2;
1108350ece4eSGeorge Rimar       expect("(");
11094dc07becSRui Ueyama       Cmd->SectionPatterns.push_back({Regex(), readFilePatterns()});
11100702c4e8SGeorge Rimar       expect(")");
1111350ece4eSGeorge Rimar     } else {
11124dc07becSRui Ueyama       Cmd->SectionPatterns.push_back({Regex(), readFilePatterns()});
1113350ece4eSGeorge Rimar     }
1114350ece4eSGeorge Rimar     expect(")");
1115be394db3SGeorge Rimar     selectSortKind(Cmd);
111610416564SRui Ueyama     return Cmd;
11170659800eSGeorge Rimar   }
11180702c4e8SGeorge Rimar 
1119be394db3SGeorge Rimar   selectSortKind(Cmd);
1120395281cfSGeorge Rimar   readSectionExcludes(Cmd);
112110416564SRui Ueyama   return Cmd;
11220659800eSGeorge Rimar }
11230659800eSGeorge Rimar 
1124a2496cbeSGeorge Rimar InputSectionDescription *
1125a2496cbeSGeorge Rimar ScriptParser::readInputSectionDescription(StringRef Tok) {
11260659800eSGeorge Rimar   // Input section wildcard can be surrounded by KEEP.
11270659800eSGeorge Rimar   // https://sourceware.org/binutils/docs/ld/Input-Section-Keep.html#Input-Section-Keep
1128a2496cbeSGeorge Rimar   if (Tok == "KEEP") {
1129e7282797SDavide Italiano     expect("(");
1130a2496cbeSGeorge Rimar     StringRef FilePattern = next();
1131a2496cbeSGeorge Rimar     InputSectionDescription *Cmd = readInputSectionRules(FilePattern);
11320ed42b0cSDavide Italiano     expect(")");
11334dc07becSRui Ueyama     for (SectionPattern &Pat : Cmd->SectionPatterns)
11344dc07becSRui Ueyama       Opt.KeptSections.push_back(&Pat.SectionRe);
113510416564SRui Ueyama     return Cmd;
113610416564SRui Ueyama   }
1137a2496cbeSGeorge Rimar   return readInputSectionRules(Tok);
11380659800eSGeorge Rimar }
11390659800eSGeorge Rimar 
114003fc010eSGeorge Rimar void ScriptParser::readSort() {
114103fc010eSGeorge Rimar   expect("(");
114203fc010eSGeorge Rimar   expect("CONSTRUCTORS");
114303fc010eSGeorge Rimar   expect(")");
114403fc010eSGeorge Rimar }
114503fc010eSGeorge Rimar 
1146eefa758eSGeorge Rimar Expr ScriptParser::readAssert() {
1147eefa758eSGeorge Rimar   expect("(");
1148eefa758eSGeorge Rimar   Expr E = readExpr();
1149eefa758eSGeorge Rimar   expect(",");
1150cd574a5eSGeorge Rimar   StringRef Msg = unquote(next());
1151eefa758eSGeorge Rimar   expect(")");
1152eefa758eSGeorge Rimar   return [=](uint64_t Dot) {
1153eefa758eSGeorge Rimar     uint64_t V = E(Dot);
1154eefa758eSGeorge Rimar     if (!V)
1155eefa758eSGeorge Rimar       error(Msg);
1156eefa758eSGeorge Rimar     return V;
1157eefa758eSGeorge Rimar   };
1158eefa758eSGeorge Rimar }
1159eefa758eSGeorge Rimar 
116025150e8bSRui Ueyama // Reads a FILL(expr) command. We handle the FILL command as an
116125150e8bSRui Ueyama // alias for =fillexp section attribute, which is different from
116225150e8bSRui Ueyama // what GNU linkers do.
116325150e8bSRui Ueyama // https://sourceware.org/binutils/docs/ld/Output-Section-Data.html
1164ff1f29e0SGeorge Rimar std::vector<uint8_t> ScriptParser::readFill() {
1165ff1f29e0SGeorge Rimar   expect("(");
1166ff1f29e0SGeorge Rimar   std::vector<uint8_t> V = readOutputSectionFiller(next());
1167ff1f29e0SGeorge Rimar   expect(")");
1168ff1f29e0SGeorge Rimar   expect(";");
1169ff1f29e0SGeorge Rimar   return V;
1170ff1f29e0SGeorge Rimar }
1171ff1f29e0SGeorge Rimar 
117210416564SRui Ueyama OutputSectionCommand *
117310416564SRui Ueyama ScriptParser::readOutputSectionDescription(StringRef OutSec) {
1174076fe157SGeorge Rimar   OutputSectionCommand *Cmd = new OutputSectionCommand(OutSec);
117558e5c4dcSGeorge Rimar 
117658e5c4dcSGeorge Rimar   // Read an address expression.
117758e5c4dcSGeorge Rimar   // https://sourceware.org/binutils/docs/ld/Output-Section-Address.html#Output-Section-Address
117858e5c4dcSGeorge Rimar   if (peek() != ":")
117958e5c4dcSGeorge Rimar     Cmd->AddrExpr = readExpr();
118058e5c4dcSGeorge Rimar 
11818e3b38abSDenis Protivensky   expect(":");
1182246f681eSDavide Italiano 
11838ceadb38SGeorge Rimar   if (skip("AT"))
11846ad7dfccSRui Ueyama     Cmd->LmaExpr = readParenExpr();
1185630c6179SGeorge Rimar   if (skip("ALIGN"))
11866ad7dfccSRui Ueyama     Cmd->AlignExpr = readParenExpr();
1187db24d9c3SGeorge Rimar   if (skip("SUBALIGN"))
1188db24d9c3SGeorge Rimar     Cmd->SubalignExpr = readParenExpr();
1189630c6179SGeorge Rimar 
1190246f681eSDavide Italiano   // Parse constraints.
1191246f681eSDavide Italiano   if (skip("ONLY_IF_RO"))
1192efc4066bSRui Ueyama     Cmd->Constraint = ConstraintKind::ReadOnly;
1193246f681eSDavide Italiano   if (skip("ONLY_IF_RW"))
1194efc4066bSRui Ueyama     Cmd->Constraint = ConstraintKind::ReadWrite;
11958e3b38abSDenis Protivensky   expect("{");
11968ec77e64SRui Ueyama 
1197025d59b1SRui Ueyama   while (!Error && !skip("}")) {
1198ceabe80eSEugene Leviant     StringRef Tok = next();
1199db741e72SEugene Leviant     if (SymbolAssignment *Assignment = readProvideOrAssignment(Tok, false))
1200ceabe80eSEugene Leviant       Cmd->Commands.emplace_back(Assignment);
1201ff1f29e0SGeorge Rimar     else if (Tok == "FILL")
1202ff1f29e0SGeorge Rimar       Cmd->Filler = readFill();
1203ceabe80eSEugene Leviant     else if (Tok == "SORT")
120403fc010eSGeorge Rimar       readSort();
1205a2496cbeSGeorge Rimar     else if (peek() == "(")
1206a2496cbeSGeorge Rimar       Cmd->Commands.emplace_back(readInputSectionDescription(Tok));
1207ceabe80eSEugene Leviant     else
1208ceabe80eSEugene Leviant       setError("unknown command " + Tok);
12098e3b38abSDenis Protivensky   }
1210076fe157SGeorge Rimar   Cmd->Phdrs = readOutputSectionPhdrs();
1211ff1f29e0SGeorge Rimar   if (peek().startswith("="))
1212ff1f29e0SGeorge Rimar     Cmd->Filler = readOutputSectionFiller(next().drop_front());
121310416564SRui Ueyama   return Cmd;
1214f71caa2bSRui Ueyama }
12158ec77e64SRui Ueyama 
12162c8f1f04SRui Ueyama // Read "=<number>" where <number> is an octal/decimal/hexadecimal number.
12172c8f1f04SRui Ueyama // https://sourceware.org/binutils/docs/ld/Output-Section-Fill.html
12182c8f1f04SRui Ueyama //
12192c8f1f04SRui Ueyama // ld.gold is not fully compatible with ld.bfd. ld.bfd handles
12202c8f1f04SRui Ueyama // hexstrings as blobs of arbitrary sizes, while ld.gold handles them
12212c8f1f04SRui Ueyama // as 32-bit big-endian values. We will do the same as ld.gold does
12222c8f1f04SRui Ueyama // because it's simpler than what ld.bfd does.
1223ff1f29e0SGeorge Rimar std::vector<uint8_t> ScriptParser::readOutputSectionFiller(StringRef Tok) {
1224965827d6SRui Ueyama   uint32_t V;
1225ff1f29e0SGeorge Rimar   if (Tok.getAsInteger(0, V)) {
1226965827d6SRui Ueyama     setError("invalid filler expression: " + Tok);
1227f71caa2bSRui Ueyama     return {};
1228e2ee72b5SGeorge Rimar   }
1229965827d6SRui Ueyama   return {uint8_t(V >> 24), uint8_t(V >> 16), uint8_t(V >> 8), uint8_t(V)};
12308e3b38abSDenis Protivensky }
12318e3b38abSDenis Protivensky 
1232a35e39caSPetr Hosek SymbolAssignment *ScriptParser::readProvideHidden(bool Provide, bool Hidden) {
1233a31c91b1SEugene Leviant   expect("(");
1234174e0a16SRui Ueyama   SymbolAssignment *Cmd = readAssignment(next());
1235a35e39caSPetr Hosek   Cmd->Provide = Provide;
1236174e0a16SRui Ueyama   Cmd->Hidden = Hidden;
1237a31c91b1SEugene Leviant   expect(")");
1238a31c91b1SEugene Leviant   expect(";");
123910416564SRui Ueyama   return Cmd;
1240eda81a1bSEugene Leviant }
1241eda81a1bSEugene Leviant 
1242db741e72SEugene Leviant SymbolAssignment *ScriptParser::readProvideOrAssignment(StringRef Tok,
1243db741e72SEugene Leviant                                                         bool MakeAbsolute) {
1244ceabe80eSEugene Leviant   SymbolAssignment *Cmd = nullptr;
1245ceabe80eSEugene Leviant   if (peek() == "=" || peek() == "+=") {
1246ceabe80eSEugene Leviant     Cmd = readAssignment(Tok);
1247ceabe80eSEugene Leviant     expect(";");
1248ceabe80eSEugene Leviant   } else if (Tok == "PROVIDE") {
1249a35e39caSPetr Hosek     Cmd = readProvideHidden(true, false);
1250a35e39caSPetr Hosek   } else if (Tok == "HIDDEN") {
1251a35e39caSPetr Hosek     Cmd = readProvideHidden(false, true);
1252ceabe80eSEugene Leviant   } else if (Tok == "PROVIDE_HIDDEN") {
1253a35e39caSPetr Hosek     Cmd = readProvideHidden(true, true);
1254ceabe80eSEugene Leviant   }
1255db741e72SEugene Leviant   if (Cmd && MakeAbsolute)
1256db741e72SEugene Leviant     Cmd->IsAbsolute = true;
1257ceabe80eSEugene Leviant   return Cmd;
1258ceabe80eSEugene Leviant }
1259ceabe80eSEugene Leviant 
126030835ea4SGeorge Rimar static uint64_t getSymbolValue(StringRef S, uint64_t Dot) {
126130835ea4SGeorge Rimar   if (S == ".")
126230835ea4SGeorge Rimar     return Dot;
1263884e786dSGeorge Rimar   return ScriptBase->getSymbolValue(S);
1264e32a3598SGeorge Rimar }
1265e32a3598SGeorge Rimar 
126630835ea4SGeorge Rimar SymbolAssignment *ScriptParser::readAssignment(StringRef Name) {
126730835ea4SGeorge Rimar   StringRef Op = next();
1268db741e72SEugene Leviant   bool IsAbsolute = false;
1269db741e72SEugene Leviant   Expr E;
127030835ea4SGeorge Rimar   assert(Op == "=" || Op == "+=");
1271db741e72SEugene Leviant   if (skip("ABSOLUTE")) {
1272db741e72SEugene Leviant     E = readParenExpr();
1273db741e72SEugene Leviant     IsAbsolute = true;
1274db741e72SEugene Leviant   } else {
1275db741e72SEugene Leviant     E = readExpr();
1276db741e72SEugene Leviant   }
127730835ea4SGeorge Rimar   if (Op == "+=")
127830835ea4SGeorge Rimar     E = [=](uint64_t Dot) { return getSymbolValue(Name, Dot) + E(Dot); };
1279db741e72SEugene Leviant   return new SymbolAssignment(Name, E, IsAbsolute);
128030835ea4SGeorge Rimar }
128130835ea4SGeorge Rimar 
128230835ea4SGeorge Rimar // This is an operator-precedence parser to parse a linker
128330835ea4SGeorge Rimar // script expression.
128430835ea4SGeorge Rimar Expr ScriptParser::readExpr() { return readExpr1(readPrimary(), 0); }
128530835ea4SGeorge Rimar 
128636c1cd23SRui Ueyama static Expr combine(StringRef Op, Expr L, Expr R) {
128736c1cd23SRui Ueyama   if (Op == "*")
128836c1cd23SRui Ueyama     return [=](uint64_t Dot) { return L(Dot) * R(Dot); };
128936c1cd23SRui Ueyama   if (Op == "/") {
129036c1cd23SRui Ueyama     return [=](uint64_t Dot) -> uint64_t {
129136c1cd23SRui Ueyama       uint64_t RHS = R(Dot);
129236c1cd23SRui Ueyama       if (RHS == 0) {
129336c1cd23SRui Ueyama         error("division by zero");
129436c1cd23SRui Ueyama         return 0;
129536c1cd23SRui Ueyama       }
129636c1cd23SRui Ueyama       return L(Dot) / RHS;
129736c1cd23SRui Ueyama     };
129836c1cd23SRui Ueyama   }
129936c1cd23SRui Ueyama   if (Op == "+")
130036c1cd23SRui Ueyama     return [=](uint64_t Dot) { return L(Dot) + R(Dot); };
130136c1cd23SRui Ueyama   if (Op == "-")
130236c1cd23SRui Ueyama     return [=](uint64_t Dot) { return L(Dot) - R(Dot); };
130336c1cd23SRui Ueyama   if (Op == "<")
130436c1cd23SRui Ueyama     return [=](uint64_t Dot) { return L(Dot) < R(Dot); };
130536c1cd23SRui Ueyama   if (Op == ">")
130636c1cd23SRui Ueyama     return [=](uint64_t Dot) { return L(Dot) > R(Dot); };
130736c1cd23SRui Ueyama   if (Op == ">=")
130836c1cd23SRui Ueyama     return [=](uint64_t Dot) { return L(Dot) >= R(Dot); };
130936c1cd23SRui Ueyama   if (Op == "<=")
131036c1cd23SRui Ueyama     return [=](uint64_t Dot) { return L(Dot) <= R(Dot); };
131136c1cd23SRui Ueyama   if (Op == "==")
131236c1cd23SRui Ueyama     return [=](uint64_t Dot) { return L(Dot) == R(Dot); };
131336c1cd23SRui Ueyama   if (Op == "!=")
131436c1cd23SRui Ueyama     return [=](uint64_t Dot) { return L(Dot) != R(Dot); };
131536c1cd23SRui Ueyama   if (Op == "&")
131636c1cd23SRui Ueyama     return [=](uint64_t Dot) { return L(Dot) & R(Dot); };
1317cc3dd629SRafael Espindola   if (Op == "|")
1318cc3dd629SRafael Espindola     return [=](uint64_t Dot) { return L(Dot) | R(Dot); };
131936c1cd23SRui Ueyama   llvm_unreachable("invalid operator");
132036c1cd23SRui Ueyama }
132136c1cd23SRui Ueyama 
1322708019c4SRui Ueyama // This is a part of the operator-precedence parser. This function
1323708019c4SRui Ueyama // assumes that the remaining token stream starts with an operator.
1324708019c4SRui Ueyama Expr ScriptParser::readExpr1(Expr Lhs, int MinPrec) {
1325708019c4SRui Ueyama   while (!atEOF() && !Error) {
1326708019c4SRui Ueyama     // Read an operator and an expression.
1327708019c4SRui Ueyama     StringRef Op1 = peek();
1328708019c4SRui Ueyama     if (Op1 == "?")
1329708019c4SRui Ueyama       return readTernary(Lhs);
1330708019c4SRui Ueyama     if (precedence(Op1) < MinPrec)
1331a31c91b1SEugene Leviant       break;
1332a31c91b1SEugene Leviant     next();
1333708019c4SRui Ueyama     Expr Rhs = readPrimary();
1334708019c4SRui Ueyama 
1335708019c4SRui Ueyama     // Evaluate the remaining part of the expression first if the
1336708019c4SRui Ueyama     // next operator has greater precedence than the previous one.
1337708019c4SRui Ueyama     // For example, if we have read "+" and "3", and if the next
1338708019c4SRui Ueyama     // operator is "*", then we'll evaluate 3 * ... part first.
1339708019c4SRui Ueyama     while (!atEOF()) {
1340708019c4SRui Ueyama       StringRef Op2 = peek();
1341708019c4SRui Ueyama       if (precedence(Op2) <= precedence(Op1))
1342eda81a1bSEugene Leviant         break;
1343708019c4SRui Ueyama       Rhs = readExpr1(Rhs, precedence(Op2));
1344eda81a1bSEugene Leviant     }
1345708019c4SRui Ueyama 
1346708019c4SRui Ueyama     Lhs = combine(Op1, Lhs, Rhs);
1347708019c4SRui Ueyama   }
1348708019c4SRui Ueyama   return Lhs;
1349708019c4SRui Ueyama }
1350708019c4SRui Ueyama 
1351708019c4SRui Ueyama uint64_t static getConstant(StringRef S) {
1352e2cc07bcSMichael J. Spencer   if (S == "COMMONPAGESIZE")
1353708019c4SRui Ueyama     return Target->PageSize;
1354e2cc07bcSMichael J. Spencer   if (S == "MAXPAGESIZE")
1355e2cc07bcSMichael J. Spencer     return Target->MaxPageSize;
1356708019c4SRui Ueyama   error("unknown constant: " + S);
1357708019c4SRui Ueyama   return 0;
1358708019c4SRui Ueyama }
1359708019c4SRui Ueyama 
1360626e0b08SRui Ueyama // Parses Tok as an integer. Returns true if successful.
1361626e0b08SRui Ueyama // It recognizes hexadecimal (prefixed with "0x" or suffixed with "H")
1362626e0b08SRui Ueyama // and decimal numbers. Decimal numbers may have "K" (kilo) or
1363626e0b08SRui Ueyama // "M" (mega) prefixes.
13649f2f7ad9SGeorge Rimar static bool readInteger(StringRef Tok, uint64_t &Result) {
1365eaeafb2bSSimon Atanasyan   if (Tok.startswith("-")) {
1366eaeafb2bSSimon Atanasyan     if (!readInteger(Tok.substr(1), Result))
1367eaeafb2bSSimon Atanasyan       return false;
1368eaeafb2bSSimon Atanasyan     Result = -Result;
1369eaeafb2bSSimon Atanasyan     return true;
1370eaeafb2bSSimon Atanasyan   }
13719f2f7ad9SGeorge Rimar   if (Tok.startswith_lower("0x"))
13729f2f7ad9SGeorge Rimar     return !Tok.substr(2).getAsInteger(16, Result);
13739f2f7ad9SGeorge Rimar   if (Tok.endswith_lower("H"))
13749f2f7ad9SGeorge Rimar     return !Tok.drop_back().getAsInteger(16, Result);
13759f2f7ad9SGeorge Rimar 
13769f2f7ad9SGeorge Rimar   int Suffix = 1;
13779f2f7ad9SGeorge Rimar   if (Tok.endswith_lower("K")) {
13789f2f7ad9SGeorge Rimar     Suffix = 1024;
13799f2f7ad9SGeorge Rimar     Tok = Tok.drop_back();
13809f2f7ad9SGeorge Rimar   } else if (Tok.endswith_lower("M")) {
13819f2f7ad9SGeorge Rimar     Suffix = 1024 * 1024;
13829f2f7ad9SGeorge Rimar     Tok = Tok.drop_back();
13839f2f7ad9SGeorge Rimar   }
13849f2f7ad9SGeorge Rimar   if (Tok.getAsInteger(10, Result))
13859f2f7ad9SGeorge Rimar     return false;
13869f2f7ad9SGeorge Rimar   Result *= Suffix;
13879f2f7ad9SGeorge Rimar   return true;
13889f2f7ad9SGeorge Rimar }
13899f2f7ad9SGeorge Rimar 
1390708019c4SRui Ueyama Expr ScriptParser::readPrimary() {
13916ad7dfccSRui Ueyama   if (peek() == "(")
13926ad7dfccSRui Ueyama     return readParenExpr();
1393708019c4SRui Ueyama 
13946ad7dfccSRui Ueyama   StringRef Tok = next();
1395708019c4SRui Ueyama 
1396eaeafb2bSSimon Atanasyan   if (Tok == "~") {
1397eaeafb2bSSimon Atanasyan     Expr E = readPrimary();
1398eaeafb2bSSimon Atanasyan     return [=](uint64_t Dot) { return ~E(Dot); };
1399eaeafb2bSSimon Atanasyan   }
1400eaeafb2bSSimon Atanasyan   if (Tok == "-") {
1401eaeafb2bSSimon Atanasyan     Expr E = readPrimary();
1402eaeafb2bSSimon Atanasyan     return [=](uint64_t Dot) { return -E(Dot); };
1403eaeafb2bSSimon Atanasyan   }
1404eaeafb2bSSimon Atanasyan 
1405708019c4SRui Ueyama   // Built-in functions are parsed here.
1406708019c4SRui Ueyama   // https://sourceware.org/binutils/docs/ld/Builtin-Functions.html.
140796659df0SGeorge Rimar   if (Tok == "ADDR") {
140896659df0SGeorge Rimar     expect("(");
140996659df0SGeorge Rimar     StringRef Name = next();
141096659df0SGeorge Rimar     expect(")");
1411884e786dSGeorge Rimar     return
1412884e786dSGeorge Rimar         [=](uint64_t Dot) { return ScriptBase->getOutputSectionAddress(Name); };
141396659df0SGeorge Rimar   }
1414eefa758eSGeorge Rimar   if (Tok == "ASSERT")
1415eefa758eSGeorge Rimar     return readAssert();
1416708019c4SRui Ueyama   if (Tok == "ALIGN") {
14176ad7dfccSRui Ueyama     Expr E = readParenExpr();
1418708019c4SRui Ueyama     return [=](uint64_t Dot) { return alignTo(Dot, E(Dot)); };
1419708019c4SRui Ueyama   }
1420708019c4SRui Ueyama   if (Tok == "CONSTANT") {
1421708019c4SRui Ueyama     expect("(");
1422708019c4SRui Ueyama     StringRef Tok = next();
1423708019c4SRui Ueyama     expect(")");
1424708019c4SRui Ueyama     return [=](uint64_t Dot) { return getConstant(Tok); };
1425708019c4SRui Ueyama   }
142654c145ceSRafael Espindola   if (Tok == "SEGMENT_START") {
142754c145ceSRafael Espindola     expect("(");
142854c145ceSRafael Espindola     next();
142954c145ceSRafael Espindola     expect(",");
143054c145ceSRafael Espindola     uint64_t Val;
14313adbbc38SRafael Espindola     if (next().getAsInteger(0, Val))
14323adbbc38SRafael Espindola       setError("integer expected");
143354c145ceSRafael Espindola     expect(")");
143454c145ceSRafael Espindola     return [=](uint64_t Dot) { return Val; };
143554c145ceSRafael Espindola   }
1436708019c4SRui Ueyama   if (Tok == "DATA_SEGMENT_ALIGN") {
1437708019c4SRui Ueyama     expect("(");
1438708019c4SRui Ueyama     Expr E = readExpr();
1439708019c4SRui Ueyama     expect(",");
1440708019c4SRui Ueyama     readExpr();
1441708019c4SRui Ueyama     expect(")");
1442f7791bb9SRui Ueyama     return [=](uint64_t Dot) { return alignTo(Dot, E(Dot)); };
1443708019c4SRui Ueyama   }
1444708019c4SRui Ueyama   if (Tok == "DATA_SEGMENT_END") {
1445708019c4SRui Ueyama     expect("(");
1446708019c4SRui Ueyama     expect(".");
1447708019c4SRui Ueyama     expect(")");
1448708019c4SRui Ueyama     return [](uint64_t Dot) { return Dot; };
1449708019c4SRui Ueyama   }
1450276b4e64SGeorge Rimar   // GNU linkers implements more complicated logic to handle
1451276b4e64SGeorge Rimar   // DATA_SEGMENT_RELRO_END. We instead ignore the arguments and just align to
1452276b4e64SGeorge Rimar   // the next page boundary for simplicity.
1453276b4e64SGeorge Rimar   if (Tok == "DATA_SEGMENT_RELRO_END") {
1454276b4e64SGeorge Rimar     expect("(");
145597bdc722SRafael Espindola     readExpr();
1456276b4e64SGeorge Rimar     expect(",");
1457276b4e64SGeorge Rimar     readExpr();
1458276b4e64SGeorge Rimar     expect(")");
1459276b4e64SGeorge Rimar     return [](uint64_t Dot) { return alignTo(Dot, Target->PageSize); };
1460276b4e64SGeorge Rimar   }
14619e69450eSGeorge Rimar   if (Tok == "SIZEOF") {
14629e69450eSGeorge Rimar     expect("(");
14639e69450eSGeorge Rimar     StringRef Name = next();
14649e69450eSGeorge Rimar     expect(")");
1465884e786dSGeorge Rimar     return [=](uint64_t Dot) { return ScriptBase->getOutputSectionSize(Name); };
14669e69450eSGeorge Rimar   }
146736fac7f0SEugene Leviant   if (Tok == "ALIGNOF") {
146836fac7f0SEugene Leviant     expect("(");
146936fac7f0SEugene Leviant     StringRef Name = next();
147036fac7f0SEugene Leviant     expect(")");
147136fac7f0SEugene Leviant     return
147236fac7f0SEugene Leviant         [=](uint64_t Dot) { return ScriptBase->getOutputSectionAlign(Name); };
147336fac7f0SEugene Leviant   }
1474e32a3598SGeorge Rimar   if (Tok == "SIZEOF_HEADERS")
1475884e786dSGeorge Rimar     return [=](uint64_t Dot) { return ScriptBase->getHeaderSize(); };
1476708019c4SRui Ueyama 
14779f2f7ad9SGeorge Rimar   // Tok is a literal number.
14789f2f7ad9SGeorge Rimar   uint64_t V;
14799f2f7ad9SGeorge Rimar   if (readInteger(Tok, V))
14809f2f7ad9SGeorge Rimar     return [=](uint64_t Dot) { return V; };
14819f2f7ad9SGeorge Rimar 
14829f2f7ad9SGeorge Rimar   // Tok is a symbol name.
148330835ea4SGeorge Rimar   if (Tok != "." && !isValidCIdentifier(Tok))
1484708019c4SRui Ueyama     setError("malformed number: " + Tok);
148530835ea4SGeorge Rimar   return [=](uint64_t Dot) { return getSymbolValue(Tok, Dot); };
1486a9c5a528SGeorge Rimar }
1487708019c4SRui Ueyama 
1488708019c4SRui Ueyama Expr ScriptParser::readTernary(Expr Cond) {
1489708019c4SRui Ueyama   next();
1490708019c4SRui Ueyama   Expr L = readExpr();
1491708019c4SRui Ueyama   expect(":");
1492708019c4SRui Ueyama   Expr R = readExpr();
1493708019c4SRui Ueyama   return [=](uint64_t Dot) { return Cond(Dot) ? L(Dot) : R(Dot); };
1494708019c4SRui Ueyama }
1495708019c4SRui Ueyama 
14966ad7dfccSRui Ueyama Expr ScriptParser::readParenExpr() {
14976ad7dfccSRui Ueyama   expect("(");
14986ad7dfccSRui Ueyama   Expr E = readExpr();
14996ad7dfccSRui Ueyama   expect(")");
15006ad7dfccSRui Ueyama   return E;
15016ad7dfccSRui Ueyama }
15026ad7dfccSRui Ueyama 
1503bbe38602SEugene Leviant std::vector<StringRef> ScriptParser::readOutputSectionPhdrs() {
1504bbe38602SEugene Leviant   std::vector<StringRef> Phdrs;
1505bbe38602SEugene Leviant   while (!Error && peek().startswith(":")) {
1506bbe38602SEugene Leviant     StringRef Tok = next();
1507bbe38602SEugene Leviant     Tok = (Tok.size() == 1) ? next() : Tok.substr(1);
1508bbe38602SEugene Leviant     if (Tok.empty()) {
1509bbe38602SEugene Leviant       setError("section header name is empty");
1510bbe38602SEugene Leviant       break;
1511bbe38602SEugene Leviant     }
1512bbe38602SEugene Leviant     Phdrs.push_back(Tok);
1513bbe38602SEugene Leviant   }
1514bbe38602SEugene Leviant   return Phdrs;
1515bbe38602SEugene Leviant }
1516bbe38602SEugene Leviant 
1517bbe38602SEugene Leviant unsigned ScriptParser::readPhdrType() {
1518bbe38602SEugene Leviant   StringRef Tok = next();
1519b0f6c590SRui Ueyama   unsigned Ret = StringSwitch<unsigned>(Tok)
1520b0f6c590SRui Ueyama                      .Case("PT_NULL", PT_NULL)
1521b0f6c590SRui Ueyama                      .Case("PT_LOAD", PT_LOAD)
1522b0f6c590SRui Ueyama                      .Case("PT_DYNAMIC", PT_DYNAMIC)
1523b0f6c590SRui Ueyama                      .Case("PT_INTERP", PT_INTERP)
1524b0f6c590SRui Ueyama                      .Case("PT_NOTE", PT_NOTE)
1525b0f6c590SRui Ueyama                      .Case("PT_SHLIB", PT_SHLIB)
1526b0f6c590SRui Ueyama                      .Case("PT_PHDR", PT_PHDR)
1527b0f6c590SRui Ueyama                      .Case("PT_TLS", PT_TLS)
1528b0f6c590SRui Ueyama                      .Case("PT_GNU_EH_FRAME", PT_GNU_EH_FRAME)
1529b0f6c590SRui Ueyama                      .Case("PT_GNU_STACK", PT_GNU_STACK)
1530b0f6c590SRui Ueyama                      .Case("PT_GNU_RELRO", PT_GNU_RELRO)
1531b0f6c590SRui Ueyama                      .Default(-1);
1532bbe38602SEugene Leviant 
1533b0f6c590SRui Ueyama   if (Ret == (unsigned)-1) {
1534b0f6c590SRui Ueyama     setError("invalid program header type: " + Tok);
1535b0f6c590SRui Ueyama     return PT_NULL;
1536b0f6c590SRui Ueyama   }
1537b0f6c590SRui Ueyama   return Ret;
1538bbe38602SEugene Leviant }
1539bbe38602SEugene Leviant 
154095769b4aSRui Ueyama void ScriptParser::readVersionDeclaration(StringRef VerStr) {
154120b6598cSGeorge Rimar   // Identifiers start at 2 because 0 and 1 are reserved
154220b6598cSGeorge Rimar   // for VER_NDX_LOCAL and VER_NDX_GLOBAL constants.
154320b6598cSGeorge Rimar   size_t VersionId = Config->VersionDefinitions.size() + 2;
154420b6598cSGeorge Rimar   Config->VersionDefinitions.push_back({VerStr, VersionId});
154520b6598cSGeorge Rimar 
154620b6598cSGeorge Rimar   if (skip("global:") || peek() != "local:")
154720b6598cSGeorge Rimar     readGlobal(VerStr);
154820b6598cSGeorge Rimar   if (skip("local:"))
154920b6598cSGeorge Rimar     readLocal();
155020b6598cSGeorge Rimar   expect("}");
155120b6598cSGeorge Rimar 
155220b6598cSGeorge Rimar   // Each version may have a parent version. For example, "Ver2" defined as
155320b6598cSGeorge Rimar   // "Ver2 { global: foo; local: *; } Ver1;" has "Ver1" as a parent. This
155420b6598cSGeorge Rimar   // version hierarchy is, probably against your instinct, purely for human; the
155520b6598cSGeorge Rimar   // runtime doesn't care about them at all. In LLD, we simply skip the token.
155620b6598cSGeorge Rimar   if (!VerStr.empty() && peek() != ";")
155720b6598cSGeorge Rimar     next();
155820b6598cSGeorge Rimar   expect(";");
155920b6598cSGeorge Rimar }
156020b6598cSGeorge Rimar 
156120b6598cSGeorge Rimar void ScriptParser::readLocal() {
156220b6598cSGeorge Rimar   Config->DefaultSymbolVersion = VER_NDX_LOCAL;
156320b6598cSGeorge Rimar   expect("*");
156420b6598cSGeorge Rimar   expect(";");
156520b6598cSGeorge Rimar }
156620b6598cSGeorge Rimar 
156720b6598cSGeorge Rimar void ScriptParser::readExtern(std::vector<SymbolVersion> *Globals) {
1568cd574a5eSGeorge Rimar   expect("\"C++\"");
156920b6598cSGeorge Rimar   expect("{");
157020b6598cSGeorge Rimar 
157120b6598cSGeorge Rimar   for (;;) {
157220b6598cSGeorge Rimar     if (peek() == "}" || Error)
157320b6598cSGeorge Rimar       break;
1574cd574a5eSGeorge Rimar     bool HasWildcard = !peek().startswith("\"") && hasWildcard(peek());
1575cd574a5eSGeorge Rimar     Globals->push_back({unquote(next()), true, HasWildcard});
157620b6598cSGeorge Rimar     expect(";");
157720b6598cSGeorge Rimar   }
157820b6598cSGeorge Rimar 
157920b6598cSGeorge Rimar   expect("}");
158020b6598cSGeorge Rimar   expect(";");
158120b6598cSGeorge Rimar }
158220b6598cSGeorge Rimar 
158320b6598cSGeorge Rimar void ScriptParser::readGlobal(StringRef VerStr) {
158420b6598cSGeorge Rimar   std::vector<SymbolVersion> *Globals;
158520b6598cSGeorge Rimar   if (VerStr.empty())
158620b6598cSGeorge Rimar     Globals = &Config->VersionScriptGlobals;
158720b6598cSGeorge Rimar   else
158820b6598cSGeorge Rimar     Globals = &Config->VersionDefinitions.back().Globals;
158920b6598cSGeorge Rimar 
159020b6598cSGeorge Rimar   for (;;) {
159120b6598cSGeorge Rimar     if (skip("extern"))
159220b6598cSGeorge Rimar       readExtern(Globals);
159320b6598cSGeorge Rimar 
159420b6598cSGeorge Rimar     StringRef Cur = peek();
159520b6598cSGeorge Rimar     if (Cur == "}" || Cur == "local:" || Error)
159620b6598cSGeorge Rimar       return;
159720b6598cSGeorge Rimar     next();
1598cd574a5eSGeorge Rimar     Globals->push_back({unquote(Cur), false, hasWildcard(Cur)});
159920b6598cSGeorge Rimar     expect(";");
160020b6598cSGeorge Rimar   }
160120b6598cSGeorge Rimar }
160220b6598cSGeorge Rimar 
160316b0cc9eSSimon Atanasyan static bool isUnderSysroot(StringRef Path) {
160416b0cc9eSSimon Atanasyan   if (Config->Sysroot == "")
160516b0cc9eSSimon Atanasyan     return false;
160616b0cc9eSSimon Atanasyan   for (; !Path.empty(); Path = sys::path::parent_path(Path))
160716b0cc9eSSimon Atanasyan     if (sys::fs::equivalent(Config->Sysroot, Path))
160816b0cc9eSSimon Atanasyan       return true;
160916b0cc9eSSimon Atanasyan   return false;
161016b0cc9eSSimon Atanasyan }
161116b0cc9eSSimon Atanasyan 
161207320e40SRui Ueyama void elf::readLinkerScript(MemoryBufferRef MB) {
161316b0cc9eSSimon Atanasyan   StringRef Path = MB.getBufferIdentifier();
161420b6598cSGeorge Rimar   ScriptParser(MB.getBuffer(), isUnderSysroot(Path)).readLinkerScript();
161520b6598cSGeorge Rimar }
161620b6598cSGeorge Rimar 
161720b6598cSGeorge Rimar void elf::readVersionScript(MemoryBufferRef MB) {
161820b6598cSGeorge Rimar   ScriptParser(MB.getBuffer(), false).readVersionScript();
1619f7c5fbb1SRui Ueyama }
16201ebc8ed7SRui Ueyama 
162107320e40SRui Ueyama template class elf::LinkerScript<ELF32LE>;
162207320e40SRui Ueyama template class elf::LinkerScript<ELF32BE>;
162307320e40SRui Ueyama template class elf::LinkerScript<ELF64LE>;
162407320e40SRui Ueyama template class elf::LinkerScript<ELF64BE>;
1625