1 //===- Driver.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 #include "Driver.h" 10 #include "Config.h" 11 #include "InputFiles.h" 12 #include "LTO.h" 13 #include "ObjC.h" 14 #include "OutputSection.h" 15 #include "OutputSegment.h" 16 #include "SymbolTable.h" 17 #include "Symbols.h" 18 #include "SyntheticSections.h" 19 #include "Target.h" 20 #include "Writer.h" 21 22 #include "lld/Common/Args.h" 23 #include "lld/Common/Driver.h" 24 #include "lld/Common/ErrorHandler.h" 25 #include "lld/Common/LLVM.h" 26 #include "lld/Common/Memory.h" 27 #include "lld/Common/Reproduce.h" 28 #include "lld/Common/Version.h" 29 #include "llvm/ADT/DenseSet.h" 30 #include "llvm/ADT/StringExtras.h" 31 #include "llvm/ADT/StringRef.h" 32 #include "llvm/BinaryFormat/MachO.h" 33 #include "llvm/BinaryFormat/Magic.h" 34 #include "llvm/Config/config.h" 35 #include "llvm/LTO/LTO.h" 36 #include "llvm/Object/Archive.h" 37 #include "llvm/Option/ArgList.h" 38 #include "llvm/Support/CommandLine.h" 39 #include "llvm/Support/FileSystem.h" 40 #include "llvm/Support/Host.h" 41 #include "llvm/Support/MemoryBuffer.h" 42 #include "llvm/Support/Parallel.h" 43 #include "llvm/Support/Path.h" 44 #include "llvm/Support/TarWriter.h" 45 #include "llvm/Support/TargetSelect.h" 46 #include "llvm/Support/TimeProfiler.h" 47 #include "llvm/TextAPI/PackedVersion.h" 48 49 #include <algorithm> 50 51 using namespace llvm; 52 using namespace llvm::MachO; 53 using namespace llvm::object; 54 using namespace llvm::opt; 55 using namespace llvm::sys; 56 using namespace lld; 57 using namespace lld::macho; 58 59 Configuration *macho::config; 60 DependencyTracker *macho::depTracker; 61 62 static HeaderFileType getOutputType(const InputArgList &args) { 63 // TODO: -r, -dylinker, -preload... 64 Arg *outputArg = args.getLastArg(OPT_bundle, OPT_dylib, OPT_execute); 65 if (outputArg == nullptr) 66 return MH_EXECUTE; 67 68 switch (outputArg->getOption().getID()) { 69 case OPT_bundle: 70 return MH_BUNDLE; 71 case OPT_dylib: 72 return MH_DYLIB; 73 case OPT_execute: 74 return MH_EXECUTE; 75 default: 76 llvm_unreachable("internal error"); 77 } 78 } 79 80 static Optional<StringRef> findLibrary(StringRef name) { 81 if (config->searchDylibsFirst) { 82 if (Optional<StringRef> path = findPathCombination( 83 "lib" + name, config->librarySearchPaths, {".tbd", ".dylib"})) 84 return path; 85 return findPathCombination("lib" + name, config->librarySearchPaths, 86 {".a"}); 87 } 88 return findPathCombination("lib" + name, config->librarySearchPaths, 89 {".tbd", ".dylib", ".a"}); 90 } 91 92 static Optional<std::string> findFramework(StringRef name) { 93 SmallString<260> symlink; 94 StringRef suffix; 95 std::tie(name, suffix) = name.split(","); 96 for (StringRef dir : config->frameworkSearchPaths) { 97 symlink = dir; 98 path::append(symlink, name + ".framework", name); 99 100 if (!suffix.empty()) { 101 // NOTE: we must resolve the symlink before trying the suffixes, because 102 // there are no symlinks for the suffixed paths. 103 SmallString<260> location; 104 if (!fs::real_path(symlink, location)) { 105 // only append suffix if realpath() succeeds 106 Twine suffixed = location + suffix; 107 if (fs::exists(suffixed)) 108 return suffixed.str(); 109 } 110 // Suffix lookup failed, fall through to the no-suffix case. 111 } 112 113 if (Optional<std::string> path = resolveDylibPath(symlink)) 114 return path; 115 } 116 return {}; 117 } 118 119 static bool warnIfNotDirectory(StringRef option, StringRef path) { 120 if (!fs::exists(path)) { 121 warn("directory not found for option -" + option + path); 122 return false; 123 } else if (!fs::is_directory(path)) { 124 warn("option -" + option + path + " references a non-directory path"); 125 return false; 126 } 127 return true; 128 } 129 130 static std::vector<StringRef> 131 getSearchPaths(unsigned optionCode, InputArgList &args, 132 const std::vector<StringRef> &roots, 133 const SmallVector<StringRef, 2> &systemPaths) { 134 std::vector<StringRef> paths; 135 StringRef optionLetter{optionCode == OPT_F ? "F" : "L"}; 136 for (StringRef path : args::getStrings(args, optionCode)) { 137 // NOTE: only absolute paths are re-rooted to syslibroot(s) 138 bool found = false; 139 if (path::is_absolute(path, path::Style::posix)) { 140 for (StringRef root : roots) { 141 SmallString<261> buffer(root); 142 path::append(buffer, path); 143 // Do not warn about paths that are computed via the syslib roots 144 if (fs::is_directory(buffer)) { 145 paths.push_back(saver.save(buffer.str())); 146 found = true; 147 } 148 } 149 } 150 if (!found && warnIfNotDirectory(optionLetter, path)) 151 paths.push_back(path); 152 } 153 154 // `-Z` suppresses the standard "system" search paths. 155 if (args.hasArg(OPT_Z)) 156 return paths; 157 158 for (const StringRef &path : systemPaths) { 159 for (const StringRef &root : roots) { 160 SmallString<261> buffer(root); 161 path::append(buffer, path); 162 if (fs::is_directory(buffer)) 163 paths.push_back(saver.save(buffer.str())); 164 } 165 } 166 return paths; 167 } 168 169 static std::vector<StringRef> getSystemLibraryRoots(InputArgList &args) { 170 std::vector<StringRef> roots; 171 for (const Arg *arg : args.filtered(OPT_syslibroot)) 172 roots.push_back(arg->getValue()); 173 // NOTE: the final `-syslibroot` being `/` will ignore all roots 174 if (roots.size() && roots.back() == "/") 175 roots.clear(); 176 // NOTE: roots can never be empty - add an empty root to simplify the library 177 // and framework search path computation. 178 if (roots.empty()) 179 roots.emplace_back(""); 180 return roots; 181 } 182 183 static std::vector<StringRef> 184 getLibrarySearchPaths(InputArgList &args, const std::vector<StringRef> &roots) { 185 return getSearchPaths(OPT_L, args, roots, {"/usr/lib", "/usr/local/lib"}); 186 } 187 188 static std::vector<StringRef> 189 getFrameworkSearchPaths(InputArgList &args, 190 const std::vector<StringRef> &roots) { 191 return getSearchPaths(OPT_F, args, roots, 192 {"/Library/Frameworks", "/System/Library/Frameworks"}); 193 } 194 195 namespace { 196 struct ArchiveMember { 197 MemoryBufferRef mbref; 198 uint32_t modTime; 199 }; 200 } // namespace 201 202 // Returns slices of MB by parsing MB as an archive file. 203 // Each slice consists of a member file in the archive. 204 static std::vector<ArchiveMember> getArchiveMembers(MemoryBufferRef mb) { 205 std::unique_ptr<Archive> file = 206 CHECK(Archive::create(mb), 207 mb.getBufferIdentifier() + ": failed to parse archive"); 208 Archive *archive = file.get(); 209 make<std::unique_ptr<Archive>>(std::move(file)); // take ownership 210 211 std::vector<ArchiveMember> v; 212 Error err = Error::success(); 213 214 // Thin archives refer to .o files, so --reproduce needs the .o files too. 215 bool addToTar = archive->isThin() && tar; 216 217 for (const Archive::Child &c : archive->children(err)) { 218 MemoryBufferRef mbref = 219 CHECK(c.getMemoryBufferRef(), 220 mb.getBufferIdentifier() + 221 ": could not get the buffer for a child of the archive"); 222 if (addToTar) 223 tar->append(relativeToRoot(check(c.getFullName())), mbref.getBuffer()); 224 uint32_t modTime = toTimeT( 225 CHECK(c.getLastModified(), mb.getBufferIdentifier() + 226 ": could not get the modification " 227 "time for a child of the archive")); 228 v.push_back({mbref, modTime}); 229 } 230 if (err) 231 fatal(mb.getBufferIdentifier() + 232 ": Archive::children failed: " + toString(std::move(err))); 233 234 return v; 235 } 236 237 static InputFile *addFile(StringRef path, bool forceLoadArchive, 238 bool isBundleLoader = false) { 239 Optional<MemoryBufferRef> buffer = readFile(path); 240 if (!buffer) 241 return nullptr; 242 MemoryBufferRef mbref = *buffer; 243 InputFile *newFile = nullptr; 244 245 file_magic magic = identify_magic(mbref.getBuffer()); 246 switch (magic) { 247 case file_magic::archive: { 248 std::unique_ptr<object::Archive> file = CHECK( 249 object::Archive::create(mbref), path + ": failed to parse archive"); 250 251 if (!file->isEmpty() && !file->hasSymbolTable()) 252 error(path + ": archive has no index; run ranlib to add one"); 253 254 if (config->allLoad || forceLoadArchive) { 255 if (Optional<MemoryBufferRef> buffer = readFile(path)) { 256 for (const ArchiveMember &member : getArchiveMembers(*buffer)) { 257 if (Optional<InputFile *> file = loadArchiveMember( 258 member.mbref, member.modTime, path, /*objCOnly=*/false)) { 259 inputFiles.insert(*file); 260 printArchiveMemberLoad( 261 (forceLoadArchive ? "-force_load" : "-all_load"), 262 inputFiles.back()); 263 } 264 } 265 } 266 } else if (config->forceLoadObjC) { 267 for (const object::Archive::Symbol &sym : file->symbols()) 268 if (sym.getName().startswith(objc::klass)) 269 symtab->addUndefined(sym.getName(), /*file=*/nullptr, 270 /*isWeakRef=*/false); 271 272 // TODO: no need to look for ObjC sections for a given archive member if 273 // we already found that it contains an ObjC symbol. We should also 274 // consider creating a LazyObjFile class in order to avoid double-loading 275 // these files here and below (as part of the ArchiveFile). 276 if (Optional<MemoryBufferRef> buffer = readFile(path)) { 277 for (const ArchiveMember &member : getArchiveMembers(*buffer)) { 278 if (Optional<InputFile *> file = loadArchiveMember( 279 member.mbref, member.modTime, path, /*objCOnly=*/true)) { 280 inputFiles.insert(*file); 281 printArchiveMemberLoad("-ObjC", inputFiles.back()); 282 } 283 } 284 } 285 } 286 287 newFile = make<ArchiveFile>(std::move(file)); 288 break; 289 } 290 case file_magic::macho_object: 291 newFile = make<ObjFile>(mbref, getModTime(path), ""); 292 break; 293 case file_magic::macho_dynamically_linked_shared_lib: 294 case file_magic::macho_dynamically_linked_shared_lib_stub: 295 case file_magic::tapi_file: 296 if (Optional<DylibFile *> dylibFile = loadDylib(mbref)) 297 newFile = *dylibFile; 298 break; 299 case file_magic::bitcode: 300 newFile = make<BitcodeFile>(mbref); 301 break; 302 case file_magic::macho_executable: 303 case file_magic::macho_bundle: 304 // We only allow executable and bundle type here if it is used 305 // as a bundle loader. 306 if (!isBundleLoader) 307 error(path + ": unhandled file type"); 308 if (Optional<DylibFile *> dylibFile = 309 loadDylib(mbref, nullptr, isBundleLoader)) 310 newFile = *dylibFile; 311 break; 312 default: 313 error(path + ": unhandled file type"); 314 } 315 if (newFile) { 316 // printArchiveMemberLoad() prints both .a and .o names, so no need to 317 // print the .a name here. 318 if (config->printEachFile && magic != file_magic::archive) 319 message(toString(newFile)); 320 inputFiles.insert(newFile); 321 } 322 return newFile; 323 } 324 325 static void addLibrary(StringRef name, bool isWeak) { 326 if (Optional<StringRef> path = findLibrary(name)) { 327 auto *dylibFile = dyn_cast_or_null<DylibFile>(addFile(*path, false)); 328 if (isWeak && dylibFile) 329 dylibFile->forceWeakImport = true; 330 return; 331 } 332 error("library not found for -l" + name); 333 } 334 335 static void addFramework(StringRef name, bool isWeak) { 336 if (Optional<std::string> path = findFramework(name)) { 337 auto *dylibFile = dyn_cast_or_null<DylibFile>(addFile(*path, false)); 338 if (isWeak && dylibFile) 339 dylibFile->forceWeakImport = true; 340 return; 341 } 342 error("framework not found for -framework " + name); 343 } 344 345 // Parses LC_LINKER_OPTION contents, which can add additional command line 346 // flags. 347 void macho::parseLCLinkerOption(InputFile *f, unsigned argc, StringRef data) { 348 SmallVector<const char *, 4> argv; 349 size_t offset = 0; 350 for (unsigned i = 0; i < argc && offset < data.size(); ++i) { 351 argv.push_back(data.data() + offset); 352 offset += strlen(data.data() + offset) + 1; 353 } 354 if (argv.size() != argc || offset > data.size()) 355 fatal(toString(f) + ": invalid LC_LINKER_OPTION"); 356 357 MachOOptTable table; 358 unsigned missingIndex, missingCount; 359 InputArgList args = table.ParseArgs(argv, missingIndex, missingCount); 360 if (missingCount) 361 fatal(Twine(args.getArgString(missingIndex)) + ": missing argument"); 362 for (const Arg *arg : args.filtered(OPT_UNKNOWN)) 363 error("unknown argument: " + arg->getAsString(args)); 364 365 for (const Arg *arg : args) { 366 switch (arg->getOption().getID()) { 367 case OPT_l: 368 addLibrary(arg->getValue(), false); 369 break; 370 case OPT_framework: 371 addFramework(arg->getValue(), false); 372 break; 373 default: 374 error(arg->getSpelling() + " is not allowed in LC_LINKER_OPTION"); 375 } 376 } 377 } 378 379 static void addFileList(StringRef path) { 380 Optional<MemoryBufferRef> buffer = readFile(path); 381 if (!buffer) 382 return; 383 MemoryBufferRef mbref = *buffer; 384 for (StringRef path : args::getLines(mbref)) 385 addFile(rerootPath(path), false); 386 } 387 388 // An order file has one entry per line, in the following format: 389 // 390 // <cpu>:<object file>:<symbol name> 391 // 392 // <cpu> and <object file> are optional. If not specified, then that entry 393 // matches any symbol of that name. Parsing this format is not quite 394 // straightforward because the symbol name itself can contain colons, so when 395 // encountering a colon, we consider the preceding characters to decide if it 396 // can be a valid CPU type or file path. 397 // 398 // If a symbol is matched by multiple entries, then it takes the lowest-ordered 399 // entry (the one nearest to the front of the list.) 400 // 401 // The file can also have line comments that start with '#'. 402 static void parseOrderFile(StringRef path) { 403 Optional<MemoryBufferRef> buffer = readFile(path); 404 if (!buffer) { 405 error("Could not read order file at " + path); 406 return; 407 } 408 409 MemoryBufferRef mbref = *buffer; 410 size_t priority = std::numeric_limits<size_t>::max(); 411 for (StringRef line : args::getLines(mbref)) { 412 StringRef objectFile, symbol; 413 line = line.take_until([](char c) { return c == '#'; }); // ignore comments 414 line = line.ltrim(); 415 416 CPUType cpuType = StringSwitch<CPUType>(line) 417 .StartsWith("i386:", CPU_TYPE_I386) 418 .StartsWith("x86_64:", CPU_TYPE_X86_64) 419 .StartsWith("arm:", CPU_TYPE_ARM) 420 .StartsWith("arm64:", CPU_TYPE_ARM64) 421 .StartsWith("ppc:", CPU_TYPE_POWERPC) 422 .StartsWith("ppc64:", CPU_TYPE_POWERPC64) 423 .Default(CPU_TYPE_ANY); 424 425 if (cpuType != CPU_TYPE_ANY && cpuType != target->cpuType) 426 continue; 427 428 // Drop the CPU type as well as the colon 429 if (cpuType != CPU_TYPE_ANY) 430 line = line.drop_until([](char c) { return c == ':'; }).drop_front(); 431 432 constexpr std::array<StringRef, 2> fileEnds = {".o:", ".o):"}; 433 for (StringRef fileEnd : fileEnds) { 434 size_t pos = line.find(fileEnd); 435 if (pos != StringRef::npos) { 436 // Split the string around the colon 437 objectFile = line.take_front(pos + fileEnd.size() - 1); 438 line = line.drop_front(pos + fileEnd.size()); 439 break; 440 } 441 } 442 symbol = line.trim(); 443 444 if (!symbol.empty()) { 445 SymbolPriorityEntry &entry = config->priorities[symbol]; 446 if (!objectFile.empty()) 447 entry.objectFiles.insert(std::make_pair(objectFile, priority)); 448 else 449 entry.anyObjectFile = std::max(entry.anyObjectFile, priority); 450 } 451 452 --priority; 453 } 454 } 455 456 // We expect sub-library names of the form "libfoo", which will match a dylib 457 // with a path of .*/libfoo.{dylib, tbd}. 458 // XXX ld64 seems to ignore the extension entirely when matching sub-libraries; 459 // I'm not sure what the use case for that is. 460 static bool markReexport(StringRef searchName, ArrayRef<StringRef> extensions) { 461 for (InputFile *file : inputFiles) { 462 if (auto *dylibFile = dyn_cast<DylibFile>(file)) { 463 StringRef filename = path::filename(dylibFile->getName()); 464 if (filename.consume_front(searchName) && 465 (filename.empty() || 466 find(extensions, filename) != extensions.end())) { 467 dylibFile->reexport = true; 468 return true; 469 } 470 } 471 } 472 return false; 473 } 474 475 // This function is called on startup. We need this for LTO since 476 // LTO calls LLVM functions to compile bitcode files to native code. 477 // Technically this can be delayed until we read bitcode files, but 478 // we don't bother to do lazily because the initialization is fast. 479 static void initLLVM() { 480 InitializeAllTargets(); 481 InitializeAllTargetMCs(); 482 InitializeAllAsmPrinters(); 483 InitializeAllAsmParsers(); 484 } 485 486 static void compileBitcodeFiles() { 487 TimeTraceScope timeScope("LTO"); 488 auto *lto = make<BitcodeCompiler>(); 489 for (InputFile *file : inputFiles) 490 if (auto *bitcodeFile = dyn_cast<BitcodeFile>(file)) 491 lto->add(*bitcodeFile); 492 493 for (ObjFile *file : lto->compile()) 494 inputFiles.insert(file); 495 } 496 497 // Replaces common symbols with defined symbols residing in __common sections. 498 // This function must be called after all symbol names are resolved (i.e. after 499 // all InputFiles have been loaded.) As a result, later operations won't see 500 // any CommonSymbols. 501 static void replaceCommonSymbols() { 502 TimeTraceScope timeScope("Replace common symbols"); 503 for (Symbol *sym : symtab->getSymbols()) { 504 auto *common = dyn_cast<CommonSymbol>(sym); 505 if (common == nullptr) 506 continue; 507 508 auto *isec = make<InputSection>(); 509 isec->file = common->getFile(); 510 isec->name = section_names::common; 511 isec->segname = segment_names::data; 512 isec->align = common->align; 513 // Casting to size_t will truncate large values on 32-bit architectures, 514 // but it's not really worth supporting the linking of 64-bit programs on 515 // 32-bit archs. 516 isec->data = {nullptr, static_cast<size_t>(common->size)}; 517 isec->flags = S_ZEROFILL; 518 inputSections.push_back(isec); 519 520 replaceSymbol<Defined>(sym, sym->getName(), isec->file, isec, /*value=*/0, 521 /*size=*/0, 522 /*isWeakDef=*/false, 523 /*isExternal=*/true, common->privateExtern, 524 /*isThumb=*/false); 525 } 526 } 527 528 static void initializeSectionRenameMap() { 529 if (config->dataConst) { 530 SmallVector<StringRef> v{section_names::got, 531 section_names::authGot, 532 section_names::authPtr, 533 section_names::nonLazySymbolPtr, 534 section_names::const_, 535 section_names::cfString, 536 section_names::moduleInitFunc, 537 section_names::moduleTermFunc, 538 section_names::objcClassList, 539 section_names::objcNonLazyClassList, 540 section_names::objcCatList, 541 section_names::objcNonLazyCatList, 542 section_names::objcProtoList, 543 section_names::objcImageInfo}; 544 for (StringRef s : v) 545 config->sectionRenameMap[{segment_names::data, s}] = { 546 segment_names::dataConst, s}; 547 } 548 config->sectionRenameMap[{segment_names::text, section_names::staticInit}] = { 549 segment_names::text, section_names::text}; 550 config->sectionRenameMap[{segment_names::import, section_names::pointers}] = { 551 config->dataConst ? segment_names::dataConst : segment_names::data, 552 section_names::nonLazySymbolPtr}; 553 } 554 555 static inline char toLowerDash(char x) { 556 if (x >= 'A' && x <= 'Z') 557 return x - 'A' + 'a'; 558 else if (x == ' ') 559 return '-'; 560 return x; 561 } 562 563 static std::string lowerDash(StringRef s) { 564 return std::string(map_iterator(s.begin(), toLowerDash), 565 map_iterator(s.end(), toLowerDash)); 566 } 567 568 // Has the side-effect of setting Config::platformInfo. 569 static PlatformKind parsePlatformVersion(const ArgList &args) { 570 const Arg *arg = args.getLastArg(OPT_platform_version); 571 if (!arg) { 572 error("must specify -platform_version"); 573 return PlatformKind::unknown; 574 } 575 576 StringRef platformStr = arg->getValue(0); 577 StringRef minVersionStr = arg->getValue(1); 578 StringRef sdkVersionStr = arg->getValue(2); 579 580 // TODO(compnerd) see if we can generate this case list via XMACROS 581 PlatformKind platform = 582 StringSwitch<PlatformKind>(lowerDash(platformStr)) 583 .Cases("macos", "1", PlatformKind::macOS) 584 .Cases("ios", "2", PlatformKind::iOS) 585 .Cases("tvos", "3", PlatformKind::tvOS) 586 .Cases("watchos", "4", PlatformKind::watchOS) 587 .Cases("bridgeos", "5", PlatformKind::bridgeOS) 588 .Cases("mac-catalyst", "6", PlatformKind::macCatalyst) 589 .Cases("ios-simulator", "7", PlatformKind::iOSSimulator) 590 .Cases("tvos-simulator", "8", PlatformKind::tvOSSimulator) 591 .Cases("watchos-simulator", "9", PlatformKind::watchOSSimulator) 592 .Cases("driverkit", "10", PlatformKind::driverKit) 593 .Default(PlatformKind::unknown); 594 if (platform == PlatformKind::unknown) 595 error(Twine("malformed platform: ") + platformStr); 596 // TODO: check validity of version strings, which varies by platform 597 // NOTE: ld64 accepts version strings with 5 components 598 // llvm::VersionTuple accepts no more than 4 components 599 // Has Apple ever published version strings with 5 components? 600 if (config->platformInfo.minimum.tryParse(minVersionStr)) 601 error(Twine("malformed minimum version: ") + minVersionStr); 602 if (config->platformInfo.sdk.tryParse(sdkVersionStr)) 603 error(Twine("malformed sdk version: ") + sdkVersionStr); 604 return platform; 605 } 606 607 // Has the side-effect of setting Config::target. 608 static TargetInfo *createTargetInfo(InputArgList &args) { 609 StringRef archName = args.getLastArgValue(OPT_arch); 610 if (archName.empty()) 611 fatal("must specify -arch"); 612 PlatformKind platform = parsePlatformVersion(args); 613 614 config->platformInfo.target = 615 MachO::Target(getArchitectureFromName(archName), platform); 616 617 uint32_t cpuType; 618 uint32_t cpuSubtype; 619 std::tie(cpuType, cpuSubtype) = getCPUTypeFromArchitecture(config->arch()); 620 621 switch (cpuType) { 622 case CPU_TYPE_X86_64: 623 return createX86_64TargetInfo(); 624 case CPU_TYPE_ARM64: 625 return createARM64TargetInfo(); 626 case CPU_TYPE_ARM64_32: 627 return createARM64_32TargetInfo(); 628 case CPU_TYPE_ARM: 629 return createARMTargetInfo(cpuSubtype); 630 default: 631 fatal("missing or unsupported -arch " + archName); 632 } 633 } 634 635 static UndefinedSymbolTreatment 636 getUndefinedSymbolTreatment(const ArgList &args) { 637 StringRef treatmentStr = args.getLastArgValue(OPT_undefined); 638 auto treatment = 639 StringSwitch<UndefinedSymbolTreatment>(treatmentStr) 640 .Cases("error", "", UndefinedSymbolTreatment::error) 641 .Case("warning", UndefinedSymbolTreatment::warning) 642 .Case("suppress", UndefinedSymbolTreatment::suppress) 643 .Case("dynamic_lookup", UndefinedSymbolTreatment::dynamic_lookup) 644 .Default(UndefinedSymbolTreatment::unknown); 645 if (treatment == UndefinedSymbolTreatment::unknown) { 646 warn(Twine("unknown -undefined TREATMENT '") + treatmentStr + 647 "', defaulting to 'error'"); 648 treatment = UndefinedSymbolTreatment::error; 649 } else if (config->namespaceKind == NamespaceKind::twolevel && 650 (treatment == UndefinedSymbolTreatment::warning || 651 treatment == UndefinedSymbolTreatment::suppress)) { 652 if (treatment == UndefinedSymbolTreatment::warning) 653 error("'-undefined warning' only valid with '-flat_namespace'"); 654 else 655 error("'-undefined suppress' only valid with '-flat_namespace'"); 656 treatment = UndefinedSymbolTreatment::error; 657 } 658 return treatment; 659 } 660 661 static void warnIfDeprecatedOption(const Option &opt) { 662 if (!opt.getGroup().isValid()) 663 return; 664 if (opt.getGroup().getID() == OPT_grp_deprecated) { 665 warn("Option `" + opt.getPrefixedName() + "' is deprecated in ld64:"); 666 warn(opt.getHelpText()); 667 } 668 } 669 670 static void warnIfUnimplementedOption(const Option &opt) { 671 if (!opt.getGroup().isValid() || !opt.hasFlag(DriverFlag::HelpHidden)) 672 return; 673 switch (opt.getGroup().getID()) { 674 case OPT_grp_deprecated: 675 // warn about deprecated options elsewhere 676 break; 677 case OPT_grp_undocumented: 678 warn("Option `" + opt.getPrefixedName() + 679 "' is undocumented. Should lld implement it?"); 680 break; 681 case OPT_grp_obsolete: 682 warn("Option `" + opt.getPrefixedName() + 683 "' is obsolete. Please modernize your usage."); 684 break; 685 case OPT_grp_ignored: 686 warn("Option `" + opt.getPrefixedName() + "' is ignored."); 687 break; 688 default: 689 warn("Option `" + opt.getPrefixedName() + 690 "' is not yet implemented. Stay tuned..."); 691 break; 692 } 693 } 694 695 static const char *getReproduceOption(InputArgList &args) { 696 if (const Arg *arg = args.getLastArg(OPT_reproduce)) 697 return arg->getValue(); 698 return getenv("LLD_REPRODUCE"); 699 } 700 701 static void parseClangOption(StringRef opt, const Twine &msg) { 702 std::string err; 703 raw_string_ostream os(err); 704 705 const char *argv[] = {"lld", opt.data()}; 706 if (cl::ParseCommandLineOptions(2, argv, "", &os)) 707 return; 708 os.flush(); 709 error(msg + ": " + StringRef(err).trim()); 710 } 711 712 static uint32_t parseDylibVersion(const ArgList &args, unsigned id) { 713 const Arg *arg = args.getLastArg(id); 714 if (!arg) 715 return 0; 716 717 if (config->outputType != MH_DYLIB) { 718 error(arg->getAsString(args) + ": only valid with -dylib"); 719 return 0; 720 } 721 722 PackedVersion version; 723 if (!version.parse32(arg->getValue())) { 724 error(arg->getAsString(args) + ": malformed version"); 725 return 0; 726 } 727 728 return version.rawValue(); 729 } 730 731 static uint32_t parseProtection(StringRef protStr) { 732 uint32_t prot = 0; 733 for (char c : protStr) { 734 switch (c) { 735 case 'r': 736 prot |= VM_PROT_READ; 737 break; 738 case 'w': 739 prot |= VM_PROT_WRITE; 740 break; 741 case 'x': 742 prot |= VM_PROT_EXECUTE; 743 break; 744 case '-': 745 break; 746 default: 747 error("unknown -segprot letter '" + Twine(c) + "' in " + protStr); 748 return 0; 749 } 750 } 751 return prot; 752 } 753 754 static std::vector<SectionAlign> parseSectAlign(const opt::InputArgList &args) { 755 std::vector<SectionAlign> sectAligns; 756 for (const Arg *arg : args.filtered(OPT_sectalign)) { 757 StringRef segName = arg->getValue(0); 758 StringRef sectName = arg->getValue(1); 759 StringRef alignStr = arg->getValue(2); 760 if (alignStr.startswith("0x") || alignStr.startswith("0X")) 761 alignStr = alignStr.drop_front(2); 762 uint32_t align; 763 if (alignStr.getAsInteger(16, align)) { 764 error("-sectalign: failed to parse '" + StringRef(arg->getValue(2)) + 765 "' as number"); 766 continue; 767 } 768 if (!isPowerOf2_32(align)) { 769 error("-sectalign: '" + StringRef(arg->getValue(2)) + 770 "' (in base 16) not a power of two"); 771 continue; 772 } 773 sectAligns.push_back({segName, sectName, align}); 774 } 775 return sectAligns; 776 } 777 778 static bool dataConstDefault(const InputArgList &args) { 779 switch (config->outputType) { 780 case MH_EXECUTE: 781 return !args.hasArg(OPT_no_pie); 782 case MH_BUNDLE: 783 // FIXME: return false when -final_name ... 784 // has prefix "/System/Library/UserEventPlugins/" 785 // or matches "/usr/libexec/locationd" "/usr/libexec/terminusd" 786 return true; 787 case MH_DYLIB: 788 return true; 789 case MH_OBJECT: 790 return false; 791 default: 792 llvm_unreachable( 793 "unsupported output type for determining data-const default"); 794 } 795 return false; 796 } 797 798 void SymbolPatterns::clear() { 799 literals.clear(); 800 globs.clear(); 801 } 802 803 void SymbolPatterns::insert(StringRef symbolName) { 804 if (symbolName.find_first_of("*?[]") == StringRef::npos) 805 literals.insert(CachedHashStringRef(symbolName)); 806 else if (Expected<GlobPattern> pattern = GlobPattern::create(symbolName)) 807 globs.emplace_back(*pattern); 808 else 809 error("invalid symbol-name pattern: " + symbolName); 810 } 811 812 bool SymbolPatterns::matchLiteral(StringRef symbolName) const { 813 return literals.contains(CachedHashStringRef(symbolName)); 814 } 815 816 bool SymbolPatterns::matchGlob(StringRef symbolName) const { 817 for (const llvm::GlobPattern &glob : globs) 818 if (glob.match(symbolName)) 819 return true; 820 return false; 821 } 822 823 bool SymbolPatterns::match(StringRef symbolName) const { 824 return matchLiteral(symbolName) || matchGlob(symbolName); 825 } 826 827 static void handleSymbolPatterns(InputArgList &args, 828 SymbolPatterns &symbolPatterns, 829 unsigned singleOptionCode, 830 unsigned listFileOptionCode) { 831 for (const Arg *arg : args.filtered(singleOptionCode)) 832 symbolPatterns.insert(arg->getValue()); 833 for (const Arg *arg : args.filtered(listFileOptionCode)) { 834 StringRef path = arg->getValue(); 835 Optional<MemoryBufferRef> buffer = readFile(path); 836 if (!buffer) { 837 error("Could not read symbol file: " + path); 838 continue; 839 } 840 MemoryBufferRef mbref = *buffer; 841 for (StringRef line : args::getLines(mbref)) { 842 line = line.take_until([](char c) { return c == '#'; }).trim(); 843 if (!line.empty()) 844 symbolPatterns.insert(line); 845 } 846 } 847 } 848 849 void createFiles(const InputArgList &args) { 850 TimeTraceScope timeScope("Load input files"); 851 // This loop should be reserved for options whose exact ordering matters. 852 // Other options should be handled via filtered() and/or getLastArg(). 853 for (const Arg *arg : args) { 854 const Option &opt = arg->getOption(); 855 warnIfDeprecatedOption(opt); 856 warnIfUnimplementedOption(opt); 857 858 switch (opt.getID()) { 859 case OPT_INPUT: 860 addFile(rerootPath(arg->getValue()), false); 861 break; 862 case OPT_weak_library: 863 if (auto *dylibFile = dyn_cast_or_null<DylibFile>( 864 addFile(rerootPath(arg->getValue()), false))) 865 dylibFile->forceWeakImport = true; 866 break; 867 case OPT_filelist: 868 addFileList(arg->getValue()); 869 break; 870 case OPT_force_load: 871 addFile(rerootPath(arg->getValue()), true); 872 break; 873 case OPT_l: 874 case OPT_weak_l: 875 addLibrary(arg->getValue(), opt.getID() == OPT_weak_l); 876 break; 877 case OPT_framework: 878 case OPT_weak_framework: 879 addFramework(arg->getValue(), opt.getID() == OPT_weak_framework); 880 break; 881 default: 882 break; 883 } 884 } 885 } 886 887 bool macho::link(ArrayRef<const char *> argsArr, bool canExitEarly, 888 raw_ostream &stdoutOS, raw_ostream &stderrOS) { 889 lld::stdoutOS = &stdoutOS; 890 lld::stderrOS = &stderrOS; 891 892 errorHandler().cleanupCallback = []() { freeArena(); }; 893 894 errorHandler().logName = args::getFilenameWithoutExe(argsArr[0]); 895 stderrOS.enable_colors(stderrOS.has_colors()); 896 897 MachOOptTable parser; 898 InputArgList args = parser.parse(argsArr.slice(1)); 899 900 errorHandler().errorLimitExceededMsg = 901 "too many errors emitted, stopping now " 902 "(use --error-limit=0 to see all errors)"; 903 errorHandler().errorLimit = args::getInteger(args, OPT_error_limit_eq, 20); 904 errorHandler().verbose = args.hasArg(OPT_verbose); 905 906 if (args.hasArg(OPT_help_hidden)) { 907 parser.printHelp(argsArr[0], /*showHidden=*/true); 908 return true; 909 } 910 if (args.hasArg(OPT_help)) { 911 parser.printHelp(argsArr[0], /*showHidden=*/false); 912 return true; 913 } 914 if (args.hasArg(OPT_version)) { 915 message(getLLDVersion()); 916 return true; 917 } 918 919 config = make<Configuration>(); 920 symtab = make<SymbolTable>(); 921 target = createTargetInfo(args); 922 depTracker = 923 make<DependencyTracker>(args.getLastArgValue(OPT_dependency_info)); 924 925 config->systemLibraryRoots = getSystemLibraryRoots(args); 926 if (const char *path = getReproduceOption(args)) { 927 // Note that --reproduce is a debug option so you can ignore it 928 // if you are trying to understand the whole picture of the code. 929 Expected<std::unique_ptr<TarWriter>> errOrWriter = 930 TarWriter::create(path, path::stem(path)); 931 if (errOrWriter) { 932 tar = std::move(*errOrWriter); 933 tar->append("response.txt", createResponseFile(args)); 934 tar->append("version.txt", getLLDVersion() + "\n"); 935 } else { 936 error("--reproduce: " + toString(errOrWriter.takeError())); 937 } 938 } 939 940 if (auto *arg = args.getLastArg(OPT_threads_eq)) { 941 StringRef v(arg->getValue()); 942 unsigned threads = 0; 943 if (!llvm::to_integer(v, threads, 0) || threads == 0) 944 error(arg->getSpelling() + ": expected a positive integer, but got '" + 945 arg->getValue() + "'"); 946 parallel::strategy = hardware_concurrency(threads); 947 config->thinLTOJobs = v; 948 } 949 if (auto *arg = args.getLastArg(OPT_thinlto_jobs_eq)) 950 config->thinLTOJobs = arg->getValue(); 951 if (!get_threadpool_strategy(config->thinLTOJobs)) 952 error("--thinlto-jobs: invalid job count: " + config->thinLTOJobs); 953 954 for (const Arg *arg : args.filtered(OPT_u)) { 955 config->explicitUndefineds.push_back(symtab->addUndefined( 956 arg->getValue(), /*file=*/nullptr, /*isWeakRef=*/false)); 957 } 958 959 for (const Arg *arg : args.filtered(OPT_U)) 960 symtab->addDynamicLookup(arg->getValue()); 961 962 config->mapFile = args.getLastArgValue(OPT_map); 963 config->outputFile = args.getLastArgValue(OPT_o, "a.out"); 964 config->astPaths = args.getAllArgValues(OPT_add_ast_path); 965 config->headerPad = args::getHex(args, OPT_headerpad, /*Default=*/32); 966 config->headerPadMaxInstallNames = 967 args.hasArg(OPT_headerpad_max_install_names); 968 config->printEachFile = args.hasArg(OPT_t); 969 config->printWhyLoad = args.hasArg(OPT_why_load); 970 config->outputType = getOutputType(args); 971 if (const Arg *arg = args.getLastArg(OPT_bundle_loader)) { 972 if (config->outputType != MH_BUNDLE) 973 error("-bundle_loader can only be used with MachO bundle output"); 974 addFile(arg->getValue(), false, true); 975 } 976 config->ltoObjPath = args.getLastArgValue(OPT_object_path_lto); 977 config->ltoNewPassManager = 978 args.hasFlag(OPT_no_lto_legacy_pass_manager, OPT_lto_legacy_pass_manager, 979 LLVM_ENABLE_NEW_PASS_MANAGER); 980 config->runtimePaths = args::getStrings(args, OPT_rpath); 981 config->allLoad = args.hasArg(OPT_all_load); 982 config->forceLoadObjC = args.hasArg(OPT_ObjC); 983 config->demangle = args.hasArg(OPT_demangle); 984 config->implicitDylibs = !args.hasArg(OPT_no_implicit_dylibs); 985 config->emitFunctionStarts = !args.hasArg(OPT_no_function_starts); 986 config->emitBitcodeBundle = args.hasArg(OPT_bitcode_bundle); 987 988 std::array<PlatformKind, 3> encryptablePlatforms{ 989 PlatformKind::iOS, PlatformKind::watchOS, PlatformKind::tvOS}; 990 config->emitEncryptionInfo = 991 args.hasFlag(OPT_encryptable, OPT_no_encryption, 992 is_contained(encryptablePlatforms, config->platform())); 993 994 #ifndef HAVE_LIBXAR 995 if (config->emitBitcodeBundle) 996 error("-bitcode_bundle unsupported because LLD wasn't built with libxar"); 997 #endif 998 999 if (const Arg *arg = args.getLastArg(OPT_install_name)) { 1000 if (config->outputType != MH_DYLIB) 1001 warn(arg->getAsString(args) + ": ignored, only has effect with -dylib"); 1002 else 1003 config->installName = arg->getValue(); 1004 } else if (config->outputType == MH_DYLIB) { 1005 config->installName = config->outputFile; 1006 } 1007 1008 if (args.hasArg(OPT_mark_dead_strippable_dylib)) { 1009 if (config->outputType != MH_DYLIB) 1010 warn("-mark_dead_strippable_dylib: ignored, only has effect with -dylib"); 1011 else 1012 config->markDeadStrippableDylib = true; 1013 } 1014 1015 if (const Arg *arg = args.getLastArg(OPT_static, OPT_dynamic)) 1016 config->staticLink = (arg->getOption().getID() == OPT_static); 1017 1018 if (const Arg *arg = 1019 args.getLastArg(OPT_flat_namespace, OPT_twolevel_namespace)) 1020 config->namespaceKind = arg->getOption().getID() == OPT_twolevel_namespace 1021 ? NamespaceKind::twolevel 1022 : NamespaceKind::flat; 1023 1024 config->undefinedSymbolTreatment = getUndefinedSymbolTreatment(args); 1025 1026 if (config->outputType == MH_EXECUTE) 1027 config->entry = symtab->addUndefined(args.getLastArgValue(OPT_e, "_main"), 1028 /*file=*/nullptr, 1029 /*isWeakRef=*/false); 1030 1031 config->librarySearchPaths = 1032 getLibrarySearchPaths(args, config->systemLibraryRoots); 1033 config->frameworkSearchPaths = 1034 getFrameworkSearchPaths(args, config->systemLibraryRoots); 1035 if (const Arg *arg = 1036 args.getLastArg(OPT_search_paths_first, OPT_search_dylibs_first)) 1037 config->searchDylibsFirst = 1038 arg->getOption().getID() == OPT_search_dylibs_first; 1039 1040 config->dylibCompatibilityVersion = 1041 parseDylibVersion(args, OPT_compatibility_version); 1042 config->dylibCurrentVersion = parseDylibVersion(args, OPT_current_version); 1043 1044 config->dataConst = 1045 args.hasFlag(OPT_data_const, OPT_no_data_const, dataConstDefault(args)); 1046 // Populate config->sectionRenameMap with builtin default renames. 1047 // Options -rename_section and -rename_segment are able to override. 1048 initializeSectionRenameMap(); 1049 // Reject every special character except '.' and '$' 1050 // TODO(gkm): verify that this is the proper set of invalid chars 1051 StringRef invalidNameChars("!\"#%&'()*+,-/:;<=>?@[\\]^`{|}~"); 1052 auto validName = [invalidNameChars](StringRef s) { 1053 if (s.find_first_of(invalidNameChars) != StringRef::npos) 1054 error("invalid name for segment or section: " + s); 1055 return s; 1056 }; 1057 for (const Arg *arg : args.filtered(OPT_rename_section)) { 1058 config->sectionRenameMap[{validName(arg->getValue(0)), 1059 validName(arg->getValue(1))}] = { 1060 validName(arg->getValue(2)), validName(arg->getValue(3))}; 1061 } 1062 for (const Arg *arg : args.filtered(OPT_rename_segment)) { 1063 config->segmentRenameMap[validName(arg->getValue(0))] = 1064 validName(arg->getValue(1)); 1065 } 1066 1067 config->sectionAlignments = parseSectAlign(args); 1068 1069 for (const Arg *arg : args.filtered(OPT_segprot)) { 1070 StringRef segName = arg->getValue(0); 1071 uint32_t maxProt = parseProtection(arg->getValue(1)); 1072 uint32_t initProt = parseProtection(arg->getValue(2)); 1073 if (maxProt != initProt && config->arch() != AK_i386) 1074 error("invalid argument '" + arg->getAsString(args) + 1075 "': max and init must be the same for non-i386 archs"); 1076 if (segName == segment_names::linkEdit) 1077 error("-segprot cannot be used to change __LINKEDIT's protections"); 1078 config->segmentProtections.push_back({segName, maxProt, initProt}); 1079 } 1080 1081 handleSymbolPatterns(args, config->exportedSymbols, OPT_exported_symbol, 1082 OPT_exported_symbols_list); 1083 handleSymbolPatterns(args, config->unexportedSymbols, OPT_unexported_symbol, 1084 OPT_unexported_symbols_list); 1085 if (!config->exportedSymbols.empty() && !config->unexportedSymbols.empty()) { 1086 error("cannot use both -exported_symbol* and -unexported_symbol* options\n" 1087 ">>> ignoring unexports"); 1088 config->unexportedSymbols.clear(); 1089 } 1090 // Explicitly-exported literal symbols must be defined, but might 1091 // languish in an archive if unreferenced elsewhere. Light a fire 1092 // under those lazy symbols! 1093 for (const CachedHashStringRef &cachedName : config->exportedSymbols.literals) 1094 symtab->addUndefined(cachedName.val(), /*file=*/nullptr, 1095 /*isWeakRef=*/false); 1096 1097 config->saveTemps = args.hasArg(OPT_save_temps); 1098 1099 config->adhocCodesign = args.hasFlag( 1100 OPT_adhoc_codesign, OPT_no_adhoc_codesign, 1101 (config->arch() == AK_arm64 || config->arch() == AK_arm64e) && 1102 config->platform() == PlatformKind::macOS); 1103 1104 if (args.hasArg(OPT_v)) { 1105 message(getLLDVersion()); 1106 message(StringRef("Library search paths:") + 1107 (config->librarySearchPaths.empty() 1108 ? "" 1109 : "\n\t" + join(config->librarySearchPaths, "\n\t"))); 1110 message(StringRef("Framework search paths:") + 1111 (config->frameworkSearchPaths.empty() 1112 ? "" 1113 : "\n\t" + join(config->frameworkSearchPaths, "\n\t"))); 1114 } 1115 1116 config->progName = argsArr[0]; 1117 1118 config->timeTraceEnabled = args.hasArg( 1119 OPT_time_trace, OPT_time_trace_granularity_eq, OPT_time_trace_file_eq); 1120 config->timeTraceGranularity = 1121 args::getInteger(args, OPT_time_trace_granularity_eq, 500); 1122 1123 // Initialize time trace profiler. 1124 if (config->timeTraceEnabled) 1125 timeTraceProfilerInitialize(config->timeTraceGranularity, config->progName); 1126 1127 { 1128 TimeTraceScope timeScope("ExecuteLinker"); 1129 1130 initLLVM(); // must be run before any call to addFile() 1131 createFiles(args); 1132 1133 config->isPic = config->outputType == MH_DYLIB || 1134 config->outputType == MH_BUNDLE || 1135 (config->outputType == MH_EXECUTE && 1136 args.hasFlag(OPT_pie, OPT_no_pie, true)); 1137 1138 // Now that all dylibs have been loaded, search for those that should be 1139 // re-exported. 1140 { 1141 auto reexportHandler = [](const Arg *arg, 1142 const std::vector<StringRef> &extensions) { 1143 config->hasReexports = true; 1144 StringRef searchName = arg->getValue(); 1145 if (!markReexport(searchName, extensions)) 1146 error(arg->getSpelling() + " " + searchName + 1147 " does not match a supplied dylib"); 1148 }; 1149 std::vector<StringRef> extensions = {".tbd"}; 1150 for (const Arg *arg : args.filtered(OPT_sub_umbrella)) 1151 reexportHandler(arg, extensions); 1152 1153 extensions.push_back(".dylib"); 1154 for (const Arg *arg : args.filtered(OPT_sub_library)) 1155 reexportHandler(arg, extensions); 1156 } 1157 1158 // Parse LTO options. 1159 if (const Arg *arg = args.getLastArg(OPT_mcpu)) 1160 parseClangOption(saver.save("-mcpu=" + StringRef(arg->getValue())), 1161 arg->getSpelling()); 1162 1163 for (const Arg *arg : args.filtered(OPT_mllvm)) 1164 parseClangOption(arg->getValue(), arg->getSpelling()); 1165 1166 compileBitcodeFiles(); 1167 replaceCommonSymbols(); 1168 1169 StringRef orderFile = args.getLastArgValue(OPT_order_file); 1170 if (!orderFile.empty()) 1171 parseOrderFile(orderFile); 1172 1173 if (config->entry) 1174 if (auto *undefined = dyn_cast<Undefined>(config->entry)) 1175 treatUndefinedSymbol(*undefined, "the entry point"); 1176 1177 // FIXME: This prints symbols that are undefined both in input files and 1178 // via -u flag twice. 1179 for (const Symbol *sym : config->explicitUndefineds) { 1180 if (const auto *undefined = dyn_cast<Undefined>(sym)) 1181 treatUndefinedSymbol(*undefined, "-u"); 1182 } 1183 // Literal exported-symbol names must be defined, but glob 1184 // patterns need not match. 1185 for (const CachedHashStringRef &cachedName : 1186 config->exportedSymbols.literals) { 1187 if (const Symbol *sym = symtab->find(cachedName)) 1188 if (const auto *undefined = dyn_cast<Undefined>(sym)) 1189 treatUndefinedSymbol(*undefined, "-exported_symbol(s_list)"); 1190 } 1191 1192 // FIXME: should terminate the link early based on errors encountered so 1193 // far? 1194 1195 createSyntheticSections(); 1196 createSyntheticSymbols(); 1197 1198 for (const Arg *arg : args.filtered(OPT_sectcreate)) { 1199 StringRef segName = arg->getValue(0); 1200 StringRef sectName = arg->getValue(1); 1201 StringRef fileName = arg->getValue(2); 1202 Optional<MemoryBufferRef> buffer = readFile(fileName); 1203 if (buffer) 1204 inputFiles.insert(make<OpaqueFile>(*buffer, segName, sectName)); 1205 } 1206 1207 { 1208 TimeTraceScope timeScope("Gathering input sections"); 1209 // Gather all InputSections into one vector. 1210 for (const InputFile *file : inputFiles) { 1211 for (const SubsectionMap &map : file->subsections) 1212 for (const SubsectionEntry &subsectionEntry : map) 1213 inputSections.push_back(subsectionEntry.isec); 1214 } 1215 } 1216 1217 // Write to an output file. 1218 if (target->wordSize == 8) 1219 writeResult<LP64>(); 1220 else 1221 writeResult<ILP32>(); 1222 1223 depTracker->write(getLLDVersion(), inputFiles, config->outputFile); 1224 } 1225 1226 if (config->timeTraceEnabled) { 1227 if (auto E = timeTraceProfilerWrite( 1228 args.getLastArgValue(OPT_time_trace_file_eq).str(), 1229 config->outputFile)) { 1230 handleAllErrors(std::move(E), 1231 [&](const StringError &SE) { error(SE.getMessage()); }); 1232 } 1233 1234 timeTraceProfilerCleanup(); 1235 } 1236 1237 if (canExitEarly) 1238 exitLld(errorCount() ? 1 : 0); 1239 1240 return !errorCount(); 1241 } 1242