blob: bb75a4b12d9bf180ba4f5d52a0be2960ac563ab4 [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"
Eugene Leviantbbe38602016-07-19 09:25:43 +000026#include "Writer.h"
Rui Ueyama960504b2016-04-19 18:58:11 +000027#include "llvm/ADT/StringSwitch.h"
George Rimar652852c2016-04-16 10:10:32 +000028#include "llvm/Support/ELF.h"
Rui Ueyamaf7c5fbb2015-09-30 17:23:26 +000029#include "llvm/Support/FileSystem.h"
30#include "llvm/Support/MemoryBuffer.h"
Rui Ueyamaf03f3cc2015-10-13 00:09:21 +000031#include "llvm/Support/Path.h"
Rui Ueyamaa47ee682015-10-11 01:53:04 +000032#include "llvm/Support/StringSaver.h"
Rui Ueyamaf7c5fbb2015-09-30 17:23:26 +000033
34using namespace llvm;
George Rimar652852c2016-04-16 10:10:32 +000035using namespace llvm::ELF;
Rui Ueyama1ebc8ed2016-02-12 21:47:28 +000036using namespace llvm::object;
Rui Ueyamaf7c5fbb2015-09-30 17:23:26 +000037using namespace lld;
Rafael Espindolae0df00b2016-02-28 00:25:54 +000038using namespace lld::elf;
Rui Ueyamaf7c5fbb2015-09-30 17:23:26 +000039
Rui Ueyama07320e42016-04-20 20:13:41 +000040ScriptConfiguration *elf::ScriptConfig;
Rui Ueyama717677a2016-02-11 21:17:59 +000041
George Rimar076fe152016-07-21 06:43:01 +000042bool SymbolAssignment::classof(const BaseCommand *C) {
43 return C->Kind == AssignmentKind;
44}
45
46bool OutputSectionCommand::classof(const BaseCommand *C) {
47 return C->Kind == OutputSectionKind;
48}
49
George Rimareea31142016-07-21 14:26:59 +000050bool InputSectionDescription::classof(const BaseCommand *C) {
51 return C->Kind == InputSectionKind;
52}
53
Rui Ueyama9c1112d2016-04-23 00:04:03 +000054// This is an operator-precedence parser to parse and evaluate
55// a linker script expression. For each linker script arithmetic
56// expression (e.g. ". = . + 0x1000"), a new instance of ExprParser
57// is created and ran.
58namespace {
59class ExprParser : public ScriptParserBase {
60public:
61 ExprParser(std::vector<StringRef> &Tokens, uint64_t Dot)
62 : ScriptParserBase(Tokens), Dot(Dot) {}
63
64 uint64_t run();
65
66private:
67 uint64_t parsePrimary();
68 uint64_t parseTernary(uint64_t Cond);
69 uint64_t apply(StringRef Op, uint64_t L, uint64_t R);
70 uint64_t parseExpr1(uint64_t Lhs, int MinPrec);
71 uint64_t parseExpr();
72
73 uint64_t Dot;
74};
75}
76
Rui Ueyama960504b2016-04-19 18:58:11 +000077static int precedence(StringRef Op) {
78 return StringSwitch<int>(Op)
79 .Case("*", 4)
George Rimarab939062016-04-25 08:14:41 +000080 .Case("/", 4)
81 .Case("+", 3)
82 .Case("-", 3)
83 .Case("<", 2)
84 .Case(">", 2)
85 .Case(">=", 2)
86 .Case("<=", 2)
87 .Case("==", 2)
88 .Case("!=", 2)
Rui Ueyama960504b2016-04-19 18:58:11 +000089 .Case("&", 1)
90 .Default(-1);
91}
92
Rui Ueyama9c1112d2016-04-23 00:04:03 +000093static uint64_t evalExpr(std::vector<StringRef> &Tokens, uint64_t Dot) {
94 return ExprParser(Tokens, Dot).run();
Rui Ueyama960504b2016-04-19 18:58:11 +000095}
96
Rui Ueyama9c1112d2016-04-23 00:04:03 +000097uint64_t ExprParser::run() {
98 uint64_t V = parseExpr();
99 if (!atEOF() && !Error)
100 setError("stray token: " + peek());
101 return V;
Rui Ueyama60118112016-04-20 20:54:13 +0000102}
103
Rui Ueyama960504b2016-04-19 18:58:11 +0000104// This is a part of the operator-precedence parser to evaluate
105// arithmetic expressions in SECTIONS command. This function evaluates an
Rui Ueyamae29a9752016-04-22 21:02:27 +0000106// integer literal, a parenthesized expression, the ALIGN function,
107// or the special variable ".".
Rui Ueyama9c1112d2016-04-23 00:04:03 +0000108uint64_t ExprParser::parsePrimary() {
109 StringRef Tok = next();
Rui Ueyama960504b2016-04-19 18:58:11 +0000110 if (Tok == ".")
111 return Dot;
112 if (Tok == "(") {
Rui Ueyama9c1112d2016-04-23 00:04:03 +0000113 uint64_t V = parseExpr();
114 expect(")");
Rui Ueyama960504b2016-04-19 18:58:11 +0000115 return V;
116 }
George Rimardffc1412016-04-22 11:40:53 +0000117 if (Tok == "ALIGN") {
Rui Ueyama9c1112d2016-04-23 00:04:03 +0000118 expect("(");
119 uint64_t V = parseExpr();
120 expect(")");
George Rimardffc1412016-04-22 11:40:53 +0000121 return alignTo(Dot, V);
122 }
Rui Ueyama5fa60982016-04-22 21:05:04 +0000123 uint64_t V = 0;
124 if (Tok.getAsInteger(0, V))
Rui Ueyama9c1112d2016-04-23 00:04:03 +0000125 setError("malformed number: " + Tok);
Rui Ueyama5fa60982016-04-22 21:05:04 +0000126 return V;
Rui Ueyama960504b2016-04-19 18:58:11 +0000127}
128
Rui Ueyama9c1112d2016-04-23 00:04:03 +0000129uint64_t ExprParser::parseTernary(uint64_t Cond) {
130 next();
131 uint64_t V = parseExpr();
132 expect(":");
133 uint64_t W = parseExpr();
George Rimarfba45c42016-04-22 11:28:54 +0000134 return Cond ? V : W;
135}
136
Rui Ueyama9c1112d2016-04-23 00:04:03 +0000137uint64_t ExprParser::apply(StringRef Op, uint64_t L, uint64_t R) {
Rui Ueyama960504b2016-04-19 18:58:11 +0000138 if (Op == "*")
139 return L * R;
140 if (Op == "/") {
141 if (R == 0) {
142 error("division by zero");
George Rimar652852c2016-04-16 10:10:32 +0000143 return 0;
144 }
Rui Ueyama960504b2016-04-19 18:58:11 +0000145 return L / R;
George Rimar652852c2016-04-16 10:10:32 +0000146 }
George Rimarab939062016-04-25 08:14:41 +0000147 if (Op == "+")
148 return L + R;
149 if (Op == "-")
150 return L - R;
151 if (Op == "<")
152 return L < R;
153 if (Op == ">")
154 return L > R;
155 if (Op == ">=")
156 return L >= R;
157 if (Op == "<=")
158 return L <= R;
159 if (Op == "==")
160 return L == R;
161 if (Op == "!=")
162 return L != R;
Rui Ueyama960504b2016-04-19 18:58:11 +0000163 if (Op == "&")
164 return L & R;
Rui Ueyama7a81d672016-04-19 19:04:03 +0000165 llvm_unreachable("invalid operator");
Rui Ueyama960504b2016-04-19 18:58:11 +0000166}
167
Rui Ueyama9c1112d2016-04-23 00:04:03 +0000168// This is a part of the operator-precedence parser.
169// This function assumes that the remaining token stream starts
170// with an operator.
171uint64_t ExprParser::parseExpr1(uint64_t Lhs, int MinPrec) {
172 while (!atEOF()) {
Rui Ueyama960504b2016-04-19 18:58:11 +0000173 // Read an operator and an expression.
Rui Ueyama9c1112d2016-04-23 00:04:03 +0000174 StringRef Op1 = peek();
George Rimarfba45c42016-04-22 11:28:54 +0000175 if (Op1 == "?")
Rui Ueyama9c1112d2016-04-23 00:04:03 +0000176 return parseTernary(Lhs);
Rui Ueyama960504b2016-04-19 18:58:11 +0000177 if (precedence(Op1) < MinPrec)
178 return Lhs;
Rui Ueyama9c1112d2016-04-23 00:04:03 +0000179 next();
180 uint64_t Rhs = parsePrimary();
Rui Ueyama960504b2016-04-19 18:58:11 +0000181
182 // Evaluate the remaining part of the expression first if the
183 // next operator has greater precedence than the previous one.
184 // For example, if we have read "+" and "3", and if the next
185 // operator is "*", then we'll evaluate 3 * ... part first.
Rui Ueyama9c1112d2016-04-23 00:04:03 +0000186 while (!atEOF()) {
187 StringRef Op2 = peek();
Rui Ueyama960504b2016-04-19 18:58:11 +0000188 if (precedence(Op2) <= precedence(Op1))
189 break;
Rui Ueyama9c1112d2016-04-23 00:04:03 +0000190 Rhs = parseExpr1(Rhs, precedence(Op2));
Rui Ueyama960504b2016-04-19 18:58:11 +0000191 }
192
193 Lhs = apply(Op1, Lhs, Rhs);
194 }
195 return Lhs;
196}
197
Rui Ueyama9c1112d2016-04-23 00:04:03 +0000198// Reads and evaluates an arithmetic expression.
199uint64_t ExprParser::parseExpr() { return parseExpr1(parsePrimary(), 0); }
George Rimar652852c2016-04-16 10:10:32 +0000200
Rui Ueyama1ebc8ed2016-02-12 21:47:28 +0000201template <class ELFT>
Rui Ueyama07320e42016-04-20 20:13:41 +0000202bool LinkerScript<ELFT>::isDiscarded(InputSectionBase<ELFT> *S) {
George Rimareea31142016-07-21 14:26:59 +0000203 return !S || !S->Live;
Rui Ueyama717677a2016-02-11 21:17:59 +0000204}
205
Rui Ueyama07320e42016-04-20 20:13:41 +0000206template <class ELFT>
207bool LinkerScript<ELFT>::shouldKeep(InputSectionBase<ELFT> *S) {
Rui Ueyama8ec77e62016-04-21 22:00:51 +0000208 for (StringRef Pat : Opt.KeptSections)
Rui Ueyama722830a2016-06-29 05:32:09 +0000209 if (globMatch(Pat, S->getSectionName()))
Rui Ueyama8ec77e62016-04-21 22:00:51 +0000210 return true;
211 return false;
George Rimar481c2ce2016-02-23 07:47:54 +0000212}
213
George Rimareea31142016-07-21 14:26:59 +0000214static bool match(StringRef Pattern, ArrayRef<StringRef> Arr) {
215 for (StringRef S : Arr)
216 if (globMatch(S, Pattern))
217 return true;
218 return false;
219}
220
George Rimar652852c2016-04-16 10:10:32 +0000221template <class ELFT>
Rui Ueyamaa7f78842016-07-20 17:19:03 +0000222std::vector<OutputSectionBase<ELFT> *>
Eugene Leviante63d81b2016-07-20 14:43:20 +0000223LinkerScript<ELFT>::createSections(OutputSectionFactory<ELFT> &Factory) {
George Rimareea31142016-07-21 14:26:59 +0000224 typedef const std::unique_ptr<ObjectFile<ELFT>> ObjectFile;
Rui Ueyamaa7f78842016-07-20 17:19:03 +0000225 std::vector<OutputSectionBase<ELFT> *> Result;
226
Eugene Leviante63d81b2016-07-20 14:43:20 +0000227 // Add input section to output section. If there is no output section yet,
228 // then create it and add to output section list.
229 auto AddInputSec = [&](InputSectionBase<ELFT> *C, StringRef Name) {
230 OutputSectionBase<ELFT> *Sec;
231 bool IsNew;
232 std::tie(Sec, IsNew) = Factory.create(C, Name);
233 if (IsNew)
Rui Ueyamaa7f78842016-07-20 17:19:03 +0000234 Result.push_back(Sec);
Eugene Leviante63d81b2016-07-20 14:43:20 +0000235 Sec->addSection(C);
236 };
237
238 // Select input sections matching rule and add them to corresponding
239 // output section. Section rules are processed in order they're listed
240 // in script, so correct input section order is maintained by design.
George Rimareea31142016-07-21 14:26:59 +0000241 for (const std::unique_ptr<BaseCommand> &Base : Opt.Commands) {
242 auto *OutCmd = dyn_cast<OutputSectionCommand>(Base.get());
243 if (!OutCmd)
244 continue;
245
246 for (const std::unique_ptr<BaseCommand> &Cmd : OutCmd->Commands) {
247 auto *InCmd = dyn_cast<InputSectionDescription>(Cmd.get());
248 if (!InCmd)
249 continue;
250
251 for (ObjectFile &F : Symtab<ELFT>::X->getObjectFiles()) {
252 for (InputSectionBase<ELFT> *S : F->getSections()) {
253 if (isDiscarded(S) || S->OutSec)
254 continue;
255
256 if (match(S->getSectionName(), InCmd->Patterns)) {
257 if (OutCmd->Name == "/DISCARD/")
258 S->Live = false;
259 else
260 AddInputSec(S, OutCmd->Name);
261 }
262 }
263 }
264 }
265 }
Eugene Leviante63d81b2016-07-20 14:43:20 +0000266
267 // Add all other input sections, which are not listed in script.
George Rimareea31142016-07-21 14:26:59 +0000268 for (ObjectFile &F : Symtab<ELFT>::X->getObjectFiles())
Eugene Leviante63d81b2016-07-20 14:43:20 +0000269 for (InputSectionBase<ELFT> *S : F->getSections())
270 if (!isDiscarded(S)) {
271 if (!S->OutSec)
272 AddInputSec(S, getOutputSectionName(S));
273 } else
274 reportDiscarded(S, F);
275
276 return Result;
277}
278
279template <class ELFT>
Rui Ueyama07320e42016-04-20 20:13:41 +0000280void LinkerScript<ELFT>::assignAddresses(
George Rimardbbd8b12016-04-21 11:21:48 +0000281 ArrayRef<OutputSectionBase<ELFT> *> Sections) {
George Rimar652852c2016-04-16 10:10:32 +0000282 // Orphan sections are sections present in the input files which
Rui Ueyama7c18c282016-04-18 21:00:40 +0000283 // are not explicitly placed into the output file by the linker script.
284 // We place orphan sections at end of file.
285 // Other linkers places them using some heuristics as described in
George Rimar652852c2016-04-16 10:10:32 +0000286 // https://sourceware.org/binutils/docs/ld/Orphan-Sections.html#Orphan-Sections.
Rui Ueyama7c18c282016-04-18 21:00:40 +0000287 for (OutputSectionBase<ELFT> *Sec : Sections) {
George Rimar652852c2016-04-16 10:10:32 +0000288 StringRef Name = Sec->getName();
Rui Ueyamac3e2a4b2016-04-21 20:30:00 +0000289 if (getSectionIndex(Name) == INT_MAX)
George Rimar076fe152016-07-21 06:43:01 +0000290 Opt.Commands.push_back(llvm::make_unique<OutputSectionCommand>(Name));
George Rimar652852c2016-04-16 10:10:32 +0000291 }
George Rimar652852c2016-04-16 10:10:32 +0000292
Rui Ueyama7c18c282016-04-18 21:00:40 +0000293 // Assign addresses as instructed by linker script SECTIONS sub-commands.
Rui Ueyama52c4e172016-07-01 10:42:25 +0000294 Dot = Out<ELFT>::ElfHeader->getSize() + Out<ELFT>::ProgramHeaders->getSize();
Eugene Leviant467c4d52016-07-01 10:27:36 +0000295 uintX_t MinVA = std::numeric_limits<uintX_t>::max();
George Rimar652852c2016-04-16 10:10:32 +0000296 uintX_t ThreadBssOffset = 0;
George Rimar652852c2016-04-16 10:10:32 +0000297
George Rimar076fe152016-07-21 06:43:01 +0000298 for (const std::unique_ptr<BaseCommand> &Base : Opt.Commands) {
299 if (auto *Cmd = dyn_cast<SymbolAssignment>(Base.get())) {
300 uint64_t Val = evalExpr(Cmd->Expr, Dot);
301 if (Cmd->Name == ".") {
Rui Ueyama05ef4cf2016-07-15 04:19:37 +0000302
Rui Ueyama05ef4cf2016-07-15 04:19:37 +0000303 Dot = Val;
304 } else {
George Rimar076fe152016-07-21 06:43:01 +0000305 auto *D = cast<DefinedRegular<ELFT>>(Symtab<ELFT>::X->find(Cmd->Name));
Rui Ueyama05ef4cf2016-07-15 04:19:37 +0000306 D->Value = Val;
307 }
George Rimar652852c2016-04-16 10:10:32 +0000308 continue;
309 }
310
Dima Stepanovfb8978f2016-05-19 18:15:54 +0000311 // Find all the sections with required name. There can be more than
George Rimar6ad330a2016-07-19 07:39:07 +0000312 // one section with such name, if the alignment, flags or type
Dima Stepanovfb8978f2016-05-19 18:15:54 +0000313 // attribute differs.
George Rimar076fe152016-07-21 06:43:01 +0000314 auto *Cmd = cast<OutputSectionCommand>(Base.get());
Dima Stepanovfb8978f2016-05-19 18:15:54 +0000315 for (OutputSectionBase<ELFT> *Sec : Sections) {
George Rimar076fe152016-07-21 06:43:01 +0000316 if (Sec->getName() != Cmd->Name)
Dima Stepanovfb8978f2016-05-19 18:15:54 +0000317 continue;
George Rimar652852c2016-04-16 10:10:32 +0000318
Dima Stepanovfb8978f2016-05-19 18:15:54 +0000319 if ((Sec->getFlags() & SHF_TLS) && Sec->getType() == SHT_NOBITS) {
320 uintX_t TVA = Dot + ThreadBssOffset;
Rui Ueyama424b4082016-06-17 01:18:46 +0000321 TVA = alignTo(TVA, Sec->getAlignment());
Dima Stepanovfb8978f2016-05-19 18:15:54 +0000322 Sec->setVA(TVA);
323 ThreadBssOffset = TVA - Dot + Sec->getSize();
324 continue;
325 }
George Rimar652852c2016-04-16 10:10:32 +0000326
Dima Stepanovfb8978f2016-05-19 18:15:54 +0000327 if (Sec->getFlags() & SHF_ALLOC) {
Rui Ueyama424b4082016-06-17 01:18:46 +0000328 Dot = alignTo(Dot, Sec->getAlignment());
Dima Stepanovfb8978f2016-05-19 18:15:54 +0000329 Sec->setVA(Dot);
Rui Ueyama52c4e172016-07-01 10:42:25 +0000330 MinVA = std::min(MinVA, Dot);
Dima Stepanovfb8978f2016-05-19 18:15:54 +0000331 Dot += Sec->getSize();
332 continue;
333 }
George Rimar652852c2016-04-16 10:10:32 +0000334 }
335 }
Rui Ueyama52c4e172016-07-01 10:42:25 +0000336
Rafael Espindola64c32d62016-07-07 14:28:47 +0000337 // ELF and Program headers need to be right before the first section in
George Rimarb91e7112016-07-19 07:42:07 +0000338 // memory. Set their addresses accordingly.
Eugene Leviant467c4d52016-07-01 10:27:36 +0000339 MinVA = alignDown(MinVA - Out<ELFT>::ElfHeader->getSize() -
340 Out<ELFT>::ProgramHeaders->getSize(),
341 Target->PageSize);
342 Out<ELFT>::ElfHeader->setVA(MinVA);
343 Out<ELFT>::ProgramHeaders->setVA(Out<ELFT>::ElfHeader->getSize() + MinVA);
George Rimar652852c2016-04-16 10:10:32 +0000344}
345
Rui Ueyama07320e42016-04-20 20:13:41 +0000346template <class ELFT>
Rafael Espindola74df5c72016-07-19 12:33:46 +0000347std::vector<PhdrEntry<ELFT>>
Eugene Leviantbbe38602016-07-19 09:25:43 +0000348LinkerScript<ELFT>::createPhdrs(ArrayRef<OutputSectionBase<ELFT> *> Sections) {
349 int TlsNum = -1;
350 int NoteNum = -1;
351 int RelroNum = -1;
352 Phdr *Load = nullptr;
353 uintX_t Flags = PF_R;
354 std::vector<Phdr> Phdrs;
355
356 for (const PhdrsCommand &Cmd : Opt.PhdrsCommands) {
Eugene Leviant865bf862016-07-21 10:43:25 +0000357 Phdrs.emplace_back(Cmd.Type, Cmd.Flags == UINT_MAX ? PF_R : Cmd.Flags);
Eugene Leviantbbe38602016-07-19 09:25:43 +0000358 Phdr &Added = Phdrs.back();
359
360 if (Cmd.HasFilehdr)
Rui Ueyama18f084f2016-07-20 19:36:41 +0000361 Added.add(Out<ELFT>::ElfHeader);
Eugene Leviantbbe38602016-07-19 09:25:43 +0000362 if (Cmd.HasPhdrs)
Rui Ueyama18f084f2016-07-20 19:36:41 +0000363 Added.add(Out<ELFT>::ProgramHeaders);
Eugene Leviantbbe38602016-07-19 09:25:43 +0000364
365 switch (Cmd.Type) {
366 case PT_INTERP:
Rui Ueyamafd03cfd2016-07-21 11:01:23 +0000367 if (Out<ELFT>::Interp)
Rui Ueyama18f084f2016-07-20 19:36:41 +0000368 Added.add(Out<ELFT>::Interp);
Eugene Leviantbbe38602016-07-19 09:25:43 +0000369 break;
370 case PT_DYNAMIC:
371 if (isOutputDynamic<ELFT>()) {
372 Added.H.p_flags = toPhdrFlags(Out<ELFT>::Dynamic->getFlags());
Rui Ueyama18f084f2016-07-20 19:36:41 +0000373 Added.add(Out<ELFT>::Dynamic);
Eugene Leviantbbe38602016-07-19 09:25:43 +0000374 }
375 break;
376 case PT_TLS:
377 TlsNum = Phdrs.size() - 1;
378 break;
379 case PT_NOTE:
380 NoteNum = Phdrs.size() - 1;
381 break;
382 case PT_GNU_RELRO:
383 RelroNum = Phdrs.size() - 1;
384 break;
385 case PT_GNU_EH_FRAME:
386 if (!Out<ELFT>::EhFrame->empty() && Out<ELFT>::EhFrameHdr) {
387 Added.H.p_flags = toPhdrFlags(Out<ELFT>::EhFrameHdr->getFlags());
Rui Ueyama18f084f2016-07-20 19:36:41 +0000388 Added.add(Out<ELFT>::EhFrameHdr);
Eugene Leviantbbe38602016-07-19 09:25:43 +0000389 }
390 break;
391 }
392 }
393
394 for (OutputSectionBase<ELFT> *Sec : Sections) {
395 if (!(Sec->getFlags() & SHF_ALLOC))
396 break;
397
398 if (TlsNum != -1 && (Sec->getFlags() & SHF_TLS))
Rui Ueyama18f084f2016-07-20 19:36:41 +0000399 Phdrs[TlsNum].add(Sec);
Eugene Leviantbbe38602016-07-19 09:25:43 +0000400
401 if (!needsPtLoad<ELFT>(Sec))
402 continue;
403
404 const std::vector<size_t> &PhdrIds =
405 getPhdrIndicesForSection(Sec->getName());
406 if (!PhdrIds.empty()) {
407 // Assign headers specified by linker script
408 for (size_t Id : PhdrIds) {
Rui Ueyama18f084f2016-07-20 19:36:41 +0000409 Phdrs[Id].add(Sec);
Eugene Leviant865bf862016-07-21 10:43:25 +0000410 if (Opt.PhdrsCommands[Id].Flags == UINT_MAX)
411 Phdrs[Id].H.p_flags |= toPhdrFlags(Sec->getFlags());
Eugene Leviantbbe38602016-07-19 09:25:43 +0000412 Phdrs[Id].H.p_flags |= toPhdrFlags(Sec->getFlags());
413 }
414 } else {
415 // If we have no load segment or flags've changed then we want new load
416 // segment.
417 uintX_t NewFlags = toPhdrFlags(Sec->getFlags());
418 if (Load == nullptr || Flags != NewFlags) {
419 Load = &*Phdrs.emplace(Phdrs.end(), PT_LOAD, NewFlags);
420 Flags = NewFlags;
421 }
Rui Ueyama18f084f2016-07-20 19:36:41 +0000422 Load->add(Sec);
Eugene Leviantbbe38602016-07-19 09:25:43 +0000423 }
424
425 if (RelroNum != -1 && isRelroSection(Sec))
Rui Ueyama18f084f2016-07-20 19:36:41 +0000426 Phdrs[RelroNum].add(Sec);
Eugene Leviantbbe38602016-07-19 09:25:43 +0000427 if (NoteNum != -1 && Sec->getType() == SHT_NOTE)
Rui Ueyama18f084f2016-07-20 19:36:41 +0000428 Phdrs[NoteNum].add(Sec);
Eugene Leviantbbe38602016-07-19 09:25:43 +0000429 }
430 return Phdrs;
431}
432
433template <class ELFT>
Rui Ueyama07320e42016-04-20 20:13:41 +0000434ArrayRef<uint8_t> LinkerScript<ELFT>::getFiller(StringRef Name) {
George Rimarf6c3cce2016-07-21 07:48:54 +0000435 for (const std::unique_ptr<BaseCommand> &Base : Opt.Commands)
436 if (auto *Cmd = dyn_cast<OutputSectionCommand>(Base.get()))
437 if (Cmd->Name == Name)
438 return Cmd->Filler;
439 return {};
George Rimare2ee72b2016-02-26 14:48:31 +0000440}
441
Rui Ueyamac3e2a4b2016-04-21 20:30:00 +0000442// Returns the index of the given section name in linker script
443// SECTIONS commands. Sections are laid out as the same order as they
444// were in the script. If a given name did not appear in the script,
445// it returns INT_MAX, so that it will be laid out at end of file.
George Rimar076fe152016-07-21 06:43:01 +0000446template <class ELFT> int LinkerScript<ELFT>::getSectionIndex(StringRef Name) {
George Rimar71b26e92016-04-21 10:22:02 +0000447 auto Begin = Opt.Commands.begin();
448 auto End = Opt.Commands.end();
George Rimar076fe152016-07-21 06:43:01 +0000449 auto I =
450 std::find_if(Begin, End, [&](const std::unique_ptr<BaseCommand> &Base) {
451 if (auto *Cmd = dyn_cast<OutputSectionCommand>(Base.get()))
452 if (Cmd->Name == Name)
453 return true;
454 return false;
455 });
Rui Ueyamac3e2a4b2016-04-21 20:30:00 +0000456 return I == End ? INT_MAX : (I - Begin);
George Rimar71b26e92016-04-21 10:22:02 +0000457}
458
459// A compartor to sort output sections. Returns -1 or 1 if
460// A or B are mentioned in linker script. Otherwise, returns 0.
Rui Ueyama07320e42016-04-20 20:13:41 +0000461template <class ELFT>
462int LinkerScript<ELFT>::compareSections(StringRef A, StringRef B) {
Rui Ueyamac3e2a4b2016-04-21 20:30:00 +0000463 int I = getSectionIndex(A);
464 int J = getSectionIndex(B);
465 if (I == INT_MAX && J == INT_MAX)
Rui Ueyama717677a2016-02-11 21:17:59 +0000466 return 0;
467 return I < J ? -1 : 1;
468}
469
George Rimar076fe152016-07-21 06:43:01 +0000470template <class ELFT> void LinkerScript<ELFT>::addScriptedSymbols() {
471 for (const std::unique_ptr<BaseCommand> &Base : Opt.Commands)
472 if (auto *Cmd = dyn_cast<SymbolAssignment>(Base.get()))
473 if (Cmd->Name != "." && Symtab<ELFT>::X->find(Cmd->Name) == nullptr)
474 Symtab<ELFT>::X->addAbsolute(Cmd->Name, STV_DEFAULT);
Eugene Levianteda81a12016-07-12 06:39:48 +0000475}
476
Eugene Leviantbbe38602016-07-19 09:25:43 +0000477template <class ELFT> bool LinkerScript<ELFT>::hasPhdrsCommands() {
478 return !Opt.PhdrsCommands.empty();
479}
480
481// Returns indices of ELF headers containing specific section, identified
482// by Name. Each index is a zero based number of ELF header listed within
483// PHDRS {} script block.
484template <class ELFT>
485std::vector<size_t>
486LinkerScript<ELFT>::getPhdrIndicesForSection(StringRef Name) {
George Rimar076fe152016-07-21 06:43:01 +0000487 for (const std::unique_ptr<BaseCommand> &Base : Opt.Commands) {
488 auto *Cmd = dyn_cast<OutputSectionCommand>(Base.get());
489 if (!Cmd || Cmd->Name != Name)
George Rimar31d842f2016-07-20 16:43:03 +0000490 continue;
491
492 std::vector<size_t> Indices;
George Rimar076fe152016-07-21 06:43:01 +0000493 for (StringRef PhdrName : Cmd->Phdrs) {
George Rimar31d842f2016-07-20 16:43:03 +0000494 auto ItPhdr =
495 std::find_if(Opt.PhdrsCommands.rbegin(), Opt.PhdrsCommands.rend(),
George Rimar076fe152016-07-21 06:43:01 +0000496 [&](PhdrsCommand &P) { return P.Name == PhdrName; });
Eugene Leviantbbe38602016-07-19 09:25:43 +0000497 if (ItPhdr == Opt.PhdrsCommands.rend())
498 error("section header '" + PhdrName + "' is not listed in PHDRS");
499 else
500 Indices.push_back(std::distance(ItPhdr, Opt.PhdrsCommands.rend()) - 1);
501 }
George Rimar31d842f2016-07-20 16:43:03 +0000502 return Indices;
Eugene Leviantbbe38602016-07-19 09:25:43 +0000503 }
George Rimar31d842f2016-07-20 16:43:03 +0000504 return {};
Eugene Leviantbbe38602016-07-19 09:25:43 +0000505}
506
Rui Ueyama07320e42016-04-20 20:13:41 +0000507class elf::ScriptParser : public ScriptParserBase {
George Rimarc3794e52016-02-24 09:21:47 +0000508 typedef void (ScriptParser::*Handler)();
509
Rui Ueyamaf7c5fbb2015-09-30 17:23:26 +0000510public:
Rui Ueyama07320e42016-04-20 20:13:41 +0000511 ScriptParser(StringRef S, bool B) : ScriptParserBase(S), IsUnderSysroot(B) {}
George Rimarf23b2322016-02-19 10:45:45 +0000512
Rui Ueyama4a465392016-04-22 22:59:24 +0000513 void run();
Rui Ueyamaf7c5fbb2015-09-30 17:23:26 +0000514
515private:
Rui Ueyama52a15092015-10-11 03:28:42 +0000516 void addFile(StringRef Path);
517
Rui Ueyamaf7c5fbb2015-09-30 17:23:26 +0000518 void readAsNeeded();
Denis Protivensky90c50992015-10-08 06:48:38 +0000519 void readEntry();
George Rimar83f406c2015-10-19 17:35:12 +0000520 void readExtern();
Rui Ueyamaf7c5fbb2015-09-30 17:23:26 +0000521 void readGroup();
Rui Ueyama31aa1f82015-10-11 01:31:55 +0000522 void readInclude();
George Rimarc3794e52016-02-24 09:21:47 +0000523 void readNothing() {}
Rui Ueyamaee592822015-10-07 00:25:09 +0000524 void readOutput();
Davide Italiano9159ce92015-10-12 21:50:08 +0000525 void readOutputArch();
Rui Ueyamaf7c5fbb2015-09-30 17:23:26 +0000526 void readOutputFormat();
Eugene Leviantbbe38602016-07-19 09:25:43 +0000527 void readPhdrs();
Davide Italiano68a39a62015-10-08 17:51:41 +0000528 void readSearchDir();
Denis Protivensky8e3b38a2015-11-12 09:52:08 +0000529 void readSections();
530
George Rimar652852c2016-04-16 10:10:32 +0000531 void readLocationCounterValue();
Eugene Levianteda81a12016-07-12 06:39:48 +0000532 void readOutputSectionDescription(StringRef OutSec);
Eugene Leviantbbe38602016-07-19 09:25:43 +0000533 std::vector<StringRef> readOutputSectionPhdrs();
534 unsigned readPhdrType();
Eugene Levianteda81a12016-07-12 06:39:48 +0000535 void readSymbolAssignment(StringRef Name);
536 std::vector<StringRef> readSectionsCommandExpr();
Rui Ueyamaf7c5fbb2015-09-30 17:23:26 +0000537
George Rimarc3794e52016-02-24 09:21:47 +0000538 const static StringMap<Handler> Cmd;
Rui Ueyama07320e42016-04-20 20:13:41 +0000539 ScriptConfiguration &Opt = *ScriptConfig;
540 StringSaver Saver = {ScriptConfig->Alloc};
Simon Atanasyan16b0cc92015-11-26 05:53:00 +0000541 bool IsUnderSysroot;
Rui Ueyamaf7c5fbb2015-09-30 17:23:26 +0000542};
Rui Ueyamaf7c5fbb2015-09-30 17:23:26 +0000543
Rafael Espindolae0df00b2016-02-28 00:25:54 +0000544const StringMap<elf::ScriptParser::Handler> elf::ScriptParser::Cmd = {
George Rimarc3794e52016-02-24 09:21:47 +0000545 {"ENTRY", &ScriptParser::readEntry},
546 {"EXTERN", &ScriptParser::readExtern},
547 {"GROUP", &ScriptParser::readGroup},
548 {"INCLUDE", &ScriptParser::readInclude},
549 {"INPUT", &ScriptParser::readGroup},
550 {"OUTPUT", &ScriptParser::readOutput},
551 {"OUTPUT_ARCH", &ScriptParser::readOutputArch},
552 {"OUTPUT_FORMAT", &ScriptParser::readOutputFormat},
Eugene Leviantbbe38602016-07-19 09:25:43 +0000553 {"PHDRS", &ScriptParser::readPhdrs},
George Rimarc3794e52016-02-24 09:21:47 +0000554 {"SEARCH_DIR", &ScriptParser::readSearchDir},
555 {"SECTIONS", &ScriptParser::readSections},
556 {";", &ScriptParser::readNothing}};
557
Rui Ueyama717677a2016-02-11 21:17:59 +0000558void ScriptParser::run() {
Rui Ueyamaf7c5fbb2015-09-30 17:23:26 +0000559 while (!atEOF()) {
560 StringRef Tok = next();
George Rimarc3794e52016-02-24 09:21:47 +0000561 if (Handler Fn = Cmd.lookup(Tok))
562 (this->*Fn)();
563 else
George Rimar57610422016-03-11 14:43:02 +0000564 setError("unknown directive: " + Tok);
Rui Ueyamaf7c5fbb2015-09-30 17:23:26 +0000565 }
566}
567
Rui Ueyama717677a2016-02-11 21:17:59 +0000568void ScriptParser::addFile(StringRef S) {
Simon Atanasyan16b0cc92015-11-26 05:53:00 +0000569 if (IsUnderSysroot && S.startswith("/")) {
570 SmallString<128> Path;
571 (Config->Sysroot + S).toStringRef(Path);
572 if (sys::fs::exists(Path)) {
573 Driver->addFile(Saver.save(Path.str()));
574 return;
575 }
576 }
577
Rui Ueyamaf03f3cc2015-10-13 00:09:21 +0000578 if (sys::path::is_absolute(S)) {
Rui Ueyama52a15092015-10-11 03:28:42 +0000579 Driver->addFile(S);
580 } else if (S.startswith("=")) {
581 if (Config->Sysroot.empty())
582 Driver->addFile(S.substr(1));
583 else
584 Driver->addFile(Saver.save(Config->Sysroot + "/" + S.substr(1)));
585 } else if (S.startswith("-l")) {
Rui Ueyama21eecb42016-02-02 21:13:09 +0000586 Driver->addLibrary(S.substr(2));
Simon Atanasyana1b8fc32015-11-26 20:23:46 +0000587 } else if (sys::fs::exists(S)) {
588 Driver->addFile(S);
Rui Ueyama52a15092015-10-11 03:28:42 +0000589 } else {
590 std::string Path = findFromSearchPaths(S);
591 if (Path.empty())
George Rimar777f9632016-03-12 08:31:34 +0000592 setError("unable to find " + S);
Rui Ueyama025d59b2016-02-02 20:27:59 +0000593 else
594 Driver->addFile(Saver.save(Path));
Rui Ueyama52a15092015-10-11 03:28:42 +0000595 }
596}
597
Rui Ueyama717677a2016-02-11 21:17:59 +0000598void ScriptParser::readAsNeeded() {
Rui Ueyamaf7c5fbb2015-09-30 17:23:26 +0000599 expect("(");
Rui Ueyama35da9b62015-10-11 20:59:12 +0000600 bool Orig = Config->AsNeeded;
601 Config->AsNeeded = true;
Rui Ueyama025d59b2016-02-02 20:27:59 +0000602 while (!Error) {
Rui Ueyamaf7c5fbb2015-09-30 17:23:26 +0000603 StringRef Tok = next();
604 if (Tok == ")")
Rui Ueyama35da9b62015-10-11 20:59:12 +0000605 break;
Rui Ueyama52a15092015-10-11 03:28:42 +0000606 addFile(Tok);
Rui Ueyamaf7c5fbb2015-09-30 17:23:26 +0000607 }
Rui Ueyama35da9b62015-10-11 20:59:12 +0000608 Config->AsNeeded = Orig;
Rui Ueyamaf7c5fbb2015-09-30 17:23:26 +0000609}
610
Rui Ueyama717677a2016-02-11 21:17:59 +0000611void ScriptParser::readEntry() {
Denis Protivensky90c50992015-10-08 06:48:38 +0000612 // -e <symbol> takes predecence over ENTRY(<symbol>).
613 expect("(");
614 StringRef Tok = next();
615 if (Config->Entry.empty())
616 Config->Entry = Tok;
617 expect(")");
618}
619
Rui Ueyama717677a2016-02-11 21:17:59 +0000620void ScriptParser::readExtern() {
George Rimar83f406c2015-10-19 17:35:12 +0000621 expect("(");
Rui Ueyama025d59b2016-02-02 20:27:59 +0000622 while (!Error) {
George Rimar83f406c2015-10-19 17:35:12 +0000623 StringRef Tok = next();
624 if (Tok == ")")
625 return;
626 Config->Undefined.push_back(Tok);
627 }
628}
629
Rui Ueyama717677a2016-02-11 21:17:59 +0000630void ScriptParser::readGroup() {
Rui Ueyamaf7c5fbb2015-09-30 17:23:26 +0000631 expect("(");
Rui Ueyama025d59b2016-02-02 20:27:59 +0000632 while (!Error) {
Rui Ueyamaf7c5fbb2015-09-30 17:23:26 +0000633 StringRef Tok = next();
634 if (Tok == ")")
635 return;
636 if (Tok == "AS_NEEDED") {
637 readAsNeeded();
638 continue;
639 }
Rui Ueyama52a15092015-10-11 03:28:42 +0000640 addFile(Tok);
Rui Ueyamaf7c5fbb2015-09-30 17:23:26 +0000641 }
642}
643
Rui Ueyama717677a2016-02-11 21:17:59 +0000644void ScriptParser::readInclude() {
Rui Ueyama31aa1f82015-10-11 01:31:55 +0000645 StringRef Tok = next();
646 auto MBOrErr = MemoryBuffer::getFile(Tok);
Rui Ueyama025d59b2016-02-02 20:27:59 +0000647 if (!MBOrErr) {
George Rimar57610422016-03-11 14:43:02 +0000648 setError("cannot open " + Tok);
Rui Ueyama025d59b2016-02-02 20:27:59 +0000649 return;
650 }
Rui Ueyama31aa1f82015-10-11 01:31:55 +0000651 std::unique_ptr<MemoryBuffer> &MB = *MBOrErr;
Rui Ueyamaa47ee682015-10-11 01:53:04 +0000652 StringRef S = Saver.save(MB->getMemBufferRef().getBuffer());
653 std::vector<StringRef> V = tokenize(S);
Rui Ueyama31aa1f82015-10-11 01:31:55 +0000654 Tokens.insert(Tokens.begin() + Pos, V.begin(), V.end());
Rui Ueyama31aa1f82015-10-11 01:31:55 +0000655}
656
Rui Ueyama717677a2016-02-11 21:17:59 +0000657void ScriptParser::readOutput() {
Rui Ueyamaee592822015-10-07 00:25:09 +0000658 // -o <file> takes predecence over OUTPUT(<file>).
659 expect("(");
660 StringRef Tok = next();
661 if (Config->OutputFile.empty())
662 Config->OutputFile = Tok;
663 expect(")");
664}
665
Rui Ueyama717677a2016-02-11 21:17:59 +0000666void ScriptParser::readOutputArch() {
Davide Italiano9159ce92015-10-12 21:50:08 +0000667 // Error checking only for now.
668 expect("(");
669 next();
670 expect(")");
671}
672
Rui Ueyama717677a2016-02-11 21:17:59 +0000673void ScriptParser::readOutputFormat() {
Rui Ueyamaf7c5fbb2015-09-30 17:23:26 +0000674 // Error checking only for now.
675 expect("(");
676 next();
Davide Italiano6836c612015-10-12 21:08:41 +0000677 StringRef Tok = next();
678 if (Tok == ")")
679 return;
Rui Ueyama025d59b2016-02-02 20:27:59 +0000680 if (Tok != ",") {
George Rimar57610422016-03-11 14:43:02 +0000681 setError("unexpected token: " + Tok);
Rui Ueyama025d59b2016-02-02 20:27:59 +0000682 return;
683 }
Davide Italiano6836c612015-10-12 21:08:41 +0000684 next();
685 expect(",");
686 next();
Rui Ueyamaf7c5fbb2015-09-30 17:23:26 +0000687 expect(")");
688}
689
Eugene Leviantbbe38602016-07-19 09:25:43 +0000690void ScriptParser::readPhdrs() {
691 expect("{");
692 while (!Error && !skip("}")) {
693 StringRef Tok = next();
Eugene Leviant865bf862016-07-21 10:43:25 +0000694 Opt.PhdrsCommands.push_back({Tok, PT_NULL, false, false, UINT_MAX});
Eugene Leviantbbe38602016-07-19 09:25:43 +0000695 PhdrsCommand &PhdrCmd = Opt.PhdrsCommands.back();
696
697 PhdrCmd.Type = readPhdrType();
698 do {
699 Tok = next();
700 if (Tok == ";")
701 break;
702 if (Tok == "FILEHDR")
703 PhdrCmd.HasFilehdr = true;
704 else if (Tok == "PHDRS")
705 PhdrCmd.HasPhdrs = true;
Eugene Leviant865bf862016-07-21 10:43:25 +0000706 else if (Tok == "FLAGS") {
707 expect("(");
708 next().getAsInteger(0, PhdrCmd.Flags);
709 expect(")");
710 } else
Eugene Leviantbbe38602016-07-19 09:25:43 +0000711 setError("unexpected header attribute: " + Tok);
712 } while (!Error);
713 }
714}
715
Rui Ueyama717677a2016-02-11 21:17:59 +0000716void ScriptParser::readSearchDir() {
Davide Italiano68a39a62015-10-08 17:51:41 +0000717 expect("(");
Rafael Espindola06501922016-03-08 17:13:12 +0000718 Config->SearchPaths.push_back(next());
Davide Italiano68a39a62015-10-08 17:51:41 +0000719 expect(")");
720}
721
Rui Ueyama717677a2016-02-11 21:17:59 +0000722void ScriptParser::readSections() {
Rui Ueyama07320e42016-04-20 20:13:41 +0000723 Opt.DoLayout = true;
Denis Protivensky8e3b38a2015-11-12 09:52:08 +0000724 expect("{");
George Rimar652852c2016-04-16 10:10:32 +0000725 while (!Error && !skip("}")) {
726 StringRef Tok = peek();
Eugene Levianteda81a12016-07-12 06:39:48 +0000727 if (Tok == ".") {
George Rimar652852c2016-04-16 10:10:32 +0000728 readLocationCounterValue();
Eugene Levianteda81a12016-07-12 06:39:48 +0000729 continue;
730 }
731 next();
732 if (peek() == "=")
733 readSymbolAssignment(Tok);
George Rimar652852c2016-04-16 10:10:32 +0000734 else
Eugene Levianteda81a12016-07-12 06:39:48 +0000735 readOutputSectionDescription(Tok);
George Rimar652852c2016-04-16 10:10:32 +0000736 }
Denis Protivensky8e3b38a2015-11-12 09:52:08 +0000737}
738
George Rimar652852c2016-04-16 10:10:32 +0000739void ScriptParser::readLocationCounterValue() {
740 expect(".");
741 expect("=");
Eugene Levianteda81a12016-07-12 06:39:48 +0000742 std::vector<StringRef> Expr = readSectionsCommandExpr();
743 if (Expr.empty())
George Rimar652852c2016-04-16 10:10:32 +0000744 error("error in location counter expression");
Eugene Levianteda81a12016-07-12 06:39:48 +0000745 else
George Rimar076fe152016-07-21 06:43:01 +0000746 Opt.Commands.push_back(llvm::make_unique<SymbolAssignment>(".", Expr));
George Rimar652852c2016-04-16 10:10:32 +0000747}
748
Eugene Levianteda81a12016-07-12 06:39:48 +0000749void ScriptParser::readOutputSectionDescription(StringRef OutSec) {
George Rimar076fe152016-07-21 06:43:01 +0000750 OutputSectionCommand *Cmd = new OutputSectionCommand(OutSec);
751 Opt.Commands.emplace_back(Cmd);
Denis Protivensky8e3b38a2015-11-12 09:52:08 +0000752 expect(":");
753 expect("{");
Rui Ueyama8ec77e62016-04-21 22:00:51 +0000754
Rui Ueyama025d59b2016-02-02 20:27:59 +0000755 while (!Error && !skip("}")) {
George Rimar481c2ce2016-02-23 07:47:54 +0000756 StringRef Tok = next();
757 if (Tok == "*") {
George Rimareea31142016-07-21 14:26:59 +0000758 auto *InCmd = new InputSectionDescription();
759 Cmd->Commands.emplace_back(InCmd);
Rui Ueyama8ec77e62016-04-21 22:00:51 +0000760 expect("(");
761 while (!Error && !skip(")"))
George Rimareea31142016-07-21 14:26:59 +0000762 InCmd->Patterns.push_back(next());
George Rimar481c2ce2016-02-23 07:47:54 +0000763 } else if (Tok == "KEEP") {
764 expect("(");
Rui Ueyama8ec77e62016-04-21 22:00:51 +0000765 expect("*");
766 expect("(");
George Rimareea31142016-07-21 14:26:59 +0000767 auto *InCmd = new InputSectionDescription();
768 Cmd->Commands.emplace_back(InCmd);
Rui Ueyama8ec77e62016-04-21 22:00:51 +0000769 while (!Error && !skip(")")) {
George Rimareea31142016-07-21 14:26:59 +0000770 Opt.KeptSections.push_back(peek());
771 InCmd->Patterns.push_back(next());
Rui Ueyama8ec77e62016-04-21 22:00:51 +0000772 }
George Rimar481c2ce2016-02-23 07:47:54 +0000773 expect(")");
774 } else {
George Rimar777f9632016-03-12 08:31:34 +0000775 setError("unknown command " + Tok);
George Rimar481c2ce2016-02-23 07:47:54 +0000776 }
Denis Protivensky8e3b38a2015-11-12 09:52:08 +0000777 }
George Rimar076fe152016-07-21 06:43:01 +0000778 Cmd->Phdrs = readOutputSectionPhdrs();
Rui Ueyama8ec77e62016-04-21 22:00:51 +0000779
George Rimare2ee72b2016-02-26 14:48:31 +0000780 StringRef Tok = peek();
781 if (Tok.startswith("=")) {
782 if (!Tok.startswith("=0x")) {
Rui Ueyama3ed2f062016-03-13 03:17:44 +0000783 setError("filler should be a hexadecimal value");
George Rimare2ee72b2016-02-26 14:48:31 +0000784 return;
785 }
Rui Ueyama3e808972016-02-28 05:09:11 +0000786 Tok = Tok.substr(3);
George Rimarf6c3cce2016-07-21 07:48:54 +0000787 Cmd->Filler = parseHex(Tok);
George Rimare2ee72b2016-02-26 14:48:31 +0000788 next();
789 }
Denis Protivensky8e3b38a2015-11-12 09:52:08 +0000790}
791
Eugene Levianteda81a12016-07-12 06:39:48 +0000792void ScriptParser::readSymbolAssignment(StringRef Name) {
793 expect("=");
794 std::vector<StringRef> Expr = readSectionsCommandExpr();
795 if (Expr.empty())
796 error("error in symbol assignment expression");
797 else
George Rimar076fe152016-07-21 06:43:01 +0000798 Opt.Commands.push_back(llvm::make_unique<SymbolAssignment>(Name, Expr));
Eugene Levianteda81a12016-07-12 06:39:48 +0000799}
800
801std::vector<StringRef> ScriptParser::readSectionsCommandExpr() {
802 std::vector<StringRef> Expr;
803 while (!Error) {
804 StringRef Tok = next();
805 if (Tok == ";")
806 break;
807 Expr.push_back(Tok);
808 }
809 return Expr;
810}
811
Eugene Leviantbbe38602016-07-19 09:25:43 +0000812std::vector<StringRef> ScriptParser::readOutputSectionPhdrs() {
813 std::vector<StringRef> Phdrs;
814 while (!Error && peek().startswith(":")) {
815 StringRef Tok = next();
816 Tok = (Tok.size() == 1) ? next() : Tok.substr(1);
817 if (Tok.empty()) {
818 setError("section header name is empty");
819 break;
820 }
Rui Ueyama047404f2016-07-20 19:36:36 +0000821 Phdrs.push_back(Tok);
Eugene Leviantbbe38602016-07-19 09:25:43 +0000822 }
823 return Phdrs;
824}
825
826unsigned ScriptParser::readPhdrType() {
Eugene Leviantbbe38602016-07-19 09:25:43 +0000827 StringRef Tok = next();
Rui Ueyamab0f6c592016-07-20 19:36:38 +0000828 unsigned Ret = StringSwitch<unsigned>(Tok)
829 .Case("PT_NULL", PT_NULL)
830 .Case("PT_LOAD", PT_LOAD)
831 .Case("PT_DYNAMIC", PT_DYNAMIC)
832 .Case("PT_INTERP", PT_INTERP)
833 .Case("PT_NOTE", PT_NOTE)
834 .Case("PT_SHLIB", PT_SHLIB)
835 .Case("PT_PHDR", PT_PHDR)
836 .Case("PT_TLS", PT_TLS)
837 .Case("PT_GNU_EH_FRAME", PT_GNU_EH_FRAME)
838 .Case("PT_GNU_STACK", PT_GNU_STACK)
839 .Case("PT_GNU_RELRO", PT_GNU_RELRO)
840 .Default(-1);
Eugene Leviantbbe38602016-07-19 09:25:43 +0000841
Rui Ueyamab0f6c592016-07-20 19:36:38 +0000842 if (Ret == (unsigned)-1) {
843 setError("invalid program header type: " + Tok);
844 return PT_NULL;
845 }
846 return Ret;
Eugene Leviantbbe38602016-07-19 09:25:43 +0000847}
848
Simon Atanasyan16b0cc92015-11-26 05:53:00 +0000849static bool isUnderSysroot(StringRef Path) {
850 if (Config->Sysroot == "")
851 return false;
852 for (; !Path.empty(); Path = sys::path::parent_path(Path))
853 if (sys::fs::equivalent(Config->Sysroot, Path))
854 return true;
855 return false;
856}
857
Rui Ueyama07320e42016-04-20 20:13:41 +0000858// Entry point.
859void elf::readLinkerScript(MemoryBufferRef MB) {
Simon Atanasyan16b0cc92015-11-26 05:53:00 +0000860 StringRef Path = MB.getBufferIdentifier();
Rui Ueyama07320e42016-04-20 20:13:41 +0000861 ScriptParser(MB.getBuffer(), isUnderSysroot(Path)).run();
Rui Ueyamaf7c5fbb2015-09-30 17:23:26 +0000862}
Rui Ueyama1ebc8ed2016-02-12 21:47:28 +0000863
Rui Ueyama07320e42016-04-20 20:13:41 +0000864template class elf::LinkerScript<ELF32LE>;
865template class elf::LinkerScript<ELF32BE>;
866template class elf::LinkerScript<ELF64LE>;
867template class elf::LinkerScript<ELF64BE>;