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