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