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