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