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