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 "ICF.h" 12 #include "InputFiles.h" 13 #include "LTO.h" 14 #include "MarkLive.h" 15 #include "ObjC.h" 16 #include "OutputSection.h" 17 #include "OutputSegment.h" 18 #include "SectionPriorities.h" 19 #include "SymbolTable.h" 20 #include "Symbols.h" 21 #include "SyntheticSections.h" 22 #include "Target.h" 23 #include "UnwindInfoSection.h" 24 #include "Writer.h" 25 26 #include "lld/Common/Args.h" 27 #include "lld/Common/Driver.h" 28 #include "lld/Common/ErrorHandler.h" 29 #include "lld/Common/LLVM.h" 30 #include "lld/Common/Memory.h" 31 #include "lld/Common/Reproduce.h" 32 #include "lld/Common/Version.h" 33 #include "llvm/ADT/DenseSet.h" 34 #include "llvm/ADT/StringExtras.h" 35 #include "llvm/ADT/StringRef.h" 36 #include "llvm/BinaryFormat/MachO.h" 37 #include "llvm/BinaryFormat/Magic.h" 38 #include "llvm/Config/llvm-config.h" 39 #include "llvm/LTO/LTO.h" 40 #include "llvm/Object/Archive.h" 41 #include "llvm/Option/ArgList.h" 42 #include "llvm/Support/CommandLine.h" 43 #include "llvm/Support/FileSystem.h" 44 #include "llvm/Support/Host.h" 45 #include "llvm/Support/MemoryBuffer.h" 46 #include "llvm/Support/Parallel.h" 47 #include "llvm/Support/Path.h" 48 #include "llvm/Support/TarWriter.h" 49 #include "llvm/Support/TargetSelect.h" 50 #include "llvm/Support/TimeProfiler.h" 51 #include "llvm/TextAPI/PackedVersion.h" 52 53 #include <algorithm> 54 55 using namespace llvm; 56 using namespace llvm::MachO; 57 using namespace llvm::object; 58 using namespace llvm::opt; 59 using namespace llvm::sys; 60 using namespace lld; 61 using namespace lld::macho; 62 63 std::unique_ptr<Configuration> macho::config; 64 std::unique_ptr<DependencyTracker> macho::depTracker; 65 66 static HeaderFileType getOutputType(const InputArgList &args) { 67 // TODO: -r, -dylinker, -preload... 68 Arg *outputArg = args.getLastArg(OPT_bundle, OPT_dylib, OPT_execute); 69 if (outputArg == nullptr) 70 return MH_EXECUTE; 71 72 switch (outputArg->getOption().getID()) { 73 case OPT_bundle: 74 return MH_BUNDLE; 75 case OPT_dylib: 76 return MH_DYLIB; 77 case OPT_execute: 78 return MH_EXECUTE; 79 default: 80 llvm_unreachable("internal error"); 81 } 82 } 83 84 static DenseMap<CachedHashStringRef, StringRef> resolvedLibraries; 85 static Optional<StringRef> findLibrary(StringRef name) { 86 CachedHashStringRef key(name); 87 auto entry = resolvedLibraries.find(key); 88 if (entry != resolvedLibraries.end()) 89 return entry->second; 90 91 auto doFind = [&] { 92 if (config->searchDylibsFirst) { 93 if (Optional<StringRef> path = findPathCombination( 94 "lib" + name, config->librarySearchPaths, {".tbd", ".dylib"})) 95 return path; 96 return findPathCombination("lib" + name, config->librarySearchPaths, 97 {".a"}); 98 } 99 return findPathCombination("lib" + name, config->librarySearchPaths, 100 {".tbd", ".dylib", ".a"}); 101 }; 102 103 Optional<StringRef> path = doFind(); 104 if (path) 105 resolvedLibraries[key] = *path; 106 107 return path; 108 } 109 110 static DenseMap<CachedHashStringRef, StringRef> resolvedFrameworks; 111 static Optional<StringRef> findFramework(StringRef name) { 112 CachedHashStringRef key(name); 113 auto entry = resolvedFrameworks.find(key); 114 if (entry != resolvedFrameworks.end()) 115 return entry->second; 116 117 SmallString<260> symlink; 118 StringRef suffix; 119 std::tie(name, suffix) = name.split(","); 120 for (StringRef dir : config->frameworkSearchPaths) { 121 symlink = dir; 122 path::append(symlink, name + ".framework", name); 123 124 if (!suffix.empty()) { 125 // NOTE: we must resolve the symlink before trying the suffixes, because 126 // there are no symlinks for the suffixed paths. 127 SmallString<260> location; 128 if (!fs::real_path(symlink, location)) { 129 // only append suffix if realpath() succeeds 130 Twine suffixed = location + suffix; 131 if (fs::exists(suffixed)) 132 return resolvedFrameworks[key] = saver().save(suffixed.str()); 133 } 134 // Suffix lookup failed, fall through to the no-suffix case. 135 } 136 137 if (Optional<StringRef> path = resolveDylibPath(symlink.str())) 138 return resolvedFrameworks[key] = *path; 139 } 140 return {}; 141 } 142 143 static bool warnIfNotDirectory(StringRef option, StringRef path) { 144 if (!fs::exists(path)) { 145 warn("directory not found for option -" + option + path); 146 return false; 147 } else if (!fs::is_directory(path)) { 148 warn("option -" + option + path + " references a non-directory path"); 149 return false; 150 } 151 return true; 152 } 153 154 static std::vector<StringRef> 155 getSearchPaths(unsigned optionCode, InputArgList &args, 156 const std::vector<StringRef> &roots, 157 const SmallVector<StringRef, 2> &systemPaths) { 158 std::vector<StringRef> paths; 159 StringRef optionLetter{optionCode == OPT_F ? "F" : "L"}; 160 for (StringRef path : args::getStrings(args, optionCode)) { 161 // NOTE: only absolute paths are re-rooted to syslibroot(s) 162 bool found = false; 163 if (path::is_absolute(path, path::Style::posix)) { 164 for (StringRef root : roots) { 165 SmallString<261> buffer(root); 166 path::append(buffer, path); 167 // Do not warn about paths that are computed via the syslib roots 168 if (fs::is_directory(buffer)) { 169 paths.push_back(saver().save(buffer.str())); 170 found = true; 171 } 172 } 173 } 174 if (!found && warnIfNotDirectory(optionLetter, path)) 175 paths.push_back(path); 176 } 177 178 // `-Z` suppresses the standard "system" search paths. 179 if (args.hasArg(OPT_Z)) 180 return paths; 181 182 for (const StringRef &path : systemPaths) { 183 for (const StringRef &root : roots) { 184 SmallString<261> buffer(root); 185 path::append(buffer, path); 186 if (fs::is_directory(buffer)) 187 paths.push_back(saver().save(buffer.str())); 188 } 189 } 190 return paths; 191 } 192 193 static std::vector<StringRef> getSystemLibraryRoots(InputArgList &args) { 194 std::vector<StringRef> roots; 195 for (const Arg *arg : args.filtered(OPT_syslibroot)) 196 roots.push_back(arg->getValue()); 197 // NOTE: the final `-syslibroot` being `/` will ignore all roots 198 if (!roots.empty() && roots.back() == "/") 199 roots.clear(); 200 // NOTE: roots can never be empty - add an empty root to simplify the library 201 // and framework search path computation. 202 if (roots.empty()) 203 roots.emplace_back(""); 204 return roots; 205 } 206 207 static std::vector<StringRef> 208 getLibrarySearchPaths(InputArgList &args, const std::vector<StringRef> &roots) { 209 return getSearchPaths(OPT_L, args, roots, {"/usr/lib", "/usr/local/lib"}); 210 } 211 212 static std::vector<StringRef> 213 getFrameworkSearchPaths(InputArgList &args, 214 const std::vector<StringRef> &roots) { 215 return getSearchPaths(OPT_F, args, roots, 216 {"/Library/Frameworks", "/System/Library/Frameworks"}); 217 } 218 219 static llvm::CachePruningPolicy getLTOCachePolicy(InputArgList &args) { 220 SmallString<128> ltoPolicy; 221 auto add = [<oPolicy](Twine val) { 222 if (!ltoPolicy.empty()) 223 ltoPolicy += ":"; 224 val.toVector(ltoPolicy); 225 }; 226 for (const Arg *arg : 227 args.filtered(OPT_thinlto_cache_policy, OPT_prune_interval_lto, 228 OPT_prune_after_lto, OPT_max_relative_cache_size_lto)) { 229 switch (arg->getOption().getID()) { 230 case OPT_thinlto_cache_policy: 231 add(arg->getValue()); 232 break; 233 case OPT_prune_interval_lto: 234 if (!strcmp("-1", arg->getValue())) 235 add("prune_interval=87600h"); // 10 years 236 else 237 add(Twine("prune_interval=") + arg->getValue() + "s"); 238 break; 239 case OPT_prune_after_lto: 240 add(Twine("prune_after=") + arg->getValue() + "s"); 241 break; 242 case OPT_max_relative_cache_size_lto: 243 add(Twine("cache_size=") + arg->getValue() + "%"); 244 break; 245 } 246 } 247 return CHECK(parseCachePruningPolicy(ltoPolicy), "invalid LTO cache policy"); 248 } 249 250 struct ArchiveFileInfo { 251 ArchiveFile *file; 252 bool isCommandLineLoad; 253 }; 254 255 static DenseMap<StringRef, ArchiveFileInfo> loadedArchives; 256 257 static InputFile *addFile(StringRef path, ForceLoad forceLoadArchive, 258 bool isCommandLineLoad, bool isLazy = false, 259 bool isExplicit = true, bool isBundleLoader = false) { 260 Optional<MemoryBufferRef> buffer = readFile(path); 261 if (!buffer) 262 return nullptr; 263 MemoryBufferRef mbref = *buffer; 264 InputFile *newFile = nullptr; 265 266 file_magic magic = identify_magic(mbref.getBuffer()); 267 switch (magic) { 268 case file_magic::archive: { 269 // Avoid loading archives twice. If the archives are being force-loaded, 270 // loading them twice would create duplicate symbol errors. In the 271 // non-force-loading case, this is just a minor performance optimization. 272 // We don't take a reference to cachedFile here because the 273 // loadArchiveMember() call below may recursively call addFile() and 274 // invalidate this reference. 275 auto entry = loadedArchives.find(path); 276 277 ArchiveFile *file; 278 if (entry == loadedArchives.end()) { 279 // No cached archive, we need to create a new one 280 std::unique_ptr<object::Archive> archive = CHECK( 281 object::Archive::create(mbref), path + ": failed to parse archive"); 282 283 if (!archive->isEmpty() && !archive->hasSymbolTable()) 284 error(path + ": archive has no index; run ranlib to add one"); 285 file = make<ArchiveFile>(std::move(archive)); 286 } else { 287 file = entry->second.file; 288 // If file is previously loaded via command line, or is loaded via 289 // LC_LINKER_OPTION and being loaded via LC_LINKER_OPTION again, 290 // using the cached archive should be enough 291 if (entry->second.isCommandLineLoad || 292 entry->second.isCommandLineLoad == isCommandLineLoad) 293 return file; 294 } 295 296 if ((isCommandLineLoad && config->allLoad) || 297 forceLoadArchive == ForceLoad::Yes) { 298 if (Optional<MemoryBufferRef> buffer = readFile(path)) { 299 Error e = Error::success(); 300 for (const object::Archive::Child &c : file->getArchive().children(e)) { 301 StringRef reason = 302 forceLoadArchive == ForceLoad::Yes ? "-force_load" : "-all_load"; 303 if (Error e = file->fetch(c, reason)) 304 error(toString(file) + ": " + reason + 305 " failed to load archive member: " + toString(std::move(e))); 306 } 307 if (e) 308 error(toString(file) + 309 ": Archive::children failed: " + toString(std::move(e))); 310 } 311 } else if (isCommandLineLoad && config->forceLoadObjC) { 312 for (const object::Archive::Symbol &sym : file->getArchive().symbols()) 313 if (sym.getName().startswith(objc::klass)) 314 file->fetch(sym); 315 316 // TODO: no need to look for ObjC sections for a given archive member if 317 // we already found that it contains an ObjC symbol. 318 if (Optional<MemoryBufferRef> buffer = readFile(path)) { 319 Error e = Error::success(); 320 for (const object::Archive::Child &c : file->getArchive().children(e)) { 321 Expected<MemoryBufferRef> mb = c.getMemoryBufferRef(); 322 if (!mb || !hasObjCSection(*mb)) 323 continue; 324 if (Error e = file->fetch(c, "-ObjC")) 325 error(toString(file) + ": -ObjC failed to load archive member: " + 326 toString(std::move(e))); 327 } 328 if (e) 329 error(toString(file) + 330 ": Archive::children failed: " + toString(std::move(e))); 331 } 332 } 333 334 file->addLazySymbols(); 335 loadedArchives[path] = ArchiveFileInfo{file, isCommandLineLoad}; 336 newFile = file; 337 break; 338 } 339 case file_magic::macho_object: 340 newFile = make<ObjFile>(mbref, getModTime(path), "", isLazy); 341 break; 342 case file_magic::macho_dynamically_linked_shared_lib: 343 case file_magic::macho_dynamically_linked_shared_lib_stub: 344 case file_magic::tapi_file: 345 if (DylibFile *dylibFile = 346 loadDylib(mbref, nullptr, /*isBundleLoader=*/false, isExplicit)) 347 newFile = dylibFile; 348 break; 349 case file_magic::bitcode: 350 newFile = make<BitcodeFile>(mbref, "", 0, isLazy); 351 break; 352 case file_magic::macho_executable: 353 case file_magic::macho_bundle: 354 // We only allow executable and bundle type here if it is used 355 // as a bundle loader. 356 if (!isBundleLoader) 357 error(path + ": unhandled file type"); 358 if (DylibFile *dylibFile = loadDylib(mbref, nullptr, isBundleLoader)) 359 newFile = dylibFile; 360 break; 361 default: 362 error(path + ": unhandled file type"); 363 } 364 if (newFile && !isa<DylibFile>(newFile)) { 365 if ((isa<ObjFile>(newFile) || isa<BitcodeFile>(newFile)) && newFile->lazy && 366 config->forceLoadObjC) { 367 for (Symbol *sym : newFile->symbols) 368 if (sym && sym->getName().startswith(objc::klass)) { 369 extract(*newFile, "-ObjC"); 370 break; 371 } 372 if (newFile->lazy && hasObjCSection(mbref)) 373 extract(*newFile, "-ObjC"); 374 } 375 376 // printArchiveMemberLoad() prints both .a and .o names, so no need to 377 // print the .a name here. Similarly skip lazy files. 378 if (config->printEachFile && magic != file_magic::archive && !isLazy) 379 message(toString(newFile)); 380 inputFiles.insert(newFile); 381 } 382 return newFile; 383 } 384 385 static void addLibrary(StringRef name, bool isNeeded, bool isWeak, 386 bool isReexport, bool isExplicit, 387 ForceLoad forceLoadArchive, 388 bool isCommandLineLoad = true) { 389 if (Optional<StringRef> path = findLibrary(name)) { 390 if (auto *dylibFile = dyn_cast_or_null<DylibFile>( 391 addFile(*path, forceLoadArchive, isCommandLineLoad, 392 /*isLazy=*/false, isExplicit))) { 393 if (isNeeded) 394 dylibFile->forceNeeded = true; 395 if (isWeak) 396 dylibFile->forceWeakImport = true; 397 if (isReexport) { 398 config->hasReexports = true; 399 dylibFile->reexport = true; 400 } 401 } 402 return; 403 } 404 error("library not found for -l" + name); 405 } 406 407 static DenseSet<StringRef> loadedObjectFrameworks; 408 static void addFramework(StringRef name, bool isNeeded, bool isWeak, 409 bool isReexport, bool isExplicit, 410 ForceLoad forceLoadArchive, 411 bool isCommandLineLoad = true) { 412 if (Optional<StringRef> path = findFramework(name)) { 413 if (loadedObjectFrameworks.contains(*path)) 414 return; 415 416 InputFile *file = addFile(*path, forceLoadArchive, isCommandLineLoad, 417 /*isLazy=*/false, isExplicit, false); 418 if (auto *dylibFile = dyn_cast_or_null<DylibFile>(file)) { 419 if (isNeeded) 420 dylibFile->forceNeeded = true; 421 if (isWeak) 422 dylibFile->forceWeakImport = true; 423 if (isReexport) { 424 config->hasReexports = true; 425 dylibFile->reexport = true; 426 } 427 } else if (isa_and_nonnull<ObjFile>(file) || 428 isa_and_nonnull<BitcodeFile>(file)) { 429 // Cache frameworks containing object or bitcode files to avoid duplicate 430 // symbols. Frameworks containing static archives are cached separately 431 // in addFile() to share caching with libraries, and frameworks 432 // containing dylibs should allow overwriting of attributes such as 433 // forceNeeded by subsequent loads 434 loadedObjectFrameworks.insert(*path); 435 } 436 return; 437 } 438 error("framework not found for -framework " + name); 439 } 440 441 // Parses LC_LINKER_OPTION contents, which can add additional command line 442 // flags. This directly parses the flags instead of using the standard argument 443 // parser to improve performance. 444 void macho::parseLCLinkerOption(InputFile *f, unsigned argc, StringRef data) { 445 SmallVector<StringRef, 4> argv; 446 size_t offset = 0; 447 for (unsigned i = 0; i < argc && offset < data.size(); ++i) { 448 argv.push_back(data.data() + offset); 449 offset += strlen(data.data() + offset) + 1; 450 } 451 if (argv.size() != argc || offset > data.size()) 452 fatal(toString(f) + ": invalid LC_LINKER_OPTION"); 453 454 unsigned i = 0; 455 StringRef arg = argv[i]; 456 if (arg.consume_front("-l")) { 457 ForceLoad forceLoadArchive = 458 config->forceLoadSwift && arg.startswith("swift") ? ForceLoad::Yes 459 : ForceLoad::No; 460 addLibrary(arg, /*isNeeded=*/false, /*isWeak=*/false, 461 /*isReexport=*/false, /*isExplicit=*/false, forceLoadArchive, 462 /*isCommandLineLoad=*/false); 463 } else if (arg == "-framework") { 464 StringRef name = argv[++i]; 465 addFramework(name, /*isNeeded=*/false, /*isWeak=*/false, 466 /*isReexport=*/false, /*isExplicit=*/false, ForceLoad::No, 467 /*isCommandLineLoad=*/false); 468 } else { 469 error(arg + " is not allowed in LC_LINKER_OPTION"); 470 } 471 } 472 473 static void addFileList(StringRef path, bool isLazy) { 474 Optional<MemoryBufferRef> buffer = readFile(path); 475 if (!buffer) 476 return; 477 MemoryBufferRef mbref = *buffer; 478 for (StringRef path : args::getLines(mbref)) 479 addFile(rerootPath(path), ForceLoad::Default, true, isLazy); 480 } 481 482 // We expect sub-library names of the form "libfoo", which will match a dylib 483 // with a path of .*/libfoo.{dylib, tbd}. 484 // XXX ld64 seems to ignore the extension entirely when matching sub-libraries; 485 // I'm not sure what the use case for that is. 486 static bool markReexport(StringRef searchName, ArrayRef<StringRef> extensions) { 487 for (InputFile *file : inputFiles) { 488 if (auto *dylibFile = dyn_cast<DylibFile>(file)) { 489 StringRef filename = path::filename(dylibFile->getName()); 490 if (filename.consume_front(searchName) && 491 (filename.empty() || 492 find(extensions, filename) != extensions.end())) { 493 dylibFile->reexport = true; 494 return true; 495 } 496 } 497 } 498 return false; 499 } 500 501 // This function is called on startup. We need this for LTO since 502 // LTO calls LLVM functions to compile bitcode files to native code. 503 // Technically this can be delayed until we read bitcode files, but 504 // we don't bother to do lazily because the initialization is fast. 505 static void initLLVM() { 506 InitializeAllTargets(); 507 InitializeAllTargetMCs(); 508 InitializeAllAsmPrinters(); 509 InitializeAllAsmParsers(); 510 } 511 512 static void compileBitcodeFiles() { 513 TimeTraceScope timeScope("LTO"); 514 auto *lto = make<BitcodeCompiler>(); 515 for (InputFile *file : inputFiles) 516 if (auto *bitcodeFile = dyn_cast<BitcodeFile>(file)) 517 if (!file->lazy) 518 lto->add(*bitcodeFile); 519 520 for (ObjFile *file : lto->compile()) 521 inputFiles.insert(file); 522 } 523 524 // Replaces common symbols with defined symbols residing in __common sections. 525 // This function must be called after all symbol names are resolved (i.e. after 526 // all InputFiles have been loaded.) As a result, later operations won't see 527 // any CommonSymbols. 528 static void replaceCommonSymbols() { 529 TimeTraceScope timeScope("Replace common symbols"); 530 ConcatOutputSection *osec = nullptr; 531 for (Symbol *sym : symtab->getSymbols()) { 532 auto *common = dyn_cast<CommonSymbol>(sym); 533 if (common == nullptr) 534 continue; 535 536 // Casting to size_t will truncate large values on 32-bit architectures, 537 // but it's not really worth supporting the linking of 64-bit programs on 538 // 32-bit archs. 539 ArrayRef<uint8_t> data = {nullptr, static_cast<size_t>(common->size)}; 540 // FIXME avoid creating one Section per symbol? 541 auto *section = 542 make<Section>(common->getFile(), segment_names::data, 543 section_names::common, S_ZEROFILL, /*addr=*/0); 544 auto *isec = make<ConcatInputSection>(*section, data, common->align); 545 if (!osec) 546 osec = ConcatOutputSection::getOrCreateForInput(isec); 547 isec->parent = osec; 548 inputSections.push_back(isec); 549 550 // FIXME: CommonSymbol should store isReferencedDynamically, noDeadStrip 551 // and pass them on here. 552 replaceSymbol<Defined>( 553 sym, sym->getName(), common->getFile(), isec, /*value=*/0, /*size=*/0, 554 /*isWeakDef=*/false, /*isExternal=*/true, common->privateExtern, 555 /*includeInSymtab=*/true, /*isThumb=*/false, 556 /*isReferencedDynamically=*/false, /*noDeadStrip=*/false); 557 } 558 } 559 560 static void initializeSectionRenameMap() { 561 if (config->dataConst) { 562 SmallVector<StringRef> v{section_names::got, 563 section_names::authGot, 564 section_names::authPtr, 565 section_names::nonLazySymbolPtr, 566 section_names::const_, 567 section_names::cfString, 568 section_names::moduleInitFunc, 569 section_names::moduleTermFunc, 570 section_names::objcClassList, 571 section_names::objcNonLazyClassList, 572 section_names::objcCatList, 573 section_names::objcNonLazyCatList, 574 section_names::objcProtoList, 575 section_names::objcImageInfo}; 576 for (StringRef s : v) 577 config->sectionRenameMap[{segment_names::data, s}] = { 578 segment_names::dataConst, s}; 579 } 580 config->sectionRenameMap[{segment_names::text, section_names::staticInit}] = { 581 segment_names::text, section_names::text}; 582 config->sectionRenameMap[{segment_names::import, section_names::pointers}] = { 583 config->dataConst ? segment_names::dataConst : segment_names::data, 584 section_names::nonLazySymbolPtr}; 585 } 586 587 static inline char toLowerDash(char x) { 588 if (x >= 'A' && x <= 'Z') 589 return x - 'A' + 'a'; 590 else if (x == ' ') 591 return '-'; 592 return x; 593 } 594 595 static std::string lowerDash(StringRef s) { 596 return std::string(map_iterator(s.begin(), toLowerDash), 597 map_iterator(s.end(), toLowerDash)); 598 } 599 600 struct PlatformVersion { 601 PlatformType platform = PLATFORM_UNKNOWN; 602 llvm::VersionTuple minimum; 603 llvm::VersionTuple sdk; 604 }; 605 606 static PlatformVersion parsePlatformVersion(const Arg *arg) { 607 assert(arg->getOption().getID() == OPT_platform_version); 608 StringRef platformStr = arg->getValue(0); 609 StringRef minVersionStr = arg->getValue(1); 610 StringRef sdkVersionStr = arg->getValue(2); 611 612 PlatformVersion platformVersion; 613 614 // TODO(compnerd) see if we can generate this case list via XMACROS 615 platformVersion.platform = 616 StringSwitch<PlatformType>(lowerDash(platformStr)) 617 .Cases("macos", "1", PLATFORM_MACOS) 618 .Cases("ios", "2", PLATFORM_IOS) 619 .Cases("tvos", "3", PLATFORM_TVOS) 620 .Cases("watchos", "4", PLATFORM_WATCHOS) 621 .Cases("bridgeos", "5", PLATFORM_BRIDGEOS) 622 .Cases("mac-catalyst", "6", PLATFORM_MACCATALYST) 623 .Cases("ios-simulator", "7", PLATFORM_IOSSIMULATOR) 624 .Cases("tvos-simulator", "8", PLATFORM_TVOSSIMULATOR) 625 .Cases("watchos-simulator", "9", PLATFORM_WATCHOSSIMULATOR) 626 .Cases("driverkit", "10", PLATFORM_DRIVERKIT) 627 .Default(PLATFORM_UNKNOWN); 628 if (platformVersion.platform == PLATFORM_UNKNOWN) 629 error(Twine("malformed platform: ") + platformStr); 630 // TODO: check validity of version strings, which varies by platform 631 // NOTE: ld64 accepts version strings with 5 components 632 // llvm::VersionTuple accepts no more than 4 components 633 // Has Apple ever published version strings with 5 components? 634 if (platformVersion.minimum.tryParse(minVersionStr)) 635 error(Twine("malformed minimum version: ") + minVersionStr); 636 if (platformVersion.sdk.tryParse(sdkVersionStr)) 637 error(Twine("malformed sdk version: ") + sdkVersionStr); 638 return platformVersion; 639 } 640 641 // Has the side-effect of setting Config::platformInfo. 642 static PlatformType parsePlatformVersions(const ArgList &args) { 643 std::map<PlatformType, PlatformVersion> platformVersions; 644 const PlatformVersion *lastVersionInfo = nullptr; 645 for (const Arg *arg : args.filtered(OPT_platform_version)) { 646 PlatformVersion version = parsePlatformVersion(arg); 647 648 // For each platform, the last flag wins: 649 // `-platform_version macos 2 3 -platform_version macos 4 5` has the same 650 // effect as just passing `-platform_version macos 4 5`. 651 // FIXME: ld64 warns on multiple flags for one platform. Should we? 652 platformVersions[version.platform] = version; 653 lastVersionInfo = &platformVersions[version.platform]; 654 } 655 656 if (platformVersions.empty()) { 657 error("must specify -platform_version"); 658 return PLATFORM_UNKNOWN; 659 } 660 if (platformVersions.size() > 2) { 661 error("must specify -platform_version at most twice"); 662 return PLATFORM_UNKNOWN; 663 } 664 if (platformVersions.size() == 2) { 665 bool isZipperedCatalyst = platformVersions.count(PLATFORM_MACOS) && 666 platformVersions.count(PLATFORM_MACCATALYST); 667 668 if (!isZipperedCatalyst) { 669 error("lld supports writing zippered outputs only for " 670 "macos and mac-catalyst"); 671 } else if (config->outputType != MH_DYLIB && 672 config->outputType != MH_BUNDLE) { 673 error("writing zippered outputs only valid for -dylib and -bundle"); 674 } else { 675 config->platformInfo.minimum = platformVersions[PLATFORM_MACOS].minimum; 676 config->platformInfo.sdk = platformVersions[PLATFORM_MACOS].sdk; 677 config->secondaryPlatformInfo = PlatformInfo{}; 678 config->secondaryPlatformInfo->minimum = 679 platformVersions[PLATFORM_MACCATALYST].minimum; 680 config->secondaryPlatformInfo->sdk = 681 platformVersions[PLATFORM_MACCATALYST].sdk; 682 } 683 return PLATFORM_MACOS; 684 } 685 686 config->platformInfo.minimum = lastVersionInfo->minimum; 687 config->platformInfo.sdk = lastVersionInfo->sdk; 688 return lastVersionInfo->platform; 689 } 690 691 // Has the side-effect of setting Config::target. 692 static TargetInfo *createTargetInfo(InputArgList &args) { 693 StringRef archName = args.getLastArgValue(OPT_arch); 694 if (archName.empty()) { 695 error("must specify -arch"); 696 return nullptr; 697 } 698 699 PlatformType platform = parsePlatformVersions(args); 700 config->platformInfo.target = 701 MachO::Target(getArchitectureFromName(archName), platform); 702 if (config->secondaryPlatformInfo) { 703 config->secondaryPlatformInfo->target = 704 MachO::Target(getArchitectureFromName(archName), PLATFORM_MACCATALYST); 705 } 706 707 uint32_t cpuType; 708 uint32_t cpuSubtype; 709 std::tie(cpuType, cpuSubtype) = getCPUTypeFromArchitecture(config->arch()); 710 711 switch (cpuType) { 712 case CPU_TYPE_X86_64: 713 return createX86_64TargetInfo(); 714 case CPU_TYPE_ARM64: 715 return createARM64TargetInfo(); 716 case CPU_TYPE_ARM64_32: 717 return createARM64_32TargetInfo(); 718 case CPU_TYPE_ARM: 719 return createARMTargetInfo(cpuSubtype); 720 default: 721 error("missing or unsupported -arch " + archName); 722 return nullptr; 723 } 724 } 725 726 static UndefinedSymbolTreatment 727 getUndefinedSymbolTreatment(const ArgList &args) { 728 StringRef treatmentStr = args.getLastArgValue(OPT_undefined); 729 auto treatment = 730 StringSwitch<UndefinedSymbolTreatment>(treatmentStr) 731 .Cases("error", "", UndefinedSymbolTreatment::error) 732 .Case("warning", UndefinedSymbolTreatment::warning) 733 .Case("suppress", UndefinedSymbolTreatment::suppress) 734 .Case("dynamic_lookup", UndefinedSymbolTreatment::dynamic_lookup) 735 .Default(UndefinedSymbolTreatment::unknown); 736 if (treatment == UndefinedSymbolTreatment::unknown) { 737 warn(Twine("unknown -undefined TREATMENT '") + treatmentStr + 738 "', defaulting to 'error'"); 739 treatment = UndefinedSymbolTreatment::error; 740 } else if (config->namespaceKind == NamespaceKind::twolevel && 741 (treatment == UndefinedSymbolTreatment::warning || 742 treatment == UndefinedSymbolTreatment::suppress)) { 743 if (treatment == UndefinedSymbolTreatment::warning) 744 error("'-undefined warning' only valid with '-flat_namespace'"); 745 else 746 error("'-undefined suppress' only valid with '-flat_namespace'"); 747 treatment = UndefinedSymbolTreatment::error; 748 } 749 return treatment; 750 } 751 752 static ICFLevel getICFLevel(const ArgList &args) { 753 StringRef icfLevelStr = args.getLastArgValue(OPT_icf_eq); 754 auto icfLevel = StringSwitch<ICFLevel>(icfLevelStr) 755 .Cases("none", "", ICFLevel::none) 756 .Case("safe", ICFLevel::safe) 757 .Case("all", ICFLevel::all) 758 .Default(ICFLevel::unknown); 759 if (icfLevel == ICFLevel::unknown) { 760 warn(Twine("unknown --icf=OPTION `") + icfLevelStr + 761 "', defaulting to `none'"); 762 icfLevel = ICFLevel::none; 763 } 764 return icfLevel; 765 } 766 767 static void warnIfDeprecatedOption(const Option &opt) { 768 if (!opt.getGroup().isValid()) 769 return; 770 if (opt.getGroup().getID() == OPT_grp_deprecated) { 771 warn("Option `" + opt.getPrefixedName() + "' is deprecated in ld64:"); 772 warn(opt.getHelpText()); 773 } 774 } 775 776 static void warnIfUnimplementedOption(const Option &opt) { 777 if (!opt.getGroup().isValid() || !opt.hasFlag(DriverFlag::HelpHidden)) 778 return; 779 switch (opt.getGroup().getID()) { 780 case OPT_grp_deprecated: 781 // warn about deprecated options elsewhere 782 break; 783 case OPT_grp_undocumented: 784 warn("Option `" + opt.getPrefixedName() + 785 "' is undocumented. Should lld implement it?"); 786 break; 787 case OPT_grp_obsolete: 788 warn("Option `" + opt.getPrefixedName() + 789 "' is obsolete. Please modernize your usage."); 790 break; 791 case OPT_grp_ignored: 792 warn("Option `" + opt.getPrefixedName() + "' is ignored."); 793 break; 794 case OPT_grp_ignored_silently: 795 break; 796 default: 797 warn("Option `" + opt.getPrefixedName() + 798 "' is not yet implemented. Stay tuned..."); 799 break; 800 } 801 } 802 803 static const char *getReproduceOption(InputArgList &args) { 804 if (const Arg *arg = args.getLastArg(OPT_reproduce)) 805 return arg->getValue(); 806 return getenv("LLD_REPRODUCE"); 807 } 808 809 static void parseClangOption(StringRef opt, const Twine &msg) { 810 std::string err; 811 raw_string_ostream os(err); 812 813 const char *argv[] = {"lld", opt.data()}; 814 if (cl::ParseCommandLineOptions(2, argv, "", &os)) 815 return; 816 os.flush(); 817 error(msg + ": " + StringRef(err).trim()); 818 } 819 820 static uint32_t parseDylibVersion(const ArgList &args, unsigned id) { 821 const Arg *arg = args.getLastArg(id); 822 if (!arg) 823 return 0; 824 825 if (config->outputType != MH_DYLIB) { 826 error(arg->getAsString(args) + ": only valid with -dylib"); 827 return 0; 828 } 829 830 PackedVersion version; 831 if (!version.parse32(arg->getValue())) { 832 error(arg->getAsString(args) + ": malformed version"); 833 return 0; 834 } 835 836 return version.rawValue(); 837 } 838 839 static uint32_t parseProtection(StringRef protStr) { 840 uint32_t prot = 0; 841 for (char c : protStr) { 842 switch (c) { 843 case 'r': 844 prot |= VM_PROT_READ; 845 break; 846 case 'w': 847 prot |= VM_PROT_WRITE; 848 break; 849 case 'x': 850 prot |= VM_PROT_EXECUTE; 851 break; 852 case '-': 853 break; 854 default: 855 error("unknown -segprot letter '" + Twine(c) + "' in " + protStr); 856 return 0; 857 } 858 } 859 return prot; 860 } 861 862 static std::vector<SectionAlign> parseSectAlign(const opt::InputArgList &args) { 863 std::vector<SectionAlign> sectAligns; 864 for (const Arg *arg : args.filtered(OPT_sectalign)) { 865 StringRef segName = arg->getValue(0); 866 StringRef sectName = arg->getValue(1); 867 StringRef alignStr = arg->getValue(2); 868 if (alignStr.startswith("0x") || alignStr.startswith("0X")) 869 alignStr = alignStr.drop_front(2); 870 uint32_t align; 871 if (alignStr.getAsInteger(16, align)) { 872 error("-sectalign: failed to parse '" + StringRef(arg->getValue(2)) + 873 "' as number"); 874 continue; 875 } 876 if (!isPowerOf2_32(align)) { 877 error("-sectalign: '" + StringRef(arg->getValue(2)) + 878 "' (in base 16) not a power of two"); 879 continue; 880 } 881 sectAligns.push_back({segName, sectName, align}); 882 } 883 return sectAligns; 884 } 885 886 PlatformType macho::removeSimulator(PlatformType platform) { 887 switch (platform) { 888 case PLATFORM_IOSSIMULATOR: 889 return PLATFORM_IOS; 890 case PLATFORM_TVOSSIMULATOR: 891 return PLATFORM_TVOS; 892 case PLATFORM_WATCHOSSIMULATOR: 893 return PLATFORM_WATCHOS; 894 default: 895 return platform; 896 } 897 } 898 899 static bool dataConstDefault(const InputArgList &args) { 900 static const std::vector<std::pair<PlatformType, VersionTuple>> minVersion = { 901 {PLATFORM_MACOS, VersionTuple(10, 15)}, 902 {PLATFORM_IOS, VersionTuple(13, 0)}, 903 {PLATFORM_TVOS, VersionTuple(13, 0)}, 904 {PLATFORM_WATCHOS, VersionTuple(6, 0)}, 905 {PLATFORM_BRIDGEOS, VersionTuple(4, 0)}}; 906 PlatformType platform = removeSimulator(config->platformInfo.target.Platform); 907 auto it = llvm::find_if(minVersion, 908 [&](const auto &p) { return p.first == platform; }); 909 if (it != minVersion.end()) 910 if (config->platformInfo.minimum < it->second) 911 return false; 912 913 switch (config->outputType) { 914 case MH_EXECUTE: 915 return !args.hasArg(OPT_no_pie); 916 case MH_BUNDLE: 917 // FIXME: return false when -final_name ... 918 // has prefix "/System/Library/UserEventPlugins/" 919 // or matches "/usr/libexec/locationd" "/usr/libexec/terminusd" 920 return true; 921 case MH_DYLIB: 922 return true; 923 case MH_OBJECT: 924 return false; 925 default: 926 llvm_unreachable( 927 "unsupported output type for determining data-const default"); 928 } 929 return false; 930 } 931 932 void SymbolPatterns::clear() { 933 literals.clear(); 934 globs.clear(); 935 } 936 937 void SymbolPatterns::insert(StringRef symbolName) { 938 if (symbolName.find_first_of("*?[]") == StringRef::npos) 939 literals.insert(CachedHashStringRef(symbolName)); 940 else if (Expected<GlobPattern> pattern = GlobPattern::create(symbolName)) 941 globs.emplace_back(*pattern); 942 else 943 error("invalid symbol-name pattern: " + symbolName); 944 } 945 946 bool SymbolPatterns::matchLiteral(StringRef symbolName) const { 947 return literals.contains(CachedHashStringRef(symbolName)); 948 } 949 950 bool SymbolPatterns::matchGlob(StringRef symbolName) const { 951 for (const GlobPattern &glob : globs) 952 if (glob.match(symbolName)) 953 return true; 954 return false; 955 } 956 957 bool SymbolPatterns::match(StringRef symbolName) const { 958 return matchLiteral(symbolName) || matchGlob(symbolName); 959 } 960 961 static void parseSymbolPatternsFile(const Arg *arg, 962 SymbolPatterns &symbolPatterns) { 963 StringRef path = arg->getValue(); 964 Optional<MemoryBufferRef> buffer = readFile(path); 965 if (!buffer) { 966 error("Could not read symbol file: " + path); 967 return; 968 } 969 MemoryBufferRef mbref = *buffer; 970 for (StringRef line : args::getLines(mbref)) { 971 line = line.take_until([](char c) { return c == '#'; }).trim(); 972 if (!line.empty()) 973 symbolPatterns.insert(line); 974 } 975 } 976 977 static void handleSymbolPatterns(InputArgList &args, 978 SymbolPatterns &symbolPatterns, 979 unsigned singleOptionCode, 980 unsigned listFileOptionCode) { 981 for (const Arg *arg : args.filtered(singleOptionCode)) 982 symbolPatterns.insert(arg->getValue()); 983 for (const Arg *arg : args.filtered(listFileOptionCode)) 984 parseSymbolPatternsFile(arg, symbolPatterns); 985 } 986 987 static void createFiles(const InputArgList &args) { 988 TimeTraceScope timeScope("Load input files"); 989 // This loop should be reserved for options whose exact ordering matters. 990 // Other options should be handled via filtered() and/or getLastArg(). 991 bool isLazy = false; 992 for (const Arg *arg : args) { 993 const Option &opt = arg->getOption(); 994 warnIfDeprecatedOption(opt); 995 warnIfUnimplementedOption(opt); 996 997 switch (opt.getID()) { 998 case OPT_INPUT: 999 addFile(rerootPath(arg->getValue()), ForceLoad::Default, true, isLazy); 1000 break; 1001 case OPT_needed_library: 1002 if (auto *dylibFile = dyn_cast_or_null<DylibFile>( 1003 addFile(rerootPath(arg->getValue()), ForceLoad::Default, true))) 1004 dylibFile->forceNeeded = true; 1005 break; 1006 case OPT_reexport_library: 1007 if (auto *dylibFile = dyn_cast_or_null<DylibFile>( 1008 addFile(rerootPath(arg->getValue()), ForceLoad::Default, true))) { 1009 config->hasReexports = true; 1010 dylibFile->reexport = true; 1011 } 1012 break; 1013 case OPT_weak_library: 1014 if (auto *dylibFile = dyn_cast_or_null<DylibFile>( 1015 addFile(rerootPath(arg->getValue()), ForceLoad::Default, true))) 1016 dylibFile->forceWeakImport = true; 1017 break; 1018 case OPT_filelist: 1019 addFileList(arg->getValue(), isLazy); 1020 break; 1021 case OPT_force_load: 1022 addFile(rerootPath(arg->getValue()), ForceLoad::Yes, true); 1023 break; 1024 case OPT_l: 1025 case OPT_needed_l: 1026 case OPT_reexport_l: 1027 case OPT_weak_l: 1028 addLibrary(arg->getValue(), opt.getID() == OPT_needed_l, 1029 opt.getID() == OPT_weak_l, opt.getID() == OPT_reexport_l, 1030 /*isExplicit=*/true, ForceLoad::Default); 1031 break; 1032 case OPT_framework: 1033 case OPT_needed_framework: 1034 case OPT_reexport_framework: 1035 case OPT_weak_framework: 1036 addFramework(arg->getValue(), opt.getID() == OPT_needed_framework, 1037 opt.getID() == OPT_weak_framework, 1038 opt.getID() == OPT_reexport_framework, /*isExplicit=*/true, 1039 ForceLoad::Default); 1040 break; 1041 case OPT_start_lib: 1042 if (isLazy) 1043 error("nested --start-lib"); 1044 isLazy = true; 1045 break; 1046 case OPT_end_lib: 1047 if (!isLazy) 1048 error("stray --end-lib"); 1049 isLazy = false; 1050 break; 1051 default: 1052 break; 1053 } 1054 } 1055 } 1056 1057 static void gatherInputSections() { 1058 TimeTraceScope timeScope("Gathering input sections"); 1059 int inputOrder = 0; 1060 for (const InputFile *file : inputFiles) { 1061 for (const Section *section : file->sections) { 1062 // Compact unwind entries require special handling elsewhere. (In 1063 // contrast, EH frames are handled like regular ConcatInputSections.) 1064 if (section->name == section_names::compactUnwind) 1065 continue; 1066 ConcatOutputSection *osec = nullptr; 1067 for (const Subsection &subsection : section->subsections) { 1068 if (auto *isec = dyn_cast<ConcatInputSection>(subsection.isec)) { 1069 if (isec->isCoalescedWeak()) 1070 continue; 1071 isec->outSecOff = inputOrder++; 1072 if (!osec) 1073 osec = ConcatOutputSection::getOrCreateForInput(isec); 1074 isec->parent = osec; 1075 inputSections.push_back(isec); 1076 } else if (auto *isec = 1077 dyn_cast<CStringInputSection>(subsection.isec)) { 1078 if (in.cStringSection->inputOrder == UnspecifiedInputOrder) 1079 in.cStringSection->inputOrder = inputOrder++; 1080 in.cStringSection->addInput(isec); 1081 } else if (auto *isec = 1082 dyn_cast<WordLiteralInputSection>(subsection.isec)) { 1083 if (in.wordLiteralSection->inputOrder == UnspecifiedInputOrder) 1084 in.wordLiteralSection->inputOrder = inputOrder++; 1085 in.wordLiteralSection->addInput(isec); 1086 } else { 1087 llvm_unreachable("unexpected input section kind"); 1088 } 1089 } 1090 } 1091 } 1092 assert(inputOrder <= UnspecifiedInputOrder); 1093 } 1094 1095 static void foldIdenticalLiterals() { 1096 TimeTraceScope timeScope("Fold identical literals"); 1097 // We always create a cStringSection, regardless of whether dedupLiterals is 1098 // true. If it isn't, we simply create a non-deduplicating CStringSection. 1099 // Either way, we must unconditionally finalize it here. 1100 in.cStringSection->finalizeContents(); 1101 if (in.wordLiteralSection) 1102 in.wordLiteralSection->finalizeContents(); 1103 } 1104 1105 static void referenceStubBinder() { 1106 bool needsStubHelper = config->outputType == MH_DYLIB || 1107 config->outputType == MH_EXECUTE || 1108 config->outputType == MH_BUNDLE; 1109 if (!needsStubHelper || !symtab->find("dyld_stub_binder")) 1110 return; 1111 1112 // dyld_stub_binder is used by dyld to resolve lazy bindings. This code here 1113 // adds a opportunistic reference to dyld_stub_binder if it happens to exist. 1114 // dyld_stub_binder is in libSystem.dylib, which is usually linked in. This 1115 // isn't needed for correctness, but the presence of that symbol suppresses 1116 // "no symbols" diagnostics from `nm`. 1117 // StubHelperSection::setup() adds a reference and errors out if 1118 // dyld_stub_binder doesn't exist in case it is actually needed. 1119 symtab->addUndefined("dyld_stub_binder", /*file=*/nullptr, /*isWeak=*/false); 1120 } 1121 1122 bool macho::link(ArrayRef<const char *> argsArr, llvm::raw_ostream &stdoutOS, 1123 llvm::raw_ostream &stderrOS, bool exitEarly, 1124 bool disableOutput) { 1125 // This driver-specific context will be freed later by lldMain(). 1126 auto *ctx = new CommonLinkerContext; 1127 1128 ctx->e.initialize(stdoutOS, stderrOS, exitEarly, disableOutput); 1129 ctx->e.cleanupCallback = []() { 1130 resolvedFrameworks.clear(); 1131 resolvedLibraries.clear(); 1132 cachedReads.clear(); 1133 concatOutputSections.clear(); 1134 inputFiles.clear(); 1135 inputSections.clear(); 1136 loadedArchives.clear(); 1137 loadedObjectFrameworks.clear(); 1138 syntheticSections.clear(); 1139 thunkMap.clear(); 1140 1141 firstTLVDataSection = nullptr; 1142 tar = nullptr; 1143 memset(&in, 0, sizeof(in)); 1144 1145 resetLoadedDylibs(); 1146 resetOutputSegments(); 1147 resetWriter(); 1148 InputFile::resetIdCount(); 1149 }; 1150 1151 ctx->e.logName = args::getFilenameWithoutExe(argsArr[0]); 1152 1153 MachOOptTable parser; 1154 InputArgList args = parser.parse(argsArr.slice(1)); 1155 1156 ctx->e.errorLimitExceededMsg = "too many errors emitted, stopping now " 1157 "(use --error-limit=0 to see all errors)"; 1158 ctx->e.errorLimit = args::getInteger(args, OPT_error_limit_eq, 20); 1159 ctx->e.verbose = args.hasArg(OPT_verbose); 1160 1161 if (args.hasArg(OPT_help_hidden)) { 1162 parser.printHelp(argsArr[0], /*showHidden=*/true); 1163 return true; 1164 } 1165 if (args.hasArg(OPT_help)) { 1166 parser.printHelp(argsArr[0], /*showHidden=*/false); 1167 return true; 1168 } 1169 if (args.hasArg(OPT_version)) { 1170 message(getLLDVersion()); 1171 return true; 1172 } 1173 1174 config = std::make_unique<Configuration>(); 1175 symtab = std::make_unique<SymbolTable>(); 1176 config->outputType = getOutputType(args); 1177 target = createTargetInfo(args); 1178 depTracker = std::make_unique<DependencyTracker>( 1179 args.getLastArgValue(OPT_dependency_info)); 1180 if (errorCount()) 1181 return false; 1182 1183 if (args.hasArg(OPT_pagezero_size)) { 1184 uint64_t pagezeroSize = args::getHex(args, OPT_pagezero_size, 0); 1185 1186 // ld64 does something really weird. It attempts to realign the value to the 1187 // page size, but assumes the the page size is 4K. This doesn't work with 1188 // most of Apple's ARM64 devices, which use a page size of 16K. This means 1189 // that it will first 4K align it by rounding down, then round up to 16K. 1190 // This probably only happened because no one using this arg with anything 1191 // other then 0, so no one checked if it did what is what it says it does. 1192 1193 // So we are not copying this weird behavior and doing the it in a logical 1194 // way, by always rounding down to page size. 1195 if (!isAligned(Align(target->getPageSize()), pagezeroSize)) { 1196 pagezeroSize -= pagezeroSize % target->getPageSize(); 1197 warn("__PAGEZERO size is not page aligned, rounding down to 0x" + 1198 Twine::utohexstr(pagezeroSize)); 1199 } 1200 1201 target->pageZeroSize = pagezeroSize; 1202 } 1203 1204 config->osoPrefix = args.getLastArgValue(OPT_oso_prefix); 1205 if (!config->osoPrefix.empty()) { 1206 // Expand special characters, such as ".", "..", or "~", if present. 1207 // Note: LD64 only expands "." and not other special characters. 1208 // That seems silly to imitate so we will not try to follow it, but rather 1209 // just use real_path() to do it. 1210 1211 // The max path length is 4096, in theory. However that seems quite long 1212 // and seems unlikely that any one would want to strip everything from the 1213 // path. Hence we've picked a reasonably large number here. 1214 SmallString<1024> expanded; 1215 if (!fs::real_path(config->osoPrefix, expanded, 1216 /*expand_tilde=*/true)) { 1217 // Note: LD64 expands "." to be `<current_dir>/` 1218 // (ie., it has a slash suffix) whereas real_path() doesn't. 1219 // So we have to append '/' to be consistent. 1220 StringRef sep = sys::path::get_separator(); 1221 // real_path removes trailing slashes as part of the normalization, but 1222 // these are meaningful for our text based stripping 1223 if (config->osoPrefix.equals(".") || config->osoPrefix.endswith(sep)) 1224 expanded += sep; 1225 config->osoPrefix = saver().save(expanded.str()); 1226 } 1227 } 1228 1229 // Must be set before any InputSections and Symbols are created. 1230 config->deadStrip = args.hasArg(OPT_dead_strip); 1231 1232 config->systemLibraryRoots = getSystemLibraryRoots(args); 1233 if (const char *path = getReproduceOption(args)) { 1234 // Note that --reproduce is a debug option so you can ignore it 1235 // if you are trying to understand the whole picture of the code. 1236 Expected<std::unique_ptr<TarWriter>> errOrWriter = 1237 TarWriter::create(path, path::stem(path)); 1238 if (errOrWriter) { 1239 tar = std::move(*errOrWriter); 1240 tar->append("response.txt", createResponseFile(args)); 1241 tar->append("version.txt", getLLDVersion() + "\n"); 1242 } else { 1243 error("--reproduce: " + toString(errOrWriter.takeError())); 1244 } 1245 } 1246 1247 if (auto *arg = args.getLastArg(OPT_threads_eq)) { 1248 StringRef v(arg->getValue()); 1249 unsigned threads = 0; 1250 if (!llvm::to_integer(v, threads, 0) || threads == 0) 1251 error(arg->getSpelling() + ": expected a positive integer, but got '" + 1252 arg->getValue() + "'"); 1253 parallel::strategy = hardware_concurrency(threads); 1254 config->thinLTOJobs = v; 1255 } 1256 if (auto *arg = args.getLastArg(OPT_thinlto_jobs_eq)) 1257 config->thinLTOJobs = arg->getValue(); 1258 if (!get_threadpool_strategy(config->thinLTOJobs)) 1259 error("--thinlto-jobs: invalid job count: " + config->thinLTOJobs); 1260 1261 for (const Arg *arg : args.filtered(OPT_u)) { 1262 config->explicitUndefineds.push_back(symtab->addUndefined( 1263 arg->getValue(), /*file=*/nullptr, /*isWeakRef=*/false)); 1264 } 1265 1266 for (const Arg *arg : args.filtered(OPT_U)) 1267 config->explicitDynamicLookups.insert(arg->getValue()); 1268 1269 config->mapFile = args.getLastArgValue(OPT_map); 1270 config->optimize = args::getInteger(args, OPT_O, 1); 1271 config->outputFile = args.getLastArgValue(OPT_o, "a.out"); 1272 config->finalOutput = 1273 args.getLastArgValue(OPT_final_output, config->outputFile); 1274 config->astPaths = args.getAllArgValues(OPT_add_ast_path); 1275 config->headerPad = args::getHex(args, OPT_headerpad, /*Default=*/32); 1276 config->headerPadMaxInstallNames = 1277 args.hasArg(OPT_headerpad_max_install_names); 1278 config->printDylibSearch = 1279 args.hasArg(OPT_print_dylib_search) || getenv("RC_TRACE_DYLIB_SEARCHING"); 1280 config->printEachFile = args.hasArg(OPT_t); 1281 config->printWhyLoad = args.hasArg(OPT_why_load); 1282 config->omitDebugInfo = args.hasArg(OPT_S); 1283 config->errorForArchMismatch = args.hasArg(OPT_arch_errors_fatal); 1284 if (const Arg *arg = args.getLastArg(OPT_bundle_loader)) { 1285 if (config->outputType != MH_BUNDLE) 1286 error("-bundle_loader can only be used with MachO bundle output"); 1287 addFile(arg->getValue(), ForceLoad::Default, true, /*isLazy=*/false, 1288 /*isExplicit=*/false, 1289 /*isBundleLoader=*/true); 1290 } 1291 if (const Arg *arg = args.getLastArg(OPT_umbrella)) { 1292 if (config->outputType != MH_DYLIB) 1293 warn("-umbrella used, but not creating dylib"); 1294 config->umbrella = arg->getValue(); 1295 } 1296 config->ltoObjPath = args.getLastArgValue(OPT_object_path_lto); 1297 config->ltoo = args::getInteger(args, OPT_lto_O, 2); 1298 if (config->ltoo > 3) 1299 error("--lto-O: invalid optimization level: " + Twine(config->ltoo)); 1300 config->thinLTOCacheDir = args.getLastArgValue(OPT_cache_path_lto); 1301 config->thinLTOCachePolicy = getLTOCachePolicy(args); 1302 config->runtimePaths = args::getStrings(args, OPT_rpath); 1303 config->allLoad = args.hasFlag(OPT_all_load, OPT_noall_load, false); 1304 config->archMultiple = args.hasArg(OPT_arch_multiple); 1305 config->applicationExtension = args.hasFlag( 1306 OPT_application_extension, OPT_no_application_extension, false); 1307 config->exportDynamic = args.hasArg(OPT_export_dynamic); 1308 config->forceLoadObjC = args.hasArg(OPT_ObjC); 1309 config->forceLoadSwift = args.hasArg(OPT_force_load_swift_libs); 1310 config->deadStripDylibs = args.hasArg(OPT_dead_strip_dylibs); 1311 config->demangle = args.hasArg(OPT_demangle); 1312 config->implicitDylibs = !args.hasArg(OPT_no_implicit_dylibs); 1313 config->emitFunctionStarts = 1314 args.hasFlag(OPT_function_starts, OPT_no_function_starts, true); 1315 config->emitBitcodeBundle = args.hasArg(OPT_bitcode_bundle); 1316 config->emitDataInCodeInfo = 1317 args.hasFlag(OPT_data_in_code_info, OPT_no_data_in_code_info, true); 1318 config->icfLevel = getICFLevel(args); 1319 config->dedupLiterals = 1320 args.hasFlag(OPT_deduplicate_literals, OPT_icf_eq, false) || 1321 config->icfLevel != ICFLevel::none; 1322 config->warnDylibInstallName = args.hasFlag( 1323 OPT_warn_dylib_install_name, OPT_no_warn_dylib_install_name, false); 1324 config->ignoreOptimizationHints = args.hasArg(OPT_ignore_optimization_hints); 1325 config->callGraphProfileSort = args.hasFlag( 1326 OPT_call_graph_profile_sort, OPT_no_call_graph_profile_sort, true); 1327 config->printSymbolOrder = args.getLastArgValue(OPT_print_symbol_order); 1328 1329 for (const Arg *arg : args.filtered(OPT_alias)) { 1330 config->aliasedSymbols.push_back( 1331 std::make_pair(arg->getValue(0), arg->getValue(1))); 1332 } 1333 1334 // FIXME: Add a commandline flag for this too. 1335 config->zeroModTime = getenv("ZERO_AR_DATE"); 1336 1337 std::array<PlatformType, 3> encryptablePlatforms{ 1338 PLATFORM_IOS, PLATFORM_WATCHOS, PLATFORM_TVOS}; 1339 config->emitEncryptionInfo = 1340 args.hasFlag(OPT_encryptable, OPT_no_encryption, 1341 is_contained(encryptablePlatforms, config->platform())); 1342 1343 #ifndef LLVM_HAVE_LIBXAR 1344 if (config->emitBitcodeBundle) 1345 error("-bitcode_bundle unsupported because LLD wasn't built with libxar"); 1346 #endif 1347 1348 if (const Arg *arg = args.getLastArg(OPT_install_name)) { 1349 if (config->warnDylibInstallName && config->outputType != MH_DYLIB) 1350 warn( 1351 arg->getAsString(args) + 1352 ": ignored, only has effect with -dylib [--warn-dylib-install-name]"); 1353 else 1354 config->installName = arg->getValue(); 1355 } else if (config->outputType == MH_DYLIB) { 1356 config->installName = config->finalOutput; 1357 } 1358 1359 if (args.hasArg(OPT_mark_dead_strippable_dylib)) { 1360 if (config->outputType != MH_DYLIB) 1361 warn("-mark_dead_strippable_dylib: ignored, only has effect with -dylib"); 1362 else 1363 config->markDeadStrippableDylib = true; 1364 } 1365 1366 if (const Arg *arg = args.getLastArg(OPT_static, OPT_dynamic)) 1367 config->staticLink = (arg->getOption().getID() == OPT_static); 1368 1369 if (const Arg *arg = 1370 args.getLastArg(OPT_flat_namespace, OPT_twolevel_namespace)) 1371 config->namespaceKind = arg->getOption().getID() == OPT_twolevel_namespace 1372 ? NamespaceKind::twolevel 1373 : NamespaceKind::flat; 1374 1375 config->undefinedSymbolTreatment = getUndefinedSymbolTreatment(args); 1376 1377 if (config->outputType == MH_EXECUTE) 1378 config->entry = symtab->addUndefined(args.getLastArgValue(OPT_e, "_main"), 1379 /*file=*/nullptr, 1380 /*isWeakRef=*/false); 1381 1382 config->librarySearchPaths = 1383 getLibrarySearchPaths(args, config->systemLibraryRoots); 1384 config->frameworkSearchPaths = 1385 getFrameworkSearchPaths(args, config->systemLibraryRoots); 1386 if (const Arg *arg = 1387 args.getLastArg(OPT_search_paths_first, OPT_search_dylibs_first)) 1388 config->searchDylibsFirst = 1389 arg->getOption().getID() == OPT_search_dylibs_first; 1390 1391 config->dylibCompatibilityVersion = 1392 parseDylibVersion(args, OPT_compatibility_version); 1393 config->dylibCurrentVersion = parseDylibVersion(args, OPT_current_version); 1394 1395 config->dataConst = 1396 args.hasFlag(OPT_data_const, OPT_no_data_const, dataConstDefault(args)); 1397 // Populate config->sectionRenameMap with builtin default renames. 1398 // Options -rename_section and -rename_segment are able to override. 1399 initializeSectionRenameMap(); 1400 // Reject every special character except '.' and '$' 1401 // TODO(gkm): verify that this is the proper set of invalid chars 1402 StringRef invalidNameChars("!\"#%&'()*+,-/:;<=>?@[\\]^`{|}~"); 1403 auto validName = [invalidNameChars](StringRef s) { 1404 if (s.find_first_of(invalidNameChars) != StringRef::npos) 1405 error("invalid name for segment or section: " + s); 1406 return s; 1407 }; 1408 for (const Arg *arg : args.filtered(OPT_rename_section)) { 1409 config->sectionRenameMap[{validName(arg->getValue(0)), 1410 validName(arg->getValue(1))}] = { 1411 validName(arg->getValue(2)), validName(arg->getValue(3))}; 1412 } 1413 for (const Arg *arg : args.filtered(OPT_rename_segment)) { 1414 config->segmentRenameMap[validName(arg->getValue(0))] = 1415 validName(arg->getValue(1)); 1416 } 1417 1418 config->sectionAlignments = parseSectAlign(args); 1419 1420 for (const Arg *arg : args.filtered(OPT_segprot)) { 1421 StringRef segName = arg->getValue(0); 1422 uint32_t maxProt = parseProtection(arg->getValue(1)); 1423 uint32_t initProt = parseProtection(arg->getValue(2)); 1424 if (maxProt != initProt && config->arch() != AK_i386) 1425 error("invalid argument '" + arg->getAsString(args) + 1426 "': max and init must be the same for non-i386 archs"); 1427 if (segName == segment_names::linkEdit) 1428 error("-segprot cannot be used to change __LINKEDIT's protections"); 1429 config->segmentProtections.push_back({segName, maxProt, initProt}); 1430 } 1431 1432 config->hasExplicitExports = 1433 args.hasArg(OPT_no_exported_symbols) || 1434 args.hasArgNoClaim(OPT_exported_symbol, OPT_exported_symbols_list); 1435 handleSymbolPatterns(args, config->exportedSymbols, OPT_exported_symbol, 1436 OPT_exported_symbols_list); 1437 handleSymbolPatterns(args, config->unexportedSymbols, OPT_unexported_symbol, 1438 OPT_unexported_symbols_list); 1439 if (config->hasExplicitExports && !config->unexportedSymbols.empty()) 1440 error("cannot use both -exported_symbol* and -unexported_symbol* options"); 1441 1442 if (args.hasArg(OPT_no_exported_symbols) && !config->exportedSymbols.empty()) 1443 error("cannot use both -exported_symbol* and -no_exported_symbols options"); 1444 1445 // Imitating LD64's: 1446 // -non_global_symbols_no_strip_list and -non_global_symbols_strip_list can't 1447 // both be present. 1448 // But -x can be used with either of these two, in which case, the last arg 1449 // takes effect. 1450 // (TODO: This is kind of confusing - considering disallowing using them 1451 // together for a more straightforward behaviour) 1452 { 1453 bool includeLocal = false; 1454 bool excludeLocal = false; 1455 for (const Arg *arg : 1456 args.filtered(OPT_x, OPT_non_global_symbols_no_strip_list, 1457 OPT_non_global_symbols_strip_list)) { 1458 switch (arg->getOption().getID()) { 1459 case OPT_x: 1460 config->localSymbolsPresence = SymtabPresence::None; 1461 break; 1462 case OPT_non_global_symbols_no_strip_list: 1463 if (excludeLocal) { 1464 error("cannot use both -non_global_symbols_no_strip_list and " 1465 "-non_global_symbols_strip_list"); 1466 } else { 1467 includeLocal = true; 1468 config->localSymbolsPresence = SymtabPresence::SelectivelyIncluded; 1469 parseSymbolPatternsFile(arg, config->localSymbolPatterns); 1470 } 1471 break; 1472 case OPT_non_global_symbols_strip_list: 1473 if (includeLocal) { 1474 error("cannot use both -non_global_symbols_no_strip_list and " 1475 "-non_global_symbols_strip_list"); 1476 } else { 1477 excludeLocal = true; 1478 config->localSymbolsPresence = SymtabPresence::SelectivelyExcluded; 1479 parseSymbolPatternsFile(arg, config->localSymbolPatterns); 1480 } 1481 break; 1482 default: 1483 llvm_unreachable("unexpected option"); 1484 } 1485 } 1486 } 1487 // Explicitly-exported literal symbols must be defined, but might 1488 // languish in an archive if unreferenced elsewhere or if they are in the 1489 // non-global strip list. Light a fire under those lazy symbols! 1490 for (const CachedHashStringRef &cachedName : config->exportedSymbols.literals) 1491 symtab->addUndefined(cachedName.val(), /*file=*/nullptr, 1492 /*isWeakRef=*/false); 1493 1494 for (const Arg *arg : args.filtered(OPT_why_live)) 1495 config->whyLive.insert(arg->getValue()); 1496 if (!config->whyLive.empty() && !config->deadStrip) 1497 warn("-why_live has no effect without -dead_strip, ignoring"); 1498 1499 config->saveTemps = args.hasArg(OPT_save_temps); 1500 1501 config->adhocCodesign = args.hasFlag( 1502 OPT_adhoc_codesign, OPT_no_adhoc_codesign, 1503 (config->arch() == AK_arm64 || config->arch() == AK_arm64e) && 1504 config->platform() == PLATFORM_MACOS); 1505 1506 if (args.hasArg(OPT_v)) { 1507 message(getLLDVersion(), lld::errs()); 1508 message(StringRef("Library search paths:") + 1509 (config->librarySearchPaths.empty() 1510 ? "" 1511 : "\n\t" + join(config->librarySearchPaths, "\n\t")), 1512 lld::errs()); 1513 message(StringRef("Framework search paths:") + 1514 (config->frameworkSearchPaths.empty() 1515 ? "" 1516 : "\n\t" + join(config->frameworkSearchPaths, "\n\t")), 1517 lld::errs()); 1518 } 1519 1520 config->progName = argsArr[0]; 1521 1522 config->timeTraceEnabled = args.hasArg(OPT_time_trace_eq); 1523 config->timeTraceGranularity = 1524 args::getInteger(args, OPT_time_trace_granularity_eq, 500); 1525 1526 // Initialize time trace profiler. 1527 if (config->timeTraceEnabled) 1528 timeTraceProfilerInitialize(config->timeTraceGranularity, config->progName); 1529 1530 { 1531 TimeTraceScope timeScope("ExecuteLinker"); 1532 1533 initLLVM(); // must be run before any call to addFile() 1534 createFiles(args); 1535 1536 config->isPic = config->outputType == MH_DYLIB || 1537 config->outputType == MH_BUNDLE || 1538 (config->outputType == MH_EXECUTE && 1539 args.hasFlag(OPT_pie, OPT_no_pie, true)); 1540 1541 // Now that all dylibs have been loaded, search for those that should be 1542 // re-exported. 1543 { 1544 auto reexportHandler = [](const Arg *arg, 1545 const std::vector<StringRef> &extensions) { 1546 config->hasReexports = true; 1547 StringRef searchName = arg->getValue(); 1548 if (!markReexport(searchName, extensions)) 1549 error(arg->getSpelling() + " " + searchName + 1550 " does not match a supplied dylib"); 1551 }; 1552 std::vector<StringRef> extensions = {".tbd"}; 1553 for (const Arg *arg : args.filtered(OPT_sub_umbrella)) 1554 reexportHandler(arg, extensions); 1555 1556 extensions.push_back(".dylib"); 1557 for (const Arg *arg : args.filtered(OPT_sub_library)) 1558 reexportHandler(arg, extensions); 1559 } 1560 1561 cl::ResetAllOptionOccurrences(); 1562 1563 // Parse LTO options. 1564 if (const Arg *arg = args.getLastArg(OPT_mcpu)) 1565 parseClangOption(saver().save("-mcpu=" + StringRef(arg->getValue())), 1566 arg->getSpelling()); 1567 1568 for (const Arg *arg : args.filtered(OPT_mllvm)) 1569 parseClangOption(arg->getValue(), arg->getSpelling()); 1570 1571 compileBitcodeFiles(); 1572 replaceCommonSymbols(); 1573 1574 StringRef orderFile = args.getLastArgValue(OPT_order_file); 1575 if (!orderFile.empty()) 1576 priorityBuilder.parseOrderFile(orderFile); 1577 1578 referenceStubBinder(); 1579 1580 // FIXME: should terminate the link early based on errors encountered so 1581 // far? 1582 1583 createSyntheticSections(); 1584 createSyntheticSymbols(); 1585 1586 for (const auto &pair : config->aliasedSymbols) { 1587 if (const auto &sym = symtab->find(pair.first)) { 1588 if (const auto &defined = dyn_cast<Defined>(sym)) { 1589 symtab->aliasDefined(defined, pair.second); 1590 continue; 1591 } 1592 } 1593 1594 warn("undefined base symbol '" + pair.first + "' for alias '" + 1595 pair.second + "'\n"); 1596 } 1597 1598 if (config->hasExplicitExports) { 1599 parallelForEach(symtab->getSymbols(), [](Symbol *sym) { 1600 if (auto *defined = dyn_cast<Defined>(sym)) { 1601 StringRef symbolName = defined->getName(); 1602 if (config->exportedSymbols.match(symbolName)) { 1603 if (defined->privateExtern) { 1604 if (defined->weakDefCanBeHidden) { 1605 // weak_def_can_be_hidden symbols behave similarly to 1606 // private_extern symbols in most cases, except for when 1607 // it is explicitly exported. 1608 // The former can be exported but the latter cannot. 1609 defined->privateExtern = false; 1610 } else { 1611 warn("cannot export hidden symbol " + toString(*defined) + 1612 "\n>>> defined in " + toString(defined->getFile())); 1613 } 1614 } 1615 } else { 1616 defined->privateExtern = true; 1617 } 1618 } 1619 }); 1620 } else if (!config->unexportedSymbols.empty()) { 1621 parallelForEach(symtab->getSymbols(), [](Symbol *sym) { 1622 if (auto *defined = dyn_cast<Defined>(sym)) 1623 if (config->unexportedSymbols.match(defined->getName())) 1624 defined->privateExtern = true; 1625 }); 1626 } 1627 1628 for (const Arg *arg : args.filtered(OPT_sectcreate)) { 1629 StringRef segName = arg->getValue(0); 1630 StringRef sectName = arg->getValue(1); 1631 StringRef fileName = arg->getValue(2); 1632 Optional<MemoryBufferRef> buffer = readFile(fileName); 1633 if (buffer) 1634 inputFiles.insert(make<OpaqueFile>(*buffer, segName, sectName)); 1635 } 1636 1637 for (const Arg *arg : args.filtered(OPT_add_empty_section)) { 1638 StringRef segName = arg->getValue(0); 1639 StringRef sectName = arg->getValue(1); 1640 inputFiles.insert(make<OpaqueFile>(MemoryBufferRef(), segName, sectName)); 1641 } 1642 1643 gatherInputSections(); 1644 if (config->callGraphProfileSort) 1645 priorityBuilder.extractCallGraphProfile(); 1646 1647 if (config->deadStrip) 1648 markLive(); 1649 1650 // ICF assumes that all literals have been folded already, so we must run 1651 // foldIdenticalLiterals before foldIdenticalSections. 1652 foldIdenticalLiterals(); 1653 if (config->icfLevel != ICFLevel::none) { 1654 if (config->icfLevel == ICFLevel::safe) 1655 markAddrSigSymbols(); 1656 foldIdenticalSections(); 1657 } 1658 1659 // Write to an output file. 1660 if (target->wordSize == 8) 1661 writeResult<LP64>(); 1662 else 1663 writeResult<ILP32>(); 1664 1665 depTracker->write(getLLDVersion(), inputFiles, config->outputFile); 1666 } 1667 1668 if (config->timeTraceEnabled) { 1669 checkError(timeTraceProfilerWrite( 1670 args.getLastArgValue(OPT_time_trace_eq).str(), config->outputFile)); 1671 1672 timeTraceProfilerCleanup(); 1673 } 1674 return errorCount() == 0; 1675 } 1676