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