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