blob: 8881154ec8d8bb40b3c45b3c1d4b69862f7855d2 [file] [log] [blame]
Rui Ueyamaf7c5fbb2015-09-30 17:23:26 +00001//===- LinkerScript.cpp ---------------------------------------------------===//
2//
3// The LLVM Linker
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This file contains the parser/evaluator of the linker script.
Rui Ueyama629e0aa52016-07-21 19:45:22 +000011// It parses a linker script and write the result to Config or ScriptConfig
12// objects.
13//
14// If SECTIONS command is used, a ScriptConfig contains an AST
15// of the command which will later be consumed by createSections() and
16// assignAddresses().
Rui Ueyamaf7c5fbb2015-09-30 17:23:26 +000017//
18//===----------------------------------------------------------------------===//
19
Rui Ueyama717677a2016-02-11 21:17:59 +000020#include "LinkerScript.h"
Rui Ueyamaf7c5fbb2015-09-30 17:23:26 +000021#include "Config.h"
22#include "Driver.h"
Rui Ueyama1ebc8ed2016-02-12 21:47:28 +000023#include "InputSection.h"
George Rimar652852c2016-04-16 10:10:32 +000024#include "OutputSections.h"
Adhemerval Zanellae77b5bf2016-04-06 20:59:11 +000025#include "ScriptParser.h"
Rui Ueyama93c9af42016-06-29 08:01:32 +000026#include "Strings.h"
Eugene Levianteda81a12016-07-12 06:39:48 +000027#include "Symbols.h"
Rui Ueyamaf7c5fbb2015-09-30 17:23:26 +000028#include "SymbolTable.h"
Eugene Leviant467c4d52016-07-01 10:27:36 +000029#include "Target.h"
Eugene Leviantbbe38602016-07-19 09:25:43 +000030#include "Writer.h"
Rui Ueyama960504b2016-04-19 18:58:11 +000031#include "llvm/ADT/StringSwitch.h"
George Rimar652852c2016-04-16 10:10:32 +000032#include "llvm/Support/ELF.h"
Rui Ueyamaf7c5fbb2015-09-30 17:23:26 +000033#include "llvm/Support/FileSystem.h"
34#include "llvm/Support/MemoryBuffer.h"
Rui Ueyamaf03f3cc2015-10-13 00:09:21 +000035#include "llvm/Support/Path.h"
Rui Ueyamaa47ee682015-10-11 01:53:04 +000036#include "llvm/Support/StringSaver.h"
Rui Ueyamaf7c5fbb2015-09-30 17:23:26 +000037
38using namespace llvm;
George Rimar652852c2016-04-16 10:10:32 +000039using namespace llvm::ELF;
Rui Ueyama1ebc8ed2016-02-12 21:47:28 +000040using namespace llvm::object;
Rui Ueyamaf7c5fbb2015-09-30 17:23:26 +000041using namespace lld;
Rafael Espindolae0df00b2016-02-28 00:25:54 +000042using namespace lld::elf;
Rui Ueyamaf7c5fbb2015-09-30 17:23:26 +000043
George Rimar884e7862016-09-08 08:19:13 +000044LinkerScriptBase *elf::ScriptBase;
Rui Ueyama07320e42016-04-20 20:13:41 +000045ScriptConfiguration *elf::ScriptConfig;
Rui Ueyama717677a2016-02-11 21:17:59 +000046
George Rimar6c55f0e2016-09-08 08:20:30 +000047template <class ELFT> static void addRegular(SymbolAssignment *Cmd) {
Rui Ueyama16024212016-08-11 23:22:52 +000048 Symbol *Sym = Symtab<ELFT>::X->addRegular(Cmd->Name, STB_GLOBAL, STV_DEFAULT);
49 Sym->Visibility = Cmd->Hidden ? STV_HIDDEN : STV_DEFAULT;
50 Cmd->Sym = Sym->body();
Eugene Leviant20d03192016-09-16 15:30:47 +000051
52 // If we have no SECTIONS then we don't have '.' and don't call
53 // assignAddresses(). We calculate symbol value immediately in this case.
54 if (!ScriptConfig->HasSections)
55 cast<DefinedRegular<ELFT>>(Cmd->Sym)->Value = Cmd->Expression(0);
Eugene Leviantceabe802016-08-11 07:56:43 +000056}
57
Rui Ueyama0c70d3c2016-08-12 03:31:09 +000058template <class ELFT> static void addSynthetic(SymbolAssignment *Cmd) {
George Rimare1937bb2016-08-19 15:36:32 +000059 Symbol *Sym = Symtab<ELFT>::X->addSynthetic(
60 Cmd->Name, nullptr, 0, Cmd->Hidden ? STV_HIDDEN : STV_DEFAULT);
Rui Ueyama16024212016-08-11 23:22:52 +000061 Cmd->Sym = Sym->body();
Eugene Leviantceabe802016-08-11 07:56:43 +000062}
63
Eugene Leviantdb741e72016-09-07 07:08:43 +000064template <class ELFT> static void addSymbol(SymbolAssignment *Cmd) {
65 if (Cmd->IsAbsolute)
66 addRegular<ELFT>(Cmd);
67 else
68 addSynthetic<ELFT>(Cmd);
69}
Rui Ueyama16024212016-08-11 23:22:52 +000070// If a symbol was in PROVIDE(), we need to define it only when
71// it is an undefined symbol.
72template <class ELFT> static bool shouldDefine(SymbolAssignment *Cmd) {
73 if (Cmd->Name == ".")
Eugene Leviantceabe802016-08-11 07:56:43 +000074 return false;
Rui Ueyama16024212016-08-11 23:22:52 +000075 if (!Cmd->Provide)
76 return true;
77 SymbolBody *B = Symtab<ELFT>::X->find(Cmd->Name);
78 return B && B->isUndefined();
Eugene Leviantceabe802016-08-11 07:56:43 +000079}
80
George Rimar076fe152016-07-21 06:43:01 +000081bool SymbolAssignment::classof(const BaseCommand *C) {
82 return C->Kind == AssignmentKind;
83}
84
85bool OutputSectionCommand::classof(const BaseCommand *C) {
86 return C->Kind == OutputSectionKind;
87}
88
George Rimareea31142016-07-21 14:26:59 +000089bool InputSectionDescription::classof(const BaseCommand *C) {
90 return C->Kind == InputSectionKind;
91}
92
George Rimareefa7582016-08-04 09:29:31 +000093bool AssertCommand::classof(const BaseCommand *C) {
94 return C->Kind == AssertKind;
95}
96
Rui Ueyama36a153c2016-07-23 14:09:58 +000097template <class ELFT> static bool isDiscarded(InputSectionBase<ELFT> *S) {
George Rimareea31142016-07-21 14:26:59 +000098 return !S || !S->Live;
Rui Ueyama717677a2016-02-11 21:17:59 +000099}
100
Rui Ueyamaf34d0e02016-08-12 01:24:53 +0000101template <class ELFT> LinkerScript<ELFT>::LinkerScript() {}
102template <class ELFT> LinkerScript<ELFT>::~LinkerScript() {}
103
Rui Ueyama07320e42016-04-20 20:13:41 +0000104template <class ELFT>
105bool LinkerScript<ELFT>::shouldKeep(InputSectionBase<ELFT> *S) {
George Rimarc91930a2016-09-02 21:17:20 +0000106 for (Regex *Re : Opt.KeptSections)
Rafael Espindola042a3f22016-09-08 14:06:08 +0000107 if (Re->match(S->Name))
George Rimareea31142016-07-21 14:26:59 +0000108 return true;
109 return false;
110}
111
George Rimar395281c2016-09-16 17:42:10 +0000112static bool fileMatches(const llvm::Regex &FileRe,
113 const llvm::Regex &ExcludedFileRe, StringRef Filename) {
114 return const_cast<Regex &>(FileRe).match(Filename) &&
115 !const_cast<Regex &>(ExcludedFileRe).match(Filename);
George Rimar06598002016-07-28 21:51:30 +0000116}
117
George Rimar575208c2016-09-15 19:15:12 +0000118static bool comparePriority(InputSectionData *A, InputSectionData *B) {
119 return getPriority(A->Name) < getPriority(B->Name);
120}
121
Rafael Espindolac0028d32016-09-08 20:47:52 +0000122static bool compareName(InputSectionData *A, InputSectionData *B) {
Rafael Espindola042a3f22016-09-08 14:06:08 +0000123 return A->Name < B->Name;
Rui Ueyama742c3832016-08-04 22:27:00 +0000124}
George Rimar350ece42016-08-03 08:35:59 +0000125
Rafael Espindolac0028d32016-09-08 20:47:52 +0000126static bool compareAlignment(InputSectionData *A, InputSectionData *B) {
Rui Ueyama742c3832016-08-04 22:27:00 +0000127 // ">" is not a mistake. Larger alignments are placed before smaller
128 // alignments in order to reduce the amount of padding necessary.
129 // This is compatible with GNU.
130 return A->Alignment > B->Alignment;
131}
George Rimar350ece42016-08-03 08:35:59 +0000132
Rafael Espindolac0028d32016-09-08 20:47:52 +0000133static std::function<bool(InputSectionData *, InputSectionData *)>
George Rimarbe394db2016-09-16 20:21:55 +0000134getComparator(SortSectionPolicy K) {
135 switch (K) {
136 case SortSectionPolicy::Alignment:
137 return compareAlignment;
138 case SortSectionPolicy::Name:
Rafael Espindolac0028d32016-09-08 20:47:52 +0000139 return compareName;
George Rimarbe394db2016-09-16 20:21:55 +0000140 case SortSectionPolicy::Priority:
141 return comparePriority;
142 default:
143 llvm_unreachable("unknown sort policy");
144 }
Rui Ueyama742c3832016-08-04 22:27:00 +0000145}
George Rimar0702c4e2016-07-29 15:32:46 +0000146
George Rimar8f66df92016-08-12 20:38:20 +0000147static bool checkConstraint(uint64_t Flags, ConstraintKind Kind) {
148 bool RO = (Kind == ConstraintKind::ReadOnly);
149 bool RW = (Kind == ConstraintKind::ReadWrite);
150 bool Writable = Flags & SHF_WRITE;
Rui Ueyamaadcdb662016-09-06 22:50:48 +0000151 return !(RO && Writable) && !(RW && !Writable);
George Rimar8f66df92016-08-12 20:38:20 +0000152}
153
Rui Ueyama48c3f1c2016-08-12 00:27:23 +0000154template <class ELFT>
Rafael Espindolad3190792016-09-16 15:10:23 +0000155static bool matchConstraints(ArrayRef<InputSectionData *> Sections,
George Rimar06ae6832016-08-12 09:07:57 +0000156 ConstraintKind Kind) {
George Rimar8f66df92016-08-12 20:38:20 +0000157 if (Kind == ConstraintKind::NoConstraint)
158 return true;
Rafael Espindolad3190792016-09-16 15:10:23 +0000159 return llvm::all_of(Sections, [=](InputSectionData *Sec2) {
160 auto *Sec = static_cast<InputSectionBase<ELFT> *>(Sec2);
George Rimar8f66df92016-08-12 20:38:20 +0000161 return checkConstraint(Sec->getSectionHdr()->sh_flags, Kind);
George Rimar06ae6832016-08-12 09:07:57 +0000162 });
163}
164
Rafael Espindolad3190792016-09-16 15:10:23 +0000165// Compute and remember which sections the InputSectionDescription matches.
Rafael Espindolabe94e1b2016-09-14 14:32:08 +0000166template <class ELFT>
Rafael Espindolad3190792016-09-16 15:10:23 +0000167void LinkerScript<ELFT>::computeInputSections(InputSectionDescription *I,
168 ConstraintKind Constraint) {
George Rimar395281c2016-09-16 17:42:10 +0000169 for (const std::pair<llvm::Regex, llvm::Regex> &V : I->SectionsVec) {
170 for (ObjectFile<ELFT> *F : Symtab<ELFT>::X->getObjectFiles()) {
171 if (fileMatches(I->FileRe, V.first, sys::path::filename(F->getName()))) {
172 Regex &Re = const_cast<Regex &>(V.second);
173 for (InputSectionBase<ELFT> *S : F->getSections())
174 if (!isDiscarded(S) && !S->OutSec && Re.match(S->Name))
175 I->Sections.push_back(S);
Rafael Espindolabe94e1b2016-09-14 14:32:08 +0000176
George Rimar395281c2016-09-16 17:42:10 +0000177 if (Re.match("COMMON"))
178 I->Sections.push_back(CommonInputSection<ELFT>::X);
179 }
180 }
181 }
Rafael Espindolad3190792016-09-16 15:10:23 +0000182
183 if (!matchConstraints<ELFT>(I->Sections, Constraint)) {
184 I->Sections.clear();
185 return;
186 }
187
George Rimarbe394db2016-09-16 20:21:55 +0000188 if (I->SortInner != SortSectionPolicy::None)
Rafael Espindolad3190792016-09-16 15:10:23 +0000189 std::stable_sort(I->Sections.begin(), I->Sections.end(),
190 getComparator(I->SortInner));
George Rimarbe394db2016-09-16 20:21:55 +0000191 if (I->SortOuter != SortSectionPolicy::None)
Rafael Espindolad3190792016-09-16 15:10:23 +0000192 std::stable_sort(I->Sections.begin(), I->Sections.end(),
193 getComparator(I->SortOuter));
194
195 // We do not add duplicate input sections, so mark them with a dummy output
196 // section for now.
197 for (InputSectionData *S : I->Sections) {
198 auto *S2 = static_cast<InputSectionBase<ELFT> *>(S);
199 S2->OutSec = (OutputSectionBase<ELFT> *)-1;
200 }
Rafael Espindolabe94e1b2016-09-14 14:32:08 +0000201}
202
203template <class ELFT>
204void LinkerScript<ELFT>::discard(ArrayRef<InputSectionBase<ELFT> *> V) {
205 for (InputSectionBase<ELFT> *S : V) {
206 S->Live = false;
207 reportDiscarded(S);
208 }
209}
210
George Rimar06ae6832016-08-12 09:07:57 +0000211template <class ELFT>
Rui Ueyama0b9ce6a2016-08-12 03:16:56 +0000212std::vector<InputSectionBase<ELFT> *>
George Rimar06ae6832016-08-12 09:07:57 +0000213LinkerScript<ELFT>::createInputSectionList(OutputSectionCommand &OutCmd) {
Rui Ueyama0b9ce6a2016-08-12 03:16:56 +0000214 std::vector<InputSectionBase<ELFT> *> Ret;
Rui Ueyamae7f912c2016-08-03 21:12:09 +0000215
George Rimar06ae6832016-08-12 09:07:57 +0000216 for (const std::unique_ptr<BaseCommand> &Base : OutCmd.Commands) {
217 if (auto *OutCmd = dyn_cast<SymbolAssignment>(Base.get())) {
218 if (shouldDefine<ELFT>(OutCmd))
Eugene Leviantdb741e72016-09-07 07:08:43 +0000219 addSymbol<ELFT>(OutCmd);
Rui Ueyama0b9ce6a2016-08-12 03:16:56 +0000220 continue;
221 }
222
223 auto *Cmd = cast<InputSectionDescription>(Base.get());
Rafael Espindolad3190792016-09-16 15:10:23 +0000224 computeInputSections(Cmd, OutCmd.Constraint);
225 for (InputSectionData *S : Cmd->Sections)
226 Ret.push_back(static_cast<InputSectionBase<ELFT> *>(S));
Rui Ueyama0b9ce6a2016-08-12 03:16:56 +0000227 }
228 return Ret;
229}
230
Petr Hoseke5d3ca52016-08-31 15:31:17 +0000231template <class ELFT>
Rafael Espindola10897f12016-09-13 14:23:14 +0000232static SectionKey<ELFT::Is64Bits> createKey(InputSectionBase<ELFT> *C,
233 StringRef OutsecName) {
234 // When using linker script the merge rules are different.
235 // Unfortunately, linker scripts are name based. This means that expressions
236 // like *(.foo*) can refer to multiple input sections that would normally be
237 // placed in different output sections. We cannot put them in different
238 // output sections or we would produce wrong results for
239 // start = .; *(.foo.*) end = .; *(.bar)
240 // and a mapping of .foo1 and .bar1 to one section and .foo2 and .bar2 to
241 // another. The problem is that there is no way to layout those output
242 // sections such that the .foo sections are the only thing between the
243 // start and end symbols.
244
245 // An extra annoyance is that we cannot simply disable merging of the contents
246 // of SHF_MERGE sections, but our implementation requires one output section
247 // per "kind" (string or not, which size/aligment).
248 // Fortunately, creating symbols in the middle of a merge section is not
249 // supported by bfd or gold, so we can just create multiple section in that
250 // case.
251 const typename ELFT::Shdr *H = C->getSectionHdr();
252 typedef typename ELFT::uint uintX_t;
253 uintX_t Flags = H->sh_flags & (SHF_MERGE | SHF_STRINGS);
254
255 uintX_t Alignment = 0;
256 if (isa<MergeInputSection<ELFT>>(C))
257 Alignment = std::max(H->sh_addralign, H->sh_entsize);
258
259 return SectionKey<ELFT::Is64Bits>{OutsecName, /*Type*/ 0, Flags, Alignment};
260}
261
262template <class ELFT>
Eugene Leviant20d03192016-09-16 15:30:47 +0000263void LinkerScript<ELFT>::addSection(OutputSectionFactory<ELFT> &Factory,
264 InputSectionBase<ELFT> *Sec,
265 StringRef Name) {
266 OutputSectionBase<ELFT> *OutSec;
267 bool IsNew;
268 std::tie(OutSec, IsNew) = Factory.create(createKey(Sec, Name), Sec);
269 if (IsNew)
270 OutputSections->push_back(OutSec);
271 OutSec->addSection(Sec);
272}
273
274template <class ELFT>
275void LinkerScript<ELFT>::processCommands(OutputSectionFactory<ELFT> &Factory) {
Rafael Espindola28c15972016-09-13 13:00:06 +0000276
Rui Ueyama48c3f1c2016-08-12 00:27:23 +0000277 for (const std::unique_ptr<BaseCommand> &Base1 : Opt.Commands) {
Rui Ueyama2ab5f732016-08-12 03:33:04 +0000278 if (auto *Cmd = dyn_cast<SymbolAssignment>(Base1.get())) {
279 if (shouldDefine<ELFT>(Cmd))
280 addRegular<ELFT>(Cmd);
281 continue;
282 }
Eugene Leviant20d03192016-09-16 15:30:47 +0000283 if (auto *Cmd = dyn_cast<AssertCommand>(Base1.get())) {
284 // If we don't have SECTIONS then output sections have already been
285 // created by Writer<EFLT>. The LinkerScript<ELFT>::assignAddresses
286 // will not be called, so ASSERT should be evaluated now.
287 if (!Opt.HasSections)
288 Cmd->Expression(0);
289 continue;
290 }
Rui Ueyama2ab5f732016-08-12 03:33:04 +0000291
Eugene Leviantceabe802016-08-11 07:56:43 +0000292 if (auto *Cmd = dyn_cast<OutputSectionCommand>(Base1.get())) {
Rafael Espindola7bd37872016-09-12 16:05:16 +0000293 std::vector<InputSectionBase<ELFT> *> V = createInputSectionList(*Cmd);
294
Rui Ueyama48c3f1c2016-08-12 00:27:23 +0000295 if (Cmd->Name == "/DISCARD/") {
Rafael Espindola7bd37872016-09-12 16:05:16 +0000296 discard(V);
Rui Ueyama48c3f1c2016-08-12 00:27:23 +0000297 continue;
298 }
Eugene Leviantceabe802016-08-11 07:56:43 +0000299
Eugene Leviant97403d12016-09-01 09:55:57 +0000300 if (V.empty())
Rui Ueyama0b9ce6a2016-08-12 03:16:56 +0000301 continue;
302
George Rimardb24d9c2016-08-19 15:18:23 +0000303 for (InputSectionBase<ELFT> *Sec : V) {
Eugene Leviant20d03192016-09-16 15:30:47 +0000304 addSection(Factory, Sec, Cmd->Name);
305 if (uint32_t Subalign = Cmd->SubalignExpr ? Cmd->SubalignExpr(0) : 0)
George Rimardb24d9c2016-08-19 15:18:23 +0000306 Sec->Alignment = Subalign;
George Rimardb24d9c2016-08-19 15:18:23 +0000307 }
Eugene Leviantceabe802016-08-11 07:56:43 +0000308 }
Rui Ueyama48c3f1c2016-08-12 00:27:23 +0000309 }
Eugene Leviant20d03192016-09-16 15:30:47 +0000310}
Eugene Leviante63d81b2016-07-20 14:43:20 +0000311
Eugene Leviant20d03192016-09-16 15:30:47 +0000312template <class ELFT>
313void LinkerScript<ELFT>::createSections(OutputSectionFactory<ELFT> &Factory) {
314 processCommands(Factory);
Rui Ueyama0b9ce6a2016-08-12 03:16:56 +0000315 // Add orphan sections.
Eugene Leviant20d03192016-09-16 15:30:47 +0000316 for (ObjectFile<ELFT> *F : Symtab<ELFT>::X->getObjectFiles())
317 for (InputSectionBase<ELFT> *S : F->getSections())
318 if (!isDiscarded(S) && !S->OutSec)
319 addSection(Factory, S, getOutputSectionName(S));
Eugene Leviante63d81b2016-07-20 14:43:20 +0000320}
321
Eugene Leviantdb741e72016-09-07 07:08:43 +0000322// Sets value of a section-defined symbol. Two kinds of
323// symbols are processed: synthetic symbols, whose value
324// is an offset from beginning of section and regular
325// symbols whose value is absolute.
326template <class ELFT>
327static void assignSectionSymbol(SymbolAssignment *Cmd,
328 OutputSectionBase<ELFT> *Sec,
329 typename ELFT::uint Off) {
330 if (!Cmd->Sym)
331 return;
332
333 if (auto *Body = dyn_cast<DefinedSynthetic<ELFT>>(Cmd->Sym)) {
334 Body->Section = Sec;
335 Body->Value = Cmd->Expression(Sec->getVA() + Off) - Sec->getVA();
336 return;
337 }
338 auto *Body = cast<DefinedRegular<ELFT>>(Cmd->Sym);
339 Body->Value = Cmd->Expression(Sec->getVA() + Off);
340}
341
Rafael Espindolad3190792016-09-16 15:10:23 +0000342template <class ELFT> void LinkerScript<ELFT>::output(InputSection<ELFT> *S) {
343 if (!AlreadyOutputIS.insert(S).second)
344 return;
345 bool IsTbss =
346 (CurOutSec->getFlags() & SHF_TLS) && CurOutSec->getType() == SHT_NOBITS;
Eugene Leviant20889c52016-08-31 08:13:33 +0000347
Rafael Espindolad3190792016-09-16 15:10:23 +0000348 uintX_t Pos = IsTbss ? Dot + ThreadBssOffset : Dot;
349 Pos = alignTo(Pos, S->Alignment);
350 S->OutSecOff = Pos - CurOutSec->getVA();
351 Pos += S->getSize();
352
353 // Update output section size after adding each section. This is so that
354 // SIZEOF works correctly in the case below:
355 // .foo { *(.aaa) a = SIZEOF(.foo); *(.bbb) }
356 CurOutSec->setSize(Pos - CurOutSec->getVA());
357
358 if (!IsTbss)
359 Dot = Pos;
360}
361
362template <class ELFT> void LinkerScript<ELFT>::flush() {
363 if (auto *OutSec = dyn_cast_or_null<OutputSection<ELFT>>(CurOutSec)) {
364 for (InputSection<ELFT> *I : OutSec->Sections)
365 output(I);
366 AlreadyOutputOS.insert(CurOutSec);
Eugene Leviant20889c52016-08-31 08:13:33 +0000367 }
368}
369
370template <class ELFT>
Rafael Espindolad3190792016-09-16 15:10:23 +0000371void LinkerScript<ELFT>::switchTo(OutputSectionBase<ELFT> *Sec) {
372 if (CurOutSec == Sec)
373 return;
374 if (AlreadyOutputOS.count(Sec))
375 return;
376
377 flush();
378 CurOutSec = Sec;
379
380 Dot = alignTo(Dot, CurOutSec->getAlignment());
381 CurOutSec->setVA(Dot);
382}
383
384template <class ELFT> void LinkerScript<ELFT>::process(BaseCommand &Base) {
385 if (auto *AssignCmd = dyn_cast<SymbolAssignment>(&Base)) {
386 if (AssignCmd->Name == ".") {
387 // Update to location counter means update to section size.
388 Dot = AssignCmd->Expression(Dot);
389 CurOutSec->setSize(Dot - CurOutSec->getVA());
390 return;
391 }
392 assignSectionSymbol<ELFT>(AssignCmd, CurOutSec, Dot - CurOutSec->getVA());
Eugene Leviantceabe802016-08-11 07:56:43 +0000393 return;
Rui Ueyama2de509c2016-08-12 00:55:08 +0000394 }
Rafael Espindolad3190792016-09-16 15:10:23 +0000395 auto &ICmd = cast<InputSectionDescription>(Base);
396 for (InputSectionData *ID : ICmd.Sections) {
397 auto *IB = static_cast<InputSectionBase<ELFT> *>(ID);
398 switchTo(IB->OutSec);
399 if (auto *I = dyn_cast<InputSection<ELFT>>(IB))
400 output(I);
401 else if (AlreadyOutputOS.insert(CurOutSec).second)
402 Dot += CurOutSec->getSize();
Eugene Leviantceabe802016-08-11 07:56:43 +0000403 }
404}
405
George Rimar8f66df92016-08-12 20:38:20 +0000406template <class ELFT>
George Rimara14b13d2016-09-07 10:46:07 +0000407static std::vector<OutputSectionBase<ELFT> *>
408findSections(OutputSectionCommand &Cmd,
Rafael Espindolad3190792016-09-16 15:10:23 +0000409 const std::vector<OutputSectionBase<ELFT> *> &Sections) {
George Rimara14b13d2016-09-07 10:46:07 +0000410 std::vector<OutputSectionBase<ELFT> *> Ret;
411 for (OutputSectionBase<ELFT> *Sec : Sections)
412 if (Sec->getName() == Cmd.Name &&
413 checkConstraint(Sec->getFlags(), Cmd.Constraint))
414 Ret.push_back(Sec);
415 return Ret;
George Rimar8f66df92016-08-12 20:38:20 +0000416}
417
Rafael Espindolad3190792016-09-16 15:10:23 +0000418template <class ELFT>
419void LinkerScript<ELFT>::assignOffsets(OutputSectionCommand *Cmd) {
420 std::vector<OutputSectionBase<ELFT> *> Sections =
421 findSections(*Cmd, *OutputSections);
422 if (Sections.empty())
423 return;
424 switchTo(Sections[0]);
425
426 // Find the last section output location. We will output orphan sections
427 // there so that end symbols point to the correct location.
428 auto E = std::find_if(Cmd->Commands.rbegin(), Cmd->Commands.rend(),
429 [](const std::unique_ptr<BaseCommand> &Cmd) {
430 return !isa<SymbolAssignment>(*Cmd);
431 })
432 .base();
433 for (auto I = Cmd->Commands.begin(); I != E; ++I)
434 process(**I);
435 flush();
436 for (OutputSectionBase<ELFT> *Base : Sections) {
437 if (!AlreadyOutputOS.insert(Base).second)
438 continue;
439 switchTo(Base);
440 Dot += CurOutSec->getSize();
441 }
442 for (auto I = E, E = Cmd->Commands.end(); I != E; ++I)
443 process(**I);
444}
445
Rafael Espindolaa4b41dc2016-08-04 12:13:05 +0000446template <class ELFT> void LinkerScript<ELFT>::assignAddresses() {
George Rimar652852c2016-04-16 10:10:32 +0000447 // Orphan sections are sections present in the input files which
Rui Ueyama7c18c282016-04-18 21:00:40 +0000448 // are not explicitly placed into the output file by the linker script.
449 // We place orphan sections at end of file.
450 // Other linkers places them using some heuristics as described in
George Rimar652852c2016-04-16 10:10:32 +0000451 // https://sourceware.org/binutils/docs/ld/Orphan-Sections.html#Orphan-Sections.
Rui Ueyamae5cc6682016-08-12 00:36:56 +0000452 for (OutputSectionBase<ELFT> *Sec : *OutputSections) {
George Rimar652852c2016-04-16 10:10:32 +0000453 StringRef Name = Sec->getName();
Rui Ueyamac3e2a4b2016-04-21 20:30:00 +0000454 if (getSectionIndex(Name) == INT_MAX)
George Rimar076fe152016-07-21 06:43:01 +0000455 Opt.Commands.push_back(llvm::make_unique<OutputSectionCommand>(Name));
George Rimar652852c2016-04-16 10:10:32 +0000456 }
George Rimar652852c2016-04-16 10:10:32 +0000457
Rui Ueyama7c18c282016-04-18 21:00:40 +0000458 // Assign addresses as instructed by linker script SECTIONS sub-commands.
Rui Ueyama4f7500b2016-08-12 04:00:22 +0000459 Dot = getHeaderSize();
Eugene Leviant467c4d52016-07-01 10:27:36 +0000460 uintX_t MinVA = std::numeric_limits<uintX_t>::max();
George Rimar652852c2016-04-16 10:10:32 +0000461
George Rimar076fe152016-07-21 06:43:01 +0000462 for (const std::unique_ptr<BaseCommand> &Base : Opt.Commands) {
463 if (auto *Cmd = dyn_cast<SymbolAssignment>(Base.get())) {
Rui Ueyama8d083e62016-07-29 05:48:39 +0000464 if (Cmd->Name == ".") {
465 Dot = Cmd->Expression(Dot);
466 } else if (Cmd->Sym) {
467 cast<DefinedRegular<ELFT>>(Cmd->Sym)->Value = Cmd->Expression(Dot);
468 }
George Rimar652852c2016-04-16 10:10:32 +0000469 continue;
470 }
471
George Rimareefa7582016-08-04 09:29:31 +0000472 if (auto *Cmd = dyn_cast<AssertCommand>(Base.get())) {
473 Cmd->Expression(Dot);
474 continue;
475 }
476
George Rimar076fe152016-07-21 06:43:01 +0000477 auto *Cmd = cast<OutputSectionCommand>(Base.get());
George Rimar652852c2016-04-16 10:10:32 +0000478
Rafael Espindolad3190792016-09-16 15:10:23 +0000479 if (Cmd->AddrExpr)
480 Dot = Cmd->AddrExpr(Dot);
George Rimar58e5c4d2016-07-25 08:29:46 +0000481
Rafael Espindolad3190792016-09-16 15:10:23 +0000482 MinVA = std::min(MinVA, Dot);
483 assignOffsets(Cmd);
George Rimar652852c2016-04-16 10:10:32 +0000484 }
Rui Ueyama52c4e172016-07-01 10:42:25 +0000485
Rafael Espindolad3190792016-09-16 15:10:23 +0000486 for (OutputSectionBase<ELFT> *Sec : *OutputSections)
487 if (!(Sec->getFlags() & SHF_ALLOC))
488 Sec->setVA(0);
Rafael Espindola4ec013a2016-09-15 21:22:11 +0000489 uintX_t HeaderSize =
490 Out<ELFT>::ElfHeader->getSize() + Out<ELFT>::ProgramHeaders->getSize();
491 if (HeaderSize > MinVA)
492 fatal("Not enough space for ELF and program headers");
493
Rafael Espindola64c32d62016-07-07 14:28:47 +0000494 // ELF and Program headers need to be right before the first section in
George Rimarb91e7112016-07-19 07:42:07 +0000495 // memory. Set their addresses accordingly.
Rafael Espindola4ec013a2016-09-15 21:22:11 +0000496 MinVA = alignDown(MinVA - HeaderSize, Target->PageSize);
Eugene Leviant467c4d52016-07-01 10:27:36 +0000497 Out<ELFT>::ElfHeader->setVA(MinVA);
498 Out<ELFT>::ProgramHeaders->setVA(Out<ELFT>::ElfHeader->getSize() + MinVA);
George Rimar652852c2016-04-16 10:10:32 +0000499}
500
Rui Ueyama464daad2016-08-22 04:55:20 +0000501// Creates program headers as instructed by PHDRS linker script command.
Rui Ueyama07320e42016-04-20 20:13:41 +0000502template <class ELFT>
Rafael Espindolaa4b41dc2016-08-04 12:13:05 +0000503std::vector<PhdrEntry<ELFT>> LinkerScript<ELFT>::createPhdrs() {
Rui Ueyamaedebbdf2016-07-24 23:47:31 +0000504 std::vector<PhdrEntry<ELFT>> Ret;
Eugene Leviantbbe38602016-07-19 09:25:43 +0000505
Rui Ueyama464daad2016-08-22 04:55:20 +0000506 // Process PHDRS and FILEHDR keywords because they are not
507 // real output sections and cannot be added in the following loop.
Eugene Leviantbbe38602016-07-19 09:25:43 +0000508 for (const PhdrsCommand &Cmd : Opt.PhdrsCommands) {
Rui Ueyamaedebbdf2016-07-24 23:47:31 +0000509 Ret.emplace_back(Cmd.Type, Cmd.Flags == UINT_MAX ? PF_R : Cmd.Flags);
510 PhdrEntry<ELFT> &Phdr = Ret.back();
Eugene Leviantbbe38602016-07-19 09:25:43 +0000511
512 if (Cmd.HasFilehdr)
Rui Ueyamaadca2452016-07-23 14:18:48 +0000513 Phdr.add(Out<ELFT>::ElfHeader);
Eugene Leviantbbe38602016-07-19 09:25:43 +0000514 if (Cmd.HasPhdrs)
Rui Ueyamaadca2452016-07-23 14:18:48 +0000515 Phdr.add(Out<ELFT>::ProgramHeaders);
Eugene Leviant56b21c82016-09-09 09:46:16 +0000516
517 if (Cmd.LMAExpr) {
518 Phdr.H.p_paddr = Cmd.LMAExpr(0);
519 Phdr.HasLMA = true;
520 }
Eugene Leviantbbe38602016-07-19 09:25:43 +0000521 }
522
Rui Ueyama464daad2016-08-22 04:55:20 +0000523 // Add output sections to program headers.
Rui Ueyamaedebbdf2016-07-24 23:47:31 +0000524 PhdrEntry<ELFT> *Load = nullptr;
525 uintX_t Flags = PF_R;
Rui Ueyama464daad2016-08-22 04:55:20 +0000526 for (OutputSectionBase<ELFT> *Sec : *OutputSections) {
Eugene Leviantbbe38602016-07-19 09:25:43 +0000527 if (!(Sec->getFlags() & SHF_ALLOC))
528 break;
529
Rui Ueyamaedebbdf2016-07-24 23:47:31 +0000530 std::vector<size_t> PhdrIds = getPhdrIndices(Sec->getName());
Eugene Leviantbbe38602016-07-19 09:25:43 +0000531 if (!PhdrIds.empty()) {
532 // Assign headers specified by linker script
533 for (size_t Id : PhdrIds) {
Rui Ueyamaedebbdf2016-07-24 23:47:31 +0000534 Ret[Id].add(Sec);
Eugene Leviant865bf862016-07-21 10:43:25 +0000535 if (Opt.PhdrsCommands[Id].Flags == UINT_MAX)
Rafael Espindola0b113672016-07-27 14:10:56 +0000536 Ret[Id].H.p_flags |= Sec->getPhdrFlags();
Eugene Leviantbbe38602016-07-19 09:25:43 +0000537 }
538 } else {
539 // If we have no load segment or flags've changed then we want new load
540 // segment.
Rafael Espindola0b113672016-07-27 14:10:56 +0000541 uintX_t NewFlags = Sec->getPhdrFlags();
Eugene Leviantbbe38602016-07-19 09:25:43 +0000542 if (Load == nullptr || Flags != NewFlags) {
Rui Ueyamaedebbdf2016-07-24 23:47:31 +0000543 Load = &*Ret.emplace(Ret.end(), PT_LOAD, NewFlags);
Eugene Leviantbbe38602016-07-19 09:25:43 +0000544 Flags = NewFlags;
545 }
Rui Ueyama18f084f2016-07-20 19:36:41 +0000546 Load->add(Sec);
Eugene Leviantbbe38602016-07-19 09:25:43 +0000547 }
Eugene Leviantbbe38602016-07-19 09:25:43 +0000548 }
Rui Ueyamaedebbdf2016-07-24 23:47:31 +0000549 return Ret;
Eugene Leviantbbe38602016-07-19 09:25:43 +0000550}
551
Eugene Leviantf9bc3bd2016-08-16 06:40:58 +0000552template <class ELFT> bool LinkerScript<ELFT>::ignoreInterpSection() {
553 // Ignore .interp section in case we have PHDRS specification
554 // and PT_INTERP isn't listed.
555 return !Opt.PhdrsCommands.empty() &&
556 llvm::find_if(Opt.PhdrsCommands, [](const PhdrsCommand &Cmd) {
557 return Cmd.Type == PT_INTERP;
558 }) == Opt.PhdrsCommands.end();
559}
560
Eugene Leviantbbe38602016-07-19 09:25:43 +0000561template <class ELFT>
Rui Ueyama07320e42016-04-20 20:13:41 +0000562ArrayRef<uint8_t> LinkerScript<ELFT>::getFiller(StringRef Name) {
George Rimarf6c3cce2016-07-21 07:48:54 +0000563 for (const std::unique_ptr<BaseCommand> &Base : Opt.Commands)
564 if (auto *Cmd = dyn_cast<OutputSectionCommand>(Base.get()))
565 if (Cmd->Name == Name)
566 return Cmd->Filler;
567 return {};
George Rimare2ee72b2016-02-26 14:48:31 +0000568}
569
George Rimar206fffa2016-08-17 08:16:57 +0000570template <class ELFT> Expr LinkerScript<ELFT>::getLma(StringRef Name) {
George Rimar8ceadb32016-08-17 07:44:19 +0000571 for (const std::unique_ptr<BaseCommand> &Base : Opt.Commands)
572 if (auto *Cmd = dyn_cast<OutputSectionCommand>(Base.get()))
573 if (Cmd->LmaExpr && Cmd->Name == Name)
574 return Cmd->LmaExpr;
575 return {};
576}
577
Rui Ueyamac3e2a4b2016-04-21 20:30:00 +0000578// Returns the index of the given section name in linker script
579// SECTIONS commands. Sections are laid out as the same order as they
580// were in the script. If a given name did not appear in the script,
581// it returns INT_MAX, so that it will be laid out at end of file.
George Rimar076fe152016-07-21 06:43:01 +0000582template <class ELFT> int LinkerScript<ELFT>::getSectionIndex(StringRef Name) {
Rui Ueyamaf510fa62016-07-26 00:21:15 +0000583 int I = 0;
584 for (std::unique_ptr<BaseCommand> &Base : Opt.Commands) {
585 if (auto *Cmd = dyn_cast<OutputSectionCommand>(Base.get()))
586 if (Cmd->Name == Name)
587 return I;
588 ++I;
589 }
590 return INT_MAX;
George Rimar71b26e92016-04-21 10:22:02 +0000591}
592
593// A compartor to sort output sections. Returns -1 or 1 if
594// A or B are mentioned in linker script. Otherwise, returns 0.
Rui Ueyama07320e42016-04-20 20:13:41 +0000595template <class ELFT>
596int LinkerScript<ELFT>::compareSections(StringRef A, StringRef B) {
Rui Ueyamac3e2a4b2016-04-21 20:30:00 +0000597 int I = getSectionIndex(A);
598 int J = getSectionIndex(B);
599 if (I == INT_MAX && J == INT_MAX)
Rui Ueyama717677a2016-02-11 21:17:59 +0000600 return 0;
601 return I < J ? -1 : 1;
602}
603
Eugene Leviantbbe38602016-07-19 09:25:43 +0000604template <class ELFT> bool LinkerScript<ELFT>::hasPhdrsCommands() {
605 return !Opt.PhdrsCommands.empty();
606}
607
George Rimar9e694502016-07-29 16:18:47 +0000608template <class ELFT>
George Rimar884e7862016-09-08 08:19:13 +0000609uint64_t LinkerScript<ELFT>::getOutputSectionAddress(StringRef Name) {
George Rimar96659df2016-08-30 09:54:01 +0000610 for (OutputSectionBase<ELFT> *Sec : *OutputSections)
611 if (Sec->getName() == Name)
612 return Sec->getVA();
613 error("undefined section " + Name);
614 return 0;
615}
616
617template <class ELFT>
George Rimar884e7862016-09-08 08:19:13 +0000618uint64_t LinkerScript<ELFT>::getOutputSectionSize(StringRef Name) {
George Rimar9e694502016-07-29 16:18:47 +0000619 for (OutputSectionBase<ELFT> *Sec : *OutputSections)
620 if (Sec->getName() == Name)
621 return Sec->getSize();
622 error("undefined section " + Name);
623 return 0;
624}
625
Eugene Leviant36fac7f2016-09-08 09:08:30 +0000626template <class ELFT>
627uint64_t LinkerScript<ELFT>::getOutputSectionAlign(StringRef Name) {
628 for (OutputSectionBase<ELFT> *Sec : *OutputSections)
629 if (Sec->getName() == Name)
630 return Sec->getAlignment();
631 error("undefined section " + Name);
632 return 0;
633}
634
George Rimar884e7862016-09-08 08:19:13 +0000635template <class ELFT> uint64_t LinkerScript<ELFT>::getHeaderSize() {
George Rimare32a3592016-08-10 07:59:34 +0000636 return Out<ELFT>::ElfHeader->getSize() + Out<ELFT>::ProgramHeaders->getSize();
637}
638
George Rimar884e7862016-09-08 08:19:13 +0000639template <class ELFT> uint64_t LinkerScript<ELFT>::getSymbolValue(StringRef S) {
640 if (SymbolBody *B = Symtab<ELFT>::X->find(S))
641 return B->getVA<ELFT>();
642 error("symbol not found: " + S);
643 return 0;
644}
645
Eugene Leviantbbe38602016-07-19 09:25:43 +0000646// Returns indices of ELF headers containing specific section, identified
647// by Name. Each index is a zero based number of ELF header listed within
648// PHDRS {} script block.
649template <class ELFT>
Rui Ueyamaedebbdf2016-07-24 23:47:31 +0000650std::vector<size_t> LinkerScript<ELFT>::getPhdrIndices(StringRef SectionName) {
George Rimar076fe152016-07-21 06:43:01 +0000651 for (const std::unique_ptr<BaseCommand> &Base : Opt.Commands) {
652 auto *Cmd = dyn_cast<OutputSectionCommand>(Base.get());
Rui Ueyamaedebbdf2016-07-24 23:47:31 +0000653 if (!Cmd || Cmd->Name != SectionName)
George Rimar31d842f2016-07-20 16:43:03 +0000654 continue;
655
Rui Ueyama29c5a2a2016-07-26 00:27:36 +0000656 std::vector<size_t> Ret;
657 for (StringRef PhdrName : Cmd->Phdrs)
658 Ret.push_back(getPhdrIndex(PhdrName));
659 return Ret;
Eugene Leviantbbe38602016-07-19 09:25:43 +0000660 }
George Rimar31d842f2016-07-20 16:43:03 +0000661 return {};
Eugene Leviantbbe38602016-07-19 09:25:43 +0000662}
663
Rui Ueyama29c5a2a2016-07-26 00:27:36 +0000664template <class ELFT>
665size_t LinkerScript<ELFT>::getPhdrIndex(StringRef PhdrName) {
666 size_t I = 0;
667 for (PhdrsCommand &Cmd : Opt.PhdrsCommands) {
668 if (Cmd.Name == PhdrName)
669 return I;
670 ++I;
671 }
672 error("section header '" + PhdrName + "' is not listed in PHDRS");
673 return 0;
674}
675
Rui Ueyama07320e42016-04-20 20:13:41 +0000676class elf::ScriptParser : public ScriptParserBase {
George Rimarc3794e52016-02-24 09:21:47 +0000677 typedef void (ScriptParser::*Handler)();
678
Rui Ueyamaf7c5fbb2015-09-30 17:23:26 +0000679public:
Rui Ueyama07320e42016-04-20 20:13:41 +0000680 ScriptParser(StringRef S, bool B) : ScriptParserBase(S), IsUnderSysroot(B) {}
George Rimarf23b2322016-02-19 10:45:45 +0000681
George Rimar20b65982016-08-31 09:08:26 +0000682 void readLinkerScript();
683 void readVersionScript();
Rui Ueyamaf7c5fbb2015-09-30 17:23:26 +0000684
685private:
Rui Ueyama52a15092015-10-11 03:28:42 +0000686 void addFile(StringRef Path);
687
Rui Ueyamaf7c5fbb2015-09-30 17:23:26 +0000688 void readAsNeeded();
Denis Protivensky90c50992015-10-08 06:48:38 +0000689 void readEntry();
George Rimar83f406c2015-10-19 17:35:12 +0000690 void readExtern();
Rui Ueyamaf7c5fbb2015-09-30 17:23:26 +0000691 void readGroup();
Rui Ueyama31aa1f82015-10-11 01:31:55 +0000692 void readInclude();
Rui Ueyamaee592822015-10-07 00:25:09 +0000693 void readOutput();
Davide Italiano9159ce92015-10-12 21:50:08 +0000694 void readOutputArch();
Rui Ueyamaf7c5fbb2015-09-30 17:23:26 +0000695 void readOutputFormat();
Eugene Leviantbbe38602016-07-19 09:25:43 +0000696 void readPhdrs();
Davide Italiano68a39a62015-10-08 17:51:41 +0000697 void readSearchDir();
Denis Protivensky8e3b38a2015-11-12 09:52:08 +0000698 void readSections();
Rui Ueyama95769b42016-08-31 20:03:54 +0000699 void readVersion();
700 void readVersionScriptCommand();
Denis Protivensky8e3b38a2015-11-12 09:52:08 +0000701
Rui Ueyama113cdec2016-07-24 23:05:57 +0000702 SymbolAssignment *readAssignment(StringRef Name);
George Rimarff1f29e2016-09-06 13:51:57 +0000703 std::vector<uint8_t> readFill();
Rui Ueyama10416562016-08-04 02:03:27 +0000704 OutputSectionCommand *readOutputSectionDescription(StringRef OutSec);
George Rimarff1f29e2016-09-06 13:51:57 +0000705 std::vector<uint8_t> readOutputSectionFiller(StringRef Tok);
Eugene Leviantbbe38602016-07-19 09:25:43 +0000706 std::vector<StringRef> readOutputSectionPhdrs();
George Rimara2496cb2016-08-30 09:46:59 +0000707 InputSectionDescription *readInputSectionDescription(StringRef Tok);
George Rimarc91930a2016-09-02 21:17:20 +0000708 Regex readFilePatterns();
George Rimar395281c2016-09-16 17:42:10 +0000709 void readSectionExcludes(InputSectionDescription *Cmd);
George Rimara2496cb2016-08-30 09:46:59 +0000710 InputSectionDescription *readInputSectionRules(StringRef FilePattern);
Eugene Leviantbbe38602016-07-19 09:25:43 +0000711 unsigned readPhdrType();
George Rimarbe394db2016-09-16 20:21:55 +0000712 SortSectionPolicy readSortKind();
Petr Hoseka35e39c2016-08-16 01:11:16 +0000713 SymbolAssignment *readProvideHidden(bool Provide, bool Hidden);
Eugene Leviantdb741e72016-09-07 07:08:43 +0000714 SymbolAssignment *readProvideOrAssignment(StringRef Tok, bool MakeAbsolute);
George Rimar03fc0102016-07-28 07:18:23 +0000715 void readSort();
George Rimareefa7582016-08-04 09:29:31 +0000716 Expr readAssert();
Rui Ueyama708019c2016-07-24 18:19:40 +0000717
718 Expr readExpr();
719 Expr readExpr1(Expr Lhs, int MinPrec);
720 Expr readPrimary();
721 Expr readTernary(Expr Cond);
Rui Ueyama6ad7dfc2016-08-17 18:59:16 +0000722 Expr readParenExpr();
Rui Ueyamaf7c5fbb2015-09-30 17:23:26 +0000723
George Rimar20b65982016-08-31 09:08:26 +0000724 // For parsing version script.
725 void readExtern(std::vector<SymbolVersion> *Globals);
Rui Ueyama95769b42016-08-31 20:03:54 +0000726 void readVersionDeclaration(StringRef VerStr);
George Rimar20b65982016-08-31 09:08:26 +0000727 void readGlobal(StringRef VerStr);
728 void readLocal();
729
Rui Ueyama07320e42016-04-20 20:13:41 +0000730 ScriptConfiguration &Opt = *ScriptConfig;
731 StringSaver Saver = {ScriptConfig->Alloc};
Simon Atanasyan16b0cc92015-11-26 05:53:00 +0000732 bool IsUnderSysroot;
Rui Ueyamaf7c5fbb2015-09-30 17:23:26 +0000733};
Rui Ueyamaf7c5fbb2015-09-30 17:23:26 +0000734
George Rimar20b65982016-08-31 09:08:26 +0000735void ScriptParser::readVersionScript() {
Rui Ueyama95769b42016-08-31 20:03:54 +0000736 readVersionScriptCommand();
737 if (!atEOF())
738 setError("EOF expected, but got " + next());
739}
740
741void ScriptParser::readVersionScriptCommand() {
George Rimar20b65982016-08-31 09:08:26 +0000742 if (skip("{")) {
Rui Ueyama95769b42016-08-31 20:03:54 +0000743 readVersionDeclaration("");
George Rimar20b65982016-08-31 09:08:26 +0000744 return;
745 }
746
Rui Ueyama95769b42016-08-31 20:03:54 +0000747 while (!atEOF() && !Error && peek() != "}") {
George Rimar20b65982016-08-31 09:08:26 +0000748 StringRef VerStr = next();
749 if (VerStr == "{") {
Rui Ueyama95769b42016-08-31 20:03:54 +0000750 setError("anonymous version definition is used in "
751 "combination with other version definitions");
George Rimar20b65982016-08-31 09:08:26 +0000752 return;
753 }
754 expect("{");
Rui Ueyama95769b42016-08-31 20:03:54 +0000755 readVersionDeclaration(VerStr);
George Rimar20b65982016-08-31 09:08:26 +0000756 }
757}
758
Rui Ueyama95769b42016-08-31 20:03:54 +0000759void ScriptParser::readVersion() {
760 expect("{");
761 readVersionScriptCommand();
762 expect("}");
763}
764
George Rimar20b65982016-08-31 09:08:26 +0000765void ScriptParser::readLinkerScript() {
Rui Ueyamaf7c5fbb2015-09-30 17:23:26 +0000766 while (!atEOF()) {
767 StringRef Tok = next();
Rui Ueyamaa27eecc2016-09-02 18:52:41 +0000768 if (Tok == ";")
769 continue;
770
Eugene Leviant20d03192016-09-16 15:30:47 +0000771 if (Tok == "ASSERT") {
772 Opt.Commands.emplace_back(new AssertCommand(readAssert()));
773 } else if (Tok == "ENTRY") {
Rui Ueyamaa27eecc2016-09-02 18:52:41 +0000774 readEntry();
775 } else if (Tok == "EXTERN") {
776 readExtern();
777 } else if (Tok == "GROUP" || Tok == "INPUT") {
778 readGroup();
779 } else if (Tok == "INCLUDE") {
780 readInclude();
781 } else if (Tok == "OUTPUT") {
782 readOutput();
783 } else if (Tok == "OUTPUT_ARCH") {
784 readOutputArch();
785 } else if (Tok == "OUTPUT_FORMAT") {
786 readOutputFormat();
787 } else if (Tok == "PHDRS") {
788 readPhdrs();
789 } else if (Tok == "SEARCH_DIR") {
790 readSearchDir();
791 } else if (Tok == "SECTIONS") {
792 readSections();
793 } else if (Tok == "VERSION") {
794 readVersion();
Eugene Leviantdb741e72016-09-07 07:08:43 +0000795 } else if (SymbolAssignment *Cmd = readProvideOrAssignment(Tok, true)) {
Eugene Leviant20d03192016-09-16 15:30:47 +0000796 Opt.Commands.emplace_back(Cmd);
Petr Hoseke5d3ca52016-08-31 15:31:17 +0000797 } else {
George Rimar57610422016-03-11 14:43:02 +0000798 setError("unknown directive: " + Tok);
Petr Hoseke5d3ca52016-08-31 15:31:17 +0000799 }
Rui Ueyamaf7c5fbb2015-09-30 17:23:26 +0000800 }
801}
802
Rui Ueyama717677a2016-02-11 21:17:59 +0000803void ScriptParser::addFile(StringRef S) {
Simon Atanasyan16b0cc92015-11-26 05:53:00 +0000804 if (IsUnderSysroot && S.startswith("/")) {
805 SmallString<128> Path;
806 (Config->Sysroot + S).toStringRef(Path);
807 if (sys::fs::exists(Path)) {
808 Driver->addFile(Saver.save(Path.str()));
809 return;
810 }
811 }
812
Rui Ueyamaf03f3cc2015-10-13 00:09:21 +0000813 if (sys::path::is_absolute(S)) {
Rui Ueyama52a15092015-10-11 03:28:42 +0000814 Driver->addFile(S);
815 } else if (S.startswith("=")) {
816 if (Config->Sysroot.empty())
817 Driver->addFile(S.substr(1));
818 else
819 Driver->addFile(Saver.save(Config->Sysroot + "/" + S.substr(1)));
820 } else if (S.startswith("-l")) {
Rui Ueyama21eecb42016-02-02 21:13:09 +0000821 Driver->addLibrary(S.substr(2));
Simon Atanasyana1b8fc32015-11-26 20:23:46 +0000822 } else if (sys::fs::exists(S)) {
823 Driver->addFile(S);
Rui Ueyama52a15092015-10-11 03:28:42 +0000824 } else {
825 std::string Path = findFromSearchPaths(S);
826 if (Path.empty())
George Rimar777f9632016-03-12 08:31:34 +0000827 setError("unable to find " + S);
Rui Ueyama025d59b2016-02-02 20:27:59 +0000828 else
829 Driver->addFile(Saver.save(Path));
Rui Ueyama52a15092015-10-11 03:28:42 +0000830 }
831}
832
Rui Ueyama717677a2016-02-11 21:17:59 +0000833void ScriptParser::readAsNeeded() {
Rui Ueyamaf7c5fbb2015-09-30 17:23:26 +0000834 expect("(");
Rui Ueyama35da9b62015-10-11 20:59:12 +0000835 bool Orig = Config->AsNeeded;
836 Config->AsNeeded = true;
Rui Ueyamaa2acc932016-08-05 01:25:45 +0000837 while (!Error && !skip(")"))
George Rimarcd574a52016-09-09 14:35:36 +0000838 addFile(unquote(next()));
Rui Ueyama35da9b62015-10-11 20:59:12 +0000839 Config->AsNeeded = Orig;
Rui Ueyamaf7c5fbb2015-09-30 17:23:26 +0000840}
841
Rui Ueyama717677a2016-02-11 21:17:59 +0000842void ScriptParser::readEntry() {
Denis Protivensky90c50992015-10-08 06:48:38 +0000843 // -e <symbol> takes predecence over ENTRY(<symbol>).
844 expect("(");
845 StringRef Tok = next();
846 if (Config->Entry.empty())
847 Config->Entry = Tok;
848 expect(")");
849}
850
Rui Ueyama717677a2016-02-11 21:17:59 +0000851void ScriptParser::readExtern() {
George Rimar83f406c2015-10-19 17:35:12 +0000852 expect("(");
Rui Ueyamaa2acc932016-08-05 01:25:45 +0000853 while (!Error && !skip(")"))
854 Config->Undefined.push_back(next());
George Rimar83f406c2015-10-19 17:35:12 +0000855}
856
Rui Ueyama717677a2016-02-11 21:17:59 +0000857void ScriptParser::readGroup() {
Rui Ueyamaf7c5fbb2015-09-30 17:23:26 +0000858 expect("(");
Rui Ueyamaa2acc932016-08-05 01:25:45 +0000859 while (!Error && !skip(")")) {
Rui Ueyamaf7c5fbb2015-09-30 17:23:26 +0000860 StringRef Tok = next();
Rui Ueyamaa2acc932016-08-05 01:25:45 +0000861 if (Tok == "AS_NEEDED")
Rui Ueyamaf7c5fbb2015-09-30 17:23:26 +0000862 readAsNeeded();
Rui Ueyamaa2acc932016-08-05 01:25:45 +0000863 else
George Rimarcd574a52016-09-09 14:35:36 +0000864 addFile(unquote(Tok));
Rui Ueyamaf7c5fbb2015-09-30 17:23:26 +0000865 }
866}
867
Rui Ueyama717677a2016-02-11 21:17:59 +0000868void ScriptParser::readInclude() {
Rui Ueyama31aa1f82015-10-11 01:31:55 +0000869 StringRef Tok = next();
George Rimarcd574a52016-09-09 14:35:36 +0000870 auto MBOrErr = MemoryBuffer::getFile(unquote(Tok));
Rui Ueyama025d59b2016-02-02 20:27:59 +0000871 if (!MBOrErr) {
George Rimar57610422016-03-11 14:43:02 +0000872 setError("cannot open " + Tok);
Rui Ueyama025d59b2016-02-02 20:27:59 +0000873 return;
874 }
Rui Ueyama31aa1f82015-10-11 01:31:55 +0000875 std::unique_ptr<MemoryBuffer> &MB = *MBOrErr;
Rui Ueyamaa47ee682015-10-11 01:53:04 +0000876 StringRef S = Saver.save(MB->getMemBufferRef().getBuffer());
877 std::vector<StringRef> V = tokenize(S);
Rui Ueyama31aa1f82015-10-11 01:31:55 +0000878 Tokens.insert(Tokens.begin() + Pos, V.begin(), V.end());
Rui Ueyama31aa1f82015-10-11 01:31:55 +0000879}
880
Rui Ueyama717677a2016-02-11 21:17:59 +0000881void ScriptParser::readOutput() {
Rui Ueyamaee592822015-10-07 00:25:09 +0000882 // -o <file> takes predecence over OUTPUT(<file>).
883 expect("(");
884 StringRef Tok = next();
885 if (Config->OutputFile.empty())
George Rimarcd574a52016-09-09 14:35:36 +0000886 Config->OutputFile = unquote(Tok);
Rui Ueyamaee592822015-10-07 00:25:09 +0000887 expect(")");
888}
889
Rui Ueyama717677a2016-02-11 21:17:59 +0000890void ScriptParser::readOutputArch() {
Davide Italiano9159ce92015-10-12 21:50:08 +0000891 // Error checking only for now.
892 expect("(");
893 next();
894 expect(")");
895}
896
Rui Ueyama717677a2016-02-11 21:17:59 +0000897void ScriptParser::readOutputFormat() {
Rui Ueyamaf7c5fbb2015-09-30 17:23:26 +0000898 // Error checking only for now.
899 expect("(");
900 next();
Davide Italiano6836c612015-10-12 21:08:41 +0000901 StringRef Tok = next();
902 if (Tok == ")")
George Rimar6c55f0e2016-09-08 08:20:30 +0000903 return;
Rui Ueyama025d59b2016-02-02 20:27:59 +0000904 if (Tok != ",") {
George Rimar57610422016-03-11 14:43:02 +0000905 setError("unexpected token: " + Tok);
Rui Ueyama025d59b2016-02-02 20:27:59 +0000906 return;
907 }
Davide Italiano6836c612015-10-12 21:08:41 +0000908 next();
909 expect(",");
910 next();
Rui Ueyamaf7c5fbb2015-09-30 17:23:26 +0000911 expect(")");
912}
913
Eugene Leviantbbe38602016-07-19 09:25:43 +0000914void ScriptParser::readPhdrs() {
915 expect("{");
916 while (!Error && !skip("}")) {
917 StringRef Tok = next();
Eugene Leviant56b21c82016-09-09 09:46:16 +0000918 Opt.PhdrsCommands.push_back(
919 {Tok, PT_NULL, false, false, UINT_MAX, nullptr});
Eugene Leviantbbe38602016-07-19 09:25:43 +0000920 PhdrsCommand &PhdrCmd = Opt.PhdrsCommands.back();
921
922 PhdrCmd.Type = readPhdrType();
923 do {
924 Tok = next();
925 if (Tok == ";")
926 break;
927 if (Tok == "FILEHDR")
928 PhdrCmd.HasFilehdr = true;
929 else if (Tok == "PHDRS")
930 PhdrCmd.HasPhdrs = true;
Eugene Leviant56b21c82016-09-09 09:46:16 +0000931 else if (Tok == "AT")
932 PhdrCmd.LMAExpr = readParenExpr();
Eugene Leviant865bf862016-07-21 10:43:25 +0000933 else if (Tok == "FLAGS") {
934 expect("(");
Rafael Espindolaeb685cd2016-08-02 22:14:57 +0000935 // Passing 0 for the value of dot is a bit of a hack. It means that
936 // we accept expressions like ".|1".
937 PhdrCmd.Flags = readExpr()(0);
Eugene Leviant865bf862016-07-21 10:43:25 +0000938 expect(")");
939 } else
Eugene Leviantbbe38602016-07-19 09:25:43 +0000940 setError("unexpected header attribute: " + Tok);
941 } while (!Error);
942 }
943}
944
Rui Ueyama717677a2016-02-11 21:17:59 +0000945void ScriptParser::readSearchDir() {
Davide Italiano68a39a62015-10-08 17:51:41 +0000946 expect("(");
Rui Ueyama86c5fb82016-09-08 23:26:54 +0000947 StringRef Tok = next();
Rui Ueyama6c7ad132016-09-02 19:20:33 +0000948 if (!Config->Nostdlib)
George Rimarcd574a52016-09-09 14:35:36 +0000949 Config->SearchPaths.push_back(unquote(Tok));
Davide Italiano68a39a62015-10-08 17:51:41 +0000950 expect(")");
951}
952
Rui Ueyama717677a2016-02-11 21:17:59 +0000953void ScriptParser::readSections() {
Eugene Leviante05336ff2016-09-14 08:32:36 +0000954 Opt.HasSections = true;
Denis Protivensky8e3b38a2015-11-12 09:52:08 +0000955 expect("{");
George Rimar652852c2016-04-16 10:10:32 +0000956 while (!Error && !skip("}")) {
Rui Ueyama113cdec2016-07-24 23:05:57 +0000957 StringRef Tok = next();
Eugene Leviantdb741e72016-09-07 07:08:43 +0000958 BaseCommand *Cmd = readProvideOrAssignment(Tok, true);
Eugene Leviantceabe802016-08-11 07:56:43 +0000959 if (!Cmd) {
960 if (Tok == "ASSERT")
961 Cmd = new AssertCommand(readAssert());
962 else
963 Cmd = readOutputSectionDescription(Tok);
Rui Ueyama708019c2016-07-24 18:19:40 +0000964 }
Rui Ueyama10416562016-08-04 02:03:27 +0000965 Opt.Commands.emplace_back(Cmd);
George Rimar652852c2016-04-16 10:10:32 +0000966 }
Denis Protivensky8e3b38a2015-11-12 09:52:08 +0000967}
968
Rui Ueyama708019c2016-07-24 18:19:40 +0000969static int precedence(StringRef Op) {
970 return StringSwitch<int>(Op)
971 .Case("*", 4)
972 .Case("/", 4)
973 .Case("+", 3)
974 .Case("-", 3)
975 .Case("<", 2)
976 .Case(">", 2)
977 .Case(">=", 2)
978 .Case("<=", 2)
979 .Case("==", 2)
980 .Case("!=", 2)
981 .Case("&", 1)
Rafael Espindolacc3dd622016-08-22 21:33:35 +0000982 .Case("|", 1)
Rui Ueyama708019c2016-07-24 18:19:40 +0000983 .Default(-1);
984}
985
George Rimarc91930a2016-09-02 21:17:20 +0000986Regex ScriptParser::readFilePatterns() {
Rui Ueyama10416562016-08-04 02:03:27 +0000987 std::vector<StringRef> V;
988 while (!Error && !skip(")"))
989 V.push_back(next());
George Rimarc91930a2016-09-02 21:17:20 +0000990 return compileGlobPatterns(V);
George Rimar0702c4e2016-07-29 15:32:46 +0000991}
992
George Rimarbe394db2016-09-16 20:21:55 +0000993SortSectionPolicy ScriptParser::readSortKind() {
Rui Ueyama742c3832016-08-04 22:27:00 +0000994 if (skip("SORT") || skip("SORT_BY_NAME"))
George Rimarbe394db2016-09-16 20:21:55 +0000995 return SortSectionPolicy::Name;
Rui Ueyama742c3832016-08-04 22:27:00 +0000996 if (skip("SORT_BY_ALIGNMENT"))
George Rimarbe394db2016-09-16 20:21:55 +0000997 return SortSectionPolicy::Alignment;
George Rimar575208c2016-09-15 19:15:12 +0000998 if (skip("SORT_BY_INIT_PRIORITY"))
George Rimarbe394db2016-09-16 20:21:55 +0000999 return SortSectionPolicy::Priority;
1000 // `SORT_NONE' disables section sorting by ignoring the command line
1001 // section sorting option.
1002 if (skip("SORT_NONE"))
1003 return SortSectionPolicy::IgnoreConfig;
1004 return SortSectionPolicy::None;
1005}
1006
1007static void selectSortKind(InputSectionDescription *Cmd) {
1008 if (Cmd->SortOuter == SortSectionPolicy::IgnoreConfig) {
1009 Cmd->SortOuter = SortSectionPolicy::None;
1010 return;
1011 }
1012
1013 if (Cmd->SortOuter != SortSectionPolicy::None) {
1014 // If the section sorting command in linker script is nested, the command
1015 // line option will be ignored.
1016 if (Cmd->SortInner != SortSectionPolicy::None)
1017 return;
1018 // If the section sorting command in linker script isn't nested, the
1019 // command line option will make the section sorting command to be treated
1020 // as nested sorting command.
1021 Cmd->SortInner = Config->SortSection;
1022 return;
1023 }
1024 // If sorting rule not specified, use command line option.
1025 Cmd->SortOuter = Config->SortSection;
Rui Ueyama742c3832016-08-04 22:27:00 +00001026}
1027
George Rimar395281c2016-09-16 17:42:10 +00001028// Method reads a list of sequence of excluded files and section globs given in
1029// a following form: ((EXCLUDE_FILE(file_pattern+))? section_pattern+)+
1030// Example: *(.foo.1 EXCLUDE_FILE (*a.o) .foo.2 EXCLUDE_FILE (*b.o) .foo.3)
1031void ScriptParser::readSectionExcludes(InputSectionDescription *Cmd) {
1032 llvm::Regex ExcludeFileRe;
1033 std::vector<StringRef> V;
1034
1035 while (!Error) {
1036 if (skip(")")) {
1037 Cmd->SectionsVec.push_back(
1038 {std::move(ExcludeFileRe), compileGlobPatterns(V)});
1039 return;
1040 }
1041
1042 if (skip("EXCLUDE_FILE")) {
1043 if (!V.empty()) {
1044 Cmd->SectionsVec.push_back(
1045 {std::move(ExcludeFileRe), compileGlobPatterns(V)});
1046 V.clear();
1047 }
1048
1049 expect("(");
1050 ExcludeFileRe = readFilePatterns();
1051 continue;
1052 }
1053
1054 V.push_back(next());
1055 }
1056}
1057
George Rimara2496cb2016-08-30 09:46:59 +00001058InputSectionDescription *
1059ScriptParser::readInputSectionRules(StringRef FilePattern) {
George Rimarc91930a2016-09-02 21:17:20 +00001060 auto *Cmd = new InputSectionDescription(FilePattern);
Davide Italiano0ed42b02016-07-25 21:47:13 +00001061 expect("(");
Davide Italianoe7282792016-07-27 01:44:01 +00001062
Rui Ueyama742c3832016-08-04 22:27:00 +00001063 // Read SORT().
George Rimarbe394db2016-09-16 20:21:55 +00001064 SortSectionPolicy K1 = readSortKind();
1065 if (K1 != SortSectionPolicy::None) {
Rui Ueyama742c3832016-08-04 22:27:00 +00001066 Cmd->SortOuter = K1;
George Rimar0702c4e2016-07-29 15:32:46 +00001067 expect("(");
George Rimarbe394db2016-09-16 20:21:55 +00001068 SortSectionPolicy K2 = readSortKind();
1069 if (K2 != SortSectionPolicy::None) {
Rui Ueyama742c3832016-08-04 22:27:00 +00001070 Cmd->SortInner = K2;
George Rimar350ece42016-08-03 08:35:59 +00001071 expect("(");
George Rimar395281c2016-09-16 17:42:10 +00001072 Cmd->SectionsVec.push_back({llvm::Regex(), readFilePatterns()});
George Rimar350ece42016-08-03 08:35:59 +00001073 expect(")");
1074 } else {
George Rimar395281c2016-09-16 17:42:10 +00001075 Cmd->SectionsVec.push_back({llvm::Regex(), readFilePatterns()});
George Rimar350ece42016-08-03 08:35:59 +00001076 }
George Rimar0702c4e2016-07-29 15:32:46 +00001077 expect(")");
George Rimarbe394db2016-09-16 20:21:55 +00001078 selectSortKind(Cmd);
Rui Ueyama10416562016-08-04 02:03:27 +00001079 return Cmd;
George Rimar06598002016-07-28 21:51:30 +00001080 }
George Rimar0702c4e2016-07-29 15:32:46 +00001081
George Rimarbe394db2016-09-16 20:21:55 +00001082 selectSortKind(Cmd);
George Rimar395281c2016-09-16 17:42:10 +00001083 readSectionExcludes(Cmd);
Rui Ueyama10416562016-08-04 02:03:27 +00001084 return Cmd;
Davide Italianoe7282792016-07-27 01:44:01 +00001085}
1086
George Rimara2496cb2016-08-30 09:46:59 +00001087InputSectionDescription *
1088ScriptParser::readInputSectionDescription(StringRef Tok) {
George Rimar06598002016-07-28 21:51:30 +00001089 // Input section wildcard can be surrounded by KEEP.
1090 // https://sourceware.org/binutils/docs/ld/Input-Section-Keep.html#Input-Section-Keep
George Rimara2496cb2016-08-30 09:46:59 +00001091 if (Tok == "KEEP") {
George Rimar06598002016-07-28 21:51:30 +00001092 expect("(");
George Rimara2496cb2016-08-30 09:46:59 +00001093 StringRef FilePattern = next();
1094 InputSectionDescription *Cmd = readInputSectionRules(FilePattern);
George Rimar06598002016-07-28 21:51:30 +00001095 expect(")");
George Rimar395281c2016-09-16 17:42:10 +00001096 for (std::pair<llvm::Regex, llvm::Regex> &Regex : Cmd->SectionsVec)
1097 Opt.KeptSections.push_back(&Regex.second);
Rui Ueyama10416562016-08-04 02:03:27 +00001098 return Cmd;
George Rimar06598002016-07-28 21:51:30 +00001099 }
George Rimara2496cb2016-08-30 09:46:59 +00001100 return readInputSectionRules(Tok);
Davide Italiano0ed42b02016-07-25 21:47:13 +00001101}
1102
George Rimar03fc0102016-07-28 07:18:23 +00001103void ScriptParser::readSort() {
1104 expect("(");
1105 expect("CONSTRUCTORS");
1106 expect(")");
1107}
1108
George Rimareefa7582016-08-04 09:29:31 +00001109Expr ScriptParser::readAssert() {
1110 expect("(");
1111 Expr E = readExpr();
1112 expect(",");
George Rimarcd574a52016-09-09 14:35:36 +00001113 StringRef Msg = unquote(next());
George Rimareefa7582016-08-04 09:29:31 +00001114 expect(")");
1115 return [=](uint64_t Dot) {
1116 uint64_t V = E(Dot);
1117 if (!V)
1118 error(Msg);
1119 return V;
1120 };
1121}
1122
Rui Ueyama25150e82016-09-06 17:46:43 +00001123// Reads a FILL(expr) command. We handle the FILL command as an
1124// alias for =fillexp section attribute, which is different from
1125// what GNU linkers do.
1126// https://sourceware.org/binutils/docs/ld/Output-Section-Data.html
George Rimarff1f29e2016-09-06 13:51:57 +00001127std::vector<uint8_t> ScriptParser::readFill() {
1128 expect("(");
1129 std::vector<uint8_t> V = readOutputSectionFiller(next());
1130 expect(")");
1131 expect(";");
1132 return V;
1133}
1134
Rui Ueyama10416562016-08-04 02:03:27 +00001135OutputSectionCommand *
1136ScriptParser::readOutputSectionDescription(StringRef OutSec) {
George Rimar076fe152016-07-21 06:43:01 +00001137 OutputSectionCommand *Cmd = new OutputSectionCommand(OutSec);
George Rimar58e5c4d2016-07-25 08:29:46 +00001138
1139 // Read an address expression.
1140 // https://sourceware.org/binutils/docs/ld/Output-Section-Address.html#Output-Section-Address
1141 if (peek() != ":")
1142 Cmd->AddrExpr = readExpr();
1143
Denis Protivensky8e3b38a2015-11-12 09:52:08 +00001144 expect(":");
Davide Italiano246f6812016-07-22 03:36:24 +00001145
George Rimar8ceadb32016-08-17 07:44:19 +00001146 if (skip("AT"))
Rui Ueyama6ad7dfc2016-08-17 18:59:16 +00001147 Cmd->LmaExpr = readParenExpr();
George Rimar630c6172016-07-26 18:06:29 +00001148 if (skip("ALIGN"))
Rui Ueyama6ad7dfc2016-08-17 18:59:16 +00001149 Cmd->AlignExpr = readParenExpr();
George Rimardb24d9c2016-08-19 15:18:23 +00001150 if (skip("SUBALIGN"))
1151 Cmd->SubalignExpr = readParenExpr();
George Rimar630c6172016-07-26 18:06:29 +00001152
Davide Italiano246f6812016-07-22 03:36:24 +00001153 // Parse constraints.
1154 if (skip("ONLY_IF_RO"))
Rui Ueyamaefc40662016-07-25 22:00:10 +00001155 Cmd->Constraint = ConstraintKind::ReadOnly;
Davide Italiano246f6812016-07-22 03:36:24 +00001156 if (skip("ONLY_IF_RW"))
Rui Ueyamaefc40662016-07-25 22:00:10 +00001157 Cmd->Constraint = ConstraintKind::ReadWrite;
Denis Protivensky8e3b38a2015-11-12 09:52:08 +00001158 expect("{");
Rui Ueyama8ec77e62016-04-21 22:00:51 +00001159
Rui Ueyama025d59b2016-02-02 20:27:59 +00001160 while (!Error && !skip("}")) {
Eugene Leviantceabe802016-08-11 07:56:43 +00001161 StringRef Tok = next();
Eugene Leviantdb741e72016-09-07 07:08:43 +00001162 if (SymbolAssignment *Assignment = readProvideOrAssignment(Tok, false))
Eugene Leviantceabe802016-08-11 07:56:43 +00001163 Cmd->Commands.emplace_back(Assignment);
George Rimarff1f29e2016-09-06 13:51:57 +00001164 else if (Tok == "FILL")
1165 Cmd->Filler = readFill();
Eugene Leviantceabe802016-08-11 07:56:43 +00001166 else if (Tok == "SORT")
George Rimar03fc0102016-07-28 07:18:23 +00001167 readSort();
George Rimara2496cb2016-08-30 09:46:59 +00001168 else if (peek() == "(")
1169 Cmd->Commands.emplace_back(readInputSectionDescription(Tok));
Eugene Leviantceabe802016-08-11 07:56:43 +00001170 else
1171 setError("unknown command " + Tok);
Denis Protivensky8e3b38a2015-11-12 09:52:08 +00001172 }
George Rimar076fe152016-07-21 06:43:01 +00001173 Cmd->Phdrs = readOutputSectionPhdrs();
George Rimarff1f29e2016-09-06 13:51:57 +00001174 if (peek().startswith("="))
1175 Cmd->Filler = readOutputSectionFiller(next().drop_front());
Rui Ueyama10416562016-08-04 02:03:27 +00001176 return Cmd;
Rui Ueyamaf71caa22016-07-29 06:14:07 +00001177}
Rui Ueyama8ec77e62016-04-21 22:00:51 +00001178
Rui Ueyama2c8f1f02016-08-29 22:01:21 +00001179// Read "=<number>" where <number> is an octal/decimal/hexadecimal number.
1180// https://sourceware.org/binutils/docs/ld/Output-Section-Fill.html
1181//
1182// ld.gold is not fully compatible with ld.bfd. ld.bfd handles
1183// hexstrings as blobs of arbitrary sizes, while ld.gold handles them
1184// as 32-bit big-endian values. We will do the same as ld.gold does
1185// because it's simpler than what ld.bfd does.
George Rimarff1f29e2016-09-06 13:51:57 +00001186std::vector<uint8_t> ScriptParser::readOutputSectionFiller(StringRef Tok) {
Rui Ueyama965827d2016-08-03 23:25:15 +00001187 uint32_t V;
George Rimarff1f29e2016-09-06 13:51:57 +00001188 if (Tok.getAsInteger(0, V)) {
Rui Ueyama965827d2016-08-03 23:25:15 +00001189 setError("invalid filler expression: " + Tok);
Rui Ueyamaf71caa22016-07-29 06:14:07 +00001190 return {};
George Rimare2ee72b2016-02-26 14:48:31 +00001191 }
Rui Ueyama2c8f1f02016-08-29 22:01:21 +00001192 return {uint8_t(V >> 24), uint8_t(V >> 16), uint8_t(V >> 8), uint8_t(V)};
Denis Protivensky8e3b38a2015-11-12 09:52:08 +00001193}
1194
Petr Hoseka35e39c2016-08-16 01:11:16 +00001195SymbolAssignment *ScriptParser::readProvideHidden(bool Provide, bool Hidden) {
Eugene Levianta31c91b2016-07-22 07:38:40 +00001196 expect("(");
Rui Ueyama174e0a12016-07-29 00:29:25 +00001197 SymbolAssignment *Cmd = readAssignment(next());
Petr Hoseka35e39c2016-08-16 01:11:16 +00001198 Cmd->Provide = Provide;
Rui Ueyama174e0a12016-07-29 00:29:25 +00001199 Cmd->Hidden = Hidden;
Eugene Levianta31c91b2016-07-22 07:38:40 +00001200 expect(")");
1201 expect(";");
Rui Ueyama10416562016-08-04 02:03:27 +00001202 return Cmd;
Eugene Levianteda81a12016-07-12 06:39:48 +00001203}
1204
Eugene Leviantdb741e72016-09-07 07:08:43 +00001205SymbolAssignment *ScriptParser::readProvideOrAssignment(StringRef Tok,
1206 bool MakeAbsolute) {
Eugene Leviantceabe802016-08-11 07:56:43 +00001207 SymbolAssignment *Cmd = nullptr;
1208 if (peek() == "=" || peek() == "+=") {
1209 Cmd = readAssignment(Tok);
1210 expect(";");
1211 } else if (Tok == "PROVIDE") {
Petr Hoseka35e39c2016-08-16 01:11:16 +00001212 Cmd = readProvideHidden(true, false);
1213 } else if (Tok == "HIDDEN") {
1214 Cmd = readProvideHidden(false, true);
Eugene Leviantceabe802016-08-11 07:56:43 +00001215 } else if (Tok == "PROVIDE_HIDDEN") {
Petr Hoseka35e39c2016-08-16 01:11:16 +00001216 Cmd = readProvideHidden(true, true);
Eugene Leviantceabe802016-08-11 07:56:43 +00001217 }
Eugene Leviantdb741e72016-09-07 07:08:43 +00001218 if (Cmd && MakeAbsolute)
1219 Cmd->IsAbsolute = true;
Eugene Leviantceabe802016-08-11 07:56:43 +00001220 return Cmd;
1221}
1222
George Rimar30835ea2016-07-28 21:08:56 +00001223static uint64_t getSymbolValue(StringRef S, uint64_t Dot) {
1224 if (S == ".")
1225 return Dot;
George Rimar884e7862016-09-08 08:19:13 +00001226 return ScriptBase->getSymbolValue(S);
George Rimare32a3592016-08-10 07:59:34 +00001227}
1228
George Rimar30835ea2016-07-28 21:08:56 +00001229SymbolAssignment *ScriptParser::readAssignment(StringRef Name) {
1230 StringRef Op = next();
Eugene Leviantdb741e72016-09-07 07:08:43 +00001231 bool IsAbsolute = false;
1232 Expr E;
George Rimar30835ea2016-07-28 21:08:56 +00001233 assert(Op == "=" || Op == "+=");
Eugene Leviantdb741e72016-09-07 07:08:43 +00001234 if (skip("ABSOLUTE")) {
1235 E = readParenExpr();
1236 IsAbsolute = true;
1237 } else {
1238 E = readExpr();
1239 }
George Rimar30835ea2016-07-28 21:08:56 +00001240 if (Op == "+=")
1241 E = [=](uint64_t Dot) { return getSymbolValue(Name, Dot) + E(Dot); };
Eugene Leviantdb741e72016-09-07 07:08:43 +00001242 return new SymbolAssignment(Name, E, IsAbsolute);
George Rimar30835ea2016-07-28 21:08:56 +00001243}
1244
1245// This is an operator-precedence parser to parse a linker
1246// script expression.
1247Expr ScriptParser::readExpr() { return readExpr1(readPrimary(), 0); }
1248
Rui Ueyama36c1cd22016-08-05 01:04:59 +00001249static Expr combine(StringRef Op, Expr L, Expr R) {
1250 if (Op == "*")
1251 return [=](uint64_t Dot) { return L(Dot) * R(Dot); };
1252 if (Op == "/") {
1253 return [=](uint64_t Dot) -> uint64_t {
1254 uint64_t RHS = R(Dot);
1255 if (RHS == 0) {
1256 error("division by zero");
1257 return 0;
1258 }
1259 return L(Dot) / RHS;
1260 };
1261 }
1262 if (Op == "+")
1263 return [=](uint64_t Dot) { return L(Dot) + R(Dot); };
1264 if (Op == "-")
1265 return [=](uint64_t Dot) { return L(Dot) - R(Dot); };
1266 if (Op == "<")
1267 return [=](uint64_t Dot) { return L(Dot) < R(Dot); };
1268 if (Op == ">")
1269 return [=](uint64_t Dot) { return L(Dot) > R(Dot); };
1270 if (Op == ">=")
1271 return [=](uint64_t Dot) { return L(Dot) >= R(Dot); };
1272 if (Op == "<=")
1273 return [=](uint64_t Dot) { return L(Dot) <= R(Dot); };
1274 if (Op == "==")
1275 return [=](uint64_t Dot) { return L(Dot) == R(Dot); };
1276 if (Op == "!=")
1277 return [=](uint64_t Dot) { return L(Dot) != R(Dot); };
1278 if (Op == "&")
1279 return [=](uint64_t Dot) { return L(Dot) & R(Dot); };
Rafael Espindolacc3dd622016-08-22 21:33:35 +00001280 if (Op == "|")
1281 return [=](uint64_t Dot) { return L(Dot) | R(Dot); };
Rui Ueyama36c1cd22016-08-05 01:04:59 +00001282 llvm_unreachable("invalid operator");
1283}
1284
Rui Ueyama708019c2016-07-24 18:19:40 +00001285// This is a part of the operator-precedence parser. This function
1286// assumes that the remaining token stream starts with an operator.
1287Expr ScriptParser::readExpr1(Expr Lhs, int MinPrec) {
1288 while (!atEOF() && !Error) {
1289 // Read an operator and an expression.
1290 StringRef Op1 = peek();
1291 if (Op1 == "?")
1292 return readTernary(Lhs);
1293 if (precedence(Op1) < MinPrec)
Eugene Levianteda81a12016-07-12 06:39:48 +00001294 break;
Rui Ueyama708019c2016-07-24 18:19:40 +00001295 next();
1296 Expr Rhs = readPrimary();
1297
1298 // Evaluate the remaining part of the expression first if the
1299 // next operator has greater precedence than the previous one.
1300 // For example, if we have read "+" and "3", and if the next
1301 // operator is "*", then we'll evaluate 3 * ... part first.
1302 while (!atEOF()) {
1303 StringRef Op2 = peek();
1304 if (precedence(Op2) <= precedence(Op1))
1305 break;
1306 Rhs = readExpr1(Rhs, precedence(Op2));
1307 }
1308
1309 Lhs = combine(Op1, Lhs, Rhs);
Eugene Levianteda81a12016-07-12 06:39:48 +00001310 }
Rui Ueyama708019c2016-07-24 18:19:40 +00001311 return Lhs;
1312}
1313
1314uint64_t static getConstant(StringRef S) {
Michael J. Spencere2cc07b2016-08-17 02:10:51 +00001315 if (S == "COMMONPAGESIZE")
Rui Ueyama708019c2016-07-24 18:19:40 +00001316 return Target->PageSize;
Michael J. Spencere2cc07b2016-08-17 02:10:51 +00001317 if (S == "MAXPAGESIZE")
1318 return Target->MaxPageSize;
Rui Ueyama708019c2016-07-24 18:19:40 +00001319 error("unknown constant: " + S);
1320 return 0;
1321}
1322
Rui Ueyama626e0b02016-09-02 18:19:00 +00001323// Parses Tok as an integer. Returns true if successful.
1324// It recognizes hexadecimal (prefixed with "0x" or suffixed with "H")
1325// and decimal numbers. Decimal numbers may have "K" (kilo) or
1326// "M" (mega) prefixes.
George Rimar9f2f7ad2016-09-02 16:01:42 +00001327static bool readInteger(StringRef Tok, uint64_t &Result) {
Simon Atanasyaneaeafb22016-09-02 21:54:35 +00001328 if (Tok.startswith("-")) {
1329 if (!readInteger(Tok.substr(1), Result))
1330 return false;
1331 Result = -Result;
1332 return true;
1333 }
George Rimar9f2f7ad2016-09-02 16:01:42 +00001334 if (Tok.startswith_lower("0x"))
1335 return !Tok.substr(2).getAsInteger(16, Result);
1336 if (Tok.endswith_lower("H"))
1337 return !Tok.drop_back().getAsInteger(16, Result);
1338
1339 int Suffix = 1;
1340 if (Tok.endswith_lower("K")) {
1341 Suffix = 1024;
1342 Tok = Tok.drop_back();
1343 } else if (Tok.endswith_lower("M")) {
1344 Suffix = 1024 * 1024;
1345 Tok = Tok.drop_back();
1346 }
1347 if (Tok.getAsInteger(10, Result))
1348 return false;
1349 Result *= Suffix;
1350 return true;
1351}
1352
Rui Ueyama708019c2016-07-24 18:19:40 +00001353Expr ScriptParser::readPrimary() {
Rui Ueyama6ad7dfc2016-08-17 18:59:16 +00001354 if (peek() == "(")
1355 return readParenExpr();
Rui Ueyama708019c2016-07-24 18:19:40 +00001356
Rui Ueyama6ad7dfc2016-08-17 18:59:16 +00001357 StringRef Tok = next();
Rui Ueyama708019c2016-07-24 18:19:40 +00001358
Simon Atanasyaneaeafb22016-09-02 21:54:35 +00001359 if (Tok == "~") {
1360 Expr E = readPrimary();
1361 return [=](uint64_t Dot) { return ~E(Dot); };
1362 }
1363 if (Tok == "-") {
1364 Expr E = readPrimary();
1365 return [=](uint64_t Dot) { return -E(Dot); };
1366 }
1367
Rui Ueyama708019c2016-07-24 18:19:40 +00001368 // Built-in functions are parsed here.
1369 // https://sourceware.org/binutils/docs/ld/Builtin-Functions.html.
George Rimar96659df2016-08-30 09:54:01 +00001370 if (Tok == "ADDR") {
1371 expect("(");
1372 StringRef Name = next();
1373 expect(")");
George Rimar884e7862016-09-08 08:19:13 +00001374 return
1375 [=](uint64_t Dot) { return ScriptBase->getOutputSectionAddress(Name); };
George Rimar96659df2016-08-30 09:54:01 +00001376 }
George Rimareefa7582016-08-04 09:29:31 +00001377 if (Tok == "ASSERT")
1378 return readAssert();
Rui Ueyama708019c2016-07-24 18:19:40 +00001379 if (Tok == "ALIGN") {
Rui Ueyama6ad7dfc2016-08-17 18:59:16 +00001380 Expr E = readParenExpr();
Rui Ueyama708019c2016-07-24 18:19:40 +00001381 return [=](uint64_t Dot) { return alignTo(Dot, E(Dot)); };
1382 }
1383 if (Tok == "CONSTANT") {
1384 expect("(");
1385 StringRef Tok = next();
1386 expect(")");
1387 return [=](uint64_t Dot) { return getConstant(Tok); };
1388 }
Rafael Espindola54c145c2016-07-28 18:16:24 +00001389 if (Tok == "SEGMENT_START") {
1390 expect("(");
1391 next();
1392 expect(",");
1393 uint64_t Val;
Rafael Espindola3adbbc32016-09-15 13:36:44 +00001394 if (next().getAsInteger(0, Val))
1395 setError("integer expected");
Rafael Espindola54c145c2016-07-28 18:16:24 +00001396 expect(")");
1397 return [=](uint64_t Dot) { return Val; };
1398 }
Rui Ueyama708019c2016-07-24 18:19:40 +00001399 if (Tok == "DATA_SEGMENT_ALIGN") {
1400 expect("(");
1401 Expr E = readExpr();
1402 expect(",");
1403 readExpr();
1404 expect(")");
Rui Ueyamaf7791bb2016-07-26 19:34:10 +00001405 return [=](uint64_t Dot) { return alignTo(Dot, E(Dot)); };
Rui Ueyama708019c2016-07-24 18:19:40 +00001406 }
1407 if (Tok == "DATA_SEGMENT_END") {
1408 expect("(");
1409 expect(".");
1410 expect(")");
1411 return [](uint64_t Dot) { return Dot; };
1412 }
George Rimar276b4e62016-07-26 17:58:44 +00001413 // GNU linkers implements more complicated logic to handle
1414 // DATA_SEGMENT_RELRO_END. We instead ignore the arguments and just align to
1415 // the next page boundary for simplicity.
1416 if (Tok == "DATA_SEGMENT_RELRO_END") {
1417 expect("(");
Rafael Espindola97bdc722016-09-14 19:14:01 +00001418 readExpr();
George Rimar276b4e62016-07-26 17:58:44 +00001419 expect(",");
1420 readExpr();
1421 expect(")");
1422 return [](uint64_t Dot) { return alignTo(Dot, Target->PageSize); };
1423 }
George Rimar9e694502016-07-29 16:18:47 +00001424 if (Tok == "SIZEOF") {
1425 expect("(");
1426 StringRef Name = next();
1427 expect(")");
George Rimar884e7862016-09-08 08:19:13 +00001428 return [=](uint64_t Dot) { return ScriptBase->getOutputSectionSize(Name); };
George Rimar9e694502016-07-29 16:18:47 +00001429 }
Eugene Leviant36fac7f2016-09-08 09:08:30 +00001430 if (Tok == "ALIGNOF") {
1431 expect("(");
1432 StringRef Name = next();
1433 expect(")");
1434 return
1435 [=](uint64_t Dot) { return ScriptBase->getOutputSectionAlign(Name); };
1436 }
George Rimare32a3592016-08-10 07:59:34 +00001437 if (Tok == "SIZEOF_HEADERS")
George Rimar884e7862016-09-08 08:19:13 +00001438 return [=](uint64_t Dot) { return ScriptBase->getHeaderSize(); };
Rui Ueyama708019c2016-07-24 18:19:40 +00001439
George Rimar9f2f7ad2016-09-02 16:01:42 +00001440 // Tok is a literal number.
1441 uint64_t V;
1442 if (readInteger(Tok, V))
1443 return [=](uint64_t Dot) { return V; };
1444
1445 // Tok is a symbol name.
1446 if (Tok != "." && !isValidCIdentifier(Tok))
1447 setError("malformed number: " + Tok);
1448 return [=](uint64_t Dot) { return getSymbolValue(Tok, Dot); };
Rui Ueyama708019c2016-07-24 18:19:40 +00001449}
1450
1451Expr ScriptParser::readTernary(Expr Cond) {
1452 next();
1453 Expr L = readExpr();
1454 expect(":");
1455 Expr R = readExpr();
1456 return [=](uint64_t Dot) { return Cond(Dot) ? L(Dot) : R(Dot); };
1457}
1458
Rui Ueyama6ad7dfc2016-08-17 18:59:16 +00001459Expr ScriptParser::readParenExpr() {
1460 expect("(");
1461 Expr E = readExpr();
1462 expect(")");
1463 return E;
1464}
1465
Eugene Leviantbbe38602016-07-19 09:25:43 +00001466std::vector<StringRef> ScriptParser::readOutputSectionPhdrs() {
1467 std::vector<StringRef> Phdrs;
1468 while (!Error && peek().startswith(":")) {
1469 StringRef Tok = next();
1470 Tok = (Tok.size() == 1) ? next() : Tok.substr(1);
1471 if (Tok.empty()) {
1472 setError("section header name is empty");
1473 break;
1474 }
Rui Ueyama047404f2016-07-20 19:36:36 +00001475 Phdrs.push_back(Tok);
Eugene Leviantbbe38602016-07-19 09:25:43 +00001476 }
1477 return Phdrs;
1478}
1479
1480unsigned ScriptParser::readPhdrType() {
Eugene Leviantbbe38602016-07-19 09:25:43 +00001481 StringRef Tok = next();
Rui Ueyamab0f6c592016-07-20 19:36:38 +00001482 unsigned Ret = StringSwitch<unsigned>(Tok)
George Rimar6c55f0e2016-09-08 08:20:30 +00001483 .Case("PT_NULL", PT_NULL)
1484 .Case("PT_LOAD", PT_LOAD)
1485 .Case("PT_DYNAMIC", PT_DYNAMIC)
1486 .Case("PT_INTERP", PT_INTERP)
1487 .Case("PT_NOTE", PT_NOTE)
1488 .Case("PT_SHLIB", PT_SHLIB)
1489 .Case("PT_PHDR", PT_PHDR)
1490 .Case("PT_TLS", PT_TLS)
1491 .Case("PT_GNU_EH_FRAME", PT_GNU_EH_FRAME)
1492 .Case("PT_GNU_STACK", PT_GNU_STACK)
1493 .Case("PT_GNU_RELRO", PT_GNU_RELRO)
1494 .Default(-1);
Eugene Leviantbbe38602016-07-19 09:25:43 +00001495
Rui Ueyamab0f6c592016-07-20 19:36:38 +00001496 if (Ret == (unsigned)-1) {
1497 setError("invalid program header type: " + Tok);
1498 return PT_NULL;
1499 }
1500 return Ret;
Eugene Leviantbbe38602016-07-19 09:25:43 +00001501}
1502
Rui Ueyama95769b42016-08-31 20:03:54 +00001503void ScriptParser::readVersionDeclaration(StringRef VerStr) {
George Rimar20b65982016-08-31 09:08:26 +00001504 // Identifiers start at 2 because 0 and 1 are reserved
1505 // for VER_NDX_LOCAL and VER_NDX_GLOBAL constants.
1506 size_t VersionId = Config->VersionDefinitions.size() + 2;
1507 Config->VersionDefinitions.push_back({VerStr, VersionId});
1508
1509 if (skip("global:") || peek() != "local:")
1510 readGlobal(VerStr);
1511 if (skip("local:"))
1512 readLocal();
1513 expect("}");
1514
1515 // Each version may have a parent version. For example, "Ver2" defined as
1516 // "Ver2 { global: foo; local: *; } Ver1;" has "Ver1" as a parent. This
1517 // version hierarchy is, probably against your instinct, purely for human; the
1518 // runtime doesn't care about them at all. In LLD, we simply skip the token.
1519 if (!VerStr.empty() && peek() != ";")
1520 next();
1521 expect(";");
1522}
1523
1524void ScriptParser::readLocal() {
1525 Config->DefaultSymbolVersion = VER_NDX_LOCAL;
1526 expect("*");
1527 expect(";");
1528}
1529
1530void ScriptParser::readExtern(std::vector<SymbolVersion> *Globals) {
George Rimarcd574a52016-09-09 14:35:36 +00001531 expect("\"C++\"");
George Rimar20b65982016-08-31 09:08:26 +00001532 expect("{");
1533
1534 for (;;) {
1535 if (peek() == "}" || Error)
1536 break;
George Rimarcd574a52016-09-09 14:35:36 +00001537 bool HasWildcard = !peek().startswith("\"") && hasWildcard(peek());
1538 Globals->push_back({unquote(next()), true, HasWildcard});
George Rimar20b65982016-08-31 09:08:26 +00001539 expect(";");
1540 }
1541
1542 expect("}");
1543 expect(";");
1544}
1545
1546void ScriptParser::readGlobal(StringRef VerStr) {
1547 std::vector<SymbolVersion> *Globals;
1548 if (VerStr.empty())
1549 Globals = &Config->VersionScriptGlobals;
1550 else
1551 Globals = &Config->VersionDefinitions.back().Globals;
1552
1553 for (;;) {
1554 if (skip("extern"))
1555 readExtern(Globals);
1556
1557 StringRef Cur = peek();
1558 if (Cur == "}" || Cur == "local:" || Error)
1559 return;
1560 next();
George Rimarcd574a52016-09-09 14:35:36 +00001561 Globals->push_back({unquote(Cur), false, hasWildcard(Cur)});
George Rimar20b65982016-08-31 09:08:26 +00001562 expect(";");
1563 }
1564}
1565
Simon Atanasyan16b0cc92015-11-26 05:53:00 +00001566static bool isUnderSysroot(StringRef Path) {
1567 if (Config->Sysroot == "")
1568 return false;
1569 for (; !Path.empty(); Path = sys::path::parent_path(Path))
1570 if (sys::fs::equivalent(Config->Sysroot, Path))
1571 return true;
1572 return false;
1573}
1574
Rui Ueyama07320e42016-04-20 20:13:41 +00001575void elf::readLinkerScript(MemoryBufferRef MB) {
Simon Atanasyan16b0cc92015-11-26 05:53:00 +00001576 StringRef Path = MB.getBufferIdentifier();
George Rimar20b65982016-08-31 09:08:26 +00001577 ScriptParser(MB.getBuffer(), isUnderSysroot(Path)).readLinkerScript();
1578}
1579
1580void elf::readVersionScript(MemoryBufferRef MB) {
1581 ScriptParser(MB.getBuffer(), false).readVersionScript();
Rui Ueyamaf7c5fbb2015-09-30 17:23:26 +00001582}
Rui Ueyama1ebc8ed2016-02-12 21:47:28 +00001583
Rui Ueyama07320e42016-04-20 20:13:41 +00001584template class elf::LinkerScript<ELF32LE>;
1585template class elf::LinkerScript<ELF32BE>;
1586template class elf::LinkerScript<ELF64LE>;
1587template class elf::LinkerScript<ELF64BE>;