Rui Ueyama | f7c5fbb | 2015-09-30 17:23:26 +0000 | [diff] [blame] | 1 | //===- 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 Ueyama | 34f2924 | 2015-10-13 19:51:57 +0000 | [diff] [blame] | 12 | // Results are written to Driver or Config object. |
Rui Ueyama | f7c5fbb | 2015-09-30 17:23:26 +0000 | [diff] [blame] | 13 | // |
| 14 | //===----------------------------------------------------------------------===// |
| 15 | |
Rui Ueyama | 717677a | 2016-02-11 21:17:59 +0000 | [diff] [blame] | 16 | #include "LinkerScript.h" |
Rui Ueyama | f7c5fbb | 2015-09-30 17:23:26 +0000 | [diff] [blame] | 17 | #include "Config.h" |
| 18 | #include "Driver.h" |
Rui Ueyama | 1ebc8ed | 2016-02-12 21:47:28 +0000 | [diff] [blame] | 19 | #include "InputSection.h" |
George Rimar | 652852c | 2016-04-16 10:10:32 +0000 | [diff] [blame] | 20 | #include "OutputSections.h" |
Adhemerval Zanella | e77b5bf | 2016-04-06 20:59:11 +0000 | [diff] [blame] | 21 | #include "ScriptParser.h" |
Rui Ueyama | f7c5fbb | 2015-09-30 17:23:26 +0000 | [diff] [blame] | 22 | #include "SymbolTable.h" |
Rui Ueyama | 960504b | 2016-04-19 18:58:11 +0000 | [diff] [blame] | 23 | #include "llvm/ADT/StringSwitch.h" |
George Rimar | 652852c | 2016-04-16 10:10:32 +0000 | [diff] [blame] | 24 | #include "llvm/Support/ELF.h" |
Rui Ueyama | f7c5fbb | 2015-09-30 17:23:26 +0000 | [diff] [blame] | 25 | #include "llvm/Support/FileSystem.h" |
| 26 | #include "llvm/Support/MemoryBuffer.h" |
Rui Ueyama | f03f3cc | 2015-10-13 00:09:21 +0000 | [diff] [blame] | 27 | #include "llvm/Support/Path.h" |
Rui Ueyama | a47ee68 | 2015-10-11 01:53:04 +0000 | [diff] [blame] | 28 | #include "llvm/Support/StringSaver.h" |
Rui Ueyama | f7c5fbb | 2015-09-30 17:23:26 +0000 | [diff] [blame] | 29 | |
| 30 | using namespace llvm; |
George Rimar | 652852c | 2016-04-16 10:10:32 +0000 | [diff] [blame] | 31 | using namespace llvm::ELF; |
Rui Ueyama | 1ebc8ed | 2016-02-12 21:47:28 +0000 | [diff] [blame] | 32 | using namespace llvm::object; |
Rui Ueyama | f7c5fbb | 2015-09-30 17:23:26 +0000 | [diff] [blame] | 33 | using namespace lld; |
Rafael Espindola | e0df00b | 2016-02-28 00:25:54 +0000 | [diff] [blame] | 34 | using namespace lld::elf; |
Rui Ueyama | f7c5fbb | 2015-09-30 17:23:26 +0000 | [diff] [blame] | 35 | |
Rui Ueyama | 07320e4 | 2016-04-20 20:13:41 +0000 | [diff] [blame] | 36 | ScriptConfiguration *elf::ScriptConfig; |
Rui Ueyama | 717677a | 2016-02-11 21:17:59 +0000 | [diff] [blame] | 37 | |
Rui Ueyama | 8ec77e6 | 2016-04-21 22:00:51 +0000 | [diff] [blame] | 38 | static bool matchStr(StringRef S, StringRef T); |
| 39 | |
George Rimar | 652852c | 2016-04-16 10:10:32 +0000 | [diff] [blame] | 40 | static uint64_t getInteger(StringRef S) { |
| 41 | uint64_t V; |
| 42 | if (S.getAsInteger(0, V)) { |
| 43 | error("malformed number: " + S); |
| 44 | return 0; |
| 45 | } |
| 46 | return V; |
| 47 | } |
| 48 | |
Rui Ueyama | 960504b | 2016-04-19 18:58:11 +0000 | [diff] [blame] | 49 | static int precedence(StringRef Op) { |
| 50 | return StringSwitch<int>(Op) |
| 51 | .Case("*", 4) |
| 52 | .Case("/", 3) |
| 53 | .Case("+", 2) |
| 54 | .Case("-", 2) |
| 55 | .Case("&", 1) |
| 56 | .Default(-1); |
| 57 | } |
| 58 | |
| 59 | static StringRef next(ArrayRef<StringRef> &Tokens) { |
| 60 | if (Tokens.empty()) { |
| 61 | error("no next token"); |
| 62 | return ""; |
| 63 | } |
| 64 | StringRef Tok = Tokens.front(); |
| 65 | Tokens = Tokens.slice(1); |
| 66 | return Tok; |
| 67 | } |
| 68 | |
Rui Ueyama | 6011811 | 2016-04-20 20:54:13 +0000 | [diff] [blame] | 69 | static bool expect(ArrayRef<StringRef> &Tokens, StringRef S) { |
| 70 | if (Tokens.empty()) { |
| 71 | error(S + " expected"); |
| 72 | return false; |
| 73 | } |
| 74 | StringRef Tok = Tokens.front(); |
| 75 | if (Tok != S) { |
| 76 | error(S + " expected, but got " + Tok); |
| 77 | return false; |
| 78 | } |
| 79 | Tokens = Tokens.slice(1); |
| 80 | return true; |
| 81 | } |
| 82 | |
Rui Ueyama | 960504b | 2016-04-19 18:58:11 +0000 | [diff] [blame] | 83 | // This is a part of the operator-precedence parser to evaluate |
| 84 | // arithmetic expressions in SECTIONS command. This function evaluates an |
| 85 | // integer literal, a parenthesized expression or the special variable ".". |
Rui Ueyama | c998a8c | 2016-04-22 00:03:13 +0000 | [diff] [blame] | 86 | template <class ELFT> |
| 87 | uint64_t LinkerScript<ELFT>::parsePrimary(ArrayRef<StringRef> &Tokens) { |
Rui Ueyama | 960504b | 2016-04-19 18:58:11 +0000 | [diff] [blame] | 88 | StringRef Tok = next(Tokens); |
| 89 | if (Tok == ".") |
| 90 | return Dot; |
| 91 | if (Tok == "(") { |
Rui Ueyama | c998a8c | 2016-04-22 00:03:13 +0000 | [diff] [blame] | 92 | uint64_t V = parseExpr(Tokens); |
Rui Ueyama | 6011811 | 2016-04-20 20:54:13 +0000 | [diff] [blame] | 93 | if (!expect(Tokens, ")")) |
| 94 | return 0; |
Rui Ueyama | 960504b | 2016-04-19 18:58:11 +0000 | [diff] [blame] | 95 | return V; |
| 96 | } |
| 97 | return getInteger(Tok); |
| 98 | } |
| 99 | |
George Rimar | fba45c4 | 2016-04-22 11:28:54 +0000 | [diff] [blame] | 100 | template <class ELFT> |
| 101 | uint64_t LinkerScript<ELFT>::parseTernary(ArrayRef<StringRef> &Tokens, |
| 102 | uint64_t Cond) { |
| 103 | next(Tokens); |
| 104 | uint64_t V = parseExpr(Tokens); |
| 105 | if (!expect(Tokens, ":")) |
| 106 | return 0; |
| 107 | uint64_t W = parseExpr(Tokens); |
| 108 | return Cond ? V : W; |
| 109 | } |
| 110 | |
Rui Ueyama | 960504b | 2016-04-19 18:58:11 +0000 | [diff] [blame] | 111 | static uint64_t apply(StringRef Op, uint64_t L, uint64_t R) { |
| 112 | if (Op == "+") |
| 113 | return L + R; |
| 114 | if (Op == "-") |
| 115 | return L - R; |
| 116 | if (Op == "*") |
| 117 | return L * R; |
| 118 | if (Op == "/") { |
| 119 | if (R == 0) { |
| 120 | error("division by zero"); |
George Rimar | 652852c | 2016-04-16 10:10:32 +0000 | [diff] [blame] | 121 | return 0; |
| 122 | } |
Rui Ueyama | 960504b | 2016-04-19 18:58:11 +0000 | [diff] [blame] | 123 | return L / R; |
George Rimar | 652852c | 2016-04-16 10:10:32 +0000 | [diff] [blame] | 124 | } |
Rui Ueyama | 960504b | 2016-04-19 18:58:11 +0000 | [diff] [blame] | 125 | if (Op == "&") |
| 126 | return L & R; |
Rui Ueyama | 7a81d67 | 2016-04-19 19:04:03 +0000 | [diff] [blame] | 127 | llvm_unreachable("invalid operator"); |
Rui Ueyama | 960504b | 2016-04-19 18:58:11 +0000 | [diff] [blame] | 128 | return 0; |
| 129 | } |
| 130 | |
| 131 | // This is an operator-precedence parser to evaluate |
| 132 | // arithmetic expressions in SECTIONS command. |
Rui Ueyama | 99e519c | 2016-04-20 20:48:25 +0000 | [diff] [blame] | 133 | // Tokens should start with an operator. |
Rui Ueyama | c998a8c | 2016-04-22 00:03:13 +0000 | [diff] [blame] | 134 | template <class ELFT> |
| 135 | uint64_t LinkerScript<ELFT>::parseExpr1(ArrayRef<StringRef> &Tokens, |
| 136 | uint64_t Lhs, int MinPrec) { |
Rui Ueyama | 960504b | 2016-04-19 18:58:11 +0000 | [diff] [blame] | 137 | while (!Tokens.empty()) { |
| 138 | // Read an operator and an expression. |
| 139 | StringRef Op1 = Tokens.front(); |
George Rimar | fba45c4 | 2016-04-22 11:28:54 +0000 | [diff] [blame] | 140 | if (Op1 == "?") |
| 141 | return parseTernary(Tokens, Lhs); |
| 142 | |
Rui Ueyama | 960504b | 2016-04-19 18:58:11 +0000 | [diff] [blame] | 143 | if (precedence(Op1) < MinPrec) |
| 144 | return Lhs; |
| 145 | next(Tokens); |
Rui Ueyama | c998a8c | 2016-04-22 00:03:13 +0000 | [diff] [blame] | 146 | uint64_t Rhs = parsePrimary(Tokens); |
Rui Ueyama | 960504b | 2016-04-19 18:58:11 +0000 | [diff] [blame] | 147 | |
| 148 | // Evaluate the remaining part of the expression first if the |
| 149 | // next operator has greater precedence than the previous one. |
| 150 | // For example, if we have read "+" and "3", and if the next |
| 151 | // operator is "*", then we'll evaluate 3 * ... part first. |
| 152 | while (!Tokens.empty()) { |
| 153 | StringRef Op2 = Tokens.front(); |
| 154 | if (precedence(Op2) <= precedence(Op1)) |
| 155 | break; |
Rui Ueyama | c998a8c | 2016-04-22 00:03:13 +0000 | [diff] [blame] | 156 | Rhs = parseExpr1(Tokens, Rhs, precedence(Op2)); |
Rui Ueyama | 960504b | 2016-04-19 18:58:11 +0000 | [diff] [blame] | 157 | } |
| 158 | |
| 159 | Lhs = apply(Op1, Lhs, Rhs); |
| 160 | } |
| 161 | return Lhs; |
| 162 | } |
| 163 | |
Rui Ueyama | c998a8c | 2016-04-22 00:03:13 +0000 | [diff] [blame] | 164 | template <class ELFT> |
| 165 | uint64_t LinkerScript<ELFT>::parseExpr(ArrayRef<StringRef> &Tokens) { |
| 166 | uint64_t V = parsePrimary(Tokens); |
| 167 | return parseExpr1(Tokens, V, 0); |
Rui Ueyama | 99e519c | 2016-04-20 20:48:25 +0000 | [diff] [blame] | 168 | } |
| 169 | |
Rui Ueyama | 960504b | 2016-04-19 18:58:11 +0000 | [diff] [blame] | 170 | // Evaluates the expression given by list of tokens. |
Rui Ueyama | c998a8c | 2016-04-22 00:03:13 +0000 | [diff] [blame] | 171 | template <class ELFT> |
| 172 | uint64_t LinkerScript<ELFT>::evaluate(ArrayRef<StringRef> Tokens) { |
| 173 | uint64_t V = parseExpr(Tokens); |
Rui Ueyama | 960504b | 2016-04-19 18:58:11 +0000 | [diff] [blame] | 174 | if (!Tokens.empty()) |
| 175 | error("stray token: " + Tokens[0]); |
| 176 | return V; |
George Rimar | 652852c | 2016-04-16 10:10:32 +0000 | [diff] [blame] | 177 | } |
| 178 | |
Rui Ueyama | 1ebc8ed | 2016-02-12 21:47:28 +0000 | [diff] [blame] | 179 | template <class ELFT> |
Rui Ueyama | 8ec77e6 | 2016-04-21 22:00:51 +0000 | [diff] [blame] | 180 | StringRef LinkerScript<ELFT>::getOutputSection(InputSectionBase<ELFT> *S) { |
Rui Ueyama | 07320e4 | 2016-04-20 20:13:41 +0000 | [diff] [blame] | 181 | for (SectionRule &R : Opt.Sections) |
Rui Ueyama | c9f402e | 2016-04-22 00:23:52 +0000 | [diff] [blame] | 182 | if (matchStr(R.SectionPattern, S->getSectionName())) |
Rui Ueyama | 8ec77e6 | 2016-04-21 22:00:51 +0000 | [diff] [blame] | 183 | return R.Dest; |
| 184 | return ""; |
Rui Ueyama | 717677a | 2016-02-11 21:17:59 +0000 | [diff] [blame] | 185 | } |
| 186 | |
Rui Ueyama | 1ebc8ed | 2016-02-12 21:47:28 +0000 | [diff] [blame] | 187 | template <class ELFT> |
Rui Ueyama | 07320e4 | 2016-04-20 20:13:41 +0000 | [diff] [blame] | 188 | bool LinkerScript<ELFT>::isDiscarded(InputSectionBase<ELFT> *S) { |
Rui Ueyama | 1ebc8ed | 2016-02-12 21:47:28 +0000 | [diff] [blame] | 189 | return getOutputSection(S) == "/DISCARD/"; |
Rui Ueyama | 717677a | 2016-02-11 21:17:59 +0000 | [diff] [blame] | 190 | } |
| 191 | |
Rui Ueyama | 07320e4 | 2016-04-20 20:13:41 +0000 | [diff] [blame] | 192 | template <class ELFT> |
| 193 | bool LinkerScript<ELFT>::shouldKeep(InputSectionBase<ELFT> *S) { |
Rui Ueyama | 8ec77e6 | 2016-04-21 22:00:51 +0000 | [diff] [blame] | 194 | for (StringRef Pat : Opt.KeptSections) |
| 195 | if (matchStr(Pat, S->getSectionName())) |
| 196 | return true; |
| 197 | return false; |
George Rimar | 481c2ce | 2016-02-23 07:47:54 +0000 | [diff] [blame] | 198 | } |
| 199 | |
George Rimar | 652852c | 2016-04-16 10:10:32 +0000 | [diff] [blame] | 200 | template <class ELFT> |
Rui Ueyama | 7c18c28 | 2016-04-18 21:00:40 +0000 | [diff] [blame] | 201 | static OutputSectionBase<ELFT> * |
George Rimar | dbbd8b1 | 2016-04-21 11:21:48 +0000 | [diff] [blame] | 202 | findSection(ArrayRef<OutputSectionBase<ELFT> *> V, StringRef Name) { |
Rui Ueyama | 7c18c28 | 2016-04-18 21:00:40 +0000 | [diff] [blame] | 203 | for (OutputSectionBase<ELFT> *Sec : V) |
| 204 | if (Sec->getName() == Name) |
| 205 | return Sec; |
| 206 | return nullptr; |
| 207 | } |
| 208 | |
| 209 | template <class ELFT> |
Rui Ueyama | 07320e4 | 2016-04-20 20:13:41 +0000 | [diff] [blame] | 210 | void LinkerScript<ELFT>::assignAddresses( |
George Rimar | dbbd8b1 | 2016-04-21 11:21:48 +0000 | [diff] [blame] | 211 | ArrayRef<OutputSectionBase<ELFT> *> Sections) { |
Rui Ueyama | 7c18c28 | 2016-04-18 21:00:40 +0000 | [diff] [blame] | 212 | typedef typename ELFT::uint uintX_t; |
| 213 | |
George Rimar | 652852c | 2016-04-16 10:10:32 +0000 | [diff] [blame] | 214 | // Orphan sections are sections present in the input files which |
Rui Ueyama | 7c18c28 | 2016-04-18 21:00:40 +0000 | [diff] [blame] | 215 | // are not explicitly placed into the output file by the linker script. |
| 216 | // We place orphan sections at end of file. |
| 217 | // Other linkers places them using some heuristics as described in |
George Rimar | 652852c | 2016-04-16 10:10:32 +0000 | [diff] [blame] | 218 | // https://sourceware.org/binutils/docs/ld/Orphan-Sections.html#Orphan-Sections. |
Rui Ueyama | 7c18c28 | 2016-04-18 21:00:40 +0000 | [diff] [blame] | 219 | for (OutputSectionBase<ELFT> *Sec : Sections) { |
George Rimar | 652852c | 2016-04-16 10:10:32 +0000 | [diff] [blame] | 220 | StringRef Name = Sec->getName(); |
Rui Ueyama | c3e2a4b | 2016-04-21 20:30:00 +0000 | [diff] [blame] | 221 | if (getSectionIndex(Name) == INT_MAX) |
Rui Ueyama | 07320e4 | 2016-04-20 20:13:41 +0000 | [diff] [blame] | 222 | Opt.Commands.push_back({SectionKind, {}, Name}); |
George Rimar | 652852c | 2016-04-16 10:10:32 +0000 | [diff] [blame] | 223 | } |
George Rimar | 652852c | 2016-04-16 10:10:32 +0000 | [diff] [blame] | 224 | |
Rui Ueyama | 7c18c28 | 2016-04-18 21:00:40 +0000 | [diff] [blame] | 225 | // Assign addresses as instructed by linker script SECTIONS sub-commands. |
Rui Ueyama | c998a8c | 2016-04-22 00:03:13 +0000 | [diff] [blame] | 226 | Dot = Out<ELFT>::ElfHeader->getSize() + Out<ELFT>::ProgramHeaders->getSize(); |
George Rimar | 652852c | 2016-04-16 10:10:32 +0000 | [diff] [blame] | 227 | uintX_t ThreadBssOffset = 0; |
George Rimar | 652852c | 2016-04-16 10:10:32 +0000 | [diff] [blame] | 228 | |
Rui Ueyama | 07320e4 | 2016-04-20 20:13:41 +0000 | [diff] [blame] | 229 | for (SectionsCommand &Cmd : Opt.Commands) { |
Rui Ueyama | 9e957a0 | 2016-04-18 21:00:45 +0000 | [diff] [blame] | 230 | if (Cmd.Kind == ExprKind) { |
Rui Ueyama | c998a8c | 2016-04-22 00:03:13 +0000 | [diff] [blame] | 231 | Dot = evaluate(Cmd.Expr); |
George Rimar | 652852c | 2016-04-16 10:10:32 +0000 | [diff] [blame] | 232 | continue; |
| 233 | } |
| 234 | |
George Rimar | dbbd8b1 | 2016-04-21 11:21:48 +0000 | [diff] [blame] | 235 | OutputSectionBase<ELFT> *Sec = findSection<ELFT>(Sections, Cmd.SectionName); |
Rui Ueyama | 7c18c28 | 2016-04-18 21:00:40 +0000 | [diff] [blame] | 236 | if (!Sec) |
George Rimar | 652852c | 2016-04-16 10:10:32 +0000 | [diff] [blame] | 237 | continue; |
| 238 | |
George Rimar | 652852c | 2016-04-16 10:10:32 +0000 | [diff] [blame] | 239 | if ((Sec->getFlags() & SHF_TLS) && Sec->getType() == SHT_NOBITS) { |
Rui Ueyama | c998a8c | 2016-04-22 00:03:13 +0000 | [diff] [blame] | 240 | uintX_t TVA = Dot + ThreadBssOffset; |
Rui Ueyama | 7c18c28 | 2016-04-18 21:00:40 +0000 | [diff] [blame] | 241 | TVA = alignTo(TVA, Sec->getAlign()); |
George Rimar | 652852c | 2016-04-16 10:10:32 +0000 | [diff] [blame] | 242 | Sec->setVA(TVA); |
Rui Ueyama | c998a8c | 2016-04-22 00:03:13 +0000 | [diff] [blame] | 243 | ThreadBssOffset = TVA - Dot + Sec->getSize(); |
George Rimar | 652852c | 2016-04-16 10:10:32 +0000 | [diff] [blame] | 244 | continue; |
| 245 | } |
| 246 | |
| 247 | if (Sec->getFlags() & SHF_ALLOC) { |
Rui Ueyama | c998a8c | 2016-04-22 00:03:13 +0000 | [diff] [blame] | 248 | Dot = alignTo(Dot, Sec->getAlign()); |
| 249 | Sec->setVA(Dot); |
| 250 | Dot += Sec->getSize(); |
George Rimar | 652852c | 2016-04-16 10:10:32 +0000 | [diff] [blame] | 251 | continue; |
| 252 | } |
| 253 | } |
| 254 | } |
| 255 | |
Rui Ueyama | 07320e4 | 2016-04-20 20:13:41 +0000 | [diff] [blame] | 256 | template <class ELFT> |
| 257 | ArrayRef<uint8_t> LinkerScript<ELFT>::getFiller(StringRef Name) { |
| 258 | auto I = Opt.Filler.find(Name); |
| 259 | if (I == Opt.Filler.end()) |
Rui Ueyama | 3e80897 | 2016-02-28 05:09:11 +0000 | [diff] [blame] | 260 | return {}; |
| 261 | return I->second; |
George Rimar | e2ee72b | 2016-02-26 14:48:31 +0000 | [diff] [blame] | 262 | } |
| 263 | |
Rui Ueyama | c3e2a4b | 2016-04-21 20:30:00 +0000 | [diff] [blame] | 264 | // Returns the index of the given section name in linker script |
| 265 | // SECTIONS commands. Sections are laid out as the same order as they |
| 266 | // were in the script. If a given name did not appear in the script, |
| 267 | // it returns INT_MAX, so that it will be laid out at end of file. |
George Rimar | 71b26e9 | 2016-04-21 10:22:02 +0000 | [diff] [blame] | 268 | template <class ELFT> |
Rui Ueyama | c3e2a4b | 2016-04-21 20:30:00 +0000 | [diff] [blame] | 269 | int LinkerScript<ELFT>::getSectionIndex(StringRef Name) { |
George Rimar | 71b26e9 | 2016-04-21 10:22:02 +0000 | [diff] [blame] | 270 | auto Begin = Opt.Commands.begin(); |
| 271 | auto End = Opt.Commands.end(); |
| 272 | auto I = std::find_if(Begin, End, [&](SectionsCommand &N) { |
| 273 | return N.Kind == SectionKind && N.SectionName == Name; |
| 274 | }); |
Rui Ueyama | c3e2a4b | 2016-04-21 20:30:00 +0000 | [diff] [blame] | 275 | return I == End ? INT_MAX : (I - Begin); |
George Rimar | 71b26e9 | 2016-04-21 10:22:02 +0000 | [diff] [blame] | 276 | } |
| 277 | |
| 278 | // A compartor to sort output sections. Returns -1 or 1 if |
| 279 | // A or B are mentioned in linker script. Otherwise, returns 0. |
Rui Ueyama | 07320e4 | 2016-04-20 20:13:41 +0000 | [diff] [blame] | 280 | template <class ELFT> |
| 281 | int LinkerScript<ELFT>::compareSections(StringRef A, StringRef B) { |
Rui Ueyama | c3e2a4b | 2016-04-21 20:30:00 +0000 | [diff] [blame] | 282 | int I = getSectionIndex(A); |
| 283 | int J = getSectionIndex(B); |
| 284 | if (I == INT_MAX && J == INT_MAX) |
Rui Ueyama | 717677a | 2016-02-11 21:17:59 +0000 | [diff] [blame] | 285 | return 0; |
| 286 | return I < J ? -1 : 1; |
| 287 | } |
| 288 | |
George Rimar | cb2aeb6 | 2016-02-24 08:49:50 +0000 | [diff] [blame] | 289 | // Returns true if S matches T. S can contain glob meta-characters. |
| 290 | // The asterisk ('*') matches zero or more characacters, and the question |
| 291 | // mark ('?') matches one character. |
Rui Ueyama | 1ebc8ed | 2016-02-12 21:47:28 +0000 | [diff] [blame] | 292 | static bool matchStr(StringRef S, StringRef T) { |
| 293 | for (;;) { |
| 294 | if (S.empty()) |
| 295 | return T.empty(); |
| 296 | if (S[0] == '*') { |
| 297 | S = S.substr(1); |
| 298 | if (S.empty()) |
| 299 | // Fast path. If a pattern is '*', it matches anything. |
| 300 | return true; |
| 301 | for (size_t I = 0, E = T.size(); I < E; ++I) |
| 302 | if (matchStr(S, T.substr(I))) |
| 303 | return true; |
| 304 | return false; |
| 305 | } |
George Rimar | cb2aeb6 | 2016-02-24 08:49:50 +0000 | [diff] [blame] | 306 | if (T.empty() || (S[0] != T[0] && S[0] != '?')) |
Rui Ueyama | 1ebc8ed | 2016-02-12 21:47:28 +0000 | [diff] [blame] | 307 | return false; |
| 308 | S = S.substr(1); |
| 309 | T = T.substr(1); |
| 310 | } |
| 311 | } |
| 312 | |
Rui Ueyama | 07320e4 | 2016-04-20 20:13:41 +0000 | [diff] [blame] | 313 | class elf::ScriptParser : public ScriptParserBase { |
George Rimar | c3794e5 | 2016-02-24 09:21:47 +0000 | [diff] [blame] | 314 | typedef void (ScriptParser::*Handler)(); |
| 315 | |
Rui Ueyama | f7c5fbb | 2015-09-30 17:23:26 +0000 | [diff] [blame] | 316 | public: |
Rui Ueyama | 07320e4 | 2016-04-20 20:13:41 +0000 | [diff] [blame] | 317 | ScriptParser(StringRef S, bool B) : ScriptParserBase(S), IsUnderSysroot(B) {} |
George Rimar | f23b232 | 2016-02-19 10:45:45 +0000 | [diff] [blame] | 318 | |
Adhemerval Zanella | e77b5bf | 2016-04-06 20:59:11 +0000 | [diff] [blame] | 319 | void run() override; |
Rui Ueyama | f7c5fbb | 2015-09-30 17:23:26 +0000 | [diff] [blame] | 320 | |
| 321 | private: |
Rui Ueyama | 52a1509 | 2015-10-11 03:28:42 +0000 | [diff] [blame] | 322 | void addFile(StringRef Path); |
| 323 | |
Rui Ueyama | f7c5fbb | 2015-09-30 17:23:26 +0000 | [diff] [blame] | 324 | void readAsNeeded(); |
Denis Protivensky | 90c5099 | 2015-10-08 06:48:38 +0000 | [diff] [blame] | 325 | void readEntry(); |
George Rimar | 83f406c | 2015-10-19 17:35:12 +0000 | [diff] [blame] | 326 | void readExtern(); |
Rui Ueyama | f7c5fbb | 2015-09-30 17:23:26 +0000 | [diff] [blame] | 327 | void readGroup(); |
Rui Ueyama | 31aa1f8 | 2015-10-11 01:31:55 +0000 | [diff] [blame] | 328 | void readInclude(); |
George Rimar | c3794e5 | 2016-02-24 09:21:47 +0000 | [diff] [blame] | 329 | void readNothing() {} |
Rui Ueyama | ee59282 | 2015-10-07 00:25:09 +0000 | [diff] [blame] | 330 | void readOutput(); |
Davide Italiano | 9159ce9 | 2015-10-12 21:50:08 +0000 | [diff] [blame] | 331 | void readOutputArch(); |
Rui Ueyama | f7c5fbb | 2015-09-30 17:23:26 +0000 | [diff] [blame] | 332 | void readOutputFormat(); |
Davide Italiano | 68a39a6 | 2015-10-08 17:51:41 +0000 | [diff] [blame] | 333 | void readSearchDir(); |
Denis Protivensky | 8e3b38a | 2015-11-12 09:52:08 +0000 | [diff] [blame] | 334 | void readSections(); |
| 335 | |
George Rimar | 652852c | 2016-04-16 10:10:32 +0000 | [diff] [blame] | 336 | void readLocationCounterValue(); |
Denis Protivensky | 8e3b38a | 2015-11-12 09:52:08 +0000 | [diff] [blame] | 337 | void readOutputSectionDescription(); |
Rui Ueyama | 8ec77e6 | 2016-04-21 22:00:51 +0000 | [diff] [blame] | 338 | void readSectionPatterns(StringRef OutSec); |
Rui Ueyama | f7c5fbb | 2015-09-30 17:23:26 +0000 | [diff] [blame] | 339 | |
George Rimar | c3794e5 | 2016-02-24 09:21:47 +0000 | [diff] [blame] | 340 | const static StringMap<Handler> Cmd; |
Rui Ueyama | 07320e4 | 2016-04-20 20:13:41 +0000 | [diff] [blame] | 341 | ScriptConfiguration &Opt = *ScriptConfig; |
| 342 | StringSaver Saver = {ScriptConfig->Alloc}; |
Simon Atanasyan | 16b0cc9 | 2015-11-26 05:53:00 +0000 | [diff] [blame] | 343 | bool IsUnderSysroot; |
Rui Ueyama | f7c5fbb | 2015-09-30 17:23:26 +0000 | [diff] [blame] | 344 | }; |
Rui Ueyama | f7c5fbb | 2015-09-30 17:23:26 +0000 | [diff] [blame] | 345 | |
Rafael Espindola | e0df00b | 2016-02-28 00:25:54 +0000 | [diff] [blame] | 346 | const StringMap<elf::ScriptParser::Handler> elf::ScriptParser::Cmd = { |
George Rimar | c3794e5 | 2016-02-24 09:21:47 +0000 | [diff] [blame] | 347 | {"ENTRY", &ScriptParser::readEntry}, |
| 348 | {"EXTERN", &ScriptParser::readExtern}, |
| 349 | {"GROUP", &ScriptParser::readGroup}, |
| 350 | {"INCLUDE", &ScriptParser::readInclude}, |
| 351 | {"INPUT", &ScriptParser::readGroup}, |
| 352 | {"OUTPUT", &ScriptParser::readOutput}, |
| 353 | {"OUTPUT_ARCH", &ScriptParser::readOutputArch}, |
| 354 | {"OUTPUT_FORMAT", &ScriptParser::readOutputFormat}, |
| 355 | {"SEARCH_DIR", &ScriptParser::readSearchDir}, |
| 356 | {"SECTIONS", &ScriptParser::readSections}, |
| 357 | {";", &ScriptParser::readNothing}}; |
| 358 | |
Rui Ueyama | 717677a | 2016-02-11 21:17:59 +0000 | [diff] [blame] | 359 | void ScriptParser::run() { |
Rui Ueyama | f7c5fbb | 2015-09-30 17:23:26 +0000 | [diff] [blame] | 360 | while (!atEOF()) { |
| 361 | StringRef Tok = next(); |
George Rimar | c3794e5 | 2016-02-24 09:21:47 +0000 | [diff] [blame] | 362 | if (Handler Fn = Cmd.lookup(Tok)) |
| 363 | (this->*Fn)(); |
| 364 | else |
George Rimar | 5761042 | 2016-03-11 14:43:02 +0000 | [diff] [blame] | 365 | setError("unknown directive: " + Tok); |
Rui Ueyama | f7c5fbb | 2015-09-30 17:23:26 +0000 | [diff] [blame] | 366 | } |
| 367 | } |
| 368 | |
Rui Ueyama | 717677a | 2016-02-11 21:17:59 +0000 | [diff] [blame] | 369 | void ScriptParser::addFile(StringRef S) { |
Simon Atanasyan | 16b0cc9 | 2015-11-26 05:53:00 +0000 | [diff] [blame] | 370 | if (IsUnderSysroot && S.startswith("/")) { |
| 371 | SmallString<128> Path; |
| 372 | (Config->Sysroot + S).toStringRef(Path); |
| 373 | if (sys::fs::exists(Path)) { |
| 374 | Driver->addFile(Saver.save(Path.str())); |
| 375 | return; |
| 376 | } |
| 377 | } |
| 378 | |
Rui Ueyama | f03f3cc | 2015-10-13 00:09:21 +0000 | [diff] [blame] | 379 | if (sys::path::is_absolute(S)) { |
Rui Ueyama | 52a1509 | 2015-10-11 03:28:42 +0000 | [diff] [blame] | 380 | Driver->addFile(S); |
| 381 | } else if (S.startswith("=")) { |
| 382 | if (Config->Sysroot.empty()) |
| 383 | Driver->addFile(S.substr(1)); |
| 384 | else |
| 385 | Driver->addFile(Saver.save(Config->Sysroot + "/" + S.substr(1))); |
| 386 | } else if (S.startswith("-l")) { |
Rui Ueyama | 21eecb4 | 2016-02-02 21:13:09 +0000 | [diff] [blame] | 387 | Driver->addLibrary(S.substr(2)); |
Simon Atanasyan | a1b8fc3 | 2015-11-26 20:23:46 +0000 | [diff] [blame] | 388 | } else if (sys::fs::exists(S)) { |
| 389 | Driver->addFile(S); |
Rui Ueyama | 52a1509 | 2015-10-11 03:28:42 +0000 | [diff] [blame] | 390 | } else { |
| 391 | std::string Path = findFromSearchPaths(S); |
| 392 | if (Path.empty()) |
George Rimar | 777f963 | 2016-03-12 08:31:34 +0000 | [diff] [blame] | 393 | setError("unable to find " + S); |
Rui Ueyama | 025d59b | 2016-02-02 20:27:59 +0000 | [diff] [blame] | 394 | else |
| 395 | Driver->addFile(Saver.save(Path)); |
Rui Ueyama | 52a1509 | 2015-10-11 03:28:42 +0000 | [diff] [blame] | 396 | } |
| 397 | } |
| 398 | |
Rui Ueyama | 717677a | 2016-02-11 21:17:59 +0000 | [diff] [blame] | 399 | void ScriptParser::readAsNeeded() { |
Rui Ueyama | f7c5fbb | 2015-09-30 17:23:26 +0000 | [diff] [blame] | 400 | expect("("); |
Rui Ueyama | 35da9b6 | 2015-10-11 20:59:12 +0000 | [diff] [blame] | 401 | bool Orig = Config->AsNeeded; |
| 402 | Config->AsNeeded = true; |
Rui Ueyama | 025d59b | 2016-02-02 20:27:59 +0000 | [diff] [blame] | 403 | while (!Error) { |
Rui Ueyama | f7c5fbb | 2015-09-30 17:23:26 +0000 | [diff] [blame] | 404 | StringRef Tok = next(); |
| 405 | if (Tok == ")") |
Rui Ueyama | 35da9b6 | 2015-10-11 20:59:12 +0000 | [diff] [blame] | 406 | break; |
Rui Ueyama | 52a1509 | 2015-10-11 03:28:42 +0000 | [diff] [blame] | 407 | addFile(Tok); |
Rui Ueyama | f7c5fbb | 2015-09-30 17:23:26 +0000 | [diff] [blame] | 408 | } |
Rui Ueyama | 35da9b6 | 2015-10-11 20:59:12 +0000 | [diff] [blame] | 409 | Config->AsNeeded = Orig; |
Rui Ueyama | f7c5fbb | 2015-09-30 17:23:26 +0000 | [diff] [blame] | 410 | } |
| 411 | |
Rui Ueyama | 717677a | 2016-02-11 21:17:59 +0000 | [diff] [blame] | 412 | void ScriptParser::readEntry() { |
Denis Protivensky | 90c5099 | 2015-10-08 06:48:38 +0000 | [diff] [blame] | 413 | // -e <symbol> takes predecence over ENTRY(<symbol>). |
| 414 | expect("("); |
| 415 | StringRef Tok = next(); |
| 416 | if (Config->Entry.empty()) |
| 417 | Config->Entry = Tok; |
| 418 | expect(")"); |
| 419 | } |
| 420 | |
Rui Ueyama | 717677a | 2016-02-11 21:17:59 +0000 | [diff] [blame] | 421 | void ScriptParser::readExtern() { |
George Rimar | 83f406c | 2015-10-19 17:35:12 +0000 | [diff] [blame] | 422 | expect("("); |
Rui Ueyama | 025d59b | 2016-02-02 20:27:59 +0000 | [diff] [blame] | 423 | while (!Error) { |
George Rimar | 83f406c | 2015-10-19 17:35:12 +0000 | [diff] [blame] | 424 | StringRef Tok = next(); |
| 425 | if (Tok == ")") |
| 426 | return; |
| 427 | Config->Undefined.push_back(Tok); |
| 428 | } |
| 429 | } |
| 430 | |
Rui Ueyama | 717677a | 2016-02-11 21:17:59 +0000 | [diff] [blame] | 431 | void ScriptParser::readGroup() { |
Rui Ueyama | f7c5fbb | 2015-09-30 17:23:26 +0000 | [diff] [blame] | 432 | expect("("); |
Rui Ueyama | 025d59b | 2016-02-02 20:27:59 +0000 | [diff] [blame] | 433 | while (!Error) { |
Rui Ueyama | f7c5fbb | 2015-09-30 17:23:26 +0000 | [diff] [blame] | 434 | StringRef Tok = next(); |
| 435 | if (Tok == ")") |
| 436 | return; |
| 437 | if (Tok == "AS_NEEDED") { |
| 438 | readAsNeeded(); |
| 439 | continue; |
| 440 | } |
Rui Ueyama | 52a1509 | 2015-10-11 03:28:42 +0000 | [diff] [blame] | 441 | addFile(Tok); |
Rui Ueyama | f7c5fbb | 2015-09-30 17:23:26 +0000 | [diff] [blame] | 442 | } |
| 443 | } |
| 444 | |
Rui Ueyama | 717677a | 2016-02-11 21:17:59 +0000 | [diff] [blame] | 445 | void ScriptParser::readInclude() { |
Rui Ueyama | 31aa1f8 | 2015-10-11 01:31:55 +0000 | [diff] [blame] | 446 | StringRef Tok = next(); |
| 447 | auto MBOrErr = MemoryBuffer::getFile(Tok); |
Rui Ueyama | 025d59b | 2016-02-02 20:27:59 +0000 | [diff] [blame] | 448 | if (!MBOrErr) { |
George Rimar | 5761042 | 2016-03-11 14:43:02 +0000 | [diff] [blame] | 449 | setError("cannot open " + Tok); |
Rui Ueyama | 025d59b | 2016-02-02 20:27:59 +0000 | [diff] [blame] | 450 | return; |
| 451 | } |
Rui Ueyama | 31aa1f8 | 2015-10-11 01:31:55 +0000 | [diff] [blame] | 452 | std::unique_ptr<MemoryBuffer> &MB = *MBOrErr; |
Rui Ueyama | a47ee68 | 2015-10-11 01:53:04 +0000 | [diff] [blame] | 453 | StringRef S = Saver.save(MB->getMemBufferRef().getBuffer()); |
| 454 | std::vector<StringRef> V = tokenize(S); |
Rui Ueyama | 31aa1f8 | 2015-10-11 01:31:55 +0000 | [diff] [blame] | 455 | Tokens.insert(Tokens.begin() + Pos, V.begin(), V.end()); |
Rui Ueyama | 31aa1f8 | 2015-10-11 01:31:55 +0000 | [diff] [blame] | 456 | } |
| 457 | |
Rui Ueyama | 717677a | 2016-02-11 21:17:59 +0000 | [diff] [blame] | 458 | void ScriptParser::readOutput() { |
Rui Ueyama | ee59282 | 2015-10-07 00:25:09 +0000 | [diff] [blame] | 459 | // -o <file> takes predecence over OUTPUT(<file>). |
| 460 | expect("("); |
| 461 | StringRef Tok = next(); |
| 462 | if (Config->OutputFile.empty()) |
| 463 | Config->OutputFile = Tok; |
| 464 | expect(")"); |
| 465 | } |
| 466 | |
Rui Ueyama | 717677a | 2016-02-11 21:17:59 +0000 | [diff] [blame] | 467 | void ScriptParser::readOutputArch() { |
Davide Italiano | 9159ce9 | 2015-10-12 21:50:08 +0000 | [diff] [blame] | 468 | // Error checking only for now. |
| 469 | expect("("); |
| 470 | next(); |
| 471 | expect(")"); |
| 472 | } |
| 473 | |
Rui Ueyama | 717677a | 2016-02-11 21:17:59 +0000 | [diff] [blame] | 474 | void ScriptParser::readOutputFormat() { |
Rui Ueyama | f7c5fbb | 2015-09-30 17:23:26 +0000 | [diff] [blame] | 475 | // Error checking only for now. |
| 476 | expect("("); |
| 477 | next(); |
Davide Italiano | 6836c61 | 2015-10-12 21:08:41 +0000 | [diff] [blame] | 478 | StringRef Tok = next(); |
| 479 | if (Tok == ")") |
| 480 | return; |
Rui Ueyama | 025d59b | 2016-02-02 20:27:59 +0000 | [diff] [blame] | 481 | if (Tok != ",") { |
George Rimar | 5761042 | 2016-03-11 14:43:02 +0000 | [diff] [blame] | 482 | setError("unexpected token: " + Tok); |
Rui Ueyama | 025d59b | 2016-02-02 20:27:59 +0000 | [diff] [blame] | 483 | return; |
| 484 | } |
Davide Italiano | 6836c61 | 2015-10-12 21:08:41 +0000 | [diff] [blame] | 485 | next(); |
| 486 | expect(","); |
| 487 | next(); |
Rui Ueyama | f7c5fbb | 2015-09-30 17:23:26 +0000 | [diff] [blame] | 488 | expect(")"); |
| 489 | } |
| 490 | |
Rui Ueyama | 717677a | 2016-02-11 21:17:59 +0000 | [diff] [blame] | 491 | void ScriptParser::readSearchDir() { |
Davide Italiano | 68a39a6 | 2015-10-08 17:51:41 +0000 | [diff] [blame] | 492 | expect("("); |
Rafael Espindola | 0650192 | 2016-03-08 17:13:12 +0000 | [diff] [blame] | 493 | Config->SearchPaths.push_back(next()); |
Davide Italiano | 68a39a6 | 2015-10-08 17:51:41 +0000 | [diff] [blame] | 494 | expect(")"); |
| 495 | } |
| 496 | |
Rui Ueyama | 717677a | 2016-02-11 21:17:59 +0000 | [diff] [blame] | 497 | void ScriptParser::readSections() { |
Rui Ueyama | 07320e4 | 2016-04-20 20:13:41 +0000 | [diff] [blame] | 498 | Opt.DoLayout = true; |
Denis Protivensky | 8e3b38a | 2015-11-12 09:52:08 +0000 | [diff] [blame] | 499 | expect("{"); |
George Rimar | 652852c | 2016-04-16 10:10:32 +0000 | [diff] [blame] | 500 | while (!Error && !skip("}")) { |
| 501 | StringRef Tok = peek(); |
| 502 | if (Tok == ".") |
| 503 | readLocationCounterValue(); |
| 504 | else |
| 505 | readOutputSectionDescription(); |
| 506 | } |
Denis Protivensky | 8e3b38a | 2015-11-12 09:52:08 +0000 | [diff] [blame] | 507 | } |
| 508 | |
Rui Ueyama | 8ec77e6 | 2016-04-21 22:00:51 +0000 | [diff] [blame] | 509 | void ScriptParser::readSectionPatterns(StringRef OutSec) { |
George Rimar | 481c2ce | 2016-02-23 07:47:54 +0000 | [diff] [blame] | 510 | expect("("); |
| 511 | while (!Error && !skip(")")) |
Rui Ueyama | 8ec77e6 | 2016-04-21 22:00:51 +0000 | [diff] [blame] | 512 | Opt.Sections.emplace_back(OutSec, next()); |
George Rimar | 481c2ce | 2016-02-23 07:47:54 +0000 | [diff] [blame] | 513 | } |
| 514 | |
George Rimar | 652852c | 2016-04-16 10:10:32 +0000 | [diff] [blame] | 515 | void ScriptParser::readLocationCounterValue() { |
| 516 | expect("."); |
| 517 | expect("="); |
Rui Ueyama | 07320e4 | 2016-04-20 20:13:41 +0000 | [diff] [blame] | 518 | Opt.Commands.push_back({ExprKind, {}, ""}); |
| 519 | SectionsCommand &Cmd = Opt.Commands.back(); |
George Rimar | 652852c | 2016-04-16 10:10:32 +0000 | [diff] [blame] | 520 | while (!Error) { |
| 521 | StringRef Tok = next(); |
| 522 | if (Tok == ";") |
| 523 | break; |
Rui Ueyama | 9e957a0 | 2016-04-18 21:00:45 +0000 | [diff] [blame] | 524 | Cmd.Expr.push_back(Tok); |
George Rimar | 652852c | 2016-04-16 10:10:32 +0000 | [diff] [blame] | 525 | } |
Rui Ueyama | 9e957a0 | 2016-04-18 21:00:45 +0000 | [diff] [blame] | 526 | if (Cmd.Expr.empty()) |
George Rimar | 652852c | 2016-04-16 10:10:32 +0000 | [diff] [blame] | 527 | error("error in location counter expression"); |
| 528 | } |
| 529 | |
Rui Ueyama | 717677a | 2016-02-11 21:17:59 +0000 | [diff] [blame] | 530 | void ScriptParser::readOutputSectionDescription() { |
Rui Ueyama | 3e80897 | 2016-02-28 05:09:11 +0000 | [diff] [blame] | 531 | StringRef OutSec = next(); |
Rui Ueyama | 07320e4 | 2016-04-20 20:13:41 +0000 | [diff] [blame] | 532 | Opt.Commands.push_back({SectionKind, {}, OutSec}); |
Denis Protivensky | 8e3b38a | 2015-11-12 09:52:08 +0000 | [diff] [blame] | 533 | expect(":"); |
| 534 | expect("{"); |
Rui Ueyama | 8ec77e6 | 2016-04-21 22:00:51 +0000 | [diff] [blame] | 535 | |
Rui Ueyama | 025d59b | 2016-02-02 20:27:59 +0000 | [diff] [blame] | 536 | while (!Error && !skip("}")) { |
George Rimar | 481c2ce | 2016-02-23 07:47:54 +0000 | [diff] [blame] | 537 | StringRef Tok = next(); |
| 538 | if (Tok == "*") { |
Rui Ueyama | 8ec77e6 | 2016-04-21 22:00:51 +0000 | [diff] [blame] | 539 | expect("("); |
| 540 | while (!Error && !skip(")")) |
| 541 | Opt.Sections.emplace_back(OutSec, next()); |
George Rimar | 481c2ce | 2016-02-23 07:47:54 +0000 | [diff] [blame] | 542 | } else if (Tok == "KEEP") { |
| 543 | expect("("); |
Rui Ueyama | 8ec77e6 | 2016-04-21 22:00:51 +0000 | [diff] [blame] | 544 | expect("*"); |
| 545 | expect("("); |
| 546 | while (!Error && !skip(")")) { |
| 547 | StringRef Sec = next(); |
| 548 | Opt.Sections.emplace_back(OutSec, Sec); |
| 549 | Opt.KeptSections.push_back(Sec); |
| 550 | } |
George Rimar | 481c2ce | 2016-02-23 07:47:54 +0000 | [diff] [blame] | 551 | expect(")"); |
| 552 | } else { |
George Rimar | 777f963 | 2016-03-12 08:31:34 +0000 | [diff] [blame] | 553 | setError("unknown command " + Tok); |
George Rimar | 481c2ce | 2016-02-23 07:47:54 +0000 | [diff] [blame] | 554 | } |
Denis Protivensky | 8e3b38a | 2015-11-12 09:52:08 +0000 | [diff] [blame] | 555 | } |
Rui Ueyama | 8ec77e6 | 2016-04-21 22:00:51 +0000 | [diff] [blame] | 556 | |
George Rimar | e2ee72b | 2016-02-26 14:48:31 +0000 | [diff] [blame] | 557 | StringRef Tok = peek(); |
| 558 | if (Tok.startswith("=")) { |
| 559 | if (!Tok.startswith("=0x")) { |
Rui Ueyama | 3ed2f06 | 2016-03-13 03:17:44 +0000 | [diff] [blame] | 560 | setError("filler should be a hexadecimal value"); |
George Rimar | e2ee72b | 2016-02-26 14:48:31 +0000 | [diff] [blame] | 561 | return; |
| 562 | } |
Rui Ueyama | 3e80897 | 2016-02-28 05:09:11 +0000 | [diff] [blame] | 563 | Tok = Tok.substr(3); |
Rui Ueyama | 07320e4 | 2016-04-20 20:13:41 +0000 | [diff] [blame] | 564 | Opt.Filler[OutSec] = parseHex(Tok); |
George Rimar | e2ee72b | 2016-02-26 14:48:31 +0000 | [diff] [blame] | 565 | next(); |
| 566 | } |
Denis Protivensky | 8e3b38a | 2015-11-12 09:52:08 +0000 | [diff] [blame] | 567 | } |
| 568 | |
Simon Atanasyan | 16b0cc9 | 2015-11-26 05:53:00 +0000 | [diff] [blame] | 569 | static bool isUnderSysroot(StringRef Path) { |
| 570 | if (Config->Sysroot == "") |
| 571 | return false; |
| 572 | for (; !Path.empty(); Path = sys::path::parent_path(Path)) |
| 573 | if (sys::fs::equivalent(Config->Sysroot, Path)) |
| 574 | return true; |
| 575 | return false; |
| 576 | } |
| 577 | |
Rui Ueyama | 07320e4 | 2016-04-20 20:13:41 +0000 | [diff] [blame] | 578 | // Entry point. |
| 579 | void elf::readLinkerScript(MemoryBufferRef MB) { |
Simon Atanasyan | 16b0cc9 | 2015-11-26 05:53:00 +0000 | [diff] [blame] | 580 | StringRef Path = MB.getBufferIdentifier(); |
Rui Ueyama | 07320e4 | 2016-04-20 20:13:41 +0000 | [diff] [blame] | 581 | ScriptParser(MB.getBuffer(), isUnderSysroot(Path)).run(); |
Rui Ueyama | f7c5fbb | 2015-09-30 17:23:26 +0000 | [diff] [blame] | 582 | } |
Rui Ueyama | 1ebc8ed | 2016-02-12 21:47:28 +0000 | [diff] [blame] | 583 | |
Rui Ueyama | 07320e4 | 2016-04-20 20:13:41 +0000 | [diff] [blame] | 584 | template class elf::LinkerScript<ELF32LE>; |
| 585 | template class elf::LinkerScript<ELF32BE>; |
| 586 | template class elf::LinkerScript<ELF64LE>; |
| 587 | template class elf::LinkerScript<ELF64BE>; |