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