1 //===- DriverUtils.cpp ----------------------------------------------------===// 2 // 3 // The LLVM Linker 4 // 5 // This file is distributed under the University of Illinois Open Source 6 // License. See LICENSE.TXT for details. 7 // 8 //===----------------------------------------------------------------------===// 9 // 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. 13 // 14 //===----------------------------------------------------------------------===// 15 16 #include "Config.h" 17 #include "Driver.h" 18 #include "Error.h" 19 #include "Symbols.h" 20 #include "lld/Support/Memory.h" 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" 32 #include <memory> 33 34 using namespace llvm::COFF; 35 using namespace llvm; 36 using llvm::cl::ExpandResponseFiles; 37 using llvm::cl::TokenizeWindowsCommandLine; 38 using llvm::sys::Process; 39 40 namespace lld { 41 namespace coff { 42 namespace { 43 44 class Executor { 45 public: 46 explicit Executor(StringRef S) : Saver(Alloc), Prog(Saver.save(S)) {} 47 void add(StringRef S) { Args.push_back(Saver.save(S).data()); } 48 void add(std::string &S) { Args.push_back(Saver.save(S).data()); } 49 void add(Twine S) { Args.push_back(Saver.save(S).data()); } 50 void add(const char *S) { Args.push_back(Saver.save(S).data()); } 51 52 void run() { 53 ErrorOr<std::string> ExeOrErr = sys::findProgramByName(Prog); 54 if (auto EC = ExeOrErr.getError()) 55 fatal(EC, "unable to find " + Prog + " in PATH: "); 56 const char *Exe = Saver.save(*ExeOrErr).data(); 57 Args.insert(Args.begin(), Exe); 58 Args.push_back(nullptr); 59 if (sys::ExecuteAndWait(Args[0], Args.data()) != 0) { 60 for (const char *S : Args) 61 if (S) 62 errs() << S << " "; 63 fatal("ExecuteAndWait failed"); 64 } 65 } 66 67 private: 68 BumpPtrAllocator Alloc; 69 StringSaver Saver; 70 StringRef Prog; 71 std::vector<const char *> Args; 72 }; 73 74 } // anonymous namespace 75 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) 81 .Case("arm", ARMNT) 82 .Default(IMAGE_FILE_MACHINE_UNKNOWN); 83 if (MT != IMAGE_FILE_MACHINE_UNKNOWN) 84 return MT; 85 fatal("unknown /machine argument: " + S); 86 } 87 88 StringRef machineToStr(MachineTypes MT) { 89 switch (MT) { 90 case ARMNT: 91 return "arm"; 92 case AMD64: 93 return "x64"; 94 case I386: 95 return "x86"; 96 default: 97 llvm_unreachable("unknown machine type"); 98 } 99 } 100 101 // Parses a string in the form of "<integer>[,<integer>]". 102 void parseNumbers(StringRef Arg, uint64_t *Addr, uint64_t *Size) { 103 StringRef S1, S2; 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); 109 } 110 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) { 114 StringRef S1, S2; 115 std::tie(S1, S2) = Arg.split('.'); 116 if (S1.getAsInteger(0, *Major)) 117 fatal("invalid number: " + S1); 118 *Minor = 0; 119 if (!S2.empty() && S2.getAsInteger(0, *Minor)) 120 fatal("invalid number: " + S2); 121 } 122 123 // Parses a string in the form of "<subsystem>[,<integer>[.<integer>]]". 124 void parseSubsystem(StringRef Arg, WindowsSubsystem *Sys, uint32_t *Major, 125 uint32_t *Minor) { 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); 141 if (!Ver.empty()) 142 parseVersion(Ver, Major, Minor); 143 } 144 145 // Parse a string of the form of "<from>=<to>". 146 // Results are directly written to Config. 147 void parseAlternateName(StringRef S) { 148 StringRef From, To; 149 std::tie(From, To) = S.split('='); 150 if (From.empty() || To.empty()) 151 fatal("/alternatename: invalid argument: " + S); 152 auto It = Config->AlternateNames.find(From); 153 if (It != Config->AlternateNames.end() && It->second != To) 154 fatal("/alternatename: conflicts: " + S); 155 Config->AlternateNames.insert(It, std::make_pair(From, To)); 156 } 157 158 // Parse a string of the form of "<from>=<to>". 159 // Results are directly written to Config. 160 void parseMerge(StringRef S) { 161 StringRef From, To; 162 std::tie(From, To) = S.split('='); 163 if (From.empty() || To.empty()) 164 fatal("/merge: invalid argument: " + S); 165 auto Pair = Config->Merge.insert(std::make_pair(From, To)); 166 bool Inserted = Pair.second; 167 if (!Inserted) { 168 StringRef Existing = Pair.first->second; 169 if (Existing != To) 170 errs() << "warning: " << S << ": already merged into " << Existing 171 << "\n"; 172 } 173 } 174 175 static uint32_t parseSectionAttributes(StringRef S) { 176 uint32_t Ret = 0; 177 for (char C : S.lower()) { 178 switch (C) { 179 case 'd': 180 Ret |= IMAGE_SCN_MEM_DISCARDABLE; 181 break; 182 case 'e': 183 Ret |= IMAGE_SCN_MEM_EXECUTE; 184 break; 185 case 'k': 186 Ret |= IMAGE_SCN_MEM_NOT_CACHED; 187 break; 188 case 'p': 189 Ret |= IMAGE_SCN_MEM_NOT_PAGED; 190 break; 191 case 'r': 192 Ret |= IMAGE_SCN_MEM_READ; 193 break; 194 case 's': 195 Ret |= IMAGE_SCN_MEM_SHARED; 196 break; 197 case 'w': 198 Ret |= IMAGE_SCN_MEM_WRITE; 199 break; 200 default: 201 fatal("/section: invalid argument: " + S); 202 } 203 } 204 return Ret; 205 } 206 207 // Parses /section option argument. 208 void parseSection(StringRef S) { 209 StringRef Name, Attrs; 210 std::tie(Name, Attrs) = S.split(','); 211 if (Name.empty() || Attrs.empty()) 212 fatal("/section: invalid argument: " + S); 213 Config->Section[Name] = parseSectionAttributes(Attrs); 214 } 215 216 // Parses a string in the form of "EMBED[,=<integer>]|NO". 217 // Results are directly written to Config. 218 void parseManifest(StringRef Arg) { 219 if (Arg.equals_lower("no")) { 220 Config->Manifest = Configuration::No; 221 return; 222 } 223 if (!Arg.startswith_lower("embed")) 224 fatal("invalid option " + Arg); 225 Config->Manifest = Configuration::Embed; 226 Arg = Arg.substr(strlen("embed")); 227 if (Arg.empty()) 228 return; 229 if (!Arg.startswith_lower(",id=")) 230 fatal("invalid option " + Arg); 231 Arg = Arg.substr(strlen(",id=")); 232 if (Arg.getAsInteger(0, Config->ManifestID)) 233 fatal("invalid option " + Arg); 234 } 235 236 // Parses a string in the form of "level=<string>|uiAccess=<string>|NO". 237 // Results are directly written to Config. 238 void parseManifestUAC(StringRef Arg) { 239 if (Arg.equals_lower("no")) { 240 Config->ManifestUAC = false; 241 return; 242 } 243 for (;;) { 244 Arg = Arg.ltrim(); 245 if (Arg.empty()) 246 return; 247 if (Arg.startswith_lower("level=")) { 248 Arg = Arg.substr(strlen("level=")); 249 std::tie(Config->ManifestLevel, Arg) = Arg.split(" "); 250 continue; 251 } 252 if (Arg.startswith_lower("uiaccess=")) { 253 Arg = Arg.substr(strlen("uiaccess=")); 254 std::tie(Config->ManifestUIAccess, Arg) = Arg.split(" "); 255 continue; 256 } 257 fatal("invalid option " + Arg); 258 } 259 } 260 261 // Quote each line with "". Existing double-quote is converted 262 // to two double-quotes. 263 static void quoteAndPrint(raw_ostream &Out, StringRef S) { 264 while (!S.empty()) { 265 StringRef Line; 266 std::tie(Line, S) = S.split("\n"); 267 if (Line.empty()) 268 continue; 269 Out << '\"'; 270 for (int I = 0, E = Line.size(); I != E; ++I) { 271 if (Line[I] == '\"') { 272 Out << "\"\""; 273 } else { 274 Out << Line[I]; 275 } 276 } 277 Out << "\"\n"; 278 } 279 } 280 281 // An RAII temporary file class that automatically removes a temporary file. 282 namespace { 283 class TemporaryFile { 284 public: 285 TemporaryFile(StringRef Prefix, StringRef Extn) { 286 SmallString<128> S; 287 if (auto EC = sys::fs::createTemporaryFile("lld-" + Prefix, Extn, S)) 288 fatal(EC, "cannot create a temporary file"); 289 Path = S.str(); 290 } 291 292 TemporaryFile(TemporaryFile &&Obj) { 293 std::swap(Path, Obj.Path); 294 } 295 296 ~TemporaryFile() { 297 if (Path.empty()) 298 return; 299 if (sys::fs::remove(Path)) 300 fatal("failed to remove " + Path); 301 } 302 303 // Returns a memory buffer of this temporary file. 304 // Note that this function does not leave the file open, 305 // so it is safe to remove the file immediately after this function 306 // is called (you cannot remove an opened file on Windows.) 307 std::unique_ptr<MemoryBuffer> getMemoryBuffer() { 308 // IsVolatileSize=true forces MemoryBuffer to not use mmap(). 309 return check(MemoryBuffer::getFile(Path, /*FileSize=*/-1, 310 /*RequiresNullTerminator=*/false, 311 /*IsVolatileSize=*/true), 312 "could not open " + Path); 313 } 314 315 std::string Path; 316 }; 317 } 318 319 // Create the default manifest file as a temporary file. 320 TemporaryFile createDefaultXml() { 321 // Create a temporary file. 322 TemporaryFile File("defaultxml", "manifest"); 323 324 // Open the temporary file for writing. 325 std::error_code EC; 326 raw_fd_ostream OS(File.Path, EC, sys::fs::F_Text); 327 if (EC) 328 fatal(EC, "failed to open " + File.Path); 329 330 // Emit the XML. Note that we do *not* verify that the XML attributes are 331 // syntactically correct. This is intentional for link.exe compatibility. 332 OS << "<?xml version=\"1.0\" standalone=\"yes\"?>\n" 333 << "<assembly xmlns=\"urn:schemas-microsoft-com:asm.v1\"\n" 334 << " manifestVersion=\"1.0\">\n"; 335 if (Config->ManifestUAC) { 336 OS << " <trustInfo>\n" 337 << " <security>\n" 338 << " <requestedPrivileges>\n" 339 << " <requestedExecutionLevel level=" << Config->ManifestLevel 340 << " uiAccess=" << Config->ManifestUIAccess << "/>\n" 341 << " </requestedPrivileges>\n" 342 << " </security>\n" 343 << " </trustInfo>\n"; 344 if (!Config->ManifestDependency.empty()) { 345 OS << " <dependency>\n" 346 << " <dependentAssembly>\n" 347 << " <assemblyIdentity " << Config->ManifestDependency << " />\n" 348 << " </dependentAssembly>\n" 349 << " </dependency>\n"; 350 } 351 } 352 OS << "</assembly>\n"; 353 OS.close(); 354 return File; 355 } 356 357 static std::string readFile(StringRef Path) { 358 std::unique_ptr<MemoryBuffer> MB = 359 check(MemoryBuffer::getFile(Path), "could not open " + Path); 360 return MB->getBuffer(); 361 } 362 363 static std::string createManifestXml() { 364 // Create the default manifest file. 365 TemporaryFile File1 = createDefaultXml(); 366 if (Config->ManifestInput.empty()) 367 return readFile(File1.Path); 368 369 // If manifest files are supplied by the user using /MANIFESTINPUT 370 // option, we need to merge them with the default manifest. 371 TemporaryFile File2("user", "manifest"); 372 373 Executor E("mt.exe"); 374 E.add("/manifest"); 375 E.add(File1.Path); 376 for (StringRef Filename : Config->ManifestInput) { 377 E.add("/manifest"); 378 E.add(Filename); 379 } 380 E.add("/nologo"); 381 E.add("/out:" + StringRef(File2.Path)); 382 E.run(); 383 return readFile(File2.Path); 384 } 385 386 // Create a resource file containing a manifest XML. 387 std::unique_ptr<MemoryBuffer> createManifestRes() { 388 // Create a temporary file for the resource script file. 389 TemporaryFile RCFile("manifest", "rc"); 390 391 // Open the temporary file for writing. 392 std::error_code EC; 393 raw_fd_ostream Out(RCFile.Path, EC, sys::fs::F_Text); 394 if (EC) 395 fatal(EC, "failed to open " + RCFile.Path); 396 397 // Write resource script to the RC file. 398 Out << "#define LANG_ENGLISH 9\n" 399 << "#define SUBLANG_DEFAULT 1\n" 400 << "#define APP_MANIFEST " << Config->ManifestID << "\n" 401 << "#define RT_MANIFEST 24\n" 402 << "LANGUAGE LANG_ENGLISH, SUBLANG_DEFAULT\n" 403 << "APP_MANIFEST RT_MANIFEST {\n"; 404 quoteAndPrint(Out, createManifestXml()); 405 Out << "}\n"; 406 Out.close(); 407 408 // Create output resource file. 409 TemporaryFile ResFile("output-resource", "res"); 410 411 Executor E("rc.exe"); 412 E.add("/fo"); 413 E.add(ResFile.Path); 414 E.add("/nologo"); 415 E.add(RCFile.Path); 416 E.run(); 417 return ResFile.getMemoryBuffer(); 418 } 419 420 void createSideBySideManifest() { 421 std::string Path = Config->ManifestFile; 422 if (Path == "") 423 Path = Config->OutputFile + ".manifest"; 424 std::error_code EC; 425 raw_fd_ostream Out(Path, EC, sys::fs::F_Text); 426 if (EC) 427 fatal(EC, "failed to create manifest"); 428 Out << createManifestXml(); 429 } 430 431 // Parse a string in the form of 432 // "<name>[=<internalname>][,@ordinal[,NONAME]][,DATA][,PRIVATE]" 433 // or "<name>=<dllname>.<name>". 434 // Used for parsing /export arguments. 435 Export parseExport(StringRef Arg) { 436 Export E; 437 StringRef Rest; 438 std::tie(E.Name, Rest) = Arg.split(","); 439 if (E.Name.empty()) 440 goto err; 441 442 if (E.Name.find('=') != StringRef::npos) { 443 StringRef X, Y; 444 std::tie(X, Y) = E.Name.split("="); 445 446 // If "<name>=<dllname>.<name>". 447 if (Y.find(".") != StringRef::npos) { 448 E.Name = X; 449 E.ForwardTo = Y; 450 return E; 451 } 452 453 E.ExtName = X; 454 E.Name = Y; 455 if (E.Name.empty()) 456 goto err; 457 } 458 459 // If "<name>=<internalname>[,@ordinal[,NONAME]][,DATA][,PRIVATE]" 460 while (!Rest.empty()) { 461 StringRef Tok; 462 std::tie(Tok, Rest) = Rest.split(","); 463 if (Tok.equals_lower("noname")) { 464 if (E.Ordinal == 0) 465 goto err; 466 E.Noname = true; 467 continue; 468 } 469 if (Tok.equals_lower("data")) { 470 E.Data = true; 471 continue; 472 } 473 if (Tok.equals_lower("private")) { 474 E.Private = true; 475 continue; 476 } 477 if (Tok.startswith("@")) { 478 int32_t Ord; 479 if (Tok.substr(1).getAsInteger(0, Ord)) 480 goto err; 481 if (Ord <= 0 || 65535 < Ord) 482 goto err; 483 E.Ordinal = Ord; 484 continue; 485 } 486 goto err; 487 } 488 return E; 489 490 err: 491 fatal("invalid /export: " + Arg); 492 } 493 494 static StringRef undecorate(StringRef Sym) { 495 if (Config->Machine != I386) 496 return Sym; 497 return Sym.startswith("_") ? Sym.substr(1) : Sym; 498 } 499 500 // Performs error checking on all /export arguments. 501 // It also sets ordinals. 502 void fixupExports() { 503 // Symbol ordinals must be unique. 504 std::set<uint16_t> Ords; 505 for (Export &E : Config->Exports) { 506 if (E.Ordinal == 0) 507 continue; 508 if (!Ords.insert(E.Ordinal).second) 509 fatal("duplicate export ordinal: " + E.Name); 510 } 511 512 for (Export &E : Config->Exports) { 513 if (!E.ForwardTo.empty()) { 514 E.SymbolName = E.Name; 515 } else if (Undefined *U = cast_or_null<Undefined>(E.Sym->WeakAlias)) { 516 E.SymbolName = U->getName(); 517 } else { 518 E.SymbolName = E.Sym->getName(); 519 } 520 } 521 522 for (Export &E : Config->Exports) { 523 if (!E.ForwardTo.empty()) { 524 E.ExportName = undecorate(E.Name); 525 } else { 526 E.ExportName = undecorate(E.ExtName.empty() ? E.Name : E.ExtName); 527 } 528 } 529 530 // Uniquefy by name. 531 std::map<StringRef, Export *> Map; 532 std::vector<Export> V; 533 for (Export &E : Config->Exports) { 534 auto Pair = Map.insert(std::make_pair(E.ExportName, &E)); 535 bool Inserted = Pair.second; 536 if (Inserted) { 537 V.push_back(E); 538 continue; 539 } 540 Export *Existing = Pair.first->second; 541 if (E == *Existing || E.Name != Existing->Name) 542 continue; 543 errs() << "warning: duplicate /export option: " << E.Name << "\n"; 544 } 545 Config->Exports = std::move(V); 546 547 // Sort by name. 548 std::sort(Config->Exports.begin(), Config->Exports.end(), 549 [](const Export &A, const Export &B) { 550 return A.ExportName < B.ExportName; 551 }); 552 } 553 554 void assignExportOrdinals() { 555 // Assign unique ordinals if default (= 0). 556 uint16_t Max = 0; 557 for (Export &E : Config->Exports) 558 Max = std::max(Max, E.Ordinal); 559 for (Export &E : Config->Exports) 560 if (E.Ordinal == 0) 561 E.Ordinal = ++Max; 562 } 563 564 // Parses a string in the form of "key=value" and check 565 // if value matches previous values for the same key. 566 void checkFailIfMismatch(StringRef Arg) { 567 StringRef K, V; 568 std::tie(K, V) = Arg.split('='); 569 if (K.empty() || V.empty()) 570 fatal("/failifmismatch: invalid argument: " + Arg); 571 StringRef Existing = Config->MustMatch[K]; 572 if (!Existing.empty() && V != Existing) 573 fatal("/failifmismatch: mismatch detected: " + Existing + " and " + V + 574 " for key " + K); 575 Config->MustMatch[K] = V; 576 } 577 578 // Convert Windows resource files (.res files) to a .obj file 579 // using cvtres.exe. 580 std::unique_ptr<MemoryBuffer> 581 convertResToCOFF(const std::vector<MemoryBufferRef> &MBs) { 582 // Create an output file path. 583 TemporaryFile File("resource-file", "obj"); 584 585 // Execute cvtres.exe. 586 Executor E("cvtres.exe"); 587 E.add("/machine:" + machineToStr(Config->Machine)); 588 E.add("/readonly"); 589 E.add("/nologo"); 590 E.add("/out:" + Twine(File.Path)); 591 592 // We must create new files because the memory buffers we have may have no 593 // underlying file still existing on the disk. 594 // It happens if it was created from a TemporaryFile, which usually delete 595 // the file just after creating the MemoryBuffer. 596 std::vector<TemporaryFile> ResFiles; 597 ResFiles.reserve(MBs.size()); 598 for (MemoryBufferRef MB : MBs) { 599 // We store the temporary file in a vector to avoid deletion 600 // before running cvtres 601 ResFiles.emplace_back("resource-file", "res"); 602 TemporaryFile& ResFile = ResFiles.back(); 603 // Write the content of the resource in a temporary file 604 std::error_code EC; 605 raw_fd_ostream OS(ResFile.Path, EC, sys::fs::F_None); 606 if (EC) 607 fatal(EC, "failed to open " + ResFile.Path); 608 OS << MB.getBuffer(); 609 OS.close(); 610 611 E.add(ResFile.Path); 612 } 613 614 E.run(); 615 return File.getMemoryBuffer(); 616 } 617 618 // Create OptTable 619 620 // Create prefix string literals used in Options.td 621 #define PREFIX(NAME, VALUE) const char *const NAME[] = VALUE; 622 #include "Options.inc" 623 #undef PREFIX 624 625 // Create table mapping all options defined in Options.td 626 static const llvm::opt::OptTable::Info infoTable[] = { 627 #define OPTION(X1, X2, ID, KIND, GROUP, ALIAS, X6, X7, X8, X9, X10) \ 628 { \ 629 X1, X2, X9, X10, OPT_##ID, llvm::opt::Option::KIND##Class, X8, X7, \ 630 OPT_##GROUP, OPT_##ALIAS, X6 \ 631 }, 632 #include "Options.inc" 633 #undef OPTION 634 }; 635 636 class COFFOptTable : public llvm::opt::OptTable { 637 public: 638 COFFOptTable() : OptTable(infoTable, true) {} 639 }; 640 641 // Parses a given list of options. 642 opt::InputArgList ArgParser::parse(ArrayRef<const char *> ArgsArr) { 643 // First, replace respnose files (@<file>-style options). 644 std::vector<const char *> Argv = replaceResponseFiles(ArgsArr); 645 646 // Make InputArgList from string vectors. 647 COFFOptTable Table; 648 unsigned MissingIndex; 649 unsigned MissingCount; 650 opt::InputArgList Args = Table.ParseArgs(Argv, MissingIndex, MissingCount); 651 652 // Print the real command line if response files are expanded. 653 if (Args.hasArg(OPT_verbose) && ArgsArr.size() != Argv.size()) { 654 outs() << "Command line:"; 655 for (const char *S : Argv) 656 outs() << " " << S; 657 outs() << "\n"; 658 } 659 660 if (MissingCount) 661 fatal(Twine(Args.getArgString(MissingIndex)) + ": missing argument"); 662 for (auto *Arg : Args.filtered(OPT_UNKNOWN)) 663 errs() << "ignoring unknown argument: " << Arg->getSpelling() << "\n"; 664 return Args; 665 } 666 667 // link.exe has an interesting feature. If LINK environment exists, 668 // its contents are handled as a command line string. So you can pass 669 // extra arguments using the environment variable. 670 opt::InputArgList ArgParser::parseLINK(ArrayRef<const char *> Args) { 671 // Concatenate LINK env and command line arguments, and then parse them. 672 Optional<std::string> Env = Process::GetEnv("LINK"); 673 if (!Env) 674 return parse(Args); 675 std::vector<const char *> V = tokenize(*Env); 676 V.insert(V.end(), Args.begin(), Args.end()); 677 return parse(V); 678 } 679 680 std::vector<const char *> ArgParser::tokenize(StringRef S) { 681 SmallVector<const char *, 16> Tokens; 682 cl::TokenizeWindowsCommandLine(S, Saver, Tokens); 683 return std::vector<const char *>(Tokens.begin(), Tokens.end()); 684 } 685 686 // Creates a new command line by replacing options starting with '@' 687 // character. '@<filename>' is replaced by the file's contents. 688 std::vector<const char *> 689 ArgParser::replaceResponseFiles(std::vector<const char *> Argv) { 690 SmallVector<const char *, 256> Tokens(Argv.data(), Argv.data() + Argv.size()); 691 ExpandResponseFiles(Saver, TokenizeWindowsCommandLine, Tokens); 692 return std::vector<const char *>(Tokens.begin(), Tokens.end()); 693 } 694 695 void printHelp(const char *Argv0) { 696 COFFOptTable Table; 697 Table.PrintHelp(outs(), Argv0, "LLVM Linker", false); 698 } 699 700 } // namespace coff 701 } // namespace lld 702