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