blob: 10008568d9fa5cc1966cf083789045f8f4529285 [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.
11// It does not construct an AST but consume linker script directives directly.
Rui Ueyama34f29242015-10-13 19:51:57 +000012// Results are written to Driver or Config object.
Rui Ueyamaf7c5fbb2015-09-30 17:23:26 +000013//
14//===----------------------------------------------------------------------===//
15
Rui Ueyama717677a2016-02-11 21:17:59 +000016#include "LinkerScript.h"
Rui Ueyamaf7c5fbb2015-09-30 17:23:26 +000017#include "Config.h"
18#include "Driver.h"
Rui Ueyama1ebc8ed2016-02-12 21:47:28 +000019#include "InputSection.h"
George Rimar652852c2016-04-16 10:10:32 +000020#include "OutputSections.h"
Adhemerval Zanellae77b5bf2016-04-06 20:59:11 +000021#include "ScriptParser.h"
Rui Ueyamaf7c5fbb2015-09-30 17:23:26 +000022#include "SymbolTable.h"
Rui Ueyama960504b2016-04-19 18:58:11 +000023#include "llvm/ADT/StringSwitch.h"
George Rimar652852c2016-04-16 10:10:32 +000024#include "llvm/Support/ELF.h"
Rui Ueyamaf7c5fbb2015-09-30 17:23:26 +000025#include "llvm/Support/FileSystem.h"
26#include "llvm/Support/MemoryBuffer.h"
Rui Ueyamaf03f3cc2015-10-13 00:09:21 +000027#include "llvm/Support/Path.h"
Rui Ueyamaa47ee682015-10-11 01:53:04 +000028#include "llvm/Support/StringSaver.h"
Rui Ueyamaf7c5fbb2015-09-30 17:23:26 +000029
30using namespace llvm;
George Rimar652852c2016-04-16 10:10:32 +000031using namespace llvm::ELF;
Rui Ueyama1ebc8ed2016-02-12 21:47:28 +000032using namespace llvm::object;
Rui Ueyamaf7c5fbb2015-09-30 17:23:26 +000033using namespace lld;
Rafael Espindolae0df00b2016-02-28 00:25:54 +000034using namespace lld::elf;
Rui Ueyamaf7c5fbb2015-09-30 17:23:26 +000035
Rui Ueyama07320e42016-04-20 20:13:41 +000036ScriptConfiguration *elf::ScriptConfig;
Rui Ueyama717677a2016-02-11 21:17:59 +000037
George Rimar652852c2016-04-16 10:10:32 +000038static uint64_t getInteger(StringRef S) {
39 uint64_t V;
40 if (S.getAsInteger(0, V)) {
41 error("malformed number: " + S);
42 return 0;
43 }
44 return V;
45}
46
Rui Ueyama960504b2016-04-19 18:58:11 +000047static int precedence(StringRef Op) {
48 return StringSwitch<int>(Op)
49 .Case("*", 4)
50 .Case("/", 3)
51 .Case("+", 2)
52 .Case("-", 2)
53 .Case("&", 1)
54 .Default(-1);
55}
56
57static StringRef next(ArrayRef<StringRef> &Tokens) {
58 if (Tokens.empty()) {
59 error("no next token");
60 return "";
61 }
62 StringRef Tok = Tokens.front();
63 Tokens = Tokens.slice(1);
64 return Tok;
65}
66
67static uint64_t parseExpr(uint64_t Lhs, int MinPrec,
68 ArrayRef<StringRef> &Tokens, uint64_t Dot);
69
70// This is a part of the operator-precedence parser to evaluate
71// arithmetic expressions in SECTIONS command. This function evaluates an
72// integer literal, a parenthesized expression or the special variable ".".
73static uint64_t parsePrimary(ArrayRef<StringRef> &Tokens, uint64_t Dot) {
74 StringRef Tok = next(Tokens);
75 if (Tok == ".")
76 return Dot;
77 if (Tok == "(") {
78 uint64_t V = parsePrimary(Tokens, Dot);
79 V = parseExpr(V, 0, Tokens, Dot);
80 if (Tokens.empty()) {
81 error(") expected");
82 } else {
83 Tok = next(Tokens);
84 if (Tok != ")")
85 error(") expected, but got " + Tok);
86 }
87 return V;
88 }
89 return getInteger(Tok);
90}
91
92static uint64_t apply(StringRef Op, uint64_t L, uint64_t R) {
93 if (Op == "+")
94 return L + R;
95 if (Op == "-")
96 return L - R;
97 if (Op == "*")
98 return L * R;
99 if (Op == "/") {
100 if (R == 0) {
101 error("division by zero");
George Rimar652852c2016-04-16 10:10:32 +0000102 return 0;
103 }
Rui Ueyama960504b2016-04-19 18:58:11 +0000104 return L / R;
George Rimar652852c2016-04-16 10:10:32 +0000105 }
Rui Ueyama960504b2016-04-19 18:58:11 +0000106 if (Op == "&")
107 return L & R;
Rui Ueyama7a81d672016-04-19 19:04:03 +0000108 llvm_unreachable("invalid operator");
Rui Ueyama960504b2016-04-19 18:58:11 +0000109 return 0;
110}
111
112// This is an operator-precedence parser to evaluate
113// arithmetic expressions in SECTIONS command.
114static uint64_t parseExpr(uint64_t Lhs, int MinPrec,
115 ArrayRef<StringRef> &Tokens, uint64_t Dot) {
116 while (!Tokens.empty()) {
117 // Read an operator and an expression.
118 StringRef Op1 = Tokens.front();
119 if (precedence(Op1) < MinPrec)
120 return Lhs;
121 next(Tokens);
122 uint64_t Rhs = parsePrimary(Tokens, Dot);
123
124 // Evaluate the remaining part of the expression first if the
125 // next operator has greater precedence than the previous one.
126 // For example, if we have read "+" and "3", and if the next
127 // operator is "*", then we'll evaluate 3 * ... part first.
128 while (!Tokens.empty()) {
129 StringRef Op2 = Tokens.front();
130 if (precedence(Op2) <= precedence(Op1))
131 break;
132 Rhs = parseExpr(Rhs, precedence(Op2), Tokens, Dot);
133 }
134
135 Lhs = apply(Op1, Lhs, Rhs);
136 }
137 return Lhs;
138}
139
140// Evaluates the expression given by list of tokens.
Rui Ueyama0536ec02016-04-19 20:50:25 +0000141static uint64_t evaluate(ArrayRef<StringRef> Tokens, uint64_t Dot) {
Rui Ueyama960504b2016-04-19 18:58:11 +0000142 uint64_t V = parsePrimary(Tokens, Dot);
143 V = parseExpr(V, 0, Tokens, Dot);
144 if (!Tokens.empty())
145 error("stray token: " + Tokens[0]);
146 return V;
George Rimar652852c2016-04-16 10:10:32 +0000147}
148
Rui Ueyama1ebc8ed2016-02-12 21:47:28 +0000149template <class ELFT>
Rui Ueyama07320e42016-04-20 20:13:41 +0000150SectionRule *LinkerScript<ELFT>::find(InputSectionBase<ELFT> *S) {
151 for (SectionRule &R : Opt.Sections)
Rui Ueyama1ebc8ed2016-02-12 21:47:28 +0000152 if (R.match(S))
George Rimar481c2ce2016-02-23 07:47:54 +0000153 return &R;
154 return nullptr;
155}
156
157template <class ELFT>
Rui Ueyama07320e42016-04-20 20:13:41 +0000158StringRef LinkerScript<ELFT>::getOutputSection(InputSectionBase<ELFT> *S) {
George Rimar481c2ce2016-02-23 07:47:54 +0000159 SectionRule *R = find(S);
160 return R ? R->Dest : "";
Rui Ueyama717677a2016-02-11 21:17:59 +0000161}
162
Rui Ueyama1ebc8ed2016-02-12 21:47:28 +0000163template <class ELFT>
Rui Ueyama07320e42016-04-20 20:13:41 +0000164bool LinkerScript<ELFT>::isDiscarded(InputSectionBase<ELFT> *S) {
Rui Ueyama1ebc8ed2016-02-12 21:47:28 +0000165 return getOutputSection(S) == "/DISCARD/";
Rui Ueyama717677a2016-02-11 21:17:59 +0000166}
167
Rui Ueyama07320e42016-04-20 20:13:41 +0000168template <class ELFT>
169bool LinkerScript<ELFT>::shouldKeep(InputSectionBase<ELFT> *S) {
George Rimar481c2ce2016-02-23 07:47:54 +0000170 SectionRule *R = find(S);
171 return R && R->Keep;
172}
173
George Rimar652852c2016-04-16 10:10:32 +0000174template <class ELFT>
Rui Ueyama7c18c282016-04-18 21:00:40 +0000175static OutputSectionBase<ELFT> *
176findSection(std::vector<OutputSectionBase<ELFT> *> &V, StringRef Name) {
177 for (OutputSectionBase<ELFT> *Sec : V)
178 if (Sec->getName() == Name)
179 return Sec;
180 return nullptr;
181}
182
183template <class ELFT>
Rui Ueyama07320e42016-04-20 20:13:41 +0000184void LinkerScript<ELFT>::assignAddresses(
Rui Ueyama7c18c282016-04-18 21:00:40 +0000185 std::vector<OutputSectionBase<ELFT> *> &Sections) {
186 typedef typename ELFT::uint uintX_t;
187
George Rimar652852c2016-04-16 10:10:32 +0000188 // Orphan sections are sections present in the input files which
Rui Ueyama7c18c282016-04-18 21:00:40 +0000189 // are not explicitly placed into the output file by the linker script.
190 // We place orphan sections at end of file.
191 // Other linkers places them using some heuristics as described in
George Rimar652852c2016-04-16 10:10:32 +0000192 // https://sourceware.org/binutils/docs/ld/Orphan-Sections.html#Orphan-Sections.
Rui Ueyama7c18c282016-04-18 21:00:40 +0000193 for (OutputSectionBase<ELFT> *Sec : Sections) {
George Rimar652852c2016-04-16 10:10:32 +0000194 StringRef Name = Sec->getName();
Rui Ueyama07320e42016-04-20 20:13:41 +0000195 auto I = std::find(Opt.SectionOrder.begin(), Opt.SectionOrder.end(), Name);
196 if (I == Opt.SectionOrder.end())
197 Opt.Commands.push_back({SectionKind, {}, Name});
George Rimar652852c2016-04-16 10:10:32 +0000198 }
George Rimar652852c2016-04-16 10:10:32 +0000199
Rui Ueyama7c18c282016-04-18 21:00:40 +0000200 // Assign addresses as instructed by linker script SECTIONS sub-commands.
George Rimar652852c2016-04-16 10:10:32 +0000201 uintX_t ThreadBssOffset = 0;
202 uintX_t VA =
203 Out<ELFT>::ElfHeader->getSize() + Out<ELFT>::ProgramHeaders->getSize();
204
Rui Ueyama07320e42016-04-20 20:13:41 +0000205 for (SectionsCommand &Cmd : Opt.Commands) {
Rui Ueyama9e957a02016-04-18 21:00:45 +0000206 if (Cmd.Kind == ExprKind) {
207 VA = evaluate(Cmd.Expr, VA);
George Rimar652852c2016-04-16 10:10:32 +0000208 continue;
209 }
210
Rui Ueyama9e957a02016-04-18 21:00:45 +0000211 OutputSectionBase<ELFT> *Sec = findSection(Sections, Cmd.SectionName);
Rui Ueyama7c18c282016-04-18 21:00:40 +0000212 if (!Sec)
George Rimar652852c2016-04-16 10:10:32 +0000213 continue;
214
George Rimar652852c2016-04-16 10:10:32 +0000215 if ((Sec->getFlags() & SHF_TLS) && Sec->getType() == SHT_NOBITS) {
216 uintX_t TVA = VA + ThreadBssOffset;
Rui Ueyama7c18c282016-04-18 21:00:40 +0000217 TVA = alignTo(TVA, Sec->getAlign());
George Rimar652852c2016-04-16 10:10:32 +0000218 Sec->setVA(TVA);
219 ThreadBssOffset = TVA - VA + Sec->getSize();
220 continue;
221 }
222
223 if (Sec->getFlags() & SHF_ALLOC) {
Rui Ueyama7c18c282016-04-18 21:00:40 +0000224 VA = alignTo(VA, Sec->getAlign());
George Rimar652852c2016-04-16 10:10:32 +0000225 Sec->setVA(VA);
226 VA += Sec->getSize();
227 continue;
228 }
229 }
230}
231
Rui Ueyama07320e42016-04-20 20:13:41 +0000232template <class ELFT>
233ArrayRef<uint8_t> LinkerScript<ELFT>::getFiller(StringRef Name) {
234 auto I = Opt.Filler.find(Name);
235 if (I == Opt.Filler.end())
Rui Ueyama3e808972016-02-28 05:09:11 +0000236 return {};
237 return I->second;
George Rimare2ee72b2016-02-26 14:48:31 +0000238}
239
Rui Ueyamae9c58062016-02-12 20:41:43 +0000240// A compartor to sort output sections. Returns -1 or 1 if both
241// A and B are mentioned in linker scripts. Otherwise, returns 0
242// to use the default rule which is implemented in Writer.cpp.
Rui Ueyama07320e42016-04-20 20:13:41 +0000243template <class ELFT>
244int LinkerScript<ELFT>::compareSections(StringRef A, StringRef B) {
245 auto E = Opt.SectionOrder.end();
246 auto I = std::find(Opt.SectionOrder.begin(), E, A);
247 auto J = std::find(Opt.SectionOrder.begin(), E, B);
Rui Ueyama3e808972016-02-28 05:09:11 +0000248 if (I == E || J == E)
Rui Ueyama717677a2016-02-11 21:17:59 +0000249 return 0;
250 return I < J ? -1 : 1;
251}
252
George Rimarcb2aeb62016-02-24 08:49:50 +0000253// Returns true if S matches T. S can contain glob meta-characters.
254// The asterisk ('*') matches zero or more characacters, and the question
255// mark ('?') matches one character.
Rui Ueyama1ebc8ed2016-02-12 21:47:28 +0000256static bool matchStr(StringRef S, StringRef T) {
257 for (;;) {
258 if (S.empty())
259 return T.empty();
260 if (S[0] == '*') {
261 S = S.substr(1);
262 if (S.empty())
263 // Fast path. If a pattern is '*', it matches anything.
264 return true;
265 for (size_t I = 0, E = T.size(); I < E; ++I)
266 if (matchStr(S, T.substr(I)))
267 return true;
268 return false;
269 }
George Rimarcb2aeb62016-02-24 08:49:50 +0000270 if (T.empty() || (S[0] != T[0] && S[0] != '?'))
Rui Ueyama1ebc8ed2016-02-12 21:47:28 +0000271 return false;
272 S = S.substr(1);
273 T = T.substr(1);
274 }
275}
276
277template <class ELFT> bool SectionRule::match(InputSectionBase<ELFT> *S) {
278 return matchStr(SectionPattern, S->getSectionName());
279}
280
Rui Ueyama07320e42016-04-20 20:13:41 +0000281class elf::ScriptParser : public ScriptParserBase {
George Rimarc3794e52016-02-24 09:21:47 +0000282 typedef void (ScriptParser::*Handler)();
283
Rui Ueyamaf7c5fbb2015-09-30 17:23:26 +0000284public:
Rui Ueyama07320e42016-04-20 20:13:41 +0000285 ScriptParser(StringRef S, bool B) : ScriptParserBase(S), IsUnderSysroot(B) {}
George Rimarf23b2322016-02-19 10:45:45 +0000286
Adhemerval Zanellae77b5bf2016-04-06 20:59:11 +0000287 void run() override;
Rui Ueyamaf7c5fbb2015-09-30 17:23:26 +0000288
289private:
Rui Ueyama52a15092015-10-11 03:28:42 +0000290 void addFile(StringRef Path);
291
Rui Ueyamaf7c5fbb2015-09-30 17:23:26 +0000292 void readAsNeeded();
Denis Protivensky90c50992015-10-08 06:48:38 +0000293 void readEntry();
George Rimar83f406c2015-10-19 17:35:12 +0000294 void readExtern();
Rui Ueyamaf7c5fbb2015-09-30 17:23:26 +0000295 void readGroup();
Rui Ueyama31aa1f82015-10-11 01:31:55 +0000296 void readInclude();
George Rimarc3794e52016-02-24 09:21:47 +0000297 void readNothing() {}
Rui Ueyamaee592822015-10-07 00:25:09 +0000298 void readOutput();
Davide Italiano9159ce92015-10-12 21:50:08 +0000299 void readOutputArch();
Rui Ueyamaf7c5fbb2015-09-30 17:23:26 +0000300 void readOutputFormat();
Davide Italiano68a39a62015-10-08 17:51:41 +0000301 void readSearchDir();
Denis Protivensky8e3b38a2015-11-12 09:52:08 +0000302 void readSections();
303
George Rimar652852c2016-04-16 10:10:32 +0000304 void readLocationCounterValue();
Denis Protivensky8e3b38a2015-11-12 09:52:08 +0000305 void readOutputSectionDescription();
George Rimar481c2ce2016-02-23 07:47:54 +0000306 void readSectionPatterns(StringRef OutSec, bool Keep);
Rui Ueyamaf7c5fbb2015-09-30 17:23:26 +0000307
George Rimarc3794e52016-02-24 09:21:47 +0000308 const static StringMap<Handler> Cmd;
Rui Ueyama07320e42016-04-20 20:13:41 +0000309 ScriptConfiguration &Opt = *ScriptConfig;
310 StringSaver Saver = {ScriptConfig->Alloc};
Simon Atanasyan16b0cc92015-11-26 05:53:00 +0000311 bool IsUnderSysroot;
Rui Ueyamaf7c5fbb2015-09-30 17:23:26 +0000312};
Rui Ueyamaf7c5fbb2015-09-30 17:23:26 +0000313
Rafael Espindolae0df00b2016-02-28 00:25:54 +0000314const StringMap<elf::ScriptParser::Handler> elf::ScriptParser::Cmd = {
George Rimarc3794e52016-02-24 09:21:47 +0000315 {"ENTRY", &ScriptParser::readEntry},
316 {"EXTERN", &ScriptParser::readExtern},
317 {"GROUP", &ScriptParser::readGroup},
318 {"INCLUDE", &ScriptParser::readInclude},
319 {"INPUT", &ScriptParser::readGroup},
320 {"OUTPUT", &ScriptParser::readOutput},
321 {"OUTPUT_ARCH", &ScriptParser::readOutputArch},
322 {"OUTPUT_FORMAT", &ScriptParser::readOutputFormat},
323 {"SEARCH_DIR", &ScriptParser::readSearchDir},
324 {"SECTIONS", &ScriptParser::readSections},
325 {";", &ScriptParser::readNothing}};
326
Rui Ueyama717677a2016-02-11 21:17:59 +0000327void ScriptParser::run() {
Rui Ueyamaf7c5fbb2015-09-30 17:23:26 +0000328 while (!atEOF()) {
329 StringRef Tok = next();
George Rimarc3794e52016-02-24 09:21:47 +0000330 if (Handler Fn = Cmd.lookup(Tok))
331 (this->*Fn)();
332 else
George Rimar57610422016-03-11 14:43:02 +0000333 setError("unknown directive: " + Tok);
Rui Ueyamaf7c5fbb2015-09-30 17:23:26 +0000334 }
335}
336
Rui Ueyama717677a2016-02-11 21:17:59 +0000337void ScriptParser::addFile(StringRef S) {
Simon Atanasyan16b0cc92015-11-26 05:53:00 +0000338 if (IsUnderSysroot && S.startswith("/")) {
339 SmallString<128> Path;
340 (Config->Sysroot + S).toStringRef(Path);
341 if (sys::fs::exists(Path)) {
342 Driver->addFile(Saver.save(Path.str()));
343 return;
344 }
345 }
346
Rui Ueyamaf03f3cc2015-10-13 00:09:21 +0000347 if (sys::path::is_absolute(S)) {
Rui Ueyama52a15092015-10-11 03:28:42 +0000348 Driver->addFile(S);
349 } else if (S.startswith("=")) {
350 if (Config->Sysroot.empty())
351 Driver->addFile(S.substr(1));
352 else
353 Driver->addFile(Saver.save(Config->Sysroot + "/" + S.substr(1)));
354 } else if (S.startswith("-l")) {
Rui Ueyama21eecb42016-02-02 21:13:09 +0000355 Driver->addLibrary(S.substr(2));
Simon Atanasyana1b8fc32015-11-26 20:23:46 +0000356 } else if (sys::fs::exists(S)) {
357 Driver->addFile(S);
Rui Ueyama52a15092015-10-11 03:28:42 +0000358 } else {
359 std::string Path = findFromSearchPaths(S);
360 if (Path.empty())
George Rimar777f9632016-03-12 08:31:34 +0000361 setError("unable to find " + S);
Rui Ueyama025d59b2016-02-02 20:27:59 +0000362 else
363 Driver->addFile(Saver.save(Path));
Rui Ueyama52a15092015-10-11 03:28:42 +0000364 }
365}
366
Rui Ueyama717677a2016-02-11 21:17:59 +0000367void ScriptParser::readAsNeeded() {
Rui Ueyamaf7c5fbb2015-09-30 17:23:26 +0000368 expect("(");
Rui Ueyama35da9b62015-10-11 20:59:12 +0000369 bool Orig = Config->AsNeeded;
370 Config->AsNeeded = true;
Rui Ueyama025d59b2016-02-02 20:27:59 +0000371 while (!Error) {
Rui Ueyamaf7c5fbb2015-09-30 17:23:26 +0000372 StringRef Tok = next();
373 if (Tok == ")")
Rui Ueyama35da9b62015-10-11 20:59:12 +0000374 break;
Rui Ueyama52a15092015-10-11 03:28:42 +0000375 addFile(Tok);
Rui Ueyamaf7c5fbb2015-09-30 17:23:26 +0000376 }
Rui Ueyama35da9b62015-10-11 20:59:12 +0000377 Config->AsNeeded = Orig;
Rui Ueyamaf7c5fbb2015-09-30 17:23:26 +0000378}
379
Rui Ueyama717677a2016-02-11 21:17:59 +0000380void ScriptParser::readEntry() {
Denis Protivensky90c50992015-10-08 06:48:38 +0000381 // -e <symbol> takes predecence over ENTRY(<symbol>).
382 expect("(");
383 StringRef Tok = next();
384 if (Config->Entry.empty())
385 Config->Entry = Tok;
386 expect(")");
387}
388
Rui Ueyama717677a2016-02-11 21:17:59 +0000389void ScriptParser::readExtern() {
George Rimar83f406c2015-10-19 17:35:12 +0000390 expect("(");
Rui Ueyama025d59b2016-02-02 20:27:59 +0000391 while (!Error) {
George Rimar83f406c2015-10-19 17:35:12 +0000392 StringRef Tok = next();
393 if (Tok == ")")
394 return;
395 Config->Undefined.push_back(Tok);
396 }
397}
398
Rui Ueyama717677a2016-02-11 21:17:59 +0000399void ScriptParser::readGroup() {
Rui Ueyamaf7c5fbb2015-09-30 17:23:26 +0000400 expect("(");
Rui Ueyama025d59b2016-02-02 20:27:59 +0000401 while (!Error) {
Rui Ueyamaf7c5fbb2015-09-30 17:23:26 +0000402 StringRef Tok = next();
403 if (Tok == ")")
404 return;
405 if (Tok == "AS_NEEDED") {
406 readAsNeeded();
407 continue;
408 }
Rui Ueyama52a15092015-10-11 03:28:42 +0000409 addFile(Tok);
Rui Ueyamaf7c5fbb2015-09-30 17:23:26 +0000410 }
411}
412
Rui Ueyama717677a2016-02-11 21:17:59 +0000413void ScriptParser::readInclude() {
Rui Ueyama31aa1f82015-10-11 01:31:55 +0000414 StringRef Tok = next();
415 auto MBOrErr = MemoryBuffer::getFile(Tok);
Rui Ueyama025d59b2016-02-02 20:27:59 +0000416 if (!MBOrErr) {
George Rimar57610422016-03-11 14:43:02 +0000417 setError("cannot open " + Tok);
Rui Ueyama025d59b2016-02-02 20:27:59 +0000418 return;
419 }
Rui Ueyama31aa1f82015-10-11 01:31:55 +0000420 std::unique_ptr<MemoryBuffer> &MB = *MBOrErr;
Rui Ueyamaa47ee682015-10-11 01:53:04 +0000421 StringRef S = Saver.save(MB->getMemBufferRef().getBuffer());
422 std::vector<StringRef> V = tokenize(S);
Rui Ueyama31aa1f82015-10-11 01:31:55 +0000423 Tokens.insert(Tokens.begin() + Pos, V.begin(), V.end());
Rui Ueyama31aa1f82015-10-11 01:31:55 +0000424}
425
Rui Ueyama717677a2016-02-11 21:17:59 +0000426void ScriptParser::readOutput() {
Rui Ueyamaee592822015-10-07 00:25:09 +0000427 // -o <file> takes predecence over OUTPUT(<file>).
428 expect("(");
429 StringRef Tok = next();
430 if (Config->OutputFile.empty())
431 Config->OutputFile = Tok;
432 expect(")");
433}
434
Rui Ueyama717677a2016-02-11 21:17:59 +0000435void ScriptParser::readOutputArch() {
Davide Italiano9159ce92015-10-12 21:50:08 +0000436 // Error checking only for now.
437 expect("(");
438 next();
439 expect(")");
440}
441
Rui Ueyama717677a2016-02-11 21:17:59 +0000442void ScriptParser::readOutputFormat() {
Rui Ueyamaf7c5fbb2015-09-30 17:23:26 +0000443 // Error checking only for now.
444 expect("(");
445 next();
Davide Italiano6836c612015-10-12 21:08:41 +0000446 StringRef Tok = next();
447 if (Tok == ")")
448 return;
Rui Ueyama025d59b2016-02-02 20:27:59 +0000449 if (Tok != ",") {
George Rimar57610422016-03-11 14:43:02 +0000450 setError("unexpected token: " + Tok);
Rui Ueyama025d59b2016-02-02 20:27:59 +0000451 return;
452 }
Davide Italiano6836c612015-10-12 21:08:41 +0000453 next();
454 expect(",");
455 next();
Rui Ueyamaf7c5fbb2015-09-30 17:23:26 +0000456 expect(")");
457}
458
Rui Ueyama717677a2016-02-11 21:17:59 +0000459void ScriptParser::readSearchDir() {
Davide Italiano68a39a62015-10-08 17:51:41 +0000460 expect("(");
Rafael Espindola06501922016-03-08 17:13:12 +0000461 Config->SearchPaths.push_back(next());
Davide Italiano68a39a62015-10-08 17:51:41 +0000462 expect(")");
463}
464
Rui Ueyama717677a2016-02-11 21:17:59 +0000465void ScriptParser::readSections() {
Rui Ueyama07320e42016-04-20 20:13:41 +0000466 Opt.DoLayout = true;
Denis Protivensky8e3b38a2015-11-12 09:52:08 +0000467 expect("{");
George Rimar652852c2016-04-16 10:10:32 +0000468 while (!Error && !skip("}")) {
469 StringRef Tok = peek();
470 if (Tok == ".")
471 readLocationCounterValue();
472 else
473 readOutputSectionDescription();
474 }
Denis Protivensky8e3b38a2015-11-12 09:52:08 +0000475}
476
George Rimar481c2ce2016-02-23 07:47:54 +0000477void ScriptParser::readSectionPatterns(StringRef OutSec, bool Keep) {
478 expect("(");
479 while (!Error && !skip(")"))
Rui Ueyama07320e42016-04-20 20:13:41 +0000480 Opt.Sections.emplace_back(OutSec, next(), Keep);
George Rimar481c2ce2016-02-23 07:47:54 +0000481}
482
George Rimar652852c2016-04-16 10:10:32 +0000483void ScriptParser::readLocationCounterValue() {
484 expect(".");
485 expect("=");
Rui Ueyama07320e42016-04-20 20:13:41 +0000486 Opt.Commands.push_back({ExprKind, {}, ""});
487 SectionsCommand &Cmd = Opt.Commands.back();
George Rimar652852c2016-04-16 10:10:32 +0000488 while (!Error) {
489 StringRef Tok = next();
490 if (Tok == ";")
491 break;
Rui Ueyama9e957a02016-04-18 21:00:45 +0000492 Cmd.Expr.push_back(Tok);
George Rimar652852c2016-04-16 10:10:32 +0000493 }
Rui Ueyama9e957a02016-04-18 21:00:45 +0000494 if (Cmd.Expr.empty())
George Rimar652852c2016-04-16 10:10:32 +0000495 error("error in location counter expression");
496}
497
Rui Ueyama717677a2016-02-11 21:17:59 +0000498void ScriptParser::readOutputSectionDescription() {
Rui Ueyama3e808972016-02-28 05:09:11 +0000499 StringRef OutSec = next();
Rui Ueyama07320e42016-04-20 20:13:41 +0000500 Opt.SectionOrder.push_back(OutSec);
501 Opt.Commands.push_back({SectionKind, {}, OutSec});
Denis Protivensky8e3b38a2015-11-12 09:52:08 +0000502 expect(":");
503 expect("{");
Rui Ueyama025d59b2016-02-02 20:27:59 +0000504 while (!Error && !skip("}")) {
George Rimar481c2ce2016-02-23 07:47:54 +0000505 StringRef Tok = next();
506 if (Tok == "*") {
Rui Ueyama3e808972016-02-28 05:09:11 +0000507 readSectionPatterns(OutSec, false);
George Rimar481c2ce2016-02-23 07:47:54 +0000508 } else if (Tok == "KEEP") {
509 expect("(");
510 next(); // Skip *
Rui Ueyama3e808972016-02-28 05:09:11 +0000511 readSectionPatterns(OutSec, true);
George Rimar481c2ce2016-02-23 07:47:54 +0000512 expect(")");
513 } else {
George Rimar777f9632016-03-12 08:31:34 +0000514 setError("unknown command " + Tok);
George Rimar481c2ce2016-02-23 07:47:54 +0000515 }
Denis Protivensky8e3b38a2015-11-12 09:52:08 +0000516 }
George Rimare2ee72b2016-02-26 14:48:31 +0000517 StringRef Tok = peek();
518 if (Tok.startswith("=")) {
519 if (!Tok.startswith("=0x")) {
Rui Ueyama3ed2f062016-03-13 03:17:44 +0000520 setError("filler should be a hexadecimal value");
George Rimare2ee72b2016-02-26 14:48:31 +0000521 return;
522 }
Rui Ueyama3e808972016-02-28 05:09:11 +0000523 Tok = Tok.substr(3);
Rui Ueyama07320e42016-04-20 20:13:41 +0000524 Opt.Filler[OutSec] = parseHex(Tok);
George Rimare2ee72b2016-02-26 14:48:31 +0000525 next();
526 }
Denis Protivensky8e3b38a2015-11-12 09:52:08 +0000527}
528
Simon Atanasyan16b0cc92015-11-26 05:53:00 +0000529static bool isUnderSysroot(StringRef Path) {
530 if (Config->Sysroot == "")
531 return false;
532 for (; !Path.empty(); Path = sys::path::parent_path(Path))
533 if (sys::fs::equivalent(Config->Sysroot, Path))
534 return true;
535 return false;
536}
537
Rui Ueyama07320e42016-04-20 20:13:41 +0000538// Entry point.
539void elf::readLinkerScript(MemoryBufferRef MB) {
Simon Atanasyan16b0cc92015-11-26 05:53:00 +0000540 StringRef Path = MB.getBufferIdentifier();
Rui Ueyama07320e42016-04-20 20:13:41 +0000541 ScriptParser(MB.getBuffer(), isUnderSysroot(Path)).run();
Rui Ueyamaf7c5fbb2015-09-30 17:23:26 +0000542}
Rui Ueyama1ebc8ed2016-02-12 21:47:28 +0000543
Rui Ueyama07320e42016-04-20 20:13:41 +0000544template class elf::LinkerScript<ELF32LE>;
545template class elf::LinkerScript<ELF32BE>;
546template class elf::LinkerScript<ELF64LE>;
547template class elf::LinkerScript<ELF64BE>;