Sam Clegg | c94d393 | 2017-11-17 18:14:09 +0000 | [diff] [blame] | 1 | //===- Driver.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 | #include "lld/Common/Driver.h" |
| 11 | #include "Config.h" |
Sam Clegg | c94d393 | 2017-11-17 18:14:09 +0000 | [diff] [blame] | 12 | #include "SymbolTable.h" |
| 13 | #include "Writer.h" |
Rui Ueyama | 3e03944 | 2017-11-28 19:58:45 +0000 | [diff] [blame] | 14 | #include "lld/Common/Args.h" |
Sam Clegg | c94d393 | 2017-11-17 18:14:09 +0000 | [diff] [blame] | 15 | #include "lld/Common/ErrorHandler.h" |
Rui Ueyama | 2017d52 | 2017-11-28 20:39:17 +0000 | [diff] [blame] | 16 | #include "lld/Common/Memory.h" |
Sam Clegg | c94d393 | 2017-11-17 18:14:09 +0000 | [diff] [blame] | 17 | #include "lld/Common/Threads.h" |
| 18 | #include "lld/Common/Version.h" |
| 19 | #include "llvm/ADT/Twine.h" |
| 20 | #include "llvm/Object/Wasm.h" |
| 21 | #include "llvm/Option/ArgList.h" |
| 22 | #include "llvm/Support/CommandLine.h" |
| 23 | #include "llvm/Support/Path.h" |
| 24 | #include "llvm/Support/Process.h" |
| 25 | |
| 26 | using namespace llvm; |
| 27 | using namespace llvm::sys; |
| 28 | using namespace llvm::wasm; |
Sam Clegg | c94d393 | 2017-11-17 18:14:09 +0000 | [diff] [blame] | 29 | |
| 30 | using namespace lld; |
| 31 | using namespace lld::wasm; |
| 32 | |
| 33 | namespace { |
| 34 | |
| 35 | // Parses command line options. |
| 36 | class WasmOptTable : public llvm::opt::OptTable { |
| 37 | public: |
| 38 | WasmOptTable(); |
| 39 | llvm::opt::InputArgList parse(ArrayRef<const char *> Argv); |
| 40 | }; |
| 41 | |
| 42 | // Create enum with OPT_xxx values for each option in Options.td |
| 43 | enum { |
| 44 | OPT_INVALID = 0, |
| 45 | #define OPTION(_1, _2, ID, _4, _5, _6, _7, _8, _9, _10, _11, _12) OPT_##ID, |
| 46 | #include "Options.inc" |
| 47 | #undef OPTION |
| 48 | }; |
| 49 | |
| 50 | class LinkerDriver { |
| 51 | public: |
| 52 | void link(ArrayRef<const char *> ArgsArr); |
| 53 | |
| 54 | private: |
| 55 | void createFiles(llvm::opt::InputArgList &Args); |
| 56 | void addFile(StringRef Path); |
| 57 | void addLibrary(StringRef Name); |
| 58 | std::vector<InputFile *> Files; |
| 59 | }; |
| 60 | |
| 61 | } // anonymous namespace |
| 62 | |
Sam Clegg | c94d393 | 2017-11-17 18:14:09 +0000 | [diff] [blame] | 63 | Configuration *lld::wasm::Config; |
Sam Clegg | c94d393 | 2017-11-17 18:14:09 +0000 | [diff] [blame] | 64 | |
| 65 | bool lld::wasm::link(ArrayRef<const char *> Args, bool CanExitEarly, |
| 66 | raw_ostream &Error) { |
| 67 | errorHandler().LogName = Args[0]; |
| 68 | errorHandler().ErrorOS = &Error; |
| 69 | errorHandler().ColorDiagnostics = Error.has_colors(); |
| 70 | errorHandler().ErrorLimitExceededMsg = |
| 71 | "too many errors emitted, stopping now (use " |
| 72 | "-error-limit=0 to see all errors)"; |
| 73 | |
| 74 | Config = make<Configuration>(); |
| 75 | Symtab = make<SymbolTable>(); |
| 76 | |
| 77 | LinkerDriver().link(Args); |
| 78 | |
| 79 | // Exit immediately if we don't need to return to the caller. |
| 80 | // This saves time because the overhead of calling destructors |
| 81 | // for all globally-allocated objects is not negligible. |
| 82 | if (CanExitEarly) |
| 83 | exitLld(errorCount() ? 1 : 0); |
| 84 | |
| 85 | freeArena(); |
| 86 | return !errorCount(); |
| 87 | } |
| 88 | |
| 89 | // Create OptTable |
| 90 | |
| 91 | // Create prefix string literals used in Options.td |
| 92 | #define PREFIX(NAME, VALUE) const char *const NAME[] = VALUE; |
| 93 | #include "Options.inc" |
| 94 | #undef PREFIX |
| 95 | |
| 96 | // Create table mapping all options defined in Options.td |
| 97 | static const opt::OptTable::Info OptInfo[] = { |
| 98 | #define OPTION(X1, X2, ID, KIND, GROUP, ALIAS, X7, X8, X9, X10, X11, X12) \ |
| 99 | {X1, X2, X10, X11, OPT_##ID, opt::Option::KIND##Class, \ |
| 100 | X9, X8, OPT_##GROUP, OPT_##ALIAS, X7, X12}, |
| 101 | #include "Options.inc" |
| 102 | #undef OPTION |
| 103 | }; |
| 104 | |
Sam Clegg | c94d393 | 2017-11-17 18:14:09 +0000 | [diff] [blame] | 105 | // Set color diagnostics according to -color-diagnostics={auto,always,never} |
| 106 | // or -no-color-diagnostics flags. |
| 107 | static void handleColorDiagnostics(opt::InputArgList &Args) { |
| 108 | auto *Arg = Args.getLastArg(OPT_color_diagnostics, OPT_color_diagnostics_eq, |
| 109 | OPT_no_color_diagnostics); |
| 110 | if (!Arg) |
| 111 | return; |
| 112 | |
| 113 | if (Arg->getOption().getID() == OPT_color_diagnostics) |
| 114 | errorHandler().ColorDiagnostics = true; |
| 115 | else if (Arg->getOption().getID() == OPT_no_color_diagnostics) |
| 116 | errorHandler().ColorDiagnostics = false; |
| 117 | else { |
| 118 | StringRef S = Arg->getValue(); |
| 119 | if (S == "always") |
| 120 | errorHandler().ColorDiagnostics = true; |
| 121 | if (S == "never") |
| 122 | errorHandler().ColorDiagnostics = false; |
| 123 | if (S != "auto") |
| 124 | error("unknown option: -color-diagnostics=" + S); |
| 125 | } |
| 126 | } |
| 127 | |
| 128 | // Find a file by concatenating given paths. |
| 129 | static Optional<std::string> findFile(StringRef Path1, const Twine &Path2) { |
| 130 | SmallString<128> S; |
| 131 | path::append(S, Path1, Path2); |
| 132 | if (fs::exists(S)) |
| 133 | return S.str().str(); |
| 134 | return None; |
| 135 | } |
| 136 | |
Sam Clegg | c94d393 | 2017-11-17 18:14:09 +0000 | [diff] [blame] | 137 | // Inject a new undefined symbol into the link. This will cause the link to |
| 138 | // fail unless this symbol can be found. |
Sam Clegg | b862159 | 2017-11-30 01:40:08 +0000 | [diff] [blame] | 139 | static void addSyntheticUndefinedFunction(StringRef Name, |
| 140 | const WasmSignature *Type) { |
Sam Clegg | c94d393 | 2017-11-17 18:14:09 +0000 | [diff] [blame] | 141 | log("injecting undefined func: " + Name); |
Sam Clegg | b862159 | 2017-11-30 01:40:08 +0000 | [diff] [blame] | 142 | Symtab->addUndefinedFunction(Name, Type); |
Sam Clegg | c94d393 | 2017-11-17 18:14:09 +0000 | [diff] [blame] | 143 | } |
| 144 | |
| 145 | static void printHelp(const char *Argv0) { |
Rui Ueyama | 6074e6b | 2017-12-11 23:19:11 +0000 | [diff] [blame^] | 146 | WasmOptTable().PrintHelp(outs(), Argv0, "LLVM Linker", false); |
Sam Clegg | c94d393 | 2017-11-17 18:14:09 +0000 | [diff] [blame] | 147 | } |
| 148 | |
| 149 | WasmOptTable::WasmOptTable() : OptTable(OptInfo) {} |
| 150 | |
| 151 | opt::InputArgList WasmOptTable::parse(ArrayRef<const char *> Argv) { |
| 152 | SmallVector<const char *, 256> Vec(Argv.data(), Argv.data() + Argv.size()); |
| 153 | |
| 154 | unsigned MissingIndex; |
| 155 | unsigned MissingCount; |
| 156 | opt::InputArgList Args = this->ParseArgs(Vec, MissingIndex, MissingCount); |
| 157 | |
| 158 | handleColorDiagnostics(Args); |
| 159 | for (auto *Arg : Args.filtered(OPT_UNKNOWN)) |
| 160 | error("unknown argument: " + Arg->getSpelling()); |
| 161 | return Args; |
| 162 | } |
| 163 | |
| 164 | void LinkerDriver::addFile(StringRef Path) { |
| 165 | Optional<MemoryBufferRef> Buffer = readFile(Path); |
| 166 | if (!Buffer.hasValue()) |
| 167 | return; |
| 168 | MemoryBufferRef MBRef = *Buffer; |
| 169 | |
| 170 | if (identify_magic(MBRef.getBuffer()) == file_magic::archive) |
| 171 | Files.push_back(make<ArchiveFile>(MBRef)); |
| 172 | else |
| 173 | Files.push_back(make<ObjFile>(MBRef)); |
| 174 | } |
| 175 | |
| 176 | // Add a given library by searching it from input search paths. |
| 177 | void LinkerDriver::addLibrary(StringRef Name) { |
| 178 | for (StringRef Dir : Config->SearchPaths) { |
| 179 | if (Optional<std::string> S = findFile(Dir, "lib" + Name + ".a")) { |
| 180 | addFile(*S); |
| 181 | return; |
| 182 | } |
| 183 | } |
| 184 | |
| 185 | error("unable to find library -l" + Name); |
| 186 | } |
| 187 | |
| 188 | void LinkerDriver::createFiles(opt::InputArgList &Args) { |
| 189 | for (auto *Arg : Args) { |
| 190 | switch (Arg->getOption().getUnaliasedOption().getID()) { |
| 191 | case OPT_l: |
| 192 | addLibrary(Arg->getValue()); |
| 193 | break; |
| 194 | case OPT_INPUT: |
| 195 | addFile(Arg->getValue()); |
| 196 | break; |
| 197 | } |
| 198 | } |
| 199 | |
| 200 | if (Files.empty()) |
| 201 | error("no input files"); |
| 202 | } |
| 203 | |
Rui Ueyama | 909d123 | 2017-12-11 17:52:28 +0000 | [diff] [blame] | 204 | static StringRef getEntry(opt::InputArgList &Args, StringRef Default) { |
Sam Clegg | 2c096ba | 2017-12-08 17:58:25 +0000 | [diff] [blame] | 205 | auto *Arg = Args.getLastArg(OPT_entry, OPT_no_entry); |
| 206 | if (!Arg) |
Rui Ueyama | 909d123 | 2017-12-11 17:52:28 +0000 | [diff] [blame] | 207 | return Default; |
Sam Clegg | 2c096ba | 2017-12-08 17:58:25 +0000 | [diff] [blame] | 208 | if (Arg->getOption().getID() == OPT_no_entry) |
| 209 | return ""; |
| 210 | return Arg->getValue(); |
| 211 | } |
| 212 | |
Sam Clegg | c94d393 | 2017-11-17 18:14:09 +0000 | [diff] [blame] | 213 | void LinkerDriver::link(ArrayRef<const char *> ArgsArr) { |
| 214 | WasmOptTable Parser; |
| 215 | opt::InputArgList Args = Parser.parse(ArgsArr.slice(1)); |
| 216 | |
| 217 | // Handle --help |
| 218 | if (Args.hasArg(OPT_help)) { |
| 219 | printHelp(ArgsArr[0]); |
| 220 | return; |
| 221 | } |
| 222 | |
| 223 | // Parse and evaluate -mllvm options. |
| 224 | std::vector<const char *> V; |
Rui Ueyama | ceb15e8 | 2017-11-28 22:17:39 +0000 | [diff] [blame] | 225 | V.push_back("wasm-ld (LLVM option parsing)"); |
Sam Clegg | c94d393 | 2017-11-17 18:14:09 +0000 | [diff] [blame] | 226 | for (auto *Arg : Args.filtered(OPT_mllvm)) |
| 227 | V.push_back(Arg->getValue()); |
| 228 | cl::ParseCommandLineOptions(V.size(), V.data()); |
| 229 | |
Rui Ueyama | 3e03944 | 2017-11-28 19:58:45 +0000 | [diff] [blame] | 230 | errorHandler().ErrorLimit = args::getInteger(Args, OPT_error_limit, 20); |
Sam Clegg | c94d393 | 2017-11-17 18:14:09 +0000 | [diff] [blame] | 231 | |
| 232 | if (Args.hasArg(OPT_version) || Args.hasArg(OPT_v)) { |
| 233 | outs() << getLLDVersion() << "\n"; |
| 234 | return; |
| 235 | } |
| 236 | |
| 237 | Config->AllowUndefined = Args.hasArg(OPT_allow_undefined); |
Sam Clegg | b862159 | 2017-11-30 01:40:08 +0000 | [diff] [blame] | 238 | Config->CheckSignatures = |
| 239 | Args.hasFlag(OPT_check_signatures, OPT_no_check_signatures, false); |
Sam Clegg | c94d393 | 2017-11-17 18:14:09 +0000 | [diff] [blame] | 240 | Config->EmitRelocs = Args.hasArg(OPT_emit_relocs); |
Rui Ueyama | 8cbb3b5 | 2017-12-11 17:52:43 +0000 | [diff] [blame] | 241 | Config->Entry = getEntry(Args, Args.hasArg(OPT_relocatable) ? "" : "_start"); |
Sam Clegg | c94d393 | 2017-11-17 18:14:09 +0000 | [diff] [blame] | 242 | Config->ImportMemory = Args.hasArg(OPT_import_memory); |
| 243 | Config->OutputFile = Args.getLastArgValue(OPT_o); |
Rui Ueyama | 8cbb3b5 | 2017-12-11 17:52:43 +0000 | [diff] [blame] | 244 | Config->Relocatable = Args.hasArg(OPT_relocatable); |
Rui Ueyama | 3e03944 | 2017-11-28 19:58:45 +0000 | [diff] [blame] | 245 | Config->SearchPaths = args::getStrings(Args, OPT_L); |
Sam Clegg | c94d393 | 2017-11-17 18:14:09 +0000 | [diff] [blame] | 246 | Config->StripAll = Args.hasArg(OPT_strip_all); |
| 247 | Config->StripDebug = Args.hasArg(OPT_strip_debug); |
Sam Clegg | c94d393 | 2017-11-17 18:14:09 +0000 | [diff] [blame] | 248 | errorHandler().Verbose = Args.hasArg(OPT_verbose); |
| 249 | ThreadsEnabled = Args.hasFlag(OPT_threads, OPT_no_threads, true); |
Sam Clegg | 22cfe52 | 2017-12-05 16:53:25 +0000 | [diff] [blame] | 250 | if (Config->Relocatable) |
| 251 | Config->EmitRelocs = true; |
Sam Clegg | c94d393 | 2017-11-17 18:14:09 +0000 | [diff] [blame] | 252 | |
Rui Ueyama | 3e03944 | 2017-11-28 19:58:45 +0000 | [diff] [blame] | 253 | Config->InitialMemory = args::getInteger(Args, OPT_initial_memory, 0); |
| 254 | Config->GlobalBase = args::getInteger(Args, OPT_global_base, 1024); |
| 255 | Config->MaxMemory = args::getInteger(Args, OPT_max_memory, 0); |
| 256 | Config->ZStackSize = |
| 257 | args::getZOptionValue(Args, OPT_z, "stack-size", WasmPageSize); |
Sam Clegg | c94d393 | 2017-11-17 18:14:09 +0000 | [diff] [blame] | 258 | |
| 259 | if (auto *Arg = Args.getLastArg(OPT_allow_undefined_file)) |
| 260 | if (Optional<MemoryBufferRef> Buf = readFile(Arg->getValue())) |
Rui Ueyama | 3e03944 | 2017-11-28 19:58:45 +0000 | [diff] [blame] | 261 | for (StringRef Sym : args::getLines(*Buf)) |
Sam Clegg | c94d393 | 2017-11-17 18:14:09 +0000 | [diff] [blame] | 262 | Config->AllowUndefinedSymbols.insert(Sym); |
| 263 | |
| 264 | if (Config->OutputFile.empty()) |
| 265 | error("no output file specified"); |
| 266 | |
| 267 | if (!Args.hasArg(OPT_INPUT)) |
| 268 | error("no input files"); |
| 269 | |
| 270 | if (Config->Relocatable && !Config->Entry.empty()) |
| 271 | error("entry point specified for relocatable output file"); |
Sam Clegg | 31de2f0 | 2017-12-07 03:19:53 +0000 | [diff] [blame] | 272 | if (Config->Relocatable && Args.hasArg(OPT_undefined)) |
| 273 | error("undefined symbols specified for relocatable output file"); |
Sam Clegg | c94d393 | 2017-11-17 18:14:09 +0000 | [diff] [blame] | 274 | |
| 275 | if (!Config->Relocatable) { |
Sam Clegg | 2c096ba | 2017-12-08 17:58:25 +0000 | [diff] [blame] | 276 | if (!Config->Entry.empty()) { |
| 277 | static WasmSignature Signature = {{}, WASM_TYPE_NORESULT}; |
| 278 | addSyntheticUndefinedFunction(Config->Entry, &Signature); |
| 279 | } |
Sam Clegg | c94d393 | 2017-11-17 18:14:09 +0000 | [diff] [blame] | 280 | |
Sam Clegg | 31de2f0 | 2017-12-07 03:19:53 +0000 | [diff] [blame] | 281 | // Handle the `--undefined <sym>` options. |
| 282 | for (StringRef S : args::getStrings(Args, OPT_undefined)) |
| 283 | addSyntheticUndefinedFunction(S, nullptr); |
| 284 | |
Sam Clegg | c5872e8 | 2017-12-08 00:13:14 +0000 | [diff] [blame] | 285 | Config->StackPointerSymbol = Symtab->addDefinedGlobal("__stack_pointer"); |
Sam Clegg | c94d393 | 2017-11-17 18:14:09 +0000 | [diff] [blame] | 286 | } |
| 287 | |
| 288 | createFiles(Args); |
| 289 | if (errorCount()) |
| 290 | return; |
| 291 | |
| 292 | // Add all files to the symbol table. This will add almost all |
| 293 | // symbols that we need to the symbol table. |
| 294 | for (InputFile *F : Files) |
| 295 | Symtab->addFile(F); |
| 296 | |
| 297 | // Make sure we have resolved all symbols. |
| 298 | if (!Config->Relocatable && !Config->AllowUndefined) { |
| 299 | Symtab->reportRemainingUndefines(); |
Sam Clegg | 31de2f0 | 2017-12-07 03:19:53 +0000 | [diff] [blame] | 300 | } else { |
| 301 | // When we allow undefined symbols we cannot include those defined in |
| 302 | // -u/--undefined since these undefined symbols have only names and no |
| 303 | // function signature, which means they cannot be written to the final |
| 304 | // output. |
| 305 | for (StringRef S : args::getStrings(Args, OPT_undefined)) { |
| 306 | Symbol *Sym = Symtab->find(S); |
| 307 | if (!Sym->isDefined()) |
| 308 | error("function forced with --undefined not found: " + Sym->getName()); |
| 309 | } |
Sam Clegg | c94d393 | 2017-11-17 18:14:09 +0000 | [diff] [blame] | 310 | } |
Rui Ueyama | 9d8ce23 | 2017-12-11 23:09:03 +0000 | [diff] [blame] | 311 | if (errorCount()) |
| 312 | return; |
Sam Clegg | c94d393 | 2017-11-17 18:14:09 +0000 | [diff] [blame] | 313 | |
Sam Clegg | 2c096ba | 2017-12-08 17:58:25 +0000 | [diff] [blame] | 314 | if (!Config->Entry.empty() && !Symtab->find(Config->Entry)->isDefined()) |
| 315 | error("entry point not found: " + Config->Entry); |
| 316 | if (errorCount()) |
| 317 | return; |
Sam Clegg | c94d393 | 2017-11-17 18:14:09 +0000 | [diff] [blame] | 318 | |
| 319 | // Write the result to the file. |
| 320 | writeResult(); |
| 321 | } |