blob: a700295f0fd1ca1192d596d68a58a8b38d979e15 [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;
George Rimare38cbab2016-09-26 19:22:50 +000041using namespace llvm::support::endian;
Rui Ueyamaf7c5fbb2015-09-30 17:23:26 +000042using namespace lld;
Rafael Espindolae0df00b2016-02-28 00:25:54 +000043using namespace lld::elf;
Rui Ueyamaf7c5fbb2015-09-30 17:23:26 +000044
George Rimar884e7862016-09-08 08:19:13 +000045LinkerScriptBase *elf::ScriptBase;
Rui Ueyama07320e42016-04-20 20:13:41 +000046ScriptConfiguration *elf::ScriptConfig;
Rui Ueyama717677a2016-02-11 21:17:59 +000047
George Rimar6c55f0e2016-09-08 08:20:30 +000048template <class ELFT> static void addRegular(SymbolAssignment *Cmd) {
Rui Ueyama16024212016-08-11 23:22:52 +000049 Symbol *Sym = Symtab<ELFT>::X->addRegular(Cmd->Name, STB_GLOBAL, STV_DEFAULT);
50 Sym->Visibility = Cmd->Hidden ? STV_HIDDEN : STV_DEFAULT;
51 Cmd->Sym = Sym->body();
Eugene Leviant20d03192016-09-16 15:30:47 +000052
53 // If we have no SECTIONS then we don't have '.' and don't call
54 // assignAddresses(). We calculate symbol value immediately in this case.
55 if (!ScriptConfig->HasSections)
56 cast<DefinedRegular<ELFT>>(Cmd->Sym)->Value = Cmd->Expression(0);
Eugene Leviantceabe802016-08-11 07:56:43 +000057}
58
Rui Ueyama0c70d3c2016-08-12 03:31:09 +000059template <class ELFT> static void addSynthetic(SymbolAssignment *Cmd) {
George Rimare1937bb2016-08-19 15:36:32 +000060 Symbol *Sym = Symtab<ELFT>::X->addSynthetic(
61 Cmd->Name, nullptr, 0, Cmd->Hidden ? STV_HIDDEN : STV_DEFAULT);
Rui Ueyama16024212016-08-11 23:22:52 +000062 Cmd->Sym = Sym->body();
Eugene Leviantceabe802016-08-11 07:56:43 +000063}
64
Eugene Leviantdb741e72016-09-07 07:08:43 +000065template <class ELFT> static void addSymbol(SymbolAssignment *Cmd) {
66 if (Cmd->IsAbsolute)
67 addRegular<ELFT>(Cmd);
68 else
69 addSynthetic<ELFT>(Cmd);
70}
Rui Ueyama16024212016-08-11 23:22:52 +000071// If a symbol was in PROVIDE(), we need to define it only when
72// it is an undefined symbol.
73template <class ELFT> static bool shouldDefine(SymbolAssignment *Cmd) {
74 if (Cmd->Name == ".")
Eugene Leviantceabe802016-08-11 07:56:43 +000075 return false;
Rui Ueyama16024212016-08-11 23:22:52 +000076 if (!Cmd->Provide)
77 return true;
78 SymbolBody *B = Symtab<ELFT>::X->find(Cmd->Name);
79 return B && B->isUndefined();
Eugene Leviantceabe802016-08-11 07:56:43 +000080}
81
George Rimar076fe152016-07-21 06:43:01 +000082bool SymbolAssignment::classof(const BaseCommand *C) {
83 return C->Kind == AssignmentKind;
84}
85
86bool OutputSectionCommand::classof(const BaseCommand *C) {
87 return C->Kind == OutputSectionKind;
88}
89
George Rimareea31142016-07-21 14:26:59 +000090bool InputSectionDescription::classof(const BaseCommand *C) {
91 return C->Kind == InputSectionKind;
92}
93
George Rimareefa7582016-08-04 09:29:31 +000094bool AssertCommand::classof(const BaseCommand *C) {
95 return C->Kind == AssertKind;
96}
97
George Rimare38cbab2016-09-26 19:22:50 +000098bool BytesDataCommand::classof(const BaseCommand *C) {
99 return C->Kind == BytesDataKind;
100}
101
Rui Ueyama36a153c2016-07-23 14:09:58 +0000102template <class ELFT> static bool isDiscarded(InputSectionBase<ELFT> *S) {
George Rimareea31142016-07-21 14:26:59 +0000103 return !S || !S->Live;
Rui Ueyama717677a2016-02-11 21:17:59 +0000104}
105
Rui Ueyamaf34d0e02016-08-12 01:24:53 +0000106template <class ELFT> LinkerScript<ELFT>::LinkerScript() {}
107template <class ELFT> LinkerScript<ELFT>::~LinkerScript() {}
108
Rui Ueyama07320e42016-04-20 20:13:41 +0000109template <class ELFT>
110bool LinkerScript<ELFT>::shouldKeep(InputSectionBase<ELFT> *S) {
Eugene Leviantcf43f172016-10-05 09:36:59 +0000111 for (InputSectionDescription *ID : Opt.KeptSections) {
112 StringRef Filename = S->getFile()->getName();
113 if (!ID->FileRe.match(sys::path::filename(Filename)))
114 continue;
Rui Ueyamab66260a2016-10-05 20:09:50 +0000115
116 for (SectionPattern &P : ID->SectionPatterns)
Eugene Leviantcf43f172016-10-05 09:36:59 +0000117 if (P.SectionRe.match(S->Name))
118 return true;
119 }
George Rimareea31142016-07-21 14:26:59 +0000120 return false;
121}
122
George Rimar575208c2016-09-15 19:15:12 +0000123static bool comparePriority(InputSectionData *A, InputSectionData *B) {
124 return getPriority(A->Name) < getPriority(B->Name);
125}
126
Rafael Espindolac0028d32016-09-08 20:47:52 +0000127static bool compareName(InputSectionData *A, InputSectionData *B) {
Rafael Espindola042a3f22016-09-08 14:06:08 +0000128 return A->Name < B->Name;
Rui Ueyama742c3832016-08-04 22:27:00 +0000129}
George Rimar350ece42016-08-03 08:35:59 +0000130
Rafael Espindolac0028d32016-09-08 20:47:52 +0000131static bool compareAlignment(InputSectionData *A, InputSectionData *B) {
Rui Ueyama742c3832016-08-04 22:27:00 +0000132 // ">" is not a mistake. Larger alignments are placed before smaller
133 // alignments in order to reduce the amount of padding necessary.
134 // This is compatible with GNU.
135 return A->Alignment > B->Alignment;
136}
George Rimar350ece42016-08-03 08:35:59 +0000137
Rafael Espindolac0028d32016-09-08 20:47:52 +0000138static std::function<bool(InputSectionData *, InputSectionData *)>
George Rimarbe394db2016-09-16 20:21:55 +0000139getComparator(SortSectionPolicy K) {
140 switch (K) {
141 case SortSectionPolicy::Alignment:
142 return compareAlignment;
143 case SortSectionPolicy::Name:
Rafael Espindolac0028d32016-09-08 20:47:52 +0000144 return compareName;
George Rimarbe394db2016-09-16 20:21:55 +0000145 case SortSectionPolicy::Priority:
146 return comparePriority;
147 default:
148 llvm_unreachable("unknown sort policy");
149 }
Rui Ueyama742c3832016-08-04 22:27:00 +0000150}
George Rimar0702c4e2016-07-29 15:32:46 +0000151
Rui Ueyama48c3f1c2016-08-12 00:27:23 +0000152template <class ELFT>
Rafael Espindolae71a3f8a2016-09-16 20:34:02 +0000153static bool matchConstraints(ArrayRef<InputSectionBase<ELFT> *> Sections,
George Rimar06ae6832016-08-12 09:07:57 +0000154 ConstraintKind Kind) {
George Rimar8f66df92016-08-12 20:38:20 +0000155 if (Kind == ConstraintKind::NoConstraint)
156 return true;
Rafael Espindolae746e522016-09-21 18:33:44 +0000157 bool IsRW = llvm::any_of(Sections, [=](InputSectionData *Sec2) {
Rafael Espindolad3190792016-09-16 15:10:23 +0000158 auto *Sec = static_cast<InputSectionBase<ELFT> *>(Sec2);
Rafael Espindolae746e522016-09-21 18:33:44 +0000159 return Sec->getSectionHdr()->sh_flags & SHF_WRITE;
George Rimar06ae6832016-08-12 09:07:57 +0000160 });
Rafael Espindolae746e522016-09-21 18:33:44 +0000161 return (IsRW && Kind == ConstraintKind::ReadWrite) ||
162 (!IsRW && Kind == ConstraintKind::ReadOnly);
George Rimar06ae6832016-08-12 09:07:57 +0000163}
164
George Rimar07171f22016-09-21 15:56:44 +0000165static void sortSections(InputSectionData **Begin, InputSectionData **End,
Rui Ueyamaee924702016-09-20 19:42:41 +0000166 SortSectionPolicy K) {
167 if (K != SortSectionPolicy::Default && K != SortSectionPolicy::None)
George Rimar07171f22016-09-21 15:56:44 +0000168 std::stable_sort(Begin, End, getComparator(K));
Rui Ueyamaee924702016-09-20 19:42:41 +0000169}
170
Rafael Espindolad3190792016-09-16 15:10:23 +0000171// Compute and remember which sections the InputSectionDescription matches.
Rafael Espindolabe94e1b2016-09-14 14:32:08 +0000172template <class ELFT>
Rafael Espindolae71a3f8a2016-09-16 20:34:02 +0000173void LinkerScript<ELFT>::computeInputSections(InputSectionDescription *I) {
Rui Ueyama4dc07be2016-09-17 02:23:40 +0000174 // Collects all sections that satisfy constraints of I
175 // and attach them to I.
176 for (SectionPattern &Pat : I->SectionPatterns) {
George Rimar07171f22016-09-21 15:56:44 +0000177 size_t SizeBefore = I->Sections.size();
George Rimar395281c2016-09-16 17:42:10 +0000178 for (ObjectFile<ELFT> *F : Symtab<ELFT>::X->getObjectFiles()) {
Rui Ueyama3ff27f42016-09-17 02:15:28 +0000179 StringRef Filename = sys::path::filename(F->getName());
Rafael Espindolaf135f0e2016-09-19 13:33:38 +0000180 if (!I->FileRe.match(Filename) || Pat.ExcludedFileRe.match(Filename))
Rui Ueyama3ff27f42016-09-17 02:15:28 +0000181 continue;
Rui Ueyama4dc07be2016-09-17 02:23:40 +0000182
Rui Ueyama3ff27f42016-09-17 02:15:28 +0000183 for (InputSectionBase<ELFT> *S : F->getSections())
Rafael Espindolaf135f0e2016-09-19 13:33:38 +0000184 if (!isDiscarded(S) && !S->OutSec && Pat.SectionRe.match(S->Name))
Rui Ueyama3ff27f42016-09-17 02:15:28 +0000185 I->Sections.push_back(S);
Rafael Espindolaf135f0e2016-09-19 13:33:38 +0000186 if (Pat.SectionRe.match("COMMON"))
Rui Ueyama3ff27f42016-09-17 02:15:28 +0000187 I->Sections.push_back(CommonInputSection<ELFT>::X);
George Rimar395281c2016-09-16 17:42:10 +0000188 }
Rafael Espindolad3190792016-09-16 15:10:23 +0000189
George Rimar07171f22016-09-21 15:56:44 +0000190 // Sort sections as instructed by SORT-family commands and --sort-section
191 // option. Because SORT-family commands can be nested at most two depth
192 // (e.g. SORT_BY_NAME(SORT_BY_ALIGNMENT(.text.*))) and because the command
193 // line option is respected even if a SORT command is given, the exact
194 // behavior we have here is a bit complicated. Here are the rules.
195 //
196 // 1. If two SORT commands are given, --sort-section is ignored.
197 // 2. If one SORT command is given, and if it is not SORT_NONE,
198 // --sort-section is handled as an inner SORT command.
199 // 3. If one SORT command is given, and if it is SORT_NONE, don't sort.
200 // 4. If no SORT command is given, sort according to --sort-section.
201 InputSectionData **Begin = I->Sections.data() + SizeBefore;
202 InputSectionData **End = I->Sections.data() + I->Sections.size();
203 if (Pat.SortOuter != SortSectionPolicy::None) {
204 if (Pat.SortInner == SortSectionPolicy::Default)
205 sortSections(Begin, End, Config->SortSection);
206 else
207 sortSections(Begin, End, Pat.SortInner);
208 sortSections(Begin, End, Pat.SortOuter);
209 }
Rui Ueyamaee924702016-09-20 19:42:41 +0000210 }
Rafael Espindolad3190792016-09-16 15:10:23 +0000211
212 // We do not add duplicate input sections, so mark them with a dummy output
213 // section for now.
214 for (InputSectionData *S : I->Sections) {
215 auto *S2 = static_cast<InputSectionBase<ELFT> *>(S);
216 S2->OutSec = (OutputSectionBase<ELFT> *)-1;
217 }
Rafael Espindolabe94e1b2016-09-14 14:32:08 +0000218}
219
220template <class ELFT>
221void LinkerScript<ELFT>::discard(ArrayRef<InputSectionBase<ELFT> *> V) {
222 for (InputSectionBase<ELFT> *S : V) {
223 S->Live = false;
224 reportDiscarded(S);
225 }
226}
227
George Rimar06ae6832016-08-12 09:07:57 +0000228template <class ELFT>
Rui Ueyama0b9ce6a2016-08-12 03:16:56 +0000229std::vector<InputSectionBase<ELFT> *>
George Rimar06ae6832016-08-12 09:07:57 +0000230LinkerScript<ELFT>::createInputSectionList(OutputSectionCommand &OutCmd) {
Rui Ueyama0b9ce6a2016-08-12 03:16:56 +0000231 std::vector<InputSectionBase<ELFT> *> Ret;
Rui Ueyamae7f912c2016-08-03 21:12:09 +0000232
George Rimar06ae6832016-08-12 09:07:57 +0000233 for (const std::unique_ptr<BaseCommand> &Base : OutCmd.Commands) {
Rafael Espindola7c3ff2e2016-09-16 21:05:36 +0000234 auto *Cmd = dyn_cast<InputSectionDescription>(Base.get());
235 if (!Cmd)
Rui Ueyama0b9ce6a2016-08-12 03:16:56 +0000236 continue;
Rafael Espindolae71a3f8a2016-09-16 20:34:02 +0000237 computeInputSections(Cmd);
Rafael Espindolad3190792016-09-16 15:10:23 +0000238 for (InputSectionData *S : Cmd->Sections)
239 Ret.push_back(static_cast<InputSectionBase<ELFT> *>(S));
Rui Ueyama0b9ce6a2016-08-12 03:16:56 +0000240 }
Rafael Espindolae71a3f8a2016-09-16 20:34:02 +0000241
Rui Ueyama0b9ce6a2016-08-12 03:16:56 +0000242 return Ret;
243}
244
Petr Hoseke5d3ca52016-08-31 15:31:17 +0000245template <class ELFT>
Rafael Espindola10897f12016-09-13 14:23:14 +0000246static SectionKey<ELFT::Is64Bits> createKey(InputSectionBase<ELFT> *C,
247 StringRef OutsecName) {
248 // When using linker script the merge rules are different.
249 // Unfortunately, linker scripts are name based. This means that expressions
250 // like *(.foo*) can refer to multiple input sections that would normally be
251 // placed in different output sections. We cannot put them in different
252 // output sections or we would produce wrong results for
253 // start = .; *(.foo.*) end = .; *(.bar)
254 // and a mapping of .foo1 and .bar1 to one section and .foo2 and .bar2 to
255 // another. The problem is that there is no way to layout those output
256 // sections such that the .foo sections are the only thing between the
257 // start and end symbols.
258
259 // An extra annoyance is that we cannot simply disable merging of the contents
260 // of SHF_MERGE sections, but our implementation requires one output section
261 // per "kind" (string or not, which size/aligment).
262 // Fortunately, creating symbols in the middle of a merge section is not
263 // supported by bfd or gold, so we can just create multiple section in that
264 // case.
265 const typename ELFT::Shdr *H = C->getSectionHdr();
266 typedef typename ELFT::uint uintX_t;
267 uintX_t Flags = H->sh_flags & (SHF_MERGE | SHF_STRINGS);
268
269 uintX_t Alignment = 0;
270 if (isa<MergeInputSection<ELFT>>(C))
271 Alignment = std::max(H->sh_addralign, H->sh_entsize);
272
273 return SectionKey<ELFT::Is64Bits>{OutsecName, /*Type*/ 0, Flags, Alignment};
274}
275
276template <class ELFT>
Eugene Leviant20d03192016-09-16 15:30:47 +0000277void LinkerScript<ELFT>::addSection(OutputSectionFactory<ELFT> &Factory,
278 InputSectionBase<ELFT> *Sec,
279 StringRef Name) {
280 OutputSectionBase<ELFT> *OutSec;
281 bool IsNew;
282 std::tie(OutSec, IsNew) = Factory.create(createKey(Sec, Name), Sec);
283 if (IsNew)
284 OutputSections->push_back(OutSec);
285 OutSec->addSection(Sec);
286}
287
288template <class ELFT>
289void LinkerScript<ELFT>::processCommands(OutputSectionFactory<ELFT> &Factory) {
Rafael Espindola28c15972016-09-13 13:00:06 +0000290
Rafael Espindola7c3ff2e2016-09-16 21:05:36 +0000291 for (unsigned I = 0; I < Opt.Commands.size(); ++I) {
292 auto Iter = Opt.Commands.begin() + I;
293 const std::unique_ptr<BaseCommand> &Base1 = *Iter;
Rui Ueyama2ab5f732016-08-12 03:33:04 +0000294 if (auto *Cmd = dyn_cast<SymbolAssignment>(Base1.get())) {
295 if (shouldDefine<ELFT>(Cmd))
296 addRegular<ELFT>(Cmd);
297 continue;
298 }
Eugene Leviant20d03192016-09-16 15:30:47 +0000299 if (auto *Cmd = dyn_cast<AssertCommand>(Base1.get())) {
300 // If we don't have SECTIONS then output sections have already been
George Rimar194470cd2016-09-17 19:21:05 +0000301 // created by Writer<ELFT>. The LinkerScript<ELFT>::assignAddresses
Eugene Leviant20d03192016-09-16 15:30:47 +0000302 // will not be called, so ASSERT should be evaluated now.
303 if (!Opt.HasSections)
304 Cmd->Expression(0);
305 continue;
306 }
Rui Ueyama2ab5f732016-08-12 03:33:04 +0000307
Eugene Leviantceabe802016-08-11 07:56:43 +0000308 if (auto *Cmd = dyn_cast<OutputSectionCommand>(Base1.get())) {
Rafael Espindola7bd37872016-09-12 16:05:16 +0000309 std::vector<InputSectionBase<ELFT> *> V = createInputSectionList(*Cmd);
310
Rui Ueyama48c3f1c2016-08-12 00:27:23 +0000311 if (Cmd->Name == "/DISCARD/") {
Rafael Espindola7bd37872016-09-12 16:05:16 +0000312 discard(V);
Rui Ueyama48c3f1c2016-08-12 00:27:23 +0000313 continue;
314 }
Eugene Leviantceabe802016-08-11 07:56:43 +0000315
Rafael Espindola7c3ff2e2016-09-16 21:05:36 +0000316 if (!matchConstraints<ELFT>(V, Cmd->Constraint)) {
317 for (InputSectionBase<ELFT> *S : V)
318 S->OutSec = nullptr;
319 Opt.Commands.erase(Iter);
George Rimardfbbbc82016-09-17 09:50:10 +0000320 --I;
Rafael Espindola7c3ff2e2016-09-16 21:05:36 +0000321 continue;
322 }
323
324 for (const std::unique_ptr<BaseCommand> &Base : Cmd->Commands)
325 if (auto *OutCmd = dyn_cast<SymbolAssignment>(Base.get()))
326 if (shouldDefine<ELFT>(OutCmd))
327 addSymbol<ELFT>(OutCmd);
328
Eugene Leviant97403d12016-09-01 09:55:57 +0000329 if (V.empty())
Rui Ueyama0b9ce6a2016-08-12 03:16:56 +0000330 continue;
331
George Rimardb24d9c2016-08-19 15:18:23 +0000332 for (InputSectionBase<ELFT> *Sec : V) {
Eugene Leviant20d03192016-09-16 15:30:47 +0000333 addSection(Factory, Sec, Cmd->Name);
334 if (uint32_t Subalign = Cmd->SubalignExpr ? Cmd->SubalignExpr(0) : 0)
George Rimardb24d9c2016-08-19 15:18:23 +0000335 Sec->Alignment = Subalign;
George Rimardb24d9c2016-08-19 15:18:23 +0000336 }
Eugene Leviantceabe802016-08-11 07:56:43 +0000337 }
Rui Ueyama48c3f1c2016-08-12 00:27:23 +0000338 }
Eugene Leviant20d03192016-09-16 15:30:47 +0000339}
Eugene Leviante63d81b2016-07-20 14:43:20 +0000340
Eugene Leviant20d03192016-09-16 15:30:47 +0000341template <class ELFT>
342void LinkerScript<ELFT>::createSections(OutputSectionFactory<ELFT> &Factory) {
343 processCommands(Factory);
Rui Ueyama0b9ce6a2016-08-12 03:16:56 +0000344 // Add orphan sections.
Eugene Leviant20d03192016-09-16 15:30:47 +0000345 for (ObjectFile<ELFT> *F : Symtab<ELFT>::X->getObjectFiles())
346 for (InputSectionBase<ELFT> *S : F->getSections())
347 if (!isDiscarded(S) && !S->OutSec)
Eugene Levianta8d12ef2016-10-05 10:10:45 +0000348 addSection(Factory, S, getOutputSectionName(S->Name));
Eugene Leviante63d81b2016-07-20 14:43:20 +0000349}
350
Eugene Leviantdb741e72016-09-07 07:08:43 +0000351// Sets value of a section-defined symbol. Two kinds of
352// symbols are processed: synthetic symbols, whose value
353// is an offset from beginning of section and regular
354// symbols whose value is absolute.
355template <class ELFT>
356static void assignSectionSymbol(SymbolAssignment *Cmd,
357 OutputSectionBase<ELFT> *Sec,
358 typename ELFT::uint Off) {
359 if (!Cmd->Sym)
360 return;
361
362 if (auto *Body = dyn_cast<DefinedSynthetic<ELFT>>(Cmd->Sym)) {
363 Body->Section = Sec;
364 Body->Value = Cmd->Expression(Sec->getVA() + Off) - Sec->getVA();
365 return;
366 }
367 auto *Body = cast<DefinedRegular<ELFT>>(Cmd->Sym);
368 Body->Value = Cmd->Expression(Sec->getVA() + Off);
369}
370
Rafael Espindolaa940e532016-09-22 12:35:44 +0000371template <class ELFT> static bool isTbss(OutputSectionBase<ELFT> *Sec) {
372 return (Sec->getFlags() & SHF_TLS) && Sec->getType() == SHT_NOBITS;
373}
374
Rafael Espindolad3190792016-09-16 15:10:23 +0000375template <class ELFT> void LinkerScript<ELFT>::output(InputSection<ELFT> *S) {
376 if (!AlreadyOutputIS.insert(S).second)
377 return;
Rafael Espindolaa940e532016-09-22 12:35:44 +0000378 bool IsTbss = isTbss(CurOutSec);
Eugene Leviant20889c52016-08-31 08:13:33 +0000379
Rafael Espindolad3190792016-09-16 15:10:23 +0000380 uintX_t Pos = IsTbss ? Dot + ThreadBssOffset : Dot;
381 Pos = alignTo(Pos, S->Alignment);
382 S->OutSecOff = Pos - CurOutSec->getVA();
383 Pos += S->getSize();
384
385 // Update output section size after adding each section. This is so that
386 // SIZEOF works correctly in the case below:
387 // .foo { *(.aaa) a = SIZEOF(.foo); *(.bbb) }
388 CurOutSec->setSize(Pos - CurOutSec->getVA());
389
Rafael Espindola7252ae52016-09-22 12:00:08 +0000390 if (IsTbss)
391 ThreadBssOffset = Pos - Dot;
392 else
Rafael Espindolad3190792016-09-16 15:10:23 +0000393 Dot = Pos;
394}
395
396template <class ELFT> void LinkerScript<ELFT>::flush() {
Rafael Espindola65499b92016-09-23 20:10:47 +0000397 if (!CurOutSec || !AlreadyOutputOS.insert(CurOutSec).second)
398 return;
399 if (auto *OutSec = dyn_cast<OutputSection<ELFT>>(CurOutSec)) {
Rafael Espindolad3190792016-09-16 15:10:23 +0000400 for (InputSection<ELFT> *I : OutSec->Sections)
401 output(I);
Rafael Espindola65499b92016-09-23 20:10:47 +0000402 } else {
403 Dot += CurOutSec->getSize();
Eugene Leviant20889c52016-08-31 08:13:33 +0000404 }
405}
406
407template <class ELFT>
Rafael Espindolad3190792016-09-16 15:10:23 +0000408void LinkerScript<ELFT>::switchTo(OutputSectionBase<ELFT> *Sec) {
409 if (CurOutSec == Sec)
410 return;
411 if (AlreadyOutputOS.count(Sec))
412 return;
413
414 flush();
415 CurOutSec = Sec;
416
417 Dot = alignTo(Dot, CurOutSec->getAlignment());
Rafael Espindolaa940e532016-09-22 12:35:44 +0000418 CurOutSec->setVA(isTbss(CurOutSec) ? Dot + ThreadBssOffset : Dot);
Eugene Leviantb71d6f72016-10-06 09:39:28 +0000419
420 // If neither AT nor AT> is specified for an allocatable section, the linker
421 // will set the LMA such that the difference between VMA and LMA for the
422 // section is the same as the preceding output section in the same region
423 // https://sourceware.org/binutils/docs-2.20/ld/Output-Section-LMA.html
424 CurOutSec->setLMAOffset(LMAOffset);
Rafael Espindolad3190792016-09-16 15:10:23 +0000425}
426
427template <class ELFT> void LinkerScript<ELFT>::process(BaseCommand &Base) {
George Rimare38cbab2016-09-26 19:22:50 +0000428 // This handles the assignments to symbol or to a location counter (.)
Rafael Espindolad3190792016-09-16 15:10:23 +0000429 if (auto *AssignCmd = dyn_cast<SymbolAssignment>(&Base)) {
430 if (AssignCmd->Name == ".") {
431 // Update to location counter means update to section size.
432 Dot = AssignCmd->Expression(Dot);
433 CurOutSec->setSize(Dot - CurOutSec->getVA());
434 return;
435 }
436 assignSectionSymbol<ELFT>(AssignCmd, CurOutSec, Dot - CurOutSec->getVA());
Eugene Leviantceabe802016-08-11 07:56:43 +0000437 return;
Rui Ueyama2de509c2016-08-12 00:55:08 +0000438 }
George Rimare38cbab2016-09-26 19:22:50 +0000439
440 // Handle BYTE(), SHORT(), LONG(), or QUAD().
441 if (auto *DataCmd = dyn_cast<BytesDataCommand>(&Base)) {
442 DataCmd->Offset = Dot - CurOutSec->getVA();
443 Dot += DataCmd->Size;
444 CurOutSec->setSize(Dot - CurOutSec->getVA());
445 return;
446 }
447
448 // It handles single input section description command,
449 // calculates and assigns the offsets for each section and also
450 // updates the output section size.
Rafael Espindolad3190792016-09-16 15:10:23 +0000451 auto &ICmd = cast<InputSectionDescription>(Base);
452 for (InputSectionData *ID : ICmd.Sections) {
453 auto *IB = static_cast<InputSectionBase<ELFT> *>(ID);
454 switchTo(IB->OutSec);
455 if (auto *I = dyn_cast<InputSection<ELFT>>(IB))
456 output(I);
Rafael Espindola65499b92016-09-23 20:10:47 +0000457 else
458 flush();
Eugene Leviantceabe802016-08-11 07:56:43 +0000459 }
460}
461
George Rimar8f66df92016-08-12 20:38:20 +0000462template <class ELFT>
George Rimara14b13d2016-09-07 10:46:07 +0000463static std::vector<OutputSectionBase<ELFT> *>
Eugene Leviant92577642016-10-10 11:23:12 +0000464findSections(StringRef Name,
Rafael Espindolad3190792016-09-16 15:10:23 +0000465 const std::vector<OutputSectionBase<ELFT> *> &Sections) {
George Rimara14b13d2016-09-07 10:46:07 +0000466 std::vector<OutputSectionBase<ELFT> *> Ret;
467 for (OutputSectionBase<ELFT> *Sec : Sections)
Eugene Leviant92577642016-10-10 11:23:12 +0000468 if (Sec->getName() == Name)
George Rimara14b13d2016-09-07 10:46:07 +0000469 Ret.push_back(Sec);
470 return Ret;
George Rimar8f66df92016-08-12 20:38:20 +0000471}
472
Rafael Espindolad3190792016-09-16 15:10:23 +0000473template <class ELFT>
474void LinkerScript<ELFT>::assignOffsets(OutputSectionCommand *Cmd) {
Eugene Leviantb71d6f72016-10-06 09:39:28 +0000475 if (Cmd->LMAExpr)
476 LMAOffset = Cmd->LMAExpr(Dot) - Dot;
Rafael Espindolad3190792016-09-16 15:10:23 +0000477 std::vector<OutputSectionBase<ELFT> *> Sections =
Eugene Leviant92577642016-10-10 11:23:12 +0000478 findSections(Cmd->Name, *OutputSections);
Rafael Espindolad3190792016-09-16 15:10:23 +0000479 if (Sections.empty())
480 return;
481 switchTo(Sections[0]);
Rafael Espindolad3190792016-09-16 15:10:23 +0000482 // Find the last section output location. We will output orphan sections
483 // there so that end symbols point to the correct location.
484 auto E = std::find_if(Cmd->Commands.rbegin(), Cmd->Commands.rend(),
485 [](const std::unique_ptr<BaseCommand> &Cmd) {
486 return !isa<SymbolAssignment>(*Cmd);
487 })
488 .base();
489 for (auto I = Cmd->Commands.begin(); I != E; ++I)
490 process(**I);
Rafael Espindola65499b92016-09-23 20:10:47 +0000491 for (OutputSectionBase<ELFT> *Base : Sections)
Rafael Espindolad3190792016-09-16 15:10:23 +0000492 switchTo(Base);
Rafael Espindola65499b92016-09-23 20:10:47 +0000493 flush();
George Rimarb31dd372016-09-19 13:27:31 +0000494 std::for_each(E, Cmd->Commands.end(),
495 [this](std::unique_ptr<BaseCommand> &B) { process(*B.get()); });
Rafael Espindolad3190792016-09-16 15:10:23 +0000496}
497
Rafael Espindola9546fff2016-09-22 14:40:50 +0000498template <class ELFT> void LinkerScript<ELFT>::adjustSectionsBeforeSorting() {
Rafael Espindola6d38e4d2016-09-20 13:12:07 +0000499 // It is common practice to use very generic linker scripts. So for any
500 // given run some of the output sections in the script will be empty.
501 // We could create corresponding empty output sections, but that would
502 // clutter the output.
503 // We instead remove trivially empty sections. The bfd linker seems even
504 // more aggressive at removing them.
505 auto Pos = std::remove_if(
506 Opt.Commands.begin(), Opt.Commands.end(),
507 [&](const std::unique_ptr<BaseCommand> &Base) {
508 auto *Cmd = dyn_cast<OutputSectionCommand>(Base.get());
509 if (!Cmd)
510 return false;
511 std::vector<OutputSectionBase<ELFT> *> Secs =
Eugene Leviant92577642016-10-10 11:23:12 +0000512 findSections(Cmd->Name, *OutputSections);
Rafael Espindola6d38e4d2016-09-20 13:12:07 +0000513 if (!Secs.empty())
514 return false;
515 for (const std::unique_ptr<BaseCommand> &I : Cmd->Commands)
516 if (!isa<InputSectionDescription>(I.get()))
517 return false;
518 return true;
519 });
520 Opt.Commands.erase(Pos, Opt.Commands.end());
521
Rafael Espindola9546fff2016-09-22 14:40:50 +0000522 // If the output section contains only symbol assignments, create a
523 // corresponding output section. The bfd linker seems to only create them if
524 // '.' is assigned to, but creating these section should not have any bad
525 // consequeces and gives us a section to put the symbol in.
526 uintX_t Flags = SHF_ALLOC;
527 uint32_t Type = 0;
528 for (const std::unique_ptr<BaseCommand> &Base : Opt.Commands) {
529 auto *Cmd = dyn_cast<OutputSectionCommand>(Base.get());
530 if (!Cmd)
531 continue;
532 std::vector<OutputSectionBase<ELFT> *> Secs =
Eugene Leviant92577642016-10-10 11:23:12 +0000533 findSections(Cmd->Name, *OutputSections);
Rafael Espindola9546fff2016-09-22 14:40:50 +0000534 if (!Secs.empty()) {
535 Flags = Secs[0]->getFlags();
536 Type = Secs[0]->getType();
537 continue;
538 }
539
540 auto *OutSec = new OutputSection<ELFT>(Cmd->Name, Type, Flags);
541 Out<ELFT>::Pool.emplace_back(OutSec);
542 OutputSections->push_back(OutSec);
543 }
544}
545
Rafael Espindola15c57952016-09-22 18:05:49 +0000546// When placing orphan sections, we want to place them after symbol assignments
547// so that an orphan after
548// begin_foo = .;
549// foo : { *(foo) }
550// end_foo = .;
551// doesn't break the intended meaning of the begin/end symbols.
552// We don't want to go over sections since Writer<ELFT>::sortSections is the
553// one in charge of deciding the order of the sections.
554// We don't want to go over alignments, since doing so in
555// rx_sec : { *(rx_sec) }
556// . = ALIGN(0x1000);
557// /* The RW PT_LOAD starts here*/
558// rw_sec : { *(rw_sec) }
559// would mean that the RW PT_LOAD would become unaligned.
560static bool shouldSkip(const BaseCommand &Cmd) {
561 if (isa<OutputSectionCommand>(Cmd))
562 return false;
563 const auto *Assign = dyn_cast<SymbolAssignment>(&Cmd);
564 if (!Assign)
565 return true;
566 return Assign->Name != ".";
567}
568
Rafael Espindola6d91fce2016-09-29 18:50:34 +0000569template <class ELFT>
570void LinkerScript<ELFT>::assignAddresses(std::vector<PhdrEntry<ELFT>> &Phdrs) {
George Rimar652852c2016-04-16 10:10:32 +0000571 // Orphan sections are sections present in the input files which
Rui Ueyama7c18c282016-04-18 21:00:40 +0000572 // are not explicitly placed into the output file by the linker script.
573 // We place orphan sections at end of file.
574 // Other linkers places them using some heuristics as described in
George Rimar652852c2016-04-16 10:10:32 +0000575 // https://sourceware.org/binutils/docs/ld/Orphan-Sections.html#Orphan-Sections.
Rafael Espindolaaab6d5c2016-09-16 21:29:07 +0000576
577 // The OutputSections are already in the correct order.
578 // This loops creates or moves commands as needed so that they are in the
579 // correct order.
580 int CmdIndex = 0;
Rui Ueyamae5cc6682016-08-12 00:36:56 +0000581 for (OutputSectionBase<ELFT> *Sec : *OutputSections) {
George Rimar652852c2016-04-16 10:10:32 +0000582 StringRef Name = Sec->getName();
Rafael Espindolaaab6d5c2016-09-16 21:29:07 +0000583
584 // Find the last spot where we can insert a command and still get the
Rafael Espindola15c57952016-09-22 18:05:49 +0000585 // correct result.
Rafael Espindolaaab6d5c2016-09-16 21:29:07 +0000586 auto CmdIter = Opt.Commands.begin() + CmdIndex;
587 auto E = Opt.Commands.end();
Rafael Espindola15c57952016-09-22 18:05:49 +0000588 while (CmdIter != E && shouldSkip(**CmdIter)) {
Rafael Espindolaaab6d5c2016-09-16 21:29:07 +0000589 ++CmdIter;
590 ++CmdIndex;
591 }
592
593 auto Pos =
594 std::find_if(CmdIter, E, [&](const std::unique_ptr<BaseCommand> &Base) {
595 auto *Cmd = dyn_cast<OutputSectionCommand>(Base.get());
596 return Cmd && Cmd->Name == Name;
597 });
598 if (Pos == E) {
599 Opt.Commands.insert(CmdIter,
600 llvm::make_unique<OutputSectionCommand>(Name));
Rafael Espindola15c57952016-09-22 18:05:49 +0000601 ++CmdIndex;
602 continue;
Rafael Espindolaaab6d5c2016-09-16 21:29:07 +0000603 }
Rafael Espindola15c57952016-09-22 18:05:49 +0000604
605 // Continue from where we found it.
606 CmdIndex = (Pos - Opt.Commands.begin()) + 1;
607 continue;
George Rimar652852c2016-04-16 10:10:32 +0000608 }
George Rimar652852c2016-04-16 10:10:32 +0000609
Rui Ueyama7c18c282016-04-18 21:00:40 +0000610 // Assign addresses as instructed by linker script SECTIONS sub-commands.
Rafael Espindolabe607332016-09-30 00:16:11 +0000611 Dot = 0;
George Rimar652852c2016-04-16 10:10:32 +0000612
George Rimar076fe152016-07-21 06:43:01 +0000613 for (const std::unique_ptr<BaseCommand> &Base : Opt.Commands) {
614 if (auto *Cmd = dyn_cast<SymbolAssignment>(Base.get())) {
Rui Ueyama8d083e62016-07-29 05:48:39 +0000615 if (Cmd->Name == ".") {
616 Dot = Cmd->Expression(Dot);
617 } else if (Cmd->Sym) {
618 cast<DefinedRegular<ELFT>>(Cmd->Sym)->Value = Cmd->Expression(Dot);
619 }
George Rimar652852c2016-04-16 10:10:32 +0000620 continue;
621 }
622
George Rimareefa7582016-08-04 09:29:31 +0000623 if (auto *Cmd = dyn_cast<AssertCommand>(Base.get())) {
624 Cmd->Expression(Dot);
625 continue;
626 }
627
George Rimar076fe152016-07-21 06:43:01 +0000628 auto *Cmd = cast<OutputSectionCommand>(Base.get());
George Rimar652852c2016-04-16 10:10:32 +0000629
Rafael Espindolad3190792016-09-16 15:10:23 +0000630 if (Cmd->AddrExpr)
631 Dot = Cmd->AddrExpr(Dot);
George Rimar58e5c4d2016-07-25 08:29:46 +0000632
Rafael Espindolad3190792016-09-16 15:10:23 +0000633 assignOffsets(Cmd);
George Rimar652852c2016-04-16 10:10:32 +0000634 }
Rui Ueyama52c4e172016-07-01 10:42:25 +0000635
Rafael Espindolaaab6d5c2016-09-16 21:29:07 +0000636 uintX_t MinVA = std::numeric_limits<uintX_t>::max();
637 for (OutputSectionBase<ELFT> *Sec : *OutputSections) {
638 if (Sec->getFlags() & SHF_ALLOC)
639 MinVA = std::min(MinVA, Sec->getVA());
640 else
Rafael Espindolad3190792016-09-16 15:10:23 +0000641 Sec->setVA(0);
Rafael Espindolaaab6d5c2016-09-16 21:29:07 +0000642 }
643
Rafael Espindola0d4b6d52016-09-22 16:47:21 +0000644 uintX_t HeaderSize = getHeaderSize();
Rafael Espindola6d91fce2016-09-29 18:50:34 +0000645 auto FirstPTLoad =
646 std::find_if(Phdrs.begin(), Phdrs.end(), [](const PhdrEntry<ELFT> &E) {
647 return E.H.p_type == PT_LOAD;
648 });
649 if (HeaderSize <= MinVA && FirstPTLoad != Phdrs.end()) {
650 // ELF and Program headers need to be right before the first section in
651 // memory. Set their addresses accordingly.
652 MinVA = alignDown(MinVA - HeaderSize, Target->PageSize);
653 Out<ELFT>::ElfHeader->setVA(MinVA);
654 Out<ELFT>::ProgramHeaders->setVA(Out<ELFT>::ElfHeader->getSize() + MinVA);
655 FirstPTLoad->First = Out<ELFT>::ElfHeader;
656 if (!FirstPTLoad->Last)
657 FirstPTLoad->Last = Out<ELFT>::ProgramHeaders;
Eugene Leviantcd8eaf82016-10-10 15:09:44 +0000658 } else if (!FirstPTLoad->First) {
Rui Ueyamab224c0482016-10-10 18:10:01 +0000659 // Sometimes the very first PT_LOAD segment can be empty.
Eugene Leviantcd8eaf82016-10-10 15:09:44 +0000660 // This happens if (all conditions met):
661 // - Linker script is used
662 // - First section in ELF image is not RO
663 // - Not enough space for program headers.
664 // The code below removes empty PT_LOAD segment and updates
665 // program headers size.
666 Phdrs.erase(FirstPTLoad);
667 Out<ELFT>::ProgramHeaders->setSize(sizeof(typename ELFT::Phdr) *
668 Phdrs.size());
Rafael Espindola6d91fce2016-09-29 18:50:34 +0000669 }
George Rimar652852c2016-04-16 10:10:32 +0000670}
671
Rui Ueyama464daad2016-08-22 04:55:20 +0000672// Creates program headers as instructed by PHDRS linker script command.
Rui Ueyama07320e42016-04-20 20:13:41 +0000673template <class ELFT>
Rafael Espindolaa4b41dc2016-08-04 12:13:05 +0000674std::vector<PhdrEntry<ELFT>> LinkerScript<ELFT>::createPhdrs() {
Rui Ueyamaedebbdf2016-07-24 23:47:31 +0000675 std::vector<PhdrEntry<ELFT>> Ret;
Eugene Leviantbbe38602016-07-19 09:25:43 +0000676
Rui Ueyama464daad2016-08-22 04:55:20 +0000677 // Process PHDRS and FILEHDR keywords because they are not
678 // real output sections and cannot be added in the following loop.
Eugene Leviantbbe38602016-07-19 09:25:43 +0000679 for (const PhdrsCommand &Cmd : Opt.PhdrsCommands) {
Rui Ueyamaedebbdf2016-07-24 23:47:31 +0000680 Ret.emplace_back(Cmd.Type, Cmd.Flags == UINT_MAX ? PF_R : Cmd.Flags);
681 PhdrEntry<ELFT> &Phdr = Ret.back();
Eugene Leviantbbe38602016-07-19 09:25:43 +0000682
683 if (Cmd.HasFilehdr)
Rui Ueyamaadca2452016-07-23 14:18:48 +0000684 Phdr.add(Out<ELFT>::ElfHeader);
Eugene Leviantbbe38602016-07-19 09:25:43 +0000685 if (Cmd.HasPhdrs)
Rui Ueyamaadca2452016-07-23 14:18:48 +0000686 Phdr.add(Out<ELFT>::ProgramHeaders);
Eugene Leviant56b21c82016-09-09 09:46:16 +0000687
688 if (Cmd.LMAExpr) {
689 Phdr.H.p_paddr = Cmd.LMAExpr(0);
690 Phdr.HasLMA = true;
691 }
Eugene Leviantbbe38602016-07-19 09:25:43 +0000692 }
693
Rui Ueyama464daad2016-08-22 04:55:20 +0000694 // Add output sections to program headers.
Rui Ueyamaedebbdf2016-07-24 23:47:31 +0000695 PhdrEntry<ELFT> *Load = nullptr;
696 uintX_t Flags = PF_R;
Rui Ueyama464daad2016-08-22 04:55:20 +0000697 for (OutputSectionBase<ELFT> *Sec : *OutputSections) {
Eugene Leviantbbe38602016-07-19 09:25:43 +0000698 if (!(Sec->getFlags() & SHF_ALLOC))
699 break;
700
Rui Ueyamaedebbdf2016-07-24 23:47:31 +0000701 std::vector<size_t> PhdrIds = getPhdrIndices(Sec->getName());
Eugene Leviantbbe38602016-07-19 09:25:43 +0000702 if (!PhdrIds.empty()) {
703 // Assign headers specified by linker script
704 for (size_t Id : PhdrIds) {
Rui Ueyamaedebbdf2016-07-24 23:47:31 +0000705 Ret[Id].add(Sec);
Eugene Leviant865bf862016-07-21 10:43:25 +0000706 if (Opt.PhdrsCommands[Id].Flags == UINT_MAX)
Rafael Espindola0b113672016-07-27 14:10:56 +0000707 Ret[Id].H.p_flags |= Sec->getPhdrFlags();
Eugene Leviantbbe38602016-07-19 09:25:43 +0000708 }
709 } else {
710 // If we have no load segment or flags've changed then we want new load
711 // segment.
Rafael Espindola0b113672016-07-27 14:10:56 +0000712 uintX_t NewFlags = Sec->getPhdrFlags();
Eugene Leviantbbe38602016-07-19 09:25:43 +0000713 if (Load == nullptr || Flags != NewFlags) {
Rui Ueyamaedebbdf2016-07-24 23:47:31 +0000714 Load = &*Ret.emplace(Ret.end(), PT_LOAD, NewFlags);
Eugene Leviantbbe38602016-07-19 09:25:43 +0000715 Flags = NewFlags;
716 }
Rui Ueyama18f084f2016-07-20 19:36:41 +0000717 Load->add(Sec);
Eugene Leviantbbe38602016-07-19 09:25:43 +0000718 }
Eugene Leviantbbe38602016-07-19 09:25:43 +0000719 }
Rui Ueyamaedebbdf2016-07-24 23:47:31 +0000720 return Ret;
Eugene Leviantbbe38602016-07-19 09:25:43 +0000721}
722
Eugene Leviantf9bc3bd2016-08-16 06:40:58 +0000723template <class ELFT> bool LinkerScript<ELFT>::ignoreInterpSection() {
724 // Ignore .interp section in case we have PHDRS specification
725 // and PT_INTERP isn't listed.
726 return !Opt.PhdrsCommands.empty() &&
727 llvm::find_if(Opt.PhdrsCommands, [](const PhdrsCommand &Cmd) {
728 return Cmd.Type == PT_INTERP;
729 }) == Opt.PhdrsCommands.end();
730}
731
Eugene Leviantbbe38602016-07-19 09:25:43 +0000732template <class ELFT>
Rui Ueyama07320e42016-04-20 20:13:41 +0000733ArrayRef<uint8_t> LinkerScript<ELFT>::getFiller(StringRef Name) {
George Rimarf6c3cce2016-07-21 07:48:54 +0000734 for (const std::unique_ptr<BaseCommand> &Base : Opt.Commands)
735 if (auto *Cmd = dyn_cast<OutputSectionCommand>(Base.get()))
736 if (Cmd->Name == Name)
737 return Cmd->Filler;
738 return {};
George Rimare2ee72b2016-02-26 14:48:31 +0000739}
740
George Rimare38cbab2016-09-26 19:22:50 +0000741template <class ELFT>
742static void writeInt(uint8_t *Buf, uint64_t Data, uint64_t Size) {
743 const endianness E = ELFT::TargetEndianness;
744
745 switch (Size) {
746 case 1:
747 *Buf = (uint8_t)Data;
748 break;
749 case 2:
750 write16<E>(Buf, Data);
751 break;
752 case 4:
753 write32<E>(Buf, Data);
754 break;
755 case 8:
756 write64<E>(Buf, Data);
757 break;
758 default:
759 llvm_unreachable("unsupported Size argument");
760 }
761}
762
763template <class ELFT>
764void LinkerScript<ELFT>::writeDataBytes(StringRef Name, uint8_t *Buf) {
765 int I = getSectionIndex(Name);
766 if (I == INT_MAX)
767 return;
768
769 OutputSectionCommand *Cmd =
770 dyn_cast<OutputSectionCommand>(Opt.Commands[I].get());
771 for (const std::unique_ptr<BaseCommand> &Base2 : Cmd->Commands)
772 if (auto *DataCmd = dyn_cast<BytesDataCommand>(Base2.get()))
773 writeInt<ELFT>(&Buf[DataCmd->Offset], DataCmd->Data, DataCmd->Size);
774}
775
Eugene Leviantb71d6f72016-10-06 09:39:28 +0000776template <class ELFT> bool LinkerScript<ELFT>::hasLMA(StringRef Name) {
George Rimar8ceadb32016-08-17 07:44:19 +0000777 for (const std::unique_ptr<BaseCommand> &Base : Opt.Commands)
778 if (auto *Cmd = dyn_cast<OutputSectionCommand>(Base.get()))
Eugene Leviantb71d6f72016-10-06 09:39:28 +0000779 if (Cmd->LMAExpr && Cmd->Name == Name)
780 return true;
781 return false;
George Rimar8ceadb32016-08-17 07:44:19 +0000782}
783
Rui Ueyamac3e2a4b2016-04-21 20:30:00 +0000784// Returns the index of the given section name in linker script
785// SECTIONS commands. Sections are laid out as the same order as they
786// were in the script. If a given name did not appear in the script,
787// it returns INT_MAX, so that it will be laid out at end of file.
George Rimar076fe152016-07-21 06:43:01 +0000788template <class ELFT> int LinkerScript<ELFT>::getSectionIndex(StringRef Name) {
Rui Ueyamaf510fa62016-07-26 00:21:15 +0000789 int I = 0;
790 for (std::unique_ptr<BaseCommand> &Base : Opt.Commands) {
791 if (auto *Cmd = dyn_cast<OutputSectionCommand>(Base.get()))
792 if (Cmd->Name == Name)
793 return I;
794 ++I;
795 }
796 return INT_MAX;
George Rimar71b26e92016-04-21 10:22:02 +0000797}
798
Eugene Leviantbbe38602016-07-19 09:25:43 +0000799template <class ELFT> bool LinkerScript<ELFT>::hasPhdrsCommands() {
800 return !Opt.PhdrsCommands.empty();
801}
802
George Rimar9e694502016-07-29 16:18:47 +0000803template <class ELFT>
George Rimar884e7862016-09-08 08:19:13 +0000804uint64_t LinkerScript<ELFT>::getOutputSectionAddress(StringRef Name) {
George Rimar96659df2016-08-30 09:54:01 +0000805 for (OutputSectionBase<ELFT> *Sec : *OutputSections)
806 if (Sec->getName() == Name)
807 return Sec->getVA();
808 error("undefined section " + Name);
809 return 0;
810}
811
812template <class ELFT>
Eugene Leviantb71d6f72016-10-06 09:39:28 +0000813uint64_t LinkerScript<ELFT>::getOutputSectionLMA(StringRef Name) {
814 for (OutputSectionBase<ELFT> *Sec : *OutputSections)
815 if (Sec->getName() == Name)
816 return Sec->getLMA();
817 error("undefined section " + Name);
818 return 0;
819}
820
821template <class ELFT>
George Rimar884e7862016-09-08 08:19:13 +0000822uint64_t LinkerScript<ELFT>::getOutputSectionSize(StringRef Name) {
George Rimar9e694502016-07-29 16:18:47 +0000823 for (OutputSectionBase<ELFT> *Sec : *OutputSections)
824 if (Sec->getName() == Name)
825 return Sec->getSize();
826 error("undefined section " + Name);
827 return 0;
828}
829
Eugene Leviant36fac7f2016-09-08 09:08:30 +0000830template <class ELFT>
831uint64_t LinkerScript<ELFT>::getOutputSectionAlign(StringRef Name) {
832 for (OutputSectionBase<ELFT> *Sec : *OutputSections)
833 if (Sec->getName() == Name)
834 return Sec->getAlignment();
835 error("undefined section " + Name);
836 return 0;
837}
838
George Rimar884e7862016-09-08 08:19:13 +0000839template <class ELFT> uint64_t LinkerScript<ELFT>::getHeaderSize() {
Rafael Espindola0d4b6d52016-09-22 16:47:21 +0000840 return elf::getHeaderSize<ELFT>();
George Rimare32a3592016-08-10 07:59:34 +0000841}
842
George Rimar884e7862016-09-08 08:19:13 +0000843template <class ELFT> uint64_t LinkerScript<ELFT>::getSymbolValue(StringRef S) {
844 if (SymbolBody *B = Symtab<ELFT>::X->find(S))
845 return B->getVA<ELFT>();
846 error("symbol not found: " + S);
847 return 0;
848}
849
George Rimarf34f45f2016-09-23 13:17:23 +0000850template <class ELFT> bool LinkerScript<ELFT>::isDefined(StringRef S) {
851 return Symtab<ELFT>::X->find(S) != nullptr;
852}
853
Eugene Leviantbbe38602016-07-19 09:25:43 +0000854// Returns indices of ELF headers containing specific section, identified
855// by Name. Each index is a zero based number of ELF header listed within
856// PHDRS {} script block.
857template <class ELFT>
Rui Ueyamaedebbdf2016-07-24 23:47:31 +0000858std::vector<size_t> LinkerScript<ELFT>::getPhdrIndices(StringRef SectionName) {
George Rimar076fe152016-07-21 06:43:01 +0000859 for (const std::unique_ptr<BaseCommand> &Base : Opt.Commands) {
860 auto *Cmd = dyn_cast<OutputSectionCommand>(Base.get());
Rui Ueyamaedebbdf2016-07-24 23:47:31 +0000861 if (!Cmd || Cmd->Name != SectionName)
George Rimar31d842f2016-07-20 16:43:03 +0000862 continue;
863
Rui Ueyama29c5a2a2016-07-26 00:27:36 +0000864 std::vector<size_t> Ret;
865 for (StringRef PhdrName : Cmd->Phdrs)
866 Ret.push_back(getPhdrIndex(PhdrName));
867 return Ret;
Eugene Leviantbbe38602016-07-19 09:25:43 +0000868 }
George Rimar31d842f2016-07-20 16:43:03 +0000869 return {};
Eugene Leviantbbe38602016-07-19 09:25:43 +0000870}
871
Rui Ueyama29c5a2a2016-07-26 00:27:36 +0000872template <class ELFT>
873size_t LinkerScript<ELFT>::getPhdrIndex(StringRef PhdrName) {
874 size_t I = 0;
875 for (PhdrsCommand &Cmd : Opt.PhdrsCommands) {
876 if (Cmd.Name == PhdrName)
877 return I;
878 ++I;
879 }
880 error("section header '" + PhdrName + "' is not listed in PHDRS");
881 return 0;
882}
883
Rui Ueyama07320e42016-04-20 20:13:41 +0000884class elf::ScriptParser : public ScriptParserBase {
George Rimarc3794e52016-02-24 09:21:47 +0000885 typedef void (ScriptParser::*Handler)();
886
Rui Ueyamaf7c5fbb2015-09-30 17:23:26 +0000887public:
Rui Ueyama07320e42016-04-20 20:13:41 +0000888 ScriptParser(StringRef S, bool B) : ScriptParserBase(S), IsUnderSysroot(B) {}
George Rimarf23b2322016-02-19 10:45:45 +0000889
George Rimar20b65982016-08-31 09:08:26 +0000890 void readLinkerScript();
891 void readVersionScript();
Rui Ueyamaf7c5fbb2015-09-30 17:23:26 +0000892
893private:
Rui Ueyama52a15092015-10-11 03:28:42 +0000894 void addFile(StringRef Path);
895
Rui Ueyamaf7c5fbb2015-09-30 17:23:26 +0000896 void readAsNeeded();
Denis Protivensky90c50992015-10-08 06:48:38 +0000897 void readEntry();
George Rimar83f406c2015-10-19 17:35:12 +0000898 void readExtern();
Rui Ueyamaf7c5fbb2015-09-30 17:23:26 +0000899 void readGroup();
Rui Ueyama31aa1f82015-10-11 01:31:55 +0000900 void readInclude();
Rui Ueyamaee592822015-10-07 00:25:09 +0000901 void readOutput();
Davide Italiano9159ce92015-10-12 21:50:08 +0000902 void readOutputArch();
Rui Ueyamaf7c5fbb2015-09-30 17:23:26 +0000903 void readOutputFormat();
Eugene Leviantbbe38602016-07-19 09:25:43 +0000904 void readPhdrs();
Davide Italiano68a39a62015-10-08 17:51:41 +0000905 void readSearchDir();
Denis Protivensky8e3b38a2015-11-12 09:52:08 +0000906 void readSections();
Rui Ueyama95769b42016-08-31 20:03:54 +0000907 void readVersion();
908 void readVersionScriptCommand();
Denis Protivensky8e3b38a2015-11-12 09:52:08 +0000909
Rui Ueyama113cdec2016-07-24 23:05:57 +0000910 SymbolAssignment *readAssignment(StringRef Name);
George Rimare38cbab2016-09-26 19:22:50 +0000911 BytesDataCommand *readBytesDataCommand(StringRef Tok);
George Rimarff1f29e2016-09-06 13:51:57 +0000912 std::vector<uint8_t> readFill();
Rui Ueyama10416562016-08-04 02:03:27 +0000913 OutputSectionCommand *readOutputSectionDescription(StringRef OutSec);
George Rimarff1f29e2016-09-06 13:51:57 +0000914 std::vector<uint8_t> readOutputSectionFiller(StringRef Tok);
Eugene Leviantbbe38602016-07-19 09:25:43 +0000915 std::vector<StringRef> readOutputSectionPhdrs();
George Rimara2496cb2016-08-30 09:46:59 +0000916 InputSectionDescription *readInputSectionDescription(StringRef Tok);
George Rimarc91930a2016-09-02 21:17:20 +0000917 Regex readFilePatterns();
George Rimar07171f22016-09-21 15:56:44 +0000918 std::vector<SectionPattern> readInputSectionsList();
George Rimara2496cb2016-08-30 09:46:59 +0000919 InputSectionDescription *readInputSectionRules(StringRef FilePattern);
Eugene Leviantbbe38602016-07-19 09:25:43 +0000920 unsigned readPhdrType();
George Rimarbe394db2016-09-16 20:21:55 +0000921 SortSectionPolicy readSortKind();
Petr Hoseka35e39c2016-08-16 01:11:16 +0000922 SymbolAssignment *readProvideHidden(bool Provide, bool Hidden);
Eugene Leviantdb741e72016-09-07 07:08:43 +0000923 SymbolAssignment *readProvideOrAssignment(StringRef Tok, bool MakeAbsolute);
George Rimar03fc0102016-07-28 07:18:23 +0000924 void readSort();
George Rimareefa7582016-08-04 09:29:31 +0000925 Expr readAssert();
Rui Ueyama708019c2016-07-24 18:19:40 +0000926
927 Expr readExpr();
928 Expr readExpr1(Expr Lhs, int MinPrec);
Eugene Leviantb71d6f72016-10-06 09:39:28 +0000929 StringRef readParenLiteral();
Rui Ueyama708019c2016-07-24 18:19:40 +0000930 Expr readPrimary();
931 Expr readTernary(Expr Cond);
Rui Ueyama6ad7dfc2016-08-17 18:59:16 +0000932 Expr readParenExpr();
Rui Ueyamaf7c5fbb2015-09-30 17:23:26 +0000933
George Rimar20b65982016-08-31 09:08:26 +0000934 // For parsing version script.
935 void readExtern(std::vector<SymbolVersion> *Globals);
Rui Ueyama95769b42016-08-31 20:03:54 +0000936 void readVersionDeclaration(StringRef VerStr);
George Rimar20b65982016-08-31 09:08:26 +0000937 void readGlobal(StringRef VerStr);
938 void readLocal();
939
Rui Ueyama07320e42016-04-20 20:13:41 +0000940 ScriptConfiguration &Opt = *ScriptConfig;
941 StringSaver Saver = {ScriptConfig->Alloc};
Simon Atanasyan16b0cc92015-11-26 05:53:00 +0000942 bool IsUnderSysroot;
Rui Ueyamaf7c5fbb2015-09-30 17:23:26 +0000943};
Rui Ueyamaf7c5fbb2015-09-30 17:23:26 +0000944
George Rimar20b65982016-08-31 09:08:26 +0000945void ScriptParser::readVersionScript() {
Rui Ueyama95769b42016-08-31 20:03:54 +0000946 readVersionScriptCommand();
947 if (!atEOF())
948 setError("EOF expected, but got " + next());
949}
950
951void ScriptParser::readVersionScriptCommand() {
George Rimar20b65982016-08-31 09:08:26 +0000952 if (skip("{")) {
Rui Ueyama95769b42016-08-31 20:03:54 +0000953 readVersionDeclaration("");
George Rimar20b65982016-08-31 09:08:26 +0000954 return;
955 }
956
Rui Ueyama95769b42016-08-31 20:03:54 +0000957 while (!atEOF() && !Error && peek() != "}") {
George Rimar20b65982016-08-31 09:08:26 +0000958 StringRef VerStr = next();
959 if (VerStr == "{") {
Rui Ueyama95769b42016-08-31 20:03:54 +0000960 setError("anonymous version definition is used in "
961 "combination with other version definitions");
George Rimar20b65982016-08-31 09:08:26 +0000962 return;
963 }
964 expect("{");
Rui Ueyama95769b42016-08-31 20:03:54 +0000965 readVersionDeclaration(VerStr);
George Rimar20b65982016-08-31 09:08:26 +0000966 }
967}
968
Rui Ueyama95769b42016-08-31 20:03:54 +0000969void ScriptParser::readVersion() {
970 expect("{");
971 readVersionScriptCommand();
972 expect("}");
973}
974
George Rimar20b65982016-08-31 09:08:26 +0000975void ScriptParser::readLinkerScript() {
Rui Ueyamaf7c5fbb2015-09-30 17:23:26 +0000976 while (!atEOF()) {
977 StringRef Tok = next();
Rui Ueyamaa27eecc2016-09-02 18:52:41 +0000978 if (Tok == ";")
979 continue;
980
Eugene Leviant20d03192016-09-16 15:30:47 +0000981 if (Tok == "ASSERT") {
982 Opt.Commands.emplace_back(new AssertCommand(readAssert()));
983 } else if (Tok == "ENTRY") {
Rui Ueyamaa27eecc2016-09-02 18:52:41 +0000984 readEntry();
985 } else if (Tok == "EXTERN") {
986 readExtern();
987 } else if (Tok == "GROUP" || Tok == "INPUT") {
988 readGroup();
989 } else if (Tok == "INCLUDE") {
990 readInclude();
991 } else if (Tok == "OUTPUT") {
992 readOutput();
993 } else if (Tok == "OUTPUT_ARCH") {
994 readOutputArch();
995 } else if (Tok == "OUTPUT_FORMAT") {
996 readOutputFormat();
997 } else if (Tok == "PHDRS") {
998 readPhdrs();
999 } else if (Tok == "SEARCH_DIR") {
1000 readSearchDir();
1001 } else if (Tok == "SECTIONS") {
1002 readSections();
1003 } else if (Tok == "VERSION") {
1004 readVersion();
Eugene Leviantdb741e72016-09-07 07:08:43 +00001005 } else if (SymbolAssignment *Cmd = readProvideOrAssignment(Tok, true)) {
Eugene Leviant20d03192016-09-16 15:30:47 +00001006 Opt.Commands.emplace_back(Cmd);
Petr Hoseke5d3ca52016-08-31 15:31:17 +00001007 } else {
George Rimar57610422016-03-11 14:43:02 +00001008 setError("unknown directive: " + Tok);
Petr Hoseke5d3ca52016-08-31 15:31:17 +00001009 }
Rui Ueyamaf7c5fbb2015-09-30 17:23:26 +00001010 }
1011}
1012
Rui Ueyama717677a2016-02-11 21:17:59 +00001013void ScriptParser::addFile(StringRef S) {
Simon Atanasyan16b0cc92015-11-26 05:53:00 +00001014 if (IsUnderSysroot && S.startswith("/")) {
1015 SmallString<128> Path;
1016 (Config->Sysroot + S).toStringRef(Path);
1017 if (sys::fs::exists(Path)) {
1018 Driver->addFile(Saver.save(Path.str()));
1019 return;
1020 }
1021 }
1022
Rui Ueyamaf03f3cc2015-10-13 00:09:21 +00001023 if (sys::path::is_absolute(S)) {
Rui Ueyama52a15092015-10-11 03:28:42 +00001024 Driver->addFile(S);
1025 } else if (S.startswith("=")) {
1026 if (Config->Sysroot.empty())
1027 Driver->addFile(S.substr(1));
1028 else
1029 Driver->addFile(Saver.save(Config->Sysroot + "/" + S.substr(1)));
1030 } else if (S.startswith("-l")) {
Rui Ueyama21eecb42016-02-02 21:13:09 +00001031 Driver->addLibrary(S.substr(2));
Simon Atanasyana1b8fc32015-11-26 20:23:46 +00001032 } else if (sys::fs::exists(S)) {
1033 Driver->addFile(S);
Rui Ueyama52a15092015-10-11 03:28:42 +00001034 } else {
1035 std::string Path = findFromSearchPaths(S);
1036 if (Path.empty())
George Rimar777f9632016-03-12 08:31:34 +00001037 setError("unable to find " + S);
Rui Ueyama025d59b2016-02-02 20:27:59 +00001038 else
1039 Driver->addFile(Saver.save(Path));
Rui Ueyama52a15092015-10-11 03:28:42 +00001040 }
1041}
1042
Rui Ueyama717677a2016-02-11 21:17:59 +00001043void ScriptParser::readAsNeeded() {
Rui Ueyamaf7c5fbb2015-09-30 17:23:26 +00001044 expect("(");
Rui Ueyama35da9b62015-10-11 20:59:12 +00001045 bool Orig = Config->AsNeeded;
1046 Config->AsNeeded = true;
Rui Ueyamaa2acc932016-08-05 01:25:45 +00001047 while (!Error && !skip(")"))
George Rimarcd574a52016-09-09 14:35:36 +00001048 addFile(unquote(next()));
Rui Ueyama35da9b62015-10-11 20:59:12 +00001049 Config->AsNeeded = Orig;
Rui Ueyamaf7c5fbb2015-09-30 17:23:26 +00001050}
1051
Rui Ueyama717677a2016-02-11 21:17:59 +00001052void ScriptParser::readEntry() {
Denis Protivensky90c50992015-10-08 06:48:38 +00001053 // -e <symbol> takes predecence over ENTRY(<symbol>).
1054 expect("(");
1055 StringRef Tok = next();
1056 if (Config->Entry.empty())
1057 Config->Entry = Tok;
1058 expect(")");
1059}
1060
Rui Ueyama717677a2016-02-11 21:17:59 +00001061void ScriptParser::readExtern() {
George Rimar83f406c2015-10-19 17:35:12 +00001062 expect("(");
Rui Ueyamaa2acc932016-08-05 01:25:45 +00001063 while (!Error && !skip(")"))
1064 Config->Undefined.push_back(next());
George Rimar83f406c2015-10-19 17:35:12 +00001065}
1066
Rui Ueyama717677a2016-02-11 21:17:59 +00001067void ScriptParser::readGroup() {
Rui Ueyamaf7c5fbb2015-09-30 17:23:26 +00001068 expect("(");
Rui Ueyamaa2acc932016-08-05 01:25:45 +00001069 while (!Error && !skip(")")) {
Rui Ueyamaf7c5fbb2015-09-30 17:23:26 +00001070 StringRef Tok = next();
Rui Ueyamaa2acc932016-08-05 01:25:45 +00001071 if (Tok == "AS_NEEDED")
Rui Ueyamaf7c5fbb2015-09-30 17:23:26 +00001072 readAsNeeded();
Rui Ueyamaa2acc932016-08-05 01:25:45 +00001073 else
George Rimarcd574a52016-09-09 14:35:36 +00001074 addFile(unquote(Tok));
Rui Ueyamaf7c5fbb2015-09-30 17:23:26 +00001075 }
1076}
1077
Rui Ueyama717677a2016-02-11 21:17:59 +00001078void ScriptParser::readInclude() {
Rui Ueyama31aa1f82015-10-11 01:31:55 +00001079 StringRef Tok = next();
George Rimarcd574a52016-09-09 14:35:36 +00001080 auto MBOrErr = MemoryBuffer::getFile(unquote(Tok));
Rui Ueyama025d59b2016-02-02 20:27:59 +00001081 if (!MBOrErr) {
George Rimar57610422016-03-11 14:43:02 +00001082 setError("cannot open " + Tok);
Rui Ueyama025d59b2016-02-02 20:27:59 +00001083 return;
1084 }
Rui Ueyama31aa1f82015-10-11 01:31:55 +00001085 std::unique_ptr<MemoryBuffer> &MB = *MBOrErr;
Rui Ueyamaa47ee682015-10-11 01:53:04 +00001086 StringRef S = Saver.save(MB->getMemBufferRef().getBuffer());
1087 std::vector<StringRef> V = tokenize(S);
Rui Ueyama31aa1f82015-10-11 01:31:55 +00001088 Tokens.insert(Tokens.begin() + Pos, V.begin(), V.end());
Rui Ueyama31aa1f82015-10-11 01:31:55 +00001089}
1090
Rui Ueyama717677a2016-02-11 21:17:59 +00001091void ScriptParser::readOutput() {
Rui Ueyamaee592822015-10-07 00:25:09 +00001092 // -o <file> takes predecence over OUTPUT(<file>).
1093 expect("(");
1094 StringRef Tok = next();
1095 if (Config->OutputFile.empty())
George Rimarcd574a52016-09-09 14:35:36 +00001096 Config->OutputFile = unquote(Tok);
Rui Ueyamaee592822015-10-07 00:25:09 +00001097 expect(")");
1098}
1099
Rui Ueyama717677a2016-02-11 21:17:59 +00001100void ScriptParser::readOutputArch() {
Davide Italiano9159ce92015-10-12 21:50:08 +00001101 // Error checking only for now.
1102 expect("(");
1103 next();
1104 expect(")");
1105}
1106
Rui Ueyama717677a2016-02-11 21:17:59 +00001107void ScriptParser::readOutputFormat() {
Rui Ueyamaf7c5fbb2015-09-30 17:23:26 +00001108 // Error checking only for now.
1109 expect("(");
1110 next();
Davide Italiano6836c612015-10-12 21:08:41 +00001111 StringRef Tok = next();
1112 if (Tok == ")")
George Rimar6c55f0e2016-09-08 08:20:30 +00001113 return;
Rui Ueyama025d59b2016-02-02 20:27:59 +00001114 if (Tok != ",") {
George Rimar57610422016-03-11 14:43:02 +00001115 setError("unexpected token: " + Tok);
Rui Ueyama025d59b2016-02-02 20:27:59 +00001116 return;
1117 }
Davide Italiano6836c612015-10-12 21:08:41 +00001118 next();
1119 expect(",");
1120 next();
Rui Ueyamaf7c5fbb2015-09-30 17:23:26 +00001121 expect(")");
1122}
1123
Eugene Leviantbbe38602016-07-19 09:25:43 +00001124void ScriptParser::readPhdrs() {
1125 expect("{");
1126 while (!Error && !skip("}")) {
1127 StringRef Tok = next();
Eugene Leviant56b21c82016-09-09 09:46:16 +00001128 Opt.PhdrsCommands.push_back(
1129 {Tok, PT_NULL, false, false, UINT_MAX, nullptr});
Eugene Leviantbbe38602016-07-19 09:25:43 +00001130 PhdrsCommand &PhdrCmd = Opt.PhdrsCommands.back();
1131
1132 PhdrCmd.Type = readPhdrType();
1133 do {
1134 Tok = next();
1135 if (Tok == ";")
1136 break;
1137 if (Tok == "FILEHDR")
1138 PhdrCmd.HasFilehdr = true;
1139 else if (Tok == "PHDRS")
1140 PhdrCmd.HasPhdrs = true;
Eugene Leviant56b21c82016-09-09 09:46:16 +00001141 else if (Tok == "AT")
1142 PhdrCmd.LMAExpr = readParenExpr();
Eugene Leviant865bf862016-07-21 10:43:25 +00001143 else if (Tok == "FLAGS") {
1144 expect("(");
Rafael Espindolaeb685cd2016-08-02 22:14:57 +00001145 // Passing 0 for the value of dot is a bit of a hack. It means that
1146 // we accept expressions like ".|1".
1147 PhdrCmd.Flags = readExpr()(0);
Eugene Leviant865bf862016-07-21 10:43:25 +00001148 expect(")");
1149 } else
Eugene Leviantbbe38602016-07-19 09:25:43 +00001150 setError("unexpected header attribute: " + Tok);
1151 } while (!Error);
1152 }
1153}
1154
Rui Ueyama717677a2016-02-11 21:17:59 +00001155void ScriptParser::readSearchDir() {
Davide Italiano68a39a62015-10-08 17:51:41 +00001156 expect("(");
Rui Ueyama86c5fb82016-09-08 23:26:54 +00001157 StringRef Tok = next();
Rui Ueyama6c7ad132016-09-02 19:20:33 +00001158 if (!Config->Nostdlib)
George Rimarcd574a52016-09-09 14:35:36 +00001159 Config->SearchPaths.push_back(unquote(Tok));
Davide Italiano68a39a62015-10-08 17:51:41 +00001160 expect(")");
1161}
1162
Rui Ueyama717677a2016-02-11 21:17:59 +00001163void ScriptParser::readSections() {
Eugene Leviante05336ff2016-09-14 08:32:36 +00001164 Opt.HasSections = true;
Denis Protivensky8e3b38a2015-11-12 09:52:08 +00001165 expect("{");
George Rimar652852c2016-04-16 10:10:32 +00001166 while (!Error && !skip("}")) {
Rui Ueyama113cdec2016-07-24 23:05:57 +00001167 StringRef Tok = next();
Eugene Leviantdb741e72016-09-07 07:08:43 +00001168 BaseCommand *Cmd = readProvideOrAssignment(Tok, true);
Eugene Leviantceabe802016-08-11 07:56:43 +00001169 if (!Cmd) {
1170 if (Tok == "ASSERT")
1171 Cmd = new AssertCommand(readAssert());
1172 else
1173 Cmd = readOutputSectionDescription(Tok);
Rui Ueyama708019c2016-07-24 18:19:40 +00001174 }
Rui Ueyama10416562016-08-04 02:03:27 +00001175 Opt.Commands.emplace_back(Cmd);
George Rimar652852c2016-04-16 10:10:32 +00001176 }
Denis Protivensky8e3b38a2015-11-12 09:52:08 +00001177}
1178
Rui Ueyama708019c2016-07-24 18:19:40 +00001179static int precedence(StringRef Op) {
1180 return StringSwitch<int>(Op)
Rui Ueyama0120e3f2016-09-23 18:06:51 +00001181 .Cases("*", "/", 5)
1182 .Cases("+", "-", 4)
1183 .Cases("<<", ">>", 3)
Rui Ueyama9c4ac5f2016-09-23 22:22:34 +00001184 .Cases("<", "<=", ">", ">=", "==", "!=", 2)
Rui Ueyama0120e3f2016-09-23 18:06:51 +00001185 .Cases("&", "|", 1)
Rui Ueyama708019c2016-07-24 18:19:40 +00001186 .Default(-1);
1187}
1188
George Rimarc91930a2016-09-02 21:17:20 +00001189Regex ScriptParser::readFilePatterns() {
Rui Ueyama10416562016-08-04 02:03:27 +00001190 std::vector<StringRef> V;
1191 while (!Error && !skip(")"))
1192 V.push_back(next());
George Rimarc91930a2016-09-02 21:17:20 +00001193 return compileGlobPatterns(V);
George Rimar0702c4e2016-07-29 15:32:46 +00001194}
1195
George Rimarbe394db2016-09-16 20:21:55 +00001196SortSectionPolicy ScriptParser::readSortKind() {
Rui Ueyama742c3832016-08-04 22:27:00 +00001197 if (skip("SORT") || skip("SORT_BY_NAME"))
George Rimarbe394db2016-09-16 20:21:55 +00001198 return SortSectionPolicy::Name;
Rui Ueyama742c3832016-08-04 22:27:00 +00001199 if (skip("SORT_BY_ALIGNMENT"))
George Rimarbe394db2016-09-16 20:21:55 +00001200 return SortSectionPolicy::Alignment;
George Rimar575208c2016-09-15 19:15:12 +00001201 if (skip("SORT_BY_INIT_PRIORITY"))
George Rimarbe394db2016-09-16 20:21:55 +00001202 return SortSectionPolicy::Priority;
George Rimarbe394db2016-09-16 20:21:55 +00001203 if (skip("SORT_NONE"))
Rui Ueyamab2a0abd2016-09-16 21:14:55 +00001204 return SortSectionPolicy::None;
1205 return SortSectionPolicy::Default;
George Rimarbe394db2016-09-16 20:21:55 +00001206}
1207
George Rimar395281c2016-09-16 17:42:10 +00001208// Method reads a list of sequence of excluded files and section globs given in
1209// a following form: ((EXCLUDE_FILE(file_pattern+))? section_pattern+)+
1210// Example: *(.foo.1 EXCLUDE_FILE (*a.o) .foo.2 EXCLUDE_FILE (*b.o) .foo.3)
George Rimaraf03be12016-09-17 19:17:25 +00001211// The semantics of that is next:
1212// * Include .foo.1 from every file.
1213// * Include .foo.2 from every file but a.o
1214// * Include .foo.3 from every file but b.o
George Rimar07171f22016-09-21 15:56:44 +00001215std::vector<SectionPattern> ScriptParser::readInputSectionsList() {
1216 std::vector<SectionPattern> Ret;
George Rimar601e9892016-09-21 08:53:21 +00001217 while (!Error && peek() != ")") {
1218 Regex ExcludeFileRe;
George Rimar395281c2016-09-16 17:42:10 +00001219 if (skip("EXCLUDE_FILE")) {
George Rimar395281c2016-09-16 17:42:10 +00001220 expect("(");
1221 ExcludeFileRe = readFilePatterns();
George Rimar395281c2016-09-16 17:42:10 +00001222 }
1223
George Rimar601e9892016-09-21 08:53:21 +00001224 std::vector<StringRef> V;
1225 while (!Error && peek() != ")" && peek() != "EXCLUDE_FILE")
1226 V.push_back(next());
1227
1228 if (!V.empty())
George Rimar07171f22016-09-21 15:56:44 +00001229 Ret.push_back({std::move(ExcludeFileRe), compileGlobPatterns(V)});
George Rimar601e9892016-09-21 08:53:21 +00001230 else
1231 setError("section pattern is expected");
George Rimar395281c2016-09-16 17:42:10 +00001232 }
George Rimar07171f22016-09-21 15:56:44 +00001233 return Ret;
George Rimar395281c2016-09-16 17:42:10 +00001234}
1235
George Rimar07171f22016-09-21 15:56:44 +00001236// Section pattern grammar can have complex expressions, for example:
1237// *(SORT(.foo.* EXCLUDE_FILE (*file1.o) .bar.*) .bar.* SORT(.zed.*))
1238// Generally is a sequence of globs and excludes that may be wrapped in a SORT()
1239// commands, like: SORT(glob0) glob1 glob2 SORT(glob4)
1240// This methods handles wrapping sequences of excluded files and section globs
1241// into SORT() if that needed and reads them all.
George Rimara2496cb2016-08-30 09:46:59 +00001242InputSectionDescription *
1243ScriptParser::readInputSectionRules(StringRef FilePattern) {
George Rimarc91930a2016-09-02 21:17:20 +00001244 auto *Cmd = new InputSectionDescription(FilePattern);
Davide Italiano0ed42b02016-07-25 21:47:13 +00001245 expect("(");
George Rimar07171f22016-09-21 15:56:44 +00001246 while (!HasError && !skip(")")) {
1247 SortSectionPolicy Outer = readSortKind();
1248 SortSectionPolicy Inner = SortSectionPolicy::Default;
1249 std::vector<SectionPattern> V;
1250 if (Outer != SortSectionPolicy::Default) {
George Rimar350ece42016-08-03 08:35:59 +00001251 expect("(");
George Rimar07171f22016-09-21 15:56:44 +00001252 Inner = readSortKind();
1253 if (Inner != SortSectionPolicy::Default) {
1254 expect("(");
1255 V = readInputSectionsList();
1256 expect(")");
1257 } else {
1258 V = readInputSectionsList();
1259 }
George Rimar350ece42016-08-03 08:35:59 +00001260 expect(")");
1261 } else {
George Rimar07171f22016-09-21 15:56:44 +00001262 V = readInputSectionsList();
George Rimar350ece42016-08-03 08:35:59 +00001263 }
George Rimar0702c4e2016-07-29 15:32:46 +00001264
George Rimar07171f22016-09-21 15:56:44 +00001265 for (SectionPattern &Pat : V) {
1266 Pat.SortInner = Inner;
1267 Pat.SortOuter = Outer;
1268 }
1269
1270 std::move(V.begin(), V.end(), std::back_inserter(Cmd->SectionPatterns));
1271 }
Rui Ueyama10416562016-08-04 02:03:27 +00001272 return Cmd;
Davide Italianoe7282792016-07-27 01:44:01 +00001273}
1274
George Rimara2496cb2016-08-30 09:46:59 +00001275InputSectionDescription *
1276ScriptParser::readInputSectionDescription(StringRef Tok) {
George Rimar06598002016-07-28 21:51:30 +00001277 // Input section wildcard can be surrounded by KEEP.
1278 // https://sourceware.org/binutils/docs/ld/Input-Section-Keep.html#Input-Section-Keep
George Rimara2496cb2016-08-30 09:46:59 +00001279 if (Tok == "KEEP") {
George Rimar06598002016-07-28 21:51:30 +00001280 expect("(");
George Rimara2496cb2016-08-30 09:46:59 +00001281 StringRef FilePattern = next();
1282 InputSectionDescription *Cmd = readInputSectionRules(FilePattern);
George Rimar06598002016-07-28 21:51:30 +00001283 expect(")");
Eugene Leviantcf43f172016-10-05 09:36:59 +00001284 Opt.KeptSections.push_back(Cmd);
Rui Ueyama10416562016-08-04 02:03:27 +00001285 return Cmd;
George Rimar06598002016-07-28 21:51:30 +00001286 }
George Rimara2496cb2016-08-30 09:46:59 +00001287 return readInputSectionRules(Tok);
Davide Italiano0ed42b02016-07-25 21:47:13 +00001288}
1289
George Rimar03fc0102016-07-28 07:18:23 +00001290void ScriptParser::readSort() {
1291 expect("(");
1292 expect("CONSTRUCTORS");
1293 expect(")");
1294}
1295
George Rimareefa7582016-08-04 09:29:31 +00001296Expr ScriptParser::readAssert() {
1297 expect("(");
1298 Expr E = readExpr();
1299 expect(",");
George Rimarcd574a52016-09-09 14:35:36 +00001300 StringRef Msg = unquote(next());
George Rimareefa7582016-08-04 09:29:31 +00001301 expect(")");
1302 return [=](uint64_t Dot) {
1303 uint64_t V = E(Dot);
1304 if (!V)
1305 error(Msg);
1306 return V;
1307 };
1308}
1309
Rui Ueyama25150e82016-09-06 17:46:43 +00001310// Reads a FILL(expr) command. We handle the FILL command as an
1311// alias for =fillexp section attribute, which is different from
1312// what GNU linkers do.
1313// https://sourceware.org/binutils/docs/ld/Output-Section-Data.html
George Rimarff1f29e2016-09-06 13:51:57 +00001314std::vector<uint8_t> ScriptParser::readFill() {
1315 expect("(");
1316 std::vector<uint8_t> V = readOutputSectionFiller(next());
1317 expect(")");
1318 expect(";");
1319 return V;
1320}
1321
Rui Ueyama10416562016-08-04 02:03:27 +00001322OutputSectionCommand *
1323ScriptParser::readOutputSectionDescription(StringRef OutSec) {
George Rimar076fe152016-07-21 06:43:01 +00001324 OutputSectionCommand *Cmd = new OutputSectionCommand(OutSec);
George Rimar58e5c4d2016-07-25 08:29:46 +00001325
1326 // Read an address expression.
1327 // https://sourceware.org/binutils/docs/ld/Output-Section-Address.html#Output-Section-Address
1328 if (peek() != ":")
1329 Cmd->AddrExpr = readExpr();
1330
Denis Protivensky8e3b38a2015-11-12 09:52:08 +00001331 expect(":");
Davide Italiano246f6812016-07-22 03:36:24 +00001332
George Rimar8ceadb32016-08-17 07:44:19 +00001333 if (skip("AT"))
Eugene Leviantb71d6f72016-10-06 09:39:28 +00001334 Cmd->LMAExpr = readParenExpr();
George Rimar630c6172016-07-26 18:06:29 +00001335 if (skip("ALIGN"))
Rui Ueyama6ad7dfc2016-08-17 18:59:16 +00001336 Cmd->AlignExpr = readParenExpr();
George Rimardb24d9c2016-08-19 15:18:23 +00001337 if (skip("SUBALIGN"))
1338 Cmd->SubalignExpr = readParenExpr();
George Rimar630c6172016-07-26 18:06:29 +00001339
Davide Italiano246f6812016-07-22 03:36:24 +00001340 // Parse constraints.
1341 if (skip("ONLY_IF_RO"))
Rui Ueyamaefc40662016-07-25 22:00:10 +00001342 Cmd->Constraint = ConstraintKind::ReadOnly;
Davide Italiano246f6812016-07-22 03:36:24 +00001343 if (skip("ONLY_IF_RW"))
Rui Ueyamaefc40662016-07-25 22:00:10 +00001344 Cmd->Constraint = ConstraintKind::ReadWrite;
Denis Protivensky8e3b38a2015-11-12 09:52:08 +00001345 expect("{");
Rui Ueyama8ec77e62016-04-21 22:00:51 +00001346
Rui Ueyama025d59b2016-02-02 20:27:59 +00001347 while (!Error && !skip("}")) {
Eugene Leviantceabe802016-08-11 07:56:43 +00001348 StringRef Tok = next();
Eugene Leviantdb741e72016-09-07 07:08:43 +00001349 if (SymbolAssignment *Assignment = readProvideOrAssignment(Tok, false))
Eugene Leviantceabe802016-08-11 07:56:43 +00001350 Cmd->Commands.emplace_back(Assignment);
George Rimare38cbab2016-09-26 19:22:50 +00001351 else if (BytesDataCommand *Data = readBytesDataCommand(Tok))
1352 Cmd->Commands.emplace_back(Data);
George Rimarff1f29e2016-09-06 13:51:57 +00001353 else if (Tok == "FILL")
1354 Cmd->Filler = readFill();
Eugene Leviantceabe802016-08-11 07:56:43 +00001355 else if (Tok == "SORT")
George Rimar03fc0102016-07-28 07:18:23 +00001356 readSort();
George Rimara2496cb2016-08-30 09:46:59 +00001357 else if (peek() == "(")
1358 Cmd->Commands.emplace_back(readInputSectionDescription(Tok));
Eugene Leviantceabe802016-08-11 07:56:43 +00001359 else
1360 setError("unknown command " + Tok);
Denis Protivensky8e3b38a2015-11-12 09:52:08 +00001361 }
George Rimar076fe152016-07-21 06:43:01 +00001362 Cmd->Phdrs = readOutputSectionPhdrs();
George Rimar4ebc5622016-09-23 13:29:20 +00001363
1364 if (skip("="))
1365 Cmd->Filler = readOutputSectionFiller(next());
1366 else if (peek().startswith("="))
George Rimarff1f29e2016-09-06 13:51:57 +00001367 Cmd->Filler = readOutputSectionFiller(next().drop_front());
George Rimar4ebc5622016-09-23 13:29:20 +00001368
Rui Ueyama10416562016-08-04 02:03:27 +00001369 return Cmd;
Rui Ueyamaf71caa22016-07-29 06:14:07 +00001370}
Rui Ueyama8ec77e62016-04-21 22:00:51 +00001371
Rui Ueyama2c8f1f02016-08-29 22:01:21 +00001372// Read "=<number>" where <number> is an octal/decimal/hexadecimal number.
1373// https://sourceware.org/binutils/docs/ld/Output-Section-Fill.html
1374//
1375// ld.gold is not fully compatible with ld.bfd. ld.bfd handles
1376// hexstrings as blobs of arbitrary sizes, while ld.gold handles them
1377// as 32-bit big-endian values. We will do the same as ld.gold does
1378// because it's simpler than what ld.bfd does.
George Rimarff1f29e2016-09-06 13:51:57 +00001379std::vector<uint8_t> ScriptParser::readOutputSectionFiller(StringRef Tok) {
Rui Ueyama965827d2016-08-03 23:25:15 +00001380 uint32_t V;
George Rimarff1f29e2016-09-06 13:51:57 +00001381 if (Tok.getAsInteger(0, V)) {
Rui Ueyama965827d2016-08-03 23:25:15 +00001382 setError("invalid filler expression: " + Tok);
Rui Ueyamaf71caa22016-07-29 06:14:07 +00001383 return {};
George Rimare2ee72b2016-02-26 14:48:31 +00001384 }
Rui Ueyama2c8f1f02016-08-29 22:01:21 +00001385 return {uint8_t(V >> 24), uint8_t(V >> 16), uint8_t(V >> 8), uint8_t(V)};
Denis Protivensky8e3b38a2015-11-12 09:52:08 +00001386}
1387
Petr Hoseka35e39c2016-08-16 01:11:16 +00001388SymbolAssignment *ScriptParser::readProvideHidden(bool Provide, bool Hidden) {
Eugene Levianta31c91b2016-07-22 07:38:40 +00001389 expect("(");
Rui Ueyama174e0a12016-07-29 00:29:25 +00001390 SymbolAssignment *Cmd = readAssignment(next());
Petr Hoseka35e39c2016-08-16 01:11:16 +00001391 Cmd->Provide = Provide;
Rui Ueyama174e0a12016-07-29 00:29:25 +00001392 Cmd->Hidden = Hidden;
Eugene Levianta31c91b2016-07-22 07:38:40 +00001393 expect(")");
1394 expect(";");
Rui Ueyama10416562016-08-04 02:03:27 +00001395 return Cmd;
Eugene Levianteda81a12016-07-12 06:39:48 +00001396}
1397
Eugene Leviantdb741e72016-09-07 07:08:43 +00001398SymbolAssignment *ScriptParser::readProvideOrAssignment(StringRef Tok,
1399 bool MakeAbsolute) {
Eugene Leviantceabe802016-08-11 07:56:43 +00001400 SymbolAssignment *Cmd = nullptr;
1401 if (peek() == "=" || peek() == "+=") {
1402 Cmd = readAssignment(Tok);
1403 expect(";");
1404 } else if (Tok == "PROVIDE") {
Petr Hoseka35e39c2016-08-16 01:11:16 +00001405 Cmd = readProvideHidden(true, false);
1406 } else if (Tok == "HIDDEN") {
1407 Cmd = readProvideHidden(false, true);
Eugene Leviantceabe802016-08-11 07:56:43 +00001408 } else if (Tok == "PROVIDE_HIDDEN") {
Petr Hoseka35e39c2016-08-16 01:11:16 +00001409 Cmd = readProvideHidden(true, true);
Eugene Leviantceabe802016-08-11 07:56:43 +00001410 }
Eugene Leviantdb741e72016-09-07 07:08:43 +00001411 if (Cmd && MakeAbsolute)
1412 Cmd->IsAbsolute = true;
Eugene Leviantceabe802016-08-11 07:56:43 +00001413 return Cmd;
1414}
1415
George Rimar30835ea2016-07-28 21:08:56 +00001416static uint64_t getSymbolValue(StringRef S, uint64_t Dot) {
1417 if (S == ".")
1418 return Dot;
George Rimar884e7862016-09-08 08:19:13 +00001419 return ScriptBase->getSymbolValue(S);
George Rimare32a3592016-08-10 07:59:34 +00001420}
1421
George Rimar30835ea2016-07-28 21:08:56 +00001422SymbolAssignment *ScriptParser::readAssignment(StringRef Name) {
1423 StringRef Op = next();
Eugene Leviantdb741e72016-09-07 07:08:43 +00001424 bool IsAbsolute = false;
1425 Expr E;
George Rimar30835ea2016-07-28 21:08:56 +00001426 assert(Op == "=" || Op == "+=");
Eugene Leviantdb741e72016-09-07 07:08:43 +00001427 if (skip("ABSOLUTE")) {
1428 E = readParenExpr();
1429 IsAbsolute = true;
1430 } else {
1431 E = readExpr();
1432 }
George Rimar30835ea2016-07-28 21:08:56 +00001433 if (Op == "+=")
1434 E = [=](uint64_t Dot) { return getSymbolValue(Name, Dot) + E(Dot); };
Eugene Leviantdb741e72016-09-07 07:08:43 +00001435 return new SymbolAssignment(Name, E, IsAbsolute);
George Rimar30835ea2016-07-28 21:08:56 +00001436}
1437
1438// This is an operator-precedence parser to parse a linker
1439// script expression.
1440Expr ScriptParser::readExpr() { return readExpr1(readPrimary(), 0); }
1441
Rui Ueyama36c1cd22016-08-05 01:04:59 +00001442static Expr combine(StringRef Op, Expr L, Expr R) {
1443 if (Op == "*")
1444 return [=](uint64_t Dot) { return L(Dot) * R(Dot); };
1445 if (Op == "/") {
1446 return [=](uint64_t Dot) -> uint64_t {
1447 uint64_t RHS = R(Dot);
1448 if (RHS == 0) {
1449 error("division by zero");
1450 return 0;
1451 }
1452 return L(Dot) / RHS;
1453 };
1454 }
1455 if (Op == "+")
1456 return [=](uint64_t Dot) { return L(Dot) + R(Dot); };
1457 if (Op == "-")
1458 return [=](uint64_t Dot) { return L(Dot) - R(Dot); };
George Rimarc8ccd1f2016-09-23 13:13:55 +00001459 if (Op == "<<")
1460 return [=](uint64_t Dot) { return L(Dot) << R(Dot); };
1461 if (Op == ">>")
1462 return [=](uint64_t Dot) { return L(Dot) >> R(Dot); };
Rui Ueyama36c1cd22016-08-05 01:04:59 +00001463 if (Op == "<")
1464 return [=](uint64_t Dot) { return L(Dot) < R(Dot); };
1465 if (Op == ">")
1466 return [=](uint64_t Dot) { return L(Dot) > R(Dot); };
1467 if (Op == ">=")
1468 return [=](uint64_t Dot) { return L(Dot) >= R(Dot); };
1469 if (Op == "<=")
1470 return [=](uint64_t Dot) { return L(Dot) <= R(Dot); };
1471 if (Op == "==")
1472 return [=](uint64_t Dot) { return L(Dot) == R(Dot); };
1473 if (Op == "!=")
1474 return [=](uint64_t Dot) { return L(Dot) != R(Dot); };
1475 if (Op == "&")
1476 return [=](uint64_t Dot) { return L(Dot) & R(Dot); };
Rafael Espindolacc3dd622016-08-22 21:33:35 +00001477 if (Op == "|")
1478 return [=](uint64_t Dot) { return L(Dot) | R(Dot); };
Rui Ueyama36c1cd22016-08-05 01:04:59 +00001479 llvm_unreachable("invalid operator");
1480}
1481
Rui Ueyama708019c2016-07-24 18:19:40 +00001482// This is a part of the operator-precedence parser. This function
1483// assumes that the remaining token stream starts with an operator.
1484Expr ScriptParser::readExpr1(Expr Lhs, int MinPrec) {
1485 while (!atEOF() && !Error) {
1486 // Read an operator and an expression.
1487 StringRef Op1 = peek();
1488 if (Op1 == "?")
1489 return readTernary(Lhs);
1490 if (precedence(Op1) < MinPrec)
Eugene Levianteda81a12016-07-12 06:39:48 +00001491 break;
Rui Ueyama708019c2016-07-24 18:19:40 +00001492 next();
1493 Expr Rhs = readPrimary();
1494
1495 // Evaluate the remaining part of the expression first if the
1496 // next operator has greater precedence than the previous one.
1497 // For example, if we have read "+" and "3", and if the next
1498 // operator is "*", then we'll evaluate 3 * ... part first.
1499 while (!atEOF()) {
1500 StringRef Op2 = peek();
1501 if (precedence(Op2) <= precedence(Op1))
1502 break;
1503 Rhs = readExpr1(Rhs, precedence(Op2));
1504 }
1505
1506 Lhs = combine(Op1, Lhs, Rhs);
Eugene Levianteda81a12016-07-12 06:39:48 +00001507 }
Rui Ueyama708019c2016-07-24 18:19:40 +00001508 return Lhs;
1509}
1510
1511uint64_t static getConstant(StringRef S) {
Michael J. Spencere2cc07b2016-08-17 02:10:51 +00001512 if (S == "COMMONPAGESIZE")
Rui Ueyama708019c2016-07-24 18:19:40 +00001513 return Target->PageSize;
Michael J. Spencere2cc07b2016-08-17 02:10:51 +00001514 if (S == "MAXPAGESIZE")
Petr Hosek997f8832016-09-28 15:20:47 +00001515 return Config->MaxPageSize;
Rui Ueyama708019c2016-07-24 18:19:40 +00001516 error("unknown constant: " + S);
1517 return 0;
1518}
1519
Rui Ueyama626e0b02016-09-02 18:19:00 +00001520// Parses Tok as an integer. Returns true if successful.
1521// It recognizes hexadecimal (prefixed with "0x" or suffixed with "H")
1522// and decimal numbers. Decimal numbers may have "K" (kilo) or
1523// "M" (mega) prefixes.
George Rimar9f2f7ad2016-09-02 16:01:42 +00001524static bool readInteger(StringRef Tok, uint64_t &Result) {
Simon Atanasyaneaeafb22016-09-02 21:54:35 +00001525 if (Tok.startswith("-")) {
1526 if (!readInteger(Tok.substr(1), Result))
1527 return false;
1528 Result = -Result;
1529 return true;
1530 }
George Rimar9f2f7ad2016-09-02 16:01:42 +00001531 if (Tok.startswith_lower("0x"))
1532 return !Tok.substr(2).getAsInteger(16, Result);
1533 if (Tok.endswith_lower("H"))
1534 return !Tok.drop_back().getAsInteger(16, Result);
1535
1536 int Suffix = 1;
1537 if (Tok.endswith_lower("K")) {
1538 Suffix = 1024;
1539 Tok = Tok.drop_back();
1540 } else if (Tok.endswith_lower("M")) {
1541 Suffix = 1024 * 1024;
1542 Tok = Tok.drop_back();
1543 }
1544 if (Tok.getAsInteger(10, Result))
1545 return false;
1546 Result *= Suffix;
1547 return true;
1548}
1549
George Rimare38cbab2016-09-26 19:22:50 +00001550BytesDataCommand *ScriptParser::readBytesDataCommand(StringRef Tok) {
1551 int Size = StringSwitch<unsigned>(Tok)
1552 .Case("BYTE", 1)
1553 .Case("SHORT", 2)
1554 .Case("LONG", 4)
1555 .Case("QUAD", 8)
1556 .Default(-1);
1557 if (Size == -1)
1558 return nullptr;
1559
1560 expect("(");
1561 uint64_t Val = 0;
1562 StringRef S = next();
1563 if (!readInteger(S, Val))
1564 setError("unexpected value: " + S);
1565 expect(")");
1566 return new BytesDataCommand(Val, Size);
1567}
1568
Eugene Leviantb71d6f72016-10-06 09:39:28 +00001569StringRef ScriptParser::readParenLiteral() {
1570 expect("(");
1571 StringRef Tok = next();
1572 expect(")");
1573 return Tok;
1574}
1575
Rui Ueyama708019c2016-07-24 18:19:40 +00001576Expr ScriptParser::readPrimary() {
Rui Ueyama6ad7dfc2016-08-17 18:59:16 +00001577 if (peek() == "(")
1578 return readParenExpr();
Rui Ueyama708019c2016-07-24 18:19:40 +00001579
Rui Ueyama6ad7dfc2016-08-17 18:59:16 +00001580 StringRef Tok = next();
Rui Ueyama708019c2016-07-24 18:19:40 +00001581
Simon Atanasyaneaeafb22016-09-02 21:54:35 +00001582 if (Tok == "~") {
1583 Expr E = readPrimary();
1584 return [=](uint64_t Dot) { return ~E(Dot); };
1585 }
1586 if (Tok == "-") {
1587 Expr E = readPrimary();
1588 return [=](uint64_t Dot) { return -E(Dot); };
1589 }
1590
Rui Ueyama708019c2016-07-24 18:19:40 +00001591 // Built-in functions are parsed here.
1592 // https://sourceware.org/binutils/docs/ld/Builtin-Functions.html.
George Rimar96659df2016-08-30 09:54:01 +00001593 if (Tok == "ADDR") {
Eugene Leviantb71d6f72016-10-06 09:39:28 +00001594 StringRef Name = readParenLiteral();
George Rimar884e7862016-09-08 08:19:13 +00001595 return
1596 [=](uint64_t Dot) { return ScriptBase->getOutputSectionAddress(Name); };
George Rimar96659df2016-08-30 09:54:01 +00001597 }
Eugene Leviantb71d6f72016-10-06 09:39:28 +00001598 if (Tok == "LOADADDR") {
1599 StringRef Name = readParenLiteral();
1600 return [=](uint64_t Dot) { return ScriptBase->getOutputSectionLMA(Name); };
1601 }
George Rimareefa7582016-08-04 09:29:31 +00001602 if (Tok == "ASSERT")
1603 return readAssert();
Rui Ueyama708019c2016-07-24 18:19:40 +00001604 if (Tok == "ALIGN") {
Rui Ueyama6ad7dfc2016-08-17 18:59:16 +00001605 Expr E = readParenExpr();
Rui Ueyama708019c2016-07-24 18:19:40 +00001606 return [=](uint64_t Dot) { return alignTo(Dot, E(Dot)); };
1607 }
1608 if (Tok == "CONSTANT") {
Eugene Leviantb71d6f72016-10-06 09:39:28 +00001609 StringRef Name = readParenLiteral();
1610 return [=](uint64_t Dot) { return getConstant(Name); };
Rui Ueyama708019c2016-07-24 18:19:40 +00001611 }
George Rimarf34f45f2016-09-23 13:17:23 +00001612 if (Tok == "DEFINED") {
1613 expect("(");
1614 StringRef Tok = next();
1615 expect(")");
George Rimarf2821022016-09-26 11:00:48 +00001616 return [=](uint64_t Dot) { return ScriptBase->isDefined(Tok) ? 1 : 0; };
George Rimarf34f45f2016-09-23 13:17:23 +00001617 }
Rafael Espindola54c145c2016-07-28 18:16:24 +00001618 if (Tok == "SEGMENT_START") {
1619 expect("(");
1620 next();
1621 expect(",");
George Rimar8c658bf2016-09-17 18:14:56 +00001622 Expr E = readExpr();
Rafael Espindola54c145c2016-07-28 18:16:24 +00001623 expect(")");
George Rimar8c658bf2016-09-17 18:14:56 +00001624 return [=](uint64_t Dot) { return E(Dot); };
Rafael Espindola54c145c2016-07-28 18:16:24 +00001625 }
Rui Ueyama708019c2016-07-24 18:19:40 +00001626 if (Tok == "DATA_SEGMENT_ALIGN") {
1627 expect("(");
1628 Expr E = readExpr();
1629 expect(",");
1630 readExpr();
1631 expect(")");
Rui Ueyamaf7791bb2016-07-26 19:34:10 +00001632 return [=](uint64_t Dot) { return alignTo(Dot, E(Dot)); };
Rui Ueyama708019c2016-07-24 18:19:40 +00001633 }
1634 if (Tok == "DATA_SEGMENT_END") {
1635 expect("(");
1636 expect(".");
1637 expect(")");
1638 return [](uint64_t Dot) { return Dot; };
1639 }
George Rimar276b4e62016-07-26 17:58:44 +00001640 // GNU linkers implements more complicated logic to handle
1641 // DATA_SEGMENT_RELRO_END. We instead ignore the arguments and just align to
1642 // the next page boundary for simplicity.
1643 if (Tok == "DATA_SEGMENT_RELRO_END") {
1644 expect("(");
Rafael Espindola97bdc722016-09-14 19:14:01 +00001645 readExpr();
George Rimar276b4e62016-07-26 17:58:44 +00001646 expect(",");
1647 readExpr();
1648 expect(")");
1649 return [](uint64_t Dot) { return alignTo(Dot, Target->PageSize); };
1650 }
George Rimar9e694502016-07-29 16:18:47 +00001651 if (Tok == "SIZEOF") {
Eugene Leviantb71d6f72016-10-06 09:39:28 +00001652 StringRef Name = readParenLiteral();
George Rimar884e7862016-09-08 08:19:13 +00001653 return [=](uint64_t Dot) { return ScriptBase->getOutputSectionSize(Name); };
George Rimar9e694502016-07-29 16:18:47 +00001654 }
Eugene Leviant36fac7f2016-09-08 09:08:30 +00001655 if (Tok == "ALIGNOF") {
Eugene Leviantb71d6f72016-10-06 09:39:28 +00001656 StringRef Name = readParenLiteral();
Eugene Leviant36fac7f2016-09-08 09:08:30 +00001657 return
1658 [=](uint64_t Dot) { return ScriptBase->getOutputSectionAlign(Name); };
1659 }
George Rimare32a3592016-08-10 07:59:34 +00001660 if (Tok == "SIZEOF_HEADERS")
George Rimar884e7862016-09-08 08:19:13 +00001661 return [=](uint64_t Dot) { return ScriptBase->getHeaderSize(); };
Rui Ueyama708019c2016-07-24 18:19:40 +00001662
George Rimar9f2f7ad2016-09-02 16:01:42 +00001663 // Tok is a literal number.
1664 uint64_t V;
1665 if (readInteger(Tok, V))
1666 return [=](uint64_t Dot) { return V; };
1667
1668 // Tok is a symbol name.
1669 if (Tok != "." && !isValidCIdentifier(Tok))
1670 setError("malformed number: " + Tok);
1671 return [=](uint64_t Dot) { return getSymbolValue(Tok, Dot); };
Rui Ueyama708019c2016-07-24 18:19:40 +00001672}
1673
1674Expr ScriptParser::readTernary(Expr Cond) {
1675 next();
1676 Expr L = readExpr();
1677 expect(":");
1678 Expr R = readExpr();
1679 return [=](uint64_t Dot) { return Cond(Dot) ? L(Dot) : R(Dot); };
1680}
1681
Rui Ueyama6ad7dfc2016-08-17 18:59:16 +00001682Expr ScriptParser::readParenExpr() {
1683 expect("(");
1684 Expr E = readExpr();
1685 expect(")");
1686 return E;
1687}
1688
Eugene Leviantbbe38602016-07-19 09:25:43 +00001689std::vector<StringRef> ScriptParser::readOutputSectionPhdrs() {
1690 std::vector<StringRef> Phdrs;
1691 while (!Error && peek().startswith(":")) {
1692 StringRef Tok = next();
1693 Tok = (Tok.size() == 1) ? next() : Tok.substr(1);
1694 if (Tok.empty()) {
1695 setError("section header name is empty");
1696 break;
1697 }
Rui Ueyama047404f2016-07-20 19:36:36 +00001698 Phdrs.push_back(Tok);
Eugene Leviantbbe38602016-07-19 09:25:43 +00001699 }
1700 return Phdrs;
1701}
1702
1703unsigned ScriptParser::readPhdrType() {
Eugene Leviantbbe38602016-07-19 09:25:43 +00001704 StringRef Tok = next();
Rui Ueyamab0f6c592016-07-20 19:36:38 +00001705 unsigned Ret = StringSwitch<unsigned>(Tok)
George Rimar6c55f0e2016-09-08 08:20:30 +00001706 .Case("PT_NULL", PT_NULL)
1707 .Case("PT_LOAD", PT_LOAD)
1708 .Case("PT_DYNAMIC", PT_DYNAMIC)
1709 .Case("PT_INTERP", PT_INTERP)
1710 .Case("PT_NOTE", PT_NOTE)
1711 .Case("PT_SHLIB", PT_SHLIB)
1712 .Case("PT_PHDR", PT_PHDR)
1713 .Case("PT_TLS", PT_TLS)
1714 .Case("PT_GNU_EH_FRAME", PT_GNU_EH_FRAME)
1715 .Case("PT_GNU_STACK", PT_GNU_STACK)
1716 .Case("PT_GNU_RELRO", PT_GNU_RELRO)
1717 .Default(-1);
Eugene Leviantbbe38602016-07-19 09:25:43 +00001718
Rui Ueyamab0f6c592016-07-20 19:36:38 +00001719 if (Ret == (unsigned)-1) {
1720 setError("invalid program header type: " + Tok);
1721 return PT_NULL;
1722 }
1723 return Ret;
Eugene Leviantbbe38602016-07-19 09:25:43 +00001724}
1725
Rui Ueyama95769b42016-08-31 20:03:54 +00001726void ScriptParser::readVersionDeclaration(StringRef VerStr) {
George Rimar20b65982016-08-31 09:08:26 +00001727 // Identifiers start at 2 because 0 and 1 are reserved
1728 // for VER_NDX_LOCAL and VER_NDX_GLOBAL constants.
1729 size_t VersionId = Config->VersionDefinitions.size() + 2;
1730 Config->VersionDefinitions.push_back({VerStr, VersionId});
1731
1732 if (skip("global:") || peek() != "local:")
1733 readGlobal(VerStr);
1734 if (skip("local:"))
1735 readLocal();
1736 expect("}");
1737
1738 // Each version may have a parent version. For example, "Ver2" defined as
1739 // "Ver2 { global: foo; local: *; } Ver1;" has "Ver1" as a parent. This
1740 // version hierarchy is, probably against your instinct, purely for human; the
1741 // runtime doesn't care about them at all. In LLD, we simply skip the token.
1742 if (!VerStr.empty() && peek() != ";")
1743 next();
1744 expect(";");
1745}
1746
1747void ScriptParser::readLocal() {
1748 Config->DefaultSymbolVersion = VER_NDX_LOCAL;
1749 expect("*");
1750 expect(";");
1751}
1752
1753void ScriptParser::readExtern(std::vector<SymbolVersion> *Globals) {
George Rimarcd574a52016-09-09 14:35:36 +00001754 expect("\"C++\"");
George Rimar20b65982016-08-31 09:08:26 +00001755 expect("{");
1756
1757 for (;;) {
1758 if (peek() == "}" || Error)
1759 break;
George Rimarcd574a52016-09-09 14:35:36 +00001760 bool HasWildcard = !peek().startswith("\"") && hasWildcard(peek());
1761 Globals->push_back({unquote(next()), true, HasWildcard});
George Rimar20b65982016-08-31 09:08:26 +00001762 expect(";");
1763 }
1764
1765 expect("}");
1766 expect(";");
1767}
1768
1769void ScriptParser::readGlobal(StringRef VerStr) {
1770 std::vector<SymbolVersion> *Globals;
1771 if (VerStr.empty())
1772 Globals = &Config->VersionScriptGlobals;
1773 else
1774 Globals = &Config->VersionDefinitions.back().Globals;
1775
1776 for (;;) {
1777 if (skip("extern"))
1778 readExtern(Globals);
1779
1780 StringRef Cur = peek();
1781 if (Cur == "}" || Cur == "local:" || Error)
1782 return;
1783 next();
George Rimarcd574a52016-09-09 14:35:36 +00001784 Globals->push_back({unquote(Cur), false, hasWildcard(Cur)});
George Rimar20b65982016-08-31 09:08:26 +00001785 expect(";");
1786 }
1787}
1788
Simon Atanasyan16b0cc92015-11-26 05:53:00 +00001789static bool isUnderSysroot(StringRef Path) {
1790 if (Config->Sysroot == "")
1791 return false;
1792 for (; !Path.empty(); Path = sys::path::parent_path(Path))
1793 if (sys::fs::equivalent(Config->Sysroot, Path))
1794 return true;
1795 return false;
1796}
1797
Rui Ueyama07320e42016-04-20 20:13:41 +00001798void elf::readLinkerScript(MemoryBufferRef MB) {
Simon Atanasyan16b0cc92015-11-26 05:53:00 +00001799 StringRef Path = MB.getBufferIdentifier();
George Rimar20b65982016-08-31 09:08:26 +00001800 ScriptParser(MB.getBuffer(), isUnderSysroot(Path)).readLinkerScript();
1801}
1802
1803void elf::readVersionScript(MemoryBufferRef MB) {
1804 ScriptParser(MB.getBuffer(), false).readVersionScript();
Rui Ueyamaf7c5fbb2015-09-30 17:23:26 +00001805}
Rui Ueyama1ebc8ed2016-02-12 21:47:28 +00001806
Rui Ueyama07320e42016-04-20 20:13:41 +00001807template class elf::LinkerScript<ELF32LE>;
1808template class elf::LinkerScript<ELF32BE>;
1809template class elf::LinkerScript<ELF64LE>;
1810template class elf::LinkerScript<ELF64BE>;