blob: 066c4eb0f8d2de2cd2ed993795a1a4946202133f [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 Espindolae71a3f8a2016-09-16 20:34:02 +0000155static bool matchConstraints(ArrayRef<InputSectionBase<ELFT> *> 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 Espindolae71a3f8a2016-09-16 20:34:02 +0000167void LinkerScript<ELFT>::computeInputSections(InputSectionDescription *I) {
George Rimar395281c2016-09-16 17:42:10 +0000168 for (const std::pair<llvm::Regex, llvm::Regex> &V : I->SectionsVec) {
169 for (ObjectFile<ELFT> *F : Symtab<ELFT>::X->getObjectFiles()) {
170 if (fileMatches(I->FileRe, V.first, sys::path::filename(F->getName()))) {
171 Regex &Re = const_cast<Regex &>(V.second);
172 for (InputSectionBase<ELFT> *S : F->getSections())
173 if (!isDiscarded(S) && !S->OutSec && Re.match(S->Name))
174 I->Sections.push_back(S);
Rafael Espindolabe94e1b2016-09-14 14:32:08 +0000175
George Rimar395281c2016-09-16 17:42:10 +0000176 if (Re.match("COMMON"))
177 I->Sections.push_back(CommonInputSection<ELFT>::X);
178 }
179 }
180 }
Rafael Espindolad3190792016-09-16 15:10:23 +0000181
Rui Ueyamab2a0abd2016-09-16 21:14:55 +0000182 if (I->SortInner != SortSectionPolicy::Default)
Rafael Espindolad3190792016-09-16 15:10:23 +0000183 std::stable_sort(I->Sections.begin(), I->Sections.end(),
184 getComparator(I->SortInner));
Rui Ueyamab2a0abd2016-09-16 21:14:55 +0000185 if (I->SortOuter != SortSectionPolicy::Default)
Rafael Espindolad3190792016-09-16 15:10:23 +0000186 std::stable_sort(I->Sections.begin(), I->Sections.end(),
187 getComparator(I->SortOuter));
188
189 // We do not add duplicate input sections, so mark them with a dummy output
190 // section for now.
191 for (InputSectionData *S : I->Sections) {
192 auto *S2 = static_cast<InputSectionBase<ELFT> *>(S);
193 S2->OutSec = (OutputSectionBase<ELFT> *)-1;
194 }
Rafael Espindolabe94e1b2016-09-14 14:32:08 +0000195}
196
197template <class ELFT>
198void LinkerScript<ELFT>::discard(ArrayRef<InputSectionBase<ELFT> *> V) {
199 for (InputSectionBase<ELFT> *S : V) {
200 S->Live = false;
201 reportDiscarded(S);
202 }
203}
204
George Rimar06ae6832016-08-12 09:07:57 +0000205template <class ELFT>
Rui Ueyama0b9ce6a2016-08-12 03:16:56 +0000206std::vector<InputSectionBase<ELFT> *>
George Rimar06ae6832016-08-12 09:07:57 +0000207LinkerScript<ELFT>::createInputSectionList(OutputSectionCommand &OutCmd) {
Rui Ueyama0b9ce6a2016-08-12 03:16:56 +0000208 std::vector<InputSectionBase<ELFT> *> Ret;
Rui Ueyamae7f912c2016-08-03 21:12:09 +0000209
George Rimar06ae6832016-08-12 09:07:57 +0000210 for (const std::unique_ptr<BaseCommand> &Base : OutCmd.Commands) {
Rafael Espindola7c3ff2e2016-09-16 21:05:36 +0000211 auto *Cmd = dyn_cast<InputSectionDescription>(Base.get());
212 if (!Cmd)
Rui Ueyama0b9ce6a2016-08-12 03:16:56 +0000213 continue;
Rafael Espindolae71a3f8a2016-09-16 20:34:02 +0000214 computeInputSections(Cmd);
Rafael Espindolad3190792016-09-16 15:10:23 +0000215 for (InputSectionData *S : Cmd->Sections)
216 Ret.push_back(static_cast<InputSectionBase<ELFT> *>(S));
Rui Ueyama0b9ce6a2016-08-12 03:16:56 +0000217 }
Rafael Espindolae71a3f8a2016-09-16 20:34:02 +0000218
Rui Ueyama0b9ce6a2016-08-12 03:16:56 +0000219 return Ret;
220}
221
Petr Hoseke5d3ca52016-08-31 15:31:17 +0000222template <class ELFT>
Rafael Espindola10897f12016-09-13 14:23:14 +0000223static SectionKey<ELFT::Is64Bits> createKey(InputSectionBase<ELFT> *C,
224 StringRef OutsecName) {
225 // When using linker script the merge rules are different.
226 // Unfortunately, linker scripts are name based. This means that expressions
227 // like *(.foo*) can refer to multiple input sections that would normally be
228 // placed in different output sections. We cannot put them in different
229 // output sections or we would produce wrong results for
230 // start = .; *(.foo.*) end = .; *(.bar)
231 // and a mapping of .foo1 and .bar1 to one section and .foo2 and .bar2 to
232 // another. The problem is that there is no way to layout those output
233 // sections such that the .foo sections are the only thing between the
234 // start and end symbols.
235
236 // An extra annoyance is that we cannot simply disable merging of the contents
237 // of SHF_MERGE sections, but our implementation requires one output section
238 // per "kind" (string or not, which size/aligment).
239 // Fortunately, creating symbols in the middle of a merge section is not
240 // supported by bfd or gold, so we can just create multiple section in that
241 // case.
242 const typename ELFT::Shdr *H = C->getSectionHdr();
243 typedef typename ELFT::uint uintX_t;
244 uintX_t Flags = H->sh_flags & (SHF_MERGE | SHF_STRINGS);
245
246 uintX_t Alignment = 0;
247 if (isa<MergeInputSection<ELFT>>(C))
248 Alignment = std::max(H->sh_addralign, H->sh_entsize);
249
250 return SectionKey<ELFT::Is64Bits>{OutsecName, /*Type*/ 0, Flags, Alignment};
251}
252
253template <class ELFT>
Eugene Leviant20d03192016-09-16 15:30:47 +0000254void LinkerScript<ELFT>::addSection(OutputSectionFactory<ELFT> &Factory,
255 InputSectionBase<ELFT> *Sec,
256 StringRef Name) {
257 OutputSectionBase<ELFT> *OutSec;
258 bool IsNew;
259 std::tie(OutSec, IsNew) = Factory.create(createKey(Sec, Name), Sec);
260 if (IsNew)
261 OutputSections->push_back(OutSec);
262 OutSec->addSection(Sec);
263}
264
265template <class ELFT>
266void LinkerScript<ELFT>::processCommands(OutputSectionFactory<ELFT> &Factory) {
Rafael Espindola28c15972016-09-13 13:00:06 +0000267
Rafael Espindola7c3ff2e2016-09-16 21:05:36 +0000268 for (unsigned I = 0; I < Opt.Commands.size(); ++I) {
269 auto Iter = Opt.Commands.begin() + I;
270 const std::unique_ptr<BaseCommand> &Base1 = *Iter;
Rui Ueyama2ab5f732016-08-12 03:33:04 +0000271 if (auto *Cmd = dyn_cast<SymbolAssignment>(Base1.get())) {
272 if (shouldDefine<ELFT>(Cmd))
273 addRegular<ELFT>(Cmd);
274 continue;
275 }
Eugene Leviant20d03192016-09-16 15:30:47 +0000276 if (auto *Cmd = dyn_cast<AssertCommand>(Base1.get())) {
277 // If we don't have SECTIONS then output sections have already been
278 // created by Writer<EFLT>. The LinkerScript<ELFT>::assignAddresses
279 // will not be called, so ASSERT should be evaluated now.
280 if (!Opt.HasSections)
281 Cmd->Expression(0);
282 continue;
283 }
Rui Ueyama2ab5f732016-08-12 03:33:04 +0000284
Eugene Leviantceabe802016-08-11 07:56:43 +0000285 if (auto *Cmd = dyn_cast<OutputSectionCommand>(Base1.get())) {
Rafael Espindola7bd37872016-09-12 16:05:16 +0000286 std::vector<InputSectionBase<ELFT> *> V = createInputSectionList(*Cmd);
287
Rui Ueyama48c3f1c2016-08-12 00:27:23 +0000288 if (Cmd->Name == "/DISCARD/") {
Rafael Espindola7bd37872016-09-12 16:05:16 +0000289 discard(V);
Rui Ueyama48c3f1c2016-08-12 00:27:23 +0000290 continue;
291 }
Eugene Leviantceabe802016-08-11 07:56:43 +0000292
Rafael Espindola7c3ff2e2016-09-16 21:05:36 +0000293 if (!matchConstraints<ELFT>(V, Cmd->Constraint)) {
294 for (InputSectionBase<ELFT> *S : V)
295 S->OutSec = nullptr;
296 Opt.Commands.erase(Iter);
297 continue;
298 }
299
300 for (const std::unique_ptr<BaseCommand> &Base : Cmd->Commands)
301 if (auto *OutCmd = dyn_cast<SymbolAssignment>(Base.get()))
302 if (shouldDefine<ELFT>(OutCmd))
303 addSymbol<ELFT>(OutCmd);
304
Eugene Leviant97403d12016-09-01 09:55:57 +0000305 if (V.empty())
Rui Ueyama0b9ce6a2016-08-12 03:16:56 +0000306 continue;
307
George Rimardb24d9c2016-08-19 15:18:23 +0000308 for (InputSectionBase<ELFT> *Sec : V) {
Eugene Leviant20d03192016-09-16 15:30:47 +0000309 addSection(Factory, Sec, Cmd->Name);
310 if (uint32_t Subalign = Cmd->SubalignExpr ? Cmd->SubalignExpr(0) : 0)
George Rimardb24d9c2016-08-19 15:18:23 +0000311 Sec->Alignment = Subalign;
George Rimardb24d9c2016-08-19 15:18:23 +0000312 }
Eugene Leviantceabe802016-08-11 07:56:43 +0000313 }
Rui Ueyama48c3f1c2016-08-12 00:27:23 +0000314 }
Eugene Leviant20d03192016-09-16 15:30:47 +0000315}
Eugene Leviante63d81b2016-07-20 14:43:20 +0000316
Eugene Leviant20d03192016-09-16 15:30:47 +0000317template <class ELFT>
318void LinkerScript<ELFT>::createSections(OutputSectionFactory<ELFT> &Factory) {
319 processCommands(Factory);
Rui Ueyama0b9ce6a2016-08-12 03:16:56 +0000320 // Add orphan sections.
Eugene Leviant20d03192016-09-16 15:30:47 +0000321 for (ObjectFile<ELFT> *F : Symtab<ELFT>::X->getObjectFiles())
322 for (InputSectionBase<ELFT> *S : F->getSections())
323 if (!isDiscarded(S) && !S->OutSec)
324 addSection(Factory, S, getOutputSectionName(S));
Eugene Leviante63d81b2016-07-20 14:43:20 +0000325}
326
Eugene Leviantdb741e72016-09-07 07:08:43 +0000327// Sets value of a section-defined symbol. Two kinds of
328// symbols are processed: synthetic symbols, whose value
329// is an offset from beginning of section and regular
330// symbols whose value is absolute.
331template <class ELFT>
332static void assignSectionSymbol(SymbolAssignment *Cmd,
333 OutputSectionBase<ELFT> *Sec,
334 typename ELFT::uint Off) {
335 if (!Cmd->Sym)
336 return;
337
338 if (auto *Body = dyn_cast<DefinedSynthetic<ELFT>>(Cmd->Sym)) {
339 Body->Section = Sec;
340 Body->Value = Cmd->Expression(Sec->getVA() + Off) - Sec->getVA();
341 return;
342 }
343 auto *Body = cast<DefinedRegular<ELFT>>(Cmd->Sym);
344 Body->Value = Cmd->Expression(Sec->getVA() + Off);
345}
346
Rafael Espindolad3190792016-09-16 15:10:23 +0000347template <class ELFT> void LinkerScript<ELFT>::output(InputSection<ELFT> *S) {
348 if (!AlreadyOutputIS.insert(S).second)
349 return;
350 bool IsTbss =
351 (CurOutSec->getFlags() & SHF_TLS) && CurOutSec->getType() == SHT_NOBITS;
Eugene Leviant20889c52016-08-31 08:13:33 +0000352
Rafael Espindolad3190792016-09-16 15:10:23 +0000353 uintX_t Pos = IsTbss ? Dot + ThreadBssOffset : Dot;
354 Pos = alignTo(Pos, S->Alignment);
355 S->OutSecOff = Pos - CurOutSec->getVA();
356 Pos += S->getSize();
357
358 // Update output section size after adding each section. This is so that
359 // SIZEOF works correctly in the case below:
360 // .foo { *(.aaa) a = SIZEOF(.foo); *(.bbb) }
361 CurOutSec->setSize(Pos - CurOutSec->getVA());
362
363 if (!IsTbss)
364 Dot = Pos;
365}
366
367template <class ELFT> void LinkerScript<ELFT>::flush() {
368 if (auto *OutSec = dyn_cast_or_null<OutputSection<ELFT>>(CurOutSec)) {
369 for (InputSection<ELFT> *I : OutSec->Sections)
370 output(I);
371 AlreadyOutputOS.insert(CurOutSec);
Eugene Leviant20889c52016-08-31 08:13:33 +0000372 }
373}
374
375template <class ELFT>
Rafael Espindolad3190792016-09-16 15:10:23 +0000376void LinkerScript<ELFT>::switchTo(OutputSectionBase<ELFT> *Sec) {
377 if (CurOutSec == Sec)
378 return;
379 if (AlreadyOutputOS.count(Sec))
380 return;
381
382 flush();
383 CurOutSec = Sec;
384
385 Dot = alignTo(Dot, CurOutSec->getAlignment());
386 CurOutSec->setVA(Dot);
387}
388
389template <class ELFT> void LinkerScript<ELFT>::process(BaseCommand &Base) {
390 if (auto *AssignCmd = dyn_cast<SymbolAssignment>(&Base)) {
391 if (AssignCmd->Name == ".") {
392 // Update to location counter means update to section size.
393 Dot = AssignCmd->Expression(Dot);
394 CurOutSec->setSize(Dot - CurOutSec->getVA());
395 return;
396 }
397 assignSectionSymbol<ELFT>(AssignCmd, CurOutSec, Dot - CurOutSec->getVA());
Eugene Leviantceabe802016-08-11 07:56:43 +0000398 return;
Rui Ueyama2de509c2016-08-12 00:55:08 +0000399 }
Rafael Espindolad3190792016-09-16 15:10:23 +0000400 auto &ICmd = cast<InputSectionDescription>(Base);
401 for (InputSectionData *ID : ICmd.Sections) {
402 auto *IB = static_cast<InputSectionBase<ELFT> *>(ID);
403 switchTo(IB->OutSec);
404 if (auto *I = dyn_cast<InputSection<ELFT>>(IB))
405 output(I);
406 else if (AlreadyOutputOS.insert(CurOutSec).second)
407 Dot += CurOutSec->getSize();
Eugene Leviantceabe802016-08-11 07:56:43 +0000408 }
409}
410
George Rimar8f66df92016-08-12 20:38:20 +0000411template <class ELFT>
George Rimara14b13d2016-09-07 10:46:07 +0000412static std::vector<OutputSectionBase<ELFT> *>
413findSections(OutputSectionCommand &Cmd,
Rafael Espindolad3190792016-09-16 15:10:23 +0000414 const std::vector<OutputSectionBase<ELFT> *> &Sections) {
George Rimara14b13d2016-09-07 10:46:07 +0000415 std::vector<OutputSectionBase<ELFT> *> Ret;
416 for (OutputSectionBase<ELFT> *Sec : Sections)
Rafael Espindola7c3ff2e2016-09-16 21:05:36 +0000417 if (Sec->getName() == Cmd.Name)
George Rimara14b13d2016-09-07 10:46:07 +0000418 Ret.push_back(Sec);
419 return Ret;
George Rimar8f66df92016-08-12 20:38:20 +0000420}
421
Rafael Espindolad3190792016-09-16 15:10:23 +0000422template <class ELFT>
423void LinkerScript<ELFT>::assignOffsets(OutputSectionCommand *Cmd) {
424 std::vector<OutputSectionBase<ELFT> *> Sections =
425 findSections(*Cmd, *OutputSections);
426 if (Sections.empty())
427 return;
428 switchTo(Sections[0]);
429
430 // Find the last section output location. We will output orphan sections
431 // there so that end symbols point to the correct location.
432 auto E = std::find_if(Cmd->Commands.rbegin(), Cmd->Commands.rend(),
433 [](const std::unique_ptr<BaseCommand> &Cmd) {
434 return !isa<SymbolAssignment>(*Cmd);
435 })
436 .base();
437 for (auto I = Cmd->Commands.begin(); I != E; ++I)
438 process(**I);
439 flush();
440 for (OutputSectionBase<ELFT> *Base : Sections) {
441 if (!AlreadyOutputOS.insert(Base).second)
442 continue;
443 switchTo(Base);
444 Dot += CurOutSec->getSize();
445 }
446 for (auto I = E, E = Cmd->Commands.end(); I != E; ++I)
447 process(**I);
448}
449
Rafael Espindolaa4b41dc2016-08-04 12:13:05 +0000450template <class ELFT> void LinkerScript<ELFT>::assignAddresses() {
George Rimar652852c2016-04-16 10:10:32 +0000451 // Orphan sections are sections present in the input files which
Rui Ueyama7c18c282016-04-18 21:00:40 +0000452 // are not explicitly placed into the output file by the linker script.
453 // We place orphan sections at end of file.
454 // Other linkers places them using some heuristics as described in
George Rimar652852c2016-04-16 10:10:32 +0000455 // https://sourceware.org/binutils/docs/ld/Orphan-Sections.html#Orphan-Sections.
Rui Ueyamae5cc6682016-08-12 00:36:56 +0000456 for (OutputSectionBase<ELFT> *Sec : *OutputSections) {
George Rimar652852c2016-04-16 10:10:32 +0000457 StringRef Name = Sec->getName();
Rui Ueyamac3e2a4b2016-04-21 20:30:00 +0000458 if (getSectionIndex(Name) == INT_MAX)
George Rimar076fe152016-07-21 06:43:01 +0000459 Opt.Commands.push_back(llvm::make_unique<OutputSectionCommand>(Name));
George Rimar652852c2016-04-16 10:10:32 +0000460 }
George Rimar652852c2016-04-16 10:10:32 +0000461
Rui Ueyama7c18c282016-04-18 21:00:40 +0000462 // Assign addresses as instructed by linker script SECTIONS sub-commands.
Rui Ueyama4f7500b2016-08-12 04:00:22 +0000463 Dot = getHeaderSize();
Eugene Leviant467c4d52016-07-01 10:27:36 +0000464 uintX_t MinVA = std::numeric_limits<uintX_t>::max();
George Rimar652852c2016-04-16 10:10:32 +0000465
George Rimar076fe152016-07-21 06:43:01 +0000466 for (const std::unique_ptr<BaseCommand> &Base : Opt.Commands) {
467 if (auto *Cmd = dyn_cast<SymbolAssignment>(Base.get())) {
Rui Ueyama8d083e62016-07-29 05:48:39 +0000468 if (Cmd->Name == ".") {
469 Dot = Cmd->Expression(Dot);
470 } else if (Cmd->Sym) {
471 cast<DefinedRegular<ELFT>>(Cmd->Sym)->Value = Cmd->Expression(Dot);
472 }
George Rimar652852c2016-04-16 10:10:32 +0000473 continue;
474 }
475
George Rimareefa7582016-08-04 09:29:31 +0000476 if (auto *Cmd = dyn_cast<AssertCommand>(Base.get())) {
477 Cmd->Expression(Dot);
478 continue;
479 }
480
George Rimar076fe152016-07-21 06:43:01 +0000481 auto *Cmd = cast<OutputSectionCommand>(Base.get());
George Rimar652852c2016-04-16 10:10:32 +0000482
Rafael Espindolad3190792016-09-16 15:10:23 +0000483 if (Cmd->AddrExpr)
484 Dot = Cmd->AddrExpr(Dot);
George Rimar58e5c4d2016-07-25 08:29:46 +0000485
Rafael Espindolad3190792016-09-16 15:10:23 +0000486 MinVA = std::min(MinVA, Dot);
487 assignOffsets(Cmd);
George Rimar652852c2016-04-16 10:10:32 +0000488 }
Rui Ueyama52c4e172016-07-01 10:42:25 +0000489
Rafael Espindolad3190792016-09-16 15:10:23 +0000490 for (OutputSectionBase<ELFT> *Sec : *OutputSections)
491 if (!(Sec->getFlags() & SHF_ALLOC))
492 Sec->setVA(0);
Rafael Espindola4ec013a2016-09-15 21:22:11 +0000493 uintX_t HeaderSize =
494 Out<ELFT>::ElfHeader->getSize() + Out<ELFT>::ProgramHeaders->getSize();
495 if (HeaderSize > MinVA)
496 fatal("Not enough space for ELF and program headers");
497
Rafael Espindola64c32d62016-07-07 14:28:47 +0000498 // ELF and Program headers need to be right before the first section in
George Rimarb91e7112016-07-19 07:42:07 +0000499 // memory. Set their addresses accordingly.
Rafael Espindola4ec013a2016-09-15 21:22:11 +0000500 MinVA = alignDown(MinVA - HeaderSize, Target->PageSize);
Eugene Leviant467c4d52016-07-01 10:27:36 +0000501 Out<ELFT>::ElfHeader->setVA(MinVA);
502 Out<ELFT>::ProgramHeaders->setVA(Out<ELFT>::ElfHeader->getSize() + MinVA);
George Rimar652852c2016-04-16 10:10:32 +0000503}
504
Rui Ueyama464daad2016-08-22 04:55:20 +0000505// Creates program headers as instructed by PHDRS linker script command.
Rui Ueyama07320e42016-04-20 20:13:41 +0000506template <class ELFT>
Rafael Espindolaa4b41dc2016-08-04 12:13:05 +0000507std::vector<PhdrEntry<ELFT>> LinkerScript<ELFT>::createPhdrs() {
Rui Ueyamaedebbdf2016-07-24 23:47:31 +0000508 std::vector<PhdrEntry<ELFT>> Ret;
Eugene Leviantbbe38602016-07-19 09:25:43 +0000509
Rui Ueyama464daad2016-08-22 04:55:20 +0000510 // Process PHDRS and FILEHDR keywords because they are not
511 // real output sections and cannot be added in the following loop.
Eugene Leviantbbe38602016-07-19 09:25:43 +0000512 for (const PhdrsCommand &Cmd : Opt.PhdrsCommands) {
Rui Ueyamaedebbdf2016-07-24 23:47:31 +0000513 Ret.emplace_back(Cmd.Type, Cmd.Flags == UINT_MAX ? PF_R : Cmd.Flags);
514 PhdrEntry<ELFT> &Phdr = Ret.back();
Eugene Leviantbbe38602016-07-19 09:25:43 +0000515
516 if (Cmd.HasFilehdr)
Rui Ueyamaadca2452016-07-23 14:18:48 +0000517 Phdr.add(Out<ELFT>::ElfHeader);
Eugene Leviantbbe38602016-07-19 09:25:43 +0000518 if (Cmd.HasPhdrs)
Rui Ueyamaadca2452016-07-23 14:18:48 +0000519 Phdr.add(Out<ELFT>::ProgramHeaders);
Eugene Leviant56b21c82016-09-09 09:46:16 +0000520
521 if (Cmd.LMAExpr) {
522 Phdr.H.p_paddr = Cmd.LMAExpr(0);
523 Phdr.HasLMA = true;
524 }
Eugene Leviantbbe38602016-07-19 09:25:43 +0000525 }
526
Rui Ueyama464daad2016-08-22 04:55:20 +0000527 // Add output sections to program headers.
Rui Ueyamaedebbdf2016-07-24 23:47:31 +0000528 PhdrEntry<ELFT> *Load = nullptr;
529 uintX_t Flags = PF_R;
Rui Ueyama464daad2016-08-22 04:55:20 +0000530 for (OutputSectionBase<ELFT> *Sec : *OutputSections) {
Eugene Leviantbbe38602016-07-19 09:25:43 +0000531 if (!(Sec->getFlags() & SHF_ALLOC))
532 break;
533
Rui Ueyamaedebbdf2016-07-24 23:47:31 +0000534 std::vector<size_t> PhdrIds = getPhdrIndices(Sec->getName());
Eugene Leviantbbe38602016-07-19 09:25:43 +0000535 if (!PhdrIds.empty()) {
536 // Assign headers specified by linker script
537 for (size_t Id : PhdrIds) {
Rui Ueyamaedebbdf2016-07-24 23:47:31 +0000538 Ret[Id].add(Sec);
Eugene Leviant865bf862016-07-21 10:43:25 +0000539 if (Opt.PhdrsCommands[Id].Flags == UINT_MAX)
Rafael Espindola0b113672016-07-27 14:10:56 +0000540 Ret[Id].H.p_flags |= Sec->getPhdrFlags();
Eugene Leviantbbe38602016-07-19 09:25:43 +0000541 }
542 } else {
543 // If we have no load segment or flags've changed then we want new load
544 // segment.
Rafael Espindola0b113672016-07-27 14:10:56 +0000545 uintX_t NewFlags = Sec->getPhdrFlags();
Eugene Leviantbbe38602016-07-19 09:25:43 +0000546 if (Load == nullptr || Flags != NewFlags) {
Rui Ueyamaedebbdf2016-07-24 23:47:31 +0000547 Load = &*Ret.emplace(Ret.end(), PT_LOAD, NewFlags);
Eugene Leviantbbe38602016-07-19 09:25:43 +0000548 Flags = NewFlags;
549 }
Rui Ueyama18f084f2016-07-20 19:36:41 +0000550 Load->add(Sec);
Eugene Leviantbbe38602016-07-19 09:25:43 +0000551 }
Eugene Leviantbbe38602016-07-19 09:25:43 +0000552 }
Rui Ueyamaedebbdf2016-07-24 23:47:31 +0000553 return Ret;
Eugene Leviantbbe38602016-07-19 09:25:43 +0000554}
555
Eugene Leviantf9bc3bd2016-08-16 06:40:58 +0000556template <class ELFT> bool LinkerScript<ELFT>::ignoreInterpSection() {
557 // Ignore .interp section in case we have PHDRS specification
558 // and PT_INTERP isn't listed.
559 return !Opt.PhdrsCommands.empty() &&
560 llvm::find_if(Opt.PhdrsCommands, [](const PhdrsCommand &Cmd) {
561 return Cmd.Type == PT_INTERP;
562 }) == Opt.PhdrsCommands.end();
563}
564
Eugene Leviantbbe38602016-07-19 09:25:43 +0000565template <class ELFT>
Rui Ueyama07320e42016-04-20 20:13:41 +0000566ArrayRef<uint8_t> LinkerScript<ELFT>::getFiller(StringRef Name) {
George Rimarf6c3cce2016-07-21 07:48:54 +0000567 for (const std::unique_ptr<BaseCommand> &Base : Opt.Commands)
568 if (auto *Cmd = dyn_cast<OutputSectionCommand>(Base.get()))
569 if (Cmd->Name == Name)
570 return Cmd->Filler;
571 return {};
George Rimare2ee72b2016-02-26 14:48:31 +0000572}
573
George Rimar206fffa2016-08-17 08:16:57 +0000574template <class ELFT> Expr LinkerScript<ELFT>::getLma(StringRef Name) {
George Rimar8ceadb32016-08-17 07:44:19 +0000575 for (const std::unique_ptr<BaseCommand> &Base : Opt.Commands)
576 if (auto *Cmd = dyn_cast<OutputSectionCommand>(Base.get()))
577 if (Cmd->LmaExpr && Cmd->Name == Name)
578 return Cmd->LmaExpr;
579 return {};
580}
581
Rui Ueyamac3e2a4b2016-04-21 20:30:00 +0000582// Returns the index of the given section name in linker script
583// SECTIONS commands. Sections are laid out as the same order as they
584// were in the script. If a given name did not appear in the script,
585// it returns INT_MAX, so that it will be laid out at end of file.
George Rimar076fe152016-07-21 06:43:01 +0000586template <class ELFT> int LinkerScript<ELFT>::getSectionIndex(StringRef Name) {
Rui Ueyamaf510fa62016-07-26 00:21:15 +0000587 int I = 0;
588 for (std::unique_ptr<BaseCommand> &Base : Opt.Commands) {
589 if (auto *Cmd = dyn_cast<OutputSectionCommand>(Base.get()))
590 if (Cmd->Name == Name)
591 return I;
592 ++I;
593 }
594 return INT_MAX;
George Rimar71b26e92016-04-21 10:22:02 +0000595}
596
597// A compartor to sort output sections. Returns -1 or 1 if
598// A or B are mentioned in linker script. Otherwise, returns 0.
Rui Ueyama07320e42016-04-20 20:13:41 +0000599template <class ELFT>
600int LinkerScript<ELFT>::compareSections(StringRef A, StringRef B) {
Rui Ueyamac3e2a4b2016-04-21 20:30:00 +0000601 int I = getSectionIndex(A);
602 int J = getSectionIndex(B);
603 if (I == INT_MAX && J == INT_MAX)
Rui Ueyama717677a2016-02-11 21:17:59 +0000604 return 0;
605 return I < J ? -1 : 1;
606}
607
Eugene Leviantbbe38602016-07-19 09:25:43 +0000608template <class ELFT> bool LinkerScript<ELFT>::hasPhdrsCommands() {
609 return !Opt.PhdrsCommands.empty();
610}
611
George Rimar9e694502016-07-29 16:18:47 +0000612template <class ELFT>
George Rimar884e7862016-09-08 08:19:13 +0000613uint64_t LinkerScript<ELFT>::getOutputSectionAddress(StringRef Name) {
George Rimar96659df2016-08-30 09:54:01 +0000614 for (OutputSectionBase<ELFT> *Sec : *OutputSections)
615 if (Sec->getName() == Name)
616 return Sec->getVA();
617 error("undefined section " + Name);
618 return 0;
619}
620
621template <class ELFT>
George Rimar884e7862016-09-08 08:19:13 +0000622uint64_t LinkerScript<ELFT>::getOutputSectionSize(StringRef Name) {
George Rimar9e694502016-07-29 16:18:47 +0000623 for (OutputSectionBase<ELFT> *Sec : *OutputSections)
624 if (Sec->getName() == Name)
625 return Sec->getSize();
626 error("undefined section " + Name);
627 return 0;
628}
629
Eugene Leviant36fac7f2016-09-08 09:08:30 +0000630template <class ELFT>
631uint64_t LinkerScript<ELFT>::getOutputSectionAlign(StringRef Name) {
632 for (OutputSectionBase<ELFT> *Sec : *OutputSections)
633 if (Sec->getName() == Name)
634 return Sec->getAlignment();
635 error("undefined section " + Name);
636 return 0;
637}
638
George Rimar884e7862016-09-08 08:19:13 +0000639template <class ELFT> uint64_t LinkerScript<ELFT>::getHeaderSize() {
George Rimare32a3592016-08-10 07:59:34 +0000640 return Out<ELFT>::ElfHeader->getSize() + Out<ELFT>::ProgramHeaders->getSize();
641}
642
George Rimar884e7862016-09-08 08:19:13 +0000643template <class ELFT> uint64_t LinkerScript<ELFT>::getSymbolValue(StringRef S) {
644 if (SymbolBody *B = Symtab<ELFT>::X->find(S))
645 return B->getVA<ELFT>();
646 error("symbol not found: " + S);
647 return 0;
648}
649
Eugene Leviantbbe38602016-07-19 09:25:43 +0000650// Returns indices of ELF headers containing specific section, identified
651// by Name. Each index is a zero based number of ELF header listed within
652// PHDRS {} script block.
653template <class ELFT>
Rui Ueyamaedebbdf2016-07-24 23:47:31 +0000654std::vector<size_t> LinkerScript<ELFT>::getPhdrIndices(StringRef SectionName) {
George Rimar076fe152016-07-21 06:43:01 +0000655 for (const std::unique_ptr<BaseCommand> &Base : Opt.Commands) {
656 auto *Cmd = dyn_cast<OutputSectionCommand>(Base.get());
Rui Ueyamaedebbdf2016-07-24 23:47:31 +0000657 if (!Cmd || Cmd->Name != SectionName)
George Rimar31d842f2016-07-20 16:43:03 +0000658 continue;
659
Rui Ueyama29c5a2a2016-07-26 00:27:36 +0000660 std::vector<size_t> Ret;
661 for (StringRef PhdrName : Cmd->Phdrs)
662 Ret.push_back(getPhdrIndex(PhdrName));
663 return Ret;
Eugene Leviantbbe38602016-07-19 09:25:43 +0000664 }
George Rimar31d842f2016-07-20 16:43:03 +0000665 return {};
Eugene Leviantbbe38602016-07-19 09:25:43 +0000666}
667
Rui Ueyama29c5a2a2016-07-26 00:27:36 +0000668template <class ELFT>
669size_t LinkerScript<ELFT>::getPhdrIndex(StringRef PhdrName) {
670 size_t I = 0;
671 for (PhdrsCommand &Cmd : Opt.PhdrsCommands) {
672 if (Cmd.Name == PhdrName)
673 return I;
674 ++I;
675 }
676 error("section header '" + PhdrName + "' is not listed in PHDRS");
677 return 0;
678}
679
Rui Ueyama07320e42016-04-20 20:13:41 +0000680class elf::ScriptParser : public ScriptParserBase {
George Rimarc3794e52016-02-24 09:21:47 +0000681 typedef void (ScriptParser::*Handler)();
682
Rui Ueyamaf7c5fbb2015-09-30 17:23:26 +0000683public:
Rui Ueyama07320e42016-04-20 20:13:41 +0000684 ScriptParser(StringRef S, bool B) : ScriptParserBase(S), IsUnderSysroot(B) {}
George Rimarf23b2322016-02-19 10:45:45 +0000685
George Rimar20b65982016-08-31 09:08:26 +0000686 void readLinkerScript();
687 void readVersionScript();
Rui Ueyamaf7c5fbb2015-09-30 17:23:26 +0000688
689private:
Rui Ueyama52a15092015-10-11 03:28:42 +0000690 void addFile(StringRef Path);
691
Rui Ueyamaf7c5fbb2015-09-30 17:23:26 +0000692 void readAsNeeded();
Denis Protivensky90c50992015-10-08 06:48:38 +0000693 void readEntry();
George Rimar83f406c2015-10-19 17:35:12 +0000694 void readExtern();
Rui Ueyamaf7c5fbb2015-09-30 17:23:26 +0000695 void readGroup();
Rui Ueyama31aa1f82015-10-11 01:31:55 +0000696 void readInclude();
Rui Ueyamaee592822015-10-07 00:25:09 +0000697 void readOutput();
Davide Italiano9159ce92015-10-12 21:50:08 +0000698 void readOutputArch();
Rui Ueyamaf7c5fbb2015-09-30 17:23:26 +0000699 void readOutputFormat();
Eugene Leviantbbe38602016-07-19 09:25:43 +0000700 void readPhdrs();
Davide Italiano68a39a62015-10-08 17:51:41 +0000701 void readSearchDir();
Denis Protivensky8e3b38a2015-11-12 09:52:08 +0000702 void readSections();
Rui Ueyama95769b42016-08-31 20:03:54 +0000703 void readVersion();
704 void readVersionScriptCommand();
Denis Protivensky8e3b38a2015-11-12 09:52:08 +0000705
Rui Ueyama113cdec2016-07-24 23:05:57 +0000706 SymbolAssignment *readAssignment(StringRef Name);
George Rimarff1f29e2016-09-06 13:51:57 +0000707 std::vector<uint8_t> readFill();
Rui Ueyama10416562016-08-04 02:03:27 +0000708 OutputSectionCommand *readOutputSectionDescription(StringRef OutSec);
George Rimarff1f29e2016-09-06 13:51:57 +0000709 std::vector<uint8_t> readOutputSectionFiller(StringRef Tok);
Eugene Leviantbbe38602016-07-19 09:25:43 +0000710 std::vector<StringRef> readOutputSectionPhdrs();
George Rimara2496cb2016-08-30 09:46:59 +0000711 InputSectionDescription *readInputSectionDescription(StringRef Tok);
George Rimarc91930a2016-09-02 21:17:20 +0000712 Regex readFilePatterns();
George Rimar395281c2016-09-16 17:42:10 +0000713 void readSectionExcludes(InputSectionDescription *Cmd);
George Rimara2496cb2016-08-30 09:46:59 +0000714 InputSectionDescription *readInputSectionRules(StringRef FilePattern);
Eugene Leviantbbe38602016-07-19 09:25:43 +0000715 unsigned readPhdrType();
George Rimarbe394db2016-09-16 20:21:55 +0000716 SortSectionPolicy readSortKind();
Petr Hoseka35e39c2016-08-16 01:11:16 +0000717 SymbolAssignment *readProvideHidden(bool Provide, bool Hidden);
Eugene Leviantdb741e72016-09-07 07:08:43 +0000718 SymbolAssignment *readProvideOrAssignment(StringRef Tok, bool MakeAbsolute);
George Rimar03fc0102016-07-28 07:18:23 +0000719 void readSort();
George Rimareefa7582016-08-04 09:29:31 +0000720 Expr readAssert();
Rui Ueyama708019c2016-07-24 18:19:40 +0000721
722 Expr readExpr();
723 Expr readExpr1(Expr Lhs, int MinPrec);
724 Expr readPrimary();
725 Expr readTernary(Expr Cond);
Rui Ueyama6ad7dfc2016-08-17 18:59:16 +0000726 Expr readParenExpr();
Rui Ueyamaf7c5fbb2015-09-30 17:23:26 +0000727
George Rimar20b65982016-08-31 09:08:26 +0000728 // For parsing version script.
729 void readExtern(std::vector<SymbolVersion> *Globals);
Rui Ueyama95769b42016-08-31 20:03:54 +0000730 void readVersionDeclaration(StringRef VerStr);
George Rimar20b65982016-08-31 09:08:26 +0000731 void readGlobal(StringRef VerStr);
732 void readLocal();
733
Rui Ueyama07320e42016-04-20 20:13:41 +0000734 ScriptConfiguration &Opt = *ScriptConfig;
735 StringSaver Saver = {ScriptConfig->Alloc};
Simon Atanasyan16b0cc92015-11-26 05:53:00 +0000736 bool IsUnderSysroot;
Rui Ueyamaf7c5fbb2015-09-30 17:23:26 +0000737};
Rui Ueyamaf7c5fbb2015-09-30 17:23:26 +0000738
George Rimar20b65982016-08-31 09:08:26 +0000739void ScriptParser::readVersionScript() {
Rui Ueyama95769b42016-08-31 20:03:54 +0000740 readVersionScriptCommand();
741 if (!atEOF())
742 setError("EOF expected, but got " + next());
743}
744
745void ScriptParser::readVersionScriptCommand() {
George Rimar20b65982016-08-31 09:08:26 +0000746 if (skip("{")) {
Rui Ueyama95769b42016-08-31 20:03:54 +0000747 readVersionDeclaration("");
George Rimar20b65982016-08-31 09:08:26 +0000748 return;
749 }
750
Rui Ueyama95769b42016-08-31 20:03:54 +0000751 while (!atEOF() && !Error && peek() != "}") {
George Rimar20b65982016-08-31 09:08:26 +0000752 StringRef VerStr = next();
753 if (VerStr == "{") {
Rui Ueyama95769b42016-08-31 20:03:54 +0000754 setError("anonymous version definition is used in "
755 "combination with other version definitions");
George Rimar20b65982016-08-31 09:08:26 +0000756 return;
757 }
758 expect("{");
Rui Ueyama95769b42016-08-31 20:03:54 +0000759 readVersionDeclaration(VerStr);
George Rimar20b65982016-08-31 09:08:26 +0000760 }
761}
762
Rui Ueyama95769b42016-08-31 20:03:54 +0000763void ScriptParser::readVersion() {
764 expect("{");
765 readVersionScriptCommand();
766 expect("}");
767}
768
George Rimar20b65982016-08-31 09:08:26 +0000769void ScriptParser::readLinkerScript() {
Rui Ueyamaf7c5fbb2015-09-30 17:23:26 +0000770 while (!atEOF()) {
771 StringRef Tok = next();
Rui Ueyamaa27eecc2016-09-02 18:52:41 +0000772 if (Tok == ";")
773 continue;
774
Eugene Leviant20d03192016-09-16 15:30:47 +0000775 if (Tok == "ASSERT") {
776 Opt.Commands.emplace_back(new AssertCommand(readAssert()));
777 } else if (Tok == "ENTRY") {
Rui Ueyamaa27eecc2016-09-02 18:52:41 +0000778 readEntry();
779 } else if (Tok == "EXTERN") {
780 readExtern();
781 } else if (Tok == "GROUP" || Tok == "INPUT") {
782 readGroup();
783 } else if (Tok == "INCLUDE") {
784 readInclude();
785 } else if (Tok == "OUTPUT") {
786 readOutput();
787 } else if (Tok == "OUTPUT_ARCH") {
788 readOutputArch();
789 } else if (Tok == "OUTPUT_FORMAT") {
790 readOutputFormat();
791 } else if (Tok == "PHDRS") {
792 readPhdrs();
793 } else if (Tok == "SEARCH_DIR") {
794 readSearchDir();
795 } else if (Tok == "SECTIONS") {
796 readSections();
797 } else if (Tok == "VERSION") {
798 readVersion();
Eugene Leviantdb741e72016-09-07 07:08:43 +0000799 } else if (SymbolAssignment *Cmd = readProvideOrAssignment(Tok, true)) {
Eugene Leviant20d03192016-09-16 15:30:47 +0000800 Opt.Commands.emplace_back(Cmd);
Petr Hoseke5d3ca52016-08-31 15:31:17 +0000801 } else {
George Rimar57610422016-03-11 14:43:02 +0000802 setError("unknown directive: " + Tok);
Petr Hoseke5d3ca52016-08-31 15:31:17 +0000803 }
Rui Ueyamaf7c5fbb2015-09-30 17:23:26 +0000804 }
805}
806
Rui Ueyama717677a2016-02-11 21:17:59 +0000807void ScriptParser::addFile(StringRef S) {
Simon Atanasyan16b0cc92015-11-26 05:53:00 +0000808 if (IsUnderSysroot && S.startswith("/")) {
809 SmallString<128> Path;
810 (Config->Sysroot + S).toStringRef(Path);
811 if (sys::fs::exists(Path)) {
812 Driver->addFile(Saver.save(Path.str()));
813 return;
814 }
815 }
816
Rui Ueyamaf03f3cc2015-10-13 00:09:21 +0000817 if (sys::path::is_absolute(S)) {
Rui Ueyama52a15092015-10-11 03:28:42 +0000818 Driver->addFile(S);
819 } else if (S.startswith("=")) {
820 if (Config->Sysroot.empty())
821 Driver->addFile(S.substr(1));
822 else
823 Driver->addFile(Saver.save(Config->Sysroot + "/" + S.substr(1)));
824 } else if (S.startswith("-l")) {
Rui Ueyama21eecb42016-02-02 21:13:09 +0000825 Driver->addLibrary(S.substr(2));
Simon Atanasyana1b8fc32015-11-26 20:23:46 +0000826 } else if (sys::fs::exists(S)) {
827 Driver->addFile(S);
Rui Ueyama52a15092015-10-11 03:28:42 +0000828 } else {
829 std::string Path = findFromSearchPaths(S);
830 if (Path.empty())
George Rimar777f9632016-03-12 08:31:34 +0000831 setError("unable to find " + S);
Rui Ueyama025d59b2016-02-02 20:27:59 +0000832 else
833 Driver->addFile(Saver.save(Path));
Rui Ueyama52a15092015-10-11 03:28:42 +0000834 }
835}
836
Rui Ueyama717677a2016-02-11 21:17:59 +0000837void ScriptParser::readAsNeeded() {
Rui Ueyamaf7c5fbb2015-09-30 17:23:26 +0000838 expect("(");
Rui Ueyama35da9b62015-10-11 20:59:12 +0000839 bool Orig = Config->AsNeeded;
840 Config->AsNeeded = true;
Rui Ueyamaa2acc932016-08-05 01:25:45 +0000841 while (!Error && !skip(")"))
George Rimarcd574a52016-09-09 14:35:36 +0000842 addFile(unquote(next()));
Rui Ueyama35da9b62015-10-11 20:59:12 +0000843 Config->AsNeeded = Orig;
Rui Ueyamaf7c5fbb2015-09-30 17:23:26 +0000844}
845
Rui Ueyama717677a2016-02-11 21:17:59 +0000846void ScriptParser::readEntry() {
Denis Protivensky90c50992015-10-08 06:48:38 +0000847 // -e <symbol> takes predecence over ENTRY(<symbol>).
848 expect("(");
849 StringRef Tok = next();
850 if (Config->Entry.empty())
851 Config->Entry = Tok;
852 expect(")");
853}
854
Rui Ueyama717677a2016-02-11 21:17:59 +0000855void ScriptParser::readExtern() {
George Rimar83f406c2015-10-19 17:35:12 +0000856 expect("(");
Rui Ueyamaa2acc932016-08-05 01:25:45 +0000857 while (!Error && !skip(")"))
858 Config->Undefined.push_back(next());
George Rimar83f406c2015-10-19 17:35:12 +0000859}
860
Rui Ueyama717677a2016-02-11 21:17:59 +0000861void ScriptParser::readGroup() {
Rui Ueyamaf7c5fbb2015-09-30 17:23:26 +0000862 expect("(");
Rui Ueyamaa2acc932016-08-05 01:25:45 +0000863 while (!Error && !skip(")")) {
Rui Ueyamaf7c5fbb2015-09-30 17:23:26 +0000864 StringRef Tok = next();
Rui Ueyamaa2acc932016-08-05 01:25:45 +0000865 if (Tok == "AS_NEEDED")
Rui Ueyamaf7c5fbb2015-09-30 17:23:26 +0000866 readAsNeeded();
Rui Ueyamaa2acc932016-08-05 01:25:45 +0000867 else
George Rimarcd574a52016-09-09 14:35:36 +0000868 addFile(unquote(Tok));
Rui Ueyamaf7c5fbb2015-09-30 17:23:26 +0000869 }
870}
871
Rui Ueyama717677a2016-02-11 21:17:59 +0000872void ScriptParser::readInclude() {
Rui Ueyama31aa1f82015-10-11 01:31:55 +0000873 StringRef Tok = next();
George Rimarcd574a52016-09-09 14:35:36 +0000874 auto MBOrErr = MemoryBuffer::getFile(unquote(Tok));
Rui Ueyama025d59b2016-02-02 20:27:59 +0000875 if (!MBOrErr) {
George Rimar57610422016-03-11 14:43:02 +0000876 setError("cannot open " + Tok);
Rui Ueyama025d59b2016-02-02 20:27:59 +0000877 return;
878 }
Rui Ueyama31aa1f82015-10-11 01:31:55 +0000879 std::unique_ptr<MemoryBuffer> &MB = *MBOrErr;
Rui Ueyamaa47ee682015-10-11 01:53:04 +0000880 StringRef S = Saver.save(MB->getMemBufferRef().getBuffer());
881 std::vector<StringRef> V = tokenize(S);
Rui Ueyama31aa1f82015-10-11 01:31:55 +0000882 Tokens.insert(Tokens.begin() + Pos, V.begin(), V.end());
Rui Ueyama31aa1f82015-10-11 01:31:55 +0000883}
884
Rui Ueyama717677a2016-02-11 21:17:59 +0000885void ScriptParser::readOutput() {
Rui Ueyamaee592822015-10-07 00:25:09 +0000886 // -o <file> takes predecence over OUTPUT(<file>).
887 expect("(");
888 StringRef Tok = next();
889 if (Config->OutputFile.empty())
George Rimarcd574a52016-09-09 14:35:36 +0000890 Config->OutputFile = unquote(Tok);
Rui Ueyamaee592822015-10-07 00:25:09 +0000891 expect(")");
892}
893
Rui Ueyama717677a2016-02-11 21:17:59 +0000894void ScriptParser::readOutputArch() {
Davide Italiano9159ce92015-10-12 21:50:08 +0000895 // Error checking only for now.
896 expect("(");
897 next();
898 expect(")");
899}
900
Rui Ueyama717677a2016-02-11 21:17:59 +0000901void ScriptParser::readOutputFormat() {
Rui Ueyamaf7c5fbb2015-09-30 17:23:26 +0000902 // Error checking only for now.
903 expect("(");
904 next();
Davide Italiano6836c612015-10-12 21:08:41 +0000905 StringRef Tok = next();
906 if (Tok == ")")
George Rimar6c55f0e2016-09-08 08:20:30 +0000907 return;
Rui Ueyama025d59b2016-02-02 20:27:59 +0000908 if (Tok != ",") {
George Rimar57610422016-03-11 14:43:02 +0000909 setError("unexpected token: " + Tok);
Rui Ueyama025d59b2016-02-02 20:27:59 +0000910 return;
911 }
Davide Italiano6836c612015-10-12 21:08:41 +0000912 next();
913 expect(",");
914 next();
Rui Ueyamaf7c5fbb2015-09-30 17:23:26 +0000915 expect(")");
916}
917
Eugene Leviantbbe38602016-07-19 09:25:43 +0000918void ScriptParser::readPhdrs() {
919 expect("{");
920 while (!Error && !skip("}")) {
921 StringRef Tok = next();
Eugene Leviant56b21c82016-09-09 09:46:16 +0000922 Opt.PhdrsCommands.push_back(
923 {Tok, PT_NULL, false, false, UINT_MAX, nullptr});
Eugene Leviantbbe38602016-07-19 09:25:43 +0000924 PhdrsCommand &PhdrCmd = Opt.PhdrsCommands.back();
925
926 PhdrCmd.Type = readPhdrType();
927 do {
928 Tok = next();
929 if (Tok == ";")
930 break;
931 if (Tok == "FILEHDR")
932 PhdrCmd.HasFilehdr = true;
933 else if (Tok == "PHDRS")
934 PhdrCmd.HasPhdrs = true;
Eugene Leviant56b21c82016-09-09 09:46:16 +0000935 else if (Tok == "AT")
936 PhdrCmd.LMAExpr = readParenExpr();
Eugene Leviant865bf862016-07-21 10:43:25 +0000937 else if (Tok == "FLAGS") {
938 expect("(");
Rafael Espindolaeb685cd2016-08-02 22:14:57 +0000939 // Passing 0 for the value of dot is a bit of a hack. It means that
940 // we accept expressions like ".|1".
941 PhdrCmd.Flags = readExpr()(0);
Eugene Leviant865bf862016-07-21 10:43:25 +0000942 expect(")");
943 } else
Eugene Leviantbbe38602016-07-19 09:25:43 +0000944 setError("unexpected header attribute: " + Tok);
945 } while (!Error);
946 }
947}
948
Rui Ueyama717677a2016-02-11 21:17:59 +0000949void ScriptParser::readSearchDir() {
Davide Italiano68a39a62015-10-08 17:51:41 +0000950 expect("(");
Rui Ueyama86c5fb82016-09-08 23:26:54 +0000951 StringRef Tok = next();
Rui Ueyama6c7ad132016-09-02 19:20:33 +0000952 if (!Config->Nostdlib)
George Rimarcd574a52016-09-09 14:35:36 +0000953 Config->SearchPaths.push_back(unquote(Tok));
Davide Italiano68a39a62015-10-08 17:51:41 +0000954 expect(")");
955}
956
Rui Ueyama717677a2016-02-11 21:17:59 +0000957void ScriptParser::readSections() {
Eugene Leviante05336ff2016-09-14 08:32:36 +0000958 Opt.HasSections = true;
Denis Protivensky8e3b38a2015-11-12 09:52:08 +0000959 expect("{");
George Rimar652852c2016-04-16 10:10:32 +0000960 while (!Error && !skip("}")) {
Rui Ueyama113cdec2016-07-24 23:05:57 +0000961 StringRef Tok = next();
Eugene Leviantdb741e72016-09-07 07:08:43 +0000962 BaseCommand *Cmd = readProvideOrAssignment(Tok, true);
Eugene Leviantceabe802016-08-11 07:56:43 +0000963 if (!Cmd) {
964 if (Tok == "ASSERT")
965 Cmd = new AssertCommand(readAssert());
966 else
967 Cmd = readOutputSectionDescription(Tok);
Rui Ueyama708019c2016-07-24 18:19:40 +0000968 }
Rui Ueyama10416562016-08-04 02:03:27 +0000969 Opt.Commands.emplace_back(Cmd);
George Rimar652852c2016-04-16 10:10:32 +0000970 }
Denis Protivensky8e3b38a2015-11-12 09:52:08 +0000971}
972
Rui Ueyama708019c2016-07-24 18:19:40 +0000973static int precedence(StringRef Op) {
974 return StringSwitch<int>(Op)
975 .Case("*", 4)
976 .Case("/", 4)
977 .Case("+", 3)
978 .Case("-", 3)
979 .Case("<", 2)
980 .Case(">", 2)
981 .Case(">=", 2)
982 .Case("<=", 2)
983 .Case("==", 2)
984 .Case("!=", 2)
985 .Case("&", 1)
Rafael Espindolacc3dd622016-08-22 21:33:35 +0000986 .Case("|", 1)
Rui Ueyama708019c2016-07-24 18:19:40 +0000987 .Default(-1);
988}
989
George Rimarc91930a2016-09-02 21:17:20 +0000990Regex ScriptParser::readFilePatterns() {
Rui Ueyama10416562016-08-04 02:03:27 +0000991 std::vector<StringRef> V;
992 while (!Error && !skip(")"))
993 V.push_back(next());
George Rimarc91930a2016-09-02 21:17:20 +0000994 return compileGlobPatterns(V);
George Rimar0702c4e2016-07-29 15:32:46 +0000995}
996
George Rimarbe394db2016-09-16 20:21:55 +0000997SortSectionPolicy ScriptParser::readSortKind() {
Rui Ueyama742c3832016-08-04 22:27:00 +0000998 if (skip("SORT") || skip("SORT_BY_NAME"))
George Rimarbe394db2016-09-16 20:21:55 +0000999 return SortSectionPolicy::Name;
Rui Ueyama742c3832016-08-04 22:27:00 +00001000 if (skip("SORT_BY_ALIGNMENT"))
George Rimarbe394db2016-09-16 20:21:55 +00001001 return SortSectionPolicy::Alignment;
George Rimar575208c2016-09-15 19:15:12 +00001002 if (skip("SORT_BY_INIT_PRIORITY"))
George Rimarbe394db2016-09-16 20:21:55 +00001003 return SortSectionPolicy::Priority;
George Rimarbe394db2016-09-16 20:21:55 +00001004 if (skip("SORT_NONE"))
Rui Ueyamab2a0abd2016-09-16 21:14:55 +00001005 return SortSectionPolicy::None;
1006 return SortSectionPolicy::Default;
George Rimarbe394db2016-09-16 20:21:55 +00001007}
1008
1009static void selectSortKind(InputSectionDescription *Cmd) {
Rui Ueyamab2a0abd2016-09-16 21:14:55 +00001010 if (Cmd->SortOuter == SortSectionPolicy::None) {
1011 Cmd->SortOuter = SortSectionPolicy::Default;
George Rimarbe394db2016-09-16 20:21:55 +00001012 return;
1013 }
1014
Rui Ueyamab2a0abd2016-09-16 21:14:55 +00001015 if (Cmd->SortOuter != SortSectionPolicy::Default) {
George Rimarbe394db2016-09-16 20:21:55 +00001016 // If the section sorting command in linker script is nested, the command
1017 // line option will be ignored.
Rui Ueyamab2a0abd2016-09-16 21:14:55 +00001018 if (Cmd->SortInner != SortSectionPolicy::Default)
George Rimarbe394db2016-09-16 20:21:55 +00001019 return;
1020 // If the section sorting command in linker script isn't nested, the
1021 // command line option will make the section sorting command to be treated
1022 // as nested sorting command.
1023 Cmd->SortInner = Config->SortSection;
1024 return;
1025 }
1026 // If sorting rule not specified, use command line option.
1027 Cmd->SortOuter = Config->SortSection;
Rui Ueyama742c3832016-08-04 22:27:00 +00001028}
1029
George Rimar395281c2016-09-16 17:42:10 +00001030// Method reads a list of sequence of excluded files and section globs given in
1031// a following form: ((EXCLUDE_FILE(file_pattern+))? section_pattern+)+
1032// Example: *(.foo.1 EXCLUDE_FILE (*a.o) .foo.2 EXCLUDE_FILE (*b.o) .foo.3)
1033void ScriptParser::readSectionExcludes(InputSectionDescription *Cmd) {
1034 llvm::Regex ExcludeFileRe;
1035 std::vector<StringRef> V;
1036
1037 while (!Error) {
1038 if (skip(")")) {
1039 Cmd->SectionsVec.push_back(
1040 {std::move(ExcludeFileRe), compileGlobPatterns(V)});
1041 return;
1042 }
1043
1044 if (skip("EXCLUDE_FILE")) {
1045 if (!V.empty()) {
1046 Cmd->SectionsVec.push_back(
1047 {std::move(ExcludeFileRe), compileGlobPatterns(V)});
1048 V.clear();
1049 }
1050
1051 expect("(");
1052 ExcludeFileRe = readFilePatterns();
1053 continue;
1054 }
1055
1056 V.push_back(next());
1057 }
1058}
1059
George Rimara2496cb2016-08-30 09:46:59 +00001060InputSectionDescription *
1061ScriptParser::readInputSectionRules(StringRef FilePattern) {
George Rimarc91930a2016-09-02 21:17:20 +00001062 auto *Cmd = new InputSectionDescription(FilePattern);
Davide Italiano0ed42b02016-07-25 21:47:13 +00001063 expect("(");
Davide Italianoe7282792016-07-27 01:44:01 +00001064
Rui Ueyama742c3832016-08-04 22:27:00 +00001065 // Read SORT().
George Rimarbe394db2016-09-16 20:21:55 +00001066 SortSectionPolicy K1 = readSortKind();
Rui Ueyamab2a0abd2016-09-16 21:14:55 +00001067 if (K1 != SortSectionPolicy::Default) {
Rui Ueyama742c3832016-08-04 22:27:00 +00001068 Cmd->SortOuter = K1;
George Rimar0702c4e2016-07-29 15:32:46 +00001069 expect("(");
George Rimarbe394db2016-09-16 20:21:55 +00001070 SortSectionPolicy K2 = readSortKind();
Rui Ueyamab2a0abd2016-09-16 21:14:55 +00001071 if (K2 != SortSectionPolicy::Default) {
Rui Ueyama742c3832016-08-04 22:27:00 +00001072 Cmd->SortInner = K2;
George Rimar350ece42016-08-03 08:35:59 +00001073 expect("(");
George Rimar395281c2016-09-16 17:42:10 +00001074 Cmd->SectionsVec.push_back({llvm::Regex(), readFilePatterns()});
George Rimar350ece42016-08-03 08:35:59 +00001075 expect(")");
1076 } else {
George Rimar395281c2016-09-16 17:42:10 +00001077 Cmd->SectionsVec.push_back({llvm::Regex(), readFilePatterns()});
George Rimar350ece42016-08-03 08:35:59 +00001078 }
George Rimar0702c4e2016-07-29 15:32:46 +00001079 expect(")");
George Rimarbe394db2016-09-16 20:21:55 +00001080 selectSortKind(Cmd);
Rui Ueyama10416562016-08-04 02:03:27 +00001081 return Cmd;
George Rimar06598002016-07-28 21:51:30 +00001082 }
George Rimar0702c4e2016-07-29 15:32:46 +00001083
George Rimarbe394db2016-09-16 20:21:55 +00001084 selectSortKind(Cmd);
George Rimar395281c2016-09-16 17:42:10 +00001085 readSectionExcludes(Cmd);
Rui Ueyama10416562016-08-04 02:03:27 +00001086 return Cmd;
Davide Italianoe7282792016-07-27 01:44:01 +00001087}
1088
George Rimara2496cb2016-08-30 09:46:59 +00001089InputSectionDescription *
1090ScriptParser::readInputSectionDescription(StringRef Tok) {
George Rimar06598002016-07-28 21:51:30 +00001091 // Input section wildcard can be surrounded by KEEP.
1092 // https://sourceware.org/binutils/docs/ld/Input-Section-Keep.html#Input-Section-Keep
George Rimara2496cb2016-08-30 09:46:59 +00001093 if (Tok == "KEEP") {
George Rimar06598002016-07-28 21:51:30 +00001094 expect("(");
George Rimara2496cb2016-08-30 09:46:59 +00001095 StringRef FilePattern = next();
1096 InputSectionDescription *Cmd = readInputSectionRules(FilePattern);
George Rimar06598002016-07-28 21:51:30 +00001097 expect(")");
George Rimar395281c2016-09-16 17:42:10 +00001098 for (std::pair<llvm::Regex, llvm::Regex> &Regex : Cmd->SectionsVec)
1099 Opt.KeptSections.push_back(&Regex.second);
Rui Ueyama10416562016-08-04 02:03:27 +00001100 return Cmd;
George Rimar06598002016-07-28 21:51:30 +00001101 }
George Rimara2496cb2016-08-30 09:46:59 +00001102 return readInputSectionRules(Tok);
Davide Italiano0ed42b02016-07-25 21:47:13 +00001103}
1104
George Rimar03fc0102016-07-28 07:18:23 +00001105void ScriptParser::readSort() {
1106 expect("(");
1107 expect("CONSTRUCTORS");
1108 expect(")");
1109}
1110
George Rimareefa7582016-08-04 09:29:31 +00001111Expr ScriptParser::readAssert() {
1112 expect("(");
1113 Expr E = readExpr();
1114 expect(",");
George Rimarcd574a52016-09-09 14:35:36 +00001115 StringRef Msg = unquote(next());
George Rimareefa7582016-08-04 09:29:31 +00001116 expect(")");
1117 return [=](uint64_t Dot) {
1118 uint64_t V = E(Dot);
1119 if (!V)
1120 error(Msg);
1121 return V;
1122 };
1123}
1124
Rui Ueyama25150e82016-09-06 17:46:43 +00001125// Reads a FILL(expr) command. We handle the FILL command as an
1126// alias for =fillexp section attribute, which is different from
1127// what GNU linkers do.
1128// https://sourceware.org/binutils/docs/ld/Output-Section-Data.html
George Rimarff1f29e2016-09-06 13:51:57 +00001129std::vector<uint8_t> ScriptParser::readFill() {
1130 expect("(");
1131 std::vector<uint8_t> V = readOutputSectionFiller(next());
1132 expect(")");
1133 expect(";");
1134 return V;
1135}
1136
Rui Ueyama10416562016-08-04 02:03:27 +00001137OutputSectionCommand *
1138ScriptParser::readOutputSectionDescription(StringRef OutSec) {
George Rimar076fe152016-07-21 06:43:01 +00001139 OutputSectionCommand *Cmd = new OutputSectionCommand(OutSec);
George Rimar58e5c4d2016-07-25 08:29:46 +00001140
1141 // Read an address expression.
1142 // https://sourceware.org/binutils/docs/ld/Output-Section-Address.html#Output-Section-Address
1143 if (peek() != ":")
1144 Cmd->AddrExpr = readExpr();
1145
Denis Protivensky8e3b38a2015-11-12 09:52:08 +00001146 expect(":");
Davide Italiano246f6812016-07-22 03:36:24 +00001147
George Rimar8ceadb32016-08-17 07:44:19 +00001148 if (skip("AT"))
Rui Ueyama6ad7dfc2016-08-17 18:59:16 +00001149 Cmd->LmaExpr = readParenExpr();
George Rimar630c6172016-07-26 18:06:29 +00001150 if (skip("ALIGN"))
Rui Ueyama6ad7dfc2016-08-17 18:59:16 +00001151 Cmd->AlignExpr = readParenExpr();
George Rimardb24d9c2016-08-19 15:18:23 +00001152 if (skip("SUBALIGN"))
1153 Cmd->SubalignExpr = readParenExpr();
George Rimar630c6172016-07-26 18:06:29 +00001154
Davide Italiano246f6812016-07-22 03:36:24 +00001155 // Parse constraints.
1156 if (skip("ONLY_IF_RO"))
Rui Ueyamaefc40662016-07-25 22:00:10 +00001157 Cmd->Constraint = ConstraintKind::ReadOnly;
Davide Italiano246f6812016-07-22 03:36:24 +00001158 if (skip("ONLY_IF_RW"))
Rui Ueyamaefc40662016-07-25 22:00:10 +00001159 Cmd->Constraint = ConstraintKind::ReadWrite;
Denis Protivensky8e3b38a2015-11-12 09:52:08 +00001160 expect("{");
Rui Ueyama8ec77e62016-04-21 22:00:51 +00001161
Rui Ueyama025d59b2016-02-02 20:27:59 +00001162 while (!Error && !skip("}")) {
Eugene Leviantceabe802016-08-11 07:56:43 +00001163 StringRef Tok = next();
Eugene Leviantdb741e72016-09-07 07:08:43 +00001164 if (SymbolAssignment *Assignment = readProvideOrAssignment(Tok, false))
Eugene Leviantceabe802016-08-11 07:56:43 +00001165 Cmd->Commands.emplace_back(Assignment);
George Rimarff1f29e2016-09-06 13:51:57 +00001166 else if (Tok == "FILL")
1167 Cmd->Filler = readFill();
Eugene Leviantceabe802016-08-11 07:56:43 +00001168 else if (Tok == "SORT")
George Rimar03fc0102016-07-28 07:18:23 +00001169 readSort();
George Rimara2496cb2016-08-30 09:46:59 +00001170 else if (peek() == "(")
1171 Cmd->Commands.emplace_back(readInputSectionDescription(Tok));
Eugene Leviantceabe802016-08-11 07:56:43 +00001172 else
1173 setError("unknown command " + Tok);
Denis Protivensky8e3b38a2015-11-12 09:52:08 +00001174 }
George Rimar076fe152016-07-21 06:43:01 +00001175 Cmd->Phdrs = readOutputSectionPhdrs();
George Rimarff1f29e2016-09-06 13:51:57 +00001176 if (peek().startswith("="))
1177 Cmd->Filler = readOutputSectionFiller(next().drop_front());
Rui Ueyama10416562016-08-04 02:03:27 +00001178 return Cmd;
Rui Ueyamaf71caa22016-07-29 06:14:07 +00001179}
Rui Ueyama8ec77e62016-04-21 22:00:51 +00001180
Rui Ueyama2c8f1f02016-08-29 22:01:21 +00001181// Read "=<number>" where <number> is an octal/decimal/hexadecimal number.
1182// https://sourceware.org/binutils/docs/ld/Output-Section-Fill.html
1183//
1184// ld.gold is not fully compatible with ld.bfd. ld.bfd handles
1185// hexstrings as blobs of arbitrary sizes, while ld.gold handles them
1186// as 32-bit big-endian values. We will do the same as ld.gold does
1187// because it's simpler than what ld.bfd does.
George Rimarff1f29e2016-09-06 13:51:57 +00001188std::vector<uint8_t> ScriptParser::readOutputSectionFiller(StringRef Tok) {
Rui Ueyama965827d2016-08-03 23:25:15 +00001189 uint32_t V;
George Rimarff1f29e2016-09-06 13:51:57 +00001190 if (Tok.getAsInteger(0, V)) {
Rui Ueyama965827d2016-08-03 23:25:15 +00001191 setError("invalid filler expression: " + Tok);
Rui Ueyamaf71caa22016-07-29 06:14:07 +00001192 return {};
George Rimare2ee72b2016-02-26 14:48:31 +00001193 }
Rui Ueyama2c8f1f02016-08-29 22:01:21 +00001194 return {uint8_t(V >> 24), uint8_t(V >> 16), uint8_t(V >> 8), uint8_t(V)};
Denis Protivensky8e3b38a2015-11-12 09:52:08 +00001195}
1196
Petr Hoseka35e39c2016-08-16 01:11:16 +00001197SymbolAssignment *ScriptParser::readProvideHidden(bool Provide, bool Hidden) {
Eugene Levianta31c91b2016-07-22 07:38:40 +00001198 expect("(");
Rui Ueyama174e0a12016-07-29 00:29:25 +00001199 SymbolAssignment *Cmd = readAssignment(next());
Petr Hoseka35e39c2016-08-16 01:11:16 +00001200 Cmd->Provide = Provide;
Rui Ueyama174e0a12016-07-29 00:29:25 +00001201 Cmd->Hidden = Hidden;
Eugene Levianta31c91b2016-07-22 07:38:40 +00001202 expect(")");
1203 expect(";");
Rui Ueyama10416562016-08-04 02:03:27 +00001204 return Cmd;
Eugene Levianteda81a12016-07-12 06:39:48 +00001205}
1206
Eugene Leviantdb741e72016-09-07 07:08:43 +00001207SymbolAssignment *ScriptParser::readProvideOrAssignment(StringRef Tok,
1208 bool MakeAbsolute) {
Eugene Leviantceabe802016-08-11 07:56:43 +00001209 SymbolAssignment *Cmd = nullptr;
1210 if (peek() == "=" || peek() == "+=") {
1211 Cmd = readAssignment(Tok);
1212 expect(";");
1213 } else if (Tok == "PROVIDE") {
Petr Hoseka35e39c2016-08-16 01:11:16 +00001214 Cmd = readProvideHidden(true, false);
1215 } else if (Tok == "HIDDEN") {
1216 Cmd = readProvideHidden(false, true);
Eugene Leviantceabe802016-08-11 07:56:43 +00001217 } else if (Tok == "PROVIDE_HIDDEN") {
Petr Hoseka35e39c2016-08-16 01:11:16 +00001218 Cmd = readProvideHidden(true, true);
Eugene Leviantceabe802016-08-11 07:56:43 +00001219 }
Eugene Leviantdb741e72016-09-07 07:08:43 +00001220 if (Cmd && MakeAbsolute)
1221 Cmd->IsAbsolute = true;
Eugene Leviantceabe802016-08-11 07:56:43 +00001222 return Cmd;
1223}
1224
George Rimar30835ea2016-07-28 21:08:56 +00001225static uint64_t getSymbolValue(StringRef S, uint64_t Dot) {
1226 if (S == ".")
1227 return Dot;
George Rimar884e7862016-09-08 08:19:13 +00001228 return ScriptBase->getSymbolValue(S);
George Rimare32a3592016-08-10 07:59:34 +00001229}
1230
George Rimar30835ea2016-07-28 21:08:56 +00001231SymbolAssignment *ScriptParser::readAssignment(StringRef Name) {
1232 StringRef Op = next();
Eugene Leviantdb741e72016-09-07 07:08:43 +00001233 bool IsAbsolute = false;
1234 Expr E;
George Rimar30835ea2016-07-28 21:08:56 +00001235 assert(Op == "=" || Op == "+=");
Eugene Leviantdb741e72016-09-07 07:08:43 +00001236 if (skip("ABSOLUTE")) {
1237 E = readParenExpr();
1238 IsAbsolute = true;
1239 } else {
1240 E = readExpr();
1241 }
George Rimar30835ea2016-07-28 21:08:56 +00001242 if (Op == "+=")
1243 E = [=](uint64_t Dot) { return getSymbolValue(Name, Dot) + E(Dot); };
Eugene Leviantdb741e72016-09-07 07:08:43 +00001244 return new SymbolAssignment(Name, E, IsAbsolute);
George Rimar30835ea2016-07-28 21:08:56 +00001245}
1246
1247// This is an operator-precedence parser to parse a linker
1248// script expression.
1249Expr ScriptParser::readExpr() { return readExpr1(readPrimary(), 0); }
1250
Rui Ueyama36c1cd22016-08-05 01:04:59 +00001251static Expr combine(StringRef Op, Expr L, Expr R) {
1252 if (Op == "*")
1253 return [=](uint64_t Dot) { return L(Dot) * R(Dot); };
1254 if (Op == "/") {
1255 return [=](uint64_t Dot) -> uint64_t {
1256 uint64_t RHS = R(Dot);
1257 if (RHS == 0) {
1258 error("division by zero");
1259 return 0;
1260 }
1261 return L(Dot) / RHS;
1262 };
1263 }
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); };
1280 if (Op == "&")
1281 return [=](uint64_t Dot) { return L(Dot) & R(Dot); };
Rafael Espindolacc3dd622016-08-22 21:33:35 +00001282 if (Op == "|")
1283 return [=](uint64_t Dot) { return L(Dot) | R(Dot); };
Rui Ueyama36c1cd22016-08-05 01:04:59 +00001284 llvm_unreachable("invalid operator");
1285}
1286
Rui Ueyama708019c2016-07-24 18:19:40 +00001287// This is a part of the operator-precedence parser. This function
1288// assumes that the remaining token stream starts with an operator.
1289Expr ScriptParser::readExpr1(Expr Lhs, int MinPrec) {
1290 while (!atEOF() && !Error) {
1291 // Read an operator and an expression.
1292 StringRef Op1 = peek();
1293 if (Op1 == "?")
1294 return readTernary(Lhs);
1295 if (precedence(Op1) < MinPrec)
Eugene Levianteda81a12016-07-12 06:39:48 +00001296 break;
Rui Ueyama708019c2016-07-24 18:19:40 +00001297 next();
1298 Expr Rhs = readPrimary();
1299
1300 // Evaluate the remaining part of the expression first if the
1301 // next operator has greater precedence than the previous one.
1302 // For example, if we have read "+" and "3", and if the next
1303 // operator is "*", then we'll evaluate 3 * ... part first.
1304 while (!atEOF()) {
1305 StringRef Op2 = peek();
1306 if (precedence(Op2) <= precedence(Op1))
1307 break;
1308 Rhs = readExpr1(Rhs, precedence(Op2));
1309 }
1310
1311 Lhs = combine(Op1, Lhs, Rhs);
Eugene Levianteda81a12016-07-12 06:39:48 +00001312 }
Rui Ueyama708019c2016-07-24 18:19:40 +00001313 return Lhs;
1314}
1315
1316uint64_t static getConstant(StringRef S) {
Michael J. Spencere2cc07b2016-08-17 02:10:51 +00001317 if (S == "COMMONPAGESIZE")
Rui Ueyama708019c2016-07-24 18:19:40 +00001318 return Target->PageSize;
Michael J. Spencere2cc07b2016-08-17 02:10:51 +00001319 if (S == "MAXPAGESIZE")
1320 return Target->MaxPageSize;
Rui Ueyama708019c2016-07-24 18:19:40 +00001321 error("unknown constant: " + S);
1322 return 0;
1323}
1324
Rui Ueyama626e0b02016-09-02 18:19:00 +00001325// Parses Tok as an integer. Returns true if successful.
1326// It recognizes hexadecimal (prefixed with "0x" or suffixed with "H")
1327// and decimal numbers. Decimal numbers may have "K" (kilo) or
1328// "M" (mega) prefixes.
George Rimar9f2f7ad2016-09-02 16:01:42 +00001329static bool readInteger(StringRef Tok, uint64_t &Result) {
Simon Atanasyaneaeafb22016-09-02 21:54:35 +00001330 if (Tok.startswith("-")) {
1331 if (!readInteger(Tok.substr(1), Result))
1332 return false;
1333 Result = -Result;
1334 return true;
1335 }
George Rimar9f2f7ad2016-09-02 16:01:42 +00001336 if (Tok.startswith_lower("0x"))
1337 return !Tok.substr(2).getAsInteger(16, Result);
1338 if (Tok.endswith_lower("H"))
1339 return !Tok.drop_back().getAsInteger(16, Result);
1340
1341 int Suffix = 1;
1342 if (Tok.endswith_lower("K")) {
1343 Suffix = 1024;
1344 Tok = Tok.drop_back();
1345 } else if (Tok.endswith_lower("M")) {
1346 Suffix = 1024 * 1024;
1347 Tok = Tok.drop_back();
1348 }
1349 if (Tok.getAsInteger(10, Result))
1350 return false;
1351 Result *= Suffix;
1352 return true;
1353}
1354
Rui Ueyama708019c2016-07-24 18:19:40 +00001355Expr ScriptParser::readPrimary() {
Rui Ueyama6ad7dfc2016-08-17 18:59:16 +00001356 if (peek() == "(")
1357 return readParenExpr();
Rui Ueyama708019c2016-07-24 18:19:40 +00001358
Rui Ueyama6ad7dfc2016-08-17 18:59:16 +00001359 StringRef Tok = next();
Rui Ueyama708019c2016-07-24 18:19:40 +00001360
Simon Atanasyaneaeafb22016-09-02 21:54:35 +00001361 if (Tok == "~") {
1362 Expr E = readPrimary();
1363 return [=](uint64_t Dot) { return ~E(Dot); };
1364 }
1365 if (Tok == "-") {
1366 Expr E = readPrimary();
1367 return [=](uint64_t Dot) { return -E(Dot); };
1368 }
1369
Rui Ueyama708019c2016-07-24 18:19:40 +00001370 // Built-in functions are parsed here.
1371 // https://sourceware.org/binutils/docs/ld/Builtin-Functions.html.
George Rimar96659df2016-08-30 09:54:01 +00001372 if (Tok == "ADDR") {
1373 expect("(");
1374 StringRef Name = next();
1375 expect(")");
George Rimar884e7862016-09-08 08:19:13 +00001376 return
1377 [=](uint64_t Dot) { return ScriptBase->getOutputSectionAddress(Name); };
George Rimar96659df2016-08-30 09:54:01 +00001378 }
George Rimareefa7582016-08-04 09:29:31 +00001379 if (Tok == "ASSERT")
1380 return readAssert();
Rui Ueyama708019c2016-07-24 18:19:40 +00001381 if (Tok == "ALIGN") {
Rui Ueyama6ad7dfc2016-08-17 18:59:16 +00001382 Expr E = readParenExpr();
Rui Ueyama708019c2016-07-24 18:19:40 +00001383 return [=](uint64_t Dot) { return alignTo(Dot, E(Dot)); };
1384 }
1385 if (Tok == "CONSTANT") {
1386 expect("(");
1387 StringRef Tok = next();
1388 expect(")");
1389 return [=](uint64_t Dot) { return getConstant(Tok); };
1390 }
Rafael Espindola54c145c2016-07-28 18:16:24 +00001391 if (Tok == "SEGMENT_START") {
1392 expect("(");
1393 next();
1394 expect(",");
1395 uint64_t Val;
Rafael Espindola3adbbc32016-09-15 13:36:44 +00001396 if (next().getAsInteger(0, Val))
1397 setError("integer expected");
Rafael Espindola54c145c2016-07-28 18:16:24 +00001398 expect(")");
1399 return [=](uint64_t Dot) { return Val; };
1400 }
Rui Ueyama708019c2016-07-24 18:19:40 +00001401 if (Tok == "DATA_SEGMENT_ALIGN") {
1402 expect("(");
1403 Expr E = readExpr();
1404 expect(",");
1405 readExpr();
1406 expect(")");
Rui Ueyamaf7791bb2016-07-26 19:34:10 +00001407 return [=](uint64_t Dot) { return alignTo(Dot, E(Dot)); };
Rui Ueyama708019c2016-07-24 18:19:40 +00001408 }
1409 if (Tok == "DATA_SEGMENT_END") {
1410 expect("(");
1411 expect(".");
1412 expect(")");
1413 return [](uint64_t Dot) { return Dot; };
1414 }
George Rimar276b4e62016-07-26 17:58:44 +00001415 // GNU linkers implements more complicated logic to handle
1416 // DATA_SEGMENT_RELRO_END. We instead ignore the arguments and just align to
1417 // the next page boundary for simplicity.
1418 if (Tok == "DATA_SEGMENT_RELRO_END") {
1419 expect("(");
Rafael Espindola97bdc722016-09-14 19:14:01 +00001420 readExpr();
George Rimar276b4e62016-07-26 17:58:44 +00001421 expect(",");
1422 readExpr();
1423 expect(")");
1424 return [](uint64_t Dot) { return alignTo(Dot, Target->PageSize); };
1425 }
George Rimar9e694502016-07-29 16:18:47 +00001426 if (Tok == "SIZEOF") {
1427 expect("(");
1428 StringRef Name = next();
1429 expect(")");
George Rimar884e7862016-09-08 08:19:13 +00001430 return [=](uint64_t Dot) { return ScriptBase->getOutputSectionSize(Name); };
George Rimar9e694502016-07-29 16:18:47 +00001431 }
Eugene Leviant36fac7f2016-09-08 09:08:30 +00001432 if (Tok == "ALIGNOF") {
1433 expect("(");
1434 StringRef Name = next();
1435 expect(")");
1436 return
1437 [=](uint64_t Dot) { return ScriptBase->getOutputSectionAlign(Name); };
1438 }
George Rimare32a3592016-08-10 07:59:34 +00001439 if (Tok == "SIZEOF_HEADERS")
George Rimar884e7862016-09-08 08:19:13 +00001440 return [=](uint64_t Dot) { return ScriptBase->getHeaderSize(); };
Rui Ueyama708019c2016-07-24 18:19:40 +00001441
George Rimar9f2f7ad2016-09-02 16:01:42 +00001442 // Tok is a literal number.
1443 uint64_t V;
1444 if (readInteger(Tok, V))
1445 return [=](uint64_t Dot) { return V; };
1446
1447 // Tok is a symbol name.
1448 if (Tok != "." && !isValidCIdentifier(Tok))
1449 setError("malformed number: " + Tok);
1450 return [=](uint64_t Dot) { return getSymbolValue(Tok, Dot); };
Rui Ueyama708019c2016-07-24 18:19:40 +00001451}
1452
1453Expr ScriptParser::readTernary(Expr Cond) {
1454 next();
1455 Expr L = readExpr();
1456 expect(":");
1457 Expr R = readExpr();
1458 return [=](uint64_t Dot) { return Cond(Dot) ? L(Dot) : R(Dot); };
1459}
1460
Rui Ueyama6ad7dfc2016-08-17 18:59:16 +00001461Expr ScriptParser::readParenExpr() {
1462 expect("(");
1463 Expr E = readExpr();
1464 expect(")");
1465 return E;
1466}
1467
Eugene Leviantbbe38602016-07-19 09:25:43 +00001468std::vector<StringRef> ScriptParser::readOutputSectionPhdrs() {
1469 std::vector<StringRef> Phdrs;
1470 while (!Error && peek().startswith(":")) {
1471 StringRef Tok = next();
1472 Tok = (Tok.size() == 1) ? next() : Tok.substr(1);
1473 if (Tok.empty()) {
1474 setError("section header name is empty");
1475 break;
1476 }
Rui Ueyama047404f2016-07-20 19:36:36 +00001477 Phdrs.push_back(Tok);
Eugene Leviantbbe38602016-07-19 09:25:43 +00001478 }
1479 return Phdrs;
1480}
1481
1482unsigned ScriptParser::readPhdrType() {
Eugene Leviantbbe38602016-07-19 09:25:43 +00001483 StringRef Tok = next();
Rui Ueyamab0f6c592016-07-20 19:36:38 +00001484 unsigned Ret = StringSwitch<unsigned>(Tok)
George Rimar6c55f0e2016-09-08 08:20:30 +00001485 .Case("PT_NULL", PT_NULL)
1486 .Case("PT_LOAD", PT_LOAD)
1487 .Case("PT_DYNAMIC", PT_DYNAMIC)
1488 .Case("PT_INTERP", PT_INTERP)
1489 .Case("PT_NOTE", PT_NOTE)
1490 .Case("PT_SHLIB", PT_SHLIB)
1491 .Case("PT_PHDR", PT_PHDR)
1492 .Case("PT_TLS", PT_TLS)
1493 .Case("PT_GNU_EH_FRAME", PT_GNU_EH_FRAME)
1494 .Case("PT_GNU_STACK", PT_GNU_STACK)
1495 .Case("PT_GNU_RELRO", PT_GNU_RELRO)
1496 .Default(-1);
Eugene Leviantbbe38602016-07-19 09:25:43 +00001497
Rui Ueyamab0f6c592016-07-20 19:36:38 +00001498 if (Ret == (unsigned)-1) {
1499 setError("invalid program header type: " + Tok);
1500 return PT_NULL;
1501 }
1502 return Ret;
Eugene Leviantbbe38602016-07-19 09:25:43 +00001503}
1504
Rui Ueyama95769b42016-08-31 20:03:54 +00001505void ScriptParser::readVersionDeclaration(StringRef VerStr) {
George Rimar20b65982016-08-31 09:08:26 +00001506 // Identifiers start at 2 because 0 and 1 are reserved
1507 // for VER_NDX_LOCAL and VER_NDX_GLOBAL constants.
1508 size_t VersionId = Config->VersionDefinitions.size() + 2;
1509 Config->VersionDefinitions.push_back({VerStr, VersionId});
1510
1511 if (skip("global:") || peek() != "local:")
1512 readGlobal(VerStr);
1513 if (skip("local:"))
1514 readLocal();
1515 expect("}");
1516
1517 // Each version may have a parent version. For example, "Ver2" defined as
1518 // "Ver2 { global: foo; local: *; } Ver1;" has "Ver1" as a parent. This
1519 // version hierarchy is, probably against your instinct, purely for human; the
1520 // runtime doesn't care about them at all. In LLD, we simply skip the token.
1521 if (!VerStr.empty() && peek() != ";")
1522 next();
1523 expect(";");
1524}
1525
1526void ScriptParser::readLocal() {
1527 Config->DefaultSymbolVersion = VER_NDX_LOCAL;
1528 expect("*");
1529 expect(";");
1530}
1531
1532void ScriptParser::readExtern(std::vector<SymbolVersion> *Globals) {
George Rimarcd574a52016-09-09 14:35:36 +00001533 expect("\"C++\"");
George Rimar20b65982016-08-31 09:08:26 +00001534 expect("{");
1535
1536 for (;;) {
1537 if (peek() == "}" || Error)
1538 break;
George Rimarcd574a52016-09-09 14:35:36 +00001539 bool HasWildcard = !peek().startswith("\"") && hasWildcard(peek());
1540 Globals->push_back({unquote(next()), true, HasWildcard});
George Rimar20b65982016-08-31 09:08:26 +00001541 expect(";");
1542 }
1543
1544 expect("}");
1545 expect(";");
1546}
1547
1548void ScriptParser::readGlobal(StringRef VerStr) {
1549 std::vector<SymbolVersion> *Globals;
1550 if (VerStr.empty())
1551 Globals = &Config->VersionScriptGlobals;
1552 else
1553 Globals = &Config->VersionDefinitions.back().Globals;
1554
1555 for (;;) {
1556 if (skip("extern"))
1557 readExtern(Globals);
1558
1559 StringRef Cur = peek();
1560 if (Cur == "}" || Cur == "local:" || Error)
1561 return;
1562 next();
George Rimarcd574a52016-09-09 14:35:36 +00001563 Globals->push_back({unquote(Cur), false, hasWildcard(Cur)});
George Rimar20b65982016-08-31 09:08:26 +00001564 expect(";");
1565 }
1566}
1567
Simon Atanasyan16b0cc92015-11-26 05:53:00 +00001568static bool isUnderSysroot(StringRef Path) {
1569 if (Config->Sysroot == "")
1570 return false;
1571 for (; !Path.empty(); Path = sys::path::parent_path(Path))
1572 if (sys::fs::equivalent(Config->Sysroot, Path))
1573 return true;
1574 return false;
1575}
1576
Rui Ueyama07320e42016-04-20 20:13:41 +00001577void elf::readLinkerScript(MemoryBufferRef MB) {
Simon Atanasyan16b0cc92015-11-26 05:53:00 +00001578 StringRef Path = MB.getBufferIdentifier();
George Rimar20b65982016-08-31 09:08:26 +00001579 ScriptParser(MB.getBuffer(), isUnderSysroot(Path)).readLinkerScript();
1580}
1581
1582void elf::readVersionScript(MemoryBufferRef MB) {
1583 ScriptParser(MB.getBuffer(), false).readVersionScript();
Rui Ueyamaf7c5fbb2015-09-30 17:23:26 +00001584}
Rui Ueyama1ebc8ed2016-02-12 21:47:28 +00001585
Rui Ueyama07320e42016-04-20 20:13:41 +00001586template class elf::LinkerScript<ELF32LE>;
1587template class elf::LinkerScript<ELF32BE>;
1588template class elf::LinkerScript<ELF64LE>;
1589template class elf::LinkerScript<ELF64BE>;