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 | 1ebc8ed | 2016-02-12 21:47:28 +0000 | [diff] [blame] | 240 | template <class ELFT> |
Rui Ueyama | 07320e4 | 2016-04-20 20:13:41 +0000 | [diff] [blame] | 241 | bool LinkerScript<ELFT>::isDiscarded(InputSectionBase<ELFT> *S) { |
George Rimar | eea3114 | 2016-07-21 14:26:59 +0000 | [diff] [blame] | 242 | return !S || !S->Live; |
Rui Ueyama | 717677a | 2016-02-11 21:17:59 +0000 | [diff] [blame] | 243 | } |
| 244 | |
Rui Ueyama | 07320e4 | 2016-04-20 20:13:41 +0000 | [diff] [blame] | 245 | template <class ELFT> |
| 246 | bool LinkerScript<ELFT>::shouldKeep(InputSectionBase<ELFT> *S) { |
Rui Ueyama | 8ec77e6 | 2016-04-21 22:00:51 +0000 | [diff] [blame] | 247 | for (StringRef Pat : Opt.KeptSections) |
Rui Ueyama | 722830a | 2016-06-29 05:32:09 +0000 | [diff] [blame] | 248 | if (globMatch(Pat, S->getSectionName())) |
Rui Ueyama | 8ec77e6 | 2016-04-21 22:00:51 +0000 | [diff] [blame] | 249 | return true; |
| 250 | return false; |
George Rimar | 481c2ce | 2016-02-23 07:47:54 +0000 | [diff] [blame] | 251 | } |
| 252 | |
George Rimar | eea3114 | 2016-07-21 14:26:59 +0000 | [diff] [blame] | 253 | static bool match(StringRef Pattern, ArrayRef<StringRef> Arr) { |
| 254 | for (StringRef S : Arr) |
| 255 | if (globMatch(S, Pattern)) |
| 256 | return true; |
| 257 | return false; |
| 258 | } |
| 259 | |
George Rimar | 652852c | 2016-04-16 10:10:32 +0000 | [diff] [blame] | 260 | template <class ELFT> |
Rui Ueyama | a7f7884 | 2016-07-20 17:19:03 +0000 | [diff] [blame] | 261 | std::vector<OutputSectionBase<ELFT> *> |
Eugene Leviant | e63d81b | 2016-07-20 14:43:20 +0000 | [diff] [blame] | 262 | LinkerScript<ELFT>::createSections(OutputSectionFactory<ELFT> &Factory) { |
George Rimar | eea3114 | 2016-07-21 14:26:59 +0000 | [diff] [blame] | 263 | typedef const std::unique_ptr<ObjectFile<ELFT>> ObjectFile; |
Rui Ueyama | a7f7884 | 2016-07-20 17:19:03 +0000 | [diff] [blame] | 264 | std::vector<OutputSectionBase<ELFT> *> Result; |
Davide Italiano | 246f681 | 2016-07-22 03:36:24 +0000 | [diff] [blame^] | 265 | DenseSet<OutputSectionBase<ELFT> *> Removed; |
Rui Ueyama | a7f7884 | 2016-07-20 17:19:03 +0000 | [diff] [blame] | 266 | |
Eugene Leviant | e63d81b | 2016-07-20 14:43:20 +0000 | [diff] [blame] | 267 | // Add input section to output section. If there is no output section yet, |
| 268 | // then create it and add to output section list. |
Davide Italiano | 246f681 | 2016-07-22 03:36:24 +0000 | [diff] [blame^] | 269 | auto AddInputSec = [&](InputSectionBase<ELFT> *C, StringRef Name, |
| 270 | ConstraintKind Constraint) { |
Eugene Leviant | e63d81b | 2016-07-20 14:43:20 +0000 | [diff] [blame] | 271 | OutputSectionBase<ELFT> *Sec; |
| 272 | bool IsNew; |
| 273 | std::tie(Sec, IsNew) = Factory.create(C, Name); |
| 274 | if (IsNew) |
Rui Ueyama | a7f7884 | 2016-07-20 17:19:03 +0000 | [diff] [blame] | 275 | Result.push_back(Sec); |
Davide Italiano | 246f681 | 2016-07-22 03:36:24 +0000 | [diff] [blame^] | 276 | if ((!(C->getSectionHdr()->sh_flags & SHF_WRITE)) && |
| 277 | Constraint == ReadWrite) { |
| 278 | Removed.insert(Sec); |
| 279 | return; |
| 280 | } |
| 281 | if ((C->getSectionHdr()->sh_flags & SHF_WRITE) && Constraint == ReadOnly) { |
| 282 | Removed.insert(Sec); |
| 283 | return; |
| 284 | } |
Eugene Leviant | e63d81b | 2016-07-20 14:43:20 +0000 | [diff] [blame] | 285 | Sec->addSection(C); |
| 286 | }; |
| 287 | |
| 288 | // Select input sections matching rule and add them to corresponding |
| 289 | // output section. Section rules are processed in order they're listed |
| 290 | // in script, so correct input section order is maintained by design. |
George Rimar | eea3114 | 2016-07-21 14:26:59 +0000 | [diff] [blame] | 291 | for (const std::unique_ptr<BaseCommand> &Base : Opt.Commands) { |
| 292 | auto *OutCmd = dyn_cast<OutputSectionCommand>(Base.get()); |
| 293 | if (!OutCmd) |
| 294 | continue; |
| 295 | |
| 296 | for (const std::unique_ptr<BaseCommand> &Cmd : OutCmd->Commands) { |
| 297 | auto *InCmd = dyn_cast<InputSectionDescription>(Cmd.get()); |
| 298 | if (!InCmd) |
| 299 | continue; |
| 300 | |
| 301 | for (ObjectFile &F : Symtab<ELFT>::X->getObjectFiles()) { |
| 302 | for (InputSectionBase<ELFT> *S : F->getSections()) { |
| 303 | if (isDiscarded(S) || S->OutSec) |
| 304 | continue; |
| 305 | |
| 306 | if (match(S->getSectionName(), InCmd->Patterns)) { |
| 307 | if (OutCmd->Name == "/DISCARD/") |
| 308 | S->Live = false; |
| 309 | else |
Davide Italiano | 246f681 | 2016-07-22 03:36:24 +0000 | [diff] [blame^] | 310 | AddInputSec(S, OutCmd->Name, OutCmd->Constraint); |
George Rimar | eea3114 | 2016-07-21 14:26:59 +0000 | [diff] [blame] | 311 | } |
| 312 | } |
| 313 | } |
| 314 | } |
| 315 | } |
Eugene Leviant | e63d81b | 2016-07-20 14:43:20 +0000 | [diff] [blame] | 316 | |
| 317 | // Add all other input sections, which are not listed in script. |
George Rimar | eea3114 | 2016-07-21 14:26:59 +0000 | [diff] [blame] | 318 | for (ObjectFile &F : Symtab<ELFT>::X->getObjectFiles()) |
Reid Kleckner | 3c944ec | 2016-07-21 18:39:28 +0000 | [diff] [blame] | 319 | for (InputSectionBase<ELFT> *S : F->getSections()) { |
Eugene Leviant | e63d81b | 2016-07-20 14:43:20 +0000 | [diff] [blame] | 320 | if (!isDiscarded(S)) { |
| 321 | if (!S->OutSec) |
Davide Italiano | 246f681 | 2016-07-22 03:36:24 +0000 | [diff] [blame^] | 322 | AddInputSec(S, getOutputSectionName(S), NoConstraint); |
Eugene Leviant | e63d81b | 2016-07-20 14:43:20 +0000 | [diff] [blame] | 323 | } else |
| 324 | reportDiscarded(S, F); |
Reid Kleckner | 3c944ec | 2016-07-21 18:39:28 +0000 | [diff] [blame] | 325 | } |
Eugene Leviant | e63d81b | 2016-07-20 14:43:20 +0000 | [diff] [blame] | 326 | |
Davide Italiano | 246f681 | 2016-07-22 03:36:24 +0000 | [diff] [blame^] | 327 | // Remove from the output all the sections which did not met the constraints. |
| 328 | Result.erase(std::remove_if(Result.begin(), Result.end(), |
| 329 | [&](OutputSectionBase<ELFT> *Sec) { |
| 330 | return Removed.count(Sec); |
| 331 | }), |
| 332 | Result.end()); |
Eugene Leviant | e63d81b | 2016-07-20 14:43:20 +0000 | [diff] [blame] | 333 | return Result; |
| 334 | } |
| 335 | |
| 336 | template <class ELFT> |
George Rimar | 10e576e | 2016-07-21 16:07:40 +0000 | [diff] [blame] | 337 | void LinkerScript<ELFT>::dispatchAssignment(SymbolAssignment *Cmd) { |
| 338 | uint64_t Val = evalExpr(Cmd->Expr, Dot); |
| 339 | if (Cmd->Name == ".") { |
| 340 | Dot = Val; |
| 341 | } else { |
| 342 | auto *D = cast<DefinedRegular<ELFT>>(Symtab<ELFT>::X->find(Cmd->Name)); |
| 343 | D->Value = Val; |
| 344 | } |
| 345 | } |
| 346 | |
| 347 | template <class ELFT> |
Rui Ueyama | 07320e4 | 2016-04-20 20:13:41 +0000 | [diff] [blame] | 348 | void LinkerScript<ELFT>::assignAddresses( |
George Rimar | dbbd8b1 | 2016-04-21 11:21:48 +0000 | [diff] [blame] | 349 | ArrayRef<OutputSectionBase<ELFT> *> Sections) { |
George Rimar | 652852c | 2016-04-16 10:10:32 +0000 | [diff] [blame] | 350 | // Orphan sections are sections present in the input files which |
Rui Ueyama | 7c18c28 | 2016-04-18 21:00:40 +0000 | [diff] [blame] | 351 | // are not explicitly placed into the output file by the linker script. |
| 352 | // We place orphan sections at end of file. |
| 353 | // Other linkers places them using some heuristics as described in |
George Rimar | 652852c | 2016-04-16 10:10:32 +0000 | [diff] [blame] | 354 | // https://sourceware.org/binutils/docs/ld/Orphan-Sections.html#Orphan-Sections. |
Rui Ueyama | 7c18c28 | 2016-04-18 21:00:40 +0000 | [diff] [blame] | 355 | for (OutputSectionBase<ELFT> *Sec : Sections) { |
George Rimar | 652852c | 2016-04-16 10:10:32 +0000 | [diff] [blame] | 356 | StringRef Name = Sec->getName(); |
Rui Ueyama | c3e2a4b | 2016-04-21 20:30:00 +0000 | [diff] [blame] | 357 | if (getSectionIndex(Name) == INT_MAX) |
George Rimar | 076fe15 | 2016-07-21 06:43:01 +0000 | [diff] [blame] | 358 | Opt.Commands.push_back(llvm::make_unique<OutputSectionCommand>(Name)); |
George Rimar | 652852c | 2016-04-16 10:10:32 +0000 | [diff] [blame] | 359 | } |
George Rimar | 652852c | 2016-04-16 10:10:32 +0000 | [diff] [blame] | 360 | |
Rui Ueyama | 7c18c28 | 2016-04-18 21:00:40 +0000 | [diff] [blame] | 361 | // Assign addresses as instructed by linker script SECTIONS sub-commands. |
Rui Ueyama | 52c4e17 | 2016-07-01 10:42:25 +0000 | [diff] [blame] | 362 | Dot = Out<ELFT>::ElfHeader->getSize() + Out<ELFT>::ProgramHeaders->getSize(); |
Eugene Leviant | 467c4d5 | 2016-07-01 10:27:36 +0000 | [diff] [blame] | 363 | uintX_t MinVA = std::numeric_limits<uintX_t>::max(); |
George Rimar | 652852c | 2016-04-16 10:10:32 +0000 | [diff] [blame] | 364 | uintX_t ThreadBssOffset = 0; |
George Rimar | 652852c | 2016-04-16 10:10:32 +0000 | [diff] [blame] | 365 | |
George Rimar | 076fe15 | 2016-07-21 06:43:01 +0000 | [diff] [blame] | 366 | for (const std::unique_ptr<BaseCommand> &Base : Opt.Commands) { |
| 367 | if (auto *Cmd = dyn_cast<SymbolAssignment>(Base.get())) { |
George Rimar | 10e576e | 2016-07-21 16:07:40 +0000 | [diff] [blame] | 368 | dispatchAssignment(Cmd); |
George Rimar | 652852c | 2016-04-16 10:10:32 +0000 | [diff] [blame] | 369 | continue; |
| 370 | } |
| 371 | |
Dima Stepanov | fb8978f | 2016-05-19 18:15:54 +0000 | [diff] [blame] | 372 | // Find all the sections with required name. There can be more than |
George Rimar | 6ad330a | 2016-07-19 07:39:07 +0000 | [diff] [blame] | 373 | // one section with such name, if the alignment, flags or type |
Dima Stepanov | fb8978f | 2016-05-19 18:15:54 +0000 | [diff] [blame] | 374 | // attribute differs. |
George Rimar | 076fe15 | 2016-07-21 06:43:01 +0000 | [diff] [blame] | 375 | auto *Cmd = cast<OutputSectionCommand>(Base.get()); |
Dima Stepanov | fb8978f | 2016-05-19 18:15:54 +0000 | [diff] [blame] | 376 | for (OutputSectionBase<ELFT> *Sec : Sections) { |
George Rimar | 076fe15 | 2016-07-21 06:43:01 +0000 | [diff] [blame] | 377 | if (Sec->getName() != Cmd->Name) |
Dima Stepanov | fb8978f | 2016-05-19 18:15:54 +0000 | [diff] [blame] | 378 | continue; |
George Rimar | 652852c | 2016-04-16 10:10:32 +0000 | [diff] [blame] | 379 | |
Dima Stepanov | fb8978f | 2016-05-19 18:15:54 +0000 | [diff] [blame] | 380 | if ((Sec->getFlags() & SHF_TLS) && Sec->getType() == SHT_NOBITS) { |
| 381 | uintX_t TVA = Dot + ThreadBssOffset; |
Rui Ueyama | 424b408 | 2016-06-17 01:18:46 +0000 | [diff] [blame] | 382 | TVA = alignTo(TVA, Sec->getAlignment()); |
Dima Stepanov | fb8978f | 2016-05-19 18:15:54 +0000 | [diff] [blame] | 383 | Sec->setVA(TVA); |
| 384 | ThreadBssOffset = TVA - Dot + Sec->getSize(); |
| 385 | continue; |
| 386 | } |
George Rimar | 652852c | 2016-04-16 10:10:32 +0000 | [diff] [blame] | 387 | |
Dima Stepanov | fb8978f | 2016-05-19 18:15:54 +0000 | [diff] [blame] | 388 | if (Sec->getFlags() & SHF_ALLOC) { |
Rui Ueyama | 424b408 | 2016-06-17 01:18:46 +0000 | [diff] [blame] | 389 | Dot = alignTo(Dot, Sec->getAlignment()); |
Dima Stepanov | fb8978f | 2016-05-19 18:15:54 +0000 | [diff] [blame] | 390 | Sec->setVA(Dot); |
Rui Ueyama | 52c4e17 | 2016-07-01 10:42:25 +0000 | [diff] [blame] | 391 | MinVA = std::min(MinVA, Dot); |
Dima Stepanov | fb8978f | 2016-05-19 18:15:54 +0000 | [diff] [blame] | 392 | Dot += Sec->getSize(); |
| 393 | continue; |
| 394 | } |
George Rimar | 652852c | 2016-04-16 10:10:32 +0000 | [diff] [blame] | 395 | } |
| 396 | } |
Rui Ueyama | 52c4e17 | 2016-07-01 10:42:25 +0000 | [diff] [blame] | 397 | |
Rafael Espindola | 64c32d6 | 2016-07-07 14:28:47 +0000 | [diff] [blame] | 398 | // 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] | 399 | // memory. Set their addresses accordingly. |
Eugene Leviant | 467c4d5 | 2016-07-01 10:27:36 +0000 | [diff] [blame] | 400 | MinVA = alignDown(MinVA - Out<ELFT>::ElfHeader->getSize() - |
| 401 | Out<ELFT>::ProgramHeaders->getSize(), |
| 402 | Target->PageSize); |
| 403 | Out<ELFT>::ElfHeader->setVA(MinVA); |
| 404 | Out<ELFT>::ProgramHeaders->setVA(Out<ELFT>::ElfHeader->getSize() + MinVA); |
George Rimar | 652852c | 2016-04-16 10:10:32 +0000 | [diff] [blame] | 405 | } |
| 406 | |
Rui Ueyama | 07320e4 | 2016-04-20 20:13:41 +0000 | [diff] [blame] | 407 | template <class ELFT> |
Rafael Espindola | 74df5c7 | 2016-07-19 12:33:46 +0000 | [diff] [blame] | 408 | std::vector<PhdrEntry<ELFT>> |
Eugene Leviant | bbe3860 | 2016-07-19 09:25:43 +0000 | [diff] [blame] | 409 | LinkerScript<ELFT>::createPhdrs(ArrayRef<OutputSectionBase<ELFT> *> Sections) { |
| 410 | int TlsNum = -1; |
| 411 | int NoteNum = -1; |
| 412 | int RelroNum = -1; |
| 413 | Phdr *Load = nullptr; |
| 414 | uintX_t Flags = PF_R; |
| 415 | std::vector<Phdr> Phdrs; |
| 416 | |
| 417 | for (const PhdrsCommand &Cmd : Opt.PhdrsCommands) { |
Eugene Leviant | 865bf86 | 2016-07-21 10:43:25 +0000 | [diff] [blame] | 418 | Phdrs.emplace_back(Cmd.Type, Cmd.Flags == UINT_MAX ? PF_R : Cmd.Flags); |
Eugene Leviant | bbe3860 | 2016-07-19 09:25:43 +0000 | [diff] [blame] | 419 | Phdr &Added = Phdrs.back(); |
| 420 | |
| 421 | if (Cmd.HasFilehdr) |
Rui Ueyama | 18f084f | 2016-07-20 19:36:41 +0000 | [diff] [blame] | 422 | Added.add(Out<ELFT>::ElfHeader); |
Eugene Leviant | bbe3860 | 2016-07-19 09:25:43 +0000 | [diff] [blame] | 423 | if (Cmd.HasPhdrs) |
Rui Ueyama | 18f084f | 2016-07-20 19:36:41 +0000 | [diff] [blame] | 424 | Added.add(Out<ELFT>::ProgramHeaders); |
Eugene Leviant | bbe3860 | 2016-07-19 09:25:43 +0000 | [diff] [blame] | 425 | |
| 426 | switch (Cmd.Type) { |
| 427 | case PT_INTERP: |
Rui Ueyama | fd03cfd | 2016-07-21 11:01:23 +0000 | [diff] [blame] | 428 | if (Out<ELFT>::Interp) |
Rui Ueyama | 18f084f | 2016-07-20 19:36:41 +0000 | [diff] [blame] | 429 | Added.add(Out<ELFT>::Interp); |
Eugene Leviant | bbe3860 | 2016-07-19 09:25:43 +0000 | [diff] [blame] | 430 | break; |
| 431 | case PT_DYNAMIC: |
| 432 | if (isOutputDynamic<ELFT>()) { |
| 433 | Added.H.p_flags = toPhdrFlags(Out<ELFT>::Dynamic->getFlags()); |
Rui Ueyama | 18f084f | 2016-07-20 19:36:41 +0000 | [diff] [blame] | 434 | Added.add(Out<ELFT>::Dynamic); |
Eugene Leviant | bbe3860 | 2016-07-19 09:25:43 +0000 | [diff] [blame] | 435 | } |
| 436 | break; |
| 437 | case PT_TLS: |
| 438 | TlsNum = Phdrs.size() - 1; |
| 439 | break; |
| 440 | case PT_NOTE: |
| 441 | NoteNum = Phdrs.size() - 1; |
| 442 | break; |
| 443 | case PT_GNU_RELRO: |
| 444 | RelroNum = Phdrs.size() - 1; |
| 445 | break; |
| 446 | case PT_GNU_EH_FRAME: |
| 447 | if (!Out<ELFT>::EhFrame->empty() && Out<ELFT>::EhFrameHdr) { |
| 448 | Added.H.p_flags = toPhdrFlags(Out<ELFT>::EhFrameHdr->getFlags()); |
Rui Ueyama | 18f084f | 2016-07-20 19:36:41 +0000 | [diff] [blame] | 449 | Added.add(Out<ELFT>::EhFrameHdr); |
Eugene Leviant | bbe3860 | 2016-07-19 09:25:43 +0000 | [diff] [blame] | 450 | } |
| 451 | break; |
| 452 | } |
| 453 | } |
| 454 | |
| 455 | for (OutputSectionBase<ELFT> *Sec : Sections) { |
| 456 | if (!(Sec->getFlags() & SHF_ALLOC)) |
| 457 | break; |
| 458 | |
| 459 | if (TlsNum != -1 && (Sec->getFlags() & SHF_TLS)) |
Rui Ueyama | 18f084f | 2016-07-20 19:36:41 +0000 | [diff] [blame] | 460 | Phdrs[TlsNum].add(Sec); |
Eugene Leviant | bbe3860 | 2016-07-19 09:25:43 +0000 | [diff] [blame] | 461 | |
| 462 | if (!needsPtLoad<ELFT>(Sec)) |
| 463 | continue; |
| 464 | |
| 465 | const std::vector<size_t> &PhdrIds = |
| 466 | getPhdrIndicesForSection(Sec->getName()); |
| 467 | if (!PhdrIds.empty()) { |
| 468 | // Assign headers specified by linker script |
| 469 | for (size_t Id : PhdrIds) { |
Rui Ueyama | 18f084f | 2016-07-20 19:36:41 +0000 | [diff] [blame] | 470 | Phdrs[Id].add(Sec); |
Eugene Leviant | 865bf86 | 2016-07-21 10:43:25 +0000 | [diff] [blame] | 471 | if (Opt.PhdrsCommands[Id].Flags == UINT_MAX) |
| 472 | Phdrs[Id].H.p_flags |= toPhdrFlags(Sec->getFlags()); |
Eugene Leviant | bbe3860 | 2016-07-19 09:25:43 +0000 | [diff] [blame] | 473 | } |
| 474 | } else { |
| 475 | // If we have no load segment or flags've changed then we want new load |
| 476 | // segment. |
| 477 | uintX_t NewFlags = toPhdrFlags(Sec->getFlags()); |
| 478 | if (Load == nullptr || Flags != NewFlags) { |
| 479 | Load = &*Phdrs.emplace(Phdrs.end(), PT_LOAD, NewFlags); |
| 480 | Flags = NewFlags; |
| 481 | } |
Rui Ueyama | 18f084f | 2016-07-20 19:36:41 +0000 | [diff] [blame] | 482 | Load->add(Sec); |
Eugene Leviant | bbe3860 | 2016-07-19 09:25:43 +0000 | [diff] [blame] | 483 | } |
| 484 | |
| 485 | if (RelroNum != -1 && isRelroSection(Sec)) |
Rui Ueyama | 18f084f | 2016-07-20 19:36:41 +0000 | [diff] [blame] | 486 | Phdrs[RelroNum].add(Sec); |
Eugene Leviant | bbe3860 | 2016-07-19 09:25:43 +0000 | [diff] [blame] | 487 | if (NoteNum != -1 && Sec->getType() == SHT_NOTE) |
Rui Ueyama | 18f084f | 2016-07-20 19:36:41 +0000 | [diff] [blame] | 488 | Phdrs[NoteNum].add(Sec); |
Eugene Leviant | bbe3860 | 2016-07-19 09:25:43 +0000 | [diff] [blame] | 489 | } |
| 490 | return Phdrs; |
| 491 | } |
| 492 | |
| 493 | template <class ELFT> |
Rui Ueyama | 07320e4 | 2016-04-20 20:13:41 +0000 | [diff] [blame] | 494 | ArrayRef<uint8_t> LinkerScript<ELFT>::getFiller(StringRef Name) { |
George Rimar | f6c3cce | 2016-07-21 07:48:54 +0000 | [diff] [blame] | 495 | for (const std::unique_ptr<BaseCommand> &Base : Opt.Commands) |
| 496 | if (auto *Cmd = dyn_cast<OutputSectionCommand>(Base.get())) |
| 497 | if (Cmd->Name == Name) |
| 498 | return Cmd->Filler; |
| 499 | return {}; |
George Rimar | e2ee72b | 2016-02-26 14:48:31 +0000 | [diff] [blame] | 500 | } |
| 501 | |
Rui Ueyama | c3e2a4b | 2016-04-21 20:30:00 +0000 | [diff] [blame] | 502 | // Returns the index of the given section name in linker script |
| 503 | // SECTIONS commands. Sections are laid out as the same order as they |
| 504 | // were in the script. If a given name did not appear in the script, |
| 505 | // 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] | 506 | template <class ELFT> int LinkerScript<ELFT>::getSectionIndex(StringRef Name) { |
George Rimar | 71b26e9 | 2016-04-21 10:22:02 +0000 | [diff] [blame] | 507 | auto Begin = Opt.Commands.begin(); |
| 508 | auto End = Opt.Commands.end(); |
George Rimar | 076fe15 | 2016-07-21 06:43:01 +0000 | [diff] [blame] | 509 | auto I = |
| 510 | std::find_if(Begin, End, [&](const std::unique_ptr<BaseCommand> &Base) { |
| 511 | if (auto *Cmd = dyn_cast<OutputSectionCommand>(Base.get())) |
| 512 | if (Cmd->Name == Name) |
| 513 | return true; |
| 514 | return false; |
| 515 | }); |
Rui Ueyama | c3e2a4b | 2016-04-21 20:30:00 +0000 | [diff] [blame] | 516 | return I == End ? INT_MAX : (I - Begin); |
George Rimar | 71b26e9 | 2016-04-21 10:22:02 +0000 | [diff] [blame] | 517 | } |
| 518 | |
| 519 | // A compartor to sort output sections. Returns -1 or 1 if |
| 520 | // A or B are mentioned in linker script. Otherwise, returns 0. |
Rui Ueyama | 07320e4 | 2016-04-20 20:13:41 +0000 | [diff] [blame] | 521 | template <class ELFT> |
| 522 | int LinkerScript<ELFT>::compareSections(StringRef A, StringRef B) { |
Rui Ueyama | c3e2a4b | 2016-04-21 20:30:00 +0000 | [diff] [blame] | 523 | int I = getSectionIndex(A); |
| 524 | int J = getSectionIndex(B); |
| 525 | if (I == INT_MAX && J == INT_MAX) |
Rui Ueyama | 717677a | 2016-02-11 21:17:59 +0000 | [diff] [blame] | 526 | return 0; |
| 527 | return I < J ? -1 : 1; |
| 528 | } |
| 529 | |
George Rimar | 076fe15 | 2016-07-21 06:43:01 +0000 | [diff] [blame] | 530 | template <class ELFT> void LinkerScript<ELFT>::addScriptedSymbols() { |
| 531 | for (const std::unique_ptr<BaseCommand> &Base : Opt.Commands) |
| 532 | if (auto *Cmd = dyn_cast<SymbolAssignment>(Base.get())) |
| 533 | if (Cmd->Name != "." && Symtab<ELFT>::X->find(Cmd->Name) == nullptr) |
| 534 | Symtab<ELFT>::X->addAbsolute(Cmd->Name, STV_DEFAULT); |
Eugene Leviant | eda81a1 | 2016-07-12 06:39:48 +0000 | [diff] [blame] | 535 | } |
| 536 | |
Eugene Leviant | bbe3860 | 2016-07-19 09:25:43 +0000 | [diff] [blame] | 537 | template <class ELFT> bool LinkerScript<ELFT>::hasPhdrsCommands() { |
| 538 | return !Opt.PhdrsCommands.empty(); |
| 539 | } |
| 540 | |
| 541 | // Returns indices of ELF headers containing specific section, identified |
| 542 | // by Name. Each index is a zero based number of ELF header listed within |
| 543 | // PHDRS {} script block. |
| 544 | template <class ELFT> |
| 545 | std::vector<size_t> |
| 546 | LinkerScript<ELFT>::getPhdrIndicesForSection(StringRef Name) { |
George Rimar | 076fe15 | 2016-07-21 06:43:01 +0000 | [diff] [blame] | 547 | for (const std::unique_ptr<BaseCommand> &Base : Opt.Commands) { |
| 548 | auto *Cmd = dyn_cast<OutputSectionCommand>(Base.get()); |
| 549 | if (!Cmd || Cmd->Name != Name) |
George Rimar | 31d842f | 2016-07-20 16:43:03 +0000 | [diff] [blame] | 550 | continue; |
| 551 | |
| 552 | std::vector<size_t> Indices; |
George Rimar | 076fe15 | 2016-07-21 06:43:01 +0000 | [diff] [blame] | 553 | for (StringRef PhdrName : Cmd->Phdrs) { |
George Rimar | 31d842f | 2016-07-20 16:43:03 +0000 | [diff] [blame] | 554 | auto ItPhdr = |
| 555 | std::find_if(Opt.PhdrsCommands.rbegin(), Opt.PhdrsCommands.rend(), |
George Rimar | 076fe15 | 2016-07-21 06:43:01 +0000 | [diff] [blame] | 556 | [&](PhdrsCommand &P) { return P.Name == PhdrName; }); |
Eugene Leviant | bbe3860 | 2016-07-19 09:25:43 +0000 | [diff] [blame] | 557 | if (ItPhdr == Opt.PhdrsCommands.rend()) |
| 558 | error("section header '" + PhdrName + "' is not listed in PHDRS"); |
| 559 | else |
| 560 | Indices.push_back(std::distance(ItPhdr, Opt.PhdrsCommands.rend()) - 1); |
| 561 | } |
George Rimar | 31d842f | 2016-07-20 16:43:03 +0000 | [diff] [blame] | 562 | return Indices; |
Eugene Leviant | bbe3860 | 2016-07-19 09:25:43 +0000 | [diff] [blame] | 563 | } |
George Rimar | 31d842f | 2016-07-20 16:43:03 +0000 | [diff] [blame] | 564 | return {}; |
Eugene Leviant | bbe3860 | 2016-07-19 09:25:43 +0000 | [diff] [blame] | 565 | } |
| 566 | |
Rui Ueyama | 07320e4 | 2016-04-20 20:13:41 +0000 | [diff] [blame] | 567 | class elf::ScriptParser : public ScriptParserBase { |
George Rimar | c3794e5 | 2016-02-24 09:21:47 +0000 | [diff] [blame] | 568 | typedef void (ScriptParser::*Handler)(); |
| 569 | |
Rui Ueyama | f7c5fbb | 2015-09-30 17:23:26 +0000 | [diff] [blame] | 570 | public: |
Rui Ueyama | 07320e4 | 2016-04-20 20:13:41 +0000 | [diff] [blame] | 571 | ScriptParser(StringRef S, bool B) : ScriptParserBase(S), IsUnderSysroot(B) {} |
George Rimar | f23b232 | 2016-02-19 10:45:45 +0000 | [diff] [blame] | 572 | |
Rui Ueyama | 4a46539 | 2016-04-22 22:59:24 +0000 | [diff] [blame] | 573 | void run(); |
Rui Ueyama | f7c5fbb | 2015-09-30 17:23:26 +0000 | [diff] [blame] | 574 | |
| 575 | private: |
Rui Ueyama | 52a1509 | 2015-10-11 03:28:42 +0000 | [diff] [blame] | 576 | void addFile(StringRef Path); |
| 577 | |
Rui Ueyama | f7c5fbb | 2015-09-30 17:23:26 +0000 | [diff] [blame] | 578 | void readAsNeeded(); |
Denis Protivensky | 90c5099 | 2015-10-08 06:48:38 +0000 | [diff] [blame] | 579 | void readEntry(); |
George Rimar | 83f406c | 2015-10-19 17:35:12 +0000 | [diff] [blame] | 580 | void readExtern(); |
Rui Ueyama | f7c5fbb | 2015-09-30 17:23:26 +0000 | [diff] [blame] | 581 | void readGroup(); |
Rui Ueyama | 31aa1f8 | 2015-10-11 01:31:55 +0000 | [diff] [blame] | 582 | void readInclude(); |
George Rimar | c3794e5 | 2016-02-24 09:21:47 +0000 | [diff] [blame] | 583 | void readNothing() {} |
Rui Ueyama | ee59282 | 2015-10-07 00:25:09 +0000 | [diff] [blame] | 584 | void readOutput(); |
Davide Italiano | 9159ce9 | 2015-10-12 21:50:08 +0000 | [diff] [blame] | 585 | void readOutputArch(); |
Rui Ueyama | f7c5fbb | 2015-09-30 17:23:26 +0000 | [diff] [blame] | 586 | void readOutputFormat(); |
Eugene Leviant | bbe3860 | 2016-07-19 09:25:43 +0000 | [diff] [blame] | 587 | void readPhdrs(); |
Davide Italiano | 68a39a6 | 2015-10-08 17:51:41 +0000 | [diff] [blame] | 588 | void readSearchDir(); |
Denis Protivensky | 8e3b38a | 2015-11-12 09:52:08 +0000 | [diff] [blame] | 589 | void readSections(); |
| 590 | |
George Rimar | 652852c | 2016-04-16 10:10:32 +0000 | [diff] [blame] | 591 | void readLocationCounterValue(); |
Eugene Leviant | eda81a1 | 2016-07-12 06:39:48 +0000 | [diff] [blame] | 592 | void readOutputSectionDescription(StringRef OutSec); |
Eugene Leviant | bbe3860 | 2016-07-19 09:25:43 +0000 | [diff] [blame] | 593 | std::vector<StringRef> readOutputSectionPhdrs(); |
| 594 | unsigned readPhdrType(); |
Eugene Leviant | eda81a1 | 2016-07-12 06:39:48 +0000 | [diff] [blame] | 595 | void readSymbolAssignment(StringRef Name); |
| 596 | std::vector<StringRef> readSectionsCommandExpr(); |
Rui Ueyama | f7c5fbb | 2015-09-30 17:23:26 +0000 | [diff] [blame] | 597 | |
George Rimar | c3794e5 | 2016-02-24 09:21:47 +0000 | [diff] [blame] | 598 | const static StringMap<Handler> Cmd; |
Rui Ueyama | 07320e4 | 2016-04-20 20:13:41 +0000 | [diff] [blame] | 599 | ScriptConfiguration &Opt = *ScriptConfig; |
| 600 | StringSaver Saver = {ScriptConfig->Alloc}; |
Simon Atanasyan | 16b0cc9 | 2015-11-26 05:53:00 +0000 | [diff] [blame] | 601 | bool IsUnderSysroot; |
Rui Ueyama | f7c5fbb | 2015-09-30 17:23:26 +0000 | [diff] [blame] | 602 | }; |
Rui Ueyama | f7c5fbb | 2015-09-30 17:23:26 +0000 | [diff] [blame] | 603 | |
Rafael Espindola | e0df00b | 2016-02-28 00:25:54 +0000 | [diff] [blame] | 604 | const StringMap<elf::ScriptParser::Handler> elf::ScriptParser::Cmd = { |
George Rimar | c3794e5 | 2016-02-24 09:21:47 +0000 | [diff] [blame] | 605 | {"ENTRY", &ScriptParser::readEntry}, |
| 606 | {"EXTERN", &ScriptParser::readExtern}, |
| 607 | {"GROUP", &ScriptParser::readGroup}, |
| 608 | {"INCLUDE", &ScriptParser::readInclude}, |
| 609 | {"INPUT", &ScriptParser::readGroup}, |
| 610 | {"OUTPUT", &ScriptParser::readOutput}, |
| 611 | {"OUTPUT_ARCH", &ScriptParser::readOutputArch}, |
| 612 | {"OUTPUT_FORMAT", &ScriptParser::readOutputFormat}, |
Eugene Leviant | bbe3860 | 2016-07-19 09:25:43 +0000 | [diff] [blame] | 613 | {"PHDRS", &ScriptParser::readPhdrs}, |
George Rimar | c3794e5 | 2016-02-24 09:21:47 +0000 | [diff] [blame] | 614 | {"SEARCH_DIR", &ScriptParser::readSearchDir}, |
| 615 | {"SECTIONS", &ScriptParser::readSections}, |
| 616 | {";", &ScriptParser::readNothing}}; |
| 617 | |
Rui Ueyama | 717677a | 2016-02-11 21:17:59 +0000 | [diff] [blame] | 618 | void ScriptParser::run() { |
Rui Ueyama | f7c5fbb | 2015-09-30 17:23:26 +0000 | [diff] [blame] | 619 | while (!atEOF()) { |
| 620 | StringRef Tok = next(); |
George Rimar | c3794e5 | 2016-02-24 09:21:47 +0000 | [diff] [blame] | 621 | if (Handler Fn = Cmd.lookup(Tok)) |
| 622 | (this->*Fn)(); |
| 623 | else |
George Rimar | 5761042 | 2016-03-11 14:43:02 +0000 | [diff] [blame] | 624 | setError("unknown directive: " + Tok); |
Rui Ueyama | f7c5fbb | 2015-09-30 17:23:26 +0000 | [diff] [blame] | 625 | } |
| 626 | } |
| 627 | |
Rui Ueyama | 717677a | 2016-02-11 21:17:59 +0000 | [diff] [blame] | 628 | void ScriptParser::addFile(StringRef S) { |
Simon Atanasyan | 16b0cc9 | 2015-11-26 05:53:00 +0000 | [diff] [blame] | 629 | if (IsUnderSysroot && S.startswith("/")) { |
| 630 | SmallString<128> Path; |
| 631 | (Config->Sysroot + S).toStringRef(Path); |
| 632 | if (sys::fs::exists(Path)) { |
| 633 | Driver->addFile(Saver.save(Path.str())); |
| 634 | return; |
| 635 | } |
| 636 | } |
| 637 | |
Rui Ueyama | f03f3cc | 2015-10-13 00:09:21 +0000 | [diff] [blame] | 638 | if (sys::path::is_absolute(S)) { |
Rui Ueyama | 52a1509 | 2015-10-11 03:28:42 +0000 | [diff] [blame] | 639 | Driver->addFile(S); |
| 640 | } else if (S.startswith("=")) { |
| 641 | if (Config->Sysroot.empty()) |
| 642 | Driver->addFile(S.substr(1)); |
| 643 | else |
| 644 | Driver->addFile(Saver.save(Config->Sysroot + "/" + S.substr(1))); |
| 645 | } else if (S.startswith("-l")) { |
Rui Ueyama | 21eecb4 | 2016-02-02 21:13:09 +0000 | [diff] [blame] | 646 | Driver->addLibrary(S.substr(2)); |
Simon Atanasyan | a1b8fc3 | 2015-11-26 20:23:46 +0000 | [diff] [blame] | 647 | } else if (sys::fs::exists(S)) { |
| 648 | Driver->addFile(S); |
Rui Ueyama | 52a1509 | 2015-10-11 03:28:42 +0000 | [diff] [blame] | 649 | } else { |
| 650 | std::string Path = findFromSearchPaths(S); |
| 651 | if (Path.empty()) |
George Rimar | 777f963 | 2016-03-12 08:31:34 +0000 | [diff] [blame] | 652 | setError("unable to find " + S); |
Rui Ueyama | 025d59b | 2016-02-02 20:27:59 +0000 | [diff] [blame] | 653 | else |
| 654 | Driver->addFile(Saver.save(Path)); |
Rui Ueyama | 52a1509 | 2015-10-11 03:28:42 +0000 | [diff] [blame] | 655 | } |
| 656 | } |
| 657 | |
Rui Ueyama | 717677a | 2016-02-11 21:17:59 +0000 | [diff] [blame] | 658 | void ScriptParser::readAsNeeded() { |
Rui Ueyama | f7c5fbb | 2015-09-30 17:23:26 +0000 | [diff] [blame] | 659 | expect("("); |
Rui Ueyama | 35da9b6 | 2015-10-11 20:59:12 +0000 | [diff] [blame] | 660 | bool Orig = Config->AsNeeded; |
| 661 | Config->AsNeeded = true; |
Rui Ueyama | 025d59b | 2016-02-02 20:27:59 +0000 | [diff] [blame] | 662 | while (!Error) { |
Rui Ueyama | f7c5fbb | 2015-09-30 17:23:26 +0000 | [diff] [blame] | 663 | StringRef Tok = next(); |
| 664 | if (Tok == ")") |
Rui Ueyama | 35da9b6 | 2015-10-11 20:59:12 +0000 | [diff] [blame] | 665 | break; |
Rui Ueyama | 52a1509 | 2015-10-11 03:28:42 +0000 | [diff] [blame] | 666 | addFile(Tok); |
Rui Ueyama | f7c5fbb | 2015-09-30 17:23:26 +0000 | [diff] [blame] | 667 | } |
Rui Ueyama | 35da9b6 | 2015-10-11 20:59:12 +0000 | [diff] [blame] | 668 | Config->AsNeeded = Orig; |
Rui Ueyama | f7c5fbb | 2015-09-30 17:23:26 +0000 | [diff] [blame] | 669 | } |
| 670 | |
Rui Ueyama | 717677a | 2016-02-11 21:17:59 +0000 | [diff] [blame] | 671 | void ScriptParser::readEntry() { |
Denis Protivensky | 90c5099 | 2015-10-08 06:48:38 +0000 | [diff] [blame] | 672 | // -e <symbol> takes predecence over ENTRY(<symbol>). |
| 673 | expect("("); |
| 674 | StringRef Tok = next(); |
| 675 | if (Config->Entry.empty()) |
| 676 | Config->Entry = Tok; |
| 677 | expect(")"); |
| 678 | } |
| 679 | |
Rui Ueyama | 717677a | 2016-02-11 21:17:59 +0000 | [diff] [blame] | 680 | void ScriptParser::readExtern() { |
George Rimar | 83f406c | 2015-10-19 17:35:12 +0000 | [diff] [blame] | 681 | expect("("); |
Rui Ueyama | 025d59b | 2016-02-02 20:27:59 +0000 | [diff] [blame] | 682 | while (!Error) { |
George Rimar | 83f406c | 2015-10-19 17:35:12 +0000 | [diff] [blame] | 683 | StringRef Tok = next(); |
| 684 | if (Tok == ")") |
| 685 | return; |
| 686 | Config->Undefined.push_back(Tok); |
| 687 | } |
| 688 | } |
| 689 | |
Rui Ueyama | 717677a | 2016-02-11 21:17:59 +0000 | [diff] [blame] | 690 | void ScriptParser::readGroup() { |
Rui Ueyama | f7c5fbb | 2015-09-30 17:23:26 +0000 | [diff] [blame] | 691 | expect("("); |
Rui Ueyama | 025d59b | 2016-02-02 20:27:59 +0000 | [diff] [blame] | 692 | while (!Error) { |
Rui Ueyama | f7c5fbb | 2015-09-30 17:23:26 +0000 | [diff] [blame] | 693 | StringRef Tok = next(); |
| 694 | if (Tok == ")") |
| 695 | return; |
| 696 | if (Tok == "AS_NEEDED") { |
| 697 | readAsNeeded(); |
| 698 | continue; |
| 699 | } |
Rui Ueyama | 52a1509 | 2015-10-11 03:28:42 +0000 | [diff] [blame] | 700 | addFile(Tok); |
Rui Ueyama | f7c5fbb | 2015-09-30 17:23:26 +0000 | [diff] [blame] | 701 | } |
| 702 | } |
| 703 | |
Rui Ueyama | 717677a | 2016-02-11 21:17:59 +0000 | [diff] [blame] | 704 | void ScriptParser::readInclude() { |
Rui Ueyama | 31aa1f8 | 2015-10-11 01:31:55 +0000 | [diff] [blame] | 705 | StringRef Tok = next(); |
| 706 | auto MBOrErr = MemoryBuffer::getFile(Tok); |
Rui Ueyama | 025d59b | 2016-02-02 20:27:59 +0000 | [diff] [blame] | 707 | if (!MBOrErr) { |
George Rimar | 5761042 | 2016-03-11 14:43:02 +0000 | [diff] [blame] | 708 | setError("cannot open " + Tok); |
Rui Ueyama | 025d59b | 2016-02-02 20:27:59 +0000 | [diff] [blame] | 709 | return; |
| 710 | } |
Rui Ueyama | 31aa1f8 | 2015-10-11 01:31:55 +0000 | [diff] [blame] | 711 | std::unique_ptr<MemoryBuffer> &MB = *MBOrErr; |
Rui Ueyama | a47ee68 | 2015-10-11 01:53:04 +0000 | [diff] [blame] | 712 | StringRef S = Saver.save(MB->getMemBufferRef().getBuffer()); |
| 713 | std::vector<StringRef> V = tokenize(S); |
Rui Ueyama | 31aa1f8 | 2015-10-11 01:31:55 +0000 | [diff] [blame] | 714 | Tokens.insert(Tokens.begin() + Pos, V.begin(), V.end()); |
Rui Ueyama | 31aa1f8 | 2015-10-11 01:31:55 +0000 | [diff] [blame] | 715 | } |
| 716 | |
Rui Ueyama | 717677a | 2016-02-11 21:17:59 +0000 | [diff] [blame] | 717 | void ScriptParser::readOutput() { |
Rui Ueyama | ee59282 | 2015-10-07 00:25:09 +0000 | [diff] [blame] | 718 | // -o <file> takes predecence over OUTPUT(<file>). |
| 719 | expect("("); |
| 720 | StringRef Tok = next(); |
| 721 | if (Config->OutputFile.empty()) |
| 722 | Config->OutputFile = Tok; |
| 723 | expect(")"); |
| 724 | } |
| 725 | |
Rui Ueyama | 717677a | 2016-02-11 21:17:59 +0000 | [diff] [blame] | 726 | void ScriptParser::readOutputArch() { |
Davide Italiano | 9159ce9 | 2015-10-12 21:50:08 +0000 | [diff] [blame] | 727 | // Error checking only for now. |
| 728 | expect("("); |
| 729 | next(); |
| 730 | expect(")"); |
| 731 | } |
| 732 | |
Rui Ueyama | 717677a | 2016-02-11 21:17:59 +0000 | [diff] [blame] | 733 | void ScriptParser::readOutputFormat() { |
Rui Ueyama | f7c5fbb | 2015-09-30 17:23:26 +0000 | [diff] [blame] | 734 | // Error checking only for now. |
| 735 | expect("("); |
| 736 | next(); |
Davide Italiano | 6836c61 | 2015-10-12 21:08:41 +0000 | [diff] [blame] | 737 | StringRef Tok = next(); |
| 738 | if (Tok == ")") |
| 739 | return; |
Rui Ueyama | 025d59b | 2016-02-02 20:27:59 +0000 | [diff] [blame] | 740 | if (Tok != ",") { |
George Rimar | 5761042 | 2016-03-11 14:43:02 +0000 | [diff] [blame] | 741 | setError("unexpected token: " + Tok); |
Rui Ueyama | 025d59b | 2016-02-02 20:27:59 +0000 | [diff] [blame] | 742 | return; |
| 743 | } |
Davide Italiano | 6836c61 | 2015-10-12 21:08:41 +0000 | [diff] [blame] | 744 | next(); |
| 745 | expect(","); |
| 746 | next(); |
Rui Ueyama | f7c5fbb | 2015-09-30 17:23:26 +0000 | [diff] [blame] | 747 | expect(")"); |
| 748 | } |
| 749 | |
Eugene Leviant | bbe3860 | 2016-07-19 09:25:43 +0000 | [diff] [blame] | 750 | void ScriptParser::readPhdrs() { |
| 751 | expect("{"); |
| 752 | while (!Error && !skip("}")) { |
| 753 | StringRef Tok = next(); |
Eugene Leviant | 865bf86 | 2016-07-21 10:43:25 +0000 | [diff] [blame] | 754 | Opt.PhdrsCommands.push_back({Tok, PT_NULL, false, false, UINT_MAX}); |
Eugene Leviant | bbe3860 | 2016-07-19 09:25:43 +0000 | [diff] [blame] | 755 | PhdrsCommand &PhdrCmd = Opt.PhdrsCommands.back(); |
| 756 | |
| 757 | PhdrCmd.Type = readPhdrType(); |
| 758 | do { |
| 759 | Tok = next(); |
| 760 | if (Tok == ";") |
| 761 | break; |
| 762 | if (Tok == "FILEHDR") |
| 763 | PhdrCmd.HasFilehdr = true; |
| 764 | else if (Tok == "PHDRS") |
| 765 | PhdrCmd.HasPhdrs = true; |
Eugene Leviant | 865bf86 | 2016-07-21 10:43:25 +0000 | [diff] [blame] | 766 | else if (Tok == "FLAGS") { |
| 767 | expect("("); |
| 768 | next().getAsInteger(0, PhdrCmd.Flags); |
| 769 | expect(")"); |
| 770 | } else |
Eugene Leviant | bbe3860 | 2016-07-19 09:25:43 +0000 | [diff] [blame] | 771 | setError("unexpected header attribute: " + Tok); |
| 772 | } while (!Error); |
| 773 | } |
| 774 | } |
| 775 | |
Rui Ueyama | 717677a | 2016-02-11 21:17:59 +0000 | [diff] [blame] | 776 | void ScriptParser::readSearchDir() { |
Davide Italiano | 68a39a6 | 2015-10-08 17:51:41 +0000 | [diff] [blame] | 777 | expect("("); |
Rafael Espindola | 0650192 | 2016-03-08 17:13:12 +0000 | [diff] [blame] | 778 | Config->SearchPaths.push_back(next()); |
Davide Italiano | 68a39a6 | 2015-10-08 17:51:41 +0000 | [diff] [blame] | 779 | expect(")"); |
| 780 | } |
| 781 | |
Rui Ueyama | 717677a | 2016-02-11 21:17:59 +0000 | [diff] [blame] | 782 | void ScriptParser::readSections() { |
Rui Ueyama | 07320e4 | 2016-04-20 20:13:41 +0000 | [diff] [blame] | 783 | Opt.DoLayout = true; |
Denis Protivensky | 8e3b38a | 2015-11-12 09:52:08 +0000 | [diff] [blame] | 784 | expect("{"); |
George Rimar | 652852c | 2016-04-16 10:10:32 +0000 | [diff] [blame] | 785 | while (!Error && !skip("}")) { |
| 786 | StringRef Tok = peek(); |
Eugene Leviant | eda81a1 | 2016-07-12 06:39:48 +0000 | [diff] [blame] | 787 | if (Tok == ".") { |
George Rimar | 652852c | 2016-04-16 10:10:32 +0000 | [diff] [blame] | 788 | readLocationCounterValue(); |
Eugene Leviant | eda81a1 | 2016-07-12 06:39:48 +0000 | [diff] [blame] | 789 | continue; |
| 790 | } |
| 791 | next(); |
| 792 | if (peek() == "=") |
| 793 | readSymbolAssignment(Tok); |
George Rimar | 652852c | 2016-04-16 10:10:32 +0000 | [diff] [blame] | 794 | else |
Eugene Leviant | eda81a1 | 2016-07-12 06:39:48 +0000 | [diff] [blame] | 795 | readOutputSectionDescription(Tok); |
George Rimar | 652852c | 2016-04-16 10:10:32 +0000 | [diff] [blame] | 796 | } |
Denis Protivensky | 8e3b38a | 2015-11-12 09:52:08 +0000 | [diff] [blame] | 797 | } |
| 798 | |
George Rimar | 652852c | 2016-04-16 10:10:32 +0000 | [diff] [blame] | 799 | void ScriptParser::readLocationCounterValue() { |
| 800 | expect("."); |
| 801 | expect("="); |
Eugene Leviant | eda81a1 | 2016-07-12 06:39:48 +0000 | [diff] [blame] | 802 | std::vector<StringRef> Expr = readSectionsCommandExpr(); |
| 803 | if (Expr.empty()) |
George Rimar | 652852c | 2016-04-16 10:10:32 +0000 | [diff] [blame] | 804 | error("error in location counter expression"); |
Eugene Leviant | eda81a1 | 2016-07-12 06:39:48 +0000 | [diff] [blame] | 805 | else |
George Rimar | 076fe15 | 2016-07-21 06:43:01 +0000 | [diff] [blame] | 806 | Opt.Commands.push_back(llvm::make_unique<SymbolAssignment>(".", Expr)); |
George Rimar | 652852c | 2016-04-16 10:10:32 +0000 | [diff] [blame] | 807 | } |
| 808 | |
Eugene Leviant | eda81a1 | 2016-07-12 06:39:48 +0000 | [diff] [blame] | 809 | void ScriptParser::readOutputSectionDescription(StringRef OutSec) { |
George Rimar | 076fe15 | 2016-07-21 06:43:01 +0000 | [diff] [blame] | 810 | OutputSectionCommand *Cmd = new OutputSectionCommand(OutSec); |
| 811 | Opt.Commands.emplace_back(Cmd); |
Denis Protivensky | 8e3b38a | 2015-11-12 09:52:08 +0000 | [diff] [blame] | 812 | expect(":"); |
Davide Italiano | 246f681 | 2016-07-22 03:36:24 +0000 | [diff] [blame^] | 813 | |
| 814 | // Parse constraints. |
| 815 | if (skip("ONLY_IF_RO")) |
| 816 | Cmd->Constraint = ReadOnly; |
| 817 | if (skip("ONLY_IF_RW")) |
| 818 | Cmd->Constraint = ReadWrite; |
Denis Protivensky | 8e3b38a | 2015-11-12 09:52:08 +0000 | [diff] [blame] | 819 | expect("{"); |
Rui Ueyama | 8ec77e6 | 2016-04-21 22:00:51 +0000 | [diff] [blame] | 820 | |
Rui Ueyama | 025d59b | 2016-02-02 20:27:59 +0000 | [diff] [blame] | 821 | while (!Error && !skip("}")) { |
George Rimar | 481c2ce | 2016-02-23 07:47:54 +0000 | [diff] [blame] | 822 | StringRef Tok = next(); |
| 823 | if (Tok == "*") { |
George Rimar | eea3114 | 2016-07-21 14:26:59 +0000 | [diff] [blame] | 824 | auto *InCmd = new InputSectionDescription(); |
| 825 | Cmd->Commands.emplace_back(InCmd); |
Rui Ueyama | 8ec77e6 | 2016-04-21 22:00:51 +0000 | [diff] [blame] | 826 | expect("("); |
| 827 | while (!Error && !skip(")")) |
George Rimar | eea3114 | 2016-07-21 14:26:59 +0000 | [diff] [blame] | 828 | InCmd->Patterns.push_back(next()); |
George Rimar | 481c2ce | 2016-02-23 07:47:54 +0000 | [diff] [blame] | 829 | } else if (Tok == "KEEP") { |
| 830 | expect("("); |
Rui Ueyama | 8ec77e6 | 2016-04-21 22:00:51 +0000 | [diff] [blame] | 831 | expect("*"); |
| 832 | expect("("); |
George Rimar | eea3114 | 2016-07-21 14:26:59 +0000 | [diff] [blame] | 833 | auto *InCmd = new InputSectionDescription(); |
| 834 | Cmd->Commands.emplace_back(InCmd); |
Rui Ueyama | 8ec77e6 | 2016-04-21 22:00:51 +0000 | [diff] [blame] | 835 | while (!Error && !skip(")")) { |
George Rimar | eea3114 | 2016-07-21 14:26:59 +0000 | [diff] [blame] | 836 | Opt.KeptSections.push_back(peek()); |
| 837 | InCmd->Patterns.push_back(next()); |
Rui Ueyama | 8ec77e6 | 2016-04-21 22:00:51 +0000 | [diff] [blame] | 838 | } |
George Rimar | 481c2ce | 2016-02-23 07:47:54 +0000 | [diff] [blame] | 839 | expect(")"); |
| 840 | } else { |
George Rimar | 777f963 | 2016-03-12 08:31:34 +0000 | [diff] [blame] | 841 | setError("unknown command " + Tok); |
George Rimar | 481c2ce | 2016-02-23 07:47:54 +0000 | [diff] [blame] | 842 | } |
Denis Protivensky | 8e3b38a | 2015-11-12 09:52:08 +0000 | [diff] [blame] | 843 | } |
George Rimar | 076fe15 | 2016-07-21 06:43:01 +0000 | [diff] [blame] | 844 | Cmd->Phdrs = readOutputSectionPhdrs(); |
Rui Ueyama | 8ec77e6 | 2016-04-21 22:00:51 +0000 | [diff] [blame] | 845 | |
George Rimar | e2ee72b | 2016-02-26 14:48:31 +0000 | [diff] [blame] | 846 | StringRef Tok = peek(); |
| 847 | if (Tok.startswith("=")) { |
| 848 | if (!Tok.startswith("=0x")) { |
Rui Ueyama | 3ed2f06 | 2016-03-13 03:17:44 +0000 | [diff] [blame] | 849 | setError("filler should be a hexadecimal value"); |
George Rimar | e2ee72b | 2016-02-26 14:48:31 +0000 | [diff] [blame] | 850 | return; |
| 851 | } |
Rui Ueyama | 3e80897 | 2016-02-28 05:09:11 +0000 | [diff] [blame] | 852 | Tok = Tok.substr(3); |
George Rimar | f6c3cce | 2016-07-21 07:48:54 +0000 | [diff] [blame] | 853 | Cmd->Filler = parseHex(Tok); |
George Rimar | e2ee72b | 2016-02-26 14:48:31 +0000 | [diff] [blame] | 854 | next(); |
| 855 | } |
Denis Protivensky | 8e3b38a | 2015-11-12 09:52:08 +0000 | [diff] [blame] | 856 | } |
| 857 | |
Eugene Leviant | eda81a1 | 2016-07-12 06:39:48 +0000 | [diff] [blame] | 858 | void ScriptParser::readSymbolAssignment(StringRef Name) { |
| 859 | expect("="); |
| 860 | std::vector<StringRef> Expr = readSectionsCommandExpr(); |
| 861 | if (Expr.empty()) |
| 862 | error("error in symbol assignment expression"); |
| 863 | else |
George Rimar | 076fe15 | 2016-07-21 06:43:01 +0000 | [diff] [blame] | 864 | Opt.Commands.push_back(llvm::make_unique<SymbolAssignment>(Name, Expr)); |
Eugene Leviant | eda81a1 | 2016-07-12 06:39:48 +0000 | [diff] [blame] | 865 | } |
| 866 | |
| 867 | std::vector<StringRef> ScriptParser::readSectionsCommandExpr() { |
| 868 | std::vector<StringRef> Expr; |
| 869 | while (!Error) { |
| 870 | StringRef Tok = next(); |
| 871 | if (Tok == ";") |
| 872 | break; |
| 873 | Expr.push_back(Tok); |
| 874 | } |
| 875 | return Expr; |
| 876 | } |
| 877 | |
Eugene Leviant | bbe3860 | 2016-07-19 09:25:43 +0000 | [diff] [blame] | 878 | std::vector<StringRef> ScriptParser::readOutputSectionPhdrs() { |
| 879 | std::vector<StringRef> Phdrs; |
| 880 | while (!Error && peek().startswith(":")) { |
| 881 | StringRef Tok = next(); |
| 882 | Tok = (Tok.size() == 1) ? next() : Tok.substr(1); |
| 883 | if (Tok.empty()) { |
| 884 | setError("section header name is empty"); |
| 885 | break; |
| 886 | } |
Rui Ueyama | 047404f | 2016-07-20 19:36:36 +0000 | [diff] [blame] | 887 | Phdrs.push_back(Tok); |
Eugene Leviant | bbe3860 | 2016-07-19 09:25:43 +0000 | [diff] [blame] | 888 | } |
| 889 | return Phdrs; |
| 890 | } |
| 891 | |
| 892 | unsigned ScriptParser::readPhdrType() { |
Eugene Leviant | bbe3860 | 2016-07-19 09:25:43 +0000 | [diff] [blame] | 893 | StringRef Tok = next(); |
Rui Ueyama | b0f6c59 | 2016-07-20 19:36:38 +0000 | [diff] [blame] | 894 | unsigned Ret = StringSwitch<unsigned>(Tok) |
| 895 | .Case("PT_NULL", PT_NULL) |
| 896 | .Case("PT_LOAD", PT_LOAD) |
| 897 | .Case("PT_DYNAMIC", PT_DYNAMIC) |
| 898 | .Case("PT_INTERP", PT_INTERP) |
| 899 | .Case("PT_NOTE", PT_NOTE) |
| 900 | .Case("PT_SHLIB", PT_SHLIB) |
| 901 | .Case("PT_PHDR", PT_PHDR) |
| 902 | .Case("PT_TLS", PT_TLS) |
| 903 | .Case("PT_GNU_EH_FRAME", PT_GNU_EH_FRAME) |
| 904 | .Case("PT_GNU_STACK", PT_GNU_STACK) |
| 905 | .Case("PT_GNU_RELRO", PT_GNU_RELRO) |
| 906 | .Default(-1); |
Eugene Leviant | bbe3860 | 2016-07-19 09:25:43 +0000 | [diff] [blame] | 907 | |
Rui Ueyama | b0f6c59 | 2016-07-20 19:36:38 +0000 | [diff] [blame] | 908 | if (Ret == (unsigned)-1) { |
| 909 | setError("invalid program header type: " + Tok); |
| 910 | return PT_NULL; |
| 911 | } |
| 912 | return Ret; |
Eugene Leviant | bbe3860 | 2016-07-19 09:25:43 +0000 | [diff] [blame] | 913 | } |
| 914 | |
Simon Atanasyan | 16b0cc9 | 2015-11-26 05:53:00 +0000 | [diff] [blame] | 915 | static bool isUnderSysroot(StringRef Path) { |
| 916 | if (Config->Sysroot == "") |
| 917 | return false; |
| 918 | for (; !Path.empty(); Path = sys::path::parent_path(Path)) |
| 919 | if (sys::fs::equivalent(Config->Sysroot, Path)) |
| 920 | return true; |
| 921 | return false; |
| 922 | } |
| 923 | |
Rui Ueyama | 07320e4 | 2016-04-20 20:13:41 +0000 | [diff] [blame] | 924 | // Entry point. |
| 925 | void elf::readLinkerScript(MemoryBufferRef MB) { |
Simon Atanasyan | 16b0cc9 | 2015-11-26 05:53:00 +0000 | [diff] [blame] | 926 | StringRef Path = MB.getBufferIdentifier(); |
Rui Ueyama | 07320e4 | 2016-04-20 20:13:41 +0000 | [diff] [blame] | 927 | ScriptParser(MB.getBuffer(), isUnderSysroot(Path)).run(); |
Rui Ueyama | f7c5fbb | 2015-09-30 17:23:26 +0000 | [diff] [blame] | 928 | } |
Rui Ueyama | 1ebc8ed | 2016-02-12 21:47:28 +0000 | [diff] [blame] | 929 | |
Rui Ueyama | 07320e4 | 2016-04-20 20:13:41 +0000 | [diff] [blame] | 930 | template class elf::LinkerScript<ELF32LE>; |
| 931 | template class elf::LinkerScript<ELF32BE>; |
| 932 | template class elf::LinkerScript<ELF64LE>; |
| 933 | template class elf::LinkerScript<ELF64BE>; |