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