blob: 175ec5f668cbc600153a0d5ef1cf7c4088c78b2b [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 Rimarceae6302016-09-16 13:30:18 +0000112static bool fileMatches(const InputSectionDescription *Desc,
113 StringRef Filename) {
114 return const_cast<Regex &>(Desc->FileRe).match(Filename) &&
115 !const_cast<Regex &>(Desc->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 *)>
Rui Ueyama742c3832016-08-04 22:27:00 +0000134getComparator(SortKind K) {
George Rimar575208c2016-09-15 19:15:12 +0000135 if (K == SortByPriority)
136 return comparePriority;
Rui Ueyama742c3832016-08-04 22:27:00 +0000137 if (K == SortByName)
Rafael Espindolac0028d32016-09-08 20:47:52 +0000138 return compareName;
139 return compareAlignment;
Rui Ueyama742c3832016-08-04 22:27:00 +0000140}
George Rimar0702c4e2016-07-29 15:32:46 +0000141
George Rimar8f66df92016-08-12 20:38:20 +0000142static bool checkConstraint(uint64_t Flags, ConstraintKind Kind) {
143 bool RO = (Kind == ConstraintKind::ReadOnly);
144 bool RW = (Kind == ConstraintKind::ReadWrite);
145 bool Writable = Flags & SHF_WRITE;
Rui Ueyamaadcdb662016-09-06 22:50:48 +0000146 return !(RO && Writable) && !(RW && !Writable);
George Rimar8f66df92016-08-12 20:38:20 +0000147}
148
Rui Ueyama48c3f1c2016-08-12 00:27:23 +0000149template <class ELFT>
Rafael Espindolad3190792016-09-16 15:10:23 +0000150static bool matchConstraints(ArrayRef<InputSectionData *> Sections,
George Rimar06ae6832016-08-12 09:07:57 +0000151 ConstraintKind Kind) {
George Rimar8f66df92016-08-12 20:38:20 +0000152 if (Kind == ConstraintKind::NoConstraint)
153 return true;
Rafael Espindolad3190792016-09-16 15:10:23 +0000154 return llvm::all_of(Sections, [=](InputSectionData *Sec2) {
155 auto *Sec = static_cast<InputSectionBase<ELFT> *>(Sec2);
George Rimar8f66df92016-08-12 20:38:20 +0000156 return checkConstraint(Sec->getSectionHdr()->sh_flags, Kind);
George Rimar06ae6832016-08-12 09:07:57 +0000157 });
158}
159
Rafael Espindolad3190792016-09-16 15:10:23 +0000160// Compute and remember which sections the InputSectionDescription matches.
Rafael Espindolabe94e1b2016-09-14 14:32:08 +0000161template <class ELFT>
Rafael Espindolad3190792016-09-16 15:10:23 +0000162void LinkerScript<ELFT>::computeInputSections(InputSectionDescription *I,
163 ConstraintKind Constraint) {
George Rimarceae6302016-09-16 13:30:18 +0000164 const Regex &Re = I->SectionRe;
George Rimarceae6302016-09-16 13:30:18 +0000165 for (ObjectFile<ELFT> *F : Symtab<ELFT>::X->getObjectFiles())
166 if (fileMatches(I, sys::path::filename(F->getName())))
167 for (InputSectionBase<ELFT> *S : F->getSections())
168 if (!isDiscarded(S) && !S->OutSec &&
169 const_cast<Regex &>(Re).match(S->Name))
Rafael Espindolad3190792016-09-16 15:10:23 +0000170 I->Sections.push_back(S);
Rafael Espindolabe94e1b2016-09-14 14:32:08 +0000171
George Rimarceae6302016-09-16 13:30:18 +0000172 if (const_cast<Regex &>(Re).match("COMMON"))
Rafael Espindolad3190792016-09-16 15:10:23 +0000173 I->Sections.push_back(CommonInputSection<ELFT>::X);
174
175 if (!matchConstraints<ELFT>(I->Sections, Constraint)) {
176 I->Sections.clear();
177 return;
178 }
179
180 if (I->SortInner)
181 std::stable_sort(I->Sections.begin(), I->Sections.end(),
182 getComparator(I->SortInner));
183 if (I->SortOuter)
184 std::stable_sort(I->Sections.begin(), I->Sections.end(),
185 getComparator(I->SortOuter));
186
187 // We do not add duplicate input sections, so mark them with a dummy output
188 // section for now.
189 for (InputSectionData *S : I->Sections) {
190 auto *S2 = static_cast<InputSectionBase<ELFT> *>(S);
191 S2->OutSec = (OutputSectionBase<ELFT> *)-1;
192 }
Rafael Espindolabe94e1b2016-09-14 14:32:08 +0000193}
194
195template <class ELFT>
196void LinkerScript<ELFT>::discard(ArrayRef<InputSectionBase<ELFT> *> V) {
197 for (InputSectionBase<ELFT> *S : V) {
198 S->Live = false;
199 reportDiscarded(S);
200 }
201}
202
George Rimar06ae6832016-08-12 09:07:57 +0000203template <class ELFT>
Rui Ueyama0b9ce6a2016-08-12 03:16:56 +0000204std::vector<InputSectionBase<ELFT> *>
George Rimar06ae6832016-08-12 09:07:57 +0000205LinkerScript<ELFT>::createInputSectionList(OutputSectionCommand &OutCmd) {
Rui Ueyama0b9ce6a2016-08-12 03:16:56 +0000206 std::vector<InputSectionBase<ELFT> *> Ret;
Rui Ueyamae7f912c2016-08-03 21:12:09 +0000207
George Rimar06ae6832016-08-12 09:07:57 +0000208 for (const std::unique_ptr<BaseCommand> &Base : OutCmd.Commands) {
209 if (auto *OutCmd = dyn_cast<SymbolAssignment>(Base.get())) {
210 if (shouldDefine<ELFT>(OutCmd))
Eugene Leviantdb741e72016-09-07 07:08:43 +0000211 addSymbol<ELFT>(OutCmd);
Rui Ueyama0b9ce6a2016-08-12 03:16:56 +0000212 continue;
213 }
214
215 auto *Cmd = cast<InputSectionDescription>(Base.get());
Rafael Espindolad3190792016-09-16 15:10:23 +0000216 computeInputSections(Cmd, OutCmd.Constraint);
217 for (InputSectionData *S : Cmd->Sections)
218 Ret.push_back(static_cast<InputSectionBase<ELFT> *>(S));
Rui Ueyama0b9ce6a2016-08-12 03:16:56 +0000219 }
220 return Ret;
221}
222
Petr Hoseke5d3ca52016-08-31 15:31:17 +0000223template <class ELFT>
Rafael Espindola10897f12016-09-13 14:23:14 +0000224static SectionKey<ELFT::Is64Bits> createKey(InputSectionBase<ELFT> *C,
225 StringRef OutsecName) {
226 // When using linker script the merge rules are different.
227 // Unfortunately, linker scripts are name based. This means that expressions
228 // like *(.foo*) can refer to multiple input sections that would normally be
229 // placed in different output sections. We cannot put them in different
230 // output sections or we would produce wrong results for
231 // start = .; *(.foo.*) end = .; *(.bar)
232 // and a mapping of .foo1 and .bar1 to one section and .foo2 and .bar2 to
233 // another. The problem is that there is no way to layout those output
234 // sections such that the .foo sections are the only thing between the
235 // start and end symbols.
236
237 // An extra annoyance is that we cannot simply disable merging of the contents
238 // of SHF_MERGE sections, but our implementation requires one output section
239 // per "kind" (string or not, which size/aligment).
240 // Fortunately, creating symbols in the middle of a merge section is not
241 // supported by bfd or gold, so we can just create multiple section in that
242 // case.
243 const typename ELFT::Shdr *H = C->getSectionHdr();
244 typedef typename ELFT::uint uintX_t;
245 uintX_t Flags = H->sh_flags & (SHF_MERGE | SHF_STRINGS);
246
247 uintX_t Alignment = 0;
248 if (isa<MergeInputSection<ELFT>>(C))
249 Alignment = std::max(H->sh_addralign, H->sh_entsize);
250
251 return SectionKey<ELFT::Is64Bits>{OutsecName, /*Type*/ 0, Flags, Alignment};
252}
253
254template <class ELFT>
Eugene Leviant20d03192016-09-16 15:30:47 +0000255void LinkerScript<ELFT>::addSection(OutputSectionFactory<ELFT> &Factory,
256 InputSectionBase<ELFT> *Sec,
257 StringRef Name) {
258 OutputSectionBase<ELFT> *OutSec;
259 bool IsNew;
260 std::tie(OutSec, IsNew) = Factory.create(createKey(Sec, Name), Sec);
261 if (IsNew)
262 OutputSections->push_back(OutSec);
263 OutSec->addSection(Sec);
264}
265
266template <class ELFT>
267void LinkerScript<ELFT>::processCommands(OutputSectionFactory<ELFT> &Factory) {
Rafael Espindola28c15972016-09-13 13:00:06 +0000268
Rui Ueyama48c3f1c2016-08-12 00:27:23 +0000269 for (const std::unique_ptr<BaseCommand> &Base1 : Opt.Commands) {
Rui Ueyama2ab5f732016-08-12 03:33:04 +0000270 if (auto *Cmd = dyn_cast<SymbolAssignment>(Base1.get())) {
271 if (shouldDefine<ELFT>(Cmd))
272 addRegular<ELFT>(Cmd);
273 continue;
274 }
Eugene Leviant20d03192016-09-16 15:30:47 +0000275 if (auto *Cmd = dyn_cast<AssertCommand>(Base1.get())) {
276 // If we don't have SECTIONS then output sections have already been
277 // created by Writer<EFLT>. The LinkerScript<ELFT>::assignAddresses
278 // will not be called, so ASSERT should be evaluated now.
279 if (!Opt.HasSections)
280 Cmd->Expression(0);
281 continue;
282 }
Rui Ueyama2ab5f732016-08-12 03:33:04 +0000283
Eugene Leviantceabe802016-08-11 07:56:43 +0000284 if (auto *Cmd = dyn_cast<OutputSectionCommand>(Base1.get())) {
Rafael Espindola7bd37872016-09-12 16:05:16 +0000285 std::vector<InputSectionBase<ELFT> *> V = createInputSectionList(*Cmd);
286
Rui Ueyama48c3f1c2016-08-12 00:27:23 +0000287 if (Cmd->Name == "/DISCARD/") {
Rafael Espindola7bd37872016-09-12 16:05:16 +0000288 discard(V);
Rui Ueyama48c3f1c2016-08-12 00:27:23 +0000289 continue;
290 }
Eugene Leviantceabe802016-08-11 07:56:43 +0000291
Eugene Leviant97403d12016-09-01 09:55:57 +0000292 if (V.empty())
Rui Ueyama0b9ce6a2016-08-12 03:16:56 +0000293 continue;
294
George Rimardb24d9c2016-08-19 15:18:23 +0000295 for (InputSectionBase<ELFT> *Sec : V) {
Eugene Leviant20d03192016-09-16 15:30:47 +0000296 addSection(Factory, Sec, Cmd->Name);
297 if (uint32_t Subalign = Cmd->SubalignExpr ? Cmd->SubalignExpr(0) : 0)
George Rimardb24d9c2016-08-19 15:18:23 +0000298 Sec->Alignment = Subalign;
George Rimardb24d9c2016-08-19 15:18:23 +0000299 }
Eugene Leviantceabe802016-08-11 07:56:43 +0000300 }
Rui Ueyama48c3f1c2016-08-12 00:27:23 +0000301 }
Eugene Leviant20d03192016-09-16 15:30:47 +0000302}
Eugene Leviante63d81b2016-07-20 14:43:20 +0000303
Eugene Leviant20d03192016-09-16 15:30:47 +0000304template <class ELFT>
305void LinkerScript<ELFT>::createSections(OutputSectionFactory<ELFT> &Factory) {
306 processCommands(Factory);
Rui Ueyama0b9ce6a2016-08-12 03:16:56 +0000307 // Add orphan sections.
Eugene Leviant20d03192016-09-16 15:30:47 +0000308 for (ObjectFile<ELFT> *F : Symtab<ELFT>::X->getObjectFiles())
309 for (InputSectionBase<ELFT> *S : F->getSections())
310 if (!isDiscarded(S) && !S->OutSec)
311 addSection(Factory, S, getOutputSectionName(S));
Eugene Leviante63d81b2016-07-20 14:43:20 +0000312}
313
Eugene Leviantdb741e72016-09-07 07:08:43 +0000314// Sets value of a section-defined symbol. Two kinds of
315// symbols are processed: synthetic symbols, whose value
316// is an offset from beginning of section and regular
317// symbols whose value is absolute.
318template <class ELFT>
319static void assignSectionSymbol(SymbolAssignment *Cmd,
320 OutputSectionBase<ELFT> *Sec,
321 typename ELFT::uint Off) {
322 if (!Cmd->Sym)
323 return;
324
325 if (auto *Body = dyn_cast<DefinedSynthetic<ELFT>>(Cmd->Sym)) {
326 Body->Section = Sec;
327 Body->Value = Cmd->Expression(Sec->getVA() + Off) - Sec->getVA();
328 return;
329 }
330 auto *Body = cast<DefinedRegular<ELFT>>(Cmd->Sym);
331 Body->Value = Cmd->Expression(Sec->getVA() + Off);
332}
333
Rafael Espindolad3190792016-09-16 15:10:23 +0000334template <class ELFT> void LinkerScript<ELFT>::output(InputSection<ELFT> *S) {
335 if (!AlreadyOutputIS.insert(S).second)
336 return;
337 bool IsTbss =
338 (CurOutSec->getFlags() & SHF_TLS) && CurOutSec->getType() == SHT_NOBITS;
Eugene Leviant20889c52016-08-31 08:13:33 +0000339
Rafael Espindolad3190792016-09-16 15:10:23 +0000340 uintX_t Pos = IsTbss ? Dot + ThreadBssOffset : Dot;
341 Pos = alignTo(Pos, S->Alignment);
342 S->OutSecOff = Pos - CurOutSec->getVA();
343 Pos += S->getSize();
344
345 // Update output section size after adding each section. This is so that
346 // SIZEOF works correctly in the case below:
347 // .foo { *(.aaa) a = SIZEOF(.foo); *(.bbb) }
348 CurOutSec->setSize(Pos - CurOutSec->getVA());
349
350 if (!IsTbss)
351 Dot = Pos;
352}
353
354template <class ELFT> void LinkerScript<ELFT>::flush() {
355 if (auto *OutSec = dyn_cast_or_null<OutputSection<ELFT>>(CurOutSec)) {
356 for (InputSection<ELFT> *I : OutSec->Sections)
357 output(I);
358 AlreadyOutputOS.insert(CurOutSec);
Eugene Leviant20889c52016-08-31 08:13:33 +0000359 }
360}
361
362template <class ELFT>
Rafael Espindolad3190792016-09-16 15:10:23 +0000363void LinkerScript<ELFT>::switchTo(OutputSectionBase<ELFT> *Sec) {
364 if (CurOutSec == Sec)
365 return;
366 if (AlreadyOutputOS.count(Sec))
367 return;
368
369 flush();
370 CurOutSec = Sec;
371
372 Dot = alignTo(Dot, CurOutSec->getAlignment());
373 CurOutSec->setVA(Dot);
374}
375
376template <class ELFT> void LinkerScript<ELFT>::process(BaseCommand &Base) {
377 if (auto *AssignCmd = dyn_cast<SymbolAssignment>(&Base)) {
378 if (AssignCmd->Name == ".") {
379 // Update to location counter means update to section size.
380 Dot = AssignCmd->Expression(Dot);
381 CurOutSec->setSize(Dot - CurOutSec->getVA());
382 return;
383 }
384 assignSectionSymbol<ELFT>(AssignCmd, CurOutSec, Dot - CurOutSec->getVA());
Eugene Leviantceabe802016-08-11 07:56:43 +0000385 return;
Rui Ueyama2de509c2016-08-12 00:55:08 +0000386 }
Rafael Espindolad3190792016-09-16 15:10:23 +0000387 auto &ICmd = cast<InputSectionDescription>(Base);
388 for (InputSectionData *ID : ICmd.Sections) {
389 auto *IB = static_cast<InputSectionBase<ELFT> *>(ID);
390 switchTo(IB->OutSec);
391 if (auto *I = dyn_cast<InputSection<ELFT>>(IB))
392 output(I);
393 else if (AlreadyOutputOS.insert(CurOutSec).second)
394 Dot += CurOutSec->getSize();
Eugene Leviantceabe802016-08-11 07:56:43 +0000395 }
396}
397
George Rimar8f66df92016-08-12 20:38:20 +0000398template <class ELFT>
George Rimara14b13d2016-09-07 10:46:07 +0000399static std::vector<OutputSectionBase<ELFT> *>
400findSections(OutputSectionCommand &Cmd,
Rafael Espindolad3190792016-09-16 15:10:23 +0000401 const std::vector<OutputSectionBase<ELFT> *> &Sections) {
George Rimara14b13d2016-09-07 10:46:07 +0000402 std::vector<OutputSectionBase<ELFT> *> Ret;
403 for (OutputSectionBase<ELFT> *Sec : Sections)
404 if (Sec->getName() == Cmd.Name &&
405 checkConstraint(Sec->getFlags(), Cmd.Constraint))
406 Ret.push_back(Sec);
407 return Ret;
George Rimar8f66df92016-08-12 20:38:20 +0000408}
409
Rafael Espindolad3190792016-09-16 15:10:23 +0000410template <class ELFT>
411void LinkerScript<ELFT>::assignOffsets(OutputSectionCommand *Cmd) {
412 std::vector<OutputSectionBase<ELFT> *> Sections =
413 findSections(*Cmd, *OutputSections);
414 if (Sections.empty())
415 return;
416 switchTo(Sections[0]);
417
418 // Find the last section output location. We will output orphan sections
419 // there so that end symbols point to the correct location.
420 auto E = std::find_if(Cmd->Commands.rbegin(), Cmd->Commands.rend(),
421 [](const std::unique_ptr<BaseCommand> &Cmd) {
422 return !isa<SymbolAssignment>(*Cmd);
423 })
424 .base();
425 for (auto I = Cmd->Commands.begin(); I != E; ++I)
426 process(**I);
427 flush();
428 for (OutputSectionBase<ELFT> *Base : Sections) {
429 if (!AlreadyOutputOS.insert(Base).second)
430 continue;
431 switchTo(Base);
432 Dot += CurOutSec->getSize();
433 }
434 for (auto I = E, E = Cmd->Commands.end(); I != E; ++I)
435 process(**I);
436}
437
Rafael Espindolaa4b41dc2016-08-04 12:13:05 +0000438template <class ELFT> void LinkerScript<ELFT>::assignAddresses() {
George Rimar652852c2016-04-16 10:10:32 +0000439 // Orphan sections are sections present in the input files which
Rui Ueyama7c18c282016-04-18 21:00:40 +0000440 // are not explicitly placed into the output file by the linker script.
441 // We place orphan sections at end of file.
442 // Other linkers places them using some heuristics as described in
George Rimar652852c2016-04-16 10:10:32 +0000443 // https://sourceware.org/binutils/docs/ld/Orphan-Sections.html#Orphan-Sections.
Rui Ueyamae5cc6682016-08-12 00:36:56 +0000444 for (OutputSectionBase<ELFT> *Sec : *OutputSections) {
George Rimar652852c2016-04-16 10:10:32 +0000445 StringRef Name = Sec->getName();
Rui Ueyamac3e2a4b2016-04-21 20:30:00 +0000446 if (getSectionIndex(Name) == INT_MAX)
George Rimar076fe152016-07-21 06:43:01 +0000447 Opt.Commands.push_back(llvm::make_unique<OutputSectionCommand>(Name));
George Rimar652852c2016-04-16 10:10:32 +0000448 }
George Rimar652852c2016-04-16 10:10:32 +0000449
Rui Ueyama7c18c282016-04-18 21:00:40 +0000450 // Assign addresses as instructed by linker script SECTIONS sub-commands.
Rui Ueyama4f7500b2016-08-12 04:00:22 +0000451 Dot = getHeaderSize();
Eugene Leviant467c4d52016-07-01 10:27:36 +0000452 uintX_t MinVA = std::numeric_limits<uintX_t>::max();
George Rimar652852c2016-04-16 10:10:32 +0000453
George Rimar076fe152016-07-21 06:43:01 +0000454 for (const std::unique_ptr<BaseCommand> &Base : Opt.Commands) {
455 if (auto *Cmd = dyn_cast<SymbolAssignment>(Base.get())) {
Rui Ueyama8d083e62016-07-29 05:48:39 +0000456 if (Cmd->Name == ".") {
457 Dot = Cmd->Expression(Dot);
458 } else if (Cmd->Sym) {
459 cast<DefinedRegular<ELFT>>(Cmd->Sym)->Value = Cmd->Expression(Dot);
460 }
George Rimar652852c2016-04-16 10:10:32 +0000461 continue;
462 }
463
George Rimareefa7582016-08-04 09:29:31 +0000464 if (auto *Cmd = dyn_cast<AssertCommand>(Base.get())) {
465 Cmd->Expression(Dot);
466 continue;
467 }
468
George Rimar076fe152016-07-21 06:43:01 +0000469 auto *Cmd = cast<OutputSectionCommand>(Base.get());
George Rimar652852c2016-04-16 10:10:32 +0000470
Rafael Espindolad3190792016-09-16 15:10:23 +0000471 if (Cmd->AddrExpr)
472 Dot = Cmd->AddrExpr(Dot);
George Rimar58e5c4d2016-07-25 08:29:46 +0000473
Rafael Espindolad3190792016-09-16 15:10:23 +0000474 MinVA = std::min(MinVA, Dot);
475 assignOffsets(Cmd);
George Rimar652852c2016-04-16 10:10:32 +0000476 }
Rui Ueyama52c4e172016-07-01 10:42:25 +0000477
Rafael Espindolad3190792016-09-16 15:10:23 +0000478 for (OutputSectionBase<ELFT> *Sec : *OutputSections)
479 if (!(Sec->getFlags() & SHF_ALLOC))
480 Sec->setVA(0);
Rafael Espindola4ec013a2016-09-15 21:22:11 +0000481 uintX_t HeaderSize =
482 Out<ELFT>::ElfHeader->getSize() + Out<ELFT>::ProgramHeaders->getSize();
483 if (HeaderSize > MinVA)
484 fatal("Not enough space for ELF and program headers");
485
Rafael Espindola64c32d62016-07-07 14:28:47 +0000486 // ELF and Program headers need to be right before the first section in
George Rimarb91e7112016-07-19 07:42:07 +0000487 // memory. Set their addresses accordingly.
Rafael Espindola4ec013a2016-09-15 21:22:11 +0000488 MinVA = alignDown(MinVA - HeaderSize, Target->PageSize);
Eugene Leviant467c4d52016-07-01 10:27:36 +0000489 Out<ELFT>::ElfHeader->setVA(MinVA);
490 Out<ELFT>::ProgramHeaders->setVA(Out<ELFT>::ElfHeader->getSize() + MinVA);
George Rimar652852c2016-04-16 10:10:32 +0000491}
492
Rui Ueyama464daad2016-08-22 04:55:20 +0000493// Creates program headers as instructed by PHDRS linker script command.
Rui Ueyama07320e42016-04-20 20:13:41 +0000494template <class ELFT>
Rafael Espindolaa4b41dc2016-08-04 12:13:05 +0000495std::vector<PhdrEntry<ELFT>> LinkerScript<ELFT>::createPhdrs() {
Rui Ueyamaedebbdf2016-07-24 23:47:31 +0000496 std::vector<PhdrEntry<ELFT>> Ret;
Eugene Leviantbbe38602016-07-19 09:25:43 +0000497
Rui Ueyama464daad2016-08-22 04:55:20 +0000498 // Process PHDRS and FILEHDR keywords because they are not
499 // real output sections and cannot be added in the following loop.
Eugene Leviantbbe38602016-07-19 09:25:43 +0000500 for (const PhdrsCommand &Cmd : Opt.PhdrsCommands) {
Rui Ueyamaedebbdf2016-07-24 23:47:31 +0000501 Ret.emplace_back(Cmd.Type, Cmd.Flags == UINT_MAX ? PF_R : Cmd.Flags);
502 PhdrEntry<ELFT> &Phdr = Ret.back();
Eugene Leviantbbe38602016-07-19 09:25:43 +0000503
504 if (Cmd.HasFilehdr)
Rui Ueyamaadca2452016-07-23 14:18:48 +0000505 Phdr.add(Out<ELFT>::ElfHeader);
Eugene Leviantbbe38602016-07-19 09:25:43 +0000506 if (Cmd.HasPhdrs)
Rui Ueyamaadca2452016-07-23 14:18:48 +0000507 Phdr.add(Out<ELFT>::ProgramHeaders);
Eugene Leviant56b21c82016-09-09 09:46:16 +0000508
509 if (Cmd.LMAExpr) {
510 Phdr.H.p_paddr = Cmd.LMAExpr(0);
511 Phdr.HasLMA = true;
512 }
Eugene Leviantbbe38602016-07-19 09:25:43 +0000513 }
514
Rui Ueyama464daad2016-08-22 04:55:20 +0000515 // Add output sections to program headers.
Rui Ueyamaedebbdf2016-07-24 23:47:31 +0000516 PhdrEntry<ELFT> *Load = nullptr;
517 uintX_t Flags = PF_R;
Rui Ueyama464daad2016-08-22 04:55:20 +0000518 for (OutputSectionBase<ELFT> *Sec : *OutputSections) {
Eugene Leviantbbe38602016-07-19 09:25:43 +0000519 if (!(Sec->getFlags() & SHF_ALLOC))
520 break;
521
Rui Ueyamaedebbdf2016-07-24 23:47:31 +0000522 std::vector<size_t> PhdrIds = getPhdrIndices(Sec->getName());
Eugene Leviantbbe38602016-07-19 09:25:43 +0000523 if (!PhdrIds.empty()) {
524 // Assign headers specified by linker script
525 for (size_t Id : PhdrIds) {
Rui Ueyamaedebbdf2016-07-24 23:47:31 +0000526 Ret[Id].add(Sec);
Eugene Leviant865bf862016-07-21 10:43:25 +0000527 if (Opt.PhdrsCommands[Id].Flags == UINT_MAX)
Rafael Espindola0b113672016-07-27 14:10:56 +0000528 Ret[Id].H.p_flags |= Sec->getPhdrFlags();
Eugene Leviantbbe38602016-07-19 09:25:43 +0000529 }
530 } else {
531 // If we have no load segment or flags've changed then we want new load
532 // segment.
Rafael Espindola0b113672016-07-27 14:10:56 +0000533 uintX_t NewFlags = Sec->getPhdrFlags();
Eugene Leviantbbe38602016-07-19 09:25:43 +0000534 if (Load == nullptr || Flags != NewFlags) {
Rui Ueyamaedebbdf2016-07-24 23:47:31 +0000535 Load = &*Ret.emplace(Ret.end(), PT_LOAD, NewFlags);
Eugene Leviantbbe38602016-07-19 09:25:43 +0000536 Flags = NewFlags;
537 }
Rui Ueyama18f084f2016-07-20 19:36:41 +0000538 Load->add(Sec);
Eugene Leviantbbe38602016-07-19 09:25:43 +0000539 }
Eugene Leviantbbe38602016-07-19 09:25:43 +0000540 }
Rui Ueyamaedebbdf2016-07-24 23:47:31 +0000541 return Ret;
Eugene Leviantbbe38602016-07-19 09:25:43 +0000542}
543
Eugene Leviantf9bc3bd2016-08-16 06:40:58 +0000544template <class ELFT> bool LinkerScript<ELFT>::ignoreInterpSection() {
545 // Ignore .interp section in case we have PHDRS specification
546 // and PT_INTERP isn't listed.
547 return !Opt.PhdrsCommands.empty() &&
548 llvm::find_if(Opt.PhdrsCommands, [](const PhdrsCommand &Cmd) {
549 return Cmd.Type == PT_INTERP;
550 }) == Opt.PhdrsCommands.end();
551}
552
Eugene Leviantbbe38602016-07-19 09:25:43 +0000553template <class ELFT>
Rui Ueyama07320e42016-04-20 20:13:41 +0000554ArrayRef<uint8_t> LinkerScript<ELFT>::getFiller(StringRef Name) {
George Rimarf6c3cce2016-07-21 07:48:54 +0000555 for (const std::unique_ptr<BaseCommand> &Base : Opt.Commands)
556 if (auto *Cmd = dyn_cast<OutputSectionCommand>(Base.get()))
557 if (Cmd->Name == Name)
558 return Cmd->Filler;
559 return {};
George Rimare2ee72b2016-02-26 14:48:31 +0000560}
561
George Rimar206fffa2016-08-17 08:16:57 +0000562template <class ELFT> Expr LinkerScript<ELFT>::getLma(StringRef Name) {
George Rimar8ceadb32016-08-17 07:44:19 +0000563 for (const std::unique_ptr<BaseCommand> &Base : Opt.Commands)
564 if (auto *Cmd = dyn_cast<OutputSectionCommand>(Base.get()))
565 if (Cmd->LmaExpr && Cmd->Name == Name)
566 return Cmd->LmaExpr;
567 return {};
568}
569
Rui Ueyamac3e2a4b2016-04-21 20:30:00 +0000570// Returns the index of the given section name in linker script
571// SECTIONS commands. Sections are laid out as the same order as they
572// were in the script. If a given name did not appear in the script,
573// it returns INT_MAX, so that it will be laid out at end of file.
George Rimar076fe152016-07-21 06:43:01 +0000574template <class ELFT> int LinkerScript<ELFT>::getSectionIndex(StringRef Name) {
Rui Ueyamaf510fa62016-07-26 00:21:15 +0000575 int I = 0;
576 for (std::unique_ptr<BaseCommand> &Base : Opt.Commands) {
577 if (auto *Cmd = dyn_cast<OutputSectionCommand>(Base.get()))
578 if (Cmd->Name == Name)
579 return I;
580 ++I;
581 }
582 return INT_MAX;
George Rimar71b26e92016-04-21 10:22:02 +0000583}
584
585// A compartor to sort output sections. Returns -1 or 1 if
586// A or B are mentioned in linker script. Otherwise, returns 0.
Rui Ueyama07320e42016-04-20 20:13:41 +0000587template <class ELFT>
588int LinkerScript<ELFT>::compareSections(StringRef A, StringRef B) {
Rui Ueyamac3e2a4b2016-04-21 20:30:00 +0000589 int I = getSectionIndex(A);
590 int J = getSectionIndex(B);
591 if (I == INT_MAX && J == INT_MAX)
Rui Ueyama717677a2016-02-11 21:17:59 +0000592 return 0;
593 return I < J ? -1 : 1;
594}
595
Eugene Leviantbbe38602016-07-19 09:25:43 +0000596template <class ELFT> bool LinkerScript<ELFT>::hasPhdrsCommands() {
597 return !Opt.PhdrsCommands.empty();
598}
599
George Rimar9e694502016-07-29 16:18:47 +0000600template <class ELFT>
George Rimar884e7862016-09-08 08:19:13 +0000601uint64_t LinkerScript<ELFT>::getOutputSectionAddress(StringRef Name) {
George Rimar96659df2016-08-30 09:54:01 +0000602 for (OutputSectionBase<ELFT> *Sec : *OutputSections)
603 if (Sec->getName() == Name)
604 return Sec->getVA();
605 error("undefined section " + Name);
606 return 0;
607}
608
609template <class ELFT>
George Rimar884e7862016-09-08 08:19:13 +0000610uint64_t LinkerScript<ELFT>::getOutputSectionSize(StringRef Name) {
George Rimar9e694502016-07-29 16:18:47 +0000611 for (OutputSectionBase<ELFT> *Sec : *OutputSections)
612 if (Sec->getName() == Name)
613 return Sec->getSize();
614 error("undefined section " + Name);
615 return 0;
616}
617
Eugene Leviant36fac7f2016-09-08 09:08:30 +0000618template <class ELFT>
619uint64_t LinkerScript<ELFT>::getOutputSectionAlign(StringRef Name) {
620 for (OutputSectionBase<ELFT> *Sec : *OutputSections)
621 if (Sec->getName() == Name)
622 return Sec->getAlignment();
623 error("undefined section " + Name);
624 return 0;
625}
626
George Rimar884e7862016-09-08 08:19:13 +0000627template <class ELFT> uint64_t LinkerScript<ELFT>::getHeaderSize() {
George Rimare32a3592016-08-10 07:59:34 +0000628 return Out<ELFT>::ElfHeader->getSize() + Out<ELFT>::ProgramHeaders->getSize();
629}
630
George Rimar884e7862016-09-08 08:19:13 +0000631template <class ELFT> uint64_t LinkerScript<ELFT>::getSymbolValue(StringRef S) {
632 if (SymbolBody *B = Symtab<ELFT>::X->find(S))
633 return B->getVA<ELFT>();
634 error("symbol not found: " + S);
635 return 0;
636}
637
Eugene Leviantbbe38602016-07-19 09:25:43 +0000638// Returns indices of ELF headers containing specific section, identified
639// by Name. Each index is a zero based number of ELF header listed within
640// PHDRS {} script block.
641template <class ELFT>
Rui Ueyamaedebbdf2016-07-24 23:47:31 +0000642std::vector<size_t> LinkerScript<ELFT>::getPhdrIndices(StringRef SectionName) {
George Rimar076fe152016-07-21 06:43:01 +0000643 for (const std::unique_ptr<BaseCommand> &Base : Opt.Commands) {
644 auto *Cmd = dyn_cast<OutputSectionCommand>(Base.get());
Rui Ueyamaedebbdf2016-07-24 23:47:31 +0000645 if (!Cmd || Cmd->Name != SectionName)
George Rimar31d842f2016-07-20 16:43:03 +0000646 continue;
647
Rui Ueyama29c5a2a2016-07-26 00:27:36 +0000648 std::vector<size_t> Ret;
649 for (StringRef PhdrName : Cmd->Phdrs)
650 Ret.push_back(getPhdrIndex(PhdrName));
651 return Ret;
Eugene Leviantbbe38602016-07-19 09:25:43 +0000652 }
George Rimar31d842f2016-07-20 16:43:03 +0000653 return {};
Eugene Leviantbbe38602016-07-19 09:25:43 +0000654}
655
Rui Ueyama29c5a2a2016-07-26 00:27:36 +0000656template <class ELFT>
657size_t LinkerScript<ELFT>::getPhdrIndex(StringRef PhdrName) {
658 size_t I = 0;
659 for (PhdrsCommand &Cmd : Opt.PhdrsCommands) {
660 if (Cmd.Name == PhdrName)
661 return I;
662 ++I;
663 }
664 error("section header '" + PhdrName + "' is not listed in PHDRS");
665 return 0;
666}
667
Rui Ueyama07320e42016-04-20 20:13:41 +0000668class elf::ScriptParser : public ScriptParserBase {
George Rimarc3794e52016-02-24 09:21:47 +0000669 typedef void (ScriptParser::*Handler)();
670
Rui Ueyamaf7c5fbb2015-09-30 17:23:26 +0000671public:
Rui Ueyama07320e42016-04-20 20:13:41 +0000672 ScriptParser(StringRef S, bool B) : ScriptParserBase(S), IsUnderSysroot(B) {}
George Rimarf23b2322016-02-19 10:45:45 +0000673
George Rimar20b65982016-08-31 09:08:26 +0000674 void readLinkerScript();
675 void readVersionScript();
Rui Ueyamaf7c5fbb2015-09-30 17:23:26 +0000676
677private:
Rui Ueyama52a15092015-10-11 03:28:42 +0000678 void addFile(StringRef Path);
679
Rui Ueyamaf7c5fbb2015-09-30 17:23:26 +0000680 void readAsNeeded();
Denis Protivensky90c50992015-10-08 06:48:38 +0000681 void readEntry();
George Rimar83f406c2015-10-19 17:35:12 +0000682 void readExtern();
Rui Ueyamaf7c5fbb2015-09-30 17:23:26 +0000683 void readGroup();
Rui Ueyama31aa1f82015-10-11 01:31:55 +0000684 void readInclude();
Rui Ueyamaee592822015-10-07 00:25:09 +0000685 void readOutput();
Davide Italiano9159ce92015-10-12 21:50:08 +0000686 void readOutputArch();
Rui Ueyamaf7c5fbb2015-09-30 17:23:26 +0000687 void readOutputFormat();
Eugene Leviantbbe38602016-07-19 09:25:43 +0000688 void readPhdrs();
Davide Italiano68a39a62015-10-08 17:51:41 +0000689 void readSearchDir();
Denis Protivensky8e3b38a2015-11-12 09:52:08 +0000690 void readSections();
Rui Ueyama95769b42016-08-31 20:03:54 +0000691 void readVersion();
692 void readVersionScriptCommand();
Denis Protivensky8e3b38a2015-11-12 09:52:08 +0000693
Rui Ueyama113cdec2016-07-24 23:05:57 +0000694 SymbolAssignment *readAssignment(StringRef Name);
George Rimarff1f29e2016-09-06 13:51:57 +0000695 std::vector<uint8_t> readFill();
Rui Ueyama10416562016-08-04 02:03:27 +0000696 OutputSectionCommand *readOutputSectionDescription(StringRef OutSec);
George Rimarff1f29e2016-09-06 13:51:57 +0000697 std::vector<uint8_t> readOutputSectionFiller(StringRef Tok);
Eugene Leviantbbe38602016-07-19 09:25:43 +0000698 std::vector<StringRef> readOutputSectionPhdrs();
George Rimara2496cb2016-08-30 09:46:59 +0000699 InputSectionDescription *readInputSectionDescription(StringRef Tok);
George Rimarc91930a2016-09-02 21:17:20 +0000700 Regex readFilePatterns();
George Rimara2496cb2016-08-30 09:46:59 +0000701 InputSectionDescription *readInputSectionRules(StringRef FilePattern);
Eugene Leviantbbe38602016-07-19 09:25:43 +0000702 unsigned readPhdrType();
Rui Ueyama742c3832016-08-04 22:27:00 +0000703 SortKind readSortKind();
Petr Hoseka35e39c2016-08-16 01:11:16 +0000704 SymbolAssignment *readProvideHidden(bool Provide, bool Hidden);
Eugene Leviantdb741e72016-09-07 07:08:43 +0000705 SymbolAssignment *readProvideOrAssignment(StringRef Tok, bool MakeAbsolute);
George Rimar03fc0102016-07-28 07:18:23 +0000706 void readSort();
George Rimareefa7582016-08-04 09:29:31 +0000707 Expr readAssert();
Rui Ueyama708019c2016-07-24 18:19:40 +0000708
709 Expr readExpr();
710 Expr readExpr1(Expr Lhs, int MinPrec);
711 Expr readPrimary();
712 Expr readTernary(Expr Cond);
Rui Ueyama6ad7dfc2016-08-17 18:59:16 +0000713 Expr readParenExpr();
Rui Ueyamaf7c5fbb2015-09-30 17:23:26 +0000714
George Rimar20b65982016-08-31 09:08:26 +0000715 // For parsing version script.
716 void readExtern(std::vector<SymbolVersion> *Globals);
Rui Ueyama95769b42016-08-31 20:03:54 +0000717 void readVersionDeclaration(StringRef VerStr);
George Rimar20b65982016-08-31 09:08:26 +0000718 void readGlobal(StringRef VerStr);
719 void readLocal();
720
Rui Ueyama07320e42016-04-20 20:13:41 +0000721 ScriptConfiguration &Opt = *ScriptConfig;
722 StringSaver Saver = {ScriptConfig->Alloc};
Simon Atanasyan16b0cc92015-11-26 05:53:00 +0000723 bool IsUnderSysroot;
Rui Ueyamaf7c5fbb2015-09-30 17:23:26 +0000724};
Rui Ueyamaf7c5fbb2015-09-30 17:23:26 +0000725
George Rimar20b65982016-08-31 09:08:26 +0000726void ScriptParser::readVersionScript() {
Rui Ueyama95769b42016-08-31 20:03:54 +0000727 readVersionScriptCommand();
728 if (!atEOF())
729 setError("EOF expected, but got " + next());
730}
731
732void ScriptParser::readVersionScriptCommand() {
George Rimar20b65982016-08-31 09:08:26 +0000733 if (skip("{")) {
Rui Ueyama95769b42016-08-31 20:03:54 +0000734 readVersionDeclaration("");
George Rimar20b65982016-08-31 09:08:26 +0000735 return;
736 }
737
Rui Ueyama95769b42016-08-31 20:03:54 +0000738 while (!atEOF() && !Error && peek() != "}") {
George Rimar20b65982016-08-31 09:08:26 +0000739 StringRef VerStr = next();
740 if (VerStr == "{") {
Rui Ueyama95769b42016-08-31 20:03:54 +0000741 setError("anonymous version definition is used in "
742 "combination with other version definitions");
George Rimar20b65982016-08-31 09:08:26 +0000743 return;
744 }
745 expect("{");
Rui Ueyama95769b42016-08-31 20:03:54 +0000746 readVersionDeclaration(VerStr);
George Rimar20b65982016-08-31 09:08:26 +0000747 }
748}
749
Rui Ueyama95769b42016-08-31 20:03:54 +0000750void ScriptParser::readVersion() {
751 expect("{");
752 readVersionScriptCommand();
753 expect("}");
754}
755
George Rimar20b65982016-08-31 09:08:26 +0000756void ScriptParser::readLinkerScript() {
Rui Ueyamaf7c5fbb2015-09-30 17:23:26 +0000757 while (!atEOF()) {
758 StringRef Tok = next();
Rui Ueyamaa27eecc2016-09-02 18:52:41 +0000759 if (Tok == ";")
760 continue;
761
Eugene Leviant20d03192016-09-16 15:30:47 +0000762 if (Tok == "ASSERT") {
763 Opt.Commands.emplace_back(new AssertCommand(readAssert()));
764 } else if (Tok == "ENTRY") {
Rui Ueyamaa27eecc2016-09-02 18:52:41 +0000765 readEntry();
766 } else if (Tok == "EXTERN") {
767 readExtern();
768 } else if (Tok == "GROUP" || Tok == "INPUT") {
769 readGroup();
770 } else if (Tok == "INCLUDE") {
771 readInclude();
772 } else if (Tok == "OUTPUT") {
773 readOutput();
774 } else if (Tok == "OUTPUT_ARCH") {
775 readOutputArch();
776 } else if (Tok == "OUTPUT_FORMAT") {
777 readOutputFormat();
778 } else if (Tok == "PHDRS") {
779 readPhdrs();
780 } else if (Tok == "SEARCH_DIR") {
781 readSearchDir();
782 } else if (Tok == "SECTIONS") {
783 readSections();
784 } else if (Tok == "VERSION") {
785 readVersion();
Eugene Leviantdb741e72016-09-07 07:08:43 +0000786 } else if (SymbolAssignment *Cmd = readProvideOrAssignment(Tok, true)) {
Eugene Leviant20d03192016-09-16 15:30:47 +0000787 Opt.Commands.emplace_back(Cmd);
Petr Hoseke5d3ca52016-08-31 15:31:17 +0000788 } else {
George Rimar57610422016-03-11 14:43:02 +0000789 setError("unknown directive: " + Tok);
Petr Hoseke5d3ca52016-08-31 15:31:17 +0000790 }
Rui Ueyamaf7c5fbb2015-09-30 17:23:26 +0000791 }
792}
793
Rui Ueyama717677a2016-02-11 21:17:59 +0000794void ScriptParser::addFile(StringRef S) {
Simon Atanasyan16b0cc92015-11-26 05:53:00 +0000795 if (IsUnderSysroot && S.startswith("/")) {
796 SmallString<128> Path;
797 (Config->Sysroot + S).toStringRef(Path);
798 if (sys::fs::exists(Path)) {
799 Driver->addFile(Saver.save(Path.str()));
800 return;
801 }
802 }
803
Rui Ueyamaf03f3cc2015-10-13 00:09:21 +0000804 if (sys::path::is_absolute(S)) {
Rui Ueyama52a15092015-10-11 03:28:42 +0000805 Driver->addFile(S);
806 } else if (S.startswith("=")) {
807 if (Config->Sysroot.empty())
808 Driver->addFile(S.substr(1));
809 else
810 Driver->addFile(Saver.save(Config->Sysroot + "/" + S.substr(1)));
811 } else if (S.startswith("-l")) {
Rui Ueyama21eecb42016-02-02 21:13:09 +0000812 Driver->addLibrary(S.substr(2));
Simon Atanasyana1b8fc32015-11-26 20:23:46 +0000813 } else if (sys::fs::exists(S)) {
814 Driver->addFile(S);
Rui Ueyama52a15092015-10-11 03:28:42 +0000815 } else {
816 std::string Path = findFromSearchPaths(S);
817 if (Path.empty())
George Rimar777f9632016-03-12 08:31:34 +0000818 setError("unable to find " + S);
Rui Ueyama025d59b2016-02-02 20:27:59 +0000819 else
820 Driver->addFile(Saver.save(Path));
Rui Ueyama52a15092015-10-11 03:28:42 +0000821 }
822}
823
Rui Ueyama717677a2016-02-11 21:17:59 +0000824void ScriptParser::readAsNeeded() {
Rui Ueyamaf7c5fbb2015-09-30 17:23:26 +0000825 expect("(");
Rui Ueyama35da9b62015-10-11 20:59:12 +0000826 bool Orig = Config->AsNeeded;
827 Config->AsNeeded = true;
Rui Ueyamaa2acc932016-08-05 01:25:45 +0000828 while (!Error && !skip(")"))
George Rimarcd574a52016-09-09 14:35:36 +0000829 addFile(unquote(next()));
Rui Ueyama35da9b62015-10-11 20:59:12 +0000830 Config->AsNeeded = Orig;
Rui Ueyamaf7c5fbb2015-09-30 17:23:26 +0000831}
832
Rui Ueyama717677a2016-02-11 21:17:59 +0000833void ScriptParser::readEntry() {
Denis Protivensky90c50992015-10-08 06:48:38 +0000834 // -e <symbol> takes predecence over ENTRY(<symbol>).
835 expect("(");
836 StringRef Tok = next();
837 if (Config->Entry.empty())
838 Config->Entry = Tok;
839 expect(")");
840}
841
Rui Ueyama717677a2016-02-11 21:17:59 +0000842void ScriptParser::readExtern() {
George Rimar83f406c2015-10-19 17:35:12 +0000843 expect("(");
Rui Ueyamaa2acc932016-08-05 01:25:45 +0000844 while (!Error && !skip(")"))
845 Config->Undefined.push_back(next());
George Rimar83f406c2015-10-19 17:35:12 +0000846}
847
Rui Ueyama717677a2016-02-11 21:17:59 +0000848void ScriptParser::readGroup() {
Rui Ueyamaf7c5fbb2015-09-30 17:23:26 +0000849 expect("(");
Rui Ueyamaa2acc932016-08-05 01:25:45 +0000850 while (!Error && !skip(")")) {
Rui Ueyamaf7c5fbb2015-09-30 17:23:26 +0000851 StringRef Tok = next();
Rui Ueyamaa2acc932016-08-05 01:25:45 +0000852 if (Tok == "AS_NEEDED")
Rui Ueyamaf7c5fbb2015-09-30 17:23:26 +0000853 readAsNeeded();
Rui Ueyamaa2acc932016-08-05 01:25:45 +0000854 else
George Rimarcd574a52016-09-09 14:35:36 +0000855 addFile(unquote(Tok));
Rui Ueyamaf7c5fbb2015-09-30 17:23:26 +0000856 }
857}
858
Rui Ueyama717677a2016-02-11 21:17:59 +0000859void ScriptParser::readInclude() {
Rui Ueyama31aa1f82015-10-11 01:31:55 +0000860 StringRef Tok = next();
George Rimarcd574a52016-09-09 14:35:36 +0000861 auto MBOrErr = MemoryBuffer::getFile(unquote(Tok));
Rui Ueyama025d59b2016-02-02 20:27:59 +0000862 if (!MBOrErr) {
George Rimar57610422016-03-11 14:43:02 +0000863 setError("cannot open " + Tok);
Rui Ueyama025d59b2016-02-02 20:27:59 +0000864 return;
865 }
Rui Ueyama31aa1f82015-10-11 01:31:55 +0000866 std::unique_ptr<MemoryBuffer> &MB = *MBOrErr;
Rui Ueyamaa47ee682015-10-11 01:53:04 +0000867 StringRef S = Saver.save(MB->getMemBufferRef().getBuffer());
868 std::vector<StringRef> V = tokenize(S);
Rui Ueyama31aa1f82015-10-11 01:31:55 +0000869 Tokens.insert(Tokens.begin() + Pos, V.begin(), V.end());
Rui Ueyama31aa1f82015-10-11 01:31:55 +0000870}
871
Rui Ueyama717677a2016-02-11 21:17:59 +0000872void ScriptParser::readOutput() {
Rui Ueyamaee592822015-10-07 00:25:09 +0000873 // -o <file> takes predecence over OUTPUT(<file>).
874 expect("(");
875 StringRef Tok = next();
876 if (Config->OutputFile.empty())
George Rimarcd574a52016-09-09 14:35:36 +0000877 Config->OutputFile = unquote(Tok);
Rui Ueyamaee592822015-10-07 00:25:09 +0000878 expect(")");
879}
880
Rui Ueyama717677a2016-02-11 21:17:59 +0000881void ScriptParser::readOutputArch() {
Davide Italiano9159ce92015-10-12 21:50:08 +0000882 // Error checking only for now.
883 expect("(");
884 next();
885 expect(")");
886}
887
Rui Ueyama717677a2016-02-11 21:17:59 +0000888void ScriptParser::readOutputFormat() {
Rui Ueyamaf7c5fbb2015-09-30 17:23:26 +0000889 // Error checking only for now.
890 expect("(");
891 next();
Davide Italiano6836c612015-10-12 21:08:41 +0000892 StringRef Tok = next();
893 if (Tok == ")")
George Rimar6c55f0e2016-09-08 08:20:30 +0000894 return;
Rui Ueyama025d59b2016-02-02 20:27:59 +0000895 if (Tok != ",") {
George Rimar57610422016-03-11 14:43:02 +0000896 setError("unexpected token: " + Tok);
Rui Ueyama025d59b2016-02-02 20:27:59 +0000897 return;
898 }
Davide Italiano6836c612015-10-12 21:08:41 +0000899 next();
900 expect(",");
901 next();
Rui Ueyamaf7c5fbb2015-09-30 17:23:26 +0000902 expect(")");
903}
904
Eugene Leviantbbe38602016-07-19 09:25:43 +0000905void ScriptParser::readPhdrs() {
906 expect("{");
907 while (!Error && !skip("}")) {
908 StringRef Tok = next();
Eugene Leviant56b21c82016-09-09 09:46:16 +0000909 Opt.PhdrsCommands.push_back(
910 {Tok, PT_NULL, false, false, UINT_MAX, nullptr});
Eugene Leviantbbe38602016-07-19 09:25:43 +0000911 PhdrsCommand &PhdrCmd = Opt.PhdrsCommands.back();
912
913 PhdrCmd.Type = readPhdrType();
914 do {
915 Tok = next();
916 if (Tok == ";")
917 break;
918 if (Tok == "FILEHDR")
919 PhdrCmd.HasFilehdr = true;
920 else if (Tok == "PHDRS")
921 PhdrCmd.HasPhdrs = true;
Eugene Leviant56b21c82016-09-09 09:46:16 +0000922 else if (Tok == "AT")
923 PhdrCmd.LMAExpr = readParenExpr();
Eugene Leviant865bf862016-07-21 10:43:25 +0000924 else if (Tok == "FLAGS") {
925 expect("(");
Rafael Espindolaeb685cd2016-08-02 22:14:57 +0000926 // Passing 0 for the value of dot is a bit of a hack. It means that
927 // we accept expressions like ".|1".
928 PhdrCmd.Flags = readExpr()(0);
Eugene Leviant865bf862016-07-21 10:43:25 +0000929 expect(")");
930 } else
Eugene Leviantbbe38602016-07-19 09:25:43 +0000931 setError("unexpected header attribute: " + Tok);
932 } while (!Error);
933 }
934}
935
Rui Ueyama717677a2016-02-11 21:17:59 +0000936void ScriptParser::readSearchDir() {
Davide Italiano68a39a62015-10-08 17:51:41 +0000937 expect("(");
Rui Ueyama86c5fb82016-09-08 23:26:54 +0000938 StringRef Tok = next();
Rui Ueyama6c7ad132016-09-02 19:20:33 +0000939 if (!Config->Nostdlib)
George Rimarcd574a52016-09-09 14:35:36 +0000940 Config->SearchPaths.push_back(unquote(Tok));
Davide Italiano68a39a62015-10-08 17:51:41 +0000941 expect(")");
942}
943
Rui Ueyama717677a2016-02-11 21:17:59 +0000944void ScriptParser::readSections() {
Eugene Leviante05336ff2016-09-14 08:32:36 +0000945 Opt.HasSections = true;
Denis Protivensky8e3b38a2015-11-12 09:52:08 +0000946 expect("{");
George Rimar652852c2016-04-16 10:10:32 +0000947 while (!Error && !skip("}")) {
Rui Ueyama113cdec2016-07-24 23:05:57 +0000948 StringRef Tok = next();
Eugene Leviantdb741e72016-09-07 07:08:43 +0000949 BaseCommand *Cmd = readProvideOrAssignment(Tok, true);
Eugene Leviantceabe802016-08-11 07:56:43 +0000950 if (!Cmd) {
951 if (Tok == "ASSERT")
952 Cmd = new AssertCommand(readAssert());
953 else
954 Cmd = readOutputSectionDescription(Tok);
Rui Ueyama708019c2016-07-24 18:19:40 +0000955 }
Rui Ueyama10416562016-08-04 02:03:27 +0000956 Opt.Commands.emplace_back(Cmd);
George Rimar652852c2016-04-16 10:10:32 +0000957 }
Denis Protivensky8e3b38a2015-11-12 09:52:08 +0000958}
959
Rui Ueyama708019c2016-07-24 18:19:40 +0000960static int precedence(StringRef Op) {
961 return StringSwitch<int>(Op)
962 .Case("*", 4)
963 .Case("/", 4)
964 .Case("+", 3)
965 .Case("-", 3)
966 .Case("<", 2)
967 .Case(">", 2)
968 .Case(">=", 2)
969 .Case("<=", 2)
970 .Case("==", 2)
971 .Case("!=", 2)
972 .Case("&", 1)
Rafael Espindolacc3dd622016-08-22 21:33:35 +0000973 .Case("|", 1)
Rui Ueyama708019c2016-07-24 18:19:40 +0000974 .Default(-1);
975}
976
George Rimarc91930a2016-09-02 21:17:20 +0000977Regex ScriptParser::readFilePatterns() {
Rui Ueyama10416562016-08-04 02:03:27 +0000978 std::vector<StringRef> V;
979 while (!Error && !skip(")"))
980 V.push_back(next());
George Rimarc91930a2016-09-02 21:17:20 +0000981 return compileGlobPatterns(V);
George Rimar0702c4e2016-07-29 15:32:46 +0000982}
983
Rui Ueyama742c3832016-08-04 22:27:00 +0000984SortKind ScriptParser::readSortKind() {
985 if (skip("SORT") || skip("SORT_BY_NAME"))
986 return SortByName;
987 if (skip("SORT_BY_ALIGNMENT"))
988 return SortByAlignment;
George Rimar575208c2016-09-15 19:15:12 +0000989 if (skip("SORT_BY_INIT_PRIORITY"))
990 return SortByPriority;
Rui Ueyama742c3832016-08-04 22:27:00 +0000991 return SortNone;
992}
993
George Rimara2496cb2016-08-30 09:46:59 +0000994InputSectionDescription *
995ScriptParser::readInputSectionRules(StringRef FilePattern) {
George Rimarc91930a2016-09-02 21:17:20 +0000996 auto *Cmd = new InputSectionDescription(FilePattern);
Davide Italiano0ed42b02016-07-25 21:47:13 +0000997 expect("(");
Davide Italianoe7282792016-07-27 01:44:01 +0000998
George Rimarceae6302016-09-16 13:30:18 +0000999 // Read EXCLUDE_FILE().
1000 if (skip("EXCLUDE_FILE")) {
1001 expect("(");
1002 Cmd->ExcludedFileRe = readFilePatterns();
1003 }
1004
Rui Ueyama742c3832016-08-04 22:27:00 +00001005 // Read SORT().
1006 if (SortKind K1 = readSortKind()) {
1007 Cmd->SortOuter = K1;
George Rimar0702c4e2016-07-29 15:32:46 +00001008 expect("(");
Rui Ueyama742c3832016-08-04 22:27:00 +00001009 if (SortKind K2 = readSortKind()) {
1010 Cmd->SortInner = K2;
George Rimar350ece42016-08-03 08:35:59 +00001011 expect("(");
George Rimarceae6302016-09-16 13:30:18 +00001012 Cmd->SectionRe = readFilePatterns();
George Rimar350ece42016-08-03 08:35:59 +00001013 expect(")");
1014 } else {
George Rimarceae6302016-09-16 13:30:18 +00001015 Cmd->SectionRe = readFilePatterns();
George Rimar350ece42016-08-03 08:35:59 +00001016 }
George Rimar0702c4e2016-07-29 15:32:46 +00001017 expect(")");
Rui Ueyama10416562016-08-04 02:03:27 +00001018 return Cmd;
George Rimar06598002016-07-28 21:51:30 +00001019 }
George Rimar0702c4e2016-07-29 15:32:46 +00001020
George Rimarceae6302016-09-16 13:30:18 +00001021 Cmd->SectionRe = readFilePatterns();
Rui Ueyama10416562016-08-04 02:03:27 +00001022 return Cmd;
Davide Italianoe7282792016-07-27 01:44:01 +00001023}
1024
George Rimara2496cb2016-08-30 09:46:59 +00001025InputSectionDescription *
1026ScriptParser::readInputSectionDescription(StringRef Tok) {
George Rimar06598002016-07-28 21:51:30 +00001027 // Input section wildcard can be surrounded by KEEP.
1028 // https://sourceware.org/binutils/docs/ld/Input-Section-Keep.html#Input-Section-Keep
George Rimara2496cb2016-08-30 09:46:59 +00001029 if (Tok == "KEEP") {
George Rimar06598002016-07-28 21:51:30 +00001030 expect("(");
George Rimara2496cb2016-08-30 09:46:59 +00001031 StringRef FilePattern = next();
1032 InputSectionDescription *Cmd = readInputSectionRules(FilePattern);
George Rimar06598002016-07-28 21:51:30 +00001033 expect(")");
George Rimarceae6302016-09-16 13:30:18 +00001034 Opt.KeptSections.push_back(&Cmd->SectionRe);
Rui Ueyama10416562016-08-04 02:03:27 +00001035 return Cmd;
George Rimar06598002016-07-28 21:51:30 +00001036 }
George Rimara2496cb2016-08-30 09:46:59 +00001037 return readInputSectionRules(Tok);
Davide Italiano0ed42b02016-07-25 21:47:13 +00001038}
1039
George Rimar03fc0102016-07-28 07:18:23 +00001040void ScriptParser::readSort() {
1041 expect("(");
1042 expect("CONSTRUCTORS");
1043 expect(")");
1044}
1045
George Rimareefa7582016-08-04 09:29:31 +00001046Expr ScriptParser::readAssert() {
1047 expect("(");
1048 Expr E = readExpr();
1049 expect(",");
George Rimarcd574a52016-09-09 14:35:36 +00001050 StringRef Msg = unquote(next());
George Rimareefa7582016-08-04 09:29:31 +00001051 expect(")");
1052 return [=](uint64_t Dot) {
1053 uint64_t V = E(Dot);
1054 if (!V)
1055 error(Msg);
1056 return V;
1057 };
1058}
1059
Rui Ueyama25150e82016-09-06 17:46:43 +00001060// Reads a FILL(expr) command. We handle the FILL command as an
1061// alias for =fillexp section attribute, which is different from
1062// what GNU linkers do.
1063// https://sourceware.org/binutils/docs/ld/Output-Section-Data.html
George Rimarff1f29e2016-09-06 13:51:57 +00001064std::vector<uint8_t> ScriptParser::readFill() {
1065 expect("(");
1066 std::vector<uint8_t> V = readOutputSectionFiller(next());
1067 expect(")");
1068 expect(";");
1069 return V;
1070}
1071
Rui Ueyama10416562016-08-04 02:03:27 +00001072OutputSectionCommand *
1073ScriptParser::readOutputSectionDescription(StringRef OutSec) {
George Rimar076fe152016-07-21 06:43:01 +00001074 OutputSectionCommand *Cmd = new OutputSectionCommand(OutSec);
George Rimar58e5c4d2016-07-25 08:29:46 +00001075
1076 // Read an address expression.
1077 // https://sourceware.org/binutils/docs/ld/Output-Section-Address.html#Output-Section-Address
1078 if (peek() != ":")
1079 Cmd->AddrExpr = readExpr();
1080
Denis Protivensky8e3b38a2015-11-12 09:52:08 +00001081 expect(":");
Davide Italiano246f6812016-07-22 03:36:24 +00001082
George Rimar8ceadb32016-08-17 07:44:19 +00001083 if (skip("AT"))
Rui Ueyama6ad7dfc2016-08-17 18:59:16 +00001084 Cmd->LmaExpr = readParenExpr();
George Rimar630c6172016-07-26 18:06:29 +00001085 if (skip("ALIGN"))
Rui Ueyama6ad7dfc2016-08-17 18:59:16 +00001086 Cmd->AlignExpr = readParenExpr();
George Rimardb24d9c2016-08-19 15:18:23 +00001087 if (skip("SUBALIGN"))
1088 Cmd->SubalignExpr = readParenExpr();
George Rimar630c6172016-07-26 18:06:29 +00001089
Davide Italiano246f6812016-07-22 03:36:24 +00001090 // Parse constraints.
1091 if (skip("ONLY_IF_RO"))
Rui Ueyamaefc40662016-07-25 22:00:10 +00001092 Cmd->Constraint = ConstraintKind::ReadOnly;
Davide Italiano246f6812016-07-22 03:36:24 +00001093 if (skip("ONLY_IF_RW"))
Rui Ueyamaefc40662016-07-25 22:00:10 +00001094 Cmd->Constraint = ConstraintKind::ReadWrite;
Denis Protivensky8e3b38a2015-11-12 09:52:08 +00001095 expect("{");
Rui Ueyama8ec77e62016-04-21 22:00:51 +00001096
Rui Ueyama025d59b2016-02-02 20:27:59 +00001097 while (!Error && !skip("}")) {
Eugene Leviantceabe802016-08-11 07:56:43 +00001098 StringRef Tok = next();
Eugene Leviantdb741e72016-09-07 07:08:43 +00001099 if (SymbolAssignment *Assignment = readProvideOrAssignment(Tok, false))
Eugene Leviantceabe802016-08-11 07:56:43 +00001100 Cmd->Commands.emplace_back(Assignment);
George Rimarff1f29e2016-09-06 13:51:57 +00001101 else if (Tok == "FILL")
1102 Cmd->Filler = readFill();
Eugene Leviantceabe802016-08-11 07:56:43 +00001103 else if (Tok == "SORT")
George Rimar03fc0102016-07-28 07:18:23 +00001104 readSort();
George Rimara2496cb2016-08-30 09:46:59 +00001105 else if (peek() == "(")
1106 Cmd->Commands.emplace_back(readInputSectionDescription(Tok));
Eugene Leviantceabe802016-08-11 07:56:43 +00001107 else
1108 setError("unknown command " + Tok);
Denis Protivensky8e3b38a2015-11-12 09:52:08 +00001109 }
George Rimar076fe152016-07-21 06:43:01 +00001110 Cmd->Phdrs = readOutputSectionPhdrs();
George Rimarff1f29e2016-09-06 13:51:57 +00001111 if (peek().startswith("="))
1112 Cmd->Filler = readOutputSectionFiller(next().drop_front());
Rui Ueyama10416562016-08-04 02:03:27 +00001113 return Cmd;
Rui Ueyamaf71caa22016-07-29 06:14:07 +00001114}
Rui Ueyama8ec77e62016-04-21 22:00:51 +00001115
Rui Ueyama2c8f1f02016-08-29 22:01:21 +00001116// Read "=<number>" where <number> is an octal/decimal/hexadecimal number.
1117// https://sourceware.org/binutils/docs/ld/Output-Section-Fill.html
1118//
1119// ld.gold is not fully compatible with ld.bfd. ld.bfd handles
1120// hexstrings as blobs of arbitrary sizes, while ld.gold handles them
1121// as 32-bit big-endian values. We will do the same as ld.gold does
1122// because it's simpler than what ld.bfd does.
George Rimarff1f29e2016-09-06 13:51:57 +00001123std::vector<uint8_t> ScriptParser::readOutputSectionFiller(StringRef Tok) {
Rui Ueyama965827d2016-08-03 23:25:15 +00001124 uint32_t V;
George Rimarff1f29e2016-09-06 13:51:57 +00001125 if (Tok.getAsInteger(0, V)) {
Rui Ueyama965827d2016-08-03 23:25:15 +00001126 setError("invalid filler expression: " + Tok);
Rui Ueyamaf71caa22016-07-29 06:14:07 +00001127 return {};
George Rimare2ee72b2016-02-26 14:48:31 +00001128 }
Rui Ueyama2c8f1f02016-08-29 22:01:21 +00001129 return {uint8_t(V >> 24), uint8_t(V >> 16), uint8_t(V >> 8), uint8_t(V)};
Denis Protivensky8e3b38a2015-11-12 09:52:08 +00001130}
1131
Petr Hoseka35e39c2016-08-16 01:11:16 +00001132SymbolAssignment *ScriptParser::readProvideHidden(bool Provide, bool Hidden) {
Eugene Levianta31c91b2016-07-22 07:38:40 +00001133 expect("(");
Rui Ueyama174e0a12016-07-29 00:29:25 +00001134 SymbolAssignment *Cmd = readAssignment(next());
Petr Hoseka35e39c2016-08-16 01:11:16 +00001135 Cmd->Provide = Provide;
Rui Ueyama174e0a12016-07-29 00:29:25 +00001136 Cmd->Hidden = Hidden;
Eugene Levianta31c91b2016-07-22 07:38:40 +00001137 expect(")");
1138 expect(";");
Rui Ueyama10416562016-08-04 02:03:27 +00001139 return Cmd;
Eugene Levianteda81a12016-07-12 06:39:48 +00001140}
1141
Eugene Leviantdb741e72016-09-07 07:08:43 +00001142SymbolAssignment *ScriptParser::readProvideOrAssignment(StringRef Tok,
1143 bool MakeAbsolute) {
Eugene Leviantceabe802016-08-11 07:56:43 +00001144 SymbolAssignment *Cmd = nullptr;
1145 if (peek() == "=" || peek() == "+=") {
1146 Cmd = readAssignment(Tok);
1147 expect(";");
1148 } else if (Tok == "PROVIDE") {
Petr Hoseka35e39c2016-08-16 01:11:16 +00001149 Cmd = readProvideHidden(true, false);
1150 } else if (Tok == "HIDDEN") {
1151 Cmd = readProvideHidden(false, true);
Eugene Leviantceabe802016-08-11 07:56:43 +00001152 } else if (Tok == "PROVIDE_HIDDEN") {
Petr Hoseka35e39c2016-08-16 01:11:16 +00001153 Cmd = readProvideHidden(true, true);
Eugene Leviantceabe802016-08-11 07:56:43 +00001154 }
Eugene Leviantdb741e72016-09-07 07:08:43 +00001155 if (Cmd && MakeAbsolute)
1156 Cmd->IsAbsolute = true;
Eugene Leviantceabe802016-08-11 07:56:43 +00001157 return Cmd;
1158}
1159
George Rimar30835ea2016-07-28 21:08:56 +00001160static uint64_t getSymbolValue(StringRef S, uint64_t Dot) {
1161 if (S == ".")
1162 return Dot;
George Rimar884e7862016-09-08 08:19:13 +00001163 return ScriptBase->getSymbolValue(S);
George Rimare32a3592016-08-10 07:59:34 +00001164}
1165
George Rimar30835ea2016-07-28 21:08:56 +00001166SymbolAssignment *ScriptParser::readAssignment(StringRef Name) {
1167 StringRef Op = next();
Eugene Leviantdb741e72016-09-07 07:08:43 +00001168 bool IsAbsolute = false;
1169 Expr E;
George Rimar30835ea2016-07-28 21:08:56 +00001170 assert(Op == "=" || Op == "+=");
Eugene Leviantdb741e72016-09-07 07:08:43 +00001171 if (skip("ABSOLUTE")) {
1172 E = readParenExpr();
1173 IsAbsolute = true;
1174 } else {
1175 E = readExpr();
1176 }
George Rimar30835ea2016-07-28 21:08:56 +00001177 if (Op == "+=")
1178 E = [=](uint64_t Dot) { return getSymbolValue(Name, Dot) + E(Dot); };
Eugene Leviantdb741e72016-09-07 07:08:43 +00001179 return new SymbolAssignment(Name, E, IsAbsolute);
George Rimar30835ea2016-07-28 21:08:56 +00001180}
1181
1182// This is an operator-precedence parser to parse a linker
1183// script expression.
1184Expr ScriptParser::readExpr() { return readExpr1(readPrimary(), 0); }
1185
Rui Ueyama36c1cd22016-08-05 01:04:59 +00001186static Expr combine(StringRef Op, Expr L, Expr R) {
1187 if (Op == "*")
1188 return [=](uint64_t Dot) { return L(Dot) * R(Dot); };
1189 if (Op == "/") {
1190 return [=](uint64_t Dot) -> uint64_t {
1191 uint64_t RHS = R(Dot);
1192 if (RHS == 0) {
1193 error("division by zero");
1194 return 0;
1195 }
1196 return L(Dot) / RHS;
1197 };
1198 }
1199 if (Op == "+")
1200 return [=](uint64_t Dot) { return L(Dot) + R(Dot); };
1201 if (Op == "-")
1202 return [=](uint64_t Dot) { return L(Dot) - R(Dot); };
1203 if (Op == "<")
1204 return [=](uint64_t Dot) { return L(Dot) < R(Dot); };
1205 if (Op == ">")
1206 return [=](uint64_t Dot) { return L(Dot) > R(Dot); };
1207 if (Op == ">=")
1208 return [=](uint64_t Dot) { return L(Dot) >= R(Dot); };
1209 if (Op == "<=")
1210 return [=](uint64_t Dot) { return L(Dot) <= R(Dot); };
1211 if (Op == "==")
1212 return [=](uint64_t Dot) { return L(Dot) == R(Dot); };
1213 if (Op == "!=")
1214 return [=](uint64_t Dot) { return L(Dot) != R(Dot); };
1215 if (Op == "&")
1216 return [=](uint64_t Dot) { return L(Dot) & R(Dot); };
Rafael Espindolacc3dd622016-08-22 21:33:35 +00001217 if (Op == "|")
1218 return [=](uint64_t Dot) { return L(Dot) | R(Dot); };
Rui Ueyama36c1cd22016-08-05 01:04:59 +00001219 llvm_unreachable("invalid operator");
1220}
1221
Rui Ueyama708019c2016-07-24 18:19:40 +00001222// This is a part of the operator-precedence parser. This function
1223// assumes that the remaining token stream starts with an operator.
1224Expr ScriptParser::readExpr1(Expr Lhs, int MinPrec) {
1225 while (!atEOF() && !Error) {
1226 // Read an operator and an expression.
1227 StringRef Op1 = peek();
1228 if (Op1 == "?")
1229 return readTernary(Lhs);
1230 if (precedence(Op1) < MinPrec)
Eugene Levianteda81a12016-07-12 06:39:48 +00001231 break;
Rui Ueyama708019c2016-07-24 18:19:40 +00001232 next();
1233 Expr Rhs = readPrimary();
1234
1235 // Evaluate the remaining part of the expression first if the
1236 // next operator has greater precedence than the previous one.
1237 // For example, if we have read "+" and "3", and if the next
1238 // operator is "*", then we'll evaluate 3 * ... part first.
1239 while (!atEOF()) {
1240 StringRef Op2 = peek();
1241 if (precedence(Op2) <= precedence(Op1))
1242 break;
1243 Rhs = readExpr1(Rhs, precedence(Op2));
1244 }
1245
1246 Lhs = combine(Op1, Lhs, Rhs);
Eugene Levianteda81a12016-07-12 06:39:48 +00001247 }
Rui Ueyama708019c2016-07-24 18:19:40 +00001248 return Lhs;
1249}
1250
1251uint64_t static getConstant(StringRef S) {
Michael J. Spencere2cc07b2016-08-17 02:10:51 +00001252 if (S == "COMMONPAGESIZE")
Rui Ueyama708019c2016-07-24 18:19:40 +00001253 return Target->PageSize;
Michael J. Spencere2cc07b2016-08-17 02:10:51 +00001254 if (S == "MAXPAGESIZE")
1255 return Target->MaxPageSize;
Rui Ueyama708019c2016-07-24 18:19:40 +00001256 error("unknown constant: " + S);
1257 return 0;
1258}
1259
Rui Ueyama626e0b02016-09-02 18:19:00 +00001260// Parses Tok as an integer. Returns true if successful.
1261// It recognizes hexadecimal (prefixed with "0x" or suffixed with "H")
1262// and decimal numbers. Decimal numbers may have "K" (kilo) or
1263// "M" (mega) prefixes.
George Rimar9f2f7ad2016-09-02 16:01:42 +00001264static bool readInteger(StringRef Tok, uint64_t &Result) {
Simon Atanasyaneaeafb22016-09-02 21:54:35 +00001265 if (Tok.startswith("-")) {
1266 if (!readInteger(Tok.substr(1), Result))
1267 return false;
1268 Result = -Result;
1269 return true;
1270 }
George Rimar9f2f7ad2016-09-02 16:01:42 +00001271 if (Tok.startswith_lower("0x"))
1272 return !Tok.substr(2).getAsInteger(16, Result);
1273 if (Tok.endswith_lower("H"))
1274 return !Tok.drop_back().getAsInteger(16, Result);
1275
1276 int Suffix = 1;
1277 if (Tok.endswith_lower("K")) {
1278 Suffix = 1024;
1279 Tok = Tok.drop_back();
1280 } else if (Tok.endswith_lower("M")) {
1281 Suffix = 1024 * 1024;
1282 Tok = Tok.drop_back();
1283 }
1284 if (Tok.getAsInteger(10, Result))
1285 return false;
1286 Result *= Suffix;
1287 return true;
1288}
1289
Rui Ueyama708019c2016-07-24 18:19:40 +00001290Expr ScriptParser::readPrimary() {
Rui Ueyama6ad7dfc2016-08-17 18:59:16 +00001291 if (peek() == "(")
1292 return readParenExpr();
Rui Ueyama708019c2016-07-24 18:19:40 +00001293
Rui Ueyama6ad7dfc2016-08-17 18:59:16 +00001294 StringRef Tok = next();
Rui Ueyama708019c2016-07-24 18:19:40 +00001295
Simon Atanasyaneaeafb22016-09-02 21:54:35 +00001296 if (Tok == "~") {
1297 Expr E = readPrimary();
1298 return [=](uint64_t Dot) { return ~E(Dot); };
1299 }
1300 if (Tok == "-") {
1301 Expr E = readPrimary();
1302 return [=](uint64_t Dot) { return -E(Dot); };
1303 }
1304
Rui Ueyama708019c2016-07-24 18:19:40 +00001305 // Built-in functions are parsed here.
1306 // https://sourceware.org/binutils/docs/ld/Builtin-Functions.html.
George Rimar96659df2016-08-30 09:54:01 +00001307 if (Tok == "ADDR") {
1308 expect("(");
1309 StringRef Name = next();
1310 expect(")");
George Rimar884e7862016-09-08 08:19:13 +00001311 return
1312 [=](uint64_t Dot) { return ScriptBase->getOutputSectionAddress(Name); };
George Rimar96659df2016-08-30 09:54:01 +00001313 }
George Rimareefa7582016-08-04 09:29:31 +00001314 if (Tok == "ASSERT")
1315 return readAssert();
Rui Ueyama708019c2016-07-24 18:19:40 +00001316 if (Tok == "ALIGN") {
Rui Ueyama6ad7dfc2016-08-17 18:59:16 +00001317 Expr E = readParenExpr();
Rui Ueyama708019c2016-07-24 18:19:40 +00001318 return [=](uint64_t Dot) { return alignTo(Dot, E(Dot)); };
1319 }
1320 if (Tok == "CONSTANT") {
1321 expect("(");
1322 StringRef Tok = next();
1323 expect(")");
1324 return [=](uint64_t Dot) { return getConstant(Tok); };
1325 }
Rafael Espindola54c145c2016-07-28 18:16:24 +00001326 if (Tok == "SEGMENT_START") {
1327 expect("(");
1328 next();
1329 expect(",");
1330 uint64_t Val;
Rafael Espindola3adbbc32016-09-15 13:36:44 +00001331 if (next().getAsInteger(0, Val))
1332 setError("integer expected");
Rafael Espindola54c145c2016-07-28 18:16:24 +00001333 expect(")");
1334 return [=](uint64_t Dot) { return Val; };
1335 }
Rui Ueyama708019c2016-07-24 18:19:40 +00001336 if (Tok == "DATA_SEGMENT_ALIGN") {
1337 expect("(");
1338 Expr E = readExpr();
1339 expect(",");
1340 readExpr();
1341 expect(")");
Rui Ueyamaf7791bb2016-07-26 19:34:10 +00001342 return [=](uint64_t Dot) { return alignTo(Dot, E(Dot)); };
Rui Ueyama708019c2016-07-24 18:19:40 +00001343 }
1344 if (Tok == "DATA_SEGMENT_END") {
1345 expect("(");
1346 expect(".");
1347 expect(")");
1348 return [](uint64_t Dot) { return Dot; };
1349 }
George Rimar276b4e62016-07-26 17:58:44 +00001350 // GNU linkers implements more complicated logic to handle
1351 // DATA_SEGMENT_RELRO_END. We instead ignore the arguments and just align to
1352 // the next page boundary for simplicity.
1353 if (Tok == "DATA_SEGMENT_RELRO_END") {
1354 expect("(");
Rafael Espindola97bdc722016-09-14 19:14:01 +00001355 readExpr();
George Rimar276b4e62016-07-26 17:58:44 +00001356 expect(",");
1357 readExpr();
1358 expect(")");
1359 return [](uint64_t Dot) { return alignTo(Dot, Target->PageSize); };
1360 }
George Rimar9e694502016-07-29 16:18:47 +00001361 if (Tok == "SIZEOF") {
1362 expect("(");
1363 StringRef Name = next();
1364 expect(")");
George Rimar884e7862016-09-08 08:19:13 +00001365 return [=](uint64_t Dot) { return ScriptBase->getOutputSectionSize(Name); };
George Rimar9e694502016-07-29 16:18:47 +00001366 }
Eugene Leviant36fac7f2016-09-08 09:08:30 +00001367 if (Tok == "ALIGNOF") {
1368 expect("(");
1369 StringRef Name = next();
1370 expect(")");
1371 return
1372 [=](uint64_t Dot) { return ScriptBase->getOutputSectionAlign(Name); };
1373 }
George Rimare32a3592016-08-10 07:59:34 +00001374 if (Tok == "SIZEOF_HEADERS")
George Rimar884e7862016-09-08 08:19:13 +00001375 return [=](uint64_t Dot) { return ScriptBase->getHeaderSize(); };
Rui Ueyama708019c2016-07-24 18:19:40 +00001376
George Rimar9f2f7ad2016-09-02 16:01:42 +00001377 // Tok is a literal number.
1378 uint64_t V;
1379 if (readInteger(Tok, V))
1380 return [=](uint64_t Dot) { return V; };
1381
1382 // Tok is a symbol name.
1383 if (Tok != "." && !isValidCIdentifier(Tok))
1384 setError("malformed number: " + Tok);
1385 return [=](uint64_t Dot) { return getSymbolValue(Tok, Dot); };
Rui Ueyama708019c2016-07-24 18:19:40 +00001386}
1387
1388Expr ScriptParser::readTernary(Expr Cond) {
1389 next();
1390 Expr L = readExpr();
1391 expect(":");
1392 Expr R = readExpr();
1393 return [=](uint64_t Dot) { return Cond(Dot) ? L(Dot) : R(Dot); };
1394}
1395
Rui Ueyama6ad7dfc2016-08-17 18:59:16 +00001396Expr ScriptParser::readParenExpr() {
1397 expect("(");
1398 Expr E = readExpr();
1399 expect(")");
1400 return E;
1401}
1402
Eugene Leviantbbe38602016-07-19 09:25:43 +00001403std::vector<StringRef> ScriptParser::readOutputSectionPhdrs() {
1404 std::vector<StringRef> Phdrs;
1405 while (!Error && peek().startswith(":")) {
1406 StringRef Tok = next();
1407 Tok = (Tok.size() == 1) ? next() : Tok.substr(1);
1408 if (Tok.empty()) {
1409 setError("section header name is empty");
1410 break;
1411 }
Rui Ueyama047404f2016-07-20 19:36:36 +00001412 Phdrs.push_back(Tok);
Eugene Leviantbbe38602016-07-19 09:25:43 +00001413 }
1414 return Phdrs;
1415}
1416
1417unsigned ScriptParser::readPhdrType() {
Eugene Leviantbbe38602016-07-19 09:25:43 +00001418 StringRef Tok = next();
Rui Ueyamab0f6c592016-07-20 19:36:38 +00001419 unsigned Ret = StringSwitch<unsigned>(Tok)
George Rimar6c55f0e2016-09-08 08:20:30 +00001420 .Case("PT_NULL", PT_NULL)
1421 .Case("PT_LOAD", PT_LOAD)
1422 .Case("PT_DYNAMIC", PT_DYNAMIC)
1423 .Case("PT_INTERP", PT_INTERP)
1424 .Case("PT_NOTE", PT_NOTE)
1425 .Case("PT_SHLIB", PT_SHLIB)
1426 .Case("PT_PHDR", PT_PHDR)
1427 .Case("PT_TLS", PT_TLS)
1428 .Case("PT_GNU_EH_FRAME", PT_GNU_EH_FRAME)
1429 .Case("PT_GNU_STACK", PT_GNU_STACK)
1430 .Case("PT_GNU_RELRO", PT_GNU_RELRO)
1431 .Default(-1);
Eugene Leviantbbe38602016-07-19 09:25:43 +00001432
Rui Ueyamab0f6c592016-07-20 19:36:38 +00001433 if (Ret == (unsigned)-1) {
1434 setError("invalid program header type: " + Tok);
1435 return PT_NULL;
1436 }
1437 return Ret;
Eugene Leviantbbe38602016-07-19 09:25:43 +00001438}
1439
Rui Ueyama95769b42016-08-31 20:03:54 +00001440void ScriptParser::readVersionDeclaration(StringRef VerStr) {
George Rimar20b65982016-08-31 09:08:26 +00001441 // Identifiers start at 2 because 0 and 1 are reserved
1442 // for VER_NDX_LOCAL and VER_NDX_GLOBAL constants.
1443 size_t VersionId = Config->VersionDefinitions.size() + 2;
1444 Config->VersionDefinitions.push_back({VerStr, VersionId});
1445
1446 if (skip("global:") || peek() != "local:")
1447 readGlobal(VerStr);
1448 if (skip("local:"))
1449 readLocal();
1450 expect("}");
1451
1452 // Each version may have a parent version. For example, "Ver2" defined as
1453 // "Ver2 { global: foo; local: *; } Ver1;" has "Ver1" as a parent. This
1454 // version hierarchy is, probably against your instinct, purely for human; the
1455 // runtime doesn't care about them at all. In LLD, we simply skip the token.
1456 if (!VerStr.empty() && peek() != ";")
1457 next();
1458 expect(";");
1459}
1460
1461void ScriptParser::readLocal() {
1462 Config->DefaultSymbolVersion = VER_NDX_LOCAL;
1463 expect("*");
1464 expect(";");
1465}
1466
1467void ScriptParser::readExtern(std::vector<SymbolVersion> *Globals) {
George Rimarcd574a52016-09-09 14:35:36 +00001468 expect("\"C++\"");
George Rimar20b65982016-08-31 09:08:26 +00001469 expect("{");
1470
1471 for (;;) {
1472 if (peek() == "}" || Error)
1473 break;
George Rimarcd574a52016-09-09 14:35:36 +00001474 bool HasWildcard = !peek().startswith("\"") && hasWildcard(peek());
1475 Globals->push_back({unquote(next()), true, HasWildcard});
George Rimar20b65982016-08-31 09:08:26 +00001476 expect(";");
1477 }
1478
1479 expect("}");
1480 expect(";");
1481}
1482
1483void ScriptParser::readGlobal(StringRef VerStr) {
1484 std::vector<SymbolVersion> *Globals;
1485 if (VerStr.empty())
1486 Globals = &Config->VersionScriptGlobals;
1487 else
1488 Globals = &Config->VersionDefinitions.back().Globals;
1489
1490 for (;;) {
1491 if (skip("extern"))
1492 readExtern(Globals);
1493
1494 StringRef Cur = peek();
1495 if (Cur == "}" || Cur == "local:" || Error)
1496 return;
1497 next();
George Rimarcd574a52016-09-09 14:35:36 +00001498 Globals->push_back({unquote(Cur), false, hasWildcard(Cur)});
George Rimar20b65982016-08-31 09:08:26 +00001499 expect(";");
1500 }
1501}
1502
Simon Atanasyan16b0cc92015-11-26 05:53:00 +00001503static bool isUnderSysroot(StringRef Path) {
1504 if (Config->Sysroot == "")
1505 return false;
1506 for (; !Path.empty(); Path = sys::path::parent_path(Path))
1507 if (sys::fs::equivalent(Config->Sysroot, Path))
1508 return true;
1509 return false;
1510}
1511
Rui Ueyama07320e42016-04-20 20:13:41 +00001512void elf::readLinkerScript(MemoryBufferRef MB) {
Simon Atanasyan16b0cc92015-11-26 05:53:00 +00001513 StringRef Path = MB.getBufferIdentifier();
George Rimar20b65982016-08-31 09:08:26 +00001514 ScriptParser(MB.getBuffer(), isUnderSysroot(Path)).readLinkerScript();
1515}
1516
1517void elf::readVersionScript(MemoryBufferRef MB) {
1518 ScriptParser(MB.getBuffer(), false).readVersionScript();
Rui Ueyamaf7c5fbb2015-09-30 17:23:26 +00001519}
Rui Ueyama1ebc8ed2016-02-12 21:47:28 +00001520
Rui Ueyama07320e42016-04-20 20:13:41 +00001521template class elf::LinkerScript<ELF32LE>;
1522template class elf::LinkerScript<ELF32BE>;
1523template class elf::LinkerScript<ELF64LE>;
1524template class elf::LinkerScript<ELF64BE>;