blob: 50f2d5045c979364385e8731a60a1dd4dd2602bd [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 Leviantceabe802016-08-11 07:56:43 +000051}
52
Rui Ueyama0c70d3c2016-08-12 03:31:09 +000053template <class ELFT> static void addSynthetic(SymbolAssignment *Cmd) {
George Rimare1937bb2016-08-19 15:36:32 +000054 Symbol *Sym = Symtab<ELFT>::X->addSynthetic(
55 Cmd->Name, nullptr, 0, Cmd->Hidden ? STV_HIDDEN : STV_DEFAULT);
Rui Ueyama16024212016-08-11 23:22:52 +000056 Cmd->Sym = Sym->body();
Eugene Leviantceabe802016-08-11 07:56:43 +000057}
58
Eugene Leviantdb741e72016-09-07 07:08:43 +000059template <class ELFT> static void addSymbol(SymbolAssignment *Cmd) {
60 if (Cmd->IsAbsolute)
61 addRegular<ELFT>(Cmd);
62 else
63 addSynthetic<ELFT>(Cmd);
64}
Rui Ueyama16024212016-08-11 23:22:52 +000065// If a symbol was in PROVIDE(), we need to define it only when
66// it is an undefined symbol.
67template <class ELFT> static bool shouldDefine(SymbolAssignment *Cmd) {
68 if (Cmd->Name == ".")
Eugene Leviantceabe802016-08-11 07:56:43 +000069 return false;
Rui Ueyama16024212016-08-11 23:22:52 +000070 if (!Cmd->Provide)
71 return true;
72 SymbolBody *B = Symtab<ELFT>::X->find(Cmd->Name);
73 return B && B->isUndefined();
Eugene Leviantceabe802016-08-11 07:56:43 +000074}
75
George Rimar076fe152016-07-21 06:43:01 +000076bool SymbolAssignment::classof(const BaseCommand *C) {
77 return C->Kind == AssignmentKind;
78}
79
80bool OutputSectionCommand::classof(const BaseCommand *C) {
81 return C->Kind == OutputSectionKind;
82}
83
George Rimareea31142016-07-21 14:26:59 +000084bool InputSectionDescription::classof(const BaseCommand *C) {
85 return C->Kind == InputSectionKind;
86}
87
George Rimareefa7582016-08-04 09:29:31 +000088bool AssertCommand::classof(const BaseCommand *C) {
89 return C->Kind == AssertKind;
90}
91
Rui Ueyama36a153c2016-07-23 14:09:58 +000092template <class ELFT> static bool isDiscarded(InputSectionBase<ELFT> *S) {
George Rimareea31142016-07-21 14:26:59 +000093 return !S || !S->Live;
Rui Ueyama717677a2016-02-11 21:17:59 +000094}
95
Rui Ueyamaf34d0e02016-08-12 01:24:53 +000096template <class ELFT> LinkerScript<ELFT>::LinkerScript() {}
97template <class ELFT> LinkerScript<ELFT>::~LinkerScript() {}
98
Rui Ueyama07320e42016-04-20 20:13:41 +000099template <class ELFT>
100bool LinkerScript<ELFT>::shouldKeep(InputSectionBase<ELFT> *S) {
George Rimarc91930a2016-09-02 21:17:20 +0000101 for (Regex *Re : Opt.KeptSections)
Rafael Espindola042a3f22016-09-08 14:06:08 +0000102 if (Re->match(S->Name))
George Rimareea31142016-07-21 14:26:59 +0000103 return true;
104 return false;
105}
106
George Rimar06598002016-07-28 21:51:30 +0000107static bool fileMatches(const InputSectionDescription *Desc,
108 StringRef Filename) {
George Rimarc91930a2016-09-02 21:17:20 +0000109 return const_cast<Regex &>(Desc->FileRe).match(Filename) &&
110 !const_cast<Regex &>(Desc->ExcludedFileRe).match(Filename);
George Rimar06598002016-07-28 21:51:30 +0000111}
112
Rui Ueyama6b274812016-07-25 22:51:07 +0000113// Returns input sections filtered by given glob patterns.
114template <class ELFT>
115std::vector<InputSectionBase<ELFT> *>
Rui Ueyamaad10c3d2016-07-28 21:05:04 +0000116LinkerScript<ELFT>::getInputSections(const InputSectionDescription *I) {
George Rimarc91930a2016-09-02 21:17:20 +0000117 const Regex &Re = I->SectionRe;
Rui Ueyama6b274812016-07-25 22:51:07 +0000118 std::vector<InputSectionBase<ELFT> *> Ret;
119 for (const std::unique_ptr<ObjectFile<ELFT>> &F :
George Rimar06598002016-07-28 21:51:30 +0000120 Symtab<ELFT>::X->getObjectFiles()) {
121 if (fileMatches(I, sys::path::filename(F->getName())))
122 for (InputSectionBase<ELFT> *S : F->getSections())
123 if (!isDiscarded(S) && !S->OutSec &&
Rafael Espindola042a3f22016-09-08 14:06:08 +0000124 const_cast<Regex &>(Re).match(S->Name))
Davide Italianoe7282792016-07-27 01:44:01 +0000125 Ret.push_back(S);
George Rimar06598002016-07-28 21:51:30 +0000126 }
Eugene Leviant3e6b0272016-07-28 19:24:13 +0000127
George Rimarc91930a2016-09-02 21:17:20 +0000128 if (const_cast<Regex &>(Re).match("COMMON"))
Rui Ueyamaad10c3d2016-07-28 21:05:04 +0000129 Ret.push_back(CommonInputSection<ELFT>::X);
Rui Ueyama6b274812016-07-25 22:51:07 +0000130 return Ret;
131}
132
Rafael Espindolac0028d32016-09-08 20:47:52 +0000133static bool compareName(InputSectionData *A, InputSectionData *B) {
Rafael Espindola042a3f22016-09-08 14:06:08 +0000134 return A->Name < B->Name;
Rui Ueyama742c3832016-08-04 22:27:00 +0000135}
George Rimar350ece42016-08-03 08:35:59 +0000136
Rafael Espindolac0028d32016-09-08 20:47:52 +0000137static bool compareAlignment(InputSectionData *A, InputSectionData *B) {
Rui Ueyama742c3832016-08-04 22:27:00 +0000138 // ">" is not a mistake. Larger alignments are placed before smaller
139 // alignments in order to reduce the amount of padding necessary.
140 // This is compatible with GNU.
141 return A->Alignment > B->Alignment;
142}
George Rimar350ece42016-08-03 08:35:59 +0000143
Rafael Espindolac0028d32016-09-08 20:47:52 +0000144static std::function<bool(InputSectionData *, InputSectionData *)>
Rui Ueyama742c3832016-08-04 22:27:00 +0000145getComparator(SortKind K) {
146 if (K == SortByName)
Rafael Espindolac0028d32016-09-08 20:47:52 +0000147 return compareName;
148 return compareAlignment;
Rui Ueyama742c3832016-08-04 22:27:00 +0000149}
George Rimar0702c4e2016-07-29 15:32:46 +0000150
151template <class ELFT>
Rafael Espindola7bd37872016-09-12 16:05:16 +0000152void LinkerScript<ELFT>::discard(ArrayRef<InputSectionBase<ELFT> *> V) {
153 for (InputSectionBase<ELFT> *S : V) {
154 S->Live = false;
155 reportDiscarded(S);
Rui Ueyama48c3f1c2016-08-12 00:27:23 +0000156 }
157}
158
George Rimar8f66df92016-08-12 20:38:20 +0000159static bool checkConstraint(uint64_t Flags, ConstraintKind Kind) {
160 bool RO = (Kind == ConstraintKind::ReadOnly);
161 bool RW = (Kind == ConstraintKind::ReadWrite);
162 bool Writable = Flags & SHF_WRITE;
Rui Ueyamaadcdb662016-09-06 22:50:48 +0000163 return !(RO && Writable) && !(RW && !Writable);
George Rimar8f66df92016-08-12 20:38:20 +0000164}
165
Rui Ueyama48c3f1c2016-08-12 00:27:23 +0000166template <class ELFT>
George Rimar06ae6832016-08-12 09:07:57 +0000167static bool matchConstraints(ArrayRef<InputSectionBase<ELFT> *> Sections,
168 ConstraintKind Kind) {
George Rimar8f66df92016-08-12 20:38:20 +0000169 if (Kind == ConstraintKind::NoConstraint)
170 return true;
171 return llvm::all_of(Sections, [=](InputSectionBase<ELFT> *Sec) {
172 return checkConstraint(Sec->getSectionHdr()->sh_flags, Kind);
George Rimar06ae6832016-08-12 09:07:57 +0000173 });
174}
175
176template <class ELFT>
Rui Ueyama0b9ce6a2016-08-12 03:16:56 +0000177std::vector<InputSectionBase<ELFT> *>
George Rimar06ae6832016-08-12 09:07:57 +0000178LinkerScript<ELFT>::createInputSectionList(OutputSectionCommand &OutCmd) {
Rui Ueyama0b9ce6a2016-08-12 03:16:56 +0000179 std::vector<InputSectionBase<ELFT> *> Ret;
Eugene Leviant97403d12016-09-01 09:55:57 +0000180 DenseSet<InputSectionBase<ELFT> *> SectionIndex;
Rui Ueyamae7f912c2016-08-03 21:12:09 +0000181
George Rimar06ae6832016-08-12 09:07:57 +0000182 for (const std::unique_ptr<BaseCommand> &Base : OutCmd.Commands) {
183 if (auto *OutCmd = dyn_cast<SymbolAssignment>(Base.get())) {
184 if (shouldDefine<ELFT>(OutCmd))
Eugene Leviantdb741e72016-09-07 07:08:43 +0000185 addSymbol<ELFT>(OutCmd);
Eugene Leviant97403d12016-09-01 09:55:57 +0000186 OutCmd->GoesAfter = Ret.empty() ? nullptr : Ret.back();
Rui Ueyama0b9ce6a2016-08-12 03:16:56 +0000187 continue;
188 }
189
190 auto *Cmd = cast<InputSectionDescription>(Base.get());
191 std::vector<InputSectionBase<ELFT> *> V = getInputSections(Cmd);
George Rimar06ae6832016-08-12 09:07:57 +0000192 if (!matchConstraints<ELFT>(V, OutCmd.Constraint))
193 continue;
Rui Ueyama0b9ce6a2016-08-12 03:16:56 +0000194 if (Cmd->SortInner)
Rafael Espindolac0028d32016-09-08 20:47:52 +0000195 std::stable_sort(V.begin(), V.end(), getComparator(Cmd->SortInner));
Rui Ueyama0b9ce6a2016-08-12 03:16:56 +0000196 if (Cmd->SortOuter)
Rafael Espindolac0028d32016-09-08 20:47:52 +0000197 std::stable_sort(V.begin(), V.end(), getComparator(Cmd->SortOuter));
Eugene Leviant97403d12016-09-01 09:55:57 +0000198
199 // Add all input sections corresponding to rule 'Cmd' to
200 // resulting vector. We do not add duplicate input sections.
201 for (InputSectionBase<ELFT> *S : V)
202 if (SectionIndex.insert(S).second)
203 Ret.push_back(S);
Rui Ueyama0b9ce6a2016-08-12 03:16:56 +0000204 }
205 return Ret;
206}
207
George Rimar6c55f0e2016-09-08 08:20:30 +0000208template <class ELFT> void LinkerScript<ELFT>::createAssignments() {
Petr Hoseke5d3ca52016-08-31 15:31:17 +0000209 for (const std::unique_ptr<SymbolAssignment> &Cmd : Opt.Assignments) {
210 if (shouldDefine<ELFT>(Cmd.get()))
211 addRegular<ELFT>(Cmd.get());
212 if (Cmd->Sym)
213 cast<DefinedRegular<ELFT>>(Cmd->Sym)->Value = Cmd->Expression(0);
214 }
215}
216
217template <class ELFT>
Rafael Espindola10897f12016-09-13 14:23:14 +0000218static SectionKey<ELFT::Is64Bits> createKey(InputSectionBase<ELFT> *C,
219 StringRef OutsecName) {
220 // When using linker script the merge rules are different.
221 // Unfortunately, linker scripts are name based. This means that expressions
222 // like *(.foo*) can refer to multiple input sections that would normally be
223 // placed in different output sections. We cannot put them in different
224 // output sections or we would produce wrong results for
225 // start = .; *(.foo.*) end = .; *(.bar)
226 // and a mapping of .foo1 and .bar1 to one section and .foo2 and .bar2 to
227 // another. The problem is that there is no way to layout those output
228 // sections such that the .foo sections are the only thing between the
229 // start and end symbols.
230
231 // An extra annoyance is that we cannot simply disable merging of the contents
232 // of SHF_MERGE sections, but our implementation requires one output section
233 // per "kind" (string or not, which size/aligment).
234 // Fortunately, creating symbols in the middle of a merge section is not
235 // supported by bfd or gold, so we can just create multiple section in that
236 // case.
237 const typename ELFT::Shdr *H = C->getSectionHdr();
238 typedef typename ELFT::uint uintX_t;
239 uintX_t Flags = H->sh_flags & (SHF_MERGE | SHF_STRINGS);
240
241 uintX_t Alignment = 0;
242 if (isa<MergeInputSection<ELFT>>(C))
243 Alignment = std::max(H->sh_addralign, H->sh_entsize);
244
245 return SectionKey<ELFT::Is64Bits>{OutsecName, /*Type*/ 0, Flags, Alignment};
246}
247
248template <class ELFT>
Rui Ueyama0b9ce6a2016-08-12 03:16:56 +0000249void LinkerScript<ELFT>::createSections(OutputSectionFactory<ELFT> &Factory) {
Rafael Espindola28c15972016-09-13 13:00:06 +0000250 auto AddSec = [&](InputSectionBase<ELFT> *Sec, StringRef Name) {
251 OutputSectionBase<ELFT> *OutSec;
252 bool IsNew;
Rafael Espindola10897f12016-09-13 14:23:14 +0000253 std::tie(OutSec, IsNew) = Factory.create(createKey(Sec, Name), Sec);
Rafael Espindola28c15972016-09-13 13:00:06 +0000254 if (IsNew)
255 OutputSections->push_back(OutSec);
256 return OutSec;
257 };
258
Rui Ueyama48c3f1c2016-08-12 00:27:23 +0000259 for (const std::unique_ptr<BaseCommand> &Base1 : Opt.Commands) {
Rui Ueyama2ab5f732016-08-12 03:33:04 +0000260 if (auto *Cmd = dyn_cast<SymbolAssignment>(Base1.get())) {
261 if (shouldDefine<ELFT>(Cmd))
262 addRegular<ELFT>(Cmd);
263 continue;
264 }
265
Eugene Leviantceabe802016-08-11 07:56:43 +0000266 if (auto *Cmd = dyn_cast<OutputSectionCommand>(Base1.get())) {
Rafael Espindola7bd37872016-09-12 16:05:16 +0000267 std::vector<InputSectionBase<ELFT> *> V = createInputSectionList(*Cmd);
268
Rui Ueyama48c3f1c2016-08-12 00:27:23 +0000269 if (Cmd->Name == "/DISCARD/") {
Rafael Espindola7bd37872016-09-12 16:05:16 +0000270 discard(V);
Rui Ueyama48c3f1c2016-08-12 00:27:23 +0000271 continue;
272 }
Eugene Leviantceabe802016-08-11 07:56:43 +0000273
Eugene Leviant97403d12016-09-01 09:55:57 +0000274 if (V.empty())
Rui Ueyama0b9ce6a2016-08-12 03:16:56 +0000275 continue;
276
George Rimardb24d9c2016-08-19 15:18:23 +0000277 for (InputSectionBase<ELFT> *Sec : V) {
Rafael Espindola28c15972016-09-13 13:00:06 +0000278 OutputSectionBase<ELFT> *OutSec = AddSec(Sec, Cmd->Name);
George Rimara14b13d2016-09-07 10:46:07 +0000279 uint32_t Subalign = Cmd->SubalignExpr ? Cmd->SubalignExpr(0) : 0;
280
George Rimardb24d9c2016-08-19 15:18:23 +0000281 if (Subalign)
282 Sec->Alignment = Subalign;
Eugene Leviant97403d12016-09-01 09:55:57 +0000283 OutSec->addSection(Sec);
George Rimardb24d9c2016-08-19 15:18:23 +0000284 }
Eugene Leviantceabe802016-08-11 07:56:43 +0000285 }
Rui Ueyama48c3f1c2016-08-12 00:27:23 +0000286 }
Eugene Leviante63d81b2016-07-20 14:43:20 +0000287
Rui Ueyama0b9ce6a2016-08-12 03:16:56 +0000288 // Add orphan sections.
Rui Ueyama6b274812016-07-25 22:51:07 +0000289 for (const std::unique_ptr<ObjectFile<ELFT>> &F :
Rui Ueyama0b9ce6a2016-08-12 03:16:56 +0000290 Symtab<ELFT>::X->getObjectFiles()) {
291 for (InputSectionBase<ELFT> *S : F->getSections()) {
Rui Ueyama2ab5f732016-08-12 03:33:04 +0000292 if (isDiscarded(S) || S->OutSec)
293 continue;
Rafael Espindola28c15972016-09-13 13:00:06 +0000294 OutputSectionBase<ELFT> *OutSec = AddSec(S, getOutputSectionName(S));
Rui Ueyama2ab5f732016-08-12 03:33:04 +0000295 OutSec->addSection(S);
Rui Ueyama0b9ce6a2016-08-12 03:16:56 +0000296 }
297 }
Eugene Leviante63d81b2016-07-20 14:43:20 +0000298}
299
Eugene Leviantdb741e72016-09-07 07:08:43 +0000300// Sets value of a section-defined symbol. Two kinds of
301// symbols are processed: synthetic symbols, whose value
302// is an offset from beginning of section and regular
303// symbols whose value is absolute.
304template <class ELFT>
305static void assignSectionSymbol(SymbolAssignment *Cmd,
306 OutputSectionBase<ELFT> *Sec,
307 typename ELFT::uint Off) {
308 if (!Cmd->Sym)
309 return;
310
311 if (auto *Body = dyn_cast<DefinedSynthetic<ELFT>>(Cmd->Sym)) {
312 Body->Section = Sec;
313 Body->Value = Cmd->Expression(Sec->getVA() + Off) - Sec->getVA();
314 return;
315 }
316 auto *Body = cast<DefinedRegular<ELFT>>(Cmd->Sym);
317 Body->Value = Cmd->Expression(Sec->getVA() + Off);
318}
319
Eugene Leviant20889c52016-08-31 08:13:33 +0000320// Linker script may define start and end symbols for special section types,
321// like .got, .eh_frame_hdr, .eh_frame and others. Those sections are not a list
322// of regular input input sections, therefore our way of defining symbols for
323// regular sections will not work. The approach we use for special section types
324// is not perfect - it handles only start and end symbols.
325template <class ELFT>
326void addStartEndSymbols(OutputSectionCommand *Cmd,
327 OutputSectionBase<ELFT> *Sec) {
328 bool Start = true;
329 BaseCommand *PrevCmd = nullptr;
330
331 for (std::unique_ptr<BaseCommand> &Base : Cmd->Commands) {
332 if (auto *AssignCmd = dyn_cast<SymbolAssignment>(Base.get())) {
Eugene Leviantdb741e72016-09-07 07:08:43 +0000333 assignSectionSymbol<ELFT>(AssignCmd, Sec, Start ? 0 : Sec->getSize());
Eugene Leviant20889c52016-08-31 08:13:33 +0000334 } else {
335 if (!Start && isa<SymbolAssignment>(PrevCmd))
336 error("section '" + Sec->getName() +
337 "' supports only start and end symbols");
338 Start = false;
339 }
340 PrevCmd = Base.get();
341 }
342}
343
344template <class ELFT>
345void assignOffsets(OutputSectionCommand *Cmd, OutputSectionBase<ELFT> *Sec) {
Eugene Leviantceabe802016-08-11 07:56:43 +0000346 auto *OutSec = dyn_cast<OutputSection<ELFT>>(Sec);
Rui Ueyama2de509c2016-08-12 00:55:08 +0000347 if (!OutSec) {
348 Sec->assignOffsets();
Eugene Leviant20889c52016-08-31 08:13:33 +0000349 // This section is not regular output section. However linker script may
350 // have defined start/end symbols for it. This case is handled below.
351 addStartEndSymbols(Cmd, Sec);
Eugene Leviantceabe802016-08-11 07:56:43 +0000352 return;
Rui Ueyama2de509c2016-08-12 00:55:08 +0000353 }
Eugene Leviantceabe802016-08-11 07:56:43 +0000354 typedef typename ELFT::uint uintX_t;
355 uintX_t Off = 0;
Eugene Leviant97403d12016-09-01 09:55:57 +0000356 auto ItCmd = Cmd->Commands.begin();
Eugene Leviantceabe802016-08-11 07:56:43 +0000357
Eugene Leviant97403d12016-09-01 09:55:57 +0000358 // Assigns values to all symbols following the given
359 // input section 'D' in output section 'Sec'. When symbols
360 // are in the beginning of output section the value of 'D'
361 // is nullptr.
362 auto AssignSuccessors = [&](InputSectionData *D) {
363 for (; ItCmd != Cmd->Commands.end(); ++ItCmd) {
364 auto *AssignCmd = dyn_cast<SymbolAssignment>(ItCmd->get());
365 if (!AssignCmd)
366 continue;
367 if (D != AssignCmd->GoesAfter)
368 break;
369
Eugene Leviant97403d12016-09-01 09:55:57 +0000370 if (AssignCmd->Name == ".") {
371 // Update to location counter means update to section size.
Eugene Leviantdb741e72016-09-07 07:08:43 +0000372 Off = AssignCmd->Expression(Sec->getVA() + Off) - Sec->getVA();
Eugene Leviant97403d12016-09-01 09:55:57 +0000373 Sec->setSize(Off);
374 continue;
375 }
Eugene Leviantdb741e72016-09-07 07:08:43 +0000376 assignSectionSymbol<ELFT>(AssignCmd, Sec, Off);
Eugene Leviantceabe802016-08-11 07:56:43 +0000377 }
Eugene Leviant97403d12016-09-01 09:55:57 +0000378 };
379
380 AssignSuccessors(nullptr);
381 for (InputSection<ELFT> *I : OutSec->Sections) {
382 Off = alignTo(Off, I->Alignment);
383 I->OutSecOff = Off;
384 Off += I->getSize();
Rui Ueyamaf4a30a52016-08-11 21:30:42 +0000385 // Update section size inside for-loop, so that SIZEOF
Eugene Leviantceabe802016-08-11 07:56:43 +0000386 // works correctly in the case below:
387 // .foo { *(.aaa) a = SIZEOF(.foo); *(.bbb) }
388 Sec->setSize(Off);
Eugene Leviant97403d12016-09-01 09:55:57 +0000389 // Add symbols following current input section.
390 AssignSuccessors(I);
Eugene Leviantceabe802016-08-11 07:56:43 +0000391 }
392}
393
George Rimar8f66df92016-08-12 20:38:20 +0000394template <class ELFT>
George Rimara14b13d2016-09-07 10:46:07 +0000395static std::vector<OutputSectionBase<ELFT> *>
396findSections(OutputSectionCommand &Cmd,
397 ArrayRef<OutputSectionBase<ELFT> *> Sections) {
398 std::vector<OutputSectionBase<ELFT> *> Ret;
399 for (OutputSectionBase<ELFT> *Sec : Sections)
400 if (Sec->getName() == Cmd.Name &&
401 checkConstraint(Sec->getFlags(), Cmd.Constraint))
402 Ret.push_back(Sec);
403 return Ret;
George Rimar8f66df92016-08-12 20:38:20 +0000404}
405
Rafael Espindolaa4b41dc2016-08-04 12:13:05 +0000406template <class ELFT> void LinkerScript<ELFT>::assignAddresses() {
George Rimar652852c2016-04-16 10:10:32 +0000407 // Orphan sections are sections present in the input files which
Rui Ueyama7c18c282016-04-18 21:00:40 +0000408 // are not explicitly placed into the output file by the linker script.
409 // We place orphan sections at end of file.
410 // Other linkers places them using some heuristics as described in
George Rimar652852c2016-04-16 10:10:32 +0000411 // https://sourceware.org/binutils/docs/ld/Orphan-Sections.html#Orphan-Sections.
Rui Ueyamae5cc6682016-08-12 00:36:56 +0000412 for (OutputSectionBase<ELFT> *Sec : *OutputSections) {
George Rimar652852c2016-04-16 10:10:32 +0000413 StringRef Name = Sec->getName();
Rui Ueyamac3e2a4b2016-04-21 20:30:00 +0000414 if (getSectionIndex(Name) == INT_MAX)
George Rimar076fe152016-07-21 06:43:01 +0000415 Opt.Commands.push_back(llvm::make_unique<OutputSectionCommand>(Name));
George Rimar652852c2016-04-16 10:10:32 +0000416 }
George Rimar652852c2016-04-16 10:10:32 +0000417
Rui Ueyama7c18c282016-04-18 21:00:40 +0000418 // Assign addresses as instructed by linker script SECTIONS sub-commands.
Rui Ueyama4f7500b2016-08-12 04:00:22 +0000419 Dot = getHeaderSize();
Eugene Leviant467c4d52016-07-01 10:27:36 +0000420 uintX_t MinVA = std::numeric_limits<uintX_t>::max();
George Rimar652852c2016-04-16 10:10:32 +0000421 uintX_t ThreadBssOffset = 0;
George Rimar652852c2016-04-16 10:10:32 +0000422
George Rimar076fe152016-07-21 06:43:01 +0000423 for (const std::unique_ptr<BaseCommand> &Base : Opt.Commands) {
424 if (auto *Cmd = dyn_cast<SymbolAssignment>(Base.get())) {
Rui Ueyama8d083e62016-07-29 05:48:39 +0000425 if (Cmd->Name == ".") {
426 Dot = Cmd->Expression(Dot);
427 } else if (Cmd->Sym) {
428 cast<DefinedRegular<ELFT>>(Cmd->Sym)->Value = Cmd->Expression(Dot);
429 }
George Rimar652852c2016-04-16 10:10:32 +0000430 continue;
431 }
432
George Rimareefa7582016-08-04 09:29:31 +0000433 if (auto *Cmd = dyn_cast<AssertCommand>(Base.get())) {
434 Cmd->Expression(Dot);
435 continue;
436 }
437
George Rimar076fe152016-07-21 06:43:01 +0000438 auto *Cmd = cast<OutputSectionCommand>(Base.get());
George Rimara14b13d2016-09-07 10:46:07 +0000439 for (OutputSectionBase<ELFT> *Sec :
440 findSections<ELFT>(*Cmd, *OutputSections)) {
George Rimar652852c2016-04-16 10:10:32 +0000441
George Rimara14b13d2016-09-07 10:46:07 +0000442 if (Cmd->AddrExpr)
443 Dot = Cmd->AddrExpr(Dot);
George Rimar58e5c4d2016-07-25 08:29:46 +0000444
George Rimara14b13d2016-09-07 10:46:07 +0000445 if ((Sec->getFlags() & SHF_TLS) && Sec->getType() == SHT_NOBITS) {
446 uintX_t TVA = Dot + ThreadBssOffset;
447 TVA = alignTo(TVA, Sec->getAlignment());
448 Sec->setVA(TVA);
449 assignOffsets(Cmd, Sec);
450 ThreadBssOffset = TVA - Dot + Sec->getSize();
451 continue;
452 }
453
454 if (!(Sec->getFlags() & SHF_ALLOC)) {
455 assignOffsets(Cmd, Sec);
456 continue;
457 }
458
459 Dot = alignTo(Dot, Sec->getAlignment());
460 Sec->setVA(Dot);
Eugene Leviant20889c52016-08-31 08:13:33 +0000461 assignOffsets(Cmd, Sec);
George Rimara14b13d2016-09-07 10:46:07 +0000462 MinVA = std::min(MinVA, Dot);
463 Dot += Sec->getSize();
George Rimar652852c2016-04-16 10:10:32 +0000464 }
465 }
Rui Ueyama52c4e172016-07-01 10:42:25 +0000466
Rafael Espindola64c32d62016-07-07 14:28:47 +0000467 // ELF and Program headers need to be right before the first section in
George Rimarb91e7112016-07-19 07:42:07 +0000468 // memory. Set their addresses accordingly.
Eugene Leviant467c4d52016-07-01 10:27:36 +0000469 MinVA = alignDown(MinVA - Out<ELFT>::ElfHeader->getSize() -
470 Out<ELFT>::ProgramHeaders->getSize(),
471 Target->PageSize);
472 Out<ELFT>::ElfHeader->setVA(MinVA);
473 Out<ELFT>::ProgramHeaders->setVA(Out<ELFT>::ElfHeader->getSize() + MinVA);
George Rimar652852c2016-04-16 10:10:32 +0000474}
475
Rui Ueyama464daad2016-08-22 04:55:20 +0000476// Creates program headers as instructed by PHDRS linker script command.
Rui Ueyama07320e42016-04-20 20:13:41 +0000477template <class ELFT>
Rafael Espindolaa4b41dc2016-08-04 12:13:05 +0000478std::vector<PhdrEntry<ELFT>> LinkerScript<ELFT>::createPhdrs() {
Rui Ueyamaedebbdf2016-07-24 23:47:31 +0000479 std::vector<PhdrEntry<ELFT>> Ret;
Eugene Leviantbbe38602016-07-19 09:25:43 +0000480
Rui Ueyama464daad2016-08-22 04:55:20 +0000481 // Process PHDRS and FILEHDR keywords because they are not
482 // real output sections and cannot be added in the following loop.
Eugene Leviantbbe38602016-07-19 09:25:43 +0000483 for (const PhdrsCommand &Cmd : Opt.PhdrsCommands) {
Rui Ueyamaedebbdf2016-07-24 23:47:31 +0000484 Ret.emplace_back(Cmd.Type, Cmd.Flags == UINT_MAX ? PF_R : Cmd.Flags);
485 PhdrEntry<ELFT> &Phdr = Ret.back();
Eugene Leviantbbe38602016-07-19 09:25:43 +0000486
487 if (Cmd.HasFilehdr)
Rui Ueyamaadca2452016-07-23 14:18:48 +0000488 Phdr.add(Out<ELFT>::ElfHeader);
Eugene Leviantbbe38602016-07-19 09:25:43 +0000489 if (Cmd.HasPhdrs)
Rui Ueyamaadca2452016-07-23 14:18:48 +0000490 Phdr.add(Out<ELFT>::ProgramHeaders);
Eugene Leviant56b21c82016-09-09 09:46:16 +0000491
492 if (Cmd.LMAExpr) {
493 Phdr.H.p_paddr = Cmd.LMAExpr(0);
494 Phdr.HasLMA = true;
495 }
Eugene Leviantbbe38602016-07-19 09:25:43 +0000496 }
497
Rui Ueyama464daad2016-08-22 04:55:20 +0000498 // Add output sections to program headers.
Rui Ueyamaedebbdf2016-07-24 23:47:31 +0000499 PhdrEntry<ELFT> *Load = nullptr;
500 uintX_t Flags = PF_R;
Rui Ueyama464daad2016-08-22 04:55:20 +0000501 for (OutputSectionBase<ELFT> *Sec : *OutputSections) {
Eugene Leviantbbe38602016-07-19 09:25:43 +0000502 if (!(Sec->getFlags() & SHF_ALLOC))
503 break;
504
Rui Ueyamaedebbdf2016-07-24 23:47:31 +0000505 std::vector<size_t> PhdrIds = getPhdrIndices(Sec->getName());
Eugene Leviantbbe38602016-07-19 09:25:43 +0000506 if (!PhdrIds.empty()) {
507 // Assign headers specified by linker script
508 for (size_t Id : PhdrIds) {
Rui Ueyamaedebbdf2016-07-24 23:47:31 +0000509 Ret[Id].add(Sec);
Eugene Leviant865bf862016-07-21 10:43:25 +0000510 if (Opt.PhdrsCommands[Id].Flags == UINT_MAX)
Rafael Espindola0b113672016-07-27 14:10:56 +0000511 Ret[Id].H.p_flags |= Sec->getPhdrFlags();
Eugene Leviantbbe38602016-07-19 09:25:43 +0000512 }
513 } else {
514 // If we have no load segment or flags've changed then we want new load
515 // segment.
Rafael Espindola0b113672016-07-27 14:10:56 +0000516 uintX_t NewFlags = Sec->getPhdrFlags();
Eugene Leviantbbe38602016-07-19 09:25:43 +0000517 if (Load == nullptr || Flags != NewFlags) {
Rui Ueyamaedebbdf2016-07-24 23:47:31 +0000518 Load = &*Ret.emplace(Ret.end(), PT_LOAD, NewFlags);
Eugene Leviantbbe38602016-07-19 09:25:43 +0000519 Flags = NewFlags;
520 }
Rui Ueyama18f084f2016-07-20 19:36:41 +0000521 Load->add(Sec);
Eugene Leviantbbe38602016-07-19 09:25:43 +0000522 }
Eugene Leviantbbe38602016-07-19 09:25:43 +0000523 }
Rui Ueyamaedebbdf2016-07-24 23:47:31 +0000524 return Ret;
Eugene Leviantbbe38602016-07-19 09:25:43 +0000525}
526
Eugene Leviantf9bc3bd2016-08-16 06:40:58 +0000527template <class ELFT> bool LinkerScript<ELFT>::ignoreInterpSection() {
528 // Ignore .interp section in case we have PHDRS specification
529 // and PT_INTERP isn't listed.
530 return !Opt.PhdrsCommands.empty() &&
531 llvm::find_if(Opt.PhdrsCommands, [](const PhdrsCommand &Cmd) {
532 return Cmd.Type == PT_INTERP;
533 }) == Opt.PhdrsCommands.end();
534}
535
Eugene Leviantbbe38602016-07-19 09:25:43 +0000536template <class ELFT>
Rui Ueyama07320e42016-04-20 20:13:41 +0000537ArrayRef<uint8_t> LinkerScript<ELFT>::getFiller(StringRef Name) {
George Rimarf6c3cce2016-07-21 07:48:54 +0000538 for (const std::unique_ptr<BaseCommand> &Base : Opt.Commands)
539 if (auto *Cmd = dyn_cast<OutputSectionCommand>(Base.get()))
540 if (Cmd->Name == Name)
541 return Cmd->Filler;
542 return {};
George Rimare2ee72b2016-02-26 14:48:31 +0000543}
544
George Rimar206fffa2016-08-17 08:16:57 +0000545template <class ELFT> Expr LinkerScript<ELFT>::getLma(StringRef Name) {
George Rimar8ceadb32016-08-17 07:44:19 +0000546 for (const std::unique_ptr<BaseCommand> &Base : Opt.Commands)
547 if (auto *Cmd = dyn_cast<OutputSectionCommand>(Base.get()))
548 if (Cmd->LmaExpr && Cmd->Name == Name)
549 return Cmd->LmaExpr;
550 return {};
551}
552
Rui Ueyamac3e2a4b2016-04-21 20:30:00 +0000553// Returns the index of the given section name in linker script
554// SECTIONS commands. Sections are laid out as the same order as they
555// were in the script. If a given name did not appear in the script,
556// it returns INT_MAX, so that it will be laid out at end of file.
George Rimar076fe152016-07-21 06:43:01 +0000557template <class ELFT> int LinkerScript<ELFT>::getSectionIndex(StringRef Name) {
Rui Ueyamaf510fa62016-07-26 00:21:15 +0000558 int I = 0;
559 for (std::unique_ptr<BaseCommand> &Base : Opt.Commands) {
560 if (auto *Cmd = dyn_cast<OutputSectionCommand>(Base.get()))
561 if (Cmd->Name == Name)
562 return I;
563 ++I;
564 }
565 return INT_MAX;
George Rimar71b26e92016-04-21 10:22:02 +0000566}
567
568// A compartor to sort output sections. Returns -1 or 1 if
569// A or B are mentioned in linker script. Otherwise, returns 0.
Rui Ueyama07320e42016-04-20 20:13:41 +0000570template <class ELFT>
571int LinkerScript<ELFT>::compareSections(StringRef A, StringRef B) {
Rui Ueyamac3e2a4b2016-04-21 20:30:00 +0000572 int I = getSectionIndex(A);
573 int J = getSectionIndex(B);
574 if (I == INT_MAX && J == INT_MAX)
Rui Ueyama717677a2016-02-11 21:17:59 +0000575 return 0;
576 return I < J ? -1 : 1;
577}
578
Eugene Leviantbbe38602016-07-19 09:25:43 +0000579template <class ELFT> bool LinkerScript<ELFT>::hasPhdrsCommands() {
580 return !Opt.PhdrsCommands.empty();
581}
582
George Rimar9e694502016-07-29 16:18:47 +0000583template <class ELFT>
George Rimar884e7862016-09-08 08:19:13 +0000584uint64_t LinkerScript<ELFT>::getOutputSectionAddress(StringRef Name) {
George Rimar96659df2016-08-30 09:54:01 +0000585 for (OutputSectionBase<ELFT> *Sec : *OutputSections)
586 if (Sec->getName() == Name)
587 return Sec->getVA();
588 error("undefined section " + Name);
589 return 0;
590}
591
592template <class ELFT>
George Rimar884e7862016-09-08 08:19:13 +0000593uint64_t LinkerScript<ELFT>::getOutputSectionSize(StringRef Name) {
George Rimar9e694502016-07-29 16:18:47 +0000594 for (OutputSectionBase<ELFT> *Sec : *OutputSections)
595 if (Sec->getName() == Name)
596 return Sec->getSize();
597 error("undefined section " + Name);
598 return 0;
599}
600
Eugene Leviant36fac7f2016-09-08 09:08:30 +0000601template <class ELFT>
602uint64_t LinkerScript<ELFT>::getOutputSectionAlign(StringRef Name) {
603 for (OutputSectionBase<ELFT> *Sec : *OutputSections)
604 if (Sec->getName() == Name)
605 return Sec->getAlignment();
606 error("undefined section " + Name);
607 return 0;
608}
609
George Rimar884e7862016-09-08 08:19:13 +0000610template <class ELFT> uint64_t LinkerScript<ELFT>::getHeaderSize() {
George Rimare32a3592016-08-10 07:59:34 +0000611 return Out<ELFT>::ElfHeader->getSize() + Out<ELFT>::ProgramHeaders->getSize();
612}
613
George Rimar884e7862016-09-08 08:19:13 +0000614template <class ELFT> uint64_t LinkerScript<ELFT>::getSymbolValue(StringRef S) {
615 if (SymbolBody *B = Symtab<ELFT>::X->find(S))
616 return B->getVA<ELFT>();
617 error("symbol not found: " + S);
618 return 0;
619}
620
Eugene Leviantbbe38602016-07-19 09:25:43 +0000621// Returns indices of ELF headers containing specific section, identified
622// by Name. Each index is a zero based number of ELF header listed within
623// PHDRS {} script block.
624template <class ELFT>
Rui Ueyamaedebbdf2016-07-24 23:47:31 +0000625std::vector<size_t> LinkerScript<ELFT>::getPhdrIndices(StringRef SectionName) {
George Rimar076fe152016-07-21 06:43:01 +0000626 for (const std::unique_ptr<BaseCommand> &Base : Opt.Commands) {
627 auto *Cmd = dyn_cast<OutputSectionCommand>(Base.get());
Rui Ueyamaedebbdf2016-07-24 23:47:31 +0000628 if (!Cmd || Cmd->Name != SectionName)
George Rimar31d842f2016-07-20 16:43:03 +0000629 continue;
630
Rui Ueyama29c5a2a2016-07-26 00:27:36 +0000631 std::vector<size_t> Ret;
632 for (StringRef PhdrName : Cmd->Phdrs)
633 Ret.push_back(getPhdrIndex(PhdrName));
634 return Ret;
Eugene Leviantbbe38602016-07-19 09:25:43 +0000635 }
George Rimar31d842f2016-07-20 16:43:03 +0000636 return {};
Eugene Leviantbbe38602016-07-19 09:25:43 +0000637}
638
Rui Ueyama29c5a2a2016-07-26 00:27:36 +0000639template <class ELFT>
640size_t LinkerScript<ELFT>::getPhdrIndex(StringRef PhdrName) {
641 size_t I = 0;
642 for (PhdrsCommand &Cmd : Opt.PhdrsCommands) {
643 if (Cmd.Name == PhdrName)
644 return I;
645 ++I;
646 }
647 error("section header '" + PhdrName + "' is not listed in PHDRS");
648 return 0;
649}
650
Rui Ueyama07320e42016-04-20 20:13:41 +0000651class elf::ScriptParser : public ScriptParserBase {
George Rimarc3794e52016-02-24 09:21:47 +0000652 typedef void (ScriptParser::*Handler)();
653
Rui Ueyamaf7c5fbb2015-09-30 17:23:26 +0000654public:
Rui Ueyama07320e42016-04-20 20:13:41 +0000655 ScriptParser(StringRef S, bool B) : ScriptParserBase(S), IsUnderSysroot(B) {}
George Rimarf23b2322016-02-19 10:45:45 +0000656
George Rimar20b65982016-08-31 09:08:26 +0000657 void readLinkerScript();
658 void readVersionScript();
Rui Ueyamaf7c5fbb2015-09-30 17:23:26 +0000659
660private:
Rui Ueyama52a15092015-10-11 03:28:42 +0000661 void addFile(StringRef Path);
662
Rui Ueyamaf7c5fbb2015-09-30 17:23:26 +0000663 void readAsNeeded();
Denis Protivensky90c50992015-10-08 06:48:38 +0000664 void readEntry();
George Rimar83f406c2015-10-19 17:35:12 +0000665 void readExtern();
Rui Ueyamaf7c5fbb2015-09-30 17:23:26 +0000666 void readGroup();
Rui Ueyama31aa1f82015-10-11 01:31:55 +0000667 void readInclude();
Rui Ueyamaee592822015-10-07 00:25:09 +0000668 void readOutput();
Davide Italiano9159ce92015-10-12 21:50:08 +0000669 void readOutputArch();
Rui Ueyamaf7c5fbb2015-09-30 17:23:26 +0000670 void readOutputFormat();
Eugene Leviantbbe38602016-07-19 09:25:43 +0000671 void readPhdrs();
Davide Italiano68a39a62015-10-08 17:51:41 +0000672 void readSearchDir();
Denis Protivensky8e3b38a2015-11-12 09:52:08 +0000673 void readSections();
Rui Ueyama95769b42016-08-31 20:03:54 +0000674 void readVersion();
675 void readVersionScriptCommand();
Denis Protivensky8e3b38a2015-11-12 09:52:08 +0000676
Rui Ueyama113cdec2016-07-24 23:05:57 +0000677 SymbolAssignment *readAssignment(StringRef Name);
George Rimarff1f29e2016-09-06 13:51:57 +0000678 std::vector<uint8_t> readFill();
Rui Ueyama10416562016-08-04 02:03:27 +0000679 OutputSectionCommand *readOutputSectionDescription(StringRef OutSec);
George Rimarff1f29e2016-09-06 13:51:57 +0000680 std::vector<uint8_t> readOutputSectionFiller(StringRef Tok);
Eugene Leviantbbe38602016-07-19 09:25:43 +0000681 std::vector<StringRef> readOutputSectionPhdrs();
George Rimara2496cb2016-08-30 09:46:59 +0000682 InputSectionDescription *readInputSectionDescription(StringRef Tok);
George Rimarc91930a2016-09-02 21:17:20 +0000683 Regex readFilePatterns();
George Rimara2496cb2016-08-30 09:46:59 +0000684 InputSectionDescription *readInputSectionRules(StringRef FilePattern);
Eugene Leviantbbe38602016-07-19 09:25:43 +0000685 unsigned readPhdrType();
Rui Ueyama742c3832016-08-04 22:27:00 +0000686 SortKind readSortKind();
Petr Hoseka35e39c2016-08-16 01:11:16 +0000687 SymbolAssignment *readProvideHidden(bool Provide, bool Hidden);
Eugene Leviantdb741e72016-09-07 07:08:43 +0000688 SymbolAssignment *readProvideOrAssignment(StringRef Tok, bool MakeAbsolute);
George Rimar03fc0102016-07-28 07:18:23 +0000689 void readSort();
George Rimareefa7582016-08-04 09:29:31 +0000690 Expr readAssert();
Rui Ueyama708019c2016-07-24 18:19:40 +0000691
692 Expr readExpr();
693 Expr readExpr1(Expr Lhs, int MinPrec);
694 Expr readPrimary();
695 Expr readTernary(Expr Cond);
Rui Ueyama6ad7dfc2016-08-17 18:59:16 +0000696 Expr readParenExpr();
Rui Ueyamaf7c5fbb2015-09-30 17:23:26 +0000697
George Rimar20b65982016-08-31 09:08:26 +0000698 // For parsing version script.
699 void readExtern(std::vector<SymbolVersion> *Globals);
Rui Ueyama95769b42016-08-31 20:03:54 +0000700 void readVersionDeclaration(StringRef VerStr);
George Rimar20b65982016-08-31 09:08:26 +0000701 void readGlobal(StringRef VerStr);
702 void readLocal();
703
Rui Ueyama07320e42016-04-20 20:13:41 +0000704 ScriptConfiguration &Opt = *ScriptConfig;
705 StringSaver Saver = {ScriptConfig->Alloc};
Simon Atanasyan16b0cc92015-11-26 05:53:00 +0000706 bool IsUnderSysroot;
Rui Ueyamaf7c5fbb2015-09-30 17:23:26 +0000707};
Rui Ueyamaf7c5fbb2015-09-30 17:23:26 +0000708
George Rimar20b65982016-08-31 09:08:26 +0000709void ScriptParser::readVersionScript() {
Rui Ueyama95769b42016-08-31 20:03:54 +0000710 readVersionScriptCommand();
711 if (!atEOF())
712 setError("EOF expected, but got " + next());
713}
714
715void ScriptParser::readVersionScriptCommand() {
George Rimar20b65982016-08-31 09:08:26 +0000716 if (skip("{")) {
Rui Ueyama95769b42016-08-31 20:03:54 +0000717 readVersionDeclaration("");
George Rimar20b65982016-08-31 09:08:26 +0000718 return;
719 }
720
Rui Ueyama95769b42016-08-31 20:03:54 +0000721 while (!atEOF() && !Error && peek() != "}") {
George Rimar20b65982016-08-31 09:08:26 +0000722 StringRef VerStr = next();
723 if (VerStr == "{") {
Rui Ueyama95769b42016-08-31 20:03:54 +0000724 setError("anonymous version definition is used in "
725 "combination with other version definitions");
George Rimar20b65982016-08-31 09:08:26 +0000726 return;
727 }
728 expect("{");
Rui Ueyama95769b42016-08-31 20:03:54 +0000729 readVersionDeclaration(VerStr);
George Rimar20b65982016-08-31 09:08:26 +0000730 }
731}
732
Rui Ueyama95769b42016-08-31 20:03:54 +0000733void ScriptParser::readVersion() {
734 expect("{");
735 readVersionScriptCommand();
736 expect("}");
737}
738
George Rimar20b65982016-08-31 09:08:26 +0000739void ScriptParser::readLinkerScript() {
Rui Ueyamaf7c5fbb2015-09-30 17:23:26 +0000740 while (!atEOF()) {
741 StringRef Tok = next();
Rui Ueyamaa27eecc2016-09-02 18:52:41 +0000742 if (Tok == ";")
743 continue;
744
745 if (Tok == "ENTRY") {
746 readEntry();
747 } else if (Tok == "EXTERN") {
748 readExtern();
749 } else if (Tok == "GROUP" || Tok == "INPUT") {
750 readGroup();
751 } else if (Tok == "INCLUDE") {
752 readInclude();
753 } else if (Tok == "OUTPUT") {
754 readOutput();
755 } else if (Tok == "OUTPUT_ARCH") {
756 readOutputArch();
757 } else if (Tok == "OUTPUT_FORMAT") {
758 readOutputFormat();
759 } else if (Tok == "PHDRS") {
760 readPhdrs();
761 } else if (Tok == "SEARCH_DIR") {
762 readSearchDir();
763 } else if (Tok == "SECTIONS") {
764 readSections();
765 } else if (Tok == "VERSION") {
766 readVersion();
Eugene Leviantdb741e72016-09-07 07:08:43 +0000767 } else if (SymbolAssignment *Cmd = readProvideOrAssignment(Tok, true)) {
Petr Hoseke5d3ca52016-08-31 15:31:17 +0000768 if (Opt.HasContents)
769 Opt.Commands.emplace_back(Cmd);
770 else
771 Opt.Assignments.emplace_back(Cmd);
772 } else {
George Rimar57610422016-03-11 14:43:02 +0000773 setError("unknown directive: " + Tok);
Petr Hoseke5d3ca52016-08-31 15:31:17 +0000774 }
Rui Ueyamaf7c5fbb2015-09-30 17:23:26 +0000775 }
776}
777
Rui Ueyama717677a2016-02-11 21:17:59 +0000778void ScriptParser::addFile(StringRef S) {
Simon Atanasyan16b0cc92015-11-26 05:53:00 +0000779 if (IsUnderSysroot && S.startswith("/")) {
780 SmallString<128> Path;
781 (Config->Sysroot + S).toStringRef(Path);
782 if (sys::fs::exists(Path)) {
783 Driver->addFile(Saver.save(Path.str()));
784 return;
785 }
786 }
787
Rui Ueyamaf03f3cc2015-10-13 00:09:21 +0000788 if (sys::path::is_absolute(S)) {
Rui Ueyama52a15092015-10-11 03:28:42 +0000789 Driver->addFile(S);
790 } else if (S.startswith("=")) {
791 if (Config->Sysroot.empty())
792 Driver->addFile(S.substr(1));
793 else
794 Driver->addFile(Saver.save(Config->Sysroot + "/" + S.substr(1)));
795 } else if (S.startswith("-l")) {
Rui Ueyama21eecb42016-02-02 21:13:09 +0000796 Driver->addLibrary(S.substr(2));
Simon Atanasyana1b8fc32015-11-26 20:23:46 +0000797 } else if (sys::fs::exists(S)) {
798 Driver->addFile(S);
Rui Ueyama52a15092015-10-11 03:28:42 +0000799 } else {
800 std::string Path = findFromSearchPaths(S);
801 if (Path.empty())
George Rimar777f9632016-03-12 08:31:34 +0000802 setError("unable to find " + S);
Rui Ueyama025d59b2016-02-02 20:27:59 +0000803 else
804 Driver->addFile(Saver.save(Path));
Rui Ueyama52a15092015-10-11 03:28:42 +0000805 }
806}
807
Rui Ueyama717677a2016-02-11 21:17:59 +0000808void ScriptParser::readAsNeeded() {
Rui Ueyamaf7c5fbb2015-09-30 17:23:26 +0000809 expect("(");
Rui Ueyama35da9b62015-10-11 20:59:12 +0000810 bool Orig = Config->AsNeeded;
811 Config->AsNeeded = true;
Rui Ueyamaa2acc932016-08-05 01:25:45 +0000812 while (!Error && !skip(")"))
George Rimarcd574a52016-09-09 14:35:36 +0000813 addFile(unquote(next()));
Rui Ueyama35da9b62015-10-11 20:59:12 +0000814 Config->AsNeeded = Orig;
Rui Ueyamaf7c5fbb2015-09-30 17:23:26 +0000815}
816
Rui Ueyama717677a2016-02-11 21:17:59 +0000817void ScriptParser::readEntry() {
Denis Protivensky90c50992015-10-08 06:48:38 +0000818 // -e <symbol> takes predecence over ENTRY(<symbol>).
819 expect("(");
820 StringRef Tok = next();
821 if (Config->Entry.empty())
822 Config->Entry = Tok;
823 expect(")");
824}
825
Rui Ueyama717677a2016-02-11 21:17:59 +0000826void ScriptParser::readExtern() {
George Rimar83f406c2015-10-19 17:35:12 +0000827 expect("(");
Rui Ueyamaa2acc932016-08-05 01:25:45 +0000828 while (!Error && !skip(")"))
829 Config->Undefined.push_back(next());
George Rimar83f406c2015-10-19 17:35:12 +0000830}
831
Rui Ueyama717677a2016-02-11 21:17:59 +0000832void ScriptParser::readGroup() {
Rui Ueyamaf7c5fbb2015-09-30 17:23:26 +0000833 expect("(");
Rui Ueyamaa2acc932016-08-05 01:25:45 +0000834 while (!Error && !skip(")")) {
Rui Ueyamaf7c5fbb2015-09-30 17:23:26 +0000835 StringRef Tok = next();
Rui Ueyamaa2acc932016-08-05 01:25:45 +0000836 if (Tok == "AS_NEEDED")
Rui Ueyamaf7c5fbb2015-09-30 17:23:26 +0000837 readAsNeeded();
Rui Ueyamaa2acc932016-08-05 01:25:45 +0000838 else
George Rimarcd574a52016-09-09 14:35:36 +0000839 addFile(unquote(Tok));
Rui Ueyamaf7c5fbb2015-09-30 17:23:26 +0000840 }
841}
842
Rui Ueyama717677a2016-02-11 21:17:59 +0000843void ScriptParser::readInclude() {
Rui Ueyama31aa1f82015-10-11 01:31:55 +0000844 StringRef Tok = next();
George Rimarcd574a52016-09-09 14:35:36 +0000845 auto MBOrErr = MemoryBuffer::getFile(unquote(Tok));
Rui Ueyama025d59b2016-02-02 20:27:59 +0000846 if (!MBOrErr) {
George Rimar57610422016-03-11 14:43:02 +0000847 setError("cannot open " + Tok);
Rui Ueyama025d59b2016-02-02 20:27:59 +0000848 return;
849 }
Rui Ueyama31aa1f82015-10-11 01:31:55 +0000850 std::unique_ptr<MemoryBuffer> &MB = *MBOrErr;
Rui Ueyamaa47ee682015-10-11 01:53:04 +0000851 StringRef S = Saver.save(MB->getMemBufferRef().getBuffer());
852 std::vector<StringRef> V = tokenize(S);
Rui Ueyama31aa1f82015-10-11 01:31:55 +0000853 Tokens.insert(Tokens.begin() + Pos, V.begin(), V.end());
Rui Ueyama31aa1f82015-10-11 01:31:55 +0000854}
855
Rui Ueyama717677a2016-02-11 21:17:59 +0000856void ScriptParser::readOutput() {
Rui Ueyamaee592822015-10-07 00:25:09 +0000857 // -o <file> takes predecence over OUTPUT(<file>).
858 expect("(");
859 StringRef Tok = next();
860 if (Config->OutputFile.empty())
George Rimarcd574a52016-09-09 14:35:36 +0000861 Config->OutputFile = unquote(Tok);
Rui Ueyamaee592822015-10-07 00:25:09 +0000862 expect(")");
863}
864
Rui Ueyama717677a2016-02-11 21:17:59 +0000865void ScriptParser::readOutputArch() {
Davide Italiano9159ce92015-10-12 21:50:08 +0000866 // Error checking only for now.
867 expect("(");
868 next();
869 expect(")");
870}
871
Rui Ueyama717677a2016-02-11 21:17:59 +0000872void ScriptParser::readOutputFormat() {
Rui Ueyamaf7c5fbb2015-09-30 17:23:26 +0000873 // Error checking only for now.
874 expect("(");
875 next();
Davide Italiano6836c612015-10-12 21:08:41 +0000876 StringRef Tok = next();
877 if (Tok == ")")
George Rimar6c55f0e2016-09-08 08:20:30 +0000878 return;
Rui Ueyama025d59b2016-02-02 20:27:59 +0000879 if (Tok != ",") {
George Rimar57610422016-03-11 14:43:02 +0000880 setError("unexpected token: " + Tok);
Rui Ueyama025d59b2016-02-02 20:27:59 +0000881 return;
882 }
Davide Italiano6836c612015-10-12 21:08:41 +0000883 next();
884 expect(",");
885 next();
Rui Ueyamaf7c5fbb2015-09-30 17:23:26 +0000886 expect(")");
887}
888
Eugene Leviantbbe38602016-07-19 09:25:43 +0000889void ScriptParser::readPhdrs() {
890 expect("{");
891 while (!Error && !skip("}")) {
892 StringRef Tok = next();
Eugene Leviant56b21c82016-09-09 09:46:16 +0000893 Opt.PhdrsCommands.push_back(
894 {Tok, PT_NULL, false, false, UINT_MAX, nullptr});
Eugene Leviantbbe38602016-07-19 09:25:43 +0000895 PhdrsCommand &PhdrCmd = Opt.PhdrsCommands.back();
896
897 PhdrCmd.Type = readPhdrType();
898 do {
899 Tok = next();
900 if (Tok == ";")
901 break;
902 if (Tok == "FILEHDR")
903 PhdrCmd.HasFilehdr = true;
904 else if (Tok == "PHDRS")
905 PhdrCmd.HasPhdrs = true;
Eugene Leviant56b21c82016-09-09 09:46:16 +0000906 else if (Tok == "AT")
907 PhdrCmd.LMAExpr = readParenExpr();
Eugene Leviant865bf862016-07-21 10:43:25 +0000908 else if (Tok == "FLAGS") {
909 expect("(");
Rafael Espindolaeb685cd2016-08-02 22:14:57 +0000910 // Passing 0 for the value of dot is a bit of a hack. It means that
911 // we accept expressions like ".|1".
912 PhdrCmd.Flags = readExpr()(0);
Eugene Leviant865bf862016-07-21 10:43:25 +0000913 expect(")");
914 } else
Eugene Leviantbbe38602016-07-19 09:25:43 +0000915 setError("unexpected header attribute: " + Tok);
916 } while (!Error);
917 }
918}
919
Rui Ueyama717677a2016-02-11 21:17:59 +0000920void ScriptParser::readSearchDir() {
Davide Italiano68a39a62015-10-08 17:51:41 +0000921 expect("(");
Rui Ueyama86c5fb82016-09-08 23:26:54 +0000922 StringRef Tok = next();
Rui Ueyama6c7ad132016-09-02 19:20:33 +0000923 if (!Config->Nostdlib)
George Rimarcd574a52016-09-09 14:35:36 +0000924 Config->SearchPaths.push_back(unquote(Tok));
Davide Italiano68a39a62015-10-08 17:51:41 +0000925 expect(")");
926}
927
Rui Ueyama717677a2016-02-11 21:17:59 +0000928void ScriptParser::readSections() {
Rui Ueyama3de0a332016-07-29 03:31:09 +0000929 Opt.HasContents = true;
Denis Protivensky8e3b38a2015-11-12 09:52:08 +0000930 expect("{");
George Rimar652852c2016-04-16 10:10:32 +0000931 while (!Error && !skip("}")) {
Rui Ueyama113cdec2016-07-24 23:05:57 +0000932 StringRef Tok = next();
Eugene Leviantdb741e72016-09-07 07:08:43 +0000933 BaseCommand *Cmd = readProvideOrAssignment(Tok, true);
Eugene Leviantceabe802016-08-11 07:56:43 +0000934 if (!Cmd) {
935 if (Tok == "ASSERT")
936 Cmd = new AssertCommand(readAssert());
937 else
938 Cmd = readOutputSectionDescription(Tok);
Rui Ueyama708019c2016-07-24 18:19:40 +0000939 }
Rui Ueyama10416562016-08-04 02:03:27 +0000940 Opt.Commands.emplace_back(Cmd);
George Rimar652852c2016-04-16 10:10:32 +0000941 }
Denis Protivensky8e3b38a2015-11-12 09:52:08 +0000942}
943
Rui Ueyama708019c2016-07-24 18:19:40 +0000944static int precedence(StringRef Op) {
945 return StringSwitch<int>(Op)
946 .Case("*", 4)
947 .Case("/", 4)
948 .Case("+", 3)
949 .Case("-", 3)
950 .Case("<", 2)
951 .Case(">", 2)
952 .Case(">=", 2)
953 .Case("<=", 2)
954 .Case("==", 2)
955 .Case("!=", 2)
956 .Case("&", 1)
Rafael Espindolacc3dd622016-08-22 21:33:35 +0000957 .Case("|", 1)
Rui Ueyama708019c2016-07-24 18:19:40 +0000958 .Default(-1);
959}
960
George Rimarc91930a2016-09-02 21:17:20 +0000961Regex ScriptParser::readFilePatterns() {
Rui Ueyama10416562016-08-04 02:03:27 +0000962 std::vector<StringRef> V;
963 while (!Error && !skip(")"))
964 V.push_back(next());
George Rimarc91930a2016-09-02 21:17:20 +0000965 return compileGlobPatterns(V);
George Rimar0702c4e2016-07-29 15:32:46 +0000966}
967
Rui Ueyama742c3832016-08-04 22:27:00 +0000968SortKind ScriptParser::readSortKind() {
969 if (skip("SORT") || skip("SORT_BY_NAME"))
970 return SortByName;
971 if (skip("SORT_BY_ALIGNMENT"))
972 return SortByAlignment;
973 return SortNone;
974}
975
George Rimara2496cb2016-08-30 09:46:59 +0000976InputSectionDescription *
977ScriptParser::readInputSectionRules(StringRef FilePattern) {
George Rimarc91930a2016-09-02 21:17:20 +0000978 auto *Cmd = new InputSectionDescription(FilePattern);
Davide Italiano0ed42b02016-07-25 21:47:13 +0000979 expect("(");
Davide Italianoe7282792016-07-27 01:44:01 +0000980
Rui Ueyama742c3832016-08-04 22:27:00 +0000981 // Read EXCLUDE_FILE().
Davide Italianoe7282792016-07-27 01:44:01 +0000982 if (skip("EXCLUDE_FILE")) {
983 expect("(");
George Rimarc91930a2016-09-02 21:17:20 +0000984 Cmd->ExcludedFileRe = readFilePatterns();
Davide Italiano0ed42b02016-07-25 21:47:13 +0000985 }
George Rimar06598002016-07-28 21:51:30 +0000986
Rui Ueyama742c3832016-08-04 22:27:00 +0000987 // Read SORT().
988 if (SortKind K1 = readSortKind()) {
989 Cmd->SortOuter = K1;
George Rimar0702c4e2016-07-29 15:32:46 +0000990 expect("(");
Rui Ueyama742c3832016-08-04 22:27:00 +0000991 if (SortKind K2 = readSortKind()) {
992 Cmd->SortInner = K2;
George Rimar350ece42016-08-03 08:35:59 +0000993 expect("(");
George Rimarc91930a2016-09-02 21:17:20 +0000994 Cmd->SectionRe = readFilePatterns();
George Rimar350ece42016-08-03 08:35:59 +0000995 expect(")");
996 } else {
George Rimarc91930a2016-09-02 21:17:20 +0000997 Cmd->SectionRe = readFilePatterns();
George Rimar350ece42016-08-03 08:35:59 +0000998 }
George Rimar0702c4e2016-07-29 15:32:46 +0000999 expect(")");
Rui Ueyama10416562016-08-04 02:03:27 +00001000 return Cmd;
George Rimar06598002016-07-28 21:51:30 +00001001 }
George Rimar0702c4e2016-07-29 15:32:46 +00001002
George Rimarc91930a2016-09-02 21:17:20 +00001003 Cmd->SectionRe = readFilePatterns();
Rui Ueyama10416562016-08-04 02:03:27 +00001004 return Cmd;
Davide Italianoe7282792016-07-27 01:44:01 +00001005}
1006
George Rimara2496cb2016-08-30 09:46:59 +00001007InputSectionDescription *
1008ScriptParser::readInputSectionDescription(StringRef Tok) {
George Rimar06598002016-07-28 21:51:30 +00001009 // Input section wildcard can be surrounded by KEEP.
1010 // https://sourceware.org/binutils/docs/ld/Input-Section-Keep.html#Input-Section-Keep
George Rimara2496cb2016-08-30 09:46:59 +00001011 if (Tok == "KEEP") {
George Rimar06598002016-07-28 21:51:30 +00001012 expect("(");
George Rimara2496cb2016-08-30 09:46:59 +00001013 StringRef FilePattern = next();
1014 InputSectionDescription *Cmd = readInputSectionRules(FilePattern);
George Rimar06598002016-07-28 21:51:30 +00001015 expect(")");
George Rimarc91930a2016-09-02 21:17:20 +00001016 Opt.KeptSections.push_back(&Cmd->SectionRe);
Rui Ueyama10416562016-08-04 02:03:27 +00001017 return Cmd;
George Rimar06598002016-07-28 21:51:30 +00001018 }
George Rimara2496cb2016-08-30 09:46:59 +00001019 return readInputSectionRules(Tok);
Davide Italiano0ed42b02016-07-25 21:47:13 +00001020}
1021
George Rimar03fc0102016-07-28 07:18:23 +00001022void ScriptParser::readSort() {
1023 expect("(");
1024 expect("CONSTRUCTORS");
1025 expect(")");
1026}
1027
George Rimareefa7582016-08-04 09:29:31 +00001028Expr ScriptParser::readAssert() {
1029 expect("(");
1030 Expr E = readExpr();
1031 expect(",");
George Rimarcd574a52016-09-09 14:35:36 +00001032 StringRef Msg = unquote(next());
George Rimareefa7582016-08-04 09:29:31 +00001033 expect(")");
1034 return [=](uint64_t Dot) {
1035 uint64_t V = E(Dot);
1036 if (!V)
1037 error(Msg);
1038 return V;
1039 };
1040}
1041
Rui Ueyama25150e82016-09-06 17:46:43 +00001042// Reads a FILL(expr) command. We handle the FILL command as an
1043// alias for =fillexp section attribute, which is different from
1044// what GNU linkers do.
1045// https://sourceware.org/binutils/docs/ld/Output-Section-Data.html
George Rimarff1f29e2016-09-06 13:51:57 +00001046std::vector<uint8_t> ScriptParser::readFill() {
1047 expect("(");
1048 std::vector<uint8_t> V = readOutputSectionFiller(next());
1049 expect(")");
1050 expect(";");
1051 return V;
1052}
1053
Rui Ueyama10416562016-08-04 02:03:27 +00001054OutputSectionCommand *
1055ScriptParser::readOutputSectionDescription(StringRef OutSec) {
George Rimar076fe152016-07-21 06:43:01 +00001056 OutputSectionCommand *Cmd = new OutputSectionCommand(OutSec);
George Rimar58e5c4d2016-07-25 08:29:46 +00001057
1058 // Read an address expression.
1059 // https://sourceware.org/binutils/docs/ld/Output-Section-Address.html#Output-Section-Address
1060 if (peek() != ":")
1061 Cmd->AddrExpr = readExpr();
1062
Denis Protivensky8e3b38a2015-11-12 09:52:08 +00001063 expect(":");
Davide Italiano246f6812016-07-22 03:36:24 +00001064
George Rimar8ceadb32016-08-17 07:44:19 +00001065 if (skip("AT"))
Rui Ueyama6ad7dfc2016-08-17 18:59:16 +00001066 Cmd->LmaExpr = readParenExpr();
George Rimar630c6172016-07-26 18:06:29 +00001067 if (skip("ALIGN"))
Rui Ueyama6ad7dfc2016-08-17 18:59:16 +00001068 Cmd->AlignExpr = readParenExpr();
George Rimardb24d9c2016-08-19 15:18:23 +00001069 if (skip("SUBALIGN"))
1070 Cmd->SubalignExpr = readParenExpr();
George Rimar630c6172016-07-26 18:06:29 +00001071
Davide Italiano246f6812016-07-22 03:36:24 +00001072 // Parse constraints.
1073 if (skip("ONLY_IF_RO"))
Rui Ueyamaefc40662016-07-25 22:00:10 +00001074 Cmd->Constraint = ConstraintKind::ReadOnly;
Davide Italiano246f6812016-07-22 03:36:24 +00001075 if (skip("ONLY_IF_RW"))
Rui Ueyamaefc40662016-07-25 22:00:10 +00001076 Cmd->Constraint = ConstraintKind::ReadWrite;
Denis Protivensky8e3b38a2015-11-12 09:52:08 +00001077 expect("{");
Rui Ueyama8ec77e62016-04-21 22:00:51 +00001078
Rui Ueyama025d59b2016-02-02 20:27:59 +00001079 while (!Error && !skip("}")) {
Eugene Leviantceabe802016-08-11 07:56:43 +00001080 StringRef Tok = next();
Eugene Leviantdb741e72016-09-07 07:08:43 +00001081 if (SymbolAssignment *Assignment = readProvideOrAssignment(Tok, false))
Eugene Leviantceabe802016-08-11 07:56:43 +00001082 Cmd->Commands.emplace_back(Assignment);
George Rimarff1f29e2016-09-06 13:51:57 +00001083 else if (Tok == "FILL")
1084 Cmd->Filler = readFill();
Eugene Leviantceabe802016-08-11 07:56:43 +00001085 else if (Tok == "SORT")
George Rimar03fc0102016-07-28 07:18:23 +00001086 readSort();
George Rimara2496cb2016-08-30 09:46:59 +00001087 else if (peek() == "(")
1088 Cmd->Commands.emplace_back(readInputSectionDescription(Tok));
Eugene Leviantceabe802016-08-11 07:56:43 +00001089 else
1090 setError("unknown command " + Tok);
Denis Protivensky8e3b38a2015-11-12 09:52:08 +00001091 }
George Rimar076fe152016-07-21 06:43:01 +00001092 Cmd->Phdrs = readOutputSectionPhdrs();
George Rimarff1f29e2016-09-06 13:51:57 +00001093 if (peek().startswith("="))
1094 Cmd->Filler = readOutputSectionFiller(next().drop_front());
Rui Ueyama10416562016-08-04 02:03:27 +00001095 return Cmd;
Rui Ueyamaf71caa22016-07-29 06:14:07 +00001096}
Rui Ueyama8ec77e62016-04-21 22:00:51 +00001097
Rui Ueyama2c8f1f02016-08-29 22:01:21 +00001098// Read "=<number>" where <number> is an octal/decimal/hexadecimal number.
1099// https://sourceware.org/binutils/docs/ld/Output-Section-Fill.html
1100//
1101// ld.gold is not fully compatible with ld.bfd. ld.bfd handles
1102// hexstrings as blobs of arbitrary sizes, while ld.gold handles them
1103// as 32-bit big-endian values. We will do the same as ld.gold does
1104// because it's simpler than what ld.bfd does.
George Rimarff1f29e2016-09-06 13:51:57 +00001105std::vector<uint8_t> ScriptParser::readOutputSectionFiller(StringRef Tok) {
Rui Ueyama965827d2016-08-03 23:25:15 +00001106 uint32_t V;
George Rimarff1f29e2016-09-06 13:51:57 +00001107 if (Tok.getAsInteger(0, V)) {
Rui Ueyama965827d2016-08-03 23:25:15 +00001108 setError("invalid filler expression: " + Tok);
Rui Ueyamaf71caa22016-07-29 06:14:07 +00001109 return {};
George Rimare2ee72b2016-02-26 14:48:31 +00001110 }
Rui Ueyama2c8f1f02016-08-29 22:01:21 +00001111 return {uint8_t(V >> 24), uint8_t(V >> 16), uint8_t(V >> 8), uint8_t(V)};
Denis Protivensky8e3b38a2015-11-12 09:52:08 +00001112}
1113
Petr Hoseka35e39c2016-08-16 01:11:16 +00001114SymbolAssignment *ScriptParser::readProvideHidden(bool Provide, bool Hidden) {
Eugene Levianta31c91b2016-07-22 07:38:40 +00001115 expect("(");
Rui Ueyama174e0a12016-07-29 00:29:25 +00001116 SymbolAssignment *Cmd = readAssignment(next());
Petr Hoseka35e39c2016-08-16 01:11:16 +00001117 Cmd->Provide = Provide;
Rui Ueyama174e0a12016-07-29 00:29:25 +00001118 Cmd->Hidden = Hidden;
Eugene Levianta31c91b2016-07-22 07:38:40 +00001119 expect(")");
1120 expect(";");
Rui Ueyama10416562016-08-04 02:03:27 +00001121 return Cmd;
Eugene Levianteda81a12016-07-12 06:39:48 +00001122}
1123
Eugene Leviantdb741e72016-09-07 07:08:43 +00001124SymbolAssignment *ScriptParser::readProvideOrAssignment(StringRef Tok,
1125 bool MakeAbsolute) {
Eugene Leviantceabe802016-08-11 07:56:43 +00001126 SymbolAssignment *Cmd = nullptr;
1127 if (peek() == "=" || peek() == "+=") {
1128 Cmd = readAssignment(Tok);
1129 expect(";");
1130 } else if (Tok == "PROVIDE") {
Petr Hoseka35e39c2016-08-16 01:11:16 +00001131 Cmd = readProvideHidden(true, false);
1132 } else if (Tok == "HIDDEN") {
1133 Cmd = readProvideHidden(false, true);
Eugene Leviantceabe802016-08-11 07:56:43 +00001134 } else if (Tok == "PROVIDE_HIDDEN") {
Petr Hoseka35e39c2016-08-16 01:11:16 +00001135 Cmd = readProvideHidden(true, true);
Eugene Leviantceabe802016-08-11 07:56:43 +00001136 }
Eugene Leviantdb741e72016-09-07 07:08:43 +00001137 if (Cmd && MakeAbsolute)
1138 Cmd->IsAbsolute = true;
Eugene Leviantceabe802016-08-11 07:56:43 +00001139 return Cmd;
1140}
1141
George Rimar30835ea2016-07-28 21:08:56 +00001142static uint64_t getSymbolValue(StringRef S, uint64_t Dot) {
1143 if (S == ".")
1144 return Dot;
George Rimar884e7862016-09-08 08:19:13 +00001145 return ScriptBase->getSymbolValue(S);
George Rimare32a3592016-08-10 07:59:34 +00001146}
1147
George Rimar30835ea2016-07-28 21:08:56 +00001148SymbolAssignment *ScriptParser::readAssignment(StringRef Name) {
1149 StringRef Op = next();
Eugene Leviantdb741e72016-09-07 07:08:43 +00001150 bool IsAbsolute = false;
1151 Expr E;
George Rimar30835ea2016-07-28 21:08:56 +00001152 assert(Op == "=" || Op == "+=");
Eugene Leviantdb741e72016-09-07 07:08:43 +00001153 if (skip("ABSOLUTE")) {
1154 E = readParenExpr();
1155 IsAbsolute = true;
1156 } else {
1157 E = readExpr();
1158 }
George Rimar30835ea2016-07-28 21:08:56 +00001159 if (Op == "+=")
1160 E = [=](uint64_t Dot) { return getSymbolValue(Name, Dot) + E(Dot); };
Eugene Leviantdb741e72016-09-07 07:08:43 +00001161 return new SymbolAssignment(Name, E, IsAbsolute);
George Rimar30835ea2016-07-28 21:08:56 +00001162}
1163
1164// This is an operator-precedence parser to parse a linker
1165// script expression.
1166Expr ScriptParser::readExpr() { return readExpr1(readPrimary(), 0); }
1167
Rui Ueyama36c1cd22016-08-05 01:04:59 +00001168static Expr combine(StringRef Op, Expr L, Expr R) {
1169 if (Op == "*")
1170 return [=](uint64_t Dot) { return L(Dot) * R(Dot); };
1171 if (Op == "/") {
1172 return [=](uint64_t Dot) -> uint64_t {
1173 uint64_t RHS = R(Dot);
1174 if (RHS == 0) {
1175 error("division by zero");
1176 return 0;
1177 }
1178 return L(Dot) / RHS;
1179 };
1180 }
1181 if (Op == "+")
1182 return [=](uint64_t Dot) { return L(Dot) + R(Dot); };
1183 if (Op == "-")
1184 return [=](uint64_t Dot) { return L(Dot) - R(Dot); };
1185 if (Op == "<")
1186 return [=](uint64_t Dot) { return L(Dot) < R(Dot); };
1187 if (Op == ">")
1188 return [=](uint64_t Dot) { return L(Dot) > R(Dot); };
1189 if (Op == ">=")
1190 return [=](uint64_t Dot) { return L(Dot) >= R(Dot); };
1191 if (Op == "<=")
1192 return [=](uint64_t Dot) { return L(Dot) <= R(Dot); };
1193 if (Op == "==")
1194 return [=](uint64_t Dot) { return L(Dot) == R(Dot); };
1195 if (Op == "!=")
1196 return [=](uint64_t Dot) { return L(Dot) != R(Dot); };
1197 if (Op == "&")
1198 return [=](uint64_t Dot) { return L(Dot) & R(Dot); };
Rafael Espindolacc3dd622016-08-22 21:33:35 +00001199 if (Op == "|")
1200 return [=](uint64_t Dot) { return L(Dot) | R(Dot); };
Rui Ueyama36c1cd22016-08-05 01:04:59 +00001201 llvm_unreachable("invalid operator");
1202}
1203
Rui Ueyama708019c2016-07-24 18:19:40 +00001204// This is a part of the operator-precedence parser. This function
1205// assumes that the remaining token stream starts with an operator.
1206Expr ScriptParser::readExpr1(Expr Lhs, int MinPrec) {
1207 while (!atEOF() && !Error) {
1208 // Read an operator and an expression.
1209 StringRef Op1 = peek();
1210 if (Op1 == "?")
1211 return readTernary(Lhs);
1212 if (precedence(Op1) < MinPrec)
Eugene Levianteda81a12016-07-12 06:39:48 +00001213 break;
Rui Ueyama708019c2016-07-24 18:19:40 +00001214 next();
1215 Expr Rhs = readPrimary();
1216
1217 // Evaluate the remaining part of the expression first if the
1218 // next operator has greater precedence than the previous one.
1219 // For example, if we have read "+" and "3", and if the next
1220 // operator is "*", then we'll evaluate 3 * ... part first.
1221 while (!atEOF()) {
1222 StringRef Op2 = peek();
1223 if (precedence(Op2) <= precedence(Op1))
1224 break;
1225 Rhs = readExpr1(Rhs, precedence(Op2));
1226 }
1227
1228 Lhs = combine(Op1, Lhs, Rhs);
Eugene Levianteda81a12016-07-12 06:39:48 +00001229 }
Rui Ueyama708019c2016-07-24 18:19:40 +00001230 return Lhs;
1231}
1232
1233uint64_t static getConstant(StringRef S) {
Michael J. Spencere2cc07b2016-08-17 02:10:51 +00001234 if (S == "COMMONPAGESIZE")
Rui Ueyama708019c2016-07-24 18:19:40 +00001235 return Target->PageSize;
Michael J. Spencere2cc07b2016-08-17 02:10:51 +00001236 if (S == "MAXPAGESIZE")
1237 return Target->MaxPageSize;
Rui Ueyama708019c2016-07-24 18:19:40 +00001238 error("unknown constant: " + S);
1239 return 0;
1240}
1241
Rui Ueyama626e0b02016-09-02 18:19:00 +00001242// Parses Tok as an integer. Returns true if successful.
1243// It recognizes hexadecimal (prefixed with "0x" or suffixed with "H")
1244// and decimal numbers. Decimal numbers may have "K" (kilo) or
1245// "M" (mega) prefixes.
George Rimar9f2f7ad2016-09-02 16:01:42 +00001246static bool readInteger(StringRef Tok, uint64_t &Result) {
Simon Atanasyaneaeafb22016-09-02 21:54:35 +00001247 if (Tok.startswith("-")) {
1248 if (!readInteger(Tok.substr(1), Result))
1249 return false;
1250 Result = -Result;
1251 return true;
1252 }
George Rimar9f2f7ad2016-09-02 16:01:42 +00001253 if (Tok.startswith_lower("0x"))
1254 return !Tok.substr(2).getAsInteger(16, Result);
1255 if (Tok.endswith_lower("H"))
1256 return !Tok.drop_back().getAsInteger(16, Result);
1257
1258 int Suffix = 1;
1259 if (Tok.endswith_lower("K")) {
1260 Suffix = 1024;
1261 Tok = Tok.drop_back();
1262 } else if (Tok.endswith_lower("M")) {
1263 Suffix = 1024 * 1024;
1264 Tok = Tok.drop_back();
1265 }
1266 if (Tok.getAsInteger(10, Result))
1267 return false;
1268 Result *= Suffix;
1269 return true;
1270}
1271
Rui Ueyama708019c2016-07-24 18:19:40 +00001272Expr ScriptParser::readPrimary() {
Rui Ueyama6ad7dfc2016-08-17 18:59:16 +00001273 if (peek() == "(")
1274 return readParenExpr();
Rui Ueyama708019c2016-07-24 18:19:40 +00001275
Rui Ueyama6ad7dfc2016-08-17 18:59:16 +00001276 StringRef Tok = next();
Rui Ueyama708019c2016-07-24 18:19:40 +00001277
Simon Atanasyaneaeafb22016-09-02 21:54:35 +00001278 if (Tok == "~") {
1279 Expr E = readPrimary();
1280 return [=](uint64_t Dot) { return ~E(Dot); };
1281 }
1282 if (Tok == "-") {
1283 Expr E = readPrimary();
1284 return [=](uint64_t Dot) { return -E(Dot); };
1285 }
1286
Rui Ueyama708019c2016-07-24 18:19:40 +00001287 // Built-in functions are parsed here.
1288 // https://sourceware.org/binutils/docs/ld/Builtin-Functions.html.
George Rimar96659df2016-08-30 09:54:01 +00001289 if (Tok == "ADDR") {
1290 expect("(");
1291 StringRef Name = next();
1292 expect(")");
George Rimar884e7862016-09-08 08:19:13 +00001293 return
1294 [=](uint64_t Dot) { return ScriptBase->getOutputSectionAddress(Name); };
George Rimar96659df2016-08-30 09:54:01 +00001295 }
George Rimareefa7582016-08-04 09:29:31 +00001296 if (Tok == "ASSERT")
1297 return readAssert();
Rui Ueyama708019c2016-07-24 18:19:40 +00001298 if (Tok == "ALIGN") {
Rui Ueyama6ad7dfc2016-08-17 18:59:16 +00001299 Expr E = readParenExpr();
Rui Ueyama708019c2016-07-24 18:19:40 +00001300 return [=](uint64_t Dot) { return alignTo(Dot, E(Dot)); };
1301 }
1302 if (Tok == "CONSTANT") {
1303 expect("(");
1304 StringRef Tok = next();
1305 expect(")");
1306 return [=](uint64_t Dot) { return getConstant(Tok); };
1307 }
Rafael Espindola54c145c2016-07-28 18:16:24 +00001308 if (Tok == "SEGMENT_START") {
1309 expect("(");
1310 next();
1311 expect(",");
1312 uint64_t Val;
1313 next().getAsInteger(0, Val);
1314 expect(")");
1315 return [=](uint64_t Dot) { return Val; };
1316 }
Rui Ueyama708019c2016-07-24 18:19:40 +00001317 if (Tok == "DATA_SEGMENT_ALIGN") {
1318 expect("(");
1319 Expr E = readExpr();
1320 expect(",");
1321 readExpr();
1322 expect(")");
Rui Ueyamaf7791bb2016-07-26 19:34:10 +00001323 return [=](uint64_t Dot) { return alignTo(Dot, E(Dot)); };
Rui Ueyama708019c2016-07-24 18:19:40 +00001324 }
1325 if (Tok == "DATA_SEGMENT_END") {
1326 expect("(");
1327 expect(".");
1328 expect(")");
1329 return [](uint64_t Dot) { return Dot; };
1330 }
George Rimar276b4e62016-07-26 17:58:44 +00001331 // GNU linkers implements more complicated logic to handle
1332 // DATA_SEGMENT_RELRO_END. We instead ignore the arguments and just align to
1333 // the next page boundary for simplicity.
1334 if (Tok == "DATA_SEGMENT_RELRO_END") {
1335 expect("(");
1336 next();
1337 expect(",");
1338 readExpr();
1339 expect(")");
1340 return [](uint64_t Dot) { return alignTo(Dot, Target->PageSize); };
1341 }
George Rimar9e694502016-07-29 16:18:47 +00001342 if (Tok == "SIZEOF") {
1343 expect("(");
1344 StringRef Name = next();
1345 expect(")");
George Rimar884e7862016-09-08 08:19:13 +00001346 return [=](uint64_t Dot) { return ScriptBase->getOutputSectionSize(Name); };
George Rimar9e694502016-07-29 16:18:47 +00001347 }
Eugene Leviant36fac7f2016-09-08 09:08:30 +00001348 if (Tok == "ALIGNOF") {
1349 expect("(");
1350 StringRef Name = next();
1351 expect(")");
1352 return
1353 [=](uint64_t Dot) { return ScriptBase->getOutputSectionAlign(Name); };
1354 }
George Rimare32a3592016-08-10 07:59:34 +00001355 if (Tok == "SIZEOF_HEADERS")
George Rimar884e7862016-09-08 08:19:13 +00001356 return [=](uint64_t Dot) { return ScriptBase->getHeaderSize(); };
Rui Ueyama708019c2016-07-24 18:19:40 +00001357
George Rimar9f2f7ad2016-09-02 16:01:42 +00001358 // Tok is a literal number.
1359 uint64_t V;
1360 if (readInteger(Tok, V))
1361 return [=](uint64_t Dot) { return V; };
1362
1363 // Tok is a symbol name.
1364 if (Tok != "." && !isValidCIdentifier(Tok))
1365 setError("malformed number: " + Tok);
1366 return [=](uint64_t Dot) { return getSymbolValue(Tok, Dot); };
Rui Ueyama708019c2016-07-24 18:19:40 +00001367}
1368
1369Expr ScriptParser::readTernary(Expr Cond) {
1370 next();
1371 Expr L = readExpr();
1372 expect(":");
1373 Expr R = readExpr();
1374 return [=](uint64_t Dot) { return Cond(Dot) ? L(Dot) : R(Dot); };
1375}
1376
Rui Ueyama6ad7dfc2016-08-17 18:59:16 +00001377Expr ScriptParser::readParenExpr() {
1378 expect("(");
1379 Expr E = readExpr();
1380 expect(")");
1381 return E;
1382}
1383
Eugene Leviantbbe38602016-07-19 09:25:43 +00001384std::vector<StringRef> ScriptParser::readOutputSectionPhdrs() {
1385 std::vector<StringRef> Phdrs;
1386 while (!Error && peek().startswith(":")) {
1387 StringRef Tok = next();
1388 Tok = (Tok.size() == 1) ? next() : Tok.substr(1);
1389 if (Tok.empty()) {
1390 setError("section header name is empty");
1391 break;
1392 }
Rui Ueyama047404f2016-07-20 19:36:36 +00001393 Phdrs.push_back(Tok);
Eugene Leviantbbe38602016-07-19 09:25:43 +00001394 }
1395 return Phdrs;
1396}
1397
1398unsigned ScriptParser::readPhdrType() {
Eugene Leviantbbe38602016-07-19 09:25:43 +00001399 StringRef Tok = next();
Rui Ueyamab0f6c592016-07-20 19:36:38 +00001400 unsigned Ret = StringSwitch<unsigned>(Tok)
George Rimar6c55f0e2016-09-08 08:20:30 +00001401 .Case("PT_NULL", PT_NULL)
1402 .Case("PT_LOAD", PT_LOAD)
1403 .Case("PT_DYNAMIC", PT_DYNAMIC)
1404 .Case("PT_INTERP", PT_INTERP)
1405 .Case("PT_NOTE", PT_NOTE)
1406 .Case("PT_SHLIB", PT_SHLIB)
1407 .Case("PT_PHDR", PT_PHDR)
1408 .Case("PT_TLS", PT_TLS)
1409 .Case("PT_GNU_EH_FRAME", PT_GNU_EH_FRAME)
1410 .Case("PT_GNU_STACK", PT_GNU_STACK)
1411 .Case("PT_GNU_RELRO", PT_GNU_RELRO)
1412 .Default(-1);
Eugene Leviantbbe38602016-07-19 09:25:43 +00001413
Rui Ueyamab0f6c592016-07-20 19:36:38 +00001414 if (Ret == (unsigned)-1) {
1415 setError("invalid program header type: " + Tok);
1416 return PT_NULL;
1417 }
1418 return Ret;
Eugene Leviantbbe38602016-07-19 09:25:43 +00001419}
1420
Rui Ueyama95769b42016-08-31 20:03:54 +00001421void ScriptParser::readVersionDeclaration(StringRef VerStr) {
George Rimar20b65982016-08-31 09:08:26 +00001422 // Identifiers start at 2 because 0 and 1 are reserved
1423 // for VER_NDX_LOCAL and VER_NDX_GLOBAL constants.
1424 size_t VersionId = Config->VersionDefinitions.size() + 2;
1425 Config->VersionDefinitions.push_back({VerStr, VersionId});
1426
1427 if (skip("global:") || peek() != "local:")
1428 readGlobal(VerStr);
1429 if (skip("local:"))
1430 readLocal();
1431 expect("}");
1432
1433 // Each version may have a parent version. For example, "Ver2" defined as
1434 // "Ver2 { global: foo; local: *; } Ver1;" has "Ver1" as a parent. This
1435 // version hierarchy is, probably against your instinct, purely for human; the
1436 // runtime doesn't care about them at all. In LLD, we simply skip the token.
1437 if (!VerStr.empty() && peek() != ";")
1438 next();
1439 expect(";");
1440}
1441
1442void ScriptParser::readLocal() {
1443 Config->DefaultSymbolVersion = VER_NDX_LOCAL;
1444 expect("*");
1445 expect(";");
1446}
1447
1448void ScriptParser::readExtern(std::vector<SymbolVersion> *Globals) {
George Rimarcd574a52016-09-09 14:35:36 +00001449 expect("\"C++\"");
George Rimar20b65982016-08-31 09:08:26 +00001450 expect("{");
1451
1452 for (;;) {
1453 if (peek() == "}" || Error)
1454 break;
George Rimarcd574a52016-09-09 14:35:36 +00001455 bool HasWildcard = !peek().startswith("\"") && hasWildcard(peek());
1456 Globals->push_back({unquote(next()), true, HasWildcard});
George Rimar20b65982016-08-31 09:08:26 +00001457 expect(";");
1458 }
1459
1460 expect("}");
1461 expect(";");
1462}
1463
1464void ScriptParser::readGlobal(StringRef VerStr) {
1465 std::vector<SymbolVersion> *Globals;
1466 if (VerStr.empty())
1467 Globals = &Config->VersionScriptGlobals;
1468 else
1469 Globals = &Config->VersionDefinitions.back().Globals;
1470
1471 for (;;) {
1472 if (skip("extern"))
1473 readExtern(Globals);
1474
1475 StringRef Cur = peek();
1476 if (Cur == "}" || Cur == "local:" || Error)
1477 return;
1478 next();
George Rimarcd574a52016-09-09 14:35:36 +00001479 Globals->push_back({unquote(Cur), false, hasWildcard(Cur)});
George Rimar20b65982016-08-31 09:08:26 +00001480 expect(";");
1481 }
1482}
1483
Simon Atanasyan16b0cc92015-11-26 05:53:00 +00001484static bool isUnderSysroot(StringRef Path) {
1485 if (Config->Sysroot == "")
1486 return false;
1487 for (; !Path.empty(); Path = sys::path::parent_path(Path))
1488 if (sys::fs::equivalent(Config->Sysroot, Path))
1489 return true;
1490 return false;
1491}
1492
Rui Ueyama07320e42016-04-20 20:13:41 +00001493void elf::readLinkerScript(MemoryBufferRef MB) {
Simon Atanasyan16b0cc92015-11-26 05:53:00 +00001494 StringRef Path = MB.getBufferIdentifier();
George Rimar20b65982016-08-31 09:08:26 +00001495 ScriptParser(MB.getBuffer(), isUnderSysroot(Path)).readLinkerScript();
1496}
1497
1498void elf::readVersionScript(MemoryBufferRef MB) {
1499 ScriptParser(MB.getBuffer(), false).readVersionScript();
Rui Ueyamaf7c5fbb2015-09-30 17:23:26 +00001500}
Rui Ueyama1ebc8ed2016-02-12 21:47:28 +00001501
Rui Ueyama07320e42016-04-20 20:13:41 +00001502template class elf::LinkerScript<ELF32LE>;
1503template class elf::LinkerScript<ELF32BE>;
1504template class elf::LinkerScript<ELF64LE>;
1505template class elf::LinkerScript<ELF64BE>;