blob: c8aaffb659b611a830173626f802abddfb44396a [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 Ueyama9c1112d2016-04-23 00:04:03 +000038// This is an operator-precedence parser to parse and evaluate
39// a linker script expression. For each linker script arithmetic
40// expression (e.g. ". = . + 0x1000"), a new instance of ExprParser
41// is created and ran.
42namespace {
43class ExprParser : public ScriptParserBase {
44public:
45 ExprParser(std::vector<StringRef> &Tokens, uint64_t Dot)
46 : ScriptParserBase(Tokens), Dot(Dot) {}
47
48 uint64_t run();
49
50private:
51 uint64_t parsePrimary();
52 uint64_t parseTernary(uint64_t Cond);
53 uint64_t apply(StringRef Op, uint64_t L, uint64_t R);
54 uint64_t parseExpr1(uint64_t Lhs, int MinPrec);
55 uint64_t parseExpr();
56
57 uint64_t Dot;
58};
59}
60
Rui Ueyama960504b2016-04-19 18:58:11 +000061static int precedence(StringRef Op) {
62 return StringSwitch<int>(Op)
63 .Case("*", 4)
George Rimarab939062016-04-25 08:14:41 +000064 .Case("/", 4)
65 .Case("+", 3)
66 .Case("-", 3)
67 .Case("<", 2)
68 .Case(">", 2)
69 .Case(">=", 2)
70 .Case("<=", 2)
71 .Case("==", 2)
72 .Case("!=", 2)
Rui Ueyama960504b2016-04-19 18:58:11 +000073 .Case("&", 1)
74 .Default(-1);
75}
76
Rui Ueyama9c1112d2016-04-23 00:04:03 +000077static uint64_t evalExpr(std::vector<StringRef> &Tokens, uint64_t Dot) {
78 return ExprParser(Tokens, Dot).run();
Rui Ueyama960504b2016-04-19 18:58:11 +000079}
80
Rui Ueyama9c1112d2016-04-23 00:04:03 +000081uint64_t ExprParser::run() {
82 uint64_t V = parseExpr();
83 if (!atEOF() && !Error)
84 setError("stray token: " + peek());
85 return V;
Rui Ueyama60118112016-04-20 20:54:13 +000086}
87
Rui Ueyama960504b2016-04-19 18:58:11 +000088// This is a part of the operator-precedence parser to evaluate
89// arithmetic expressions in SECTIONS command. This function evaluates an
Rui Ueyamae29a9752016-04-22 21:02:27 +000090// integer literal, a parenthesized expression, the ALIGN function,
91// or the special variable ".".
Rui Ueyama9c1112d2016-04-23 00:04:03 +000092uint64_t ExprParser::parsePrimary() {
93 StringRef Tok = next();
Rui Ueyama960504b2016-04-19 18:58:11 +000094 if (Tok == ".")
95 return Dot;
96 if (Tok == "(") {
Rui Ueyama9c1112d2016-04-23 00:04:03 +000097 uint64_t V = parseExpr();
98 expect(")");
Rui Ueyama960504b2016-04-19 18:58:11 +000099 return V;
100 }
George Rimardffc1412016-04-22 11:40:53 +0000101 if (Tok == "ALIGN") {
Rui Ueyama9c1112d2016-04-23 00:04:03 +0000102 expect("(");
103 uint64_t V = parseExpr();
104 expect(")");
George Rimardffc1412016-04-22 11:40:53 +0000105 return alignTo(Dot, V);
106 }
Rui Ueyama5fa60982016-04-22 21:05:04 +0000107 uint64_t V = 0;
108 if (Tok.getAsInteger(0, V))
Rui Ueyama9c1112d2016-04-23 00:04:03 +0000109 setError("malformed number: " + Tok);
Rui Ueyama5fa60982016-04-22 21:05:04 +0000110 return V;
Rui Ueyama960504b2016-04-19 18:58:11 +0000111}
112
Rui Ueyama9c1112d2016-04-23 00:04:03 +0000113uint64_t ExprParser::parseTernary(uint64_t Cond) {
114 next();
115 uint64_t V = parseExpr();
116 expect(":");
117 uint64_t W = parseExpr();
George Rimarfba45c42016-04-22 11:28:54 +0000118 return Cond ? V : W;
119}
120
Rui Ueyama9c1112d2016-04-23 00:04:03 +0000121uint64_t ExprParser::apply(StringRef Op, uint64_t L, uint64_t R) {
Rui Ueyama960504b2016-04-19 18:58:11 +0000122 if (Op == "*")
123 return L * R;
124 if (Op == "/") {
125 if (R == 0) {
126 error("division by zero");
George Rimar652852c2016-04-16 10:10:32 +0000127 return 0;
128 }
Rui Ueyama960504b2016-04-19 18:58:11 +0000129 return L / R;
George Rimar652852c2016-04-16 10:10:32 +0000130 }
George Rimarab939062016-04-25 08:14:41 +0000131 if (Op == "+")
132 return L + R;
133 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;
Rui Ueyama960504b2016-04-19 18:58:11 +0000147 if (Op == "&")
148 return L & R;
Rui Ueyama7a81d672016-04-19 19:04:03 +0000149 llvm_unreachable("invalid operator");
Rui Ueyama960504b2016-04-19 18:58:11 +0000150 return 0;
151}
152
Rui Ueyama9c1112d2016-04-23 00:04:03 +0000153// This is a part of the operator-precedence parser.
154// This function assumes that the remaining token stream starts
155// with an operator.
156uint64_t ExprParser::parseExpr1(uint64_t Lhs, int MinPrec) {
157 while (!atEOF()) {
Rui Ueyama960504b2016-04-19 18:58:11 +0000158 // Read an operator and an expression.
Rui Ueyama9c1112d2016-04-23 00:04:03 +0000159 StringRef Op1 = peek();
George Rimarfba45c42016-04-22 11:28:54 +0000160 if (Op1 == "?")
Rui Ueyama9c1112d2016-04-23 00:04:03 +0000161 return parseTernary(Lhs);
Rui Ueyama960504b2016-04-19 18:58:11 +0000162 if (precedence(Op1) < MinPrec)
163 return Lhs;
Rui Ueyama9c1112d2016-04-23 00:04:03 +0000164 next();
165 uint64_t Rhs = parsePrimary();
Rui Ueyama960504b2016-04-19 18:58:11 +0000166
167 // Evaluate the remaining part of the expression first if the
168 // next operator has greater precedence than the previous one.
169 // For example, if we have read "+" and "3", and if the next
170 // operator is "*", then we'll evaluate 3 * ... part first.
Rui Ueyama9c1112d2016-04-23 00:04:03 +0000171 while (!atEOF()) {
172 StringRef Op2 = peek();
Rui Ueyama960504b2016-04-19 18:58:11 +0000173 if (precedence(Op2) <= precedence(Op1))
174 break;
Rui Ueyama9c1112d2016-04-23 00:04:03 +0000175 Rhs = parseExpr1(Rhs, precedence(Op2));
Rui Ueyama960504b2016-04-19 18:58:11 +0000176 }
177
178 Lhs = apply(Op1, Lhs, Rhs);
179 }
180 return Lhs;
181}
182
Rui Ueyama9c1112d2016-04-23 00:04:03 +0000183// Reads and evaluates an arithmetic expression.
184uint64_t ExprParser::parseExpr() { return parseExpr1(parsePrimary(), 0); }
George Rimar652852c2016-04-16 10:10:32 +0000185
Rui Ueyama1ebc8ed2016-02-12 21:47:28 +0000186template <class ELFT>
Rui Ueyama8ec77e62016-04-21 22:00:51 +0000187StringRef LinkerScript<ELFT>::getOutputSection(InputSectionBase<ELFT> *S) {
Rui Ueyama07320e42016-04-20 20:13:41 +0000188 for (SectionRule &R : Opt.Sections)
Rui Ueyama722830a2016-06-29 05:32:09 +0000189 if (globMatch(R.SectionPattern, S->getSectionName()))
Rui Ueyama8ec77e62016-04-21 22:00:51 +0000190 return R.Dest;
191 return "";
Rui Ueyama717677a2016-02-11 21:17:59 +0000192}
193
Rui Ueyama1ebc8ed2016-02-12 21:47:28 +0000194template <class ELFT>
Rui Ueyama07320e42016-04-20 20:13:41 +0000195bool LinkerScript<ELFT>::isDiscarded(InputSectionBase<ELFT> *S) {
Rui Ueyama1ebc8ed2016-02-12 21:47:28 +0000196 return getOutputSection(S) == "/DISCARD/";
Rui Ueyama717677a2016-02-11 21:17:59 +0000197}
198
Rui Ueyama07320e42016-04-20 20:13:41 +0000199template <class ELFT>
200bool LinkerScript<ELFT>::shouldKeep(InputSectionBase<ELFT> *S) {
Rui Ueyama8ec77e62016-04-21 22:00:51 +0000201 for (StringRef Pat : Opt.KeptSections)
Rui Ueyama722830a2016-06-29 05:32:09 +0000202 if (globMatch(Pat, S->getSectionName()))
Rui Ueyama8ec77e62016-04-21 22:00:51 +0000203 return true;
204 return false;
George Rimar481c2ce2016-02-23 07:47:54 +0000205}
206
George Rimar652852c2016-04-16 10:10:32 +0000207template <class ELFT>
Rui Ueyama07320e42016-04-20 20:13:41 +0000208void LinkerScript<ELFT>::assignAddresses(
George Rimardbbd8b12016-04-21 11:21:48 +0000209 ArrayRef<OutputSectionBase<ELFT> *> Sections) {
George Rimar652852c2016-04-16 10:10:32 +0000210 // Orphan sections are sections present in the input files which
Rui Ueyama7c18c282016-04-18 21:00:40 +0000211 // are not explicitly placed into the output file by the linker script.
212 // We place orphan sections at end of file.
213 // Other linkers places them using some heuristics as described in
George Rimar652852c2016-04-16 10:10:32 +0000214 // https://sourceware.org/binutils/docs/ld/Orphan-Sections.html#Orphan-Sections.
Rui Ueyama7c18c282016-04-18 21:00:40 +0000215 for (OutputSectionBase<ELFT> *Sec : Sections) {
George Rimar652852c2016-04-16 10:10:32 +0000216 StringRef Name = Sec->getName();
Rui Ueyamac3e2a4b2016-04-21 20:30:00 +0000217 if (getSectionIndex(Name) == INT_MAX)
Rui Ueyama07320e42016-04-20 20:13:41 +0000218 Opt.Commands.push_back({SectionKind, {}, Name});
George Rimar652852c2016-04-16 10:10:32 +0000219 }
George Rimar652852c2016-04-16 10:10:32 +0000220
Rui Ueyama7c18c282016-04-18 21:00:40 +0000221 // Assign addresses as instructed by linker script SECTIONS sub-commands.
Rui Ueyamac998a8c2016-04-22 00:03:13 +0000222 Dot = Out<ELFT>::ElfHeader->getSize() + Out<ELFT>::ProgramHeaders->getSize();
George Rimar652852c2016-04-16 10:10:32 +0000223 uintX_t ThreadBssOffset = 0;
George Rimar652852c2016-04-16 10:10:32 +0000224
Rui Ueyama07320e42016-04-20 20:13:41 +0000225 for (SectionsCommand &Cmd : Opt.Commands) {
Rui Ueyama9e957a02016-04-18 21:00:45 +0000226 if (Cmd.Kind == ExprKind) {
Rui Ueyama9c1112d2016-04-23 00:04:03 +0000227 Dot = evalExpr(Cmd.Expr, Dot);
George Rimar652852c2016-04-16 10:10:32 +0000228 continue;
229 }
230
Dima Stepanovfb8978f2016-05-19 18:15:54 +0000231 // Find all the sections with required name. There can be more than
232 // ont section with such name, if the alignment, flags or type
233 // attribute differs.
234 for (OutputSectionBase<ELFT> *Sec : Sections) {
235 if (Sec->getName() != Cmd.SectionName)
236 continue;
George Rimar652852c2016-04-16 10:10:32 +0000237
Dima Stepanovfb8978f2016-05-19 18:15:54 +0000238 if ((Sec->getFlags() & SHF_TLS) && Sec->getType() == SHT_NOBITS) {
239 uintX_t TVA = Dot + ThreadBssOffset;
Rui Ueyama424b4082016-06-17 01:18:46 +0000240 TVA = alignTo(TVA, Sec->getAlignment());
Dima Stepanovfb8978f2016-05-19 18:15:54 +0000241 Sec->setVA(TVA);
242 ThreadBssOffset = TVA - Dot + Sec->getSize();
243 continue;
244 }
George Rimar652852c2016-04-16 10:10:32 +0000245
Dima Stepanovfb8978f2016-05-19 18:15:54 +0000246 if (Sec->getFlags() & SHF_ALLOC) {
Rui Ueyama424b4082016-06-17 01:18:46 +0000247 Dot = alignTo(Dot, Sec->getAlignment());
Dima Stepanovfb8978f2016-05-19 18:15:54 +0000248 Sec->setVA(Dot);
249 Dot += Sec->getSize();
250 continue;
251 }
George Rimar652852c2016-04-16 10:10:32 +0000252 }
253 }
254}
255
Rui Ueyama07320e42016-04-20 20:13:41 +0000256template <class ELFT>
257ArrayRef<uint8_t> LinkerScript<ELFT>::getFiller(StringRef Name) {
258 auto I = Opt.Filler.find(Name);
259 if (I == Opt.Filler.end())
Rui Ueyama3e808972016-02-28 05:09:11 +0000260 return {};
261 return I->second;
George Rimare2ee72b2016-02-26 14:48:31 +0000262}
263
Rui Ueyamac3e2a4b2016-04-21 20:30:00 +0000264// Returns the index of the given section name in linker script
265// SECTIONS commands. Sections are laid out as the same order as they
266// were in the script. If a given name did not appear in the script,
267// it returns INT_MAX, so that it will be laid out at end of file.
George Rimar71b26e92016-04-21 10:22:02 +0000268template <class ELFT>
Rui Ueyamac3e2a4b2016-04-21 20:30:00 +0000269int LinkerScript<ELFT>::getSectionIndex(StringRef Name) {
George Rimar71b26e92016-04-21 10:22:02 +0000270 auto Begin = Opt.Commands.begin();
271 auto End = Opt.Commands.end();
272 auto I = std::find_if(Begin, End, [&](SectionsCommand &N) {
273 return N.Kind == SectionKind && N.SectionName == Name;
274 });
Rui Ueyamac3e2a4b2016-04-21 20:30:00 +0000275 return I == End ? INT_MAX : (I - Begin);
George Rimar71b26e92016-04-21 10:22:02 +0000276}
277
278// A compartor to sort output sections. Returns -1 or 1 if
279// A or B are mentioned in linker script. Otherwise, returns 0.
Rui Ueyama07320e42016-04-20 20:13:41 +0000280template <class ELFT>
281int LinkerScript<ELFT>::compareSections(StringRef A, StringRef B) {
Rui Ueyamac3e2a4b2016-04-21 20:30:00 +0000282 int I = getSectionIndex(A);
283 int J = getSectionIndex(B);
284 if (I == INT_MAX && J == INT_MAX)
Rui Ueyama717677a2016-02-11 21:17:59 +0000285 return 0;
286 return I < J ? -1 : 1;
287}
288
George Rimarcb2aeb62016-02-24 08:49:50 +0000289// Returns true if S matches T. S can contain glob meta-characters.
Davide Italiano47ae3c32016-06-26 19:02:43 +0000290// The asterisk ('*') matches zero or more characters, and the question
George Rimarcb2aeb62016-02-24 08:49:50 +0000291// mark ('?') matches one character.
Rui Ueyama722830a2016-06-29 05:32:09 +0000292bool elf::globMatch(StringRef S, StringRef T) {
Rui Ueyama1ebc8ed2016-02-12 21:47:28 +0000293 for (;;) {
294 if (S.empty())
295 return T.empty();
296 if (S[0] == '*') {
297 S = S.substr(1);
298 if (S.empty())
299 // Fast path. If a pattern is '*', it matches anything.
300 return true;
301 for (size_t I = 0, E = T.size(); I < E; ++I)
Rui Ueyama722830a2016-06-29 05:32:09 +0000302 if (globMatch(S, T.substr(I)))
Rui Ueyama1ebc8ed2016-02-12 21:47:28 +0000303 return true;
304 return false;
305 }
George Rimarcb2aeb62016-02-24 08:49:50 +0000306 if (T.empty() || (S[0] != T[0] && S[0] != '?'))
Rui Ueyama1ebc8ed2016-02-12 21:47:28 +0000307 return false;
308 S = S.substr(1);
309 T = T.substr(1);
310 }
311}
312
Rui Ueyama07320e42016-04-20 20:13:41 +0000313class elf::ScriptParser : public ScriptParserBase {
George Rimarc3794e52016-02-24 09:21:47 +0000314 typedef void (ScriptParser::*Handler)();
315
Rui Ueyamaf7c5fbb2015-09-30 17:23:26 +0000316public:
Rui Ueyama07320e42016-04-20 20:13:41 +0000317 ScriptParser(StringRef S, bool B) : ScriptParserBase(S), IsUnderSysroot(B) {}
George Rimarf23b2322016-02-19 10:45:45 +0000318
Rui Ueyama4a465392016-04-22 22:59:24 +0000319 void run();
Rui Ueyamaf7c5fbb2015-09-30 17:23:26 +0000320
321private:
Rui Ueyama52a15092015-10-11 03:28:42 +0000322 void addFile(StringRef Path);
323
Rui Ueyamaf7c5fbb2015-09-30 17:23:26 +0000324 void readAsNeeded();
Denis Protivensky90c50992015-10-08 06:48:38 +0000325 void readEntry();
George Rimar83f406c2015-10-19 17:35:12 +0000326 void readExtern();
Rui Ueyamaf7c5fbb2015-09-30 17:23:26 +0000327 void readGroup();
Rui Ueyama31aa1f82015-10-11 01:31:55 +0000328 void readInclude();
George Rimarc3794e52016-02-24 09:21:47 +0000329 void readNothing() {}
Rui Ueyamaee592822015-10-07 00:25:09 +0000330 void readOutput();
Davide Italiano9159ce92015-10-12 21:50:08 +0000331 void readOutputArch();
Rui Ueyamaf7c5fbb2015-09-30 17:23:26 +0000332 void readOutputFormat();
Davide Italiano68a39a62015-10-08 17:51:41 +0000333 void readSearchDir();
Denis Protivensky8e3b38a2015-11-12 09:52:08 +0000334 void readSections();
335
George Rimar652852c2016-04-16 10:10:32 +0000336 void readLocationCounterValue();
Denis Protivensky8e3b38a2015-11-12 09:52:08 +0000337 void readOutputSectionDescription();
Rui Ueyamaf7c5fbb2015-09-30 17:23:26 +0000338
George Rimarc3794e52016-02-24 09:21:47 +0000339 const static StringMap<Handler> Cmd;
Rui Ueyama07320e42016-04-20 20:13:41 +0000340 ScriptConfiguration &Opt = *ScriptConfig;
341 StringSaver Saver = {ScriptConfig->Alloc};
Simon Atanasyan16b0cc92015-11-26 05:53:00 +0000342 bool IsUnderSysroot;
Rui Ueyamaf7c5fbb2015-09-30 17:23:26 +0000343};
Rui Ueyamaf7c5fbb2015-09-30 17:23:26 +0000344
Rafael Espindolae0df00b2016-02-28 00:25:54 +0000345const StringMap<elf::ScriptParser::Handler> elf::ScriptParser::Cmd = {
George Rimarc3794e52016-02-24 09:21:47 +0000346 {"ENTRY", &ScriptParser::readEntry},
347 {"EXTERN", &ScriptParser::readExtern},
348 {"GROUP", &ScriptParser::readGroup},
349 {"INCLUDE", &ScriptParser::readInclude},
350 {"INPUT", &ScriptParser::readGroup},
351 {"OUTPUT", &ScriptParser::readOutput},
352 {"OUTPUT_ARCH", &ScriptParser::readOutputArch},
353 {"OUTPUT_FORMAT", &ScriptParser::readOutputFormat},
354 {"SEARCH_DIR", &ScriptParser::readSearchDir},
355 {"SECTIONS", &ScriptParser::readSections},
356 {";", &ScriptParser::readNothing}};
357
Rui Ueyama717677a2016-02-11 21:17:59 +0000358void ScriptParser::run() {
Rui Ueyamaf7c5fbb2015-09-30 17:23:26 +0000359 while (!atEOF()) {
360 StringRef Tok = next();
George Rimarc3794e52016-02-24 09:21:47 +0000361 if (Handler Fn = Cmd.lookup(Tok))
362 (this->*Fn)();
363 else
George Rimar57610422016-03-11 14:43:02 +0000364 setError("unknown directive: " + Tok);
Rui Ueyamaf7c5fbb2015-09-30 17:23:26 +0000365 }
366}
367
Rui Ueyama717677a2016-02-11 21:17:59 +0000368void ScriptParser::addFile(StringRef S) {
Simon Atanasyan16b0cc92015-11-26 05:53:00 +0000369 if (IsUnderSysroot && S.startswith("/")) {
370 SmallString<128> Path;
371 (Config->Sysroot + S).toStringRef(Path);
372 if (sys::fs::exists(Path)) {
373 Driver->addFile(Saver.save(Path.str()));
374 return;
375 }
376 }
377
Rui Ueyamaf03f3cc2015-10-13 00:09:21 +0000378 if (sys::path::is_absolute(S)) {
Rui Ueyama52a15092015-10-11 03:28:42 +0000379 Driver->addFile(S);
380 } else if (S.startswith("=")) {
381 if (Config->Sysroot.empty())
382 Driver->addFile(S.substr(1));
383 else
384 Driver->addFile(Saver.save(Config->Sysroot + "/" + S.substr(1)));
385 } else if (S.startswith("-l")) {
Rui Ueyama21eecb42016-02-02 21:13:09 +0000386 Driver->addLibrary(S.substr(2));
Simon Atanasyana1b8fc32015-11-26 20:23:46 +0000387 } else if (sys::fs::exists(S)) {
388 Driver->addFile(S);
Rui Ueyama52a15092015-10-11 03:28:42 +0000389 } else {
390 std::string Path = findFromSearchPaths(S);
391 if (Path.empty())
George Rimar777f9632016-03-12 08:31:34 +0000392 setError("unable to find " + S);
Rui Ueyama025d59b2016-02-02 20:27:59 +0000393 else
394 Driver->addFile(Saver.save(Path));
Rui Ueyama52a15092015-10-11 03:28:42 +0000395 }
396}
397
Rui Ueyama717677a2016-02-11 21:17:59 +0000398void ScriptParser::readAsNeeded() {
Rui Ueyamaf7c5fbb2015-09-30 17:23:26 +0000399 expect("(");
Rui Ueyama35da9b62015-10-11 20:59:12 +0000400 bool Orig = Config->AsNeeded;
401 Config->AsNeeded = true;
Rui Ueyama025d59b2016-02-02 20:27:59 +0000402 while (!Error) {
Rui Ueyamaf7c5fbb2015-09-30 17:23:26 +0000403 StringRef Tok = next();
404 if (Tok == ")")
Rui Ueyama35da9b62015-10-11 20:59:12 +0000405 break;
Rui Ueyama52a15092015-10-11 03:28:42 +0000406 addFile(Tok);
Rui Ueyamaf7c5fbb2015-09-30 17:23:26 +0000407 }
Rui Ueyama35da9b62015-10-11 20:59:12 +0000408 Config->AsNeeded = Orig;
Rui Ueyamaf7c5fbb2015-09-30 17:23:26 +0000409}
410
Rui Ueyama717677a2016-02-11 21:17:59 +0000411void ScriptParser::readEntry() {
Denis Protivensky90c50992015-10-08 06:48:38 +0000412 // -e <symbol> takes predecence over ENTRY(<symbol>).
413 expect("(");
414 StringRef Tok = next();
415 if (Config->Entry.empty())
416 Config->Entry = Tok;
417 expect(")");
418}
419
Rui Ueyama717677a2016-02-11 21:17:59 +0000420void ScriptParser::readExtern() {
George Rimar83f406c2015-10-19 17:35:12 +0000421 expect("(");
Rui Ueyama025d59b2016-02-02 20:27:59 +0000422 while (!Error) {
George Rimar83f406c2015-10-19 17:35:12 +0000423 StringRef Tok = next();
424 if (Tok == ")")
425 return;
426 Config->Undefined.push_back(Tok);
427 }
428}
429
Rui Ueyama717677a2016-02-11 21:17:59 +0000430void ScriptParser::readGroup() {
Rui Ueyamaf7c5fbb2015-09-30 17:23:26 +0000431 expect("(");
Rui Ueyama025d59b2016-02-02 20:27:59 +0000432 while (!Error) {
Rui Ueyamaf7c5fbb2015-09-30 17:23:26 +0000433 StringRef Tok = next();
434 if (Tok == ")")
435 return;
436 if (Tok == "AS_NEEDED") {
437 readAsNeeded();
438 continue;
439 }
Rui Ueyama52a15092015-10-11 03:28:42 +0000440 addFile(Tok);
Rui Ueyamaf7c5fbb2015-09-30 17:23:26 +0000441 }
442}
443
Rui Ueyama717677a2016-02-11 21:17:59 +0000444void ScriptParser::readInclude() {
Rui Ueyama31aa1f82015-10-11 01:31:55 +0000445 StringRef Tok = next();
446 auto MBOrErr = MemoryBuffer::getFile(Tok);
Rui Ueyama025d59b2016-02-02 20:27:59 +0000447 if (!MBOrErr) {
George Rimar57610422016-03-11 14:43:02 +0000448 setError("cannot open " + Tok);
Rui Ueyama025d59b2016-02-02 20:27:59 +0000449 return;
450 }
Rui Ueyama31aa1f82015-10-11 01:31:55 +0000451 std::unique_ptr<MemoryBuffer> &MB = *MBOrErr;
Rui Ueyamaa47ee682015-10-11 01:53:04 +0000452 StringRef S = Saver.save(MB->getMemBufferRef().getBuffer());
453 std::vector<StringRef> V = tokenize(S);
Rui Ueyama31aa1f82015-10-11 01:31:55 +0000454 Tokens.insert(Tokens.begin() + Pos, V.begin(), V.end());
Rui Ueyama31aa1f82015-10-11 01:31:55 +0000455}
456
Rui Ueyama717677a2016-02-11 21:17:59 +0000457void ScriptParser::readOutput() {
Rui Ueyamaee592822015-10-07 00:25:09 +0000458 // -o <file> takes predecence over OUTPUT(<file>).
459 expect("(");
460 StringRef Tok = next();
461 if (Config->OutputFile.empty())
462 Config->OutputFile = Tok;
463 expect(")");
464}
465
Rui Ueyama717677a2016-02-11 21:17:59 +0000466void ScriptParser::readOutputArch() {
Davide Italiano9159ce92015-10-12 21:50:08 +0000467 // Error checking only for now.
468 expect("(");
469 next();
470 expect(")");
471}
472
Rui Ueyama717677a2016-02-11 21:17:59 +0000473void ScriptParser::readOutputFormat() {
Rui Ueyamaf7c5fbb2015-09-30 17:23:26 +0000474 // Error checking only for now.
475 expect("(");
476 next();
Davide Italiano6836c612015-10-12 21:08:41 +0000477 StringRef Tok = next();
478 if (Tok == ")")
479 return;
Rui Ueyama025d59b2016-02-02 20:27:59 +0000480 if (Tok != ",") {
George Rimar57610422016-03-11 14:43:02 +0000481 setError("unexpected token: " + Tok);
Rui Ueyama025d59b2016-02-02 20:27:59 +0000482 return;
483 }
Davide Italiano6836c612015-10-12 21:08:41 +0000484 next();
485 expect(",");
486 next();
Rui Ueyamaf7c5fbb2015-09-30 17:23:26 +0000487 expect(")");
488}
489
Rui Ueyama717677a2016-02-11 21:17:59 +0000490void ScriptParser::readSearchDir() {
Davide Italiano68a39a62015-10-08 17:51:41 +0000491 expect("(");
Rafael Espindola06501922016-03-08 17:13:12 +0000492 Config->SearchPaths.push_back(next());
Davide Italiano68a39a62015-10-08 17:51:41 +0000493 expect(")");
494}
495
Rui Ueyama717677a2016-02-11 21:17:59 +0000496void ScriptParser::readSections() {
Rui Ueyama07320e42016-04-20 20:13:41 +0000497 Opt.DoLayout = true;
Denis Protivensky8e3b38a2015-11-12 09:52:08 +0000498 expect("{");
George Rimar652852c2016-04-16 10:10:32 +0000499 while (!Error && !skip("}")) {
500 StringRef Tok = peek();
501 if (Tok == ".")
502 readLocationCounterValue();
503 else
504 readOutputSectionDescription();
505 }
Denis Protivensky8e3b38a2015-11-12 09:52:08 +0000506}
507
George Rimar652852c2016-04-16 10:10:32 +0000508void ScriptParser::readLocationCounterValue() {
509 expect(".");
510 expect("=");
Rui Ueyama07320e42016-04-20 20:13:41 +0000511 Opt.Commands.push_back({ExprKind, {}, ""});
512 SectionsCommand &Cmd = Opt.Commands.back();
George Rimar652852c2016-04-16 10:10:32 +0000513 while (!Error) {
514 StringRef Tok = next();
515 if (Tok == ";")
516 break;
Rui Ueyama9e957a02016-04-18 21:00:45 +0000517 Cmd.Expr.push_back(Tok);
George Rimar652852c2016-04-16 10:10:32 +0000518 }
Rui Ueyama9e957a02016-04-18 21:00:45 +0000519 if (Cmd.Expr.empty())
George Rimar652852c2016-04-16 10:10:32 +0000520 error("error in location counter expression");
521}
522
Rui Ueyama717677a2016-02-11 21:17:59 +0000523void ScriptParser::readOutputSectionDescription() {
Rui Ueyama3e808972016-02-28 05:09:11 +0000524 StringRef OutSec = next();
Rui Ueyama07320e42016-04-20 20:13:41 +0000525 Opt.Commands.push_back({SectionKind, {}, OutSec});
Denis Protivensky8e3b38a2015-11-12 09:52:08 +0000526 expect(":");
527 expect("{");
Rui Ueyama8ec77e62016-04-21 22:00:51 +0000528
Rui Ueyama025d59b2016-02-02 20:27:59 +0000529 while (!Error && !skip("}")) {
George Rimar481c2ce2016-02-23 07:47:54 +0000530 StringRef Tok = next();
531 if (Tok == "*") {
Rui Ueyama8ec77e62016-04-21 22:00:51 +0000532 expect("(");
533 while (!Error && !skip(")"))
534 Opt.Sections.emplace_back(OutSec, next());
George Rimar481c2ce2016-02-23 07:47:54 +0000535 } else if (Tok == "KEEP") {
536 expect("(");
Rui Ueyama8ec77e62016-04-21 22:00:51 +0000537 expect("*");
538 expect("(");
539 while (!Error && !skip(")")) {
540 StringRef Sec = next();
541 Opt.Sections.emplace_back(OutSec, Sec);
542 Opt.KeptSections.push_back(Sec);
543 }
George Rimar481c2ce2016-02-23 07:47:54 +0000544 expect(")");
545 } else {
George Rimar777f9632016-03-12 08:31:34 +0000546 setError("unknown command " + Tok);
George Rimar481c2ce2016-02-23 07:47:54 +0000547 }
Denis Protivensky8e3b38a2015-11-12 09:52:08 +0000548 }
Rui Ueyama8ec77e62016-04-21 22:00:51 +0000549
George Rimare2ee72b2016-02-26 14:48:31 +0000550 StringRef Tok = peek();
551 if (Tok.startswith("=")) {
552 if (!Tok.startswith("=0x")) {
Rui Ueyama3ed2f062016-03-13 03:17:44 +0000553 setError("filler should be a hexadecimal value");
George Rimare2ee72b2016-02-26 14:48:31 +0000554 return;
555 }
Rui Ueyama3e808972016-02-28 05:09:11 +0000556 Tok = Tok.substr(3);
Rui Ueyama07320e42016-04-20 20:13:41 +0000557 Opt.Filler[OutSec] = parseHex(Tok);
George Rimare2ee72b2016-02-26 14:48:31 +0000558 next();
559 }
Denis Protivensky8e3b38a2015-11-12 09:52:08 +0000560}
561
Simon Atanasyan16b0cc92015-11-26 05:53:00 +0000562static bool isUnderSysroot(StringRef Path) {
563 if (Config->Sysroot == "")
564 return false;
565 for (; !Path.empty(); Path = sys::path::parent_path(Path))
566 if (sys::fs::equivalent(Config->Sysroot, Path))
567 return true;
568 return false;
569}
570
Rui Ueyama07320e42016-04-20 20:13:41 +0000571// Entry point.
572void elf::readLinkerScript(MemoryBufferRef MB) {
Simon Atanasyan16b0cc92015-11-26 05:53:00 +0000573 StringRef Path = MB.getBufferIdentifier();
Rui Ueyama07320e42016-04-20 20:13:41 +0000574 ScriptParser(MB.getBuffer(), isUnderSysroot(Path)).run();
Rui Ueyamaf7c5fbb2015-09-30 17:23:26 +0000575}
Rui Ueyama1ebc8ed2016-02-12 21:47:28 +0000576
Rui Ueyama07320e42016-04-20 20:13:41 +0000577template class elf::LinkerScript<ELF32LE>;
578template class elf::LinkerScript<ELF32BE>;
579template class elf::LinkerScript<ELF64LE>;
580template class elf::LinkerScript<ELF64BE>;