blob: 5387b026ce221cc94982f0a797c51689acc6e22c [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
Rui Ueyama9c1112d2016-04-23 00:04:03 +000040// This is an operator-precedence parser to parse and evaluate
41// a linker script expression. For each linker script arithmetic
42// expression (e.g. ". = . + 0x1000"), a new instance of ExprParser
43// is created and ran.
44namespace {
45class ExprParser : public ScriptParserBase {
46public:
47 ExprParser(std::vector<StringRef> &Tokens, uint64_t Dot)
48 : ScriptParserBase(Tokens), Dot(Dot) {}
49
50 uint64_t run();
51
52private:
53 uint64_t parsePrimary();
54 uint64_t parseTernary(uint64_t Cond);
55 uint64_t apply(StringRef Op, uint64_t L, uint64_t R);
56 uint64_t parseExpr1(uint64_t Lhs, int MinPrec);
57 uint64_t parseExpr();
58
59 uint64_t Dot;
60};
61}
62
Rui Ueyama960504b2016-04-19 18:58:11 +000063static int precedence(StringRef Op) {
64 return StringSwitch<int>(Op)
65 .Case("*", 4)
George Rimarab939062016-04-25 08:14:41 +000066 .Case("/", 4)
67 .Case("+", 3)
68 .Case("-", 3)
69 .Case("<", 2)
70 .Case(">", 2)
71 .Case(">=", 2)
72 .Case("<=", 2)
73 .Case("==", 2)
74 .Case("!=", 2)
Rui Ueyama960504b2016-04-19 18:58:11 +000075 .Case("&", 1)
76 .Default(-1);
77}
78
Rui Ueyama9c1112d2016-04-23 00:04:03 +000079static uint64_t evalExpr(std::vector<StringRef> &Tokens, uint64_t Dot) {
80 return ExprParser(Tokens, Dot).run();
Rui Ueyama960504b2016-04-19 18:58:11 +000081}
82
Rui Ueyama9c1112d2016-04-23 00:04:03 +000083uint64_t ExprParser::run() {
84 uint64_t V = parseExpr();
85 if (!atEOF() && !Error)
86 setError("stray token: " + peek());
87 return V;
Rui Ueyama60118112016-04-20 20:54:13 +000088}
89
Rui Ueyama960504b2016-04-19 18:58:11 +000090// This is a part of the operator-precedence parser to evaluate
91// arithmetic expressions in SECTIONS command. This function evaluates an
Rui Ueyamae29a9752016-04-22 21:02:27 +000092// integer literal, a parenthesized expression, the ALIGN function,
93// or the special variable ".".
Rui Ueyama9c1112d2016-04-23 00:04:03 +000094uint64_t ExprParser::parsePrimary() {
95 StringRef Tok = next();
Rui Ueyama960504b2016-04-19 18:58:11 +000096 if (Tok == ".")
97 return Dot;
98 if (Tok == "(") {
Rui Ueyama9c1112d2016-04-23 00:04:03 +000099 uint64_t V = parseExpr();
100 expect(")");
Rui Ueyama960504b2016-04-19 18:58:11 +0000101 return V;
102 }
George Rimardffc1412016-04-22 11:40:53 +0000103 if (Tok == "ALIGN") {
Rui Ueyama9c1112d2016-04-23 00:04:03 +0000104 expect("(");
105 uint64_t V = parseExpr();
106 expect(")");
George Rimardffc1412016-04-22 11:40:53 +0000107 return alignTo(Dot, V);
108 }
Rui Ueyama5fa60982016-04-22 21:05:04 +0000109 uint64_t V = 0;
110 if (Tok.getAsInteger(0, V))
Rui Ueyama9c1112d2016-04-23 00:04:03 +0000111 setError("malformed number: " + Tok);
Rui Ueyama5fa60982016-04-22 21:05:04 +0000112 return V;
Rui Ueyama960504b2016-04-19 18:58:11 +0000113}
114
Rui Ueyama9c1112d2016-04-23 00:04:03 +0000115uint64_t ExprParser::parseTernary(uint64_t Cond) {
116 next();
117 uint64_t V = parseExpr();
118 expect(":");
119 uint64_t W = parseExpr();
George Rimarfba45c42016-04-22 11:28:54 +0000120 return Cond ? V : W;
121}
122
Rui Ueyama9c1112d2016-04-23 00:04:03 +0000123uint64_t ExprParser::apply(StringRef Op, uint64_t L, uint64_t R) {
Rui Ueyama960504b2016-04-19 18:58:11 +0000124 if (Op == "*")
125 return L * R;
126 if (Op == "/") {
127 if (R == 0) {
128 error("division by zero");
George Rimar652852c2016-04-16 10:10:32 +0000129 return 0;
130 }
Rui Ueyama960504b2016-04-19 18:58:11 +0000131 return L / R;
George Rimar652852c2016-04-16 10:10:32 +0000132 }
George Rimarab939062016-04-25 08:14:41 +0000133 if (Op == "+")
134 return L + R;
135 if (Op == "-")
136 return L - R;
137 if (Op == "<")
138 return L < R;
139 if (Op == ">")
140 return L > R;
141 if (Op == ">=")
142 return L >= R;
143 if (Op == "<=")
144 return L <= R;
145 if (Op == "==")
146 return L == R;
147 if (Op == "!=")
148 return L != R;
Rui Ueyama960504b2016-04-19 18:58:11 +0000149 if (Op == "&")
150 return L & R;
Rui Ueyama7a81d672016-04-19 19:04:03 +0000151 llvm_unreachable("invalid operator");
Rui Ueyama960504b2016-04-19 18:58:11 +0000152 return 0;
153}
154
Rui Ueyama9c1112d2016-04-23 00:04:03 +0000155// This is a part of the operator-precedence parser.
156// This function assumes that the remaining token stream starts
157// with an operator.
158uint64_t ExprParser::parseExpr1(uint64_t Lhs, int MinPrec) {
159 while (!atEOF()) {
Rui Ueyama960504b2016-04-19 18:58:11 +0000160 // Read an operator and an expression.
Rui Ueyama9c1112d2016-04-23 00:04:03 +0000161 StringRef Op1 = peek();
George Rimarfba45c42016-04-22 11:28:54 +0000162 if (Op1 == "?")
Rui Ueyama9c1112d2016-04-23 00:04:03 +0000163 return parseTernary(Lhs);
Rui Ueyama960504b2016-04-19 18:58:11 +0000164 if (precedence(Op1) < MinPrec)
165 return Lhs;
Rui Ueyama9c1112d2016-04-23 00:04:03 +0000166 next();
167 uint64_t Rhs = parsePrimary();
Rui Ueyama960504b2016-04-19 18:58:11 +0000168
169 // Evaluate the remaining part of the expression first if the
170 // next operator has greater precedence than the previous one.
171 // For example, if we have read "+" and "3", and if the next
172 // operator is "*", then we'll evaluate 3 * ... part first.
Rui Ueyama9c1112d2016-04-23 00:04:03 +0000173 while (!atEOF()) {
174 StringRef Op2 = peek();
Rui Ueyama960504b2016-04-19 18:58:11 +0000175 if (precedence(Op2) <= precedence(Op1))
176 break;
Rui Ueyama9c1112d2016-04-23 00:04:03 +0000177 Rhs = parseExpr1(Rhs, precedence(Op2));
Rui Ueyama960504b2016-04-19 18:58:11 +0000178 }
179
180 Lhs = apply(Op1, Lhs, Rhs);
181 }
182 return Lhs;
183}
184
Rui Ueyama9c1112d2016-04-23 00:04:03 +0000185// Reads and evaluates an arithmetic expression.
186uint64_t ExprParser::parseExpr() { return parseExpr1(parsePrimary(), 0); }
George Rimar652852c2016-04-16 10:10:32 +0000187
Rui Ueyama1ebc8ed2016-02-12 21:47:28 +0000188template <class ELFT>
Rui Ueyama8ec77e62016-04-21 22:00:51 +0000189StringRef LinkerScript<ELFT>::getOutputSection(InputSectionBase<ELFT> *S) {
Rui Ueyama07320e42016-04-20 20:13:41 +0000190 for (SectionRule &R : Opt.Sections)
Rui Ueyamac9f402e2016-04-22 00:23:52 +0000191 if (matchStr(R.SectionPattern, S->getSectionName()))
Rui Ueyama8ec77e62016-04-21 22:00:51 +0000192 return R.Dest;
193 return "";
Rui Ueyama717677a2016-02-11 21:17:59 +0000194}
195
Rui Ueyama1ebc8ed2016-02-12 21:47:28 +0000196template <class ELFT>
Rui Ueyama07320e42016-04-20 20:13:41 +0000197bool LinkerScript<ELFT>::isDiscarded(InputSectionBase<ELFT> *S) {
Rui Ueyama1ebc8ed2016-02-12 21:47:28 +0000198 return getOutputSection(S) == "/DISCARD/";
Rui Ueyama717677a2016-02-11 21:17:59 +0000199}
200
Rui Ueyama07320e42016-04-20 20:13:41 +0000201template <class ELFT>
202bool LinkerScript<ELFT>::shouldKeep(InputSectionBase<ELFT> *S) {
Rui Ueyama8ec77e62016-04-21 22:00:51 +0000203 for (StringRef Pat : Opt.KeptSections)
204 if (matchStr(Pat, S->getSectionName()))
205 return true;
206 return false;
George Rimar481c2ce2016-02-23 07:47:54 +0000207}
208
George Rimar652852c2016-04-16 10:10:32 +0000209template <class ELFT>
Rui Ueyama07320e42016-04-20 20:13:41 +0000210void LinkerScript<ELFT>::assignAddresses(
George Rimardbbd8b12016-04-21 11:21:48 +0000211 ArrayRef<OutputSectionBase<ELFT> *> Sections) {
George Rimar652852c2016-04-16 10:10:32 +0000212 // Orphan sections are sections present in the input files which
Rui Ueyama7c18c282016-04-18 21:00:40 +0000213 // are not explicitly placed into the output file by the linker script.
214 // We place orphan sections at end of file.
215 // Other linkers places them using some heuristics as described in
George Rimar652852c2016-04-16 10:10:32 +0000216 // https://sourceware.org/binutils/docs/ld/Orphan-Sections.html#Orphan-Sections.
Rui Ueyama7c18c282016-04-18 21:00:40 +0000217 for (OutputSectionBase<ELFT> *Sec : Sections) {
George Rimar652852c2016-04-16 10:10:32 +0000218 StringRef Name = Sec->getName();
Rui Ueyamac3e2a4b2016-04-21 20:30:00 +0000219 if (getSectionIndex(Name) == INT_MAX)
Rui Ueyama07320e42016-04-20 20:13:41 +0000220 Opt.Commands.push_back({SectionKind, {}, Name});
George Rimar652852c2016-04-16 10:10:32 +0000221 }
George Rimar652852c2016-04-16 10:10:32 +0000222
Rui Ueyama7c18c282016-04-18 21:00:40 +0000223 // Assign addresses as instructed by linker script SECTIONS sub-commands.
Rui Ueyamac998a8c2016-04-22 00:03:13 +0000224 Dot = Out<ELFT>::ElfHeader->getSize() + Out<ELFT>::ProgramHeaders->getSize();
George Rimar652852c2016-04-16 10:10:32 +0000225 uintX_t ThreadBssOffset = 0;
George Rimar652852c2016-04-16 10:10:32 +0000226
Rui Ueyama07320e42016-04-20 20:13:41 +0000227 for (SectionsCommand &Cmd : Opt.Commands) {
Rui Ueyama9e957a02016-04-18 21:00:45 +0000228 if (Cmd.Kind == ExprKind) {
Rui Ueyama9c1112d2016-04-23 00:04:03 +0000229 Dot = evalExpr(Cmd.Expr, Dot);
George Rimar652852c2016-04-16 10:10:32 +0000230 continue;
231 }
232
Dima Stepanovfb8978f2016-05-19 18:15:54 +0000233 // Find all the sections with required name. There can be more than
234 // ont section with such name, if the alignment, flags or type
235 // attribute differs.
236 for (OutputSectionBase<ELFT> *Sec : Sections) {
237 if (Sec->getName() != Cmd.SectionName)
238 continue;
George Rimar652852c2016-04-16 10:10:32 +0000239
Dima Stepanovfb8978f2016-05-19 18:15:54 +0000240 if ((Sec->getFlags() & SHF_TLS) && Sec->getType() == SHT_NOBITS) {
241 uintX_t TVA = Dot + ThreadBssOffset;
242 TVA = alignTo(TVA, Sec->getAlign());
243 Sec->setVA(TVA);
244 ThreadBssOffset = TVA - Dot + Sec->getSize();
245 continue;
246 }
George Rimar652852c2016-04-16 10:10:32 +0000247
Dima Stepanovfb8978f2016-05-19 18:15:54 +0000248 if (Sec->getFlags() & SHF_ALLOC) {
249 Dot = alignTo(Dot, Sec->getAlign());
250 Sec->setVA(Dot);
251 Dot += Sec->getSize();
252 continue;
253 }
George Rimar652852c2016-04-16 10:10:32 +0000254 }
255 }
256}
257
Rui Ueyama07320e42016-04-20 20:13:41 +0000258template <class ELFT>
259ArrayRef<uint8_t> LinkerScript<ELFT>::getFiller(StringRef Name) {
260 auto I = Opt.Filler.find(Name);
261 if (I == Opt.Filler.end())
Rui Ueyama3e808972016-02-28 05:09:11 +0000262 return {};
263 return I->second;
George Rimare2ee72b2016-02-26 14:48:31 +0000264}
265
Rui Ueyamac3e2a4b2016-04-21 20:30:00 +0000266// Returns the index of the given section name in linker script
267// SECTIONS commands. Sections are laid out as the same order as they
268// were in the script. If a given name did not appear in the script,
269// it returns INT_MAX, so that it will be laid out at end of file.
George Rimar71b26e92016-04-21 10:22:02 +0000270template <class ELFT>
Rui Ueyamac3e2a4b2016-04-21 20:30:00 +0000271int LinkerScript<ELFT>::getSectionIndex(StringRef Name) {
George Rimar71b26e92016-04-21 10:22:02 +0000272 auto Begin = Opt.Commands.begin();
273 auto End = Opt.Commands.end();
274 auto I = std::find_if(Begin, End, [&](SectionsCommand &N) {
275 return N.Kind == SectionKind && N.SectionName == Name;
276 });
Rui Ueyamac3e2a4b2016-04-21 20:30:00 +0000277 return I == End ? INT_MAX : (I - Begin);
George Rimar71b26e92016-04-21 10:22:02 +0000278}
279
280// A compartor to sort output sections. Returns -1 or 1 if
281// A or B are mentioned in linker script. Otherwise, returns 0.
Rui Ueyama07320e42016-04-20 20:13:41 +0000282template <class ELFT>
283int LinkerScript<ELFT>::compareSections(StringRef A, StringRef B) {
Rui Ueyamac3e2a4b2016-04-21 20:30:00 +0000284 int I = getSectionIndex(A);
285 int J = getSectionIndex(B);
286 if (I == INT_MAX && J == INT_MAX)
Rui Ueyama717677a2016-02-11 21:17:59 +0000287 return 0;
288 return I < J ? -1 : 1;
289}
290
George Rimarcb2aeb62016-02-24 08:49:50 +0000291// Returns true if S matches T. S can contain glob meta-characters.
292// The asterisk ('*') matches zero or more characacters, and the question
293// mark ('?') matches one character.
Rui Ueyama1ebc8ed2016-02-12 21:47:28 +0000294static bool matchStr(StringRef S, StringRef T) {
295 for (;;) {
296 if (S.empty())
297 return T.empty();
298 if (S[0] == '*') {
299 S = S.substr(1);
300 if (S.empty())
301 // Fast path. If a pattern is '*', it matches anything.
302 return true;
303 for (size_t I = 0, E = T.size(); I < E; ++I)
304 if (matchStr(S, T.substr(I)))
305 return true;
306 return false;
307 }
George Rimarcb2aeb62016-02-24 08:49:50 +0000308 if (T.empty() || (S[0] != T[0] && S[0] != '?'))
Rui Ueyama1ebc8ed2016-02-12 21:47:28 +0000309 return false;
310 S = S.substr(1);
311 T = T.substr(1);
312 }
313}
314
Rui Ueyama07320e42016-04-20 20:13:41 +0000315class elf::ScriptParser : public ScriptParserBase {
George Rimarc3794e52016-02-24 09:21:47 +0000316 typedef void (ScriptParser::*Handler)();
317
Rui Ueyamaf7c5fbb2015-09-30 17:23:26 +0000318public:
Rui Ueyama07320e42016-04-20 20:13:41 +0000319 ScriptParser(StringRef S, bool B) : ScriptParserBase(S), IsUnderSysroot(B) {}
George Rimarf23b2322016-02-19 10:45:45 +0000320
Rui Ueyama4a465392016-04-22 22:59:24 +0000321 void run();
Rui Ueyamaf7c5fbb2015-09-30 17:23:26 +0000322
323private:
Rui Ueyama52a15092015-10-11 03:28:42 +0000324 void addFile(StringRef Path);
325
Rui Ueyamaf7c5fbb2015-09-30 17:23:26 +0000326 void readAsNeeded();
Denis Protivensky90c50992015-10-08 06:48:38 +0000327 void readEntry();
George Rimar83f406c2015-10-19 17:35:12 +0000328 void readExtern();
Rui Ueyamaf7c5fbb2015-09-30 17:23:26 +0000329 void readGroup();
Rui Ueyama31aa1f82015-10-11 01:31:55 +0000330 void readInclude();
George Rimarc3794e52016-02-24 09:21:47 +0000331 void readNothing() {}
Rui Ueyamaee592822015-10-07 00:25:09 +0000332 void readOutput();
Davide Italiano9159ce92015-10-12 21:50:08 +0000333 void readOutputArch();
Rui Ueyamaf7c5fbb2015-09-30 17:23:26 +0000334 void readOutputFormat();
Davide Italiano68a39a62015-10-08 17:51:41 +0000335 void readSearchDir();
Denis Protivensky8e3b38a2015-11-12 09:52:08 +0000336 void readSections();
337
George Rimar652852c2016-04-16 10:10:32 +0000338 void readLocationCounterValue();
Denis Protivensky8e3b38a2015-11-12 09:52:08 +0000339 void readOutputSectionDescription();
Rui Ueyamaf7c5fbb2015-09-30 17:23:26 +0000340
George Rimarc3794e52016-02-24 09:21:47 +0000341 const static StringMap<Handler> Cmd;
Rui Ueyama07320e42016-04-20 20:13:41 +0000342 ScriptConfiguration &Opt = *ScriptConfig;
343 StringSaver Saver = {ScriptConfig->Alloc};
Simon Atanasyan16b0cc92015-11-26 05:53:00 +0000344 bool IsUnderSysroot;
Rui Ueyamaf7c5fbb2015-09-30 17:23:26 +0000345};
Rui Ueyamaf7c5fbb2015-09-30 17:23:26 +0000346
Rafael Espindolae0df00b2016-02-28 00:25:54 +0000347const StringMap<elf::ScriptParser::Handler> elf::ScriptParser::Cmd = {
George Rimarc3794e52016-02-24 09:21:47 +0000348 {"ENTRY", &ScriptParser::readEntry},
349 {"EXTERN", &ScriptParser::readExtern},
350 {"GROUP", &ScriptParser::readGroup},
351 {"INCLUDE", &ScriptParser::readInclude},
352 {"INPUT", &ScriptParser::readGroup},
353 {"OUTPUT", &ScriptParser::readOutput},
354 {"OUTPUT_ARCH", &ScriptParser::readOutputArch},
355 {"OUTPUT_FORMAT", &ScriptParser::readOutputFormat},
356 {"SEARCH_DIR", &ScriptParser::readSearchDir},
357 {"SECTIONS", &ScriptParser::readSections},
358 {";", &ScriptParser::readNothing}};
359
Rui Ueyama717677a2016-02-11 21:17:59 +0000360void ScriptParser::run() {
Rui Ueyamaf7c5fbb2015-09-30 17:23:26 +0000361 while (!atEOF()) {
362 StringRef Tok = next();
George Rimarc3794e52016-02-24 09:21:47 +0000363 if (Handler Fn = Cmd.lookup(Tok))
364 (this->*Fn)();
365 else
George Rimar57610422016-03-11 14:43:02 +0000366 setError("unknown directive: " + Tok);
Rui Ueyamaf7c5fbb2015-09-30 17:23:26 +0000367 }
368}
369
Rui Ueyama717677a2016-02-11 21:17:59 +0000370void ScriptParser::addFile(StringRef S) {
Simon Atanasyan16b0cc92015-11-26 05:53:00 +0000371 if (IsUnderSysroot && S.startswith("/")) {
372 SmallString<128> Path;
373 (Config->Sysroot + S).toStringRef(Path);
374 if (sys::fs::exists(Path)) {
375 Driver->addFile(Saver.save(Path.str()));
376 return;
377 }
378 }
379
Rui Ueyamaf03f3cc2015-10-13 00:09:21 +0000380 if (sys::path::is_absolute(S)) {
Rui Ueyama52a15092015-10-11 03:28:42 +0000381 Driver->addFile(S);
382 } else if (S.startswith("=")) {
383 if (Config->Sysroot.empty())
384 Driver->addFile(S.substr(1));
385 else
386 Driver->addFile(Saver.save(Config->Sysroot + "/" + S.substr(1)));
387 } else if (S.startswith("-l")) {
Rui Ueyama21eecb42016-02-02 21:13:09 +0000388 Driver->addLibrary(S.substr(2));
Simon Atanasyana1b8fc32015-11-26 20:23:46 +0000389 } else if (sys::fs::exists(S)) {
390 Driver->addFile(S);
Rui Ueyama52a15092015-10-11 03:28:42 +0000391 } else {
392 std::string Path = findFromSearchPaths(S);
393 if (Path.empty())
George Rimar777f9632016-03-12 08:31:34 +0000394 setError("unable to find " + S);
Rui Ueyama025d59b2016-02-02 20:27:59 +0000395 else
396 Driver->addFile(Saver.save(Path));
Rui Ueyama52a15092015-10-11 03:28:42 +0000397 }
398}
399
Rui Ueyama717677a2016-02-11 21:17:59 +0000400void ScriptParser::readAsNeeded() {
Rui Ueyamaf7c5fbb2015-09-30 17:23:26 +0000401 expect("(");
Rui Ueyama35da9b62015-10-11 20:59:12 +0000402 bool Orig = Config->AsNeeded;
403 Config->AsNeeded = true;
Rui Ueyama025d59b2016-02-02 20:27:59 +0000404 while (!Error) {
Rui Ueyamaf7c5fbb2015-09-30 17:23:26 +0000405 StringRef Tok = next();
406 if (Tok == ")")
Rui Ueyama35da9b62015-10-11 20:59:12 +0000407 break;
Rui Ueyama52a15092015-10-11 03:28:42 +0000408 addFile(Tok);
Rui Ueyamaf7c5fbb2015-09-30 17:23:26 +0000409 }
Rui Ueyama35da9b62015-10-11 20:59:12 +0000410 Config->AsNeeded = Orig;
Rui Ueyamaf7c5fbb2015-09-30 17:23:26 +0000411}
412
Rui Ueyama717677a2016-02-11 21:17:59 +0000413void ScriptParser::readEntry() {
Denis Protivensky90c50992015-10-08 06:48:38 +0000414 // -e <symbol> takes predecence over ENTRY(<symbol>).
415 expect("(");
416 StringRef Tok = next();
417 if (Config->Entry.empty())
418 Config->Entry = Tok;
419 expect(")");
420}
421
Rui Ueyama717677a2016-02-11 21:17:59 +0000422void ScriptParser::readExtern() {
George Rimar83f406c2015-10-19 17:35:12 +0000423 expect("(");
Rui Ueyama025d59b2016-02-02 20:27:59 +0000424 while (!Error) {
George Rimar83f406c2015-10-19 17:35:12 +0000425 StringRef Tok = next();
426 if (Tok == ")")
427 return;
428 Config->Undefined.push_back(Tok);
429 }
430}
431
Rui Ueyama717677a2016-02-11 21:17:59 +0000432void ScriptParser::readGroup() {
Rui Ueyamaf7c5fbb2015-09-30 17:23:26 +0000433 expect("(");
Rui Ueyama025d59b2016-02-02 20:27:59 +0000434 while (!Error) {
Rui Ueyamaf7c5fbb2015-09-30 17:23:26 +0000435 StringRef Tok = next();
436 if (Tok == ")")
437 return;
438 if (Tok == "AS_NEEDED") {
439 readAsNeeded();
440 continue;
441 }
Rui Ueyama52a15092015-10-11 03:28:42 +0000442 addFile(Tok);
Rui Ueyamaf7c5fbb2015-09-30 17:23:26 +0000443 }
444}
445
Rui Ueyama717677a2016-02-11 21:17:59 +0000446void ScriptParser::readInclude() {
Rui Ueyama31aa1f82015-10-11 01:31:55 +0000447 StringRef Tok = next();
448 auto MBOrErr = MemoryBuffer::getFile(Tok);
Rui Ueyama025d59b2016-02-02 20:27:59 +0000449 if (!MBOrErr) {
George Rimar57610422016-03-11 14:43:02 +0000450 setError("cannot open " + Tok);
Rui Ueyama025d59b2016-02-02 20:27:59 +0000451 return;
452 }
Rui Ueyama31aa1f82015-10-11 01:31:55 +0000453 std::unique_ptr<MemoryBuffer> &MB = *MBOrErr;
Rui Ueyamaa47ee682015-10-11 01:53:04 +0000454 StringRef S = Saver.save(MB->getMemBufferRef().getBuffer());
455 std::vector<StringRef> V = tokenize(S);
Rui Ueyama31aa1f82015-10-11 01:31:55 +0000456 Tokens.insert(Tokens.begin() + Pos, V.begin(), V.end());
Rui Ueyama31aa1f82015-10-11 01:31:55 +0000457}
458
Rui Ueyama717677a2016-02-11 21:17:59 +0000459void ScriptParser::readOutput() {
Rui Ueyamaee592822015-10-07 00:25:09 +0000460 // -o <file> takes predecence over OUTPUT(<file>).
461 expect("(");
462 StringRef Tok = next();
463 if (Config->OutputFile.empty())
464 Config->OutputFile = Tok;
465 expect(")");
466}
467
Rui Ueyama717677a2016-02-11 21:17:59 +0000468void ScriptParser::readOutputArch() {
Davide Italiano9159ce92015-10-12 21:50:08 +0000469 // Error checking only for now.
470 expect("(");
471 next();
472 expect(")");
473}
474
Rui Ueyama717677a2016-02-11 21:17:59 +0000475void ScriptParser::readOutputFormat() {
Rui Ueyamaf7c5fbb2015-09-30 17:23:26 +0000476 // Error checking only for now.
477 expect("(");
478 next();
Davide Italiano6836c612015-10-12 21:08:41 +0000479 StringRef Tok = next();
480 if (Tok == ")")
481 return;
Rui Ueyama025d59b2016-02-02 20:27:59 +0000482 if (Tok != ",") {
George Rimar57610422016-03-11 14:43:02 +0000483 setError("unexpected token: " + Tok);
Rui Ueyama025d59b2016-02-02 20:27:59 +0000484 return;
485 }
Davide Italiano6836c612015-10-12 21:08:41 +0000486 next();
487 expect(",");
488 next();
Rui Ueyamaf7c5fbb2015-09-30 17:23:26 +0000489 expect(")");
490}
491
Rui Ueyama717677a2016-02-11 21:17:59 +0000492void ScriptParser::readSearchDir() {
Davide Italiano68a39a62015-10-08 17:51:41 +0000493 expect("(");
Rafael Espindola06501922016-03-08 17:13:12 +0000494 Config->SearchPaths.push_back(next());
Davide Italiano68a39a62015-10-08 17:51:41 +0000495 expect(")");
496}
497
Rui Ueyama717677a2016-02-11 21:17:59 +0000498void ScriptParser::readSections() {
Rui Ueyama07320e42016-04-20 20:13:41 +0000499 Opt.DoLayout = true;
Denis Protivensky8e3b38a2015-11-12 09:52:08 +0000500 expect("{");
George Rimar652852c2016-04-16 10:10:32 +0000501 while (!Error && !skip("}")) {
502 StringRef Tok = peek();
503 if (Tok == ".")
504 readLocationCounterValue();
505 else
506 readOutputSectionDescription();
507 }
Denis Protivensky8e3b38a2015-11-12 09:52:08 +0000508}
509
George Rimar652852c2016-04-16 10:10:32 +0000510void ScriptParser::readLocationCounterValue() {
511 expect(".");
512 expect("=");
Rui Ueyama07320e42016-04-20 20:13:41 +0000513 Opt.Commands.push_back({ExprKind, {}, ""});
514 SectionsCommand &Cmd = Opt.Commands.back();
George Rimar652852c2016-04-16 10:10:32 +0000515 while (!Error) {
516 StringRef Tok = next();
517 if (Tok == ";")
518 break;
Rui Ueyama9e957a02016-04-18 21:00:45 +0000519 Cmd.Expr.push_back(Tok);
George Rimar652852c2016-04-16 10:10:32 +0000520 }
Rui Ueyama9e957a02016-04-18 21:00:45 +0000521 if (Cmd.Expr.empty())
George Rimar652852c2016-04-16 10:10:32 +0000522 error("error in location counter expression");
523}
524
Rui Ueyama717677a2016-02-11 21:17:59 +0000525void ScriptParser::readOutputSectionDescription() {
Rui Ueyama3e808972016-02-28 05:09:11 +0000526 StringRef OutSec = next();
Rui Ueyama07320e42016-04-20 20:13:41 +0000527 Opt.Commands.push_back({SectionKind, {}, OutSec});
Denis Protivensky8e3b38a2015-11-12 09:52:08 +0000528 expect(":");
529 expect("{");
Rui Ueyama8ec77e62016-04-21 22:00:51 +0000530
Rui Ueyama025d59b2016-02-02 20:27:59 +0000531 while (!Error && !skip("}")) {
George Rimar481c2ce2016-02-23 07:47:54 +0000532 StringRef Tok = next();
533 if (Tok == "*") {
Rui Ueyama8ec77e62016-04-21 22:00:51 +0000534 expect("(");
535 while (!Error && !skip(")"))
536 Opt.Sections.emplace_back(OutSec, next());
George Rimar481c2ce2016-02-23 07:47:54 +0000537 } else if (Tok == "KEEP") {
538 expect("(");
Rui Ueyama8ec77e62016-04-21 22:00:51 +0000539 expect("*");
540 expect("(");
541 while (!Error && !skip(")")) {
542 StringRef Sec = next();
543 Opt.Sections.emplace_back(OutSec, Sec);
544 Opt.KeptSections.push_back(Sec);
545 }
George Rimar481c2ce2016-02-23 07:47:54 +0000546 expect(")");
547 } else {
George Rimar777f9632016-03-12 08:31:34 +0000548 setError("unknown command " + Tok);
George Rimar481c2ce2016-02-23 07:47:54 +0000549 }
Denis Protivensky8e3b38a2015-11-12 09:52:08 +0000550 }
Rui Ueyama8ec77e62016-04-21 22:00:51 +0000551
George Rimare2ee72b2016-02-26 14:48:31 +0000552 StringRef Tok = peek();
553 if (Tok.startswith("=")) {
554 if (!Tok.startswith("=0x")) {
Rui Ueyama3ed2f062016-03-13 03:17:44 +0000555 setError("filler should be a hexadecimal value");
George Rimare2ee72b2016-02-26 14:48:31 +0000556 return;
557 }
Rui Ueyama3e808972016-02-28 05:09:11 +0000558 Tok = Tok.substr(3);
Rui Ueyama07320e42016-04-20 20:13:41 +0000559 Opt.Filler[OutSec] = parseHex(Tok);
George Rimare2ee72b2016-02-26 14:48:31 +0000560 next();
561 }
Denis Protivensky8e3b38a2015-11-12 09:52:08 +0000562}
563
Simon Atanasyan16b0cc92015-11-26 05:53:00 +0000564static bool isUnderSysroot(StringRef Path) {
565 if (Config->Sysroot == "")
566 return false;
567 for (; !Path.empty(); Path = sys::path::parent_path(Path))
568 if (sys::fs::equivalent(Config->Sysroot, Path))
569 return true;
570 return false;
571}
572
Rui Ueyama07320e42016-04-20 20:13:41 +0000573// Entry point.
574void elf::readLinkerScript(MemoryBufferRef MB) {
Simon Atanasyan16b0cc92015-11-26 05:53:00 +0000575 StringRef Path = MB.getBufferIdentifier();
Rui Ueyama07320e42016-04-20 20:13:41 +0000576 ScriptParser(MB.getBuffer(), isUnderSysroot(Path)).run();
Rui Ueyamaf7c5fbb2015-09-30 17:23:26 +0000577}
Rui Ueyama1ebc8ed2016-02-12 21:47:28 +0000578
Rui Ueyama07320e42016-04-20 20:13:41 +0000579template class elf::LinkerScript<ELF32LE>;
580template class elf::LinkerScript<ELF32BE>;
581template class elf::LinkerScript<ELF64LE>;
582template class elf::LinkerScript<ELF64BE>;