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" |
| 19 | #include "SymbolTable.h" |
| 20 | #include "llvm/Support/FileSystem.h" |
| 21 | #include "llvm/Support/MemoryBuffer.h" |
Rui Ueyama | f03f3cc | 2015-10-13 00:09:21 +0000 | [diff] [blame] | 22 | #include "llvm/Support/Path.h" |
Rui Ueyama | a47ee68 | 2015-10-11 01:53:04 +0000 | [diff] [blame] | 23 | #include "llvm/Support/StringSaver.h" |
Rui Ueyama | f7c5fbb | 2015-09-30 17:23:26 +0000 | [diff] [blame] | 24 | |
| 25 | using namespace llvm; |
| 26 | using namespace lld; |
| 27 | using namespace lld::elf2; |
| 28 | |
Rui Ueyama | 717677a | 2016-02-11 21:17:59 +0000 | [diff] [blame^] | 29 | LinkerScript *elf2::Script; |
| 30 | |
| 31 | void LinkerScript::finalize() { |
| 32 | for (const std::pair<StringRef, std::vector<StringRef>> &P : Sections) |
| 33 | for (StringRef S : P.second) |
| 34 | RevSections[S] = P.first; |
| 35 | } |
| 36 | |
| 37 | StringRef LinkerScript::getOutputSection(StringRef S) { |
| 38 | return RevSections.lookup(S); |
| 39 | } |
| 40 | |
| 41 | bool LinkerScript::isDiscarded(StringRef S) { |
| 42 | return RevSections.lookup(S) == "/DISCARD/"; |
| 43 | } |
| 44 | |
| 45 | int LinkerScript::compareSections(StringRef A, StringRef B) { |
| 46 | auto I = Sections.find(A); |
| 47 | auto E = Sections.end(); |
| 48 | if (I == E) |
| 49 | return 0; |
| 50 | auto J = Sections.find(B); |
| 51 | if (J == E) |
| 52 | return 0; |
| 53 | return I < J ? -1 : 1; |
| 54 | } |
| 55 | |
| 56 | class elf2::ScriptParser { |
Rui Ueyama | f7c5fbb | 2015-09-30 17:23:26 +0000 | [diff] [blame] | 57 | public: |
Rui Ueyama | 717677a | 2016-02-11 21:17:59 +0000 | [diff] [blame^] | 58 | ScriptParser(BumpPtrAllocator *A, StringRef S, bool B) |
Simon Atanasyan | 16b0cc9 | 2015-11-26 05:53:00 +0000 | [diff] [blame] | 59 | : Saver(*A), Tokens(tokenize(S)), IsUnderSysroot(B) {} |
Rui Ueyama | f7c5fbb | 2015-09-30 17:23:26 +0000 | [diff] [blame] | 60 | void run(); |
| 61 | |
| 62 | private: |
Rui Ueyama | 025d59b | 2016-02-02 20:27:59 +0000 | [diff] [blame] | 63 | void setError(const Twine &Msg); |
Rui Ueyama | f7c5fbb | 2015-09-30 17:23:26 +0000 | [diff] [blame] | 64 | static std::vector<StringRef> tokenize(StringRef S); |
| 65 | static StringRef skipSpace(StringRef S); |
Rui Ueyama | 025d59b | 2016-02-02 20:27:59 +0000 | [diff] [blame] | 66 | bool atEOF(); |
Rui Ueyama | f7c5fbb | 2015-09-30 17:23:26 +0000 | [diff] [blame] | 67 | StringRef next(); |
Denis Protivensky | 8e3b38a | 2015-11-12 09:52:08 +0000 | [diff] [blame] | 68 | bool skip(StringRef Tok); |
Rui Ueyama | f7c5fbb | 2015-09-30 17:23:26 +0000 | [diff] [blame] | 69 | void expect(StringRef Expect); |
| 70 | |
Rui Ueyama | 52a1509 | 2015-10-11 03:28:42 +0000 | [diff] [blame] | 71 | void addFile(StringRef Path); |
| 72 | |
Rui Ueyama | f7c5fbb | 2015-09-30 17:23:26 +0000 | [diff] [blame] | 73 | void readAsNeeded(); |
Denis Protivensky | 90c5099 | 2015-10-08 06:48:38 +0000 | [diff] [blame] | 74 | void readEntry(); |
George Rimar | 83f406c | 2015-10-19 17:35:12 +0000 | [diff] [blame] | 75 | void readExtern(); |
Rui Ueyama | f7c5fbb | 2015-09-30 17:23:26 +0000 | [diff] [blame] | 76 | void readGroup(); |
Rui Ueyama | 31aa1f8 | 2015-10-11 01:31:55 +0000 | [diff] [blame] | 77 | void readInclude(); |
Rui Ueyama | ee59282 | 2015-10-07 00:25:09 +0000 | [diff] [blame] | 78 | void readOutput(); |
Davide Italiano | 9159ce9 | 2015-10-12 21:50:08 +0000 | [diff] [blame] | 79 | void readOutputArch(); |
Rui Ueyama | f7c5fbb | 2015-09-30 17:23:26 +0000 | [diff] [blame] | 80 | void readOutputFormat(); |
Davide Italiano | 68a39a6 | 2015-10-08 17:51:41 +0000 | [diff] [blame] | 81 | void readSearchDir(); |
Denis Protivensky | 8e3b38a | 2015-11-12 09:52:08 +0000 | [diff] [blame] | 82 | void readSections(); |
| 83 | |
| 84 | void readOutputSectionDescription(); |
Rui Ueyama | f7c5fbb | 2015-09-30 17:23:26 +0000 | [diff] [blame] | 85 | |
Rui Ueyama | a47ee68 | 2015-10-11 01:53:04 +0000 | [diff] [blame] | 86 | StringSaver Saver; |
Rui Ueyama | f7c5fbb | 2015-09-30 17:23:26 +0000 | [diff] [blame] | 87 | std::vector<StringRef> Tokens; |
Rui Ueyama | 025d59b | 2016-02-02 20:27:59 +0000 | [diff] [blame] | 88 | bool Error = false; |
Rui Ueyama | f7c5fbb | 2015-09-30 17:23:26 +0000 | [diff] [blame] | 89 | size_t Pos = 0; |
Simon Atanasyan | 16b0cc9 | 2015-11-26 05:53:00 +0000 | [diff] [blame] | 90 | bool IsUnderSysroot; |
Rui Ueyama | f7c5fbb | 2015-09-30 17:23:26 +0000 | [diff] [blame] | 91 | }; |
Rui Ueyama | f7c5fbb | 2015-09-30 17:23:26 +0000 | [diff] [blame] | 92 | |
Rui Ueyama | 717677a | 2016-02-11 21:17:59 +0000 | [diff] [blame^] | 93 | void ScriptParser::run() { |
Rui Ueyama | f7c5fbb | 2015-09-30 17:23:26 +0000 | [diff] [blame] | 94 | while (!atEOF()) { |
| 95 | StringRef Tok = next(); |
Davide Italiano | 10d268d | 2015-10-13 20:48:56 +0000 | [diff] [blame] | 96 | if (Tok == ";") |
| 97 | continue; |
Denis Protivensky | 90c5099 | 2015-10-08 06:48:38 +0000 | [diff] [blame] | 98 | if (Tok == "ENTRY") { |
| 99 | readEntry(); |
George Rimar | 83f406c | 2015-10-19 17:35:12 +0000 | [diff] [blame] | 100 | } else if (Tok == "EXTERN") { |
| 101 | readExtern(); |
Rui Ueyama | 00f9727 | 2015-10-11 01:31:57 +0000 | [diff] [blame] | 102 | } else if (Tok == "GROUP" || Tok == "INPUT") { |
Rui Ueyama | f7c5fbb | 2015-09-30 17:23:26 +0000 | [diff] [blame] | 103 | readGroup(); |
Rui Ueyama | 31aa1f8 | 2015-10-11 01:31:55 +0000 | [diff] [blame] | 104 | } else if (Tok == "INCLUDE") { |
| 105 | readInclude(); |
Rui Ueyama | ee59282 | 2015-10-07 00:25:09 +0000 | [diff] [blame] | 106 | } else if (Tok == "OUTPUT") { |
| 107 | readOutput(); |
Davide Italiano | 9159ce9 | 2015-10-12 21:50:08 +0000 | [diff] [blame] | 108 | } else if (Tok == "OUTPUT_ARCH") { |
| 109 | readOutputArch(); |
Rui Ueyama | f7c5fbb | 2015-09-30 17:23:26 +0000 | [diff] [blame] | 110 | } else if (Tok == "OUTPUT_FORMAT") { |
| 111 | readOutputFormat(); |
Davide Italiano | 68a39a6 | 2015-10-08 17:51:41 +0000 | [diff] [blame] | 112 | } else if (Tok == "SEARCH_DIR") { |
| 113 | readSearchDir(); |
Denis Protivensky | 8e3b38a | 2015-11-12 09:52:08 +0000 | [diff] [blame] | 114 | } else if (Tok == "SECTIONS") { |
| 115 | readSections(); |
Rui Ueyama | f7c5fbb | 2015-09-30 17:23:26 +0000 | [diff] [blame] | 116 | } else { |
Rui Ueyama | 025d59b | 2016-02-02 20:27:59 +0000 | [diff] [blame] | 117 | setError("unknown directive: " + Tok); |
| 118 | return; |
Rui Ueyama | f7c5fbb | 2015-09-30 17:23:26 +0000 | [diff] [blame] | 119 | } |
| 120 | } |
| 121 | } |
| 122 | |
Rui Ueyama | 025d59b | 2016-02-02 20:27:59 +0000 | [diff] [blame] | 123 | // We don't want to record cascading errors. Keep only the first one. |
Rui Ueyama | 717677a | 2016-02-11 21:17:59 +0000 | [diff] [blame^] | 124 | void ScriptParser::setError(const Twine &Msg) { |
Rui Ueyama | 025d59b | 2016-02-02 20:27:59 +0000 | [diff] [blame] | 125 | if (Error) |
| 126 | return; |
| 127 | error(Msg); |
| 128 | Error = true; |
| 129 | } |
| 130 | |
Rui Ueyama | f7c5fbb | 2015-09-30 17:23:26 +0000 | [diff] [blame] | 131 | // Split S into linker script tokens. |
Rui Ueyama | 717677a | 2016-02-11 21:17:59 +0000 | [diff] [blame^] | 132 | std::vector<StringRef> ScriptParser::tokenize(StringRef S) { |
Rui Ueyama | f7c5fbb | 2015-09-30 17:23:26 +0000 | [diff] [blame] | 133 | std::vector<StringRef> Ret; |
| 134 | for (;;) { |
| 135 | S = skipSpace(S); |
| 136 | if (S.empty()) |
| 137 | return Ret; |
| 138 | |
| 139 | // Quoted token |
| 140 | if (S.startswith("\"")) { |
| 141 | size_t E = S.find("\"", 1); |
Rui Ueyama | 025d59b | 2016-02-02 20:27:59 +0000 | [diff] [blame] | 142 | if (E == StringRef::npos) { |
| 143 | error("unclosed quote"); |
| 144 | return {}; |
| 145 | } |
Pete Cooper | 6025933 | 2016-01-22 23:46:37 +0000 | [diff] [blame] | 146 | Ret.push_back(S.substr(1, E - 1)); |
Rui Ueyama | f7c5fbb | 2015-09-30 17:23:26 +0000 | [diff] [blame] | 147 | S = S.substr(E + 1); |
| 148 | continue; |
| 149 | } |
| 150 | |
| 151 | // Unquoted token |
| 152 | size_t Pos = S.find_first_not_of( |
| 153 | "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz" |
| 154 | "0123456789_.$/\\~=+[]*?-:"); |
| 155 | // A character that cannot start a word (which is usually a |
| 156 | // punctuation) forms a single character token. |
| 157 | if (Pos == 0) |
| 158 | Pos = 1; |
| 159 | Ret.push_back(S.substr(0, Pos)); |
| 160 | S = S.substr(Pos); |
| 161 | } |
| 162 | } |
| 163 | |
| 164 | // Skip leading whitespace characters or /**/-style comments. |
Rui Ueyama | 717677a | 2016-02-11 21:17:59 +0000 | [diff] [blame^] | 165 | StringRef ScriptParser::skipSpace(StringRef S) { |
Rui Ueyama | f7c5fbb | 2015-09-30 17:23:26 +0000 | [diff] [blame] | 166 | for (;;) { |
| 167 | if (S.startswith("/*")) { |
| 168 | size_t E = S.find("*/", 2); |
Rui Ueyama | 025d59b | 2016-02-02 20:27:59 +0000 | [diff] [blame] | 169 | if (E == StringRef::npos) { |
| 170 | error("unclosed comment in a linker script"); |
| 171 | return ""; |
| 172 | } |
Rui Ueyama | f7c5fbb | 2015-09-30 17:23:26 +0000 | [diff] [blame] | 173 | S = S.substr(E + 2); |
| 174 | continue; |
| 175 | } |
| 176 | size_t Size = S.size(); |
| 177 | S = S.ltrim(); |
| 178 | if (S.size() == Size) |
| 179 | return S; |
| 180 | } |
| 181 | } |
| 182 | |
Rui Ueyama | 025d59b | 2016-02-02 20:27:59 +0000 | [diff] [blame] | 183 | // An errneous token is handled as if it were the last token before EOF. |
Rui Ueyama | 717677a | 2016-02-11 21:17:59 +0000 | [diff] [blame^] | 184 | bool ScriptParser::atEOF() { return Error || Tokens.size() == Pos; } |
Rui Ueyama | 025d59b | 2016-02-02 20:27:59 +0000 | [diff] [blame] | 185 | |
Rui Ueyama | 717677a | 2016-02-11 21:17:59 +0000 | [diff] [blame^] | 186 | StringRef ScriptParser::next() { |
Rui Ueyama | 025d59b | 2016-02-02 20:27:59 +0000 | [diff] [blame] | 187 | if (Error) |
| 188 | return ""; |
| 189 | if (atEOF()) { |
| 190 | setError("unexpected EOF"); |
| 191 | return ""; |
| 192 | } |
Rui Ueyama | f7c5fbb | 2015-09-30 17:23:26 +0000 | [diff] [blame] | 193 | return Tokens[Pos++]; |
| 194 | } |
| 195 | |
Rui Ueyama | 717677a | 2016-02-11 21:17:59 +0000 | [diff] [blame^] | 196 | bool ScriptParser::skip(StringRef Tok) { |
Rui Ueyama | 025d59b | 2016-02-02 20:27:59 +0000 | [diff] [blame] | 197 | if (Error) |
| 198 | return false; |
| 199 | if (atEOF()) { |
| 200 | setError("unexpected EOF"); |
| 201 | return false; |
| 202 | } |
Denis Protivensky | 8e3b38a | 2015-11-12 09:52:08 +0000 | [diff] [blame] | 203 | if (Tok != Tokens[Pos]) |
| 204 | return false; |
| 205 | ++Pos; |
| 206 | return true; |
| 207 | } |
| 208 | |
Rui Ueyama | 717677a | 2016-02-11 21:17:59 +0000 | [diff] [blame^] | 209 | void ScriptParser::expect(StringRef Expect) { |
Rui Ueyama | 025d59b | 2016-02-02 20:27:59 +0000 | [diff] [blame] | 210 | if (Error) |
| 211 | return; |
Rui Ueyama | f7c5fbb | 2015-09-30 17:23:26 +0000 | [diff] [blame] | 212 | StringRef Tok = next(); |
| 213 | if (Tok != Expect) |
Rui Ueyama | 025d59b | 2016-02-02 20:27:59 +0000 | [diff] [blame] | 214 | setError(Expect + " expected, but got " + Tok); |
Rui Ueyama | f7c5fbb | 2015-09-30 17:23:26 +0000 | [diff] [blame] | 215 | } |
| 216 | |
Rui Ueyama | 717677a | 2016-02-11 21:17:59 +0000 | [diff] [blame^] | 217 | void ScriptParser::addFile(StringRef S) { |
Simon Atanasyan | 16b0cc9 | 2015-11-26 05:53:00 +0000 | [diff] [blame] | 218 | if (IsUnderSysroot && S.startswith("/")) { |
| 219 | SmallString<128> Path; |
| 220 | (Config->Sysroot + S).toStringRef(Path); |
| 221 | if (sys::fs::exists(Path)) { |
| 222 | Driver->addFile(Saver.save(Path.str())); |
| 223 | return; |
| 224 | } |
| 225 | } |
| 226 | |
Rui Ueyama | f03f3cc | 2015-10-13 00:09:21 +0000 | [diff] [blame] | 227 | if (sys::path::is_absolute(S)) { |
Rui Ueyama | 52a1509 | 2015-10-11 03:28:42 +0000 | [diff] [blame] | 228 | Driver->addFile(S); |
| 229 | } else if (S.startswith("=")) { |
| 230 | if (Config->Sysroot.empty()) |
| 231 | Driver->addFile(S.substr(1)); |
| 232 | else |
| 233 | Driver->addFile(Saver.save(Config->Sysroot + "/" + S.substr(1))); |
| 234 | } else if (S.startswith("-l")) { |
Rui Ueyama | 21eecb4 | 2016-02-02 21:13:09 +0000 | [diff] [blame] | 235 | Driver->addLibrary(S.substr(2)); |
Simon Atanasyan | a1b8fc3 | 2015-11-26 20:23:46 +0000 | [diff] [blame] | 236 | } else if (sys::fs::exists(S)) { |
| 237 | Driver->addFile(S); |
Rui Ueyama | 52a1509 | 2015-10-11 03:28:42 +0000 | [diff] [blame] | 238 | } else { |
| 239 | std::string Path = findFromSearchPaths(S); |
| 240 | if (Path.empty()) |
Rui Ueyama | 025d59b | 2016-02-02 20:27:59 +0000 | [diff] [blame] | 241 | setError("Unable to find " + S); |
| 242 | else |
| 243 | Driver->addFile(Saver.save(Path)); |
Rui Ueyama | 52a1509 | 2015-10-11 03:28:42 +0000 | [diff] [blame] | 244 | } |
| 245 | } |
| 246 | |
Rui Ueyama | 717677a | 2016-02-11 21:17:59 +0000 | [diff] [blame^] | 247 | void ScriptParser::readAsNeeded() { |
Rui Ueyama | f7c5fbb | 2015-09-30 17:23:26 +0000 | [diff] [blame] | 248 | expect("("); |
Rui Ueyama | 35da9b6 | 2015-10-11 20:59:12 +0000 | [diff] [blame] | 249 | bool Orig = Config->AsNeeded; |
| 250 | Config->AsNeeded = true; |
Rui Ueyama | 025d59b | 2016-02-02 20:27:59 +0000 | [diff] [blame] | 251 | while (!Error) { |
Rui Ueyama | f7c5fbb | 2015-09-30 17:23:26 +0000 | [diff] [blame] | 252 | StringRef Tok = next(); |
| 253 | if (Tok == ")") |
Rui Ueyama | 35da9b6 | 2015-10-11 20:59:12 +0000 | [diff] [blame] | 254 | break; |
Rui Ueyama | 52a1509 | 2015-10-11 03:28:42 +0000 | [diff] [blame] | 255 | addFile(Tok); |
Rui Ueyama | f7c5fbb | 2015-09-30 17:23:26 +0000 | [diff] [blame] | 256 | } |
Rui Ueyama | 35da9b6 | 2015-10-11 20:59:12 +0000 | [diff] [blame] | 257 | Config->AsNeeded = Orig; |
Rui Ueyama | f7c5fbb | 2015-09-30 17:23:26 +0000 | [diff] [blame] | 258 | } |
| 259 | |
Rui Ueyama | 717677a | 2016-02-11 21:17:59 +0000 | [diff] [blame^] | 260 | void ScriptParser::readEntry() { |
Denis Protivensky | 90c5099 | 2015-10-08 06:48:38 +0000 | [diff] [blame] | 261 | // -e <symbol> takes predecence over ENTRY(<symbol>). |
| 262 | expect("("); |
| 263 | StringRef Tok = next(); |
| 264 | if (Config->Entry.empty()) |
| 265 | Config->Entry = Tok; |
| 266 | expect(")"); |
| 267 | } |
| 268 | |
Rui Ueyama | 717677a | 2016-02-11 21:17:59 +0000 | [diff] [blame^] | 269 | void ScriptParser::readExtern() { |
George Rimar | 83f406c | 2015-10-19 17:35:12 +0000 | [diff] [blame] | 270 | expect("("); |
Rui Ueyama | 025d59b | 2016-02-02 20:27:59 +0000 | [diff] [blame] | 271 | while (!Error) { |
George Rimar | 83f406c | 2015-10-19 17:35:12 +0000 | [diff] [blame] | 272 | StringRef Tok = next(); |
| 273 | if (Tok == ")") |
| 274 | return; |
| 275 | Config->Undefined.push_back(Tok); |
| 276 | } |
| 277 | } |
| 278 | |
Rui Ueyama | 717677a | 2016-02-11 21:17:59 +0000 | [diff] [blame^] | 279 | void ScriptParser::readGroup() { |
Rui Ueyama | f7c5fbb | 2015-09-30 17:23:26 +0000 | [diff] [blame] | 280 | expect("("); |
Rui Ueyama | 025d59b | 2016-02-02 20:27:59 +0000 | [diff] [blame] | 281 | while (!Error) { |
Rui Ueyama | f7c5fbb | 2015-09-30 17:23:26 +0000 | [diff] [blame] | 282 | StringRef Tok = next(); |
| 283 | if (Tok == ")") |
| 284 | return; |
| 285 | if (Tok == "AS_NEEDED") { |
| 286 | readAsNeeded(); |
| 287 | continue; |
| 288 | } |
Rui Ueyama | 52a1509 | 2015-10-11 03:28:42 +0000 | [diff] [blame] | 289 | addFile(Tok); |
Rui Ueyama | f7c5fbb | 2015-09-30 17:23:26 +0000 | [diff] [blame] | 290 | } |
| 291 | } |
| 292 | |
Rui Ueyama | 717677a | 2016-02-11 21:17:59 +0000 | [diff] [blame^] | 293 | void ScriptParser::readInclude() { |
Rui Ueyama | 31aa1f8 | 2015-10-11 01:31:55 +0000 | [diff] [blame] | 294 | StringRef Tok = next(); |
| 295 | auto MBOrErr = MemoryBuffer::getFile(Tok); |
Rui Ueyama | 025d59b | 2016-02-02 20:27:59 +0000 | [diff] [blame] | 296 | if (!MBOrErr) { |
| 297 | setError("cannot open " + Tok); |
| 298 | return; |
| 299 | } |
Rui Ueyama | 31aa1f8 | 2015-10-11 01:31:55 +0000 | [diff] [blame] | 300 | std::unique_ptr<MemoryBuffer> &MB = *MBOrErr; |
Rui Ueyama | a47ee68 | 2015-10-11 01:53:04 +0000 | [diff] [blame] | 301 | StringRef S = Saver.save(MB->getMemBufferRef().getBuffer()); |
| 302 | std::vector<StringRef> V = tokenize(S); |
Rui Ueyama | 31aa1f8 | 2015-10-11 01:31:55 +0000 | [diff] [blame] | 303 | Tokens.insert(Tokens.begin() + Pos, V.begin(), V.end()); |
Rui Ueyama | 31aa1f8 | 2015-10-11 01:31:55 +0000 | [diff] [blame] | 304 | } |
| 305 | |
Rui Ueyama | 717677a | 2016-02-11 21:17:59 +0000 | [diff] [blame^] | 306 | void ScriptParser::readOutput() { |
Rui Ueyama | ee59282 | 2015-10-07 00:25:09 +0000 | [diff] [blame] | 307 | // -o <file> takes predecence over OUTPUT(<file>). |
| 308 | expect("("); |
| 309 | StringRef Tok = next(); |
| 310 | if (Config->OutputFile.empty()) |
| 311 | Config->OutputFile = Tok; |
| 312 | expect(")"); |
| 313 | } |
| 314 | |
Rui Ueyama | 717677a | 2016-02-11 21:17:59 +0000 | [diff] [blame^] | 315 | void ScriptParser::readOutputArch() { |
Davide Italiano | 9159ce9 | 2015-10-12 21:50:08 +0000 | [diff] [blame] | 316 | // Error checking only for now. |
| 317 | expect("("); |
| 318 | next(); |
| 319 | expect(")"); |
| 320 | } |
| 321 | |
Rui Ueyama | 717677a | 2016-02-11 21:17:59 +0000 | [diff] [blame^] | 322 | void ScriptParser::readOutputFormat() { |
Rui Ueyama | f7c5fbb | 2015-09-30 17:23:26 +0000 | [diff] [blame] | 323 | // Error checking only for now. |
| 324 | expect("("); |
| 325 | next(); |
Davide Italiano | 6836c61 | 2015-10-12 21:08:41 +0000 | [diff] [blame] | 326 | StringRef Tok = next(); |
| 327 | if (Tok == ")") |
| 328 | return; |
Rui Ueyama | 025d59b | 2016-02-02 20:27:59 +0000 | [diff] [blame] | 329 | if (Tok != ",") { |
| 330 | setError("unexpected token: " + Tok); |
| 331 | return; |
| 332 | } |
Davide Italiano | 6836c61 | 2015-10-12 21:08:41 +0000 | [diff] [blame] | 333 | next(); |
| 334 | expect(","); |
| 335 | next(); |
Rui Ueyama | f7c5fbb | 2015-09-30 17:23:26 +0000 | [diff] [blame] | 336 | expect(")"); |
| 337 | } |
| 338 | |
Rui Ueyama | 717677a | 2016-02-11 21:17:59 +0000 | [diff] [blame^] | 339 | void ScriptParser::readSearchDir() { |
Davide Italiano | 68a39a6 | 2015-10-08 17:51:41 +0000 | [diff] [blame] | 340 | expect("("); |
Rui Ueyama | 52a1509 | 2015-10-11 03:28:42 +0000 | [diff] [blame] | 341 | Config->SearchPaths.push_back(next()); |
Davide Italiano | 68a39a6 | 2015-10-08 17:51:41 +0000 | [diff] [blame] | 342 | expect(")"); |
| 343 | } |
| 344 | |
Rui Ueyama | 717677a | 2016-02-11 21:17:59 +0000 | [diff] [blame^] | 345 | void ScriptParser::readSections() { |
Denis Protivensky | 8e3b38a | 2015-11-12 09:52:08 +0000 | [diff] [blame] | 346 | expect("{"); |
Rui Ueyama | 025d59b | 2016-02-02 20:27:59 +0000 | [diff] [blame] | 347 | while (!Error && !skip("}")) |
Denis Protivensky | 8e3b38a | 2015-11-12 09:52:08 +0000 | [diff] [blame] | 348 | readOutputSectionDescription(); |
| 349 | } |
| 350 | |
Rui Ueyama | 717677a | 2016-02-11 21:17:59 +0000 | [diff] [blame^] | 351 | void ScriptParser::readOutputSectionDescription() { |
| 352 | std::vector<StringRef> &V = Script->Sections[next()]; |
Denis Protivensky | 8e3b38a | 2015-11-12 09:52:08 +0000 | [diff] [blame] | 353 | expect(":"); |
| 354 | expect("{"); |
Rui Ueyama | 025d59b | 2016-02-02 20:27:59 +0000 | [diff] [blame] | 355 | while (!Error && !skip("}")) { |
Denis Protivensky | 8e3b38a | 2015-11-12 09:52:08 +0000 | [diff] [blame] | 356 | next(); // Skip input file name. |
| 357 | expect("("); |
Rui Ueyama | 025d59b | 2016-02-02 20:27:59 +0000 | [diff] [blame] | 358 | while (!Error && !skip(")")) |
Rui Ueyama | 717677a | 2016-02-11 21:17:59 +0000 | [diff] [blame^] | 359 | V.push_back(next()); |
Denis Protivensky | 8e3b38a | 2015-11-12 09:52:08 +0000 | [diff] [blame] | 360 | } |
| 361 | } |
| 362 | |
Simon Atanasyan | 16b0cc9 | 2015-11-26 05:53:00 +0000 | [diff] [blame] | 363 | static bool isUnderSysroot(StringRef Path) { |
| 364 | if (Config->Sysroot == "") |
| 365 | return false; |
| 366 | for (; !Path.empty(); Path = sys::path::parent_path(Path)) |
| 367 | if (sys::fs::equivalent(Config->Sysroot, Path)) |
| 368 | return true; |
| 369 | return false; |
| 370 | } |
| 371 | |
Rui Ueyama | f7c5fbb | 2015-09-30 17:23:26 +0000 | [diff] [blame] | 372 | // Entry point. The other functions or classes are private to this file. |
Rui Ueyama | 83cd6e0 | 2016-01-06 20:11:55 +0000 | [diff] [blame] | 373 | void elf2::readLinkerScript(BumpPtrAllocator *A, MemoryBufferRef MB) { |
Simon Atanasyan | 16b0cc9 | 2015-11-26 05:53:00 +0000 | [diff] [blame] | 374 | StringRef Path = MB.getBufferIdentifier(); |
Rui Ueyama | 717677a | 2016-02-11 21:17:59 +0000 | [diff] [blame^] | 375 | ScriptParser(A, MB.getBuffer(), isUnderSysroot(Path)).run(); |
Rui Ueyama | f7c5fbb | 2015-09-30 17:23:26 +0000 | [diff] [blame] | 376 | } |