1 //===- DriverUtils.cpp ----------------------------------------------------===//
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file contains utility functions for the driver. Because there
11 // are so many small functions, we created this separate file to make
12 // Driver.cpp less cluttered.
14 //===----------------------------------------------------------------------===//
21 #include "llvm/ADT/Optional.h"
22 #include "llvm/ADT/StringSwitch.h"
23 #include "llvm/Object/COFF.h"
24 #include "llvm/Option/Arg.h"
25 #include "llvm/Option/ArgList.h"
26 #include "llvm/Option/Option.h"
27 #include "llvm/Support/CommandLine.h"
28 #include "llvm/Support/FileUtilities.h"
29 #include "llvm/Support/Process.h"
30 #include "llvm/Support/Program.h"
31 #include "llvm/Support/raw_ostream.h"
34 using namespace llvm::COFF;
36 using llvm::cl::ExpandResponseFiles;
37 using llvm::cl::TokenizeWindowsCommandLine;
38 using llvm::sys::Process;
46 explicit Executor(StringRef S) : Prog(Saver.save(S)) {}
47 void add(StringRef S) { Args.push_back(Saver.save(S)); }
48 void add(std::string &S) { Args.push_back(Saver.save(S)); }
49 void add(Twine S) { Args.push_back(Saver.save(S)); }
50 void add(const char *S) { Args.push_back(Saver.save(S)); }
53 ErrorOr<std::string> ExeOrErr = sys::findProgramByName(Prog);
54 if (auto EC = ExeOrErr.getError())
55 fatal(EC, "unable to find " + Prog + " in PATH: ");
56 StringRef Exe = Saver.save(*ExeOrErr);
57 Args.insert(Args.begin(), Exe);
59 std::vector<const char *> Vec;
60 for (StringRef S : Args)
61 Vec.push_back(S.data());
62 Vec.push_back(nullptr);
64 if (sys::ExecuteAndWait(Args[0], Vec.data()) != 0)
65 fatal("ExecuteAndWait failed: " +
66 llvm::join(Args.begin(), Args.end(), " "));
71 std::vector<StringRef> Args;
74 } // anonymous namespace
76 // Returns /machine's value.
77 MachineTypes getMachineType(StringRef S) {
78 MachineTypes MT = StringSwitch<MachineTypes>(S.lower())
79 .Cases("x64", "amd64", AMD64)
80 .Cases("x86", "i386", I386)
82 .Default(IMAGE_FILE_MACHINE_UNKNOWN);
83 if (MT != IMAGE_FILE_MACHINE_UNKNOWN)
85 fatal("unknown /machine argument: " + S);
88 StringRef machineToStr(MachineTypes MT) {
97 llvm_unreachable("unknown machine type");
101 // Parses a string in the form of "<integer>[,<integer>]".
102 void parseNumbers(StringRef Arg, uint64_t *Addr, uint64_t *Size) {
104 std::tie(S1, S2) = Arg.split(',');
105 if (S1.getAsInteger(0, *Addr))
106 fatal("invalid number: " + S1);
107 if (Size && !S2.empty() && S2.getAsInteger(0, *Size))
108 fatal("invalid number: " + S2);
111 // Parses a string in the form of "<integer>[.<integer>]".
112 // If second number is not present, Minor is set to 0.
113 void parseVersion(StringRef Arg, uint32_t *Major, uint32_t *Minor) {
115 std::tie(S1, S2) = Arg.split('.');
116 if (S1.getAsInteger(0, *Major))
117 fatal("invalid number: " + S1);
119 if (!S2.empty() && S2.getAsInteger(0, *Minor))
120 fatal("invalid number: " + S2);
123 // Parses a string in the form of "<subsystem>[,<integer>[.<integer>]]".
124 void parseSubsystem(StringRef Arg, WindowsSubsystem *Sys, uint32_t *Major,
126 StringRef SysStr, Ver;
127 std::tie(SysStr, Ver) = Arg.split(',');
128 *Sys = StringSwitch<WindowsSubsystem>(SysStr.lower())
129 .Case("boot_application", IMAGE_SUBSYSTEM_WINDOWS_BOOT_APPLICATION)
130 .Case("console", IMAGE_SUBSYSTEM_WINDOWS_CUI)
131 .Case("efi_application", IMAGE_SUBSYSTEM_EFI_APPLICATION)
132 .Case("efi_boot_service_driver", IMAGE_SUBSYSTEM_EFI_BOOT_SERVICE_DRIVER)
133 .Case("efi_rom", IMAGE_SUBSYSTEM_EFI_ROM)
134 .Case("efi_runtime_driver", IMAGE_SUBSYSTEM_EFI_RUNTIME_DRIVER)
135 .Case("native", IMAGE_SUBSYSTEM_NATIVE)
136 .Case("posix", IMAGE_SUBSYSTEM_POSIX_CUI)
137 .Case("windows", IMAGE_SUBSYSTEM_WINDOWS_GUI)
138 .Default(IMAGE_SUBSYSTEM_UNKNOWN);
139 if (*Sys == IMAGE_SUBSYSTEM_UNKNOWN)
140 fatal("unknown subsystem: " + SysStr);
142 parseVersion(Ver, Major, Minor);
145 // Parse a string of the form of "<from>=<to>".
146 // Results are directly written to Config.
147 void parseAlternateName(StringRef S) {
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));
158 // Parse a string of the form of "<from>=<to>".
159 // Results are directly written to Config.
160 void parseMerge(StringRef S) {
162 std::tie(From, To) = S.split('=');
163 if (From.empty() || To.empty())
164 fatal("/merge: invalid argument: " + S);
165 auto Pair = Config->Merge.insert(std::make_pair(From, To));
166 bool Inserted = Pair.second;
168 StringRef Existing = Pair.first->second;
170 warn(S + ": already merged into " + Existing);
174 static uint32_t parseSectionAttributes(StringRef S) {
176 for (char C : S.lower()) {
179 Ret |= IMAGE_SCN_MEM_DISCARDABLE;
182 Ret |= IMAGE_SCN_MEM_EXECUTE;
185 Ret |= IMAGE_SCN_MEM_NOT_CACHED;
188 Ret |= IMAGE_SCN_MEM_NOT_PAGED;
191 Ret |= IMAGE_SCN_MEM_READ;
194 Ret |= IMAGE_SCN_MEM_SHARED;
197 Ret |= IMAGE_SCN_MEM_WRITE;
200 fatal("/section: invalid argument: " + S);
206 // Parses /section option argument.
207 void parseSection(StringRef S) {
208 StringRef Name, Attrs;
209 std::tie(Name, Attrs) = S.split(',');
210 if (Name.empty() || Attrs.empty())
211 fatal("/section: invalid argument: " + S);
212 Config->Section[Name] = parseSectionAttributes(Attrs);
215 // Parses a string in the form of "EMBED[,=<integer>]|NO".
216 // Results are directly written to Config.
217 void parseManifest(StringRef Arg) {
218 if (Arg.equals_lower("no")) {
219 Config->Manifest = Configuration::No;
222 if (!Arg.startswith_lower("embed"))
223 fatal("invalid option " + Arg);
224 Config->Manifest = Configuration::Embed;
225 Arg = Arg.substr(strlen("embed"));
228 if (!Arg.startswith_lower(",id="))
229 fatal("invalid option " + Arg);
230 Arg = Arg.substr(strlen(",id="));
231 if (Arg.getAsInteger(0, Config->ManifestID))
232 fatal("invalid option " + Arg);
235 // Parses a string in the form of "level=<string>|uiAccess=<string>|NO".
236 // Results are directly written to Config.
237 void parseManifestUAC(StringRef Arg) {
238 if (Arg.equals_lower("no")) {
239 Config->ManifestUAC = false;
246 if (Arg.startswith_lower("level=")) {
247 Arg = Arg.substr(strlen("level="));
248 std::tie(Config->ManifestLevel, Arg) = Arg.split(" ");
251 if (Arg.startswith_lower("uiaccess=")) {
252 Arg = Arg.substr(strlen("uiaccess="));
253 std::tie(Config->ManifestUIAccess, Arg) = Arg.split(" ");
256 fatal("invalid option " + Arg);
260 // Quote each line with "". Existing double-quote is converted
261 // to two double-quotes.
262 static void quoteAndPrint(raw_ostream &Out, StringRef S) {
265 std::tie(Line, S) = S.split("\n");
269 for (int I = 0, E = Line.size(); I != E; ++I) {
270 if (Line[I] == '\"') {
280 // An RAII temporary file class that automatically removes a temporary file.
282 class TemporaryFile {
284 TemporaryFile(StringRef Prefix, StringRef Extn, StringRef Contents = "") {
286 if (auto EC = sys::fs::createTemporaryFile("lld-" + Prefix, Extn, S))
287 fatal(EC, "cannot create a temporary file");
290 if (!Contents.empty()) {
292 raw_fd_ostream OS(Path, EC, sys::fs::F_None);
294 fatal(EC, "failed to open " + Path);
299 TemporaryFile(TemporaryFile &&Obj) {
300 std::swap(Path, Obj.Path);
306 if (sys::fs::remove(Path))
307 fatal("failed to remove " + Path);
310 // Returns a memory buffer of this temporary file.
311 // Note that this function does not leave the file open,
312 // so it is safe to remove the file immediately after this function
313 // is called (you cannot remove an opened file on Windows.)
314 std::unique_ptr<MemoryBuffer> getMemoryBuffer() {
315 // IsVolatileSize=true forces MemoryBuffer to not use mmap().
316 return check(MemoryBuffer::getFile(Path, /*FileSize=*/-1,
317 /*RequiresNullTerminator=*/false,
318 /*IsVolatileSize=*/true),
319 "could not open " + Path);
326 // Create the default manifest file as a temporary file.
327 TemporaryFile createDefaultXml() {
328 // Create a temporary file.
329 TemporaryFile File("defaultxml", "manifest");
331 // Open the temporary file for writing.
333 raw_fd_ostream OS(File.Path, EC, sys::fs::F_Text);
335 fatal(EC, "failed to open " + File.Path);
337 // Emit the XML. Note that we do *not* verify that the XML attributes are
338 // syntactically correct. This is intentional for link.exe compatibility.
339 OS << "<?xml version=\"1.0\" standalone=\"yes\"?>\n"
340 << "<assembly xmlns=\"urn:schemas-microsoft-com:asm.v1\"\n"
341 << " manifestVersion=\"1.0\">\n";
342 if (Config->ManifestUAC) {
343 OS << " <trustInfo>\n"
345 << " <requestedPrivileges>\n"
346 << " <requestedExecutionLevel level=" << Config->ManifestLevel
347 << " uiAccess=" << Config->ManifestUIAccess << "/>\n"
348 << " </requestedPrivileges>\n"
350 << " </trustInfo>\n";
351 if (!Config->ManifestDependency.empty()) {
352 OS << " <dependency>\n"
353 << " <dependentAssembly>\n"
354 << " <assemblyIdentity " << Config->ManifestDependency << " />\n"
355 << " </dependentAssembly>\n"
356 << " </dependency>\n";
359 OS << "</assembly>\n";
364 static std::string readFile(StringRef Path) {
365 std::unique_ptr<MemoryBuffer> MB =
366 check(MemoryBuffer::getFile(Path), "could not open " + Path);
367 return MB->getBuffer();
370 static std::string createManifestXml() {
371 // Create the default manifest file.
372 TemporaryFile File1 = createDefaultXml();
373 if (Config->ManifestInput.empty())
374 return readFile(File1.Path);
376 // If manifest files are supplied by the user using /MANIFESTINPUT
377 // option, we need to merge them with the default manifest.
378 TemporaryFile File2("user", "manifest");
380 Executor E("mt.exe");
383 for (StringRef Filename : Config->ManifestInput) {
388 E.add("/out:" + StringRef(File2.Path));
390 return readFile(File2.Path);
393 // Create a resource file containing a manifest XML.
394 std::unique_ptr<MemoryBuffer> createManifestRes() {
395 // Create a temporary file for the resource script file.
396 TemporaryFile RCFile("manifest", "rc");
398 // Open the temporary file for writing.
400 raw_fd_ostream Out(RCFile.Path, EC, sys::fs::F_Text);
402 fatal(EC, "failed to open " + RCFile.Path);
404 // Write resource script to the RC file.
405 Out << "#define LANG_ENGLISH 9\n"
406 << "#define SUBLANG_DEFAULT 1\n"
407 << "#define APP_MANIFEST " << Config->ManifestID << "\n"
408 << "#define RT_MANIFEST 24\n"
409 << "LANGUAGE LANG_ENGLISH, SUBLANG_DEFAULT\n"
410 << "APP_MANIFEST RT_MANIFEST {\n";
411 quoteAndPrint(Out, createManifestXml());
415 // Create output resource file.
416 TemporaryFile ResFile("output-resource", "res");
418 Executor E("rc.exe");
424 return ResFile.getMemoryBuffer();
427 void createSideBySideManifest() {
428 std::string Path = Config->ManifestFile;
430 Path = Config->OutputFile + ".manifest";
432 raw_fd_ostream Out(Path, EC, sys::fs::F_Text);
434 fatal(EC, "failed to create manifest");
435 Out << createManifestXml();
438 // Parse a string in the form of
439 // "<name>[=<internalname>][,@ordinal[,NONAME]][,DATA][,PRIVATE]"
440 // or "<name>=<dllname>.<name>".
441 // Used for parsing /export arguments.
442 Export parseExport(StringRef Arg) {
445 std::tie(E.Name, Rest) = Arg.split(",");
449 if (E.Name.find('=') != StringRef::npos) {
451 std::tie(X, Y) = E.Name.split("=");
453 // If "<name>=<dllname>.<name>".
454 if (Y.find(".") != StringRef::npos) {
466 // If "<name>=<internalname>[,@ordinal[,NONAME]][,DATA][,PRIVATE]"
467 while (!Rest.empty()) {
469 std::tie(Tok, Rest) = Rest.split(",");
470 if (Tok.equals_lower("noname")) {
476 if (Tok.equals_lower("data")) {
480 if (Tok.equals_lower("constant")) {
484 if (Tok.equals_lower("private")) {
488 if (Tok.startswith("@")) {
490 if (Tok.substr(1).getAsInteger(0, Ord))
492 if (Ord <= 0 || 65535 < Ord)
502 fatal("invalid /export: " + Arg);
505 static StringRef undecorate(StringRef Sym) {
506 if (Config->Machine != I386)
508 return Sym.startswith("_") ? Sym.substr(1) : Sym;
511 // Performs error checking on all /export arguments.
512 // It also sets ordinals.
513 void fixupExports() {
514 // Symbol ordinals must be unique.
515 std::set<uint16_t> Ords;
516 for (Export &E : Config->Exports) {
519 if (!Ords.insert(E.Ordinal).second)
520 fatal("duplicate export ordinal: " + E.Name);
523 for (Export &E : Config->Exports) {
524 SymbolBody *Sym = E.Sym;
525 if (!E.ForwardTo.empty() || !Sym) {
526 E.SymbolName = E.Name;
528 if (auto *U = dyn_cast<Undefined>(Sym))
531 E.SymbolName = Sym->getName();
535 for (Export &E : Config->Exports) {
536 if (!E.ForwardTo.empty()) {
537 E.ExportName = undecorate(E.Name);
539 E.ExportName = undecorate(E.ExtName.empty() ? E.Name : E.ExtName);
544 std::map<StringRef, Export *> Map;
545 std::vector<Export> V;
546 for (Export &E : Config->Exports) {
547 auto Pair = Map.insert(std::make_pair(E.ExportName, &E));
548 bool Inserted = Pair.second;
553 Export *Existing = Pair.first->second;
554 if (E == *Existing || E.Name != Existing->Name)
556 warn("duplicate /export option: " + E.Name);
558 Config->Exports = std::move(V);
561 std::sort(Config->Exports.begin(), Config->Exports.end(),
562 [](const Export &A, const Export &B) {
563 return A.ExportName < B.ExportName;
567 void assignExportOrdinals() {
568 // Assign unique ordinals if default (= 0).
570 for (Export &E : Config->Exports)
571 Max = std::max(Max, E.Ordinal);
572 for (Export &E : Config->Exports)
577 // Parses a string in the form of "key=value" and check
578 // if value matches previous values for the same key.
579 void checkFailIfMismatch(StringRef Arg) {
581 std::tie(K, V) = Arg.split('=');
582 if (K.empty() || V.empty())
583 fatal("/failifmismatch: invalid argument: " + Arg);
584 StringRef Existing = Config->MustMatch[K];
585 if (!Existing.empty() && V != Existing)
586 fatal("/failifmismatch: mismatch detected: " + Existing + " and " + V +
588 Config->MustMatch[K] = V;
591 // Convert Windows resource files (.res files) to a .obj file
593 std::unique_ptr<MemoryBuffer>
594 convertResToCOFF(const std::vector<MemoryBufferRef> &MBs) {
595 // Create an output file path.
596 TemporaryFile File("resource-file", "obj");
598 // Execute cvtres.exe.
599 Executor E("cvtres.exe");
600 E.add("/machine:" + machineToStr(Config->Machine));
603 E.add("/out:" + Twine(File.Path));
605 // We must create new files because the memory buffers we have may have no
606 // underlying file still existing on the disk.
607 // It happens if it was created from a TemporaryFile, which usually delete
608 // the file just after creating the MemoryBuffer.
609 std::vector<TemporaryFile> ResFiles;
610 ResFiles.reserve(MBs.size());
611 for (MemoryBufferRef MB : MBs) {
612 // We store the temporary file in a vector to avoid deletion
613 // before running cvtres
614 ResFiles.emplace_back("resource-file", "res");
615 TemporaryFile& ResFile = ResFiles.back();
616 // Write the content of the resource in a temporary file
618 raw_fd_ostream OS(ResFile.Path, EC, sys::fs::F_None);
620 fatal(EC, "failed to open " + ResFile.Path);
621 OS << MB.getBuffer();
628 return File.getMemoryBuffer();
631 // Run MSVC link.exe for given in-memory object files.
632 // Command line options are copied from those given to LLD.
633 // This is for the /msvclto option.
634 void runMSVCLinker(std::string Rsp, ArrayRef<StringRef> Objects) {
635 // Write the in-memory object files to disk.
636 std::vector<TemporaryFile> Temps;
637 for (StringRef S : Objects) {
638 Temps.emplace_back("lto", "obj", S);
639 Rsp += quote(Temps.back().Path) + "\n";
642 log("link.exe " + Rsp);
644 // Run MSVC link.exe.
645 Temps.emplace_back("lto", "rsp", Rsp);
646 Executor E("link.exe");
647 E.add(Twine("@" + Temps.back().Path));
653 // Create prefix string literals used in Options.td
654 #define PREFIX(NAME, VALUE) const char *const NAME[] = VALUE;
655 #include "Options.inc"
658 // Create table mapping all options defined in Options.td
659 static const llvm::opt::OptTable::Info infoTable[] = {
660 #define OPTION(X1, X2, ID, KIND, GROUP, ALIAS, X6, X7, X8, X9, X10) \
662 X1, X2, X9, X10, OPT_##ID, llvm::opt::Option::KIND##Class, X8, X7, \
663 OPT_##GROUP, OPT_##ALIAS, X6 \
665 #include "Options.inc"
669 class COFFOptTable : public llvm::opt::OptTable {
671 COFFOptTable() : OptTable(infoTable, true) {}
674 // Parses a given list of options.
675 opt::InputArgList ArgParser::parse(ArrayRef<const char *> ArgsArr) {
676 // First, replace respnose files (@<file>-style options).
677 std::vector<const char *> Argv = replaceResponseFiles(ArgsArr);
679 // Make InputArgList from string vectors.
681 unsigned MissingIndex;
682 unsigned MissingCount;
683 opt::InputArgList Args = Table.ParseArgs(Argv, MissingIndex, MissingCount);
685 // Print the real command line if response files are expanded.
686 if (Args.hasArg(OPT_verbose) && ArgsArr.size() != Argv.size()) {
687 std::string Msg = "Command line:";
688 for (const char *S : Argv)
689 Msg += " " + std::string(S);
694 fatal(Twine(Args.getArgString(MissingIndex)) + ": missing argument");
695 for (auto *Arg : Args.filtered(OPT_UNKNOWN))
696 warn("ignoring unknown argument: " + Arg->getSpelling());
700 // link.exe has an interesting feature. If LINK or _LINK_ environment
701 // variables exist, their contents are handled as command line strings.
702 // So you can pass extra arguments using them.
703 opt::InputArgList ArgParser::parseLINK(std::vector<const char *> Args) {
704 // Concatenate LINK env and command line arguments, and then parse them.
705 if (Optional<std::string> S = Process::GetEnv("LINK")) {
706 std::vector<const char *> V = tokenize(*S);
707 Args.insert(Args.begin(), V.begin(), V.end());
709 if (Optional<std::string> S = Process::GetEnv("_LINK_")) {
710 std::vector<const char *> V = tokenize(*S);
711 Args.insert(Args.begin(), V.begin(), V.end());
716 std::vector<const char *> ArgParser::tokenize(StringRef S) {
717 SmallVector<const char *, 16> Tokens;
718 cl::TokenizeWindowsCommandLine(S, Saver, Tokens);
719 return std::vector<const char *>(Tokens.begin(), Tokens.end());
722 // Creates a new command line by replacing options starting with '@'
723 // character. '@<filename>' is replaced by the file's contents.
724 std::vector<const char *>
725 ArgParser::replaceResponseFiles(std::vector<const char *> Argv) {
726 SmallVector<const char *, 256> Tokens(Argv.data(), Argv.data() + Argv.size());
727 ExpandResponseFiles(Saver, TokenizeWindowsCommandLine, Tokens);
728 return std::vector<const char *>(Tokens.begin(), Tokens.end());
731 void printHelp(const char *Argv0) {
733 Table.PrintHelp(outs(), Argv0, "LLVM Linker", false);