1 //===- Driver.cpp ---------------------------------------------------------===// 2 // 3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 4 // See https://llvm.org/LICENSE.txt for license information. 5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 6 // 7 //===----------------------------------------------------------------------===// 8 9 #include "lld/Common/Driver.h" 10 #include "Config.h" 11 #include "InputChunks.h" 12 #include "InputGlobal.h" 13 #include "MarkLive.h" 14 #include "SymbolTable.h" 15 #include "Writer.h" 16 #include "lld/Common/Args.h" 17 #include "lld/Common/ErrorHandler.h" 18 #include "lld/Common/Filesystem.h" 19 #include "lld/Common/Memory.h" 20 #include "lld/Common/Reproduce.h" 21 #include "lld/Common/Strings.h" 22 #include "lld/Common/Version.h" 23 #include "llvm/ADT/Twine.h" 24 #include "llvm/Object/Wasm.h" 25 #include "llvm/Option/Arg.h" 26 #include "llvm/Option/ArgList.h" 27 #include "llvm/Support/CommandLine.h" 28 #include "llvm/Support/Host.h" 29 #include "llvm/Support/Parallel.h" 30 #include "llvm/Support/Path.h" 31 #include "llvm/Support/Process.h" 32 #include "llvm/Support/TarWriter.h" 33 #include "llvm/Support/TargetSelect.h" 34 35 #define DEBUG_TYPE "lld" 36 37 using namespace llvm; 38 using namespace llvm::object; 39 using namespace llvm::sys; 40 using namespace llvm::wasm; 41 42 namespace lld { 43 namespace wasm { 44 Configuration *config; 45 46 namespace { 47 48 // Create enum with OPT_xxx values for each option in Options.td 49 enum { 50 OPT_INVALID = 0, 51 #define OPTION(_1, _2, ID, _4, _5, _6, _7, _8, _9, _10, _11, _12) OPT_##ID, 52 #include "Options.inc" 53 #undef OPTION 54 }; 55 56 // This function is called on startup. We need this for LTO since 57 // LTO calls LLVM functions to compile bitcode files to native code. 58 // Technically this can be delayed until we read bitcode files, but 59 // we don't bother to do lazily because the initialization is fast. 60 static void initLLVM() { 61 InitializeAllTargets(); 62 InitializeAllTargetMCs(); 63 InitializeAllAsmPrinters(); 64 InitializeAllAsmParsers(); 65 } 66 67 class LinkerDriver { 68 public: 69 void link(ArrayRef<const char *> argsArr); 70 71 private: 72 void createFiles(opt::InputArgList &args); 73 void addFile(StringRef path); 74 void addLibrary(StringRef name); 75 76 // True if we are in --whole-archive and --no-whole-archive. 77 bool inWholeArchive = false; 78 79 std::vector<InputFile *> files; 80 }; 81 } // anonymous namespace 82 83 bool link(ArrayRef<const char *> args, bool canExitEarly, raw_ostream &stdoutOS, 84 raw_ostream &stderrOS) { 85 lld::stdoutOS = &stdoutOS; 86 lld::stderrOS = &stderrOS; 87 88 errorHandler().logName = args::getFilenameWithoutExe(args[0]); 89 errorHandler().errorLimitExceededMsg = 90 "too many errors emitted, stopping now (use " 91 "-error-limit=0 to see all errors)"; 92 stderrOS.enable_colors(stderrOS.has_colors()); 93 94 config = make<Configuration>(); 95 symtab = make<SymbolTable>(); 96 97 initLLVM(); 98 LinkerDriver().link(args); 99 100 // Exit immediately if we don't need to return to the caller. 101 // This saves time because the overhead of calling destructors 102 // for all globally-allocated objects is not negligible. 103 if (canExitEarly) 104 exitLld(errorCount() ? 1 : 0); 105 106 freeArena(); 107 return !errorCount(); 108 } 109 110 // Create prefix string literals used in Options.td 111 #define PREFIX(NAME, VALUE) const char *const NAME[] = VALUE; 112 #include "Options.inc" 113 #undef PREFIX 114 115 // Create table mapping all options defined in Options.td 116 static const opt::OptTable::Info optInfo[] = { 117 #define OPTION(X1, X2, ID, KIND, GROUP, ALIAS, X7, X8, X9, X10, X11, X12) \ 118 {X1, X2, X10, X11, OPT_##ID, opt::Option::KIND##Class, \ 119 X9, X8, OPT_##GROUP, OPT_##ALIAS, X7, X12}, 120 #include "Options.inc" 121 #undef OPTION 122 }; 123 124 namespace { 125 class WasmOptTable : public llvm::opt::OptTable { 126 public: 127 WasmOptTable() : OptTable(optInfo) {} 128 opt::InputArgList parse(ArrayRef<const char *> argv); 129 }; 130 } // namespace 131 132 // Set color diagnostics according to -color-diagnostics={auto,always,never} 133 // or -no-color-diagnostics flags. 134 static void handleColorDiagnostics(opt::InputArgList &args) { 135 auto *arg = args.getLastArg(OPT_color_diagnostics, OPT_color_diagnostics_eq, 136 OPT_no_color_diagnostics); 137 if (!arg) 138 return; 139 if (arg->getOption().getID() == OPT_color_diagnostics) { 140 lld::errs().enable_colors(true); 141 } else if (arg->getOption().getID() == OPT_no_color_diagnostics) { 142 lld::errs().enable_colors(false); 143 } else { 144 StringRef s = arg->getValue(); 145 if (s == "always") 146 lld::errs().enable_colors(true); 147 else if (s == "never") 148 lld::errs().enable_colors(false); 149 else if (s != "auto") 150 error("unknown option: --color-diagnostics=" + s); 151 } 152 } 153 154 static cl::TokenizerCallback getQuotingStyle(opt::InputArgList &args) { 155 if (auto *arg = args.getLastArg(OPT_rsp_quoting)) { 156 StringRef s = arg->getValue(); 157 if (s != "windows" && s != "posix") 158 error("invalid response file quoting: " + s); 159 if (s == "windows") 160 return cl::TokenizeWindowsCommandLine; 161 return cl::TokenizeGNUCommandLine; 162 } 163 if (Triple(sys::getProcessTriple()).isOSWindows()) 164 return cl::TokenizeWindowsCommandLine; 165 return cl::TokenizeGNUCommandLine; 166 } 167 168 // Find a file by concatenating given paths. 169 static Optional<std::string> findFile(StringRef path1, const Twine &path2) { 170 SmallString<128> s; 171 path::append(s, path1, path2); 172 if (fs::exists(s)) 173 return std::string(s); 174 return None; 175 } 176 177 opt::InputArgList WasmOptTable::parse(ArrayRef<const char *> argv) { 178 SmallVector<const char *, 256> vec(argv.data(), argv.data() + argv.size()); 179 180 unsigned missingIndex; 181 unsigned missingCount; 182 183 // We need to get the quoting style for response files before parsing all 184 // options so we parse here before and ignore all the options but 185 // --rsp-quoting. 186 opt::InputArgList args = this->ParseArgs(vec, missingIndex, missingCount); 187 188 // Expand response files (arguments in the form of @<filename>) 189 // and then parse the argument again. 190 cl::ExpandResponseFiles(saver, getQuotingStyle(args), vec); 191 args = this->ParseArgs(vec, missingIndex, missingCount); 192 193 handleColorDiagnostics(args); 194 for (auto *arg : args.filtered(OPT_UNKNOWN)) 195 error("unknown argument: " + arg->getAsString(args)); 196 return args; 197 } 198 199 // Currently we allow a ".imports" to live alongside a library. This can 200 // be used to specify a list of symbols which can be undefined at link 201 // time (imported from the environment. For example libc.a include an 202 // import file that lists the syscall functions it relies on at runtime. 203 // In the long run this information would be better stored as a symbol 204 // attribute/flag in the object file itself. 205 // See: https://github.com/WebAssembly/tool-conventions/issues/35 206 static void readImportFile(StringRef filename) { 207 if (Optional<MemoryBufferRef> buf = readFile(filename)) 208 for (StringRef sym : args::getLines(*buf)) 209 config->allowUndefinedSymbols.insert(sym); 210 } 211 212 // Returns slices of MB by parsing MB as an archive file. 213 // Each slice consists of a member file in the archive. 214 std::vector<MemoryBufferRef> static getArchiveMembers(MemoryBufferRef mb) { 215 std::unique_ptr<Archive> file = 216 CHECK(Archive::create(mb), 217 mb.getBufferIdentifier() + ": failed to parse archive"); 218 219 std::vector<MemoryBufferRef> v; 220 Error err = Error::success(); 221 for (const Archive::Child &c : file->children(err)) { 222 MemoryBufferRef mbref = 223 CHECK(c.getMemoryBufferRef(), 224 mb.getBufferIdentifier() + 225 ": could not get the buffer for a child of the archive"); 226 v.push_back(mbref); 227 } 228 if (err) 229 fatal(mb.getBufferIdentifier() + 230 ": Archive::children failed: " + toString(std::move(err))); 231 232 // Take ownership of memory buffers created for members of thin archives. 233 for (std::unique_ptr<MemoryBuffer> &mb : file->takeThinBuffers()) 234 make<std::unique_ptr<MemoryBuffer>>(std::move(mb)); 235 236 return v; 237 } 238 239 void LinkerDriver::addFile(StringRef path) { 240 Optional<MemoryBufferRef> buffer = readFile(path); 241 if (!buffer.hasValue()) 242 return; 243 MemoryBufferRef mbref = *buffer; 244 245 switch (identify_magic(mbref.getBuffer())) { 246 case file_magic::archive: { 247 SmallString<128> importFile = path; 248 path::replace_extension(importFile, ".imports"); 249 if (fs::exists(importFile)) 250 readImportFile(importFile.str()); 251 252 // Handle -whole-archive. 253 if (inWholeArchive) { 254 for (MemoryBufferRef &m : getArchiveMembers(mbref)) 255 files.push_back(createObjectFile(m, path)); 256 return; 257 } 258 259 std::unique_ptr<Archive> file = 260 CHECK(Archive::create(mbref), path + ": failed to parse archive"); 261 262 if (!file->isEmpty() && !file->hasSymbolTable()) { 263 error(mbref.getBufferIdentifier() + 264 ": archive has no index; run ranlib to add one"); 265 } 266 267 files.push_back(make<ArchiveFile>(mbref)); 268 return; 269 } 270 case file_magic::bitcode: 271 case file_magic::wasm_object: 272 files.push_back(createObjectFile(mbref)); 273 break; 274 default: 275 error("unknown file type: " + mbref.getBufferIdentifier()); 276 } 277 } 278 279 // Add a given library by searching it from input search paths. 280 void LinkerDriver::addLibrary(StringRef name) { 281 for (StringRef dir : config->searchPaths) { 282 if (Optional<std::string> s = findFile(dir, "lib" + name + ".a")) { 283 addFile(*s); 284 return; 285 } 286 } 287 288 error("unable to find library -l" + name); 289 } 290 291 void LinkerDriver::createFiles(opt::InputArgList &args) { 292 for (auto *arg : args) { 293 switch (arg->getOption().getID()) { 294 case OPT_l: 295 addLibrary(arg->getValue()); 296 break; 297 case OPT_INPUT: 298 addFile(arg->getValue()); 299 break; 300 case OPT_whole_archive: 301 inWholeArchive = true; 302 break; 303 case OPT_no_whole_archive: 304 inWholeArchive = false; 305 break; 306 } 307 } 308 if (files.empty() && errorCount() == 0) 309 error("no input files"); 310 } 311 312 static StringRef getEntry(opt::InputArgList &args) { 313 auto *arg = args.getLastArg(OPT_entry, OPT_no_entry); 314 if (!arg) { 315 if (args.hasArg(OPT_relocatable)) 316 return ""; 317 if (args.hasArg(OPT_shared)) 318 return "__wasm_call_ctors"; 319 return "_start"; 320 } 321 if (arg->getOption().getID() == OPT_no_entry) 322 return ""; 323 return arg->getValue(); 324 } 325 326 // Initializes Config members by the command line options. 327 static void readConfigs(opt::InputArgList &args) { 328 config->allowUndefined = args.hasArg(OPT_allow_undefined); 329 config->checkFeatures = 330 args.hasFlag(OPT_check_features, OPT_no_check_features, true); 331 config->compressRelocations = args.hasArg(OPT_compress_relocations); 332 config->demangle = args.hasFlag(OPT_demangle, OPT_no_demangle, true); 333 config->disableVerify = args.hasArg(OPT_disable_verify); 334 config->emitRelocs = args.hasArg(OPT_emit_relocs); 335 config->experimentalPic = args.hasArg(OPT_experimental_pic); 336 config->entry = getEntry(args); 337 config->exportAll = args.hasArg(OPT_export_all); 338 config->exportTable = args.hasArg(OPT_export_table); 339 config->growableTable = args.hasArg(OPT_growable_table); 340 errorHandler().fatalWarnings = 341 args.hasFlag(OPT_fatal_warnings, OPT_no_fatal_warnings, false); 342 config->importMemory = args.hasArg(OPT_import_memory); 343 config->sharedMemory = args.hasArg(OPT_shared_memory); 344 config->importTable = args.hasArg(OPT_import_table); 345 config->ltoo = args::getInteger(args, OPT_lto_O, 2); 346 config->ltoPartitions = args::getInteger(args, OPT_lto_partitions, 1); 347 config->mapFile = args.getLastArgValue(OPT_Map); 348 config->optimize = args::getInteger(args, OPT_O, 0); 349 config->outputFile = args.getLastArgValue(OPT_o); 350 config->relocatable = args.hasArg(OPT_relocatable); 351 config->gcSections = 352 args.hasFlag(OPT_gc_sections, OPT_no_gc_sections, !config->relocatable); 353 config->mergeDataSegments = 354 args.hasFlag(OPT_merge_data_segments, OPT_no_merge_data_segments, 355 !config->relocatable); 356 config->pie = args.hasFlag(OPT_pie, OPT_no_pie, false); 357 config->printGcSections = 358 args.hasFlag(OPT_print_gc_sections, OPT_no_print_gc_sections, false); 359 config->saveTemps = args.hasArg(OPT_save_temps); 360 config->searchPaths = args::getStrings(args, OPT_L); 361 config->shared = args.hasArg(OPT_shared); 362 config->stripAll = args.hasArg(OPT_strip_all); 363 config->stripDebug = args.hasArg(OPT_strip_debug); 364 config->stackFirst = args.hasArg(OPT_stack_first); 365 config->trace = args.hasArg(OPT_trace); 366 config->thinLTOCacheDir = args.getLastArgValue(OPT_thinlto_cache_dir); 367 config->thinLTOCachePolicy = CHECK( 368 parseCachePruningPolicy(args.getLastArgValue(OPT_thinlto_cache_policy)), 369 "--thinlto-cache-policy: invalid cache policy"); 370 errorHandler().verbose = args.hasArg(OPT_verbose); 371 LLVM_DEBUG(errorHandler().verbose = true); 372 373 config->initialMemory = args::getInteger(args, OPT_initial_memory, 0); 374 config->globalBase = args::getInteger(args, OPT_global_base, 1024); 375 config->maxMemory = args::getInteger(args, OPT_max_memory, 0); 376 config->zStackSize = 377 args::getZOptionValue(args, OPT_z, "stack-size", WasmPageSize); 378 379 // Default value of exportDynamic depends on `-shared` 380 config->exportDynamic = 381 args.hasFlag(OPT_export_dynamic, OPT_no_export_dynamic, config->shared); 382 383 // Parse wasm32/64. 384 if (auto *arg = args.getLastArg(OPT_m)) { 385 StringRef s = arg->getValue(); 386 if (s == "wasm32") 387 config->is64 = false; 388 else if (s == "wasm64") 389 config->is64 = true; 390 else 391 error("invalid target architecture: " + s); 392 } 393 394 // --threads= takes a positive integer and provides the default value for 395 // --thinlto-jobs=. 396 if (auto *arg = args.getLastArg(OPT_threads)) { 397 StringRef v(arg->getValue()); 398 unsigned threads = 0; 399 if (!llvm::to_integer(v, threads, 0) || threads == 0) 400 error(arg->getSpelling() + ": expected a positive integer, but got '" + 401 arg->getValue() + "'"); 402 parallel::strategy = hardware_concurrency(threads); 403 config->thinLTOJobs = v; 404 } 405 if (auto *arg = args.getLastArg(OPT_thinlto_jobs)) 406 config->thinLTOJobs = arg->getValue(); 407 408 if (auto *arg = args.getLastArg(OPT_features)) { 409 config->features = 410 llvm::Optional<std::vector<std::string>>(std::vector<std::string>()); 411 for (StringRef s : arg->getValues()) 412 config->features->push_back(std::string(s)); 413 } 414 415 if (args.hasArg(OPT_print_map)) 416 config->mapFile = "-"; 417 } 418 419 // Some Config members do not directly correspond to any particular 420 // command line options, but computed based on other Config values. 421 // This function initialize such members. See Config.h for the details 422 // of these values. 423 static void setConfigs() { 424 config->isPic = config->pie || config->shared; 425 426 if (config->isPic) { 427 if (config->exportTable) 428 error("-shared/-pie is incompatible with --export-table"); 429 config->importTable = true; 430 } 431 432 if (config->shared) { 433 config->importMemory = true; 434 config->allowUndefined = true; 435 } 436 } 437 438 // Some command line options or some combinations of them are not allowed. 439 // This function checks for such errors. 440 static void checkOptions(opt::InputArgList &args) { 441 if (!config->stripDebug && !config->stripAll && config->compressRelocations) 442 error("--compress-relocations is incompatible with output debug" 443 " information. Please pass --strip-debug or --strip-all"); 444 445 if (config->ltoo > 3) 446 error("invalid optimization level for LTO: " + Twine(config->ltoo)); 447 if (config->ltoPartitions == 0) 448 error("--lto-partitions: number of threads must be > 0"); 449 if (!get_threadpool_strategy(config->thinLTOJobs)) 450 error("--thinlto-jobs: invalid job count: " + config->thinLTOJobs); 451 452 if (config->pie && config->shared) 453 error("-shared and -pie may not be used together"); 454 455 if (config->outputFile.empty()) 456 error("no output file specified"); 457 458 if (config->importTable && config->exportTable) 459 error("--import-table and --export-table may not be used together"); 460 461 if (config->relocatable) { 462 if (!config->entry.empty()) 463 error("entry point specified for relocatable output file"); 464 if (config->gcSections) 465 error("-r and --gc-sections may not be used together"); 466 if (config->compressRelocations) 467 error("-r -and --compress-relocations may not be used together"); 468 if (args.hasArg(OPT_undefined)) 469 error("-r -and --undefined may not be used together"); 470 if (config->pie) 471 error("-r and -pie may not be used together"); 472 if (config->sharedMemory) 473 error("-r and --shared-memory may not be used together"); 474 } 475 476 // To begin to prepare for Module Linking-style shared libraries, start 477 // warning about uses of `-shared` and related flags outside of Experimental 478 // mode, to give anyone using them a heads-up that they will be changing. 479 // 480 // Also, warn about flags which request explicit exports. 481 if (!config->experimentalPic) { 482 // -shared will change meaning when Module Linking is implemented. 483 if (config->shared) { 484 warn("creating shared libraries, with -shared, is not yet stable"); 485 } 486 487 // -pie will change meaning when Module Linking is implemented. 488 if (config->pie) { 489 warn("creating PIEs, with -pie, is not yet stable"); 490 } 491 } 492 } 493 494 // Force Sym to be entered in the output. Used for -u or equivalent. 495 static Symbol *handleUndefined(StringRef name) { 496 Symbol *sym = symtab->find(name); 497 if (!sym) 498 return nullptr; 499 500 // Since symbol S may not be used inside the program, LTO may 501 // eliminate it. Mark the symbol as "used" to prevent it. 502 sym->isUsedInRegularObj = true; 503 504 if (auto *lazySym = dyn_cast<LazySymbol>(sym)) 505 lazySym->fetch(); 506 507 return sym; 508 } 509 510 static void handleLibcall(StringRef name) { 511 Symbol *sym = symtab->find(name); 512 if (!sym) 513 return; 514 515 if (auto *lazySym = dyn_cast<LazySymbol>(sym)) { 516 MemoryBufferRef mb = lazySym->getMemberBuffer(); 517 if (isBitcode(mb)) 518 lazySym->fetch(); 519 } 520 } 521 522 static UndefinedGlobal * 523 createUndefinedGlobal(StringRef name, llvm::wasm::WasmGlobalType *type) { 524 auto *sym = cast<UndefinedGlobal>(symtab->addUndefinedGlobal( 525 name, None, None, WASM_SYMBOL_UNDEFINED, nullptr, type)); 526 config->allowUndefinedSymbols.insert(sym->getName()); 527 sym->isUsedInRegularObj = true; 528 return sym; 529 } 530 531 static GlobalSymbol *createGlobalVariable(StringRef name, bool isMutable, 532 int value) { 533 llvm::wasm::WasmGlobal wasmGlobal; 534 if (config->is64.getValueOr(false)) { 535 wasmGlobal.Type = {WASM_TYPE_I64, isMutable}; 536 wasmGlobal.InitExpr.Value.Int64 = value; 537 wasmGlobal.InitExpr.Opcode = WASM_OPCODE_I64_CONST; 538 } else { 539 wasmGlobal.Type = {WASM_TYPE_I32, isMutable}; 540 wasmGlobal.InitExpr.Value.Int32 = value; 541 wasmGlobal.InitExpr.Opcode = WASM_OPCODE_I32_CONST; 542 } 543 wasmGlobal.SymbolName = name; 544 return symtab->addSyntheticGlobal(name, WASM_SYMBOL_VISIBILITY_HIDDEN, 545 make<InputGlobal>(wasmGlobal, nullptr)); 546 } 547 548 // Create ABI-defined synthetic symbols 549 static void createSyntheticSymbols() { 550 if (config->relocatable) 551 return; 552 553 static WasmSignature nullSignature = {{}, {}}; 554 static WasmSignature i32ArgSignature = {{}, {ValType::I32}}; 555 static WasmSignature i64ArgSignature = {{}, {ValType::I64}}; 556 static llvm::wasm::WasmGlobalType globalTypeI32 = {WASM_TYPE_I32, false}; 557 static llvm::wasm::WasmGlobalType globalTypeI64 = {WASM_TYPE_I64, false}; 558 static llvm::wasm::WasmGlobalType mutableGlobalTypeI32 = {WASM_TYPE_I32, 559 true}; 560 static llvm::wasm::WasmGlobalType mutableGlobalTypeI64 = {WASM_TYPE_I64, 561 true}; 562 WasmSym::callCtors = symtab->addSyntheticFunction( 563 "__wasm_call_ctors", WASM_SYMBOL_VISIBILITY_HIDDEN, 564 make<SyntheticFunction>(nullSignature, "__wasm_call_ctors")); 565 566 if (config->isPic) { 567 // For PIC code we create a synthetic function __wasm_apply_relocs which 568 // is called from __wasm_call_ctors before the user-level constructors. 569 WasmSym::applyRelocs = symtab->addSyntheticFunction( 570 "__wasm_apply_relocs", WASM_SYMBOL_VISIBILITY_HIDDEN, 571 make<SyntheticFunction>(nullSignature, "__wasm_apply_relocs")); 572 } 573 574 575 if (config->isPic) { 576 WasmSym::stackPointer = 577 createUndefinedGlobal("__stack_pointer", config->is64.getValueOr(false) 578 ? &mutableGlobalTypeI64 579 : &mutableGlobalTypeI32); 580 // For PIC code, we import two global variables (__memory_base and 581 // __table_base) from the environment and use these as the offset at 582 // which to load our static data and function table. 583 // See: 584 // https://github.com/WebAssembly/tool-conventions/blob/master/DynamicLinking.md 585 WasmSym::memoryBase = createUndefinedGlobal( 586 "__memory_base", 587 config->is64.getValueOr(false) ? &globalTypeI64 : &globalTypeI32); 588 WasmSym::tableBase = createUndefinedGlobal("__table_base", &globalTypeI32); 589 WasmSym::memoryBase->markLive(); 590 WasmSym::tableBase->markLive(); 591 } else { 592 // For non-PIC code 593 WasmSym::stackPointer = createGlobalVariable("__stack_pointer", true, 0); 594 WasmSym::stackPointer->markLive(); 595 } 596 597 if (config->sharedMemory && !config->shared) { 598 // Passive segments are used to avoid memory being reinitialized on each 599 // thread's instantiation. These passive segments are initialized and 600 // dropped in __wasm_init_memory, which is registered as the start function 601 WasmSym::initMemory = symtab->addSyntheticFunction( 602 "__wasm_init_memory", WASM_SYMBOL_VISIBILITY_HIDDEN, 603 make<SyntheticFunction>(nullSignature, "__wasm_init_memory")); 604 WasmSym::initMemoryFlag = symtab->addSyntheticDataSymbol( 605 "__wasm_init_memory_flag", WASM_SYMBOL_VISIBILITY_HIDDEN); 606 assert(WasmSym::initMemoryFlag); 607 WasmSym::tlsBase = createGlobalVariable("__tls_base", true, 0); 608 WasmSym::tlsSize = createGlobalVariable("__tls_size", false, 0); 609 WasmSym::tlsAlign = createGlobalVariable("__tls_align", false, 1); 610 WasmSym::initTLS = symtab->addSyntheticFunction( 611 "__wasm_init_tls", WASM_SYMBOL_VISIBILITY_HIDDEN, 612 make<SyntheticFunction>( 613 config->is64.getValueOr(false) ? i64ArgSignature : i32ArgSignature, 614 "__wasm_init_tls")); 615 } 616 } 617 618 static void createOptionalSymbols() { 619 if (config->relocatable) 620 return; 621 622 WasmSym::dsoHandle = symtab->addOptionalDataSymbol("__dso_handle"); 623 624 if (!config->shared) 625 WasmSym::dataEnd = symtab->addOptionalDataSymbol("__data_end"); 626 627 if (!config->isPic) { 628 WasmSym::globalBase = symtab->addOptionalDataSymbol("__global_base"); 629 WasmSym::heapBase = symtab->addOptionalDataSymbol("__heap_base"); 630 WasmSym::definedMemoryBase = symtab->addOptionalDataSymbol("__memory_base"); 631 WasmSym::definedTableBase = symtab->addOptionalDataSymbol("__table_base"); 632 } 633 } 634 635 // Reconstructs command line arguments so that so that you can re-run 636 // the same command with the same inputs. This is for --reproduce. 637 static std::string createResponseFile(const opt::InputArgList &args) { 638 SmallString<0> data; 639 raw_svector_ostream os(data); 640 641 // Copy the command line to the output while rewriting paths. 642 for (auto *arg : args) { 643 switch (arg->getOption().getID()) { 644 case OPT_reproduce: 645 break; 646 case OPT_INPUT: 647 os << quote(relativeToRoot(arg->getValue())) << "\n"; 648 break; 649 case OPT_o: 650 // If -o path contains directories, "lld @response.txt" will likely 651 // fail because the archive we are creating doesn't contain empty 652 // directories for the output path (-o doesn't create directories). 653 // Strip directories to prevent the issue. 654 os << "-o " << quote(sys::path::filename(arg->getValue())) << "\n"; 655 break; 656 default: 657 os << toString(*arg) << "\n"; 658 } 659 } 660 return std::string(data.str()); 661 } 662 663 // The --wrap option is a feature to rename symbols so that you can write 664 // wrappers for existing functions. If you pass `-wrap=foo`, all 665 // occurrences of symbol `foo` are resolved to `wrap_foo` (so, you are 666 // expected to write `wrap_foo` function as a wrapper). The original 667 // symbol becomes accessible as `real_foo`, so you can call that from your 668 // wrapper. 669 // 670 // This data structure is instantiated for each -wrap option. 671 struct WrappedSymbol { 672 Symbol *sym; 673 Symbol *real; 674 Symbol *wrap; 675 }; 676 677 static Symbol *addUndefined(StringRef name) { 678 return symtab->addUndefinedFunction(name, None, None, WASM_SYMBOL_UNDEFINED, 679 nullptr, nullptr, false); 680 } 681 682 // Handles -wrap option. 683 // 684 // This function instantiates wrapper symbols. At this point, they seem 685 // like they are not being used at all, so we explicitly set some flags so 686 // that LTO won't eliminate them. 687 static std::vector<WrappedSymbol> addWrappedSymbols(opt::InputArgList &args) { 688 std::vector<WrappedSymbol> v; 689 DenseSet<StringRef> seen; 690 691 for (auto *arg : args.filtered(OPT_wrap)) { 692 StringRef name = arg->getValue(); 693 if (!seen.insert(name).second) 694 continue; 695 696 Symbol *sym = symtab->find(name); 697 if (!sym) 698 continue; 699 700 Symbol *real = addUndefined(saver.save("__real_" + name)); 701 Symbol *wrap = addUndefined(saver.save("__wrap_" + name)); 702 v.push_back({sym, real, wrap}); 703 704 // We want to tell LTO not to inline symbols to be overwritten 705 // because LTO doesn't know the final symbol contents after renaming. 706 real->canInline = false; 707 sym->canInline = false; 708 709 // Tell LTO not to eliminate these symbols. 710 sym->isUsedInRegularObj = true; 711 wrap->isUsedInRegularObj = true; 712 real->isUsedInRegularObj = false; 713 } 714 return v; 715 } 716 717 // Do renaming for -wrap by updating pointers to symbols. 718 // 719 // When this function is executed, only InputFiles and symbol table 720 // contain pointers to symbol objects. We visit them to replace pointers, 721 // so that wrapped symbols are swapped as instructed by the command line. 722 static void wrapSymbols(ArrayRef<WrappedSymbol> wrapped) { 723 DenseMap<Symbol *, Symbol *> map; 724 for (const WrappedSymbol &w : wrapped) { 725 map[w.sym] = w.wrap; 726 map[w.real] = w.sym; 727 } 728 729 // Update pointers in input files. 730 parallelForEach(symtab->objectFiles, [&](InputFile *file) { 731 MutableArrayRef<Symbol *> syms = file->getMutableSymbols(); 732 for (size_t i = 0, e = syms.size(); i != e; ++i) 733 if (Symbol *s = map.lookup(syms[i])) 734 syms[i] = s; 735 }); 736 737 // Update pointers in the symbol table. 738 for (const WrappedSymbol &w : wrapped) 739 symtab->wrap(w.sym, w.real, w.wrap); 740 } 741 742 void LinkerDriver::link(ArrayRef<const char *> argsArr) { 743 WasmOptTable parser; 744 opt::InputArgList args = parser.parse(argsArr.slice(1)); 745 746 // Handle --help 747 if (args.hasArg(OPT_help)) { 748 parser.PrintHelp(lld::outs(), 749 (std::string(argsArr[0]) + " [options] file...").c_str(), 750 "LLVM Linker", false); 751 return; 752 } 753 754 // Handle --version 755 if (args.hasArg(OPT_version) || args.hasArg(OPT_v)) { 756 lld::outs() << getLLDVersion() << "\n"; 757 return; 758 } 759 760 // Handle --reproduce 761 if (auto *arg = args.getLastArg(OPT_reproduce)) { 762 StringRef path = arg->getValue(); 763 Expected<std::unique_ptr<TarWriter>> errOrWriter = 764 TarWriter::create(path, path::stem(path)); 765 if (errOrWriter) { 766 tar = std::move(*errOrWriter); 767 tar->append("response.txt", createResponseFile(args)); 768 tar->append("version.txt", getLLDVersion() + "\n"); 769 } else { 770 error("--reproduce: " + toString(errOrWriter.takeError())); 771 } 772 } 773 774 // Parse and evaluate -mllvm options. 775 std::vector<const char *> v; 776 v.push_back("wasm-ld (LLVM option parsing)"); 777 for (auto *arg : args.filtered(OPT_mllvm)) 778 v.push_back(arg->getValue()); 779 cl::ParseCommandLineOptions(v.size(), v.data()); 780 781 errorHandler().errorLimit = args::getInteger(args, OPT_error_limit, 20); 782 783 readConfigs(args); 784 785 createFiles(args); 786 if (errorCount()) 787 return; 788 789 setConfigs(); 790 checkOptions(args); 791 if (errorCount()) 792 return; 793 794 if (auto *arg = args.getLastArg(OPT_allow_undefined_file)) 795 readImportFile(arg->getValue()); 796 797 // Fail early if the output file or map file is not writable. If a user has a 798 // long link, e.g. due to a large LTO link, they do not wish to run it and 799 // find that it failed because there was a mistake in their command-line. 800 if (auto e = tryCreateFile(config->outputFile)) 801 error("cannot open output file " + config->outputFile + ": " + e.message()); 802 if (auto e = tryCreateFile(config->mapFile)) 803 error("cannot open map file " + config->mapFile + ": " + e.message()); 804 if (errorCount()) 805 return; 806 807 // Handle --trace-symbol. 808 for (auto *arg : args.filtered(OPT_trace_symbol)) 809 symtab->trace(arg->getValue()); 810 811 for (auto *arg : args.filtered(OPT_export)) 812 config->exportedSymbols.insert(arg->getValue()); 813 814 createSyntheticSymbols(); 815 816 // Add all files to the symbol table. This will add almost all 817 // symbols that we need to the symbol table. 818 for (InputFile *f : files) 819 symtab->addFile(f); 820 if (errorCount()) 821 return; 822 823 // Handle the `--undefined <sym>` options. 824 for (auto *arg : args.filtered(OPT_undefined)) 825 handleUndefined(arg->getValue()); 826 827 // Handle the `--export <sym>` options 828 // This works like --undefined but also exports the symbol if its found 829 for (auto *arg : args.filtered(OPT_export)) 830 handleUndefined(arg->getValue()); 831 832 Symbol *entrySym = nullptr; 833 if (!config->relocatable && !config->entry.empty()) { 834 entrySym = handleUndefined(config->entry); 835 if (entrySym && entrySym->isDefined()) 836 entrySym->forceExport = true; 837 else 838 error("entry symbol not defined (pass --no-entry to suppress): " + 839 config->entry); 840 } 841 842 createOptionalSymbols(); 843 844 if (errorCount()) 845 return; 846 847 // Create wrapped symbols for -wrap option. 848 std::vector<WrappedSymbol> wrapped = addWrappedSymbols(args); 849 850 // If any of our inputs are bitcode files, the LTO code generator may create 851 // references to certain library functions that might not be explicit in the 852 // bitcode file's symbol table. If any of those library functions are defined 853 // in a bitcode file in an archive member, we need to arrange to use LTO to 854 // compile those archive members by adding them to the link beforehand. 855 // 856 // We only need to add libcall symbols to the link before LTO if the symbol's 857 // definition is in bitcode. Any other required libcall symbols will be added 858 // to the link after LTO when we add the LTO object file to the link. 859 if (!symtab->bitcodeFiles.empty()) 860 for (auto *s : lto::LTO::getRuntimeLibcallSymbols()) 861 handleLibcall(s); 862 if (errorCount()) 863 return; 864 865 // Do link-time optimization if given files are LLVM bitcode files. 866 // This compiles bitcode files into real object files. 867 symtab->addCombinedLTOObject(); 868 if (errorCount()) 869 return; 870 871 // Resolve any variant symbols that were created due to signature 872 // mismatchs. 873 symtab->handleSymbolVariants(); 874 if (errorCount()) 875 return; 876 877 // Apply symbol renames for -wrap. 878 if (!wrapped.empty()) 879 wrapSymbols(wrapped); 880 881 for (auto *arg : args.filtered(OPT_export)) { 882 Symbol *sym = symtab->find(arg->getValue()); 883 if (sym && sym->isDefined()) 884 sym->forceExport = true; 885 else if (!config->allowUndefined) 886 error(Twine("symbol exported via --export not found: ") + 887 arg->getValue()); 888 } 889 890 if (!config->relocatable) { 891 // Add synthetic dummies for weak undefined functions. Must happen 892 // after LTO otherwise functions may not yet have signatures. 893 symtab->handleWeakUndefines(); 894 } 895 896 if (entrySym) 897 entrySym->setHidden(false); 898 899 if (errorCount()) 900 return; 901 902 // Do size optimizations: garbage collection 903 markLive(); 904 905 // Write the result to the file. 906 writeResult(); 907 } 908 909 } // namespace wasm 910 } // namespace lld 911