blob: 43d8022af27abd331a67f023933fcf8a5fb015cc [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
Rui Ueyama8ec77e62016-04-21 22:00:51 +000038static bool matchStr(StringRef S, StringRef T);
39
George Rimar652852c2016-04-16 10:10:32 +000040static uint64_t getInteger(StringRef S) {
41 uint64_t V;
42 if (S.getAsInteger(0, V)) {
43 error("malformed number: " + S);
44 return 0;
45 }
46 return V;
47}
48
Rui Ueyama960504b2016-04-19 18:58:11 +000049static int precedence(StringRef Op) {
50 return StringSwitch<int>(Op)
51 .Case("*", 4)
52 .Case("/", 3)
53 .Case("+", 2)
54 .Case("-", 2)
55 .Case("&", 1)
56 .Default(-1);
57}
58
59static StringRef next(ArrayRef<StringRef> &Tokens) {
60 if (Tokens.empty()) {
61 error("no next token");
62 return "";
63 }
64 StringRef Tok = Tokens.front();
65 Tokens = Tokens.slice(1);
66 return Tok;
67}
68
Rui Ueyama60118112016-04-20 20:54:13 +000069static bool expect(ArrayRef<StringRef> &Tokens, StringRef S) {
70 if (Tokens.empty()) {
71 error(S + " expected");
72 return false;
73 }
74 StringRef Tok = Tokens.front();
75 if (Tok != S) {
76 error(S + " expected, but got " + Tok);
77 return false;
78 }
79 Tokens = Tokens.slice(1);
80 return true;
81}
82
Rui Ueyama960504b2016-04-19 18:58:11 +000083// 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 ".".
Rui Ueyamac998a8c2016-04-22 00:03:13 +000086template <class ELFT>
87uint64_t LinkerScript<ELFT>::parsePrimary(ArrayRef<StringRef> &Tokens) {
Rui Ueyama960504b2016-04-19 18:58:11 +000088 StringRef Tok = next(Tokens);
89 if (Tok == ".")
90 return Dot;
91 if (Tok == "(") {
Rui Ueyamac998a8c2016-04-22 00:03:13 +000092 uint64_t V = parseExpr(Tokens);
Rui Ueyama60118112016-04-20 20:54:13 +000093 if (!expect(Tokens, ")"))
94 return 0;
Rui Ueyama960504b2016-04-19 18:58:11 +000095 return V;
96 }
97 return getInteger(Tok);
98}
99
100static uint64_t apply(StringRef Op, uint64_t L, uint64_t R) {
101 if (Op == "+")
102 return L + R;
103 if (Op == "-")
104 return L - R;
105 if (Op == "*")
106 return L * R;
107 if (Op == "/") {
108 if (R == 0) {
109 error("division by zero");
George Rimar652852c2016-04-16 10:10:32 +0000110 return 0;
111 }
Rui Ueyama960504b2016-04-19 18:58:11 +0000112 return L / R;
George Rimar652852c2016-04-16 10:10:32 +0000113 }
Rui Ueyama960504b2016-04-19 18:58:11 +0000114 if (Op == "&")
115 return L & R;
Rui Ueyama7a81d672016-04-19 19:04:03 +0000116 llvm_unreachable("invalid operator");
Rui Ueyama960504b2016-04-19 18:58:11 +0000117 return 0;
118}
119
120// This is an operator-precedence parser to evaluate
121// arithmetic expressions in SECTIONS command.
Rui Ueyama99e519c2016-04-20 20:48:25 +0000122// Tokens should start with an operator.
Rui Ueyamac998a8c2016-04-22 00:03:13 +0000123template <class ELFT>
124uint64_t LinkerScript<ELFT>::parseExpr1(ArrayRef<StringRef> &Tokens,
125 uint64_t Lhs, int MinPrec) {
Rui Ueyama960504b2016-04-19 18:58:11 +0000126 while (!Tokens.empty()) {
127 // Read an operator and an expression.
128 StringRef Op1 = Tokens.front();
129 if (precedence(Op1) < MinPrec)
130 return Lhs;
131 next(Tokens);
Rui Ueyamac998a8c2016-04-22 00:03:13 +0000132 uint64_t Rhs = parsePrimary(Tokens);
Rui Ueyama960504b2016-04-19 18:58:11 +0000133
134 // Evaluate the remaining part of the expression first if the
135 // next operator has greater precedence than the previous one.
136 // For example, if we have read "+" and "3", and if the next
137 // operator is "*", then we'll evaluate 3 * ... part first.
138 while (!Tokens.empty()) {
139 StringRef Op2 = Tokens.front();
140 if (precedence(Op2) <= precedence(Op1))
141 break;
Rui Ueyamac998a8c2016-04-22 00:03:13 +0000142 Rhs = parseExpr1(Tokens, Rhs, precedence(Op2));
Rui Ueyama960504b2016-04-19 18:58:11 +0000143 }
144
145 Lhs = apply(Op1, Lhs, Rhs);
146 }
147 return Lhs;
148}
149
Rui Ueyamac998a8c2016-04-22 00:03:13 +0000150template <class ELFT>
151uint64_t LinkerScript<ELFT>::parseExpr(ArrayRef<StringRef> &Tokens) {
152 uint64_t V = parsePrimary(Tokens);
153 return parseExpr1(Tokens, V, 0);
Rui Ueyama99e519c2016-04-20 20:48:25 +0000154}
155
Rui Ueyama960504b2016-04-19 18:58:11 +0000156// Evaluates the expression given by list of tokens.
Rui Ueyamac998a8c2016-04-22 00:03:13 +0000157template <class ELFT>
158uint64_t LinkerScript<ELFT>::evaluate(ArrayRef<StringRef> Tokens) {
159 uint64_t V = parseExpr(Tokens);
Rui Ueyama960504b2016-04-19 18:58:11 +0000160 if (!Tokens.empty())
161 error("stray token: " + Tokens[0]);
162 return V;
George Rimar652852c2016-04-16 10:10:32 +0000163}
164
Rui Ueyama1ebc8ed2016-02-12 21:47:28 +0000165template <class ELFT>
Rui Ueyama8ec77e62016-04-21 22:00:51 +0000166StringRef LinkerScript<ELFT>::getOutputSection(InputSectionBase<ELFT> *S) {
Rui Ueyama07320e42016-04-20 20:13:41 +0000167 for (SectionRule &R : Opt.Sections)
Rui Ueyama1ebc8ed2016-02-12 21:47:28 +0000168 if (R.match(S))
Rui Ueyama8ec77e62016-04-21 22:00:51 +0000169 return R.Dest;
170 return "";
Rui Ueyama717677a2016-02-11 21:17:59 +0000171}
172
Rui Ueyama1ebc8ed2016-02-12 21:47:28 +0000173template <class ELFT>
Rui Ueyama07320e42016-04-20 20:13:41 +0000174bool LinkerScript<ELFT>::isDiscarded(InputSectionBase<ELFT> *S) {
Rui Ueyama1ebc8ed2016-02-12 21:47:28 +0000175 return getOutputSection(S) == "/DISCARD/";
Rui Ueyama717677a2016-02-11 21:17:59 +0000176}
177
Rui Ueyama07320e42016-04-20 20:13:41 +0000178template <class ELFT>
179bool LinkerScript<ELFT>::shouldKeep(InputSectionBase<ELFT> *S) {
Rui Ueyama8ec77e62016-04-21 22:00:51 +0000180 for (StringRef Pat : Opt.KeptSections)
181 if (matchStr(Pat, S->getSectionName()))
182 return true;
183 return false;
George Rimar481c2ce2016-02-23 07:47:54 +0000184}
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();
Rui Ueyamac3e2a4b2016-04-21 20:30:00 +0000207 if (getSectionIndex(Name) == INT_MAX)
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.
Rui Ueyamac998a8c2016-04-22 00:03:13 +0000212 Dot = Out<ELFT>::ElfHeader->getSize() + Out<ELFT>::ProgramHeaders->getSize();
George Rimar652852c2016-04-16 10:10:32 +0000213 uintX_t ThreadBssOffset = 0;
George Rimar652852c2016-04-16 10:10:32 +0000214
Rui Ueyama07320e42016-04-20 20:13:41 +0000215 for (SectionsCommand &Cmd : Opt.Commands) {
Rui Ueyama9e957a02016-04-18 21:00:45 +0000216 if (Cmd.Kind == ExprKind) {
Rui Ueyamac998a8c2016-04-22 00:03:13 +0000217 Dot = evaluate(Cmd.Expr);
George Rimar652852c2016-04-16 10:10:32 +0000218 continue;
219 }
220
George Rimardbbd8b12016-04-21 11:21:48 +0000221 OutputSectionBase<ELFT> *Sec = findSection<ELFT>(Sections, Cmd.SectionName);
Rui Ueyama7c18c282016-04-18 21:00:40 +0000222 if (!Sec)
George Rimar652852c2016-04-16 10:10:32 +0000223 continue;
224
George Rimar652852c2016-04-16 10:10:32 +0000225 if ((Sec->getFlags() & SHF_TLS) && Sec->getType() == SHT_NOBITS) {
Rui Ueyamac998a8c2016-04-22 00:03:13 +0000226 uintX_t TVA = Dot + ThreadBssOffset;
Rui Ueyama7c18c282016-04-18 21:00:40 +0000227 TVA = alignTo(TVA, Sec->getAlign());
George Rimar652852c2016-04-16 10:10:32 +0000228 Sec->setVA(TVA);
Rui Ueyamac998a8c2016-04-22 00:03:13 +0000229 ThreadBssOffset = TVA - Dot + Sec->getSize();
George Rimar652852c2016-04-16 10:10:32 +0000230 continue;
231 }
232
233 if (Sec->getFlags() & SHF_ALLOC) {
Rui Ueyamac998a8c2016-04-22 00:03:13 +0000234 Dot = alignTo(Dot, Sec->getAlign());
235 Sec->setVA(Dot);
236 Dot += Sec->getSize();
George Rimar652852c2016-04-16 10:10:32 +0000237 continue;
238 }
239 }
240}
241
Rui Ueyama07320e42016-04-20 20:13:41 +0000242template <class ELFT>
243ArrayRef<uint8_t> LinkerScript<ELFT>::getFiller(StringRef Name) {
244 auto I = Opt.Filler.find(Name);
245 if (I == Opt.Filler.end())
Rui Ueyama3e808972016-02-28 05:09:11 +0000246 return {};
247 return I->second;
George Rimare2ee72b2016-02-26 14:48:31 +0000248}
249
Rui Ueyamac3e2a4b2016-04-21 20:30:00 +0000250// Returns the index of the given section name in linker script
251// SECTIONS commands. Sections are laid out as the same order as they
252// were in the script. If a given name did not appear in the script,
253// it returns INT_MAX, so that it will be laid out at end of file.
George Rimar71b26e92016-04-21 10:22:02 +0000254template <class ELFT>
Rui Ueyamac3e2a4b2016-04-21 20:30:00 +0000255int LinkerScript<ELFT>::getSectionIndex(StringRef Name) {
George Rimar71b26e92016-04-21 10:22:02 +0000256 auto Begin = Opt.Commands.begin();
257 auto End = Opt.Commands.end();
258 auto I = std::find_if(Begin, End, [&](SectionsCommand &N) {
259 return N.Kind == SectionKind && N.SectionName == Name;
260 });
Rui Ueyamac3e2a4b2016-04-21 20:30:00 +0000261 return I == End ? INT_MAX : (I - Begin);
George Rimar71b26e92016-04-21 10:22:02 +0000262}
263
264// A compartor to sort output sections. Returns -1 or 1 if
265// A or B are mentioned in linker script. Otherwise, returns 0.
Rui Ueyama07320e42016-04-20 20:13:41 +0000266template <class ELFT>
267int LinkerScript<ELFT>::compareSections(StringRef A, StringRef B) {
Rui Ueyamac3e2a4b2016-04-21 20:30:00 +0000268 int I = getSectionIndex(A);
269 int J = getSectionIndex(B);
270 if (I == INT_MAX && J == INT_MAX)
Rui Ueyama717677a2016-02-11 21:17:59 +0000271 return 0;
272 return I < J ? -1 : 1;
273}
274
George Rimarcb2aeb62016-02-24 08:49:50 +0000275// Returns true if S matches T. S can contain glob meta-characters.
276// The asterisk ('*') matches zero or more characacters, and the question
277// mark ('?') matches one character.
Rui Ueyama1ebc8ed2016-02-12 21:47:28 +0000278static bool matchStr(StringRef S, StringRef T) {
279 for (;;) {
280 if (S.empty())
281 return T.empty();
282 if (S[0] == '*') {
283 S = S.substr(1);
284 if (S.empty())
285 // Fast path. If a pattern is '*', it matches anything.
286 return true;
287 for (size_t I = 0, E = T.size(); I < E; ++I)
288 if (matchStr(S, T.substr(I)))
289 return true;
290 return false;
291 }
George Rimarcb2aeb62016-02-24 08:49:50 +0000292 if (T.empty() || (S[0] != T[0] && S[0] != '?'))
Rui Ueyama1ebc8ed2016-02-12 21:47:28 +0000293 return false;
294 S = S.substr(1);
295 T = T.substr(1);
296 }
297}
298
299template <class ELFT> bool SectionRule::match(InputSectionBase<ELFT> *S) {
300 return matchStr(SectionPattern, S->getSectionName());
301}
302
Rui Ueyama07320e42016-04-20 20:13:41 +0000303class elf::ScriptParser : public ScriptParserBase {
George Rimarc3794e52016-02-24 09:21:47 +0000304 typedef void (ScriptParser::*Handler)();
305
Rui Ueyamaf7c5fbb2015-09-30 17:23:26 +0000306public:
Rui Ueyama07320e42016-04-20 20:13:41 +0000307 ScriptParser(StringRef S, bool B) : ScriptParserBase(S), IsUnderSysroot(B) {}
George Rimarf23b2322016-02-19 10:45:45 +0000308
Adhemerval Zanellae77b5bf2016-04-06 20:59:11 +0000309 void run() override;
Rui Ueyamaf7c5fbb2015-09-30 17:23:26 +0000310
311private:
Rui Ueyama52a15092015-10-11 03:28:42 +0000312 void addFile(StringRef Path);
313
Rui Ueyamaf7c5fbb2015-09-30 17:23:26 +0000314 void readAsNeeded();
Denis Protivensky90c50992015-10-08 06:48:38 +0000315 void readEntry();
George Rimar83f406c2015-10-19 17:35:12 +0000316 void readExtern();
Rui Ueyamaf7c5fbb2015-09-30 17:23:26 +0000317 void readGroup();
Rui Ueyama31aa1f82015-10-11 01:31:55 +0000318 void readInclude();
George Rimarc3794e52016-02-24 09:21:47 +0000319 void readNothing() {}
Rui Ueyamaee592822015-10-07 00:25:09 +0000320 void readOutput();
Davide Italiano9159ce92015-10-12 21:50:08 +0000321 void readOutputArch();
Rui Ueyamaf7c5fbb2015-09-30 17:23:26 +0000322 void readOutputFormat();
Davide Italiano68a39a62015-10-08 17:51:41 +0000323 void readSearchDir();
Denis Protivensky8e3b38a2015-11-12 09:52:08 +0000324 void readSections();
325
George Rimar652852c2016-04-16 10:10:32 +0000326 void readLocationCounterValue();
Denis Protivensky8e3b38a2015-11-12 09:52:08 +0000327 void readOutputSectionDescription();
Rui Ueyama8ec77e62016-04-21 22:00:51 +0000328 void readSectionPatterns(StringRef OutSec);
Rui Ueyamaf7c5fbb2015-09-30 17:23:26 +0000329
George Rimarc3794e52016-02-24 09:21:47 +0000330 const static StringMap<Handler> Cmd;
Rui Ueyama07320e42016-04-20 20:13:41 +0000331 ScriptConfiguration &Opt = *ScriptConfig;
332 StringSaver Saver = {ScriptConfig->Alloc};
Simon Atanasyan16b0cc92015-11-26 05:53:00 +0000333 bool IsUnderSysroot;
Rui Ueyamaf7c5fbb2015-09-30 17:23:26 +0000334};
Rui Ueyamaf7c5fbb2015-09-30 17:23:26 +0000335
Rafael Espindolae0df00b2016-02-28 00:25:54 +0000336const StringMap<elf::ScriptParser::Handler> elf::ScriptParser::Cmd = {
George Rimarc3794e52016-02-24 09:21:47 +0000337 {"ENTRY", &ScriptParser::readEntry},
338 {"EXTERN", &ScriptParser::readExtern},
339 {"GROUP", &ScriptParser::readGroup},
340 {"INCLUDE", &ScriptParser::readInclude},
341 {"INPUT", &ScriptParser::readGroup},
342 {"OUTPUT", &ScriptParser::readOutput},
343 {"OUTPUT_ARCH", &ScriptParser::readOutputArch},
344 {"OUTPUT_FORMAT", &ScriptParser::readOutputFormat},
345 {"SEARCH_DIR", &ScriptParser::readSearchDir},
346 {"SECTIONS", &ScriptParser::readSections},
347 {";", &ScriptParser::readNothing}};
348
Rui Ueyama717677a2016-02-11 21:17:59 +0000349void ScriptParser::run() {
Rui Ueyamaf7c5fbb2015-09-30 17:23:26 +0000350 while (!atEOF()) {
351 StringRef Tok = next();
George Rimarc3794e52016-02-24 09:21:47 +0000352 if (Handler Fn = Cmd.lookup(Tok))
353 (this->*Fn)();
354 else
George Rimar57610422016-03-11 14:43:02 +0000355 setError("unknown directive: " + Tok);
Rui Ueyamaf7c5fbb2015-09-30 17:23:26 +0000356 }
357}
358
Rui Ueyama717677a2016-02-11 21:17:59 +0000359void ScriptParser::addFile(StringRef S) {
Simon Atanasyan16b0cc92015-11-26 05:53:00 +0000360 if (IsUnderSysroot && S.startswith("/")) {
361 SmallString<128> Path;
362 (Config->Sysroot + S).toStringRef(Path);
363 if (sys::fs::exists(Path)) {
364 Driver->addFile(Saver.save(Path.str()));
365 return;
366 }
367 }
368
Rui Ueyamaf03f3cc2015-10-13 00:09:21 +0000369 if (sys::path::is_absolute(S)) {
Rui Ueyama52a15092015-10-11 03:28:42 +0000370 Driver->addFile(S);
371 } else if (S.startswith("=")) {
372 if (Config->Sysroot.empty())
373 Driver->addFile(S.substr(1));
374 else
375 Driver->addFile(Saver.save(Config->Sysroot + "/" + S.substr(1)));
376 } else if (S.startswith("-l")) {
Rui Ueyama21eecb42016-02-02 21:13:09 +0000377 Driver->addLibrary(S.substr(2));
Simon Atanasyana1b8fc32015-11-26 20:23:46 +0000378 } else if (sys::fs::exists(S)) {
379 Driver->addFile(S);
Rui Ueyama52a15092015-10-11 03:28:42 +0000380 } else {
381 std::string Path = findFromSearchPaths(S);
382 if (Path.empty())
George Rimar777f9632016-03-12 08:31:34 +0000383 setError("unable to find " + S);
Rui Ueyama025d59b2016-02-02 20:27:59 +0000384 else
385 Driver->addFile(Saver.save(Path));
Rui Ueyama52a15092015-10-11 03:28:42 +0000386 }
387}
388
Rui Ueyama717677a2016-02-11 21:17:59 +0000389void ScriptParser::readAsNeeded() {
Rui Ueyamaf7c5fbb2015-09-30 17:23:26 +0000390 expect("(");
Rui Ueyama35da9b62015-10-11 20:59:12 +0000391 bool Orig = Config->AsNeeded;
392 Config->AsNeeded = true;
Rui Ueyama025d59b2016-02-02 20:27:59 +0000393 while (!Error) {
Rui Ueyamaf7c5fbb2015-09-30 17:23:26 +0000394 StringRef Tok = next();
395 if (Tok == ")")
Rui Ueyama35da9b62015-10-11 20:59:12 +0000396 break;
Rui Ueyama52a15092015-10-11 03:28:42 +0000397 addFile(Tok);
Rui Ueyamaf7c5fbb2015-09-30 17:23:26 +0000398 }
Rui Ueyama35da9b62015-10-11 20:59:12 +0000399 Config->AsNeeded = Orig;
Rui Ueyamaf7c5fbb2015-09-30 17:23:26 +0000400}
401
Rui Ueyama717677a2016-02-11 21:17:59 +0000402void ScriptParser::readEntry() {
Denis Protivensky90c50992015-10-08 06:48:38 +0000403 // -e <symbol> takes predecence over ENTRY(<symbol>).
404 expect("(");
405 StringRef Tok = next();
406 if (Config->Entry.empty())
407 Config->Entry = Tok;
408 expect(")");
409}
410
Rui Ueyama717677a2016-02-11 21:17:59 +0000411void ScriptParser::readExtern() {
George Rimar83f406c2015-10-19 17:35:12 +0000412 expect("(");
Rui Ueyama025d59b2016-02-02 20:27:59 +0000413 while (!Error) {
George Rimar83f406c2015-10-19 17:35:12 +0000414 StringRef Tok = next();
415 if (Tok == ")")
416 return;
417 Config->Undefined.push_back(Tok);
418 }
419}
420
Rui Ueyama717677a2016-02-11 21:17:59 +0000421void ScriptParser::readGroup() {
Rui Ueyamaf7c5fbb2015-09-30 17:23:26 +0000422 expect("(");
Rui Ueyama025d59b2016-02-02 20:27:59 +0000423 while (!Error) {
Rui Ueyamaf7c5fbb2015-09-30 17:23:26 +0000424 StringRef Tok = next();
425 if (Tok == ")")
426 return;
427 if (Tok == "AS_NEEDED") {
428 readAsNeeded();
429 continue;
430 }
Rui Ueyama52a15092015-10-11 03:28:42 +0000431 addFile(Tok);
Rui Ueyamaf7c5fbb2015-09-30 17:23:26 +0000432 }
433}
434
Rui Ueyama717677a2016-02-11 21:17:59 +0000435void ScriptParser::readInclude() {
Rui Ueyama31aa1f82015-10-11 01:31:55 +0000436 StringRef Tok = next();
437 auto MBOrErr = MemoryBuffer::getFile(Tok);
Rui Ueyama025d59b2016-02-02 20:27:59 +0000438 if (!MBOrErr) {
George Rimar57610422016-03-11 14:43:02 +0000439 setError("cannot open " + Tok);
Rui Ueyama025d59b2016-02-02 20:27:59 +0000440 return;
441 }
Rui Ueyama31aa1f82015-10-11 01:31:55 +0000442 std::unique_ptr<MemoryBuffer> &MB = *MBOrErr;
Rui Ueyamaa47ee682015-10-11 01:53:04 +0000443 StringRef S = Saver.save(MB->getMemBufferRef().getBuffer());
444 std::vector<StringRef> V = tokenize(S);
Rui Ueyama31aa1f82015-10-11 01:31:55 +0000445 Tokens.insert(Tokens.begin() + Pos, V.begin(), V.end());
Rui Ueyama31aa1f82015-10-11 01:31:55 +0000446}
447
Rui Ueyama717677a2016-02-11 21:17:59 +0000448void ScriptParser::readOutput() {
Rui Ueyamaee592822015-10-07 00:25:09 +0000449 // -o <file> takes predecence over OUTPUT(<file>).
450 expect("(");
451 StringRef Tok = next();
452 if (Config->OutputFile.empty())
453 Config->OutputFile = Tok;
454 expect(")");
455}
456
Rui Ueyama717677a2016-02-11 21:17:59 +0000457void ScriptParser::readOutputArch() {
Davide Italiano9159ce92015-10-12 21:50:08 +0000458 // Error checking only for now.
459 expect("(");
460 next();
461 expect(")");
462}
463
Rui Ueyama717677a2016-02-11 21:17:59 +0000464void ScriptParser::readOutputFormat() {
Rui Ueyamaf7c5fbb2015-09-30 17:23:26 +0000465 // Error checking only for now.
466 expect("(");
467 next();
Davide Italiano6836c612015-10-12 21:08:41 +0000468 StringRef Tok = next();
469 if (Tok == ")")
470 return;
Rui Ueyama025d59b2016-02-02 20:27:59 +0000471 if (Tok != ",") {
George Rimar57610422016-03-11 14:43:02 +0000472 setError("unexpected token: " + Tok);
Rui Ueyama025d59b2016-02-02 20:27:59 +0000473 return;
474 }
Davide Italiano6836c612015-10-12 21:08:41 +0000475 next();
476 expect(",");
477 next();
Rui Ueyamaf7c5fbb2015-09-30 17:23:26 +0000478 expect(")");
479}
480
Rui Ueyama717677a2016-02-11 21:17:59 +0000481void ScriptParser::readSearchDir() {
Davide Italiano68a39a62015-10-08 17:51:41 +0000482 expect("(");
Rafael Espindola06501922016-03-08 17:13:12 +0000483 Config->SearchPaths.push_back(next());
Davide Italiano68a39a62015-10-08 17:51:41 +0000484 expect(")");
485}
486
Rui Ueyama717677a2016-02-11 21:17:59 +0000487void ScriptParser::readSections() {
Rui Ueyama07320e42016-04-20 20:13:41 +0000488 Opt.DoLayout = true;
Denis Protivensky8e3b38a2015-11-12 09:52:08 +0000489 expect("{");
George Rimar652852c2016-04-16 10:10:32 +0000490 while (!Error && !skip("}")) {
491 StringRef Tok = peek();
492 if (Tok == ".")
493 readLocationCounterValue();
494 else
495 readOutputSectionDescription();
496 }
Denis Protivensky8e3b38a2015-11-12 09:52:08 +0000497}
498
Rui Ueyama8ec77e62016-04-21 22:00:51 +0000499void ScriptParser::readSectionPatterns(StringRef OutSec) {
George Rimar481c2ce2016-02-23 07:47:54 +0000500 expect("(");
501 while (!Error && !skip(")"))
Rui Ueyama8ec77e62016-04-21 22:00:51 +0000502 Opt.Sections.emplace_back(OutSec, next());
George Rimar481c2ce2016-02-23 07:47:54 +0000503}
504
George Rimar652852c2016-04-16 10:10:32 +0000505void ScriptParser::readLocationCounterValue() {
506 expect(".");
507 expect("=");
Rui Ueyama07320e42016-04-20 20:13:41 +0000508 Opt.Commands.push_back({ExprKind, {}, ""});
509 SectionsCommand &Cmd = Opt.Commands.back();
George Rimar652852c2016-04-16 10:10:32 +0000510 while (!Error) {
511 StringRef Tok = next();
512 if (Tok == ";")
513 break;
Rui Ueyama9e957a02016-04-18 21:00:45 +0000514 Cmd.Expr.push_back(Tok);
George Rimar652852c2016-04-16 10:10:32 +0000515 }
Rui Ueyama9e957a02016-04-18 21:00:45 +0000516 if (Cmd.Expr.empty())
George Rimar652852c2016-04-16 10:10:32 +0000517 error("error in location counter expression");
518}
519
Rui Ueyama717677a2016-02-11 21:17:59 +0000520void ScriptParser::readOutputSectionDescription() {
Rui Ueyama3e808972016-02-28 05:09:11 +0000521 StringRef OutSec = next();
Rui Ueyama07320e42016-04-20 20:13:41 +0000522 Opt.Commands.push_back({SectionKind, {}, OutSec});
Denis Protivensky8e3b38a2015-11-12 09:52:08 +0000523 expect(":");
524 expect("{");
Rui Ueyama8ec77e62016-04-21 22:00:51 +0000525
Rui Ueyama025d59b2016-02-02 20:27:59 +0000526 while (!Error && !skip("}")) {
George Rimar481c2ce2016-02-23 07:47:54 +0000527 StringRef Tok = next();
528 if (Tok == "*") {
Rui Ueyama8ec77e62016-04-21 22:00:51 +0000529 expect("(");
530 while (!Error && !skip(")"))
531 Opt.Sections.emplace_back(OutSec, next());
George Rimar481c2ce2016-02-23 07:47:54 +0000532 } else if (Tok == "KEEP") {
533 expect("(");
Rui Ueyama8ec77e62016-04-21 22:00:51 +0000534 expect("*");
535 expect("(");
536 while (!Error && !skip(")")) {
537 StringRef Sec = next();
538 Opt.Sections.emplace_back(OutSec, Sec);
539 Opt.KeptSections.push_back(Sec);
540 }
George Rimar481c2ce2016-02-23 07:47:54 +0000541 expect(")");
542 } else {
George Rimar777f9632016-03-12 08:31:34 +0000543 setError("unknown command " + Tok);
George Rimar481c2ce2016-02-23 07:47:54 +0000544 }
Denis Protivensky8e3b38a2015-11-12 09:52:08 +0000545 }
Rui Ueyama8ec77e62016-04-21 22:00:51 +0000546
George Rimare2ee72b2016-02-26 14:48:31 +0000547 StringRef Tok = peek();
548 if (Tok.startswith("=")) {
549 if (!Tok.startswith("=0x")) {
Rui Ueyama3ed2f062016-03-13 03:17:44 +0000550 setError("filler should be a hexadecimal value");
George Rimare2ee72b2016-02-26 14:48:31 +0000551 return;
552 }
Rui Ueyama3e808972016-02-28 05:09:11 +0000553 Tok = Tok.substr(3);
Rui Ueyama07320e42016-04-20 20:13:41 +0000554 Opt.Filler[OutSec] = parseHex(Tok);
George Rimare2ee72b2016-02-26 14:48:31 +0000555 next();
556 }
Denis Protivensky8e3b38a2015-11-12 09:52:08 +0000557}
558
Simon Atanasyan16b0cc92015-11-26 05:53:00 +0000559static bool isUnderSysroot(StringRef Path) {
560 if (Config->Sysroot == "")
561 return false;
562 for (; !Path.empty(); Path = sys::path::parent_path(Path))
563 if (sys::fs::equivalent(Config->Sysroot, Path))
564 return true;
565 return false;
566}
567
Rui Ueyama07320e42016-04-20 20:13:41 +0000568// Entry point.
569void elf::readLinkerScript(MemoryBufferRef MB) {
Simon Atanasyan16b0cc92015-11-26 05:53:00 +0000570 StringRef Path = MB.getBufferIdentifier();
Rui Ueyama07320e42016-04-20 20:13:41 +0000571 ScriptParser(MB.getBuffer(), isUnderSysroot(Path)).run();
Rui Ueyamaf7c5fbb2015-09-30 17:23:26 +0000572}
Rui Ueyama1ebc8ed2016-02-12 21:47:28 +0000573
Rui Ueyama07320e42016-04-20 20:13:41 +0000574template class elf::LinkerScript<ELF32LE>;
575template class elf::LinkerScript<ELF32BE>;
576template class elf::LinkerScript<ELF64LE>;
577template class elf::LinkerScript<ELF64BE>;