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. |
Rui Ueyama | 629e0aa5 | 2016-07-21 19:45:22 +0000 | [diff] [blame] | 11 | // It parses a linker script and write the result to Config or ScriptConfig |
| 12 | // objects. |
| 13 | // |
| 14 | // If SECTIONS command is used, a ScriptConfig contains an AST |
| 15 | // of the command which will later be consumed by createSections() and |
| 16 | // assignAddresses(). |
Rui Ueyama | f7c5fbb | 2015-09-30 17:23:26 +0000 | [diff] [blame] | 17 | // |
| 18 | //===----------------------------------------------------------------------===// |
| 19 | |
Rui Ueyama | 717677a | 2016-02-11 21:17:59 +0000 | [diff] [blame] | 20 | #include "LinkerScript.h" |
Rui Ueyama | f7c5fbb | 2015-09-30 17:23:26 +0000 | [diff] [blame] | 21 | #include "Config.h" |
| 22 | #include "Driver.h" |
Rui Ueyama | 1ebc8ed | 2016-02-12 21:47:28 +0000 | [diff] [blame] | 23 | #include "InputSection.h" |
George Rimar | 652852c | 2016-04-16 10:10:32 +0000 | [diff] [blame] | 24 | #include "OutputSections.h" |
Adhemerval Zanella | e77b5bf | 2016-04-06 20:59:11 +0000 | [diff] [blame] | 25 | #include "ScriptParser.h" |
Rui Ueyama | 93c9af4 | 2016-06-29 08:01:32 +0000 | [diff] [blame] | 26 | #include "Strings.h" |
Eugene Leviant | eda81a1 | 2016-07-12 06:39:48 +0000 | [diff] [blame] | 27 | #include "Symbols.h" |
Rui Ueyama | f7c5fbb | 2015-09-30 17:23:26 +0000 | [diff] [blame] | 28 | #include "SymbolTable.h" |
Eugene Leviant | 467c4d5 | 2016-07-01 10:27:36 +0000 | [diff] [blame] | 29 | #include "Target.h" |
Eugene Leviant | bbe3860 | 2016-07-19 09:25:43 +0000 | [diff] [blame] | 30 | #include "Writer.h" |
Rui Ueyama | 960504b | 2016-04-19 18:58:11 +0000 | [diff] [blame] | 31 | #include "llvm/ADT/StringSwitch.h" |
George Rimar | 652852c | 2016-04-16 10:10:32 +0000 | [diff] [blame] | 32 | #include "llvm/Support/ELF.h" |
Rui Ueyama | f7c5fbb | 2015-09-30 17:23:26 +0000 | [diff] [blame] | 33 | #include "llvm/Support/FileSystem.h" |
| 34 | #include "llvm/Support/MemoryBuffer.h" |
Rui Ueyama | f03f3cc | 2015-10-13 00:09:21 +0000 | [diff] [blame] | 35 | #include "llvm/Support/Path.h" |
Rui Ueyama | a47ee68 | 2015-10-11 01:53:04 +0000 | [diff] [blame] | 36 | #include "llvm/Support/StringSaver.h" |
Rui Ueyama | f7c5fbb | 2015-09-30 17:23:26 +0000 | [diff] [blame] | 37 | |
| 38 | using namespace llvm; |
George Rimar | 652852c | 2016-04-16 10:10:32 +0000 | [diff] [blame] | 39 | using namespace llvm::ELF; |
Rui Ueyama | 1ebc8ed | 2016-02-12 21:47:28 +0000 | [diff] [blame] | 40 | using namespace llvm::object; |
Rui Ueyama | f7c5fbb | 2015-09-30 17:23:26 +0000 | [diff] [blame] | 41 | using namespace lld; |
Rafael Espindola | e0df00b | 2016-02-28 00:25:54 +0000 | [diff] [blame] | 42 | using namespace lld::elf; |
Rui Ueyama | f7c5fbb | 2015-09-30 17:23:26 +0000 | [diff] [blame] | 43 | |
Rui Ueyama | 07320e4 | 2016-04-20 20:13:41 +0000 | [diff] [blame] | 44 | ScriptConfiguration *elf::ScriptConfig; |
Rui Ueyama | 717677a | 2016-02-11 21:17:59 +0000 | [diff] [blame] | 45 | |
George Rimar | 076fe15 | 2016-07-21 06:43:01 +0000 | [diff] [blame] | 46 | bool SymbolAssignment::classof(const BaseCommand *C) { |
| 47 | return C->Kind == AssignmentKind; |
| 48 | } |
| 49 | |
| 50 | bool OutputSectionCommand::classof(const BaseCommand *C) { |
| 51 | return C->Kind == OutputSectionKind; |
| 52 | } |
| 53 | |
George Rimar | eea3114 | 2016-07-21 14:26:59 +0000 | [diff] [blame] | 54 | bool InputSectionDescription::classof(const BaseCommand *C) { |
| 55 | return C->Kind == InputSectionKind; |
| 56 | } |
| 57 | |
Rui Ueyama | 9c1112d | 2016-04-23 00:04:03 +0000 | [diff] [blame] | 58 | // This is an operator-precedence parser to parse and evaluate |
| 59 | // a linker script expression. For each linker script arithmetic |
| 60 | // expression (e.g. ". = . + 0x1000"), a new instance of ExprParser |
| 61 | // is created and ran. |
| 62 | namespace { |
| 63 | class ExprParser : public ScriptParserBase { |
| 64 | public: |
| 65 | ExprParser(std::vector<StringRef> &Tokens, uint64_t Dot) |
| 66 | : ScriptParserBase(Tokens), Dot(Dot) {} |
| 67 | |
| 68 | uint64_t run(); |
| 69 | |
| 70 | private: |
| 71 | uint64_t parsePrimary(); |
| 72 | uint64_t parseTernary(uint64_t Cond); |
| 73 | uint64_t apply(StringRef Op, uint64_t L, uint64_t R); |
| 74 | uint64_t parseExpr1(uint64_t Lhs, int MinPrec); |
| 75 | uint64_t parseExpr(); |
| 76 | |
| 77 | uint64_t Dot; |
| 78 | }; |
| 79 | } |
| 80 | |
Rui Ueyama | 960504b | 2016-04-19 18:58:11 +0000 | [diff] [blame] | 81 | static int precedence(StringRef Op) { |
| 82 | return StringSwitch<int>(Op) |
| 83 | .Case("*", 4) |
George Rimar | ab93906 | 2016-04-25 08:14:41 +0000 | [diff] [blame] | 84 | .Case("/", 4) |
| 85 | .Case("+", 3) |
| 86 | .Case("-", 3) |
| 87 | .Case("<", 2) |
| 88 | .Case(">", 2) |
| 89 | .Case(">=", 2) |
| 90 | .Case("<=", 2) |
| 91 | .Case("==", 2) |
| 92 | .Case("!=", 2) |
Rui Ueyama | 960504b | 2016-04-19 18:58:11 +0000 | [diff] [blame] | 93 | .Case("&", 1) |
| 94 | .Default(-1); |
| 95 | } |
| 96 | |
Rui Ueyama | 9c1112d | 2016-04-23 00:04:03 +0000 | [diff] [blame] | 97 | static uint64_t evalExpr(std::vector<StringRef> &Tokens, uint64_t Dot) { |
| 98 | return ExprParser(Tokens, Dot).run(); |
Rui Ueyama | 960504b | 2016-04-19 18:58:11 +0000 | [diff] [blame] | 99 | } |
| 100 | |
Rui Ueyama | 9c1112d | 2016-04-23 00:04:03 +0000 | [diff] [blame] | 101 | uint64_t ExprParser::run() { |
| 102 | uint64_t V = parseExpr(); |
| 103 | if (!atEOF() && !Error) |
| 104 | setError("stray token: " + peek()); |
| 105 | return V; |
Rui Ueyama | 6011811 | 2016-04-20 20:54:13 +0000 | [diff] [blame] | 106 | } |
| 107 | |
George Rimar | 92e93fb | 2016-07-21 19:48:00 +0000 | [diff] [blame] | 108 | uint64_t static getConstantValue(StringRef C) { |
| 109 | if (C == "COMMONPAGESIZE" || C == "MAXPAGESIZE") |
| 110 | return Target->PageSize; |
| 111 | error("unknown constant: " + C); |
| 112 | return 0; |
| 113 | } |
| 114 | |
Rui Ueyama | 960504b | 2016-04-19 18:58:11 +0000 | [diff] [blame] | 115 | // This is a part of the operator-precedence parser to evaluate |
| 116 | // arithmetic expressions in SECTIONS command. This function evaluates an |
Rui Ueyama | e29a975 | 2016-04-22 21:02:27 +0000 | [diff] [blame] | 117 | // integer literal, a parenthesized expression, the ALIGN function, |
| 118 | // or the special variable ".". |
Rui Ueyama | 9c1112d | 2016-04-23 00:04:03 +0000 | [diff] [blame] | 119 | uint64_t ExprParser::parsePrimary() { |
| 120 | StringRef Tok = next(); |
Rui Ueyama | 960504b | 2016-04-19 18:58:11 +0000 | [diff] [blame] | 121 | if (Tok == ".") |
| 122 | return Dot; |
| 123 | if (Tok == "(") { |
Rui Ueyama | 9c1112d | 2016-04-23 00:04:03 +0000 | [diff] [blame] | 124 | uint64_t V = parseExpr(); |
| 125 | expect(")"); |
Rui Ueyama | 960504b | 2016-04-19 18:58:11 +0000 | [diff] [blame] | 126 | return V; |
| 127 | } |
George Rimar | dffc141 | 2016-04-22 11:40:53 +0000 | [diff] [blame] | 128 | if (Tok == "ALIGN") { |
Rui Ueyama | 9c1112d | 2016-04-23 00:04:03 +0000 | [diff] [blame] | 129 | expect("("); |
| 130 | uint64_t V = parseExpr(); |
| 131 | expect(")"); |
George Rimar | dffc141 | 2016-04-22 11:40:53 +0000 | [diff] [blame] | 132 | return alignTo(Dot, V); |
| 133 | } |
George Rimar | 92e93fb | 2016-07-21 19:48:00 +0000 | [diff] [blame] | 134 | if (Tok == "CONSTANT") { |
| 135 | expect("("); |
| 136 | uint64_t V = getConstantValue(next()); |
| 137 | expect(")"); |
| 138 | return V; |
| 139 | } |
| 140 | // Documentations says there are two ways to compute |
| 141 | // the value of DATA_SEGMENT_ALIGN command, depending on whether the second |
| 142 | // uses fewer COMMONPAGESIZE sized pages for the data segment(area between the |
| 143 | // result of this expression and `DATA_SEGMENT_END') than the first or not. |
| 144 | // That is possible optimization, that we do not support, so we compute that |
| 145 | // function always as (ALIGN(MAXPAGESIZE) + (. & (MAXPAGESIZE - 1))) now. |
| 146 | if (Tok == "DATA_SEGMENT_ALIGN") { |
| 147 | expect("("); |
| 148 | uint64_t L = parseExpr(); |
| 149 | expect(","); |
| 150 | parseExpr(); |
| 151 | expect(")"); |
| 152 | return alignTo(Dot, L) + (Dot & (L - 1)); |
| 153 | } |
| 154 | // Since we do not support the optimization from comment above, |
| 155 | // we can just ignore that command. |
| 156 | if (Tok == "DATA_SEGMENT_END") { |
| 157 | expect("("); |
| 158 | expect("."); |
| 159 | expect(")"); |
| 160 | return Dot; |
| 161 | } |
Rui Ueyama | 5fa6098 | 2016-04-22 21:05:04 +0000 | [diff] [blame] | 162 | uint64_t V = 0; |
| 163 | if (Tok.getAsInteger(0, V)) |
Rui Ueyama | 9c1112d | 2016-04-23 00:04:03 +0000 | [diff] [blame] | 164 | setError("malformed number: " + Tok); |
Rui Ueyama | 5fa6098 | 2016-04-22 21:05:04 +0000 | [diff] [blame] | 165 | return V; |
Rui Ueyama | 960504b | 2016-04-19 18:58:11 +0000 | [diff] [blame] | 166 | } |
| 167 | |
Rui Ueyama | 9c1112d | 2016-04-23 00:04:03 +0000 | [diff] [blame] | 168 | uint64_t ExprParser::parseTernary(uint64_t Cond) { |
| 169 | next(); |
| 170 | uint64_t V = parseExpr(); |
| 171 | expect(":"); |
| 172 | uint64_t W = parseExpr(); |
George Rimar | fba45c4 | 2016-04-22 11:28:54 +0000 | [diff] [blame] | 173 | return Cond ? V : W; |
| 174 | } |
| 175 | |
Rui Ueyama | 9c1112d | 2016-04-23 00:04:03 +0000 | [diff] [blame] | 176 | uint64_t ExprParser::apply(StringRef Op, uint64_t L, uint64_t R) { |
Rui Ueyama | 960504b | 2016-04-19 18:58:11 +0000 | [diff] [blame] | 177 | if (Op == "*") |
| 178 | return L * R; |
| 179 | if (Op == "/") { |
| 180 | if (R == 0) { |
| 181 | error("division by zero"); |
George Rimar | 652852c | 2016-04-16 10:10:32 +0000 | [diff] [blame] | 182 | return 0; |
| 183 | } |
Rui Ueyama | 960504b | 2016-04-19 18:58:11 +0000 | [diff] [blame] | 184 | return L / R; |
George Rimar | 652852c | 2016-04-16 10:10:32 +0000 | [diff] [blame] | 185 | } |
George Rimar | ab93906 | 2016-04-25 08:14:41 +0000 | [diff] [blame] | 186 | if (Op == "+") |
| 187 | return L + R; |
| 188 | if (Op == "-") |
| 189 | return L - R; |
| 190 | if (Op == "<") |
| 191 | return L < R; |
| 192 | if (Op == ">") |
| 193 | return L > R; |
| 194 | if (Op == ">=") |
| 195 | return L >= R; |
| 196 | if (Op == "<=") |
| 197 | return L <= R; |
| 198 | if (Op == "==") |
| 199 | return L == R; |
| 200 | if (Op == "!=") |
| 201 | return L != R; |
Rui Ueyama | 960504b | 2016-04-19 18:58:11 +0000 | [diff] [blame] | 202 | if (Op == "&") |
| 203 | return L & R; |
Rui Ueyama | 7a81d67 | 2016-04-19 19:04:03 +0000 | [diff] [blame] | 204 | llvm_unreachable("invalid operator"); |
Rui Ueyama | 960504b | 2016-04-19 18:58:11 +0000 | [diff] [blame] | 205 | } |
| 206 | |
Rui Ueyama | 9c1112d | 2016-04-23 00:04:03 +0000 | [diff] [blame] | 207 | // This is a part of the operator-precedence parser. |
| 208 | // This function assumes that the remaining token stream starts |
| 209 | // with an operator. |
| 210 | uint64_t ExprParser::parseExpr1(uint64_t Lhs, int MinPrec) { |
| 211 | while (!atEOF()) { |
Rui Ueyama | 960504b | 2016-04-19 18:58:11 +0000 | [diff] [blame] | 212 | // Read an operator and an expression. |
Rui Ueyama | 9c1112d | 2016-04-23 00:04:03 +0000 | [diff] [blame] | 213 | StringRef Op1 = peek(); |
George Rimar | fba45c4 | 2016-04-22 11:28:54 +0000 | [diff] [blame] | 214 | if (Op1 == "?") |
Rui Ueyama | 9c1112d | 2016-04-23 00:04:03 +0000 | [diff] [blame] | 215 | return parseTernary(Lhs); |
Rui Ueyama | 960504b | 2016-04-19 18:58:11 +0000 | [diff] [blame] | 216 | if (precedence(Op1) < MinPrec) |
| 217 | return Lhs; |
Rui Ueyama | 9c1112d | 2016-04-23 00:04:03 +0000 | [diff] [blame] | 218 | next(); |
| 219 | uint64_t Rhs = parsePrimary(); |
Rui Ueyama | 960504b | 2016-04-19 18:58:11 +0000 | [diff] [blame] | 220 | |
| 221 | // Evaluate the remaining part of the expression first if the |
| 222 | // next operator has greater precedence than the previous one. |
| 223 | // For example, if we have read "+" and "3", and if the next |
| 224 | // operator is "*", then we'll evaluate 3 * ... part first. |
Rui Ueyama | 9c1112d | 2016-04-23 00:04:03 +0000 | [diff] [blame] | 225 | while (!atEOF()) { |
| 226 | StringRef Op2 = peek(); |
Rui Ueyama | 960504b | 2016-04-19 18:58:11 +0000 | [diff] [blame] | 227 | if (precedence(Op2) <= precedence(Op1)) |
| 228 | break; |
Rui Ueyama | 9c1112d | 2016-04-23 00:04:03 +0000 | [diff] [blame] | 229 | Rhs = parseExpr1(Rhs, precedence(Op2)); |
Rui Ueyama | 960504b | 2016-04-19 18:58:11 +0000 | [diff] [blame] | 230 | } |
| 231 | |
| 232 | Lhs = apply(Op1, Lhs, Rhs); |
| 233 | } |
| 234 | return Lhs; |
| 235 | } |
| 236 | |
Rui Ueyama | 9c1112d | 2016-04-23 00:04:03 +0000 | [diff] [blame] | 237 | // Reads and evaluates an arithmetic expression. |
| 238 | uint64_t ExprParser::parseExpr() { return parseExpr1(parsePrimary(), 0); } |
George Rimar | 652852c | 2016-04-16 10:10:32 +0000 | [diff] [blame] | 239 | |
Rui Ueyama | 36a153c | 2016-07-23 14:09:58 +0000 | [diff] [blame] | 240 | template <class ELFT> static bool isDiscarded(InputSectionBase<ELFT> *S) { |
George Rimar | eea3114 | 2016-07-21 14:26:59 +0000 | [diff] [blame] | 241 | return !S || !S->Live; |
Rui Ueyama | 717677a | 2016-02-11 21:17:59 +0000 | [diff] [blame] | 242 | } |
| 243 | |
Rui Ueyama | 07320e4 | 2016-04-20 20:13:41 +0000 | [diff] [blame] | 244 | template <class ELFT> |
| 245 | bool LinkerScript<ELFT>::shouldKeep(InputSectionBase<ELFT> *S) { |
Rui Ueyama | 8ec77e6 | 2016-04-21 22:00:51 +0000 | [diff] [blame] | 246 | for (StringRef Pat : Opt.KeptSections) |
Rui Ueyama | 722830a | 2016-06-29 05:32:09 +0000 | [diff] [blame] | 247 | if (globMatch(Pat, S->getSectionName())) |
Rui Ueyama | 8ec77e6 | 2016-04-21 22:00:51 +0000 | [diff] [blame] | 248 | return true; |
| 249 | return false; |
George Rimar | 481c2ce | 2016-02-23 07:47:54 +0000 | [diff] [blame] | 250 | } |
| 251 | |
George Rimar | eea3114 | 2016-07-21 14:26:59 +0000 | [diff] [blame] | 252 | static bool match(StringRef Pattern, ArrayRef<StringRef> Arr) { |
| 253 | for (StringRef S : Arr) |
| 254 | if (globMatch(S, Pattern)) |
| 255 | return true; |
| 256 | return false; |
| 257 | } |
| 258 | |
George Rimar | 652852c | 2016-04-16 10:10:32 +0000 | [diff] [blame] | 259 | template <class ELFT> |
Rui Ueyama | a7f7884 | 2016-07-20 17:19:03 +0000 | [diff] [blame] | 260 | std::vector<OutputSectionBase<ELFT> *> |
Eugene Leviant | e63d81b | 2016-07-20 14:43:20 +0000 | [diff] [blame] | 261 | LinkerScript<ELFT>::createSections(OutputSectionFactory<ELFT> &Factory) { |
George Rimar | eea3114 | 2016-07-21 14:26:59 +0000 | [diff] [blame] | 262 | typedef const std::unique_ptr<ObjectFile<ELFT>> ObjectFile; |
Rui Ueyama | a7f7884 | 2016-07-20 17:19:03 +0000 | [diff] [blame] | 263 | std::vector<OutputSectionBase<ELFT> *> Result; |
Davide Italiano | 246f681 | 2016-07-22 03:36:24 +0000 | [diff] [blame] | 264 | DenseSet<OutputSectionBase<ELFT> *> Removed; |
Rui Ueyama | a7f7884 | 2016-07-20 17:19:03 +0000 | [diff] [blame] | 265 | |
Eugene Leviant | e63d81b | 2016-07-20 14:43:20 +0000 | [diff] [blame] | 266 | // Add input section to output section. If there is no output section yet, |
| 267 | // then create it and add to output section list. |
Davide Italiano | 246f681 | 2016-07-22 03:36:24 +0000 | [diff] [blame] | 268 | auto AddInputSec = [&](InputSectionBase<ELFT> *C, StringRef Name, |
| 269 | ConstraintKind Constraint) { |
Eugene Leviant | e63d81b | 2016-07-20 14:43:20 +0000 | [diff] [blame] | 270 | OutputSectionBase<ELFT> *Sec; |
| 271 | bool IsNew; |
| 272 | std::tie(Sec, IsNew) = Factory.create(C, Name); |
| 273 | if (IsNew) |
Rui Ueyama | a7f7884 | 2016-07-20 17:19:03 +0000 | [diff] [blame] | 274 | Result.push_back(Sec); |
Davide Italiano | 246f681 | 2016-07-22 03:36:24 +0000 | [diff] [blame] | 275 | if ((!(C->getSectionHdr()->sh_flags & SHF_WRITE)) && |
| 276 | Constraint == ReadWrite) { |
| 277 | Removed.insert(Sec); |
| 278 | return; |
| 279 | } |
| 280 | if ((C->getSectionHdr()->sh_flags & SHF_WRITE) && Constraint == ReadOnly) { |
| 281 | Removed.insert(Sec); |
| 282 | return; |
| 283 | } |
Eugene Leviant | e63d81b | 2016-07-20 14:43:20 +0000 | [diff] [blame] | 284 | Sec->addSection(C); |
| 285 | }; |
| 286 | |
| 287 | // Select input sections matching rule and add them to corresponding |
| 288 | // output section. Section rules are processed in order they're listed |
| 289 | // in script, so correct input section order is maintained by design. |
George Rimar | eea3114 | 2016-07-21 14:26:59 +0000 | [diff] [blame] | 290 | for (const std::unique_ptr<BaseCommand> &Base : Opt.Commands) { |
| 291 | auto *OutCmd = dyn_cast<OutputSectionCommand>(Base.get()); |
| 292 | if (!OutCmd) |
| 293 | continue; |
| 294 | |
| 295 | for (const std::unique_ptr<BaseCommand> &Cmd : OutCmd->Commands) { |
| 296 | auto *InCmd = dyn_cast<InputSectionDescription>(Cmd.get()); |
| 297 | if (!InCmd) |
| 298 | continue; |
| 299 | |
| 300 | for (ObjectFile &F : Symtab<ELFT>::X->getObjectFiles()) { |
| 301 | for (InputSectionBase<ELFT> *S : F->getSections()) { |
| 302 | if (isDiscarded(S) || S->OutSec) |
| 303 | continue; |
| 304 | |
| 305 | if (match(S->getSectionName(), InCmd->Patterns)) { |
| 306 | if (OutCmd->Name == "/DISCARD/") |
| 307 | S->Live = false; |
| 308 | else |
Davide Italiano | 246f681 | 2016-07-22 03:36:24 +0000 | [diff] [blame] | 309 | AddInputSec(S, OutCmd->Name, OutCmd->Constraint); |
George Rimar | eea3114 | 2016-07-21 14:26:59 +0000 | [diff] [blame] | 310 | } |
| 311 | } |
| 312 | } |
| 313 | } |
| 314 | } |
Eugene Leviant | e63d81b | 2016-07-20 14:43:20 +0000 | [diff] [blame] | 315 | |
| 316 | // Add all other input sections, which are not listed in script. |
George Rimar | eea3114 | 2016-07-21 14:26:59 +0000 | [diff] [blame] | 317 | for (ObjectFile &F : Symtab<ELFT>::X->getObjectFiles()) |
Reid Kleckner | 3c944ec | 2016-07-21 18:39:28 +0000 | [diff] [blame] | 318 | for (InputSectionBase<ELFT> *S : F->getSections()) { |
Eugene Leviant | e63d81b | 2016-07-20 14:43:20 +0000 | [diff] [blame] | 319 | if (!isDiscarded(S)) { |
| 320 | if (!S->OutSec) |
Davide Italiano | 246f681 | 2016-07-22 03:36:24 +0000 | [diff] [blame] | 321 | AddInputSec(S, getOutputSectionName(S), NoConstraint); |
Eugene Leviant | e63d81b | 2016-07-20 14:43:20 +0000 | [diff] [blame] | 322 | } else |
| 323 | reportDiscarded(S, F); |
Reid Kleckner | 3c944ec | 2016-07-21 18:39:28 +0000 | [diff] [blame] | 324 | } |
Eugene Leviant | e63d81b | 2016-07-20 14:43:20 +0000 | [diff] [blame] | 325 | |
Davide Italiano | 246f681 | 2016-07-22 03:36:24 +0000 | [diff] [blame] | 326 | // Remove from the output all the sections which did not met the constraints. |
| 327 | Result.erase(std::remove_if(Result.begin(), Result.end(), |
| 328 | [&](OutputSectionBase<ELFT> *Sec) { |
| 329 | return Removed.count(Sec); |
| 330 | }), |
| 331 | Result.end()); |
Eugene Leviant | e63d81b | 2016-07-20 14:43:20 +0000 | [diff] [blame] | 332 | return Result; |
| 333 | } |
| 334 | |
| 335 | template <class ELFT> |
George Rimar | 10e576e | 2016-07-21 16:07:40 +0000 | [diff] [blame] | 336 | void LinkerScript<ELFT>::dispatchAssignment(SymbolAssignment *Cmd) { |
| 337 | uint64_t Val = evalExpr(Cmd->Expr, Dot); |
| 338 | if (Cmd->Name == ".") { |
| 339 | Dot = Val; |
Eugene Leviant | a31c91b | 2016-07-22 07:38:40 +0000 | [diff] [blame] | 340 | } else if (!Cmd->Ignore) { |
George Rimar | 10e576e | 2016-07-21 16:07:40 +0000 | [diff] [blame] | 341 | auto *D = cast<DefinedRegular<ELFT>>(Symtab<ELFT>::X->find(Cmd->Name)); |
| 342 | D->Value = Val; |
| 343 | } |
| 344 | } |
| 345 | |
| 346 | template <class ELFT> |
Rui Ueyama | 07320e4 | 2016-04-20 20:13:41 +0000 | [diff] [blame] | 347 | void LinkerScript<ELFT>::assignAddresses( |
George Rimar | dbbd8b1 | 2016-04-21 11:21:48 +0000 | [diff] [blame] | 348 | ArrayRef<OutputSectionBase<ELFT> *> Sections) { |
George Rimar | 652852c | 2016-04-16 10:10:32 +0000 | [diff] [blame] | 349 | // Orphan sections are sections present in the input files which |
Rui Ueyama | 7c18c28 | 2016-04-18 21:00:40 +0000 | [diff] [blame] | 350 | // are not explicitly placed into the output file by the linker script. |
| 351 | // We place orphan sections at end of file. |
| 352 | // Other linkers places them using some heuristics as described in |
George Rimar | 652852c | 2016-04-16 10:10:32 +0000 | [diff] [blame] | 353 | // https://sourceware.org/binutils/docs/ld/Orphan-Sections.html#Orphan-Sections. |
Rui Ueyama | 7c18c28 | 2016-04-18 21:00:40 +0000 | [diff] [blame] | 354 | for (OutputSectionBase<ELFT> *Sec : Sections) { |
George Rimar | 652852c | 2016-04-16 10:10:32 +0000 | [diff] [blame] | 355 | StringRef Name = Sec->getName(); |
Rui Ueyama | c3e2a4b | 2016-04-21 20:30:00 +0000 | [diff] [blame] | 356 | if (getSectionIndex(Name) == INT_MAX) |
George Rimar | 076fe15 | 2016-07-21 06:43:01 +0000 | [diff] [blame] | 357 | Opt.Commands.push_back(llvm::make_unique<OutputSectionCommand>(Name)); |
George Rimar | 652852c | 2016-04-16 10:10:32 +0000 | [diff] [blame] | 358 | } |
George Rimar | 652852c | 2016-04-16 10:10:32 +0000 | [diff] [blame] | 359 | |
Rui Ueyama | 7c18c28 | 2016-04-18 21:00:40 +0000 | [diff] [blame] | 360 | // Assign addresses as instructed by linker script SECTIONS sub-commands. |
Rui Ueyama | 52c4e17 | 2016-07-01 10:42:25 +0000 | [diff] [blame] | 361 | Dot = Out<ELFT>::ElfHeader->getSize() + Out<ELFT>::ProgramHeaders->getSize(); |
Eugene Leviant | 467c4d5 | 2016-07-01 10:27:36 +0000 | [diff] [blame] | 362 | uintX_t MinVA = std::numeric_limits<uintX_t>::max(); |
George Rimar | 652852c | 2016-04-16 10:10:32 +0000 | [diff] [blame] | 363 | uintX_t ThreadBssOffset = 0; |
George Rimar | 652852c | 2016-04-16 10:10:32 +0000 | [diff] [blame] | 364 | |
George Rimar | 076fe15 | 2016-07-21 06:43:01 +0000 | [diff] [blame] | 365 | for (const std::unique_ptr<BaseCommand> &Base : Opt.Commands) { |
| 366 | if (auto *Cmd = dyn_cast<SymbolAssignment>(Base.get())) { |
George Rimar | 10e576e | 2016-07-21 16:07:40 +0000 | [diff] [blame] | 367 | dispatchAssignment(Cmd); |
George Rimar | 652852c | 2016-04-16 10:10:32 +0000 | [diff] [blame] | 368 | continue; |
| 369 | } |
| 370 | |
Dima Stepanov | fb8978f | 2016-05-19 18:15:54 +0000 | [diff] [blame] | 371 | // Find all the sections with required name. There can be more than |
George Rimar | 6ad330a | 2016-07-19 07:39:07 +0000 | [diff] [blame] | 372 | // one section with such name, if the alignment, flags or type |
Dima Stepanov | fb8978f | 2016-05-19 18:15:54 +0000 | [diff] [blame] | 373 | // attribute differs. |
George Rimar | 076fe15 | 2016-07-21 06:43:01 +0000 | [diff] [blame] | 374 | auto *Cmd = cast<OutputSectionCommand>(Base.get()); |
Dima Stepanov | fb8978f | 2016-05-19 18:15:54 +0000 | [diff] [blame] | 375 | for (OutputSectionBase<ELFT> *Sec : Sections) { |
George Rimar | 076fe15 | 2016-07-21 06:43:01 +0000 | [diff] [blame] | 376 | if (Sec->getName() != Cmd->Name) |
Dima Stepanov | fb8978f | 2016-05-19 18:15:54 +0000 | [diff] [blame] | 377 | continue; |
George Rimar | 652852c | 2016-04-16 10:10:32 +0000 | [diff] [blame] | 378 | |
Dima Stepanov | fb8978f | 2016-05-19 18:15:54 +0000 | [diff] [blame] | 379 | if ((Sec->getFlags() & SHF_TLS) && Sec->getType() == SHT_NOBITS) { |
| 380 | uintX_t TVA = Dot + ThreadBssOffset; |
Rui Ueyama | 424b408 | 2016-06-17 01:18:46 +0000 | [diff] [blame] | 381 | TVA = alignTo(TVA, Sec->getAlignment()); |
Dima Stepanov | fb8978f | 2016-05-19 18:15:54 +0000 | [diff] [blame] | 382 | Sec->setVA(TVA); |
| 383 | ThreadBssOffset = TVA - Dot + Sec->getSize(); |
| 384 | continue; |
| 385 | } |
George Rimar | 652852c | 2016-04-16 10:10:32 +0000 | [diff] [blame] | 386 | |
Dima Stepanov | fb8978f | 2016-05-19 18:15:54 +0000 | [diff] [blame] | 387 | if (Sec->getFlags() & SHF_ALLOC) { |
Rui Ueyama | 424b408 | 2016-06-17 01:18:46 +0000 | [diff] [blame] | 388 | Dot = alignTo(Dot, Sec->getAlignment()); |
Dima Stepanov | fb8978f | 2016-05-19 18:15:54 +0000 | [diff] [blame] | 389 | Sec->setVA(Dot); |
Rui Ueyama | 52c4e17 | 2016-07-01 10:42:25 +0000 | [diff] [blame] | 390 | MinVA = std::min(MinVA, Dot); |
Dima Stepanov | fb8978f | 2016-05-19 18:15:54 +0000 | [diff] [blame] | 391 | Dot += Sec->getSize(); |
| 392 | continue; |
| 393 | } |
George Rimar | 652852c | 2016-04-16 10:10:32 +0000 | [diff] [blame] | 394 | } |
| 395 | } |
Rui Ueyama | 52c4e17 | 2016-07-01 10:42:25 +0000 | [diff] [blame] | 396 | |
Rafael Espindola | 64c32d6 | 2016-07-07 14:28:47 +0000 | [diff] [blame] | 397 | // ELF and Program headers need to be right before the first section in |
George Rimar | b91e711 | 2016-07-19 07:42:07 +0000 | [diff] [blame] | 398 | // memory. Set their addresses accordingly. |
Eugene Leviant | 467c4d5 | 2016-07-01 10:27:36 +0000 | [diff] [blame] | 399 | MinVA = alignDown(MinVA - Out<ELFT>::ElfHeader->getSize() - |
| 400 | Out<ELFT>::ProgramHeaders->getSize(), |
| 401 | Target->PageSize); |
| 402 | Out<ELFT>::ElfHeader->setVA(MinVA); |
| 403 | Out<ELFT>::ProgramHeaders->setVA(Out<ELFT>::ElfHeader->getSize() + MinVA); |
George Rimar | 652852c | 2016-04-16 10:10:32 +0000 | [diff] [blame] | 404 | } |
| 405 | |
Rui Ueyama | 07320e4 | 2016-04-20 20:13:41 +0000 | [diff] [blame] | 406 | template <class ELFT> |
Rafael Espindola | 74df5c7 | 2016-07-19 12:33:46 +0000 | [diff] [blame] | 407 | std::vector<PhdrEntry<ELFT>> |
Eugene Leviant | bbe3860 | 2016-07-19 09:25:43 +0000 | [diff] [blame] | 408 | LinkerScript<ELFT>::createPhdrs(ArrayRef<OutputSectionBase<ELFT> *> Sections) { |
| 409 | int TlsNum = -1; |
| 410 | int NoteNum = -1; |
| 411 | int RelroNum = -1; |
Rui Ueyama | adca245 | 2016-07-23 14:18:48 +0000 | [diff] [blame] | 412 | PhdrEntry<ELFT> *Load = nullptr; |
Eugene Leviant | bbe3860 | 2016-07-19 09:25:43 +0000 | [diff] [blame] | 413 | uintX_t Flags = PF_R; |
Rui Ueyama | adca245 | 2016-07-23 14:18:48 +0000 | [diff] [blame] | 414 | std::vector<PhdrEntry<ELFT>> Phdrs; |
Eugene Leviant | bbe3860 | 2016-07-19 09:25:43 +0000 | [diff] [blame] | 415 | |
| 416 | for (const PhdrsCommand &Cmd : Opt.PhdrsCommands) { |
Eugene Leviant | 865bf86 | 2016-07-21 10:43:25 +0000 | [diff] [blame] | 417 | Phdrs.emplace_back(Cmd.Type, Cmd.Flags == UINT_MAX ? PF_R : Cmd.Flags); |
Rui Ueyama | adca245 | 2016-07-23 14:18:48 +0000 | [diff] [blame] | 418 | PhdrEntry<ELFT> &Phdr = Phdrs.back(); |
Eugene Leviant | bbe3860 | 2016-07-19 09:25:43 +0000 | [diff] [blame] | 419 | |
| 420 | if (Cmd.HasFilehdr) |
Rui Ueyama | adca245 | 2016-07-23 14:18:48 +0000 | [diff] [blame] | 421 | Phdr.add(Out<ELFT>::ElfHeader); |
Eugene Leviant | bbe3860 | 2016-07-19 09:25:43 +0000 | [diff] [blame] | 422 | if (Cmd.HasPhdrs) |
Rui Ueyama | adca245 | 2016-07-23 14:18:48 +0000 | [diff] [blame] | 423 | Phdr.add(Out<ELFT>::ProgramHeaders); |
Eugene Leviant | bbe3860 | 2016-07-19 09:25:43 +0000 | [diff] [blame] | 424 | |
| 425 | switch (Cmd.Type) { |
| 426 | case PT_INTERP: |
Rui Ueyama | fd03cfd | 2016-07-21 11:01:23 +0000 | [diff] [blame] | 427 | if (Out<ELFT>::Interp) |
Rui Ueyama | adca245 | 2016-07-23 14:18:48 +0000 | [diff] [blame] | 428 | Phdr.add(Out<ELFT>::Interp); |
Eugene Leviant | bbe3860 | 2016-07-19 09:25:43 +0000 | [diff] [blame] | 429 | break; |
| 430 | case PT_DYNAMIC: |
| 431 | if (isOutputDynamic<ELFT>()) { |
Rui Ueyama | adca245 | 2016-07-23 14:18:48 +0000 | [diff] [blame] | 432 | Phdr.H.p_flags = toPhdrFlags(Out<ELFT>::Dynamic->getFlags()); |
| 433 | Phdr.add(Out<ELFT>::Dynamic); |
Eugene Leviant | bbe3860 | 2016-07-19 09:25:43 +0000 | [diff] [blame] | 434 | } |
| 435 | break; |
| 436 | case PT_TLS: |
| 437 | TlsNum = Phdrs.size() - 1; |
| 438 | break; |
| 439 | case PT_NOTE: |
| 440 | NoteNum = Phdrs.size() - 1; |
| 441 | break; |
| 442 | case PT_GNU_RELRO: |
| 443 | RelroNum = Phdrs.size() - 1; |
| 444 | break; |
| 445 | case PT_GNU_EH_FRAME: |
| 446 | if (!Out<ELFT>::EhFrame->empty() && Out<ELFT>::EhFrameHdr) { |
Rui Ueyama | adca245 | 2016-07-23 14:18:48 +0000 | [diff] [blame] | 447 | Phdr.H.p_flags = toPhdrFlags(Out<ELFT>::EhFrameHdr->getFlags()); |
| 448 | Phdr.add(Out<ELFT>::EhFrameHdr); |
Eugene Leviant | bbe3860 | 2016-07-19 09:25:43 +0000 | [diff] [blame] | 449 | } |
| 450 | break; |
| 451 | } |
| 452 | } |
| 453 | |
| 454 | for (OutputSectionBase<ELFT> *Sec : Sections) { |
| 455 | if (!(Sec->getFlags() & SHF_ALLOC)) |
| 456 | break; |
| 457 | |
| 458 | if (TlsNum != -1 && (Sec->getFlags() & SHF_TLS)) |
Rui Ueyama | 18f084f | 2016-07-20 19:36:41 +0000 | [diff] [blame] | 459 | Phdrs[TlsNum].add(Sec); |
Eugene Leviant | bbe3860 | 2016-07-19 09:25:43 +0000 | [diff] [blame] | 460 | |
| 461 | if (!needsPtLoad<ELFT>(Sec)) |
| 462 | continue; |
| 463 | |
| 464 | const std::vector<size_t> &PhdrIds = |
| 465 | getPhdrIndicesForSection(Sec->getName()); |
| 466 | if (!PhdrIds.empty()) { |
| 467 | // Assign headers specified by linker script |
| 468 | for (size_t Id : PhdrIds) { |
Rui Ueyama | 18f084f | 2016-07-20 19:36:41 +0000 | [diff] [blame] | 469 | Phdrs[Id].add(Sec); |
Eugene Leviant | 865bf86 | 2016-07-21 10:43:25 +0000 | [diff] [blame] | 470 | if (Opt.PhdrsCommands[Id].Flags == UINT_MAX) |
| 471 | Phdrs[Id].H.p_flags |= toPhdrFlags(Sec->getFlags()); |
Eugene Leviant | bbe3860 | 2016-07-19 09:25:43 +0000 | [diff] [blame] | 472 | } |
| 473 | } else { |
| 474 | // If we have no load segment or flags've changed then we want new load |
| 475 | // segment. |
| 476 | uintX_t NewFlags = toPhdrFlags(Sec->getFlags()); |
| 477 | if (Load == nullptr || Flags != NewFlags) { |
| 478 | Load = &*Phdrs.emplace(Phdrs.end(), PT_LOAD, NewFlags); |
| 479 | Flags = NewFlags; |
| 480 | } |
Rui Ueyama | 18f084f | 2016-07-20 19:36:41 +0000 | [diff] [blame] | 481 | Load->add(Sec); |
Eugene Leviant | bbe3860 | 2016-07-19 09:25:43 +0000 | [diff] [blame] | 482 | } |
| 483 | |
| 484 | if (RelroNum != -1 && isRelroSection(Sec)) |
Rui Ueyama | 18f084f | 2016-07-20 19:36:41 +0000 | [diff] [blame] | 485 | Phdrs[RelroNum].add(Sec); |
Eugene Leviant | bbe3860 | 2016-07-19 09:25:43 +0000 | [diff] [blame] | 486 | if (NoteNum != -1 && Sec->getType() == SHT_NOTE) |
Rui Ueyama | 18f084f | 2016-07-20 19:36:41 +0000 | [diff] [blame] | 487 | Phdrs[NoteNum].add(Sec); |
Eugene Leviant | bbe3860 | 2016-07-19 09:25:43 +0000 | [diff] [blame] | 488 | } |
| 489 | return Phdrs; |
| 490 | } |
| 491 | |
| 492 | template <class ELFT> |
Rui Ueyama | 07320e4 | 2016-04-20 20:13:41 +0000 | [diff] [blame] | 493 | ArrayRef<uint8_t> LinkerScript<ELFT>::getFiller(StringRef Name) { |
George Rimar | f6c3cce | 2016-07-21 07:48:54 +0000 | [diff] [blame] | 494 | for (const std::unique_ptr<BaseCommand> &Base : Opt.Commands) |
| 495 | if (auto *Cmd = dyn_cast<OutputSectionCommand>(Base.get())) |
| 496 | if (Cmd->Name == Name) |
| 497 | return Cmd->Filler; |
| 498 | return {}; |
George Rimar | e2ee72b | 2016-02-26 14:48:31 +0000 | [diff] [blame] | 499 | } |
| 500 | |
Rui Ueyama | c3e2a4b | 2016-04-21 20:30:00 +0000 | [diff] [blame] | 501 | // Returns the index of the given section name in linker script |
| 502 | // SECTIONS commands. Sections are laid out as the same order as they |
| 503 | // were in the script. If a given name did not appear in the script, |
| 504 | // it returns INT_MAX, so that it will be laid out at end of file. |
George Rimar | 076fe15 | 2016-07-21 06:43:01 +0000 | [diff] [blame] | 505 | template <class ELFT> int LinkerScript<ELFT>::getSectionIndex(StringRef Name) { |
George Rimar | 71b26e9 | 2016-04-21 10:22:02 +0000 | [diff] [blame] | 506 | auto Begin = Opt.Commands.begin(); |
| 507 | auto End = Opt.Commands.end(); |
George Rimar | 076fe15 | 2016-07-21 06:43:01 +0000 | [diff] [blame] | 508 | auto I = |
| 509 | std::find_if(Begin, End, [&](const std::unique_ptr<BaseCommand> &Base) { |
| 510 | if (auto *Cmd = dyn_cast<OutputSectionCommand>(Base.get())) |
| 511 | if (Cmd->Name == Name) |
| 512 | return true; |
| 513 | return false; |
| 514 | }); |
Rui Ueyama | c3e2a4b | 2016-04-21 20:30:00 +0000 | [diff] [blame] | 515 | return I == End ? INT_MAX : (I - Begin); |
George Rimar | 71b26e9 | 2016-04-21 10:22:02 +0000 | [diff] [blame] | 516 | } |
| 517 | |
| 518 | // A compartor to sort output sections. Returns -1 or 1 if |
| 519 | // A or B are mentioned in linker script. Otherwise, returns 0. |
Rui Ueyama | 07320e4 | 2016-04-20 20:13:41 +0000 | [diff] [blame] | 520 | template <class ELFT> |
| 521 | int LinkerScript<ELFT>::compareSections(StringRef A, StringRef B) { |
Rui Ueyama | c3e2a4b | 2016-04-21 20:30:00 +0000 | [diff] [blame] | 522 | int I = getSectionIndex(A); |
| 523 | int J = getSectionIndex(B); |
| 524 | if (I == INT_MAX && J == INT_MAX) |
Rui Ueyama | 717677a | 2016-02-11 21:17:59 +0000 | [diff] [blame] | 525 | return 0; |
| 526 | return I < J ? -1 : 1; |
| 527 | } |
| 528 | |
George Rimar | 076fe15 | 2016-07-21 06:43:01 +0000 | [diff] [blame] | 529 | template <class ELFT> void LinkerScript<ELFT>::addScriptedSymbols() { |
Eugene Leviant | a31c91b | 2016-07-22 07:38:40 +0000 | [diff] [blame] | 530 | for (const std::unique_ptr<BaseCommand> &Base : Opt.Commands) { |
| 531 | auto *Cmd = dyn_cast<SymbolAssignment>(Base.get()); |
| 532 | if (!Cmd || Cmd->Name == ".") |
| 533 | continue; |
| 534 | |
| 535 | if (Symtab<ELFT>::X->find(Cmd->Name) == nullptr) |
| 536 | Symtab<ELFT>::X->addAbsolute(Cmd->Name, |
| 537 | Cmd->Hidden ? STV_HIDDEN : STV_DEFAULT); |
| 538 | else |
| 539 | // Symbol already exists in symbol table. If it is provided |
| 540 | // then we can't override its value. |
| 541 | Cmd->Ignore = Cmd->Provide; |
| 542 | } |
Eugene Leviant | eda81a1 | 2016-07-12 06:39:48 +0000 | [diff] [blame] | 543 | } |
| 544 | |
Eugene Leviant | bbe3860 | 2016-07-19 09:25:43 +0000 | [diff] [blame] | 545 | template <class ELFT> bool LinkerScript<ELFT>::hasPhdrsCommands() { |
| 546 | return !Opt.PhdrsCommands.empty(); |
| 547 | } |
| 548 | |
| 549 | // Returns indices of ELF headers containing specific section, identified |
| 550 | // by Name. Each index is a zero based number of ELF header listed within |
| 551 | // PHDRS {} script block. |
| 552 | template <class ELFT> |
| 553 | std::vector<size_t> |
| 554 | LinkerScript<ELFT>::getPhdrIndicesForSection(StringRef Name) { |
George Rimar | 076fe15 | 2016-07-21 06:43:01 +0000 | [diff] [blame] | 555 | for (const std::unique_ptr<BaseCommand> &Base : Opt.Commands) { |
| 556 | auto *Cmd = dyn_cast<OutputSectionCommand>(Base.get()); |
| 557 | if (!Cmd || Cmd->Name != Name) |
George Rimar | 31d842f | 2016-07-20 16:43:03 +0000 | [diff] [blame] | 558 | continue; |
| 559 | |
| 560 | std::vector<size_t> Indices; |
George Rimar | 076fe15 | 2016-07-21 06:43:01 +0000 | [diff] [blame] | 561 | for (StringRef PhdrName : Cmd->Phdrs) { |
George Rimar | 31d842f | 2016-07-20 16:43:03 +0000 | [diff] [blame] | 562 | auto ItPhdr = |
| 563 | std::find_if(Opt.PhdrsCommands.rbegin(), Opt.PhdrsCommands.rend(), |
George Rimar | 076fe15 | 2016-07-21 06:43:01 +0000 | [diff] [blame] | 564 | [&](PhdrsCommand &P) { return P.Name == PhdrName; }); |
Eugene Leviant | bbe3860 | 2016-07-19 09:25:43 +0000 | [diff] [blame] | 565 | if (ItPhdr == Opt.PhdrsCommands.rend()) |
| 566 | error("section header '" + PhdrName + "' is not listed in PHDRS"); |
| 567 | else |
| 568 | Indices.push_back(std::distance(ItPhdr, Opt.PhdrsCommands.rend()) - 1); |
| 569 | } |
George Rimar | 31d842f | 2016-07-20 16:43:03 +0000 | [diff] [blame] | 570 | return Indices; |
Eugene Leviant | bbe3860 | 2016-07-19 09:25:43 +0000 | [diff] [blame] | 571 | } |
George Rimar | 31d842f | 2016-07-20 16:43:03 +0000 | [diff] [blame] | 572 | return {}; |
Eugene Leviant | bbe3860 | 2016-07-19 09:25:43 +0000 | [diff] [blame] | 573 | } |
| 574 | |
Rui Ueyama | 07320e4 | 2016-04-20 20:13:41 +0000 | [diff] [blame] | 575 | class elf::ScriptParser : public ScriptParserBase { |
George Rimar | c3794e5 | 2016-02-24 09:21:47 +0000 | [diff] [blame] | 576 | typedef void (ScriptParser::*Handler)(); |
| 577 | |
Rui Ueyama | f7c5fbb | 2015-09-30 17:23:26 +0000 | [diff] [blame] | 578 | public: |
Rui Ueyama | 07320e4 | 2016-04-20 20:13:41 +0000 | [diff] [blame] | 579 | ScriptParser(StringRef S, bool B) : ScriptParserBase(S), IsUnderSysroot(B) {} |
George Rimar | f23b232 | 2016-02-19 10:45:45 +0000 | [diff] [blame] | 580 | |
Rui Ueyama | 4a46539 | 2016-04-22 22:59:24 +0000 | [diff] [blame] | 581 | void run(); |
Rui Ueyama | f7c5fbb | 2015-09-30 17:23:26 +0000 | [diff] [blame] | 582 | |
| 583 | private: |
Rui Ueyama | 52a1509 | 2015-10-11 03:28:42 +0000 | [diff] [blame] | 584 | void addFile(StringRef Path); |
| 585 | |
Rui Ueyama | f7c5fbb | 2015-09-30 17:23:26 +0000 | [diff] [blame] | 586 | void readAsNeeded(); |
Denis Protivensky | 90c5099 | 2015-10-08 06:48:38 +0000 | [diff] [blame] | 587 | void readEntry(); |
George Rimar | 83f406c | 2015-10-19 17:35:12 +0000 | [diff] [blame] | 588 | void readExtern(); |
Rui Ueyama | f7c5fbb | 2015-09-30 17:23:26 +0000 | [diff] [blame] | 589 | void readGroup(); |
Rui Ueyama | 31aa1f8 | 2015-10-11 01:31:55 +0000 | [diff] [blame] | 590 | void readInclude(); |
George Rimar | c3794e5 | 2016-02-24 09:21:47 +0000 | [diff] [blame] | 591 | void readNothing() {} |
Rui Ueyama | ee59282 | 2015-10-07 00:25:09 +0000 | [diff] [blame] | 592 | void readOutput(); |
Davide Italiano | 9159ce9 | 2015-10-12 21:50:08 +0000 | [diff] [blame] | 593 | void readOutputArch(); |
Rui Ueyama | f7c5fbb | 2015-09-30 17:23:26 +0000 | [diff] [blame] | 594 | void readOutputFormat(); |
Eugene Leviant | bbe3860 | 2016-07-19 09:25:43 +0000 | [diff] [blame] | 595 | void readPhdrs(); |
Davide Italiano | 68a39a6 | 2015-10-08 17:51:41 +0000 | [diff] [blame] | 596 | void readSearchDir(); |
Denis Protivensky | 8e3b38a | 2015-11-12 09:52:08 +0000 | [diff] [blame] | 597 | void readSections(); |
| 598 | |
George Rimar | 652852c | 2016-04-16 10:10:32 +0000 | [diff] [blame] | 599 | void readLocationCounterValue(); |
Eugene Leviant | eda81a1 | 2016-07-12 06:39:48 +0000 | [diff] [blame] | 600 | void readOutputSectionDescription(StringRef OutSec); |
Eugene Leviant | bbe3860 | 2016-07-19 09:25:43 +0000 | [diff] [blame] | 601 | std::vector<StringRef> readOutputSectionPhdrs(); |
| 602 | unsigned readPhdrType(); |
Eugene Leviant | a31c91b | 2016-07-22 07:38:40 +0000 | [diff] [blame] | 603 | void readProvide(bool Hidden); |
| 604 | SymbolAssignment *readSymbolAssignment(StringRef Name); |
Eugene Leviant | eda81a1 | 2016-07-12 06:39:48 +0000 | [diff] [blame] | 605 | std::vector<StringRef> readSectionsCommandExpr(); |
Rui Ueyama | f7c5fbb | 2015-09-30 17:23:26 +0000 | [diff] [blame] | 606 | |
George Rimar | c3794e5 | 2016-02-24 09:21:47 +0000 | [diff] [blame] | 607 | const static StringMap<Handler> Cmd; |
Rui Ueyama | 07320e4 | 2016-04-20 20:13:41 +0000 | [diff] [blame] | 608 | ScriptConfiguration &Opt = *ScriptConfig; |
| 609 | StringSaver Saver = {ScriptConfig->Alloc}; |
Simon Atanasyan | 16b0cc9 | 2015-11-26 05:53:00 +0000 | [diff] [blame] | 610 | bool IsUnderSysroot; |
Rui Ueyama | f7c5fbb | 2015-09-30 17:23:26 +0000 | [diff] [blame] | 611 | }; |
Rui Ueyama | f7c5fbb | 2015-09-30 17:23:26 +0000 | [diff] [blame] | 612 | |
Rafael Espindola | e0df00b | 2016-02-28 00:25:54 +0000 | [diff] [blame] | 613 | const StringMap<elf::ScriptParser::Handler> elf::ScriptParser::Cmd = { |
George Rimar | c3794e5 | 2016-02-24 09:21:47 +0000 | [diff] [blame] | 614 | {"ENTRY", &ScriptParser::readEntry}, |
| 615 | {"EXTERN", &ScriptParser::readExtern}, |
| 616 | {"GROUP", &ScriptParser::readGroup}, |
| 617 | {"INCLUDE", &ScriptParser::readInclude}, |
| 618 | {"INPUT", &ScriptParser::readGroup}, |
| 619 | {"OUTPUT", &ScriptParser::readOutput}, |
| 620 | {"OUTPUT_ARCH", &ScriptParser::readOutputArch}, |
| 621 | {"OUTPUT_FORMAT", &ScriptParser::readOutputFormat}, |
Eugene Leviant | bbe3860 | 2016-07-19 09:25:43 +0000 | [diff] [blame] | 622 | {"PHDRS", &ScriptParser::readPhdrs}, |
George Rimar | c3794e5 | 2016-02-24 09:21:47 +0000 | [diff] [blame] | 623 | {"SEARCH_DIR", &ScriptParser::readSearchDir}, |
| 624 | {"SECTIONS", &ScriptParser::readSections}, |
| 625 | {";", &ScriptParser::readNothing}}; |
| 626 | |
Rui Ueyama | 717677a | 2016-02-11 21:17:59 +0000 | [diff] [blame] | 627 | void ScriptParser::run() { |
Rui Ueyama | f7c5fbb | 2015-09-30 17:23:26 +0000 | [diff] [blame] | 628 | while (!atEOF()) { |
| 629 | StringRef Tok = next(); |
George Rimar | c3794e5 | 2016-02-24 09:21:47 +0000 | [diff] [blame] | 630 | if (Handler Fn = Cmd.lookup(Tok)) |
| 631 | (this->*Fn)(); |
| 632 | else |
George Rimar | 5761042 | 2016-03-11 14:43:02 +0000 | [diff] [blame] | 633 | setError("unknown directive: " + Tok); |
Rui Ueyama | f7c5fbb | 2015-09-30 17:23:26 +0000 | [diff] [blame] | 634 | } |
| 635 | } |
| 636 | |
Rui Ueyama | 717677a | 2016-02-11 21:17:59 +0000 | [diff] [blame] | 637 | void ScriptParser::addFile(StringRef S) { |
Simon Atanasyan | 16b0cc9 | 2015-11-26 05:53:00 +0000 | [diff] [blame] | 638 | if (IsUnderSysroot && S.startswith("/")) { |
| 639 | SmallString<128> Path; |
| 640 | (Config->Sysroot + S).toStringRef(Path); |
| 641 | if (sys::fs::exists(Path)) { |
| 642 | Driver->addFile(Saver.save(Path.str())); |
| 643 | return; |
| 644 | } |
| 645 | } |
| 646 | |
Rui Ueyama | f03f3cc | 2015-10-13 00:09:21 +0000 | [diff] [blame] | 647 | if (sys::path::is_absolute(S)) { |
Rui Ueyama | 52a1509 | 2015-10-11 03:28:42 +0000 | [diff] [blame] | 648 | Driver->addFile(S); |
| 649 | } else if (S.startswith("=")) { |
| 650 | if (Config->Sysroot.empty()) |
| 651 | Driver->addFile(S.substr(1)); |
| 652 | else |
| 653 | Driver->addFile(Saver.save(Config->Sysroot + "/" + S.substr(1))); |
| 654 | } else if (S.startswith("-l")) { |
Rui Ueyama | 21eecb4 | 2016-02-02 21:13:09 +0000 | [diff] [blame] | 655 | Driver->addLibrary(S.substr(2)); |
Simon Atanasyan | a1b8fc3 | 2015-11-26 20:23:46 +0000 | [diff] [blame] | 656 | } else if (sys::fs::exists(S)) { |
| 657 | Driver->addFile(S); |
Rui Ueyama | 52a1509 | 2015-10-11 03:28:42 +0000 | [diff] [blame] | 658 | } else { |
| 659 | std::string Path = findFromSearchPaths(S); |
| 660 | if (Path.empty()) |
George Rimar | 777f963 | 2016-03-12 08:31:34 +0000 | [diff] [blame] | 661 | setError("unable to find " + S); |
Rui Ueyama | 025d59b | 2016-02-02 20:27:59 +0000 | [diff] [blame] | 662 | else |
| 663 | Driver->addFile(Saver.save(Path)); |
Rui Ueyama | 52a1509 | 2015-10-11 03:28:42 +0000 | [diff] [blame] | 664 | } |
| 665 | } |
| 666 | |
Rui Ueyama | 717677a | 2016-02-11 21:17:59 +0000 | [diff] [blame] | 667 | void ScriptParser::readAsNeeded() { |
Rui Ueyama | f7c5fbb | 2015-09-30 17:23:26 +0000 | [diff] [blame] | 668 | expect("("); |
Rui Ueyama | 35da9b6 | 2015-10-11 20:59:12 +0000 | [diff] [blame] | 669 | bool Orig = Config->AsNeeded; |
| 670 | Config->AsNeeded = true; |
Rui Ueyama | 025d59b | 2016-02-02 20:27:59 +0000 | [diff] [blame] | 671 | while (!Error) { |
Rui Ueyama | f7c5fbb | 2015-09-30 17:23:26 +0000 | [diff] [blame] | 672 | StringRef Tok = next(); |
| 673 | if (Tok == ")") |
Rui Ueyama | 35da9b6 | 2015-10-11 20:59:12 +0000 | [diff] [blame] | 674 | break; |
Rui Ueyama | 52a1509 | 2015-10-11 03:28:42 +0000 | [diff] [blame] | 675 | addFile(Tok); |
Rui Ueyama | f7c5fbb | 2015-09-30 17:23:26 +0000 | [diff] [blame] | 676 | } |
Rui Ueyama | 35da9b6 | 2015-10-11 20:59:12 +0000 | [diff] [blame] | 677 | Config->AsNeeded = Orig; |
Rui Ueyama | f7c5fbb | 2015-09-30 17:23:26 +0000 | [diff] [blame] | 678 | } |
| 679 | |
Rui Ueyama | 717677a | 2016-02-11 21:17:59 +0000 | [diff] [blame] | 680 | void ScriptParser::readEntry() { |
Denis Protivensky | 90c5099 | 2015-10-08 06:48:38 +0000 | [diff] [blame] | 681 | // -e <symbol> takes predecence over ENTRY(<symbol>). |
| 682 | expect("("); |
| 683 | StringRef Tok = next(); |
| 684 | if (Config->Entry.empty()) |
| 685 | Config->Entry = Tok; |
| 686 | expect(")"); |
| 687 | } |
| 688 | |
Rui Ueyama | 717677a | 2016-02-11 21:17:59 +0000 | [diff] [blame] | 689 | void ScriptParser::readExtern() { |
George Rimar | 83f406c | 2015-10-19 17:35:12 +0000 | [diff] [blame] | 690 | expect("("); |
Rui Ueyama | 025d59b | 2016-02-02 20:27:59 +0000 | [diff] [blame] | 691 | while (!Error) { |
George Rimar | 83f406c | 2015-10-19 17:35:12 +0000 | [diff] [blame] | 692 | StringRef Tok = next(); |
| 693 | if (Tok == ")") |
| 694 | return; |
| 695 | Config->Undefined.push_back(Tok); |
| 696 | } |
| 697 | } |
| 698 | |
Rui Ueyama | 717677a | 2016-02-11 21:17:59 +0000 | [diff] [blame] | 699 | void ScriptParser::readGroup() { |
Rui Ueyama | f7c5fbb | 2015-09-30 17:23:26 +0000 | [diff] [blame] | 700 | expect("("); |
Rui Ueyama | 025d59b | 2016-02-02 20:27:59 +0000 | [diff] [blame] | 701 | while (!Error) { |
Rui Ueyama | f7c5fbb | 2015-09-30 17:23:26 +0000 | [diff] [blame] | 702 | StringRef Tok = next(); |
| 703 | if (Tok == ")") |
| 704 | return; |
| 705 | if (Tok == "AS_NEEDED") { |
| 706 | readAsNeeded(); |
| 707 | continue; |
| 708 | } |
Rui Ueyama | 52a1509 | 2015-10-11 03:28:42 +0000 | [diff] [blame] | 709 | addFile(Tok); |
Rui Ueyama | f7c5fbb | 2015-09-30 17:23:26 +0000 | [diff] [blame] | 710 | } |
| 711 | } |
| 712 | |
Rui Ueyama | 717677a | 2016-02-11 21:17:59 +0000 | [diff] [blame] | 713 | void ScriptParser::readInclude() { |
Rui Ueyama | 31aa1f8 | 2015-10-11 01:31:55 +0000 | [diff] [blame] | 714 | StringRef Tok = next(); |
| 715 | auto MBOrErr = MemoryBuffer::getFile(Tok); |
Rui Ueyama | 025d59b | 2016-02-02 20:27:59 +0000 | [diff] [blame] | 716 | if (!MBOrErr) { |
George Rimar | 5761042 | 2016-03-11 14:43:02 +0000 | [diff] [blame] | 717 | setError("cannot open " + Tok); |
Rui Ueyama | 025d59b | 2016-02-02 20:27:59 +0000 | [diff] [blame] | 718 | return; |
| 719 | } |
Rui Ueyama | 31aa1f8 | 2015-10-11 01:31:55 +0000 | [diff] [blame] | 720 | std::unique_ptr<MemoryBuffer> &MB = *MBOrErr; |
Rui Ueyama | a47ee68 | 2015-10-11 01:53:04 +0000 | [diff] [blame] | 721 | StringRef S = Saver.save(MB->getMemBufferRef().getBuffer()); |
| 722 | std::vector<StringRef> V = tokenize(S); |
Rui Ueyama | 31aa1f8 | 2015-10-11 01:31:55 +0000 | [diff] [blame] | 723 | Tokens.insert(Tokens.begin() + Pos, V.begin(), V.end()); |
Rui Ueyama | 31aa1f8 | 2015-10-11 01:31:55 +0000 | [diff] [blame] | 724 | } |
| 725 | |
Rui Ueyama | 717677a | 2016-02-11 21:17:59 +0000 | [diff] [blame] | 726 | void ScriptParser::readOutput() { |
Rui Ueyama | ee59282 | 2015-10-07 00:25:09 +0000 | [diff] [blame] | 727 | // -o <file> takes predecence over OUTPUT(<file>). |
| 728 | expect("("); |
| 729 | StringRef Tok = next(); |
| 730 | if (Config->OutputFile.empty()) |
| 731 | Config->OutputFile = Tok; |
| 732 | expect(")"); |
| 733 | } |
| 734 | |
Rui Ueyama | 717677a | 2016-02-11 21:17:59 +0000 | [diff] [blame] | 735 | void ScriptParser::readOutputArch() { |
Davide Italiano | 9159ce9 | 2015-10-12 21:50:08 +0000 | [diff] [blame] | 736 | // Error checking only for now. |
| 737 | expect("("); |
| 738 | next(); |
| 739 | expect(")"); |
| 740 | } |
| 741 | |
Rui Ueyama | 717677a | 2016-02-11 21:17:59 +0000 | [diff] [blame] | 742 | void ScriptParser::readOutputFormat() { |
Rui Ueyama | f7c5fbb | 2015-09-30 17:23:26 +0000 | [diff] [blame] | 743 | // Error checking only for now. |
| 744 | expect("("); |
| 745 | next(); |
Davide Italiano | 6836c61 | 2015-10-12 21:08:41 +0000 | [diff] [blame] | 746 | StringRef Tok = next(); |
| 747 | if (Tok == ")") |
| 748 | return; |
Rui Ueyama | 025d59b | 2016-02-02 20:27:59 +0000 | [diff] [blame] | 749 | if (Tok != ",") { |
George Rimar | 5761042 | 2016-03-11 14:43:02 +0000 | [diff] [blame] | 750 | setError("unexpected token: " + Tok); |
Rui Ueyama | 025d59b | 2016-02-02 20:27:59 +0000 | [diff] [blame] | 751 | return; |
| 752 | } |
Davide Italiano | 6836c61 | 2015-10-12 21:08:41 +0000 | [diff] [blame] | 753 | next(); |
| 754 | expect(","); |
| 755 | next(); |
Rui Ueyama | f7c5fbb | 2015-09-30 17:23:26 +0000 | [diff] [blame] | 756 | expect(")"); |
| 757 | } |
| 758 | |
Eugene Leviant | bbe3860 | 2016-07-19 09:25:43 +0000 | [diff] [blame] | 759 | void ScriptParser::readPhdrs() { |
| 760 | expect("{"); |
| 761 | while (!Error && !skip("}")) { |
| 762 | StringRef Tok = next(); |
Eugene Leviant | 865bf86 | 2016-07-21 10:43:25 +0000 | [diff] [blame] | 763 | Opt.PhdrsCommands.push_back({Tok, PT_NULL, false, false, UINT_MAX}); |
Eugene Leviant | bbe3860 | 2016-07-19 09:25:43 +0000 | [diff] [blame] | 764 | PhdrsCommand &PhdrCmd = Opt.PhdrsCommands.back(); |
| 765 | |
| 766 | PhdrCmd.Type = readPhdrType(); |
| 767 | do { |
| 768 | Tok = next(); |
| 769 | if (Tok == ";") |
| 770 | break; |
| 771 | if (Tok == "FILEHDR") |
| 772 | PhdrCmd.HasFilehdr = true; |
| 773 | else if (Tok == "PHDRS") |
| 774 | PhdrCmd.HasPhdrs = true; |
Eugene Leviant | 865bf86 | 2016-07-21 10:43:25 +0000 | [diff] [blame] | 775 | else if (Tok == "FLAGS") { |
| 776 | expect("("); |
| 777 | next().getAsInteger(0, PhdrCmd.Flags); |
| 778 | expect(")"); |
| 779 | } else |
Eugene Leviant | bbe3860 | 2016-07-19 09:25:43 +0000 | [diff] [blame] | 780 | setError("unexpected header attribute: " + Tok); |
| 781 | } while (!Error); |
| 782 | } |
| 783 | } |
| 784 | |
Rui Ueyama | 717677a | 2016-02-11 21:17:59 +0000 | [diff] [blame] | 785 | void ScriptParser::readSearchDir() { |
Davide Italiano | 68a39a6 | 2015-10-08 17:51:41 +0000 | [diff] [blame] | 786 | expect("("); |
Rafael Espindola | 0650192 | 2016-03-08 17:13:12 +0000 | [diff] [blame] | 787 | Config->SearchPaths.push_back(next()); |
Davide Italiano | 68a39a6 | 2015-10-08 17:51:41 +0000 | [diff] [blame] | 788 | expect(")"); |
| 789 | } |
| 790 | |
Rui Ueyama | 717677a | 2016-02-11 21:17:59 +0000 | [diff] [blame] | 791 | void ScriptParser::readSections() { |
Rui Ueyama | 07320e4 | 2016-04-20 20:13:41 +0000 | [diff] [blame] | 792 | Opt.DoLayout = true; |
Denis Protivensky | 8e3b38a | 2015-11-12 09:52:08 +0000 | [diff] [blame] | 793 | expect("{"); |
George Rimar | 652852c | 2016-04-16 10:10:32 +0000 | [diff] [blame] | 794 | while (!Error && !skip("}")) { |
| 795 | StringRef Tok = peek(); |
Eugene Leviant | eda81a1 | 2016-07-12 06:39:48 +0000 | [diff] [blame] | 796 | if (Tok == ".") { |
George Rimar | 652852c | 2016-04-16 10:10:32 +0000 | [diff] [blame] | 797 | readLocationCounterValue(); |
Eugene Leviant | eda81a1 | 2016-07-12 06:39:48 +0000 | [diff] [blame] | 798 | continue; |
| 799 | } |
| 800 | next(); |
Eugene Leviant | a31c91b | 2016-07-22 07:38:40 +0000 | [diff] [blame] | 801 | if (Tok == "PROVIDE") |
| 802 | readProvide(false); |
| 803 | else if (Tok == "PROVIDE_HIDDEN") |
| 804 | readProvide(true); |
| 805 | else if (peek() == "=") |
Eugene Leviant | eda81a1 | 2016-07-12 06:39:48 +0000 | [diff] [blame] | 806 | readSymbolAssignment(Tok); |
George Rimar | 652852c | 2016-04-16 10:10:32 +0000 | [diff] [blame] | 807 | else |
Eugene Leviant | eda81a1 | 2016-07-12 06:39:48 +0000 | [diff] [blame] | 808 | readOutputSectionDescription(Tok); |
George Rimar | 652852c | 2016-04-16 10:10:32 +0000 | [diff] [blame] | 809 | } |
Denis Protivensky | 8e3b38a | 2015-11-12 09:52:08 +0000 | [diff] [blame] | 810 | } |
| 811 | |
George Rimar | 652852c | 2016-04-16 10:10:32 +0000 | [diff] [blame] | 812 | void ScriptParser::readLocationCounterValue() { |
| 813 | expect("."); |
| 814 | expect("="); |
Eugene Leviant | eda81a1 | 2016-07-12 06:39:48 +0000 | [diff] [blame] | 815 | std::vector<StringRef> Expr = readSectionsCommandExpr(); |
| 816 | if (Expr.empty()) |
George Rimar | 652852c | 2016-04-16 10:10:32 +0000 | [diff] [blame] | 817 | error("error in location counter expression"); |
Eugene Leviant | eda81a1 | 2016-07-12 06:39:48 +0000 | [diff] [blame] | 818 | else |
George Rimar | 076fe15 | 2016-07-21 06:43:01 +0000 | [diff] [blame] | 819 | Opt.Commands.push_back(llvm::make_unique<SymbolAssignment>(".", Expr)); |
George Rimar | 652852c | 2016-04-16 10:10:32 +0000 | [diff] [blame] | 820 | } |
| 821 | |
Eugene Leviant | eda81a1 | 2016-07-12 06:39:48 +0000 | [diff] [blame] | 822 | void ScriptParser::readOutputSectionDescription(StringRef OutSec) { |
George Rimar | 076fe15 | 2016-07-21 06:43:01 +0000 | [diff] [blame] | 823 | OutputSectionCommand *Cmd = new OutputSectionCommand(OutSec); |
| 824 | Opt.Commands.emplace_back(Cmd); |
Denis Protivensky | 8e3b38a | 2015-11-12 09:52:08 +0000 | [diff] [blame] | 825 | expect(":"); |
Davide Italiano | 246f681 | 2016-07-22 03:36:24 +0000 | [diff] [blame] | 826 | |
| 827 | // Parse constraints. |
| 828 | if (skip("ONLY_IF_RO")) |
| 829 | Cmd->Constraint = ReadOnly; |
| 830 | if (skip("ONLY_IF_RW")) |
| 831 | Cmd->Constraint = ReadWrite; |
Denis Protivensky | 8e3b38a | 2015-11-12 09:52:08 +0000 | [diff] [blame] | 832 | expect("{"); |
Rui Ueyama | 8ec77e6 | 2016-04-21 22:00:51 +0000 | [diff] [blame] | 833 | |
Rui Ueyama | 025d59b | 2016-02-02 20:27:59 +0000 | [diff] [blame] | 834 | while (!Error && !skip("}")) { |
George Rimar | 481c2ce | 2016-02-23 07:47:54 +0000 | [diff] [blame] | 835 | StringRef Tok = next(); |
| 836 | if (Tok == "*") { |
George Rimar | eea3114 | 2016-07-21 14:26:59 +0000 | [diff] [blame] | 837 | auto *InCmd = new InputSectionDescription(); |
| 838 | Cmd->Commands.emplace_back(InCmd); |
Rui Ueyama | 8ec77e6 | 2016-04-21 22:00:51 +0000 | [diff] [blame] | 839 | expect("("); |
| 840 | while (!Error && !skip(")")) |
George Rimar | eea3114 | 2016-07-21 14:26:59 +0000 | [diff] [blame] | 841 | InCmd->Patterns.push_back(next()); |
George Rimar | 481c2ce | 2016-02-23 07:47:54 +0000 | [diff] [blame] | 842 | } else if (Tok == "KEEP") { |
| 843 | expect("("); |
Rui Ueyama | 8ec77e6 | 2016-04-21 22:00:51 +0000 | [diff] [blame] | 844 | expect("*"); |
| 845 | expect("("); |
George Rimar | eea3114 | 2016-07-21 14:26:59 +0000 | [diff] [blame] | 846 | auto *InCmd = new InputSectionDescription(); |
| 847 | Cmd->Commands.emplace_back(InCmd); |
Rui Ueyama | 8ec77e6 | 2016-04-21 22:00:51 +0000 | [diff] [blame] | 848 | while (!Error && !skip(")")) { |
George Rimar | eea3114 | 2016-07-21 14:26:59 +0000 | [diff] [blame] | 849 | Opt.KeptSections.push_back(peek()); |
| 850 | InCmd->Patterns.push_back(next()); |
Rui Ueyama | 8ec77e6 | 2016-04-21 22:00:51 +0000 | [diff] [blame] | 851 | } |
George Rimar | 481c2ce | 2016-02-23 07:47:54 +0000 | [diff] [blame] | 852 | expect(")"); |
| 853 | } else { |
George Rimar | 777f963 | 2016-03-12 08:31:34 +0000 | [diff] [blame] | 854 | setError("unknown command " + Tok); |
George Rimar | 481c2ce | 2016-02-23 07:47:54 +0000 | [diff] [blame] | 855 | } |
Denis Protivensky | 8e3b38a | 2015-11-12 09:52:08 +0000 | [diff] [blame] | 856 | } |
George Rimar | 076fe15 | 2016-07-21 06:43:01 +0000 | [diff] [blame] | 857 | Cmd->Phdrs = readOutputSectionPhdrs(); |
Rui Ueyama | 8ec77e6 | 2016-04-21 22:00:51 +0000 | [diff] [blame] | 858 | |
George Rimar | e2ee72b | 2016-02-26 14:48:31 +0000 | [diff] [blame] | 859 | StringRef Tok = peek(); |
| 860 | if (Tok.startswith("=")) { |
| 861 | if (!Tok.startswith("=0x")) { |
Rui Ueyama | 3ed2f06 | 2016-03-13 03:17:44 +0000 | [diff] [blame] | 862 | setError("filler should be a hexadecimal value"); |
George Rimar | e2ee72b | 2016-02-26 14:48:31 +0000 | [diff] [blame] | 863 | return; |
| 864 | } |
Rui Ueyama | 3e80897 | 2016-02-28 05:09:11 +0000 | [diff] [blame] | 865 | Tok = Tok.substr(3); |
George Rimar | f6c3cce | 2016-07-21 07:48:54 +0000 | [diff] [blame] | 866 | Cmd->Filler = parseHex(Tok); |
George Rimar | e2ee72b | 2016-02-26 14:48:31 +0000 | [diff] [blame] | 867 | next(); |
| 868 | } |
Denis Protivensky | 8e3b38a | 2015-11-12 09:52:08 +0000 | [diff] [blame] | 869 | } |
| 870 | |
Eugene Leviant | a31c91b | 2016-07-22 07:38:40 +0000 | [diff] [blame] | 871 | void ScriptParser::readProvide(bool Hidden) { |
| 872 | expect("("); |
| 873 | if (SymbolAssignment *Assignment = readSymbolAssignment(next())) { |
| 874 | Assignment->Provide = true; |
| 875 | Assignment->Hidden = Hidden; |
| 876 | } |
| 877 | expect(")"); |
| 878 | expect(";"); |
Eugene Leviant | eda81a1 | 2016-07-12 06:39:48 +0000 | [diff] [blame] | 879 | } |
| 880 | |
Eugene Leviant | a31c91b | 2016-07-22 07:38:40 +0000 | [diff] [blame] | 881 | SymbolAssignment *ScriptParser::readSymbolAssignment(StringRef Name) { |
| 882 | expect("="); |
| 883 | std::vector<StringRef> Expr = readSectionsCommandExpr(); |
| 884 | if (Expr.empty()) { |
| 885 | error("error in symbol assignment expression"); |
| 886 | } else { |
| 887 | Opt.Commands.push_back(llvm::make_unique<SymbolAssignment>(Name, Expr)); |
| 888 | return static_cast<SymbolAssignment *>(Opt.Commands.back().get()); |
| 889 | } |
| 890 | return nullptr; |
| 891 | } |
| 892 | |
| 893 | // This function reads balanced expression until semicolon is seen. |
Eugene Leviant | eda81a1 | 2016-07-12 06:39:48 +0000 | [diff] [blame] | 894 | std::vector<StringRef> ScriptParser::readSectionsCommandExpr() { |
Eugene Leviant | a31c91b | 2016-07-22 07:38:40 +0000 | [diff] [blame] | 895 | int Braces = 0; |
Eugene Leviant | eda81a1 | 2016-07-12 06:39:48 +0000 | [diff] [blame] | 896 | std::vector<StringRef> Expr; |
| 897 | while (!Error) { |
Eugene Leviant | a31c91b | 2016-07-22 07:38:40 +0000 | [diff] [blame] | 898 | StringRef Tok = peek(); |
| 899 | |
| 900 | if (Tok == "(") |
| 901 | Braces++; |
| 902 | else if (Tok == ")") |
| 903 | if (--Braces < 0) |
| 904 | break; |
| 905 | |
| 906 | next(); |
Eugene Leviant | eda81a1 | 2016-07-12 06:39:48 +0000 | [diff] [blame] | 907 | if (Tok == ";") |
| 908 | break; |
| 909 | Expr.push_back(Tok); |
| 910 | } |
| 911 | return Expr; |
| 912 | } |
| 913 | |
Eugene Leviant | bbe3860 | 2016-07-19 09:25:43 +0000 | [diff] [blame] | 914 | std::vector<StringRef> ScriptParser::readOutputSectionPhdrs() { |
| 915 | std::vector<StringRef> Phdrs; |
| 916 | while (!Error && peek().startswith(":")) { |
| 917 | StringRef Tok = next(); |
| 918 | Tok = (Tok.size() == 1) ? next() : Tok.substr(1); |
| 919 | if (Tok.empty()) { |
| 920 | setError("section header name is empty"); |
| 921 | break; |
| 922 | } |
Rui Ueyama | 047404f | 2016-07-20 19:36:36 +0000 | [diff] [blame] | 923 | Phdrs.push_back(Tok); |
Eugene Leviant | bbe3860 | 2016-07-19 09:25:43 +0000 | [diff] [blame] | 924 | } |
| 925 | return Phdrs; |
| 926 | } |
| 927 | |
| 928 | unsigned ScriptParser::readPhdrType() { |
Eugene Leviant | bbe3860 | 2016-07-19 09:25:43 +0000 | [diff] [blame] | 929 | StringRef Tok = next(); |
Rui Ueyama | b0f6c59 | 2016-07-20 19:36:38 +0000 | [diff] [blame] | 930 | unsigned Ret = StringSwitch<unsigned>(Tok) |
| 931 | .Case("PT_NULL", PT_NULL) |
| 932 | .Case("PT_LOAD", PT_LOAD) |
| 933 | .Case("PT_DYNAMIC", PT_DYNAMIC) |
| 934 | .Case("PT_INTERP", PT_INTERP) |
| 935 | .Case("PT_NOTE", PT_NOTE) |
| 936 | .Case("PT_SHLIB", PT_SHLIB) |
| 937 | .Case("PT_PHDR", PT_PHDR) |
| 938 | .Case("PT_TLS", PT_TLS) |
| 939 | .Case("PT_GNU_EH_FRAME", PT_GNU_EH_FRAME) |
| 940 | .Case("PT_GNU_STACK", PT_GNU_STACK) |
| 941 | .Case("PT_GNU_RELRO", PT_GNU_RELRO) |
| 942 | .Default(-1); |
Eugene Leviant | bbe3860 | 2016-07-19 09:25:43 +0000 | [diff] [blame] | 943 | |
Rui Ueyama | b0f6c59 | 2016-07-20 19:36:38 +0000 | [diff] [blame] | 944 | if (Ret == (unsigned)-1) { |
| 945 | setError("invalid program header type: " + Tok); |
| 946 | return PT_NULL; |
| 947 | } |
| 948 | return Ret; |
Eugene Leviant | bbe3860 | 2016-07-19 09:25:43 +0000 | [diff] [blame] | 949 | } |
| 950 | |
Simon Atanasyan | 16b0cc9 | 2015-11-26 05:53:00 +0000 | [diff] [blame] | 951 | static bool isUnderSysroot(StringRef Path) { |
| 952 | if (Config->Sysroot == "") |
| 953 | return false; |
| 954 | for (; !Path.empty(); Path = sys::path::parent_path(Path)) |
| 955 | if (sys::fs::equivalent(Config->Sysroot, Path)) |
| 956 | return true; |
| 957 | return false; |
| 958 | } |
| 959 | |
Rui Ueyama | 07320e4 | 2016-04-20 20:13:41 +0000 | [diff] [blame] | 960 | // Entry point. |
| 961 | void elf::readLinkerScript(MemoryBufferRef MB) { |
Simon Atanasyan | 16b0cc9 | 2015-11-26 05:53:00 +0000 | [diff] [blame] | 962 | StringRef Path = MB.getBufferIdentifier(); |
Rui Ueyama | 07320e4 | 2016-04-20 20:13:41 +0000 | [diff] [blame] | 963 | ScriptParser(MB.getBuffer(), isUnderSysroot(Path)).run(); |
Rui Ueyama | f7c5fbb | 2015-09-30 17:23:26 +0000 | [diff] [blame] | 964 | } |
Rui Ueyama | 1ebc8ed | 2016-02-12 21:47:28 +0000 | [diff] [blame] | 965 | |
Rui Ueyama | 07320e4 | 2016-04-20 20:13:41 +0000 | [diff] [blame] | 966 | template class elf::LinkerScript<ELF32LE>; |
| 967 | template class elf::LinkerScript<ELF32BE>; |
| 968 | template class elf::LinkerScript<ELF64LE>; |
| 969 | template class elf::LinkerScript<ELF64BE>; |