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 | |
| 16 | #include "Config.h" |
| 17 | #include "Driver.h" |
| 18 | #include "SymbolTable.h" |
| 19 | #include "llvm/Support/FileSystem.h" |
| 20 | #include "llvm/Support/MemoryBuffer.h" |
Rui Ueyama | f03f3cc | 2015-10-13 00:09:21 +0000 | [diff] [blame] | 21 | #include "llvm/Support/Path.h" |
Rui Ueyama | a47ee68 | 2015-10-11 01:53:04 +0000 | [diff] [blame] | 22 | #include "llvm/Support/StringSaver.h" |
Rui Ueyama | f7c5fbb | 2015-09-30 17:23:26 +0000 | [diff] [blame] | 23 | |
| 24 | using namespace llvm; |
| 25 | using namespace lld; |
| 26 | using namespace lld::elf2; |
| 27 | |
| 28 | namespace { |
| 29 | class LinkerScript { |
| 30 | public: |
Rui Ueyama | a47ee68 | 2015-10-11 01:53:04 +0000 | [diff] [blame] | 31 | LinkerScript(BumpPtrAllocator *A, StringRef S) |
| 32 | : Saver(*A), Tokens(tokenize(S)) {} |
Rui Ueyama | f7c5fbb | 2015-09-30 17:23:26 +0000 | [diff] [blame] | 33 | void run(); |
| 34 | |
| 35 | private: |
| 36 | static std::vector<StringRef> tokenize(StringRef S); |
| 37 | static StringRef skipSpace(StringRef S); |
| 38 | StringRef next(); |
| 39 | bool atEOF() { return Tokens.size() == Pos; } |
| 40 | void expect(StringRef Expect); |
| 41 | |
Rui Ueyama | 52a1509 | 2015-10-11 03:28:42 +0000 | [diff] [blame] | 42 | void addFile(StringRef Path); |
| 43 | |
Rui Ueyama | f7c5fbb | 2015-09-30 17:23:26 +0000 | [diff] [blame] | 44 | void readAsNeeded(); |
Denis Protivensky | 90c5099 | 2015-10-08 06:48:38 +0000 | [diff] [blame] | 45 | void readEntry(); |
Rui Ueyama | f7c5fbb | 2015-09-30 17:23:26 +0000 | [diff] [blame] | 46 | void readGroup(); |
Rui Ueyama | 31aa1f8 | 2015-10-11 01:31:55 +0000 | [diff] [blame] | 47 | void readInclude(); |
Rui Ueyama | ee59282 | 2015-10-07 00:25:09 +0000 | [diff] [blame] | 48 | void readOutput(); |
Davide Italiano | 9159ce9 | 2015-10-12 21:50:08 +0000 | [diff] [blame] | 49 | void readOutputArch(); |
Rui Ueyama | f7c5fbb | 2015-09-30 17:23:26 +0000 | [diff] [blame] | 50 | void readOutputFormat(); |
Davide Italiano | 68a39a6 | 2015-10-08 17:51:41 +0000 | [diff] [blame] | 51 | void readSearchDir(); |
Rui Ueyama | f7c5fbb | 2015-09-30 17:23:26 +0000 | [diff] [blame] | 52 | |
Rui Ueyama | a47ee68 | 2015-10-11 01:53:04 +0000 | [diff] [blame] | 53 | StringSaver Saver; |
Rui Ueyama | f7c5fbb | 2015-09-30 17:23:26 +0000 | [diff] [blame] | 54 | std::vector<StringRef> Tokens; |
| 55 | size_t Pos = 0; |
| 56 | }; |
| 57 | } |
| 58 | |
| 59 | void LinkerScript::run() { |
| 60 | while (!atEOF()) { |
| 61 | StringRef Tok = next(); |
Denis Protivensky | 90c5099 | 2015-10-08 06:48:38 +0000 | [diff] [blame] | 62 | if (Tok == "ENTRY") { |
| 63 | readEntry(); |
Rui Ueyama | 00f9727 | 2015-10-11 01:31:57 +0000 | [diff] [blame] | 64 | } else if (Tok == "GROUP" || Tok == "INPUT") { |
Rui Ueyama | f7c5fbb | 2015-09-30 17:23:26 +0000 | [diff] [blame] | 65 | readGroup(); |
Rui Ueyama | 31aa1f8 | 2015-10-11 01:31:55 +0000 | [diff] [blame] | 66 | } else if (Tok == "INCLUDE") { |
| 67 | readInclude(); |
Rui Ueyama | ee59282 | 2015-10-07 00:25:09 +0000 | [diff] [blame] | 68 | } else if (Tok == "OUTPUT") { |
| 69 | readOutput(); |
Davide Italiano | 9159ce9 | 2015-10-12 21:50:08 +0000 | [diff] [blame] | 70 | } else if (Tok == "OUTPUT_ARCH") { |
| 71 | readOutputArch(); |
Rui Ueyama | f7c5fbb | 2015-09-30 17:23:26 +0000 | [diff] [blame] | 72 | } else if (Tok == "OUTPUT_FORMAT") { |
| 73 | readOutputFormat(); |
Davide Italiano | 68a39a6 | 2015-10-08 17:51:41 +0000 | [diff] [blame] | 74 | } else if (Tok == "SEARCH_DIR") { |
| 75 | readSearchDir(); |
Rui Ueyama | f7c5fbb | 2015-09-30 17:23:26 +0000 | [diff] [blame] | 76 | } else { |
| 77 | error("unknown directive: " + Tok); |
| 78 | } |
| 79 | } |
| 80 | } |
| 81 | |
| 82 | // Split S into linker script tokens. |
| 83 | std::vector<StringRef> LinkerScript::tokenize(StringRef S) { |
| 84 | std::vector<StringRef> Ret; |
| 85 | for (;;) { |
| 86 | S = skipSpace(S); |
| 87 | if (S.empty()) |
| 88 | return Ret; |
| 89 | |
| 90 | // Quoted token |
| 91 | if (S.startswith("\"")) { |
| 92 | size_t E = S.find("\"", 1); |
| 93 | if (E == StringRef::npos) |
| 94 | error("unclosed quote"); |
| 95 | Ret.push_back(S.substr(1, E)); |
| 96 | S = S.substr(E + 1); |
| 97 | continue; |
| 98 | } |
| 99 | |
| 100 | // Unquoted token |
| 101 | size_t Pos = S.find_first_not_of( |
| 102 | "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz" |
| 103 | "0123456789_.$/\\~=+[]*?-:"); |
| 104 | // A character that cannot start a word (which is usually a |
| 105 | // punctuation) forms a single character token. |
| 106 | if (Pos == 0) |
| 107 | Pos = 1; |
| 108 | Ret.push_back(S.substr(0, Pos)); |
| 109 | S = S.substr(Pos); |
| 110 | } |
| 111 | } |
| 112 | |
| 113 | // Skip leading whitespace characters or /**/-style comments. |
| 114 | StringRef LinkerScript::skipSpace(StringRef S) { |
| 115 | for (;;) { |
| 116 | if (S.startswith("/*")) { |
| 117 | size_t E = S.find("*/", 2); |
| 118 | if (E == StringRef::npos) |
| 119 | error("unclosed comment in a linker script"); |
| 120 | S = S.substr(E + 2); |
| 121 | continue; |
| 122 | } |
| 123 | size_t Size = S.size(); |
| 124 | S = S.ltrim(); |
| 125 | if (S.size() == Size) |
| 126 | return S; |
| 127 | } |
| 128 | } |
| 129 | |
| 130 | StringRef LinkerScript::next() { |
| 131 | if (Pos == Tokens.size()) |
| 132 | error("unexpected EOF"); |
| 133 | return Tokens[Pos++]; |
| 134 | } |
| 135 | |
| 136 | void LinkerScript::expect(StringRef Expect) { |
| 137 | StringRef Tok = next(); |
| 138 | if (Tok != Expect) |
| 139 | error(Expect + " expected, but got " + Tok); |
| 140 | } |
| 141 | |
Rui Ueyama | 52a1509 | 2015-10-11 03:28:42 +0000 | [diff] [blame] | 142 | void LinkerScript::addFile(StringRef S) { |
Rui Ueyama | f03f3cc | 2015-10-13 00:09:21 +0000 | [diff] [blame] | 143 | if (sys::path::is_absolute(S)) { |
Rui Ueyama | 52a1509 | 2015-10-11 03:28:42 +0000 | [diff] [blame] | 144 | Driver->addFile(S); |
| 145 | } else if (S.startswith("=")) { |
| 146 | if (Config->Sysroot.empty()) |
| 147 | Driver->addFile(S.substr(1)); |
| 148 | else |
| 149 | Driver->addFile(Saver.save(Config->Sysroot + "/" + S.substr(1))); |
| 150 | } else if (S.startswith("-l")) { |
| 151 | Driver->addFile(searchLibrary(S.substr(2))); |
| 152 | } else { |
| 153 | std::string Path = findFromSearchPaths(S); |
| 154 | if (Path.empty()) |
| 155 | error("Unable to find " + S); |
| 156 | Driver->addFile(Saver.save(Path)); |
| 157 | } |
| 158 | } |
| 159 | |
Rui Ueyama | f7c5fbb | 2015-09-30 17:23:26 +0000 | [diff] [blame] | 160 | void LinkerScript::readAsNeeded() { |
| 161 | expect("("); |
Rui Ueyama | 35da9b6 | 2015-10-11 20:59:12 +0000 | [diff] [blame] | 162 | bool Orig = Config->AsNeeded; |
| 163 | Config->AsNeeded = true; |
Rui Ueyama | f7c5fbb | 2015-09-30 17:23:26 +0000 | [diff] [blame] | 164 | for (;;) { |
| 165 | StringRef Tok = next(); |
| 166 | if (Tok == ")") |
Rui Ueyama | 35da9b6 | 2015-10-11 20:59:12 +0000 | [diff] [blame] | 167 | break; |
Rui Ueyama | 52a1509 | 2015-10-11 03:28:42 +0000 | [diff] [blame] | 168 | addFile(Tok); |
Rui Ueyama | f7c5fbb | 2015-09-30 17:23:26 +0000 | [diff] [blame] | 169 | } |
Rui Ueyama | 35da9b6 | 2015-10-11 20:59:12 +0000 | [diff] [blame] | 170 | Config->AsNeeded = Orig; |
Rui Ueyama | f7c5fbb | 2015-09-30 17:23:26 +0000 | [diff] [blame] | 171 | } |
| 172 | |
Denis Protivensky | 90c5099 | 2015-10-08 06:48:38 +0000 | [diff] [blame] | 173 | void LinkerScript::readEntry() { |
| 174 | // -e <symbol> takes predecence over ENTRY(<symbol>). |
| 175 | expect("("); |
| 176 | StringRef Tok = next(); |
| 177 | if (Config->Entry.empty()) |
| 178 | Config->Entry = Tok; |
| 179 | expect(")"); |
| 180 | } |
| 181 | |
Rui Ueyama | f7c5fbb | 2015-09-30 17:23:26 +0000 | [diff] [blame] | 182 | void LinkerScript::readGroup() { |
| 183 | expect("("); |
| 184 | for (;;) { |
| 185 | StringRef Tok = next(); |
| 186 | if (Tok == ")") |
| 187 | return; |
| 188 | if (Tok == "AS_NEEDED") { |
| 189 | readAsNeeded(); |
| 190 | continue; |
| 191 | } |
Rui Ueyama | 52a1509 | 2015-10-11 03:28:42 +0000 | [diff] [blame] | 192 | addFile(Tok); |
Rui Ueyama | f7c5fbb | 2015-09-30 17:23:26 +0000 | [diff] [blame] | 193 | } |
| 194 | } |
| 195 | |
Rui Ueyama | 31aa1f8 | 2015-10-11 01:31:55 +0000 | [diff] [blame] | 196 | void LinkerScript::readInclude() { |
| 197 | StringRef Tok = next(); |
| 198 | auto MBOrErr = MemoryBuffer::getFile(Tok); |
Rui Ueyama | 1c42afc | 2015-10-12 15:49:06 +0000 | [diff] [blame] | 199 | error(MBOrErr, "cannot open " + Tok); |
Rui Ueyama | 31aa1f8 | 2015-10-11 01:31:55 +0000 | [diff] [blame] | 200 | std::unique_ptr<MemoryBuffer> &MB = *MBOrErr; |
Rui Ueyama | a47ee68 | 2015-10-11 01:53:04 +0000 | [diff] [blame] | 201 | StringRef S = Saver.save(MB->getMemBufferRef().getBuffer()); |
| 202 | std::vector<StringRef> V = tokenize(S); |
Rui Ueyama | 31aa1f8 | 2015-10-11 01:31:55 +0000 | [diff] [blame] | 203 | Tokens.insert(Tokens.begin() + Pos, V.begin(), V.end()); |
Rui Ueyama | 31aa1f8 | 2015-10-11 01:31:55 +0000 | [diff] [blame] | 204 | } |
| 205 | |
Rui Ueyama | ee59282 | 2015-10-07 00:25:09 +0000 | [diff] [blame] | 206 | void LinkerScript::readOutput() { |
| 207 | // -o <file> takes predecence over OUTPUT(<file>). |
| 208 | expect("("); |
| 209 | StringRef Tok = next(); |
| 210 | if (Config->OutputFile.empty()) |
| 211 | Config->OutputFile = Tok; |
| 212 | expect(")"); |
| 213 | } |
| 214 | |
Davide Italiano | 9159ce9 | 2015-10-12 21:50:08 +0000 | [diff] [blame] | 215 | void LinkerScript::readOutputArch() { |
| 216 | // Error checking only for now. |
| 217 | expect("("); |
| 218 | next(); |
| 219 | expect(")"); |
| 220 | } |
| 221 | |
Rui Ueyama | f7c5fbb | 2015-09-30 17:23:26 +0000 | [diff] [blame] | 222 | void LinkerScript::readOutputFormat() { |
| 223 | // Error checking only for now. |
| 224 | expect("("); |
| 225 | next(); |
Davide Italiano | 6836c61 | 2015-10-12 21:08:41 +0000 | [diff] [blame] | 226 | StringRef Tok = next(); |
| 227 | if (Tok == ")") |
| 228 | return; |
| 229 | if (Tok != ",") |
| 230 | error("unexpected token: " + Tok); |
| 231 | next(); |
| 232 | expect(","); |
| 233 | next(); |
Rui Ueyama | f7c5fbb | 2015-09-30 17:23:26 +0000 | [diff] [blame] | 234 | expect(")"); |
| 235 | } |
| 236 | |
Davide Italiano | 68a39a6 | 2015-10-08 17:51:41 +0000 | [diff] [blame] | 237 | void LinkerScript::readSearchDir() { |
| 238 | expect("("); |
Rui Ueyama | 52a1509 | 2015-10-11 03:28:42 +0000 | [diff] [blame] | 239 | Config->SearchPaths.push_back(next()); |
Davide Italiano | 68a39a6 | 2015-10-08 17:51:41 +0000 | [diff] [blame] | 240 | expect(")"); |
| 241 | } |
| 242 | |
Rui Ueyama | f7c5fbb | 2015-09-30 17:23:26 +0000 | [diff] [blame] | 243 | // Entry point. The other functions or classes are private to this file. |
Rui Ueyama | a47ee68 | 2015-10-11 01:53:04 +0000 | [diff] [blame] | 244 | void lld::elf2::readLinkerScript(BumpPtrAllocator *A, MemoryBufferRef MB) { |
| 245 | LinkerScript(A, MB.getBuffer()).run(); |
Rui Ueyama | f7c5fbb | 2015-09-30 17:23:26 +0000 | [diff] [blame] | 246 | } |