blob: 61abdc185e11f3bbddae996a4b7d09c2274a1c00 [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"
Eugene Levianteda81a12016-07-12 06:39:48 +000023#include "Symbols.h"
Rui Ueyamaf7c5fbb2015-09-30 17:23:26 +000024#include "SymbolTable.h"
Eugene Leviant467c4d52016-07-01 10:27:36 +000025#include "Target.h"
Rui Ueyama960504b2016-04-19 18:58:11 +000026#include "llvm/ADT/StringSwitch.h"
George Rimar652852c2016-04-16 10:10:32 +000027#include "llvm/Support/ELF.h"
Rui Ueyamaf7c5fbb2015-09-30 17:23:26 +000028#include "llvm/Support/FileSystem.h"
29#include "llvm/Support/MemoryBuffer.h"
Rui Ueyamaf03f3cc2015-10-13 00:09:21 +000030#include "llvm/Support/Path.h"
Rui Ueyamaa47ee682015-10-11 01:53:04 +000031#include "llvm/Support/StringSaver.h"
Rui Ueyamaf7c5fbb2015-09-30 17:23:26 +000032
33using namespace llvm;
George Rimar652852c2016-04-16 10:10:32 +000034using namespace llvm::ELF;
Rui Ueyama1ebc8ed2016-02-12 21:47:28 +000035using namespace llvm::object;
Rui Ueyamaf7c5fbb2015-09-30 17:23:26 +000036using namespace lld;
Rafael Espindolae0df00b2016-02-28 00:25:54 +000037using namespace lld::elf;
Rui Ueyamaf7c5fbb2015-09-30 17:23:26 +000038
Rui Ueyama07320e42016-04-20 20:13:41 +000039ScriptConfiguration *elf::ScriptConfig;
Rui Ueyama717677a2016-02-11 21:17:59 +000040
Rui Ueyama9c1112d2016-04-23 00:04:03 +000041// This is an operator-precedence parser to parse and evaluate
42// a linker script expression. For each linker script arithmetic
43// expression (e.g. ". = . + 0x1000"), a new instance of ExprParser
44// is created and ran.
45namespace {
46class ExprParser : public ScriptParserBase {
47public:
48 ExprParser(std::vector<StringRef> &Tokens, uint64_t Dot)
49 : ScriptParserBase(Tokens), Dot(Dot) {}
50
51 uint64_t run();
52
53private:
54 uint64_t parsePrimary();
55 uint64_t parseTernary(uint64_t Cond);
56 uint64_t apply(StringRef Op, uint64_t L, uint64_t R);
57 uint64_t parseExpr1(uint64_t Lhs, int MinPrec);
58 uint64_t parseExpr();
59
60 uint64_t Dot;
61};
62}
63
Rui Ueyama960504b2016-04-19 18:58:11 +000064static int precedence(StringRef Op) {
65 return StringSwitch<int>(Op)
66 .Case("*", 4)
George Rimarab939062016-04-25 08:14:41 +000067 .Case("/", 4)
68 .Case("+", 3)
69 .Case("-", 3)
70 .Case("<", 2)
71 .Case(">", 2)
72 .Case(">=", 2)
73 .Case("<=", 2)
74 .Case("==", 2)
75 .Case("!=", 2)
Rui Ueyama960504b2016-04-19 18:58:11 +000076 .Case("&", 1)
77 .Default(-1);
78}
79
Rui Ueyama9c1112d2016-04-23 00:04:03 +000080static uint64_t evalExpr(std::vector<StringRef> &Tokens, uint64_t Dot) {
81 return ExprParser(Tokens, Dot).run();
Rui Ueyama960504b2016-04-19 18:58:11 +000082}
83
Rui Ueyama9c1112d2016-04-23 00:04:03 +000084uint64_t ExprParser::run() {
85 uint64_t V = parseExpr();
86 if (!atEOF() && !Error)
87 setError("stray token: " + peek());
88 return V;
Rui Ueyama60118112016-04-20 20:54:13 +000089}
90
Rui Ueyama960504b2016-04-19 18:58:11 +000091// This is a part of the operator-precedence parser to evaluate
92// arithmetic expressions in SECTIONS command. This function evaluates an
Rui Ueyamae29a9752016-04-22 21:02:27 +000093// integer literal, a parenthesized expression, the ALIGN function,
94// or the special variable ".".
Rui Ueyama9c1112d2016-04-23 00:04:03 +000095uint64_t ExprParser::parsePrimary() {
96 StringRef Tok = next();
Rui Ueyama960504b2016-04-19 18:58:11 +000097 if (Tok == ".")
98 return Dot;
99 if (Tok == "(") {
Rui Ueyama9c1112d2016-04-23 00:04:03 +0000100 uint64_t V = parseExpr();
101 expect(")");
Rui Ueyama960504b2016-04-19 18:58:11 +0000102 return V;
103 }
George Rimardffc1412016-04-22 11:40:53 +0000104 if (Tok == "ALIGN") {
Rui Ueyama9c1112d2016-04-23 00:04:03 +0000105 expect("(");
106 uint64_t V = parseExpr();
107 expect(")");
George Rimardffc1412016-04-22 11:40:53 +0000108 return alignTo(Dot, V);
109 }
Rui Ueyama5fa60982016-04-22 21:05:04 +0000110 uint64_t V = 0;
111 if (Tok.getAsInteger(0, V))
Rui Ueyama9c1112d2016-04-23 00:04:03 +0000112 setError("malformed number: " + Tok);
Rui Ueyama5fa60982016-04-22 21:05:04 +0000113 return V;
Rui Ueyama960504b2016-04-19 18:58:11 +0000114}
115
Rui Ueyama9c1112d2016-04-23 00:04:03 +0000116uint64_t ExprParser::parseTernary(uint64_t Cond) {
117 next();
118 uint64_t V = parseExpr();
119 expect(":");
120 uint64_t W = parseExpr();
George Rimarfba45c42016-04-22 11:28:54 +0000121 return Cond ? V : W;
122}
123
Rui Ueyama9c1112d2016-04-23 00:04:03 +0000124uint64_t ExprParser::apply(StringRef Op, uint64_t L, uint64_t R) {
Rui Ueyama960504b2016-04-19 18:58:11 +0000125 if (Op == "*")
126 return L * R;
127 if (Op == "/") {
128 if (R == 0) {
129 error("division by zero");
George Rimar652852c2016-04-16 10:10:32 +0000130 return 0;
131 }
Rui Ueyama960504b2016-04-19 18:58:11 +0000132 return L / R;
George Rimar652852c2016-04-16 10:10:32 +0000133 }
George Rimarab939062016-04-25 08:14:41 +0000134 if (Op == "+")
135 return L + R;
136 if (Op == "-")
137 return L - R;
138 if (Op == "<")
139 return L < R;
140 if (Op == ">")
141 return L > R;
142 if (Op == ">=")
143 return L >= R;
144 if (Op == "<=")
145 return L <= R;
146 if (Op == "==")
147 return L == R;
148 if (Op == "!=")
149 return L != R;
Rui Ueyama960504b2016-04-19 18:58:11 +0000150 if (Op == "&")
151 return L & R;
Rui Ueyama7a81d672016-04-19 19:04:03 +0000152 llvm_unreachable("invalid operator");
Rui Ueyama960504b2016-04-19 18:58:11 +0000153}
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.
Rui Ueyama52c4e172016-07-01 10:42:25 +0000224 Dot = Out<ELFT>::ElfHeader->getSize() + Out<ELFT>::ProgramHeaders->getSize();
Eugene Leviant467c4d52016-07-01 10:27:36 +0000225 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 Ueyama05ef4cf2016-07-15 04:19:37 +0000229 if (Cmd.Kind == AssignmentKind) {
230 uint64_t Val = evalExpr(Cmd.Expr, Dot);
231
232 if (Cmd.Name == ".") {
233 Dot = Val;
234 } else {
235 auto *D = cast<DefinedRegular<ELFT>>(Symtab<ELFT>::X->find(Cmd.Name));
236 D->Value = Val;
237 }
George Rimar652852c2016-04-16 10:10:32 +0000238 continue;
239 }
240
Dima Stepanovfb8978f2016-05-19 18:15:54 +0000241 // Find all the sections with required name. There can be more than
242 // ont section with such name, if the alignment, flags or type
243 // attribute differs.
Rui Ueyama05ef4cf2016-07-15 04:19:37 +0000244 assert(Cmd.Kind == SectionKind);
Dima Stepanovfb8978f2016-05-19 18:15:54 +0000245 for (OutputSectionBase<ELFT> *Sec : Sections) {
Eugene Levianteda81a12016-07-12 06:39:48 +0000246 if (Sec->getName() != Cmd.Name)
Dima Stepanovfb8978f2016-05-19 18:15:54 +0000247 continue;
George Rimar652852c2016-04-16 10:10:32 +0000248
Dima Stepanovfb8978f2016-05-19 18:15:54 +0000249 if ((Sec->getFlags() & SHF_TLS) && Sec->getType() == SHT_NOBITS) {
250 uintX_t TVA = Dot + ThreadBssOffset;
Rui Ueyama424b4082016-06-17 01:18:46 +0000251 TVA = alignTo(TVA, Sec->getAlignment());
Dima Stepanovfb8978f2016-05-19 18:15:54 +0000252 Sec->setVA(TVA);
253 ThreadBssOffset = TVA - Dot + Sec->getSize();
254 continue;
255 }
George Rimar652852c2016-04-16 10:10:32 +0000256
Dima Stepanovfb8978f2016-05-19 18:15:54 +0000257 if (Sec->getFlags() & SHF_ALLOC) {
Rui Ueyama424b4082016-06-17 01:18:46 +0000258 Dot = alignTo(Dot, Sec->getAlignment());
Dima Stepanovfb8978f2016-05-19 18:15:54 +0000259 Sec->setVA(Dot);
Rui Ueyama52c4e172016-07-01 10:42:25 +0000260 MinVA = std::min(MinVA, Dot);
Dima Stepanovfb8978f2016-05-19 18:15:54 +0000261 Dot += Sec->getSize();
262 continue;
263 }
George Rimar652852c2016-04-16 10:10:32 +0000264 }
265 }
Rui Ueyama52c4e172016-07-01 10:42:25 +0000266
Rafael Espindola64c32d62016-07-07 14:28:47 +0000267 // ELF and Program headers need to be right before the first section in
268 // memory.
Eugene Leviant467c4d52016-07-01 10:27:36 +0000269 // Set their addresses accordingly.
270 MinVA = alignDown(MinVA - Out<ELFT>::ElfHeader->getSize() -
271 Out<ELFT>::ProgramHeaders->getSize(),
272 Target->PageSize);
273 Out<ELFT>::ElfHeader->setVA(MinVA);
274 Out<ELFT>::ProgramHeaders->setVA(Out<ELFT>::ElfHeader->getSize() + MinVA);
George Rimar652852c2016-04-16 10:10:32 +0000275}
276
Rui Ueyama07320e42016-04-20 20:13:41 +0000277template <class ELFT>
278ArrayRef<uint8_t> LinkerScript<ELFT>::getFiller(StringRef Name) {
279 auto I = Opt.Filler.find(Name);
280 if (I == Opt.Filler.end())
Rui Ueyama3e808972016-02-28 05:09:11 +0000281 return {};
282 return I->second;
George Rimare2ee72b2016-02-26 14:48:31 +0000283}
284
Rui Ueyamac3e2a4b2016-04-21 20:30:00 +0000285// Returns the index of the given section name in linker script
286// SECTIONS commands. Sections are laid out as the same order as they
287// were in the script. If a given name did not appear in the script,
288// it returns INT_MAX, so that it will be laid out at end of file.
George Rimar71b26e92016-04-21 10:22:02 +0000289template <class ELFT>
Rui Ueyamac3e2a4b2016-04-21 20:30:00 +0000290int LinkerScript<ELFT>::getSectionIndex(StringRef Name) {
George Rimar71b26e92016-04-21 10:22:02 +0000291 auto Begin = Opt.Commands.begin();
292 auto End = Opt.Commands.end();
293 auto I = std::find_if(Begin, End, [&](SectionsCommand &N) {
Eugene Levianteda81a12016-07-12 06:39:48 +0000294 return N.Kind == SectionKind && N.Name == Name;
George Rimar71b26e92016-04-21 10:22:02 +0000295 });
Rui Ueyamac3e2a4b2016-04-21 20:30:00 +0000296 return I == End ? INT_MAX : (I - Begin);
George Rimar71b26e92016-04-21 10:22:02 +0000297}
298
299// A compartor to sort output sections. Returns -1 or 1 if
300// A or B are mentioned in linker script. Otherwise, returns 0.
Rui Ueyama07320e42016-04-20 20:13:41 +0000301template <class ELFT>
302int LinkerScript<ELFT>::compareSections(StringRef A, StringRef B) {
Rui Ueyamac3e2a4b2016-04-21 20:30:00 +0000303 int I = getSectionIndex(A);
304 int J = getSectionIndex(B);
305 if (I == INT_MAX && J == INT_MAX)
Rui Ueyama717677a2016-02-11 21:17:59 +0000306 return 0;
307 return I < J ? -1 : 1;
308}
309
Eugene Leviantb0304112016-07-14 09:21:24 +0000310template <class ELFT>
311void LinkerScript<ELFT>::addScriptedSymbols() {
Eugene Levianteda81a12016-07-12 06:39:48 +0000312 for (SectionsCommand &Cmd : Opt.Commands)
Rui Ueyama05ef4cf2016-07-15 04:19:37 +0000313 if (Cmd.Kind == AssignmentKind)
Eugene Leviant0e36f422016-07-15 11:20:04 +0000314 if (Cmd.Name != "." && Symtab<ELFT>::X->find(Cmd.Name) == nullptr)
Rui Ueyama05ef4cf2016-07-15 04:19:37 +0000315 Symtab<ELFT>::X->addAbsolute(Cmd.Name, STV_DEFAULT);
Eugene Levianteda81a12016-07-12 06:39:48 +0000316}
317
Rui Ueyama07320e42016-04-20 20:13:41 +0000318class elf::ScriptParser : public ScriptParserBase {
George Rimarc3794e52016-02-24 09:21:47 +0000319 typedef void (ScriptParser::*Handler)();
320
Rui Ueyamaf7c5fbb2015-09-30 17:23:26 +0000321public:
Rui Ueyama07320e42016-04-20 20:13:41 +0000322 ScriptParser(StringRef S, bool B) : ScriptParserBase(S), IsUnderSysroot(B) {}
George Rimarf23b2322016-02-19 10:45:45 +0000323
Rui Ueyama4a465392016-04-22 22:59:24 +0000324 void run();
Rui Ueyamaf7c5fbb2015-09-30 17:23:26 +0000325
326private:
Rui Ueyama52a15092015-10-11 03:28:42 +0000327 void addFile(StringRef Path);
328
Rui Ueyamaf7c5fbb2015-09-30 17:23:26 +0000329 void readAsNeeded();
Denis Protivensky90c50992015-10-08 06:48:38 +0000330 void readEntry();
George Rimar83f406c2015-10-19 17:35:12 +0000331 void readExtern();
Rui Ueyamaf7c5fbb2015-09-30 17:23:26 +0000332 void readGroup();
Rui Ueyama31aa1f82015-10-11 01:31:55 +0000333 void readInclude();
George Rimarc3794e52016-02-24 09:21:47 +0000334 void readNothing() {}
Rui Ueyamaee592822015-10-07 00:25:09 +0000335 void readOutput();
Davide Italiano9159ce92015-10-12 21:50:08 +0000336 void readOutputArch();
Rui Ueyamaf7c5fbb2015-09-30 17:23:26 +0000337 void readOutputFormat();
Davide Italiano68a39a62015-10-08 17:51:41 +0000338 void readSearchDir();
Denis Protivensky8e3b38a2015-11-12 09:52:08 +0000339 void readSections();
340
George Rimar652852c2016-04-16 10:10:32 +0000341 void readLocationCounterValue();
Eugene Levianteda81a12016-07-12 06:39:48 +0000342 void readOutputSectionDescription(StringRef OutSec);
343 void readSymbolAssignment(StringRef Name);
344 std::vector<StringRef> readSectionsCommandExpr();
Rui Ueyamaf7c5fbb2015-09-30 17:23:26 +0000345
George Rimarc3794e52016-02-24 09:21:47 +0000346 const static StringMap<Handler> Cmd;
Rui Ueyama07320e42016-04-20 20:13:41 +0000347 ScriptConfiguration &Opt = *ScriptConfig;
348 StringSaver Saver = {ScriptConfig->Alloc};
Simon Atanasyan16b0cc92015-11-26 05:53:00 +0000349 bool IsUnderSysroot;
Rui Ueyamaf7c5fbb2015-09-30 17:23:26 +0000350};
Rui Ueyamaf7c5fbb2015-09-30 17:23:26 +0000351
Rafael Espindolae0df00b2016-02-28 00:25:54 +0000352const StringMap<elf::ScriptParser::Handler> elf::ScriptParser::Cmd = {
George Rimarc3794e52016-02-24 09:21:47 +0000353 {"ENTRY", &ScriptParser::readEntry},
354 {"EXTERN", &ScriptParser::readExtern},
355 {"GROUP", &ScriptParser::readGroup},
356 {"INCLUDE", &ScriptParser::readInclude},
357 {"INPUT", &ScriptParser::readGroup},
358 {"OUTPUT", &ScriptParser::readOutput},
359 {"OUTPUT_ARCH", &ScriptParser::readOutputArch},
360 {"OUTPUT_FORMAT", &ScriptParser::readOutputFormat},
361 {"SEARCH_DIR", &ScriptParser::readSearchDir},
362 {"SECTIONS", &ScriptParser::readSections},
363 {";", &ScriptParser::readNothing}};
364
Rui Ueyama717677a2016-02-11 21:17:59 +0000365void ScriptParser::run() {
Rui Ueyamaf7c5fbb2015-09-30 17:23:26 +0000366 while (!atEOF()) {
367 StringRef Tok = next();
George Rimarc3794e52016-02-24 09:21:47 +0000368 if (Handler Fn = Cmd.lookup(Tok))
369 (this->*Fn)();
370 else
George Rimar57610422016-03-11 14:43:02 +0000371 setError("unknown directive: " + Tok);
Rui Ueyamaf7c5fbb2015-09-30 17:23:26 +0000372 }
373}
374
Rui Ueyama717677a2016-02-11 21:17:59 +0000375void ScriptParser::addFile(StringRef S) {
Simon Atanasyan16b0cc92015-11-26 05:53:00 +0000376 if (IsUnderSysroot && S.startswith("/")) {
377 SmallString<128> Path;
378 (Config->Sysroot + S).toStringRef(Path);
379 if (sys::fs::exists(Path)) {
380 Driver->addFile(Saver.save(Path.str()));
381 return;
382 }
383 }
384
Rui Ueyamaf03f3cc2015-10-13 00:09:21 +0000385 if (sys::path::is_absolute(S)) {
Rui Ueyama52a15092015-10-11 03:28:42 +0000386 Driver->addFile(S);
387 } else if (S.startswith("=")) {
388 if (Config->Sysroot.empty())
389 Driver->addFile(S.substr(1));
390 else
391 Driver->addFile(Saver.save(Config->Sysroot + "/" + S.substr(1)));
392 } else if (S.startswith("-l")) {
Rui Ueyama21eecb42016-02-02 21:13:09 +0000393 Driver->addLibrary(S.substr(2));
Simon Atanasyana1b8fc32015-11-26 20:23:46 +0000394 } else if (sys::fs::exists(S)) {
395 Driver->addFile(S);
Rui Ueyama52a15092015-10-11 03:28:42 +0000396 } else {
397 std::string Path = findFromSearchPaths(S);
398 if (Path.empty())
George Rimar777f9632016-03-12 08:31:34 +0000399 setError("unable to find " + S);
Rui Ueyama025d59b2016-02-02 20:27:59 +0000400 else
401 Driver->addFile(Saver.save(Path));
Rui Ueyama52a15092015-10-11 03:28:42 +0000402 }
403}
404
Rui Ueyama717677a2016-02-11 21:17:59 +0000405void ScriptParser::readAsNeeded() {
Rui Ueyamaf7c5fbb2015-09-30 17:23:26 +0000406 expect("(");
Rui Ueyama35da9b62015-10-11 20:59:12 +0000407 bool Orig = Config->AsNeeded;
408 Config->AsNeeded = true;
Rui Ueyama025d59b2016-02-02 20:27:59 +0000409 while (!Error) {
Rui Ueyamaf7c5fbb2015-09-30 17:23:26 +0000410 StringRef Tok = next();
411 if (Tok == ")")
Rui Ueyama35da9b62015-10-11 20:59:12 +0000412 break;
Rui Ueyama52a15092015-10-11 03:28:42 +0000413 addFile(Tok);
Rui Ueyamaf7c5fbb2015-09-30 17:23:26 +0000414 }
Rui Ueyama35da9b62015-10-11 20:59:12 +0000415 Config->AsNeeded = Orig;
Rui Ueyamaf7c5fbb2015-09-30 17:23:26 +0000416}
417
Rui Ueyama717677a2016-02-11 21:17:59 +0000418void ScriptParser::readEntry() {
Denis Protivensky90c50992015-10-08 06:48:38 +0000419 // -e <symbol> takes predecence over ENTRY(<symbol>).
420 expect("(");
421 StringRef Tok = next();
422 if (Config->Entry.empty())
423 Config->Entry = Tok;
424 expect(")");
425}
426
Rui Ueyama717677a2016-02-11 21:17:59 +0000427void ScriptParser::readExtern() {
George Rimar83f406c2015-10-19 17:35:12 +0000428 expect("(");
Rui Ueyama025d59b2016-02-02 20:27:59 +0000429 while (!Error) {
George Rimar83f406c2015-10-19 17:35:12 +0000430 StringRef Tok = next();
431 if (Tok == ")")
432 return;
433 Config->Undefined.push_back(Tok);
434 }
435}
436
Rui Ueyama717677a2016-02-11 21:17:59 +0000437void ScriptParser::readGroup() {
Rui Ueyamaf7c5fbb2015-09-30 17:23:26 +0000438 expect("(");
Rui Ueyama025d59b2016-02-02 20:27:59 +0000439 while (!Error) {
Rui Ueyamaf7c5fbb2015-09-30 17:23:26 +0000440 StringRef Tok = next();
441 if (Tok == ")")
442 return;
443 if (Tok == "AS_NEEDED") {
444 readAsNeeded();
445 continue;
446 }
Rui Ueyama52a15092015-10-11 03:28:42 +0000447 addFile(Tok);
Rui Ueyamaf7c5fbb2015-09-30 17:23:26 +0000448 }
449}
450
Rui Ueyama717677a2016-02-11 21:17:59 +0000451void ScriptParser::readInclude() {
Rui Ueyama31aa1f82015-10-11 01:31:55 +0000452 StringRef Tok = next();
453 auto MBOrErr = MemoryBuffer::getFile(Tok);
Rui Ueyama025d59b2016-02-02 20:27:59 +0000454 if (!MBOrErr) {
George Rimar57610422016-03-11 14:43:02 +0000455 setError("cannot open " + Tok);
Rui Ueyama025d59b2016-02-02 20:27:59 +0000456 return;
457 }
Rui Ueyama31aa1f82015-10-11 01:31:55 +0000458 std::unique_ptr<MemoryBuffer> &MB = *MBOrErr;
Rui Ueyamaa47ee682015-10-11 01:53:04 +0000459 StringRef S = Saver.save(MB->getMemBufferRef().getBuffer());
460 std::vector<StringRef> V = tokenize(S);
Rui Ueyama31aa1f82015-10-11 01:31:55 +0000461 Tokens.insert(Tokens.begin() + Pos, V.begin(), V.end());
Rui Ueyama31aa1f82015-10-11 01:31:55 +0000462}
463
Rui Ueyama717677a2016-02-11 21:17:59 +0000464void ScriptParser::readOutput() {
Rui Ueyamaee592822015-10-07 00:25:09 +0000465 // -o <file> takes predecence over OUTPUT(<file>).
466 expect("(");
467 StringRef Tok = next();
468 if (Config->OutputFile.empty())
469 Config->OutputFile = Tok;
470 expect(")");
471}
472
Rui Ueyama717677a2016-02-11 21:17:59 +0000473void ScriptParser::readOutputArch() {
Davide Italiano9159ce92015-10-12 21:50:08 +0000474 // Error checking only for now.
475 expect("(");
476 next();
477 expect(")");
478}
479
Rui Ueyama717677a2016-02-11 21:17:59 +0000480void ScriptParser::readOutputFormat() {
Rui Ueyamaf7c5fbb2015-09-30 17:23:26 +0000481 // Error checking only for now.
482 expect("(");
483 next();
Davide Italiano6836c612015-10-12 21:08:41 +0000484 StringRef Tok = next();
485 if (Tok == ")")
486 return;
Rui Ueyama025d59b2016-02-02 20:27:59 +0000487 if (Tok != ",") {
George Rimar57610422016-03-11 14:43:02 +0000488 setError("unexpected token: " + Tok);
Rui Ueyama025d59b2016-02-02 20:27:59 +0000489 return;
490 }
Davide Italiano6836c612015-10-12 21:08:41 +0000491 next();
492 expect(",");
493 next();
Rui Ueyamaf7c5fbb2015-09-30 17:23:26 +0000494 expect(")");
495}
496
Rui Ueyama717677a2016-02-11 21:17:59 +0000497void ScriptParser::readSearchDir() {
Davide Italiano68a39a62015-10-08 17:51:41 +0000498 expect("(");
Rafael Espindola06501922016-03-08 17:13:12 +0000499 Config->SearchPaths.push_back(next());
Davide Italiano68a39a62015-10-08 17:51:41 +0000500 expect(")");
501}
502
Rui Ueyama717677a2016-02-11 21:17:59 +0000503void ScriptParser::readSections() {
Rui Ueyama07320e42016-04-20 20:13:41 +0000504 Opt.DoLayout = true;
Denis Protivensky8e3b38a2015-11-12 09:52:08 +0000505 expect("{");
George Rimar652852c2016-04-16 10:10:32 +0000506 while (!Error && !skip("}")) {
507 StringRef Tok = peek();
Eugene Levianteda81a12016-07-12 06:39:48 +0000508 if (Tok == ".") {
George Rimar652852c2016-04-16 10:10:32 +0000509 readLocationCounterValue();
Eugene Levianteda81a12016-07-12 06:39:48 +0000510 continue;
511 }
512 next();
513 if (peek() == "=")
514 readSymbolAssignment(Tok);
George Rimar652852c2016-04-16 10:10:32 +0000515 else
Eugene Levianteda81a12016-07-12 06:39:48 +0000516 readOutputSectionDescription(Tok);
George Rimar652852c2016-04-16 10:10:32 +0000517 }
Denis Protivensky8e3b38a2015-11-12 09:52:08 +0000518}
519
George Rimar652852c2016-04-16 10:10:32 +0000520void ScriptParser::readLocationCounterValue() {
521 expect(".");
522 expect("=");
Eugene Levianteda81a12016-07-12 06:39:48 +0000523 std::vector<StringRef> Expr = readSectionsCommandExpr();
524 if (Expr.empty())
George Rimar652852c2016-04-16 10:10:32 +0000525 error("error in location counter expression");
Eugene Levianteda81a12016-07-12 06:39:48 +0000526 else
Rui Ueyama05ef4cf2016-07-15 04:19:37 +0000527 Opt.Commands.push_back({AssignmentKind, std::move(Expr), "."});
George Rimar652852c2016-04-16 10:10:32 +0000528}
529
Eugene Levianteda81a12016-07-12 06:39:48 +0000530void ScriptParser::readOutputSectionDescription(StringRef OutSec) {
Rui Ueyama07320e42016-04-20 20:13:41 +0000531 Opt.Commands.push_back({SectionKind, {}, OutSec});
Denis Protivensky8e3b38a2015-11-12 09:52:08 +0000532 expect(":");
533 expect("{");
Rui Ueyama8ec77e62016-04-21 22:00:51 +0000534
Rui Ueyama025d59b2016-02-02 20:27:59 +0000535 while (!Error && !skip("}")) {
George Rimar481c2ce2016-02-23 07:47:54 +0000536 StringRef Tok = next();
537 if (Tok == "*") {
Rui Ueyama8ec77e62016-04-21 22:00:51 +0000538 expect("(");
539 while (!Error && !skip(")"))
540 Opt.Sections.emplace_back(OutSec, next());
George Rimar481c2ce2016-02-23 07:47:54 +0000541 } else if (Tok == "KEEP") {
542 expect("(");
Rui Ueyama8ec77e62016-04-21 22:00:51 +0000543 expect("*");
544 expect("(");
545 while (!Error && !skip(")")) {
546 StringRef Sec = next();
547 Opt.Sections.emplace_back(OutSec, Sec);
548 Opt.KeptSections.push_back(Sec);
549 }
George Rimar481c2ce2016-02-23 07:47:54 +0000550 expect(")");
551 } else {
George Rimar777f9632016-03-12 08:31:34 +0000552 setError("unknown command " + Tok);
George Rimar481c2ce2016-02-23 07:47:54 +0000553 }
Denis Protivensky8e3b38a2015-11-12 09:52:08 +0000554 }
Rui Ueyama8ec77e62016-04-21 22:00:51 +0000555
George Rimare2ee72b2016-02-26 14:48:31 +0000556 StringRef Tok = peek();
557 if (Tok.startswith("=")) {
558 if (!Tok.startswith("=0x")) {
Rui Ueyama3ed2f062016-03-13 03:17:44 +0000559 setError("filler should be a hexadecimal value");
George Rimare2ee72b2016-02-26 14:48:31 +0000560 return;
561 }
Rui Ueyama3e808972016-02-28 05:09:11 +0000562 Tok = Tok.substr(3);
Rui Ueyama07320e42016-04-20 20:13:41 +0000563 Opt.Filler[OutSec] = parseHex(Tok);
George Rimare2ee72b2016-02-26 14:48:31 +0000564 next();
565 }
Denis Protivensky8e3b38a2015-11-12 09:52:08 +0000566}
567
Eugene Levianteda81a12016-07-12 06:39:48 +0000568void ScriptParser::readSymbolAssignment(StringRef Name) {
569 expect("=");
570 std::vector<StringRef> Expr = readSectionsCommandExpr();
571 if (Expr.empty())
572 error("error in symbol assignment expression");
573 else
Rui Ueyama05ef4cf2016-07-15 04:19:37 +0000574 Opt.Commands.push_back({AssignmentKind, std::move(Expr), Name});
Eugene Levianteda81a12016-07-12 06:39:48 +0000575}
576
577std::vector<StringRef> ScriptParser::readSectionsCommandExpr() {
578 std::vector<StringRef> Expr;
579 while (!Error) {
580 StringRef Tok = next();
581 if (Tok == ";")
582 break;
583 Expr.push_back(Tok);
584 }
585 return Expr;
586}
587
Simon Atanasyan16b0cc92015-11-26 05:53:00 +0000588static bool isUnderSysroot(StringRef Path) {
589 if (Config->Sysroot == "")
590 return false;
591 for (; !Path.empty(); Path = sys::path::parent_path(Path))
592 if (sys::fs::equivalent(Config->Sysroot, Path))
593 return true;
594 return false;
595}
596
Rui Ueyama07320e42016-04-20 20:13:41 +0000597// Entry point.
598void elf::readLinkerScript(MemoryBufferRef MB) {
Simon Atanasyan16b0cc92015-11-26 05:53:00 +0000599 StringRef Path = MB.getBufferIdentifier();
Rui Ueyama07320e42016-04-20 20:13:41 +0000600 ScriptParser(MB.getBuffer(), isUnderSysroot(Path)).run();
Rui Ueyamaf7c5fbb2015-09-30 17:23:26 +0000601}
Rui Ueyama1ebc8ed2016-02-12 21:47:28 +0000602
Rui Ueyama07320e42016-04-20 20:13:41 +0000603template class elf::LinkerScript<ELF32LE>;
604template class elf::LinkerScript<ELF32BE>;
605template class elf::LinkerScript<ELF64LE>;
606template class elf::LinkerScript<ELF64BE>;