blob: 966acf3eb6f418e847c75c7e5b233d58c1fb13e7 [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
Rui Ueyama60118112016-04-20 20:54:13 +000067static bool expect(ArrayRef<StringRef> &Tokens, StringRef S) {
68 if (Tokens.empty()) {
69 error(S + " expected");
70 return false;
71 }
72 StringRef Tok = Tokens.front();
73 if (Tok != S) {
74 error(S + " expected, but got " + Tok);
75 return false;
76 }
77 Tokens = Tokens.slice(1);
78 return true;
79}
80
Rui Ueyama99e519c2016-04-20 20:48:25 +000081static uint64_t parseExpr(ArrayRef<StringRef> &Tokens, uint64_t Dot);
Rui Ueyama960504b2016-04-19 18:58:11 +000082
83// This is a part of the operator-precedence parser to evaluate
84// arithmetic expressions in SECTIONS command. This function evaluates an
85// integer literal, a parenthesized expression or the special variable ".".
86static uint64_t parsePrimary(ArrayRef<StringRef> &Tokens, uint64_t Dot) {
87 StringRef Tok = next(Tokens);
88 if (Tok == ".")
89 return Dot;
90 if (Tok == "(") {
Rui Ueyama99e519c2016-04-20 20:48:25 +000091 uint64_t V = parseExpr(Tokens, Dot);
Rui Ueyama60118112016-04-20 20:54:13 +000092 if (!expect(Tokens, ")"))
93 return 0;
Rui Ueyama960504b2016-04-19 18:58:11 +000094 return V;
95 }
96 return getInteger(Tok);
97}
98
99static uint64_t apply(StringRef Op, uint64_t L, uint64_t R) {
100 if (Op == "+")
101 return L + R;
102 if (Op == "-")
103 return L - R;
104 if (Op == "*")
105 return L * R;
106 if (Op == "/") {
107 if (R == 0) {
108 error("division by zero");
George Rimar652852c2016-04-16 10:10:32 +0000109 return 0;
110 }
Rui Ueyama960504b2016-04-19 18:58:11 +0000111 return L / R;
George Rimar652852c2016-04-16 10:10:32 +0000112 }
Rui Ueyama960504b2016-04-19 18:58:11 +0000113 if (Op == "&")
114 return L & R;
Rui Ueyama7a81d672016-04-19 19:04:03 +0000115 llvm_unreachable("invalid operator");
Rui Ueyama960504b2016-04-19 18:58:11 +0000116 return 0;
117}
118
119// This is an operator-precedence parser to evaluate
120// arithmetic expressions in SECTIONS command.
Rui Ueyama99e519c2016-04-20 20:48:25 +0000121// Tokens should start with an operator.
122static uint64_t parseExpr1(uint64_t Lhs, int MinPrec,
123 ArrayRef<StringRef> &Tokens, uint64_t Dot) {
Rui Ueyama960504b2016-04-19 18:58:11 +0000124 while (!Tokens.empty()) {
125 // Read an operator and an expression.
126 StringRef Op1 = Tokens.front();
127 if (precedence(Op1) < MinPrec)
128 return Lhs;
129 next(Tokens);
130 uint64_t Rhs = parsePrimary(Tokens, Dot);
131
132 // Evaluate the remaining part of the expression first if the
133 // next operator has greater precedence than the previous one.
134 // For example, if we have read "+" and "3", and if the next
135 // operator is "*", then we'll evaluate 3 * ... part first.
136 while (!Tokens.empty()) {
137 StringRef Op2 = Tokens.front();
138 if (precedence(Op2) <= precedence(Op1))
139 break;
Rui Ueyama99e519c2016-04-20 20:48:25 +0000140 Rhs = parseExpr1(Rhs, precedence(Op2), Tokens, Dot);
Rui Ueyama960504b2016-04-19 18:58:11 +0000141 }
142
143 Lhs = apply(Op1, Lhs, Rhs);
144 }
145 return Lhs;
146}
147
Rui Ueyama99e519c2016-04-20 20:48:25 +0000148static uint64_t parseExpr(ArrayRef<StringRef> &Tokens, uint64_t Dot) {
149 uint64_t V = parsePrimary(Tokens, Dot);
150 return parseExpr1(V, 0, Tokens, Dot);
151}
152
Rui Ueyama960504b2016-04-19 18:58:11 +0000153// Evaluates the expression given by list of tokens.
Rui Ueyama0536ec02016-04-19 20:50:25 +0000154static uint64_t evaluate(ArrayRef<StringRef> Tokens, uint64_t Dot) {
Rui Ueyama99e519c2016-04-20 20:48:25 +0000155 uint64_t V = parseExpr(Tokens, Dot);
Rui Ueyama960504b2016-04-19 18:58:11 +0000156 if (!Tokens.empty())
157 error("stray token: " + Tokens[0]);
158 return V;
George Rimar652852c2016-04-16 10:10:32 +0000159}
160
Rui Ueyama1ebc8ed2016-02-12 21:47:28 +0000161template <class ELFT>
Rui Ueyama07320e42016-04-20 20:13:41 +0000162SectionRule *LinkerScript<ELFT>::find(InputSectionBase<ELFT> *S) {
163 for (SectionRule &R : Opt.Sections)
Rui Ueyama1ebc8ed2016-02-12 21:47:28 +0000164 if (R.match(S))
George Rimar481c2ce2016-02-23 07:47:54 +0000165 return &R;
166 return nullptr;
167}
168
169template <class ELFT>
Rui Ueyama07320e42016-04-20 20:13:41 +0000170StringRef LinkerScript<ELFT>::getOutputSection(InputSectionBase<ELFT> *S) {
George Rimar481c2ce2016-02-23 07:47:54 +0000171 SectionRule *R = find(S);
172 return R ? R->Dest : "";
Rui Ueyama717677a2016-02-11 21:17:59 +0000173}
174
Rui Ueyama1ebc8ed2016-02-12 21:47:28 +0000175template <class ELFT>
Rui Ueyama07320e42016-04-20 20:13:41 +0000176bool LinkerScript<ELFT>::isDiscarded(InputSectionBase<ELFT> *S) {
Rui Ueyama1ebc8ed2016-02-12 21:47:28 +0000177 return getOutputSection(S) == "/DISCARD/";
Rui Ueyama717677a2016-02-11 21:17:59 +0000178}
179
Rui Ueyama07320e42016-04-20 20:13:41 +0000180template <class ELFT>
181bool LinkerScript<ELFT>::shouldKeep(InputSectionBase<ELFT> *S) {
George Rimar481c2ce2016-02-23 07:47:54 +0000182 SectionRule *R = find(S);
183 return R && R->Keep;
184}
185
George Rimar652852c2016-04-16 10:10:32 +0000186template <class ELFT>
Rui Ueyama7c18c282016-04-18 21:00:40 +0000187static OutputSectionBase<ELFT> *
George Rimardbbd8b12016-04-21 11:21:48 +0000188findSection(ArrayRef<OutputSectionBase<ELFT> *> V, StringRef Name) {
Rui Ueyama7c18c282016-04-18 21:00:40 +0000189 for (OutputSectionBase<ELFT> *Sec : V)
190 if (Sec->getName() == Name)
191 return Sec;
192 return nullptr;
193}
194
195template <class ELFT>
Rui Ueyama07320e42016-04-20 20:13:41 +0000196void LinkerScript<ELFT>::assignAddresses(
George Rimardbbd8b12016-04-21 11:21:48 +0000197 ArrayRef<OutputSectionBase<ELFT> *> Sections) {
Rui Ueyama7c18c282016-04-18 21:00:40 +0000198 typedef typename ELFT::uint uintX_t;
199
George Rimar652852c2016-04-16 10:10:32 +0000200 // Orphan sections are sections present in the input files which
Rui Ueyama7c18c282016-04-18 21:00:40 +0000201 // are not explicitly placed into the output file by the linker script.
202 // We place orphan sections at end of file.
203 // Other linkers places them using some heuristics as described in
George Rimar652852c2016-04-16 10:10:32 +0000204 // https://sourceware.org/binutils/docs/ld/Orphan-Sections.html#Orphan-Sections.
Rui Ueyama7c18c282016-04-18 21:00:40 +0000205 for (OutputSectionBase<ELFT> *Sec : Sections) {
George Rimar652852c2016-04-16 10:10:32 +0000206 StringRef Name = Sec->getName();
George Rimar71b26e92016-04-21 10:22:02 +0000207 if (getSectionOrder(Name) == (uint32_t)-1)
Rui Ueyama07320e42016-04-20 20:13:41 +0000208 Opt.Commands.push_back({SectionKind, {}, Name});
George Rimar652852c2016-04-16 10:10:32 +0000209 }
George Rimar652852c2016-04-16 10:10:32 +0000210
Rui Ueyama7c18c282016-04-18 21:00:40 +0000211 // Assign addresses as instructed by linker script SECTIONS sub-commands.
George Rimar652852c2016-04-16 10:10:32 +0000212 uintX_t ThreadBssOffset = 0;
213 uintX_t VA =
214 Out<ELFT>::ElfHeader->getSize() + Out<ELFT>::ProgramHeaders->getSize();
215
Rui Ueyama07320e42016-04-20 20:13:41 +0000216 for (SectionsCommand &Cmd : Opt.Commands) {
Rui Ueyama9e957a02016-04-18 21:00:45 +0000217 if (Cmd.Kind == ExprKind) {
218 VA = evaluate(Cmd.Expr, VA);
George Rimar652852c2016-04-16 10:10:32 +0000219 continue;
220 }
221
George Rimardbbd8b12016-04-21 11:21:48 +0000222 OutputSectionBase<ELFT> *Sec = findSection<ELFT>(Sections, Cmd.SectionName);
Rui Ueyama7c18c282016-04-18 21:00:40 +0000223 if (!Sec)
George Rimar652852c2016-04-16 10:10:32 +0000224 continue;
225
George Rimar652852c2016-04-16 10:10:32 +0000226 if ((Sec->getFlags() & SHF_TLS) && Sec->getType() == SHT_NOBITS) {
227 uintX_t TVA = VA + ThreadBssOffset;
Rui Ueyama7c18c282016-04-18 21:00:40 +0000228 TVA = alignTo(TVA, Sec->getAlign());
George Rimar652852c2016-04-16 10:10:32 +0000229 Sec->setVA(TVA);
230 ThreadBssOffset = TVA - VA + Sec->getSize();
231 continue;
232 }
233
234 if (Sec->getFlags() & SHF_ALLOC) {
Rui Ueyama7c18c282016-04-18 21:00:40 +0000235 VA = alignTo(VA, Sec->getAlign());
George Rimar652852c2016-04-16 10:10:32 +0000236 Sec->setVA(VA);
237 VA += Sec->getSize();
238 continue;
239 }
240 }
241}
242
Rui Ueyama07320e42016-04-20 20:13:41 +0000243template <class ELFT>
244ArrayRef<uint8_t> LinkerScript<ELFT>::getFiller(StringRef Name) {
245 auto I = Opt.Filler.find(Name);
246 if (I == Opt.Filler.end())
Rui Ueyama3e808972016-02-28 05:09:11 +0000247 return {};
248 return I->second;
George Rimare2ee72b2016-02-26 14:48:31 +0000249}
250
George Rimar71b26e92016-04-21 10:22:02 +0000251template <class ELFT>
252uint32_t LinkerScript<ELFT>::getSectionOrder(StringRef Name) {
253 auto Begin = Opt.Commands.begin();
254 auto End = Opt.Commands.end();
255 auto I = std::find_if(Begin, End, [&](SectionsCommand &N) {
256 return N.Kind == SectionKind && N.SectionName == Name;
257 });
258 return I == End ? (uint32_t)-1 : (I - Begin);
259}
260
261// A compartor to sort output sections. Returns -1 or 1 if
262// A or B are mentioned in linker script. Otherwise, returns 0.
Rui Ueyama07320e42016-04-20 20:13:41 +0000263template <class ELFT>
264int LinkerScript<ELFT>::compareSections(StringRef A, StringRef B) {
George Rimar71b26e92016-04-21 10:22:02 +0000265 uint32_t I = getSectionOrder(A);
266 uint32_t J = getSectionOrder(B);
267 if (I == (uint32_t)-1 && J == (uint32_t)-1)
Rui Ueyama717677a2016-02-11 21:17:59 +0000268 return 0;
269 return I < J ? -1 : 1;
270}
271
George Rimarcb2aeb62016-02-24 08:49:50 +0000272// Returns true if S matches T. S can contain glob meta-characters.
273// The asterisk ('*') matches zero or more characacters, and the question
274// mark ('?') matches one character.
Rui Ueyama1ebc8ed2016-02-12 21:47:28 +0000275static bool matchStr(StringRef S, StringRef T) {
276 for (;;) {
277 if (S.empty())
278 return T.empty();
279 if (S[0] == '*') {
280 S = S.substr(1);
281 if (S.empty())
282 // Fast path. If a pattern is '*', it matches anything.
283 return true;
284 for (size_t I = 0, E = T.size(); I < E; ++I)
285 if (matchStr(S, T.substr(I)))
286 return true;
287 return false;
288 }
George Rimarcb2aeb62016-02-24 08:49:50 +0000289 if (T.empty() || (S[0] != T[0] && S[0] != '?'))
Rui Ueyama1ebc8ed2016-02-12 21:47:28 +0000290 return false;
291 S = S.substr(1);
292 T = T.substr(1);
293 }
294}
295
296template <class ELFT> bool SectionRule::match(InputSectionBase<ELFT> *S) {
297 return matchStr(SectionPattern, S->getSectionName());
298}
299
Rui Ueyama07320e42016-04-20 20:13:41 +0000300class elf::ScriptParser : public ScriptParserBase {
George Rimarc3794e52016-02-24 09:21:47 +0000301 typedef void (ScriptParser::*Handler)();
302
Rui Ueyamaf7c5fbb2015-09-30 17:23:26 +0000303public:
Rui Ueyama07320e42016-04-20 20:13:41 +0000304 ScriptParser(StringRef S, bool B) : ScriptParserBase(S), IsUnderSysroot(B) {}
George Rimarf23b2322016-02-19 10:45:45 +0000305
Adhemerval Zanellae77b5bf2016-04-06 20:59:11 +0000306 void run() override;
Rui Ueyamaf7c5fbb2015-09-30 17:23:26 +0000307
308private:
Rui Ueyama52a15092015-10-11 03:28:42 +0000309 void addFile(StringRef Path);
310
Rui Ueyamaf7c5fbb2015-09-30 17:23:26 +0000311 void readAsNeeded();
Denis Protivensky90c50992015-10-08 06:48:38 +0000312 void readEntry();
George Rimar83f406c2015-10-19 17:35:12 +0000313 void readExtern();
Rui Ueyamaf7c5fbb2015-09-30 17:23:26 +0000314 void readGroup();
Rui Ueyama31aa1f82015-10-11 01:31:55 +0000315 void readInclude();
George Rimarc3794e52016-02-24 09:21:47 +0000316 void readNothing() {}
Rui Ueyamaee592822015-10-07 00:25:09 +0000317 void readOutput();
Davide Italiano9159ce92015-10-12 21:50:08 +0000318 void readOutputArch();
Rui Ueyamaf7c5fbb2015-09-30 17:23:26 +0000319 void readOutputFormat();
Davide Italiano68a39a62015-10-08 17:51:41 +0000320 void readSearchDir();
Denis Protivensky8e3b38a2015-11-12 09:52:08 +0000321 void readSections();
322
George Rimar652852c2016-04-16 10:10:32 +0000323 void readLocationCounterValue();
Denis Protivensky8e3b38a2015-11-12 09:52:08 +0000324 void readOutputSectionDescription();
George Rimar481c2ce2016-02-23 07:47:54 +0000325 void readSectionPatterns(StringRef OutSec, bool Keep);
Rui Ueyamaf7c5fbb2015-09-30 17:23:26 +0000326
George Rimarc3794e52016-02-24 09:21:47 +0000327 const static StringMap<Handler> Cmd;
Rui Ueyama07320e42016-04-20 20:13:41 +0000328 ScriptConfiguration &Opt = *ScriptConfig;
329 StringSaver Saver = {ScriptConfig->Alloc};
Simon Atanasyan16b0cc92015-11-26 05:53:00 +0000330 bool IsUnderSysroot;
Rui Ueyamaf7c5fbb2015-09-30 17:23:26 +0000331};
Rui Ueyamaf7c5fbb2015-09-30 17:23:26 +0000332
Rafael Espindolae0df00b2016-02-28 00:25:54 +0000333const StringMap<elf::ScriptParser::Handler> elf::ScriptParser::Cmd = {
George Rimarc3794e52016-02-24 09:21:47 +0000334 {"ENTRY", &ScriptParser::readEntry},
335 {"EXTERN", &ScriptParser::readExtern},
336 {"GROUP", &ScriptParser::readGroup},
337 {"INCLUDE", &ScriptParser::readInclude},
338 {"INPUT", &ScriptParser::readGroup},
339 {"OUTPUT", &ScriptParser::readOutput},
340 {"OUTPUT_ARCH", &ScriptParser::readOutputArch},
341 {"OUTPUT_FORMAT", &ScriptParser::readOutputFormat},
342 {"SEARCH_DIR", &ScriptParser::readSearchDir},
343 {"SECTIONS", &ScriptParser::readSections},
344 {";", &ScriptParser::readNothing}};
345
Rui Ueyama717677a2016-02-11 21:17:59 +0000346void ScriptParser::run() {
Rui Ueyamaf7c5fbb2015-09-30 17:23:26 +0000347 while (!atEOF()) {
348 StringRef Tok = next();
George Rimarc3794e52016-02-24 09:21:47 +0000349 if (Handler Fn = Cmd.lookup(Tok))
350 (this->*Fn)();
351 else
George Rimar57610422016-03-11 14:43:02 +0000352 setError("unknown directive: " + Tok);
Rui Ueyamaf7c5fbb2015-09-30 17:23:26 +0000353 }
354}
355
Rui Ueyama717677a2016-02-11 21:17:59 +0000356void ScriptParser::addFile(StringRef S) {
Simon Atanasyan16b0cc92015-11-26 05:53:00 +0000357 if (IsUnderSysroot && S.startswith("/")) {
358 SmallString<128> Path;
359 (Config->Sysroot + S).toStringRef(Path);
360 if (sys::fs::exists(Path)) {
361 Driver->addFile(Saver.save(Path.str()));
362 return;
363 }
364 }
365
Rui Ueyamaf03f3cc2015-10-13 00:09:21 +0000366 if (sys::path::is_absolute(S)) {
Rui Ueyama52a15092015-10-11 03:28:42 +0000367 Driver->addFile(S);
368 } else if (S.startswith("=")) {
369 if (Config->Sysroot.empty())
370 Driver->addFile(S.substr(1));
371 else
372 Driver->addFile(Saver.save(Config->Sysroot + "/" + S.substr(1)));
373 } else if (S.startswith("-l")) {
Rui Ueyama21eecb42016-02-02 21:13:09 +0000374 Driver->addLibrary(S.substr(2));
Simon Atanasyana1b8fc32015-11-26 20:23:46 +0000375 } else if (sys::fs::exists(S)) {
376 Driver->addFile(S);
Rui Ueyama52a15092015-10-11 03:28:42 +0000377 } else {
378 std::string Path = findFromSearchPaths(S);
379 if (Path.empty())
George Rimar777f9632016-03-12 08:31:34 +0000380 setError("unable to find " + S);
Rui Ueyama025d59b2016-02-02 20:27:59 +0000381 else
382 Driver->addFile(Saver.save(Path));
Rui Ueyama52a15092015-10-11 03:28:42 +0000383 }
384}
385
Rui Ueyama717677a2016-02-11 21:17:59 +0000386void ScriptParser::readAsNeeded() {
Rui Ueyamaf7c5fbb2015-09-30 17:23:26 +0000387 expect("(");
Rui Ueyama35da9b62015-10-11 20:59:12 +0000388 bool Orig = Config->AsNeeded;
389 Config->AsNeeded = true;
Rui Ueyama025d59b2016-02-02 20:27:59 +0000390 while (!Error) {
Rui Ueyamaf7c5fbb2015-09-30 17:23:26 +0000391 StringRef Tok = next();
392 if (Tok == ")")
Rui Ueyama35da9b62015-10-11 20:59:12 +0000393 break;
Rui Ueyama52a15092015-10-11 03:28:42 +0000394 addFile(Tok);
Rui Ueyamaf7c5fbb2015-09-30 17:23:26 +0000395 }
Rui Ueyama35da9b62015-10-11 20:59:12 +0000396 Config->AsNeeded = Orig;
Rui Ueyamaf7c5fbb2015-09-30 17:23:26 +0000397}
398
Rui Ueyama717677a2016-02-11 21:17:59 +0000399void ScriptParser::readEntry() {
Denis Protivensky90c50992015-10-08 06:48:38 +0000400 // -e <symbol> takes predecence over ENTRY(<symbol>).
401 expect("(");
402 StringRef Tok = next();
403 if (Config->Entry.empty())
404 Config->Entry = Tok;
405 expect(")");
406}
407
Rui Ueyama717677a2016-02-11 21:17:59 +0000408void ScriptParser::readExtern() {
George Rimar83f406c2015-10-19 17:35:12 +0000409 expect("(");
Rui Ueyama025d59b2016-02-02 20:27:59 +0000410 while (!Error) {
George Rimar83f406c2015-10-19 17:35:12 +0000411 StringRef Tok = next();
412 if (Tok == ")")
413 return;
414 Config->Undefined.push_back(Tok);
415 }
416}
417
Rui Ueyama717677a2016-02-11 21:17:59 +0000418void ScriptParser::readGroup() {
Rui Ueyamaf7c5fbb2015-09-30 17:23:26 +0000419 expect("(");
Rui Ueyama025d59b2016-02-02 20:27:59 +0000420 while (!Error) {
Rui Ueyamaf7c5fbb2015-09-30 17:23:26 +0000421 StringRef Tok = next();
422 if (Tok == ")")
423 return;
424 if (Tok == "AS_NEEDED") {
425 readAsNeeded();
426 continue;
427 }
Rui Ueyama52a15092015-10-11 03:28:42 +0000428 addFile(Tok);
Rui Ueyamaf7c5fbb2015-09-30 17:23:26 +0000429 }
430}
431
Rui Ueyama717677a2016-02-11 21:17:59 +0000432void ScriptParser::readInclude() {
Rui Ueyama31aa1f82015-10-11 01:31:55 +0000433 StringRef Tok = next();
434 auto MBOrErr = MemoryBuffer::getFile(Tok);
Rui Ueyama025d59b2016-02-02 20:27:59 +0000435 if (!MBOrErr) {
George Rimar57610422016-03-11 14:43:02 +0000436 setError("cannot open " + Tok);
Rui Ueyama025d59b2016-02-02 20:27:59 +0000437 return;
438 }
Rui Ueyama31aa1f82015-10-11 01:31:55 +0000439 std::unique_ptr<MemoryBuffer> &MB = *MBOrErr;
Rui Ueyamaa47ee682015-10-11 01:53:04 +0000440 StringRef S = Saver.save(MB->getMemBufferRef().getBuffer());
441 std::vector<StringRef> V = tokenize(S);
Rui Ueyama31aa1f82015-10-11 01:31:55 +0000442 Tokens.insert(Tokens.begin() + Pos, V.begin(), V.end());
Rui Ueyama31aa1f82015-10-11 01:31:55 +0000443}
444
Rui Ueyama717677a2016-02-11 21:17:59 +0000445void ScriptParser::readOutput() {
Rui Ueyamaee592822015-10-07 00:25:09 +0000446 // -o <file> takes predecence over OUTPUT(<file>).
447 expect("(");
448 StringRef Tok = next();
449 if (Config->OutputFile.empty())
450 Config->OutputFile = Tok;
451 expect(")");
452}
453
Rui Ueyama717677a2016-02-11 21:17:59 +0000454void ScriptParser::readOutputArch() {
Davide Italiano9159ce92015-10-12 21:50:08 +0000455 // Error checking only for now.
456 expect("(");
457 next();
458 expect(")");
459}
460
Rui Ueyama717677a2016-02-11 21:17:59 +0000461void ScriptParser::readOutputFormat() {
Rui Ueyamaf7c5fbb2015-09-30 17:23:26 +0000462 // Error checking only for now.
463 expect("(");
464 next();
Davide Italiano6836c612015-10-12 21:08:41 +0000465 StringRef Tok = next();
466 if (Tok == ")")
467 return;
Rui Ueyama025d59b2016-02-02 20:27:59 +0000468 if (Tok != ",") {
George Rimar57610422016-03-11 14:43:02 +0000469 setError("unexpected token: " + Tok);
Rui Ueyama025d59b2016-02-02 20:27:59 +0000470 return;
471 }
Davide Italiano6836c612015-10-12 21:08:41 +0000472 next();
473 expect(",");
474 next();
Rui Ueyamaf7c5fbb2015-09-30 17:23:26 +0000475 expect(")");
476}
477
Rui Ueyama717677a2016-02-11 21:17:59 +0000478void ScriptParser::readSearchDir() {
Davide Italiano68a39a62015-10-08 17:51:41 +0000479 expect("(");
Rafael Espindola06501922016-03-08 17:13:12 +0000480 Config->SearchPaths.push_back(next());
Davide Italiano68a39a62015-10-08 17:51:41 +0000481 expect(")");
482}
483
Rui Ueyama717677a2016-02-11 21:17:59 +0000484void ScriptParser::readSections() {
Rui Ueyama07320e42016-04-20 20:13:41 +0000485 Opt.DoLayout = true;
Denis Protivensky8e3b38a2015-11-12 09:52:08 +0000486 expect("{");
George Rimar652852c2016-04-16 10:10:32 +0000487 while (!Error && !skip("}")) {
488 StringRef Tok = peek();
489 if (Tok == ".")
490 readLocationCounterValue();
491 else
492 readOutputSectionDescription();
493 }
Denis Protivensky8e3b38a2015-11-12 09:52:08 +0000494}
495
George Rimar481c2ce2016-02-23 07:47:54 +0000496void ScriptParser::readSectionPatterns(StringRef OutSec, bool Keep) {
497 expect("(");
498 while (!Error && !skip(")"))
Rui Ueyama07320e42016-04-20 20:13:41 +0000499 Opt.Sections.emplace_back(OutSec, next(), Keep);
George Rimar481c2ce2016-02-23 07:47:54 +0000500}
501
George Rimar652852c2016-04-16 10:10:32 +0000502void ScriptParser::readLocationCounterValue() {
503 expect(".");
504 expect("=");
Rui Ueyama07320e42016-04-20 20:13:41 +0000505 Opt.Commands.push_back({ExprKind, {}, ""});
506 SectionsCommand &Cmd = Opt.Commands.back();
George Rimar652852c2016-04-16 10:10:32 +0000507 while (!Error) {
508 StringRef Tok = next();
509 if (Tok == ";")
510 break;
Rui Ueyama9e957a02016-04-18 21:00:45 +0000511 Cmd.Expr.push_back(Tok);
George Rimar652852c2016-04-16 10:10:32 +0000512 }
Rui Ueyama9e957a02016-04-18 21:00:45 +0000513 if (Cmd.Expr.empty())
George Rimar652852c2016-04-16 10:10:32 +0000514 error("error in location counter expression");
515}
516
Rui Ueyama717677a2016-02-11 21:17:59 +0000517void ScriptParser::readOutputSectionDescription() {
Rui Ueyama3e808972016-02-28 05:09:11 +0000518 StringRef OutSec = next();
Rui Ueyama07320e42016-04-20 20:13:41 +0000519 Opt.Commands.push_back({SectionKind, {}, OutSec});
Denis Protivensky8e3b38a2015-11-12 09:52:08 +0000520 expect(":");
521 expect("{");
Rui Ueyama025d59b2016-02-02 20:27:59 +0000522 while (!Error && !skip("}")) {
George Rimar481c2ce2016-02-23 07:47:54 +0000523 StringRef Tok = next();
524 if (Tok == "*") {
Rui Ueyama3e808972016-02-28 05:09:11 +0000525 readSectionPatterns(OutSec, false);
George Rimar481c2ce2016-02-23 07:47:54 +0000526 } else if (Tok == "KEEP") {
527 expect("(");
528 next(); // Skip *
Rui Ueyama3e808972016-02-28 05:09:11 +0000529 readSectionPatterns(OutSec, true);
George Rimar481c2ce2016-02-23 07:47:54 +0000530 expect(")");
531 } else {
George Rimar777f9632016-03-12 08:31:34 +0000532 setError("unknown command " + Tok);
George Rimar481c2ce2016-02-23 07:47:54 +0000533 }
Denis Protivensky8e3b38a2015-11-12 09:52:08 +0000534 }
George Rimare2ee72b2016-02-26 14:48:31 +0000535 StringRef Tok = peek();
536 if (Tok.startswith("=")) {
537 if (!Tok.startswith("=0x")) {
Rui Ueyama3ed2f062016-03-13 03:17:44 +0000538 setError("filler should be a hexadecimal value");
George Rimare2ee72b2016-02-26 14:48:31 +0000539 return;
540 }
Rui Ueyama3e808972016-02-28 05:09:11 +0000541 Tok = Tok.substr(3);
Rui Ueyama07320e42016-04-20 20:13:41 +0000542 Opt.Filler[OutSec] = parseHex(Tok);
George Rimare2ee72b2016-02-26 14:48:31 +0000543 next();
544 }
Denis Protivensky8e3b38a2015-11-12 09:52:08 +0000545}
546
Simon Atanasyan16b0cc92015-11-26 05:53:00 +0000547static bool isUnderSysroot(StringRef Path) {
548 if (Config->Sysroot == "")
549 return false;
550 for (; !Path.empty(); Path = sys::path::parent_path(Path))
551 if (sys::fs::equivalent(Config->Sysroot, Path))
552 return true;
553 return false;
554}
555
Rui Ueyama07320e42016-04-20 20:13:41 +0000556// Entry point.
557void elf::readLinkerScript(MemoryBufferRef MB) {
Simon Atanasyan16b0cc92015-11-26 05:53:00 +0000558 StringRef Path = MB.getBufferIdentifier();
Rui Ueyama07320e42016-04-20 20:13:41 +0000559 ScriptParser(MB.getBuffer(), isUnderSysroot(Path)).run();
Rui Ueyamaf7c5fbb2015-09-30 17:23:26 +0000560}
Rui Ueyama1ebc8ed2016-02-12 21:47:28 +0000561
Rui Ueyama07320e42016-04-20 20:13:41 +0000562template class elf::LinkerScript<ELF32LE>;
563template class elf::LinkerScript<ELF32BE>;
564template class elf::LinkerScript<ELF64LE>;
565template class elf::LinkerScript<ELF64BE>;