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