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