blob: 7ae61e9a84aeff4325726daa7c507f5d555a3531 [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
George Rimar575208c2016-09-15 19:15:12 +0000113static bool comparePriority(InputSectionData *A, InputSectionData *B) {
114 return getPriority(A->Name) < getPriority(B->Name);
115}
116
Rafael Espindolac0028d32016-09-08 20:47:52 +0000117static bool compareName(InputSectionData *A, InputSectionData *B) {
Rafael Espindola042a3f22016-09-08 14:06:08 +0000118 return A->Name < B->Name;
Rui Ueyama742c3832016-08-04 22:27:00 +0000119}
George Rimar350ece42016-08-03 08:35:59 +0000120
Rafael Espindolac0028d32016-09-08 20:47:52 +0000121static bool compareAlignment(InputSectionData *A, InputSectionData *B) {
Rui Ueyama742c3832016-08-04 22:27:00 +0000122 // ">" is not a mistake. Larger alignments are placed before smaller
123 // alignments in order to reduce the amount of padding necessary.
124 // This is compatible with GNU.
125 return A->Alignment > B->Alignment;
126}
George Rimar350ece42016-08-03 08:35:59 +0000127
Rafael Espindolac0028d32016-09-08 20:47:52 +0000128static std::function<bool(InputSectionData *, InputSectionData *)>
Rui Ueyama742c3832016-08-04 22:27:00 +0000129getComparator(SortKind K) {
George Rimar575208c2016-09-15 19:15:12 +0000130 if (K == SortByPriority)
131 return comparePriority;
Rui Ueyama742c3832016-08-04 22:27:00 +0000132 if (K == SortByName)
Rafael Espindolac0028d32016-09-08 20:47:52 +0000133 return compareName;
134 return compareAlignment;
Rui Ueyama742c3832016-08-04 22:27:00 +0000135}
George Rimar0702c4e2016-07-29 15:32:46 +0000136
George Rimar8f66df92016-08-12 20:38:20 +0000137static bool checkConstraint(uint64_t Flags, ConstraintKind Kind) {
138 bool RO = (Kind == ConstraintKind::ReadOnly);
139 bool RW = (Kind == ConstraintKind::ReadWrite);
140 bool Writable = Flags & SHF_WRITE;
Rui Ueyamaadcdb662016-09-06 22:50:48 +0000141 return !(RO && Writable) && !(RW && !Writable);
George Rimar8f66df92016-08-12 20:38:20 +0000142}
143
Rui Ueyama48c3f1c2016-08-12 00:27:23 +0000144template <class ELFT>
George Rimar06ae6832016-08-12 09:07:57 +0000145static bool matchConstraints(ArrayRef<InputSectionBase<ELFT> *> Sections,
146 ConstraintKind Kind) {
George Rimar8f66df92016-08-12 20:38:20 +0000147 if (Kind == ConstraintKind::NoConstraint)
148 return true;
149 return llvm::all_of(Sections, [=](InputSectionBase<ELFT> *Sec) {
150 return checkConstraint(Sec->getSectionHdr()->sh_flags, Kind);
George Rimar06ae6832016-08-12 09:07:57 +0000151 });
152}
153
Rafael Espindolabe94e1b2016-09-14 14:32:08 +0000154// Returns input sections filtered by given glob patterns.
155template <class ELFT>
156std::vector<InputSectionBase<ELFT> *>
157LinkerScript<ELFT>::getInputSections(const InputSectionDescription *I) {
158 const Regex &Re = I->SectionRe;
159 std::vector<InputSectionBase<ELFT> *> Ret;
160 for (ObjectFile<ELFT> *F : Symtab<ELFT>::X->getObjectFiles())
161 if (fileMatches(I, sys::path::filename(F->getName())))
162 for (InputSectionBase<ELFT> *S : F->getSections())
163 if (!isDiscarded(S) && !S->OutSec &&
164 const_cast<Regex &>(Re).match(S->Name))
165 Ret.push_back(S);
166
167 if (const_cast<Regex &>(Re).match("COMMON"))
168 Ret.push_back(CommonInputSection<ELFT>::X);
169 return Ret;
170}
171
172template <class ELFT>
173void LinkerScript<ELFT>::discard(ArrayRef<InputSectionBase<ELFT> *> V) {
174 for (InputSectionBase<ELFT> *S : V) {
175 S->Live = false;
176 reportDiscarded(S);
177 }
178}
179
George Rimar06ae6832016-08-12 09:07:57 +0000180template <class ELFT>
Rui Ueyama0b9ce6a2016-08-12 03:16:56 +0000181std::vector<InputSectionBase<ELFT> *>
George Rimar06ae6832016-08-12 09:07:57 +0000182LinkerScript<ELFT>::createInputSectionList(OutputSectionCommand &OutCmd) {
Rui Ueyama0b9ce6a2016-08-12 03:16:56 +0000183 std::vector<InputSectionBase<ELFT> *> Ret;
Eugene Leviant97403d12016-09-01 09:55:57 +0000184 DenseSet<InputSectionBase<ELFT> *> SectionIndex;
Rui Ueyamae7f912c2016-08-03 21:12:09 +0000185
George Rimar06ae6832016-08-12 09:07:57 +0000186 for (const std::unique_ptr<BaseCommand> &Base : OutCmd.Commands) {
187 if (auto *OutCmd = dyn_cast<SymbolAssignment>(Base.get())) {
188 if (shouldDefine<ELFT>(OutCmd))
Eugene Leviantdb741e72016-09-07 07:08:43 +0000189 addSymbol<ELFT>(OutCmd);
Eugene Leviant97403d12016-09-01 09:55:57 +0000190 OutCmd->GoesAfter = Ret.empty() ? nullptr : Ret.back();
Rui Ueyama0b9ce6a2016-08-12 03:16:56 +0000191 continue;
192 }
193
194 auto *Cmd = cast<InputSectionDescription>(Base.get());
195 std::vector<InputSectionBase<ELFT> *> V = getInputSections(Cmd);
George Rimar06ae6832016-08-12 09:07:57 +0000196 if (!matchConstraints<ELFT>(V, OutCmd.Constraint))
197 continue;
Rui Ueyama0b9ce6a2016-08-12 03:16:56 +0000198 if (Cmd->SortInner)
Rafael Espindolac0028d32016-09-08 20:47:52 +0000199 std::stable_sort(V.begin(), V.end(), getComparator(Cmd->SortInner));
Rui Ueyama0b9ce6a2016-08-12 03:16:56 +0000200 if (Cmd->SortOuter)
Rafael Espindolac0028d32016-09-08 20:47:52 +0000201 std::stable_sort(V.begin(), V.end(), getComparator(Cmd->SortOuter));
Eugene Leviant97403d12016-09-01 09:55:57 +0000202
203 // Add all input sections corresponding to rule 'Cmd' to
204 // resulting vector. We do not add duplicate input sections.
205 for (InputSectionBase<ELFT> *S : V)
206 if (SectionIndex.insert(S).second)
207 Ret.push_back(S);
Rui Ueyama0b9ce6a2016-08-12 03:16:56 +0000208 }
209 return Ret;
210}
211
George Rimar6c55f0e2016-09-08 08:20:30 +0000212template <class ELFT> void LinkerScript<ELFT>::createAssignments() {
Petr Hoseke5d3ca52016-08-31 15:31:17 +0000213 for (const std::unique_ptr<SymbolAssignment> &Cmd : Opt.Assignments) {
214 if (shouldDefine<ELFT>(Cmd.get()))
215 addRegular<ELFT>(Cmd.get());
216 if (Cmd->Sym)
217 cast<DefinedRegular<ELFT>>(Cmd->Sym)->Value = Cmd->Expression(0);
218 }
219}
220
221template <class ELFT>
Rafael Espindola10897f12016-09-13 14:23:14 +0000222static SectionKey<ELFT::Is64Bits> createKey(InputSectionBase<ELFT> *C,
223 StringRef OutsecName) {
224 // When using linker script the merge rules are different.
225 // Unfortunately, linker scripts are name based. This means that expressions
226 // like *(.foo*) can refer to multiple input sections that would normally be
227 // placed in different output sections. We cannot put them in different
228 // output sections or we would produce wrong results for
229 // start = .; *(.foo.*) end = .; *(.bar)
230 // and a mapping of .foo1 and .bar1 to one section and .foo2 and .bar2 to
231 // another. The problem is that there is no way to layout those output
232 // sections such that the .foo sections are the only thing between the
233 // start and end symbols.
234
235 // An extra annoyance is that we cannot simply disable merging of the contents
236 // of SHF_MERGE sections, but our implementation requires one output section
237 // per "kind" (string or not, which size/aligment).
238 // Fortunately, creating symbols in the middle of a merge section is not
239 // supported by bfd or gold, so we can just create multiple section in that
240 // case.
241 const typename ELFT::Shdr *H = C->getSectionHdr();
242 typedef typename ELFT::uint uintX_t;
243 uintX_t Flags = H->sh_flags & (SHF_MERGE | SHF_STRINGS);
244
245 uintX_t Alignment = 0;
246 if (isa<MergeInputSection<ELFT>>(C))
247 Alignment = std::max(H->sh_addralign, H->sh_entsize);
248
249 return SectionKey<ELFT::Is64Bits>{OutsecName, /*Type*/ 0, Flags, Alignment};
250}
251
252template <class ELFT>
Rui Ueyama0b9ce6a2016-08-12 03:16:56 +0000253void LinkerScript<ELFT>::createSections(OutputSectionFactory<ELFT> &Factory) {
Rafael Espindola28c15972016-09-13 13:00:06 +0000254 auto AddSec = [&](InputSectionBase<ELFT> *Sec, StringRef Name) {
255 OutputSectionBase<ELFT> *OutSec;
256 bool IsNew;
Rafael Espindola10897f12016-09-13 14:23:14 +0000257 std::tie(OutSec, IsNew) = Factory.create(createKey(Sec, Name), Sec);
Rafael Espindola28c15972016-09-13 13:00:06 +0000258 if (IsNew)
259 OutputSections->push_back(OutSec);
260 return OutSec;
261 };
262
Rui Ueyama48c3f1c2016-08-12 00:27:23 +0000263 for (const std::unique_ptr<BaseCommand> &Base1 : Opt.Commands) {
Rui Ueyama2ab5f732016-08-12 03:33:04 +0000264 if (auto *Cmd = dyn_cast<SymbolAssignment>(Base1.get())) {
265 if (shouldDefine<ELFT>(Cmd))
266 addRegular<ELFT>(Cmd);
267 continue;
268 }
269
Eugene Leviantceabe802016-08-11 07:56:43 +0000270 if (auto *Cmd = dyn_cast<OutputSectionCommand>(Base1.get())) {
Rafael Espindola7bd37872016-09-12 16:05:16 +0000271 std::vector<InputSectionBase<ELFT> *> V = createInputSectionList(*Cmd);
272
Rui Ueyama48c3f1c2016-08-12 00:27:23 +0000273 if (Cmd->Name == "/DISCARD/") {
Rafael Espindola7bd37872016-09-12 16:05:16 +0000274 discard(V);
Rui Ueyama48c3f1c2016-08-12 00:27:23 +0000275 continue;
276 }
Eugene Leviantceabe802016-08-11 07:56:43 +0000277
Eugene Leviant97403d12016-09-01 09:55:57 +0000278 if (V.empty())
Rui Ueyama0b9ce6a2016-08-12 03:16:56 +0000279 continue;
280
George Rimardb24d9c2016-08-19 15:18:23 +0000281 for (InputSectionBase<ELFT> *Sec : V) {
Rafael Espindola28c15972016-09-13 13:00:06 +0000282 OutputSectionBase<ELFT> *OutSec = AddSec(Sec, Cmd->Name);
George Rimara14b13d2016-09-07 10:46:07 +0000283 uint32_t Subalign = Cmd->SubalignExpr ? Cmd->SubalignExpr(0) : 0;
284
George Rimardb24d9c2016-08-19 15:18:23 +0000285 if (Subalign)
286 Sec->Alignment = Subalign;
Eugene Leviant97403d12016-09-01 09:55:57 +0000287 OutSec->addSection(Sec);
George Rimardb24d9c2016-08-19 15:18:23 +0000288 }
Eugene Leviantceabe802016-08-11 07:56:43 +0000289 }
Rui Ueyama48c3f1c2016-08-12 00:27:23 +0000290 }
Eugene Leviante63d81b2016-07-20 14:43:20 +0000291
Rui Ueyama0b9ce6a2016-08-12 03:16:56 +0000292 // Add orphan sections.
Rui Ueyama38dbd3e2016-09-14 00:05:51 +0000293 for (ObjectFile<ELFT> *F : Symtab<ELFT>::X->getObjectFiles()) {
Rui Ueyama0b9ce6a2016-08-12 03:16:56 +0000294 for (InputSectionBase<ELFT> *S : F->getSections()) {
Rui Ueyama2ab5f732016-08-12 03:33:04 +0000295 if (isDiscarded(S) || S->OutSec)
296 continue;
Rafael Espindola28c15972016-09-13 13:00:06 +0000297 OutputSectionBase<ELFT> *OutSec = AddSec(S, getOutputSectionName(S));
Rui Ueyama2ab5f732016-08-12 03:33:04 +0000298 OutSec->addSection(S);
Rui Ueyama0b9ce6a2016-08-12 03:16:56 +0000299 }
300 }
Eugene Leviante63d81b2016-07-20 14:43:20 +0000301}
302
Eugene Leviantdb741e72016-09-07 07:08:43 +0000303// Sets value of a section-defined symbol. Two kinds of
304// symbols are processed: synthetic symbols, whose value
305// is an offset from beginning of section and regular
306// symbols whose value is absolute.
307template <class ELFT>
308static void assignSectionSymbol(SymbolAssignment *Cmd,
309 OutputSectionBase<ELFT> *Sec,
310 typename ELFT::uint Off) {
311 if (!Cmd->Sym)
312 return;
313
314 if (auto *Body = dyn_cast<DefinedSynthetic<ELFT>>(Cmd->Sym)) {
315 Body->Section = Sec;
316 Body->Value = Cmd->Expression(Sec->getVA() + Off) - Sec->getVA();
317 return;
318 }
319 auto *Body = cast<DefinedRegular<ELFT>>(Cmd->Sym);
320 Body->Value = Cmd->Expression(Sec->getVA() + Off);
321}
322
Eugene Leviant20889c52016-08-31 08:13:33 +0000323// Linker script may define start and end symbols for special section types,
324// like .got, .eh_frame_hdr, .eh_frame and others. Those sections are not a list
325// of regular input input sections, therefore our way of defining symbols for
326// regular sections will not work. The approach we use for special section types
327// is not perfect - it handles only start and end symbols.
328template <class ELFT>
329void addStartEndSymbols(OutputSectionCommand *Cmd,
330 OutputSectionBase<ELFT> *Sec) {
331 bool Start = true;
332 BaseCommand *PrevCmd = nullptr;
333
334 for (std::unique_ptr<BaseCommand> &Base : Cmd->Commands) {
335 if (auto *AssignCmd = dyn_cast<SymbolAssignment>(Base.get())) {
Eugene Leviantdb741e72016-09-07 07:08:43 +0000336 assignSectionSymbol<ELFT>(AssignCmd, Sec, Start ? 0 : Sec->getSize());
Eugene Leviant20889c52016-08-31 08:13:33 +0000337 } else {
338 if (!Start && isa<SymbolAssignment>(PrevCmd))
339 error("section '" + Sec->getName() +
340 "' supports only start and end symbols");
341 Start = false;
342 }
343 PrevCmd = Base.get();
344 }
345}
346
347template <class ELFT>
348void assignOffsets(OutputSectionCommand *Cmd, OutputSectionBase<ELFT> *Sec) {
Eugene Leviantceabe802016-08-11 07:56:43 +0000349 auto *OutSec = dyn_cast<OutputSection<ELFT>>(Sec);
Rui Ueyama2de509c2016-08-12 00:55:08 +0000350 if (!OutSec) {
351 Sec->assignOffsets();
Eugene Leviant20889c52016-08-31 08:13:33 +0000352 // This section is not regular output section. However linker script may
353 // have defined start/end symbols for it. This case is handled below.
354 addStartEndSymbols(Cmd, Sec);
Eugene Leviantceabe802016-08-11 07:56:43 +0000355 return;
Rui Ueyama2de509c2016-08-12 00:55:08 +0000356 }
Eugene Leviantceabe802016-08-11 07:56:43 +0000357 typedef typename ELFT::uint uintX_t;
358 uintX_t Off = 0;
Eugene Leviant97403d12016-09-01 09:55:57 +0000359 auto ItCmd = Cmd->Commands.begin();
Eugene Leviantceabe802016-08-11 07:56:43 +0000360
Eugene Leviant97403d12016-09-01 09:55:57 +0000361 // Assigns values to all symbols following the given
362 // input section 'D' in output section 'Sec'. When symbols
363 // are in the beginning of output section the value of 'D'
364 // is nullptr.
365 auto AssignSuccessors = [&](InputSectionData *D) {
366 for (; ItCmd != Cmd->Commands.end(); ++ItCmd) {
367 auto *AssignCmd = dyn_cast<SymbolAssignment>(ItCmd->get());
368 if (!AssignCmd)
369 continue;
370 if (D != AssignCmd->GoesAfter)
371 break;
372
Eugene Leviant97403d12016-09-01 09:55:57 +0000373 if (AssignCmd->Name == ".") {
374 // Update to location counter means update to section size.
Eugene Leviantdb741e72016-09-07 07:08:43 +0000375 Off = AssignCmd->Expression(Sec->getVA() + Off) - Sec->getVA();
Eugene Leviant97403d12016-09-01 09:55:57 +0000376 Sec->setSize(Off);
377 continue;
378 }
Eugene Leviantdb741e72016-09-07 07:08:43 +0000379 assignSectionSymbol<ELFT>(AssignCmd, Sec, Off);
Eugene Leviantceabe802016-08-11 07:56:43 +0000380 }
Eugene Leviant97403d12016-09-01 09:55:57 +0000381 };
382
383 AssignSuccessors(nullptr);
384 for (InputSection<ELFT> *I : OutSec->Sections) {
385 Off = alignTo(Off, I->Alignment);
386 I->OutSecOff = Off;
387 Off += I->getSize();
Rui Ueyamaf4a30a52016-08-11 21:30:42 +0000388 // Update section size inside for-loop, so that SIZEOF
Eugene Leviantceabe802016-08-11 07:56:43 +0000389 // works correctly in the case below:
390 // .foo { *(.aaa) a = SIZEOF(.foo); *(.bbb) }
391 Sec->setSize(Off);
Eugene Leviant97403d12016-09-01 09:55:57 +0000392 // Add symbols following current input section.
393 AssignSuccessors(I);
Eugene Leviantceabe802016-08-11 07:56:43 +0000394 }
395}
396
George Rimar8f66df92016-08-12 20:38:20 +0000397template <class ELFT>
George Rimara14b13d2016-09-07 10:46:07 +0000398static std::vector<OutputSectionBase<ELFT> *>
399findSections(OutputSectionCommand &Cmd,
400 ArrayRef<OutputSectionBase<ELFT> *> Sections) {
401 std::vector<OutputSectionBase<ELFT> *> Ret;
402 for (OutputSectionBase<ELFT> *Sec : Sections)
403 if (Sec->getName() == Cmd.Name &&
404 checkConstraint(Sec->getFlags(), Cmd.Constraint))
405 Ret.push_back(Sec);
406 return Ret;
George Rimar8f66df92016-08-12 20:38:20 +0000407}
408
Rafael Espindolaa4b41dc2016-08-04 12:13:05 +0000409template <class ELFT> void LinkerScript<ELFT>::assignAddresses() {
George Rimar652852c2016-04-16 10:10:32 +0000410 // Orphan sections are sections present in the input files which
Rui Ueyama7c18c282016-04-18 21:00:40 +0000411 // are not explicitly placed into the output file by the linker script.
412 // We place orphan sections at end of file.
413 // Other linkers places them using some heuristics as described in
George Rimar652852c2016-04-16 10:10:32 +0000414 // https://sourceware.org/binutils/docs/ld/Orphan-Sections.html#Orphan-Sections.
Rui Ueyamae5cc6682016-08-12 00:36:56 +0000415 for (OutputSectionBase<ELFT> *Sec : *OutputSections) {
George Rimar652852c2016-04-16 10:10:32 +0000416 StringRef Name = Sec->getName();
Rui Ueyamac3e2a4b2016-04-21 20:30:00 +0000417 if (getSectionIndex(Name) == INT_MAX)
George Rimar076fe152016-07-21 06:43:01 +0000418 Opt.Commands.push_back(llvm::make_unique<OutputSectionCommand>(Name));
George Rimar652852c2016-04-16 10:10:32 +0000419 }
George Rimar652852c2016-04-16 10:10:32 +0000420
Rui Ueyama7c18c282016-04-18 21:00:40 +0000421 // Assign addresses as instructed by linker script SECTIONS sub-commands.
Rui Ueyama4f7500b2016-08-12 04:00:22 +0000422 Dot = getHeaderSize();
Eugene Leviant467c4d52016-07-01 10:27:36 +0000423 uintX_t MinVA = std::numeric_limits<uintX_t>::max();
George Rimar652852c2016-04-16 10:10:32 +0000424 uintX_t ThreadBssOffset = 0;
George Rimar652852c2016-04-16 10:10:32 +0000425
George Rimar076fe152016-07-21 06:43:01 +0000426 for (const std::unique_ptr<BaseCommand> &Base : Opt.Commands) {
427 if (auto *Cmd = dyn_cast<SymbolAssignment>(Base.get())) {
Rui Ueyama8d083e62016-07-29 05:48:39 +0000428 if (Cmd->Name == ".") {
429 Dot = Cmd->Expression(Dot);
430 } else if (Cmd->Sym) {
431 cast<DefinedRegular<ELFT>>(Cmd->Sym)->Value = Cmd->Expression(Dot);
432 }
George Rimar652852c2016-04-16 10:10:32 +0000433 continue;
434 }
435
George Rimareefa7582016-08-04 09:29:31 +0000436 if (auto *Cmd = dyn_cast<AssertCommand>(Base.get())) {
437 Cmd->Expression(Dot);
438 continue;
439 }
440
George Rimar076fe152016-07-21 06:43:01 +0000441 auto *Cmd = cast<OutputSectionCommand>(Base.get());
George Rimara14b13d2016-09-07 10:46:07 +0000442 for (OutputSectionBase<ELFT> *Sec :
443 findSections<ELFT>(*Cmd, *OutputSections)) {
George Rimar652852c2016-04-16 10:10:32 +0000444
George Rimara14b13d2016-09-07 10:46:07 +0000445 if (Cmd->AddrExpr)
446 Dot = Cmd->AddrExpr(Dot);
George Rimar58e5c4d2016-07-25 08:29:46 +0000447
George Rimara14b13d2016-09-07 10:46:07 +0000448 if ((Sec->getFlags() & SHF_TLS) && Sec->getType() == SHT_NOBITS) {
449 uintX_t TVA = Dot + ThreadBssOffset;
450 TVA = alignTo(TVA, Sec->getAlignment());
451 Sec->setVA(TVA);
452 assignOffsets(Cmd, Sec);
453 ThreadBssOffset = TVA - Dot + Sec->getSize();
454 continue;
455 }
456
457 if (!(Sec->getFlags() & SHF_ALLOC)) {
458 assignOffsets(Cmd, Sec);
459 continue;
460 }
461
462 Dot = alignTo(Dot, Sec->getAlignment());
463 Sec->setVA(Dot);
Eugene Leviant20889c52016-08-31 08:13:33 +0000464 assignOffsets(Cmd, Sec);
George Rimara14b13d2016-09-07 10:46:07 +0000465 MinVA = std::min(MinVA, Dot);
466 Dot += Sec->getSize();
George Rimar652852c2016-04-16 10:10:32 +0000467 }
468 }
Rui Ueyama52c4e172016-07-01 10:42:25 +0000469
Rafael Espindola64c32d62016-07-07 14:28:47 +0000470 // ELF and Program headers need to be right before the first section in
George Rimarb91e7112016-07-19 07:42:07 +0000471 // memory. Set their addresses accordingly.
Eugene Leviant467c4d52016-07-01 10:27:36 +0000472 MinVA = alignDown(MinVA - Out<ELFT>::ElfHeader->getSize() -
473 Out<ELFT>::ProgramHeaders->getSize(),
474 Target->PageSize);
475 Out<ELFT>::ElfHeader->setVA(MinVA);
476 Out<ELFT>::ProgramHeaders->setVA(Out<ELFT>::ElfHeader->getSize() + MinVA);
George Rimar652852c2016-04-16 10:10:32 +0000477}
478
Rui Ueyama464daad2016-08-22 04:55:20 +0000479// Creates program headers as instructed by PHDRS linker script command.
Rui Ueyama07320e42016-04-20 20:13:41 +0000480template <class ELFT>
Rafael Espindolaa4b41dc2016-08-04 12:13:05 +0000481std::vector<PhdrEntry<ELFT>> LinkerScript<ELFT>::createPhdrs() {
Rui Ueyamaedebbdf2016-07-24 23:47:31 +0000482 std::vector<PhdrEntry<ELFT>> Ret;
Eugene Leviantbbe38602016-07-19 09:25:43 +0000483
Rui Ueyama464daad2016-08-22 04:55:20 +0000484 // Process PHDRS and FILEHDR keywords because they are not
485 // real output sections and cannot be added in the following loop.
Eugene Leviantbbe38602016-07-19 09:25:43 +0000486 for (const PhdrsCommand &Cmd : Opt.PhdrsCommands) {
Rui Ueyamaedebbdf2016-07-24 23:47:31 +0000487 Ret.emplace_back(Cmd.Type, Cmd.Flags == UINT_MAX ? PF_R : Cmd.Flags);
488 PhdrEntry<ELFT> &Phdr = Ret.back();
Eugene Leviantbbe38602016-07-19 09:25:43 +0000489
490 if (Cmd.HasFilehdr)
Rui Ueyamaadca2452016-07-23 14:18:48 +0000491 Phdr.add(Out<ELFT>::ElfHeader);
Eugene Leviantbbe38602016-07-19 09:25:43 +0000492 if (Cmd.HasPhdrs)
Rui Ueyamaadca2452016-07-23 14:18:48 +0000493 Phdr.add(Out<ELFT>::ProgramHeaders);
Eugene Leviant56b21c82016-09-09 09:46:16 +0000494
495 if (Cmd.LMAExpr) {
496 Phdr.H.p_paddr = Cmd.LMAExpr(0);
497 Phdr.HasLMA = true;
498 }
Eugene Leviantbbe38602016-07-19 09:25:43 +0000499 }
500
Rui Ueyama464daad2016-08-22 04:55:20 +0000501 // Add output sections to program headers.
Rui Ueyamaedebbdf2016-07-24 23:47:31 +0000502 PhdrEntry<ELFT> *Load = nullptr;
503 uintX_t Flags = PF_R;
Rui Ueyama464daad2016-08-22 04:55:20 +0000504 for (OutputSectionBase<ELFT> *Sec : *OutputSections) {
Eugene Leviantbbe38602016-07-19 09:25:43 +0000505 if (!(Sec->getFlags() & SHF_ALLOC))
506 break;
507
Rui Ueyamaedebbdf2016-07-24 23:47:31 +0000508 std::vector<size_t> PhdrIds = getPhdrIndices(Sec->getName());
Eugene Leviantbbe38602016-07-19 09:25:43 +0000509 if (!PhdrIds.empty()) {
510 // Assign headers specified by linker script
511 for (size_t Id : PhdrIds) {
Rui Ueyamaedebbdf2016-07-24 23:47:31 +0000512 Ret[Id].add(Sec);
Eugene Leviant865bf862016-07-21 10:43:25 +0000513 if (Opt.PhdrsCommands[Id].Flags == UINT_MAX)
Rafael Espindola0b113672016-07-27 14:10:56 +0000514 Ret[Id].H.p_flags |= Sec->getPhdrFlags();
Eugene Leviantbbe38602016-07-19 09:25:43 +0000515 }
516 } else {
517 // If we have no load segment or flags've changed then we want new load
518 // segment.
Rafael Espindola0b113672016-07-27 14:10:56 +0000519 uintX_t NewFlags = Sec->getPhdrFlags();
Eugene Leviantbbe38602016-07-19 09:25:43 +0000520 if (Load == nullptr || Flags != NewFlags) {
Rui Ueyamaedebbdf2016-07-24 23:47:31 +0000521 Load = &*Ret.emplace(Ret.end(), PT_LOAD, NewFlags);
Eugene Leviantbbe38602016-07-19 09:25:43 +0000522 Flags = NewFlags;
523 }
Rui Ueyama18f084f2016-07-20 19:36:41 +0000524 Load->add(Sec);
Eugene Leviantbbe38602016-07-19 09:25:43 +0000525 }
Eugene Leviantbbe38602016-07-19 09:25:43 +0000526 }
Rui Ueyamaedebbdf2016-07-24 23:47:31 +0000527 return Ret;
Eugene Leviantbbe38602016-07-19 09:25:43 +0000528}
529
Eugene Leviantf9bc3bd2016-08-16 06:40:58 +0000530template <class ELFT> bool LinkerScript<ELFT>::ignoreInterpSection() {
531 // Ignore .interp section in case we have PHDRS specification
532 // and PT_INTERP isn't listed.
533 return !Opt.PhdrsCommands.empty() &&
534 llvm::find_if(Opt.PhdrsCommands, [](const PhdrsCommand &Cmd) {
535 return Cmd.Type == PT_INTERP;
536 }) == Opt.PhdrsCommands.end();
537}
538
Eugene Leviantbbe38602016-07-19 09:25:43 +0000539template <class ELFT>
Rui Ueyama07320e42016-04-20 20:13:41 +0000540ArrayRef<uint8_t> LinkerScript<ELFT>::getFiller(StringRef Name) {
George Rimarf6c3cce2016-07-21 07:48:54 +0000541 for (const std::unique_ptr<BaseCommand> &Base : Opt.Commands)
542 if (auto *Cmd = dyn_cast<OutputSectionCommand>(Base.get()))
543 if (Cmd->Name == Name)
544 return Cmd->Filler;
545 return {};
George Rimare2ee72b2016-02-26 14:48:31 +0000546}
547
George Rimar206fffa2016-08-17 08:16:57 +0000548template <class ELFT> Expr LinkerScript<ELFT>::getLma(StringRef Name) {
George Rimar8ceadb32016-08-17 07:44:19 +0000549 for (const std::unique_ptr<BaseCommand> &Base : Opt.Commands)
550 if (auto *Cmd = dyn_cast<OutputSectionCommand>(Base.get()))
551 if (Cmd->LmaExpr && Cmd->Name == Name)
552 return Cmd->LmaExpr;
553 return {};
554}
555
Rui Ueyamac3e2a4b2016-04-21 20:30:00 +0000556// Returns the index of the given section name in linker script
557// SECTIONS commands. Sections are laid out as the same order as they
558// were in the script. If a given name did not appear in the script,
559// it returns INT_MAX, so that it will be laid out at end of file.
George Rimar076fe152016-07-21 06:43:01 +0000560template <class ELFT> int LinkerScript<ELFT>::getSectionIndex(StringRef Name) {
Rui Ueyamaf510fa62016-07-26 00:21:15 +0000561 int I = 0;
562 for (std::unique_ptr<BaseCommand> &Base : Opt.Commands) {
563 if (auto *Cmd = dyn_cast<OutputSectionCommand>(Base.get()))
564 if (Cmd->Name == Name)
565 return I;
566 ++I;
567 }
568 return INT_MAX;
George Rimar71b26e92016-04-21 10:22:02 +0000569}
570
571// A compartor to sort output sections. Returns -1 or 1 if
572// A or B are mentioned in linker script. Otherwise, returns 0.
Rui Ueyama07320e42016-04-20 20:13:41 +0000573template <class ELFT>
574int LinkerScript<ELFT>::compareSections(StringRef A, StringRef B) {
Rui Ueyamac3e2a4b2016-04-21 20:30:00 +0000575 int I = getSectionIndex(A);
576 int J = getSectionIndex(B);
577 if (I == INT_MAX && J == INT_MAX)
Rui Ueyama717677a2016-02-11 21:17:59 +0000578 return 0;
579 return I < J ? -1 : 1;
580}
581
Eugene Leviantbbe38602016-07-19 09:25:43 +0000582template <class ELFT> bool LinkerScript<ELFT>::hasPhdrsCommands() {
583 return !Opt.PhdrsCommands.empty();
584}
585
George Rimar9e694502016-07-29 16:18:47 +0000586template <class ELFT>
George Rimar884e7862016-09-08 08:19:13 +0000587uint64_t LinkerScript<ELFT>::getOutputSectionAddress(StringRef Name) {
George Rimar96659df2016-08-30 09:54:01 +0000588 for (OutputSectionBase<ELFT> *Sec : *OutputSections)
589 if (Sec->getName() == Name)
590 return Sec->getVA();
591 error("undefined section " + Name);
592 return 0;
593}
594
595template <class ELFT>
George Rimar884e7862016-09-08 08:19:13 +0000596uint64_t LinkerScript<ELFT>::getOutputSectionSize(StringRef Name) {
George Rimar9e694502016-07-29 16:18:47 +0000597 for (OutputSectionBase<ELFT> *Sec : *OutputSections)
598 if (Sec->getName() == Name)
599 return Sec->getSize();
600 error("undefined section " + Name);
601 return 0;
602}
603
Eugene Leviant36fac7f2016-09-08 09:08:30 +0000604template <class ELFT>
605uint64_t LinkerScript<ELFT>::getOutputSectionAlign(StringRef Name) {
606 for (OutputSectionBase<ELFT> *Sec : *OutputSections)
607 if (Sec->getName() == Name)
608 return Sec->getAlignment();
609 error("undefined section " + Name);
610 return 0;
611}
612
George Rimar884e7862016-09-08 08:19:13 +0000613template <class ELFT> uint64_t LinkerScript<ELFT>::getHeaderSize() {
George Rimare32a3592016-08-10 07:59:34 +0000614 return Out<ELFT>::ElfHeader->getSize() + Out<ELFT>::ProgramHeaders->getSize();
615}
616
George Rimar884e7862016-09-08 08:19:13 +0000617template <class ELFT> uint64_t LinkerScript<ELFT>::getSymbolValue(StringRef S) {
618 if (SymbolBody *B = Symtab<ELFT>::X->find(S))
619 return B->getVA<ELFT>();
620 error("symbol not found: " + S);
621 return 0;
622}
623
Eugene Leviantbbe38602016-07-19 09:25:43 +0000624// Returns indices of ELF headers containing specific section, identified
625// by Name. Each index is a zero based number of ELF header listed within
626// PHDRS {} script block.
627template <class ELFT>
Rui Ueyamaedebbdf2016-07-24 23:47:31 +0000628std::vector<size_t> LinkerScript<ELFT>::getPhdrIndices(StringRef SectionName) {
George Rimar076fe152016-07-21 06:43:01 +0000629 for (const std::unique_ptr<BaseCommand> &Base : Opt.Commands) {
630 auto *Cmd = dyn_cast<OutputSectionCommand>(Base.get());
Rui Ueyamaedebbdf2016-07-24 23:47:31 +0000631 if (!Cmd || Cmd->Name != SectionName)
George Rimar31d842f2016-07-20 16:43:03 +0000632 continue;
633
Rui Ueyama29c5a2a2016-07-26 00:27:36 +0000634 std::vector<size_t> Ret;
635 for (StringRef PhdrName : Cmd->Phdrs)
636 Ret.push_back(getPhdrIndex(PhdrName));
637 return Ret;
Eugene Leviantbbe38602016-07-19 09:25:43 +0000638 }
George Rimar31d842f2016-07-20 16:43:03 +0000639 return {};
Eugene Leviantbbe38602016-07-19 09:25:43 +0000640}
641
Rui Ueyama29c5a2a2016-07-26 00:27:36 +0000642template <class ELFT>
643size_t LinkerScript<ELFT>::getPhdrIndex(StringRef PhdrName) {
644 size_t I = 0;
645 for (PhdrsCommand &Cmd : Opt.PhdrsCommands) {
646 if (Cmd.Name == PhdrName)
647 return I;
648 ++I;
649 }
650 error("section header '" + PhdrName + "' is not listed in PHDRS");
651 return 0;
652}
653
Rui Ueyama07320e42016-04-20 20:13:41 +0000654class elf::ScriptParser : public ScriptParserBase {
George Rimarc3794e52016-02-24 09:21:47 +0000655 typedef void (ScriptParser::*Handler)();
656
Rui Ueyamaf7c5fbb2015-09-30 17:23:26 +0000657public:
Rui Ueyama07320e42016-04-20 20:13:41 +0000658 ScriptParser(StringRef S, bool B) : ScriptParserBase(S), IsUnderSysroot(B) {}
George Rimarf23b2322016-02-19 10:45:45 +0000659
George Rimar20b65982016-08-31 09:08:26 +0000660 void readLinkerScript();
661 void readVersionScript();
Rui Ueyamaf7c5fbb2015-09-30 17:23:26 +0000662
663private:
Rui Ueyama52a15092015-10-11 03:28:42 +0000664 void addFile(StringRef Path);
665
Rui Ueyamaf7c5fbb2015-09-30 17:23:26 +0000666 void readAsNeeded();
Denis Protivensky90c50992015-10-08 06:48:38 +0000667 void readEntry();
George Rimar83f406c2015-10-19 17:35:12 +0000668 void readExtern();
Rui Ueyamaf7c5fbb2015-09-30 17:23:26 +0000669 void readGroup();
Rui Ueyama31aa1f82015-10-11 01:31:55 +0000670 void readInclude();
Rui Ueyamaee592822015-10-07 00:25:09 +0000671 void readOutput();
Davide Italiano9159ce92015-10-12 21:50:08 +0000672 void readOutputArch();
Rui Ueyamaf7c5fbb2015-09-30 17:23:26 +0000673 void readOutputFormat();
Eugene Leviantbbe38602016-07-19 09:25:43 +0000674 void readPhdrs();
Davide Italiano68a39a62015-10-08 17:51:41 +0000675 void readSearchDir();
Denis Protivensky8e3b38a2015-11-12 09:52:08 +0000676 void readSections();
Rui Ueyama95769b42016-08-31 20:03:54 +0000677 void readVersion();
678 void readVersionScriptCommand();
Denis Protivensky8e3b38a2015-11-12 09:52:08 +0000679
Rui Ueyama113cdec2016-07-24 23:05:57 +0000680 SymbolAssignment *readAssignment(StringRef Name);
George Rimarff1f29e2016-09-06 13:51:57 +0000681 std::vector<uint8_t> readFill();
Rui Ueyama10416562016-08-04 02:03:27 +0000682 OutputSectionCommand *readOutputSectionDescription(StringRef OutSec);
George Rimarff1f29e2016-09-06 13:51:57 +0000683 std::vector<uint8_t> readOutputSectionFiller(StringRef Tok);
Eugene Leviantbbe38602016-07-19 09:25:43 +0000684 std::vector<StringRef> readOutputSectionPhdrs();
George Rimara2496cb2016-08-30 09:46:59 +0000685 InputSectionDescription *readInputSectionDescription(StringRef Tok);
George Rimarc91930a2016-09-02 21:17:20 +0000686 Regex readFilePatterns();
George Rimara2496cb2016-08-30 09:46:59 +0000687 InputSectionDescription *readInputSectionRules(StringRef FilePattern);
Eugene Leviantbbe38602016-07-19 09:25:43 +0000688 unsigned readPhdrType();
Rui Ueyama742c3832016-08-04 22:27:00 +0000689 SortKind readSortKind();
Petr Hoseka35e39c2016-08-16 01:11:16 +0000690 SymbolAssignment *readProvideHidden(bool Provide, bool Hidden);
Eugene Leviantdb741e72016-09-07 07:08:43 +0000691 SymbolAssignment *readProvideOrAssignment(StringRef Tok, bool MakeAbsolute);
George Rimar03fc0102016-07-28 07:18:23 +0000692 void readSort();
George Rimareefa7582016-08-04 09:29:31 +0000693 Expr readAssert();
Rui Ueyama708019c2016-07-24 18:19:40 +0000694
695 Expr readExpr();
696 Expr readExpr1(Expr Lhs, int MinPrec);
697 Expr readPrimary();
698 Expr readTernary(Expr Cond);
Rui Ueyama6ad7dfc2016-08-17 18:59:16 +0000699 Expr readParenExpr();
Rui Ueyamaf7c5fbb2015-09-30 17:23:26 +0000700
George Rimar20b65982016-08-31 09:08:26 +0000701 // For parsing version script.
702 void readExtern(std::vector<SymbolVersion> *Globals);
Rui Ueyama95769b42016-08-31 20:03:54 +0000703 void readVersionDeclaration(StringRef VerStr);
George Rimar20b65982016-08-31 09:08:26 +0000704 void readGlobal(StringRef VerStr);
705 void readLocal();
706
Rui Ueyama07320e42016-04-20 20:13:41 +0000707 ScriptConfiguration &Opt = *ScriptConfig;
708 StringSaver Saver = {ScriptConfig->Alloc};
Simon Atanasyan16b0cc92015-11-26 05:53:00 +0000709 bool IsUnderSysroot;
Rui Ueyamaf7c5fbb2015-09-30 17:23:26 +0000710};
Rui Ueyamaf7c5fbb2015-09-30 17:23:26 +0000711
George Rimar20b65982016-08-31 09:08:26 +0000712void ScriptParser::readVersionScript() {
Rui Ueyama95769b42016-08-31 20:03:54 +0000713 readVersionScriptCommand();
714 if (!atEOF())
715 setError("EOF expected, but got " + next());
716}
717
718void ScriptParser::readVersionScriptCommand() {
George Rimar20b65982016-08-31 09:08:26 +0000719 if (skip("{")) {
Rui Ueyama95769b42016-08-31 20:03:54 +0000720 readVersionDeclaration("");
George Rimar20b65982016-08-31 09:08:26 +0000721 return;
722 }
723
Rui Ueyama95769b42016-08-31 20:03:54 +0000724 while (!atEOF() && !Error && peek() != "}") {
George Rimar20b65982016-08-31 09:08:26 +0000725 StringRef VerStr = next();
726 if (VerStr == "{") {
Rui Ueyama95769b42016-08-31 20:03:54 +0000727 setError("anonymous version definition is used in "
728 "combination with other version definitions");
George Rimar20b65982016-08-31 09:08:26 +0000729 return;
730 }
731 expect("{");
Rui Ueyama95769b42016-08-31 20:03:54 +0000732 readVersionDeclaration(VerStr);
George Rimar20b65982016-08-31 09:08:26 +0000733 }
734}
735
Rui Ueyama95769b42016-08-31 20:03:54 +0000736void ScriptParser::readVersion() {
737 expect("{");
738 readVersionScriptCommand();
739 expect("}");
740}
741
George Rimar20b65982016-08-31 09:08:26 +0000742void ScriptParser::readLinkerScript() {
Rui Ueyamaf7c5fbb2015-09-30 17:23:26 +0000743 while (!atEOF()) {
744 StringRef Tok = next();
Rui Ueyamaa27eecc2016-09-02 18:52:41 +0000745 if (Tok == ";")
746 continue;
747
748 if (Tok == "ENTRY") {
749 readEntry();
750 } else if (Tok == "EXTERN") {
751 readExtern();
752 } else if (Tok == "GROUP" || Tok == "INPUT") {
753 readGroup();
754 } else if (Tok == "INCLUDE") {
755 readInclude();
756 } else if (Tok == "OUTPUT") {
757 readOutput();
758 } else if (Tok == "OUTPUT_ARCH") {
759 readOutputArch();
760 } else if (Tok == "OUTPUT_FORMAT") {
761 readOutputFormat();
762 } else if (Tok == "PHDRS") {
763 readPhdrs();
764 } else if (Tok == "SEARCH_DIR") {
765 readSearchDir();
766 } else if (Tok == "SECTIONS") {
767 readSections();
768 } else if (Tok == "VERSION") {
769 readVersion();
Eugene Leviantdb741e72016-09-07 07:08:43 +0000770 } else if (SymbolAssignment *Cmd = readProvideOrAssignment(Tok, true)) {
Eugene Leviante05336ff2016-09-14 08:32:36 +0000771 if (Opt.HasSections)
Petr Hoseke5d3ca52016-08-31 15:31:17 +0000772 Opt.Commands.emplace_back(Cmd);
773 else
774 Opt.Assignments.emplace_back(Cmd);
775 } else {
George Rimar57610422016-03-11 14:43:02 +0000776 setError("unknown directive: " + Tok);
Petr Hoseke5d3ca52016-08-31 15:31:17 +0000777 }
Rui Ueyamaf7c5fbb2015-09-30 17:23:26 +0000778 }
779}
780
Rui Ueyama717677a2016-02-11 21:17:59 +0000781void ScriptParser::addFile(StringRef S) {
Simon Atanasyan16b0cc92015-11-26 05:53:00 +0000782 if (IsUnderSysroot && S.startswith("/")) {
783 SmallString<128> Path;
784 (Config->Sysroot + S).toStringRef(Path);
785 if (sys::fs::exists(Path)) {
786 Driver->addFile(Saver.save(Path.str()));
787 return;
788 }
789 }
790
Rui Ueyamaf03f3cc2015-10-13 00:09:21 +0000791 if (sys::path::is_absolute(S)) {
Rui Ueyama52a15092015-10-11 03:28:42 +0000792 Driver->addFile(S);
793 } else if (S.startswith("=")) {
794 if (Config->Sysroot.empty())
795 Driver->addFile(S.substr(1));
796 else
797 Driver->addFile(Saver.save(Config->Sysroot + "/" + S.substr(1)));
798 } else if (S.startswith("-l")) {
Rui Ueyama21eecb42016-02-02 21:13:09 +0000799 Driver->addLibrary(S.substr(2));
Simon Atanasyana1b8fc32015-11-26 20:23:46 +0000800 } else if (sys::fs::exists(S)) {
801 Driver->addFile(S);
Rui Ueyama52a15092015-10-11 03:28:42 +0000802 } else {
803 std::string Path = findFromSearchPaths(S);
804 if (Path.empty())
George Rimar777f9632016-03-12 08:31:34 +0000805 setError("unable to find " + S);
Rui Ueyama025d59b2016-02-02 20:27:59 +0000806 else
807 Driver->addFile(Saver.save(Path));
Rui Ueyama52a15092015-10-11 03:28:42 +0000808 }
809}
810
Rui Ueyama717677a2016-02-11 21:17:59 +0000811void ScriptParser::readAsNeeded() {
Rui Ueyamaf7c5fbb2015-09-30 17:23:26 +0000812 expect("(");
Rui Ueyama35da9b62015-10-11 20:59:12 +0000813 bool Orig = Config->AsNeeded;
814 Config->AsNeeded = true;
Rui Ueyamaa2acc932016-08-05 01:25:45 +0000815 while (!Error && !skip(")"))
George Rimarcd574a52016-09-09 14:35:36 +0000816 addFile(unquote(next()));
Rui Ueyama35da9b62015-10-11 20:59:12 +0000817 Config->AsNeeded = Orig;
Rui Ueyamaf7c5fbb2015-09-30 17:23:26 +0000818}
819
Rui Ueyama717677a2016-02-11 21:17:59 +0000820void ScriptParser::readEntry() {
Denis Protivensky90c50992015-10-08 06:48:38 +0000821 // -e <symbol> takes predecence over ENTRY(<symbol>).
822 expect("(");
823 StringRef Tok = next();
824 if (Config->Entry.empty())
825 Config->Entry = Tok;
826 expect(")");
827}
828
Rui Ueyama717677a2016-02-11 21:17:59 +0000829void ScriptParser::readExtern() {
George Rimar83f406c2015-10-19 17:35:12 +0000830 expect("(");
Rui Ueyamaa2acc932016-08-05 01:25:45 +0000831 while (!Error && !skip(")"))
832 Config->Undefined.push_back(next());
George Rimar83f406c2015-10-19 17:35:12 +0000833}
834
Rui Ueyama717677a2016-02-11 21:17:59 +0000835void ScriptParser::readGroup() {
Rui Ueyamaf7c5fbb2015-09-30 17:23:26 +0000836 expect("(");
Rui Ueyamaa2acc932016-08-05 01:25:45 +0000837 while (!Error && !skip(")")) {
Rui Ueyamaf7c5fbb2015-09-30 17:23:26 +0000838 StringRef Tok = next();
Rui Ueyamaa2acc932016-08-05 01:25:45 +0000839 if (Tok == "AS_NEEDED")
Rui Ueyamaf7c5fbb2015-09-30 17:23:26 +0000840 readAsNeeded();
Rui Ueyamaa2acc932016-08-05 01:25:45 +0000841 else
George Rimarcd574a52016-09-09 14:35:36 +0000842 addFile(unquote(Tok));
Rui Ueyamaf7c5fbb2015-09-30 17:23:26 +0000843 }
844}
845
Rui Ueyama717677a2016-02-11 21:17:59 +0000846void ScriptParser::readInclude() {
Rui Ueyama31aa1f82015-10-11 01:31:55 +0000847 StringRef Tok = next();
George Rimarcd574a52016-09-09 14:35:36 +0000848 auto MBOrErr = MemoryBuffer::getFile(unquote(Tok));
Rui Ueyama025d59b2016-02-02 20:27:59 +0000849 if (!MBOrErr) {
George Rimar57610422016-03-11 14:43:02 +0000850 setError("cannot open " + Tok);
Rui Ueyama025d59b2016-02-02 20:27:59 +0000851 return;
852 }
Rui Ueyama31aa1f82015-10-11 01:31:55 +0000853 std::unique_ptr<MemoryBuffer> &MB = *MBOrErr;
Rui Ueyamaa47ee682015-10-11 01:53:04 +0000854 StringRef S = Saver.save(MB->getMemBufferRef().getBuffer());
855 std::vector<StringRef> V = tokenize(S);
Rui Ueyama31aa1f82015-10-11 01:31:55 +0000856 Tokens.insert(Tokens.begin() + Pos, V.begin(), V.end());
Rui Ueyama31aa1f82015-10-11 01:31:55 +0000857}
858
Rui Ueyama717677a2016-02-11 21:17:59 +0000859void ScriptParser::readOutput() {
Rui Ueyamaee592822015-10-07 00:25:09 +0000860 // -o <file> takes predecence over OUTPUT(<file>).
861 expect("(");
862 StringRef Tok = next();
863 if (Config->OutputFile.empty())
George Rimarcd574a52016-09-09 14:35:36 +0000864 Config->OutputFile = unquote(Tok);
Rui Ueyamaee592822015-10-07 00:25:09 +0000865 expect(")");
866}
867
Rui Ueyama717677a2016-02-11 21:17:59 +0000868void ScriptParser::readOutputArch() {
Davide Italiano9159ce92015-10-12 21:50:08 +0000869 // Error checking only for now.
870 expect("(");
871 next();
872 expect(")");
873}
874
Rui Ueyama717677a2016-02-11 21:17:59 +0000875void ScriptParser::readOutputFormat() {
Rui Ueyamaf7c5fbb2015-09-30 17:23:26 +0000876 // Error checking only for now.
877 expect("(");
878 next();
Davide Italiano6836c612015-10-12 21:08:41 +0000879 StringRef Tok = next();
880 if (Tok == ")")
George Rimar6c55f0e2016-09-08 08:20:30 +0000881 return;
Rui Ueyama025d59b2016-02-02 20:27:59 +0000882 if (Tok != ",") {
George Rimar57610422016-03-11 14:43:02 +0000883 setError("unexpected token: " + Tok);
Rui Ueyama025d59b2016-02-02 20:27:59 +0000884 return;
885 }
Davide Italiano6836c612015-10-12 21:08:41 +0000886 next();
887 expect(",");
888 next();
Rui Ueyamaf7c5fbb2015-09-30 17:23:26 +0000889 expect(")");
890}
891
Eugene Leviantbbe38602016-07-19 09:25:43 +0000892void ScriptParser::readPhdrs() {
893 expect("{");
894 while (!Error && !skip("}")) {
895 StringRef Tok = next();
Eugene Leviant56b21c82016-09-09 09:46:16 +0000896 Opt.PhdrsCommands.push_back(
897 {Tok, PT_NULL, false, false, UINT_MAX, nullptr});
Eugene Leviantbbe38602016-07-19 09:25:43 +0000898 PhdrsCommand &PhdrCmd = Opt.PhdrsCommands.back();
899
900 PhdrCmd.Type = readPhdrType();
901 do {
902 Tok = next();
903 if (Tok == ";")
904 break;
905 if (Tok == "FILEHDR")
906 PhdrCmd.HasFilehdr = true;
907 else if (Tok == "PHDRS")
908 PhdrCmd.HasPhdrs = true;
Eugene Leviant56b21c82016-09-09 09:46:16 +0000909 else if (Tok == "AT")
910 PhdrCmd.LMAExpr = readParenExpr();
Eugene Leviant865bf862016-07-21 10:43:25 +0000911 else if (Tok == "FLAGS") {
912 expect("(");
Rafael Espindolaeb685cd2016-08-02 22:14:57 +0000913 // Passing 0 for the value of dot is a bit of a hack. It means that
914 // we accept expressions like ".|1".
915 PhdrCmd.Flags = readExpr()(0);
Eugene Leviant865bf862016-07-21 10:43:25 +0000916 expect(")");
917 } else
Eugene Leviantbbe38602016-07-19 09:25:43 +0000918 setError("unexpected header attribute: " + Tok);
919 } while (!Error);
920 }
921}
922
Rui Ueyama717677a2016-02-11 21:17:59 +0000923void ScriptParser::readSearchDir() {
Davide Italiano68a39a62015-10-08 17:51:41 +0000924 expect("(");
Rui Ueyama86c5fb82016-09-08 23:26:54 +0000925 StringRef Tok = next();
Rui Ueyama6c7ad132016-09-02 19:20:33 +0000926 if (!Config->Nostdlib)
George Rimarcd574a52016-09-09 14:35:36 +0000927 Config->SearchPaths.push_back(unquote(Tok));
Davide Italiano68a39a62015-10-08 17:51:41 +0000928 expect(")");
929}
930
Rui Ueyama717677a2016-02-11 21:17:59 +0000931void ScriptParser::readSections() {
Eugene Leviante05336ff2016-09-14 08:32:36 +0000932 Opt.HasSections = true;
Denis Protivensky8e3b38a2015-11-12 09:52:08 +0000933 expect("{");
George Rimar652852c2016-04-16 10:10:32 +0000934 while (!Error && !skip("}")) {
Rui Ueyama113cdec2016-07-24 23:05:57 +0000935 StringRef Tok = next();
Eugene Leviantdb741e72016-09-07 07:08:43 +0000936 BaseCommand *Cmd = readProvideOrAssignment(Tok, true);
Eugene Leviantceabe802016-08-11 07:56:43 +0000937 if (!Cmd) {
938 if (Tok == "ASSERT")
939 Cmd = new AssertCommand(readAssert());
940 else
941 Cmd = readOutputSectionDescription(Tok);
Rui Ueyama708019c2016-07-24 18:19:40 +0000942 }
Rui Ueyama10416562016-08-04 02:03:27 +0000943 Opt.Commands.emplace_back(Cmd);
George Rimar652852c2016-04-16 10:10:32 +0000944 }
Denis Protivensky8e3b38a2015-11-12 09:52:08 +0000945}
946
Rui Ueyama708019c2016-07-24 18:19:40 +0000947static int precedence(StringRef Op) {
948 return StringSwitch<int>(Op)
949 .Case("*", 4)
950 .Case("/", 4)
951 .Case("+", 3)
952 .Case("-", 3)
953 .Case("<", 2)
954 .Case(">", 2)
955 .Case(">=", 2)
956 .Case("<=", 2)
957 .Case("==", 2)
958 .Case("!=", 2)
959 .Case("&", 1)
Rafael Espindolacc3dd622016-08-22 21:33:35 +0000960 .Case("|", 1)
Rui Ueyama708019c2016-07-24 18:19:40 +0000961 .Default(-1);
962}
963
George Rimarc91930a2016-09-02 21:17:20 +0000964Regex ScriptParser::readFilePatterns() {
Rui Ueyama10416562016-08-04 02:03:27 +0000965 std::vector<StringRef> V;
966 while (!Error && !skip(")"))
967 V.push_back(next());
George Rimarc91930a2016-09-02 21:17:20 +0000968 return compileGlobPatterns(V);
George Rimar0702c4e2016-07-29 15:32:46 +0000969}
970
Rui Ueyama742c3832016-08-04 22:27:00 +0000971SortKind ScriptParser::readSortKind() {
972 if (skip("SORT") || skip("SORT_BY_NAME"))
973 return SortByName;
974 if (skip("SORT_BY_ALIGNMENT"))
975 return SortByAlignment;
George Rimar575208c2016-09-15 19:15:12 +0000976 if (skip("SORT_BY_INIT_PRIORITY"))
977 return SortByPriority;
Rui Ueyama742c3832016-08-04 22:27:00 +0000978 return SortNone;
979}
980
George Rimara2496cb2016-08-30 09:46:59 +0000981InputSectionDescription *
982ScriptParser::readInputSectionRules(StringRef FilePattern) {
George Rimarc91930a2016-09-02 21:17:20 +0000983 auto *Cmd = new InputSectionDescription(FilePattern);
Davide Italiano0ed42b02016-07-25 21:47:13 +0000984 expect("(");
Davide Italianoe7282792016-07-27 01:44:01 +0000985
Rui Ueyama742c3832016-08-04 22:27:00 +0000986 // Read EXCLUDE_FILE().
Davide Italianoe7282792016-07-27 01:44:01 +0000987 if (skip("EXCLUDE_FILE")) {
988 expect("(");
George Rimarc91930a2016-09-02 21:17:20 +0000989 Cmd->ExcludedFileRe = readFilePatterns();
Davide Italiano0ed42b02016-07-25 21:47:13 +0000990 }
George Rimar06598002016-07-28 21:51:30 +0000991
Rui Ueyama742c3832016-08-04 22:27:00 +0000992 // Read SORT().
993 if (SortKind K1 = readSortKind()) {
994 Cmd->SortOuter = K1;
George Rimar0702c4e2016-07-29 15:32:46 +0000995 expect("(");
Rui Ueyama742c3832016-08-04 22:27:00 +0000996 if (SortKind K2 = readSortKind()) {
997 Cmd->SortInner = K2;
George Rimar350ece42016-08-03 08:35:59 +0000998 expect("(");
George Rimarc91930a2016-09-02 21:17:20 +0000999 Cmd->SectionRe = readFilePatterns();
George Rimar350ece42016-08-03 08:35:59 +00001000 expect(")");
1001 } else {
George Rimarc91930a2016-09-02 21:17:20 +00001002 Cmd->SectionRe = readFilePatterns();
George Rimar350ece42016-08-03 08:35:59 +00001003 }
George Rimar0702c4e2016-07-29 15:32:46 +00001004 expect(")");
Rui Ueyama10416562016-08-04 02:03:27 +00001005 return Cmd;
George Rimar06598002016-07-28 21:51:30 +00001006 }
George Rimar0702c4e2016-07-29 15:32:46 +00001007
George Rimarc91930a2016-09-02 21:17:20 +00001008 Cmd->SectionRe = readFilePatterns();
Rui Ueyama10416562016-08-04 02:03:27 +00001009 return Cmd;
Davide Italianoe7282792016-07-27 01:44:01 +00001010}
1011
George Rimara2496cb2016-08-30 09:46:59 +00001012InputSectionDescription *
1013ScriptParser::readInputSectionDescription(StringRef Tok) {
George Rimar06598002016-07-28 21:51:30 +00001014 // Input section wildcard can be surrounded by KEEP.
1015 // https://sourceware.org/binutils/docs/ld/Input-Section-Keep.html#Input-Section-Keep
George Rimara2496cb2016-08-30 09:46:59 +00001016 if (Tok == "KEEP") {
George Rimar06598002016-07-28 21:51:30 +00001017 expect("(");
George Rimara2496cb2016-08-30 09:46:59 +00001018 StringRef FilePattern = next();
1019 InputSectionDescription *Cmd = readInputSectionRules(FilePattern);
George Rimar06598002016-07-28 21:51:30 +00001020 expect(")");
George Rimarc91930a2016-09-02 21:17:20 +00001021 Opt.KeptSections.push_back(&Cmd->SectionRe);
Rui Ueyama10416562016-08-04 02:03:27 +00001022 return Cmd;
George Rimar06598002016-07-28 21:51:30 +00001023 }
George Rimara2496cb2016-08-30 09:46:59 +00001024 return readInputSectionRules(Tok);
Davide Italiano0ed42b02016-07-25 21:47:13 +00001025}
1026
George Rimar03fc0102016-07-28 07:18:23 +00001027void ScriptParser::readSort() {
1028 expect("(");
1029 expect("CONSTRUCTORS");
1030 expect(")");
1031}
1032
George Rimareefa7582016-08-04 09:29:31 +00001033Expr ScriptParser::readAssert() {
1034 expect("(");
1035 Expr E = readExpr();
1036 expect(",");
George Rimarcd574a52016-09-09 14:35:36 +00001037 StringRef Msg = unquote(next());
George Rimareefa7582016-08-04 09:29:31 +00001038 expect(")");
1039 return [=](uint64_t Dot) {
1040 uint64_t V = E(Dot);
1041 if (!V)
1042 error(Msg);
1043 return V;
1044 };
1045}
1046
Rui Ueyama25150e82016-09-06 17:46:43 +00001047// Reads a FILL(expr) command. We handle the FILL command as an
1048// alias for =fillexp section attribute, which is different from
1049// what GNU linkers do.
1050// https://sourceware.org/binutils/docs/ld/Output-Section-Data.html
George Rimarff1f29e2016-09-06 13:51:57 +00001051std::vector<uint8_t> ScriptParser::readFill() {
1052 expect("(");
1053 std::vector<uint8_t> V = readOutputSectionFiller(next());
1054 expect(")");
1055 expect(";");
1056 return V;
1057}
1058
Rui Ueyama10416562016-08-04 02:03:27 +00001059OutputSectionCommand *
1060ScriptParser::readOutputSectionDescription(StringRef OutSec) {
George Rimar076fe152016-07-21 06:43:01 +00001061 OutputSectionCommand *Cmd = new OutputSectionCommand(OutSec);
George Rimar58e5c4d2016-07-25 08:29:46 +00001062
1063 // Read an address expression.
1064 // https://sourceware.org/binutils/docs/ld/Output-Section-Address.html#Output-Section-Address
1065 if (peek() != ":")
1066 Cmd->AddrExpr = readExpr();
1067
Denis Protivensky8e3b38a2015-11-12 09:52:08 +00001068 expect(":");
Davide Italiano246f6812016-07-22 03:36:24 +00001069
George Rimar8ceadb32016-08-17 07:44:19 +00001070 if (skip("AT"))
Rui Ueyama6ad7dfc2016-08-17 18:59:16 +00001071 Cmd->LmaExpr = readParenExpr();
George Rimar630c6172016-07-26 18:06:29 +00001072 if (skip("ALIGN"))
Rui Ueyama6ad7dfc2016-08-17 18:59:16 +00001073 Cmd->AlignExpr = readParenExpr();
George Rimardb24d9c2016-08-19 15:18:23 +00001074 if (skip("SUBALIGN"))
1075 Cmd->SubalignExpr = readParenExpr();
George Rimar630c6172016-07-26 18:06:29 +00001076
Davide Italiano246f6812016-07-22 03:36:24 +00001077 // Parse constraints.
1078 if (skip("ONLY_IF_RO"))
Rui Ueyamaefc40662016-07-25 22:00:10 +00001079 Cmd->Constraint = ConstraintKind::ReadOnly;
Davide Italiano246f6812016-07-22 03:36:24 +00001080 if (skip("ONLY_IF_RW"))
Rui Ueyamaefc40662016-07-25 22:00:10 +00001081 Cmd->Constraint = ConstraintKind::ReadWrite;
Denis Protivensky8e3b38a2015-11-12 09:52:08 +00001082 expect("{");
Rui Ueyama8ec77e62016-04-21 22:00:51 +00001083
Rui Ueyama025d59b2016-02-02 20:27:59 +00001084 while (!Error && !skip("}")) {
Eugene Leviantceabe802016-08-11 07:56:43 +00001085 StringRef Tok = next();
Eugene Leviantdb741e72016-09-07 07:08:43 +00001086 if (SymbolAssignment *Assignment = readProvideOrAssignment(Tok, false))
Eugene Leviantceabe802016-08-11 07:56:43 +00001087 Cmd->Commands.emplace_back(Assignment);
George Rimarff1f29e2016-09-06 13:51:57 +00001088 else if (Tok == "FILL")
1089 Cmd->Filler = readFill();
Eugene Leviantceabe802016-08-11 07:56:43 +00001090 else if (Tok == "SORT")
George Rimar03fc0102016-07-28 07:18:23 +00001091 readSort();
George Rimara2496cb2016-08-30 09:46:59 +00001092 else if (peek() == "(")
1093 Cmd->Commands.emplace_back(readInputSectionDescription(Tok));
Eugene Leviantceabe802016-08-11 07:56:43 +00001094 else
1095 setError("unknown command " + Tok);
Denis Protivensky8e3b38a2015-11-12 09:52:08 +00001096 }
George Rimar076fe152016-07-21 06:43:01 +00001097 Cmd->Phdrs = readOutputSectionPhdrs();
George Rimarff1f29e2016-09-06 13:51:57 +00001098 if (peek().startswith("="))
1099 Cmd->Filler = readOutputSectionFiller(next().drop_front());
Rui Ueyama10416562016-08-04 02:03:27 +00001100 return Cmd;
Rui Ueyamaf71caa22016-07-29 06:14:07 +00001101}
Rui Ueyama8ec77e62016-04-21 22:00:51 +00001102
Rui Ueyama2c8f1f02016-08-29 22:01:21 +00001103// Read "=<number>" where <number> is an octal/decimal/hexadecimal number.
1104// https://sourceware.org/binutils/docs/ld/Output-Section-Fill.html
1105//
1106// ld.gold is not fully compatible with ld.bfd. ld.bfd handles
1107// hexstrings as blobs of arbitrary sizes, while ld.gold handles them
1108// as 32-bit big-endian values. We will do the same as ld.gold does
1109// because it's simpler than what ld.bfd does.
George Rimarff1f29e2016-09-06 13:51:57 +00001110std::vector<uint8_t> ScriptParser::readOutputSectionFiller(StringRef Tok) {
Rui Ueyama965827d2016-08-03 23:25:15 +00001111 uint32_t V;
George Rimarff1f29e2016-09-06 13:51:57 +00001112 if (Tok.getAsInteger(0, V)) {
Rui Ueyama965827d2016-08-03 23:25:15 +00001113 setError("invalid filler expression: " + Tok);
Rui Ueyamaf71caa22016-07-29 06:14:07 +00001114 return {};
George Rimare2ee72b2016-02-26 14:48:31 +00001115 }
Rui Ueyama2c8f1f02016-08-29 22:01:21 +00001116 return {uint8_t(V >> 24), uint8_t(V >> 16), uint8_t(V >> 8), uint8_t(V)};
Denis Protivensky8e3b38a2015-11-12 09:52:08 +00001117}
1118
Petr Hoseka35e39c2016-08-16 01:11:16 +00001119SymbolAssignment *ScriptParser::readProvideHidden(bool Provide, bool Hidden) {
Eugene Levianta31c91b2016-07-22 07:38:40 +00001120 expect("(");
Rui Ueyama174e0a12016-07-29 00:29:25 +00001121 SymbolAssignment *Cmd = readAssignment(next());
Petr Hoseka35e39c2016-08-16 01:11:16 +00001122 Cmd->Provide = Provide;
Rui Ueyama174e0a12016-07-29 00:29:25 +00001123 Cmd->Hidden = Hidden;
Eugene Levianta31c91b2016-07-22 07:38:40 +00001124 expect(")");
1125 expect(";");
Rui Ueyama10416562016-08-04 02:03:27 +00001126 return Cmd;
Eugene Levianteda81a12016-07-12 06:39:48 +00001127}
1128
Eugene Leviantdb741e72016-09-07 07:08:43 +00001129SymbolAssignment *ScriptParser::readProvideOrAssignment(StringRef Tok,
1130 bool MakeAbsolute) {
Eugene Leviantceabe802016-08-11 07:56:43 +00001131 SymbolAssignment *Cmd = nullptr;
1132 if (peek() == "=" || peek() == "+=") {
1133 Cmd = readAssignment(Tok);
1134 expect(";");
1135 } else if (Tok == "PROVIDE") {
Petr Hoseka35e39c2016-08-16 01:11:16 +00001136 Cmd = readProvideHidden(true, false);
1137 } else if (Tok == "HIDDEN") {
1138 Cmd = readProvideHidden(false, true);
Eugene Leviantceabe802016-08-11 07:56:43 +00001139 } else if (Tok == "PROVIDE_HIDDEN") {
Petr Hoseka35e39c2016-08-16 01:11:16 +00001140 Cmd = readProvideHidden(true, true);
Eugene Leviantceabe802016-08-11 07:56:43 +00001141 }
Eugene Leviantdb741e72016-09-07 07:08:43 +00001142 if (Cmd && MakeAbsolute)
1143 Cmd->IsAbsolute = true;
Eugene Leviantceabe802016-08-11 07:56:43 +00001144 return Cmd;
1145}
1146
George Rimar30835ea2016-07-28 21:08:56 +00001147static uint64_t getSymbolValue(StringRef S, uint64_t Dot) {
1148 if (S == ".")
1149 return Dot;
George Rimar884e7862016-09-08 08:19:13 +00001150 return ScriptBase->getSymbolValue(S);
George Rimare32a3592016-08-10 07:59:34 +00001151}
1152
George Rimar30835ea2016-07-28 21:08:56 +00001153SymbolAssignment *ScriptParser::readAssignment(StringRef Name) {
1154 StringRef Op = next();
Eugene Leviantdb741e72016-09-07 07:08:43 +00001155 bool IsAbsolute = false;
1156 Expr E;
George Rimar30835ea2016-07-28 21:08:56 +00001157 assert(Op == "=" || Op == "+=");
Eugene Leviantdb741e72016-09-07 07:08:43 +00001158 if (skip("ABSOLUTE")) {
1159 E = readParenExpr();
1160 IsAbsolute = true;
1161 } else {
1162 E = readExpr();
1163 }
George Rimar30835ea2016-07-28 21:08:56 +00001164 if (Op == "+=")
1165 E = [=](uint64_t Dot) { return getSymbolValue(Name, Dot) + E(Dot); };
Eugene Leviantdb741e72016-09-07 07:08:43 +00001166 return new SymbolAssignment(Name, E, IsAbsolute);
George Rimar30835ea2016-07-28 21:08:56 +00001167}
1168
1169// This is an operator-precedence parser to parse a linker
1170// script expression.
1171Expr ScriptParser::readExpr() { return readExpr1(readPrimary(), 0); }
1172
Rui Ueyama36c1cd22016-08-05 01:04:59 +00001173static Expr combine(StringRef Op, Expr L, Expr R) {
1174 if (Op == "*")
1175 return [=](uint64_t Dot) { return L(Dot) * R(Dot); };
1176 if (Op == "/") {
1177 return [=](uint64_t Dot) -> uint64_t {
1178 uint64_t RHS = R(Dot);
1179 if (RHS == 0) {
1180 error("division by zero");
1181 return 0;
1182 }
1183 return L(Dot) / RHS;
1184 };
1185 }
1186 if (Op == "+")
1187 return [=](uint64_t Dot) { return L(Dot) + R(Dot); };
1188 if (Op == "-")
1189 return [=](uint64_t Dot) { return L(Dot) - R(Dot); };
1190 if (Op == "<")
1191 return [=](uint64_t Dot) { return L(Dot) < R(Dot); };
1192 if (Op == ">")
1193 return [=](uint64_t Dot) { return L(Dot) > R(Dot); };
1194 if (Op == ">=")
1195 return [=](uint64_t Dot) { return L(Dot) >= R(Dot); };
1196 if (Op == "<=")
1197 return [=](uint64_t Dot) { return L(Dot) <= R(Dot); };
1198 if (Op == "==")
1199 return [=](uint64_t Dot) { return L(Dot) == R(Dot); };
1200 if (Op == "!=")
1201 return [=](uint64_t Dot) { return L(Dot) != R(Dot); };
1202 if (Op == "&")
1203 return [=](uint64_t Dot) { return L(Dot) & R(Dot); };
Rafael Espindolacc3dd622016-08-22 21:33:35 +00001204 if (Op == "|")
1205 return [=](uint64_t Dot) { return L(Dot) | R(Dot); };
Rui Ueyama36c1cd22016-08-05 01:04:59 +00001206 llvm_unreachable("invalid operator");
1207}
1208
Rui Ueyama708019c2016-07-24 18:19:40 +00001209// This is a part of the operator-precedence parser. This function
1210// assumes that the remaining token stream starts with an operator.
1211Expr ScriptParser::readExpr1(Expr Lhs, int MinPrec) {
1212 while (!atEOF() && !Error) {
1213 // Read an operator and an expression.
1214 StringRef Op1 = peek();
1215 if (Op1 == "?")
1216 return readTernary(Lhs);
1217 if (precedence(Op1) < MinPrec)
Eugene Levianteda81a12016-07-12 06:39:48 +00001218 break;
Rui Ueyama708019c2016-07-24 18:19:40 +00001219 next();
1220 Expr Rhs = readPrimary();
1221
1222 // Evaluate the remaining part of the expression first if the
1223 // next operator has greater precedence than the previous one.
1224 // For example, if we have read "+" and "3", and if the next
1225 // operator is "*", then we'll evaluate 3 * ... part first.
1226 while (!atEOF()) {
1227 StringRef Op2 = peek();
1228 if (precedence(Op2) <= precedence(Op1))
1229 break;
1230 Rhs = readExpr1(Rhs, precedence(Op2));
1231 }
1232
1233 Lhs = combine(Op1, Lhs, Rhs);
Eugene Levianteda81a12016-07-12 06:39:48 +00001234 }
Rui Ueyama708019c2016-07-24 18:19:40 +00001235 return Lhs;
1236}
1237
1238uint64_t static getConstant(StringRef S) {
Michael J. Spencere2cc07b2016-08-17 02:10:51 +00001239 if (S == "COMMONPAGESIZE")
Rui Ueyama708019c2016-07-24 18:19:40 +00001240 return Target->PageSize;
Michael J. Spencere2cc07b2016-08-17 02:10:51 +00001241 if (S == "MAXPAGESIZE")
1242 return Target->MaxPageSize;
Rui Ueyama708019c2016-07-24 18:19:40 +00001243 error("unknown constant: " + S);
1244 return 0;
1245}
1246
Rui Ueyama626e0b02016-09-02 18:19:00 +00001247// Parses Tok as an integer. Returns true if successful.
1248// It recognizes hexadecimal (prefixed with "0x" or suffixed with "H")
1249// and decimal numbers. Decimal numbers may have "K" (kilo) or
1250// "M" (mega) prefixes.
George Rimar9f2f7ad2016-09-02 16:01:42 +00001251static bool readInteger(StringRef Tok, uint64_t &Result) {
Simon Atanasyaneaeafb22016-09-02 21:54:35 +00001252 if (Tok.startswith("-")) {
1253 if (!readInteger(Tok.substr(1), Result))
1254 return false;
1255 Result = -Result;
1256 return true;
1257 }
George Rimar9f2f7ad2016-09-02 16:01:42 +00001258 if (Tok.startswith_lower("0x"))
1259 return !Tok.substr(2).getAsInteger(16, Result);
1260 if (Tok.endswith_lower("H"))
1261 return !Tok.drop_back().getAsInteger(16, Result);
1262
1263 int Suffix = 1;
1264 if (Tok.endswith_lower("K")) {
1265 Suffix = 1024;
1266 Tok = Tok.drop_back();
1267 } else if (Tok.endswith_lower("M")) {
1268 Suffix = 1024 * 1024;
1269 Tok = Tok.drop_back();
1270 }
1271 if (Tok.getAsInteger(10, Result))
1272 return false;
1273 Result *= Suffix;
1274 return true;
1275}
1276
Rui Ueyama708019c2016-07-24 18:19:40 +00001277Expr ScriptParser::readPrimary() {
Rui Ueyama6ad7dfc2016-08-17 18:59:16 +00001278 if (peek() == "(")
1279 return readParenExpr();
Rui Ueyama708019c2016-07-24 18:19:40 +00001280
Rui Ueyama6ad7dfc2016-08-17 18:59:16 +00001281 StringRef Tok = next();
Rui Ueyama708019c2016-07-24 18:19:40 +00001282
Simon Atanasyaneaeafb22016-09-02 21:54:35 +00001283 if (Tok == "~") {
1284 Expr E = readPrimary();
1285 return [=](uint64_t Dot) { return ~E(Dot); };
1286 }
1287 if (Tok == "-") {
1288 Expr E = readPrimary();
1289 return [=](uint64_t Dot) { return -E(Dot); };
1290 }
1291
Rui Ueyama708019c2016-07-24 18:19:40 +00001292 // Built-in functions are parsed here.
1293 // https://sourceware.org/binutils/docs/ld/Builtin-Functions.html.
George Rimar96659df2016-08-30 09:54:01 +00001294 if (Tok == "ADDR") {
1295 expect("(");
1296 StringRef Name = next();
1297 expect(")");
George Rimar884e7862016-09-08 08:19:13 +00001298 return
1299 [=](uint64_t Dot) { return ScriptBase->getOutputSectionAddress(Name); };
George Rimar96659df2016-08-30 09:54:01 +00001300 }
George Rimareefa7582016-08-04 09:29:31 +00001301 if (Tok == "ASSERT")
1302 return readAssert();
Rui Ueyama708019c2016-07-24 18:19:40 +00001303 if (Tok == "ALIGN") {
Rui Ueyama6ad7dfc2016-08-17 18:59:16 +00001304 Expr E = readParenExpr();
Rui Ueyama708019c2016-07-24 18:19:40 +00001305 return [=](uint64_t Dot) { return alignTo(Dot, E(Dot)); };
1306 }
1307 if (Tok == "CONSTANT") {
1308 expect("(");
1309 StringRef Tok = next();
1310 expect(")");
1311 return [=](uint64_t Dot) { return getConstant(Tok); };
1312 }
Rafael Espindola54c145c2016-07-28 18:16:24 +00001313 if (Tok == "SEGMENT_START") {
1314 expect("(");
1315 next();
1316 expect(",");
1317 uint64_t Val;
Rafael Espindola3adbbc32016-09-15 13:36:44 +00001318 if (next().getAsInteger(0, Val))
1319 setError("integer expected");
Rafael Espindola54c145c2016-07-28 18:16:24 +00001320 expect(")");
1321 return [=](uint64_t Dot) { return Val; };
1322 }
Rui Ueyama708019c2016-07-24 18:19:40 +00001323 if (Tok == "DATA_SEGMENT_ALIGN") {
1324 expect("(");
1325 Expr E = readExpr();
1326 expect(",");
1327 readExpr();
1328 expect(")");
Rui Ueyamaf7791bb2016-07-26 19:34:10 +00001329 return [=](uint64_t Dot) { return alignTo(Dot, E(Dot)); };
Rui Ueyama708019c2016-07-24 18:19:40 +00001330 }
1331 if (Tok == "DATA_SEGMENT_END") {
1332 expect("(");
1333 expect(".");
1334 expect(")");
1335 return [](uint64_t Dot) { return Dot; };
1336 }
George Rimar276b4e62016-07-26 17:58:44 +00001337 // GNU linkers implements more complicated logic to handle
1338 // DATA_SEGMENT_RELRO_END. We instead ignore the arguments and just align to
1339 // the next page boundary for simplicity.
1340 if (Tok == "DATA_SEGMENT_RELRO_END") {
1341 expect("(");
Rafael Espindola97bdc722016-09-14 19:14:01 +00001342 readExpr();
George Rimar276b4e62016-07-26 17:58:44 +00001343 expect(",");
1344 readExpr();
1345 expect(")");
1346 return [](uint64_t Dot) { return alignTo(Dot, Target->PageSize); };
1347 }
George Rimar9e694502016-07-29 16:18:47 +00001348 if (Tok == "SIZEOF") {
1349 expect("(");
1350 StringRef Name = next();
1351 expect(")");
George Rimar884e7862016-09-08 08:19:13 +00001352 return [=](uint64_t Dot) { return ScriptBase->getOutputSectionSize(Name); };
George Rimar9e694502016-07-29 16:18:47 +00001353 }
Eugene Leviant36fac7f2016-09-08 09:08:30 +00001354 if (Tok == "ALIGNOF") {
1355 expect("(");
1356 StringRef Name = next();
1357 expect(")");
1358 return
1359 [=](uint64_t Dot) { return ScriptBase->getOutputSectionAlign(Name); };
1360 }
George Rimare32a3592016-08-10 07:59:34 +00001361 if (Tok == "SIZEOF_HEADERS")
George Rimar884e7862016-09-08 08:19:13 +00001362 return [=](uint64_t Dot) { return ScriptBase->getHeaderSize(); };
Rui Ueyama708019c2016-07-24 18:19:40 +00001363
George Rimar9f2f7ad2016-09-02 16:01:42 +00001364 // Tok is a literal number.
1365 uint64_t V;
1366 if (readInteger(Tok, V))
1367 return [=](uint64_t Dot) { return V; };
1368
1369 // Tok is a symbol name.
1370 if (Tok != "." && !isValidCIdentifier(Tok))
1371 setError("malformed number: " + Tok);
1372 return [=](uint64_t Dot) { return getSymbolValue(Tok, Dot); };
Rui Ueyama708019c2016-07-24 18:19:40 +00001373}
1374
1375Expr ScriptParser::readTernary(Expr Cond) {
1376 next();
1377 Expr L = readExpr();
1378 expect(":");
1379 Expr R = readExpr();
1380 return [=](uint64_t Dot) { return Cond(Dot) ? L(Dot) : R(Dot); };
1381}
1382
Rui Ueyama6ad7dfc2016-08-17 18:59:16 +00001383Expr ScriptParser::readParenExpr() {
1384 expect("(");
1385 Expr E = readExpr();
1386 expect(")");
1387 return E;
1388}
1389
Eugene Leviantbbe38602016-07-19 09:25:43 +00001390std::vector<StringRef> ScriptParser::readOutputSectionPhdrs() {
1391 std::vector<StringRef> Phdrs;
1392 while (!Error && peek().startswith(":")) {
1393 StringRef Tok = next();
1394 Tok = (Tok.size() == 1) ? next() : Tok.substr(1);
1395 if (Tok.empty()) {
1396 setError("section header name is empty");
1397 break;
1398 }
Rui Ueyama047404f2016-07-20 19:36:36 +00001399 Phdrs.push_back(Tok);
Eugene Leviantbbe38602016-07-19 09:25:43 +00001400 }
1401 return Phdrs;
1402}
1403
1404unsigned ScriptParser::readPhdrType() {
Eugene Leviantbbe38602016-07-19 09:25:43 +00001405 StringRef Tok = next();
Rui Ueyamab0f6c592016-07-20 19:36:38 +00001406 unsigned Ret = StringSwitch<unsigned>(Tok)
George Rimar6c55f0e2016-09-08 08:20:30 +00001407 .Case("PT_NULL", PT_NULL)
1408 .Case("PT_LOAD", PT_LOAD)
1409 .Case("PT_DYNAMIC", PT_DYNAMIC)
1410 .Case("PT_INTERP", PT_INTERP)
1411 .Case("PT_NOTE", PT_NOTE)
1412 .Case("PT_SHLIB", PT_SHLIB)
1413 .Case("PT_PHDR", PT_PHDR)
1414 .Case("PT_TLS", PT_TLS)
1415 .Case("PT_GNU_EH_FRAME", PT_GNU_EH_FRAME)
1416 .Case("PT_GNU_STACK", PT_GNU_STACK)
1417 .Case("PT_GNU_RELRO", PT_GNU_RELRO)
1418 .Default(-1);
Eugene Leviantbbe38602016-07-19 09:25:43 +00001419
Rui Ueyamab0f6c592016-07-20 19:36:38 +00001420 if (Ret == (unsigned)-1) {
1421 setError("invalid program header type: " + Tok);
1422 return PT_NULL;
1423 }
1424 return Ret;
Eugene Leviantbbe38602016-07-19 09:25:43 +00001425}
1426
Rui Ueyama95769b42016-08-31 20:03:54 +00001427void ScriptParser::readVersionDeclaration(StringRef VerStr) {
George Rimar20b65982016-08-31 09:08:26 +00001428 // Identifiers start at 2 because 0 and 1 are reserved
1429 // for VER_NDX_LOCAL and VER_NDX_GLOBAL constants.
1430 size_t VersionId = Config->VersionDefinitions.size() + 2;
1431 Config->VersionDefinitions.push_back({VerStr, VersionId});
1432
1433 if (skip("global:") || peek() != "local:")
1434 readGlobal(VerStr);
1435 if (skip("local:"))
1436 readLocal();
1437 expect("}");
1438
1439 // Each version may have a parent version. For example, "Ver2" defined as
1440 // "Ver2 { global: foo; local: *; } Ver1;" has "Ver1" as a parent. This
1441 // version hierarchy is, probably against your instinct, purely for human; the
1442 // runtime doesn't care about them at all. In LLD, we simply skip the token.
1443 if (!VerStr.empty() && peek() != ";")
1444 next();
1445 expect(";");
1446}
1447
1448void ScriptParser::readLocal() {
1449 Config->DefaultSymbolVersion = VER_NDX_LOCAL;
1450 expect("*");
1451 expect(";");
1452}
1453
1454void ScriptParser::readExtern(std::vector<SymbolVersion> *Globals) {
George Rimarcd574a52016-09-09 14:35:36 +00001455 expect("\"C++\"");
George Rimar20b65982016-08-31 09:08:26 +00001456 expect("{");
1457
1458 for (;;) {
1459 if (peek() == "}" || Error)
1460 break;
George Rimarcd574a52016-09-09 14:35:36 +00001461 bool HasWildcard = !peek().startswith("\"") && hasWildcard(peek());
1462 Globals->push_back({unquote(next()), true, HasWildcard});
George Rimar20b65982016-08-31 09:08:26 +00001463 expect(";");
1464 }
1465
1466 expect("}");
1467 expect(";");
1468}
1469
1470void ScriptParser::readGlobal(StringRef VerStr) {
1471 std::vector<SymbolVersion> *Globals;
1472 if (VerStr.empty())
1473 Globals = &Config->VersionScriptGlobals;
1474 else
1475 Globals = &Config->VersionDefinitions.back().Globals;
1476
1477 for (;;) {
1478 if (skip("extern"))
1479 readExtern(Globals);
1480
1481 StringRef Cur = peek();
1482 if (Cur == "}" || Cur == "local:" || Error)
1483 return;
1484 next();
George Rimarcd574a52016-09-09 14:35:36 +00001485 Globals->push_back({unquote(Cur), false, hasWildcard(Cur)});
George Rimar20b65982016-08-31 09:08:26 +00001486 expect(";");
1487 }
1488}
1489
Simon Atanasyan16b0cc92015-11-26 05:53:00 +00001490static bool isUnderSysroot(StringRef Path) {
1491 if (Config->Sysroot == "")
1492 return false;
1493 for (; !Path.empty(); Path = sys::path::parent_path(Path))
1494 if (sys::fs::equivalent(Config->Sysroot, Path))
1495 return true;
1496 return false;
1497}
1498
Rui Ueyama07320e42016-04-20 20:13:41 +00001499void elf::readLinkerScript(MemoryBufferRef MB) {
Simon Atanasyan16b0cc92015-11-26 05:53:00 +00001500 StringRef Path = MB.getBufferIdentifier();
George Rimar20b65982016-08-31 09:08:26 +00001501 ScriptParser(MB.getBuffer(), isUnderSysroot(Path)).readLinkerScript();
1502}
1503
1504void elf::readVersionScript(MemoryBufferRef MB) {
1505 ScriptParser(MB.getBuffer(), false).readVersionScript();
Rui Ueyamaf7c5fbb2015-09-30 17:23:26 +00001506}
Rui Ueyama1ebc8ed2016-02-12 21:47:28 +00001507
Rui Ueyama07320e42016-04-20 20:13:41 +00001508template class elf::LinkerScript<ELF32LE>;
1509template class elf::LinkerScript<ELF32BE>;
1510template class elf::LinkerScript<ELF64LE>;
1511template class elf::LinkerScript<ELF64BE>;