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) { |
Rui Ueyama | 1ebc8ed | 2016-02-12 21:47:28 +0000 | [diff] [blame] | 199 | return 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 | 07320e4 | 2016-04-20 20:13:41 +0000 | [diff] [blame] | 211 | void LinkerScript<ELFT>::assignAddresses( |
George Rimar | dbbd8b1 | 2016-04-21 11:21:48 +0000 | [diff] [blame] | 212 | ArrayRef<OutputSectionBase<ELFT> *> Sections) { |
George Rimar | 652852c | 2016-04-16 10:10:32 +0000 | [diff] [blame] | 213 | // Orphan sections are sections present in the input files which |
Rui Ueyama | 7c18c28 | 2016-04-18 21:00:40 +0000 | [diff] [blame] | 214 | // are not explicitly placed into the output file by the linker script. |
| 215 | // We place orphan sections at end of file. |
| 216 | // Other linkers places them using some heuristics as described in |
George Rimar | 652852c | 2016-04-16 10:10:32 +0000 | [diff] [blame] | 217 | // https://sourceware.org/binutils/docs/ld/Orphan-Sections.html#Orphan-Sections. |
Rui Ueyama | 7c18c28 | 2016-04-18 21:00:40 +0000 | [diff] [blame] | 218 | for (OutputSectionBase<ELFT> *Sec : Sections) { |
George Rimar | 652852c | 2016-04-16 10:10:32 +0000 | [diff] [blame] | 219 | StringRef Name = Sec->getName(); |
Rui Ueyama | c3e2a4b | 2016-04-21 20:30:00 +0000 | [diff] [blame] | 220 | if (getSectionIndex(Name) == INT_MAX) |
Eugene Leviant | bbe3860 | 2016-07-19 09:25:43 +0000 | [diff] [blame] | 221 | Opt.Commands.push_back({SectionKind, {}, Name, {}}); |
George Rimar | 652852c | 2016-04-16 10:10:32 +0000 | [diff] [blame] | 222 | } |
George Rimar | 652852c | 2016-04-16 10:10:32 +0000 | [diff] [blame] | 223 | |
Rui Ueyama | 7c18c28 | 2016-04-18 21:00:40 +0000 | [diff] [blame] | 224 | // Assign addresses as instructed by linker script SECTIONS sub-commands. |
Rui Ueyama | 52c4e17 | 2016-07-01 10:42:25 +0000 | [diff] [blame] | 225 | Dot = Out<ELFT>::ElfHeader->getSize() + Out<ELFT>::ProgramHeaders->getSize(); |
Eugene Leviant | 467c4d5 | 2016-07-01 10:27:36 +0000 | [diff] [blame] | 226 | uintX_t MinVA = std::numeric_limits<uintX_t>::max(); |
George Rimar | 652852c | 2016-04-16 10:10:32 +0000 | [diff] [blame] | 227 | uintX_t ThreadBssOffset = 0; |
George Rimar | 652852c | 2016-04-16 10:10:32 +0000 | [diff] [blame] | 228 | |
Rui Ueyama | 07320e4 | 2016-04-20 20:13:41 +0000 | [diff] [blame] | 229 | for (SectionsCommand &Cmd : Opt.Commands) { |
Rui Ueyama | 05ef4cf | 2016-07-15 04:19:37 +0000 | [diff] [blame] | 230 | if (Cmd.Kind == AssignmentKind) { |
| 231 | uint64_t Val = evalExpr(Cmd.Expr, Dot); |
| 232 | |
| 233 | if (Cmd.Name == ".") { |
| 234 | Dot = Val; |
| 235 | } else { |
| 236 | auto *D = cast<DefinedRegular<ELFT>>(Symtab<ELFT>::X->find(Cmd.Name)); |
| 237 | D->Value = Val; |
| 238 | } |
George Rimar | 652852c | 2016-04-16 10:10:32 +0000 | [diff] [blame] | 239 | continue; |
| 240 | } |
| 241 | |
Dima Stepanov | fb8978f | 2016-05-19 18:15:54 +0000 | [diff] [blame] | 242 | // Find all the sections with required name. There can be more than |
George Rimar | 6ad330a | 2016-07-19 07:39:07 +0000 | [diff] [blame] | 243 | // one section with such name, if the alignment, flags or type |
Dima Stepanov | fb8978f | 2016-05-19 18:15:54 +0000 | [diff] [blame] | 244 | // attribute differs. |
Rui Ueyama | 05ef4cf | 2016-07-15 04:19:37 +0000 | [diff] [blame] | 245 | assert(Cmd.Kind == SectionKind); |
Dima Stepanov | fb8978f | 2016-05-19 18:15:54 +0000 | [diff] [blame] | 246 | for (OutputSectionBase<ELFT> *Sec : Sections) { |
Eugene Leviant | eda81a1 | 2016-07-12 06:39:48 +0000 | [diff] [blame] | 247 | if (Sec->getName() != Cmd.Name) |
Dima Stepanov | fb8978f | 2016-05-19 18:15:54 +0000 | [diff] [blame] | 248 | continue; |
George Rimar | 652852c | 2016-04-16 10:10:32 +0000 | [diff] [blame] | 249 | |
Dima Stepanov | fb8978f | 2016-05-19 18:15:54 +0000 | [diff] [blame] | 250 | if ((Sec->getFlags() & SHF_TLS) && Sec->getType() == SHT_NOBITS) { |
| 251 | uintX_t TVA = Dot + ThreadBssOffset; |
Rui Ueyama | 424b408 | 2016-06-17 01:18:46 +0000 | [diff] [blame] | 252 | TVA = alignTo(TVA, Sec->getAlignment()); |
Dima Stepanov | fb8978f | 2016-05-19 18:15:54 +0000 | [diff] [blame] | 253 | Sec->setVA(TVA); |
| 254 | ThreadBssOffset = TVA - Dot + Sec->getSize(); |
| 255 | continue; |
| 256 | } |
George Rimar | 652852c | 2016-04-16 10:10:32 +0000 | [diff] [blame] | 257 | |
Dima Stepanov | fb8978f | 2016-05-19 18:15:54 +0000 | [diff] [blame] | 258 | if (Sec->getFlags() & SHF_ALLOC) { |
Rui Ueyama | 424b408 | 2016-06-17 01:18:46 +0000 | [diff] [blame] | 259 | Dot = alignTo(Dot, Sec->getAlignment()); |
Dima Stepanov | fb8978f | 2016-05-19 18:15:54 +0000 | [diff] [blame] | 260 | Sec->setVA(Dot); |
Rui Ueyama | 52c4e17 | 2016-07-01 10:42:25 +0000 | [diff] [blame] | 261 | MinVA = std::min(MinVA, Dot); |
Dima Stepanov | fb8978f | 2016-05-19 18:15:54 +0000 | [diff] [blame] | 262 | Dot += Sec->getSize(); |
| 263 | continue; |
| 264 | } |
George Rimar | 652852c | 2016-04-16 10:10:32 +0000 | [diff] [blame] | 265 | } |
| 266 | } |
Rui Ueyama | 52c4e17 | 2016-07-01 10:42:25 +0000 | [diff] [blame] | 267 | |
Rafael Espindola | 64c32d6 | 2016-07-07 14:28:47 +0000 | [diff] [blame] | 268 | // 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] | 269 | // memory. Set their addresses accordingly. |
Eugene Leviant | 467c4d5 | 2016-07-01 10:27:36 +0000 | [diff] [blame] | 270 | MinVA = alignDown(MinVA - Out<ELFT>::ElfHeader->getSize() - |
| 271 | Out<ELFT>::ProgramHeaders->getSize(), |
| 272 | Target->PageSize); |
| 273 | Out<ELFT>::ElfHeader->setVA(MinVA); |
| 274 | Out<ELFT>::ProgramHeaders->setVA(Out<ELFT>::ElfHeader->getSize() + MinVA); |
George Rimar | 652852c | 2016-04-16 10:10:32 +0000 | [diff] [blame] | 275 | } |
| 276 | |
Rui Ueyama | 07320e4 | 2016-04-20 20:13:41 +0000 | [diff] [blame] | 277 | template <class ELFT> |
Rafael Espindola | 74df5c7 | 2016-07-19 12:33:46 +0000 | [diff] [blame^] | 278 | std::vector<PhdrEntry<ELFT>> |
Eugene Leviant | bbe3860 | 2016-07-19 09:25:43 +0000 | [diff] [blame] | 279 | LinkerScript<ELFT>::createPhdrs(ArrayRef<OutputSectionBase<ELFT> *> Sections) { |
| 280 | int TlsNum = -1; |
| 281 | int NoteNum = -1; |
| 282 | int RelroNum = -1; |
| 283 | Phdr *Load = nullptr; |
| 284 | uintX_t Flags = PF_R; |
| 285 | std::vector<Phdr> Phdrs; |
| 286 | |
| 287 | for (const PhdrsCommand &Cmd : Opt.PhdrsCommands) { |
| 288 | Phdrs.emplace_back(Cmd.Type, PF_R); |
| 289 | Phdr &Added = Phdrs.back(); |
| 290 | |
| 291 | if (Cmd.HasFilehdr) |
| 292 | Added.AddSec(Out<ELFT>::ElfHeader); |
| 293 | if (Cmd.HasPhdrs) |
| 294 | Added.AddSec(Out<ELFT>::ProgramHeaders); |
| 295 | |
| 296 | switch (Cmd.Type) { |
| 297 | case PT_INTERP: |
| 298 | if (needsInterpSection<ELFT>()) |
| 299 | Added.AddSec(Out<ELFT>::Interp); |
| 300 | break; |
| 301 | case PT_DYNAMIC: |
| 302 | if (isOutputDynamic<ELFT>()) { |
| 303 | Added.H.p_flags = toPhdrFlags(Out<ELFT>::Dynamic->getFlags()); |
| 304 | Added.AddSec(Out<ELFT>::Dynamic); |
| 305 | } |
| 306 | break; |
| 307 | case PT_TLS: |
| 308 | TlsNum = Phdrs.size() - 1; |
| 309 | break; |
| 310 | case PT_NOTE: |
| 311 | NoteNum = Phdrs.size() - 1; |
| 312 | break; |
| 313 | case PT_GNU_RELRO: |
| 314 | RelroNum = Phdrs.size() - 1; |
| 315 | break; |
| 316 | case PT_GNU_EH_FRAME: |
| 317 | if (!Out<ELFT>::EhFrame->empty() && Out<ELFT>::EhFrameHdr) { |
| 318 | Added.H.p_flags = toPhdrFlags(Out<ELFT>::EhFrameHdr->getFlags()); |
| 319 | Added.AddSec(Out<ELFT>::EhFrameHdr); |
| 320 | } |
| 321 | break; |
| 322 | } |
| 323 | } |
| 324 | |
| 325 | for (OutputSectionBase<ELFT> *Sec : Sections) { |
| 326 | if (!(Sec->getFlags() & SHF_ALLOC)) |
| 327 | break; |
| 328 | |
| 329 | if (TlsNum != -1 && (Sec->getFlags() & SHF_TLS)) |
| 330 | Phdrs[TlsNum].AddSec(Sec); |
| 331 | |
| 332 | if (!needsPtLoad<ELFT>(Sec)) |
| 333 | continue; |
| 334 | |
| 335 | const std::vector<size_t> &PhdrIds = |
| 336 | getPhdrIndicesForSection(Sec->getName()); |
| 337 | if (!PhdrIds.empty()) { |
| 338 | // Assign headers specified by linker script |
| 339 | for (size_t Id : PhdrIds) { |
| 340 | Phdrs[Id].AddSec(Sec); |
| 341 | Phdrs[Id].H.p_flags |= toPhdrFlags(Sec->getFlags()); |
| 342 | } |
| 343 | } else { |
| 344 | // If we have no load segment or flags've changed then we want new load |
| 345 | // segment. |
| 346 | uintX_t NewFlags = toPhdrFlags(Sec->getFlags()); |
| 347 | if (Load == nullptr || Flags != NewFlags) { |
| 348 | Load = &*Phdrs.emplace(Phdrs.end(), PT_LOAD, NewFlags); |
| 349 | Flags = NewFlags; |
| 350 | } |
| 351 | Load->AddSec(Sec); |
| 352 | } |
| 353 | |
| 354 | if (RelroNum != -1 && isRelroSection(Sec)) |
| 355 | Phdrs[RelroNum].AddSec(Sec); |
| 356 | if (NoteNum != -1 && Sec->getType() == SHT_NOTE) |
| 357 | Phdrs[NoteNum].AddSec(Sec); |
| 358 | } |
| 359 | return Phdrs; |
| 360 | } |
| 361 | |
| 362 | template <class ELFT> |
Rui Ueyama | 07320e4 | 2016-04-20 20:13:41 +0000 | [diff] [blame] | 363 | ArrayRef<uint8_t> LinkerScript<ELFT>::getFiller(StringRef Name) { |
| 364 | auto I = Opt.Filler.find(Name); |
| 365 | if (I == Opt.Filler.end()) |
Rui Ueyama | 3e80897 | 2016-02-28 05:09:11 +0000 | [diff] [blame] | 366 | return {}; |
| 367 | return I->second; |
George Rimar | e2ee72b | 2016-02-26 14:48:31 +0000 | [diff] [blame] | 368 | } |
| 369 | |
Rui Ueyama | c3e2a4b | 2016-04-21 20:30:00 +0000 | [diff] [blame] | 370 | // Returns the index of the given section name in linker script |
| 371 | // SECTIONS commands. Sections are laid out as the same order as they |
| 372 | // were in the script. If a given name did not appear in the script, |
| 373 | // 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] | 374 | template <class ELFT> |
Rui Ueyama | c3e2a4b | 2016-04-21 20:30:00 +0000 | [diff] [blame] | 375 | int LinkerScript<ELFT>::getSectionIndex(StringRef Name) { |
George Rimar | 71b26e9 | 2016-04-21 10:22:02 +0000 | [diff] [blame] | 376 | auto Begin = Opt.Commands.begin(); |
| 377 | auto End = Opt.Commands.end(); |
| 378 | auto I = std::find_if(Begin, End, [&](SectionsCommand &N) { |
Eugene Leviant | eda81a1 | 2016-07-12 06:39:48 +0000 | [diff] [blame] | 379 | return N.Kind == SectionKind && N.Name == Name; |
George Rimar | 71b26e9 | 2016-04-21 10:22:02 +0000 | [diff] [blame] | 380 | }); |
Rui Ueyama | c3e2a4b | 2016-04-21 20:30:00 +0000 | [diff] [blame] | 381 | return I == End ? INT_MAX : (I - Begin); |
George Rimar | 71b26e9 | 2016-04-21 10:22:02 +0000 | [diff] [blame] | 382 | } |
| 383 | |
| 384 | // A compartor to sort output sections. Returns -1 or 1 if |
| 385 | // A or B are mentioned in linker script. Otherwise, returns 0. |
Rui Ueyama | 07320e4 | 2016-04-20 20:13:41 +0000 | [diff] [blame] | 386 | template <class ELFT> |
| 387 | int LinkerScript<ELFT>::compareSections(StringRef A, StringRef B) { |
Rui Ueyama | c3e2a4b | 2016-04-21 20:30:00 +0000 | [diff] [blame] | 388 | int I = getSectionIndex(A); |
| 389 | int J = getSectionIndex(B); |
| 390 | if (I == INT_MAX && J == INT_MAX) |
Rui Ueyama | 717677a | 2016-02-11 21:17:59 +0000 | [diff] [blame] | 391 | return 0; |
| 392 | return I < J ? -1 : 1; |
| 393 | } |
| 394 | |
Eugene Leviant | b030411 | 2016-07-14 09:21:24 +0000 | [diff] [blame] | 395 | template <class ELFT> |
| 396 | void LinkerScript<ELFT>::addScriptedSymbols() { |
Eugene Leviant | eda81a1 | 2016-07-12 06:39:48 +0000 | [diff] [blame] | 397 | for (SectionsCommand &Cmd : Opt.Commands) |
Rui Ueyama | 05ef4cf | 2016-07-15 04:19:37 +0000 | [diff] [blame] | 398 | if (Cmd.Kind == AssignmentKind) |
Eugene Leviant | 0e36f42 | 2016-07-15 11:20:04 +0000 | [diff] [blame] | 399 | if (Cmd.Name != "." && Symtab<ELFT>::X->find(Cmd.Name) == nullptr) |
Rui Ueyama | 05ef4cf | 2016-07-15 04:19:37 +0000 | [diff] [blame] | 400 | Symtab<ELFT>::X->addAbsolute(Cmd.Name, STV_DEFAULT); |
Eugene Leviant | eda81a1 | 2016-07-12 06:39:48 +0000 | [diff] [blame] | 401 | } |
| 402 | |
Eugene Leviant | bbe3860 | 2016-07-19 09:25:43 +0000 | [diff] [blame] | 403 | template <class ELFT> bool LinkerScript<ELFT>::hasPhdrsCommands() { |
| 404 | return !Opt.PhdrsCommands.empty(); |
| 405 | } |
| 406 | |
| 407 | // Returns indices of ELF headers containing specific section, identified |
| 408 | // by Name. Each index is a zero based number of ELF header listed within |
| 409 | // PHDRS {} script block. |
| 410 | template <class ELFT> |
| 411 | std::vector<size_t> |
| 412 | LinkerScript<ELFT>::getPhdrIndicesForSection(StringRef Name) { |
| 413 | std::vector<size_t> Indices; |
| 414 | auto ItSect = std::find_if( |
| 415 | Opt.Commands.begin(), Opt.Commands.end(), |
| 416 | [Name](const SectionsCommand &Cmd) { return Cmd.Name == Name; }); |
| 417 | if (ItSect != Opt.Commands.end()) { |
| 418 | SectionsCommand &SecCmd = (*ItSect); |
| 419 | for (StringRef PhdrName : SecCmd.Phdrs) { |
| 420 | auto ItPhdr = std::find_if( |
| 421 | Opt.PhdrsCommands.rbegin(), Opt.PhdrsCommands.rend(), |
| 422 | [PhdrName](PhdrsCommand &Cmd) { return Cmd.Name == PhdrName; }); |
| 423 | if (ItPhdr == Opt.PhdrsCommands.rend()) |
| 424 | error("section header '" + PhdrName + "' is not listed in PHDRS"); |
| 425 | else |
| 426 | Indices.push_back(std::distance(ItPhdr, Opt.PhdrsCommands.rend()) - 1); |
| 427 | } |
| 428 | } |
| 429 | return Indices; |
| 430 | } |
| 431 | |
Rui Ueyama | 07320e4 | 2016-04-20 20:13:41 +0000 | [diff] [blame] | 432 | class elf::ScriptParser : public ScriptParserBase { |
George Rimar | c3794e5 | 2016-02-24 09:21:47 +0000 | [diff] [blame] | 433 | typedef void (ScriptParser::*Handler)(); |
| 434 | |
Rui Ueyama | f7c5fbb | 2015-09-30 17:23:26 +0000 | [diff] [blame] | 435 | public: |
Rui Ueyama | 07320e4 | 2016-04-20 20:13:41 +0000 | [diff] [blame] | 436 | ScriptParser(StringRef S, bool B) : ScriptParserBase(S), IsUnderSysroot(B) {} |
George Rimar | f23b232 | 2016-02-19 10:45:45 +0000 | [diff] [blame] | 437 | |
Rui Ueyama | 4a46539 | 2016-04-22 22:59:24 +0000 | [diff] [blame] | 438 | void run(); |
Rui Ueyama | f7c5fbb | 2015-09-30 17:23:26 +0000 | [diff] [blame] | 439 | |
| 440 | private: |
Rui Ueyama | 52a1509 | 2015-10-11 03:28:42 +0000 | [diff] [blame] | 441 | void addFile(StringRef Path); |
| 442 | |
Rui Ueyama | f7c5fbb | 2015-09-30 17:23:26 +0000 | [diff] [blame] | 443 | void readAsNeeded(); |
Denis Protivensky | 90c5099 | 2015-10-08 06:48:38 +0000 | [diff] [blame] | 444 | void readEntry(); |
George Rimar | 83f406c | 2015-10-19 17:35:12 +0000 | [diff] [blame] | 445 | void readExtern(); |
Rui Ueyama | f7c5fbb | 2015-09-30 17:23:26 +0000 | [diff] [blame] | 446 | void readGroup(); |
Rui Ueyama | 31aa1f8 | 2015-10-11 01:31:55 +0000 | [diff] [blame] | 447 | void readInclude(); |
George Rimar | c3794e5 | 2016-02-24 09:21:47 +0000 | [diff] [blame] | 448 | void readNothing() {} |
Rui Ueyama | ee59282 | 2015-10-07 00:25:09 +0000 | [diff] [blame] | 449 | void readOutput(); |
Davide Italiano | 9159ce9 | 2015-10-12 21:50:08 +0000 | [diff] [blame] | 450 | void readOutputArch(); |
Rui Ueyama | f7c5fbb | 2015-09-30 17:23:26 +0000 | [diff] [blame] | 451 | void readOutputFormat(); |
Eugene Leviant | bbe3860 | 2016-07-19 09:25:43 +0000 | [diff] [blame] | 452 | void readPhdrs(); |
Davide Italiano | 68a39a6 | 2015-10-08 17:51:41 +0000 | [diff] [blame] | 453 | void readSearchDir(); |
Denis Protivensky | 8e3b38a | 2015-11-12 09:52:08 +0000 | [diff] [blame] | 454 | void readSections(); |
| 455 | |
George Rimar | 652852c | 2016-04-16 10:10:32 +0000 | [diff] [blame] | 456 | void readLocationCounterValue(); |
Eugene Leviant | eda81a1 | 2016-07-12 06:39:48 +0000 | [diff] [blame] | 457 | void readOutputSectionDescription(StringRef OutSec); |
Eugene Leviant | bbe3860 | 2016-07-19 09:25:43 +0000 | [diff] [blame] | 458 | std::vector<StringRef> readOutputSectionPhdrs(); |
| 459 | unsigned readPhdrType(); |
Eugene Leviant | eda81a1 | 2016-07-12 06:39:48 +0000 | [diff] [blame] | 460 | void readSymbolAssignment(StringRef Name); |
| 461 | std::vector<StringRef> readSectionsCommandExpr(); |
Rui Ueyama | f7c5fbb | 2015-09-30 17:23:26 +0000 | [diff] [blame] | 462 | |
George Rimar | c3794e5 | 2016-02-24 09:21:47 +0000 | [diff] [blame] | 463 | const static StringMap<Handler> Cmd; |
Rui Ueyama | 07320e4 | 2016-04-20 20:13:41 +0000 | [diff] [blame] | 464 | ScriptConfiguration &Opt = *ScriptConfig; |
| 465 | StringSaver Saver = {ScriptConfig->Alloc}; |
Simon Atanasyan | 16b0cc9 | 2015-11-26 05:53:00 +0000 | [diff] [blame] | 466 | bool IsUnderSysroot; |
Rui Ueyama | f7c5fbb | 2015-09-30 17:23:26 +0000 | [diff] [blame] | 467 | }; |
Rui Ueyama | f7c5fbb | 2015-09-30 17:23:26 +0000 | [diff] [blame] | 468 | |
Rafael Espindola | e0df00b | 2016-02-28 00:25:54 +0000 | [diff] [blame] | 469 | const StringMap<elf::ScriptParser::Handler> elf::ScriptParser::Cmd = { |
George Rimar | c3794e5 | 2016-02-24 09:21:47 +0000 | [diff] [blame] | 470 | {"ENTRY", &ScriptParser::readEntry}, |
| 471 | {"EXTERN", &ScriptParser::readExtern}, |
| 472 | {"GROUP", &ScriptParser::readGroup}, |
| 473 | {"INCLUDE", &ScriptParser::readInclude}, |
| 474 | {"INPUT", &ScriptParser::readGroup}, |
| 475 | {"OUTPUT", &ScriptParser::readOutput}, |
| 476 | {"OUTPUT_ARCH", &ScriptParser::readOutputArch}, |
| 477 | {"OUTPUT_FORMAT", &ScriptParser::readOutputFormat}, |
Eugene Leviant | bbe3860 | 2016-07-19 09:25:43 +0000 | [diff] [blame] | 478 | {"PHDRS", &ScriptParser::readPhdrs}, |
George Rimar | c3794e5 | 2016-02-24 09:21:47 +0000 | [diff] [blame] | 479 | {"SEARCH_DIR", &ScriptParser::readSearchDir}, |
| 480 | {"SECTIONS", &ScriptParser::readSections}, |
| 481 | {";", &ScriptParser::readNothing}}; |
| 482 | |
Rui Ueyama | 717677a | 2016-02-11 21:17:59 +0000 | [diff] [blame] | 483 | void ScriptParser::run() { |
Rui Ueyama | f7c5fbb | 2015-09-30 17:23:26 +0000 | [diff] [blame] | 484 | while (!atEOF()) { |
| 485 | StringRef Tok = next(); |
George Rimar | c3794e5 | 2016-02-24 09:21:47 +0000 | [diff] [blame] | 486 | if (Handler Fn = Cmd.lookup(Tok)) |
| 487 | (this->*Fn)(); |
| 488 | else |
George Rimar | 5761042 | 2016-03-11 14:43:02 +0000 | [diff] [blame] | 489 | setError("unknown directive: " + Tok); |
Rui Ueyama | f7c5fbb | 2015-09-30 17:23:26 +0000 | [diff] [blame] | 490 | } |
| 491 | } |
| 492 | |
Rui Ueyama | 717677a | 2016-02-11 21:17:59 +0000 | [diff] [blame] | 493 | void ScriptParser::addFile(StringRef S) { |
Simon Atanasyan | 16b0cc9 | 2015-11-26 05:53:00 +0000 | [diff] [blame] | 494 | if (IsUnderSysroot && S.startswith("/")) { |
| 495 | SmallString<128> Path; |
| 496 | (Config->Sysroot + S).toStringRef(Path); |
| 497 | if (sys::fs::exists(Path)) { |
| 498 | Driver->addFile(Saver.save(Path.str())); |
| 499 | return; |
| 500 | } |
| 501 | } |
| 502 | |
Rui Ueyama | f03f3cc | 2015-10-13 00:09:21 +0000 | [diff] [blame] | 503 | if (sys::path::is_absolute(S)) { |
Rui Ueyama | 52a1509 | 2015-10-11 03:28:42 +0000 | [diff] [blame] | 504 | Driver->addFile(S); |
| 505 | } else if (S.startswith("=")) { |
| 506 | if (Config->Sysroot.empty()) |
| 507 | Driver->addFile(S.substr(1)); |
| 508 | else |
| 509 | Driver->addFile(Saver.save(Config->Sysroot + "/" + S.substr(1))); |
| 510 | } else if (S.startswith("-l")) { |
Rui Ueyama | 21eecb4 | 2016-02-02 21:13:09 +0000 | [diff] [blame] | 511 | Driver->addLibrary(S.substr(2)); |
Simon Atanasyan | a1b8fc3 | 2015-11-26 20:23:46 +0000 | [diff] [blame] | 512 | } else if (sys::fs::exists(S)) { |
| 513 | Driver->addFile(S); |
Rui Ueyama | 52a1509 | 2015-10-11 03:28:42 +0000 | [diff] [blame] | 514 | } else { |
| 515 | std::string Path = findFromSearchPaths(S); |
| 516 | if (Path.empty()) |
George Rimar | 777f963 | 2016-03-12 08:31:34 +0000 | [diff] [blame] | 517 | setError("unable to find " + S); |
Rui Ueyama | 025d59b | 2016-02-02 20:27:59 +0000 | [diff] [blame] | 518 | else |
| 519 | Driver->addFile(Saver.save(Path)); |
Rui Ueyama | 52a1509 | 2015-10-11 03:28:42 +0000 | [diff] [blame] | 520 | } |
| 521 | } |
| 522 | |
Rui Ueyama | 717677a | 2016-02-11 21:17:59 +0000 | [diff] [blame] | 523 | void ScriptParser::readAsNeeded() { |
Rui Ueyama | f7c5fbb | 2015-09-30 17:23:26 +0000 | [diff] [blame] | 524 | expect("("); |
Rui Ueyama | 35da9b6 | 2015-10-11 20:59:12 +0000 | [diff] [blame] | 525 | bool Orig = Config->AsNeeded; |
| 526 | Config->AsNeeded = true; |
Rui Ueyama | 025d59b | 2016-02-02 20:27:59 +0000 | [diff] [blame] | 527 | while (!Error) { |
Rui Ueyama | f7c5fbb | 2015-09-30 17:23:26 +0000 | [diff] [blame] | 528 | StringRef Tok = next(); |
| 529 | if (Tok == ")") |
Rui Ueyama | 35da9b6 | 2015-10-11 20:59:12 +0000 | [diff] [blame] | 530 | break; |
Rui Ueyama | 52a1509 | 2015-10-11 03:28:42 +0000 | [diff] [blame] | 531 | addFile(Tok); |
Rui Ueyama | f7c5fbb | 2015-09-30 17:23:26 +0000 | [diff] [blame] | 532 | } |
Rui Ueyama | 35da9b6 | 2015-10-11 20:59:12 +0000 | [diff] [blame] | 533 | Config->AsNeeded = Orig; |
Rui Ueyama | f7c5fbb | 2015-09-30 17:23:26 +0000 | [diff] [blame] | 534 | } |
| 535 | |
Rui Ueyama | 717677a | 2016-02-11 21:17:59 +0000 | [diff] [blame] | 536 | void ScriptParser::readEntry() { |
Denis Protivensky | 90c5099 | 2015-10-08 06:48:38 +0000 | [diff] [blame] | 537 | // -e <symbol> takes predecence over ENTRY(<symbol>). |
| 538 | expect("("); |
| 539 | StringRef Tok = next(); |
| 540 | if (Config->Entry.empty()) |
| 541 | Config->Entry = Tok; |
| 542 | expect(")"); |
| 543 | } |
| 544 | |
Rui Ueyama | 717677a | 2016-02-11 21:17:59 +0000 | [diff] [blame] | 545 | void ScriptParser::readExtern() { |
George Rimar | 83f406c | 2015-10-19 17:35:12 +0000 | [diff] [blame] | 546 | expect("("); |
Rui Ueyama | 025d59b | 2016-02-02 20:27:59 +0000 | [diff] [blame] | 547 | while (!Error) { |
George Rimar | 83f406c | 2015-10-19 17:35:12 +0000 | [diff] [blame] | 548 | StringRef Tok = next(); |
| 549 | if (Tok == ")") |
| 550 | return; |
| 551 | Config->Undefined.push_back(Tok); |
| 552 | } |
| 553 | } |
| 554 | |
Rui Ueyama | 717677a | 2016-02-11 21:17:59 +0000 | [diff] [blame] | 555 | void ScriptParser::readGroup() { |
Rui Ueyama | f7c5fbb | 2015-09-30 17:23:26 +0000 | [diff] [blame] | 556 | expect("("); |
Rui Ueyama | 025d59b | 2016-02-02 20:27:59 +0000 | [diff] [blame] | 557 | while (!Error) { |
Rui Ueyama | f7c5fbb | 2015-09-30 17:23:26 +0000 | [diff] [blame] | 558 | StringRef Tok = next(); |
| 559 | if (Tok == ")") |
| 560 | return; |
| 561 | if (Tok == "AS_NEEDED") { |
| 562 | readAsNeeded(); |
| 563 | continue; |
| 564 | } |
Rui Ueyama | 52a1509 | 2015-10-11 03:28:42 +0000 | [diff] [blame] | 565 | addFile(Tok); |
Rui Ueyama | f7c5fbb | 2015-09-30 17:23:26 +0000 | [diff] [blame] | 566 | } |
| 567 | } |
| 568 | |
Rui Ueyama | 717677a | 2016-02-11 21:17:59 +0000 | [diff] [blame] | 569 | void ScriptParser::readInclude() { |
Rui Ueyama | 31aa1f8 | 2015-10-11 01:31:55 +0000 | [diff] [blame] | 570 | StringRef Tok = next(); |
| 571 | auto MBOrErr = MemoryBuffer::getFile(Tok); |
Rui Ueyama | 025d59b | 2016-02-02 20:27:59 +0000 | [diff] [blame] | 572 | if (!MBOrErr) { |
George Rimar | 5761042 | 2016-03-11 14:43:02 +0000 | [diff] [blame] | 573 | setError("cannot open " + Tok); |
Rui Ueyama | 025d59b | 2016-02-02 20:27:59 +0000 | [diff] [blame] | 574 | return; |
| 575 | } |
Rui Ueyama | 31aa1f8 | 2015-10-11 01:31:55 +0000 | [diff] [blame] | 576 | std::unique_ptr<MemoryBuffer> &MB = *MBOrErr; |
Rui Ueyama | a47ee68 | 2015-10-11 01:53:04 +0000 | [diff] [blame] | 577 | StringRef S = Saver.save(MB->getMemBufferRef().getBuffer()); |
| 578 | std::vector<StringRef> V = tokenize(S); |
Rui Ueyama | 31aa1f8 | 2015-10-11 01:31:55 +0000 | [diff] [blame] | 579 | Tokens.insert(Tokens.begin() + Pos, V.begin(), V.end()); |
Rui Ueyama | 31aa1f8 | 2015-10-11 01:31:55 +0000 | [diff] [blame] | 580 | } |
| 581 | |
Rui Ueyama | 717677a | 2016-02-11 21:17:59 +0000 | [diff] [blame] | 582 | void ScriptParser::readOutput() { |
Rui Ueyama | ee59282 | 2015-10-07 00:25:09 +0000 | [diff] [blame] | 583 | // -o <file> takes predecence over OUTPUT(<file>). |
| 584 | expect("("); |
| 585 | StringRef Tok = next(); |
| 586 | if (Config->OutputFile.empty()) |
| 587 | Config->OutputFile = Tok; |
| 588 | expect(")"); |
| 589 | } |
| 590 | |
Rui Ueyama | 717677a | 2016-02-11 21:17:59 +0000 | [diff] [blame] | 591 | void ScriptParser::readOutputArch() { |
Davide Italiano | 9159ce9 | 2015-10-12 21:50:08 +0000 | [diff] [blame] | 592 | // Error checking only for now. |
| 593 | expect("("); |
| 594 | next(); |
| 595 | expect(")"); |
| 596 | } |
| 597 | |
Rui Ueyama | 717677a | 2016-02-11 21:17:59 +0000 | [diff] [blame] | 598 | void ScriptParser::readOutputFormat() { |
Rui Ueyama | f7c5fbb | 2015-09-30 17:23:26 +0000 | [diff] [blame] | 599 | // Error checking only for now. |
| 600 | expect("("); |
| 601 | next(); |
Davide Italiano | 6836c61 | 2015-10-12 21:08:41 +0000 | [diff] [blame] | 602 | StringRef Tok = next(); |
| 603 | if (Tok == ")") |
| 604 | return; |
Rui Ueyama | 025d59b | 2016-02-02 20:27:59 +0000 | [diff] [blame] | 605 | if (Tok != ",") { |
George Rimar | 5761042 | 2016-03-11 14:43:02 +0000 | [diff] [blame] | 606 | setError("unexpected token: " + Tok); |
Rui Ueyama | 025d59b | 2016-02-02 20:27:59 +0000 | [diff] [blame] | 607 | return; |
| 608 | } |
Davide Italiano | 6836c61 | 2015-10-12 21:08:41 +0000 | [diff] [blame] | 609 | next(); |
| 610 | expect(","); |
| 611 | next(); |
Rui Ueyama | f7c5fbb | 2015-09-30 17:23:26 +0000 | [diff] [blame] | 612 | expect(")"); |
| 613 | } |
| 614 | |
Eugene Leviant | bbe3860 | 2016-07-19 09:25:43 +0000 | [diff] [blame] | 615 | void ScriptParser::readPhdrs() { |
| 616 | expect("{"); |
| 617 | while (!Error && !skip("}")) { |
| 618 | StringRef Tok = next(); |
| 619 | Opt.PhdrsCommands.push_back({Tok, PT_NULL, false, false}); |
| 620 | PhdrsCommand &PhdrCmd = Opt.PhdrsCommands.back(); |
| 621 | |
| 622 | PhdrCmd.Type = readPhdrType(); |
| 623 | do { |
| 624 | Tok = next(); |
| 625 | if (Tok == ";") |
| 626 | break; |
| 627 | if (Tok == "FILEHDR") |
| 628 | PhdrCmd.HasFilehdr = true; |
| 629 | else if (Tok == "PHDRS") |
| 630 | PhdrCmd.HasPhdrs = true; |
| 631 | else |
| 632 | setError("unexpected header attribute: " + Tok); |
| 633 | } while (!Error); |
| 634 | } |
| 635 | } |
| 636 | |
Rui Ueyama | 717677a | 2016-02-11 21:17:59 +0000 | [diff] [blame] | 637 | void ScriptParser::readSearchDir() { |
Davide Italiano | 68a39a6 | 2015-10-08 17:51:41 +0000 | [diff] [blame] | 638 | expect("("); |
Rafael Espindola | 0650192 | 2016-03-08 17:13:12 +0000 | [diff] [blame] | 639 | Config->SearchPaths.push_back(next()); |
Davide Italiano | 68a39a6 | 2015-10-08 17:51:41 +0000 | [diff] [blame] | 640 | expect(")"); |
| 641 | } |
| 642 | |
Rui Ueyama | 717677a | 2016-02-11 21:17:59 +0000 | [diff] [blame] | 643 | void ScriptParser::readSections() { |
Rui Ueyama | 07320e4 | 2016-04-20 20:13:41 +0000 | [diff] [blame] | 644 | Opt.DoLayout = true; |
Denis Protivensky | 8e3b38a | 2015-11-12 09:52:08 +0000 | [diff] [blame] | 645 | expect("{"); |
George Rimar | 652852c | 2016-04-16 10:10:32 +0000 | [diff] [blame] | 646 | while (!Error && !skip("}")) { |
| 647 | StringRef Tok = peek(); |
Eugene Leviant | eda81a1 | 2016-07-12 06:39:48 +0000 | [diff] [blame] | 648 | if (Tok == ".") { |
George Rimar | 652852c | 2016-04-16 10:10:32 +0000 | [diff] [blame] | 649 | readLocationCounterValue(); |
Eugene Leviant | eda81a1 | 2016-07-12 06:39:48 +0000 | [diff] [blame] | 650 | continue; |
| 651 | } |
| 652 | next(); |
| 653 | if (peek() == "=") |
| 654 | readSymbolAssignment(Tok); |
George Rimar | 652852c | 2016-04-16 10:10:32 +0000 | [diff] [blame] | 655 | else |
Eugene Leviant | eda81a1 | 2016-07-12 06:39:48 +0000 | [diff] [blame] | 656 | readOutputSectionDescription(Tok); |
George Rimar | 652852c | 2016-04-16 10:10:32 +0000 | [diff] [blame] | 657 | } |
Denis Protivensky | 8e3b38a | 2015-11-12 09:52:08 +0000 | [diff] [blame] | 658 | } |
| 659 | |
George Rimar | 652852c | 2016-04-16 10:10:32 +0000 | [diff] [blame] | 660 | void ScriptParser::readLocationCounterValue() { |
| 661 | expect("."); |
| 662 | expect("="); |
Eugene Leviant | eda81a1 | 2016-07-12 06:39:48 +0000 | [diff] [blame] | 663 | std::vector<StringRef> Expr = readSectionsCommandExpr(); |
| 664 | if (Expr.empty()) |
George Rimar | 652852c | 2016-04-16 10:10:32 +0000 | [diff] [blame] | 665 | error("error in location counter expression"); |
Eugene Leviant | eda81a1 | 2016-07-12 06:39:48 +0000 | [diff] [blame] | 666 | else |
Eugene Leviant | bbe3860 | 2016-07-19 09:25:43 +0000 | [diff] [blame] | 667 | Opt.Commands.push_back({AssignmentKind, std::move(Expr), ".", {}}); |
George Rimar | 652852c | 2016-04-16 10:10:32 +0000 | [diff] [blame] | 668 | } |
| 669 | |
Eugene Leviant | eda81a1 | 2016-07-12 06:39:48 +0000 | [diff] [blame] | 670 | void ScriptParser::readOutputSectionDescription(StringRef OutSec) { |
Eugene Leviant | bbe3860 | 2016-07-19 09:25:43 +0000 | [diff] [blame] | 671 | Opt.Commands.push_back({SectionKind, {}, OutSec, {}}); |
| 672 | SectionsCommand &Cmd = Opt.Commands.back(); |
Denis Protivensky | 8e3b38a | 2015-11-12 09:52:08 +0000 | [diff] [blame] | 673 | expect(":"); |
| 674 | expect("{"); |
Rui Ueyama | 8ec77e6 | 2016-04-21 22:00:51 +0000 | [diff] [blame] | 675 | |
Rui Ueyama | 025d59b | 2016-02-02 20:27:59 +0000 | [diff] [blame] | 676 | while (!Error && !skip("}")) { |
George Rimar | 481c2ce | 2016-02-23 07:47:54 +0000 | [diff] [blame] | 677 | StringRef Tok = next(); |
| 678 | if (Tok == "*") { |
Rui Ueyama | 8ec77e6 | 2016-04-21 22:00:51 +0000 | [diff] [blame] | 679 | expect("("); |
| 680 | while (!Error && !skip(")")) |
| 681 | Opt.Sections.emplace_back(OutSec, next()); |
George Rimar | 481c2ce | 2016-02-23 07:47:54 +0000 | [diff] [blame] | 682 | } else if (Tok == "KEEP") { |
| 683 | expect("("); |
Rui Ueyama | 8ec77e6 | 2016-04-21 22:00:51 +0000 | [diff] [blame] | 684 | expect("*"); |
| 685 | expect("("); |
| 686 | while (!Error && !skip(")")) { |
| 687 | StringRef Sec = next(); |
| 688 | Opt.Sections.emplace_back(OutSec, Sec); |
| 689 | Opt.KeptSections.push_back(Sec); |
| 690 | } |
George Rimar | 481c2ce | 2016-02-23 07:47:54 +0000 | [diff] [blame] | 691 | expect(")"); |
| 692 | } else { |
George Rimar | 777f963 | 2016-03-12 08:31:34 +0000 | [diff] [blame] | 693 | setError("unknown command " + Tok); |
George Rimar | 481c2ce | 2016-02-23 07:47:54 +0000 | [diff] [blame] | 694 | } |
Denis Protivensky | 8e3b38a | 2015-11-12 09:52:08 +0000 | [diff] [blame] | 695 | } |
Eugene Leviant | bbe3860 | 2016-07-19 09:25:43 +0000 | [diff] [blame] | 696 | Cmd.Phdrs = readOutputSectionPhdrs(); |
Rui Ueyama | 8ec77e6 | 2016-04-21 22:00:51 +0000 | [diff] [blame] | 697 | |
George Rimar | e2ee72b | 2016-02-26 14:48:31 +0000 | [diff] [blame] | 698 | StringRef Tok = peek(); |
| 699 | if (Tok.startswith("=")) { |
| 700 | if (!Tok.startswith("=0x")) { |
Rui Ueyama | 3ed2f06 | 2016-03-13 03:17:44 +0000 | [diff] [blame] | 701 | setError("filler should be a hexadecimal value"); |
George Rimar | e2ee72b | 2016-02-26 14:48:31 +0000 | [diff] [blame] | 702 | return; |
| 703 | } |
Rui Ueyama | 3e80897 | 2016-02-28 05:09:11 +0000 | [diff] [blame] | 704 | Tok = Tok.substr(3); |
Rui Ueyama | 07320e4 | 2016-04-20 20:13:41 +0000 | [diff] [blame] | 705 | Opt.Filler[OutSec] = parseHex(Tok); |
George Rimar | e2ee72b | 2016-02-26 14:48:31 +0000 | [diff] [blame] | 706 | next(); |
| 707 | } |
Denis Protivensky | 8e3b38a | 2015-11-12 09:52:08 +0000 | [diff] [blame] | 708 | } |
| 709 | |
Eugene Leviant | eda81a1 | 2016-07-12 06:39:48 +0000 | [diff] [blame] | 710 | void ScriptParser::readSymbolAssignment(StringRef Name) { |
| 711 | expect("="); |
| 712 | std::vector<StringRef> Expr = readSectionsCommandExpr(); |
| 713 | if (Expr.empty()) |
| 714 | error("error in symbol assignment expression"); |
| 715 | else |
Eugene Leviant | bbe3860 | 2016-07-19 09:25:43 +0000 | [diff] [blame] | 716 | Opt.Commands.push_back({AssignmentKind, std::move(Expr), Name, {}}); |
Eugene Leviant | eda81a1 | 2016-07-12 06:39:48 +0000 | [diff] [blame] | 717 | } |
| 718 | |
| 719 | std::vector<StringRef> ScriptParser::readSectionsCommandExpr() { |
| 720 | std::vector<StringRef> Expr; |
| 721 | while (!Error) { |
| 722 | StringRef Tok = next(); |
| 723 | if (Tok == ";") |
| 724 | break; |
| 725 | Expr.push_back(Tok); |
| 726 | } |
| 727 | return Expr; |
| 728 | } |
| 729 | |
Eugene Leviant | bbe3860 | 2016-07-19 09:25:43 +0000 | [diff] [blame] | 730 | std::vector<StringRef> ScriptParser::readOutputSectionPhdrs() { |
| 731 | std::vector<StringRef> Phdrs; |
| 732 | while (!Error && peek().startswith(":")) { |
| 733 | StringRef Tok = next(); |
| 734 | Tok = (Tok.size() == 1) ? next() : Tok.substr(1); |
| 735 | if (Tok.empty()) { |
| 736 | setError("section header name is empty"); |
| 737 | break; |
| 738 | } |
| 739 | else |
| 740 | Phdrs.push_back(Tok); |
| 741 | } |
| 742 | return Phdrs; |
| 743 | } |
| 744 | |
| 745 | unsigned ScriptParser::readPhdrType() { |
| 746 | static const char *typeNames[] = { |
| 747 | "PT_NULL", "PT_LOAD", "PT_DYNAMIC", "PT_INTERP", |
| 748 | "PT_NOTE", "PT_SHLIB", "PT_PHDR", "PT_TLS", |
| 749 | "PT_GNU_EH_FRAME", "PT_GNU_STACK", "PT_GNU_RELRO"}; |
| 750 | static unsigned typeCodes[] = { |
| 751 | PT_NULL, PT_LOAD, PT_DYNAMIC, PT_INTERP, PT_NOTE, PT_SHLIB, |
| 752 | PT_PHDR, PT_TLS, PT_GNU_EH_FRAME, PT_GNU_STACK, PT_GNU_RELRO}; |
| 753 | |
| 754 | unsigned PhdrType = PT_NULL; |
| 755 | StringRef Tok = next(); |
| 756 | auto It = std::find(std::begin(typeNames), std::end(typeNames), Tok); |
| 757 | if (It != std::end(typeNames)) |
| 758 | PhdrType = typeCodes[std::distance(std::begin(typeNames), It)]; |
| 759 | else |
| 760 | setError("invalid program header type"); |
| 761 | |
| 762 | return PhdrType; |
| 763 | } |
| 764 | |
Simon Atanasyan | 16b0cc9 | 2015-11-26 05:53:00 +0000 | [diff] [blame] | 765 | static bool isUnderSysroot(StringRef Path) { |
| 766 | if (Config->Sysroot == "") |
| 767 | return false; |
| 768 | for (; !Path.empty(); Path = sys::path::parent_path(Path)) |
| 769 | if (sys::fs::equivalent(Config->Sysroot, Path)) |
| 770 | return true; |
| 771 | return false; |
| 772 | } |
| 773 | |
Rui Ueyama | 07320e4 | 2016-04-20 20:13:41 +0000 | [diff] [blame] | 774 | // Entry point. |
| 775 | void elf::readLinkerScript(MemoryBufferRef MB) { |
Simon Atanasyan | 16b0cc9 | 2015-11-26 05:53:00 +0000 | [diff] [blame] | 776 | StringRef Path = MB.getBufferIdentifier(); |
Rui Ueyama | 07320e4 | 2016-04-20 20:13:41 +0000 | [diff] [blame] | 777 | ScriptParser(MB.getBuffer(), isUnderSysroot(Path)).run(); |
Rui Ueyama | f7c5fbb | 2015-09-30 17:23:26 +0000 | [diff] [blame] | 778 | } |
Rui Ueyama | 1ebc8ed | 2016-02-12 21:47:28 +0000 | [diff] [blame] | 779 | |
Rui Ueyama | 07320e4 | 2016-04-20 20:13:41 +0000 | [diff] [blame] | 780 | template class elf::LinkerScript<ELF32LE>; |
| 781 | template class elf::LinkerScript<ELF32BE>; |
| 782 | template class elf::LinkerScript<ELF64LE>; |
| 783 | template class elf::LinkerScript<ELF64BE>; |