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