Run DCE after a LoopFlatten test to reduce spurious output [nfc]
[llvm-project.git] / lld / COFF / DriverUtils.cpp
blob583f6af070b62580890537bfd728d7bb65110653
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.
13 //===----------------------------------------------------------------------===//
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>
41 using namespace llvm::COFF;
42 using namespace llvm::opt;
43 using namespace llvm;
44 using llvm::sys::Process;
46 namespace lld {
47 namespace coff {
48 namespace {
50 const uint16_t SUBLANG_ENGLISH_US = 0x0409;
51 const uint16_t RT_MANIFEST = 24;
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)); }
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);
68 if (sys::ExecuteAndWait(args[0], args) != 0)
69 fatal("ExecuteAndWait failed: " +
70 llvm::join(args.begin(), args.end(), " "));
73 private:
74 StringRef prog;
75 std::vector<StringRef> args;
78 } // anonymous namespace
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);
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);
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);
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();
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));
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);
177 void LinkerDriver::parsePDBPageSize(StringRef s) {
178 int v;
179 if (s.getAsInteger(0, v)) {
180 error("/pdbpagesize: invalid argument: " + s);
181 return;
183 if (v != 4096 && v != 8192 && v != 16384 && v != 32768) {
184 error("/pdbpagesize: invalid argument: " + s);
185 return;
188 ctx.config.pdbPageSize = v;
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);
220 return ret;
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);
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;
238 int v;
239 if (align.getAsInteger(0, v)) {
240 error("/aligncomm: invalid argument: " + s);
241 return;
243 ctx.config.alignComm[std::string(name)] =
244 std::max(ctx.config.alignComm[std::string(name)], 1 << v);
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;
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);
268 // Parses a string in the form of "EMBED[,=<integer>]|NO".
269 // Results are directly written to
270 // Config.
271 void LinkerDriver::parseManifest(StringRef arg) {
272 if (arg.equals_insensitive("no")) {
273 ctx.config.manifest = Configuration::No;
274 return;
276 if (!arg.starts_with_insensitive("embed"))
277 fatal("invalid option " + arg);
278 ctx.config.manifest = Configuration::Embed;
279 arg = arg.substr(strlen("embed"));
280 if (arg.empty())
281 return;
282 if (!arg.starts_with_insensitive(",id="))
283 fatal("invalid option " + arg);
284 arg = arg.substr(strlen(",id="));
285 if (arg.getAsInteger(0, ctx.config.manifestID))
286 fatal("invalid option " + arg);
289 // Parses a string in the form of "level=<string>|uiAccess=<string>|NO".
290 // Results are directly written to Config.
291 void LinkerDriver::parseManifestUAC(StringRef arg) {
292 if (arg.equals_insensitive("no")) {
293 ctx.config.manifestUAC = false;
294 return;
296 for (;;) {
297 arg = arg.ltrim();
298 if (arg.empty())
299 return;
300 if (arg.starts_with_insensitive("level=")) {
301 arg = arg.substr(strlen("level="));
302 std::tie(ctx.config.manifestLevel, arg) = arg.split(" ");
303 continue;
305 if (arg.starts_with_insensitive("uiaccess=")) {
306 arg = arg.substr(strlen("uiaccess="));
307 std::tie(ctx.config.manifestUIAccess, arg) = arg.split(" ");
308 continue;
310 fatal("invalid option " + arg);
314 // Parses a string in the form of "cd|net[,(cd|net)]*"
315 // Results are directly written to Config.
316 void LinkerDriver::parseSwaprun(StringRef arg) {
317 do {
318 auto [swaprun, newArg] = arg.split(',');
319 if (swaprun.equals_insensitive("cd"))
320 ctx.config.swaprunCD = true;
321 else if (swaprun.equals_insensitive("net"))
322 ctx.config.swaprunNet = true;
323 else if (swaprun.empty())
324 error("/swaprun: missing argument");
325 else
326 error("/swaprun: invalid argument: " + swaprun);
327 // To catch trailing commas, e.g. `/spawrun:cd,`
328 if (newArg.empty() && arg.ends_with(","))
329 error("/swaprun: missing argument");
330 arg = newArg;
331 } while (!arg.empty());
334 // An RAII temporary file class that automatically removes a temporary file.
335 namespace {
336 class TemporaryFile {
337 public:
338 TemporaryFile(StringRef prefix, StringRef extn, StringRef contents = "") {
339 SmallString<128> s;
340 if (auto ec = sys::fs::createTemporaryFile("lld-" + prefix, extn, s))
341 fatal("cannot create a temporary file: " + ec.message());
342 path = std::string(s.str());
344 if (!contents.empty()) {
345 std::error_code ec;
346 raw_fd_ostream os(path, ec, sys::fs::OF_None);
347 if (ec)
348 fatal("failed to open " + path + ": " + ec.message());
349 os << contents;
353 TemporaryFile(TemporaryFile &&obj) noexcept { std::swap(path, obj.path); }
355 ~TemporaryFile() {
356 if (path.empty())
357 return;
358 if (sys::fs::remove(path))
359 fatal("failed to remove " + path);
362 // Returns a memory buffer of this temporary file.
363 // Note that this function does not leave the file open,
364 // so it is safe to remove the file immediately after this function
365 // is called (you cannot remove an opened file on Windows.)
366 std::unique_ptr<MemoryBuffer> getMemoryBuffer() {
367 // IsVolatile=true forces MemoryBuffer to not use mmap().
368 return CHECK(MemoryBuffer::getFile(path, /*IsText=*/false,
369 /*RequiresNullTerminator=*/false,
370 /*IsVolatile=*/true),
371 "could not open " + path);
374 std::string path;
378 std::string LinkerDriver::createDefaultXml() {
379 std::string ret;
380 raw_string_ostream os(ret);
382 // Emit the XML. Note that we do *not* verify that the XML attributes are
383 // syntactically correct. This is intentional for link.exe compatibility.
384 os << "<?xml version=\"1.0\" standalone=\"yes\"?>\n"
385 << "<assembly xmlns=\"urn:schemas-microsoft-com:asm.v1\"\n"
386 << " manifestVersion=\"1.0\">\n";
387 if (ctx.config.manifestUAC) {
388 os << " <trustInfo>\n"
389 << " <security>\n"
390 << " <requestedPrivileges>\n"
391 << " <requestedExecutionLevel level=" << ctx.config.manifestLevel
392 << " uiAccess=" << ctx.config.manifestUIAccess << "/>\n"
393 << " </requestedPrivileges>\n"
394 << " </security>\n"
395 << " </trustInfo>\n";
397 for (auto manifestDependency : ctx.config.manifestDependencies) {
398 os << " <dependency>\n"
399 << " <dependentAssembly>\n"
400 << " <assemblyIdentity " << manifestDependency << " />\n"
401 << " </dependentAssembly>\n"
402 << " </dependency>\n";
404 os << "</assembly>\n";
405 return os.str();
408 std::string
409 LinkerDriver::createManifestXmlWithInternalMt(StringRef defaultXml) {
410 std::unique_ptr<MemoryBuffer> defaultXmlCopy =
411 MemoryBuffer::getMemBufferCopy(defaultXml);
413 windows_manifest::WindowsManifestMerger merger;
414 if (auto e = merger.merge(*defaultXmlCopy.get()))
415 fatal("internal manifest tool failed on default xml: " +
416 toString(std::move(e)));
418 for (StringRef filename : ctx.config.manifestInput) {
419 std::unique_ptr<MemoryBuffer> manifest =
420 check(MemoryBuffer::getFile(filename));
421 // Call takeBuffer to include in /reproduce: output if applicable.
422 if (auto e = merger.merge(takeBuffer(std::move(manifest))))
423 fatal("internal manifest tool failed on file " + filename + ": " +
424 toString(std::move(e)));
427 return std::string(merger.getMergedManifest().get()->getBuffer());
430 std::string
431 LinkerDriver::createManifestXmlWithExternalMt(StringRef defaultXml) {
432 // Create the default manifest file as a temporary file.
433 TemporaryFile Default("defaultxml", "manifest");
434 std::error_code ec;
435 raw_fd_ostream os(Default.path, ec, sys::fs::OF_TextWithCRLF);
436 if (ec)
437 fatal("failed to open " + Default.path + ": " + ec.message());
438 os << defaultXml;
439 os.close();
441 // Merge user-supplied manifests if they are given. Since libxml2 is not
442 // enabled, we must shell out to Microsoft's mt.exe tool.
443 TemporaryFile user("user", "manifest");
445 Executor e("mt.exe");
446 e.add("/manifest");
447 e.add(Default.path);
448 for (StringRef filename : ctx.config.manifestInput) {
449 e.add("/manifest");
450 e.add(filename);
452 // Manually add the file to the /reproduce: tar if needed.
453 if (tar)
454 if (auto mbOrErr = MemoryBuffer::getFile(filename))
455 takeBuffer(std::move(*mbOrErr));
457 e.add("/nologo");
458 e.add("/out:" + StringRef(user.path));
459 e.run();
461 return std::string(
462 CHECK(MemoryBuffer::getFile(user.path), "could not open " + user.path)
463 .get()
464 ->getBuffer());
467 std::string LinkerDriver::createManifestXml() {
468 std::string defaultXml = createDefaultXml();
469 if (ctx.config.manifestInput.empty())
470 return defaultXml;
472 if (windows_manifest::isAvailable())
473 return createManifestXmlWithInternalMt(defaultXml);
475 return createManifestXmlWithExternalMt(defaultXml);
478 std::unique_ptr<WritableMemoryBuffer>
479 LinkerDriver::createMemoryBufferForManifestRes(size_t manifestSize) {
480 size_t resSize = alignTo(
481 object::WIN_RES_MAGIC_SIZE + object::WIN_RES_NULL_ENTRY_SIZE +
482 sizeof(object::WinResHeaderPrefix) + sizeof(object::WinResIDs) +
483 sizeof(object::WinResHeaderSuffix) + manifestSize,
484 object::WIN_RES_DATA_ALIGNMENT);
485 return WritableMemoryBuffer::getNewMemBuffer(resSize, ctx.config.outputFile +
486 ".manifest.res");
489 static void writeResFileHeader(char *&buf) {
490 memcpy(buf, COFF::WinResMagic, sizeof(COFF::WinResMagic));
491 buf += sizeof(COFF::WinResMagic);
492 memset(buf, 0, object::WIN_RES_NULL_ENTRY_SIZE);
493 buf += object::WIN_RES_NULL_ENTRY_SIZE;
496 static void writeResEntryHeader(char *&buf, size_t manifestSize,
497 int manifestID) {
498 // Write the prefix.
499 auto *prefix = reinterpret_cast<object::WinResHeaderPrefix *>(buf);
500 prefix->DataSize = manifestSize;
501 prefix->HeaderSize = sizeof(object::WinResHeaderPrefix) +
502 sizeof(object::WinResIDs) +
503 sizeof(object::WinResHeaderSuffix);
504 buf += sizeof(object::WinResHeaderPrefix);
506 // Write the Type/Name IDs.
507 auto *iDs = reinterpret_cast<object::WinResIDs *>(buf);
508 iDs->setType(RT_MANIFEST);
509 iDs->setName(manifestID);
510 buf += sizeof(object::WinResIDs);
512 // Write the suffix.
513 auto *suffix = reinterpret_cast<object::WinResHeaderSuffix *>(buf);
514 suffix->DataVersion = 0;
515 suffix->MemoryFlags = object::WIN_RES_PURE_MOVEABLE;
516 suffix->Language = SUBLANG_ENGLISH_US;
517 suffix->Version = 0;
518 suffix->Characteristics = 0;
519 buf += sizeof(object::WinResHeaderSuffix);
522 // Create a resource file containing a manifest XML.
523 std::unique_ptr<MemoryBuffer> LinkerDriver::createManifestRes() {
524 std::string manifest = createManifestXml();
526 std::unique_ptr<WritableMemoryBuffer> res =
527 createMemoryBufferForManifestRes(manifest.size());
529 char *buf = res->getBufferStart();
530 writeResFileHeader(buf);
531 writeResEntryHeader(buf, manifest.size(), ctx.config.manifestID);
533 // Copy the manifest data into the .res file.
534 std::copy(manifest.begin(), manifest.end(), buf);
535 return std::move(res);
538 void LinkerDriver::createSideBySideManifest() {
539 std::string path = std::string(ctx.config.manifestFile);
540 if (path == "")
541 path = ctx.config.outputFile + ".manifest";
542 std::error_code ec;
543 raw_fd_ostream out(path, ec, sys::fs::OF_TextWithCRLF);
544 if (ec)
545 fatal("failed to create manifest: " + ec.message());
546 out << createManifestXml();
549 // Parse a string in the form of
550 // "<name>[=<internalname>][,@ordinal[,NONAME]][,DATA][,PRIVATE]"
551 // or "<name>=<dllname>.<name>".
552 // Used for parsing /export arguments.
553 Export LinkerDriver::parseExport(StringRef arg) {
554 Export e;
555 e.source = ExportSource::Export;
557 StringRef rest;
558 std::tie(e.name, rest) = arg.split(",");
559 if (e.name.empty())
560 goto err;
562 if (e.name.contains('=')) {
563 auto [x, y] = e.name.split("=");
565 // If "<name>=<dllname>.<name>".
566 if (y.contains(".")) {
567 e.name = x;
568 e.forwardTo = y;
569 return e;
572 e.extName = x;
573 e.name = y;
574 if (e.name.empty())
575 goto err;
578 // If "<name>=<internalname>[,@ordinal[,NONAME]][,DATA][,PRIVATE]"
579 while (!rest.empty()) {
580 StringRef tok;
581 std::tie(tok, rest) = rest.split(",");
582 if (tok.equals_insensitive("noname")) {
583 if (e.ordinal == 0)
584 goto err;
585 e.noname = true;
586 continue;
588 if (tok.equals_insensitive("data")) {
589 e.data = true;
590 continue;
592 if (tok.equals_insensitive("constant")) {
593 e.constant = true;
594 continue;
596 if (tok.equals_insensitive("private")) {
597 e.isPrivate = true;
598 continue;
600 if (tok.starts_with("@")) {
601 int32_t ord;
602 if (tok.substr(1).getAsInteger(0, ord))
603 goto err;
604 if (ord <= 0 || 65535 < ord)
605 goto err;
606 e.ordinal = ord;
607 continue;
609 goto err;
611 return e;
613 err:
614 fatal("invalid /export: " + arg);
617 static StringRef undecorate(COFFLinkerContext &ctx, StringRef sym) {
618 if (ctx.config.machine != I386)
619 return sym;
620 // In MSVC mode, a fully decorated stdcall function is exported
621 // as-is with the leading underscore (with type IMPORT_NAME).
622 // In MinGW mode, a decorated stdcall function gets the underscore
623 // removed, just like normal cdecl functions.
624 if (sym.starts_with("_") && sym.contains('@') && !ctx.config.mingw)
625 return sym;
626 return sym.starts_with("_") ? sym.substr(1) : sym;
629 // Convert stdcall/fastcall style symbols into unsuffixed symbols,
630 // with or without a leading underscore. (MinGW specific.)
631 static StringRef killAt(StringRef sym, bool prefix) {
632 if (sym.empty())
633 return sym;
634 // Strip any trailing stdcall suffix
635 sym = sym.substr(0, sym.find('@', 1));
636 if (!sym.starts_with("@")) {
637 if (prefix && !sym.starts_with("_"))
638 return saver().save("_" + sym);
639 return sym;
641 // For fastcall, remove the leading @ and replace it with an
642 // underscore, if prefixes are used.
643 sym = sym.substr(1);
644 if (prefix)
645 sym = saver().save("_" + sym);
646 return sym;
649 static StringRef exportSourceName(ExportSource s) {
650 switch (s) {
651 case ExportSource::Directives:
652 return "source file (directives)";
653 case ExportSource::Export:
654 return "/export";
655 case ExportSource::ModuleDefinition:
656 return "/def";
657 default:
658 llvm_unreachable("unknown ExportSource");
662 // Performs error checking on all /export arguments.
663 // It also sets ordinals.
664 void LinkerDriver::fixupExports() {
665 llvm::TimeTraceScope timeScope("Fixup exports");
666 // Symbol ordinals must be unique.
667 std::set<uint16_t> ords;
668 for (Export &e : ctx.config.exports) {
669 if (e.ordinal == 0)
670 continue;
671 if (!ords.insert(e.ordinal).second)
672 fatal("duplicate export ordinal: " + e.name);
675 for (Export &e : ctx.config.exports) {
676 if (!e.forwardTo.empty()) {
677 e.exportName = undecorate(ctx, e.name);
678 } else {
679 e.exportName = undecorate(ctx, e.extName.empty() ? e.name : e.extName);
683 if (ctx.config.killAt && ctx.config.machine == I386) {
684 for (Export &e : ctx.config.exports) {
685 e.name = killAt(e.name, true);
686 e.exportName = killAt(e.exportName, false);
687 e.extName = killAt(e.extName, true);
688 e.symbolName = killAt(e.symbolName, true);
692 // Uniquefy by name.
693 DenseMap<StringRef, std::pair<Export *, unsigned>> map(
694 ctx.config.exports.size());
695 std::vector<Export> v;
696 for (Export &e : ctx.config.exports) {
697 auto pair = map.insert(std::make_pair(e.exportName, std::make_pair(&e, 0)));
698 bool inserted = pair.second;
699 if (inserted) {
700 pair.first->second.second = v.size();
701 v.push_back(e);
702 continue;
704 Export *existing = pair.first->second.first;
705 if (e == *existing || e.name != existing->name)
706 continue;
707 // If the existing export comes from .OBJ directives, we are allowed to
708 // overwrite it with /DEF: or /EXPORT without any warning, as MSVC link.exe
709 // does.
710 if (existing->source == ExportSource::Directives) {
711 *existing = e;
712 v[pair.first->second.second] = e;
713 continue;
715 if (existing->source == e.source) {
716 warn(Twine("duplicate ") + exportSourceName(existing->source) +
717 " option: " + e.name);
718 } else {
719 warn("duplicate export: " + e.name +
720 Twine(" first seen in " + exportSourceName(existing->source) +
721 Twine(", now in " + exportSourceName(e.source))));
724 ctx.config.exports = std::move(v);
726 // Sort by name.
727 llvm::sort(ctx.config.exports, [](const Export &a, const Export &b) {
728 return a.exportName < b.exportName;
732 void LinkerDriver::assignExportOrdinals() {
733 // Assign unique ordinals if default (= 0).
734 uint32_t max = 0;
735 for (Export &e : ctx.config.exports)
736 max = std::max(max, (uint32_t)e.ordinal);
737 for (Export &e : ctx.config.exports)
738 if (e.ordinal == 0)
739 e.ordinal = ++max;
740 if (max > std::numeric_limits<uint16_t>::max())
741 fatal("too many exported symbols (got " + Twine(max) + ", max " +
742 Twine(std::numeric_limits<uint16_t>::max()) + ")");
745 // Parses a string in the form of "key=value" and check
746 // if value matches previous values for the same key.
747 void LinkerDriver::checkFailIfMismatch(StringRef arg, InputFile *source) {
748 auto [k, v] = arg.split('=');
749 if (k.empty() || v.empty())
750 fatal("/failifmismatch: invalid argument: " + arg);
751 std::pair<StringRef, InputFile *> existing = ctx.config.mustMatch[k];
752 if (!existing.first.empty() && v != existing.first) {
753 std::string sourceStr = source ? toString(source) : "cmd-line";
754 std::string existingStr =
755 existing.second ? toString(existing.second) : "cmd-line";
756 fatal("/failifmismatch: mismatch detected for '" + k + "':\n>>> " +
757 existingStr + " has value " + existing.first + "\n>>> " + sourceStr +
758 " has value " + v);
760 ctx.config.mustMatch[k] = {v, source};
763 // Convert Windows resource files (.res files) to a .obj file.
764 // Does what cvtres.exe does, but in-process and cross-platform.
765 MemoryBufferRef LinkerDriver::convertResToCOFF(ArrayRef<MemoryBufferRef> mbs,
766 ArrayRef<ObjFile *> objs) {
767 object::WindowsResourceParser parser(/* MinGW */ ctx.config.mingw);
769 std::vector<std::string> duplicates;
770 for (MemoryBufferRef mb : mbs) {
771 std::unique_ptr<object::Binary> bin = check(object::createBinary(mb));
772 object::WindowsResource *rf = dyn_cast<object::WindowsResource>(bin.get());
773 if (!rf)
774 fatal("cannot compile non-resource file as resource");
776 if (auto ec = parser.parse(rf, duplicates))
777 fatal(toString(std::move(ec)));
780 // Note: This processes all .res files before all objs. Ideally they'd be
781 // handled in the same order they were linked (to keep the right one, if
782 // there are duplicates that are tolerated due to forceMultipleRes).
783 for (ObjFile *f : objs) {
784 object::ResourceSectionRef rsf;
785 if (auto ec = rsf.load(f->getCOFFObj()))
786 fatal(toString(f) + ": " + toString(std::move(ec)));
788 if (auto ec = parser.parse(rsf, f->getName(), duplicates))
789 fatal(toString(std::move(ec)));
792 if (ctx.config.mingw)
793 parser.cleanUpManifests(duplicates);
795 for (const auto &dupeDiag : duplicates)
796 if (ctx.config.forceMultipleRes)
797 warn(dupeDiag);
798 else
799 error(dupeDiag);
801 Expected<std::unique_ptr<MemoryBuffer>> e =
802 llvm::object::writeWindowsResourceCOFF(ctx.config.machine, parser,
803 ctx.config.timestamp);
804 if (!e)
805 fatal("failed to write .res to COFF: " + toString(e.takeError()));
807 MemoryBufferRef mbref = **e;
808 make<std::unique_ptr<MemoryBuffer>>(std::move(*e)); // take ownership
809 return mbref;
812 // Create OptTable
814 // Create prefix string literals used in Options.td
815 #define PREFIX(NAME, VALUE) \
816 static constexpr llvm::StringLiteral NAME##_init[] = VALUE; \
817 static constexpr llvm::ArrayRef<llvm::StringLiteral> NAME( \
818 NAME##_init, std::size(NAME##_init) - 1);
819 #include "Options.inc"
820 #undef PREFIX
822 // Create table mapping all options defined in Options.td
823 static constexpr llvm::opt::OptTable::Info infoTable[] = {
824 #define OPTION(...) LLVM_CONSTRUCT_OPT_INFO(__VA_ARGS__),
825 #include "Options.inc"
826 #undef OPTION
829 COFFOptTable::COFFOptTable() : GenericOptTable(infoTable, true) {}
831 // Set color diagnostics according to --color-diagnostics={auto,always,never}
832 // or --no-color-diagnostics flags.
833 static void handleColorDiagnostics(opt::InputArgList &args) {
834 auto *arg = args.getLastArg(OPT_color_diagnostics, OPT_color_diagnostics_eq,
835 OPT_no_color_diagnostics);
836 if (!arg)
837 return;
838 if (arg->getOption().getID() == OPT_color_diagnostics) {
839 lld::errs().enable_colors(true);
840 } else if (arg->getOption().getID() == OPT_no_color_diagnostics) {
841 lld::errs().enable_colors(false);
842 } else {
843 StringRef s = arg->getValue();
844 if (s == "always")
845 lld::errs().enable_colors(true);
846 else if (s == "never")
847 lld::errs().enable_colors(false);
848 else if (s != "auto")
849 error("unknown option: --color-diagnostics=" + s);
853 static cl::TokenizerCallback getQuotingStyle(opt::InputArgList &args) {
854 if (auto *arg = args.getLastArg(OPT_rsp_quoting)) {
855 StringRef s = arg->getValue();
856 if (s != "windows" && s != "posix")
857 error("invalid response file quoting: " + s);
858 if (s == "windows")
859 return cl::TokenizeWindowsCommandLine;
860 return cl::TokenizeGNUCommandLine;
862 // The COFF linker always defaults to Windows quoting.
863 return cl::TokenizeWindowsCommandLine;
866 ArgParser::ArgParser(COFFLinkerContext &c) : ctx(c) {}
868 // Parses a given list of options.
869 opt::InputArgList ArgParser::parse(ArrayRef<const char *> argv) {
870 // Make InputArgList from string vectors.
871 unsigned missingIndex;
872 unsigned missingCount;
874 // We need to get the quoting style for response files before parsing all
875 // options so we parse here before and ignore all the options but
876 // --rsp-quoting and /lldignoreenv.
877 // (This means --rsp-quoting can't be added through %LINK%.)
878 opt::InputArgList args =
879 ctx.optTable.ParseArgs(argv, missingIndex, missingCount);
881 // Expand response files (arguments in the form of @<filename>) and insert
882 // flags from %LINK% and %_LINK_%, and then parse the argument again.
883 SmallVector<const char *, 256> expandedArgv(argv.data(),
884 argv.data() + argv.size());
885 if (!args.hasArg(OPT_lldignoreenv))
886 addLINK(expandedArgv);
887 cl::ExpandResponseFiles(saver(), getQuotingStyle(args), expandedArgv);
888 args = ctx.optTable.ParseArgs(ArrayRef(expandedArgv).drop_front(),
889 missingIndex, missingCount);
891 // Print the real command line if response files are expanded.
892 if (args.hasArg(OPT_verbose) && argv.size() != expandedArgv.size()) {
893 std::string msg = "Command line:";
894 for (const char *s : expandedArgv)
895 msg += " " + std::string(s);
896 message(msg);
899 // Save the command line after response file expansion so we can write it to
900 // the PDB if necessary. Mimic MSVC, which skips input files.
901 ctx.config.argv = {argv[0]};
902 for (opt::Arg *arg : args) {
903 if (arg->getOption().getKind() != opt::Option::InputClass) {
904 ctx.config.argv.emplace_back(args.getArgString(arg->getIndex()));
908 // Handle /WX early since it converts missing argument warnings to errors.
909 errorHandler().fatalWarnings = args.hasFlag(OPT_WX, OPT_WX_no, false);
911 if (missingCount)
912 fatal(Twine(args.getArgString(missingIndex)) + ": missing argument");
914 handleColorDiagnostics(args);
916 for (opt::Arg *arg : args.filtered(OPT_UNKNOWN)) {
917 std::string nearest;
918 if (ctx.optTable.findNearest(arg->getAsString(args), nearest) > 1)
919 warn("ignoring unknown argument '" + arg->getAsString(args) + "'");
920 else
921 warn("ignoring unknown argument '" + arg->getAsString(args) +
922 "', did you mean '" + nearest + "'");
925 if (args.hasArg(OPT_lib))
926 warn("ignoring /lib since it's not the first argument");
928 return args;
931 // Tokenizes and parses a given string as command line in .drective section.
932 ParsedDirectives ArgParser::parseDirectives(StringRef s) {
933 ParsedDirectives result;
934 SmallVector<const char *, 16> rest;
936 // Handle /EXPORT and /INCLUDE in a fast path. These directives can appear for
937 // potentially every symbol in the object, so they must be handled quickly.
938 SmallVector<StringRef, 16> tokens;
939 cl::TokenizeWindowsCommandLineNoCopy(s, saver(), tokens);
940 for (StringRef tok : tokens) {
941 if (tok.starts_with_insensitive("/export:") ||
942 tok.starts_with_insensitive("-export:"))
943 result.exports.push_back(tok.substr(strlen("/export:")));
944 else if (tok.starts_with_insensitive("/include:") ||
945 tok.starts_with_insensitive("-include:"))
946 result.includes.push_back(tok.substr(strlen("/include:")));
947 else if (tok.starts_with_insensitive("/exclude-symbols:") ||
948 tok.starts_with_insensitive("-exclude-symbols:"))
949 result.excludes.push_back(tok.substr(strlen("/exclude-symbols:")));
950 else {
951 // Copy substrings that are not valid C strings. The tokenizer may have
952 // already copied quoted arguments for us, so those do not need to be
953 // copied again.
954 bool HasNul = tok.end() != s.end() && tok.data()[tok.size()] == '\0';
955 rest.push_back(HasNul ? tok.data() : saver().save(tok).data());
959 // Make InputArgList from unparsed string vectors.
960 unsigned missingIndex;
961 unsigned missingCount;
963 result.args = ctx.optTable.ParseArgs(rest, missingIndex, missingCount);
965 if (missingCount)
966 fatal(Twine(result.args.getArgString(missingIndex)) + ": missing argument");
967 for (auto *arg : result.args.filtered(OPT_UNKNOWN))
968 warn("ignoring unknown argument: " + arg->getAsString(result.args));
969 return result;
972 // link.exe has an interesting feature. If LINK or _LINK_ environment
973 // variables exist, their contents are handled as command line strings.
974 // So you can pass extra arguments using them.
975 void ArgParser::addLINK(SmallVector<const char *, 256> &argv) {
976 // Concatenate LINK env and command line arguments, and then parse them.
977 if (std::optional<std::string> s = Process::GetEnv("LINK")) {
978 std::vector<const char *> v = tokenize(*s);
979 argv.insert(std::next(argv.begin()), v.begin(), v.end());
981 if (std::optional<std::string> s = Process::GetEnv("_LINK_")) {
982 std::vector<const char *> v = tokenize(*s);
983 argv.insert(std::next(argv.begin()), v.begin(), v.end());
987 std::vector<const char *> ArgParser::tokenize(StringRef s) {
988 SmallVector<const char *, 16> tokens;
989 cl::TokenizeWindowsCommandLine(s, saver(), tokens);
990 return std::vector<const char *>(tokens.begin(), tokens.end());
993 void LinkerDriver::printHelp(const char *argv0) {
994 ctx.optTable.printHelp(lld::outs(),
995 (std::string(argv0) + " [options] file...").c_str(),
996 "LLVM Linker", false);
999 } // namespace coff
1000 } // namespace lld