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