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