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.
11f7c5fbb1SRui Ueyama //
12f7c5fbb1SRui Ueyama //===----------------------------------------------------------------------===//
13f7c5fbb1SRui Ueyama 
14717677afSRui Ueyama #include "LinkerScript.h"
15f7c5fbb1SRui Ueyama #include "Config.h"
16f7c5fbb1SRui Ueyama #include "Driver.h"
171ebc8ed7SRui Ueyama #include "InputSection.h"
189381eb10SRui Ueyama #include "Memory.h"
19652852c5SGeorge Rimar #include "OutputSections.h"
20e77b5bf6SAdhemerval Zanella #include "ScriptParser.h"
2193c9af42SRui Ueyama #include "Strings.h"
22f7c5fbb1SRui Ueyama #include "SymbolTable.h"
2355518e7dSRui Ueyama #include "Symbols.h"
243fb5a6dcSGeorge Rimar #include "SyntheticSections.h"
25467c4d55SEugene Leviant #include "Target.h"
26bbe38602SEugene Leviant #include "Writer.h"
2722886a28SEugene Zelenko #include "llvm/ADT/STLExtras.h"
288c6a5aafSRui Ueyama #include "llvm/ADT/SmallString.h"
2922886a28SEugene Zelenko #include "llvm/ADT/StringRef.h"
30960504b9SRui Ueyama #include "llvm/ADT/StringSwitch.h"
3122886a28SEugene Zelenko #include "llvm/Support/Casting.h"
32652852c5SGeorge Rimar #include "llvm/Support/ELF.h"
3322886a28SEugene Zelenko #include "llvm/Support/Endian.h"
3422886a28SEugene Zelenko #include "llvm/Support/ErrorHandling.h"
35f7c5fbb1SRui Ueyama #include "llvm/Support/FileSystem.h"
3622886a28SEugene Zelenko #include "llvm/Support/MathExtras.h"
37f03f3cc1SRui Ueyama #include "llvm/Support/Path.h"
3822886a28SEugene Zelenko #include <algorithm>
3922886a28SEugene Zelenko #include <cassert>
4022886a28SEugene Zelenko #include <cstddef>
4122886a28SEugene Zelenko #include <cstdint>
4222886a28SEugene Zelenko #include <iterator>
4322886a28SEugene Zelenko #include <limits>
4422886a28SEugene Zelenko #include <memory>
4522886a28SEugene Zelenko #include <string>
4622886a28SEugene Zelenko #include <tuple>
4722886a28SEugene Zelenko #include <vector>
48f7c5fbb1SRui Ueyama 
49f7c5fbb1SRui Ueyama using namespace llvm;
50652852c5SGeorge Rimar using namespace llvm::ELF;
511ebc8ed7SRui Ueyama using namespace llvm::object;
52e38cbab5SGeorge Rimar using namespace llvm::support::endian;
53f7c5fbb1SRui Ueyama using namespace lld;
54e0df00b9SRafael Espindola using namespace lld::elf;
55f7c5fbb1SRui Ueyama 
56884e786dSGeorge Rimar LinkerScriptBase *elf::ScriptBase;
5707320e40SRui Ueyama ScriptConfiguration *elf::ScriptConfig;
58717677afSRui Ueyama 
598f1f3c40SMeador Inge template <class ELFT> static SymbolBody *addRegular(SymbolAssignment *Cmd) {
603dabfc6bSRafael Espindola   uint8_t Visibility = Cmd->Hidden ? STV_HIDDEN : STV_DEFAULT;
618f1f3c40SMeador Inge   Symbol *Sym = Symtab<ELFT>::X->addUndefined(
628f1f3c40SMeador Inge       Cmd->Name, /*IsLocal=*/false, STB_GLOBAL, Visibility,
638f1f3c40SMeador Inge       /*Type*/ 0,
648f1f3c40SMeador Inge       /*CanOmitFromDynSym*/ false, /*File*/ nullptr);
6520d03194SEugene Leviant 
668f1f3c40SMeador Inge   replaceBody<DefinedRegular<ELFT>>(Sym, Cmd->Name, /*IsLocal=*/false,
678f1f3c40SMeador Inge                                     Visibility, STT_NOTYPE, 0, 0, nullptr,
688f1f3c40SMeador Inge                                     nullptr);
698f1f3c40SMeador Inge   return Sym->body();
70ceabe80eSEugene Leviant }
71ceabe80eSEugene Leviant 
728f1f3c40SMeador Inge template <class ELFT> static SymbolBody *addSynthetic(SymbolAssignment *Cmd) {
738f1f3c40SMeador Inge   uint8_t Visibility = Cmd->Hidden ? STV_HIDDEN : STV_DEFAULT;
74afaa9343SEugene Leviant   const OutputSectionBase *Sec =
75afaa9343SEugene Leviant       ScriptConfig->HasSections ? nullptr : Cmd->Expression.Section();
768f1f3c40SMeador Inge   Symbol *Sym = Symtab<ELFT>::X->addUndefined(
778f1f3c40SMeador Inge       Cmd->Name, /*IsLocal=*/false, STB_GLOBAL, Visibility,
788f1f3c40SMeador Inge       /*Type*/ 0,
798f1f3c40SMeador Inge       /*CanOmitFromDynSym*/ false, /*File*/ nullptr);
80afaa9343SEugene Leviant 
818f1f3c40SMeador Inge   replaceBody<DefinedSynthetic>(Sym, Cmd->Name, 0, Sec);
828f1f3c40SMeador Inge   return Sym->body();
83ceabe80eSEugene Leviant }
84ceabe80eSEugene Leviant 
8522375f24SRui Ueyama static bool isUnderSysroot(StringRef Path) {
8622375f24SRui Ueyama   if (Config->Sysroot == "")
8722375f24SRui Ueyama     return false;
8822375f24SRui Ueyama   for (; !Path.empty(); Path = sys::path::parent_path(Path))
8922375f24SRui Ueyama     if (sys::fs::equivalent(Config->Sysroot, Path))
9022375f24SRui Ueyama       return true;
9122375f24SRui Ueyama   return false;
9222375f24SRui Ueyama }
9322375f24SRui Ueyama 
94b2b70975SGeorge Rimar // Sets value of a symbol. Two kinds of symbols are processed: synthetic
95b2b70975SGeorge Rimar // symbols, whose value is an offset from beginning of section and regular
96b2b70975SGeorge Rimar // symbols whose value is absolute.
97b2b70975SGeorge Rimar template <class ELFT>
98b2b70975SGeorge Rimar static void assignSymbol(SymbolAssignment *Cmd, typename ELFT::uint Dot = 0) {
99b2b70975SGeorge Rimar   if (!Cmd->Sym)
1008f1f3c40SMeador Inge     return;
1018f1f3c40SMeador Inge 
102b2b70975SGeorge Rimar   if (auto *Body = dyn_cast<DefinedSynthetic>(Cmd->Sym)) {
103b2b70975SGeorge Rimar     Body->Section = Cmd->Expression.Section();
104193b158bSRafael Espindola     if (Body->Section)
105193b158bSRafael Espindola       Body->Value = Cmd->Expression(Dot) - Body->Section->Addr;
106b2b70975SGeorge Rimar     return;
107db741e72SEugene Leviant   }
108b2b70975SGeorge Rimar 
109b2b70975SGeorge Rimar   cast<DefinedRegular<ELFT>>(Cmd->Sym)->Value = Cmd->Expression(Dot);
1108f1f3c40SMeador Inge }
1118f1f3c40SMeador Inge 
1128f1f3c40SMeador Inge template <class ELFT> static void addSymbol(SymbolAssignment *Cmd) {
1131602421cSRui Ueyama   if (Cmd->Name == ".")
1148f1f3c40SMeador Inge     return;
1158f1f3c40SMeador Inge 
1168f1f3c40SMeador Inge   // If a symbol was in PROVIDE(), we need to define it only when
1178f1f3c40SMeador Inge   // it is a referenced undefined symbol.
1181602421cSRui Ueyama   SymbolBody *B = Symtab<ELFT>::X->find(Cmd->Name);
1198f1f3c40SMeador Inge   if (Cmd->Provide && (!B || B->isDefined()))
1208f1f3c40SMeador Inge     return;
1218f1f3c40SMeador Inge 
1228f1f3c40SMeador Inge   // Otherwise, create a new symbol if one does not exist or an
1238f1f3c40SMeador Inge   // undefined one does exist.
1248f1f3c40SMeador Inge   if (Cmd->Expression.IsAbsolute())
1258f1f3c40SMeador Inge     Cmd->Sym = addRegular<ELFT>(Cmd);
1268f1f3c40SMeador Inge   else
1278f1f3c40SMeador Inge     Cmd->Sym = addSynthetic<ELFT>(Cmd);
128b2b70975SGeorge Rimar 
129b2b70975SGeorge Rimar   // If there are sections, then let the value be assigned later in
130b2b70975SGeorge Rimar   // `assignAddresses`.
131b2b70975SGeorge Rimar   if (!ScriptConfig->HasSections)
1328f1f3c40SMeador Inge     assignSymbol<ELFT>(Cmd);
133ceabe80eSEugene Leviant }
134ceabe80eSEugene Leviant 
135076fe157SGeorge Rimar bool SymbolAssignment::classof(const BaseCommand *C) {
136076fe157SGeorge Rimar   return C->Kind == AssignmentKind;
137076fe157SGeorge Rimar }
138076fe157SGeorge Rimar 
139076fe157SGeorge Rimar bool OutputSectionCommand::classof(const BaseCommand *C) {
140076fe157SGeorge Rimar   return C->Kind == OutputSectionKind;
141076fe157SGeorge Rimar }
142076fe157SGeorge Rimar 
143eea3114fSGeorge Rimar bool InputSectionDescription::classof(const BaseCommand *C) {
144eea3114fSGeorge Rimar   return C->Kind == InputSectionKind;
145eea3114fSGeorge Rimar }
146eea3114fSGeorge Rimar 
147eefa758eSGeorge Rimar bool AssertCommand::classof(const BaseCommand *C) {
148eefa758eSGeorge Rimar   return C->Kind == AssertKind;
149eefa758eSGeorge Rimar }
150eefa758eSGeorge Rimar 
151e38cbab5SGeorge Rimar bool BytesDataCommand::classof(const BaseCommand *C) {
152e38cbab5SGeorge Rimar   return C->Kind == BytesDataKind;
153e38cbab5SGeorge Rimar }
154e38cbab5SGeorge Rimar 
15522886a28SEugene Zelenko template <class ELFT> LinkerScript<ELFT>::LinkerScript() = default;
15622886a28SEugene Zelenko template <class ELFT> LinkerScript<ELFT>::~LinkerScript() = default;
157f34d0e08SRui Ueyama 
158e0be2901SRui Ueyama template <class ELFT> static StringRef basename(InputSectionBase<ELFT> *S) {
159e0be2901SRui Ueyama   if (S->getFile())
160e0be2901SRui Ueyama     return sys::path::filename(S->getFile()->getName());
161e0be2901SRui Ueyama   return "";
162e0be2901SRui Ueyama }
163e0be2901SRui Ueyama 
16407320e40SRui Ueyama template <class ELFT>
16507320e40SRui Ueyama bool LinkerScript<ELFT>::shouldKeep(InputSectionBase<ELFT> *S) {
166e0be2901SRui Ueyama   for (InputSectionDescription *ID : Opt.KeptSections)
167e0be2901SRui Ueyama     if (ID->FilePat.match(basename(S)))
168cf43f179SEugene Leviant       for (SectionPattern &P : ID->SectionPatterns)
169f91282e1SRui Ueyama         if (P.SectionPat.match(S->Name))
170eea3114fSGeorge Rimar           return true;
171eea3114fSGeorge Rimar   return false;
172eea3114fSGeorge Rimar }
173eea3114fSGeorge Rimar 
174575208caSGeorge Rimar static bool comparePriority(InputSectionData *A, InputSectionData *B) {
175575208caSGeorge Rimar   return getPriority(A->Name) < getPriority(B->Name);
176575208caSGeorge Rimar }
177575208caSGeorge Rimar 
178c0028d3dSRafael Espindola static bool compareName(InputSectionData *A, InputSectionData *B) {
179042a3f20SRafael Espindola   return A->Name < B->Name;
1800702c4e8SGeorge Rimar }
181742c3836SRui Ueyama 
182c0028d3dSRafael Espindola static bool compareAlignment(InputSectionData *A, InputSectionData *B) {
183742c3836SRui Ueyama   // ">" is not a mistake. Larger alignments are placed before smaller
184742c3836SRui Ueyama   // alignments in order to reduce the amount of padding necessary.
185742c3836SRui Ueyama   // This is compatible with GNU.
186742c3836SRui Ueyama   return A->Alignment > B->Alignment;
187742c3836SRui Ueyama }
188742c3836SRui Ueyama 
189c0028d3dSRafael Espindola static std::function<bool(InputSectionData *, InputSectionData *)>
190be394db3SGeorge Rimar getComparator(SortSectionPolicy K) {
191be394db3SGeorge Rimar   switch (K) {
192be394db3SGeorge Rimar   case SortSectionPolicy::Alignment:
193c0028d3dSRafael Espindola     return compareAlignment;
194be394db3SGeorge Rimar   case SortSectionPolicy::Name:
195be394db3SGeorge Rimar     return compareName;
196be394db3SGeorge Rimar   case SortSectionPolicy::Priority:
197be394db3SGeorge Rimar     return comparePriority;
198be394db3SGeorge Rimar   default:
199be394db3SGeorge Rimar     llvm_unreachable("unknown sort policy");
200be394db3SGeorge Rimar   }
201742c3836SRui Ueyama }
2020702c4e8SGeorge Rimar 
20348c3f1ceSRui Ueyama template <class ELFT>
204e71a3f8aSRafael Espindola static bool matchConstraints(ArrayRef<InputSectionBase<ELFT> *> Sections,
20506ae6836SGeorge Rimar                              ConstraintKind Kind) {
2068f66df92SGeorge Rimar   if (Kind == ConstraintKind::NoConstraint)
2078f66df92SGeorge Rimar     return true;
208e746e52cSRafael Espindola   bool IsRW = llvm::any_of(Sections, [=](InputSectionData *Sec2) {
209d3190795SRafael Espindola     auto *Sec = static_cast<InputSectionBase<ELFT> *>(Sec2);
2101854a8ebSRafael Espindola     return Sec->Flags & SHF_WRITE;
21106ae6836SGeorge Rimar   });
212e746e52cSRafael Espindola   return (IsRW && Kind == ConstraintKind::ReadWrite) ||
213e746e52cSRafael Espindola          (!IsRW && Kind == ConstraintKind::ReadOnly);
21406ae6836SGeorge Rimar }
21506ae6836SGeorge Rimar 
21607171f21SGeorge Rimar static void sortSections(InputSectionData **Begin, InputSectionData **End,
217ee924709SRui Ueyama                          SortSectionPolicy K) {
218ee924709SRui Ueyama   if (K != SortSectionPolicy::Default && K != SortSectionPolicy::None)
21907171f21SGeorge Rimar     std::stable_sort(Begin, End, getComparator(K));
220ee924709SRui Ueyama }
221ee924709SRui Ueyama 
222d3190795SRafael Espindola // Compute and remember which sections the InputSectionDescription matches.
223be94e1b6SRafael Espindola template <class ELFT>
224e71a3f8aSRafael Espindola void LinkerScript<ELFT>::computeInputSections(InputSectionDescription *I) {
2254dc07becSRui Ueyama   // Collects all sections that satisfy constraints of I
2264dc07becSRui Ueyama   // and attach them to I.
2274dc07becSRui Ueyama   for (SectionPattern &Pat : I->SectionPatterns) {
22807171f21SGeorge Rimar     size_t SizeBefore = I->Sections.size();
2298c6a5aafSRui Ueyama 
2308c6a5aafSRui Ueyama     for (InputSectionBase<ELFT> *S : Symtab<ELFT>::X->Sections) {
231f94efdddSRui Ueyama       if (!S->Live || S->Assigned)
2328c6a5aafSRui Ueyama         continue;
2338c6a5aafSRui Ueyama 
234e0be2901SRui Ueyama       StringRef Filename = basename(S);
235e0be2901SRui Ueyama       if (!I->FilePat.match(Filename) || Pat.ExcludedFilePat.match(Filename))
236e0be2901SRui Ueyama         continue;
237e0be2901SRui Ueyama       if (!Pat.SectionPat.match(S->Name))
238e0be2901SRui Ueyama         continue;
239d3190795SRafael Espindola       I->Sections.push_back(S);
240f94efdddSRui Ueyama       S->Assigned = true;
241f94efdddSRui Ueyama     }
242d3190795SRafael Espindola 
243ee924709SRui Ueyama     // Sort sections as instructed by SORT-family commands and --sort-section
244ee924709SRui Ueyama     // option. Because SORT-family commands can be nested at most two depth
245ee924709SRui Ueyama     // (e.g. SORT_BY_NAME(SORT_BY_ALIGNMENT(.text.*))) and because the command
246ee924709SRui Ueyama     // line option is respected even if a SORT command is given, the exact
247ee924709SRui Ueyama     // behavior we have here is a bit complicated. Here are the rules.
248ee924709SRui Ueyama     //
249ee924709SRui Ueyama     // 1. If two SORT commands are given, --sort-section is ignored.
250ee924709SRui Ueyama     // 2. If one SORT command is given, and if it is not SORT_NONE,
251ee924709SRui Ueyama     //    --sort-section is handled as an inner SORT command.
252ee924709SRui Ueyama     // 3. If one SORT command is given, and if it is SORT_NONE, don't sort.
253ee924709SRui Ueyama     // 4. If no SORT command is given, sort according to --sort-section.
25407171f21SGeorge Rimar     InputSectionData **Begin = I->Sections.data() + SizeBefore;
25507171f21SGeorge Rimar     InputSectionData **End = I->Sections.data() + I->Sections.size();
25607171f21SGeorge Rimar     if (Pat.SortOuter != SortSectionPolicy::None) {
25707171f21SGeorge Rimar       if (Pat.SortInner == SortSectionPolicy::Default)
25807171f21SGeorge Rimar         sortSections(Begin, End, Config->SortSection);
259ee924709SRui Ueyama       else
26007171f21SGeorge Rimar         sortSections(Begin, End, Pat.SortInner);
26107171f21SGeorge Rimar       sortSections(Begin, End, Pat.SortOuter);
26207171f21SGeorge Rimar     }
263ee924709SRui Ueyama   }
264be94e1b6SRafael Espindola }
265be94e1b6SRafael Espindola 
266be94e1b6SRafael Espindola template <class ELFT>
267be94e1b6SRafael Espindola void LinkerScript<ELFT>::discard(ArrayRef<InputSectionBase<ELFT> *> V) {
268be94e1b6SRafael Espindola   for (InputSectionBase<ELFT> *S : V) {
269be94e1b6SRafael Espindola     S->Live = false;
270be94e1b6SRafael Espindola     reportDiscarded(S);
271be94e1b6SRafael Espindola   }
272be94e1b6SRafael Espindola }
273be94e1b6SRafael Espindola 
27406ae6836SGeorge Rimar template <class ELFT>
2750b9ce6a4SRui Ueyama std::vector<InputSectionBase<ELFT> *>
27606ae6836SGeorge Rimar LinkerScript<ELFT>::createInputSectionList(OutputSectionCommand &OutCmd) {
2770b9ce6a4SRui Ueyama   std::vector<InputSectionBase<ELFT> *> Ret;
278e7f912cdSRui Ueyama 
27906ae6836SGeorge Rimar   for (const std::unique_ptr<BaseCommand> &Base : OutCmd.Commands) {
2807c3ff2ebSRafael Espindola     auto *Cmd = dyn_cast<InputSectionDescription>(Base.get());
2817c3ff2ebSRafael Espindola     if (!Cmd)
2820b9ce6a4SRui Ueyama       continue;
283e71a3f8aSRafael Espindola     computeInputSections(Cmd);
284d3190795SRafael Espindola     for (InputSectionData *S : Cmd->Sections)
285d3190795SRafael Espindola       Ret.push_back(static_cast<InputSectionBase<ELFT> *>(S));
2860b9ce6a4SRui Ueyama   }
287e71a3f8aSRafael Espindola 
2880b9ce6a4SRui Ueyama   return Ret;
2890b9ce6a4SRui Ueyama }
2900b9ce6a4SRui Ueyama 
291e5d3ca50SPetr Hosek template <class ELFT>
29220d03194SEugene Leviant void LinkerScript<ELFT>::addSection(OutputSectionFactory<ELFT> &Factory,
29320d03194SEugene Leviant                                     InputSectionBase<ELFT> *Sec,
29420d03194SEugene Leviant                                     StringRef Name) {
295e08e78dfSRafael Espindola   OutputSectionBase *OutSec;
29628c1597aSRafael Espindola   bool IsNew;
297fe12450eSRafael Espindola   std::tie(OutSec, IsNew) = Factory.create(Sec, Name);
29828c1597aSRafael Espindola   if (IsNew)
29928c1597aSRafael Espindola     OutputSections->push_back(OutSec);
30020d03194SEugene Leviant   OutSec->addSection(Sec);
30120d03194SEugene Leviant }
30220d03194SEugene Leviant 
30320d03194SEugene Leviant template <class ELFT>
30420d03194SEugene Leviant void LinkerScript<ELFT>::processCommands(OutputSectionFactory<ELFT> &Factory) {
3057c3ff2ebSRafael Espindola   for (unsigned I = 0; I < Opt.Commands.size(); ++I) {
3067c3ff2ebSRafael Espindola     auto Iter = Opt.Commands.begin() + I;
3077c3ff2ebSRafael Espindola     const std::unique_ptr<BaseCommand> &Base1 = *Iter;
3080b1b695aSRui Ueyama 
3090b1b695aSRui Ueyama     // Handle symbol assignments outside of any output section.
3102ab5f73dSRui Ueyama     if (auto *Cmd = dyn_cast<SymbolAssignment>(Base1.get())) {
311b0de56b5SRafael Espindola       addSymbol<ELFT>(Cmd);
3122ab5f73dSRui Ueyama       continue;
3132ab5f73dSRui Ueyama     }
3140b1b695aSRui Ueyama 
31520d03194SEugene Leviant     if (auto *Cmd = dyn_cast<AssertCommand>(Base1.get())) {
31620d03194SEugene Leviant       // If we don't have SECTIONS then output sections have already been
317194470cdSGeorge Rimar       // created by Writer<ELFT>. The LinkerScript<ELFT>::assignAddresses
31820d03194SEugene Leviant       // will not be called, so ASSERT should be evaluated now.
31920d03194SEugene Leviant       if (!Opt.HasSections)
32020d03194SEugene Leviant         Cmd->Expression(0);
32120d03194SEugene Leviant       continue;
32220d03194SEugene Leviant     }
3232ab5f73dSRui Ueyama 
324ceabe80eSEugene Leviant     if (auto *Cmd = dyn_cast<OutputSectionCommand>(Base1.get())) {
3257bd37870SRafael Espindola       std::vector<InputSectionBase<ELFT> *> V = createInputSectionList(*Cmd);
3267bd37870SRafael Espindola 
3270b1b695aSRui Ueyama       // The output section name `/DISCARD/' is special.
3280b1b695aSRui Ueyama       // Any input section assigned to it is discarded.
32948c3f1ceSRui Ueyama       if (Cmd->Name == "/DISCARD/") {
3307bd37870SRafael Espindola         discard(V);
33148c3f1ceSRui Ueyama         continue;
33248c3f1ceSRui Ueyama       }
3330b9ce6a4SRui Ueyama 
3340b1b695aSRui Ueyama       // This is for ONLY_IF_RO and ONLY_IF_RW. An output section directive
3350b1b695aSRui Ueyama       // ".foo : ONLY_IF_R[OW] { ... }" is handled only if all member input
3360b1b695aSRui Ueyama       // sections satisfy a given constraint. If not, a directive is handled
3370b1b695aSRui Ueyama       // as if it wasn't present from the beginning.
3380b1b695aSRui Ueyama       //
3390b1b695aSRui Ueyama       // Because we'll iterate over Commands many more times, the easiest
3400b1b695aSRui Ueyama       // way to "make it as if it wasn't present" is to just remove it.
3417c3ff2ebSRafael Espindola       if (!matchConstraints<ELFT>(V, Cmd->Constraint)) {
3427c3ff2ebSRafael Espindola         for (InputSectionBase<ELFT> *S : V)
343f94efdddSRui Ueyama           S->Assigned = false;
3447c3ff2ebSRafael Espindola         Opt.Commands.erase(Iter);
345dfbbbc86SGeorge Rimar         --I;
3467c3ff2ebSRafael Espindola         continue;
3477c3ff2ebSRafael Espindola       }
3487c3ff2ebSRafael Espindola 
3490b1b695aSRui Ueyama       // A directive may contain symbol definitions like this:
3500b1b695aSRui Ueyama       // ".foo : { ...; bar = .; }". Handle them.
3517c3ff2ebSRafael Espindola       for (const std::unique_ptr<BaseCommand> &Base : Cmd->Commands)
3527c3ff2ebSRafael Espindola         if (auto *OutCmd = dyn_cast<SymbolAssignment>(Base.get()))
3537c3ff2ebSRafael Espindola           addSymbol<ELFT>(OutCmd);
3547c3ff2ebSRafael Espindola 
3550b1b695aSRui Ueyama       // Handle subalign (e.g. ".foo : SUBALIGN(32) { ... }"). If subalign
3560b1b695aSRui Ueyama       // is given, input sections are aligned to that value, whether the
3570b1b695aSRui Ueyama       // given value is larger or smaller than the original section alignment.
3580b1b695aSRui Ueyama       if (Cmd->SubalignExpr) {
3590b1b695aSRui Ueyama         uint32_t Subalign = Cmd->SubalignExpr(0);
3600b1b695aSRui Ueyama         for (InputSectionBase<ELFT> *S : V)
3610b1b695aSRui Ueyama           S->Alignment = Subalign;
36220d03194SEugene Leviant       }
3630b1b695aSRui Ueyama 
3640b1b695aSRui Ueyama       // Add input sections to an output section.
3650b1b695aSRui Ueyama       for (InputSectionBase<ELFT> *S : V)
3660b1b695aSRui Ueyama         addSection(Factory, S, Cmd->Name);
367eea3114fSGeorge Rimar     }
36848c3f1ceSRui Ueyama   }
369db24d9c3SGeorge Rimar }
370e63d81bdSEugene Leviant 
3710b1b695aSRui Ueyama // Add sections that didn't match any sections command.
37220d03194SEugene Leviant template <class ELFT>
37393c64025SGeorge Rimar void LinkerScript<ELFT>::addOrphanSections(
37493c64025SGeorge Rimar     OutputSectionFactory<ELFT> &Factory) {
3758c6a5aafSRui Ueyama   for (InputSectionBase<ELFT> *S : Symtab<ELFT>::X->Sections)
3768f9026baSRafael Espindola     if (S->Live && !S->OutSec)
37755518e7dSRui Ueyama       addSection(Factory, S, getOutputSectionName(S->Name));
378e63d81bdSEugene Leviant }
379e63d81bdSEugene Leviant 
380e08e78dfSRafael Espindola template <class ELFT> static bool isTbss(OutputSectionBase *Sec) {
38104a2e348SRafael Espindola   return (Sec->Flags & SHF_TLS) && Sec->Type == SHT_NOBITS;
382a940e539SRafael Espindola }
383a940e539SRafael Espindola 
384d3190795SRafael Espindola template <class ELFT> void LinkerScript<ELFT>::output(InputSection<ELFT> *S) {
385d3190795SRafael Espindola   if (!AlreadyOutputIS.insert(S).second)
386ceabe80eSEugene Leviant     return;
387e08e78dfSRafael Espindola   bool IsTbss = isTbss<ELFT>(CurOutSec);
388d3190795SRafael Espindola 
389d3190795SRafael Espindola   uintX_t Pos = IsTbss ? Dot + ThreadBssOffset : Dot;
390d3190795SRafael Espindola   Pos = alignTo(Pos, S->Alignment);
39104a2e348SRafael Espindola   S->OutSecOff = Pos - CurOutSec->Addr;
392d3190795SRafael Espindola   Pos += S->getSize();
393d3190795SRafael Espindola 
394d3190795SRafael Espindola   // Update output section size after adding each section. This is so that
395d3190795SRafael Espindola   // SIZEOF works correctly in the case below:
396d3190795SRafael Espindola   // .foo { *(.aaa) a = SIZEOF(.foo); *(.bbb) }
39704a2e348SRafael Espindola   CurOutSec->Size = Pos - CurOutSec->Addr;
398d3190795SRafael Espindola 
399b889744eSMeador Inge   // If there is a memory region associated with this input section, then
400b889744eSMeador Inge   // place the section in that region and update the region index.
401b889744eSMeador Inge   if (CurMemRegion) {
402b889744eSMeador Inge     CurMemRegion->Offset += CurOutSec->Size;
403b889744eSMeador Inge     uint64_t CurSize = CurMemRegion->Offset - CurMemRegion->Origin;
404b889744eSMeador Inge     if (CurSize > CurMemRegion->Length) {
405b889744eSMeador Inge       uint64_t OverflowAmt = CurSize - CurMemRegion->Length;
406b889744eSMeador Inge       error("section '" + CurOutSec->Name + "' will not fit in region '" +
407b889744eSMeador Inge             CurMemRegion->Name + "': overflowed by " + Twine(OverflowAmt) +
408b889744eSMeador Inge             " bytes");
409b889744eSMeador Inge     }
410b889744eSMeador Inge   }
411b889744eSMeador Inge 
4127252ae52SRafael Espindola   if (IsTbss)
4137252ae52SRafael Espindola     ThreadBssOffset = Pos - Dot;
4147252ae52SRafael Espindola   else
415d3190795SRafael Espindola     Dot = Pos;
4162de509c3SRui Ueyama }
417ceabe80eSEugene Leviant 
418d3190795SRafael Espindola template <class ELFT> void LinkerScript<ELFT>::flush() {
41965499b90SRafael Espindola   if (!CurOutSec || !AlreadyOutputOS.insert(CurOutSec).second)
42065499b90SRafael Espindola     return;
42165499b90SRafael Espindola   if (auto *OutSec = dyn_cast<OutputSection<ELFT>>(CurOutSec)) {
422d3190795SRafael Espindola     for (InputSection<ELFT> *I : OutSec->Sections)
423d3190795SRafael Espindola       output(I);
42465499b90SRafael Espindola   } else {
42504a2e348SRafael Espindola     Dot += CurOutSec->Size;
426d3190795SRafael Espindola   }
427d3190795SRafael Espindola }
42897403d15SEugene Leviant 
429d3190795SRafael Espindola template <class ELFT>
430e08e78dfSRafael Espindola void LinkerScript<ELFT>::switchTo(OutputSectionBase *Sec) {
431d3190795SRafael Espindola   if (CurOutSec == Sec)
432d3190795SRafael Espindola     return;
433d3190795SRafael Espindola   if (AlreadyOutputOS.count(Sec))
434d3190795SRafael Espindola     return;
435d3190795SRafael Espindola 
436d3190795SRafael Espindola   flush();
437d3190795SRafael Espindola   CurOutSec = Sec;
438d3190795SRafael Espindola 
43904a2e348SRafael Espindola   Dot = alignTo(Dot, CurOutSec->Addralign);
440e08e78dfSRafael Espindola   CurOutSec->Addr = isTbss<ELFT>(CurOutSec) ? Dot + ThreadBssOffset : Dot;
441b71d6f7aSEugene Leviant 
442b71d6f7aSEugene Leviant   // If neither AT nor AT> is specified for an allocatable section, the linker
443b71d6f7aSEugene Leviant   // will set the LMA such that the difference between VMA and LMA for the
444b71d6f7aSEugene Leviant   // section is the same as the preceding output section in the same region
445b71d6f7aSEugene Leviant   // https://sourceware.org/binutils/docs-2.20/ld/Output-Section-LMA.html
446b71d6f7aSEugene Leviant   CurOutSec->setLMAOffset(LMAOffset);
447d3190795SRafael Espindola }
448d3190795SRafael Espindola 
449d3190795SRafael Espindola template <class ELFT> void LinkerScript<ELFT>::process(BaseCommand &Base) {
450e38cbab5SGeorge Rimar   // This handles the assignments to symbol or to a location counter (.)
451d3190795SRafael Espindola   if (auto *AssignCmd = dyn_cast<SymbolAssignment>(&Base)) {
45297403d15SEugene Leviant     if (AssignCmd->Name == ".") {
45397403d15SEugene Leviant       // Update to location counter means update to section size.
45414460e02SGeorge Rimar       uintX_t Val = AssignCmd->Expression(Dot);
45514460e02SGeorge Rimar       if (Val < Dot)
45614460e02SGeorge Rimar         error("unable to move location counter backward for: " +
45714460e02SGeorge Rimar               CurOutSec->Name);
45814460e02SGeorge Rimar       Dot = Val;
45904a2e348SRafael Espindola       CurOutSec->Size = Dot - CurOutSec->Addr;
460d3190795SRafael Espindola       return;
46197403d15SEugene Leviant     }
462b2b70975SGeorge Rimar     assignSymbol<ELFT>(AssignCmd, Dot);
463d3190795SRafael Espindola     return;
46497403d15SEugene Leviant   }
465e38cbab5SGeorge Rimar 
466e38cbab5SGeorge Rimar   // Handle BYTE(), SHORT(), LONG(), or QUAD().
467e38cbab5SGeorge Rimar   if (auto *DataCmd = dyn_cast<BytesDataCommand>(&Base)) {
46804a2e348SRafael Espindola     DataCmd->Offset = Dot - CurOutSec->Addr;
469e38cbab5SGeorge Rimar     Dot += DataCmd->Size;
47004a2e348SRafael Espindola     CurOutSec->Size = Dot - CurOutSec->Addr;
471e38cbab5SGeorge Rimar     return;
472e38cbab5SGeorge Rimar   }
473e38cbab5SGeorge Rimar 
474b2d99d6aSMeador Inge   if (auto *AssertCmd = dyn_cast<AssertCommand>(&Base)) {
475b2d99d6aSMeador Inge     AssertCmd->Expression(Dot);
476b2d99d6aSMeador Inge     return;
477b2d99d6aSMeador Inge   }
478b2d99d6aSMeador Inge 
479e38cbab5SGeorge Rimar   // It handles single input section description command,
480e38cbab5SGeorge Rimar   // calculates and assigns the offsets for each section and also
481e38cbab5SGeorge Rimar   // updates the output section size.
482d3190795SRafael Espindola   auto &ICmd = cast<InputSectionDescription>(Base);
483d3190795SRafael Espindola   for (InputSectionData *ID : ICmd.Sections) {
4843fb5a6dcSGeorge Rimar     // We tentatively added all synthetic sections at the beginning and removed
4853fb5a6dcSGeorge Rimar     // empty ones afterwards (because there is no way to know whether they were
4863fb5a6dcSGeorge Rimar     // going be empty or not other than actually running linker scripts.)
4873fb5a6dcSGeorge Rimar     // We need to ignore remains of empty sections.
4883fb5a6dcSGeorge Rimar     if (auto *Sec = dyn_cast<SyntheticSection<ELFT>>(ID))
4893fb5a6dcSGeorge Rimar       if (Sec->empty())
4903fb5a6dcSGeorge Rimar         continue;
4913fb5a6dcSGeorge Rimar 
492d3190795SRafael Espindola     auto *IB = static_cast<InputSectionBase<ELFT> *>(ID);
493d3190795SRafael Espindola     switchTo(IB->OutSec);
494d3190795SRafael Espindola     if (auto *I = dyn_cast<InputSection<ELFT>>(IB))
495d3190795SRafael Espindola       output(I);
49665499b90SRafael Espindola     else
49765499b90SRafael Espindola       flush();
498ceabe80eSEugene Leviant   }
499ceabe80eSEugene Leviant }
500ceabe80eSEugene Leviant 
5018f66df92SGeorge Rimar template <class ELFT>
5022b074553SRafael Espindola static OutputSectionBase *
5032b074553SRafael Espindola findSection(StringRef Name, const std::vector<OutputSectionBase *> &Sections) {
5042b074553SRafael Espindola   auto End = Sections.end();
5052b074553SRafael Espindola   auto HasName = [=](OutputSectionBase *Sec) { return Sec->getName() == Name; };
5062b074553SRafael Espindola   auto I = std::find_if(Sections.begin(), End, HasName);
507e08e78dfSRafael Espindola   std::vector<OutputSectionBase *> Ret;
5082b074553SRafael Espindola   if (I == End)
5092b074553SRafael Espindola     return nullptr;
5102b074553SRafael Espindola   assert(std::find_if(I + 1, End, HasName) == End);
5112b074553SRafael Espindola   return *I;
5128f66df92SGeorge Rimar }
5138f66df92SGeorge Rimar 
514b889744eSMeador Inge // This function searches for a memory region to place the given output
515b889744eSMeador Inge // section in. If found, a pointer to the appropriate memory region is
516b889744eSMeador Inge // returned. Otherwise, a nullptr is returned.
517b889744eSMeador Inge template <class ELFT>
518b889744eSMeador Inge MemoryRegion *LinkerScript<ELFT>::findMemoryRegion(OutputSectionCommand *Cmd,
519b889744eSMeador Inge                                                    OutputSectionBase *Sec) {
520b889744eSMeador Inge   // If a memory region name was specified in the output section command,
521b889744eSMeador Inge   // then try to find that region first.
522b889744eSMeador Inge   if (!Cmd->MemoryRegionName.empty()) {
523b889744eSMeador Inge     auto It = Opt.MemoryRegions.find(Cmd->MemoryRegionName);
524b889744eSMeador Inge     if (It != Opt.MemoryRegions.end())
525b889744eSMeador Inge       return &It->second;
526b889744eSMeador Inge     error("memory region '" + Cmd->MemoryRegionName + "' not declared");
527b889744eSMeador Inge     return nullptr;
528b889744eSMeador Inge   }
529b889744eSMeador Inge 
530b889744eSMeador Inge   // The memory region name is empty, thus a suitable region must be
531b889744eSMeador Inge   // searched for in the region map. If the region map is empty, just
532b889744eSMeador Inge   // return. Note that this check doesn't happen at the very beginning
533b889744eSMeador Inge   // so that uses of undeclared regions can be caught.
534b889744eSMeador Inge   if (!Opt.MemoryRegions.size())
535b889744eSMeador Inge     return nullptr;
536b889744eSMeador Inge 
537b889744eSMeador Inge   // See if a region can be found by matching section flags.
538b889744eSMeador Inge   for (auto &MRI : Opt.MemoryRegions) {
539b889744eSMeador Inge     MemoryRegion &MR = MRI.second;
5408a8a953eSRui Ueyama     if ((MR.Flags & Sec->Flags) != 0 && (MR.NegFlags & Sec->Flags) == 0)
541b889744eSMeador Inge       return &MR;
542b889744eSMeador Inge   }
543b889744eSMeador Inge 
544b889744eSMeador Inge   // Otherwise, no suitable region was found.
545b889744eSMeador Inge   if (Sec->Flags & SHF_ALLOC)
546b889744eSMeador Inge     error("no memory region specified for section '" + Sec->Name + "'");
547b889744eSMeador Inge   return nullptr;
548b889744eSMeador Inge }
549b889744eSMeador Inge 
5500b1b695aSRui Ueyama // This function assigns offsets to input sections and an output section
5510b1b695aSRui Ueyama // for a single sections command (e.g. ".text { *(.text); }").
552d3190795SRafael Espindola template <class ELFT>
553d3190795SRafael Espindola void LinkerScript<ELFT>::assignOffsets(OutputSectionCommand *Cmd) {
554b71d6f7aSEugene Leviant   if (Cmd->LMAExpr)
555b71d6f7aSEugene Leviant     LMAOffset = Cmd->LMAExpr(Dot) - Dot;
5562b074553SRafael Espindola   OutputSectionBase *Sec = findSection<ELFT>(Cmd->Name, *OutputSections);
5572b074553SRafael Espindola   if (!Sec)
558d3190795SRafael Espindola     return;
559b889744eSMeador Inge 
560165088aaSPetr Hosek   // Handle align (e.g. ".foo : ALIGN(16) { ... }").
561165088aaSPetr Hosek   if (Cmd->AlignExpr)
562165088aaSPetr Hosek     Sec->updateAlignment(Cmd->AlignExpr(0));
563165088aaSPetr Hosek 
564b889744eSMeador Inge   // Try and find an appropriate memory region to assign offsets in.
565b889744eSMeador Inge   CurMemRegion = findMemoryRegion(Cmd, Sec);
566b889744eSMeador Inge   if (CurMemRegion)
567b889744eSMeador Inge     Dot = CurMemRegion->Offset;
568b889744eSMeador Inge   switchTo(Sec);
5690b1b695aSRui Ueyama 
570d3190795SRafael Espindola   // Find the last section output location. We will output orphan sections
571d3190795SRafael Espindola   // there so that end symbols point to the correct location.
572d3190795SRafael Espindola   auto E = std::find_if(Cmd->Commands.rbegin(), Cmd->Commands.rend(),
573d3190795SRafael Espindola                         [](const std::unique_ptr<BaseCommand> &Cmd) {
574d3190795SRafael Espindola                           return !isa<SymbolAssignment>(*Cmd);
575d3190795SRafael Espindola                         })
576d3190795SRafael Espindola                .base();
577d3190795SRafael Espindola   for (auto I = Cmd->Commands.begin(); I != E; ++I)
578d3190795SRafael Espindola     process(**I);
5792506cb4dSEugene Leviant   flush();
580b31dd370SGeorge Rimar   std::for_each(E, Cmd->Commands.end(),
581b31dd370SGeorge Rimar                 [this](std::unique_ptr<BaseCommand> &B) { process(*B.get()); });
582d3190795SRafael Espindola }
583d3190795SRafael Espindola 
58407fe6129SRafael Espindola template <class ELFT> void LinkerScript<ELFT>::removeEmptyCommands() {
5856d38e4dbSRafael Espindola   // It is common practice to use very generic linker scripts. So for any
5866d38e4dbSRafael Espindola   // given run some of the output sections in the script will be empty.
5876d38e4dbSRafael Espindola   // We could create corresponding empty output sections, but that would
5886d38e4dbSRafael Espindola   // clutter the output.
5896d38e4dbSRafael Espindola   // We instead remove trivially empty sections. The bfd linker seems even
5906d38e4dbSRafael Espindola   // more aggressive at removing them.
5916d38e4dbSRafael Espindola   auto Pos = std::remove_if(
5926d38e4dbSRafael Espindola       Opt.Commands.begin(), Opt.Commands.end(),
5936d38e4dbSRafael Espindola       [&](const std::unique_ptr<BaseCommand> &Base) {
5940b1b695aSRui Ueyama         if (auto *Cmd = dyn_cast<OutputSectionCommand>(Base.get()))
5952b074553SRafael Espindola           return !findSection<ELFT>(Cmd->Name, *OutputSections);
5960b1b695aSRui Ueyama         return false;
5976d38e4dbSRafael Espindola       });
5986d38e4dbSRafael Espindola   Opt.Commands.erase(Pos, Opt.Commands.end());
59907fe6129SRafael Espindola }
60007fe6129SRafael Espindola 
6016a53737cSRafael Espindola static bool isAllSectionDescription(const OutputSectionCommand &Cmd) {
6026a53737cSRafael Espindola   for (const std::unique_ptr<BaseCommand> &I : Cmd.Commands)
6036a53737cSRafael Espindola     if (!isa<InputSectionDescription>(*I))
6046a53737cSRafael Espindola       return false;
6056a53737cSRafael Espindola   return true;
6066a53737cSRafael Espindola }
6076d38e4dbSRafael Espindola 
6086a53737cSRafael Espindola template <class ELFT> void LinkerScript<ELFT>::adjustSectionsBeforeSorting() {
6099546fffbSRafael Espindola   // If the output section contains only symbol assignments, create a
6109546fffbSRafael Espindola   // corresponding output section. The bfd linker seems to only create them if
6119546fffbSRafael Espindola   // '.' is assigned to, but creating these section should not have any bad
6129546fffbSRafael Espindola   // consequeces and gives us a section to put the symbol in.
6139546fffbSRafael Espindola   uintX_t Flags = SHF_ALLOC;
614f93b8c29SRafael Espindola   uint32_t Type = SHT_NOBITS;
6159546fffbSRafael Espindola   for (const std::unique_ptr<BaseCommand> &Base : Opt.Commands) {
6169546fffbSRafael Espindola     auto *Cmd = dyn_cast<OutputSectionCommand>(Base.get());
6179546fffbSRafael Espindola     if (!Cmd)
6189546fffbSRafael Espindola       continue;
6192b074553SRafael Espindola     if (OutputSectionBase *Sec =
6202b074553SRafael Espindola             findSection<ELFT>(Cmd->Name, *OutputSections)) {
6212b074553SRafael Espindola       Flags = Sec->Flags;
6222b074553SRafael Espindola       Type = Sec->Type;
6239546fffbSRafael Espindola       continue;
6249546fffbSRafael Espindola     }
6259546fffbSRafael Espindola 
6266a53737cSRafael Espindola     if (isAllSectionDescription(*Cmd))
6276a53737cSRafael Espindola       continue;
6286a53737cSRafael Espindola 
62995642b95SRui Ueyama     auto *OutSec = make<OutputSection<ELFT>>(Cmd->Name, Type, Flags);
6309546fffbSRafael Espindola     OutputSections->push_back(OutSec);
6319546fffbSRafael Espindola   }
632f7a17448SRafael Espindola }
633f7a17448SRafael Espindola 
634f7a17448SRafael Espindola template <class ELFT> void LinkerScript<ELFT>::adjustSectionsAfterSorting() {
635f7a17448SRafael Espindola   placeOrphanSections();
636f7a17448SRafael Espindola 
637f7a17448SRafael Espindola   // If output section command doesn't specify any segments,
638f7a17448SRafael Espindola   // and we haven't previously assigned any section to segment,
639f7a17448SRafael Espindola   // then we simply assign section to the very first load segment.
640f7a17448SRafael Espindola   // Below is an example of such linker script:
641f7a17448SRafael Espindola   // PHDRS { seg PT_LOAD; }
642f7a17448SRafael Espindola   // SECTIONS { .aaa : { *(.aaa) } }
643f7a17448SRafael Espindola   std::vector<StringRef> DefPhdrs;
644f7a17448SRafael Espindola   auto FirstPtLoad =
645f7a17448SRafael Espindola       std::find_if(Opt.PhdrsCommands.begin(), Opt.PhdrsCommands.end(),
646f7a17448SRafael Espindola                    [](const PhdrsCommand &Cmd) { return Cmd.Type == PT_LOAD; });
647f7a17448SRafael Espindola   if (FirstPtLoad != Opt.PhdrsCommands.end())
648f7a17448SRafael Espindola     DefPhdrs.push_back(FirstPtLoad->Name);
649f7a17448SRafael Espindola 
650f7a17448SRafael Espindola   // Walk the commands and propagate the program headers to commands that don't
651f7a17448SRafael Espindola   // explicitly specify them.
652f7a17448SRafael Espindola   for (const std::unique_ptr<BaseCommand> &Base : Opt.Commands) {
653f7a17448SRafael Espindola     auto *Cmd = dyn_cast<OutputSectionCommand>(Base.get());
654f7a17448SRafael Espindola     if (!Cmd)
655f7a17448SRafael Espindola       continue;
656f7a17448SRafael Espindola     if (Cmd->Phdrs.empty())
657f7a17448SRafael Espindola       Cmd->Phdrs = DefPhdrs;
658f7a17448SRafael Espindola     else
659f7a17448SRafael Espindola       DefPhdrs = Cmd->Phdrs;
660f7a17448SRafael Espindola   }
6616a53737cSRafael Espindola 
6626a53737cSRafael Espindola   removeEmptyCommands();
6639546fffbSRafael Espindola }
6649546fffbSRafael Espindola 
66515c57951SRafael Espindola // When placing orphan sections, we want to place them after symbol assignments
66615c57951SRafael Espindola // so that an orphan after
66715c57951SRafael Espindola //   begin_foo = .;
66815c57951SRafael Espindola //   foo : { *(foo) }
66915c57951SRafael Espindola //   end_foo = .;
67015c57951SRafael Espindola // doesn't break the intended meaning of the begin/end symbols.
67115c57951SRafael Espindola // We don't want to go over sections since Writer<ELFT>::sortSections is the
67215c57951SRafael Espindola // one in charge of deciding the order of the sections.
67315c57951SRafael Espindola // We don't want to go over alignments, since doing so in
67415c57951SRafael Espindola //  rx_sec : { *(rx_sec) }
67515c57951SRafael Espindola //  . = ALIGN(0x1000);
67615c57951SRafael Espindola //  /* The RW PT_LOAD starts here*/
67715c57951SRafael Espindola //  rw_sec : { *(rw_sec) }
67815c57951SRafael Espindola // would mean that the RW PT_LOAD would become unaligned.
6795fcc99c2SRafael Espindola static bool shouldSkip(const BaseCommand &Cmd) {
68015c57951SRafael Espindola   if (isa<OutputSectionCommand>(Cmd))
68115c57951SRafael Espindola     return false;
68215c57951SRafael Espindola   const auto *Assign = dyn_cast<SymbolAssignment>(&Cmd);
68315c57951SRafael Espindola   if (!Assign)
68415c57951SRafael Espindola     return true;
6855fcc99c2SRafael Espindola   return Assign->Name != ".";
68615c57951SRafael Espindola }
68715c57951SRafael Espindola 
6886697ec29SRui Ueyama // Orphan sections are sections present in the input files which are
6896697ec29SRui Ueyama // not explicitly placed into the output file by the linker script.
6906697ec29SRui Ueyama //
6916697ec29SRui Ueyama // When the control reaches this function, Opt.Commands contains
6926697ec29SRui Ueyama // output section commands for non-orphan sections only. This function
6936697ec29SRui Ueyama // adds new elements for orphan sections to Opt.Commands so that all
6946697ec29SRui Ueyama // sections are explicitly handled by Opt.Commands.
6956697ec29SRui Ueyama //
6966697ec29SRui Ueyama // Writer<ELFT>::sortSections has already sorted output sections.
6976697ec29SRui Ueyama // What we need to do is to scan OutputSections vector and
6986697ec29SRui Ueyama // Opt.Commands in parallel to find orphan sections. If there is an
6996697ec29SRui Ueyama // output section that doesn't have a corresponding entry in
7006697ec29SRui Ueyama // Opt.Commands, we will insert a new entry to Opt.Commands.
7016697ec29SRui Ueyama //
7026697ec29SRui Ueyama // There is some ambiguity as to where exactly a new entry should be
7036697ec29SRui Ueyama // inserted, because Opt.Commands contains not only output section
7046697ec29SRui Ueyama // commands but other types of commands such as symbol assignment
7056697ec29SRui Ueyama // expressions. There's no correct answer here due to the lack of the
7066697ec29SRui Ueyama // formal specification of the linker script. We use heuristics to
7076697ec29SRui Ueyama // determine whether a new output command should be added before or
7086697ec29SRui Ueyama // after another commands. For the details, look at shouldSkip
7096697ec29SRui Ueyama // function.
71093c64025SGeorge Rimar template <class ELFT> void LinkerScript<ELFT>::placeOrphanSections() {
711aab6d5c5SRafael Espindola   // The OutputSections are already in the correct order.
712aab6d5c5SRafael Espindola   // This loops creates or moves commands as needed so that they are in the
713aab6d5c5SRafael Espindola   // correct order.
714aab6d5c5SRafael Espindola   int CmdIndex = 0;
7155fcc99c2SRafael Espindola 
7165fcc99c2SRafael Espindola   // As a horrible special case, skip the first . assignment if it is before any
7175fcc99c2SRafael Espindola   // section. We do this because it is common to set a load address by starting
7185fcc99c2SRafael Espindola   // the script with ". = 0xabcd" and the expectation is that every section is
7195fcc99c2SRafael Espindola   // after that.
7205fcc99c2SRafael Espindola   auto FirstSectionOrDotAssignment =
7215fcc99c2SRafael Espindola       std::find_if(Opt.Commands.begin(), Opt.Commands.end(),
7225fcc99c2SRafael Espindola                    [](const std::unique_ptr<BaseCommand> &Cmd) {
7235fcc99c2SRafael Espindola                      if (isa<OutputSectionCommand>(*Cmd))
7245fcc99c2SRafael Espindola                        return true;
7255fcc99c2SRafael Espindola                      const auto *Assign = dyn_cast<SymbolAssignment>(Cmd.get());
7265fcc99c2SRafael Espindola                      if (!Assign)
7275fcc99c2SRafael Espindola                        return false;
7285fcc99c2SRafael Espindola                      return Assign->Name == ".";
7295fcc99c2SRafael Espindola                    });
7305fcc99c2SRafael Espindola   if (FirstSectionOrDotAssignment != Opt.Commands.end()) {
7315fcc99c2SRafael Espindola     CmdIndex = FirstSectionOrDotAssignment - Opt.Commands.begin();
7325fcc99c2SRafael Espindola     if (isa<SymbolAssignment>(**FirstSectionOrDotAssignment))
7335fcc99c2SRafael Espindola       ++CmdIndex;
7345fcc99c2SRafael Espindola   }
7355fcc99c2SRafael Espindola 
736e08e78dfSRafael Espindola   for (OutputSectionBase *Sec : *OutputSections) {
737652852c5SGeorge Rimar     StringRef Name = Sec->getName();
738aab6d5c5SRafael Espindola 
739aab6d5c5SRafael Espindola     // Find the last spot where we can insert a command and still get the
74015c57951SRafael Espindola     // correct result.
741aab6d5c5SRafael Espindola     auto CmdIter = Opt.Commands.begin() + CmdIndex;
742aab6d5c5SRafael Espindola     auto E = Opt.Commands.end();
7435fcc99c2SRafael Espindola     while (CmdIter != E && shouldSkip(**CmdIter)) {
744aab6d5c5SRafael Espindola       ++CmdIter;
745aab6d5c5SRafael Espindola       ++CmdIndex;
746aab6d5c5SRafael Espindola     }
747aab6d5c5SRafael Espindola 
748aab6d5c5SRafael Espindola     auto Pos =
749aab6d5c5SRafael Espindola         std::find_if(CmdIter, E, [&](const std::unique_ptr<BaseCommand> &Base) {
750aab6d5c5SRafael Espindola           auto *Cmd = dyn_cast<OutputSectionCommand>(Base.get());
751aab6d5c5SRafael Espindola           return Cmd && Cmd->Name == Name;
752aab6d5c5SRafael Espindola         });
753aab6d5c5SRafael Espindola     if (Pos == E) {
754aab6d5c5SRafael Espindola       Opt.Commands.insert(CmdIter,
755aab6d5c5SRafael Espindola                           llvm::make_unique<OutputSectionCommand>(Name));
756aab6d5c5SRafael Espindola       ++CmdIndex;
75715c57951SRafael Espindola       continue;
75815c57951SRafael Espindola     }
75915c57951SRafael Espindola 
76015c57951SRafael Espindola     // Continue from where we found it.
76115c57951SRafael Espindola     CmdIndex = (Pos - Opt.Commands.begin()) + 1;
762652852c5SGeorge Rimar   }
763337f903cSRafael Espindola }
764337f903cSRafael Espindola 
765337f903cSRafael Espindola template <class ELFT>
76617cb7c0aSRafael Espindola void LinkerScript<ELFT>::assignAddresses(std::vector<PhdrEntry> &Phdrs) {
7677c18c28cSRui Ueyama   // Assign addresses as instructed by linker script SECTIONS sub-commands.
768be607334SRafael Espindola   Dot = 0;
769652852c5SGeorge Rimar 
77006f4743aSRafael Espindola   // A symbol can be assigned before any section is mentioned in the linker
77106f4743aSRafael Espindola   // script. In an DSO, the symbol values are addresses, so the only important
77206f4743aSRafael Espindola   // section values are:
77306f4743aSRafael Espindola   // * SHN_UNDEF
77406f4743aSRafael Espindola   // * SHN_ABS
77506f4743aSRafael Espindola   // * Any value meaning a regular section.
77606f4743aSRafael Espindola   // To handle that, create a dummy aether section that fills the void before
77706f4743aSRafael Espindola   // the linker scripts switches to another section. It has an index of one
77806f4743aSRafael Espindola   // which will map to whatever the first actual section is.
77906f4743aSRafael Espindola   auto *Aether = make<OutputSectionBase>("", 0, SHF_ALLOC);
78006f4743aSRafael Espindola   Aether->SectionIndex = 1;
78106f4743aSRafael Espindola   switchTo(Aether);
78206f4743aSRafael Espindola 
783076fe157SGeorge Rimar   for (const std::unique_ptr<BaseCommand> &Base : Opt.Commands) {
784076fe157SGeorge Rimar     if (auto *Cmd = dyn_cast<SymbolAssignment>(Base.get())) {
7858d083e6aSRui Ueyama       if (Cmd->Name == ".") {
7868d083e6aSRui Ueyama         Dot = Cmd->Expression(Dot);
7878d083e6aSRui Ueyama       } else if (Cmd->Sym) {
788b2b70975SGeorge Rimar         assignSymbol<ELFT>(Cmd, Dot);
7898d083e6aSRui Ueyama       }
79005ef4cffSRui Ueyama       continue;
791652852c5SGeorge Rimar     }
792652852c5SGeorge Rimar 
793eefa758eSGeorge Rimar     if (auto *Cmd = dyn_cast<AssertCommand>(Base.get())) {
794eefa758eSGeorge Rimar       Cmd->Expression(Dot);
795eefa758eSGeorge Rimar       continue;
796eefa758eSGeorge Rimar     }
797eefa758eSGeorge Rimar 
798076fe157SGeorge Rimar     auto *Cmd = cast<OutputSectionCommand>(Base.get());
79958e5c4dcSGeorge Rimar     if (Cmd->AddrExpr)
80058e5c4dcSGeorge Rimar       Dot = Cmd->AddrExpr(Dot);
801d3190795SRafael Espindola     assignOffsets(Cmd);
802a14b13d8SGeorge Rimar   }
803467c4d55SEugene Leviant 
804aab6d5c5SRafael Espindola   uintX_t MinVA = std::numeric_limits<uintX_t>::max();
805193b158bSRafael Espindola   for (OutputSectionBase *Sec : *OutputSections)
80604a2e348SRafael Espindola     if (Sec->Flags & SHF_ALLOC)
807e08e78dfSRafael Espindola       MinVA = std::min<uint64_t>(MinVA, Sec->Addr);
808aab6d5c5SRafael Espindola 
8098c495e20SRafael Espindola   allocateHeaders<ELFT>(Phdrs, *OutputSections, MinVA);
810fb8978fcSDima Stepanov }
811652852c5SGeorge Rimar 
812464daadcSRui Ueyama // Creates program headers as instructed by PHDRS linker script command.
81317cb7c0aSRafael Espindola template <class ELFT> std::vector<PhdrEntry> LinkerScript<ELFT>::createPhdrs() {
81417cb7c0aSRafael Espindola   std::vector<PhdrEntry> Ret;
815bbe38602SEugene Leviant 
816464daadcSRui Ueyama   // Process PHDRS and FILEHDR keywords because they are not
817464daadcSRui Ueyama   // real output sections and cannot be added in the following loop.
818bbe38602SEugene Leviant   for (const PhdrsCommand &Cmd : Opt.PhdrsCommands) {
819edebbdf1SRui Ueyama     Ret.emplace_back(Cmd.Type, Cmd.Flags == UINT_MAX ? PF_R : Cmd.Flags);
82017cb7c0aSRafael Espindola     PhdrEntry &Phdr = Ret.back();
821bbe38602SEugene Leviant 
822bbe38602SEugene Leviant     if (Cmd.HasFilehdr)
823adca245fSRui Ueyama       Phdr.add(Out<ELFT>::ElfHeader);
824bbe38602SEugene Leviant     if (Cmd.HasPhdrs)
825adca245fSRui Ueyama       Phdr.add(Out<ELFT>::ProgramHeaders);
82656b21c86SEugene Leviant 
82756b21c86SEugene Leviant     if (Cmd.LMAExpr) {
82817cb7c0aSRafael Espindola       Phdr.p_paddr = Cmd.LMAExpr(0);
82956b21c86SEugene Leviant       Phdr.HasLMA = true;
83056b21c86SEugene Leviant     }
831bbe38602SEugene Leviant   }
832bbe38602SEugene Leviant 
833464daadcSRui Ueyama   // Add output sections to program headers.
834e08e78dfSRafael Espindola   for (OutputSectionBase *Sec : *OutputSections) {
83504a2e348SRafael Espindola     if (!(Sec->Flags & SHF_ALLOC))
836bbe38602SEugene Leviant       break;
837bbe38602SEugene Leviant 
838bbe38602SEugene Leviant     // Assign headers specified by linker script
839f7a17448SRafael Espindola     for (size_t Id : getPhdrIndices(Sec->getName())) {
840edebbdf1SRui Ueyama       Ret[Id].add(Sec);
841865bf863SEugene Leviant       if (Opt.PhdrsCommands[Id].Flags == UINT_MAX)
84217cb7c0aSRafael Espindola         Ret[Id].p_flags |= Sec->getPhdrFlags();
843bbe38602SEugene Leviant     }
844bbe38602SEugene Leviant   }
845edebbdf1SRui Ueyama   return Ret;
846bbe38602SEugene Leviant }
847bbe38602SEugene Leviant 
848f9bc3bd2SEugene Leviant template <class ELFT> bool LinkerScript<ELFT>::ignoreInterpSection() {
849f9bc3bd2SEugene Leviant   // Ignore .interp section in case we have PHDRS specification
850f9bc3bd2SEugene Leviant   // and PT_INTERP isn't listed.
851f9bc3bd2SEugene Leviant   return !Opt.PhdrsCommands.empty() &&
852f9bc3bd2SEugene Leviant          llvm::find_if(Opt.PhdrsCommands, [](const PhdrsCommand &Cmd) {
853f9bc3bd2SEugene Leviant            return Cmd.Type == PT_INTERP;
854f9bc3bd2SEugene Leviant          }) == Opt.PhdrsCommands.end();
855f9bc3bd2SEugene Leviant }
856f9bc3bd2SEugene Leviant 
85793c64025SGeorge Rimar template <class ELFT> uint32_t LinkerScript<ELFT>::getFiller(StringRef Name) {
858f6c3ccefSGeorge Rimar   for (const std::unique_ptr<BaseCommand> &Base : Opt.Commands)
859f6c3ccefSGeorge Rimar     if (auto *Cmd = dyn_cast<OutputSectionCommand>(Base.get()))
860f6c3ccefSGeorge Rimar       if (Cmd->Name == Name)
861f6c3ccefSGeorge Rimar         return Cmd->Filler;
86216068aebSRui Ueyama   return 0;
863e2ee72b5SGeorge Rimar }
864e2ee72b5SGeorge Rimar 
865e38cbab5SGeorge Rimar template <class ELFT>
866e38cbab5SGeorge Rimar static void writeInt(uint8_t *Buf, uint64_t Data, uint64_t Size) {
867e38cbab5SGeorge Rimar   const endianness E = ELFT::TargetEndianness;
868e38cbab5SGeorge Rimar 
869e38cbab5SGeorge Rimar   switch (Size) {
870e38cbab5SGeorge Rimar   case 1:
871e38cbab5SGeorge Rimar     *Buf = (uint8_t)Data;
872e38cbab5SGeorge Rimar     break;
873e38cbab5SGeorge Rimar   case 2:
874e38cbab5SGeorge Rimar     write16<E>(Buf, Data);
875e38cbab5SGeorge Rimar     break;
876e38cbab5SGeorge Rimar   case 4:
877e38cbab5SGeorge Rimar     write32<E>(Buf, Data);
878e38cbab5SGeorge Rimar     break;
879e38cbab5SGeorge Rimar   case 8:
880e38cbab5SGeorge Rimar     write64<E>(Buf, Data);
881e38cbab5SGeorge Rimar     break;
882e38cbab5SGeorge Rimar   default:
883e38cbab5SGeorge Rimar     llvm_unreachable("unsupported Size argument");
884e38cbab5SGeorge Rimar   }
885e38cbab5SGeorge Rimar }
886e38cbab5SGeorge Rimar 
887e38cbab5SGeorge Rimar template <class ELFT>
888e38cbab5SGeorge Rimar void LinkerScript<ELFT>::writeDataBytes(StringRef Name, uint8_t *Buf) {
889e38cbab5SGeorge Rimar   int I = getSectionIndex(Name);
890e38cbab5SGeorge Rimar   if (I == INT_MAX)
891e38cbab5SGeorge Rimar     return;
892e38cbab5SGeorge Rimar 
8936e68c5e5SRui Ueyama   auto *Cmd = dyn_cast<OutputSectionCommand>(Opt.Commands[I].get());
8946e68c5e5SRui Ueyama   for (const std::unique_ptr<BaseCommand> &Base : Cmd->Commands)
8956e68c5e5SRui Ueyama     if (auto *Data = dyn_cast<BytesDataCommand>(Base.get()))
89695c7d8d2SMeador Inge       writeInt<ELFT>(Buf + Data->Offset, Data->Expression(0), Data->Size);
897e38cbab5SGeorge Rimar }
898e38cbab5SGeorge Rimar 
899b71d6f7aSEugene Leviant template <class ELFT> bool LinkerScript<ELFT>::hasLMA(StringRef Name) {
9008ceadb38SGeorge Rimar   for (const std::unique_ptr<BaseCommand> &Base : Opt.Commands)
9018ceadb38SGeorge Rimar     if (auto *Cmd = dyn_cast<OutputSectionCommand>(Base.get()))
902b71d6f7aSEugene Leviant       if (Cmd->LMAExpr && Cmd->Name == Name)
903b71d6f7aSEugene Leviant         return true;
904b71d6f7aSEugene Leviant   return false;
9058ceadb38SGeorge Rimar }
9068ceadb38SGeorge Rimar 
907c3e2a4b0SRui Ueyama // Returns the index of the given section name in linker script
908c3e2a4b0SRui Ueyama // SECTIONS commands. Sections are laid out as the same order as they
909c3e2a4b0SRui Ueyama // were in the script. If a given name did not appear in the script,
910c3e2a4b0SRui Ueyama // it returns INT_MAX, so that it will be laid out at end of file.
911076fe157SGeorge Rimar template <class ELFT> int LinkerScript<ELFT>::getSectionIndex(StringRef Name) {
9126e68c5e5SRui Ueyama   for (int I = 0, E = Opt.Commands.size(); I != E; ++I)
9136e68c5e5SRui Ueyama     if (auto *Cmd = dyn_cast<OutputSectionCommand>(Opt.Commands[I].get()))
914076fe157SGeorge Rimar       if (Cmd->Name == Name)
915f510fa6bSRui Ueyama         return I;
916f510fa6bSRui Ueyama   return INT_MAX;
91771b26e94SGeorge Rimar }
91871b26e94SGeorge Rimar 
919bbe38602SEugene Leviant template <class ELFT> bool LinkerScript<ELFT>::hasPhdrsCommands() {
920bbe38602SEugene Leviant   return !Opt.PhdrsCommands.empty();
921bbe38602SEugene Leviant }
922bbe38602SEugene Leviant 
9239e69450eSGeorge Rimar template <class ELFT>
924ed30ce7aSEugene Leviant const OutputSectionBase *LinkerScript<ELFT>::getOutputSection(const Twine &Loc,
925ed30ce7aSEugene Leviant                                                               StringRef Name) {
926afaa9343SEugene Leviant   static OutputSectionBase FakeSec("", 0, 0);
92796659df0SGeorge Rimar 
928e08e78dfSRafael Espindola   for (OutputSectionBase *Sec : *OutputSections)
929b71d6f7aSEugene Leviant     if (Sec->getName() == Name)
930afaa9343SEugene Leviant       return Sec;
931ed30ce7aSEugene Leviant 
932ed30ce7aSEugene Leviant   error(Loc + ": undefined section " + Name);
933afaa9343SEugene Leviant   return &FakeSec;
93436fac7f0SEugene Leviant }
93536fac7f0SEugene Leviant 
936edf75e79SRui Ueyama // This function is essentially the same as getOutputSection(Name)->Size,
937edf75e79SRui Ueyama // but it won't print out an error message if a given section is not found.
938edf75e79SRui Ueyama //
939edf75e79SRui Ueyama // Linker script does not create an output section if its content is empty.
940edf75e79SRui Ueyama // We want to allow SIZEOF(.foo) where .foo is a section which happened to
941edf75e79SRui Ueyama // be empty. That is why this function is different from getOutputSection().
942edf75e79SRui Ueyama template <class ELFT>
943edf75e79SRui Ueyama uint64_t LinkerScript<ELFT>::getOutputSectionSize(StringRef Name) {
944edf75e79SRui Ueyama   for (OutputSectionBase *Sec : *OutputSections)
945edf75e79SRui Ueyama     if (Sec->getName() == Name)
946edf75e79SRui Ueyama       return Sec->Size;
947edf75e79SRui Ueyama   return 0;
948edf75e79SRui Ueyama }
949edf75e79SRui Ueyama 
950884e786dSGeorge Rimar template <class ELFT> uint64_t LinkerScript<ELFT>::getHeaderSize() {
9510d4b6d5cSRafael Espindola   return elf::getHeaderSize<ELFT>();
952e32a3598SGeorge Rimar }
953e32a3598SGeorge Rimar 
954f6aeed36SEugene Leviant template <class ELFT>
955f6aeed36SEugene Leviant uint64_t LinkerScript<ELFT>::getSymbolValue(const Twine &Loc, StringRef S) {
956884e786dSGeorge Rimar   if (SymbolBody *B = Symtab<ELFT>::X->find(S))
957884e786dSGeorge Rimar     return B->getVA<ELFT>();
958f6aeed36SEugene Leviant   error(Loc + ": symbol not found: " + S);
959884e786dSGeorge Rimar   return 0;
960884e786dSGeorge Rimar }
961884e786dSGeorge Rimar 
962f34f45fdSGeorge Rimar template <class ELFT> bool LinkerScript<ELFT>::isDefined(StringRef S) {
963f34f45fdSGeorge Rimar   return Symtab<ELFT>::X->find(S) != nullptr;
964f34f45fdSGeorge Rimar }
965f34f45fdSGeorge Rimar 
9662f831dcaSRafael Espindola template <class ELFT> bool LinkerScript<ELFT>::isAbsolute(StringRef S) {
9672f831dcaSRafael Espindola   SymbolBody *Sym = Symtab<ELFT>::X->find(S);
9682f831dcaSRafael Espindola   auto *DR = dyn_cast_or_null<DefinedRegular<ELFT>>(Sym);
9692f831dcaSRafael Espindola   return DR && !DR->Section;
9702f831dcaSRafael Espindola }
9712f831dcaSRafael Espindola 
972afaa9343SEugene Leviant // Gets section symbol belongs to. Symbol "." doesn't belong to any
973afaa9343SEugene Leviant // specific section but isn't absolute at the same time, so we try
974afaa9343SEugene Leviant // to find suitable section for it as well.
975afaa9343SEugene Leviant template <class ELFT>
976afaa9343SEugene Leviant const OutputSectionBase *LinkerScript<ELFT>::getSymbolSection(StringRef S) {
97706f4743aSRafael Espindola   if (SymbolBody *Sym = Symtab<ELFT>::X->find(S))
97860aed443SGeorge Rimar     return SymbolTableSection<ELFT>::getOutputSection(Sym);
97906f4743aSRafael Espindola   return CurOutSec;
980afaa9343SEugene Leviant }
981afaa9343SEugene Leviant 
982bbe38602SEugene Leviant // Returns indices of ELF headers containing specific section, identified
983bbe38602SEugene Leviant // by Name. Each index is a zero based number of ELF header listed within
984bbe38602SEugene Leviant // PHDRS {} script block.
985bbe38602SEugene Leviant template <class ELFT>
986edebbdf1SRui Ueyama std::vector<size_t> LinkerScript<ELFT>::getPhdrIndices(StringRef SectionName) {
987076fe157SGeorge Rimar   for (const std::unique_ptr<BaseCommand> &Base : Opt.Commands) {
988076fe157SGeorge Rimar     auto *Cmd = dyn_cast<OutputSectionCommand>(Base.get());
989edebbdf1SRui Ueyama     if (!Cmd || Cmd->Name != SectionName)
99031d842f5SGeorge Rimar       continue;
99131d842f5SGeorge Rimar 
99229c5a2a9SRui Ueyama     std::vector<size_t> Ret;
99329c5a2a9SRui Ueyama     for (StringRef PhdrName : Cmd->Phdrs)
9942a942c4bSEugene Leviant       Ret.push_back(getPhdrIndex(Cmd->Location, PhdrName));
99529c5a2a9SRui Ueyama     return Ret;
996bbe38602SEugene Leviant   }
99731d842f5SGeorge Rimar   return {};
99831d842f5SGeorge Rimar }
999bbe38602SEugene Leviant 
100029c5a2a9SRui Ueyama template <class ELFT>
10012a942c4bSEugene Leviant size_t LinkerScript<ELFT>::getPhdrIndex(const Twine &Loc, StringRef PhdrName) {
100229c5a2a9SRui Ueyama   size_t I = 0;
100329c5a2a9SRui Ueyama   for (PhdrsCommand &Cmd : Opt.PhdrsCommands) {
100429c5a2a9SRui Ueyama     if (Cmd.Name == PhdrName)
100529c5a2a9SRui Ueyama       return I;
100629c5a2a9SRui Ueyama     ++I;
100729c5a2a9SRui Ueyama   }
10082a942c4bSEugene Leviant   error(Loc + ": section header '" + PhdrName + "' is not listed in PHDRS");
100929c5a2a9SRui Ueyama   return 0;
101029c5a2a9SRui Ueyama }
101129c5a2a9SRui Ueyama 
101203ff0166SEugene Leviant class elf::ScriptParser final : public ScriptParserBase {
1013c3794e58SGeorge Rimar   typedef void (ScriptParser::*Handler)();
1014c3794e58SGeorge Rimar 
1015f7c5fbb1SRui Ueyama public:
101622375f24SRui Ueyama   ScriptParser(MemoryBufferRef MB)
101722375f24SRui Ueyama       : ScriptParserBase(MB),
101822375f24SRui Ueyama         IsUnderSysroot(isUnderSysroot(MB.getBufferIdentifier())) {}
1019f23b2320SGeorge Rimar 
102020b6598cSGeorge Rimar   void readLinkerScript();
102120b6598cSGeorge Rimar   void readVersionScript();
1022d0ebd84cSRafael Espindola   void readDynamicList();
1023f7c5fbb1SRui Ueyama 
1024f7c5fbb1SRui Ueyama private:
102552a1509eSRui Ueyama   void addFile(StringRef Path);
102652a1509eSRui Ueyama 
1027f7c5fbb1SRui Ueyama   void readAsNeeded();
102890c5099eSDenis Protivensky   void readEntry();
102983f406cfSGeorge Rimar   void readExtern();
1030f7c5fbb1SRui Ueyama   void readGroup();
103131aa1f83SRui Ueyama   void readInclude();
1032b889744eSMeador Inge   void readMemory();
1033ee59282bSRui Ueyama   void readOutput();
10349159ce93SDavide Italiano   void readOutputArch();
1035f7c5fbb1SRui Ueyama   void readOutputFormat();
1036bbe38602SEugene Leviant   void readPhdrs();
103768a39a65SDavide Italiano   void readSearchDir();
10388e3b38abSDenis Protivensky   void readSections();
103995769b4aSRui Ueyama   void readVersion();
104095769b4aSRui Ueyama   void readVersionScriptCommand();
10418e3b38abSDenis Protivensky 
1042113cdec9SRui Ueyama   SymbolAssignment *readAssignment(StringRef Name);
1043e38cbab5SGeorge Rimar   BytesDataCommand *readBytesDataCommand(StringRef Tok);
104416068aebSRui Ueyama   uint32_t readFill();
104510416564SRui Ueyama   OutputSectionCommand *readOutputSectionDescription(StringRef OutSec);
104616068aebSRui Ueyama   uint32_t readOutputSectionFiller(StringRef Tok);
1047bbe38602SEugene Leviant   std::vector<StringRef> readOutputSectionPhdrs();
1048a2496cbeSGeorge Rimar   InputSectionDescription *readInputSectionDescription(StringRef Tok);
1049db688454SEugene Leviant   StringMatcher readFilePatterns();
105007171f21SGeorge Rimar   std::vector<SectionPattern> readInputSectionsList();
1051a2496cbeSGeorge Rimar   InputSectionDescription *readInputSectionRules(StringRef FilePattern);
1052bbe38602SEugene Leviant   unsigned readPhdrType();
1053be394db3SGeorge Rimar   SortSectionPolicy readSortKind();
1054a35e39caSPetr Hosek   SymbolAssignment *readProvideHidden(bool Provide, bool Hidden);
1055c96da110SRafael Espindola   SymbolAssignment *readProvideOrAssignment(StringRef Tok);
105603fc010eSGeorge Rimar   void readSort();
1057eefa758eSGeorge Rimar   Expr readAssert();
1058708019c4SRui Ueyama 
105924e626ccSRui Ueyama   uint64_t readMemoryAssignment(StringRef, StringRef, StringRef);
106024e626ccSRui Ueyama   std::pair<uint32_t, uint32_t> readMemoryAttributes();
106124e626ccSRui Ueyama 
1062708019c4SRui Ueyama   Expr readExpr();
1063708019c4SRui Ueyama   Expr readExpr1(Expr Lhs, int MinPrec);
1064b71d6f7aSEugene Leviant   StringRef readParenLiteral();
1065708019c4SRui Ueyama   Expr readPrimary();
1066708019c4SRui Ueyama   Expr readTernary(Expr Cond);
10676ad7dfccSRui Ueyama   Expr readParenExpr();
1068f7c5fbb1SRui Ueyama 
106920b6598cSGeorge Rimar   // For parsing version script.
107012450b20SRui Ueyama   std::vector<SymbolVersion> readVersionExtern();
107112450b20SRui Ueyama   void readAnonymousDeclaration();
107295769b4aSRui Ueyama   void readVersionDeclaration(StringRef VerStr);
107312450b20SRui Ueyama   std::vector<SymbolVersion> readSymbols();
1074e999ddb8SRafael Espindola   void readLocals();
107520b6598cSGeorge Rimar 
107607320e40SRui Ueyama   ScriptConfiguration &Opt = *ScriptConfig;
107716b0cc9eSSimon Atanasyan   bool IsUnderSysroot;
1078f7c5fbb1SRui Ueyama };
1079f7c5fbb1SRui Ueyama 
1080d0ebd84cSRafael Espindola void ScriptParser::readDynamicList() {
1081d0ebd84cSRafael Espindola   expect("{");
1082d0ebd84cSRafael Espindola   readAnonymousDeclaration();
1083d0ebd84cSRafael Espindola   if (!atEOF())
1084d0ebd84cSRafael Espindola     setError("EOF expected, but got " + next());
1085d0ebd84cSRafael Espindola }
1086d0ebd84cSRafael Espindola 
108720b6598cSGeorge Rimar void ScriptParser::readVersionScript() {
108895769b4aSRui Ueyama   readVersionScriptCommand();
108920b6598cSGeorge Rimar   if (!atEOF())
109095769b4aSRui Ueyama     setError("EOF expected, but got " + next());
109195769b4aSRui Ueyama }
109295769b4aSRui Ueyama 
109395769b4aSRui Ueyama void ScriptParser::readVersionScriptCommand() {
109483043f23SRui Ueyama   if (consume("{")) {
109512450b20SRui Ueyama     readAnonymousDeclaration();
109620b6598cSGeorge Rimar     return;
109720b6598cSGeorge Rimar   }
109820b6598cSGeorge Rimar 
109995769b4aSRui Ueyama   while (!atEOF() && !Error && peek() != "}") {
110020b6598cSGeorge Rimar     StringRef VerStr = next();
110120b6598cSGeorge Rimar     if (VerStr == "{") {
110295769b4aSRui Ueyama       setError("anonymous version definition is used in "
110395769b4aSRui Ueyama                "combination with other version definitions");
110420b6598cSGeorge Rimar       return;
110520b6598cSGeorge Rimar     }
110620b6598cSGeorge Rimar     expect("{");
110795769b4aSRui Ueyama     readVersionDeclaration(VerStr);
110820b6598cSGeorge Rimar   }
110920b6598cSGeorge Rimar }
111020b6598cSGeorge Rimar 
111195769b4aSRui Ueyama void ScriptParser::readVersion() {
111295769b4aSRui Ueyama   expect("{");
111395769b4aSRui Ueyama   readVersionScriptCommand();
111495769b4aSRui Ueyama   expect("}");
111595769b4aSRui Ueyama }
111695769b4aSRui Ueyama 
111720b6598cSGeorge Rimar void ScriptParser::readLinkerScript() {
1118f7c5fbb1SRui Ueyama   while (!atEOF()) {
1119f7c5fbb1SRui Ueyama     StringRef Tok = next();
1120a27eeccaSRui Ueyama     if (Tok == ";")
1121a27eeccaSRui Ueyama       continue;
1122a27eeccaSRui Ueyama 
112320d03194SEugene Leviant     if (Tok == "ASSERT") {
112420d03194SEugene Leviant       Opt.Commands.emplace_back(new AssertCommand(readAssert()));
112520d03194SEugene Leviant     } else if (Tok == "ENTRY") {
1126a27eeccaSRui Ueyama       readEntry();
1127a27eeccaSRui Ueyama     } else if (Tok == "EXTERN") {
1128a27eeccaSRui Ueyama       readExtern();
1129a27eeccaSRui Ueyama     } else if (Tok == "GROUP" || Tok == "INPUT") {
1130a27eeccaSRui Ueyama       readGroup();
1131a27eeccaSRui Ueyama     } else if (Tok == "INCLUDE") {
1132a27eeccaSRui Ueyama       readInclude();
1133b889744eSMeador Inge     } else if (Tok == "MEMORY") {
1134b889744eSMeador Inge       readMemory();
1135a27eeccaSRui Ueyama     } else if (Tok == "OUTPUT") {
1136a27eeccaSRui Ueyama       readOutput();
1137a27eeccaSRui Ueyama     } else if (Tok == "OUTPUT_ARCH") {
1138a27eeccaSRui Ueyama       readOutputArch();
1139a27eeccaSRui Ueyama     } else if (Tok == "OUTPUT_FORMAT") {
1140a27eeccaSRui Ueyama       readOutputFormat();
1141a27eeccaSRui Ueyama     } else if (Tok == "PHDRS") {
1142a27eeccaSRui Ueyama       readPhdrs();
1143a27eeccaSRui Ueyama     } else if (Tok == "SEARCH_DIR") {
1144a27eeccaSRui Ueyama       readSearchDir();
1145a27eeccaSRui Ueyama     } else if (Tok == "SECTIONS") {
1146a27eeccaSRui Ueyama       readSections();
1147a27eeccaSRui Ueyama     } else if (Tok == "VERSION") {
1148a27eeccaSRui Ueyama       readVersion();
1149c96da110SRafael Espindola     } else if (SymbolAssignment *Cmd = readProvideOrAssignment(Tok)) {
11500df80befSPetr Hosek       Opt.Commands.emplace_back(Cmd);
1151e5d3ca50SPetr Hosek     } else {
11525761042dSGeorge Rimar       setError("unknown directive: " + Tok);
1153f7c5fbb1SRui Ueyama     }
1154f7c5fbb1SRui Ueyama   }
1155e5d3ca50SPetr Hosek }
1156f7c5fbb1SRui Ueyama 
1157717677afSRui Ueyama void ScriptParser::addFile(StringRef S) {
115816b0cc9eSSimon Atanasyan   if (IsUnderSysroot && S.startswith("/")) {
11595af1687fSJustin Bogner     SmallString<128> PathData;
11605af1687fSJustin Bogner     StringRef Path = (Config->Sysroot + S).toStringRef(PathData);
116116b0cc9eSSimon Atanasyan     if (sys::fs::exists(Path)) {
11625af1687fSJustin Bogner       Driver->addFile(Saver.save(Path));
116316b0cc9eSSimon Atanasyan       return;
116416b0cc9eSSimon Atanasyan     }
116516b0cc9eSSimon Atanasyan   }
116616b0cc9eSSimon Atanasyan 
1167f03f3cc1SRui Ueyama   if (sys::path::is_absolute(S)) {
116852a1509eSRui Ueyama     Driver->addFile(S);
116952a1509eSRui Ueyama   } else if (S.startswith("=")) {
117052a1509eSRui Ueyama     if (Config->Sysroot.empty())
117152a1509eSRui Ueyama       Driver->addFile(S.substr(1));
117252a1509eSRui Ueyama     else
117352a1509eSRui Ueyama       Driver->addFile(Saver.save(Config->Sysroot + "/" + S.substr(1)));
117452a1509eSRui Ueyama   } else if (S.startswith("-l")) {
117521eecb4fSRui Ueyama     Driver->addLibrary(S.substr(2));
1176a1b8fc3bSSimon Atanasyan   } else if (sys::fs::exists(S)) {
1177a1b8fc3bSSimon Atanasyan     Driver->addFile(S);
117852a1509eSRui Ueyama   } else {
1179061f9286SRui Ueyama     if (Optional<std::string> Path = findFromSearchPaths(S))
1180061f9286SRui Ueyama       Driver->addFile(Saver.save(*Path));
1181025d59b1SRui Ueyama     else
1182061f9286SRui Ueyama       setError("unable to find " + S);
118352a1509eSRui Ueyama   }
118452a1509eSRui Ueyama }
118552a1509eSRui Ueyama 
1186717677afSRui Ueyama void ScriptParser::readAsNeeded() {
1187f7c5fbb1SRui Ueyama   expect("(");
118835da9b6eSRui Ueyama   bool Orig = Config->AsNeeded;
118935da9b6eSRui Ueyama   Config->AsNeeded = true;
119083043f23SRui Ueyama   while (!Error && !consume(")"))
1191cd574a5eSGeorge Rimar     addFile(unquote(next()));
119235da9b6eSRui Ueyama   Config->AsNeeded = Orig;
1193f7c5fbb1SRui Ueyama }
1194f7c5fbb1SRui Ueyama 
1195717677afSRui Ueyama void ScriptParser::readEntry() {
119690c5099eSDenis Protivensky   // -e <symbol> takes predecence over ENTRY(<symbol>).
119790c5099eSDenis Protivensky   expect("(");
119890c5099eSDenis Protivensky   StringRef Tok = next();
119990c5099eSDenis Protivensky   if (Config->Entry.empty())
120090c5099eSDenis Protivensky     Config->Entry = Tok;
120190c5099eSDenis Protivensky   expect(")");
120290c5099eSDenis Protivensky }
120390c5099eSDenis Protivensky 
1204717677afSRui Ueyama void ScriptParser::readExtern() {
120583f406cfSGeorge Rimar   expect("(");
120683043f23SRui Ueyama   while (!Error && !consume(")"))
1207a2acc931SRui Ueyama     Config->Undefined.push_back(next());
120883f406cfSGeorge Rimar }
120983f406cfSGeorge Rimar 
1210717677afSRui Ueyama void ScriptParser::readGroup() {
1211f7c5fbb1SRui Ueyama   expect("(");
121283043f23SRui Ueyama   while (!Error && !consume(")")) {
1213f7c5fbb1SRui Ueyama     StringRef Tok = next();
1214a2acc931SRui Ueyama     if (Tok == "AS_NEEDED")
1215f7c5fbb1SRui Ueyama       readAsNeeded();
1216a2acc931SRui Ueyama     else
1217cd574a5eSGeorge Rimar       addFile(unquote(Tok));
1218f7c5fbb1SRui Ueyama   }
1219f7c5fbb1SRui Ueyama }
1220f7c5fbb1SRui Ueyama 
1221717677afSRui Ueyama void ScriptParser::readInclude() {
1222d4500653SGeorge Rimar   StringRef Tok = unquote(next());
1223ec1c75e0SRui Ueyama 
1224d4500653SGeorge Rimar   // https://sourceware.org/binutils/docs/ld/File-Commands.html:
1225d4500653SGeorge Rimar   // The file will be searched for in the current directory, and in any
1226d4500653SGeorge Rimar   // directory specified with the -L option.
1227ec1c75e0SRui Ueyama   if (sys::fs::exists(Tok)) {
1228ec1c75e0SRui Ueyama     if (Optional<MemoryBufferRef> MB = readFile(Tok))
1229ec1c75e0SRui Ueyama       tokenize(*MB);
1230025d59b1SRui Ueyama     return;
1231025d59b1SRui Ueyama   }
1232ec1c75e0SRui Ueyama   if (Optional<std::string> Path = findFromSearchPaths(Tok)) {
1233ec1c75e0SRui Ueyama     if (Optional<MemoryBufferRef> MB = readFile(*Path))
1234ec1c75e0SRui Ueyama       tokenize(*MB);
1235ec1c75e0SRui Ueyama     return;
1236ec1c75e0SRui Ueyama   }
1237ec1c75e0SRui Ueyama   setError("cannot open " + Tok);
123831aa1f83SRui Ueyama }
123931aa1f83SRui Ueyama 
1240717677afSRui Ueyama void ScriptParser::readOutput() {
1241ee59282bSRui Ueyama   // -o <file> takes predecence over OUTPUT(<file>).
1242ee59282bSRui Ueyama   expect("(");
1243ee59282bSRui Ueyama   StringRef Tok = next();
1244ee59282bSRui Ueyama   if (Config->OutputFile.empty())
1245cd574a5eSGeorge Rimar     Config->OutputFile = unquote(Tok);
1246ee59282bSRui Ueyama   expect(")");
1247ee59282bSRui Ueyama }
1248ee59282bSRui Ueyama 
1249717677afSRui Ueyama void ScriptParser::readOutputArch() {
1250*4e01c3e8SGeorge Rimar   // OUTPUT_ARCH is ignored for now.
12519159ce93SDavide Italiano   expect("(");
1252*4e01c3e8SGeorge Rimar   while (!Error && !consume(")"))
12535424e7c7SJustin Bogner     skip();
12549159ce93SDavide Italiano }
12559159ce93SDavide Italiano 
1256717677afSRui Ueyama void ScriptParser::readOutputFormat() {
1257f7c5fbb1SRui Ueyama   // Error checking only for now.
1258f7c5fbb1SRui Ueyama   expect("(");
12595424e7c7SJustin Bogner   skip();
12606836c618SDavide Italiano   StringRef Tok = next();
12616836c618SDavide Italiano   if (Tok == ")")
12626836c618SDavide Italiano     return;
1263025d59b1SRui Ueyama   if (Tok != ",") {
12645761042dSGeorge Rimar     setError("unexpected token: " + Tok);
1265025d59b1SRui Ueyama     return;
1266025d59b1SRui Ueyama   }
12675424e7c7SJustin Bogner   skip();
12686836c618SDavide Italiano   expect(",");
12695424e7c7SJustin Bogner   skip();
1270f7c5fbb1SRui Ueyama   expect(")");
1271f7c5fbb1SRui Ueyama }
1272f7c5fbb1SRui Ueyama 
1273bbe38602SEugene Leviant void ScriptParser::readPhdrs() {
1274bbe38602SEugene Leviant   expect("{");
127583043f23SRui Ueyama   while (!Error && !consume("}")) {
1276bbe38602SEugene Leviant     StringRef Tok = next();
127756b21c86SEugene Leviant     Opt.PhdrsCommands.push_back(
127856b21c86SEugene Leviant         {Tok, PT_NULL, false, false, UINT_MAX, nullptr});
1279bbe38602SEugene Leviant     PhdrsCommand &PhdrCmd = Opt.PhdrsCommands.back();
1280bbe38602SEugene Leviant 
1281bbe38602SEugene Leviant     PhdrCmd.Type = readPhdrType();
1282bbe38602SEugene Leviant     do {
1283bbe38602SEugene Leviant       Tok = next();
1284bbe38602SEugene Leviant       if (Tok == ";")
1285bbe38602SEugene Leviant         break;
1286bbe38602SEugene Leviant       if (Tok == "FILEHDR")
1287bbe38602SEugene Leviant         PhdrCmd.HasFilehdr = true;
1288bbe38602SEugene Leviant       else if (Tok == "PHDRS")
1289bbe38602SEugene Leviant         PhdrCmd.HasPhdrs = true;
129056b21c86SEugene Leviant       else if (Tok == "AT")
129156b21c86SEugene Leviant         PhdrCmd.LMAExpr = readParenExpr();
1292865bf863SEugene Leviant       else if (Tok == "FLAGS") {
1293865bf863SEugene Leviant         expect("(");
1294eb685cd7SRafael Espindola         // Passing 0 for the value of dot is a bit of a hack. It means that
1295eb685cd7SRafael Espindola         // we accept expressions like ".|1".
1296eb685cd7SRafael Espindola         PhdrCmd.Flags = readExpr()(0);
1297865bf863SEugene Leviant         expect(")");
1298865bf863SEugene Leviant       } else
1299bbe38602SEugene Leviant         setError("unexpected header attribute: " + Tok);
1300bbe38602SEugene Leviant     } while (!Error);
1301bbe38602SEugene Leviant   }
1302bbe38602SEugene Leviant }
1303bbe38602SEugene Leviant 
1304717677afSRui Ueyama void ScriptParser::readSearchDir() {
130568a39a65SDavide Italiano   expect("(");
130686c5fb82SRui Ueyama   StringRef Tok = next();
13076c7ad13fSRui Ueyama   if (!Config->Nostdlib)
1308cd574a5eSGeorge Rimar     Config->SearchPaths.push_back(unquote(Tok));
130968a39a65SDavide Italiano   expect(")");
131068a39a65SDavide Italiano }
131168a39a65SDavide Italiano 
1312717677afSRui Ueyama void ScriptParser::readSections() {
1313e05336ffSEugene Leviant   Opt.HasSections = true;
131418a30962SGeorge Rimar   // -no-rosegment is used to avoid placing read only non-executable sections in
131518a30962SGeorge Rimar   // their own segment. We do the same if SECTIONS command is present in linker
131618a30962SGeorge Rimar   // script. See comment for computeFlags().
131718a30962SGeorge Rimar   Config->SingleRoRx = true;
131818a30962SGeorge Rimar 
13198e3b38abSDenis Protivensky   expect("{");
132083043f23SRui Ueyama   while (!Error && !consume("}")) {
1321113cdec9SRui Ueyama     StringRef Tok = next();
1322c96da110SRafael Espindola     BaseCommand *Cmd = readProvideOrAssignment(Tok);
1323ceabe80eSEugene Leviant     if (!Cmd) {
1324ceabe80eSEugene Leviant       if (Tok == "ASSERT")
1325eefa758eSGeorge Rimar         Cmd = new AssertCommand(readAssert());
1326ceabe80eSEugene Leviant       else
132710416564SRui Ueyama         Cmd = readOutputSectionDescription(Tok);
13288e3b38abSDenis Protivensky     }
132910416564SRui Ueyama     Opt.Commands.emplace_back(Cmd);
1330652852c5SGeorge Rimar   }
1331708019c4SRui Ueyama }
13328e3b38abSDenis Protivensky 
1333708019c4SRui Ueyama static int precedence(StringRef Op) {
1334708019c4SRui Ueyama   return StringSwitch<int>(Op)
13350120e3f2SRui Ueyama       .Cases("*", "/", 5)
13360120e3f2SRui Ueyama       .Cases("+", "-", 4)
13370120e3f2SRui Ueyama       .Cases("<<", ">>", 3)
13389c4ac5f2SRui Ueyama       .Cases("<", "<=", ">", ">=", "==", "!=", 2)
13390120e3f2SRui Ueyama       .Cases("&", "|", 1)
1340708019c4SRui Ueyama       .Default(-1);
1341708019c4SRui Ueyama }
1342708019c4SRui Ueyama 
1343db688454SEugene Leviant StringMatcher ScriptParser::readFilePatterns() {
134410416564SRui Ueyama   std::vector<StringRef> V;
134583043f23SRui Ueyama   while (!Error && !consume(")"))
134610416564SRui Ueyama     V.push_back(next());
1347f91282e1SRui Ueyama   return StringMatcher(V);
13480702c4e8SGeorge Rimar }
13490702c4e8SGeorge Rimar 
1350be394db3SGeorge Rimar SortSectionPolicy ScriptParser::readSortKind() {
135183043f23SRui Ueyama   if (consume("SORT") || consume("SORT_BY_NAME"))
1352be394db3SGeorge Rimar     return SortSectionPolicy::Name;
135383043f23SRui Ueyama   if (consume("SORT_BY_ALIGNMENT"))
1354be394db3SGeorge Rimar     return SortSectionPolicy::Alignment;
135583043f23SRui Ueyama   if (consume("SORT_BY_INIT_PRIORITY"))
1356be394db3SGeorge Rimar     return SortSectionPolicy::Priority;
135783043f23SRui Ueyama   if (consume("SORT_NONE"))
1358be394db3SGeorge Rimar     return SortSectionPolicy::None;
1359b2a0abdfSRui Ueyama   return SortSectionPolicy::Default;
1360be394db3SGeorge Rimar }
1361be394db3SGeorge Rimar 
1362395281cfSGeorge Rimar // Method reads a list of sequence of excluded files and section globs given in
1363395281cfSGeorge Rimar // a following form: ((EXCLUDE_FILE(file_pattern+))? section_pattern+)+
1364395281cfSGeorge Rimar // Example: *(.foo.1 EXCLUDE_FILE (*a.o) .foo.2 EXCLUDE_FILE (*b.o) .foo.3)
1365af03be19SGeorge Rimar // The semantics of that is next:
1366af03be19SGeorge Rimar // * Include .foo.1 from every file.
1367af03be19SGeorge Rimar // * Include .foo.2 from every file but a.o
1368af03be19SGeorge Rimar // * Include .foo.3 from every file but b.o
136907171f21SGeorge Rimar std::vector<SectionPattern> ScriptParser::readInputSectionsList() {
137007171f21SGeorge Rimar   std::vector<SectionPattern> Ret;
1371601e9898SGeorge Rimar   while (!Error && peek() != ")") {
1372f91282e1SRui Ueyama     StringMatcher ExcludeFilePat;
137383043f23SRui Ueyama     if (consume("EXCLUDE_FILE")) {
1374395281cfSGeorge Rimar       expect("(");
1375f91282e1SRui Ueyama       ExcludeFilePat = readFilePatterns();
1376395281cfSGeorge Rimar     }
1377395281cfSGeorge Rimar 
1378601e9898SGeorge Rimar     std::vector<StringRef> V;
1379601e9898SGeorge Rimar     while (!Error && peek() != ")" && peek() != "EXCLUDE_FILE")
1380395281cfSGeorge Rimar       V.push_back(next());
1381601e9898SGeorge Rimar 
1382601e9898SGeorge Rimar     if (!V.empty())
1383f91282e1SRui Ueyama       Ret.push_back({std::move(ExcludeFilePat), StringMatcher(V)});
1384601e9898SGeorge Rimar     else
1385601e9898SGeorge Rimar       setError("section pattern is expected");
1386395281cfSGeorge Rimar   }
138707171f21SGeorge Rimar   return Ret;
1388395281cfSGeorge Rimar }
1389395281cfSGeorge Rimar 
1390f8f6f1e7SRui Ueyama // Reads contents of "SECTIONS" directive. That directive contains a
1391f8f6f1e7SRui Ueyama // list of glob patterns for input sections. The grammar is as follows.
1392f8f6f1e7SRui Ueyama //
1393f8f6f1e7SRui Ueyama // <patterns> ::= <section-list>
1394f8f6f1e7SRui Ueyama //              | <sort> "(" <section-list> ")"
1395f8f6f1e7SRui Ueyama //              | <sort> "(" <sort> "(" <section-list> ")" ")"
1396f8f6f1e7SRui Ueyama //
1397f8f6f1e7SRui Ueyama // <sort>     ::= "SORT" | "SORT_BY_NAME" | "SORT_BY_ALIGNMENT"
1398f8f6f1e7SRui Ueyama //              | "SORT_BY_INIT_PRIORITY" | "SORT_NONE"
1399f8f6f1e7SRui Ueyama //
1400f8f6f1e7SRui Ueyama // <section-list> is parsed by readInputSectionsList().
1401a2496cbeSGeorge Rimar InputSectionDescription *
1402a2496cbeSGeorge Rimar ScriptParser::readInputSectionRules(StringRef FilePattern) {
1403c91930a1SGeorge Rimar   auto *Cmd = new InputSectionDescription(FilePattern);
14040ed42b0cSDavide Italiano   expect("(");
1405f373dd76SRui Ueyama   while (!Error && !consume(")")) {
140607171f21SGeorge Rimar     SortSectionPolicy Outer = readSortKind();
140707171f21SGeorge Rimar     SortSectionPolicy Inner = SortSectionPolicy::Default;
140807171f21SGeorge Rimar     std::vector<SectionPattern> V;
140907171f21SGeorge Rimar     if (Outer != SortSectionPolicy::Default) {
14100702c4e8SGeorge Rimar       expect("(");
141107171f21SGeorge Rimar       Inner = readSortKind();
141207171f21SGeorge Rimar       if (Inner != SortSectionPolicy::Default) {
1413350ece4eSGeorge Rimar         expect("(");
141407171f21SGeorge Rimar         V = readInputSectionsList();
14150702c4e8SGeorge Rimar         expect(")");
1416350ece4eSGeorge Rimar       } else {
141707171f21SGeorge Rimar         V = readInputSectionsList();
1418350ece4eSGeorge Rimar       }
1419350ece4eSGeorge Rimar       expect(")");
142007171f21SGeorge Rimar     } else {
142107171f21SGeorge Rimar       V = readInputSectionsList();
14220659800eSGeorge Rimar     }
14230702c4e8SGeorge Rimar 
142407171f21SGeorge Rimar     for (SectionPattern &Pat : V) {
142507171f21SGeorge Rimar       Pat.SortInner = Inner;
142607171f21SGeorge Rimar       Pat.SortOuter = Outer;
142707171f21SGeorge Rimar     }
142807171f21SGeorge Rimar 
142907171f21SGeorge Rimar     std::move(V.begin(), V.end(), std::back_inserter(Cmd->SectionPatterns));
143007171f21SGeorge Rimar   }
143110416564SRui Ueyama   return Cmd;
14320659800eSGeorge Rimar }
14330659800eSGeorge Rimar 
1434a2496cbeSGeorge Rimar InputSectionDescription *
1435a2496cbeSGeorge Rimar ScriptParser::readInputSectionDescription(StringRef Tok) {
14360659800eSGeorge Rimar   // Input section wildcard can be surrounded by KEEP.
14370659800eSGeorge Rimar   // https://sourceware.org/binutils/docs/ld/Input-Section-Keep.html#Input-Section-Keep
1438a2496cbeSGeorge Rimar   if (Tok == "KEEP") {
1439e7282797SDavide Italiano     expect("(");
1440a2496cbeSGeorge Rimar     StringRef FilePattern = next();
1441a2496cbeSGeorge Rimar     InputSectionDescription *Cmd = readInputSectionRules(FilePattern);
14420ed42b0cSDavide Italiano     expect(")");
1443cf43f179SEugene Leviant     Opt.KeptSections.push_back(Cmd);
144410416564SRui Ueyama     return Cmd;
144510416564SRui Ueyama   }
1446a2496cbeSGeorge Rimar   return readInputSectionRules(Tok);
14470659800eSGeorge Rimar }
14480659800eSGeorge Rimar 
144903fc010eSGeorge Rimar void ScriptParser::readSort() {
145003fc010eSGeorge Rimar   expect("(");
145103fc010eSGeorge Rimar   expect("CONSTRUCTORS");
145203fc010eSGeorge Rimar   expect(")");
145303fc010eSGeorge Rimar }
145403fc010eSGeorge Rimar 
1455eefa758eSGeorge Rimar Expr ScriptParser::readAssert() {
1456eefa758eSGeorge Rimar   expect("(");
1457eefa758eSGeorge Rimar   Expr E = readExpr();
1458eefa758eSGeorge Rimar   expect(",");
1459cd574a5eSGeorge Rimar   StringRef Msg = unquote(next());
1460eefa758eSGeorge Rimar   expect(")");
1461eefa758eSGeorge Rimar   return [=](uint64_t Dot) {
1462eefa758eSGeorge Rimar     uint64_t V = E(Dot);
1463eefa758eSGeorge Rimar     if (!V)
1464eefa758eSGeorge Rimar       error(Msg);
1465eefa758eSGeorge Rimar     return V;
1466eefa758eSGeorge Rimar   };
1467eefa758eSGeorge Rimar }
1468eefa758eSGeorge Rimar 
146925150e8bSRui Ueyama // Reads a FILL(expr) command. We handle the FILL command as an
147025150e8bSRui Ueyama // alias for =fillexp section attribute, which is different from
147125150e8bSRui Ueyama // what GNU linkers do.
147225150e8bSRui Ueyama // https://sourceware.org/binutils/docs/ld/Output-Section-Data.html
147316068aebSRui Ueyama uint32_t ScriptParser::readFill() {
1474ff1f29e0SGeorge Rimar   expect("(");
147516068aebSRui Ueyama   uint32_t V = readOutputSectionFiller(next());
1476ff1f29e0SGeorge Rimar   expect(")");
1477ff1f29e0SGeorge Rimar   expect(";");
1478ff1f29e0SGeorge Rimar   return V;
1479ff1f29e0SGeorge Rimar }
1480ff1f29e0SGeorge Rimar 
148110416564SRui Ueyama OutputSectionCommand *
148210416564SRui Ueyama ScriptParser::readOutputSectionDescription(StringRef OutSec) {
1483076fe157SGeorge Rimar   OutputSectionCommand *Cmd = new OutputSectionCommand(OutSec);
14842a942c4bSEugene Leviant   Cmd->Location = getCurrentLocation();
148558e5c4dcSGeorge Rimar 
148658e5c4dcSGeorge Rimar   // Read an address expression.
148758e5c4dcSGeorge Rimar   // https://sourceware.org/binutils/docs/ld/Output-Section-Address.html#Output-Section-Address
148858e5c4dcSGeorge Rimar   if (peek() != ":")
148958e5c4dcSGeorge Rimar     Cmd->AddrExpr = readExpr();
149058e5c4dcSGeorge Rimar 
14918e3b38abSDenis Protivensky   expect(":");
1492246f681eSDavide Italiano 
149383043f23SRui Ueyama   if (consume("AT"))
1494b71d6f7aSEugene Leviant     Cmd->LMAExpr = readParenExpr();
149583043f23SRui Ueyama   if (consume("ALIGN"))
14966ad7dfccSRui Ueyama     Cmd->AlignExpr = readParenExpr();
149783043f23SRui Ueyama   if (consume("SUBALIGN"))
1498db24d9c3SGeorge Rimar     Cmd->SubalignExpr = readParenExpr();
1499630c6179SGeorge Rimar 
1500246f681eSDavide Italiano   // Parse constraints.
150183043f23SRui Ueyama   if (consume("ONLY_IF_RO"))
1502efc4066bSRui Ueyama     Cmd->Constraint = ConstraintKind::ReadOnly;
150383043f23SRui Ueyama   if (consume("ONLY_IF_RW"))
1504efc4066bSRui Ueyama     Cmd->Constraint = ConstraintKind::ReadWrite;
15058e3b38abSDenis Protivensky   expect("{");
15068ec77e64SRui Ueyama 
150783043f23SRui Ueyama   while (!Error && !consume("}")) {
1508ceabe80eSEugene Leviant     StringRef Tok = next();
15092fe07923SGeorge Rimar     if (Tok == ";") {
151069750755SGeorge Rimar       // Empty commands are allowed. Do nothing here.
15112fe07923SGeorge Rimar     } else if (SymbolAssignment *Assignment = readProvideOrAssignment(Tok)) {
1512ceabe80eSEugene Leviant       Cmd->Commands.emplace_back(Assignment);
1513b2d99d6aSMeador Inge     } else if (BytesDataCommand *Data = readBytesDataCommand(Tok)) {
1514e38cbab5SGeorge Rimar       Cmd->Commands.emplace_back(Data);
1515b2d99d6aSMeador Inge     } else if (Tok == "ASSERT") {
1516b2d99d6aSMeador Inge       Cmd->Commands.emplace_back(new AssertCommand(readAssert()));
1517b2d99d6aSMeador Inge       expect(";");
15188e2eca22SGeorge Rimar     } else if (Tok == "CONSTRUCTORS") {
15198e2eca22SGeorge Rimar       // CONSTRUCTORS is a keyword to make the linker recognize C++ ctors/dtors
15208e2eca22SGeorge Rimar       // by name. This is for very old file formats such as ECOFF/XCOFF.
15218e2eca22SGeorge Rimar       // For ELF, we should ignore.
1522b2d99d6aSMeador Inge     } else if (Tok == "FILL") {
1523ff1f29e0SGeorge Rimar       Cmd->Filler = readFill();
1524b2d99d6aSMeador Inge     } else if (Tok == "SORT") {
152503fc010eSGeorge Rimar       readSort();
1526b2d99d6aSMeador Inge     } else if (peek() == "(") {
1527a2496cbeSGeorge Rimar       Cmd->Commands.emplace_back(readInputSectionDescription(Tok));
1528b2d99d6aSMeador Inge     } else {
1529ceabe80eSEugene Leviant       setError("unknown command " + Tok);
15308e3b38abSDenis Protivensky     }
1531b2d99d6aSMeador Inge   }
1532b889744eSMeador Inge 
1533b889744eSMeador Inge   if (consume(">"))
1534b889744eSMeador Inge     Cmd->MemoryRegionName = next();
1535b889744eSMeador Inge 
1536076fe157SGeorge Rimar   Cmd->Phdrs = readOutputSectionPhdrs();
15374ebc5620SGeorge Rimar 
153883043f23SRui Ueyama   if (consume("="))
15394ebc5620SGeorge Rimar     Cmd->Filler = readOutputSectionFiller(next());
15404ebc5620SGeorge Rimar   else if (peek().startswith("="))
1541ff1f29e0SGeorge Rimar     Cmd->Filler = readOutputSectionFiller(next().drop_front());
15424ebc5620SGeorge Rimar 
15437185a1acSGeorge Rimar   // Consume optional comma following output section command.
15447185a1acSGeorge Rimar   consume(",");
15457185a1acSGeorge Rimar 
154610416564SRui Ueyama   return Cmd;
1547f71caa2bSRui Ueyama }
15488ec77e64SRui Ueyama 
15492c8f1f04SRui Ueyama // Read "=<number>" where <number> is an octal/decimal/hexadecimal number.
15502c8f1f04SRui Ueyama // https://sourceware.org/binutils/docs/ld/Output-Section-Fill.html
15512c8f1f04SRui Ueyama //
15522c8f1f04SRui Ueyama // ld.gold is not fully compatible with ld.bfd. ld.bfd handles
15532c8f1f04SRui Ueyama // hexstrings as blobs of arbitrary sizes, while ld.gold handles them
15542c8f1f04SRui Ueyama // as 32-bit big-endian values. We will do the same as ld.gold does
15552c8f1f04SRui Ueyama // because it's simpler than what ld.bfd does.
155616068aebSRui Ueyama uint32_t ScriptParser::readOutputSectionFiller(StringRef Tok) {
1557965827d6SRui Ueyama   uint32_t V;
155816068aebSRui Ueyama   if (!Tok.getAsInteger(0, V))
155916068aebSRui Ueyama     return V;
1560965827d6SRui Ueyama   setError("invalid filler expression: " + Tok);
156116068aebSRui Ueyama   return 0;
15628e3b38abSDenis Protivensky }
15638e3b38abSDenis Protivensky 
1564a35e39caSPetr Hosek SymbolAssignment *ScriptParser::readProvideHidden(bool Provide, bool Hidden) {
1565a31c91b1SEugene Leviant   expect("(");
1566174e0a16SRui Ueyama   SymbolAssignment *Cmd = readAssignment(next());
1567a35e39caSPetr Hosek   Cmd->Provide = Provide;
1568174e0a16SRui Ueyama   Cmd->Hidden = Hidden;
1569a31c91b1SEugene Leviant   expect(")");
1570a31c91b1SEugene Leviant   expect(";");
157110416564SRui Ueyama   return Cmd;
1572eda81a1bSEugene Leviant }
1573eda81a1bSEugene Leviant 
1574c96da110SRafael Espindola SymbolAssignment *ScriptParser::readProvideOrAssignment(StringRef Tok) {
1575ceabe80eSEugene Leviant   SymbolAssignment *Cmd = nullptr;
1576ceabe80eSEugene Leviant   if (peek() == "=" || peek() == "+=") {
1577ceabe80eSEugene Leviant     Cmd = readAssignment(Tok);
1578ceabe80eSEugene Leviant     expect(";");
1579ceabe80eSEugene Leviant   } else if (Tok == "PROVIDE") {
1580a35e39caSPetr Hosek     Cmd = readProvideHidden(true, false);
1581a35e39caSPetr Hosek   } else if (Tok == "HIDDEN") {
1582a35e39caSPetr Hosek     Cmd = readProvideHidden(false, true);
1583ceabe80eSEugene Leviant   } else if (Tok == "PROVIDE_HIDDEN") {
1584a35e39caSPetr Hosek     Cmd = readProvideHidden(true, true);
1585ceabe80eSEugene Leviant   }
1586ceabe80eSEugene Leviant   return Cmd;
1587ceabe80eSEugene Leviant }
1588ceabe80eSEugene Leviant 
1589f6aeed36SEugene Leviant static uint64_t getSymbolValue(const Twine &Loc, StringRef S, uint64_t Dot) {
159030835ea4SGeorge Rimar   if (S == ".")
159130835ea4SGeorge Rimar     return Dot;
1592f6aeed36SEugene Leviant   return ScriptBase->getSymbolValue(Loc, S);
1593e32a3598SGeorge Rimar }
1594e32a3598SGeorge Rimar 
15952f831dcaSRafael Espindola static bool isAbsolute(StringRef S) {
15962f831dcaSRafael Espindola   if (S == ".")
15972f831dcaSRafael Espindola     return false;
15982f831dcaSRafael Espindola   return ScriptBase->isAbsolute(S);
15992f831dcaSRafael Espindola }
16002f831dcaSRafael Espindola 
160130835ea4SGeorge Rimar SymbolAssignment *ScriptParser::readAssignment(StringRef Name) {
160230835ea4SGeorge Rimar   StringRef Op = next();
1603db741e72SEugene Leviant   Expr E;
160430835ea4SGeorge Rimar   assert(Op == "=" || Op == "+=");
160583043f23SRui Ueyama   if (consume("ABSOLUTE")) {
16067c1381a0SRui Ueyama     // The RHS may be something like "ABSOLUTE(.) & 0xff".
16077c1381a0SRui Ueyama     // Call readExpr1 to read the whole expression.
16087c1381a0SRui Ueyama     E = readExpr1(readParenExpr(), 0);
1609009d1742SRui Ueyama     E.IsAbsolute = [] { return true; };
1610db741e72SEugene Leviant   } else {
1611db741e72SEugene Leviant     E = readExpr();
1612db741e72SEugene Leviant   }
1613f6aeed36SEugene Leviant   if (Op == "+=") {
1614f6aeed36SEugene Leviant     std::string Loc = getCurrentLocation();
1615f6aeed36SEugene Leviant     E = [=](uint64_t Dot) {
1616f6aeed36SEugene Leviant       return getSymbolValue(Loc, Name, Dot) + E(Dot);
1617f6aeed36SEugene Leviant     };
1618f6aeed36SEugene Leviant   }
1619f661393aSRafael Espindola   return new SymbolAssignment(Name, E);
162030835ea4SGeorge Rimar }
162130835ea4SGeorge Rimar 
162230835ea4SGeorge Rimar // This is an operator-precedence parser to parse a linker
162330835ea4SGeorge Rimar // script expression.
162430835ea4SGeorge Rimar Expr ScriptParser::readExpr() { return readExpr1(readPrimary(), 0); }
162530835ea4SGeorge Rimar 
162636c1cd23SRui Ueyama static Expr combine(StringRef Op, Expr L, Expr R) {
1627cc4d3e57SGeorge Rimar   auto IsAbs = [=] { return L.IsAbsolute() && R.IsAbsolute(); };
1628cc4d3e57SGeorge Rimar   auto GetOutSec = [=] {
1629cc4d3e57SGeorge Rimar     const OutputSectionBase *S = L.Section();
1630cc4d3e57SGeorge Rimar     return S ? S : R.Section();
1631cc4d3e57SGeorge Rimar   };
1632cc4d3e57SGeorge Rimar 
163336c1cd23SRui Ueyama   if (Op == "*")
163436c1cd23SRui Ueyama     return [=](uint64_t Dot) { return L(Dot) * R(Dot); };
163536c1cd23SRui Ueyama   if (Op == "/") {
163636c1cd23SRui Ueyama     return [=](uint64_t Dot) -> uint64_t {
163736c1cd23SRui Ueyama       uint64_t RHS = R(Dot);
163836c1cd23SRui Ueyama       if (RHS == 0) {
163936c1cd23SRui Ueyama         error("division by zero");
164036c1cd23SRui Ueyama         return 0;
164136c1cd23SRui Ueyama       }
164236c1cd23SRui Ueyama       return L(Dot) / RHS;
164336c1cd23SRui Ueyama     };
164436c1cd23SRui Ueyama   }
164536c1cd23SRui Ueyama   if (Op == "+")
1646cc4d3e57SGeorge Rimar     return {[=](uint64_t Dot) { return L(Dot) + R(Dot); }, IsAbs, GetOutSec};
164736c1cd23SRui Ueyama   if (Op == "-")
1648cc4d3e57SGeorge Rimar     return {[=](uint64_t Dot) { return L(Dot) - R(Dot); }, IsAbs, GetOutSec};
1649c8ccd1f1SGeorge Rimar   if (Op == "<<")
1650c8ccd1f1SGeorge Rimar     return [=](uint64_t Dot) { return L(Dot) << R(Dot); };
1651c8ccd1f1SGeorge Rimar   if (Op == ">>")
1652c8ccd1f1SGeorge Rimar     return [=](uint64_t Dot) { return L(Dot) >> R(Dot); };
165336c1cd23SRui Ueyama   if (Op == "<")
165436c1cd23SRui Ueyama     return [=](uint64_t Dot) { return L(Dot) < R(Dot); };
165536c1cd23SRui Ueyama   if (Op == ">")
165636c1cd23SRui Ueyama     return [=](uint64_t Dot) { return L(Dot) > R(Dot); };
165736c1cd23SRui Ueyama   if (Op == ">=")
165836c1cd23SRui Ueyama     return [=](uint64_t Dot) { return L(Dot) >= R(Dot); };
165936c1cd23SRui Ueyama   if (Op == "<=")
166036c1cd23SRui Ueyama     return [=](uint64_t Dot) { return L(Dot) <= R(Dot); };
166136c1cd23SRui Ueyama   if (Op == "==")
166236c1cd23SRui Ueyama     return [=](uint64_t Dot) { return L(Dot) == R(Dot); };
166336c1cd23SRui Ueyama   if (Op == "!=")
166436c1cd23SRui Ueyama     return [=](uint64_t Dot) { return L(Dot) != R(Dot); };
166536c1cd23SRui Ueyama   if (Op == "&")
166636c1cd23SRui Ueyama     return [=](uint64_t Dot) { return L(Dot) & R(Dot); };
1667cc3dd629SRafael Espindola   if (Op == "|")
1668cc3dd629SRafael Espindola     return [=](uint64_t Dot) { return L(Dot) | R(Dot); };
166936c1cd23SRui Ueyama   llvm_unreachable("invalid operator");
167036c1cd23SRui Ueyama }
167136c1cd23SRui Ueyama 
1672708019c4SRui Ueyama // This is a part of the operator-precedence parser. This function
1673708019c4SRui Ueyama // assumes that the remaining token stream starts with an operator.
1674708019c4SRui Ueyama Expr ScriptParser::readExpr1(Expr Lhs, int MinPrec) {
1675708019c4SRui Ueyama   while (!atEOF() && !Error) {
1676708019c4SRui Ueyama     // Read an operator and an expression.
167746247b85SRui Ueyama     if (consume("?"))
1678708019c4SRui Ueyama       return readTernary(Lhs);
167946247b85SRui Ueyama     StringRef Op1 = peek();
1680708019c4SRui Ueyama     if (precedence(Op1) < MinPrec)
1681a31c91b1SEugene Leviant       break;
16825424e7c7SJustin Bogner     skip();
1683708019c4SRui Ueyama     Expr Rhs = readPrimary();
1684708019c4SRui Ueyama 
1685708019c4SRui Ueyama     // Evaluate the remaining part of the expression first if the
1686708019c4SRui Ueyama     // next operator has greater precedence than the previous one.
1687708019c4SRui Ueyama     // For example, if we have read "+" and "3", and if the next
1688708019c4SRui Ueyama     // operator is "*", then we'll evaluate 3 * ... part first.
1689708019c4SRui Ueyama     while (!atEOF()) {
1690708019c4SRui Ueyama       StringRef Op2 = peek();
1691708019c4SRui Ueyama       if (precedence(Op2) <= precedence(Op1))
1692eda81a1bSEugene Leviant         break;
1693708019c4SRui Ueyama       Rhs = readExpr1(Rhs, precedence(Op2));
1694eda81a1bSEugene Leviant     }
1695708019c4SRui Ueyama 
1696708019c4SRui Ueyama     Lhs = combine(Op1, Lhs, Rhs);
1697708019c4SRui Ueyama   }
1698708019c4SRui Ueyama   return Lhs;
1699708019c4SRui Ueyama }
1700708019c4SRui Ueyama 
1701708019c4SRui Ueyama uint64_t static getConstant(StringRef S) {
1702e2cc07bcSMichael J. Spencer   if (S == "COMMONPAGESIZE")
1703708019c4SRui Ueyama     return Target->PageSize;
1704e2cc07bcSMichael J. Spencer   if (S == "MAXPAGESIZE")
1705997f8838SPetr Hosek     return Config->MaxPageSize;
1706708019c4SRui Ueyama   error("unknown constant: " + S);
1707708019c4SRui Ueyama   return 0;
1708708019c4SRui Ueyama }
1709708019c4SRui Ueyama 
1710626e0b08SRui Ueyama // Parses Tok as an integer. Returns true if successful.
1711626e0b08SRui Ueyama // It recognizes hexadecimal (prefixed with "0x" or suffixed with "H")
1712626e0b08SRui Ueyama // and decimal numbers. Decimal numbers may have "K" (kilo) or
1713626e0b08SRui Ueyama // "M" (mega) prefixes.
17149f2f7ad9SGeorge Rimar static bool readInteger(StringRef Tok, uint64_t &Result) {
171546247b85SRui Ueyama   // Negative number
1716eaeafb2bSSimon Atanasyan   if (Tok.startswith("-")) {
1717eaeafb2bSSimon Atanasyan     if (!readInteger(Tok.substr(1), Result))
1718eaeafb2bSSimon Atanasyan       return false;
1719eaeafb2bSSimon Atanasyan     Result = -Result;
1720eaeafb2bSSimon Atanasyan     return true;
1721eaeafb2bSSimon Atanasyan   }
172246247b85SRui Ueyama 
172346247b85SRui Ueyama   // Hexadecimal
17249f2f7ad9SGeorge Rimar   if (Tok.startswith_lower("0x"))
17259f2f7ad9SGeorge Rimar     return !Tok.substr(2).getAsInteger(16, Result);
17269f2f7ad9SGeorge Rimar   if (Tok.endswith_lower("H"))
17279f2f7ad9SGeorge Rimar     return !Tok.drop_back().getAsInteger(16, Result);
17289f2f7ad9SGeorge Rimar 
172946247b85SRui Ueyama   // Decimal
17309f2f7ad9SGeorge Rimar   int Suffix = 1;
17319f2f7ad9SGeorge Rimar   if (Tok.endswith_lower("K")) {
17329f2f7ad9SGeorge Rimar     Suffix = 1024;
17339f2f7ad9SGeorge Rimar     Tok = Tok.drop_back();
17349f2f7ad9SGeorge Rimar   } else if (Tok.endswith_lower("M")) {
17359f2f7ad9SGeorge Rimar     Suffix = 1024 * 1024;
17369f2f7ad9SGeorge Rimar     Tok = Tok.drop_back();
17379f2f7ad9SGeorge Rimar   }
17389f2f7ad9SGeorge Rimar   if (Tok.getAsInteger(10, Result))
17399f2f7ad9SGeorge Rimar     return false;
17409f2f7ad9SGeorge Rimar   Result *= Suffix;
17419f2f7ad9SGeorge Rimar   return true;
17429f2f7ad9SGeorge Rimar }
17439f2f7ad9SGeorge Rimar 
1744e38cbab5SGeorge Rimar BytesDataCommand *ScriptParser::readBytesDataCommand(StringRef Tok) {
1745e38cbab5SGeorge Rimar   int Size = StringSwitch<unsigned>(Tok)
1746e38cbab5SGeorge Rimar                  .Case("BYTE", 1)
1747e38cbab5SGeorge Rimar                  .Case("SHORT", 2)
1748e38cbab5SGeorge Rimar                  .Case("LONG", 4)
1749e38cbab5SGeorge Rimar                  .Case("QUAD", 8)
1750e38cbab5SGeorge Rimar                  .Default(-1);
1751e38cbab5SGeorge Rimar   if (Size == -1)
1752e38cbab5SGeorge Rimar     return nullptr;
1753e38cbab5SGeorge Rimar 
175495c7d8d2SMeador Inge   return new BytesDataCommand(readParenExpr(), Size);
1755e38cbab5SGeorge Rimar }
1756e38cbab5SGeorge Rimar 
1757b71d6f7aSEugene Leviant StringRef ScriptParser::readParenLiteral() {
1758b71d6f7aSEugene Leviant   expect("(");
1759b71d6f7aSEugene Leviant   StringRef Tok = next();
1760b71d6f7aSEugene Leviant   expect(")");
1761b71d6f7aSEugene Leviant   return Tok;
1762b71d6f7aSEugene Leviant }
1763b71d6f7aSEugene Leviant 
1764708019c4SRui Ueyama Expr ScriptParser::readPrimary() {
17656ad7dfccSRui Ueyama   if (peek() == "(")
17666ad7dfccSRui Ueyama     return readParenExpr();
1767708019c4SRui Ueyama 
17686ad7dfccSRui Ueyama   StringRef Tok = next();
1769b5f1c3ecSRui Ueyama   std::string Location = getCurrentLocation();
1770708019c4SRui Ueyama 
1771eaeafb2bSSimon Atanasyan   if (Tok == "~") {
1772eaeafb2bSSimon Atanasyan     Expr E = readPrimary();
1773eaeafb2bSSimon Atanasyan     return [=](uint64_t Dot) { return ~E(Dot); };
1774eaeafb2bSSimon Atanasyan   }
1775eaeafb2bSSimon Atanasyan   if (Tok == "-") {
1776eaeafb2bSSimon Atanasyan     Expr E = readPrimary();
1777eaeafb2bSSimon Atanasyan     return [=](uint64_t Dot) { return -E(Dot); };
1778eaeafb2bSSimon Atanasyan   }
1779eaeafb2bSSimon Atanasyan 
1780708019c4SRui Ueyama   // Built-in functions are parsed here.
1781708019c4SRui Ueyama   // https://sourceware.org/binutils/docs/ld/Builtin-Functions.html.
178296659df0SGeorge Rimar   if (Tok == "ADDR") {
1783b71d6f7aSEugene Leviant     StringRef Name = readParenLiteral();
1784ed30ce7aSEugene Leviant     return {[=](uint64_t Dot) {
1785ed30ce7aSEugene Leviant               return ScriptBase->getOutputSection(Location, Name)->Addr;
1786ed30ce7aSEugene Leviant             },
1787009d1742SRui Ueyama             [=] { return false; },
1788ed30ce7aSEugene Leviant             [=] { return ScriptBase->getOutputSection(Location, Name); }};
178996659df0SGeorge Rimar   }
1790b71d6f7aSEugene Leviant   if (Tok == "LOADADDR") {
1791b71d6f7aSEugene Leviant     StringRef Name = readParenLiteral();
1792afaa9343SEugene Leviant     return [=](uint64_t Dot) {
1793ed30ce7aSEugene Leviant       return ScriptBase->getOutputSection(Location, Name)->getLMA();
1794afaa9343SEugene Leviant     };
1795b71d6f7aSEugene Leviant   }
1796eefa758eSGeorge Rimar   if (Tok == "ASSERT")
1797eefa758eSGeorge Rimar     return readAssert();
1798708019c4SRui Ueyama   if (Tok == "ALIGN") {
17995d804dc8SRui Ueyama     expect("(");
18005d804dc8SRui Ueyama     Expr E = readExpr();
18015d804dc8SRui Ueyama     if (consume(",")) {
18025d804dc8SRui Ueyama       Expr E2 = readExpr();
18035d804dc8SRui Ueyama       expect(")");
18045d804dc8SRui Ueyama       return [=](uint64_t Dot) { return alignTo(E(Dot), E2(Dot)); };
18055d804dc8SRui Ueyama     }
18065d804dc8SRui Ueyama     expect(")");
1807708019c4SRui Ueyama     return [=](uint64_t Dot) { return alignTo(Dot, E(Dot)); };
1808708019c4SRui Ueyama   }
1809708019c4SRui Ueyama   if (Tok == "CONSTANT") {
1810b71d6f7aSEugene Leviant     StringRef Name = readParenLiteral();
1811b0de56b5SRafael Espindola     return [=](uint64_t Dot) { return getConstant(Name); };
1812708019c4SRui Ueyama   }
1813f34f45fdSGeorge Rimar   if (Tok == "DEFINED") {
18140ee25a69SRui Ueyama     StringRef Name = readParenLiteral();
18150ee25a69SRui Ueyama     return [=](uint64_t Dot) { return ScriptBase->isDefined(Name) ? 1 : 0; };
1816f34f45fdSGeorge Rimar   }
181754c145ceSRafael Espindola   if (Tok == "SEGMENT_START") {
181854c145ceSRafael Espindola     expect("(");
18195424e7c7SJustin Bogner     skip();
182054c145ceSRafael Espindola     expect(",");
18218c658bf8SGeorge Rimar     Expr E = readExpr();
182254c145ceSRafael Espindola     expect(")");
18238c658bf8SGeorge Rimar     return [=](uint64_t Dot) { return E(Dot); };
182454c145ceSRafael Espindola   }
1825708019c4SRui Ueyama   if (Tok == "DATA_SEGMENT_ALIGN") {
1826708019c4SRui Ueyama     expect("(");
1827708019c4SRui Ueyama     Expr E = readExpr();
1828708019c4SRui Ueyama     expect(",");
1829708019c4SRui Ueyama     readExpr();
1830708019c4SRui Ueyama     expect(")");
1831f7791bb9SRui Ueyama     return [=](uint64_t Dot) { return alignTo(Dot, E(Dot)); };
1832708019c4SRui Ueyama   }
1833708019c4SRui Ueyama   if (Tok == "DATA_SEGMENT_END") {
1834708019c4SRui Ueyama     expect("(");
1835708019c4SRui Ueyama     expect(".");
1836708019c4SRui Ueyama     expect(")");
1837708019c4SRui Ueyama     return [](uint64_t Dot) { return Dot; };
1838708019c4SRui Ueyama   }
1839276b4e64SGeorge Rimar   // GNU linkers implements more complicated logic to handle
1840276b4e64SGeorge Rimar   // DATA_SEGMENT_RELRO_END. We instead ignore the arguments and just align to
1841276b4e64SGeorge Rimar   // the next page boundary for simplicity.
1842276b4e64SGeorge Rimar   if (Tok == "DATA_SEGMENT_RELRO_END") {
1843276b4e64SGeorge Rimar     expect("(");
184497bdc722SRafael Espindola     readExpr();
1845276b4e64SGeorge Rimar     expect(",");
1846276b4e64SGeorge Rimar     readExpr();
1847276b4e64SGeorge Rimar     expect(")");
1848276b4e64SGeorge Rimar     return [](uint64_t Dot) { return alignTo(Dot, Target->PageSize); };
1849276b4e64SGeorge Rimar   }
18509e69450eSGeorge Rimar   if (Tok == "SIZEOF") {
1851b71d6f7aSEugene Leviant     StringRef Name = readParenLiteral();
1852edf75e79SRui Ueyama     return [=](uint64_t Dot) { return ScriptBase->getOutputSectionSize(Name); };
18539e69450eSGeorge Rimar   }
185436fac7f0SEugene Leviant   if (Tok == "ALIGNOF") {
1855b71d6f7aSEugene Leviant     StringRef Name = readParenLiteral();
1856afaa9343SEugene Leviant     return [=](uint64_t Dot) {
1857ed30ce7aSEugene Leviant       return ScriptBase->getOutputSection(Location, Name)->Addralign;
1858afaa9343SEugene Leviant     };
185936fac7f0SEugene Leviant   }
1860e32a3598SGeorge Rimar   if (Tok == "SIZEOF_HEADERS")
1861b0de56b5SRafael Espindola     return [=](uint64_t Dot) { return ScriptBase->getHeaderSize(); };
1862708019c4SRui Ueyama 
18639f2f7ad9SGeorge Rimar   // Tok is a literal number.
18649f2f7ad9SGeorge Rimar   uint64_t V;
18659f2f7ad9SGeorge Rimar   if (readInteger(Tok, V))
1866b0de56b5SRafael Espindola     return [=](uint64_t Dot) { return V; };
18679f2f7ad9SGeorge Rimar 
18689f2f7ad9SGeorge Rimar   // Tok is a symbol name.
186930835ea4SGeorge Rimar   if (Tok != "." && !isValidCIdentifier(Tok))
1870708019c4SRui Ueyama     setError("malformed number: " + Tok);
1871f6aeed36SEugene Leviant   return {[=](uint64_t Dot) { return getSymbolValue(Location, Tok, Dot); },
1872009d1742SRui Ueyama           [=] { return isAbsolute(Tok); },
1873009d1742SRui Ueyama           [=] { return ScriptBase->getSymbolSection(Tok); }};
1874a9c5a528SGeorge Rimar }
1875708019c4SRui Ueyama 
1876708019c4SRui Ueyama Expr ScriptParser::readTernary(Expr Cond) {
1877708019c4SRui Ueyama   Expr L = readExpr();
1878708019c4SRui Ueyama   expect(":");
1879708019c4SRui Ueyama   Expr R = readExpr();
1880708019c4SRui Ueyama   return [=](uint64_t Dot) { return Cond(Dot) ? L(Dot) : R(Dot); };
1881708019c4SRui Ueyama }
1882708019c4SRui Ueyama 
18836ad7dfccSRui Ueyama Expr ScriptParser::readParenExpr() {
18846ad7dfccSRui Ueyama   expect("(");
18856ad7dfccSRui Ueyama   Expr E = readExpr();
18866ad7dfccSRui Ueyama   expect(")");
18876ad7dfccSRui Ueyama   return E;
18886ad7dfccSRui Ueyama }
18896ad7dfccSRui Ueyama 
1890bbe38602SEugene Leviant std::vector<StringRef> ScriptParser::readOutputSectionPhdrs() {
1891bbe38602SEugene Leviant   std::vector<StringRef> Phdrs;
1892bbe38602SEugene Leviant   while (!Error && peek().startswith(":")) {
1893bbe38602SEugene Leviant     StringRef Tok = next();
1894da841c16SGeorge Rimar     Phdrs.push_back((Tok.size() == 1) ? next() : Tok.substr(1));
1895bbe38602SEugene Leviant   }
1896bbe38602SEugene Leviant   return Phdrs;
1897bbe38602SEugene Leviant }
1898bbe38602SEugene Leviant 
189995dd718cSGeorge Rimar // Read a program header type name. The next token must be a
190095dd718cSGeorge Rimar // name of a program header type or a constant (e.g. "0x3").
1901bbe38602SEugene Leviant unsigned ScriptParser::readPhdrType() {
1902bbe38602SEugene Leviant   StringRef Tok = next();
190395dd718cSGeorge Rimar   uint64_t Val;
190495dd718cSGeorge Rimar   if (readInteger(Tok, Val))
190595dd718cSGeorge Rimar     return Val;
190695dd718cSGeorge Rimar 
1907b0f6c590SRui Ueyama   unsigned Ret = StringSwitch<unsigned>(Tok)
1908b0f6c590SRui Ueyama                      .Case("PT_NULL", PT_NULL)
1909b0f6c590SRui Ueyama                      .Case("PT_LOAD", PT_LOAD)
1910b0f6c590SRui Ueyama                      .Case("PT_DYNAMIC", PT_DYNAMIC)
1911b0f6c590SRui Ueyama                      .Case("PT_INTERP", PT_INTERP)
1912b0f6c590SRui Ueyama                      .Case("PT_NOTE", PT_NOTE)
1913b0f6c590SRui Ueyama                      .Case("PT_SHLIB", PT_SHLIB)
1914b0f6c590SRui Ueyama                      .Case("PT_PHDR", PT_PHDR)
1915b0f6c590SRui Ueyama                      .Case("PT_TLS", PT_TLS)
1916b0f6c590SRui Ueyama                      .Case("PT_GNU_EH_FRAME", PT_GNU_EH_FRAME)
1917b0f6c590SRui Ueyama                      .Case("PT_GNU_STACK", PT_GNU_STACK)
1918b0f6c590SRui Ueyama                      .Case("PT_GNU_RELRO", PT_GNU_RELRO)
1919270173f2SGeorge Rimar                      .Case("PT_OPENBSD_RANDOMIZE", PT_OPENBSD_RANDOMIZE)
1920cc6e567cSGeorge Rimar                      .Case("PT_OPENBSD_WXNEEDED", PT_OPENBSD_WXNEEDED)
1921a2a32c2cSGeorge Rimar                      .Case("PT_OPENBSD_BOOTDATA", PT_OPENBSD_BOOTDATA)
1922b0f6c590SRui Ueyama                      .Default(-1);
1923bbe38602SEugene Leviant 
1924b0f6c590SRui Ueyama   if (Ret == (unsigned)-1) {
1925b0f6c590SRui Ueyama     setError("invalid program header type: " + Tok);
1926b0f6c590SRui Ueyama     return PT_NULL;
1927b0f6c590SRui Ueyama   }
1928b0f6c590SRui Ueyama   return Ret;
1929bbe38602SEugene Leviant }
1930bbe38602SEugene Leviant 
193112450b20SRui Ueyama // Reads a list of symbols, e.g. "{ global: foo; bar; local: *; };".
193212450b20SRui Ueyama void ScriptParser::readAnonymousDeclaration() {
193312450b20SRui Ueyama   // Read global symbols first. "global:" is default, so if there's
193412450b20SRui Ueyama   // no label, we assume global symbols.
19354524268cSRafael Espindola   if (peek() != "local") {
19364524268cSRafael Espindola     if (consume("global"))
19374524268cSRafael Espindola       expect(":");
193812450b20SRui Ueyama     Config->VersionScriptGlobals = readSymbols();
19394524268cSRafael Espindola   }
1940e999ddb8SRafael Espindola   readLocals();
194112450b20SRui Ueyama   expect("}");
194212450b20SRui Ueyama   expect(";");
194312450b20SRui Ueyama }
194412450b20SRui Ueyama 
1945e999ddb8SRafael Espindola void ScriptParser::readLocals() {
19464524268cSRafael Espindola   if (!consume("local"))
1947e999ddb8SRafael Espindola     return;
19484524268cSRafael Espindola   expect(":");
1949e999ddb8SRafael Espindola   std::vector<SymbolVersion> Locals = readSymbols();
1950e999ddb8SRafael Espindola   for (SymbolVersion V : Locals) {
1951e999ddb8SRafael Espindola     if (V.Name == "*") {
1952e999ddb8SRafael Espindola       Config->DefaultSymbolVersion = VER_NDX_LOCAL;
1953e999ddb8SRafael Espindola       continue;
1954e999ddb8SRafael Espindola     }
1955e999ddb8SRafael Espindola     Config->VersionScriptLocals.push_back(V);
1956e999ddb8SRafael Espindola   }
1957e999ddb8SRafael Espindola }
1958e999ddb8SRafael Espindola 
195912450b20SRui Ueyama // Reads a list of symbols, e.g. "VerStr { global: foo; bar; local: *; };".
196095769b4aSRui Ueyama void ScriptParser::readVersionDeclaration(StringRef VerStr) {
196120b6598cSGeorge Rimar   // Identifiers start at 2 because 0 and 1 are reserved
196220b6598cSGeorge Rimar   // for VER_NDX_LOCAL and VER_NDX_GLOBAL constants.
1963da805c48SRui Ueyama   uint16_t VersionId = Config->VersionDefinitions.size() + 2;
196420b6598cSGeorge Rimar   Config->VersionDefinitions.push_back({VerStr, VersionId});
196520b6598cSGeorge Rimar 
196612450b20SRui Ueyama   // Read global symbols.
19674524268cSRafael Espindola   if (peek() != "local") {
19684524268cSRafael Espindola     if (consume("global"))
19694524268cSRafael Espindola       expect(":");
197012450b20SRui Ueyama     Config->VersionDefinitions.back().Globals = readSymbols();
19714524268cSRafael Espindola   }
1972e999ddb8SRafael Espindola   readLocals();
197320b6598cSGeorge Rimar   expect("}");
197420b6598cSGeorge Rimar 
197512450b20SRui Ueyama   // Each version may have a parent version. For example, "Ver2"
197612450b20SRui Ueyama   // defined as "Ver2 { global: foo; local: *; } Ver1;" has "Ver1"
197712450b20SRui Ueyama   // as a parent. This version hierarchy is, probably against your
197812450b20SRui Ueyama   // instinct, purely for hint; the runtime doesn't care about it
197912450b20SRui Ueyama   // at all. In LLD, we simply ignore it.
198012450b20SRui Ueyama   if (peek() != ";")
19815424e7c7SJustin Bogner     skip();
198220b6598cSGeorge Rimar   expect(";");
198320b6598cSGeorge Rimar }
198420b6598cSGeorge Rimar 
198512450b20SRui Ueyama // Reads a list of symbols for a versions cript.
198612450b20SRui Ueyama std::vector<SymbolVersion> ScriptParser::readSymbols() {
198712450b20SRui Ueyama   std::vector<SymbolVersion> Ret;
1988e0fc2421SGeorge Rimar   for (;;) {
19891ef90d2fSRafael Espindola     if (consume("extern")) {
199012450b20SRui Ueyama       for (SymbolVersion V : readVersionExtern())
199112450b20SRui Ueyama         Ret.push_back(V);
19921ef90d2fSRafael Espindola       continue;
19931ef90d2fSRafael Espindola     }
1994e0fc2421SGeorge Rimar 
1995f3965c02SDmitry Mikulin     if (peek() == "}" || (peek() == "local" && peek(1) == ":") || Error)
199612450b20SRui Ueyama       break;
19970ee25a69SRui Ueyama     StringRef Tok = next();
199812450b20SRui Ueyama     Ret.push_back({unquote(Tok), false, hasWildcard(Tok)});
1999e0fc2421SGeorge Rimar     expect(";");
2000e0fc2421SGeorge Rimar   }
200112450b20SRui Ueyama   return Ret;
2002e0fc2421SGeorge Rimar }
2003e0fc2421SGeorge Rimar 
200412450b20SRui Ueyama // Reads an "extern C++" directive, e.g.,
200512450b20SRui Ueyama // "extern "C++" { ns::*; "f(int, double)"; };"
200612450b20SRui Ueyama std::vector<SymbolVersion> ScriptParser::readVersionExtern() {
20077e71415cSRafael Espindola   StringRef Tok = next();
20087e71415cSRafael Espindola   bool IsCXX = Tok == "\"C++\"";
20097e71415cSRafael Espindola   if (!IsCXX && Tok != "\"C\"")
2010d0ebd84cSRafael Espindola     setError("Unknown language");
201120b6598cSGeorge Rimar   expect("{");
201220b6598cSGeorge Rimar 
201312450b20SRui Ueyama   std::vector<SymbolVersion> Ret;
20140ee25a69SRui Ueyama   while (!Error && peek() != "}") {
20150ee25a69SRui Ueyama     StringRef Tok = next();
20160ee25a69SRui Ueyama     bool HasWildcard = !Tok.startswith("\"") && hasWildcard(Tok);
20177e71415cSRafael Espindola     Ret.push_back({unquote(Tok), IsCXX, HasWildcard});
201820b6598cSGeorge Rimar     expect(";");
201920b6598cSGeorge Rimar   }
202020b6598cSGeorge Rimar 
202120b6598cSGeorge Rimar   expect("}");
202220b6598cSGeorge Rimar   expect(";");
202312450b20SRui Ueyama   return Ret;
202420b6598cSGeorge Rimar }
202520b6598cSGeorge Rimar 
202624e626ccSRui Ueyama uint64_t ScriptParser::readMemoryAssignment(
202724e626ccSRui Ueyama     StringRef S1, StringRef S2, StringRef S3) {
202824e626ccSRui Ueyama   if (!(consume(S1) || consume(S2) || consume(S3))) {
202924e626ccSRui Ueyama     setError("expected one of: " + S1 + ", " + S2 + ", or " + S3);
203024e626ccSRui Ueyama     return 0;
203124e626ccSRui Ueyama   }
203224e626ccSRui Ueyama   expect("=");
203324e626ccSRui Ueyama 
203424e626ccSRui Ueyama   // TODO: Fully support constant expressions.
203524e626ccSRui Ueyama   uint64_t Val;
203624e626ccSRui Ueyama   if (!readInteger(next(), Val))
203724e626ccSRui Ueyama     setError("nonconstant expression for "+ S1);
203824e626ccSRui Ueyama   return Val;
203924e626ccSRui Ueyama }
204024e626ccSRui Ueyama 
204124e626ccSRui Ueyama // Parse the MEMORY command as specified in:
204224e626ccSRui Ueyama // https://sourceware.org/binutils/docs/ld/MEMORY.html
204324e626ccSRui Ueyama //
204424e626ccSRui Ueyama // MEMORY { name [(attr)] : ORIGIN = origin, LENGTH = len ... }
2045b889744eSMeador Inge void ScriptParser::readMemory() {
2046b889744eSMeador Inge   expect("{");
2047b889744eSMeador Inge   while (!Error && !consume("}")) {
2048b889744eSMeador Inge     StringRef Name = next();
204924e626ccSRui Ueyama 
2050b889744eSMeador Inge     uint32_t Flags = 0;
20518a8a953eSRui Ueyama     uint32_t NegFlags = 0;
2052b889744eSMeador Inge     if (consume("(")) {
20538a8a953eSRui Ueyama       std::tie(Flags, NegFlags) = readMemoryAttributes();
2054b889744eSMeador Inge       expect(")");
2055b889744eSMeador Inge     }
2056b889744eSMeador Inge     expect(":");
2057b889744eSMeador Inge 
205824e626ccSRui Ueyama     uint64_t Origin = readMemoryAssignment("ORIGIN", "org", "o");
2059b889744eSMeador Inge     expect(",");
206024e626ccSRui Ueyama     uint64_t Length = readMemoryAssignment("LENGTH", "len", "l");
2061b889744eSMeador Inge 
2062b889744eSMeador Inge     // Add the memory region to the region map (if it doesn't already exist).
2063b889744eSMeador Inge     auto It = Opt.MemoryRegions.find(Name);
2064b889744eSMeador Inge     if (It != Opt.MemoryRegions.end())
2065b889744eSMeador Inge       setError("region '" + Name + "' already defined");
2066b889744eSMeador Inge     else
20678a8a953eSRui Ueyama       Opt.MemoryRegions[Name] = {Name, Origin, Length, Origin, Flags, NegFlags};
2068b889744eSMeador Inge   }
2069b889744eSMeador Inge }
2070b889744eSMeador Inge 
2071b889744eSMeador Inge // This function parses the attributes used to match against section
2072b889744eSMeador Inge // flags when placing output sections in a memory region. These flags
2073b889744eSMeador Inge // are only used when an explicit memory region name is not used.
2074b889744eSMeador Inge std::pair<uint32_t, uint32_t> ScriptParser::readMemoryAttributes() {
2075b889744eSMeador Inge   uint32_t Flags = 0;
20768a8a953eSRui Ueyama   uint32_t NegFlags = 0;
2077b889744eSMeador Inge   bool Invert = false;
2078481ac996SRui Ueyama 
2079481ac996SRui Ueyama   for (char C : next().lower()) {
2080b889744eSMeador Inge     uint32_t Flag = 0;
2081b889744eSMeador Inge     if (C == '!')
2082b889744eSMeador Inge       Invert = !Invert;
2083481ac996SRui Ueyama     else if (C == 'w')
2084b889744eSMeador Inge       Flag = SHF_WRITE;
2085481ac996SRui Ueyama     else if (C == 'x')
2086b889744eSMeador Inge       Flag = SHF_EXECINSTR;
2087481ac996SRui Ueyama     else if (C == 'a')
2088b889744eSMeador Inge       Flag = SHF_ALLOC;
2089481ac996SRui Ueyama     else if (C != 'r')
2090b889744eSMeador Inge       setError("invalid memory region attribute");
2091481ac996SRui Ueyama 
2092b889744eSMeador Inge     if (Invert)
20938a8a953eSRui Ueyama       NegFlags |= Flag;
2094b889744eSMeador Inge     else
2095b889744eSMeador Inge       Flags |= Flag;
2096b889744eSMeador Inge   }
20978a8a953eSRui Ueyama   return {Flags, NegFlags};
2098b889744eSMeador Inge }
2099b889744eSMeador Inge 
210007320e40SRui Ueyama void elf::readLinkerScript(MemoryBufferRef MB) {
210122375f24SRui Ueyama   ScriptParser(MB).readLinkerScript();
210220b6598cSGeorge Rimar }
210320b6598cSGeorge Rimar 
210420b6598cSGeorge Rimar void elf::readVersionScript(MemoryBufferRef MB) {
210522375f24SRui Ueyama   ScriptParser(MB).readVersionScript();
2106f7c5fbb1SRui Ueyama }
21071ebc8ed7SRui Ueyama 
2108d0ebd84cSRafael Espindola void elf::readDynamicList(MemoryBufferRef MB) {
2109d0ebd84cSRafael Espindola   ScriptParser(MB).readDynamicList();
2110d0ebd84cSRafael Espindola }
2111d0ebd84cSRafael Espindola 
211207320e40SRui Ueyama template class elf::LinkerScript<ELF32LE>;
211307320e40SRui Ueyama template class elf::LinkerScript<ELF32BE>;
211407320e40SRui Ueyama template class elf::LinkerScript<ELF64LE>;
211507320e40SRui Ueyama template class elf::LinkerScript<ELF64BE>;
2116