]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - contrib/llvm/tools/lld/COFF/DriverUtils.cpp
Update lld to trunk r290819 and resolve conflicts.
[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) : Saver(Alloc), Prog(Saver.save(S)) {}
47   void add(StringRef S) { Args.push_back(Saver.save(S).data()); }
48   void add(std::string &S) { Args.push_back(Saver.save(S).data()); }
49   void add(Twine S) { Args.push_back(Saver.save(S).data()); }
50   void add(const char *S) { Args.push_back(Saver.save(S).data()); }
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     const char *Exe = Saver.save(*ExeOrErr).data();
57     Args.insert(Args.begin(), Exe);
58     Args.push_back(nullptr);
59     if (sys::ExecuteAndWait(Args[0], Args.data()) != 0) {
60       for (const char *S : Args)
61         if (S)
62           errs() << S << " ";
63       fatal("ExecuteAndWait failed");
64     }
65   }
66
67 private:
68   BumpPtrAllocator Alloc;
69   StringSaver Saver;
70   StringRef Prog;
71   std::vector<const char *> 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       errs() << "warning: " << S << ": already merged into " << Existing
171              << "\n";
172   }
173 }
174
175 static uint32_t parseSectionAttributes(StringRef S) {
176   uint32_t Ret = 0;
177   for (char C : S.lower()) {
178     switch (C) {
179     case 'd':
180       Ret |= IMAGE_SCN_MEM_DISCARDABLE;
181       break;
182     case 'e':
183       Ret |= IMAGE_SCN_MEM_EXECUTE;
184       break;
185     case 'k':
186       Ret |= IMAGE_SCN_MEM_NOT_CACHED;
187       break;
188     case 'p':
189       Ret |= IMAGE_SCN_MEM_NOT_PAGED;
190       break;
191     case 'r':
192       Ret |= IMAGE_SCN_MEM_READ;
193       break;
194     case 's':
195       Ret |= IMAGE_SCN_MEM_SHARED;
196       break;
197     case 'w':
198       Ret |= IMAGE_SCN_MEM_WRITE;
199       break;
200     default:
201       fatal("/section: invalid argument: " + S);
202     }
203   }
204   return Ret;
205 }
206
207 // Parses /section option argument.
208 void parseSection(StringRef S) {
209   StringRef Name, Attrs;
210   std::tie(Name, Attrs) = S.split(',');
211   if (Name.empty() || Attrs.empty())
212     fatal("/section: invalid argument: " + S);
213   Config->Section[Name] = parseSectionAttributes(Attrs);
214 }
215
216 // Parses a string in the form of "EMBED[,=<integer>]|NO".
217 // Results are directly written to Config.
218 void parseManifest(StringRef Arg) {
219   if (Arg.equals_lower("no")) {
220     Config->Manifest = Configuration::No;
221     return;
222   }
223   if (!Arg.startswith_lower("embed"))
224     fatal("invalid option " + Arg);
225   Config->Manifest = Configuration::Embed;
226   Arg = Arg.substr(strlen("embed"));
227   if (Arg.empty())
228     return;
229   if (!Arg.startswith_lower(",id="))
230     fatal("invalid option " + Arg);
231   Arg = Arg.substr(strlen(",id="));
232   if (Arg.getAsInteger(0, Config->ManifestID))
233     fatal("invalid option " + Arg);
234 }
235
236 // Parses a string in the form of "level=<string>|uiAccess=<string>|NO".
237 // Results are directly written to Config.
238 void parseManifestUAC(StringRef Arg) {
239   if (Arg.equals_lower("no")) {
240     Config->ManifestUAC = false;
241     return;
242   }
243   for (;;) {
244     Arg = Arg.ltrim();
245     if (Arg.empty())
246       return;
247     if (Arg.startswith_lower("level=")) {
248       Arg = Arg.substr(strlen("level="));
249       std::tie(Config->ManifestLevel, Arg) = Arg.split(" ");
250       continue;
251     }
252     if (Arg.startswith_lower("uiaccess=")) {
253       Arg = Arg.substr(strlen("uiaccess="));
254       std::tie(Config->ManifestUIAccess, Arg) = Arg.split(" ");
255       continue;
256     }
257     fatal("invalid option " + Arg);
258   }
259 }
260
261 // Quote each line with "". Existing double-quote is converted
262 // to two double-quotes.
263 static void quoteAndPrint(raw_ostream &Out, StringRef S) {
264   while (!S.empty()) {
265     StringRef Line;
266     std::tie(Line, S) = S.split("\n");
267     if (Line.empty())
268       continue;
269     Out << '\"';
270     for (int I = 0, E = Line.size(); I != E; ++I) {
271       if (Line[I] == '\"') {
272         Out << "\"\"";
273       } else {
274         Out << Line[I];
275       }
276     }
277     Out << "\"\n";
278   }
279 }
280
281 // An RAII temporary file class that automatically removes a temporary file.
282 namespace {
283 class TemporaryFile {
284 public:
285   TemporaryFile(StringRef Prefix, StringRef Extn) {
286     SmallString<128> S;
287     if (auto EC = sys::fs::createTemporaryFile("lld-" + Prefix, Extn, S))
288       fatal(EC, "cannot create a temporary file");
289     Path = S.str();
290   }
291
292   TemporaryFile(TemporaryFile &&Obj) {
293     std::swap(Path, Obj.Path);
294   }
295
296   ~TemporaryFile() {
297     if (Path.empty())
298       return;
299     if (sys::fs::remove(Path))
300       fatal("failed to remove " + Path);
301   }
302
303   // Returns a memory buffer of this temporary file.
304   // Note that this function does not leave the file open,
305   // so it is safe to remove the file immediately after this function
306   // is called (you cannot remove an opened file on Windows.)
307   std::unique_ptr<MemoryBuffer> getMemoryBuffer() {
308     // IsVolatileSize=true forces MemoryBuffer to not use mmap().
309     return check(MemoryBuffer::getFile(Path, /*FileSize=*/-1,
310                                        /*RequiresNullTerminator=*/false,
311                                        /*IsVolatileSize=*/true),
312                  "could not open " + Path);
313   }
314
315   std::string Path;
316 };
317 }
318
319 // Create the default manifest file as a temporary file.
320 TemporaryFile createDefaultXml() {
321   // Create a temporary file.
322   TemporaryFile File("defaultxml", "manifest");
323
324   // Open the temporary file for writing.
325   std::error_code EC;
326   raw_fd_ostream OS(File.Path, EC, sys::fs::F_Text);
327   if (EC)
328     fatal(EC, "failed to open " + File.Path);
329
330   // Emit the XML. Note that we do *not* verify that the XML attributes are
331   // syntactically correct. This is intentional for link.exe compatibility.
332   OS << "<?xml version=\"1.0\" standalone=\"yes\"?>\n"
333      << "<assembly xmlns=\"urn:schemas-microsoft-com:asm.v1\"\n"
334      << "          manifestVersion=\"1.0\">\n";
335   if (Config->ManifestUAC) {
336     OS << "  <trustInfo>\n"
337        << "    <security>\n"
338        << "      <requestedPrivileges>\n"
339        << "         <requestedExecutionLevel level=" << Config->ManifestLevel
340        << " uiAccess=" << Config->ManifestUIAccess << "/>\n"
341        << "      </requestedPrivileges>\n"
342        << "    </security>\n"
343        << "  </trustInfo>\n";
344     if (!Config->ManifestDependency.empty()) {
345       OS << "  <dependency>\n"
346          << "    <dependentAssembly>\n"
347          << "      <assemblyIdentity " << Config->ManifestDependency << " />\n"
348          << "    </dependentAssembly>\n"
349          << "  </dependency>\n";
350     }
351   }
352   OS << "</assembly>\n";
353   OS.close();
354   return File;
355 }
356
357 static std::string readFile(StringRef Path) {
358   std::unique_ptr<MemoryBuffer> MB =
359       check(MemoryBuffer::getFile(Path), "could not open " + Path);
360   return MB->getBuffer();
361 }
362
363 static std::string createManifestXml() {
364   // Create the default manifest file.
365   TemporaryFile File1 = createDefaultXml();
366   if (Config->ManifestInput.empty())
367     return readFile(File1.Path);
368
369   // If manifest files are supplied by the user using /MANIFESTINPUT
370   // option, we need to merge them with the default manifest.
371   TemporaryFile File2("user", "manifest");
372
373   Executor E("mt.exe");
374   E.add("/manifest");
375   E.add(File1.Path);
376   for (StringRef Filename : Config->ManifestInput) {
377     E.add("/manifest");
378     E.add(Filename);
379   }
380   E.add("/nologo");
381   E.add("/out:" + StringRef(File2.Path));
382   E.run();
383   return readFile(File2.Path);
384 }
385
386 // Create a resource file containing a manifest XML.
387 std::unique_ptr<MemoryBuffer> createManifestRes() {
388   // Create a temporary file for the resource script file.
389   TemporaryFile RCFile("manifest", "rc");
390
391   // Open the temporary file for writing.
392   std::error_code EC;
393   raw_fd_ostream Out(RCFile.Path, EC, sys::fs::F_Text);
394   if (EC)
395     fatal(EC, "failed to open " + RCFile.Path);
396
397   // Write resource script to the RC file.
398   Out << "#define LANG_ENGLISH 9\n"
399       << "#define SUBLANG_DEFAULT 1\n"
400       << "#define APP_MANIFEST " << Config->ManifestID << "\n"
401       << "#define RT_MANIFEST 24\n"
402       << "LANGUAGE LANG_ENGLISH, SUBLANG_DEFAULT\n"
403       << "APP_MANIFEST RT_MANIFEST {\n";
404   quoteAndPrint(Out, createManifestXml());
405   Out << "}\n";
406   Out.close();
407
408   // Create output resource file.
409   TemporaryFile ResFile("output-resource", "res");
410
411   Executor E("rc.exe");
412   E.add("/fo");
413   E.add(ResFile.Path);
414   E.add("/nologo");
415   E.add(RCFile.Path);
416   E.run();
417   return ResFile.getMemoryBuffer();
418 }
419
420 void createSideBySideManifest() {
421   std::string Path = Config->ManifestFile;
422   if (Path == "")
423     Path = Config->OutputFile + ".manifest";
424   std::error_code EC;
425   raw_fd_ostream Out(Path, EC, sys::fs::F_Text);
426   if (EC)
427     fatal(EC, "failed to create manifest");
428   Out << createManifestXml();
429 }
430
431 // Parse a string in the form of
432 // "<name>[=<internalname>][,@ordinal[,NONAME]][,DATA][,PRIVATE]"
433 // or "<name>=<dllname>.<name>".
434 // Used for parsing /export arguments.
435 Export parseExport(StringRef Arg) {
436   Export E;
437   StringRef Rest;
438   std::tie(E.Name, Rest) = Arg.split(",");
439   if (E.Name.empty())
440     goto err;
441
442   if (E.Name.find('=') != StringRef::npos) {
443     StringRef X, Y;
444     std::tie(X, Y) = E.Name.split("=");
445
446     // If "<name>=<dllname>.<name>".
447     if (Y.find(".") != StringRef::npos) {
448       E.Name = X;
449       E.ForwardTo = Y;
450       return E;
451     }
452
453     E.ExtName = X;
454     E.Name = Y;
455     if (E.Name.empty())
456       goto err;
457   }
458
459   // If "<name>=<internalname>[,@ordinal[,NONAME]][,DATA][,PRIVATE]"
460   while (!Rest.empty()) {
461     StringRef Tok;
462     std::tie(Tok, Rest) = Rest.split(",");
463     if (Tok.equals_lower("noname")) {
464       if (E.Ordinal == 0)
465         goto err;
466       E.Noname = true;
467       continue;
468     }
469     if (Tok.equals_lower("data")) {
470       E.Data = true;
471       continue;
472     }
473     if (Tok.equals_lower("private")) {
474       E.Private = true;
475       continue;
476     }
477     if (Tok.startswith("@")) {
478       int32_t Ord;
479       if (Tok.substr(1).getAsInteger(0, Ord))
480         goto err;
481       if (Ord <= 0 || 65535 < Ord)
482         goto err;
483       E.Ordinal = Ord;
484       continue;
485     }
486     goto err;
487   }
488   return E;
489
490 err:
491   fatal("invalid /export: " + Arg);
492 }
493
494 static StringRef undecorate(StringRef Sym) {
495   if (Config->Machine != I386)
496     return Sym;
497   return Sym.startswith("_") ? Sym.substr(1) : Sym;
498 }
499
500 // Performs error checking on all /export arguments.
501 // It also sets ordinals.
502 void fixupExports() {
503   // Symbol ordinals must be unique.
504   std::set<uint16_t> Ords;
505   for (Export &E : Config->Exports) {
506     if (E.Ordinal == 0)
507       continue;
508     if (!Ords.insert(E.Ordinal).second)
509       fatal("duplicate export ordinal: " + E.Name);
510   }
511
512   for (Export &E : Config->Exports) {
513     SymbolBody *Sym = E.Sym;
514     if (!E.ForwardTo.empty()) {
515       E.SymbolName = E.Name;
516     } else {
517       if (auto *U = dyn_cast<Undefined>(Sym))
518         if (U->WeakAlias)
519           Sym = U->WeakAlias;
520       E.SymbolName = Sym->getName();
521     }
522   }
523
524   for (Export &E : Config->Exports) {
525     if (!E.ForwardTo.empty()) {
526       E.ExportName = undecorate(E.Name);
527     } else {
528       E.ExportName = undecorate(E.ExtName.empty() ? E.Name : E.ExtName);
529     }
530   }
531
532   // Uniquefy by name.
533   std::map<StringRef, Export *> Map;
534   std::vector<Export> V;
535   for (Export &E : Config->Exports) {
536     auto Pair = Map.insert(std::make_pair(E.ExportName, &E));
537     bool Inserted = Pair.second;
538     if (Inserted) {
539       V.push_back(E);
540       continue;
541     }
542     Export *Existing = Pair.first->second;
543     if (E == *Existing || E.Name != Existing->Name)
544       continue;
545     errs() << "warning: duplicate /export option: " << E.Name << "\n";
546   }
547   Config->Exports = std::move(V);
548
549   // Sort by name.
550   std::sort(Config->Exports.begin(), Config->Exports.end(),
551             [](const Export &A, const Export &B) {
552               return A.ExportName < B.ExportName;
553             });
554 }
555
556 void assignExportOrdinals() {
557   // Assign unique ordinals if default (= 0).
558   uint16_t Max = 0;
559   for (Export &E : Config->Exports)
560     Max = std::max(Max, E.Ordinal);
561   for (Export &E : Config->Exports)
562     if (E.Ordinal == 0)
563       E.Ordinal = ++Max;
564 }
565
566 // Parses a string in the form of "key=value" and check
567 // if value matches previous values for the same key.
568 void checkFailIfMismatch(StringRef Arg) {
569   StringRef K, V;
570   std::tie(K, V) = Arg.split('=');
571   if (K.empty() || V.empty())
572     fatal("/failifmismatch: invalid argument: " + Arg);
573   StringRef Existing = Config->MustMatch[K];
574   if (!Existing.empty() && V != Existing)
575     fatal("/failifmismatch: mismatch detected: " + Existing + " and " + V +
576           " for key " + K);
577   Config->MustMatch[K] = V;
578 }
579
580 // Convert Windows resource files (.res files) to a .obj file
581 // using cvtres.exe.
582 std::unique_ptr<MemoryBuffer>
583 convertResToCOFF(const std::vector<MemoryBufferRef> &MBs) {
584   // Create an output file path.
585   TemporaryFile File("resource-file", "obj");
586
587   // Execute cvtres.exe.
588   Executor E("cvtres.exe");
589   E.add("/machine:" + machineToStr(Config->Machine));
590   E.add("/readonly");
591   E.add("/nologo");
592   E.add("/out:" + Twine(File.Path));
593
594   // We must create new files because the memory buffers we have may have no
595   // underlying file still existing on the disk.
596   // It happens if it was created from a TemporaryFile, which usually delete
597   // the file just after creating the MemoryBuffer.
598   std::vector<TemporaryFile> ResFiles;
599   ResFiles.reserve(MBs.size());
600   for (MemoryBufferRef MB : MBs) {
601     // We store the temporary file in a vector to avoid deletion
602     // before running cvtres
603     ResFiles.emplace_back("resource-file", "res");
604     TemporaryFile& ResFile = ResFiles.back();
605     // Write the content of the resource in a temporary file
606     std::error_code EC;
607     raw_fd_ostream OS(ResFile.Path, EC, sys::fs::F_None);
608     if (EC)
609       fatal(EC, "failed to open " + ResFile.Path);
610     OS << MB.getBuffer();
611     OS.close();
612
613     E.add(ResFile.Path);
614   }
615
616   E.run();
617   return File.getMemoryBuffer();
618 }
619
620 // Create OptTable
621
622 // Create prefix string literals used in Options.td
623 #define PREFIX(NAME, VALUE) const char *const NAME[] = VALUE;
624 #include "Options.inc"
625 #undef PREFIX
626
627 // Create table mapping all options defined in Options.td
628 static const llvm::opt::OptTable::Info infoTable[] = {
629 #define OPTION(X1, X2, ID, KIND, GROUP, ALIAS, X6, X7, X8, X9, X10)    \
630   {                                                                    \
631     X1, X2, X9, X10, OPT_##ID, llvm::opt::Option::KIND##Class, X8, X7, \
632     OPT_##GROUP, OPT_##ALIAS, X6                                       \
633   },
634 #include "Options.inc"
635 #undef OPTION
636 };
637
638 class COFFOptTable : public llvm::opt::OptTable {
639 public:
640   COFFOptTable() : OptTable(infoTable, true) {}
641 };
642
643 // Parses a given list of options.
644 opt::InputArgList ArgParser::parse(ArrayRef<const char *> ArgsArr) {
645   // First, replace respnose files (@<file>-style options).
646   std::vector<const char *> Argv = replaceResponseFiles(ArgsArr);
647
648   // Make InputArgList from string vectors.
649   COFFOptTable Table;
650   unsigned MissingIndex;
651   unsigned MissingCount;
652   opt::InputArgList Args = Table.ParseArgs(Argv, MissingIndex, MissingCount);
653
654   // Print the real command line if response files are expanded.
655   if (Args.hasArg(OPT_verbose) && ArgsArr.size() != Argv.size()) {
656     outs() << "Command line:";
657     for (const char *S : Argv)
658       outs() << " " << S;
659     outs() << "\n";
660   }
661
662   if (MissingCount)
663     fatal(Twine(Args.getArgString(MissingIndex)) + ": missing argument");
664   for (auto *Arg : Args.filtered(OPT_UNKNOWN))
665     errs() << "ignoring unknown argument: " << Arg->getSpelling() << "\n";
666   return Args;
667 }
668
669 // link.exe has an interesting feature. If LINK environment exists,
670 // its contents are handled as a command line string. So you can pass
671 // extra arguments using the environment variable.
672 opt::InputArgList ArgParser::parseLINK(ArrayRef<const char *> Args) {
673   // Concatenate LINK env and command line arguments, and then parse them.
674   Optional<std::string> Env = Process::GetEnv("LINK");
675   if (!Env)
676     return parse(Args);
677   std::vector<const char *> V = tokenize(*Env);
678   V.insert(V.end(), Args.begin(), Args.end());
679   return parse(V);
680 }
681
682 std::vector<const char *> ArgParser::tokenize(StringRef S) {
683   SmallVector<const char *, 16> Tokens;
684   cl::TokenizeWindowsCommandLine(S, Saver, Tokens);
685   return std::vector<const char *>(Tokens.begin(), Tokens.end());
686 }
687
688 // Creates a new command line by replacing options starting with '@'
689 // character. '@<filename>' is replaced by the file's contents.
690 std::vector<const char *>
691 ArgParser::replaceResponseFiles(std::vector<const char *> Argv) {
692   SmallVector<const char *, 256> Tokens(Argv.data(), Argv.data() + Argv.size());
693   ExpandResponseFiles(Saver, TokenizeWindowsCommandLine, Tokens);
694   return std::vector<const char *>(Tokens.begin(), Tokens.end());
695 }
696
697 void printHelp(const char *Argv0) {
698   COFFOptTable Table;
699   Table.PrintHelp(outs(), Argv0, "LLVM Linker", false);
700 }
701
702 } // namespace coff
703 } // namespace lld