]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - contrib/llvm/tools/lld/COFF/DriverUtils.cpp
Merge ^/head r318964 through r319164.
[FreeBSD/FreeBSD.git] / contrib / llvm / tools / lld / COFF / DriverUtils.cpp
1 //===- DriverUtils.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 // This file contains utility functions for the driver. Because there
11 // are so many small functions, we created this separate file to make
12 // Driver.cpp less cluttered.
13 //
14 //===----------------------------------------------------------------------===//
15
16 #include "Config.h"
17 #include "Driver.h"
18 #include "Error.h"
19 #include "Memory.h"
20 #include "Symbols.h"
21 #include "llvm/ADT/Optional.h"
22 #include "llvm/ADT/StringSwitch.h"
23 #include "llvm/Object/COFF.h"
24 #include "llvm/Option/Arg.h"
25 #include "llvm/Option/ArgList.h"
26 #include "llvm/Option/Option.h"
27 #include "llvm/Support/CommandLine.h"
28 #include "llvm/Support/FileUtilities.h"
29 #include "llvm/Support/Process.h"
30 #include "llvm/Support/Program.h"
31 #include "llvm/Support/raw_ostream.h"
32 #include <memory>
33
34 using namespace llvm::COFF;
35 using namespace llvm;
36 using llvm::cl::ExpandResponseFiles;
37 using llvm::cl::TokenizeWindowsCommandLine;
38 using llvm::sys::Process;
39
40 namespace lld {
41 namespace coff {
42 namespace {
43
44 class Executor {
45 public:
46   explicit Executor(StringRef S) : Prog(Saver.save(S)) {}
47   void add(StringRef S) { Args.push_back(Saver.save(S)); }
48   void add(std::string &S) { Args.push_back(Saver.save(S)); }
49   void add(Twine S) { Args.push_back(Saver.save(S)); }
50   void add(const char *S) { Args.push_back(Saver.save(S)); }
51
52   void run() {
53     ErrorOr<std::string> ExeOrErr = sys::findProgramByName(Prog);
54     if (auto EC = ExeOrErr.getError())
55       fatal(EC, "unable to find " + Prog + " in PATH: ");
56     StringRef Exe = Saver.save(*ExeOrErr);
57     Args.insert(Args.begin(), Exe);
58
59     std::vector<const char *> Vec;
60     for (StringRef S : Args)
61       Vec.push_back(S.data());
62     Vec.push_back(nullptr);
63
64     if (sys::ExecuteAndWait(Args[0], Vec.data()) != 0)
65       fatal("ExecuteAndWait failed: " +
66             llvm::join(Args.begin(), Args.end(), " "));
67   }
68
69 private:
70   StringRef Prog;
71   std::vector<StringRef> Args;
72 };
73
74 } // anonymous namespace
75
76 // Returns /machine's value.
77 MachineTypes getMachineType(StringRef S) {
78   MachineTypes MT = StringSwitch<MachineTypes>(S.lower())
79                         .Cases("x64", "amd64", AMD64)
80                         .Cases("x86", "i386", I386)
81                         .Case("arm", ARMNT)
82                         .Default(IMAGE_FILE_MACHINE_UNKNOWN);
83   if (MT != IMAGE_FILE_MACHINE_UNKNOWN)
84     return MT;
85   fatal("unknown /machine argument: " + S);
86 }
87
88 StringRef machineToStr(MachineTypes MT) {
89   switch (MT) {
90   case ARMNT:
91     return "arm";
92   case AMD64:
93     return "x64";
94   case I386:
95     return "x86";
96   default:
97     llvm_unreachable("unknown machine type");
98   }
99 }
100
101 // Parses a string in the form of "<integer>[,<integer>]".
102 void parseNumbers(StringRef Arg, uint64_t *Addr, uint64_t *Size) {
103   StringRef S1, S2;
104   std::tie(S1, S2) = Arg.split(',');
105   if (S1.getAsInteger(0, *Addr))
106     fatal("invalid number: " + S1);
107   if (Size && !S2.empty() && S2.getAsInteger(0, *Size))
108     fatal("invalid number: " + S2);
109 }
110
111 // Parses a string in the form of "<integer>[.<integer>]".
112 // If second number is not present, Minor is set to 0.
113 void parseVersion(StringRef Arg, uint32_t *Major, uint32_t *Minor) {
114   StringRef S1, S2;
115   std::tie(S1, S2) = Arg.split('.');
116   if (S1.getAsInteger(0, *Major))
117     fatal("invalid number: " + S1);
118   *Minor = 0;
119   if (!S2.empty() && S2.getAsInteger(0, *Minor))
120     fatal("invalid number: " + S2);
121 }
122
123 // Parses a string in the form of "<subsystem>[,<integer>[.<integer>]]".
124 void parseSubsystem(StringRef Arg, WindowsSubsystem *Sys, uint32_t *Major,
125                     uint32_t *Minor) {
126   StringRef SysStr, Ver;
127   std::tie(SysStr, Ver) = Arg.split(',');
128   *Sys = StringSwitch<WindowsSubsystem>(SysStr.lower())
129     .Case("boot_application", IMAGE_SUBSYSTEM_WINDOWS_BOOT_APPLICATION)
130     .Case("console", IMAGE_SUBSYSTEM_WINDOWS_CUI)
131     .Case("efi_application", IMAGE_SUBSYSTEM_EFI_APPLICATION)
132     .Case("efi_boot_service_driver", IMAGE_SUBSYSTEM_EFI_BOOT_SERVICE_DRIVER)
133     .Case("efi_rom", IMAGE_SUBSYSTEM_EFI_ROM)
134     .Case("efi_runtime_driver", IMAGE_SUBSYSTEM_EFI_RUNTIME_DRIVER)
135     .Case("native", IMAGE_SUBSYSTEM_NATIVE)
136     .Case("posix", IMAGE_SUBSYSTEM_POSIX_CUI)
137     .Case("windows", IMAGE_SUBSYSTEM_WINDOWS_GUI)
138     .Default(IMAGE_SUBSYSTEM_UNKNOWN);
139   if (*Sys == IMAGE_SUBSYSTEM_UNKNOWN)
140     fatal("unknown subsystem: " + SysStr);
141   if (!Ver.empty())
142     parseVersion(Ver, Major, Minor);
143 }
144
145 // Parse a string of the form of "<from>=<to>".
146 // Results are directly written to Config.
147 void parseAlternateName(StringRef S) {
148   StringRef From, To;
149   std::tie(From, To) = S.split('=');
150   if (From.empty() || To.empty())
151     fatal("/alternatename: invalid argument: " + S);
152   auto It = Config->AlternateNames.find(From);
153   if (It != Config->AlternateNames.end() && It->second != To)
154     fatal("/alternatename: conflicts: " + S);
155   Config->AlternateNames.insert(It, std::make_pair(From, To));
156 }
157
158 // Parse a string of the form of "<from>=<to>".
159 // Results are directly written to Config.
160 void parseMerge(StringRef S) {
161   StringRef From, To;
162   std::tie(From, To) = S.split('=');
163   if (From.empty() || To.empty())
164     fatal("/merge: invalid argument: " + S);
165   auto Pair = Config->Merge.insert(std::make_pair(From, To));
166   bool Inserted = Pair.second;
167   if (!Inserted) {
168     StringRef Existing = Pair.first->second;
169     if (Existing != To)
170       warn(S + ": already merged into " + Existing);
171   }
172 }
173
174 static uint32_t parseSectionAttributes(StringRef S) {
175   uint32_t Ret = 0;
176   for (char C : S.lower()) {
177     switch (C) {
178     case 'd':
179       Ret |= IMAGE_SCN_MEM_DISCARDABLE;
180       break;
181     case 'e':
182       Ret |= IMAGE_SCN_MEM_EXECUTE;
183       break;
184     case 'k':
185       Ret |= IMAGE_SCN_MEM_NOT_CACHED;
186       break;
187     case 'p':
188       Ret |= IMAGE_SCN_MEM_NOT_PAGED;
189       break;
190     case 'r':
191       Ret |= IMAGE_SCN_MEM_READ;
192       break;
193     case 's':
194       Ret |= IMAGE_SCN_MEM_SHARED;
195       break;
196     case 'w':
197       Ret |= IMAGE_SCN_MEM_WRITE;
198       break;
199     default:
200       fatal("/section: invalid argument: " + S);
201     }
202   }
203   return Ret;
204 }
205
206 // Parses /section option argument.
207 void parseSection(StringRef S) {
208   StringRef Name, Attrs;
209   std::tie(Name, Attrs) = S.split(',');
210   if (Name.empty() || Attrs.empty())
211     fatal("/section: invalid argument: " + S);
212   Config->Section[Name] = parseSectionAttributes(Attrs);
213 }
214
215 // Parses a string in the form of "EMBED[,=<integer>]|NO".
216 // Results are directly written to Config.
217 void parseManifest(StringRef Arg) {
218   if (Arg.equals_lower("no")) {
219     Config->Manifest = Configuration::No;
220     return;
221   }
222   if (!Arg.startswith_lower("embed"))
223     fatal("invalid option " + Arg);
224   Config->Manifest = Configuration::Embed;
225   Arg = Arg.substr(strlen("embed"));
226   if (Arg.empty())
227     return;
228   if (!Arg.startswith_lower(",id="))
229     fatal("invalid option " + Arg);
230   Arg = Arg.substr(strlen(",id="));
231   if (Arg.getAsInteger(0, Config->ManifestID))
232     fatal("invalid option " + Arg);
233 }
234
235 // Parses a string in the form of "level=<string>|uiAccess=<string>|NO".
236 // Results are directly written to Config.
237 void parseManifestUAC(StringRef Arg) {
238   if (Arg.equals_lower("no")) {
239     Config->ManifestUAC = false;
240     return;
241   }
242   for (;;) {
243     Arg = Arg.ltrim();
244     if (Arg.empty())
245       return;
246     if (Arg.startswith_lower("level=")) {
247       Arg = Arg.substr(strlen("level="));
248       std::tie(Config->ManifestLevel, Arg) = Arg.split(" ");
249       continue;
250     }
251     if (Arg.startswith_lower("uiaccess=")) {
252       Arg = Arg.substr(strlen("uiaccess="));
253       std::tie(Config->ManifestUIAccess, Arg) = Arg.split(" ");
254       continue;
255     }
256     fatal("invalid option " + Arg);
257   }
258 }
259
260 // Quote each line with "". Existing double-quote is converted
261 // to two double-quotes.
262 static void quoteAndPrint(raw_ostream &Out, StringRef S) {
263   while (!S.empty()) {
264     StringRef Line;
265     std::tie(Line, S) = S.split("\n");
266     if (Line.empty())
267       continue;
268     Out << '\"';
269     for (int I = 0, E = Line.size(); I != E; ++I) {
270       if (Line[I] == '\"') {
271         Out << "\"\"";
272       } else {
273         Out << Line[I];
274       }
275     }
276     Out << "\"\n";
277   }
278 }
279
280 // An RAII temporary file class that automatically removes a temporary file.
281 namespace {
282 class TemporaryFile {
283 public:
284   TemporaryFile(StringRef Prefix, StringRef Extn, StringRef Contents = "") {
285     SmallString<128> S;
286     if (auto EC = sys::fs::createTemporaryFile("lld-" + Prefix, Extn, S))
287       fatal(EC, "cannot create a temporary file");
288     Path = S.str();
289
290     if (!Contents.empty()) {
291       std::error_code EC;
292       raw_fd_ostream OS(Path, EC, sys::fs::F_None);
293       if (EC)
294         fatal(EC, "failed to open " + Path);
295       OS << Contents;
296     }
297   }
298
299   TemporaryFile(TemporaryFile &&Obj) {
300     std::swap(Path, Obj.Path);
301   }
302
303   ~TemporaryFile() {
304     if (Path.empty())
305       return;
306     if (sys::fs::remove(Path))
307       fatal("failed to remove " + Path);
308   }
309
310   // Returns a memory buffer of this temporary file.
311   // Note that this function does not leave the file open,
312   // so it is safe to remove the file immediately after this function
313   // is called (you cannot remove an opened file on Windows.)
314   std::unique_ptr<MemoryBuffer> getMemoryBuffer() {
315     // IsVolatileSize=true forces MemoryBuffer to not use mmap().
316     return check(MemoryBuffer::getFile(Path, /*FileSize=*/-1,
317                                        /*RequiresNullTerminator=*/false,
318                                        /*IsVolatileSize=*/true),
319                  "could not open " + Path);
320   }
321
322   std::string Path;
323 };
324 }
325
326 // Create the default manifest file as a temporary file.
327 TemporaryFile createDefaultXml() {
328   // Create a temporary file.
329   TemporaryFile File("defaultxml", "manifest");
330
331   // Open the temporary file for writing.
332   std::error_code EC;
333   raw_fd_ostream OS(File.Path, EC, sys::fs::F_Text);
334   if (EC)
335     fatal(EC, "failed to open " + File.Path);
336
337   // Emit the XML. Note that we do *not* verify that the XML attributes are
338   // syntactically correct. This is intentional for link.exe compatibility.
339   OS << "<?xml version=\"1.0\" standalone=\"yes\"?>\n"
340      << "<assembly xmlns=\"urn:schemas-microsoft-com:asm.v1\"\n"
341      << "          manifestVersion=\"1.0\">\n";
342   if (Config->ManifestUAC) {
343     OS << "  <trustInfo>\n"
344        << "    <security>\n"
345        << "      <requestedPrivileges>\n"
346        << "         <requestedExecutionLevel level=" << Config->ManifestLevel
347        << " uiAccess=" << Config->ManifestUIAccess << "/>\n"
348        << "      </requestedPrivileges>\n"
349        << "    </security>\n"
350        << "  </trustInfo>\n";
351     if (!Config->ManifestDependency.empty()) {
352       OS << "  <dependency>\n"
353          << "    <dependentAssembly>\n"
354          << "      <assemblyIdentity " << Config->ManifestDependency << " />\n"
355          << "    </dependentAssembly>\n"
356          << "  </dependency>\n";
357     }
358   }
359   OS << "</assembly>\n";
360   OS.close();
361   return File;
362 }
363
364 static std::string readFile(StringRef Path) {
365   std::unique_ptr<MemoryBuffer> MB =
366       check(MemoryBuffer::getFile(Path), "could not open " + Path);
367   return MB->getBuffer();
368 }
369
370 static std::string createManifestXml() {
371   // Create the default manifest file.
372   TemporaryFile File1 = createDefaultXml();
373   if (Config->ManifestInput.empty())
374     return readFile(File1.Path);
375
376   // If manifest files are supplied by the user using /MANIFESTINPUT
377   // option, we need to merge them with the default manifest.
378   TemporaryFile File2("user", "manifest");
379
380   Executor E("mt.exe");
381   E.add("/manifest");
382   E.add(File1.Path);
383   for (StringRef Filename : Config->ManifestInput) {
384     E.add("/manifest");
385     E.add(Filename);
386   }
387   E.add("/nologo");
388   E.add("/out:" + StringRef(File2.Path));
389   E.run();
390   return readFile(File2.Path);
391 }
392
393 // Create a resource file containing a manifest XML.
394 std::unique_ptr<MemoryBuffer> createManifestRes() {
395   // Create a temporary file for the resource script file.
396   TemporaryFile RCFile("manifest", "rc");
397
398   // Open the temporary file for writing.
399   std::error_code EC;
400   raw_fd_ostream Out(RCFile.Path, EC, sys::fs::F_Text);
401   if (EC)
402     fatal(EC, "failed to open " + RCFile.Path);
403
404   // Write resource script to the RC file.
405   Out << "#define LANG_ENGLISH 9\n"
406       << "#define SUBLANG_DEFAULT 1\n"
407       << "#define APP_MANIFEST " << Config->ManifestID << "\n"
408       << "#define RT_MANIFEST 24\n"
409       << "LANGUAGE LANG_ENGLISH, SUBLANG_DEFAULT\n"
410       << "APP_MANIFEST RT_MANIFEST {\n";
411   quoteAndPrint(Out, createManifestXml());
412   Out << "}\n";
413   Out.close();
414
415   // Create output resource file.
416   TemporaryFile ResFile("output-resource", "res");
417
418   Executor E("rc.exe");
419   E.add("/fo");
420   E.add(ResFile.Path);
421   E.add("/nologo");
422   E.add(RCFile.Path);
423   E.run();
424   return ResFile.getMemoryBuffer();
425 }
426
427 void createSideBySideManifest() {
428   std::string Path = Config->ManifestFile;
429   if (Path == "")
430     Path = Config->OutputFile + ".manifest";
431   std::error_code EC;
432   raw_fd_ostream Out(Path, EC, sys::fs::F_Text);
433   if (EC)
434     fatal(EC, "failed to create manifest");
435   Out << createManifestXml();
436 }
437
438 // Parse a string in the form of
439 // "<name>[=<internalname>][,@ordinal[,NONAME]][,DATA][,PRIVATE]"
440 // or "<name>=<dllname>.<name>".
441 // Used for parsing /export arguments.
442 Export parseExport(StringRef Arg) {
443   Export E;
444   StringRef Rest;
445   std::tie(E.Name, Rest) = Arg.split(",");
446   if (E.Name.empty())
447     goto err;
448
449   if (E.Name.find('=') != StringRef::npos) {
450     StringRef X, Y;
451     std::tie(X, Y) = E.Name.split("=");
452
453     // If "<name>=<dllname>.<name>".
454     if (Y.find(".") != StringRef::npos) {
455       E.Name = X;
456       E.ForwardTo = Y;
457       return E;
458     }
459
460     E.ExtName = X;
461     E.Name = Y;
462     if (E.Name.empty())
463       goto err;
464   }
465
466   // If "<name>=<internalname>[,@ordinal[,NONAME]][,DATA][,PRIVATE]"
467   while (!Rest.empty()) {
468     StringRef Tok;
469     std::tie(Tok, Rest) = Rest.split(",");
470     if (Tok.equals_lower("noname")) {
471       if (E.Ordinal == 0)
472         goto err;
473       E.Noname = true;
474       continue;
475     }
476     if (Tok.equals_lower("data")) {
477       E.Data = true;
478       continue;
479     }
480     if (Tok.equals_lower("constant")) {
481       E.Constant = true;
482       continue;
483     }
484     if (Tok.equals_lower("private")) {
485       E.Private = true;
486       continue;
487     }
488     if (Tok.startswith("@")) {
489       int32_t Ord;
490       if (Tok.substr(1).getAsInteger(0, Ord))
491         goto err;
492       if (Ord <= 0 || 65535 < Ord)
493         goto err;
494       E.Ordinal = Ord;
495       continue;
496     }
497     goto err;
498   }
499   return E;
500
501 err:
502   fatal("invalid /export: " + Arg);
503 }
504
505 static StringRef undecorate(StringRef Sym) {
506   if (Config->Machine != I386)
507     return Sym;
508   return Sym.startswith("_") ? Sym.substr(1) : Sym;
509 }
510
511 // Performs error checking on all /export arguments.
512 // It also sets ordinals.
513 void fixupExports() {
514   // Symbol ordinals must be unique.
515   std::set<uint16_t> Ords;
516   for (Export &E : Config->Exports) {
517     if (E.Ordinal == 0)
518       continue;
519     if (!Ords.insert(E.Ordinal).second)
520       fatal("duplicate export ordinal: " + E.Name);
521   }
522
523   for (Export &E : Config->Exports) {
524     SymbolBody *Sym = E.Sym;
525     if (!E.ForwardTo.empty()) {
526       E.SymbolName = E.Name;
527     } else {
528       if (auto *U = dyn_cast<Undefined>(Sym))
529         if (U->WeakAlias)
530           Sym = U->WeakAlias;
531       E.SymbolName = Sym->getName();
532     }
533   }
534
535   for (Export &E : Config->Exports) {
536     if (!E.ForwardTo.empty()) {
537       E.ExportName = undecorate(E.Name);
538     } else {
539       E.ExportName = undecorate(E.ExtName.empty() ? E.Name : E.ExtName);
540     }
541   }
542
543   // Uniquefy by name.
544   std::map<StringRef, Export *> Map;
545   std::vector<Export> V;
546   for (Export &E : Config->Exports) {
547     auto Pair = Map.insert(std::make_pair(E.ExportName, &E));
548     bool Inserted = Pair.second;
549     if (Inserted) {
550       V.push_back(E);
551       continue;
552     }
553     Export *Existing = Pair.first->second;
554     if (E == *Existing || E.Name != Existing->Name)
555       continue;
556     warn("duplicate /export option: " + E.Name);
557   }
558   Config->Exports = std::move(V);
559
560   // Sort by name.
561   std::sort(Config->Exports.begin(), Config->Exports.end(),
562             [](const Export &A, const Export &B) {
563               return A.ExportName < B.ExportName;
564             });
565 }
566
567 void assignExportOrdinals() {
568   // Assign unique ordinals if default (= 0).
569   uint16_t Max = 0;
570   for (Export &E : Config->Exports)
571     Max = std::max(Max, E.Ordinal);
572   for (Export &E : Config->Exports)
573     if (E.Ordinal == 0)
574       E.Ordinal = ++Max;
575 }
576
577 // Parses a string in the form of "key=value" and check
578 // if value matches previous values for the same key.
579 void checkFailIfMismatch(StringRef Arg) {
580   StringRef K, V;
581   std::tie(K, V) = Arg.split('=');
582   if (K.empty() || V.empty())
583     fatal("/failifmismatch: invalid argument: " + Arg);
584   StringRef Existing = Config->MustMatch[K];
585   if (!Existing.empty() && V != Existing)
586     fatal("/failifmismatch: mismatch detected: " + Existing + " and " + V +
587           " for key " + K);
588   Config->MustMatch[K] = V;
589 }
590
591 // Convert Windows resource files (.res files) to a .obj file
592 // using cvtres.exe.
593 std::unique_ptr<MemoryBuffer>
594 convertResToCOFF(const std::vector<MemoryBufferRef> &MBs) {
595   // Create an output file path.
596   TemporaryFile File("resource-file", "obj");
597
598   // Execute cvtres.exe.
599   Executor E("cvtres.exe");
600   E.add("/machine:" + machineToStr(Config->Machine));
601   E.add("/readonly");
602   E.add("/nologo");
603   E.add("/out:" + Twine(File.Path));
604
605   // We must create new files because the memory buffers we have may have no
606   // underlying file still existing on the disk.
607   // It happens if it was created from a TemporaryFile, which usually delete
608   // the file just after creating the MemoryBuffer.
609   std::vector<TemporaryFile> ResFiles;
610   ResFiles.reserve(MBs.size());
611   for (MemoryBufferRef MB : MBs) {
612     // We store the temporary file in a vector to avoid deletion
613     // before running cvtres
614     ResFiles.emplace_back("resource-file", "res");
615     TemporaryFile& ResFile = ResFiles.back();
616     // Write the content of the resource in a temporary file
617     std::error_code EC;
618     raw_fd_ostream OS(ResFile.Path, EC, sys::fs::F_None);
619     if (EC)
620       fatal(EC, "failed to open " + ResFile.Path);
621     OS << MB.getBuffer();
622     OS.close();
623
624     E.add(ResFile.Path);
625   }
626
627   E.run();
628   return File.getMemoryBuffer();
629 }
630
631 // Run MSVC link.exe for given in-memory object files.
632 // Command line options are copied from those given to LLD.
633 // This is for the /msvclto option.
634 void runMSVCLinker(std::string Rsp, ArrayRef<StringRef> Objects) {
635   // Write the in-memory object files to disk.
636   std::vector<TemporaryFile> Temps;
637   for (StringRef S : Objects) {
638     Temps.emplace_back("lto", "obj", S);
639     Rsp += quote(Temps.back().Path) + "\n";
640   }
641
642   log("link.exe " + Rsp);
643
644   // Run MSVC link.exe.
645   Temps.emplace_back("lto", "rsp", Rsp);
646   Executor E("link.exe");
647   E.add(Twine("@" + Temps.back().Path));
648   E.run();
649 }
650
651 // Create OptTable
652
653 // Create prefix string literals used in Options.td
654 #define PREFIX(NAME, VALUE) const char *const NAME[] = VALUE;
655 #include "Options.inc"
656 #undef PREFIX
657
658 // Create table mapping all options defined in Options.td
659 static const llvm::opt::OptTable::Info infoTable[] = {
660 #define OPTION(X1, X2, ID, KIND, GROUP, ALIAS, X6, X7, X8, X9, X10)    \
661   {                                                                    \
662     X1, X2, X9, X10, OPT_##ID, llvm::opt::Option::KIND##Class, X8, X7, \
663     OPT_##GROUP, OPT_##ALIAS, X6                                       \
664   },
665 #include "Options.inc"
666 #undef OPTION
667 };
668
669 class COFFOptTable : public llvm::opt::OptTable {
670 public:
671   COFFOptTable() : OptTable(infoTable, true) {}
672 };
673
674 // Parses a given list of options.
675 opt::InputArgList ArgParser::parse(ArrayRef<const char *> ArgsArr) {
676   // First, replace respnose files (@<file>-style options).
677   std::vector<const char *> Argv = replaceResponseFiles(ArgsArr);
678
679   // Make InputArgList from string vectors.
680   COFFOptTable Table;
681   unsigned MissingIndex;
682   unsigned MissingCount;
683   opt::InputArgList Args = Table.ParseArgs(Argv, MissingIndex, MissingCount);
684
685   // Print the real command line if response files are expanded.
686   if (Args.hasArg(OPT_verbose) && ArgsArr.size() != Argv.size()) {
687     std::string Msg = "Command line:";
688     for (const char *S : Argv)
689       Msg += " " + std::string(S);
690     message(Msg);
691   }
692
693   if (MissingCount)
694     fatal(Twine(Args.getArgString(MissingIndex)) + ": missing argument");
695   for (auto *Arg : Args.filtered(OPT_UNKNOWN))
696     warn("ignoring unknown argument: " + Arg->getSpelling());
697   return Args;
698 }
699
700 // link.exe has an interesting feature. If LINK or _LINK_ environment
701 // variables exist, their contents are handled as command line strings.
702 // So you can pass extra arguments using them.
703 opt::InputArgList ArgParser::parseLINK(std::vector<const char *> Args) {
704   // Concatenate LINK env and command line arguments, and then parse them.
705   if (Optional<std::string> S = Process::GetEnv("LINK")) {
706     std::vector<const char *> V = tokenize(*S);
707     Args.insert(Args.begin(), V.begin(), V.end());
708   }
709   if (Optional<std::string> S = Process::GetEnv("_LINK_")) {
710     std::vector<const char *> V = tokenize(*S);
711     Args.insert(Args.begin(), V.begin(), V.end());
712   }
713   return parse(Args);
714 }
715
716 std::vector<const char *> ArgParser::tokenize(StringRef S) {
717   SmallVector<const char *, 16> Tokens;
718   cl::TokenizeWindowsCommandLine(S, Saver, Tokens);
719   return std::vector<const char *>(Tokens.begin(), Tokens.end());
720 }
721
722 // Creates a new command line by replacing options starting with '@'
723 // character. '@<filename>' is replaced by the file's contents.
724 std::vector<const char *>
725 ArgParser::replaceResponseFiles(std::vector<const char *> Argv) {
726   SmallVector<const char *, 256> Tokens(Argv.data(), Argv.data() + Argv.size());
727   ExpandResponseFiles(Saver, TokenizeWindowsCommandLine, Tokens);
728   return std::vector<const char *>(Tokens.begin(), Tokens.end());
729 }
730
731 void printHelp(const char *Argv0) {
732   COFFOptTable Table;
733   Table.PrintHelp(outs(), Argv0, "LLVM Linker", false);
734 }
735
736 } // namespace coff
737 } // namespace lld