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