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