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