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 static DenseMap<StringRef, ArchiveFile *> loadedArchives; 251 252 static InputFile *addFile(StringRef path, ForceLoad forceLoadArchive, 253 bool isLazy = false, bool isExplicit = true, 254 bool isBundleLoader = false) { 255 Optional<MemoryBufferRef> buffer = readFile(path); 256 if (!buffer) 257 return nullptr; 258 MemoryBufferRef mbref = *buffer; 259 InputFile *newFile = nullptr; 260 261 file_magic magic = identify_magic(mbref.getBuffer()); 262 switch (magic) { 263 case file_magic::archive: { 264 // Avoid loading archives twice. If the archives are being force-loaded, 265 // loading them twice would create duplicate symbol errors. In the 266 // non-force-loading case, this is just a minor performance optimization. 267 // We don't take a reference to cachedFile here because the 268 // loadArchiveMember() call below may recursively call addFile() and 269 // invalidate this reference. 270 if (ArchiveFile *cachedFile = loadedArchives[path]) 271 return cachedFile; 272 273 std::unique_ptr<object::Archive> archive = CHECK( 274 object::Archive::create(mbref), path + ": failed to parse archive"); 275 276 if (!archive->isEmpty() && !archive->hasSymbolTable()) 277 error(path + ": archive has no index; run ranlib to add one"); 278 279 auto *file = make<ArchiveFile>(std::move(archive)); 280 if ((forceLoadArchive == ForceLoad::Default && config->allLoad) || 281 forceLoadArchive == ForceLoad::Yes) { 282 if (Optional<MemoryBufferRef> buffer = readFile(path)) { 283 Error e = Error::success(); 284 for (const object::Archive::Child &c : file->getArchive().children(e)) { 285 StringRef reason = 286 forceLoadArchive == ForceLoad::Yes ? "-force_load" : "-all_load"; 287 if (Error e = file->fetch(c, reason)) 288 error(toString(file) + ": " + reason + 289 " failed to load archive member: " + toString(std::move(e))); 290 } 291 if (e) 292 error(toString(file) + 293 ": Archive::children failed: " + toString(std::move(e))); 294 } 295 } else if (forceLoadArchive == ForceLoad::Default && 296 config->forceLoadObjC) { 297 for (const object::Archive::Symbol &sym : file->getArchive().symbols()) 298 if (sym.getName().startswith(objc::klass)) 299 file->fetch(sym); 300 301 // TODO: no need to look for ObjC sections for a given archive member if 302 // we already found that it contains an ObjC symbol. 303 if (Optional<MemoryBufferRef> buffer = readFile(path)) { 304 Error e = Error::success(); 305 for (const object::Archive::Child &c : file->getArchive().children(e)) { 306 Expected<MemoryBufferRef> mb = c.getMemoryBufferRef(); 307 if (!mb || !hasObjCSection(*mb)) 308 continue; 309 if (Error e = file->fetch(c, "-ObjC")) 310 error(toString(file) + ": -ObjC failed to load archive member: " + 311 toString(std::move(e))); 312 } 313 if (e) 314 error(toString(file) + 315 ": Archive::children failed: " + toString(std::move(e))); 316 } 317 } 318 319 file->addLazySymbols(); 320 newFile = loadedArchives[path] = file; 321 break; 322 } 323 case file_magic::macho_object: 324 newFile = make<ObjFile>(mbref, getModTime(path), "", isLazy); 325 break; 326 case file_magic::macho_dynamically_linked_shared_lib: 327 case file_magic::macho_dynamically_linked_shared_lib_stub: 328 case file_magic::tapi_file: 329 if (DylibFile *dylibFile = loadDylib(mbref)) { 330 if (isExplicit) 331 dylibFile->explicitlyLinked = true; 332 newFile = dylibFile; 333 } 334 break; 335 case file_magic::bitcode: 336 newFile = make<BitcodeFile>(mbref, "", 0, isLazy); 337 break; 338 case file_magic::macho_executable: 339 case file_magic::macho_bundle: 340 // We only allow executable and bundle type here if it is used 341 // as a bundle loader. 342 if (!isBundleLoader) 343 error(path + ": unhandled file type"); 344 if (DylibFile *dylibFile = loadDylib(mbref, nullptr, isBundleLoader)) 345 newFile = dylibFile; 346 break; 347 default: 348 error(path + ": unhandled file type"); 349 } 350 if (newFile && !isa<DylibFile>(newFile)) { 351 if ((isa<ObjFile>(newFile) || isa<BitcodeFile>(newFile)) && newFile->lazy && 352 config->forceLoadObjC) { 353 for (Symbol *sym : newFile->symbols) 354 if (sym && sym->getName().startswith(objc::klass)) { 355 extract(*newFile, "-ObjC"); 356 break; 357 } 358 if (newFile->lazy && hasObjCSection(mbref)) 359 extract(*newFile, "-ObjC"); 360 } 361 362 // printArchiveMemberLoad() prints both .a and .o names, so no need to 363 // print the .a name here. Similarly skip lazy files. 364 if (config->printEachFile && magic != file_magic::archive && !isLazy) 365 message(toString(newFile)); 366 inputFiles.insert(newFile); 367 } 368 return newFile; 369 } 370 371 static void addLibrary(StringRef name, bool isNeeded, bool isWeak, 372 bool isReexport, bool isExplicit, 373 ForceLoad forceLoadArchive) { 374 if (Optional<StringRef> path = findLibrary(name)) { 375 if (auto *dylibFile = dyn_cast_or_null<DylibFile>( 376 addFile(*path, forceLoadArchive, /*isLazy=*/false, isExplicit))) { 377 if (isNeeded) 378 dylibFile->forceNeeded = true; 379 if (isWeak) 380 dylibFile->forceWeakImport = true; 381 if (isReexport) { 382 config->hasReexports = true; 383 dylibFile->reexport = true; 384 } 385 } 386 return; 387 } 388 error("library not found for -l" + name); 389 } 390 391 static void addFramework(StringRef name, bool isNeeded, bool isWeak, 392 bool isReexport, bool isExplicit, 393 ForceLoad forceLoadArchive) { 394 if (Optional<StringRef> path = findFramework(name)) { 395 if (auto *dylibFile = dyn_cast_or_null<DylibFile>( 396 addFile(*path, forceLoadArchive, /*isLazy=*/false, isExplicit))) { 397 if (isNeeded) 398 dylibFile->forceNeeded = true; 399 if (isWeak) 400 dylibFile->forceWeakImport = true; 401 if (isReexport) { 402 config->hasReexports = true; 403 dylibFile->reexport = true; 404 } 405 } 406 return; 407 } 408 error("framework not found for -framework " + name); 409 } 410 411 // Parses LC_LINKER_OPTION contents, which can add additional command line 412 // flags. This directly parses the flags instead of using the standard argument 413 // parser to improve performance. 414 void macho::parseLCLinkerOption(InputFile *f, unsigned argc, StringRef data) { 415 SmallVector<StringRef, 4> argv; 416 size_t offset = 0; 417 for (unsigned i = 0; i < argc && offset < data.size(); ++i) { 418 argv.push_back(data.data() + offset); 419 offset += strlen(data.data() + offset) + 1; 420 } 421 if (argv.size() != argc || offset > data.size()) 422 fatal(toString(f) + ": invalid LC_LINKER_OPTION"); 423 424 unsigned i = 0; 425 StringRef arg = argv[i]; 426 if (arg.consume_front("-l")) { 427 ForceLoad forceLoadArchive = 428 config->forceLoadSwift && arg.startswith("swift") ? ForceLoad::Yes 429 : ForceLoad::No; 430 addLibrary(arg, /*isNeeded=*/false, /*isWeak=*/false, 431 /*isReexport=*/false, /*isExplicit=*/false, forceLoadArchive); 432 } else if (arg == "-framework") { 433 StringRef name = argv[++i]; 434 addFramework(name, /*isNeeded=*/false, /*isWeak=*/false, 435 /*isReexport=*/false, /*isExplicit=*/false, ForceLoad::No); 436 } else { 437 error(arg + " is not allowed in LC_LINKER_OPTION"); 438 } 439 } 440 441 static void addFileList(StringRef path, bool isLazy) { 442 Optional<MemoryBufferRef> buffer = readFile(path); 443 if (!buffer) 444 return; 445 MemoryBufferRef mbref = *buffer; 446 for (StringRef path : args::getLines(mbref)) 447 addFile(rerootPath(path), ForceLoad::Default, isLazy); 448 } 449 450 // An order file has one entry per line, in the following format: 451 // 452 // <cpu>:<object file>:<symbol name> 453 // 454 // <cpu> and <object file> are optional. If not specified, then that entry 455 // matches any symbol of that name. Parsing this format is not quite 456 // straightforward because the symbol name itself can contain colons, so when 457 // encountering a colon, we consider the preceding characters to decide if it 458 // can be a valid CPU type or file path. 459 // 460 // If a symbol is matched by multiple entries, then it takes the lowest-ordered 461 // entry (the one nearest to the front of the list.) 462 // 463 // The file can also have line comments that start with '#'. 464 // We expect sub-library names of the form "libfoo", which will match a dylib 465 // with a path of .*/libfoo.{dylib, tbd}. 466 // XXX ld64 seems to ignore the extension entirely when matching sub-libraries; 467 // I'm not sure what the use case for that is. 468 static bool markReexport(StringRef searchName, ArrayRef<StringRef> extensions) { 469 for (InputFile *file : inputFiles) { 470 if (auto *dylibFile = dyn_cast<DylibFile>(file)) { 471 StringRef filename = path::filename(dylibFile->getName()); 472 if (filename.consume_front(searchName) && 473 (filename.empty() || 474 find(extensions, filename) != extensions.end())) { 475 dylibFile->reexport = true; 476 return true; 477 } 478 } 479 } 480 return false; 481 } 482 483 // This function is called on startup. We need this for LTO since 484 // LTO calls LLVM functions to compile bitcode files to native code. 485 // Technically this can be delayed until we read bitcode files, but 486 // we don't bother to do lazily because the initialization is fast. 487 static void initLLVM() { 488 InitializeAllTargets(); 489 InitializeAllTargetMCs(); 490 InitializeAllAsmPrinters(); 491 InitializeAllAsmParsers(); 492 } 493 494 static void compileBitcodeFiles() { 495 // FIXME: Remove this once LTO.cpp honors config->exportDynamic. 496 if (config->exportDynamic) 497 for (InputFile *file : inputFiles) 498 if (isa<BitcodeFile>(file)) { 499 warn("the effect of -export_dynamic on LTO is not yet implemented"); 500 break; 501 } 502 503 TimeTraceScope timeScope("LTO"); 504 auto *lto = make<BitcodeCompiler>(); 505 for (InputFile *file : inputFiles) 506 if (auto *bitcodeFile = dyn_cast<BitcodeFile>(file)) 507 if (!file->lazy) 508 lto->add(*bitcodeFile); 509 510 for (ObjFile *file : lto->compile()) 511 inputFiles.insert(file); 512 } 513 514 // Replaces common symbols with defined symbols residing in __common sections. 515 // This function must be called after all symbol names are resolved (i.e. after 516 // all InputFiles have been loaded.) As a result, later operations won't see 517 // any CommonSymbols. 518 static void replaceCommonSymbols() { 519 TimeTraceScope timeScope("Replace common symbols"); 520 ConcatOutputSection *osec = nullptr; 521 for (Symbol *sym : symtab->getSymbols()) { 522 auto *common = dyn_cast<CommonSymbol>(sym); 523 if (common == nullptr) 524 continue; 525 526 // Casting to size_t will truncate large values on 32-bit architectures, 527 // but it's not really worth supporting the linking of 64-bit programs on 528 // 32-bit archs. 529 ArrayRef<uint8_t> data = {nullptr, static_cast<size_t>(common->size)}; 530 auto *isec = make<ConcatInputSection>( 531 segment_names::data, section_names::common, common->getFile(), data, 532 common->align, S_ZEROFILL); 533 if (!osec) 534 osec = ConcatOutputSection::getOrCreateForInput(isec); 535 isec->parent = osec; 536 inputSections.push_back(isec); 537 538 // FIXME: CommonSymbol should store isReferencedDynamically, noDeadStrip 539 // and pass them on here. 540 replaceSymbol<Defined>(sym, sym->getName(), isec->getFile(), isec, 541 /*value=*/0, 542 /*size=*/0, 543 /*isWeakDef=*/false, 544 /*isExternal=*/true, common->privateExtern, 545 /*isThumb=*/false, 546 /*isReferencedDynamically=*/false, 547 /*noDeadStrip=*/false); 548 } 549 } 550 551 static void initializeSectionRenameMap() { 552 if (config->dataConst) { 553 SmallVector<StringRef> v{section_names::got, 554 section_names::authGot, 555 section_names::authPtr, 556 section_names::nonLazySymbolPtr, 557 section_names::const_, 558 section_names::cfString, 559 section_names::moduleInitFunc, 560 section_names::moduleTermFunc, 561 section_names::objcClassList, 562 section_names::objcNonLazyClassList, 563 section_names::objcCatList, 564 section_names::objcNonLazyCatList, 565 section_names::objcProtoList, 566 section_names::objcImageInfo}; 567 for (StringRef s : v) 568 config->sectionRenameMap[{segment_names::data, s}] = { 569 segment_names::dataConst, s}; 570 } 571 config->sectionRenameMap[{segment_names::text, section_names::staticInit}] = { 572 segment_names::text, section_names::text}; 573 config->sectionRenameMap[{segment_names::import, section_names::pointers}] = { 574 config->dataConst ? segment_names::dataConst : segment_names::data, 575 section_names::nonLazySymbolPtr}; 576 } 577 578 static inline char toLowerDash(char x) { 579 if (x >= 'A' && x <= 'Z') 580 return x - 'A' + 'a'; 581 else if (x == ' ') 582 return '-'; 583 return x; 584 } 585 586 static std::string lowerDash(StringRef s) { 587 return std::string(map_iterator(s.begin(), toLowerDash), 588 map_iterator(s.end(), toLowerDash)); 589 } 590 591 // Has the side-effect of setting Config::platformInfo. 592 static PlatformType parsePlatformVersion(const ArgList &args) { 593 const Arg *arg = args.getLastArg(OPT_platform_version); 594 if (!arg) { 595 error("must specify -platform_version"); 596 return PLATFORM_UNKNOWN; 597 } 598 599 StringRef platformStr = arg->getValue(0); 600 StringRef minVersionStr = arg->getValue(1); 601 StringRef sdkVersionStr = arg->getValue(2); 602 603 // TODO(compnerd) see if we can generate this case list via XMACROS 604 PlatformType platform = 605 StringSwitch<PlatformType>(lowerDash(platformStr)) 606 .Cases("macos", "1", PLATFORM_MACOS) 607 .Cases("ios", "2", PLATFORM_IOS) 608 .Cases("tvos", "3", PLATFORM_TVOS) 609 .Cases("watchos", "4", PLATFORM_WATCHOS) 610 .Cases("bridgeos", "5", PLATFORM_BRIDGEOS) 611 .Cases("mac-catalyst", "6", PLATFORM_MACCATALYST) 612 .Cases("ios-simulator", "7", PLATFORM_IOSSIMULATOR) 613 .Cases("tvos-simulator", "8", PLATFORM_TVOSSIMULATOR) 614 .Cases("watchos-simulator", "9", PLATFORM_WATCHOSSIMULATOR) 615 .Cases("driverkit", "10", PLATFORM_DRIVERKIT) 616 .Default(PLATFORM_UNKNOWN); 617 if (platform == PLATFORM_UNKNOWN) 618 error(Twine("malformed platform: ") + platformStr); 619 // TODO: check validity of version strings, which varies by platform 620 // NOTE: ld64 accepts version strings with 5 components 621 // llvm::VersionTuple accepts no more than 4 components 622 // Has Apple ever published version strings with 5 components? 623 if (config->platformInfo.minimum.tryParse(minVersionStr)) 624 error(Twine("malformed minimum version: ") + minVersionStr); 625 if (config->platformInfo.sdk.tryParse(sdkVersionStr)) 626 error(Twine("malformed sdk version: ") + sdkVersionStr); 627 return platform; 628 } 629 630 // Has the side-effect of setting Config::target. 631 static TargetInfo *createTargetInfo(InputArgList &args) { 632 StringRef archName = args.getLastArgValue(OPT_arch); 633 if (archName.empty()) { 634 error("must specify -arch"); 635 return nullptr; 636 } 637 638 PlatformType platform = parsePlatformVersion(args); 639 config->platformInfo.target = 640 MachO::Target(getArchitectureFromName(archName), platform); 641 642 uint32_t cpuType; 643 uint32_t cpuSubtype; 644 std::tie(cpuType, cpuSubtype) = getCPUTypeFromArchitecture(config->arch()); 645 646 switch (cpuType) { 647 case CPU_TYPE_X86_64: 648 return createX86_64TargetInfo(); 649 case CPU_TYPE_ARM64: 650 return createARM64TargetInfo(); 651 case CPU_TYPE_ARM64_32: 652 return createARM64_32TargetInfo(); 653 case CPU_TYPE_ARM: 654 return createARMTargetInfo(cpuSubtype); 655 default: 656 error("missing or unsupported -arch " + archName); 657 return nullptr; 658 } 659 } 660 661 static UndefinedSymbolTreatment 662 getUndefinedSymbolTreatment(const ArgList &args) { 663 StringRef treatmentStr = args.getLastArgValue(OPT_undefined); 664 auto treatment = 665 StringSwitch<UndefinedSymbolTreatment>(treatmentStr) 666 .Cases("error", "", UndefinedSymbolTreatment::error) 667 .Case("warning", UndefinedSymbolTreatment::warning) 668 .Case("suppress", UndefinedSymbolTreatment::suppress) 669 .Case("dynamic_lookup", UndefinedSymbolTreatment::dynamic_lookup) 670 .Default(UndefinedSymbolTreatment::unknown); 671 if (treatment == UndefinedSymbolTreatment::unknown) { 672 warn(Twine("unknown -undefined TREATMENT '") + treatmentStr + 673 "', defaulting to 'error'"); 674 treatment = UndefinedSymbolTreatment::error; 675 } else if (config->namespaceKind == NamespaceKind::twolevel && 676 (treatment == UndefinedSymbolTreatment::warning || 677 treatment == UndefinedSymbolTreatment::suppress)) { 678 if (treatment == UndefinedSymbolTreatment::warning) 679 error("'-undefined warning' only valid with '-flat_namespace'"); 680 else 681 error("'-undefined suppress' only valid with '-flat_namespace'"); 682 treatment = UndefinedSymbolTreatment::error; 683 } 684 return treatment; 685 } 686 687 static ICFLevel getICFLevel(const ArgList &args) { 688 StringRef icfLevelStr = args.getLastArgValue(OPT_icf_eq); 689 auto icfLevel = StringSwitch<ICFLevel>(icfLevelStr) 690 .Cases("none", "", ICFLevel::none) 691 .Case("safe", ICFLevel::safe) 692 .Case("all", ICFLevel::all) 693 .Default(ICFLevel::unknown); 694 if (icfLevel == ICFLevel::unknown) { 695 warn(Twine("unknown --icf=OPTION `") + icfLevelStr + 696 "', defaulting to `none'"); 697 icfLevel = ICFLevel::none; 698 } else if (icfLevel == ICFLevel::safe) { 699 warn(Twine("`--icf=safe' is not yet implemented, reverting to `none'")); 700 icfLevel = ICFLevel::none; 701 } 702 return icfLevel; 703 } 704 705 static void warnIfDeprecatedOption(const Option &opt) { 706 if (!opt.getGroup().isValid()) 707 return; 708 if (opt.getGroup().getID() == OPT_grp_deprecated) { 709 warn("Option `" + opt.getPrefixedName() + "' is deprecated in ld64:"); 710 warn(opt.getHelpText()); 711 } 712 } 713 714 static void warnIfUnimplementedOption(const Option &opt) { 715 if (!opt.getGroup().isValid() || !opt.hasFlag(DriverFlag::HelpHidden)) 716 return; 717 switch (opt.getGroup().getID()) { 718 case OPT_grp_deprecated: 719 // warn about deprecated options elsewhere 720 break; 721 case OPT_grp_undocumented: 722 warn("Option `" + opt.getPrefixedName() + 723 "' is undocumented. Should lld implement it?"); 724 break; 725 case OPT_grp_obsolete: 726 warn("Option `" + opt.getPrefixedName() + 727 "' is obsolete. Please modernize your usage."); 728 break; 729 case OPT_grp_ignored: 730 warn("Option `" + opt.getPrefixedName() + "' is ignored."); 731 break; 732 case OPT_grp_ignored_silently: 733 break; 734 default: 735 warn("Option `" + opt.getPrefixedName() + 736 "' is not yet implemented. Stay tuned..."); 737 break; 738 } 739 } 740 741 static const char *getReproduceOption(InputArgList &args) { 742 if (const Arg *arg = args.getLastArg(OPT_reproduce)) 743 return arg->getValue(); 744 return getenv("LLD_REPRODUCE"); 745 } 746 747 static void parseClangOption(StringRef opt, const Twine &msg) { 748 std::string err; 749 raw_string_ostream os(err); 750 751 const char *argv[] = {"lld", opt.data()}; 752 if (cl::ParseCommandLineOptions(2, argv, "", &os)) 753 return; 754 os.flush(); 755 error(msg + ": " + StringRef(err).trim()); 756 } 757 758 static uint32_t parseDylibVersion(const ArgList &args, unsigned id) { 759 const Arg *arg = args.getLastArg(id); 760 if (!arg) 761 return 0; 762 763 if (config->outputType != MH_DYLIB) { 764 error(arg->getAsString(args) + ": only valid with -dylib"); 765 return 0; 766 } 767 768 PackedVersion version; 769 if (!version.parse32(arg->getValue())) { 770 error(arg->getAsString(args) + ": malformed version"); 771 return 0; 772 } 773 774 return version.rawValue(); 775 } 776 777 static uint32_t parseProtection(StringRef protStr) { 778 uint32_t prot = 0; 779 for (char c : protStr) { 780 switch (c) { 781 case 'r': 782 prot |= VM_PROT_READ; 783 break; 784 case 'w': 785 prot |= VM_PROT_WRITE; 786 break; 787 case 'x': 788 prot |= VM_PROT_EXECUTE; 789 break; 790 case '-': 791 break; 792 default: 793 error("unknown -segprot letter '" + Twine(c) + "' in " + protStr); 794 return 0; 795 } 796 } 797 return prot; 798 } 799 800 static std::vector<SectionAlign> parseSectAlign(const opt::InputArgList &args) { 801 std::vector<SectionAlign> sectAligns; 802 for (const Arg *arg : args.filtered(OPT_sectalign)) { 803 StringRef segName = arg->getValue(0); 804 StringRef sectName = arg->getValue(1); 805 StringRef alignStr = arg->getValue(2); 806 if (alignStr.startswith("0x") || alignStr.startswith("0X")) 807 alignStr = alignStr.drop_front(2); 808 uint32_t align; 809 if (alignStr.getAsInteger(16, align)) { 810 error("-sectalign: failed to parse '" + StringRef(arg->getValue(2)) + 811 "' as number"); 812 continue; 813 } 814 if (!isPowerOf2_32(align)) { 815 error("-sectalign: '" + StringRef(arg->getValue(2)) + 816 "' (in base 16) not a power of two"); 817 continue; 818 } 819 sectAligns.push_back({segName, sectName, align}); 820 } 821 return sectAligns; 822 } 823 824 PlatformType macho::removeSimulator(PlatformType platform) { 825 switch (platform) { 826 case PLATFORM_IOSSIMULATOR: 827 return PLATFORM_IOS; 828 case PLATFORM_TVOSSIMULATOR: 829 return PLATFORM_TVOS; 830 case PLATFORM_WATCHOSSIMULATOR: 831 return PLATFORM_WATCHOS; 832 default: 833 return platform; 834 } 835 } 836 837 static bool dataConstDefault(const InputArgList &args) { 838 static const std::vector<std::pair<PlatformType, VersionTuple>> minVersion = { 839 {PLATFORM_MACOS, VersionTuple(10, 15)}, 840 {PLATFORM_IOS, VersionTuple(13, 0)}, 841 {PLATFORM_TVOS, VersionTuple(13, 0)}, 842 {PLATFORM_WATCHOS, VersionTuple(6, 0)}, 843 {PLATFORM_BRIDGEOS, VersionTuple(4, 0)}}; 844 PlatformType platform = removeSimulator(config->platformInfo.target.Platform); 845 auto it = llvm::find_if(minVersion, 846 [&](const auto &p) { return p.first == platform; }); 847 if (it != minVersion.end()) 848 if (config->platformInfo.minimum < it->second) 849 return false; 850 851 switch (config->outputType) { 852 case MH_EXECUTE: 853 return !args.hasArg(OPT_no_pie); 854 case MH_BUNDLE: 855 // FIXME: return false when -final_name ... 856 // has prefix "/System/Library/UserEventPlugins/" 857 // or matches "/usr/libexec/locationd" "/usr/libexec/terminusd" 858 return true; 859 case MH_DYLIB: 860 return true; 861 case MH_OBJECT: 862 return false; 863 default: 864 llvm_unreachable( 865 "unsupported output type for determining data-const default"); 866 } 867 return false; 868 } 869 870 void SymbolPatterns::clear() { 871 literals.clear(); 872 globs.clear(); 873 } 874 875 void SymbolPatterns::insert(StringRef symbolName) { 876 if (symbolName.find_first_of("*?[]") == StringRef::npos) 877 literals.insert(CachedHashStringRef(symbolName)); 878 else if (Expected<GlobPattern> pattern = GlobPattern::create(symbolName)) 879 globs.emplace_back(*pattern); 880 else 881 error("invalid symbol-name pattern: " + symbolName); 882 } 883 884 bool SymbolPatterns::matchLiteral(StringRef symbolName) const { 885 return literals.contains(CachedHashStringRef(symbolName)); 886 } 887 888 bool SymbolPatterns::matchGlob(StringRef symbolName) const { 889 for (const GlobPattern &glob : globs) 890 if (glob.match(symbolName)) 891 return true; 892 return false; 893 } 894 895 bool SymbolPatterns::match(StringRef symbolName) const { 896 return matchLiteral(symbolName) || matchGlob(symbolName); 897 } 898 899 static void handleSymbolPatterns(InputArgList &args, 900 SymbolPatterns &symbolPatterns, 901 unsigned singleOptionCode, 902 unsigned listFileOptionCode) { 903 for (const Arg *arg : args.filtered(singleOptionCode)) 904 symbolPatterns.insert(arg->getValue()); 905 for (const Arg *arg : args.filtered(listFileOptionCode)) { 906 StringRef path = arg->getValue(); 907 Optional<MemoryBufferRef> buffer = readFile(path); 908 if (!buffer) { 909 error("Could not read symbol file: " + path); 910 continue; 911 } 912 MemoryBufferRef mbref = *buffer; 913 for (StringRef line : args::getLines(mbref)) { 914 line = line.take_until([](char c) { return c == '#'; }).trim(); 915 if (!line.empty()) 916 symbolPatterns.insert(line); 917 } 918 } 919 } 920 921 static void createFiles(const InputArgList &args) { 922 TimeTraceScope timeScope("Load input files"); 923 // This loop should be reserved for options whose exact ordering matters. 924 // Other options should be handled via filtered() and/or getLastArg(). 925 bool isLazy = false; 926 for (const Arg *arg : args) { 927 const Option &opt = arg->getOption(); 928 warnIfDeprecatedOption(opt); 929 warnIfUnimplementedOption(opt); 930 931 switch (opt.getID()) { 932 case OPT_INPUT: 933 addFile(rerootPath(arg->getValue()), ForceLoad::Default, isLazy); 934 break; 935 case OPT_needed_library: 936 if (auto *dylibFile = dyn_cast_or_null<DylibFile>( 937 addFile(rerootPath(arg->getValue()), ForceLoad::Default))) 938 dylibFile->forceNeeded = true; 939 break; 940 case OPT_reexport_library: 941 if (auto *dylibFile = dyn_cast_or_null<DylibFile>( 942 addFile(rerootPath(arg->getValue()), ForceLoad::Default))) { 943 config->hasReexports = true; 944 dylibFile->reexport = true; 945 } 946 break; 947 case OPT_weak_library: 948 if (auto *dylibFile = dyn_cast_or_null<DylibFile>( 949 addFile(rerootPath(arg->getValue()), ForceLoad::Default))) 950 dylibFile->forceWeakImport = true; 951 break; 952 case OPT_filelist: 953 addFileList(arg->getValue(), isLazy); 954 break; 955 case OPT_force_load: 956 addFile(rerootPath(arg->getValue()), ForceLoad::Yes); 957 break; 958 case OPT_l: 959 case OPT_needed_l: 960 case OPT_reexport_l: 961 case OPT_weak_l: 962 addLibrary(arg->getValue(), opt.getID() == OPT_needed_l, 963 opt.getID() == OPT_weak_l, opt.getID() == OPT_reexport_l, 964 /*isExplicit=*/true, ForceLoad::Default); 965 break; 966 case OPT_framework: 967 case OPT_needed_framework: 968 case OPT_reexport_framework: 969 case OPT_weak_framework: 970 addFramework(arg->getValue(), opt.getID() == OPT_needed_framework, 971 opt.getID() == OPT_weak_framework, 972 opt.getID() == OPT_reexport_framework, /*isExplicit=*/true, 973 ForceLoad::Default); 974 break; 975 case OPT_start_lib: 976 if (isLazy) 977 error("nested --start-lib"); 978 isLazy = true; 979 break; 980 case OPT_end_lib: 981 if (!isLazy) 982 error("stray --end-lib"); 983 isLazy = false; 984 break; 985 default: 986 break; 987 } 988 } 989 } 990 991 static void gatherInputSections() { 992 TimeTraceScope timeScope("Gathering input sections"); 993 int inputOrder = 0; 994 for (const InputFile *file : inputFiles) { 995 for (const Section §ion : file->sections) { 996 const Subsections &subsections = section.subsections; 997 if (subsections.empty()) 998 continue; 999 if (subsections[0].isec->getName() == section_names::compactUnwind) 1000 // Compact unwind entries require special handling elsewhere. 1001 continue; 1002 ConcatOutputSection *osec = nullptr; 1003 for (const Subsection &subsection : subsections) { 1004 if (auto *isec = dyn_cast<ConcatInputSection>(subsection.isec)) { 1005 if (isec->isCoalescedWeak()) 1006 continue; 1007 isec->outSecOff = inputOrder++; 1008 if (!osec) 1009 osec = ConcatOutputSection::getOrCreateForInput(isec); 1010 isec->parent = osec; 1011 inputSections.push_back(isec); 1012 } else if (auto *isec = 1013 dyn_cast<CStringInputSection>(subsection.isec)) { 1014 if (in.cStringSection->inputOrder == UnspecifiedInputOrder) 1015 in.cStringSection->inputOrder = inputOrder++; 1016 in.cStringSection->addInput(isec); 1017 } else if (auto *isec = 1018 dyn_cast<WordLiteralInputSection>(subsection.isec)) { 1019 if (in.wordLiteralSection->inputOrder == UnspecifiedInputOrder) 1020 in.wordLiteralSection->inputOrder = inputOrder++; 1021 in.wordLiteralSection->addInput(isec); 1022 } else { 1023 llvm_unreachable("unexpected input section kind"); 1024 } 1025 } 1026 } 1027 } 1028 assert(inputOrder <= UnspecifiedInputOrder); 1029 } 1030 1031 static void foldIdenticalLiterals() { 1032 // We always create a cStringSection, regardless of whether dedupLiterals is 1033 // true. If it isn't, we simply create a non-deduplicating CStringSection. 1034 // Either way, we must unconditionally finalize it here. 1035 in.cStringSection->finalizeContents(); 1036 if (in.wordLiteralSection) 1037 in.wordLiteralSection->finalizeContents(); 1038 } 1039 1040 static void referenceStubBinder() { 1041 bool needsStubHelper = config->outputType == MH_DYLIB || 1042 config->outputType == MH_EXECUTE || 1043 config->outputType == MH_BUNDLE; 1044 if (!needsStubHelper || !symtab->find("dyld_stub_binder")) 1045 return; 1046 1047 // dyld_stub_binder is used by dyld to resolve lazy bindings. This code here 1048 // adds a opportunistic reference to dyld_stub_binder if it happens to exist. 1049 // dyld_stub_binder is in libSystem.dylib, which is usually linked in. This 1050 // isn't needed for correctness, but the presence of that symbol suppresses 1051 // "no symbols" diagnostics from `nm`. 1052 // StubHelperSection::setup() adds a reference and errors out if 1053 // dyld_stub_binder doesn't exist in case it is actually needed. 1054 symtab->addUndefined("dyld_stub_binder", /*file=*/nullptr, /*isWeak=*/false); 1055 } 1056 1057 bool macho::link(ArrayRef<const char *> argsArr, llvm::raw_ostream &stdoutOS, 1058 llvm::raw_ostream &stderrOS, bool exitEarly, 1059 bool disableOutput) { 1060 // This driver-specific context will be freed later by lldMain(). 1061 auto *ctx = new CommonLinkerContext; 1062 1063 ctx->e.initialize(stdoutOS, stderrOS, exitEarly, disableOutput); 1064 ctx->e.cleanupCallback = []() { 1065 resolvedFrameworks.clear(); 1066 resolvedLibraries.clear(); 1067 cachedReads.clear(); 1068 concatOutputSections.clear(); 1069 inputFiles.clear(); 1070 inputSections.clear(); 1071 loadedArchives.clear(); 1072 syntheticSections.clear(); 1073 thunkMap.clear(); 1074 1075 firstTLVDataSection = nullptr; 1076 tar = nullptr; 1077 memset(&in, 0, sizeof(in)); 1078 1079 resetLoadedDylibs(); 1080 resetOutputSegments(); 1081 resetWriter(); 1082 InputFile::resetIdCount(); 1083 }; 1084 1085 ctx->e.logName = args::getFilenameWithoutExe(argsArr[0]); 1086 1087 MachOOptTable parser; 1088 InputArgList args = parser.parse(argsArr.slice(1)); 1089 1090 ctx->e.errorLimitExceededMsg = "too many errors emitted, stopping now " 1091 "(use --error-limit=0 to see all errors)"; 1092 ctx->e.errorLimit = args::getInteger(args, OPT_error_limit_eq, 20); 1093 ctx->e.verbose = args.hasArg(OPT_verbose); 1094 1095 if (args.hasArg(OPT_help_hidden)) { 1096 parser.printHelp(argsArr[0], /*showHidden=*/true); 1097 return true; 1098 } 1099 if (args.hasArg(OPT_help)) { 1100 parser.printHelp(argsArr[0], /*showHidden=*/false); 1101 return true; 1102 } 1103 if (args.hasArg(OPT_version)) { 1104 message(getLLDVersion()); 1105 return true; 1106 } 1107 1108 config = std::make_unique<Configuration>(); 1109 symtab = std::make_unique<SymbolTable>(); 1110 target = createTargetInfo(args); 1111 depTracker = std::make_unique<DependencyTracker>( 1112 args.getLastArgValue(OPT_dependency_info)); 1113 if (errorCount()) 1114 return false; 1115 1116 config->osoPrefix = args.getLastArgValue(OPT_oso_prefix); 1117 if (!config->osoPrefix.empty()) { 1118 // Expand special characters, such as ".", "..", or "~", if present. 1119 // Note: LD64 only expands "." and not other special characters. 1120 // That seems silly to imitate so we will not try to follow it, but rather 1121 // just use real_path() to do it. 1122 1123 // The max path length is 4096, in theory. However that seems quite long 1124 // and seems unlikely that any one would want to strip everything from the 1125 // path. Hence we've picked a reasonably large number here. 1126 SmallString<1024> expanded; 1127 if (!fs::real_path(config->osoPrefix, expanded, 1128 /*expand_tilde=*/true)) { 1129 // Note: LD64 expands "." to be `<current_dir>/` 1130 // (ie., it has a slash suffix) whereas real_path() doesn't. 1131 // So we have to append '/' to be consistent. 1132 StringRef sep = sys::path::get_separator(); 1133 // real_path removes trailing slashes as part of the normalization, but 1134 // these are meaningful for our text based stripping 1135 if (config->osoPrefix.equals(".") || config->osoPrefix.endswith(sep)) 1136 expanded += sep; 1137 config->osoPrefix = saver().save(expanded.str()); 1138 } 1139 } 1140 1141 // Must be set before any InputSections and Symbols are created. 1142 config->deadStrip = args.hasArg(OPT_dead_strip); 1143 1144 config->systemLibraryRoots = getSystemLibraryRoots(args); 1145 if (const char *path = getReproduceOption(args)) { 1146 // Note that --reproduce is a debug option so you can ignore it 1147 // if you are trying to understand the whole picture of the code. 1148 Expected<std::unique_ptr<TarWriter>> errOrWriter = 1149 TarWriter::create(path, path::stem(path)); 1150 if (errOrWriter) { 1151 tar = std::move(*errOrWriter); 1152 tar->append("response.txt", createResponseFile(args)); 1153 tar->append("version.txt", getLLDVersion() + "\n"); 1154 } else { 1155 error("--reproduce: " + toString(errOrWriter.takeError())); 1156 } 1157 } 1158 1159 if (auto *arg = args.getLastArg(OPT_threads_eq)) { 1160 StringRef v(arg->getValue()); 1161 unsigned threads = 0; 1162 if (!llvm::to_integer(v, threads, 0) || threads == 0) 1163 error(arg->getSpelling() + ": expected a positive integer, but got '" + 1164 arg->getValue() + "'"); 1165 parallel::strategy = hardware_concurrency(threads); 1166 config->thinLTOJobs = v; 1167 } 1168 if (auto *arg = args.getLastArg(OPT_thinlto_jobs_eq)) 1169 config->thinLTOJobs = arg->getValue(); 1170 if (!get_threadpool_strategy(config->thinLTOJobs)) 1171 error("--thinlto-jobs: invalid job count: " + config->thinLTOJobs); 1172 1173 for (const Arg *arg : args.filtered(OPT_u)) { 1174 config->explicitUndefineds.push_back(symtab->addUndefined( 1175 arg->getValue(), /*file=*/nullptr, /*isWeakRef=*/false)); 1176 } 1177 1178 for (const Arg *arg : args.filtered(OPT_U)) 1179 config->explicitDynamicLookups.insert(arg->getValue()); 1180 1181 config->mapFile = args.getLastArgValue(OPT_map); 1182 config->optimize = args::getInteger(args, OPT_O, 1); 1183 config->outputFile = args.getLastArgValue(OPT_o, "a.out"); 1184 config->finalOutput = 1185 args.getLastArgValue(OPT_final_output, config->outputFile); 1186 config->astPaths = args.getAllArgValues(OPT_add_ast_path); 1187 config->headerPad = args::getHex(args, OPT_headerpad, /*Default=*/32); 1188 config->headerPadMaxInstallNames = 1189 args.hasArg(OPT_headerpad_max_install_names); 1190 config->printDylibSearch = 1191 args.hasArg(OPT_print_dylib_search) || getenv("RC_TRACE_DYLIB_SEARCHING"); 1192 config->printEachFile = args.hasArg(OPT_t); 1193 config->printWhyLoad = args.hasArg(OPT_why_load); 1194 config->omitDebugInfo = args.hasArg(OPT_S); 1195 config->outputType = getOutputType(args); 1196 config->errorForArchMismatch = args.hasArg(OPT_arch_errors_fatal); 1197 if (const Arg *arg = args.getLastArg(OPT_bundle_loader)) { 1198 if (config->outputType != MH_BUNDLE) 1199 error("-bundle_loader can only be used with MachO bundle output"); 1200 addFile(arg->getValue(), ForceLoad::Default, /*isLazy=*/false, 1201 /*isExplicit=*/false, 1202 /*isBundleLoader=*/true); 1203 } 1204 if (const Arg *arg = args.getLastArg(OPT_umbrella)) { 1205 if (config->outputType != MH_DYLIB) 1206 warn("-umbrella used, but not creating dylib"); 1207 config->umbrella = arg->getValue(); 1208 } 1209 config->ltoObjPath = args.getLastArgValue(OPT_object_path_lto); 1210 config->ltoNewPassManager = 1211 args.hasFlag(OPT_no_lto_legacy_pass_manager, OPT_lto_legacy_pass_manager, 1212 LLVM_ENABLE_NEW_PASS_MANAGER); 1213 config->ltoo = args::getInteger(args, OPT_lto_O, 2); 1214 if (config->ltoo > 3) 1215 error("--lto-O: invalid optimization level: " + Twine(config->ltoo)); 1216 config->thinLTOCacheDir = args.getLastArgValue(OPT_cache_path_lto); 1217 config->thinLTOCachePolicy = getLTOCachePolicy(args); 1218 config->runtimePaths = args::getStrings(args, OPT_rpath); 1219 config->allLoad = args.hasFlag(OPT_all_load, OPT_noall_load, false); 1220 config->archMultiple = args.hasArg(OPT_arch_multiple); 1221 config->applicationExtension = args.hasFlag( 1222 OPT_application_extension, OPT_no_application_extension, false); 1223 config->exportDynamic = args.hasArg(OPT_export_dynamic); 1224 config->forceLoadObjC = args.hasArg(OPT_ObjC); 1225 config->forceLoadSwift = args.hasArg(OPT_force_load_swift_libs); 1226 config->deadStripDylibs = args.hasArg(OPT_dead_strip_dylibs); 1227 config->demangle = args.hasArg(OPT_demangle); 1228 config->implicitDylibs = !args.hasArg(OPT_no_implicit_dylibs); 1229 config->emitFunctionStarts = 1230 args.hasFlag(OPT_function_starts, OPT_no_function_starts, true); 1231 config->emitBitcodeBundle = args.hasArg(OPT_bitcode_bundle); 1232 config->emitDataInCodeInfo = 1233 args.hasFlag(OPT_data_in_code_info, OPT_no_data_in_code_info, true); 1234 config->icfLevel = getICFLevel(args); 1235 config->dedupLiterals = 1236 args.hasFlag(OPT_deduplicate_literals, OPT_icf_eq, false) || 1237 config->icfLevel != ICFLevel::none; 1238 config->warnDylibInstallName = args.hasFlag( 1239 OPT_warn_dylib_install_name, OPT_no_warn_dylib_install_name, false); 1240 config->callGraphProfileSort = args.hasFlag( 1241 OPT_call_graph_profile_sort, OPT_no_call_graph_profile_sort, true); 1242 config->printSymbolOrder = args.getLastArgValue(OPT_print_symbol_order); 1243 1244 // FIXME: Add a commandline flag for this too. 1245 config->zeroModTime = getenv("ZERO_AR_DATE"); 1246 1247 std::array<PlatformType, 3> encryptablePlatforms{ 1248 PLATFORM_IOS, PLATFORM_WATCHOS, PLATFORM_TVOS}; 1249 config->emitEncryptionInfo = 1250 args.hasFlag(OPT_encryptable, OPT_no_encryption, 1251 is_contained(encryptablePlatforms, config->platform())); 1252 1253 #ifndef LLVM_HAVE_LIBXAR 1254 if (config->emitBitcodeBundle) 1255 error("-bitcode_bundle unsupported because LLD wasn't built with libxar"); 1256 #endif 1257 1258 if (const Arg *arg = args.getLastArg(OPT_install_name)) { 1259 if (config->warnDylibInstallName && config->outputType != MH_DYLIB) 1260 warn( 1261 arg->getAsString(args) + 1262 ": ignored, only has effect with -dylib [--warn-dylib-install-name]"); 1263 else 1264 config->installName = arg->getValue(); 1265 } else if (config->outputType == MH_DYLIB) { 1266 config->installName = config->finalOutput; 1267 } 1268 1269 if (args.hasArg(OPT_mark_dead_strippable_dylib)) { 1270 if (config->outputType != MH_DYLIB) 1271 warn("-mark_dead_strippable_dylib: ignored, only has effect with -dylib"); 1272 else 1273 config->markDeadStrippableDylib = true; 1274 } 1275 1276 if (const Arg *arg = args.getLastArg(OPT_static, OPT_dynamic)) 1277 config->staticLink = (arg->getOption().getID() == OPT_static); 1278 1279 if (const Arg *arg = 1280 args.getLastArg(OPT_flat_namespace, OPT_twolevel_namespace)) 1281 config->namespaceKind = arg->getOption().getID() == OPT_twolevel_namespace 1282 ? NamespaceKind::twolevel 1283 : NamespaceKind::flat; 1284 1285 config->undefinedSymbolTreatment = getUndefinedSymbolTreatment(args); 1286 1287 if (config->outputType == MH_EXECUTE) 1288 config->entry = symtab->addUndefined(args.getLastArgValue(OPT_e, "_main"), 1289 /*file=*/nullptr, 1290 /*isWeakRef=*/false); 1291 1292 config->librarySearchPaths = 1293 getLibrarySearchPaths(args, config->systemLibraryRoots); 1294 config->frameworkSearchPaths = 1295 getFrameworkSearchPaths(args, config->systemLibraryRoots); 1296 if (const Arg *arg = 1297 args.getLastArg(OPT_search_paths_first, OPT_search_dylibs_first)) 1298 config->searchDylibsFirst = 1299 arg->getOption().getID() == OPT_search_dylibs_first; 1300 1301 config->dylibCompatibilityVersion = 1302 parseDylibVersion(args, OPT_compatibility_version); 1303 config->dylibCurrentVersion = parseDylibVersion(args, OPT_current_version); 1304 1305 config->dataConst = 1306 args.hasFlag(OPT_data_const, OPT_no_data_const, dataConstDefault(args)); 1307 // Populate config->sectionRenameMap with builtin default renames. 1308 // Options -rename_section and -rename_segment are able to override. 1309 initializeSectionRenameMap(); 1310 // Reject every special character except '.' and '$' 1311 // TODO(gkm): verify that this is the proper set of invalid chars 1312 StringRef invalidNameChars("!\"#%&'()*+,-/:;<=>?@[\\]^`{|}~"); 1313 auto validName = [invalidNameChars](StringRef s) { 1314 if (s.find_first_of(invalidNameChars) != StringRef::npos) 1315 error("invalid name for segment or section: " + s); 1316 return s; 1317 }; 1318 for (const Arg *arg : args.filtered(OPT_rename_section)) { 1319 config->sectionRenameMap[{validName(arg->getValue(0)), 1320 validName(arg->getValue(1))}] = { 1321 validName(arg->getValue(2)), validName(arg->getValue(3))}; 1322 } 1323 for (const Arg *arg : args.filtered(OPT_rename_segment)) { 1324 config->segmentRenameMap[validName(arg->getValue(0))] = 1325 validName(arg->getValue(1)); 1326 } 1327 1328 config->sectionAlignments = parseSectAlign(args); 1329 1330 for (const Arg *arg : args.filtered(OPT_segprot)) { 1331 StringRef segName = arg->getValue(0); 1332 uint32_t maxProt = parseProtection(arg->getValue(1)); 1333 uint32_t initProt = parseProtection(arg->getValue(2)); 1334 if (maxProt != initProt && config->arch() != AK_i386) 1335 error("invalid argument '" + arg->getAsString(args) + 1336 "': max and init must be the same for non-i386 archs"); 1337 if (segName == segment_names::linkEdit) 1338 error("-segprot cannot be used to change __LINKEDIT's protections"); 1339 config->segmentProtections.push_back({segName, maxProt, initProt}); 1340 } 1341 1342 handleSymbolPatterns(args, config->exportedSymbols, OPT_exported_symbol, 1343 OPT_exported_symbols_list); 1344 handleSymbolPatterns(args, config->unexportedSymbols, OPT_unexported_symbol, 1345 OPT_unexported_symbols_list); 1346 if (!config->exportedSymbols.empty() && !config->unexportedSymbols.empty()) { 1347 error("cannot use both -exported_symbol* and -unexported_symbol* options\n" 1348 ">>> ignoring unexports"); 1349 config->unexportedSymbols.clear(); 1350 } 1351 // Explicitly-exported literal symbols must be defined, but might 1352 // languish in an archive if unreferenced elsewhere. Light a fire 1353 // under those lazy symbols! 1354 for (const CachedHashStringRef &cachedName : config->exportedSymbols.literals) 1355 symtab->addUndefined(cachedName.val(), /*file=*/nullptr, 1356 /*isWeakRef=*/false); 1357 1358 config->saveTemps = args.hasArg(OPT_save_temps); 1359 1360 config->adhocCodesign = args.hasFlag( 1361 OPT_adhoc_codesign, OPT_no_adhoc_codesign, 1362 (config->arch() == AK_arm64 || config->arch() == AK_arm64e) && 1363 config->platform() == PLATFORM_MACOS); 1364 1365 if (args.hasArg(OPT_v)) { 1366 message(getLLDVersion(), lld::errs()); 1367 message(StringRef("Library search paths:") + 1368 (config->librarySearchPaths.empty() 1369 ? "" 1370 : "\n\t" + join(config->librarySearchPaths, "\n\t")), 1371 lld::errs()); 1372 message(StringRef("Framework search paths:") + 1373 (config->frameworkSearchPaths.empty() 1374 ? "" 1375 : "\n\t" + join(config->frameworkSearchPaths, "\n\t")), 1376 lld::errs()); 1377 } 1378 1379 config->progName = argsArr[0]; 1380 1381 config->timeTraceEnabled = args.hasArg( 1382 OPT_time_trace, OPT_time_trace_granularity_eq, OPT_time_trace_file_eq); 1383 config->timeTraceGranularity = 1384 args::getInteger(args, OPT_time_trace_granularity_eq, 500); 1385 1386 // Initialize time trace profiler. 1387 if (config->timeTraceEnabled) 1388 timeTraceProfilerInitialize(config->timeTraceGranularity, config->progName); 1389 1390 { 1391 TimeTraceScope timeScope("ExecuteLinker"); 1392 1393 initLLVM(); // must be run before any call to addFile() 1394 createFiles(args); 1395 1396 config->isPic = config->outputType == MH_DYLIB || 1397 config->outputType == MH_BUNDLE || 1398 (config->outputType == MH_EXECUTE && 1399 args.hasFlag(OPT_pie, OPT_no_pie, true)); 1400 1401 // Now that all dylibs have been loaded, search for those that should be 1402 // re-exported. 1403 { 1404 auto reexportHandler = [](const Arg *arg, 1405 const std::vector<StringRef> &extensions) { 1406 config->hasReexports = true; 1407 StringRef searchName = arg->getValue(); 1408 if (!markReexport(searchName, extensions)) 1409 error(arg->getSpelling() + " " + searchName + 1410 " does not match a supplied dylib"); 1411 }; 1412 std::vector<StringRef> extensions = {".tbd"}; 1413 for (const Arg *arg : args.filtered(OPT_sub_umbrella)) 1414 reexportHandler(arg, extensions); 1415 1416 extensions.push_back(".dylib"); 1417 for (const Arg *arg : args.filtered(OPT_sub_library)) 1418 reexportHandler(arg, extensions); 1419 } 1420 1421 cl::ResetAllOptionOccurrences(); 1422 1423 // Parse LTO options. 1424 if (const Arg *arg = args.getLastArg(OPT_mcpu)) 1425 parseClangOption(saver().save("-mcpu=" + StringRef(arg->getValue())), 1426 arg->getSpelling()); 1427 1428 for (const Arg *arg : args.filtered(OPT_mllvm)) 1429 parseClangOption(arg->getValue(), arg->getSpelling()); 1430 1431 compileBitcodeFiles(); 1432 replaceCommonSymbols(); 1433 1434 StringRef orderFile = args.getLastArgValue(OPT_order_file); 1435 if (!orderFile.empty()) { 1436 parseOrderFile(orderFile); 1437 config->callGraphProfileSort = false; 1438 } 1439 1440 referenceStubBinder(); 1441 1442 // FIXME: should terminate the link early based on errors encountered so 1443 // far? 1444 1445 createSyntheticSections(); 1446 createSyntheticSymbols(); 1447 1448 if (!config->exportedSymbols.empty()) { 1449 parallelForEach(symtab->getSymbols(), [](Symbol *sym) { 1450 if (auto *defined = dyn_cast<Defined>(sym)) { 1451 StringRef symbolName = defined->getName(); 1452 if (config->exportedSymbols.match(symbolName)) { 1453 if (defined->privateExtern) { 1454 if (defined->weakDefCanBeHidden) { 1455 // weak_def_can_be_hidden symbols behave similarly to 1456 // private_extern symbols in most cases, except for when 1457 // it is explicitly exported. 1458 // The former can be exported but the latter cannot. 1459 defined->privateExtern = false; 1460 } else { 1461 warn("cannot export hidden symbol " + symbolName + 1462 "\n>>> defined in " + toString(defined->getFile())); 1463 } 1464 } 1465 } else { 1466 defined->privateExtern = true; 1467 } 1468 } 1469 }); 1470 } else if (!config->unexportedSymbols.empty()) { 1471 parallelForEach(symtab->getSymbols(), [](Symbol *sym) { 1472 if (auto *defined = dyn_cast<Defined>(sym)) 1473 if (config->unexportedSymbols.match(defined->getName())) 1474 defined->privateExtern = true; 1475 }); 1476 } 1477 1478 for (const Arg *arg : args.filtered(OPT_sectcreate)) { 1479 StringRef segName = arg->getValue(0); 1480 StringRef sectName = arg->getValue(1); 1481 StringRef fileName = arg->getValue(2); 1482 Optional<MemoryBufferRef> buffer = readFile(fileName); 1483 if (buffer) 1484 inputFiles.insert(make<OpaqueFile>(*buffer, segName, sectName)); 1485 } 1486 1487 for (const Arg *arg : args.filtered(OPT_add_empty_section)) { 1488 StringRef segName = arg->getValue(0); 1489 StringRef sectName = arg->getValue(1); 1490 inputFiles.insert(make<OpaqueFile>(MemoryBufferRef(), segName, sectName)); 1491 } 1492 1493 gatherInputSections(); 1494 if (config->callGraphProfileSort) 1495 extractCallGraphProfile(); 1496 1497 if (config->deadStrip) 1498 markLive(); 1499 1500 // ICF assumes that all literals have been folded already, so we must run 1501 // foldIdenticalLiterals before foldIdenticalSections. 1502 foldIdenticalLiterals(); 1503 if (config->icfLevel != ICFLevel::none) 1504 foldIdenticalSections(); 1505 1506 // Write to an output file. 1507 if (target->wordSize == 8) 1508 writeResult<LP64>(); 1509 else 1510 writeResult<ILP32>(); 1511 1512 depTracker->write(getLLDVersion(), inputFiles, config->outputFile); 1513 } 1514 1515 if (config->timeTraceEnabled) { 1516 checkError(timeTraceProfilerWrite( 1517 args.getLastArgValue(OPT_time_trace_file_eq).str(), 1518 config->outputFile)); 1519 1520 timeTraceProfilerCleanup(); 1521 } 1522 return errorCount() == 0; 1523 } 1524