blob: d10fc5bc629fe56957fd84db86708e51efccbf2d [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 Ueyama93c9af42016-06-29 08:01:32 +000022#include "Strings.h"
Rui Ueyamaf7c5fbb2015-09-30 17:23:26 +000023#include "SymbolTable.h"
Eugene Leviant467c4d52016-07-01 10:27:36 +000024#include "Target.h"
Rui Ueyama960504b2016-04-19 18:58:11 +000025#include "llvm/ADT/StringSwitch.h"
George Rimar652852c2016-04-16 10:10:32 +000026#include "llvm/Support/ELF.h"
Rui Ueyamaf7c5fbb2015-09-30 17:23:26 +000027#include "llvm/Support/FileSystem.h"
28#include "llvm/Support/MemoryBuffer.h"
Rui Ueyamaf03f3cc2015-10-13 00:09:21 +000029#include "llvm/Support/Path.h"
Rui Ueyamaa47ee682015-10-11 01:53:04 +000030#include "llvm/Support/StringSaver.h"
Rui Ueyamaf7c5fbb2015-09-30 17:23:26 +000031
32using namespace llvm;
George Rimar652852c2016-04-16 10:10:32 +000033using namespace llvm::ELF;
Rui Ueyama1ebc8ed2016-02-12 21:47:28 +000034using namespace llvm::object;
Rui Ueyamaf7c5fbb2015-09-30 17:23:26 +000035using namespace lld;
Rafael Espindolae0df00b2016-02-28 00:25:54 +000036using namespace lld::elf;
Rui Ueyamaf7c5fbb2015-09-30 17:23:26 +000037
Rui Ueyama07320e42016-04-20 20:13:41 +000038ScriptConfiguration *elf::ScriptConfig;
Rui Ueyama717677a2016-02-11 21:17:59 +000039
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 Ueyama722830a2016-06-29 05:32:09 +0000191 if (globMatch(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)
Rui Ueyama722830a2016-06-29 05:32:09 +0000204 if (globMatch(Pat, S->getSectionName()))
Rui Ueyama8ec77e62016-04-21 22:00:51 +0000205 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.
Eugene Leviant467c4d52016-07-01 10:27:36 +0000224 Dot = Out<ELFT>::ElfHeader->getSize() + Out<ELFT>::ProgramHeaders->getSize();
225 uintX_t MinVA = std::numeric_limits<uintX_t>::max();
George Rimar652852c2016-04-16 10:10:32 +0000226 uintX_t ThreadBssOffset = 0;
George Rimar652852c2016-04-16 10:10:32 +0000227
Rui Ueyama07320e42016-04-20 20:13:41 +0000228 for (SectionsCommand &Cmd : Opt.Commands) {
Rui Ueyama9e957a02016-04-18 21:00:45 +0000229 if (Cmd.Kind == ExprKind) {
Rui Ueyama9c1112d2016-04-23 00:04:03 +0000230 Dot = evalExpr(Cmd.Expr, Dot);
George Rimar652852c2016-04-16 10:10:32 +0000231 continue;
232 }
233
Dima Stepanovfb8978f2016-05-19 18:15:54 +0000234 // Find all the sections with required name. There can be more than
235 // ont section with such name, if the alignment, flags or type
236 // attribute differs.
237 for (OutputSectionBase<ELFT> *Sec : Sections) {
238 if (Sec->getName() != Cmd.SectionName)
239 continue;
George Rimar652852c2016-04-16 10:10:32 +0000240
Dima Stepanovfb8978f2016-05-19 18:15:54 +0000241 if ((Sec->getFlags() & SHF_TLS) && Sec->getType() == SHT_NOBITS) {
242 uintX_t TVA = Dot + ThreadBssOffset;
Rui Ueyama424b4082016-06-17 01:18:46 +0000243 TVA = alignTo(TVA, Sec->getAlignment());
Dima Stepanovfb8978f2016-05-19 18:15:54 +0000244 Sec->setVA(TVA);
245 ThreadBssOffset = TVA - Dot + Sec->getSize();
246 continue;
247 }
George Rimar652852c2016-04-16 10:10:32 +0000248
Dima Stepanovfb8978f2016-05-19 18:15:54 +0000249 if (Sec->getFlags() & SHF_ALLOC) {
Rui Ueyama424b4082016-06-17 01:18:46 +0000250 Dot = alignTo(Dot, Sec->getAlignment());
Dima Stepanovfb8978f2016-05-19 18:15:54 +0000251 Sec->setVA(Dot);
Eugene Leviant467c4d52016-07-01 10:27:36 +0000252 MinVA = std::min(MinVA, Dot);
Dima Stepanovfb8978f2016-05-19 18:15:54 +0000253 Dot += Sec->getSize();
254 continue;
255 }
George Rimar652852c2016-04-16 10:10:32 +0000256 }
257 }
Eugene Leviant467c4d52016-07-01 10:27:36 +0000258
259 // ELF and Program headers need to be right before the first section in memory.
260 // Set their addresses accordingly.
261 MinVA = alignDown(MinVA - Out<ELFT>::ElfHeader->getSize() -
262 Out<ELFT>::ProgramHeaders->getSize(),
263 Target->PageSize);
264 Out<ELFT>::ElfHeader->setVA(MinVA);
265 Out<ELFT>::ProgramHeaders->setVA(Out<ELFT>::ElfHeader->getSize() + MinVA);
George Rimar652852c2016-04-16 10:10:32 +0000266}
267
Rui Ueyama07320e42016-04-20 20:13:41 +0000268template <class ELFT>
269ArrayRef<uint8_t> LinkerScript<ELFT>::getFiller(StringRef Name) {
270 auto I = Opt.Filler.find(Name);
271 if (I == Opt.Filler.end())
Rui Ueyama3e808972016-02-28 05:09:11 +0000272 return {};
273 return I->second;
George Rimare2ee72b2016-02-26 14:48:31 +0000274}
275
Rui Ueyamac3e2a4b2016-04-21 20:30:00 +0000276// Returns the index of the given section name in linker script
277// SECTIONS commands. Sections are laid out as the same order as they
278// were in the script. If a given name did not appear in the script,
279// it returns INT_MAX, so that it will be laid out at end of file.
George Rimar71b26e92016-04-21 10:22:02 +0000280template <class ELFT>
Rui Ueyamac3e2a4b2016-04-21 20:30:00 +0000281int LinkerScript<ELFT>::getSectionIndex(StringRef Name) {
George Rimar71b26e92016-04-21 10:22:02 +0000282 auto Begin = Opt.Commands.begin();
283 auto End = Opt.Commands.end();
284 auto I = std::find_if(Begin, End, [&](SectionsCommand &N) {
285 return N.Kind == SectionKind && N.SectionName == Name;
286 });
Rui Ueyamac3e2a4b2016-04-21 20:30:00 +0000287 return I == End ? INT_MAX : (I - Begin);
George Rimar71b26e92016-04-21 10:22:02 +0000288}
289
290// A compartor to sort output sections. Returns -1 or 1 if
291// A or B are mentioned in linker script. Otherwise, returns 0.
Rui Ueyama07320e42016-04-20 20:13:41 +0000292template <class ELFT>
293int LinkerScript<ELFT>::compareSections(StringRef A, StringRef B) {
Rui Ueyamac3e2a4b2016-04-21 20:30:00 +0000294 int I = getSectionIndex(A);
295 int J = getSectionIndex(B);
296 if (I == INT_MAX && J == INT_MAX)
Rui Ueyama717677a2016-02-11 21:17:59 +0000297 return 0;
298 return I < J ? -1 : 1;
299}
300
Rui Ueyama07320e42016-04-20 20:13:41 +0000301class elf::ScriptParser : public ScriptParserBase {
George Rimarc3794e52016-02-24 09:21:47 +0000302 typedef void (ScriptParser::*Handler)();
303
Rui Ueyamaf7c5fbb2015-09-30 17:23:26 +0000304public:
Rui Ueyama07320e42016-04-20 20:13:41 +0000305 ScriptParser(StringRef S, bool B) : ScriptParserBase(S), IsUnderSysroot(B) {}
George Rimarf23b2322016-02-19 10:45:45 +0000306
Rui Ueyama4a465392016-04-22 22:59:24 +0000307 void run();
Rui Ueyamaf7c5fbb2015-09-30 17:23:26 +0000308
309private:
Rui Ueyama52a15092015-10-11 03:28:42 +0000310 void addFile(StringRef Path);
311
Rui Ueyamaf7c5fbb2015-09-30 17:23:26 +0000312 void readAsNeeded();
Denis Protivensky90c50992015-10-08 06:48:38 +0000313 void readEntry();
George Rimar83f406c2015-10-19 17:35:12 +0000314 void readExtern();
Rui Ueyamaf7c5fbb2015-09-30 17:23:26 +0000315 void readGroup();
Rui Ueyama31aa1f82015-10-11 01:31:55 +0000316 void readInclude();
George Rimarc3794e52016-02-24 09:21:47 +0000317 void readNothing() {}
Rui Ueyamaee592822015-10-07 00:25:09 +0000318 void readOutput();
Davide Italiano9159ce92015-10-12 21:50:08 +0000319 void readOutputArch();
Rui Ueyamaf7c5fbb2015-09-30 17:23:26 +0000320 void readOutputFormat();
Davide Italiano68a39a62015-10-08 17:51:41 +0000321 void readSearchDir();
Denis Protivensky8e3b38a2015-11-12 09:52:08 +0000322 void readSections();
323
George Rimar652852c2016-04-16 10:10:32 +0000324 void readLocationCounterValue();
Denis Protivensky8e3b38a2015-11-12 09:52:08 +0000325 void readOutputSectionDescription();
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 Rimar652852c2016-04-16 10:10:32 +0000496void ScriptParser::readLocationCounterValue() {
497 expect(".");
498 expect("=");
Rui Ueyama07320e42016-04-20 20:13:41 +0000499 Opt.Commands.push_back({ExprKind, {}, ""});
500 SectionsCommand &Cmd = Opt.Commands.back();
George Rimar652852c2016-04-16 10:10:32 +0000501 while (!Error) {
502 StringRef Tok = next();
503 if (Tok == ";")
504 break;
Rui Ueyama9e957a02016-04-18 21:00:45 +0000505 Cmd.Expr.push_back(Tok);
George Rimar652852c2016-04-16 10:10:32 +0000506 }
Rui Ueyama9e957a02016-04-18 21:00:45 +0000507 if (Cmd.Expr.empty())
George Rimar652852c2016-04-16 10:10:32 +0000508 error("error in location counter expression");
509}
510
Rui Ueyama717677a2016-02-11 21:17:59 +0000511void ScriptParser::readOutputSectionDescription() {
Rui Ueyama3e808972016-02-28 05:09:11 +0000512 StringRef OutSec = next();
Rui Ueyama07320e42016-04-20 20:13:41 +0000513 Opt.Commands.push_back({SectionKind, {}, OutSec});
Denis Protivensky8e3b38a2015-11-12 09:52:08 +0000514 expect(":");
515 expect("{");
Rui Ueyama8ec77e62016-04-21 22:00:51 +0000516
Rui Ueyama025d59b2016-02-02 20:27:59 +0000517 while (!Error && !skip("}")) {
George Rimar481c2ce2016-02-23 07:47:54 +0000518 StringRef Tok = next();
519 if (Tok == "*") {
Rui Ueyama8ec77e62016-04-21 22:00:51 +0000520 expect("(");
521 while (!Error && !skip(")"))
522 Opt.Sections.emplace_back(OutSec, next());
George Rimar481c2ce2016-02-23 07:47:54 +0000523 } else if (Tok == "KEEP") {
524 expect("(");
Rui Ueyama8ec77e62016-04-21 22:00:51 +0000525 expect("*");
526 expect("(");
527 while (!Error && !skip(")")) {
528 StringRef Sec = next();
529 Opt.Sections.emplace_back(OutSec, Sec);
530 Opt.KeptSections.push_back(Sec);
531 }
George Rimar481c2ce2016-02-23 07:47:54 +0000532 expect(")");
533 } else {
George Rimar777f9632016-03-12 08:31:34 +0000534 setError("unknown command " + Tok);
George Rimar481c2ce2016-02-23 07:47:54 +0000535 }
Denis Protivensky8e3b38a2015-11-12 09:52:08 +0000536 }
Rui Ueyama8ec77e62016-04-21 22:00:51 +0000537
George Rimare2ee72b2016-02-26 14:48:31 +0000538 StringRef Tok = peek();
539 if (Tok.startswith("=")) {
540 if (!Tok.startswith("=0x")) {
Rui Ueyama3ed2f062016-03-13 03:17:44 +0000541 setError("filler should be a hexadecimal value");
George Rimare2ee72b2016-02-26 14:48:31 +0000542 return;
543 }
Rui Ueyama3e808972016-02-28 05:09:11 +0000544 Tok = Tok.substr(3);
Rui Ueyama07320e42016-04-20 20:13:41 +0000545 Opt.Filler[OutSec] = parseHex(Tok);
George Rimare2ee72b2016-02-26 14:48:31 +0000546 next();
547 }
Denis Protivensky8e3b38a2015-11-12 09:52:08 +0000548}
549
Simon Atanasyan16b0cc92015-11-26 05:53:00 +0000550static bool isUnderSysroot(StringRef Path) {
551 if (Config->Sysroot == "")
552 return false;
553 for (; !Path.empty(); Path = sys::path::parent_path(Path))
554 if (sys::fs::equivalent(Config->Sysroot, Path))
555 return true;
556 return false;
557}
558
Rui Ueyama07320e42016-04-20 20:13:41 +0000559// Entry point.
560void elf::readLinkerScript(MemoryBufferRef MB) {
Simon Atanasyan16b0cc92015-11-26 05:53:00 +0000561 StringRef Path = MB.getBufferIdentifier();
Rui Ueyama07320e42016-04-20 20:13:41 +0000562 ScriptParser(MB.getBuffer(), isUnderSysroot(Path)).run();
Rui Ueyamaf7c5fbb2015-09-30 17:23:26 +0000563}
Rui Ueyama1ebc8ed2016-02-12 21:47:28 +0000564
Rui Ueyama07320e42016-04-20 20:13:41 +0000565template class elf::LinkerScript<ELF32LE>;
566template class elf::LinkerScript<ELF32BE>;
567template class elf::LinkerScript<ELF64LE>;
568template class elf::LinkerScript<ELF64BE>;