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 //===----------------------------------------------------------------------===//
20 #include "llvm/ADT/Optional.h"
21 #include "llvm/ADT/StringSwitch.h"
22 #include "llvm/Object/COFF.h"
23 #include "llvm/Option/Arg.h"
24 #include "llvm/Option/ArgList.h"
25 #include "llvm/Option/Option.h"
26 #include "llvm/Support/CommandLine.h"
27 #include "llvm/Support/FileUtilities.h"
28 #include "llvm/Support/Process.h"
29 #include "llvm/Support/Program.h"
30 #include "llvm/Support/raw_ostream.h"
33 using namespace llvm::COFF;
35 using llvm::cl::ExpandResponseFiles;
36 using llvm::cl::TokenizeWindowsCommandLine;
37 using llvm::sys::Process;
45 explicit Executor(StringRef S) : Saver(Alloc), Prog(Saver.save(S)) {}
46 void add(StringRef S) { Args.push_back(Saver.save(S)); }
47 void add(std::string &S) { Args.push_back(Saver.save(S)); }
48 void add(Twine S) { Args.push_back(Saver.save(S)); }
49 void add(const char *S) { Args.push_back(Saver.save(S)); }
52 ErrorOr<std::string> ExeOrErr = llvm::sys::findProgramByName(Prog);
53 if (auto EC = ExeOrErr.getError())
54 fatal(EC, "unable to find " + Prog + " in PATH: ");
55 const char *Exe = Saver.save(*ExeOrErr);
56 Args.insert(Args.begin(), Exe);
57 Args.push_back(nullptr);
58 if (llvm::sys::ExecuteAndWait(Args[0], Args.data()) != 0) {
59 for (const char *S : Args)
61 llvm::errs() << S << " ";
62 fatal("ExecuteAndWait failed");
67 llvm::BumpPtrAllocator Alloc;
68 llvm::StringSaver Saver;
70 std::vector<const char *> Args;
73 } // anonymous namespace
75 // Returns /machine's value.
76 MachineTypes getMachineType(StringRef S) {
77 MachineTypes MT = StringSwitch<MachineTypes>(S.lower())
83 .Default(IMAGE_FILE_MACHINE_UNKNOWN);
84 if (MT != IMAGE_FILE_MACHINE_UNKNOWN)
86 fatal("unknown /machine argument: " + S);
89 StringRef machineToStr(MachineTypes MT) {
98 llvm_unreachable("unknown machine type");
102 // Parses a string in the form of "<integer>[,<integer>]".
103 void parseNumbers(StringRef Arg, uint64_t *Addr, uint64_t *Size) {
105 std::tie(S1, S2) = Arg.split(',');
106 if (S1.getAsInteger(0, *Addr))
107 fatal("invalid number: " + S1);
108 if (Size && !S2.empty() && S2.getAsInteger(0, *Size))
109 fatal("invalid number: " + S2);
112 // Parses a string in the form of "<integer>[.<integer>]".
113 // If second number is not present, Minor is set to 0.
114 void parseVersion(StringRef Arg, uint32_t *Major, uint32_t *Minor) {
116 std::tie(S1, S2) = Arg.split('.');
117 if (S1.getAsInteger(0, *Major))
118 fatal("invalid number: " + S1);
120 if (!S2.empty() && S2.getAsInteger(0, *Minor))
121 fatal("invalid number: " + S2);
124 // Parses a string in the form of "<subsystem>[,<integer>[.<integer>]]".
125 void parseSubsystem(StringRef Arg, WindowsSubsystem *Sys, uint32_t *Major,
127 StringRef SysStr, Ver;
128 std::tie(SysStr, Ver) = Arg.split(',');
129 *Sys = StringSwitch<WindowsSubsystem>(SysStr.lower())
130 .Case("boot_application", IMAGE_SUBSYSTEM_WINDOWS_BOOT_APPLICATION)
131 .Case("console", IMAGE_SUBSYSTEM_WINDOWS_CUI)
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)
141 fatal("unknown subsystem: " + SysStr);
143 parseVersion(Ver, Major, Minor);
146 // Parse a string of the form of "<from>=<to>".
147 // Results are directly written to Config.
148 void parseAlternateName(StringRef S) {
150 std::tie(From, To) = S.split('=');
151 if (From.empty() || To.empty())
152 fatal("/alternatename: invalid argument: " + S);
153 auto It = Config->AlternateNames.find(From);
154 if (It != Config->AlternateNames.end() && It->second != To)
155 fatal("/alternatename: conflicts: " + S);
156 Config->AlternateNames.insert(It, std::make_pair(From, To));
159 // Parse a string of the form of "<from>=<to>".
160 // Results are directly written to Config.
161 void parseMerge(StringRef S) {
163 std::tie(From, To) = S.split('=');
164 if (From.empty() || To.empty())
165 fatal("/merge: invalid argument: " + S);
166 auto Pair = Config->Merge.insert(std::make_pair(From, To));
167 bool Inserted = Pair.second;
169 StringRef Existing = Pair.first->second;
171 llvm::errs() << "warning: " << S << ": already merged into "
176 static uint32_t parseSectionAttributes(StringRef S) {
178 for (char C : S.lower()) {
181 Ret |= IMAGE_SCN_MEM_DISCARDABLE;
184 Ret |= IMAGE_SCN_MEM_EXECUTE;
187 Ret |= IMAGE_SCN_MEM_NOT_CACHED;
190 Ret |= IMAGE_SCN_MEM_NOT_PAGED;
193 Ret |= IMAGE_SCN_MEM_READ;
196 Ret |= IMAGE_SCN_MEM_SHARED;
199 Ret |= IMAGE_SCN_MEM_WRITE;
202 fatal("/section: invalid argument: " + S);
208 // Parses /section option argument.
209 void parseSection(StringRef S) {
210 StringRef Name, Attrs;
211 std::tie(Name, Attrs) = S.split(',');
212 if (Name.empty() || Attrs.empty())
213 fatal("/section: invalid argument: " + S);
214 Config->Section[Name] = parseSectionAttributes(Attrs);
217 // Parses a string in the form of "EMBED[,=<integer>]|NO".
218 // Results are directly written to Config.
219 void parseManifest(StringRef Arg) {
220 if (Arg.equals_lower("no")) {
221 Config->Manifest = Configuration::No;
224 if (!Arg.startswith_lower("embed"))
225 fatal("invalid option " + Arg);
226 Config->Manifest = Configuration::Embed;
227 Arg = Arg.substr(strlen("embed"));
230 if (!Arg.startswith_lower(",id="))
231 fatal("invalid option " + Arg);
232 Arg = Arg.substr(strlen(",id="));
233 if (Arg.getAsInteger(0, Config->ManifestID))
234 fatal("invalid option " + Arg);
237 // Parses a string in the form of "level=<string>|uiAccess=<string>|NO".
238 // Results are directly written to Config.
239 void parseManifestUAC(StringRef Arg) {
240 if (Arg.equals_lower("no")) {
241 Config->ManifestUAC = false;
248 if (Arg.startswith_lower("level=")) {
249 Arg = Arg.substr(strlen("level="));
250 std::tie(Config->ManifestLevel, Arg) = Arg.split(" ");
253 if (Arg.startswith_lower("uiaccess=")) {
254 Arg = Arg.substr(strlen("uiaccess="));
255 std::tie(Config->ManifestUIAccess, Arg) = Arg.split(" ");
258 fatal("invalid option " + Arg);
262 // Quote each line with "". Existing double-quote is converted
263 // to two double-quotes.
264 static void quoteAndPrint(raw_ostream &Out, StringRef S) {
267 std::tie(Line, S) = S.split("\n");
271 for (int I = 0, E = Line.size(); I != E; ++I) {
272 if (Line[I] == '\"') {
282 // Create the default manifest file as a temporary file.
283 static std::string createDefaultXml() {
284 // Create a temporary file.
285 SmallString<128> Path;
286 if (auto EC = sys::fs::createTemporaryFile("tmp", "manifest", Path))
287 fatal(EC, "cannot create a temporary file");
289 // Open the temporary file for writing.
291 llvm::raw_fd_ostream OS(Path, EC, sys::fs::F_Text);
293 fatal(EC, "failed to open " + Path);
295 // Emit the XML. Note that we do *not* verify that the XML attributes are
296 // syntactically correct. This is intentional for link.exe compatibility.
297 OS << "<?xml version=\"1.0\" standalone=\"yes\"?>\n"
298 << "<assembly xmlns=\"urn:schemas-microsoft-com:asm.v1\"\n"
299 << " manifestVersion=\"1.0\">\n";
300 if (Config->ManifestUAC) {
301 OS << " <trustInfo>\n"
303 << " <requestedPrivileges>\n"
304 << " <requestedExecutionLevel level=" << Config->ManifestLevel
305 << " uiAccess=" << Config->ManifestUIAccess << "/>\n"
306 << " </requestedPrivileges>\n"
308 << " </trustInfo>\n";
309 if (!Config->ManifestDependency.empty()) {
310 OS << " <dependency>\n"
311 << " <dependentAssembly>\n"
312 << " <assemblyIdentity " << Config->ManifestDependency << " />\n"
313 << " </dependentAssembly>\n"
314 << " </dependency>\n";
317 OS << "</assembly>\n";
319 return StringRef(Path);
322 static std::string readFile(StringRef Path) {
323 std::unique_ptr<MemoryBuffer> MB =
324 check(MemoryBuffer::getFile(Path), "could not open " + Path);
325 std::unique_ptr<MemoryBuffer> Buf(std::move(MB));
326 return Buf->getBuffer();
329 static std::string createManifestXml() {
330 // Create the default manifest file.
331 std::string Path1 = createDefaultXml();
332 if (Config->ManifestInput.empty())
333 return readFile(Path1);
335 // If manifest files are supplied by the user using /MANIFESTINPUT
336 // option, we need to merge them with the default manifest.
337 SmallString<128> Path2;
338 if (auto EC = sys::fs::createTemporaryFile("tmp", "manifest", Path2))
339 fatal(EC, "cannot create a temporary file");
340 FileRemover Remover1(Path1);
341 FileRemover Remover2(Path2);
343 Executor E("mt.exe");
346 for (StringRef Filename : Config->ManifestInput) {
351 E.add("/out:" + StringRef(Path2));
353 return readFile(Path2);
356 // Create a resource file containing a manifest XML.
357 std::unique_ptr<MemoryBuffer> createManifestRes() {
358 // Create a temporary file for the resource script file.
359 SmallString<128> RCPath;
360 if (auto EC = sys::fs::createTemporaryFile("tmp", "rc", RCPath))
361 fatal(EC, "cannot create a temporary file");
362 FileRemover RCRemover(RCPath);
364 // Open the temporary file for writing.
366 llvm::raw_fd_ostream Out(RCPath, EC, sys::fs::F_Text);
368 fatal(EC, "failed to open " + RCPath);
370 // Write resource script to the RC file.
371 Out << "#define LANG_ENGLISH 9\n"
372 << "#define SUBLANG_DEFAULT 1\n"
373 << "#define APP_MANIFEST " << Config->ManifestID << "\n"
374 << "#define RT_MANIFEST 24\n"
375 << "LANGUAGE LANG_ENGLISH, SUBLANG_DEFAULT\n"
376 << "APP_MANIFEST RT_MANIFEST {\n";
377 quoteAndPrint(Out, createManifestXml());
381 // Create output resource file.
382 SmallString<128> ResPath;
383 if (auto EC = sys::fs::createTemporaryFile("tmp", "res", ResPath))
384 fatal(EC, "cannot create a temporary file");
386 Executor E("rc.exe");
388 E.add(ResPath.str());
392 return check(MemoryBuffer::getFile(ResPath), "could not open " + ResPath);
395 void createSideBySideManifest() {
396 std::string Path = Config->ManifestFile;
398 Path = Config->OutputFile + ".manifest";
400 llvm::raw_fd_ostream Out(Path, EC, llvm::sys::fs::F_Text);
402 fatal(EC, "failed to create manifest");
403 Out << createManifestXml();
406 // Parse a string in the form of
407 // "<name>[=<internalname>][,@ordinal[,NONAME]][,DATA][,PRIVATE]"
408 // or "<name>=<dllname>.<name>".
409 // Used for parsing /export arguments.
410 Export parseExport(StringRef Arg) {
413 std::tie(E.Name, Rest) = Arg.split(",");
417 if (E.Name.find('=') != StringRef::npos) {
419 std::tie(X, Y) = E.Name.split("=");
421 // If "<name>=<dllname>.<name>".
422 if (Y.find(".") != StringRef::npos) {
434 // If "<name>=<internalname>[,@ordinal[,NONAME]][,DATA][,PRIVATE]"
435 while (!Rest.empty()) {
437 std::tie(Tok, Rest) = Rest.split(",");
438 if (Tok.equals_lower("noname")) {
444 if (Tok.equals_lower("data")) {
448 if (Tok.equals_lower("private")) {
452 if (Tok.startswith("@")) {
454 if (Tok.substr(1).getAsInteger(0, Ord))
456 if (Ord <= 0 || 65535 < Ord)
466 fatal("invalid /export: " + Arg);
469 static StringRef undecorate(StringRef Sym) {
470 if (Config->Machine != I386)
472 return Sym.startswith("_") ? Sym.substr(1) : Sym;
475 // Performs error checking on all /export arguments.
476 // It also sets ordinals.
477 void fixupExports() {
478 // Symbol ordinals must be unique.
479 std::set<uint16_t> Ords;
480 for (Export &E : Config->Exports) {
483 if (!Ords.insert(E.Ordinal).second)
484 fatal("duplicate export ordinal: " + E.Name);
487 for (Export &E : Config->Exports) {
488 if (!E.ForwardTo.empty()) {
489 E.SymbolName = E.Name;
490 } else if (Undefined *U = cast_or_null<Undefined>(E.Sym->WeakAlias)) {
491 E.SymbolName = U->getName();
493 E.SymbolName = E.Sym->getName();
497 for (Export &E : Config->Exports) {
498 if (!E.ForwardTo.empty()) {
499 E.ExportName = undecorate(E.Name);
501 E.ExportName = undecorate(E.ExtName.empty() ? E.Name : E.ExtName);
506 std::map<StringRef, Export *> Map;
507 std::vector<Export> V;
508 for (Export &E : Config->Exports) {
509 auto Pair = Map.insert(std::make_pair(E.ExportName, &E));
510 bool Inserted = Pair.second;
515 Export *Existing = Pair.first->second;
516 if (E == *Existing || E.Name != Existing->Name)
518 llvm::errs() << "warning: duplicate /export option: " << E.Name << "\n";
520 Config->Exports = std::move(V);
523 std::sort(Config->Exports.begin(), Config->Exports.end(),
524 [](const Export &A, const Export &B) {
525 return A.ExportName < B.ExportName;
529 void assignExportOrdinals() {
530 // Assign unique ordinals if default (= 0).
532 for (Export &E : Config->Exports)
533 Max = std::max(Max, E.Ordinal);
534 for (Export &E : Config->Exports)
539 // Parses a string in the form of "key=value" and check
540 // if value matches previous values for the same key.
541 void checkFailIfMismatch(StringRef Arg) {
543 std::tie(K, V) = Arg.split('=');
544 if (K.empty() || V.empty())
545 fatal("/failifmismatch: invalid argument: " + Arg);
546 StringRef Existing = Config->MustMatch[K];
547 if (!Existing.empty() && V != Existing)
548 fatal("/failifmismatch: mismatch detected: " + Existing + " and " + V +
550 Config->MustMatch[K] = V;
553 // Convert Windows resource files (.res files) to a .obj file
555 std::unique_ptr<MemoryBuffer>
556 convertResToCOFF(const std::vector<MemoryBufferRef> &MBs) {
557 // Create an output file path.
558 SmallString<128> Path;
559 if (auto EC = llvm::sys::fs::createTemporaryFile("resource", "obj", Path))
560 fatal(EC, "could not create temporary file");
562 // Execute cvtres.exe.
563 Executor E("cvtres.exe");
564 E.add("/machine:" + machineToStr(Config->Machine));
567 E.add("/out:" + Path);
568 for (MemoryBufferRef MB : MBs)
569 E.add(MB.getBufferIdentifier());
571 return check(MemoryBuffer::getFile(Path), "could not open " + Path);
576 // Create prefix string literals used in Options.td
577 #define PREFIX(NAME, VALUE) const char *const NAME[] = VALUE;
578 #include "Options.inc"
581 // Create table mapping all options defined in Options.td
582 static const llvm::opt::OptTable::Info infoTable[] = {
583 #define OPTION(X1, X2, ID, KIND, GROUP, ALIAS, X6, X7, X8, X9, X10) \
585 X1, X2, X9, X10, OPT_##ID, llvm::opt::Option::KIND##Class, X8, X7, \
586 OPT_##GROUP, OPT_##ALIAS, X6 \
588 #include "Options.inc"
592 class COFFOptTable : public llvm::opt::OptTable {
594 COFFOptTable() : OptTable(infoTable, true) {}
597 // Parses a given list of options.
598 llvm::opt::InputArgList ArgParser::parse(ArrayRef<const char *> ArgsArr) {
599 // First, replace respnose files (@<file>-style options).
600 std::vector<const char *> Argv = replaceResponseFiles(ArgsArr);
602 // Make InputArgList from string vectors.
604 unsigned MissingIndex;
605 unsigned MissingCount;
606 llvm::opt::InputArgList Args =
607 Table.ParseArgs(Argv, MissingIndex, MissingCount);
609 // Print the real command line if response files are expanded.
610 if (Args.hasArg(OPT_verbose) && ArgsArr.size() != Argv.size()) {
611 llvm::outs() << "Command line:";
612 for (const char *S : Argv)
613 llvm::outs() << " " << S;
614 llvm::outs() << "\n";
618 fatal("missing arg value for \"" + Twine(Args.getArgString(MissingIndex)) +
619 "\", expected " + Twine(MissingCount) +
620 (MissingCount == 1 ? " argument." : " arguments."));
621 for (auto *Arg : Args.filtered(OPT_UNKNOWN))
622 llvm::errs() << "ignoring unknown argument: " << Arg->getSpelling() << "\n";
626 llvm::opt::InputArgList ArgParser::parseLINK(ArrayRef<const char *> Args) {
627 // Concatenate LINK env and given arguments and parse them.
628 Optional<std::string> Env = Process::GetEnv("LINK");
631 std::vector<const char *> V = tokenize(*Env);
632 V.insert(V.end(), Args.begin(), Args.end());
636 std::vector<const char *> ArgParser::tokenize(StringRef S) {
637 SmallVector<const char *, 16> Tokens;
638 StringSaver Saver(AllocAux);
639 llvm::cl::TokenizeWindowsCommandLine(S, Saver, Tokens);
640 return std::vector<const char *>(Tokens.begin(), Tokens.end());
643 // Creates a new command line by replacing options starting with '@'
644 // character. '@<filename>' is replaced by the file's contents.
645 std::vector<const char *>
646 ArgParser::replaceResponseFiles(std::vector<const char *> Argv) {
647 SmallVector<const char *, 256> Tokens(Argv.data(), Argv.data() + Argv.size());
648 StringSaver Saver(AllocAux);
649 ExpandResponseFiles(Saver, TokenizeWindowsCommandLine, Tokens);
650 return std::vector<const char *>(Tokens.begin(), Tokens.end());
653 void printHelp(const char *Argv0) {
655 Table.PrintHelp(llvm::outs(), Argv0, "LLVM Linker", false);