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