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