1 //===- DriverUtils.cpp ----------------------------------------------------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 // This file contains utility functions for the driver. Because there
10 // are so many small functions, we created this separate file to make
11 // Driver.cpp less cluttered.
12 //
13 //===----------------------------------------------------------------------===//
14 
15 #include "Config.h"
16 #include "Driver.h"
17 #include "Symbols.h"
18 #include "lld/Common/ErrorHandler.h"
19 #include "lld/Common/Memory.h"
20 #include "llvm/ADT/Optional.h"
21 #include "llvm/ADT/StringSwitch.h"
22 #include "llvm/BinaryFormat/COFF.h"
23 #include "llvm/Object/COFF.h"
24 #include "llvm/Object/WindowsResource.h"
25 #include "llvm/Option/Arg.h"
26 #include "llvm/Option/ArgList.h"
27 #include "llvm/Option/Option.h"
28 #include "llvm/Support/CommandLine.h"
29 #include "llvm/Support/FileUtilities.h"
30 #include "llvm/Support/MathExtras.h"
31 #include "llvm/Support/Process.h"
32 #include "llvm/Support/Program.h"
33 #include "llvm/Support/raw_ostream.h"
34 #include "llvm/WindowsManifest/WindowsManifestMerger.h"
35 #include <limits>
36 #include <memory>
37 
38 using namespace llvm::COFF;
39 using namespace llvm;
40 using llvm::sys::Process;
41 
42 namespace lld {
43 namespace coff {
44 namespace {
45 
46 const uint16_t SUBLANG_ENGLISH_US = 0x0409;
47 const uint16_t RT_MANIFEST = 24;
48 
49 class Executor {
50 public:
51   explicit Executor(StringRef s) : prog(saver.save(s)) {}
52   void add(StringRef s) { args.push_back(saver.save(s)); }
53   void add(std::string &s) { args.push_back(saver.save(s)); }
54   void add(Twine s) { args.push_back(saver.save(s)); }
55   void add(const char *s) { args.push_back(saver.save(s)); }
56 
57   void run() {
58     ErrorOr<std::string> exeOrErr = sys::findProgramByName(prog);
59     if (auto ec = exeOrErr.getError())
60       fatal("unable to find " + prog + " in PATH: " + ec.message());
61     StringRef exe = saver.save(*exeOrErr);
62     args.insert(args.begin(), exe);
63 
64     if (sys::ExecuteAndWait(args[0], args) != 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 // Parses a string in the form of "<integer>[,<integer>]".
77 void parseNumbers(StringRef arg, uint64_t *addr, uint64_t *size) {
78   StringRef s1, s2;
79   std::tie(s1, s2) = arg.split(',');
80   if (s1.getAsInteger(0, *addr))
81     fatal("invalid number: " + s1);
82   if (size && !s2.empty() && s2.getAsInteger(0, *size))
83     fatal("invalid number: " + s2);
84 }
85 
86 // Parses a string in the form of "<integer>[.<integer>]".
87 // If second number is not present, Minor is set to 0.
88 void parseVersion(StringRef arg, uint32_t *major, uint32_t *minor) {
89   StringRef s1, s2;
90   std::tie(s1, s2) = arg.split('.');
91   if (s1.getAsInteger(10, *major))
92     fatal("invalid number: " + s1);
93   *minor = 0;
94   if (!s2.empty() && s2.getAsInteger(10, *minor))
95     fatal("invalid number: " + s2);
96 }
97 
98 void parseGuard(StringRef fullArg) {
99   SmallVector<StringRef, 1> splitArgs;
100   fullArg.split(splitArgs, ",");
101   for (StringRef arg : splitArgs) {
102     if (arg.equals_insensitive("no"))
103       config->guardCF = GuardCFLevel::Off;
104     else if (arg.equals_insensitive("nolongjmp"))
105       config->guardCF &= ~GuardCFLevel::LongJmp;
106     else if (arg.equals_insensitive("noehcont"))
107       config->guardCF &= ~GuardCFLevel::EHCont;
108     else if (arg.equals_insensitive("cf"))
109       config->guardCF = GuardCFLevel::CF;
110     else if (arg.equals_insensitive("longjmp"))
111       config->guardCF |= GuardCFLevel::CF | GuardCFLevel::LongJmp;
112     else if (arg.equals_insensitive("ehcont"))
113       config->guardCF |= GuardCFLevel::CF | GuardCFLevel::EHCont;
114     else
115       fatal("invalid argument to /guard: " + arg);
116   }
117 }
118 
119 // Parses a string in the form of "<subsystem>[,<integer>[.<integer>]]".
120 void parseSubsystem(StringRef arg, WindowsSubsystem *sys, uint32_t *major,
121                     uint32_t *minor, bool *gotVersion) {
122   StringRef sysStr, ver;
123   std::tie(sysStr, ver) = arg.split(',');
124   std::string sysStrLower = sysStr.lower();
125   *sys = StringSwitch<WindowsSubsystem>(sysStrLower)
126     .Case("boot_application", IMAGE_SUBSYSTEM_WINDOWS_BOOT_APPLICATION)
127     .Case("console", IMAGE_SUBSYSTEM_WINDOWS_CUI)
128     .Case("default", IMAGE_SUBSYSTEM_UNKNOWN)
129     .Case("efi_application", IMAGE_SUBSYSTEM_EFI_APPLICATION)
130     .Case("efi_boot_service_driver", IMAGE_SUBSYSTEM_EFI_BOOT_SERVICE_DRIVER)
131     .Case("efi_rom", IMAGE_SUBSYSTEM_EFI_ROM)
132     .Case("efi_runtime_driver", IMAGE_SUBSYSTEM_EFI_RUNTIME_DRIVER)
133     .Case("native", IMAGE_SUBSYSTEM_NATIVE)
134     .Case("posix", IMAGE_SUBSYSTEM_POSIX_CUI)
135     .Case("windows", IMAGE_SUBSYSTEM_WINDOWS_GUI)
136     .Default(IMAGE_SUBSYSTEM_UNKNOWN);
137   if (*sys == IMAGE_SUBSYSTEM_UNKNOWN && sysStrLower != "default")
138     fatal("unknown subsystem: " + sysStr);
139   if (!ver.empty())
140     parseVersion(ver, major, minor);
141   if (gotVersion)
142     *gotVersion = !ver.empty();
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   if (from == ".rsrc" || to == ".rsrc")
166     fatal("/merge: cannot merge '.rsrc' with any section");
167   if (from == ".reloc" || to == ".reloc")
168     fatal("/merge: cannot merge '.reloc' with any section");
169   auto pair = config->merge.insert(std::make_pair(from, to));
170   bool inserted = pair.second;
171   if (!inserted) {
172     StringRef existing = pair.first->second;
173     if (existing != to)
174       warn(s + ": already merged into " + existing);
175   }
176 }
177 
178 static uint32_t parseSectionAttributes(StringRef s) {
179   uint32_t ret = 0;
180   for (char c : s.lower()) {
181     switch (c) {
182     case 'd':
183       ret |= IMAGE_SCN_MEM_DISCARDABLE;
184       break;
185     case 'e':
186       ret |= IMAGE_SCN_MEM_EXECUTE;
187       break;
188     case 'k':
189       ret |= IMAGE_SCN_MEM_NOT_CACHED;
190       break;
191     case 'p':
192       ret |= IMAGE_SCN_MEM_NOT_PAGED;
193       break;
194     case 'r':
195       ret |= IMAGE_SCN_MEM_READ;
196       break;
197     case 's':
198       ret |= IMAGE_SCN_MEM_SHARED;
199       break;
200     case 'w':
201       ret |= IMAGE_SCN_MEM_WRITE;
202       break;
203     default:
204       fatal("/section: invalid argument: " + s);
205     }
206   }
207   return ret;
208 }
209 
210 // Parses /section option argument.
211 void parseSection(StringRef s) {
212   StringRef name, attrs;
213   std::tie(name, attrs) = s.split(',');
214   if (name.empty() || attrs.empty())
215     fatal("/section: invalid argument: " + s);
216   config->section[name] = parseSectionAttributes(attrs);
217 }
218 
219 // Parses /aligncomm option argument.
220 void parseAligncomm(StringRef s) {
221   StringRef name, align;
222   std::tie(name, align) = s.split(',');
223   if (name.empty() || align.empty()) {
224     error("/aligncomm: invalid argument: " + s);
225     return;
226   }
227   int v;
228   if (align.getAsInteger(0, v)) {
229     error("/aligncomm: invalid argument: " + s);
230     return;
231   }
232   config->alignComm[std::string(name)] =
233       std::max(config->alignComm[std::string(name)], 1 << v);
234 }
235 
236 // Parses /functionpadmin option argument.
237 void parseFunctionPadMin(llvm::opt::Arg *a, llvm::COFF::MachineTypes machine) {
238   StringRef arg = a->getNumValues() ? a->getValue() : "";
239   if (!arg.empty()) {
240     // Optional padding in bytes is given.
241     if (arg.getAsInteger(0, config->functionPadMin))
242       error("/functionpadmin: invalid argument: " + arg);
243     return;
244   }
245   // No optional argument given.
246   // Set default padding based on machine, similar to link.exe.
247   // There is no default padding for ARM platforms.
248   if (machine == I386) {
249     config->functionPadMin = 5;
250   } else if (machine == AMD64) {
251     config->functionPadMin = 6;
252   } else {
253     error("/functionpadmin: invalid argument for this machine: " + arg);
254   }
255 }
256 
257 // Parses a string in the form of "EMBED[,=<integer>]|NO".
258 // Results are directly written to Config.
259 void parseManifest(StringRef arg) {
260   if (arg.equals_insensitive("no")) {
261     config->manifest = Configuration::No;
262     return;
263   }
264   if (!arg.startswith_insensitive("embed"))
265     fatal("invalid option " + arg);
266   config->manifest = Configuration::Embed;
267   arg = arg.substr(strlen("embed"));
268   if (arg.empty())
269     return;
270   if (!arg.startswith_insensitive(",id="))
271     fatal("invalid option " + arg);
272   arg = arg.substr(strlen(",id="));
273   if (arg.getAsInteger(0, config->manifestID))
274     fatal("invalid option " + arg);
275 }
276 
277 // Parses a string in the form of "level=<string>|uiAccess=<string>|NO".
278 // Results are directly written to Config.
279 void parseManifestUAC(StringRef arg) {
280   if (arg.equals_insensitive("no")) {
281     config->manifestUAC = false;
282     return;
283   }
284   for (;;) {
285     arg = arg.ltrim();
286     if (arg.empty())
287       return;
288     if (arg.startswith_insensitive("level=")) {
289       arg = arg.substr(strlen("level="));
290       std::tie(config->manifestLevel, arg) = arg.split(" ");
291       continue;
292     }
293     if (arg.startswith_insensitive("uiaccess=")) {
294       arg = arg.substr(strlen("uiaccess="));
295       std::tie(config->manifestUIAccess, arg) = arg.split(" ");
296       continue;
297     }
298     fatal("invalid option " + arg);
299   }
300 }
301 
302 // Parses a string in the form of "cd|net[,(cd|net)]*"
303 // Results are directly written to Config.
304 void parseSwaprun(StringRef arg) {
305   do {
306     StringRef swaprun, newArg;
307     std::tie(swaprun, newArg) = arg.split(',');
308     if (swaprun.equals_insensitive("cd"))
309       config->swaprunCD = true;
310     else if (swaprun.equals_insensitive("net"))
311       config->swaprunNet = true;
312     else if (swaprun.empty())
313       error("/swaprun: missing argument");
314     else
315       error("/swaprun: invalid argument: " + swaprun);
316     // To catch trailing commas, e.g. `/spawrun:cd,`
317     if (newArg.empty() && arg.endswith(","))
318       error("/swaprun: missing argument");
319     arg = newArg;
320   } while (!arg.empty());
321 }
322 
323 // An RAII temporary file class that automatically removes a temporary file.
324 namespace {
325 class TemporaryFile {
326 public:
327   TemporaryFile(StringRef prefix, StringRef extn, StringRef contents = "") {
328     SmallString<128> s;
329     if (auto ec = sys::fs::createTemporaryFile("lld-" + prefix, extn, s))
330       fatal("cannot create a temporary file: " + ec.message());
331     path = std::string(s.str());
332 
333     if (!contents.empty()) {
334       std::error_code ec;
335       raw_fd_ostream os(path, ec, sys::fs::OF_None);
336       if (ec)
337         fatal("failed to open " + path + ": " + ec.message());
338       os << contents;
339     }
340   }
341 
342   TemporaryFile(TemporaryFile &&obj) {
343     std::swap(path, obj.path);
344   }
345 
346   ~TemporaryFile() {
347     if (path.empty())
348       return;
349     if (sys::fs::remove(path))
350       fatal("failed to remove " + path);
351   }
352 
353   // Returns a memory buffer of this temporary file.
354   // Note that this function does not leave the file open,
355   // so it is safe to remove the file immediately after this function
356   // is called (you cannot remove an opened file on Windows.)
357   std::unique_ptr<MemoryBuffer> getMemoryBuffer() {
358     // IsVolatile=true forces MemoryBuffer to not use mmap().
359     return CHECK(MemoryBuffer::getFile(path, /*IsText=*/false,
360                                        /*RequiresNullTerminator=*/false,
361                                        /*IsVolatile=*/true),
362                  "could not open " + path);
363   }
364 
365   std::string path;
366 };
367 }
368 
369 static std::string createDefaultXml() {
370   std::string ret;
371   raw_string_ostream os(ret);
372 
373   // Emit the XML. Note that we do *not* verify that the XML attributes are
374   // syntactically correct. This is intentional for link.exe compatibility.
375   os << "<?xml version=\"1.0\" standalone=\"yes\"?>\n"
376      << "<assembly xmlns=\"urn:schemas-microsoft-com:asm.v1\"\n"
377      << "          manifestVersion=\"1.0\">\n";
378   if (config->manifestUAC) {
379     os << "  <trustInfo>\n"
380        << "    <security>\n"
381        << "      <requestedPrivileges>\n"
382        << "         <requestedExecutionLevel level=" << config->manifestLevel
383        << " uiAccess=" << config->manifestUIAccess << "/>\n"
384        << "      </requestedPrivileges>\n"
385        << "    </security>\n"
386        << "  </trustInfo>\n";
387   }
388   for (auto manifestDependency : config->manifestDependencies) {
389     os << "  <dependency>\n"
390        << "    <dependentAssembly>\n"
391        << "      <assemblyIdentity " << manifestDependency << " />\n"
392        << "    </dependentAssembly>\n"
393        << "  </dependency>\n";
394   }
395   os << "</assembly>\n";
396   return os.str();
397 }
398 
399 static std::string createManifestXmlWithInternalMt(StringRef defaultXml) {
400   std::unique_ptr<MemoryBuffer> defaultXmlCopy =
401       MemoryBuffer::getMemBufferCopy(defaultXml);
402 
403   windows_manifest::WindowsManifestMerger merger;
404   if (auto e = merger.merge(*defaultXmlCopy.get()))
405     fatal("internal manifest tool failed on default xml: " +
406           toString(std::move(e)));
407 
408   for (StringRef filename : config->manifestInput) {
409     std::unique_ptr<MemoryBuffer> manifest =
410         check(MemoryBuffer::getFile(filename));
411     // Call takeBuffer to include in /reproduce: output if applicable.
412     if (auto e = merger.merge(driver->takeBuffer(std::move(manifest))))
413       fatal("internal manifest tool failed on file " + filename + ": " +
414             toString(std::move(e)));
415   }
416 
417   return std::string(merger.getMergedManifest().get()->getBuffer());
418 }
419 
420 static std::string createManifestXmlWithExternalMt(StringRef defaultXml) {
421   // Create the default manifest file as a temporary file.
422   TemporaryFile Default("defaultxml", "manifest");
423   std::error_code ec;
424   raw_fd_ostream os(Default.path, ec, sys::fs::OF_TextWithCRLF);
425   if (ec)
426     fatal("failed to open " + Default.path + ": " + ec.message());
427   os << defaultXml;
428   os.close();
429 
430   // Merge user-supplied manifests if they are given.  Since libxml2 is not
431   // enabled, we must shell out to Microsoft's mt.exe tool.
432   TemporaryFile user("user", "manifest");
433 
434   Executor e("mt.exe");
435   e.add("/manifest");
436   e.add(Default.path);
437   for (StringRef filename : config->manifestInput) {
438     e.add("/manifest");
439     e.add(filename);
440 
441     // Manually add the file to the /reproduce: tar if needed.
442     if (driver->tar)
443       if (auto mbOrErr = MemoryBuffer::getFile(filename))
444         driver->takeBuffer(std::move(*mbOrErr));
445   }
446   e.add("/nologo");
447   e.add("/out:" + StringRef(user.path));
448   e.run();
449 
450   return std::string(
451       CHECK(MemoryBuffer::getFile(user.path), "could not open " + user.path)
452           .get()
453           ->getBuffer());
454 }
455 
456 static std::string createManifestXml() {
457   std::string defaultXml = createDefaultXml();
458   if (config->manifestInput.empty())
459     return defaultXml;
460 
461   if (windows_manifest::isAvailable())
462     return createManifestXmlWithInternalMt(defaultXml);
463 
464   return createManifestXmlWithExternalMt(defaultXml);
465 }
466 
467 static std::unique_ptr<WritableMemoryBuffer>
468 createMemoryBufferForManifestRes(size_t manifestSize) {
469   size_t resSize = alignTo(
470       object::WIN_RES_MAGIC_SIZE + object::WIN_RES_NULL_ENTRY_SIZE +
471           sizeof(object::WinResHeaderPrefix) + sizeof(object::WinResIDs) +
472           sizeof(object::WinResHeaderSuffix) + manifestSize,
473       object::WIN_RES_DATA_ALIGNMENT);
474   return WritableMemoryBuffer::getNewMemBuffer(resSize, config->outputFile +
475                                                             ".manifest.res");
476 }
477 
478 static void writeResFileHeader(char *&buf) {
479   memcpy(buf, COFF::WinResMagic, sizeof(COFF::WinResMagic));
480   buf += sizeof(COFF::WinResMagic);
481   memset(buf, 0, object::WIN_RES_NULL_ENTRY_SIZE);
482   buf += object::WIN_RES_NULL_ENTRY_SIZE;
483 }
484 
485 static void writeResEntryHeader(char *&buf, size_t manifestSize) {
486   // Write the prefix.
487   auto *prefix = reinterpret_cast<object::WinResHeaderPrefix *>(buf);
488   prefix->DataSize = manifestSize;
489   prefix->HeaderSize = sizeof(object::WinResHeaderPrefix) +
490                        sizeof(object::WinResIDs) +
491                        sizeof(object::WinResHeaderSuffix);
492   buf += sizeof(object::WinResHeaderPrefix);
493 
494   // Write the Type/Name IDs.
495   auto *iDs = reinterpret_cast<object::WinResIDs *>(buf);
496   iDs->setType(RT_MANIFEST);
497   iDs->setName(config->manifestID);
498   buf += sizeof(object::WinResIDs);
499 
500   // Write the suffix.
501   auto *suffix = reinterpret_cast<object::WinResHeaderSuffix *>(buf);
502   suffix->DataVersion = 0;
503   suffix->MemoryFlags = object::WIN_RES_PURE_MOVEABLE;
504   suffix->Language = SUBLANG_ENGLISH_US;
505   suffix->Version = 0;
506   suffix->Characteristics = 0;
507   buf += sizeof(object::WinResHeaderSuffix);
508 }
509 
510 // Create a resource file containing a manifest XML.
511 std::unique_ptr<MemoryBuffer> createManifestRes() {
512   std::string manifest = createManifestXml();
513 
514   std::unique_ptr<WritableMemoryBuffer> res =
515       createMemoryBufferForManifestRes(manifest.size());
516 
517   char *buf = res->getBufferStart();
518   writeResFileHeader(buf);
519   writeResEntryHeader(buf, manifest.size());
520 
521   // Copy the manifest data into the .res file.
522   std::copy(manifest.begin(), manifest.end(), buf);
523   return std::move(res);
524 }
525 
526 void createSideBySideManifest() {
527   std::string path = std::string(config->manifestFile);
528   if (path == "")
529     path = config->outputFile + ".manifest";
530   std::error_code ec;
531   raw_fd_ostream out(path, ec, sys::fs::OF_TextWithCRLF);
532   if (ec)
533     fatal("failed to create manifest: " + ec.message());
534   out << createManifestXml();
535 }
536 
537 // Parse a string in the form of
538 // "<name>[=<internalname>][,@ordinal[,NONAME]][,DATA][,PRIVATE]"
539 // or "<name>=<dllname>.<name>".
540 // Used for parsing /export arguments.
541 Export parseExport(StringRef arg) {
542   Export e;
543   StringRef rest;
544   std::tie(e.name, rest) = arg.split(",");
545   if (e.name.empty())
546     goto err;
547 
548   if (e.name.contains('=')) {
549     StringRef x, y;
550     std::tie(x, y) = e.name.split("=");
551 
552     // If "<name>=<dllname>.<name>".
553     if (y.contains(".")) {
554       e.name = x;
555       e.forwardTo = y;
556       return e;
557     }
558 
559     e.extName = x;
560     e.name = y;
561     if (e.name.empty())
562       goto err;
563   }
564 
565   // If "<name>=<internalname>[,@ordinal[,NONAME]][,DATA][,PRIVATE]"
566   while (!rest.empty()) {
567     StringRef tok;
568     std::tie(tok, rest) = rest.split(",");
569     if (tok.equals_insensitive("noname")) {
570       if (e.ordinal == 0)
571         goto err;
572       e.noname = true;
573       continue;
574     }
575     if (tok.equals_insensitive("data")) {
576       e.data = true;
577       continue;
578     }
579     if (tok.equals_insensitive("constant")) {
580       e.constant = true;
581       continue;
582     }
583     if (tok.equals_insensitive("private")) {
584       e.isPrivate = true;
585       continue;
586     }
587     if (tok.startswith("@")) {
588       int32_t ord;
589       if (tok.substr(1).getAsInteger(0, ord))
590         goto err;
591       if (ord <= 0 || 65535 < ord)
592         goto err;
593       e.ordinal = ord;
594       continue;
595     }
596     goto err;
597   }
598   return e;
599 
600 err:
601   fatal("invalid /export: " + arg);
602 }
603 
604 static StringRef undecorate(StringRef sym) {
605   if (config->machine != I386)
606     return sym;
607   // In MSVC mode, a fully decorated stdcall function is exported
608   // as-is with the leading underscore (with type IMPORT_NAME).
609   // In MinGW mode, a decorated stdcall function gets the underscore
610   // removed, just like normal cdecl functions.
611   if (sym.startswith("_") && sym.contains('@') && !config->mingw)
612     return sym;
613   return sym.startswith("_") ? sym.substr(1) : sym;
614 }
615 
616 // Convert stdcall/fastcall style symbols into unsuffixed symbols,
617 // with or without a leading underscore. (MinGW specific.)
618 static StringRef killAt(StringRef sym, bool prefix) {
619   if (sym.empty())
620     return sym;
621   // Strip any trailing stdcall suffix
622   sym = sym.substr(0, sym.find('@', 1));
623   if (!sym.startswith("@")) {
624     if (prefix && !sym.startswith("_"))
625       return saver.save("_" + sym);
626     return sym;
627   }
628   // For fastcall, remove the leading @ and replace it with an
629   // underscore, if prefixes are used.
630   sym = sym.substr(1);
631   if (prefix)
632     sym = saver.save("_" + sym);
633   return sym;
634 }
635 
636 // Performs error checking on all /export arguments.
637 // It also sets ordinals.
638 void fixupExports() {
639   // Symbol ordinals must be unique.
640   std::set<uint16_t> ords;
641   for (Export &e : config->exports) {
642     if (e.ordinal == 0)
643       continue;
644     if (!ords.insert(e.ordinal).second)
645       fatal("duplicate export ordinal: " + e.name);
646   }
647 
648   for (Export &e : config->exports) {
649     if (!e.forwardTo.empty()) {
650       e.exportName = undecorate(e.name);
651     } else {
652       e.exportName = undecorate(e.extName.empty() ? e.name : e.extName);
653     }
654   }
655 
656   if (config->killAt && config->machine == I386) {
657     for (Export &e : config->exports) {
658       e.name = killAt(e.name, true);
659       e.exportName = killAt(e.exportName, false);
660       e.extName = killAt(e.extName, true);
661       e.symbolName = killAt(e.symbolName, true);
662     }
663   }
664 
665   // Uniquefy by name.
666   DenseMap<StringRef, Export *> map(config->exports.size());
667   std::vector<Export> v;
668   for (Export &e : config->exports) {
669     auto pair = map.insert(std::make_pair(e.exportName, &e));
670     bool inserted = pair.second;
671     if (inserted) {
672       v.push_back(e);
673       continue;
674     }
675     Export *existing = pair.first->second;
676     if (e == *existing || e.name != existing->name)
677       continue;
678     warn("duplicate /export option: " + e.name);
679   }
680   config->exports = std::move(v);
681 
682   // Sort by name.
683   std::sort(config->exports.begin(), config->exports.end(),
684             [](const Export &a, const Export &b) {
685               return a.exportName < b.exportName;
686             });
687 }
688 
689 void assignExportOrdinals() {
690   // Assign unique ordinals if default (= 0).
691   uint32_t max = 0;
692   for (Export &e : config->exports)
693     max = std::max(max, (uint32_t)e.ordinal);
694   for (Export &e : config->exports)
695     if (e.ordinal == 0)
696       e.ordinal = ++max;
697   if (max > std::numeric_limits<uint16_t>::max())
698     fatal("too many exported symbols (max " +
699           Twine(std::numeric_limits<uint16_t>::max()) + ")");
700 }
701 
702 // Parses a string in the form of "key=value" and check
703 // if value matches previous values for the same key.
704 void checkFailIfMismatch(StringRef arg, InputFile *source) {
705   StringRef k, v;
706   std::tie(k, v) = arg.split('=');
707   if (k.empty() || v.empty())
708     fatal("/failifmismatch: invalid argument: " + arg);
709   std::pair<StringRef, InputFile *> existing = config->mustMatch[k];
710   if (!existing.first.empty() && v != existing.first) {
711     std::string sourceStr = source ? toString(source) : "cmd-line";
712     std::string existingStr =
713         existing.second ? toString(existing.second) : "cmd-line";
714     fatal("/failifmismatch: mismatch detected for '" + k + "':\n>>> " +
715           existingStr + " has value " + existing.first + "\n>>> " + sourceStr +
716           " has value " + v);
717   }
718   config->mustMatch[k] = {v, source};
719 }
720 
721 // Convert Windows resource files (.res files) to a .obj file.
722 // Does what cvtres.exe does, but in-process and cross-platform.
723 MemoryBufferRef convertResToCOFF(ArrayRef<MemoryBufferRef> mbs,
724                                  ArrayRef<ObjFile *> objs) {
725   object::WindowsResourceParser parser(/* MinGW */ config->mingw);
726 
727   std::vector<std::string> duplicates;
728   for (MemoryBufferRef mb : mbs) {
729     std::unique_ptr<object::Binary> bin = check(object::createBinary(mb));
730     object::WindowsResource *rf = dyn_cast<object::WindowsResource>(bin.get());
731     if (!rf)
732       fatal("cannot compile non-resource file as resource");
733 
734     if (auto ec = parser.parse(rf, duplicates))
735       fatal(toString(std::move(ec)));
736   }
737 
738   // Note: This processes all .res files before all objs. Ideally they'd be
739   // handled in the same order they were linked (to keep the right one, if
740   // there are duplicates that are tolerated due to forceMultipleRes).
741   for (ObjFile *f : objs) {
742     object::ResourceSectionRef rsf;
743     if (auto ec = rsf.load(f->getCOFFObj()))
744       fatal(toString(f) + ": " + toString(std::move(ec)));
745 
746     if (auto ec = parser.parse(rsf, f->getName(), duplicates))
747       fatal(toString(std::move(ec)));
748   }
749 
750   if (config->mingw)
751     parser.cleanUpManifests(duplicates);
752 
753   for (const auto &dupeDiag : duplicates)
754     if (config->forceMultipleRes)
755       warn(dupeDiag);
756     else
757       error(dupeDiag);
758 
759   Expected<std::unique_ptr<MemoryBuffer>> e =
760       llvm::object::writeWindowsResourceCOFF(config->machine, parser,
761                                              config->timestamp);
762   if (!e)
763     fatal("failed to write .res to COFF: " + toString(e.takeError()));
764 
765   MemoryBufferRef mbref = **e;
766   make<std::unique_ptr<MemoryBuffer>>(std::move(*e)); // take ownership
767   return mbref;
768 }
769 
770 // Create OptTable
771 
772 // Create prefix string literals used in Options.td
773 #define PREFIX(NAME, VALUE) const char *const NAME[] = VALUE;
774 #include "Options.inc"
775 #undef PREFIX
776 
777 // Create table mapping all options defined in Options.td
778 static const llvm::opt::OptTable::Info infoTable[] = {
779 #define OPTION(X1, X2, ID, KIND, GROUP, ALIAS, X7, X8, X9, X10, X11, X12)      \
780   {X1, X2, X10,         X11,         OPT_##ID, llvm::opt::Option::KIND##Class, \
781    X9, X8, OPT_##GROUP, OPT_##ALIAS, X7,       X12},
782 #include "Options.inc"
783 #undef OPTION
784 };
785 
786 COFFOptTable::COFFOptTable() : OptTable(infoTable, true) {}
787 
788 COFFOptTable optTable;
789 
790 // Set color diagnostics according to --color-diagnostics={auto,always,never}
791 // or --no-color-diagnostics flags.
792 static void handleColorDiagnostics(opt::InputArgList &args) {
793   auto *arg = args.getLastArg(OPT_color_diagnostics, OPT_color_diagnostics_eq,
794                               OPT_no_color_diagnostics);
795   if (!arg)
796     return;
797   if (arg->getOption().getID() == OPT_color_diagnostics) {
798     lld::errs().enable_colors(true);
799   } else if (arg->getOption().getID() == OPT_no_color_diagnostics) {
800     lld::errs().enable_colors(false);
801   } else {
802     StringRef s = arg->getValue();
803     if (s == "always")
804       lld::errs().enable_colors(true);
805     else if (s == "never")
806       lld::errs().enable_colors(false);
807     else if (s != "auto")
808       error("unknown option: --color-diagnostics=" + s);
809   }
810 }
811 
812 static cl::TokenizerCallback getQuotingStyle(opt::InputArgList &args) {
813   if (auto *arg = args.getLastArg(OPT_rsp_quoting)) {
814     StringRef s = arg->getValue();
815     if (s != "windows" && s != "posix")
816       error("invalid response file quoting: " + s);
817     if (s == "windows")
818       return cl::TokenizeWindowsCommandLine;
819     return cl::TokenizeGNUCommandLine;
820   }
821   // The COFF linker always defaults to Windows quoting.
822   return cl::TokenizeWindowsCommandLine;
823 }
824 
825 // Parses a given list of options.
826 opt::InputArgList ArgParser::parse(ArrayRef<const char *> argv) {
827   // Make InputArgList from string vectors.
828   unsigned missingIndex;
829   unsigned missingCount;
830 
831   // We need to get the quoting style for response files before parsing all
832   // options so we parse here before and ignore all the options but
833   // --rsp-quoting and /lldignoreenv.
834   // (This means --rsp-quoting can't be added through %LINK%.)
835   opt::InputArgList args = optTable.ParseArgs(argv, missingIndex, missingCount);
836 
837   // Expand response files (arguments in the form of @<filename>) and insert
838   // flags from %LINK% and %_LINK_%, and then parse the argument again.
839   SmallVector<const char *, 256> expandedArgv(argv.data(),
840                                               argv.data() + argv.size());
841   if (!args.hasArg(OPT_lldignoreenv))
842     addLINK(expandedArgv);
843   cl::ExpandResponseFiles(saver, getQuotingStyle(args), expandedArgv);
844   args = optTable.ParseArgs(makeArrayRef(expandedArgv).drop_front(),
845                             missingIndex, missingCount);
846 
847   // Print the real command line if response files are expanded.
848   if (args.hasArg(OPT_verbose) && argv.size() != expandedArgv.size()) {
849     std::string msg = "Command line:";
850     for (const char *s : expandedArgv)
851       msg += " " + std::string(s);
852     message(msg);
853   }
854 
855   // Save the command line after response file expansion so we can write it to
856   // the PDB if necessary.
857   config->argv = {expandedArgv.begin(), expandedArgv.end()};
858 
859   // Handle /WX early since it converts missing argument warnings to errors.
860   errorHandler().fatalWarnings = args.hasFlag(OPT_WX, OPT_WX_no, false);
861 
862   if (missingCount)
863     fatal(Twine(args.getArgString(missingIndex)) + ": missing argument");
864 
865   handleColorDiagnostics(args);
866 
867   for (opt::Arg *arg : args.filtered(OPT_UNKNOWN)) {
868     std::string nearest;
869     if (optTable.findNearest(arg->getAsString(args), nearest) > 1)
870       warn("ignoring unknown argument '" + arg->getAsString(args) + "'");
871     else
872       warn("ignoring unknown argument '" + arg->getAsString(args) +
873            "', did you mean '" + nearest + "'");
874   }
875 
876   if (args.hasArg(OPT_lib))
877     warn("ignoring /lib since it's not the first argument");
878 
879   return args;
880 }
881 
882 // Tokenizes and parses a given string as command line in .drective section.
883 ParsedDirectives ArgParser::parseDirectives(StringRef s) {
884   ParsedDirectives result;
885   SmallVector<const char *, 16> rest;
886 
887   // Handle /EXPORT and /INCLUDE in a fast path. These directives can appear for
888   // potentially every symbol in the object, so they must be handled quickly.
889   SmallVector<StringRef, 16> tokens;
890   cl::TokenizeWindowsCommandLineNoCopy(s, saver, tokens);
891   for (StringRef tok : tokens) {
892     if (tok.startswith_insensitive("/export:") ||
893         tok.startswith_insensitive("-export:"))
894       result.exports.push_back(tok.substr(strlen("/export:")));
895     else if (tok.startswith_insensitive("/include:") ||
896              tok.startswith_insensitive("-include:"))
897       result.includes.push_back(tok.substr(strlen("/include:")));
898     else {
899       // Copy substrings that are not valid C strings. The tokenizer may have
900       // already copied quoted arguments for us, so those do not need to be
901       // copied again.
902       bool HasNul = tok.end() != s.end() && tok.data()[tok.size()] == '\0';
903       rest.push_back(HasNul ? tok.data() : saver.save(tok).data());
904     }
905   }
906 
907   // Make InputArgList from unparsed string vectors.
908   unsigned missingIndex;
909   unsigned missingCount;
910 
911   result.args = optTable.ParseArgs(rest, missingIndex, missingCount);
912 
913   if (missingCount)
914     fatal(Twine(result.args.getArgString(missingIndex)) + ": missing argument");
915   for (auto *arg : result.args.filtered(OPT_UNKNOWN))
916     warn("ignoring unknown argument: " + arg->getAsString(result.args));
917   return result;
918 }
919 
920 // link.exe has an interesting feature. If LINK or _LINK_ environment
921 // variables exist, their contents are handled as command line strings.
922 // So you can pass extra arguments using them.
923 void ArgParser::addLINK(SmallVector<const char *, 256> &argv) {
924   // Concatenate LINK env and command line arguments, and then parse them.
925   if (Optional<std::string> s = Process::GetEnv("LINK")) {
926     std::vector<const char *> v = tokenize(*s);
927     argv.insert(std::next(argv.begin()), v.begin(), v.end());
928   }
929   if (Optional<std::string> s = Process::GetEnv("_LINK_")) {
930     std::vector<const char *> v = tokenize(*s);
931     argv.insert(std::next(argv.begin()), v.begin(), v.end());
932   }
933 }
934 
935 std::vector<const char *> ArgParser::tokenize(StringRef s) {
936   SmallVector<const char *, 16> tokens;
937   cl::TokenizeWindowsCommandLine(s, saver, tokens);
938   return std::vector<const char *>(tokens.begin(), tokens.end());
939 }
940 
941 void printHelp(const char *argv0) {
942   optTable.printHelp(lld::outs(),
943                      (std::string(argv0) + " [options] file...").c_str(),
944                      "LLVM Linker", false);
945 }
946 
947 } // namespace coff
948 } // namespace lld
949