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. 12 // 13 //===----------------------------------------------------------------------===// 14 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> 41 42 using namespace llvm::COFF; 43 using namespace llvm::object; 44 using namespace llvm::opt; 45 using namespace llvm; 46 using llvm::sys::Process; 47 48 namespace lld { 49 namespace coff { 50 namespace { 51 52 const uint16_t SUBLANG_ENGLISH_US = 0x0409; 53 const uint16_t RT_MANIFEST = 24; 54 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)); } 62 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); 69 70 if (sys::ExecuteAndWait(args[0], args) != 0) 71 fatal("ExecuteAndWait failed: " + 72 llvm::join(args.begin(), args.end(), " ")); 73 } 74 75 private: 76 StringRef prog; 77 std::vector<StringRef> args; 78 }; 79 80 } // anonymous namespace 81 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; 89 } 90 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; 101 } 102 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; 119 } 120 } 121 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(); 146 } 147 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)); 158 } 159 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; 176 } 177 } 178 179 void LinkerDriver::parsePDBPageSize(StringRef s) { 180 int v; 181 if (s.getAsInteger(0, v)) { 182 Err(ctx) << "/pdbpagesize: invalid argument: " << s; 183 return; 184 } 185 if (v != 4096 && v != 8192 && v != 16384 && v != 32768) { 186 Err(ctx) << "/pdbpagesize: invalid argument: " << s; 187 return; 188 } 189 190 ctx.config.pdbPageSize = v; 191 } 192 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; 220 } 221 } 222 return ret; 223 } 224 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); 231 } 232 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; 239 } 240 int v; 241 if (align.getAsInteger(0, v)) { 242 Err(ctx) << "/aligncomm: invalid argument: " << s; 243 return; 244 } 245 ctx.config.alignComm[std::string(name)] = 246 std::max(ctx.config.alignComm[std::string(name)], 1 << v); 247 } 248 249 void LinkerDriver::parseDosStub(StringRef path) { 250 std::unique_ptr<MemoryBuffer> stub = 251 CHECK(MemoryBuffer::getFile(path), "could not open " + path); 252 size_t bufferSize = stub->getBufferSize(); 253 const char *bufferStart = stub->getBufferStart(); 254 // MS link.exe compatibility: 255 // 1. stub must be greater than or equal to 64 bytes 256 // 2. stub must start with a valid dos signature 'MZ' 257 if (bufferSize < 64) 258 Err(ctx) << "/stub: stub must be greater than or equal to 64 bytes: " 259 << path; 260 if (bufferStart[0] != 'M' || bufferStart[1] != 'Z') 261 Err(ctx) << "/stub: invalid DOS signature: " << path; 262 ctx.config.dosStub = std::move(stub); 263 } 264 265 // Parses /functionpadmin option argument. 266 void LinkerDriver::parseFunctionPadMin(llvm::opt::Arg *a) { 267 StringRef arg = a->getNumValues() ? a->getValue() : ""; 268 if (!arg.empty()) { 269 // Optional padding in bytes is given. 270 if (arg.getAsInteger(0, ctx.config.functionPadMin)) 271 Err(ctx) << "/functionpadmin: invalid argument: " << arg; 272 return; 273 } 274 // No optional argument given. 275 // Set default padding based on machine, similar to link.exe. 276 // There is no default padding for ARM platforms. 277 if (ctx.config.machine == I386) { 278 ctx.config.functionPadMin = 5; 279 } else if (ctx.config.machine == AMD64) { 280 ctx.config.functionPadMin = 6; 281 } else { 282 Err(ctx) << "/functionpadmin: invalid argument for this machine: " << arg; 283 } 284 } 285 286 // Parses /dependentloadflag option argument. 287 void LinkerDriver::parseDependentLoadFlags(llvm::opt::Arg *a) { 288 StringRef arg = a->getNumValues() ? a->getValue() : ""; 289 if (!arg.empty()) { 290 if (arg.getAsInteger(0, ctx.config.dependentLoadFlags)) 291 Err(ctx) << "/dependentloadflag: invalid argument: " << arg; 292 return; 293 } 294 // MSVC linker reports error "no argument specified", although MSDN describes 295 // argument as optional. 296 Err(ctx) << "/dependentloadflag: no argument specified"; 297 } 298 299 // Parses a string in the form of "EMBED[,=<integer>]|NO". 300 // Results are directly written to 301 // Config. 302 void LinkerDriver::parseManifest(StringRef arg) { 303 if (arg.equals_insensitive("no")) { 304 ctx.config.manifest = Configuration::No; 305 return; 306 } 307 if (!arg.starts_with_insensitive("embed")) 308 Fatal(ctx) << "invalid option " << arg; 309 ctx.config.manifest = Configuration::Embed; 310 arg = arg.substr(strlen("embed")); 311 if (arg.empty()) 312 return; 313 if (!arg.starts_with_insensitive(",id=")) 314 Fatal(ctx) << "invalid option " << arg; 315 arg = arg.substr(strlen(",id=")); 316 if (arg.getAsInteger(0, ctx.config.manifestID)) 317 Fatal(ctx) << "invalid option " << arg; 318 } 319 320 // Parses a string in the form of "level=<string>|uiAccess=<string>|NO". 321 // Results are directly written to Config. 322 void LinkerDriver::parseManifestUAC(StringRef arg) { 323 if (arg.equals_insensitive("no")) { 324 ctx.config.manifestUAC = false; 325 return; 326 } 327 for (;;) { 328 arg = arg.ltrim(); 329 if (arg.empty()) 330 return; 331 if (arg.consume_front_insensitive("level=")) { 332 std::tie(ctx.config.manifestLevel, arg) = arg.split(" "); 333 continue; 334 } 335 if (arg.consume_front_insensitive("uiaccess=")) { 336 std::tie(ctx.config.manifestUIAccess, arg) = arg.split(" "); 337 continue; 338 } 339 Fatal(ctx) << "invalid option " << arg; 340 } 341 } 342 343 // Parses a string in the form of "cd|net[,(cd|net)]*" 344 // Results are directly written to Config. 345 void LinkerDriver::parseSwaprun(StringRef arg) { 346 do { 347 auto [swaprun, newArg] = arg.split(','); 348 if (swaprun.equals_insensitive("cd")) 349 ctx.config.swaprunCD = true; 350 else if (swaprun.equals_insensitive("net")) 351 ctx.config.swaprunNet = true; 352 else if (swaprun.empty()) 353 Err(ctx) << "/swaprun: missing argument"; 354 else 355 Err(ctx) << "/swaprun: invalid argument: " << swaprun; 356 // To catch trailing commas, e.g. `/spawrun:cd,` 357 if (newArg.empty() && arg.ends_with(",")) 358 Err(ctx) << "/swaprun: missing argument"; 359 arg = newArg; 360 } while (!arg.empty()); 361 } 362 363 // An RAII temporary file class that automatically removes a temporary file. 364 namespace { 365 class TemporaryFile { 366 public: 367 TemporaryFile(COFFLinkerContext &ctx, StringRef prefix, StringRef extn, 368 StringRef contents = "") 369 : ctx(ctx) { 370 SmallString<128> s; 371 if (auto ec = sys::fs::createTemporaryFile("lld-" + prefix, extn, s)) 372 Fatal(ctx) << "cannot create a temporary file: " << ec.message(); 373 path = std::string(s); 374 375 if (!contents.empty()) { 376 std::error_code ec; 377 raw_fd_ostream os(path, ec, sys::fs::OF_None); 378 if (ec) 379 Fatal(ctx) << "failed to open " << path << ": " << ec.message(); 380 os << contents; 381 } 382 } 383 384 TemporaryFile(TemporaryFile &&obj) noexcept : ctx(obj.ctx) { 385 std::swap(path, obj.path); 386 } 387 388 ~TemporaryFile() { 389 if (path.empty()) 390 return; 391 if (sys::fs::remove(path)) 392 Fatal(ctx) << "failed to remove " << path; 393 } 394 395 // Returns a memory buffer of this temporary file. 396 // Note that this function does not leave the file open, 397 // so it is safe to remove the file immediately after this function 398 // is called (you cannot remove an opened file on Windows.) 399 std::unique_ptr<MemoryBuffer> getMemoryBuffer() { 400 // IsVolatile=true forces MemoryBuffer to not use mmap(). 401 return CHECK(MemoryBuffer::getFile(path, /*IsText=*/false, 402 /*RequiresNullTerminator=*/false, 403 /*IsVolatile=*/true), 404 "could not open " + path); 405 } 406 407 COFFLinkerContext &ctx; 408 std::string path; 409 }; 410 } 411 412 std::string LinkerDriver::createDefaultXml() { 413 std::string ret; 414 raw_string_ostream os(ret); 415 416 // Emit the XML. Note that we do *not* verify that the XML attributes are 417 // syntactically correct. This is intentional for link.exe compatibility. 418 os << "<?xml version=\"1.0\" standalone=\"yes\"?>\n" 419 << "<assembly xmlns=\"urn:schemas-microsoft-com:asm.v1\"\n" 420 << " manifestVersion=\"1.0\">\n"; 421 if (ctx.config.manifestUAC) { 422 os << " <trustInfo>\n" 423 << " <security>\n" 424 << " <requestedPrivileges>\n" 425 << " <requestedExecutionLevel level=" << ctx.config.manifestLevel 426 << " uiAccess=" << ctx.config.manifestUIAccess << "/>\n" 427 << " </requestedPrivileges>\n" 428 << " </security>\n" 429 << " </trustInfo>\n"; 430 } 431 for (auto manifestDependency : ctx.config.manifestDependencies) { 432 os << " <dependency>\n" 433 << " <dependentAssembly>\n" 434 << " <assemblyIdentity " << manifestDependency << " />\n" 435 << " </dependentAssembly>\n" 436 << " </dependency>\n"; 437 } 438 os << "</assembly>\n"; 439 return ret; 440 } 441 442 std::string 443 LinkerDriver::createManifestXmlWithInternalMt(StringRef defaultXml) { 444 std::unique_ptr<MemoryBuffer> defaultXmlCopy = 445 MemoryBuffer::getMemBufferCopy(defaultXml); 446 447 windows_manifest::WindowsManifestMerger merger; 448 if (auto e = merger.merge(*defaultXmlCopy.get())) 449 Fatal(ctx) << "internal manifest tool failed on default xml: " 450 << toString(std::move(e)); 451 452 for (StringRef filename : ctx.config.manifestInput) { 453 std::unique_ptr<MemoryBuffer> manifest = 454 check(MemoryBuffer::getFile(filename)); 455 // Call takeBuffer to include in /reproduce: output if applicable. 456 if (auto e = merger.merge(takeBuffer(std::move(manifest)))) 457 Fatal(ctx) << "internal manifest tool failed on file " << filename << ": " 458 << toString(std::move(e)); 459 } 460 461 return std::string(merger.getMergedManifest().get()->getBuffer()); 462 } 463 464 std::string 465 LinkerDriver::createManifestXmlWithExternalMt(StringRef defaultXml) { 466 // Create the default manifest file as a temporary file. 467 TemporaryFile Default(ctx, "defaultxml", "manifest"); 468 std::error_code ec; 469 raw_fd_ostream os(Default.path, ec, sys::fs::OF_TextWithCRLF); 470 if (ec) 471 Fatal(ctx) << "failed to open " << Default.path << ": " << ec.message(); 472 os << defaultXml; 473 os.close(); 474 475 // Merge user-supplied manifests if they are given. Since libxml2 is not 476 // enabled, we must shell out to Microsoft's mt.exe tool. 477 TemporaryFile user(ctx, "user", "manifest"); 478 479 Executor e("mt.exe"); 480 e.add("/manifest"); 481 e.add(Default.path); 482 for (StringRef filename : ctx.config.manifestInput) { 483 e.add("/manifest"); 484 e.add(filename); 485 486 // Manually add the file to the /reproduce: tar if needed. 487 if (tar) 488 if (auto mbOrErr = MemoryBuffer::getFile(filename)) 489 takeBuffer(std::move(*mbOrErr)); 490 } 491 e.add("/nologo"); 492 e.add("/out:" + StringRef(user.path)); 493 e.run(); 494 495 return std::string( 496 CHECK(MemoryBuffer::getFile(user.path), "could not open " + user.path) 497 .get() 498 ->getBuffer()); 499 } 500 501 std::string LinkerDriver::createManifestXml() { 502 std::string defaultXml = createDefaultXml(); 503 if (ctx.config.manifestInput.empty()) 504 return defaultXml; 505 506 if (windows_manifest::isAvailable()) 507 return createManifestXmlWithInternalMt(defaultXml); 508 509 return createManifestXmlWithExternalMt(defaultXml); 510 } 511 512 std::unique_ptr<WritableMemoryBuffer> 513 LinkerDriver::createMemoryBufferForManifestRes(size_t manifestSize) { 514 size_t resSize = alignTo( 515 object::WIN_RES_MAGIC_SIZE + object::WIN_RES_NULL_ENTRY_SIZE + 516 sizeof(object::WinResHeaderPrefix) + sizeof(object::WinResIDs) + 517 sizeof(object::WinResHeaderSuffix) + manifestSize, 518 object::WIN_RES_DATA_ALIGNMENT); 519 return WritableMemoryBuffer::getNewMemBuffer(resSize, ctx.config.outputFile + 520 ".manifest.res"); 521 } 522 523 static void writeResFileHeader(char *&buf) { 524 memcpy(buf, COFF::WinResMagic, sizeof(COFF::WinResMagic)); 525 buf += sizeof(COFF::WinResMagic); 526 memset(buf, 0, object::WIN_RES_NULL_ENTRY_SIZE); 527 buf += object::WIN_RES_NULL_ENTRY_SIZE; 528 } 529 530 static void writeResEntryHeader(char *&buf, size_t manifestSize, 531 int manifestID) { 532 // Write the prefix. 533 auto *prefix = reinterpret_cast<object::WinResHeaderPrefix *>(buf); 534 prefix->DataSize = manifestSize; 535 prefix->HeaderSize = sizeof(object::WinResHeaderPrefix) + 536 sizeof(object::WinResIDs) + 537 sizeof(object::WinResHeaderSuffix); 538 buf += sizeof(object::WinResHeaderPrefix); 539 540 // Write the Type/Name IDs. 541 auto *iDs = reinterpret_cast<object::WinResIDs *>(buf); 542 iDs->setType(RT_MANIFEST); 543 iDs->setName(manifestID); 544 buf += sizeof(object::WinResIDs); 545 546 // Write the suffix. 547 auto *suffix = reinterpret_cast<object::WinResHeaderSuffix *>(buf); 548 suffix->DataVersion = 0; 549 suffix->MemoryFlags = object::WIN_RES_PURE_MOVEABLE; 550 suffix->Language = SUBLANG_ENGLISH_US; 551 suffix->Version = 0; 552 suffix->Characteristics = 0; 553 buf += sizeof(object::WinResHeaderSuffix); 554 } 555 556 // Create a resource file containing a manifest XML. 557 std::unique_ptr<MemoryBuffer> LinkerDriver::createManifestRes() { 558 std::string manifest = createManifestXml(); 559 560 std::unique_ptr<WritableMemoryBuffer> res = 561 createMemoryBufferForManifestRes(manifest.size()); 562 563 char *buf = res->getBufferStart(); 564 writeResFileHeader(buf); 565 writeResEntryHeader(buf, manifest.size(), ctx.config.manifestID); 566 567 // Copy the manifest data into the .res file. 568 std::copy(manifest.begin(), manifest.end(), buf); 569 return std::move(res); 570 } 571 572 void LinkerDriver::createSideBySideManifest() { 573 std::string path = std::string(ctx.config.manifestFile); 574 if (path == "") 575 path = ctx.config.outputFile + ".manifest"; 576 std::error_code ec; 577 raw_fd_ostream out(path, ec, sys::fs::OF_TextWithCRLF); 578 if (ec) 579 Fatal(ctx) << "failed to create manifest: " << ec.message(); 580 out << createManifestXml(); 581 } 582 583 // Parse a string in the form of 584 // "<name>[=<internalname>][,@ordinal[,NONAME]][,DATA][,PRIVATE]" 585 // or "<name>=<dllname>.<name>". 586 // Used for parsing /export arguments. 587 Export LinkerDriver::parseExport(StringRef arg) { 588 Export e; 589 e.source = ExportSource::Export; 590 591 StringRef rest; 592 std::tie(e.name, rest) = arg.split(","); 593 if (e.name.empty()) 594 goto err; 595 596 if (e.name.contains('=')) { 597 auto [x, y] = e.name.split("="); 598 599 // If "<name>=<dllname>.<name>". 600 if (y.contains(".")) { 601 e.name = x; 602 e.forwardTo = y; 603 } else { 604 e.extName = x; 605 e.name = y; 606 if (e.name.empty()) 607 goto err; 608 } 609 } 610 611 // Optional parameters 612 // "[,@ordinal[,NONAME]][,DATA][,PRIVATE][,EXPORTAS,exportname]" 613 while (!rest.empty()) { 614 StringRef tok; 615 std::tie(tok, rest) = rest.split(","); 616 if (tok.equals_insensitive("noname")) { 617 if (e.ordinal == 0) 618 goto err; 619 e.noname = true; 620 continue; 621 } 622 if (tok.equals_insensitive("data")) { 623 e.data = true; 624 continue; 625 } 626 if (tok.equals_insensitive("constant")) { 627 e.constant = true; 628 continue; 629 } 630 if (tok.equals_insensitive("private")) { 631 e.isPrivate = true; 632 continue; 633 } 634 if (tok.equals_insensitive("exportas")) { 635 if (!rest.empty() && !rest.contains(',')) 636 e.exportAs = rest; 637 else 638 Err(ctx) << "invalid EXPORTAS value: " << rest; 639 break; 640 } 641 if (tok.starts_with("@")) { 642 int32_t ord; 643 if (tok.substr(1).getAsInteger(0, ord)) 644 goto err; 645 if (ord <= 0 || 65535 < ord) 646 goto err; 647 e.ordinal = ord; 648 continue; 649 } 650 goto err; 651 } 652 return e; 653 654 err: 655 Fatal(ctx) << "invalid /export: " << arg; 656 llvm_unreachable(""); 657 } 658 659 // Parses a string in the form of "key=value" and check 660 // if value matches previous values for the same key. 661 void LinkerDriver::checkFailIfMismatch(StringRef arg, InputFile *source) { 662 auto [k, v] = arg.split('='); 663 if (k.empty() || v.empty()) 664 Fatal(ctx) << "/failifmismatch: invalid argument: " << arg; 665 std::pair<StringRef, InputFile *> existing = ctx.config.mustMatch[k]; 666 if (!existing.first.empty() && v != existing.first) { 667 std::string sourceStr = source ? toString(source) : "cmd-line"; 668 std::string existingStr = 669 existing.second ? toString(existing.second) : "cmd-line"; 670 Fatal(ctx) << "/failifmismatch: mismatch detected for '" << k << "':\n>>> " 671 << existingStr << " has value " << existing.first << "\n>>> " 672 << sourceStr << " has value " << v; 673 } 674 ctx.config.mustMatch[k] = {v, source}; 675 } 676 677 // Convert Windows resource files (.res files) to a .obj file. 678 // Does what cvtres.exe does, but in-process and cross-platform. 679 MemoryBufferRef LinkerDriver::convertResToCOFF(ArrayRef<MemoryBufferRef> mbs, 680 ArrayRef<ObjFile *> objs) { 681 object::WindowsResourceParser parser(/* MinGW */ ctx.config.mingw); 682 683 std::vector<std::string> duplicates; 684 for (MemoryBufferRef mb : mbs) { 685 std::unique_ptr<object::Binary> bin = check(object::createBinary(mb)); 686 object::WindowsResource *rf = dyn_cast<object::WindowsResource>(bin.get()); 687 if (!rf) 688 Fatal(ctx) << "cannot compile non-resource file as resource"; 689 690 if (auto ec = parser.parse(rf, duplicates)) 691 Fatal(ctx) << toString(std::move(ec)); 692 } 693 694 // Note: This processes all .res files before all objs. Ideally they'd be 695 // handled in the same order they were linked (to keep the right one, if 696 // there are duplicates that are tolerated due to forceMultipleRes). 697 for (ObjFile *f : objs) { 698 object::ResourceSectionRef rsf; 699 if (auto ec = rsf.load(f->getCOFFObj())) 700 Fatal(ctx) << toString(f) << ": " << toString(std::move(ec)); 701 702 if (auto ec = parser.parse(rsf, f->getName(), duplicates)) 703 Fatal(ctx) << toString(std::move(ec)); 704 } 705 706 if (ctx.config.mingw) 707 parser.cleanUpManifests(duplicates); 708 709 for (const auto &dupeDiag : duplicates) 710 if (ctx.config.forceMultipleRes) 711 Warn(ctx) << dupeDiag; 712 else 713 Err(ctx) << dupeDiag; 714 715 Expected<std::unique_ptr<MemoryBuffer>> e = 716 llvm::object::writeWindowsResourceCOFF(ctx.config.machine, parser, 717 ctx.config.timestamp); 718 if (!e) 719 Fatal(ctx) << "failed to write .res to COFF: " << toString(e.takeError()); 720 721 MemoryBufferRef mbref = **e; 722 make<std::unique_ptr<MemoryBuffer>>(std::move(*e)); // take ownership 723 return mbref; 724 } 725 726 // Create OptTable 727 728 #define OPTTABLE_STR_TABLE_CODE 729 #include "Options.inc" 730 #undef OPTTABLE_STR_TABLE_CODE 731 732 // Create prefix string literals used in Options.td 733 #define OPTTABLE_PREFIXES_TABLE_CODE 734 #include "Options.inc" 735 #undef OPTTABLE_PREFIXES_TABLE_CODE 736 737 // Create table mapping all options defined in Options.td 738 static constexpr llvm::opt::OptTable::Info infoTable[] = { 739 #define OPTION(...) LLVM_CONSTRUCT_OPT_INFO(__VA_ARGS__), 740 #include "Options.inc" 741 #undef OPTION 742 }; 743 744 COFFOptTable::COFFOptTable() 745 : GenericOptTable(OptionStrTable, OptionPrefixesTable, infoTable, true) {} 746 747 // Set color diagnostics according to --color-diagnostics={auto,always,never} 748 // or --no-color-diagnostics flags. 749 static void handleColorDiagnostics(COFFLinkerContext &ctx, 750 opt::InputArgList &args) { 751 auto *arg = args.getLastArg(OPT_color_diagnostics, OPT_color_diagnostics_eq, 752 OPT_no_color_diagnostics); 753 if (!arg) 754 return; 755 if (arg->getOption().getID() == OPT_color_diagnostics) { 756 ctx.e.errs().enable_colors(true); 757 } else if (arg->getOption().getID() == OPT_no_color_diagnostics) { 758 ctx.e.errs().enable_colors(false); 759 } else { 760 StringRef s = arg->getValue(); 761 if (s == "always") 762 ctx.e.errs().enable_colors(true); 763 else if (s == "never") 764 ctx.e.errs().enable_colors(false); 765 else if (s != "auto") 766 Err(ctx) << "unknown option: --color-diagnostics=" << s; 767 } 768 } 769 770 static cl::TokenizerCallback getQuotingStyle(COFFLinkerContext &ctx, 771 opt::InputArgList &args) { 772 if (auto *arg = args.getLastArg(OPT_rsp_quoting)) { 773 StringRef s = arg->getValue(); 774 if (s != "windows" && s != "posix") 775 Err(ctx) << "invalid response file quoting: " << s; 776 if (s == "windows") 777 return cl::TokenizeWindowsCommandLine; 778 return cl::TokenizeGNUCommandLine; 779 } 780 // The COFF linker always defaults to Windows quoting. 781 return cl::TokenizeWindowsCommandLine; 782 } 783 784 ArgParser::ArgParser(COFFLinkerContext &c) : ctx(c) {} 785 786 // Parses a given list of options. 787 opt::InputArgList ArgParser::parse(ArrayRef<const char *> argv) { 788 // Make InputArgList from string vectors. 789 unsigned missingIndex; 790 unsigned missingCount; 791 792 // We need to get the quoting style for response files before parsing all 793 // options so we parse here before and ignore all the options but 794 // --rsp-quoting and /lldignoreenv. 795 // (This means --rsp-quoting can't be added through %LINK%.) 796 opt::InputArgList args = 797 ctx.optTable.ParseArgs(argv, missingIndex, missingCount); 798 799 // Expand response files (arguments in the form of @<filename>) and insert 800 // flags from %LINK% and %_LINK_%, and then parse the argument again. 801 SmallVector<const char *, 256> expandedArgv(argv.data(), 802 argv.data() + argv.size()); 803 if (!args.hasArg(OPT_lldignoreenv)) 804 addLINK(expandedArgv); 805 cl::ExpandResponseFiles(saver(), getQuotingStyle(ctx, args), expandedArgv); 806 args = ctx.optTable.ParseArgs(ArrayRef(expandedArgv).drop_front(), 807 missingIndex, missingCount); 808 809 // Print the real command line if response files are expanded. 810 if (args.hasArg(OPT_verbose) && argv.size() != expandedArgv.size()) { 811 std::string msg = "Command line:"; 812 for (const char *s : expandedArgv) 813 msg += " " + std::string(s); 814 Msg(ctx) << msg; 815 } 816 817 // Save the command line after response file expansion so we can write it to 818 // the PDB if necessary. Mimic MSVC, which skips input files. 819 ctx.config.argv = {argv[0]}; 820 for (opt::Arg *arg : args) { 821 if (arg->getOption().getKind() != opt::Option::InputClass) { 822 ctx.config.argv.emplace_back(args.getArgString(arg->getIndex())); 823 } 824 } 825 826 // Handle /WX early since it converts missing argument warnings to errors. 827 ctx.e.fatalWarnings = args.hasFlag(OPT_WX, OPT_WX_no, false); 828 829 if (missingCount) 830 Fatal(ctx) << args.getArgString(missingIndex) << ": missing argument"; 831 832 handleColorDiagnostics(ctx, args); 833 834 for (opt::Arg *arg : args.filtered(OPT_UNKNOWN)) { 835 std::string nearest; 836 if (ctx.optTable.findNearest(arg->getAsString(args), nearest) > 1) 837 Warn(ctx) << "ignoring unknown argument '" << arg->getAsString(args) 838 << "'"; 839 else 840 Warn(ctx) << "ignoring unknown argument '" << arg->getAsString(args) 841 << "', did you mean '" << nearest << "'"; 842 } 843 844 if (args.hasArg(OPT_lib)) 845 Warn(ctx) << "ignoring /lib since it's not the first argument"; 846 847 return args; 848 } 849 850 // Tokenizes and parses a given string as command line in .drective section. 851 ParsedDirectives ArgParser::parseDirectives(StringRef s) { 852 ParsedDirectives result; 853 SmallVector<const char *, 16> rest; 854 855 // Handle /EXPORT and /INCLUDE in a fast path. These directives can appear for 856 // potentially every symbol in the object, so they must be handled quickly. 857 SmallVector<StringRef, 16> tokens; 858 cl::TokenizeWindowsCommandLineNoCopy(s, saver(), tokens); 859 for (StringRef tok : tokens) { 860 if (tok.starts_with_insensitive("/export:") || 861 tok.starts_with_insensitive("-export:")) 862 result.exports.push_back(tok.substr(strlen("/export:"))); 863 else if (tok.starts_with_insensitive("/include:") || 864 tok.starts_with_insensitive("-include:")) 865 result.includes.push_back(tok.substr(strlen("/include:"))); 866 else if (tok.starts_with_insensitive("/exclude-symbols:") || 867 tok.starts_with_insensitive("-exclude-symbols:")) 868 result.excludes.push_back(tok.substr(strlen("/exclude-symbols:"))); 869 else { 870 // Copy substrings that are not valid C strings. The tokenizer may have 871 // already copied quoted arguments for us, so those do not need to be 872 // copied again. 873 bool HasNul = tok.end() != s.end() && tok.data()[tok.size()] == '\0'; 874 rest.push_back(HasNul ? tok.data() : saver().save(tok).data()); 875 } 876 } 877 878 // Make InputArgList from unparsed string vectors. 879 unsigned missingIndex; 880 unsigned missingCount; 881 882 result.args = ctx.optTable.ParseArgs(rest, missingIndex, missingCount); 883 884 if (missingCount) 885 Fatal(ctx) << result.args.getArgString(missingIndex) 886 << ": missing argument"; 887 for (auto *arg : result.args.filtered(OPT_UNKNOWN)) 888 Warn(ctx) << "ignoring unknown argument: " << arg->getAsString(result.args); 889 return result; 890 } 891 892 // link.exe has an interesting feature. If LINK or _LINK_ environment 893 // variables exist, their contents are handled as command line strings. 894 // So you can pass extra arguments using them. 895 void ArgParser::addLINK(SmallVector<const char *, 256> &argv) { 896 // Concatenate LINK env and command line arguments, and then parse them. 897 if (std::optional<std::string> s = Process::GetEnv("LINK")) { 898 std::vector<const char *> v = tokenize(*s); 899 argv.insert(std::next(argv.begin()), v.begin(), v.end()); 900 } 901 if (std::optional<std::string> s = Process::GetEnv("_LINK_")) { 902 std::vector<const char *> v = tokenize(*s); 903 argv.insert(std::next(argv.begin()), v.begin(), v.end()); 904 } 905 } 906 907 std::vector<const char *> ArgParser::tokenize(StringRef s) { 908 SmallVector<const char *, 16> tokens; 909 cl::TokenizeWindowsCommandLine(s, saver(), tokens); 910 return std::vector<const char *>(tokens.begin(), tokens.end()); 911 } 912 913 void LinkerDriver::printHelp(const char *argv0) { 914 ctx.optTable.printHelp(ctx.e.outs(), 915 (std::string(argv0) + " [options] file...").c_str(), 916 "LLVM Linker", false); 917 } 918 919 } // namespace coff 920 } // namespace lld 921