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 "Driver.h" 11 #include "Config.h" 12 #include "Error.h" 13 #include "InputFiles.h" 14 #include "Memory.h" 15 #include "SymbolTable.h" 16 #include "Symbols.h" 17 #include "Writer.h" 18 #include "lld/Driver/Driver.h" 19 #include "llvm/ADT/Optional.h" 20 #include "llvm/ADT/StringSwitch.h" 21 #include "llvm/BinaryFormat/Magic.h" 22 #include "llvm/Object/ArchiveWriter.h" 23 #include "llvm/Object/COFFImportFile.h" 24 #include "llvm/Object/COFFModuleDefinition.h" 25 #include "llvm/Option/Arg.h" 26 #include "llvm/Option/ArgList.h" 27 #include "llvm/Option/Option.h" 28 #include "llvm/Support/Debug.h" 29 #include "llvm/Support/Path.h" 30 #include "llvm/Support/Process.h" 31 #include "llvm/Support/TarWriter.h" 32 #include "llvm/Support/TargetSelect.h" 33 #include "llvm/Support/raw_ostream.h" 34 #include "llvm/ToolDrivers/llvm-lib/LibDriver.h" 35 #include <algorithm> 36 #include <memory> 37 38 #include <future> 39 40 using namespace llvm; 41 using namespace llvm::object; 42 using namespace llvm::COFF; 43 using llvm::sys::Process; 44 45 namespace lld { 46 namespace coff { 47 48 Configuration *Config; 49 LinkerDriver *Driver; 50 51 BumpPtrAllocator BAlloc; 52 StringSaver Saver{BAlloc}; 53 std::vector<SpecificAllocBase *> SpecificAllocBase::Instances; 54 55 bool link(ArrayRef<const char *> Args, raw_ostream &Diag) { 56 ErrorCount = 0; 57 ErrorOS = &Diag; 58 59 Config = make<Configuration>(); 60 Config->Argv = {Args.begin(), Args.end()}; 61 Config->ColorDiagnostics = ErrorOS->has_colors(); 62 63 Symtab = make<SymbolTable>(); 64 65 Driver = make<LinkerDriver>(); 66 Driver->link(Args); 67 return !ErrorCount; 68 } 69 70 // Drop directory components and replace extension with ".exe" or ".dll". 71 static std::string getOutputPath(StringRef Path) { 72 auto P = Path.find_last_of("\\/"); 73 StringRef S = (P == StringRef::npos) ? Path : Path.substr(P + 1); 74 const char* E = Config->DLL ? ".dll" : ".exe"; 75 return (S.substr(0, S.rfind('.')) + E).str(); 76 } 77 78 // ErrorOr is not default constructible, so it cannot be used as the type 79 // parameter of a future. 80 // FIXME: We could open the file in createFutureForFile and avoid needing to 81 // return an error here, but for the moment that would cost us a file descriptor 82 // (a limited resource on Windows) for the duration that the future is pending. 83 typedef std::pair<std::unique_ptr<MemoryBuffer>, std::error_code> MBErrPair; 84 85 // Create a std::future that opens and maps a file using the best strategy for 86 // the host platform. 87 static std::future<MBErrPair> createFutureForFile(std::string Path) { 88 #if LLVM_ON_WIN32 89 // On Windows, file I/O is relatively slow so it is best to do this 90 // asynchronously. 91 auto Strategy = std::launch::async; 92 #else 93 auto Strategy = std::launch::deferred; 94 #endif 95 return std::async(Strategy, [=]() { 96 auto MBOrErr = MemoryBuffer::getFile(Path); 97 if (!MBOrErr) 98 return MBErrPair{nullptr, MBOrErr.getError()}; 99 return MBErrPair{std::move(*MBOrErr), std::error_code()}; 100 }); 101 } 102 103 MemoryBufferRef LinkerDriver::takeBuffer(std::unique_ptr<MemoryBuffer> MB) { 104 MemoryBufferRef MBRef = *MB; 105 make<std::unique_ptr<MemoryBuffer>>(std::move(MB)); // take ownership 106 107 if (Driver->Tar) 108 Driver->Tar->append(relativeToRoot(MBRef.getBufferIdentifier()), 109 MBRef.getBuffer()); 110 return MBRef; 111 } 112 113 void LinkerDriver::addBuffer(std::unique_ptr<MemoryBuffer> MB) { 114 MemoryBufferRef MBRef = takeBuffer(std::move(MB)); 115 116 // File type is detected by contents, not by file extension. 117 file_magic Magic = identify_magic(MBRef.getBuffer()); 118 if (Magic == file_magic::windows_resource) { 119 Resources.push_back(MBRef); 120 return; 121 } 122 123 FilePaths.push_back(MBRef.getBufferIdentifier()); 124 if (Magic == file_magic::archive) 125 return Symtab->addFile(make<ArchiveFile>(MBRef)); 126 if (Magic == file_magic::bitcode) 127 return Symtab->addFile(make<BitcodeFile>(MBRef)); 128 129 if (Magic == file_magic::coff_cl_gl_object) 130 error(MBRef.getBufferIdentifier() + ": is not a native COFF file. " 131 "Recompile without /GL"); 132 else 133 Symtab->addFile(make<ObjFile>(MBRef)); 134 } 135 136 void LinkerDriver::enqueuePath(StringRef Path) { 137 auto Future = 138 std::make_shared<std::future<MBErrPair>>(createFutureForFile(Path)); 139 std::string PathStr = Path; 140 enqueueTask([=]() { 141 auto MBOrErr = Future->get(); 142 if (MBOrErr.second) 143 error("could not open " + PathStr + ": " + MBOrErr.second.message()); 144 else 145 Driver->addBuffer(std::move(MBOrErr.first)); 146 }); 147 } 148 149 void LinkerDriver::addArchiveBuffer(MemoryBufferRef MB, StringRef SymName, 150 StringRef ParentName) { 151 file_magic Magic = identify_magic(MB.getBuffer()); 152 if (Magic == file_magic::coff_import_library) { 153 Symtab->addFile(make<ImportFile>(MB)); 154 return; 155 } 156 157 InputFile *Obj; 158 if (Magic == file_magic::coff_object) { 159 Obj = make<ObjFile>(MB); 160 } else if (Magic == file_magic::bitcode) { 161 Obj = make<BitcodeFile>(MB); 162 } else { 163 error("unknown file type: " + MB.getBufferIdentifier()); 164 return; 165 } 166 167 Obj->ParentName = ParentName; 168 Symtab->addFile(Obj); 169 log("Loaded " + toString(Obj) + " for " + SymName); 170 } 171 172 void LinkerDriver::enqueueArchiveMember(const Archive::Child &C, 173 StringRef SymName, 174 StringRef ParentName) { 175 if (!C.getParent()->isThin()) { 176 MemoryBufferRef MB = check( 177 C.getMemoryBufferRef(), 178 "could not get the buffer for the member defining symbol " + SymName); 179 enqueueTask([=]() { Driver->addArchiveBuffer(MB, SymName, ParentName); }); 180 return; 181 } 182 183 auto Future = std::make_shared<std::future<MBErrPair>>(createFutureForFile( 184 check(C.getFullName(), 185 "could not get the filename for the member defining symbol " + 186 SymName))); 187 enqueueTask([=]() { 188 auto MBOrErr = Future->get(); 189 if (MBOrErr.second) 190 fatal(MBOrErr.second, 191 "could not get the buffer for the member defining " + SymName); 192 Driver->addArchiveBuffer(takeBuffer(std::move(MBOrErr.first)), SymName, 193 ParentName); 194 }); 195 } 196 197 static bool isDecorated(StringRef Sym) { 198 return Sym.startswith("_") || Sym.startswith("@") || Sym.startswith("?"); 199 } 200 201 // Parses .drectve section contents and returns a list of files 202 // specified by /defaultlib. 203 void LinkerDriver::parseDirectives(StringRef S) { 204 ArgParser Parser; 205 opt::InputArgList Args = Parser.parse(S); 206 207 for (auto *Arg : Args) { 208 switch (Arg->getOption().getUnaliasedOption().getID()) { 209 case OPT_aligncomm: 210 parseAligncomm(Arg->getValue()); 211 break; 212 case OPT_alternatename: 213 parseAlternateName(Arg->getValue()); 214 break; 215 case OPT_defaultlib: 216 if (Optional<StringRef> Path = findLib(Arg->getValue())) 217 enqueuePath(*Path); 218 break; 219 case OPT_export: { 220 Export E = parseExport(Arg->getValue()); 221 E.Directives = true; 222 Config->Exports.push_back(E); 223 break; 224 } 225 case OPT_failifmismatch: 226 checkFailIfMismatch(Arg->getValue()); 227 break; 228 case OPT_incl: 229 addUndefined(Arg->getValue()); 230 break; 231 case OPT_merge: 232 parseMerge(Arg->getValue()); 233 break; 234 case OPT_nodefaultlib: 235 Config->NoDefaultLibs.insert(doFindLib(Arg->getValue())); 236 break; 237 case OPT_section: 238 parseSection(Arg->getValue()); 239 break; 240 case OPT_editandcontinue: 241 case OPT_fastfail: 242 case OPT_guardsym: 243 case OPT_throwingnew: 244 break; 245 default: 246 error(Arg->getSpelling() + " is not allowed in .drectve"); 247 } 248 } 249 } 250 251 // Find file from search paths. You can omit ".obj", this function takes 252 // care of that. Note that the returned path is not guaranteed to exist. 253 StringRef LinkerDriver::doFindFile(StringRef Filename) { 254 bool HasPathSep = (Filename.find_first_of("/\\") != StringRef::npos); 255 if (HasPathSep) 256 return Filename; 257 bool HasExt = Filename.contains('.'); 258 for (StringRef Dir : SearchPaths) { 259 SmallString<128> Path = Dir; 260 sys::path::append(Path, Filename); 261 if (sys::fs::exists(Path.str())) 262 return Saver.save(Path.str()); 263 if (!HasExt) { 264 Path.append(".obj"); 265 if (sys::fs::exists(Path.str())) 266 return Saver.save(Path.str()); 267 } 268 } 269 return Filename; 270 } 271 272 // Resolves a file path. This never returns the same path 273 // (in that case, it returns None). 274 Optional<StringRef> LinkerDriver::findFile(StringRef Filename) { 275 StringRef Path = doFindFile(Filename); 276 bool Seen = !VisitedFiles.insert(Path.lower()).second; 277 if (Seen) 278 return None; 279 return Path; 280 } 281 282 // Find library file from search path. 283 StringRef LinkerDriver::doFindLib(StringRef Filename) { 284 // Add ".lib" to Filename if that has no file extension. 285 bool HasExt = Filename.contains('.'); 286 if (!HasExt) 287 Filename = Saver.save(Filename + ".lib"); 288 return doFindFile(Filename); 289 } 290 291 // Resolves a library path. /nodefaultlib options are taken into 292 // consideration. This never returns the same path (in that case, 293 // it returns None). 294 Optional<StringRef> LinkerDriver::findLib(StringRef Filename) { 295 if (Config->NoDefaultLibAll) 296 return None; 297 if (!VisitedLibs.insert(Filename.lower()).second) 298 return None; 299 StringRef Path = doFindLib(Filename); 300 if (Config->NoDefaultLibs.count(Path)) 301 return None; 302 if (!VisitedFiles.insert(Path.lower()).second) 303 return None; 304 return Path; 305 } 306 307 // Parses LIB environment which contains a list of search paths. 308 void LinkerDriver::addLibSearchPaths() { 309 Optional<std::string> EnvOpt = Process::GetEnv("LIB"); 310 if (!EnvOpt.hasValue()) 311 return; 312 StringRef Env = Saver.save(*EnvOpt); 313 while (!Env.empty()) { 314 StringRef Path; 315 std::tie(Path, Env) = Env.split(';'); 316 SearchPaths.push_back(Path); 317 } 318 } 319 320 SymbolBody *LinkerDriver::addUndefined(StringRef Name) { 321 SymbolBody *B = Symtab->addUndefined(Name); 322 Config->GCRoot.insert(B); 323 return B; 324 } 325 326 // Symbol names are mangled by appending "_" prefix on x86. 327 StringRef LinkerDriver::mangle(StringRef Sym) { 328 assert(Config->Machine != IMAGE_FILE_MACHINE_UNKNOWN); 329 if (Config->Machine == I386) 330 return Saver.save("_" + Sym); 331 return Sym; 332 } 333 334 // Windows specific -- find default entry point name. 335 StringRef LinkerDriver::findDefaultEntry() { 336 // User-defined main functions and their corresponding entry points. 337 static const char *Entries[][2] = { 338 {"main", "mainCRTStartup"}, 339 {"wmain", "wmainCRTStartup"}, 340 {"WinMain", "WinMainCRTStartup"}, 341 {"wWinMain", "wWinMainCRTStartup"}, 342 }; 343 for (auto E : Entries) { 344 StringRef Entry = Symtab->findMangle(mangle(E[0])); 345 if (!Entry.empty() && !isa<Undefined>(Symtab->find(Entry)->body())) 346 return mangle(E[1]); 347 } 348 return ""; 349 } 350 351 WindowsSubsystem LinkerDriver::inferSubsystem() { 352 if (Config->DLL) 353 return IMAGE_SUBSYSTEM_WINDOWS_GUI; 354 if (Symtab->findUnderscore("main") || Symtab->findUnderscore("wmain")) 355 return IMAGE_SUBSYSTEM_WINDOWS_CUI; 356 if (Symtab->findUnderscore("WinMain") || Symtab->findUnderscore("wWinMain")) 357 return IMAGE_SUBSYSTEM_WINDOWS_GUI; 358 return IMAGE_SUBSYSTEM_UNKNOWN; 359 } 360 361 static uint64_t getDefaultImageBase() { 362 if (Config->is64()) 363 return Config->DLL ? 0x180000000 : 0x140000000; 364 return Config->DLL ? 0x10000000 : 0x400000; 365 } 366 367 static std::string createResponseFile(const opt::InputArgList &Args, 368 ArrayRef<StringRef> FilePaths, 369 ArrayRef<StringRef> SearchPaths) { 370 SmallString<0> Data; 371 raw_svector_ostream OS(Data); 372 373 for (auto *Arg : Args) { 374 switch (Arg->getOption().getID()) { 375 case OPT_linkrepro: 376 case OPT_INPUT: 377 case OPT_defaultlib: 378 case OPT_libpath: 379 break; 380 default: 381 OS << toString(Arg) << "\n"; 382 } 383 } 384 385 for (StringRef Path : SearchPaths) { 386 std::string RelPath = relativeToRoot(Path); 387 OS << "/libpath:" << quote(RelPath) << "\n"; 388 } 389 390 for (StringRef Path : FilePaths) 391 OS << quote(relativeToRoot(Path)) << "\n"; 392 393 return Data.str(); 394 } 395 396 static unsigned getDefaultDebugType(const opt::InputArgList &Args) { 397 unsigned DebugTypes = static_cast<unsigned>(DebugType::CV); 398 if (Args.hasArg(OPT_driver)) 399 DebugTypes |= static_cast<unsigned>(DebugType::PData); 400 if (Args.hasArg(OPT_profile)) 401 DebugTypes |= static_cast<unsigned>(DebugType::Fixup); 402 return DebugTypes; 403 } 404 405 static unsigned parseDebugType(StringRef Arg) { 406 SmallVector<StringRef, 3> Types; 407 Arg.split(Types, ',', /*KeepEmpty=*/false); 408 409 unsigned DebugTypes = static_cast<unsigned>(DebugType::None); 410 for (StringRef Type : Types) 411 DebugTypes |= StringSwitch<unsigned>(Type.lower()) 412 .Case("cv", static_cast<unsigned>(DebugType::CV)) 413 .Case("pdata", static_cast<unsigned>(DebugType::PData)) 414 .Case("fixup", static_cast<unsigned>(DebugType::Fixup)) 415 .Default(0); 416 return DebugTypes; 417 } 418 419 static std::string getMapFile(const opt::InputArgList &Args) { 420 auto *Arg = Args.getLastArg(OPT_lldmap, OPT_lldmap_file); 421 if (!Arg) 422 return ""; 423 if (Arg->getOption().getID() == OPT_lldmap_file) 424 return Arg->getValue(); 425 426 assert(Arg->getOption().getID() == OPT_lldmap); 427 StringRef OutFile = Config->OutputFile; 428 return (OutFile.substr(0, OutFile.rfind('.')) + ".map").str(); 429 } 430 431 static std::string getImplibPath() { 432 if (!Config->Implib.empty()) 433 return Config->Implib; 434 SmallString<128> Out = StringRef(Config->OutputFile); 435 sys::path::replace_extension(Out, ".lib"); 436 return Out.str(); 437 } 438 439 // 440 // The import name is caculated as the following: 441 // 442 // | LIBRARY w/ ext | LIBRARY w/o ext | no LIBRARY 443 // -----+----------------+---------------------+------------------ 444 // LINK | {value} | {value}.{.dll/.exe} | {output name} 445 // LIB | {value} | {value}.dll | {output name}.dll 446 // 447 static std::string getImportName(bool AsLib) { 448 SmallString<128> Out; 449 450 if (Config->ImportName.empty()) { 451 Out.assign(sys::path::filename(Config->OutputFile)); 452 if (AsLib) 453 sys::path::replace_extension(Out, ".dll"); 454 } else { 455 Out.assign(Config->ImportName); 456 if (!sys::path::has_extension(Out)) 457 sys::path::replace_extension(Out, 458 (Config->DLL || AsLib) ? ".dll" : ".exe"); 459 } 460 461 return Out.str(); 462 } 463 464 static void createImportLibrary(bool AsLib) { 465 std::vector<COFFShortExport> Exports; 466 for (Export &E1 : Config->Exports) { 467 COFFShortExport E2; 468 E2.Name = E1.Name; 469 E2.SymbolName = E1.SymbolName; 470 E2.ExtName = E1.ExtName; 471 E2.Ordinal = E1.Ordinal; 472 E2.Noname = E1.Noname; 473 E2.Data = E1.Data; 474 E2.Private = E1.Private; 475 E2.Constant = E1.Constant; 476 Exports.push_back(E2); 477 } 478 479 writeImportLibrary(getImportName(AsLib), getImplibPath(), Exports, 480 Config->Machine, false); 481 } 482 483 static void parseModuleDefs(StringRef Path) { 484 std::unique_ptr<MemoryBuffer> MB = check( 485 MemoryBuffer::getFile(Path, -1, false, true), "could not open " + Path); 486 COFFModuleDefinition M = 487 check(parseCOFFModuleDefinition(MB->getMemBufferRef(), Config->Machine)); 488 489 if (Config->OutputFile.empty()) 490 Config->OutputFile = Saver.save(M.OutputFile); 491 Config->ImportName = Saver.save(M.ImportName); 492 if (M.ImageBase) 493 Config->ImageBase = M.ImageBase; 494 if (M.StackReserve) 495 Config->StackReserve = M.StackReserve; 496 if (M.StackCommit) 497 Config->StackCommit = M.StackCommit; 498 if (M.HeapReserve) 499 Config->HeapReserve = M.HeapReserve; 500 if (M.HeapCommit) 501 Config->HeapCommit = M.HeapCommit; 502 if (M.MajorImageVersion) 503 Config->MajorImageVersion = M.MajorImageVersion; 504 if (M.MinorImageVersion) 505 Config->MinorImageVersion = M.MinorImageVersion; 506 if (M.MajorOSVersion) 507 Config->MajorOSVersion = M.MajorOSVersion; 508 if (M.MinorOSVersion) 509 Config->MinorOSVersion = M.MinorOSVersion; 510 511 for (COFFShortExport E1 : M.Exports) { 512 Export E2; 513 E2.Name = Saver.save(E1.Name); 514 if (E1.isWeak()) 515 E2.ExtName = Saver.save(E1.ExtName); 516 E2.Ordinal = E1.Ordinal; 517 E2.Noname = E1.Noname; 518 E2.Data = E1.Data; 519 E2.Private = E1.Private; 520 E2.Constant = E1.Constant; 521 Config->Exports.push_back(E2); 522 } 523 } 524 525 // A helper function for filterBitcodeFiles. 526 static bool needsRebuilding(MemoryBufferRef MB) { 527 // The MSVC linker doesn't support thin archives, so if it's a thin 528 // archive, we always need to rebuild it. 529 std::unique_ptr<Archive> File = 530 check(Archive::create(MB), "Failed to read " + MB.getBufferIdentifier()); 531 if (File->isThin()) 532 return true; 533 534 // Returns true if the archive contains at least one bitcode file. 535 for (MemoryBufferRef Member : getArchiveMembers(File.get())) 536 if (identify_magic(Member.getBuffer()) == file_magic::bitcode) 537 return true; 538 return false; 539 } 540 541 // Opens a given path as an archive file and removes bitcode files 542 // from them if exists. This function is to appease the MSVC linker as 543 // their linker doesn't like archive files containing non-native 544 // object files. 545 // 546 // If a given archive doesn't contain bitcode files, the archive path 547 // is returned as-is. Otherwise, a new temporary file is created and 548 // its path is returned. 549 static Optional<std::string> 550 filterBitcodeFiles(StringRef Path, std::vector<std::string> &TemporaryFiles) { 551 std::unique_ptr<MemoryBuffer> MB = check( 552 MemoryBuffer::getFile(Path, -1, false, true), "could not open " + Path); 553 MemoryBufferRef MBRef = MB->getMemBufferRef(); 554 file_magic Magic = identify_magic(MBRef.getBuffer()); 555 556 if (Magic == file_magic::bitcode) 557 return None; 558 if (Magic != file_magic::archive) 559 return Path.str(); 560 if (!needsRebuilding(MBRef)) 561 return Path.str(); 562 563 std::unique_ptr<Archive> File = 564 check(Archive::create(MBRef), 565 MBRef.getBufferIdentifier() + ": failed to parse archive"); 566 567 std::vector<NewArchiveMember> New; 568 for (MemoryBufferRef Member : getArchiveMembers(File.get())) 569 if (identify_magic(Member.getBuffer()) != file_magic::bitcode) 570 New.emplace_back(Member); 571 572 if (New.empty()) 573 return None; 574 575 log("Creating a temporary archive for " + Path + " to remove bitcode files"); 576 577 SmallString<128> S; 578 if (auto EC = sys::fs::createTemporaryFile("lld-" + sys::path::stem(Path), 579 ".lib", S)) 580 fatal(EC, "cannot create a temporary file"); 581 std::string Temp = S.str(); 582 TemporaryFiles.push_back(Temp); 583 584 std::error_code EC = 585 llvm::writeArchive(Temp, New, /*WriteSymtab=*/true, Archive::Kind::K_GNU, 586 /*Deterministics=*/true, 587 /*Thin=*/false); 588 if (EC) 589 error("failed to create a new archive " + S.str() + ": " + EC.message()); 590 return Temp; 591 } 592 593 // Create response file contents and invoke the MSVC linker. 594 void LinkerDriver::invokeMSVC(opt::InputArgList &Args) { 595 std::string Rsp = "/nologo\n"; 596 std::vector<std::string> Temps; 597 598 // Write out archive members that we used in symbol resolution and pass these 599 // to MSVC before any archives, so that MSVC uses the same objects to satisfy 600 // references. 601 for (ObjFile *Obj : ObjFile::Instances) { 602 if (Obj->ParentName.empty()) 603 continue; 604 SmallString<128> S; 605 int Fd; 606 if (auto EC = sys::fs::createTemporaryFile( 607 "lld-" + sys::path::filename(Obj->ParentName), ".obj", Fd, S)) 608 fatal(EC, "cannot create a temporary file"); 609 raw_fd_ostream OS(Fd, /*shouldClose*/ true); 610 OS << Obj->MB.getBuffer(); 611 Temps.push_back(S.str()); 612 Rsp += quote(S) + "\n"; 613 } 614 615 for (auto *Arg : Args) { 616 switch (Arg->getOption().getID()) { 617 case OPT_linkrepro: 618 case OPT_lldmap: 619 case OPT_lldmap_file: 620 case OPT_lldsavetemps: 621 case OPT_msvclto: 622 // LLD-specific options are stripped. 623 break; 624 case OPT_opt: 625 if (!StringRef(Arg->getValue()).startswith("lld")) 626 Rsp += toString(Arg) + " "; 627 break; 628 case OPT_INPUT: { 629 if (Optional<StringRef> Path = doFindFile(Arg->getValue())) { 630 if (Optional<std::string> S = filterBitcodeFiles(*Path, Temps)) 631 Rsp += quote(*S) + "\n"; 632 continue; 633 } 634 Rsp += quote(Arg->getValue()) + "\n"; 635 break; 636 } 637 default: 638 Rsp += toString(Arg) + "\n"; 639 } 640 } 641 642 std::vector<StringRef> ObjFiles = Symtab->compileBitcodeFiles(); 643 runMSVCLinker(Rsp, ObjFiles); 644 645 for (StringRef Path : Temps) 646 sys::fs::remove(Path); 647 } 648 649 void LinkerDriver::enqueueTask(std::function<void()> Task) { 650 TaskQueue.push_back(std::move(Task)); 651 } 652 653 bool LinkerDriver::run() { 654 bool DidWork = !TaskQueue.empty(); 655 while (!TaskQueue.empty()) { 656 TaskQueue.front()(); 657 TaskQueue.pop_front(); 658 } 659 return DidWork; 660 } 661 662 void LinkerDriver::link(ArrayRef<const char *> ArgsArr) { 663 // If the first command line argument is "/lib", link.exe acts like lib.exe. 664 // We call our own implementation of lib.exe that understands bitcode files. 665 if (ArgsArr.size() > 1 && StringRef(ArgsArr[1]).equals_lower("/lib")) { 666 if (llvm::libDriverMain(ArgsArr.slice(1)) != 0) 667 fatal("lib failed"); 668 return; 669 } 670 671 // Needed for LTO. 672 InitializeAllTargetInfos(); 673 InitializeAllTargets(); 674 InitializeAllTargetMCs(); 675 InitializeAllAsmParsers(); 676 InitializeAllAsmPrinters(); 677 InitializeAllDisassemblers(); 678 679 // Parse command line options. 680 ArgParser Parser; 681 opt::InputArgList Args = Parser.parseLINK(ArgsArr.slice(1)); 682 683 // Parse and evaluate -mllvm options. 684 std::vector<const char *> V; 685 V.push_back("lld-link (LLVM option parsing)"); 686 for (auto *Arg : Args.filtered(OPT_mllvm)) 687 V.push_back(Arg->getValue()); 688 cl::ParseCommandLineOptions(V.size(), V.data()); 689 690 // Handle /errorlimit early, because error() depends on it. 691 if (auto *Arg = Args.getLastArg(OPT_errorlimit)) { 692 int N = 20; 693 StringRef S = Arg->getValue(); 694 if (S.getAsInteger(10, N)) 695 error(Arg->getSpelling() + " number expected, but got " + S); 696 Config->ErrorLimit = N; 697 } 698 699 // Handle /help 700 if (Args.hasArg(OPT_help)) { 701 printHelp(ArgsArr[0]); 702 return; 703 } 704 705 if (auto *Arg = Args.getLastArg(OPT_linkrepro)) { 706 SmallString<64> Path = StringRef(Arg->getValue()); 707 sys::path::append(Path, "repro.tar"); 708 709 Expected<std::unique_ptr<TarWriter>> ErrOrWriter = 710 TarWriter::create(Path, "repro"); 711 712 if (ErrOrWriter) { 713 Tar = std::move(*ErrOrWriter); 714 } else { 715 error("/linkrepro: failed to open " + Path + ": " + 716 toString(ErrOrWriter.takeError())); 717 } 718 } 719 720 if (!Args.hasArgNoClaim(OPT_INPUT)) { 721 if (Args.hasArgNoClaim(OPT_deffile)) 722 Config->NoEntry = true; 723 else 724 fatal("no input files"); 725 } 726 727 // Construct search path list. 728 SearchPaths.push_back(""); 729 for (auto *Arg : Args.filtered(OPT_libpath)) 730 SearchPaths.push_back(Arg->getValue()); 731 addLibSearchPaths(); 732 733 // Handle /out 734 if (auto *Arg = Args.getLastArg(OPT_out)) 735 Config->OutputFile = Arg->getValue(); 736 737 // Handle /verbose 738 if (Args.hasArg(OPT_verbose)) 739 Config->Verbose = true; 740 741 // Handle /force or /force:unresolved 742 if (Args.hasArg(OPT_force) || Args.hasArg(OPT_force_unresolved)) 743 Config->Force = true; 744 745 // Handle /debug 746 if (Args.hasArg(OPT_debug)) { 747 Config->Debug = true; 748 if (auto *Arg = Args.getLastArg(OPT_debugtype)) 749 Config->DebugTypes = parseDebugType(Arg->getValue()); 750 else 751 Config->DebugTypes = getDefaultDebugType(Args); 752 } 753 754 // Create a dummy PDB file to satisfy build sytem rules. 755 if (auto *Arg = Args.getLastArg(OPT_pdb)) 756 Config->PDBPath = Arg->getValue(); 757 758 // Handle /noentry 759 if (Args.hasArg(OPT_noentry)) { 760 if (Args.hasArg(OPT_dll)) 761 Config->NoEntry = true; 762 else 763 error("/noentry must be specified with /dll"); 764 } 765 766 // Handle /dll 767 if (Args.hasArg(OPT_dll)) { 768 Config->DLL = true; 769 Config->ManifestID = 2; 770 } 771 772 // Handle /fixed 773 if (Args.hasArg(OPT_fixed)) { 774 if (Args.hasArg(OPT_dynamicbase)) { 775 error("/fixed must not be specified with /dynamicbase"); 776 } else { 777 Config->Relocatable = false; 778 Config->DynamicBase = false; 779 } 780 } 781 782 if (Args.hasArg(OPT_appcontainer)) 783 Config->AppContainer = true; 784 785 // Handle /machine 786 if (auto *Arg = Args.getLastArg(OPT_machine)) 787 Config->Machine = getMachineType(Arg->getValue()); 788 789 // Handle /nodefaultlib:<filename> 790 for (auto *Arg : Args.filtered(OPT_nodefaultlib)) 791 Config->NoDefaultLibs.insert(doFindLib(Arg->getValue())); 792 793 // Handle /nodefaultlib 794 if (Args.hasArg(OPT_nodefaultlib_all)) 795 Config->NoDefaultLibAll = true; 796 797 // Handle /base 798 if (auto *Arg = Args.getLastArg(OPT_base)) 799 parseNumbers(Arg->getValue(), &Config->ImageBase); 800 801 // Handle /stack 802 if (auto *Arg = Args.getLastArg(OPT_stack)) 803 parseNumbers(Arg->getValue(), &Config->StackReserve, &Config->StackCommit); 804 805 // Handle /heap 806 if (auto *Arg = Args.getLastArg(OPT_heap)) 807 parseNumbers(Arg->getValue(), &Config->HeapReserve, &Config->HeapCommit); 808 809 // Handle /version 810 if (auto *Arg = Args.getLastArg(OPT_version)) 811 parseVersion(Arg->getValue(), &Config->MajorImageVersion, 812 &Config->MinorImageVersion); 813 814 // Handle /subsystem 815 if (auto *Arg = Args.getLastArg(OPT_subsystem)) 816 parseSubsystem(Arg->getValue(), &Config->Subsystem, &Config->MajorOSVersion, 817 &Config->MinorOSVersion); 818 819 // Handle /alternatename 820 for (auto *Arg : Args.filtered(OPT_alternatename)) 821 parseAlternateName(Arg->getValue()); 822 823 // Handle /include 824 for (auto *Arg : Args.filtered(OPT_incl)) 825 addUndefined(Arg->getValue()); 826 827 // Handle /implib 828 if (auto *Arg = Args.getLastArg(OPT_implib)) 829 Config->Implib = Arg->getValue(); 830 831 // Handle /opt 832 for (auto *Arg : Args.filtered(OPT_opt)) { 833 std::string Str = StringRef(Arg->getValue()).lower(); 834 SmallVector<StringRef, 1> Vec; 835 StringRef(Str).split(Vec, ','); 836 for (StringRef S : Vec) { 837 if (S == "noref") { 838 Config->DoGC = false; 839 Config->DoICF = false; 840 continue; 841 } 842 if (S == "icf" || StringRef(S).startswith("icf=")) { 843 Config->DoICF = true; 844 continue; 845 } 846 if (S == "noicf") { 847 Config->DoICF = false; 848 continue; 849 } 850 if (StringRef(S).startswith("lldlto=")) { 851 StringRef OptLevel = StringRef(S).substr(7); 852 if (OptLevel.getAsInteger(10, Config->LTOOptLevel) || 853 Config->LTOOptLevel > 3) 854 error("/opt:lldlto: invalid optimization level: " + OptLevel); 855 continue; 856 } 857 if (StringRef(S).startswith("lldltojobs=")) { 858 StringRef Jobs = StringRef(S).substr(11); 859 if (Jobs.getAsInteger(10, Config->LTOJobs) || Config->LTOJobs == 0) 860 error("/opt:lldltojobs: invalid job count: " + Jobs); 861 continue; 862 } 863 if (StringRef(S).startswith("lldltopartitions=")) { 864 StringRef N = StringRef(S).substr(17); 865 if (N.getAsInteger(10, Config->LTOPartitions) || 866 Config->LTOPartitions == 0) 867 error("/opt:lldltopartitions: invalid partition count: " + N); 868 continue; 869 } 870 if (S != "ref" && S != "lbr" && S != "nolbr") 871 error("/opt: unknown option: " + S); 872 } 873 } 874 875 // Handle /lldsavetemps 876 if (Args.hasArg(OPT_lldsavetemps)) 877 Config->SaveTemps = true; 878 879 // Handle /failifmismatch 880 for (auto *Arg : Args.filtered(OPT_failifmismatch)) 881 checkFailIfMismatch(Arg->getValue()); 882 883 // Handle /merge 884 for (auto *Arg : Args.filtered(OPT_merge)) 885 parseMerge(Arg->getValue()); 886 887 // Handle /section 888 for (auto *Arg : Args.filtered(OPT_section)) 889 parseSection(Arg->getValue()); 890 891 // Handle /aligncomm 892 for (auto *Arg : Args.filtered(OPT_aligncomm)) 893 parseAligncomm(Arg->getValue()); 894 895 // Handle /manifestdependency. This enables /manifest unless /manifest:no is 896 // also passed. 897 if (auto *Arg = Args.getLastArg(OPT_manifestdependency)) { 898 Config->ManifestDependency = Arg->getValue(); 899 Config->Manifest = Configuration::SideBySide; 900 } 901 902 // Handle /manifest and /manifest: 903 if (auto *Arg = Args.getLastArg(OPT_manifest, OPT_manifest_colon)) { 904 if (Arg->getOption().getID() == OPT_manifest) 905 Config->Manifest = Configuration::SideBySide; 906 else 907 parseManifest(Arg->getValue()); 908 } 909 910 // Handle /manifestuac 911 if (auto *Arg = Args.getLastArg(OPT_manifestuac)) 912 parseManifestUAC(Arg->getValue()); 913 914 // Handle /manifestfile 915 if (auto *Arg = Args.getLastArg(OPT_manifestfile)) 916 Config->ManifestFile = Arg->getValue(); 917 918 // Handle /manifestinput 919 for (auto *Arg : Args.filtered(OPT_manifestinput)) 920 Config->ManifestInput.push_back(Arg->getValue()); 921 922 if (!Config->ManifestInput.empty() && 923 Config->Manifest != Configuration::Embed) { 924 fatal("/MANIFESTINPUT: requires /MANIFEST:EMBED"); 925 } 926 927 // Handle miscellaneous boolean flags. 928 if (Args.hasArg(OPT_allowisolation_no)) 929 Config->AllowIsolation = false; 930 if (Args.hasArg(OPT_dynamicbase_no)) 931 Config->DynamicBase = false; 932 if (Args.hasArg(OPT_nxcompat_no)) 933 Config->NxCompat = false; 934 if (Args.hasArg(OPT_tsaware_no)) 935 Config->TerminalServerAware = false; 936 if (Args.hasArg(OPT_nosymtab)) 937 Config->WriteSymtab = false; 938 939 Config->MapFile = getMapFile(Args); 940 941 if (ErrorCount) 942 return; 943 944 // Create a list of input files. Files can be given as arguments 945 // for /defaultlib option. 946 std::vector<MemoryBufferRef> MBs; 947 for (auto *Arg : Args.filtered(OPT_INPUT)) 948 if (Optional<StringRef> Path = findFile(Arg->getValue())) 949 enqueuePath(*Path); 950 for (auto *Arg : Args.filtered(OPT_defaultlib)) 951 if (Optional<StringRef> Path = findLib(Arg->getValue())) 952 enqueuePath(*Path); 953 954 // Windows specific -- Create a resource file containing a manifest file. 955 if (Config->Manifest == Configuration::Embed) 956 addBuffer(createManifestRes()); 957 958 // Read all input files given via the command line. 959 run(); 960 961 // We should have inferred a machine type by now from the input files, but if 962 // not we assume x64. 963 if (Config->Machine == IMAGE_FILE_MACHINE_UNKNOWN) { 964 warn("/machine is not specified. x64 is assumed"); 965 Config->Machine = AMD64; 966 } 967 968 // Input files can be Windows resource files (.res files). We use 969 // WindowsResource to convert resource files to a regular COFF file, 970 // then link the resulting file normally. 971 if (!Resources.empty()) 972 addBuffer(convertResToCOFF(Resources)); 973 974 if (Tar) 975 Tar->append("response.txt", 976 createResponseFile(Args, FilePaths, 977 ArrayRef<StringRef>(SearchPaths).slice(1))); 978 979 // Handle /largeaddressaware 980 if (Config->is64() || Args.hasArg(OPT_largeaddressaware)) 981 Config->LargeAddressAware = true; 982 983 // Handle /highentropyva 984 if (Config->is64() && !Args.hasArg(OPT_highentropyva_no)) 985 Config->HighEntropyVA = true; 986 987 // Handle /entry and /dll 988 if (auto *Arg = Args.getLastArg(OPT_entry)) { 989 Config->Entry = addUndefined(mangle(Arg->getValue())); 990 } else if (Args.hasArg(OPT_dll) && !Config->NoEntry) { 991 StringRef S = (Config->Machine == I386) ? "__DllMainCRTStartup@12" 992 : "_DllMainCRTStartup"; 993 Config->Entry = addUndefined(S); 994 } else if (!Config->NoEntry) { 995 // Windows specific -- If entry point name is not given, we need to 996 // infer that from user-defined entry name. 997 StringRef S = findDefaultEntry(); 998 if (S.empty()) 999 fatal("entry point must be defined"); 1000 Config->Entry = addUndefined(S); 1001 log("Entry name inferred: " + S); 1002 } 1003 1004 // Handle /export 1005 for (auto *Arg : Args.filtered(OPT_export)) { 1006 Export E = parseExport(Arg->getValue()); 1007 if (Config->Machine == I386) { 1008 if (!isDecorated(E.Name)) 1009 E.Name = Saver.save("_" + E.Name); 1010 if (!E.ExtName.empty() && !isDecorated(E.ExtName)) 1011 E.ExtName = Saver.save("_" + E.ExtName); 1012 } 1013 Config->Exports.push_back(E); 1014 } 1015 1016 // Handle /def 1017 if (auto *Arg = Args.getLastArg(OPT_deffile)) { 1018 // parseModuleDefs mutates Config object. 1019 parseModuleDefs(Arg->getValue()); 1020 } 1021 1022 // Handle generation of import library from a def file. 1023 if (!Args.hasArgNoClaim(OPT_INPUT)) { 1024 fixupExports(); 1025 createImportLibrary(/*AsLib=*/true); 1026 exit(0); 1027 } 1028 1029 // Handle /delayload 1030 for (auto *Arg : Args.filtered(OPT_delayload)) { 1031 Config->DelayLoads.insert(StringRef(Arg->getValue()).lower()); 1032 if (Config->Machine == I386) { 1033 Config->DelayLoadHelper = addUndefined("___delayLoadHelper2@8"); 1034 } else { 1035 Config->DelayLoadHelper = addUndefined("__delayLoadHelper2"); 1036 } 1037 } 1038 1039 // Set default image name if neither /out or /def set it. 1040 if (Config->OutputFile.empty()) { 1041 Config->OutputFile = 1042 getOutputPath((*Args.filtered(OPT_INPUT).begin())->getValue()); 1043 } 1044 1045 // Put the PDB next to the image if no /pdb flag was passed. 1046 if (Config->Debug && Config->PDBPath.empty()) { 1047 Config->PDBPath = Config->OutputFile; 1048 sys::path::replace_extension(Config->PDBPath, ".pdb"); 1049 } 1050 1051 // Disable PDB generation if the user requested it. 1052 if (Args.hasArg(OPT_nopdb)) 1053 Config->PDBPath = ""; 1054 1055 // Set default image base if /base is not given. 1056 if (Config->ImageBase == uint64_t(-1)) 1057 Config->ImageBase = getDefaultImageBase(); 1058 1059 Symtab->addSynthetic(mangle("__ImageBase"), nullptr); 1060 if (Config->Machine == I386) { 1061 Symtab->addAbsolute("___safe_se_handler_table", 0); 1062 Symtab->addAbsolute("___safe_se_handler_count", 0); 1063 } 1064 1065 // We do not support /guard:cf (control flow protection) yet. 1066 // Define CFG symbols anyway so that we can link MSVC 2015 CRT. 1067 Symtab->addAbsolute(mangle("__guard_fids_count"), 0); 1068 Symtab->addAbsolute(mangle("__guard_fids_table"), 0); 1069 Symtab->addAbsolute(mangle("__guard_flags"), 0x100); 1070 Symtab->addAbsolute(mangle("__guard_iat_count"), 0); 1071 Symtab->addAbsolute(mangle("__guard_iat_table"), 0); 1072 Symtab->addAbsolute(mangle("__guard_longjmp_count"), 0); 1073 Symtab->addAbsolute(mangle("__guard_longjmp_table"), 0); 1074 1075 // This code may add new undefined symbols to the link, which may enqueue more 1076 // symbol resolution tasks, so we need to continue executing tasks until we 1077 // converge. 1078 do { 1079 // Windows specific -- if entry point is not found, 1080 // search for its mangled names. 1081 if (Config->Entry) 1082 Symtab->mangleMaybe(Config->Entry); 1083 1084 // Windows specific -- Make sure we resolve all dllexported symbols. 1085 for (Export &E : Config->Exports) { 1086 if (!E.ForwardTo.empty()) 1087 continue; 1088 E.Sym = addUndefined(E.Name); 1089 if (!E.Directives) 1090 Symtab->mangleMaybe(E.Sym); 1091 } 1092 1093 // Add weak aliases. Weak aliases is a mechanism to give remaining 1094 // undefined symbols final chance to be resolved successfully. 1095 for (auto Pair : Config->AlternateNames) { 1096 StringRef From = Pair.first; 1097 StringRef To = Pair.second; 1098 Symbol *Sym = Symtab->find(From); 1099 if (!Sym) 1100 continue; 1101 if (auto *U = dyn_cast<Undefined>(Sym->body())) 1102 if (!U->WeakAlias) 1103 U->WeakAlias = Symtab->addUndefined(To); 1104 } 1105 1106 // Windows specific -- if __load_config_used can be resolved, resolve it. 1107 if (Symtab->findUnderscore("_load_config_used")) 1108 addUndefined(mangle("_load_config_used")); 1109 } while (run()); 1110 1111 if (ErrorCount) 1112 return; 1113 1114 // If /msvclto is given, we use the MSVC linker to link LTO output files. 1115 // This is useful because MSVC link.exe can generate complete PDBs. 1116 if (Args.hasArg(OPT_msvclto)) { 1117 invokeMSVC(Args); 1118 exit(0); 1119 } 1120 1121 // Do LTO by compiling bitcode input files to a set of native COFF files then 1122 // link those files. 1123 Symtab->addCombinedLTOObjects(); 1124 run(); 1125 1126 // Make sure we have resolved all symbols. 1127 Symtab->reportRemainingUndefines(); 1128 1129 // Windows specific -- if no /subsystem is given, we need to infer 1130 // that from entry point name. 1131 if (Config->Subsystem == IMAGE_SUBSYSTEM_UNKNOWN) { 1132 Config->Subsystem = inferSubsystem(); 1133 if (Config->Subsystem == IMAGE_SUBSYSTEM_UNKNOWN) 1134 fatal("subsystem must be defined"); 1135 } 1136 1137 // Handle /safeseh. 1138 if (Args.hasArg(OPT_safeseh)) { 1139 for (ObjFile *File : ObjFile::Instances) 1140 if (!File->SEHCompat) 1141 error("/safeseh: " + File->getName() + " is not compatible with SEH"); 1142 if (ErrorCount) 1143 return; 1144 } 1145 1146 // Windows specific -- when we are creating a .dll file, we also 1147 // need to create a .lib file. 1148 if (!Config->Exports.empty() || Config->DLL) { 1149 fixupExports(); 1150 createImportLibrary(/*AsLib=*/false); 1151 assignExportOrdinals(); 1152 } 1153 1154 // Set extra alignment for .comm symbols 1155 for (auto Pair : Config->AlignComm) { 1156 StringRef Name = Pair.first; 1157 int Align = Pair.second; 1158 Symbol *Sym = Symtab->find(Name); 1159 if (!Sym) { 1160 warn("/aligncomm symbol " + Name + " not found"); 1161 continue; 1162 } 1163 auto *DC = dyn_cast<DefinedCommon>(Sym->body()); 1164 if (!DC) { 1165 warn("/aligncomm symbol " + Name + " of wrong kind"); 1166 continue; 1167 } 1168 DC->getChunk()->setAlign(Align); 1169 } 1170 1171 // Windows specific -- Create a side-by-side manifest file. 1172 if (Config->Manifest == Configuration::SideBySide) 1173 createSideBySideManifest(); 1174 1175 // Identify unreferenced COMDAT sections. 1176 if (Config->DoGC) 1177 markLive(Symtab->getChunks()); 1178 1179 // Identify identical COMDAT sections to merge them. 1180 if (Config->DoICF) 1181 doICF(Symtab->getChunks()); 1182 1183 // Write the result. 1184 writeResult(); 1185 1186 // Call exit to avoid calling destructors. 1187 exit(0); 1188 } 1189 1190 } // namespace coff 1191 } // namespace lld 1192