blob: 7fa953d3f364ff0c2f4d791c8038e9bfd4fe2f7b [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
Rui Ueyama9c1112d2016-04-23 00:04:03 +000050// This is an operator-precedence parser to parse and evaluate
51// a linker script expression. For each linker script arithmetic
52// expression (e.g. ". = . + 0x1000"), a new instance of ExprParser
53// is created and ran.
54namespace {
55class ExprParser : public ScriptParserBase {
56public:
57 ExprParser(std::vector<StringRef> &Tokens, uint64_t Dot)
58 : ScriptParserBase(Tokens), Dot(Dot) {}
59
60 uint64_t run();
61
62private:
63 uint64_t parsePrimary();
64 uint64_t parseTernary(uint64_t Cond);
65 uint64_t apply(StringRef Op, uint64_t L, uint64_t R);
66 uint64_t parseExpr1(uint64_t Lhs, int MinPrec);
67 uint64_t parseExpr();
68
69 uint64_t Dot;
70};
71}
72
Rui Ueyama960504b2016-04-19 18:58:11 +000073static int precedence(StringRef Op) {
74 return StringSwitch<int>(Op)
75 .Case("*", 4)
George Rimarab939062016-04-25 08:14:41 +000076 .Case("/", 4)
77 .Case("+", 3)
78 .Case("-", 3)
79 .Case("<", 2)
80 .Case(">", 2)
81 .Case(">=", 2)
82 .Case("<=", 2)
83 .Case("==", 2)
84 .Case("!=", 2)
Rui Ueyama960504b2016-04-19 18:58:11 +000085 .Case("&", 1)
86 .Default(-1);
87}
88
Rui Ueyama9c1112d2016-04-23 00:04:03 +000089static uint64_t evalExpr(std::vector<StringRef> &Tokens, uint64_t Dot) {
90 return ExprParser(Tokens, Dot).run();
Rui Ueyama960504b2016-04-19 18:58:11 +000091}
92
Rui Ueyama9c1112d2016-04-23 00:04:03 +000093uint64_t ExprParser::run() {
94 uint64_t V = parseExpr();
95 if (!atEOF() && !Error)
96 setError("stray token: " + peek());
97 return V;
Rui Ueyama60118112016-04-20 20:54:13 +000098}
99
Rui Ueyama960504b2016-04-19 18:58:11 +0000100// This is a part of the operator-precedence parser to evaluate
101// arithmetic expressions in SECTIONS command. This function evaluates an
Rui Ueyamae29a9752016-04-22 21:02:27 +0000102// integer literal, a parenthesized expression, the ALIGN function,
103// or the special variable ".".
Rui Ueyama9c1112d2016-04-23 00:04:03 +0000104uint64_t ExprParser::parsePrimary() {
105 StringRef Tok = next();
Rui Ueyama960504b2016-04-19 18:58:11 +0000106 if (Tok == ".")
107 return Dot;
108 if (Tok == "(") {
Rui Ueyama9c1112d2016-04-23 00:04:03 +0000109 uint64_t V = parseExpr();
110 expect(")");
Rui Ueyama960504b2016-04-19 18:58:11 +0000111 return V;
112 }
George Rimardffc1412016-04-22 11:40:53 +0000113 if (Tok == "ALIGN") {
Rui Ueyama9c1112d2016-04-23 00:04:03 +0000114 expect("(");
115 uint64_t V = parseExpr();
116 expect(")");
George Rimardffc1412016-04-22 11:40:53 +0000117 return alignTo(Dot, V);
118 }
Rui Ueyama5fa60982016-04-22 21:05:04 +0000119 uint64_t V = 0;
120 if (Tok.getAsInteger(0, V))
Rui Ueyama9c1112d2016-04-23 00:04:03 +0000121 setError("malformed number: " + Tok);
Rui Ueyama5fa60982016-04-22 21:05:04 +0000122 return V;
Rui Ueyama960504b2016-04-19 18:58:11 +0000123}
124
Rui Ueyama9c1112d2016-04-23 00:04:03 +0000125uint64_t ExprParser::parseTernary(uint64_t Cond) {
126 next();
127 uint64_t V = parseExpr();
128 expect(":");
129 uint64_t W = parseExpr();
George Rimarfba45c42016-04-22 11:28:54 +0000130 return Cond ? V : W;
131}
132
Rui Ueyama9c1112d2016-04-23 00:04:03 +0000133uint64_t ExprParser::apply(StringRef Op, uint64_t L, uint64_t R) {
Rui Ueyama960504b2016-04-19 18:58:11 +0000134 if (Op == "*")
135 return L * R;
136 if (Op == "/") {
137 if (R == 0) {
138 error("division by zero");
George Rimar652852c2016-04-16 10:10:32 +0000139 return 0;
140 }
Rui Ueyama960504b2016-04-19 18:58:11 +0000141 return L / R;
George Rimar652852c2016-04-16 10:10:32 +0000142 }
George Rimarab939062016-04-25 08:14:41 +0000143 if (Op == "+")
144 return L + R;
145 if (Op == "-")
146 return L - R;
147 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;
Rui Ueyama960504b2016-04-19 18:58:11 +0000159 if (Op == "&")
160 return L & R;
Rui Ueyama7a81d672016-04-19 19:04:03 +0000161 llvm_unreachable("invalid operator");
Rui Ueyama960504b2016-04-19 18:58:11 +0000162}
163
Rui Ueyama9c1112d2016-04-23 00:04:03 +0000164// This is a part of the operator-precedence parser.
165// This function assumes that the remaining token stream starts
166// with an operator.
167uint64_t ExprParser::parseExpr1(uint64_t Lhs, int MinPrec) {
168 while (!atEOF()) {
Rui Ueyama960504b2016-04-19 18:58:11 +0000169 // Read an operator and an expression.
Rui Ueyama9c1112d2016-04-23 00:04:03 +0000170 StringRef Op1 = peek();
George Rimarfba45c42016-04-22 11:28:54 +0000171 if (Op1 == "?")
Rui Ueyama9c1112d2016-04-23 00:04:03 +0000172 return parseTernary(Lhs);
Rui Ueyama960504b2016-04-19 18:58:11 +0000173 if (precedence(Op1) < MinPrec)
174 return Lhs;
Rui Ueyama9c1112d2016-04-23 00:04:03 +0000175 next();
176 uint64_t Rhs = parsePrimary();
Rui Ueyama960504b2016-04-19 18:58:11 +0000177
178 // Evaluate the remaining part of the expression first if the
179 // next operator has greater precedence than the previous one.
180 // For example, if we have read "+" and "3", and if the next
181 // operator is "*", then we'll evaluate 3 * ... part first.
Rui Ueyama9c1112d2016-04-23 00:04:03 +0000182 while (!atEOF()) {
183 StringRef Op2 = peek();
Rui Ueyama960504b2016-04-19 18:58:11 +0000184 if (precedence(Op2) <= precedence(Op1))
185 break;
Rui Ueyama9c1112d2016-04-23 00:04:03 +0000186 Rhs = parseExpr1(Rhs, precedence(Op2));
Rui Ueyama960504b2016-04-19 18:58:11 +0000187 }
188
189 Lhs = apply(Op1, Lhs, Rhs);
190 }
191 return Lhs;
192}
193
Rui Ueyama9c1112d2016-04-23 00:04:03 +0000194// Reads and evaluates an arithmetic expression.
195uint64_t ExprParser::parseExpr() { return parseExpr1(parsePrimary(), 0); }
George Rimar652852c2016-04-16 10:10:32 +0000196
Rui Ueyama1ebc8ed2016-02-12 21:47:28 +0000197template <class ELFT>
Rui Ueyama8ec77e62016-04-21 22:00:51 +0000198StringRef LinkerScript<ELFT>::getOutputSection(InputSectionBase<ELFT> *S) {
Rui Ueyama07320e42016-04-20 20:13:41 +0000199 for (SectionRule &R : Opt.Sections)
Rui Ueyama722830a2016-06-29 05:32:09 +0000200 if (globMatch(R.SectionPattern, S->getSectionName()))
Rui Ueyama8ec77e62016-04-21 22:00:51 +0000201 return R.Dest;
202 return "";
Rui Ueyama717677a2016-02-11 21:17:59 +0000203}
204
Rui Ueyama1ebc8ed2016-02-12 21:47:28 +0000205template <class ELFT>
Rui Ueyama07320e42016-04-20 20:13:41 +0000206bool LinkerScript<ELFT>::isDiscarded(InputSectionBase<ELFT> *S) {
Eugene Leviante63d81b2016-07-20 14:43:20 +0000207 return !S || !S->Live || getOutputSection(S) == "/DISCARD/";
Rui Ueyama717677a2016-02-11 21:17:59 +0000208}
209
Rui Ueyama07320e42016-04-20 20:13:41 +0000210template <class ELFT>
211bool LinkerScript<ELFT>::shouldKeep(InputSectionBase<ELFT> *S) {
Rui Ueyama8ec77e62016-04-21 22:00:51 +0000212 for (StringRef Pat : Opt.KeptSections)
Rui Ueyama722830a2016-06-29 05:32:09 +0000213 if (globMatch(Pat, S->getSectionName()))
Rui Ueyama8ec77e62016-04-21 22:00:51 +0000214 return true;
215 return false;
George Rimar481c2ce2016-02-23 07:47:54 +0000216}
217
George Rimar652852c2016-04-16 10:10:32 +0000218template <class ELFT>
Rui Ueyamaa7f78842016-07-20 17:19:03 +0000219std::vector<OutputSectionBase<ELFT> *>
Eugene Leviante63d81b2016-07-20 14:43:20 +0000220LinkerScript<ELFT>::createSections(OutputSectionFactory<ELFT> &Factory) {
Rui Ueyamaa7f78842016-07-20 17:19:03 +0000221 std::vector<OutputSectionBase<ELFT> *> Result;
222
Eugene Leviante63d81b2016-07-20 14:43:20 +0000223 // Add input section to output section. If there is no output section yet,
224 // then create it and add to output section list.
225 auto AddInputSec = [&](InputSectionBase<ELFT> *C, StringRef Name) {
226 OutputSectionBase<ELFT> *Sec;
227 bool IsNew;
228 std::tie(Sec, IsNew) = Factory.create(C, Name);
229 if (IsNew)
Rui Ueyamaa7f78842016-07-20 17:19:03 +0000230 Result.push_back(Sec);
Eugene Leviante63d81b2016-07-20 14:43:20 +0000231 Sec->addSection(C);
232 };
233
234 // Select input sections matching rule and add them to corresponding
235 // output section. Section rules are processed in order they're listed
236 // in script, so correct input section order is maintained by design.
237 for (SectionRule &R : Opt.Sections)
238 for (const std::unique_ptr<ObjectFile<ELFT>> &F :
239 Symtab<ELFT>::X->getObjectFiles())
240 for (InputSectionBase<ELFT> *S : F->getSections())
241 if (!isDiscarded(S) && !S->OutSec &&
242 globMatch(R.SectionPattern, S->getSectionName()))
243 // Add single input section to output section.
244 AddInputSec(S, R.Dest);
245
246 // Add all other input sections, which are not listed in script.
247 for (const std::unique_ptr<ObjectFile<ELFT>> &F :
248 Symtab<ELFT>::X->getObjectFiles())
249 for (InputSectionBase<ELFT> *S : F->getSections())
250 if (!isDiscarded(S)) {
251 if (!S->OutSec)
252 AddInputSec(S, getOutputSectionName(S));
253 } else
254 reportDiscarded(S, F);
255
256 return Result;
257}
258
259template <class ELFT>
Rui Ueyama07320e42016-04-20 20:13:41 +0000260void LinkerScript<ELFT>::assignAddresses(
George Rimardbbd8b12016-04-21 11:21:48 +0000261 ArrayRef<OutputSectionBase<ELFT> *> Sections) {
George Rimar652852c2016-04-16 10:10:32 +0000262 // Orphan sections are sections present in the input files which
Rui Ueyama7c18c282016-04-18 21:00:40 +0000263 // are not explicitly placed into the output file by the linker script.
264 // We place orphan sections at end of file.
265 // Other linkers places them using some heuristics as described in
George Rimar652852c2016-04-16 10:10:32 +0000266 // https://sourceware.org/binutils/docs/ld/Orphan-Sections.html#Orphan-Sections.
Rui Ueyama7c18c282016-04-18 21:00:40 +0000267 for (OutputSectionBase<ELFT> *Sec : Sections) {
George Rimar652852c2016-04-16 10:10:32 +0000268 StringRef Name = Sec->getName();
Rui Ueyamac3e2a4b2016-04-21 20:30:00 +0000269 if (getSectionIndex(Name) == INT_MAX)
George Rimar076fe152016-07-21 06:43:01 +0000270 Opt.Commands.push_back(llvm::make_unique<OutputSectionCommand>(Name));
George Rimar652852c2016-04-16 10:10:32 +0000271 }
George Rimar652852c2016-04-16 10:10:32 +0000272
Rui Ueyama7c18c282016-04-18 21:00:40 +0000273 // Assign addresses as instructed by linker script SECTIONS sub-commands.
Rui Ueyama52c4e172016-07-01 10:42:25 +0000274 Dot = Out<ELFT>::ElfHeader->getSize() + Out<ELFT>::ProgramHeaders->getSize();
Eugene Leviant467c4d52016-07-01 10:27:36 +0000275 uintX_t MinVA = std::numeric_limits<uintX_t>::max();
George Rimar652852c2016-04-16 10:10:32 +0000276 uintX_t ThreadBssOffset = 0;
George Rimar652852c2016-04-16 10:10:32 +0000277
George Rimar076fe152016-07-21 06:43:01 +0000278 for (const std::unique_ptr<BaseCommand> &Base : Opt.Commands) {
279 if (auto *Cmd = dyn_cast<SymbolAssignment>(Base.get())) {
280 uint64_t Val = evalExpr(Cmd->Expr, Dot);
281 if (Cmd->Name == ".") {
Rui Ueyama05ef4cf2016-07-15 04:19:37 +0000282
Rui Ueyama05ef4cf2016-07-15 04:19:37 +0000283 Dot = Val;
284 } else {
George Rimar076fe152016-07-21 06:43:01 +0000285 auto *D = cast<DefinedRegular<ELFT>>(Symtab<ELFT>::X->find(Cmd->Name));
Rui Ueyama05ef4cf2016-07-15 04:19:37 +0000286 D->Value = Val;
287 }
George Rimar652852c2016-04-16 10:10:32 +0000288 continue;
289 }
290
Dima Stepanovfb8978f2016-05-19 18:15:54 +0000291 // Find all the sections with required name. There can be more than
George Rimar6ad330a2016-07-19 07:39:07 +0000292 // one section with such name, if the alignment, flags or type
Dima Stepanovfb8978f2016-05-19 18:15:54 +0000293 // attribute differs.
George Rimar076fe152016-07-21 06:43:01 +0000294 auto *Cmd = cast<OutputSectionCommand>(Base.get());
Dima Stepanovfb8978f2016-05-19 18:15:54 +0000295 for (OutputSectionBase<ELFT> *Sec : Sections) {
George Rimar076fe152016-07-21 06:43:01 +0000296 if (Sec->getName() != Cmd->Name)
Dima Stepanovfb8978f2016-05-19 18:15:54 +0000297 continue;
George Rimar652852c2016-04-16 10:10:32 +0000298
Dima Stepanovfb8978f2016-05-19 18:15:54 +0000299 if ((Sec->getFlags() & SHF_TLS) && Sec->getType() == SHT_NOBITS) {
300 uintX_t TVA = Dot + ThreadBssOffset;
Rui Ueyama424b4082016-06-17 01:18:46 +0000301 TVA = alignTo(TVA, Sec->getAlignment());
Dima Stepanovfb8978f2016-05-19 18:15:54 +0000302 Sec->setVA(TVA);
303 ThreadBssOffset = TVA - Dot + Sec->getSize();
304 continue;
305 }
George Rimar652852c2016-04-16 10:10:32 +0000306
Dima Stepanovfb8978f2016-05-19 18:15:54 +0000307 if (Sec->getFlags() & SHF_ALLOC) {
Rui Ueyama424b4082016-06-17 01:18:46 +0000308 Dot = alignTo(Dot, Sec->getAlignment());
Dima Stepanovfb8978f2016-05-19 18:15:54 +0000309 Sec->setVA(Dot);
Rui Ueyama52c4e172016-07-01 10:42:25 +0000310 MinVA = std::min(MinVA, Dot);
Dima Stepanovfb8978f2016-05-19 18:15:54 +0000311 Dot += Sec->getSize();
312 continue;
313 }
George Rimar652852c2016-04-16 10:10:32 +0000314 }
315 }
Rui Ueyama52c4e172016-07-01 10:42:25 +0000316
Rafael Espindola64c32d62016-07-07 14:28:47 +0000317 // ELF and Program headers need to be right before the first section in
George Rimarb91e7112016-07-19 07:42:07 +0000318 // memory. Set their addresses accordingly.
Eugene Leviant467c4d52016-07-01 10:27:36 +0000319 MinVA = alignDown(MinVA - Out<ELFT>::ElfHeader->getSize() -
320 Out<ELFT>::ProgramHeaders->getSize(),
321 Target->PageSize);
322 Out<ELFT>::ElfHeader->setVA(MinVA);
323 Out<ELFT>::ProgramHeaders->setVA(Out<ELFT>::ElfHeader->getSize() + MinVA);
George Rimar652852c2016-04-16 10:10:32 +0000324}
325
Rui Ueyama07320e42016-04-20 20:13:41 +0000326template <class ELFT>
Rafael Espindola74df5c72016-07-19 12:33:46 +0000327std::vector<PhdrEntry<ELFT>>
Eugene Leviantbbe38602016-07-19 09:25:43 +0000328LinkerScript<ELFT>::createPhdrs(ArrayRef<OutputSectionBase<ELFT> *> Sections) {
329 int TlsNum = -1;
330 int NoteNum = -1;
331 int RelroNum = -1;
332 Phdr *Load = nullptr;
333 uintX_t Flags = PF_R;
334 std::vector<Phdr> Phdrs;
335
336 for (const PhdrsCommand &Cmd : Opt.PhdrsCommands) {
337 Phdrs.emplace_back(Cmd.Type, PF_R);
338 Phdr &Added = Phdrs.back();
339
340 if (Cmd.HasFilehdr)
Rui Ueyama18f084f2016-07-20 19:36:41 +0000341 Added.add(Out<ELFT>::ElfHeader);
Eugene Leviantbbe38602016-07-19 09:25:43 +0000342 if (Cmd.HasPhdrs)
Rui Ueyama18f084f2016-07-20 19:36:41 +0000343 Added.add(Out<ELFT>::ProgramHeaders);
Eugene Leviantbbe38602016-07-19 09:25:43 +0000344
345 switch (Cmd.Type) {
346 case PT_INTERP:
347 if (needsInterpSection<ELFT>())
Rui Ueyama18f084f2016-07-20 19:36:41 +0000348 Added.add(Out<ELFT>::Interp);
Eugene Leviantbbe38602016-07-19 09:25:43 +0000349 break;
350 case PT_DYNAMIC:
351 if (isOutputDynamic<ELFT>()) {
352 Added.H.p_flags = toPhdrFlags(Out<ELFT>::Dynamic->getFlags());
Rui Ueyama18f084f2016-07-20 19:36:41 +0000353 Added.add(Out<ELFT>::Dynamic);
Eugene Leviantbbe38602016-07-19 09:25:43 +0000354 }
355 break;
356 case PT_TLS:
357 TlsNum = Phdrs.size() - 1;
358 break;
359 case PT_NOTE:
360 NoteNum = Phdrs.size() - 1;
361 break;
362 case PT_GNU_RELRO:
363 RelroNum = Phdrs.size() - 1;
364 break;
365 case PT_GNU_EH_FRAME:
366 if (!Out<ELFT>::EhFrame->empty() && Out<ELFT>::EhFrameHdr) {
367 Added.H.p_flags = toPhdrFlags(Out<ELFT>::EhFrameHdr->getFlags());
Rui Ueyama18f084f2016-07-20 19:36:41 +0000368 Added.add(Out<ELFT>::EhFrameHdr);
Eugene Leviantbbe38602016-07-19 09:25:43 +0000369 }
370 break;
371 }
372 }
373
374 for (OutputSectionBase<ELFT> *Sec : Sections) {
375 if (!(Sec->getFlags() & SHF_ALLOC))
376 break;
377
378 if (TlsNum != -1 && (Sec->getFlags() & SHF_TLS))
Rui Ueyama18f084f2016-07-20 19:36:41 +0000379 Phdrs[TlsNum].add(Sec);
Eugene Leviantbbe38602016-07-19 09:25:43 +0000380
381 if (!needsPtLoad<ELFT>(Sec))
382 continue;
383
384 const std::vector<size_t> &PhdrIds =
385 getPhdrIndicesForSection(Sec->getName());
386 if (!PhdrIds.empty()) {
387 // Assign headers specified by linker script
388 for (size_t Id : PhdrIds) {
Rui Ueyama18f084f2016-07-20 19:36:41 +0000389 Phdrs[Id].add(Sec);
Eugene Leviantbbe38602016-07-19 09:25:43 +0000390 Phdrs[Id].H.p_flags |= toPhdrFlags(Sec->getFlags());
391 }
392 } else {
393 // If we have no load segment or flags've changed then we want new load
394 // segment.
395 uintX_t NewFlags = toPhdrFlags(Sec->getFlags());
396 if (Load == nullptr || Flags != NewFlags) {
397 Load = &*Phdrs.emplace(Phdrs.end(), PT_LOAD, NewFlags);
398 Flags = NewFlags;
399 }
Rui Ueyama18f084f2016-07-20 19:36:41 +0000400 Load->add(Sec);
Eugene Leviantbbe38602016-07-19 09:25:43 +0000401 }
402
403 if (RelroNum != -1 && isRelroSection(Sec))
Rui Ueyama18f084f2016-07-20 19:36:41 +0000404 Phdrs[RelroNum].add(Sec);
Eugene Leviantbbe38602016-07-19 09:25:43 +0000405 if (NoteNum != -1 && Sec->getType() == SHT_NOTE)
Rui Ueyama18f084f2016-07-20 19:36:41 +0000406 Phdrs[NoteNum].add(Sec);
Eugene Leviantbbe38602016-07-19 09:25:43 +0000407 }
408 return Phdrs;
409}
410
411template <class ELFT>
Rui Ueyama07320e42016-04-20 20:13:41 +0000412ArrayRef<uint8_t> LinkerScript<ELFT>::getFiller(StringRef Name) {
George Rimarf6c3cce2016-07-21 07:48:54 +0000413 for (const std::unique_ptr<BaseCommand> &Base : Opt.Commands)
414 if (auto *Cmd = dyn_cast<OutputSectionCommand>(Base.get()))
415 if (Cmd->Name == Name)
416 return Cmd->Filler;
417 return {};
George Rimare2ee72b2016-02-26 14:48:31 +0000418}
419
Rui Ueyamac3e2a4b2016-04-21 20:30:00 +0000420// Returns the index of the given section name in linker script
421// SECTIONS commands. Sections are laid out as the same order as they
422// were in the script. If a given name did not appear in the script,
423// it returns INT_MAX, so that it will be laid out at end of file.
George Rimar076fe152016-07-21 06:43:01 +0000424template <class ELFT> int LinkerScript<ELFT>::getSectionIndex(StringRef Name) {
George Rimar71b26e92016-04-21 10:22:02 +0000425 auto Begin = Opt.Commands.begin();
426 auto End = Opt.Commands.end();
George Rimar076fe152016-07-21 06:43:01 +0000427 auto I =
428 std::find_if(Begin, End, [&](const std::unique_ptr<BaseCommand> &Base) {
429 if (auto *Cmd = dyn_cast<OutputSectionCommand>(Base.get()))
430 if (Cmd->Name == Name)
431 return true;
432 return false;
433 });
Rui Ueyamac3e2a4b2016-04-21 20:30:00 +0000434 return I == End ? INT_MAX : (I - Begin);
George Rimar71b26e92016-04-21 10:22:02 +0000435}
436
437// A compartor to sort output sections. Returns -1 or 1 if
438// A or B are mentioned in linker script. Otherwise, returns 0.
Rui Ueyama07320e42016-04-20 20:13:41 +0000439template <class ELFT>
440int LinkerScript<ELFT>::compareSections(StringRef A, StringRef B) {
Rui Ueyamac3e2a4b2016-04-21 20:30:00 +0000441 int I = getSectionIndex(A);
442 int J = getSectionIndex(B);
443 if (I == INT_MAX && J == INT_MAX)
Rui Ueyama717677a2016-02-11 21:17:59 +0000444 return 0;
445 return I < J ? -1 : 1;
446}
447
George Rimar076fe152016-07-21 06:43:01 +0000448template <class ELFT> void LinkerScript<ELFT>::addScriptedSymbols() {
449 for (const std::unique_ptr<BaseCommand> &Base : Opt.Commands)
450 if (auto *Cmd = dyn_cast<SymbolAssignment>(Base.get()))
451 if (Cmd->Name != "." && Symtab<ELFT>::X->find(Cmd->Name) == nullptr)
452 Symtab<ELFT>::X->addAbsolute(Cmd->Name, STV_DEFAULT);
Eugene Levianteda81a12016-07-12 06:39:48 +0000453}
454
Eugene Leviantbbe38602016-07-19 09:25:43 +0000455template <class ELFT> bool LinkerScript<ELFT>::hasPhdrsCommands() {
456 return !Opt.PhdrsCommands.empty();
457}
458
459// Returns indices of ELF headers containing specific section, identified
460// by Name. Each index is a zero based number of ELF header listed within
461// PHDRS {} script block.
462template <class ELFT>
463std::vector<size_t>
464LinkerScript<ELFT>::getPhdrIndicesForSection(StringRef Name) {
George Rimar076fe152016-07-21 06:43:01 +0000465 for (const std::unique_ptr<BaseCommand> &Base : Opt.Commands) {
466 auto *Cmd = dyn_cast<OutputSectionCommand>(Base.get());
467 if (!Cmd || Cmd->Name != Name)
George Rimar31d842f2016-07-20 16:43:03 +0000468 continue;
469
470 std::vector<size_t> Indices;
George Rimar076fe152016-07-21 06:43:01 +0000471 for (StringRef PhdrName : Cmd->Phdrs) {
George Rimar31d842f2016-07-20 16:43:03 +0000472 auto ItPhdr =
473 std::find_if(Opt.PhdrsCommands.rbegin(), Opt.PhdrsCommands.rend(),
George Rimar076fe152016-07-21 06:43:01 +0000474 [&](PhdrsCommand &P) { return P.Name == PhdrName; });
Eugene Leviantbbe38602016-07-19 09:25:43 +0000475 if (ItPhdr == Opt.PhdrsCommands.rend())
476 error("section header '" + PhdrName + "' is not listed in PHDRS");
477 else
478 Indices.push_back(std::distance(ItPhdr, Opt.PhdrsCommands.rend()) - 1);
479 }
George Rimar31d842f2016-07-20 16:43:03 +0000480 return Indices;
Eugene Leviantbbe38602016-07-19 09:25:43 +0000481 }
George Rimar31d842f2016-07-20 16:43:03 +0000482 return {};
Eugene Leviantbbe38602016-07-19 09:25:43 +0000483}
484
Rui Ueyama07320e42016-04-20 20:13:41 +0000485class elf::ScriptParser : public ScriptParserBase {
George Rimarc3794e52016-02-24 09:21:47 +0000486 typedef void (ScriptParser::*Handler)();
487
Rui Ueyamaf7c5fbb2015-09-30 17:23:26 +0000488public:
Rui Ueyama07320e42016-04-20 20:13:41 +0000489 ScriptParser(StringRef S, bool B) : ScriptParserBase(S), IsUnderSysroot(B) {}
George Rimarf23b2322016-02-19 10:45:45 +0000490
Rui Ueyama4a465392016-04-22 22:59:24 +0000491 void run();
Rui Ueyamaf7c5fbb2015-09-30 17:23:26 +0000492
493private:
Rui Ueyama52a15092015-10-11 03:28:42 +0000494 void addFile(StringRef Path);
495
Rui Ueyamaf7c5fbb2015-09-30 17:23:26 +0000496 void readAsNeeded();
Denis Protivensky90c50992015-10-08 06:48:38 +0000497 void readEntry();
George Rimar83f406c2015-10-19 17:35:12 +0000498 void readExtern();
Rui Ueyamaf7c5fbb2015-09-30 17:23:26 +0000499 void readGroup();
Rui Ueyama31aa1f82015-10-11 01:31:55 +0000500 void readInclude();
George Rimarc3794e52016-02-24 09:21:47 +0000501 void readNothing() {}
Rui Ueyamaee592822015-10-07 00:25:09 +0000502 void readOutput();
Davide Italiano9159ce92015-10-12 21:50:08 +0000503 void readOutputArch();
Rui Ueyamaf7c5fbb2015-09-30 17:23:26 +0000504 void readOutputFormat();
Eugene Leviantbbe38602016-07-19 09:25:43 +0000505 void readPhdrs();
Davide Italiano68a39a62015-10-08 17:51:41 +0000506 void readSearchDir();
Denis Protivensky8e3b38a2015-11-12 09:52:08 +0000507 void readSections();
508
George Rimar652852c2016-04-16 10:10:32 +0000509 void readLocationCounterValue();
Eugene Levianteda81a12016-07-12 06:39:48 +0000510 void readOutputSectionDescription(StringRef OutSec);
Eugene Leviantbbe38602016-07-19 09:25:43 +0000511 std::vector<StringRef> readOutputSectionPhdrs();
512 unsigned readPhdrType();
Eugene Levianteda81a12016-07-12 06:39:48 +0000513 void readSymbolAssignment(StringRef Name);
514 std::vector<StringRef> readSectionsCommandExpr();
Rui Ueyamaf7c5fbb2015-09-30 17:23:26 +0000515
George Rimarc3794e52016-02-24 09:21:47 +0000516 const static StringMap<Handler> Cmd;
Rui Ueyama07320e42016-04-20 20:13:41 +0000517 ScriptConfiguration &Opt = *ScriptConfig;
518 StringSaver Saver = {ScriptConfig->Alloc};
Simon Atanasyan16b0cc92015-11-26 05:53:00 +0000519 bool IsUnderSysroot;
Rui Ueyamaf7c5fbb2015-09-30 17:23:26 +0000520};
Rui Ueyamaf7c5fbb2015-09-30 17:23:26 +0000521
Rafael Espindolae0df00b2016-02-28 00:25:54 +0000522const StringMap<elf::ScriptParser::Handler> elf::ScriptParser::Cmd = {
George Rimarc3794e52016-02-24 09:21:47 +0000523 {"ENTRY", &ScriptParser::readEntry},
524 {"EXTERN", &ScriptParser::readExtern},
525 {"GROUP", &ScriptParser::readGroup},
526 {"INCLUDE", &ScriptParser::readInclude},
527 {"INPUT", &ScriptParser::readGroup},
528 {"OUTPUT", &ScriptParser::readOutput},
529 {"OUTPUT_ARCH", &ScriptParser::readOutputArch},
530 {"OUTPUT_FORMAT", &ScriptParser::readOutputFormat},
Eugene Leviantbbe38602016-07-19 09:25:43 +0000531 {"PHDRS", &ScriptParser::readPhdrs},
George Rimarc3794e52016-02-24 09:21:47 +0000532 {"SEARCH_DIR", &ScriptParser::readSearchDir},
533 {"SECTIONS", &ScriptParser::readSections},
534 {";", &ScriptParser::readNothing}};
535
Rui Ueyama717677a2016-02-11 21:17:59 +0000536void ScriptParser::run() {
Rui Ueyamaf7c5fbb2015-09-30 17:23:26 +0000537 while (!atEOF()) {
538 StringRef Tok = next();
George Rimarc3794e52016-02-24 09:21:47 +0000539 if (Handler Fn = Cmd.lookup(Tok))
540 (this->*Fn)();
541 else
George Rimar57610422016-03-11 14:43:02 +0000542 setError("unknown directive: " + Tok);
Rui Ueyamaf7c5fbb2015-09-30 17:23:26 +0000543 }
544}
545
Rui Ueyama717677a2016-02-11 21:17:59 +0000546void ScriptParser::addFile(StringRef S) {
Simon Atanasyan16b0cc92015-11-26 05:53:00 +0000547 if (IsUnderSysroot && S.startswith("/")) {
548 SmallString<128> Path;
549 (Config->Sysroot + S).toStringRef(Path);
550 if (sys::fs::exists(Path)) {
551 Driver->addFile(Saver.save(Path.str()));
552 return;
553 }
554 }
555
Rui Ueyamaf03f3cc2015-10-13 00:09:21 +0000556 if (sys::path::is_absolute(S)) {
Rui Ueyama52a15092015-10-11 03:28:42 +0000557 Driver->addFile(S);
558 } else if (S.startswith("=")) {
559 if (Config->Sysroot.empty())
560 Driver->addFile(S.substr(1));
561 else
562 Driver->addFile(Saver.save(Config->Sysroot + "/" + S.substr(1)));
563 } else if (S.startswith("-l")) {
Rui Ueyama21eecb42016-02-02 21:13:09 +0000564 Driver->addLibrary(S.substr(2));
Simon Atanasyana1b8fc32015-11-26 20:23:46 +0000565 } else if (sys::fs::exists(S)) {
566 Driver->addFile(S);
Rui Ueyama52a15092015-10-11 03:28:42 +0000567 } else {
568 std::string Path = findFromSearchPaths(S);
569 if (Path.empty())
George Rimar777f9632016-03-12 08:31:34 +0000570 setError("unable to find " + S);
Rui Ueyama025d59b2016-02-02 20:27:59 +0000571 else
572 Driver->addFile(Saver.save(Path));
Rui Ueyama52a15092015-10-11 03:28:42 +0000573 }
574}
575
Rui Ueyama717677a2016-02-11 21:17:59 +0000576void ScriptParser::readAsNeeded() {
Rui Ueyamaf7c5fbb2015-09-30 17:23:26 +0000577 expect("(");
Rui Ueyama35da9b62015-10-11 20:59:12 +0000578 bool Orig = Config->AsNeeded;
579 Config->AsNeeded = true;
Rui Ueyama025d59b2016-02-02 20:27:59 +0000580 while (!Error) {
Rui Ueyamaf7c5fbb2015-09-30 17:23:26 +0000581 StringRef Tok = next();
582 if (Tok == ")")
Rui Ueyama35da9b62015-10-11 20:59:12 +0000583 break;
Rui Ueyama52a15092015-10-11 03:28:42 +0000584 addFile(Tok);
Rui Ueyamaf7c5fbb2015-09-30 17:23:26 +0000585 }
Rui Ueyama35da9b62015-10-11 20:59:12 +0000586 Config->AsNeeded = Orig;
Rui Ueyamaf7c5fbb2015-09-30 17:23:26 +0000587}
588
Rui Ueyama717677a2016-02-11 21:17:59 +0000589void ScriptParser::readEntry() {
Denis Protivensky90c50992015-10-08 06:48:38 +0000590 // -e <symbol> takes predecence over ENTRY(<symbol>).
591 expect("(");
592 StringRef Tok = next();
593 if (Config->Entry.empty())
594 Config->Entry = Tok;
595 expect(")");
596}
597
Rui Ueyama717677a2016-02-11 21:17:59 +0000598void ScriptParser::readExtern() {
George Rimar83f406c2015-10-19 17:35:12 +0000599 expect("(");
Rui Ueyama025d59b2016-02-02 20:27:59 +0000600 while (!Error) {
George Rimar83f406c2015-10-19 17:35:12 +0000601 StringRef Tok = next();
602 if (Tok == ")")
603 return;
604 Config->Undefined.push_back(Tok);
605 }
606}
607
Rui Ueyama717677a2016-02-11 21:17:59 +0000608void ScriptParser::readGroup() {
Rui Ueyamaf7c5fbb2015-09-30 17:23:26 +0000609 expect("(");
Rui Ueyama025d59b2016-02-02 20:27:59 +0000610 while (!Error) {
Rui Ueyamaf7c5fbb2015-09-30 17:23:26 +0000611 StringRef Tok = next();
612 if (Tok == ")")
613 return;
614 if (Tok == "AS_NEEDED") {
615 readAsNeeded();
616 continue;
617 }
Rui Ueyama52a15092015-10-11 03:28:42 +0000618 addFile(Tok);
Rui Ueyamaf7c5fbb2015-09-30 17:23:26 +0000619 }
620}
621
Rui Ueyama717677a2016-02-11 21:17:59 +0000622void ScriptParser::readInclude() {
Rui Ueyama31aa1f82015-10-11 01:31:55 +0000623 StringRef Tok = next();
624 auto MBOrErr = MemoryBuffer::getFile(Tok);
Rui Ueyama025d59b2016-02-02 20:27:59 +0000625 if (!MBOrErr) {
George Rimar57610422016-03-11 14:43:02 +0000626 setError("cannot open " + Tok);
Rui Ueyama025d59b2016-02-02 20:27:59 +0000627 return;
628 }
Rui Ueyama31aa1f82015-10-11 01:31:55 +0000629 std::unique_ptr<MemoryBuffer> &MB = *MBOrErr;
Rui Ueyamaa47ee682015-10-11 01:53:04 +0000630 StringRef S = Saver.save(MB->getMemBufferRef().getBuffer());
631 std::vector<StringRef> V = tokenize(S);
Rui Ueyama31aa1f82015-10-11 01:31:55 +0000632 Tokens.insert(Tokens.begin() + Pos, V.begin(), V.end());
Rui Ueyama31aa1f82015-10-11 01:31:55 +0000633}
634
Rui Ueyama717677a2016-02-11 21:17:59 +0000635void ScriptParser::readOutput() {
Rui Ueyamaee592822015-10-07 00:25:09 +0000636 // -o <file> takes predecence over OUTPUT(<file>).
637 expect("(");
638 StringRef Tok = next();
639 if (Config->OutputFile.empty())
640 Config->OutputFile = Tok;
641 expect(")");
642}
643
Rui Ueyama717677a2016-02-11 21:17:59 +0000644void ScriptParser::readOutputArch() {
Davide Italiano9159ce92015-10-12 21:50:08 +0000645 // Error checking only for now.
646 expect("(");
647 next();
648 expect(")");
649}
650
Rui Ueyama717677a2016-02-11 21:17:59 +0000651void ScriptParser::readOutputFormat() {
Rui Ueyamaf7c5fbb2015-09-30 17:23:26 +0000652 // Error checking only for now.
653 expect("(");
654 next();
Davide Italiano6836c612015-10-12 21:08:41 +0000655 StringRef Tok = next();
656 if (Tok == ")")
657 return;
Rui Ueyama025d59b2016-02-02 20:27:59 +0000658 if (Tok != ",") {
George Rimar57610422016-03-11 14:43:02 +0000659 setError("unexpected token: " + Tok);
Rui Ueyama025d59b2016-02-02 20:27:59 +0000660 return;
661 }
Davide Italiano6836c612015-10-12 21:08:41 +0000662 next();
663 expect(",");
664 next();
Rui Ueyamaf7c5fbb2015-09-30 17:23:26 +0000665 expect(")");
666}
667
Eugene Leviantbbe38602016-07-19 09:25:43 +0000668void ScriptParser::readPhdrs() {
669 expect("{");
670 while (!Error && !skip("}")) {
671 StringRef Tok = next();
672 Opt.PhdrsCommands.push_back({Tok, PT_NULL, false, false});
673 PhdrsCommand &PhdrCmd = Opt.PhdrsCommands.back();
674
675 PhdrCmd.Type = readPhdrType();
676 do {
677 Tok = next();
678 if (Tok == ";")
679 break;
680 if (Tok == "FILEHDR")
681 PhdrCmd.HasFilehdr = true;
682 else if (Tok == "PHDRS")
683 PhdrCmd.HasPhdrs = true;
684 else
685 setError("unexpected header attribute: " + Tok);
686 } while (!Error);
687 }
688}
689
Rui Ueyama717677a2016-02-11 21:17:59 +0000690void ScriptParser::readSearchDir() {
Davide Italiano68a39a62015-10-08 17:51:41 +0000691 expect("(");
Rafael Espindola06501922016-03-08 17:13:12 +0000692 Config->SearchPaths.push_back(next());
Davide Italiano68a39a62015-10-08 17:51:41 +0000693 expect(")");
694}
695
Rui Ueyama717677a2016-02-11 21:17:59 +0000696void ScriptParser::readSections() {
Rui Ueyama07320e42016-04-20 20:13:41 +0000697 Opt.DoLayout = true;
Denis Protivensky8e3b38a2015-11-12 09:52:08 +0000698 expect("{");
George Rimar652852c2016-04-16 10:10:32 +0000699 while (!Error && !skip("}")) {
700 StringRef Tok = peek();
Eugene Levianteda81a12016-07-12 06:39:48 +0000701 if (Tok == ".") {
George Rimar652852c2016-04-16 10:10:32 +0000702 readLocationCounterValue();
Eugene Levianteda81a12016-07-12 06:39:48 +0000703 continue;
704 }
705 next();
706 if (peek() == "=")
707 readSymbolAssignment(Tok);
George Rimar652852c2016-04-16 10:10:32 +0000708 else
Eugene Levianteda81a12016-07-12 06:39:48 +0000709 readOutputSectionDescription(Tok);
George Rimar652852c2016-04-16 10:10:32 +0000710 }
Denis Protivensky8e3b38a2015-11-12 09:52:08 +0000711}
712
George Rimar652852c2016-04-16 10:10:32 +0000713void ScriptParser::readLocationCounterValue() {
714 expect(".");
715 expect("=");
Eugene Levianteda81a12016-07-12 06:39:48 +0000716 std::vector<StringRef> Expr = readSectionsCommandExpr();
717 if (Expr.empty())
George Rimar652852c2016-04-16 10:10:32 +0000718 error("error in location counter expression");
Eugene Levianteda81a12016-07-12 06:39:48 +0000719 else
George Rimar076fe152016-07-21 06:43:01 +0000720 Opt.Commands.push_back(llvm::make_unique<SymbolAssignment>(".", Expr));
George Rimar652852c2016-04-16 10:10:32 +0000721}
722
Eugene Levianteda81a12016-07-12 06:39:48 +0000723void ScriptParser::readOutputSectionDescription(StringRef OutSec) {
George Rimar076fe152016-07-21 06:43:01 +0000724 OutputSectionCommand *Cmd = new OutputSectionCommand(OutSec);
725 Opt.Commands.emplace_back(Cmd);
Denis Protivensky8e3b38a2015-11-12 09:52:08 +0000726 expect(":");
727 expect("{");
Rui Ueyama8ec77e62016-04-21 22:00:51 +0000728
Rui Ueyama025d59b2016-02-02 20:27:59 +0000729 while (!Error && !skip("}")) {
George Rimar481c2ce2016-02-23 07:47:54 +0000730 StringRef Tok = next();
731 if (Tok == "*") {
Rui Ueyama8ec77e62016-04-21 22:00:51 +0000732 expect("(");
733 while (!Error && !skip(")"))
734 Opt.Sections.emplace_back(OutSec, next());
George Rimar481c2ce2016-02-23 07:47:54 +0000735 } else if (Tok == "KEEP") {
736 expect("(");
Rui Ueyama8ec77e62016-04-21 22:00:51 +0000737 expect("*");
738 expect("(");
739 while (!Error && !skip(")")) {
740 StringRef Sec = next();
741 Opt.Sections.emplace_back(OutSec, Sec);
742 Opt.KeptSections.push_back(Sec);
743 }
George Rimar481c2ce2016-02-23 07:47:54 +0000744 expect(")");
745 } else {
George Rimar777f9632016-03-12 08:31:34 +0000746 setError("unknown command " + Tok);
George Rimar481c2ce2016-02-23 07:47:54 +0000747 }
Denis Protivensky8e3b38a2015-11-12 09:52:08 +0000748 }
George Rimar076fe152016-07-21 06:43:01 +0000749 Cmd->Phdrs = readOutputSectionPhdrs();
Rui Ueyama8ec77e62016-04-21 22:00:51 +0000750
George Rimare2ee72b2016-02-26 14:48:31 +0000751 StringRef Tok = peek();
752 if (Tok.startswith("=")) {
753 if (!Tok.startswith("=0x")) {
Rui Ueyama3ed2f062016-03-13 03:17:44 +0000754 setError("filler should be a hexadecimal value");
George Rimare2ee72b2016-02-26 14:48:31 +0000755 return;
756 }
Rui Ueyama3e808972016-02-28 05:09:11 +0000757 Tok = Tok.substr(3);
George Rimarf6c3cce2016-07-21 07:48:54 +0000758 Cmd->Filler = parseHex(Tok);
George Rimare2ee72b2016-02-26 14:48:31 +0000759 next();
760 }
Denis Protivensky8e3b38a2015-11-12 09:52:08 +0000761}
762
Eugene Levianteda81a12016-07-12 06:39:48 +0000763void ScriptParser::readSymbolAssignment(StringRef Name) {
764 expect("=");
765 std::vector<StringRef> Expr = readSectionsCommandExpr();
766 if (Expr.empty())
767 error("error in symbol assignment expression");
768 else
George Rimar076fe152016-07-21 06:43:01 +0000769 Opt.Commands.push_back(llvm::make_unique<SymbolAssignment>(Name, Expr));
Eugene Levianteda81a12016-07-12 06:39:48 +0000770}
771
772std::vector<StringRef> ScriptParser::readSectionsCommandExpr() {
773 std::vector<StringRef> Expr;
774 while (!Error) {
775 StringRef Tok = next();
776 if (Tok == ";")
777 break;
778 Expr.push_back(Tok);
779 }
780 return Expr;
781}
782
Eugene Leviantbbe38602016-07-19 09:25:43 +0000783std::vector<StringRef> ScriptParser::readOutputSectionPhdrs() {
784 std::vector<StringRef> Phdrs;
785 while (!Error && peek().startswith(":")) {
786 StringRef Tok = next();
787 Tok = (Tok.size() == 1) ? next() : Tok.substr(1);
788 if (Tok.empty()) {
789 setError("section header name is empty");
790 break;
791 }
Rui Ueyama047404f2016-07-20 19:36:36 +0000792 Phdrs.push_back(Tok);
Eugene Leviantbbe38602016-07-19 09:25:43 +0000793 }
794 return Phdrs;
795}
796
797unsigned ScriptParser::readPhdrType() {
Eugene Leviantbbe38602016-07-19 09:25:43 +0000798 StringRef Tok = next();
Rui Ueyamab0f6c592016-07-20 19:36:38 +0000799 unsigned Ret = StringSwitch<unsigned>(Tok)
800 .Case("PT_NULL", PT_NULL)
801 .Case("PT_LOAD", PT_LOAD)
802 .Case("PT_DYNAMIC", PT_DYNAMIC)
803 .Case("PT_INTERP", PT_INTERP)
804 .Case("PT_NOTE", PT_NOTE)
805 .Case("PT_SHLIB", PT_SHLIB)
806 .Case("PT_PHDR", PT_PHDR)
807 .Case("PT_TLS", PT_TLS)
808 .Case("PT_GNU_EH_FRAME", PT_GNU_EH_FRAME)
809 .Case("PT_GNU_STACK", PT_GNU_STACK)
810 .Case("PT_GNU_RELRO", PT_GNU_RELRO)
811 .Default(-1);
Eugene Leviantbbe38602016-07-19 09:25:43 +0000812
Rui Ueyamab0f6c592016-07-20 19:36:38 +0000813 if (Ret == (unsigned)-1) {
814 setError("invalid program header type: " + Tok);
815 return PT_NULL;
816 }
817 return Ret;
Eugene Leviantbbe38602016-07-19 09:25:43 +0000818}
819
Simon Atanasyan16b0cc92015-11-26 05:53:00 +0000820static bool isUnderSysroot(StringRef Path) {
821 if (Config->Sysroot == "")
822 return false;
823 for (; !Path.empty(); Path = sys::path::parent_path(Path))
824 if (sys::fs::equivalent(Config->Sysroot, Path))
825 return true;
826 return false;
827}
828
Rui Ueyama07320e42016-04-20 20:13:41 +0000829// Entry point.
830void elf::readLinkerScript(MemoryBufferRef MB) {
Simon Atanasyan16b0cc92015-11-26 05:53:00 +0000831 StringRef Path = MB.getBufferIdentifier();
Rui Ueyama07320e42016-04-20 20:13:41 +0000832 ScriptParser(MB.getBuffer(), isUnderSysroot(Path)).run();
Rui Ueyamaf7c5fbb2015-09-30 17:23:26 +0000833}
Rui Ueyama1ebc8ed2016-02-12 21:47:28 +0000834
Rui Ueyama07320e42016-04-20 20:13:41 +0000835template class elf::LinkerScript<ELF32LE>;
836template class elf::LinkerScript<ELF32BE>;
837template class elf::LinkerScript<ELF64LE>;
838template class elf::LinkerScript<ELF64BE>;