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