| 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/CommonLinkerContext.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/CGData/CodeGenDataWriter.h" |
| 39 | #include "llvm/Config/llvm-config.h" |
| 40 | #include "llvm/LTO/LTO.h" |
| 41 | #include "llvm/Object/Archive.h" |
| 42 | #include "llvm/Option/ArgList.h" |
| 43 | #include "llvm/Support/CommandLine.h" |
| 44 | #include "llvm/Support/FileSystem.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/TargetParser/Host.h" |
| 51 | #include "llvm/TextAPI/Architecture.h" |
| 52 | #include "llvm/TextAPI/PackedVersion.h" |
| 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 | std::unique_ptr<Configuration> macho::config; |
| 63 | std::unique_ptr<DependencyTracker> macho::depTracker; |
| 64 | |
| 65 | static HeaderFileType getOutputType(const InputArgList &args) { |
| 66 | // TODO: -r, -dylinker, -preload... |
| 67 | Arg *outputArg = args.getLastArg(Ids: OPT_bundle, Ids: OPT_dylib, Ids: 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 std::optional<StringRef> findLibrary(StringRef name) { |
| 85 | CachedHashStringRef key(name); |
| 86 | auto entry = resolvedLibraries.find(Val: key); |
| 87 | if (entry != resolvedLibraries.end()) |
| 88 | return entry->second; |
| 89 | |
| 90 | auto doFind = [&] { |
| 91 | // Special case for Csu support files required for Mac OS X 10.7 and older |
| 92 | // (crt1.o) |
| 93 | if (name.ends_with(Suffix: ".o" )) |
| 94 | return findPathCombination(name, roots: config->librarySearchPaths, extensions: {"" }); |
| 95 | if (config->searchDylibsFirst) { |
| 96 | if (std::optional<StringRef> path = |
| 97 | findPathCombination(name: "lib" + name, roots: config->librarySearchPaths, |
| 98 | extensions: {".tbd" , ".dylib" , ".so" })) |
| 99 | return path; |
| 100 | return findPathCombination(name: "lib" + name, roots: config->librarySearchPaths, |
| 101 | extensions: {".a" }); |
| 102 | } |
| 103 | return findPathCombination(name: "lib" + name, roots: config->librarySearchPaths, |
| 104 | extensions: {".tbd" , ".dylib" , ".so" , ".a" }); |
| 105 | }; |
| 106 | |
| 107 | std::optional<StringRef> path = doFind(); |
| 108 | if (path) |
| 109 | resolvedLibraries[key] = *path; |
| 110 | |
| 111 | return path; |
| 112 | } |
| 113 | |
| 114 | static DenseMap<CachedHashStringRef, StringRef> resolvedFrameworks; |
| 115 | static std::optional<StringRef> findFramework(StringRef name) { |
| 116 | CachedHashStringRef key(name); |
| 117 | auto entry = resolvedFrameworks.find(Val: key); |
| 118 | if (entry != resolvedFrameworks.end()) |
| 119 | return entry->second; |
| 120 | |
| 121 | SmallString<260> symlink; |
| 122 | StringRef suffix; |
| 123 | std::tie(args&: name, args&: suffix) = name.split(Separator: "," ); |
| 124 | for (StringRef dir : config->frameworkSearchPaths) { |
| 125 | symlink = dir; |
| 126 | path::append(path&: symlink, a: name + ".framework" , b: name); |
| 127 | |
| 128 | if (!suffix.empty()) { |
| 129 | // NOTE: we must resolve the symlink before trying the suffixes, because |
| 130 | // there are no symlinks for the suffixed paths. |
| 131 | SmallString<260> location; |
| 132 | if (!fs::real_path(path: symlink, output&: location)) { |
| 133 | // only append suffix if realpath() succeeds |
| 134 | Twine suffixed = location + suffix; |
| 135 | if (fs::exists(Path: suffixed)) |
| 136 | return resolvedFrameworks[key] = saver().save(S: suffixed.str()); |
| 137 | } |
| 138 | // Suffix lookup failed, fall through to the no-suffix case. |
| 139 | } |
| 140 | |
| 141 | if (std::optional<StringRef> path = resolveDylibPath(path: symlink.str())) |
| 142 | return resolvedFrameworks[key] = *path; |
| 143 | } |
| 144 | return {}; |
| 145 | } |
| 146 | |
| 147 | static bool warnIfNotDirectory(StringRef option, StringRef path) { |
| 148 | if (!fs::exists(Path: path)) { |
| 149 | warn(msg: "directory not found for option -" + option + path); |
| 150 | return false; |
| 151 | } else if (!fs::is_directory(Path: path)) { |
| 152 | warn(msg: "option -" + option + path + " references a non-directory path" ); |
| 153 | return false; |
| 154 | } |
| 155 | return true; |
| 156 | } |
| 157 | |
| 158 | static std::vector<StringRef> |
| 159 | getSearchPaths(unsigned optionCode, InputArgList &args, |
| 160 | const std::vector<StringRef> &roots, |
| 161 | const SmallVector<StringRef, 2> &systemPaths) { |
| 162 | std::vector<StringRef> paths; |
| 163 | StringRef optionLetter{optionCode == OPT_F ? "F" : "L" }; |
| 164 | for (StringRef path : args::getStrings(args, id: optionCode)) { |
| 165 | // NOTE: only absolute paths are re-rooted to syslibroot(s) |
| 166 | bool found = false; |
| 167 | if (path::is_absolute(path, style: path::Style::posix)) { |
| 168 | for (StringRef root : roots) { |
| 169 | SmallString<261> buffer(root); |
| 170 | path::append(path&: buffer, a: path); |
| 171 | // Do not warn about paths that are computed via the syslib roots |
| 172 | if (fs::is_directory(Path: buffer)) { |
| 173 | paths.push_back(x: saver().save(S: buffer.str())); |
| 174 | found = true; |
| 175 | } |
| 176 | } |
| 177 | } |
| 178 | if (!found && warnIfNotDirectory(option: optionLetter, path)) |
| 179 | paths.push_back(x: path); |
| 180 | } |
| 181 | |
| 182 | // `-Z` suppresses the standard "system" search paths. |
| 183 | if (args.hasArg(Ids: OPT_Z)) |
| 184 | return paths; |
| 185 | |
| 186 | for (const StringRef &path : systemPaths) { |
| 187 | for (const StringRef &root : roots) { |
| 188 | SmallString<261> buffer(root); |
| 189 | path::append(path&: buffer, a: path); |
| 190 | if (fs::is_directory(Path: buffer)) |
| 191 | paths.push_back(x: saver().save(S: buffer.str())); |
| 192 | } |
| 193 | } |
| 194 | return paths; |
| 195 | } |
| 196 | |
| 197 | static std::vector<StringRef> getSystemLibraryRoots(InputArgList &args) { |
| 198 | std::vector<StringRef> roots; |
| 199 | for (const Arg *arg : args.filtered(Ids: OPT_syslibroot)) |
| 200 | roots.push_back(x: arg->getValue()); |
| 201 | // NOTE: the final `-syslibroot` being `/` will ignore all roots |
| 202 | if (!roots.empty() && roots.back() == "/" ) |
| 203 | roots.clear(); |
| 204 | // NOTE: roots can never be empty - add an empty root to simplify the library |
| 205 | // and framework search path computation. |
| 206 | if (roots.empty()) |
| 207 | roots.emplace_back(args: "" ); |
| 208 | return roots; |
| 209 | } |
| 210 | |
| 211 | static std::vector<StringRef> |
| 212 | getLibrarySearchPaths(InputArgList &args, const std::vector<StringRef> &roots) { |
| 213 | return getSearchPaths(optionCode: OPT_L, args, roots, systemPaths: {"/usr/lib" , "/usr/local/lib" }); |
| 214 | } |
| 215 | |
| 216 | static std::vector<StringRef> |
| 217 | getFrameworkSearchPaths(InputArgList &args, |
| 218 | const std::vector<StringRef> &roots) { |
| 219 | return getSearchPaths(optionCode: OPT_F, args, roots, |
| 220 | systemPaths: {"/Library/Frameworks" , "/System/Library/Frameworks" }); |
| 221 | } |
| 222 | |
| 223 | static llvm::CachePruningPolicy getLTOCachePolicy(InputArgList &args) { |
| 224 | SmallString<128> ltoPolicy; |
| 225 | auto add = [<oPolicy](Twine val) { |
| 226 | if (!ltoPolicy.empty()) |
| 227 | ltoPolicy += ":" ; |
| 228 | val.toVector(Out&: ltoPolicy); |
| 229 | }; |
| 230 | for (const Arg *arg : |
| 231 | args.filtered(Ids: OPT_thinlto_cache_policy_eq, Ids: OPT_prune_interval_lto, |
| 232 | Ids: OPT_prune_after_lto, Ids: OPT_max_relative_cache_size_lto)) { |
| 233 | switch (arg->getOption().getID()) { |
| 234 | case OPT_thinlto_cache_policy_eq: |
| 235 | add(arg->getValue()); |
| 236 | break; |
| 237 | case OPT_prune_interval_lto: |
| 238 | if (!strcmp(s1: "-1" , s2: arg->getValue())) |
| 239 | add("prune_interval=87600h" ); // 10 years |
| 240 | else |
| 241 | add(Twine("prune_interval=" ) + arg->getValue() + "s" ); |
| 242 | break; |
| 243 | case OPT_prune_after_lto: |
| 244 | add(Twine("prune_after=" ) + arg->getValue() + "s" ); |
| 245 | break; |
| 246 | case OPT_max_relative_cache_size_lto: |
| 247 | add(Twine("cache_size=" ) + arg->getValue() + "%" ); |
| 248 | break; |
| 249 | } |
| 250 | } |
| 251 | return CHECK(parseCachePruningPolicy(ltoPolicy), "invalid LTO cache policy" ); |
| 252 | } |
| 253 | |
| 254 | // What caused a given library to be loaded. Only relevant for archives. |
| 255 | // Note that this does not tell us *how* we should load the library, i.e. |
| 256 | // whether we should do it lazily or eagerly (AKA force loading). The "how" is |
| 257 | // decided within addFile(). |
| 258 | enum class LoadType { |
| 259 | CommandLine, // Library was passed as a regular CLI argument |
| 260 | CommandLineForce, // Library was passed via `-force_load` |
| 261 | LCLinkerOption, // Library was passed via LC_LINKER_OPTIONS |
| 262 | }; |
| 263 | |
| 264 | struct ArchiveFileInfo { |
| 265 | ArchiveFile *file; |
| 266 | bool isCommandLineLoad; |
| 267 | }; |
| 268 | |
| 269 | static DenseMap<StringRef, ArchiveFileInfo> loadedArchives; |
| 270 | |
| 271 | static void saveThinArchiveToRepro(ArchiveFile const *file) { |
| 272 | assert(tar && file->getArchive().isThin()); |
| 273 | |
| 274 | Error e = Error::success(); |
| 275 | for (const object::Archive::Child &c : file->getArchive().children(Err&: e)) { |
| 276 | MemoryBufferRef mb = CHECK(c.getMemoryBufferRef(), |
| 277 | toString(file) + ": failed to get buffer" ); |
| 278 | tar->append(Path: relativeToRoot(CHECK(c.getFullName(), file)), Data: mb.getBuffer()); |
| 279 | } |
| 280 | if (e) |
| 281 | error(msg: toString(file) + |
| 282 | ": Archive::children failed: " + toString(E: std::move(e))); |
| 283 | } |
| 284 | |
| 285 | static InputFile *addFile(StringRef path, LoadType loadType, |
| 286 | bool isLazy = false, bool isExplicit = true, |
| 287 | bool isBundleLoader = false, |
| 288 | bool isForceHidden = false) { |
| 289 | std::optional<MemoryBufferRef> buffer = readFile(path); |
| 290 | if (!buffer) |
| 291 | return nullptr; |
| 292 | MemoryBufferRef mbref = *buffer; |
| 293 | InputFile *newFile = nullptr; |
| 294 | |
| 295 | file_magic magic = identify_magic(magic: mbref.getBuffer()); |
| 296 | switch (magic) { |
| 297 | case file_magic::archive: { |
| 298 | bool isCommandLineLoad = loadType != LoadType::LCLinkerOption; |
| 299 | // Avoid loading archives twice. If the archives are being force-loaded, |
| 300 | // loading them twice would create duplicate symbol errors. In the |
| 301 | // non-force-loading case, this is just a minor performance optimization. |
| 302 | // We don't take a reference to cachedFile here because the |
| 303 | // loadArchiveMember() call below may recursively call addFile() and |
| 304 | // invalidate this reference. |
| 305 | auto entry = loadedArchives.find(Val: path); |
| 306 | |
| 307 | ArchiveFile *file; |
| 308 | if (entry == loadedArchives.end()) { |
| 309 | // No cached archive, we need to create a new one |
| 310 | std::unique_ptr<object::Archive> archive = CHECK( |
| 311 | object::Archive::create(mbref), path + ": failed to parse archive" ); |
| 312 | |
| 313 | file = make<ArchiveFile>(args: std::move(archive), args&: isForceHidden); |
| 314 | |
| 315 | if (tar && file->getArchive().isThin()) |
| 316 | saveThinArchiveToRepro(file); |
| 317 | } else { |
| 318 | file = entry->second.file; |
| 319 | // Command-line loads take precedence. If file is previously loaded via |
| 320 | // command line, or is loaded via LC_LINKER_OPTION and being loaded via |
| 321 | // LC_LINKER_OPTION again, using the cached archive is enough. |
| 322 | if (entry->second.isCommandLineLoad || !isCommandLineLoad) |
| 323 | return file; |
| 324 | } |
| 325 | |
| 326 | bool isLCLinkerForceLoad = loadType == LoadType::LCLinkerOption && |
| 327 | config->forceLoadSwift && |
| 328 | path::filename(path).starts_with(Prefix: "libswift" ); |
| 329 | if ((isCommandLineLoad && config->allLoad) || |
| 330 | loadType == LoadType::CommandLineForce || isLCLinkerForceLoad) { |
| 331 | if (readFile(path)) { |
| 332 | Error e = Error::success(); |
| 333 | for (const object::Archive::Child &c : file->getArchive().children(Err&: e)) { |
| 334 | StringRef reason; |
| 335 | switch (loadType) { |
| 336 | case LoadType::LCLinkerOption: |
| 337 | reason = "LC_LINKER_OPTION" ; |
| 338 | break; |
| 339 | case LoadType::CommandLineForce: |
| 340 | reason = "-force_load" ; |
| 341 | break; |
| 342 | case LoadType::CommandLine: |
| 343 | reason = "-all_load" ; |
| 344 | break; |
| 345 | } |
| 346 | if (Error e = file->fetch(c, reason)) { |
| 347 | if (config->warnThinArchiveMissingMembers) |
| 348 | warn(msg: toString(file) + ": " + reason + |
| 349 | " failed to load archive member: " + toString(E: std::move(e))); |
| 350 | else |
| 351 | llvm::consumeError(Err: std::move(e)); |
| 352 | } |
| 353 | } |
| 354 | if (e) |
| 355 | error(msg: toString(file) + |
| 356 | ": Archive::children failed: " + toString(E: std::move(e))); |
| 357 | } |
| 358 | } else if (isCommandLineLoad && config->forceLoadObjC) { |
| 359 | if (file->getArchive().hasSymbolTable()) { |
| 360 | for (const object::Archive::Symbol &sym : file->getArchive().symbols()) |
| 361 | if (sym.getName().starts_with(Prefix: objc::symbol_names::klass)) |
| 362 | file->fetch(sym); |
| 363 | } |
| 364 | |
| 365 | // TODO: no need to look for ObjC sections for a given archive member if |
| 366 | // we already found that it contains an ObjC symbol. |
| 367 | if (readFile(path)) { |
| 368 | Error e = Error::success(); |
| 369 | for (const object::Archive::Child &c : file->getArchive().children(Err&: e)) { |
| 370 | Expected<MemoryBufferRef> mb = c.getMemoryBufferRef(); |
| 371 | if (!mb) { |
| 372 | // We used to create broken repro tarballs that only included those |
| 373 | // object files from thin archives that ended up being used. |
| 374 | if (config->warnThinArchiveMissingMembers) |
| 375 | warn(msg: toString(file) + ": -ObjC failed to open archive member: " + |
| 376 | toString(E: mb.takeError())); |
| 377 | else |
| 378 | llvm::consumeError(Err: mb.takeError()); |
| 379 | continue; |
| 380 | } |
| 381 | |
| 382 | if (!hasObjCSection(*mb)) |
| 383 | continue; |
| 384 | if (Error e = file->fetch(c, reason: "-ObjC" )) |
| 385 | error(msg: toString(file) + ": -ObjC failed to load archive member: " + |
| 386 | toString(E: std::move(e))); |
| 387 | } |
| 388 | if (e) |
| 389 | error(msg: toString(file) + |
| 390 | ": Archive::children failed: " + toString(E: std::move(e))); |
| 391 | } |
| 392 | } |
| 393 | file->addLazySymbols(); |
| 394 | loadedArchives[path] = ArchiveFileInfo{.file: file, .isCommandLineLoad: isCommandLineLoad}; |
| 395 | newFile = file; |
| 396 | break; |
| 397 | } |
| 398 | case file_magic::macho_object: |
| 399 | newFile = make<ObjFile>(args&: mbref, args: getModTime(path), args: "" , args&: isLazy); |
| 400 | break; |
| 401 | case file_magic::macho_dynamically_linked_shared_lib: |
| 402 | case file_magic::macho_dynamically_linked_shared_lib_stub: |
| 403 | case file_magic::tapi_file: |
| 404 | if (DylibFile *dylibFile = |
| 405 | loadDylib(mbref, umbrella: nullptr, /*isBundleLoader=*/false, explicitlyLinked: isExplicit)) |
| 406 | newFile = dylibFile; |
| 407 | break; |
| 408 | case file_magic::bitcode: |
| 409 | newFile = make<BitcodeFile>(args&: mbref, args: "" , args: 0, args&: isLazy); |
| 410 | break; |
| 411 | case file_magic::macho_executable: |
| 412 | case file_magic::macho_bundle: |
| 413 | // We only allow executable and bundle type here if it is used |
| 414 | // as a bundle loader. |
| 415 | if (!isBundleLoader) |
| 416 | error(msg: path + ": unhandled file type" ); |
| 417 | if (DylibFile *dylibFile = loadDylib(mbref, umbrella: nullptr, isBundleLoader)) |
| 418 | newFile = dylibFile; |
| 419 | break; |
| 420 | default: |
| 421 | error(msg: path + ": unhandled file type" ); |
| 422 | } |
| 423 | if (newFile && !isa<DylibFile>(Val: newFile)) { |
| 424 | if ((isa<ObjFile>(Val: newFile) || isa<BitcodeFile>(Val: newFile)) && newFile->lazy && |
| 425 | config->forceLoadObjC) { |
| 426 | for (Symbol *sym : newFile->symbols) |
| 427 | if (sym && sym->getName().starts_with(Prefix: objc::symbol_names::klass)) { |
| 428 | extract(file&: *newFile, reason: "-ObjC" ); |
| 429 | break; |
| 430 | } |
| 431 | if (newFile->lazy && hasObjCSection(mbref)) |
| 432 | extract(file&: *newFile, reason: "-ObjC" ); |
| 433 | } |
| 434 | |
| 435 | // printArchiveMemberLoad() prints both .a and .o names, so no need to |
| 436 | // print the .a name here. Similarly skip lazy files. |
| 437 | if (config->printEachFile && magic != file_magic::archive && !isLazy) |
| 438 | message(msg: toString(file: newFile)); |
| 439 | inputFiles.insert(X: newFile); |
| 440 | } |
| 441 | return newFile; |
| 442 | } |
| 443 | |
| 444 | static std::vector<StringRef> missingAutolinkWarnings; |
| 445 | static void addLibrary(StringRef name, bool isNeeded, bool isWeak, |
| 446 | bool isReexport, bool isHidden, bool isExplicit, |
| 447 | LoadType loadType) { |
| 448 | if (std::optional<StringRef> path = findLibrary(name)) { |
| 449 | if (auto *dylibFile = dyn_cast_or_null<DylibFile>( |
| 450 | Val: addFile(path: *path, loadType, /*isLazy=*/false, isExplicit, |
| 451 | /*isBundleLoader=*/false, isForceHidden: isHidden))) { |
| 452 | if (isNeeded) |
| 453 | dylibFile->forceNeeded = true; |
| 454 | if (isWeak) |
| 455 | dylibFile->forceWeakImport = true; |
| 456 | if (isReexport) { |
| 457 | config->hasReexports = true; |
| 458 | dylibFile->reexport = true; |
| 459 | } |
| 460 | } |
| 461 | return; |
| 462 | } |
| 463 | if (loadType == LoadType::LCLinkerOption) { |
| 464 | missingAutolinkWarnings.push_back( |
| 465 | x: saver().save(S: "auto-linked library not found for -l" + name)); |
| 466 | return; |
| 467 | } |
| 468 | error(msg: "library not found for -l" + name); |
| 469 | } |
| 470 | |
| 471 | static DenseSet<StringRef> loadedObjectFrameworks; |
| 472 | static void addFramework(StringRef name, bool isNeeded, bool isWeak, |
| 473 | bool isReexport, bool isExplicit, LoadType loadType) { |
| 474 | if (std::optional<StringRef> path = findFramework(name)) { |
| 475 | if (loadedObjectFrameworks.contains(V: *path)) |
| 476 | return; |
| 477 | |
| 478 | InputFile *file = |
| 479 | addFile(path: *path, loadType, /*isLazy=*/false, isExplicit, isBundleLoader: false); |
| 480 | if (auto *dylibFile = dyn_cast_or_null<DylibFile>(Val: file)) { |
| 481 | if (isNeeded) |
| 482 | dylibFile->forceNeeded = true; |
| 483 | if (isWeak) |
| 484 | dylibFile->forceWeakImport = true; |
| 485 | if (isReexport) { |
| 486 | config->hasReexports = true; |
| 487 | dylibFile->reexport = true; |
| 488 | } |
| 489 | } else if (isa_and_nonnull<ObjFile>(Val: file) || |
| 490 | isa_and_nonnull<BitcodeFile>(Val: file)) { |
| 491 | // Cache frameworks containing object or bitcode files to avoid duplicate |
| 492 | // symbols. Frameworks containing static archives are cached separately |
| 493 | // in addFile() to share caching with libraries, and frameworks |
| 494 | // containing dylibs should allow overwriting of attributes such as |
| 495 | // forceNeeded by subsequent loads |
| 496 | loadedObjectFrameworks.insert(V: *path); |
| 497 | } |
| 498 | return; |
| 499 | } |
| 500 | if (loadType == LoadType::LCLinkerOption) { |
| 501 | missingAutolinkWarnings.push_back( |
| 502 | x: saver().save(S: "auto-linked framework not found for -framework " + name)); |
| 503 | return; |
| 504 | } |
| 505 | error(msg: "framework not found for -framework " + name); |
| 506 | } |
| 507 | |
| 508 | // Parses LC_LINKER_OPTION contents, which can add additional command line |
| 509 | // flags. This directly parses the flags instead of using the standard argument |
| 510 | // parser to improve performance. |
| 511 | void macho::parseLCLinkerOption( |
| 512 | llvm::SmallVectorImpl<StringRef> &LCLinkerOptions, InputFile *f, |
| 513 | unsigned argc, StringRef data) { |
| 514 | if (config->ignoreAutoLink) |
| 515 | return; |
| 516 | |
| 517 | SmallVector<StringRef, 4> argv; |
| 518 | size_t offset = 0; |
| 519 | for (unsigned i = 0; i < argc && offset < data.size(); ++i) { |
| 520 | argv.push_back(Elt: data.data() + offset); |
| 521 | offset += strlen(s: data.data() + offset) + 1; |
| 522 | } |
| 523 | if (argv.size() != argc || offset > data.size()) |
| 524 | fatal(msg: toString(file: f) + ": invalid LC_LINKER_OPTION" ); |
| 525 | |
| 526 | unsigned i = 0; |
| 527 | StringRef arg = argv[i]; |
| 528 | if (arg.consume_front(Prefix: "-l" )) { |
| 529 | if (config->ignoreAutoLinkOptions.contains(key: arg)) |
| 530 | return; |
| 531 | } else if (arg == "-framework" ) { |
| 532 | StringRef name = argv[++i]; |
| 533 | if (config->ignoreAutoLinkOptions.contains(key: name)) |
| 534 | return; |
| 535 | } else { |
| 536 | error(msg: arg + " is not allowed in LC_LINKER_OPTION" ); |
| 537 | } |
| 538 | |
| 539 | LCLinkerOptions.append(RHS: argv); |
| 540 | } |
| 541 | |
| 542 | void macho::resolveLCLinkerOptions() { |
| 543 | while (!unprocessedLCLinkerOptions.empty()) { |
| 544 | SmallVector<StringRef> LCLinkerOptions(unprocessedLCLinkerOptions); |
| 545 | unprocessedLCLinkerOptions.clear(); |
| 546 | |
| 547 | for (unsigned i = 0; i < LCLinkerOptions.size(); ++i) { |
| 548 | StringRef arg = LCLinkerOptions[i]; |
| 549 | if (arg.consume_front(Prefix: "-l" )) { |
| 550 | assert(!config->ignoreAutoLinkOptions.contains(arg)); |
| 551 | addLibrary(name: arg, /*isNeeded=*/false, /*isWeak=*/false, |
| 552 | /*isReexport=*/false, /*isHidden=*/false, |
| 553 | /*isExplicit=*/false, loadType: LoadType::LCLinkerOption); |
| 554 | } else if (arg == "-framework" ) { |
| 555 | StringRef name = LCLinkerOptions[++i]; |
| 556 | assert(!config->ignoreAutoLinkOptions.contains(name)); |
| 557 | addFramework(name, /*isNeeded=*/false, /*isWeak=*/false, |
| 558 | /*isReexport=*/false, /*isExplicit=*/false, |
| 559 | loadType: LoadType::LCLinkerOption); |
| 560 | } else { |
| 561 | error(msg: arg + " is not allowed in LC_LINKER_OPTION" ); |
| 562 | } |
| 563 | } |
| 564 | } |
| 565 | } |
| 566 | |
| 567 | static void addFileList(StringRef path, bool isLazy) { |
| 568 | std::optional<MemoryBufferRef> buffer = readFile(path); |
| 569 | if (!buffer) |
| 570 | return; |
| 571 | MemoryBufferRef mbref = *buffer; |
| 572 | for (StringRef path : args::getLines(mb: mbref)) |
| 573 | addFile(path: rerootPath(path), loadType: LoadType::CommandLine, isLazy); |
| 574 | } |
| 575 | |
| 576 | // We expect sub-library names of the form "libfoo", which will match a dylib |
| 577 | // with a path of .*/libfoo.{dylib, tbd}. |
| 578 | // XXX ld64 seems to ignore the extension entirely when matching sub-libraries; |
| 579 | // I'm not sure what the use case for that is. |
| 580 | static bool markReexport(StringRef searchName, ArrayRef<StringRef> extensions) { |
| 581 | for (InputFile *file : inputFiles) { |
| 582 | if (auto *dylibFile = dyn_cast<DylibFile>(Val: file)) { |
| 583 | StringRef filename = path::filename(path: dylibFile->getName()); |
| 584 | if (filename.consume_front(Prefix: searchName) && |
| 585 | (filename.empty() || llvm::is_contained(Range&: extensions, Element: filename))) { |
| 586 | dylibFile->reexport = true; |
| 587 | return true; |
| 588 | } |
| 589 | } |
| 590 | } |
| 591 | return false; |
| 592 | } |
| 593 | |
| 594 | // This function is called on startup. We need this for LTO since |
| 595 | // LTO calls LLVM functions to compile bitcode files to native code. |
| 596 | // Technically this can be delayed until we read bitcode files, but |
| 597 | // we don't bother to do lazily because the initialization is fast. |
| 598 | static void initLLVM() { |
| 599 | InitializeAllTargets(); |
| 600 | InitializeAllTargetMCs(); |
| 601 | InitializeAllAsmPrinters(); |
| 602 | InitializeAllAsmParsers(); |
| 603 | } |
| 604 | |
| 605 | static bool compileBitcodeFiles() { |
| 606 | TimeTraceScope timeScope("LTO" ); |
| 607 | auto *lto = make<BitcodeCompiler>(); |
| 608 | for (InputFile *file : inputFiles) |
| 609 | if (auto *bitcodeFile = dyn_cast<BitcodeFile>(Val: file)) |
| 610 | if (!file->lazy) |
| 611 | lto->add(f&: *bitcodeFile); |
| 612 | |
| 613 | std::vector<ObjFile *> compiled = lto->compile(); |
| 614 | inputFiles.insert_range(R&: compiled); |
| 615 | |
| 616 | return !compiled.empty(); |
| 617 | } |
| 618 | |
| 619 | // Replaces common symbols with defined symbols residing in __common sections. |
| 620 | // This function must be called after all symbol names are resolved (i.e. after |
| 621 | // all InputFiles have been loaded.) As a result, later operations won't see |
| 622 | // any CommonSymbols. |
| 623 | static void replaceCommonSymbols() { |
| 624 | TimeTraceScope timeScope("Replace common symbols" ); |
| 625 | ConcatOutputSection *osec = nullptr; |
| 626 | for (Symbol *sym : symtab->getSymbols()) { |
| 627 | auto *common = dyn_cast<CommonSymbol>(Val: sym); |
| 628 | if (common == nullptr) |
| 629 | continue; |
| 630 | |
| 631 | // Casting to size_t will truncate large values on 32-bit architectures, |
| 632 | // but it's not really worth supporting the linking of 64-bit programs on |
| 633 | // 32-bit archs. |
| 634 | ArrayRef<uint8_t> data = {nullptr, static_cast<size_t>(common->size)}; |
| 635 | // FIXME avoid creating one Section per symbol? |
| 636 | auto *section = |
| 637 | make<Section>(args: common->getFile(), args: segment_names::data, |
| 638 | args: section_names::common, args: S_ZEROFILL, /*addr=*/args: 0); |
| 639 | auto *isec = make<ConcatInputSection>(args&: *section, args&: data, args: common->align); |
| 640 | if (!osec) |
| 641 | osec = ConcatOutputSection::getOrCreateForInput(isec); |
| 642 | isec->parent = osec; |
| 643 | addInputSection(inputSection: isec); |
| 644 | |
| 645 | // FIXME: CommonSymbol should store isReferencedDynamically, noDeadStrip |
| 646 | // and pass them on here. |
| 647 | replaceSymbol<Defined>( |
| 648 | s: sym, arg: sym->getName(), arg: common->getFile(), arg&: isec, /*value=*/arg: 0, arg: common->size, |
| 649 | /*isWeakDef=*/arg: false, /*isExternal=*/arg: true, arg: common->privateExtern, |
| 650 | /*includeInSymtab=*/arg: true, /*isReferencedDynamically=*/arg: false, |
| 651 | /*noDeadStrip=*/arg: false); |
| 652 | } |
| 653 | } |
| 654 | |
| 655 | static void initializeSectionRenameMap() { |
| 656 | if (config->dataConst) { |
| 657 | SmallVector<StringRef> v{section_names::got, |
| 658 | section_names::authGot, |
| 659 | section_names::authPtr, |
| 660 | section_names::nonLazySymbolPtr, |
| 661 | section_names::const_, |
| 662 | section_names::cfString, |
| 663 | section_names::moduleInitFunc, |
| 664 | section_names::moduleTermFunc, |
| 665 | section_names::objcClassList, |
| 666 | section_names::objcNonLazyClassList, |
| 667 | section_names::objcCatList, |
| 668 | section_names::objcNonLazyCatList, |
| 669 | section_names::objcProtoList, |
| 670 | section_names::objCImageInfo}; |
| 671 | for (StringRef s : v) |
| 672 | config->sectionRenameMap[{segment_names::data, s}] = { |
| 673 | segment_names::dataConst, s}; |
| 674 | } |
| 675 | config->sectionRenameMap[{segment_names::text, section_names::staticInit}] = { |
| 676 | segment_names::text, section_names::text}; |
| 677 | config->sectionRenameMap[{segment_names::import, section_names::pointers}] = { |
| 678 | config->dataConst ? segment_names::dataConst : segment_names::data, |
| 679 | section_names::nonLazySymbolPtr}; |
| 680 | } |
| 681 | |
| 682 | static inline char toLowerDash(char x) { |
| 683 | if (x >= 'A' && x <= 'Z') |
| 684 | return x - 'A' + 'a'; |
| 685 | else if (x == ' ') |
| 686 | return '-'; |
| 687 | return x; |
| 688 | } |
| 689 | |
| 690 | static std::string lowerDash(StringRef s) { |
| 691 | return std::string(map_iterator(I: s.begin(), F: toLowerDash), |
| 692 | map_iterator(I: s.end(), F: toLowerDash)); |
| 693 | } |
| 694 | |
| 695 | struct PlatformVersion { |
| 696 | PlatformType platform = PLATFORM_UNKNOWN; |
| 697 | llvm::VersionTuple minimum; |
| 698 | llvm::VersionTuple sdk; |
| 699 | }; |
| 700 | |
| 701 | static PlatformVersion parsePlatformVersion(const Arg *arg) { |
| 702 | assert(arg->getOption().getID() == OPT_platform_version); |
| 703 | StringRef platformStr = arg->getValue(N: 0); |
| 704 | StringRef minVersionStr = arg->getValue(N: 1); |
| 705 | StringRef sdkVersionStr = arg->getValue(N: 2); |
| 706 | |
| 707 | PlatformVersion platformVersion; |
| 708 | |
| 709 | // TODO(compnerd) see if we can generate this case list via XMACROS |
| 710 | platformVersion.platform = |
| 711 | StringSwitch<PlatformType>(lowerDash(s: platformStr)) |
| 712 | .Cases(S0: "macos" , S1: "1" , Value: PLATFORM_MACOS) |
| 713 | .Cases(S0: "ios" , S1: "2" , Value: PLATFORM_IOS) |
| 714 | .Cases(S0: "tvos" , S1: "3" , Value: PLATFORM_TVOS) |
| 715 | .Cases(S0: "watchos" , S1: "4" , Value: PLATFORM_WATCHOS) |
| 716 | .Cases(S0: "bridgeos" , S1: "5" , Value: PLATFORM_BRIDGEOS) |
| 717 | .Cases(S0: "mac-catalyst" , S1: "6" , Value: PLATFORM_MACCATALYST) |
| 718 | .Cases(S0: "ios-simulator" , S1: "7" , Value: PLATFORM_IOSSIMULATOR) |
| 719 | .Cases(S0: "tvos-simulator" , S1: "8" , Value: PLATFORM_TVOSSIMULATOR) |
| 720 | .Cases(S0: "watchos-simulator" , S1: "9" , Value: PLATFORM_WATCHOSSIMULATOR) |
| 721 | .Cases(S0: "driverkit" , S1: "10" , Value: PLATFORM_DRIVERKIT) |
| 722 | .Cases(S0: "xros" , S1: "11" , Value: PLATFORM_XROS) |
| 723 | .Cases(S0: "xros-simulator" , S1: "12" , Value: PLATFORM_XROS_SIMULATOR) |
| 724 | .Default(Value: PLATFORM_UNKNOWN); |
| 725 | if (platformVersion.platform == PLATFORM_UNKNOWN) |
| 726 | error(msg: Twine("malformed platform: " ) + platformStr); |
| 727 | // TODO: check validity of version strings, which varies by platform |
| 728 | // NOTE: ld64 accepts version strings with 5 components |
| 729 | // llvm::VersionTuple accepts no more than 4 components |
| 730 | // Has Apple ever published version strings with 5 components? |
| 731 | if (platformVersion.minimum.tryParse(string: minVersionStr)) |
| 732 | error(msg: Twine("malformed minimum version: " ) + minVersionStr); |
| 733 | if (platformVersion.sdk.tryParse(string: sdkVersionStr)) |
| 734 | error(msg: Twine("malformed sdk version: " ) + sdkVersionStr); |
| 735 | return platformVersion; |
| 736 | } |
| 737 | |
| 738 | // Has the side-effect of setting Config::platformInfo and |
| 739 | // potentially Config::secondaryPlatformInfo. |
| 740 | static void setPlatformVersions(StringRef archName, const ArgList &args) { |
| 741 | std::map<PlatformType, PlatformVersion> platformVersions; |
| 742 | const PlatformVersion *lastVersionInfo = nullptr; |
| 743 | for (const Arg *arg : args.filtered(Ids: OPT_platform_version)) { |
| 744 | PlatformVersion version = parsePlatformVersion(arg); |
| 745 | |
| 746 | // For each platform, the last flag wins: |
| 747 | // `-platform_version macos 2 3 -platform_version macos 4 5` has the same |
| 748 | // effect as just passing `-platform_version macos 4 5`. |
| 749 | // FIXME: ld64 warns on multiple flags for one platform. Should we? |
| 750 | platformVersions[version.platform] = version; |
| 751 | lastVersionInfo = &platformVersions[version.platform]; |
| 752 | } |
| 753 | |
| 754 | if (platformVersions.empty()) { |
| 755 | error(msg: "must specify -platform_version" ); |
| 756 | return; |
| 757 | } |
| 758 | if (platformVersions.size() > 2) { |
| 759 | error(msg: "must specify -platform_version at most twice" ); |
| 760 | return; |
| 761 | } |
| 762 | if (platformVersions.size() == 2) { |
| 763 | bool isZipperedCatalyst = platformVersions.count(x: PLATFORM_MACOS) && |
| 764 | platformVersions.count(x: PLATFORM_MACCATALYST); |
| 765 | |
| 766 | if (!isZipperedCatalyst) { |
| 767 | error(msg: "lld supports writing zippered outputs only for " |
| 768 | "macos and mac-catalyst" ); |
| 769 | } else if (config->outputType != MH_DYLIB && |
| 770 | config->outputType != MH_BUNDLE) { |
| 771 | error(msg: "writing zippered outputs only valid for -dylib and -bundle" ); |
| 772 | } |
| 773 | |
| 774 | config->platformInfo = { |
| 775 | .target: MachO::Target(getArchitectureFromName(Name: archName), PLATFORM_MACOS, |
| 776 | platformVersions[PLATFORM_MACOS].minimum), |
| 777 | .sdk: platformVersions[PLATFORM_MACOS].sdk}; |
| 778 | config->secondaryPlatformInfo = { |
| 779 | .target: MachO::Target(getArchitectureFromName(Name: archName), PLATFORM_MACCATALYST, |
| 780 | platformVersions[PLATFORM_MACCATALYST].minimum), |
| 781 | .sdk: platformVersions[PLATFORM_MACCATALYST].sdk}; |
| 782 | return; |
| 783 | } |
| 784 | |
| 785 | config->platformInfo = {.target: MachO::Target(getArchitectureFromName(Name: archName), |
| 786 | lastVersionInfo->platform, |
| 787 | lastVersionInfo->minimum), |
| 788 | .sdk: lastVersionInfo->sdk}; |
| 789 | } |
| 790 | |
| 791 | // Has the side-effect of setting Config::target. |
| 792 | static TargetInfo *createTargetInfo(InputArgList &args) { |
| 793 | StringRef archName = args.getLastArgValue(Id: OPT_arch); |
| 794 | if (archName.empty()) { |
| 795 | error(msg: "must specify -arch" ); |
| 796 | return nullptr; |
| 797 | } |
| 798 | |
| 799 | setPlatformVersions(archName, args); |
| 800 | auto [cpuType, cpuSubtype] = getCPUTypeFromArchitecture(Arch: config->arch()); |
| 801 | switch (cpuType) { |
| 802 | case CPU_TYPE_X86_64: |
| 803 | return createX86_64TargetInfo(); |
| 804 | case CPU_TYPE_ARM64: |
| 805 | return createARM64TargetInfo(); |
| 806 | case CPU_TYPE_ARM64_32: |
| 807 | return createARM64_32TargetInfo(); |
| 808 | default: |
| 809 | error(msg: "missing or unsupported -arch " + archName); |
| 810 | return nullptr; |
| 811 | } |
| 812 | } |
| 813 | |
| 814 | static UndefinedSymbolTreatment |
| 815 | getUndefinedSymbolTreatment(const ArgList &args) { |
| 816 | StringRef treatmentStr = args.getLastArgValue(Id: OPT_undefined); |
| 817 | auto treatment = |
| 818 | StringSwitch<UndefinedSymbolTreatment>(treatmentStr) |
| 819 | .Cases(S0: "error" , S1: "" , Value: UndefinedSymbolTreatment::error) |
| 820 | .Case(S: "warning" , Value: UndefinedSymbolTreatment::warning) |
| 821 | .Case(S: "suppress" , Value: UndefinedSymbolTreatment::suppress) |
| 822 | .Case(S: "dynamic_lookup" , Value: UndefinedSymbolTreatment::dynamic_lookup) |
| 823 | .Default(Value: UndefinedSymbolTreatment::unknown); |
| 824 | if (treatment == UndefinedSymbolTreatment::unknown) { |
| 825 | warn(msg: Twine("unknown -undefined TREATMENT '" ) + treatmentStr + |
| 826 | "', defaulting to 'error'" ); |
| 827 | treatment = UndefinedSymbolTreatment::error; |
| 828 | } else if (config->namespaceKind == NamespaceKind::twolevel && |
| 829 | (treatment == UndefinedSymbolTreatment::warning || |
| 830 | treatment == UndefinedSymbolTreatment::suppress)) { |
| 831 | if (treatment == UndefinedSymbolTreatment::warning) |
| 832 | fatal(msg: "'-undefined warning' only valid with '-flat_namespace'" ); |
| 833 | else |
| 834 | fatal(msg: "'-undefined suppress' only valid with '-flat_namespace'" ); |
| 835 | treatment = UndefinedSymbolTreatment::error; |
| 836 | } |
| 837 | return treatment; |
| 838 | } |
| 839 | |
| 840 | static ICFLevel getICFLevel(const ArgList &args) { |
| 841 | StringRef icfLevelStr = args.getLastArgValue(Id: OPT_icf_eq); |
| 842 | auto icfLevel = StringSwitch<ICFLevel>(icfLevelStr) |
| 843 | .Cases(S0: "none" , S1: "" , Value: ICFLevel::none) |
| 844 | .Case(S: "safe" , Value: ICFLevel::safe) |
| 845 | .Case(S: "safe_thunks" , Value: ICFLevel::safe_thunks) |
| 846 | .Case(S: "all" , Value: ICFLevel::all) |
| 847 | .Default(Value: ICFLevel::unknown); |
| 848 | |
| 849 | if ((icfLevel == ICFLevel::safe_thunks) && (config->arch() != AK_arm64)) { |
| 850 | error(msg: "--icf=safe_thunks is only supported on arm64 targets" ); |
| 851 | } |
| 852 | |
| 853 | if (icfLevel == ICFLevel::unknown) { |
| 854 | warn(msg: Twine("unknown --icf=OPTION `" ) + icfLevelStr + |
| 855 | "', defaulting to `none'" ); |
| 856 | icfLevel = ICFLevel::none; |
| 857 | } |
| 858 | return icfLevel; |
| 859 | } |
| 860 | |
| 861 | static ObjCStubsMode getObjCStubsMode(const ArgList &args) { |
| 862 | const Arg *arg = args.getLastArg(Ids: OPT_objc_stubs_fast, Ids: OPT_objc_stubs_small); |
| 863 | if (!arg) |
| 864 | return ObjCStubsMode::fast; |
| 865 | |
| 866 | if (arg->getOption().getID() == OPT_objc_stubs_small) { |
| 867 | if (is_contained(Set: {AK_arm64e, AK_arm64}, Element: config->arch())) |
| 868 | return ObjCStubsMode::small; |
| 869 | else |
| 870 | warn(msg: "-objc_stubs_small is not yet implemented, defaulting to " |
| 871 | "-objc_stubs_fast" ); |
| 872 | } |
| 873 | return ObjCStubsMode::fast; |
| 874 | } |
| 875 | |
| 876 | static void warnIfDeprecatedOption(const Option &opt) { |
| 877 | if (!opt.getGroup().isValid()) |
| 878 | return; |
| 879 | if (opt.getGroup().getID() == OPT_grp_deprecated) { |
| 880 | warn(msg: "Option `" + opt.getPrefixedName() + "' is deprecated in ld64:" ); |
| 881 | warn(msg: opt.getHelpText()); |
| 882 | } |
| 883 | } |
| 884 | |
| 885 | static void warnIfUnimplementedOption(const Option &opt) { |
| 886 | if (!opt.getGroup().isValid() || !opt.hasFlag(Val: DriverFlag::HelpHidden)) |
| 887 | return; |
| 888 | switch (opt.getGroup().getID()) { |
| 889 | case OPT_grp_deprecated: |
| 890 | // warn about deprecated options elsewhere |
| 891 | break; |
| 892 | case OPT_grp_undocumented: |
| 893 | warn(msg: "Option `" + opt.getPrefixedName() + |
| 894 | "' is undocumented. Should lld implement it?" ); |
| 895 | break; |
| 896 | case OPT_grp_obsolete: |
| 897 | warn(msg: "Option `" + opt.getPrefixedName() + |
| 898 | "' is obsolete. Please modernize your usage." ); |
| 899 | break; |
| 900 | case OPT_grp_ignored: |
| 901 | warn(msg: "Option `" + opt.getPrefixedName() + "' is ignored." ); |
| 902 | break; |
| 903 | case OPT_grp_ignored_silently: |
| 904 | break; |
| 905 | default: |
| 906 | warn(msg: "Option `" + opt.getPrefixedName() + |
| 907 | "' is not yet implemented. Stay tuned..." ); |
| 908 | break; |
| 909 | } |
| 910 | } |
| 911 | |
| 912 | static const char *getReproduceOption(InputArgList &args) { |
| 913 | if (const Arg *arg = args.getLastArg(Ids: OPT_reproduce)) |
| 914 | return arg->getValue(); |
| 915 | return getenv(name: "LLD_REPRODUCE" ); |
| 916 | } |
| 917 | |
| 918 | // Parse options of the form "old;new". |
| 919 | static std::pair<StringRef, StringRef> getOldNewOptions(opt::InputArgList &args, |
| 920 | unsigned id) { |
| 921 | auto *arg = args.getLastArg(Ids: id); |
| 922 | if (!arg) |
| 923 | return {"" , "" }; |
| 924 | |
| 925 | StringRef s = arg->getValue(); |
| 926 | std::pair<StringRef, StringRef> ret = s.split(Separator: ';'); |
| 927 | if (ret.second.empty()) |
| 928 | error(msg: arg->getSpelling() + " expects 'old;new' format, but got " + s); |
| 929 | return ret; |
| 930 | } |
| 931 | |
| 932 | // Parse options of the form "old;new[;extra]". |
| 933 | static std::tuple<StringRef, StringRef, StringRef> |
| 934 | (opt::InputArgList &args, unsigned id) { |
| 935 | auto [oldDir, second] = getOldNewOptions(args, id); |
| 936 | auto [newDir, extraDir] = second.split(Separator: ';'); |
| 937 | return {oldDir, newDir, extraDir}; |
| 938 | } |
| 939 | |
| 940 | static void parseClangOption(StringRef opt, const Twine &msg) { |
| 941 | std::string err; |
| 942 | raw_string_ostream os(err); |
| 943 | |
| 944 | const char *argv[] = {"lld" , opt.data()}; |
| 945 | if (cl::ParseCommandLineOptions(argc: 2, argv, Overview: "" , Errs: &os)) |
| 946 | return; |
| 947 | error(msg: msg + ": " + StringRef(err).trim()); |
| 948 | } |
| 949 | |
| 950 | static uint32_t parseDylibVersion(const ArgList &args, unsigned id) { |
| 951 | const Arg *arg = args.getLastArg(Ids: id); |
| 952 | if (!arg) |
| 953 | return 0; |
| 954 | |
| 955 | if (config->outputType != MH_DYLIB) { |
| 956 | error(msg: arg->getAsString(Args: args) + ": only valid with -dylib" ); |
| 957 | return 0; |
| 958 | } |
| 959 | |
| 960 | PackedVersion version; |
| 961 | if (!version.parse32(Str: arg->getValue())) { |
| 962 | error(msg: arg->getAsString(Args: args) + ": malformed version" ); |
| 963 | return 0; |
| 964 | } |
| 965 | |
| 966 | return version.rawValue(); |
| 967 | } |
| 968 | |
| 969 | static uint32_t parseProtection(StringRef protStr) { |
| 970 | uint32_t prot = 0; |
| 971 | for (char c : protStr) { |
| 972 | switch (c) { |
| 973 | case 'r': |
| 974 | prot |= VM_PROT_READ; |
| 975 | break; |
| 976 | case 'w': |
| 977 | prot |= VM_PROT_WRITE; |
| 978 | break; |
| 979 | case 'x': |
| 980 | prot |= VM_PROT_EXECUTE; |
| 981 | break; |
| 982 | case '-': |
| 983 | break; |
| 984 | default: |
| 985 | error(msg: "unknown -segprot letter '" + Twine(c) + "' in " + protStr); |
| 986 | return 0; |
| 987 | } |
| 988 | } |
| 989 | return prot; |
| 990 | } |
| 991 | |
| 992 | static std::vector<SectionAlign> parseSectAlign(const opt::InputArgList &args) { |
| 993 | std::vector<SectionAlign> sectAligns; |
| 994 | for (const Arg *arg : args.filtered(Ids: OPT_sectalign)) { |
| 995 | StringRef segName = arg->getValue(N: 0); |
| 996 | StringRef sectName = arg->getValue(N: 1); |
| 997 | StringRef alignStr = arg->getValue(N: 2); |
| 998 | alignStr.consume_front_insensitive(Prefix: "0x" ); |
| 999 | uint32_t align; |
| 1000 | if (alignStr.getAsInteger(Radix: 16, Result&: align)) { |
| 1001 | error(msg: "-sectalign: failed to parse '" + StringRef(arg->getValue(N: 2)) + |
| 1002 | "' as number" ); |
| 1003 | continue; |
| 1004 | } |
| 1005 | if (!isPowerOf2_32(Value: align)) { |
| 1006 | error(msg: "-sectalign: '" + StringRef(arg->getValue(N: 2)) + |
| 1007 | "' (in base 16) not a power of two" ); |
| 1008 | continue; |
| 1009 | } |
| 1010 | sectAligns.push_back(x: {.segName: segName, .sectName: sectName, .align: align}); |
| 1011 | } |
| 1012 | return sectAligns; |
| 1013 | } |
| 1014 | |
| 1015 | PlatformType macho::removeSimulator(PlatformType platform) { |
| 1016 | switch (platform) { |
| 1017 | case PLATFORM_IOSSIMULATOR: |
| 1018 | return PLATFORM_IOS; |
| 1019 | case PLATFORM_TVOSSIMULATOR: |
| 1020 | return PLATFORM_TVOS; |
| 1021 | case PLATFORM_WATCHOSSIMULATOR: |
| 1022 | return PLATFORM_WATCHOS; |
| 1023 | case PLATFORM_XROS_SIMULATOR: |
| 1024 | return PLATFORM_XROS; |
| 1025 | default: |
| 1026 | return platform; |
| 1027 | } |
| 1028 | } |
| 1029 | |
| 1030 | static bool supportsNoPie() { |
| 1031 | return !(config->arch() == AK_arm64 || config->arch() == AK_arm64e || |
| 1032 | config->arch() == AK_arm64_32); |
| 1033 | } |
| 1034 | |
| 1035 | static bool shouldAdhocSignByDefault(Architecture arch, PlatformType platform) { |
| 1036 | if (arch != AK_arm64 && arch != AK_arm64e) |
| 1037 | return false; |
| 1038 | |
| 1039 | return platform == PLATFORM_MACOS || platform == PLATFORM_IOSSIMULATOR || |
| 1040 | platform == PLATFORM_TVOSSIMULATOR || |
| 1041 | platform == PLATFORM_WATCHOSSIMULATOR || |
| 1042 | platform == PLATFORM_XROS_SIMULATOR; |
| 1043 | } |
| 1044 | |
| 1045 | template <std::size_t N> |
| 1046 | using MinVersions = std::array<std::pair<PlatformType, VersionTuple>, N>; |
| 1047 | |
| 1048 | /// Returns true if the platform is greater than the min version. |
| 1049 | /// Returns false if the platform does not exist. |
| 1050 | template <std::size_t N> |
| 1051 | static bool greaterEqMinVersion(const MinVersions<N> &minVersions, |
| 1052 | bool ignoreSimulator) { |
| 1053 | PlatformType platform = config->platformInfo.target.Platform; |
| 1054 | if (ignoreSimulator) |
| 1055 | platform = removeSimulator(platform); |
| 1056 | auto it = llvm::find_if(minVersions, |
| 1057 | [&](const auto &p) { return p.first == platform; }); |
| 1058 | if (it != minVersions.end()) |
| 1059 | if (config->platformInfo.target.MinDeployment >= it->second) |
| 1060 | return true; |
| 1061 | return false; |
| 1062 | } |
| 1063 | |
| 1064 | static bool dataConstDefault(const InputArgList &args) { |
| 1065 | static const MinVersions<6> minVersion = {._M_elems: { |
| 1066 | {PLATFORM_MACOS, VersionTuple(10, 15)}, |
| 1067 | {PLATFORM_IOS, VersionTuple(13, 0)}, |
| 1068 | {PLATFORM_TVOS, VersionTuple(13, 0)}, |
| 1069 | {PLATFORM_WATCHOS, VersionTuple(6, 0)}, |
| 1070 | {PLATFORM_XROS, VersionTuple(1, 0)}, |
| 1071 | {PLATFORM_BRIDGEOS, VersionTuple(4, 0)}, |
| 1072 | }}; |
| 1073 | if (!greaterEqMinVersion(minVersions: minVersion, ignoreSimulator: true)) |
| 1074 | return false; |
| 1075 | |
| 1076 | switch (config->outputType) { |
| 1077 | case MH_EXECUTE: |
| 1078 | return !(args.hasArg(Ids: OPT_no_pie) && supportsNoPie()); |
| 1079 | case MH_BUNDLE: |
| 1080 | // FIXME: return false when -final_name ... |
| 1081 | // has prefix "/System/Library/UserEventPlugins/" |
| 1082 | // or matches "/usr/libexec/locationd" "/usr/libexec/terminusd" |
| 1083 | return true; |
| 1084 | case MH_DYLIB: |
| 1085 | return true; |
| 1086 | case MH_OBJECT: |
| 1087 | return false; |
| 1088 | default: |
| 1089 | llvm_unreachable( |
| 1090 | "unsupported output type for determining data-const default" ); |
| 1091 | } |
| 1092 | return false; |
| 1093 | } |
| 1094 | |
| 1095 | static bool shouldEmitChainedFixups(const InputArgList &args) { |
| 1096 | const Arg *arg = args.getLastArg(Ids: OPT_fixup_chains, Ids: OPT_no_fixup_chains); |
| 1097 | if (arg && arg->getOption().matches(ID: OPT_no_fixup_chains)) |
| 1098 | return false; |
| 1099 | |
| 1100 | bool requested = arg && arg->getOption().matches(ID: OPT_fixup_chains); |
| 1101 | if (!config->isPic) { |
| 1102 | if (requested) |
| 1103 | error(msg: "-fixup_chains is incompatible with -no_pie" ); |
| 1104 | |
| 1105 | return false; |
| 1106 | } |
| 1107 | |
| 1108 | if (!is_contained(Set: {AK_x86_64, AK_x86_64h, AK_arm64}, Element: config->arch())) { |
| 1109 | if (requested) |
| 1110 | error(msg: "-fixup_chains is only supported on x86_64 and arm64 targets" ); |
| 1111 | |
| 1112 | return false; |
| 1113 | } |
| 1114 | |
| 1115 | if (args.hasArg(Ids: OPT_preload)) { |
| 1116 | if (requested) |
| 1117 | error(msg: "-fixup_chains is incompatible with -preload" ); |
| 1118 | |
| 1119 | return false; |
| 1120 | } |
| 1121 | |
| 1122 | if (requested) |
| 1123 | return true; |
| 1124 | |
| 1125 | static const MinVersions<9> minVersion = {._M_elems: { |
| 1126 | {PLATFORM_IOS, VersionTuple(13, 4)}, |
| 1127 | {PLATFORM_IOSSIMULATOR, VersionTuple(16, 0)}, |
| 1128 | {PLATFORM_MACOS, VersionTuple(13, 0)}, |
| 1129 | {PLATFORM_TVOS, VersionTuple(14, 0)}, |
| 1130 | {PLATFORM_TVOSSIMULATOR, VersionTuple(15, 0)}, |
| 1131 | {PLATFORM_WATCHOS, VersionTuple(7, 0)}, |
| 1132 | {PLATFORM_WATCHOSSIMULATOR, VersionTuple(8, 0)}, |
| 1133 | {PLATFORM_XROS, VersionTuple(1, 0)}, |
| 1134 | {PLATFORM_XROS_SIMULATOR, VersionTuple(1, 0)}, |
| 1135 | }}; |
| 1136 | return greaterEqMinVersion(minVersions: minVersion, ignoreSimulator: false); |
| 1137 | } |
| 1138 | |
| 1139 | static bool shouldEmitRelativeMethodLists(const InputArgList &args) { |
| 1140 | const Arg *arg = args.getLastArg(Ids: OPT_objc_relative_method_lists, |
| 1141 | Ids: OPT_no_objc_relative_method_lists); |
| 1142 | if (arg && arg->getOption().getID() == OPT_objc_relative_method_lists) |
| 1143 | return true; |
| 1144 | if (arg && arg->getOption().getID() == OPT_no_objc_relative_method_lists) |
| 1145 | return false; |
| 1146 | |
| 1147 | // If no flag is specified, enable this on newer versions by default. |
| 1148 | // The min versions is taken from |
| 1149 | // ld64(https://github.com/apple-oss-distributions/ld64/blob/47f477cb721755419018f7530038b272e9d0cdea/src/ld/ld.hpp#L310) |
| 1150 | // to mimic to operation of ld64 |
| 1151 | // [here](https://github.com/apple-oss-distributions/ld64/blob/47f477cb721755419018f7530038b272e9d0cdea/src/ld/Options.cpp#L6085-L6101) |
| 1152 | static const MinVersions<6> minVersion = {._M_elems: { |
| 1153 | {PLATFORM_MACOS, VersionTuple(10, 16)}, |
| 1154 | {PLATFORM_IOS, VersionTuple(14, 0)}, |
| 1155 | {PLATFORM_WATCHOS, VersionTuple(7, 0)}, |
| 1156 | {PLATFORM_TVOS, VersionTuple(14, 0)}, |
| 1157 | {PLATFORM_BRIDGEOS, VersionTuple(5, 0)}, |
| 1158 | {PLATFORM_XROS, VersionTuple(1, 0)}, |
| 1159 | }}; |
| 1160 | return greaterEqMinVersion(minVersions: minVersion, ignoreSimulator: true); |
| 1161 | } |
| 1162 | |
| 1163 | void SymbolPatterns::clear() { |
| 1164 | literals.clear(); |
| 1165 | globs.clear(); |
| 1166 | } |
| 1167 | |
| 1168 | void SymbolPatterns::insert(StringRef symbolName) { |
| 1169 | if (symbolName.find_first_of(Chars: "*?[]" ) == StringRef::npos) |
| 1170 | literals.insert(X: CachedHashStringRef(symbolName)); |
| 1171 | else if (Expected<GlobPattern> pattern = GlobPattern::create(Pat: symbolName)) |
| 1172 | globs.emplace_back(args&: *pattern); |
| 1173 | else |
| 1174 | error(msg: "invalid symbol-name pattern: " + symbolName); |
| 1175 | } |
| 1176 | |
| 1177 | bool SymbolPatterns::matchLiteral(StringRef symbolName) const { |
| 1178 | return literals.contains(key: CachedHashStringRef(symbolName)); |
| 1179 | } |
| 1180 | |
| 1181 | bool SymbolPatterns::matchGlob(StringRef symbolName) const { |
| 1182 | for (const GlobPattern &glob : globs) |
| 1183 | if (glob.match(S: symbolName)) |
| 1184 | return true; |
| 1185 | return false; |
| 1186 | } |
| 1187 | |
| 1188 | bool SymbolPatterns::match(StringRef symbolName) const { |
| 1189 | return matchLiteral(symbolName) || matchGlob(symbolName); |
| 1190 | } |
| 1191 | |
| 1192 | static void parseSymbolPatternsFile(const Arg *arg, |
| 1193 | SymbolPatterns &symbolPatterns) { |
| 1194 | StringRef path = arg->getValue(); |
| 1195 | std::optional<MemoryBufferRef> buffer = readFile(path); |
| 1196 | if (!buffer) { |
| 1197 | error(msg: "Could not read symbol file: " + path); |
| 1198 | return; |
| 1199 | } |
| 1200 | MemoryBufferRef mbref = *buffer; |
| 1201 | for (StringRef line : args::getLines(mb: mbref)) { |
| 1202 | line = line.take_until(F: [](char c) { return c == '#'; }).trim(); |
| 1203 | if (!line.empty()) |
| 1204 | symbolPatterns.insert(symbolName: line); |
| 1205 | } |
| 1206 | } |
| 1207 | |
| 1208 | static void handleSymbolPatterns(InputArgList &args, |
| 1209 | SymbolPatterns &symbolPatterns, |
| 1210 | unsigned singleOptionCode, |
| 1211 | unsigned listFileOptionCode) { |
| 1212 | for (const Arg *arg : args.filtered(Ids: singleOptionCode)) |
| 1213 | symbolPatterns.insert(symbolName: arg->getValue()); |
| 1214 | for (const Arg *arg : args.filtered(Ids: listFileOptionCode)) |
| 1215 | parseSymbolPatternsFile(arg, symbolPatterns); |
| 1216 | } |
| 1217 | |
| 1218 | static void createFiles(const InputArgList &args) { |
| 1219 | TimeTraceScope timeScope("Load input files" ); |
| 1220 | // This loop should be reserved for options whose exact ordering matters. |
| 1221 | // Other options should be handled via filtered() and/or getLastArg(). |
| 1222 | bool isLazy = false; |
| 1223 | // If we've processed an opening --start-lib, without a matching --end-lib |
| 1224 | bool inLib = false; |
| 1225 | for (const Arg *arg : args) { |
| 1226 | const Option &opt = arg->getOption(); |
| 1227 | warnIfDeprecatedOption(opt); |
| 1228 | warnIfUnimplementedOption(opt); |
| 1229 | |
| 1230 | switch (opt.getID()) { |
| 1231 | case OPT_INPUT: |
| 1232 | addFile(path: rerootPath(path: arg->getValue()), loadType: LoadType::CommandLine, isLazy); |
| 1233 | break; |
| 1234 | case OPT_needed_library: |
| 1235 | if (auto *dylibFile = dyn_cast_or_null<DylibFile>( |
| 1236 | Val: addFile(path: rerootPath(path: arg->getValue()), loadType: LoadType::CommandLine))) |
| 1237 | dylibFile->forceNeeded = true; |
| 1238 | break; |
| 1239 | case OPT_reexport_library: |
| 1240 | if (auto *dylibFile = dyn_cast_or_null<DylibFile>( |
| 1241 | Val: addFile(path: rerootPath(path: arg->getValue()), loadType: LoadType::CommandLine))) { |
| 1242 | config->hasReexports = true; |
| 1243 | dylibFile->reexport = true; |
| 1244 | } |
| 1245 | break; |
| 1246 | case OPT_weak_library: |
| 1247 | if (auto *dylibFile = dyn_cast_or_null<DylibFile>( |
| 1248 | Val: addFile(path: rerootPath(path: arg->getValue()), loadType: LoadType::CommandLine))) |
| 1249 | dylibFile->forceWeakImport = true; |
| 1250 | break; |
| 1251 | case OPT_filelist: |
| 1252 | addFileList(path: arg->getValue(), isLazy); |
| 1253 | break; |
| 1254 | case OPT_force_load: |
| 1255 | addFile(path: rerootPath(path: arg->getValue()), loadType: LoadType::CommandLineForce); |
| 1256 | break; |
| 1257 | case OPT_load_hidden: |
| 1258 | addFile(path: rerootPath(path: arg->getValue()), loadType: LoadType::CommandLine, |
| 1259 | /*isLazy=*/false, /*isExplicit=*/true, /*isBundleLoader=*/false, |
| 1260 | /*isForceHidden=*/true); |
| 1261 | break; |
| 1262 | case OPT_l: |
| 1263 | case OPT_needed_l: |
| 1264 | case OPT_reexport_l: |
| 1265 | case OPT_weak_l: |
| 1266 | case OPT_hidden_l: |
| 1267 | addLibrary(name: arg->getValue(), isNeeded: opt.getID() == OPT_needed_l, |
| 1268 | isWeak: opt.getID() == OPT_weak_l, isReexport: opt.getID() == OPT_reexport_l, |
| 1269 | isHidden: opt.getID() == OPT_hidden_l, |
| 1270 | /*isExplicit=*/true, loadType: LoadType::CommandLine); |
| 1271 | break; |
| 1272 | case OPT_framework: |
| 1273 | case OPT_needed_framework: |
| 1274 | case OPT_reexport_framework: |
| 1275 | case OPT_weak_framework: |
| 1276 | addFramework(name: arg->getValue(), isNeeded: opt.getID() == OPT_needed_framework, |
| 1277 | isWeak: opt.getID() == OPT_weak_framework, |
| 1278 | isReexport: opt.getID() == OPT_reexport_framework, /*isExplicit=*/true, |
| 1279 | loadType: LoadType::CommandLine); |
| 1280 | break; |
| 1281 | case OPT_start_lib: |
| 1282 | if (inLib) |
| 1283 | error(msg: "nested --start-lib" ); |
| 1284 | inLib = true; |
| 1285 | if (!config->allLoad) |
| 1286 | isLazy = true; |
| 1287 | break; |
| 1288 | case OPT_end_lib: |
| 1289 | if (!inLib) |
| 1290 | error(msg: "stray --end-lib" ); |
| 1291 | inLib = false; |
| 1292 | isLazy = false; |
| 1293 | break; |
| 1294 | default: |
| 1295 | break; |
| 1296 | } |
| 1297 | } |
| 1298 | } |
| 1299 | |
| 1300 | static void gatherInputSections() { |
| 1301 | TimeTraceScope timeScope("Gathering input sections" ); |
| 1302 | for (const InputFile *file : inputFiles) { |
| 1303 | for (const Section *section : file->sections) { |
| 1304 | // Compact unwind entries require special handling elsewhere. (In |
| 1305 | // contrast, EH frames are handled like regular ConcatInputSections.) |
| 1306 | if (section->name == section_names::compactUnwind) |
| 1307 | continue; |
| 1308 | // Addrsig sections contain metadata only needed at link time. |
| 1309 | if (section->name == section_names::addrSig) |
| 1310 | continue; |
| 1311 | for (const Subsection &subsection : section->subsections) |
| 1312 | addInputSection(inputSection: subsection.isec); |
| 1313 | } |
| 1314 | if (!file->objCImageInfo.empty()) |
| 1315 | in.objCImageInfo->addFile(file); |
| 1316 | } |
| 1317 | } |
| 1318 | |
| 1319 | static void codegenDataGenerate() { |
| 1320 | TimeTraceScope timeScope("Generating codegen data" ); |
| 1321 | |
| 1322 | OutlinedHashTreeRecord globalOutlineRecord; |
| 1323 | StableFunctionMapRecord globalMergeRecord; |
| 1324 | for (ConcatInputSection *isec : inputSections) { |
| 1325 | if (isec->getSegName() != segment_names::data) |
| 1326 | continue; |
| 1327 | if (isec->getName() == section_names::outlinedHashTree) { |
| 1328 | // Read outlined hash tree from each section. |
| 1329 | OutlinedHashTreeRecord localOutlineRecord; |
| 1330 | // Use a pointer to allow modification by the function. |
| 1331 | auto *data = isec->data.data(); |
| 1332 | localOutlineRecord.deserialize(Ptr&: data); |
| 1333 | |
| 1334 | // Merge it to the global hash tree. |
| 1335 | globalOutlineRecord.merge(Other: localOutlineRecord); |
| 1336 | } |
| 1337 | if (isec->getName() == section_names::functionMap) { |
| 1338 | // Read stable functions from each section. |
| 1339 | StableFunctionMapRecord localMergeRecord; |
| 1340 | // Use a pointer to allow modification by the function. |
| 1341 | auto *data = isec->data.data(); |
| 1342 | localMergeRecord.deserialize(Ptr&: data); |
| 1343 | |
| 1344 | // Merge it to the global function map. |
| 1345 | globalMergeRecord.merge(Other: localMergeRecord); |
| 1346 | } |
| 1347 | } |
| 1348 | |
| 1349 | globalMergeRecord.finalize(); |
| 1350 | |
| 1351 | CodeGenDataWriter Writer; |
| 1352 | if (!globalOutlineRecord.empty()) |
| 1353 | Writer.addRecord(Record&: globalOutlineRecord); |
| 1354 | if (!globalMergeRecord.empty()) |
| 1355 | Writer.addRecord(Record&: globalMergeRecord); |
| 1356 | |
| 1357 | std::error_code EC; |
| 1358 | auto fileName = config->codegenDataGeneratePath; |
| 1359 | assert(!fileName.empty()); |
| 1360 | raw_fd_ostream Output(fileName, EC, sys::fs::OF_None); |
| 1361 | if (EC) |
| 1362 | error(msg: "fail to create " + fileName + ": " + EC.message()); |
| 1363 | |
| 1364 | if (auto E = Writer.write(OS&: Output)) |
| 1365 | error(msg: "fail to write CGData: " + toString(E: std::move(E))); |
| 1366 | } |
| 1367 | |
| 1368 | static void foldIdenticalLiterals() { |
| 1369 | TimeTraceScope timeScope("Fold identical literals" ); |
| 1370 | // We always create a cStringSection, regardless of whether dedupLiterals is |
| 1371 | // true. If it isn't, we simply create a non-deduplicating CStringSection. |
| 1372 | // Either way, we must unconditionally finalize it here. |
| 1373 | in.cStringSection->finalizeContents(); |
| 1374 | in.objcMethnameSection->finalizeContents(); |
| 1375 | in.wordLiteralSection->finalizeContents(); |
| 1376 | } |
| 1377 | |
| 1378 | static void addSynthenticMethnames() { |
| 1379 | std::string &data = *make<std::string>(); |
| 1380 | llvm::raw_string_ostream os(data); |
| 1381 | for (Symbol *sym : symtab->getSymbols()) |
| 1382 | if (isa<Undefined>(Val: sym)) |
| 1383 | if (ObjCStubsSection::isObjCStubSymbol(sym)) |
| 1384 | os << ObjCStubsSection::getMethname(sym) << '\0'; |
| 1385 | |
| 1386 | if (data.empty()) |
| 1387 | return; |
| 1388 | |
| 1389 | const auto *buf = reinterpret_cast<const uint8_t *>(data.c_str()); |
| 1390 | Section §ion = *make<Section>(/*file=*/args: nullptr, args: segment_names::text, |
| 1391 | args: section_names::objcMethname, |
| 1392 | args: S_CSTRING_LITERALS, /*addr=*/args: 0); |
| 1393 | |
| 1394 | auto *isec = |
| 1395 | make<CStringInputSection>(args&: section, args: ArrayRef<uint8_t>{buf, data.size()}, |
| 1396 | /*align=*/args: 1, /*dedupLiterals=*/args: true); |
| 1397 | isec->splitIntoPieces(); |
| 1398 | for (auto &piece : isec->pieces) |
| 1399 | piece.live = true; |
| 1400 | section.subsections.push_back(x: {.offset: 0, .isec: isec}); |
| 1401 | in.objcMethnameSection->addInput(isec); |
| 1402 | in.objcMethnameSection->isec->markLive(off: 0); |
| 1403 | } |
| 1404 | |
| 1405 | static void referenceStubBinder() { |
| 1406 | bool needsStubHelper = config->outputType == MH_DYLIB || |
| 1407 | config->outputType == MH_EXECUTE || |
| 1408 | config->outputType == MH_BUNDLE; |
| 1409 | if (!needsStubHelper || !symtab->find(name: "dyld_stub_binder" )) |
| 1410 | return; |
| 1411 | |
| 1412 | // dyld_stub_binder is used by dyld to resolve lazy bindings. This code here |
| 1413 | // adds a opportunistic reference to dyld_stub_binder if it happens to exist. |
| 1414 | // dyld_stub_binder is in libSystem.dylib, which is usually linked in. This |
| 1415 | // isn't needed for correctness, but the presence of that symbol suppresses |
| 1416 | // "no symbols" diagnostics from `nm`. |
| 1417 | // StubHelperSection::setUp() adds a reference and errors out if |
| 1418 | // dyld_stub_binder doesn't exist in case it is actually needed. |
| 1419 | symtab->addUndefined(name: "dyld_stub_binder" , /*file=*/nullptr, /*isWeak=*/isWeakRef: false); |
| 1420 | } |
| 1421 | |
| 1422 | static void createAliases() { |
| 1423 | for (const auto &pair : config->aliasedSymbols) { |
| 1424 | if (const auto &sym = symtab->find(name: pair.first)) { |
| 1425 | if (const auto &defined = dyn_cast<Defined>(Val: sym)) { |
| 1426 | symtab->aliasDefined(src: defined, target: pair.second, newFile: defined->getFile()) |
| 1427 | ->noDeadStrip = true; |
| 1428 | } else { |
| 1429 | error(msg: "TODO: support aliasing to symbols of kind " + |
| 1430 | Twine(sym->kind())); |
| 1431 | } |
| 1432 | } else { |
| 1433 | warn(msg: "undefined base symbol '" + pair.first + "' for alias '" + |
| 1434 | pair.second + "'\n" ); |
| 1435 | } |
| 1436 | } |
| 1437 | |
| 1438 | for (const InputFile *file : inputFiles) { |
| 1439 | if (auto *objFile = dyn_cast<ObjFile>(Val: file)) { |
| 1440 | for (const AliasSymbol *alias : objFile->aliases) { |
| 1441 | if (const auto &aliased = symtab->find(name: alias->getAliasedName())) { |
| 1442 | if (const auto &defined = dyn_cast<Defined>(Val: aliased)) { |
| 1443 | symtab->aliasDefined(src: defined, target: alias->getName(), newFile: alias->getFile(), |
| 1444 | makePrivateExtern: alias->privateExtern); |
| 1445 | } else { |
| 1446 | // Common, dylib, and undefined symbols are all valid alias |
| 1447 | // referents (undefineds can become valid Defined symbols later on |
| 1448 | // in the link.) |
| 1449 | error(msg: "TODO: support aliasing to symbols of kind " + |
| 1450 | Twine(aliased->kind())); |
| 1451 | } |
| 1452 | } else { |
| 1453 | // This shouldn't happen since MC generates undefined symbols to |
| 1454 | // represent the alias referents. Thus we fatal() instead of just |
| 1455 | // warning here. |
| 1456 | fatal(msg: "unable to find alias referent " + alias->getAliasedName() + |
| 1457 | " for " + alias->getName()); |
| 1458 | } |
| 1459 | } |
| 1460 | } |
| 1461 | } |
| 1462 | } |
| 1463 | |
| 1464 | static void handleExplicitExports() { |
| 1465 | static constexpr int kMaxWarnings = 3; |
| 1466 | if (config->hasExplicitExports) { |
| 1467 | std::atomic<uint64_t> warningsCount{0}; |
| 1468 | parallelForEach(R: symtab->getSymbols(), Fn: [&warningsCount](Symbol *sym) { |
| 1469 | if (auto *defined = dyn_cast<Defined>(Val: sym)) { |
| 1470 | if (config->exportedSymbols.match(symbolName: sym->getName())) { |
| 1471 | if (defined->privateExtern) { |
| 1472 | if (defined->weakDefCanBeHidden) { |
| 1473 | // weak_def_can_be_hidden symbols behave similarly to |
| 1474 | // private_extern symbols in most cases, except for when |
| 1475 | // it is explicitly exported. |
| 1476 | // The former can be exported but the latter cannot. |
| 1477 | defined->privateExtern = false; |
| 1478 | } else { |
| 1479 | // Only print the first 3 warnings verbosely, and |
| 1480 | // shorten the rest to avoid crowding logs. |
| 1481 | if (warningsCount.fetch_add(i: 1, m: std::memory_order_relaxed) < |
| 1482 | kMaxWarnings) |
| 1483 | warn(msg: "cannot export hidden symbol " + toString(*defined) + |
| 1484 | "\n>>> defined in " + toString(file: defined->getFile())); |
| 1485 | } |
| 1486 | } |
| 1487 | } else { |
| 1488 | defined->privateExtern = true; |
| 1489 | } |
| 1490 | } else if (auto *dysym = dyn_cast<DylibSymbol>(Val: sym)) { |
| 1491 | dysym->shouldReexport = config->exportedSymbols.match(symbolName: sym->getName()); |
| 1492 | } |
| 1493 | }); |
| 1494 | if (warningsCount > kMaxWarnings) |
| 1495 | warn(msg: "<... " + Twine(warningsCount - kMaxWarnings) + |
| 1496 | " more similar warnings...>" ); |
| 1497 | } else if (!config->unexportedSymbols.empty()) { |
| 1498 | parallelForEach(R: symtab->getSymbols(), Fn: [](Symbol *sym) { |
| 1499 | if (auto *defined = dyn_cast<Defined>(Val: sym)) |
| 1500 | if (config->unexportedSymbols.match(symbolName: defined->getName())) |
| 1501 | defined->privateExtern = true; |
| 1502 | }); |
| 1503 | } |
| 1504 | } |
| 1505 | |
| 1506 | static void eraseInitializerSymbols() { |
| 1507 | for (ConcatInputSection *isec : in.initOffsets->inputs()) |
| 1508 | for (Defined *sym : isec->symbols) |
| 1509 | sym->used = false; |
| 1510 | } |
| 1511 | |
| 1512 | static SmallVector<StringRef, 0> getRuntimePaths(opt::InputArgList &args) { |
| 1513 | SmallVector<StringRef, 0> vals; |
| 1514 | DenseSet<StringRef> seen; |
| 1515 | for (const Arg *arg : args.filtered(Ids: OPT_rpath)) { |
| 1516 | StringRef val = arg->getValue(); |
| 1517 | if (seen.insert(V: val).second) |
| 1518 | vals.push_back(Elt: val); |
| 1519 | else if (config->warnDuplicateRpath) |
| 1520 | warn(msg: "duplicate -rpath '" + val + "' ignored [--warn-duplicate-rpath]" ); |
| 1521 | } |
| 1522 | return vals; |
| 1523 | } |
| 1524 | |
| 1525 | static SmallVector<StringRef, 0> getAllowableClients(opt::InputArgList &args) { |
| 1526 | SmallVector<StringRef, 0> vals; |
| 1527 | DenseSet<StringRef> seen; |
| 1528 | for (const Arg *arg : args.filtered(Ids: OPT_allowable_client)) { |
| 1529 | StringRef val = arg->getValue(); |
| 1530 | if (seen.insert(V: val).second) |
| 1531 | vals.push_back(Elt: val); |
| 1532 | } |
| 1533 | return vals; |
| 1534 | } |
| 1535 | |
| 1536 | namespace lld { |
| 1537 | namespace macho { |
| 1538 | bool link(ArrayRef<const char *> argsArr, llvm::raw_ostream &stdoutOS, |
| 1539 | llvm::raw_ostream &stderrOS, bool exitEarly, bool disableOutput) { |
| 1540 | // This driver-specific context will be freed later by lldMain(). |
| 1541 | auto *ctx = new CommonLinkerContext; |
| 1542 | |
| 1543 | ctx->e.initialize(stdoutOS, stderrOS, exitEarly, disableOutput); |
| 1544 | ctx->e.cleanupCallback = []() { |
| 1545 | resolvedFrameworks.clear(); |
| 1546 | resolvedLibraries.clear(); |
| 1547 | cachedReads.clear(); |
| 1548 | concatOutputSections.clear(); |
| 1549 | inputFiles.clear(); |
| 1550 | inputSections.clear(); |
| 1551 | inputSectionsOrder = 0; |
| 1552 | loadedArchives.clear(); |
| 1553 | loadedObjectFrameworks.clear(); |
| 1554 | missingAutolinkWarnings.clear(); |
| 1555 | syntheticSections.clear(); |
| 1556 | thunkMap.clear(); |
| 1557 | unprocessedLCLinkerOptions.clear(); |
| 1558 | ObjCSelRefsHelper::cleanup(); |
| 1559 | |
| 1560 | firstTLVDataSection = nullptr; |
| 1561 | tar = nullptr; |
| 1562 | memset(s: &in, c: 0, n: sizeof(in)); |
| 1563 | |
| 1564 | resetLoadedDylibs(); |
| 1565 | resetOutputSegments(); |
| 1566 | resetWriter(); |
| 1567 | InputFile::resetIdCount(); |
| 1568 | |
| 1569 | objc::doCleanup(); |
| 1570 | }; |
| 1571 | |
| 1572 | ctx->e.logName = args::getFilenameWithoutExe(path: argsArr[0]); |
| 1573 | |
| 1574 | MachOOptTable parser; |
| 1575 | InputArgList args = parser.parse(ctx&: *ctx, argv: argsArr.slice(N: 1)); |
| 1576 | |
| 1577 | ctx->e.errorLimitExceededMsg = "too many errors emitted, stopping now " |
| 1578 | "(use --error-limit=0 to see all errors)" ; |
| 1579 | ctx->e.errorLimit = args::getInteger(args, key: OPT_error_limit_eq, Default: 20); |
| 1580 | ctx->e.verbose = args.hasArg(Ids: OPT_verbose); |
| 1581 | |
| 1582 | if (args.hasArg(Ids: OPT_help_hidden)) { |
| 1583 | parser.printHelp(ctx&: *ctx, argv0: argsArr[0], /*showHidden=*/true); |
| 1584 | return true; |
| 1585 | } |
| 1586 | if (args.hasArg(Ids: OPT_help)) { |
| 1587 | parser.printHelp(ctx&: *ctx, argv0: argsArr[0], /*showHidden=*/false); |
| 1588 | return true; |
| 1589 | } |
| 1590 | if (args.hasArg(Ids: OPT_version)) { |
| 1591 | message(msg: getLLDVersion()); |
| 1592 | return true; |
| 1593 | } |
| 1594 | |
| 1595 | config = std::make_unique<Configuration>(); |
| 1596 | symtab = std::make_unique<SymbolTable>(); |
| 1597 | config->outputType = getOutputType(args); |
| 1598 | target = createTargetInfo(args); |
| 1599 | depTracker = std::make_unique<DependencyTracker>( |
| 1600 | args: args.getLastArgValue(Id: OPT_dependency_info)); |
| 1601 | |
| 1602 | config->ltoo = args::getInteger(args, key: OPT_lto_O, Default: 2); |
| 1603 | if (config->ltoo > 3) |
| 1604 | error(msg: "--lto-O: invalid optimization level: " + Twine(config->ltoo)); |
| 1605 | unsigned ltoCgo = |
| 1606 | args::getInteger(args, key: OPT_lto_CGO, Default: args::getCGOptLevel(optLevelLTO: config->ltoo)); |
| 1607 | if (auto level = CodeGenOpt::getLevel(OL: ltoCgo)) |
| 1608 | config->ltoCgo = *level; |
| 1609 | else |
| 1610 | error(msg: "--lto-CGO: invalid codegen optimization level: " + Twine(ltoCgo)); |
| 1611 | |
| 1612 | if (errorCount()) |
| 1613 | return false; |
| 1614 | |
| 1615 | if (args.hasArg(Ids: OPT_pagezero_size)) { |
| 1616 | uint64_t pagezeroSize = args::getHex(args, key: OPT_pagezero_size, Default: 0); |
| 1617 | |
| 1618 | // ld64 does something really weird. It attempts to realign the value to the |
| 1619 | // page size, but assumes the page size is 4K. This doesn't work with most |
| 1620 | // of Apple's ARM64 devices, which use a page size of 16K. This means that |
| 1621 | // it will first 4K align it by rounding down, then round up to 16K. This |
| 1622 | // probably only happened because no one using this arg with anything other |
| 1623 | // then 0, so no one checked if it did what is what it says it does. |
| 1624 | |
| 1625 | // So we are not copying this weird behavior and doing the it in a logical |
| 1626 | // way, by always rounding down to page size. |
| 1627 | if (!isAligned(Lhs: Align(target->getPageSize()), SizeInBytes: pagezeroSize)) { |
| 1628 | pagezeroSize -= pagezeroSize % target->getPageSize(); |
| 1629 | warn(msg: "__PAGEZERO size is not page aligned, rounding down to 0x" + |
| 1630 | Twine::utohexstr(Val: pagezeroSize)); |
| 1631 | } |
| 1632 | |
| 1633 | target->pageZeroSize = pagezeroSize; |
| 1634 | } |
| 1635 | |
| 1636 | config->osoPrefix = args.getLastArgValue(Id: OPT_oso_prefix); |
| 1637 | if (!config->osoPrefix.empty()) { |
| 1638 | // Expand special characters, such as ".", "..", or "~", if present. |
| 1639 | // Note: LD64 only expands "." and not other special characters. |
| 1640 | // That seems silly to imitate so we will not try to follow it, but rather |
| 1641 | // just use real_path() to do it. |
| 1642 | |
| 1643 | // The max path length is 4096, in theory. However that seems quite long |
| 1644 | // and seems unlikely that any one would want to strip everything from the |
| 1645 | // path. Hence we've picked a reasonably large number here. |
| 1646 | SmallString<1024> expanded; |
| 1647 | if (!fs::real_path(path: config->osoPrefix, output&: expanded, |
| 1648 | /*expand_tilde=*/true)) { |
| 1649 | // Note: LD64 expands "." to be `<current_dir>/` |
| 1650 | // (ie., it has a slash suffix) whereas real_path() doesn't. |
| 1651 | // So we have to append '/' to be consistent. |
| 1652 | StringRef sep = sys::path::get_separator(); |
| 1653 | // real_path removes trailing slashes as part of the normalization, but |
| 1654 | // these are meaningful for our text based stripping |
| 1655 | if (config->osoPrefix == "." || config->osoPrefix.ends_with(Suffix: sep)) |
| 1656 | expanded += sep; |
| 1657 | config->osoPrefix = saver().save(S: expanded.str()); |
| 1658 | } |
| 1659 | } |
| 1660 | |
| 1661 | bool pie = args.hasFlag(Pos: OPT_pie, Neg: OPT_no_pie, Default: true); |
| 1662 | if (!supportsNoPie() && !pie) { |
| 1663 | warn(msg: "-no_pie ignored for arm64" ); |
| 1664 | pie = true; |
| 1665 | } |
| 1666 | |
| 1667 | config->isPic = config->outputType == MH_DYLIB || |
| 1668 | config->outputType == MH_BUNDLE || |
| 1669 | (config->outputType == MH_EXECUTE && pie); |
| 1670 | |
| 1671 | // Must be set before any InputSections and Symbols are created. |
| 1672 | config->deadStrip = args.hasArg(Ids: OPT_dead_strip); |
| 1673 | config->interposable = args.hasArg(Ids: OPT_interposable); |
| 1674 | |
| 1675 | config->systemLibraryRoots = getSystemLibraryRoots(args); |
| 1676 | if (const char *path = getReproduceOption(args)) { |
| 1677 | // Note that --reproduce is a debug option so you can ignore it |
| 1678 | // if you are trying to understand the whole picture of the code. |
| 1679 | Expected<std::unique_ptr<TarWriter>> errOrWriter = |
| 1680 | TarWriter::create(OutputPath: path, BaseDir: path::stem(path)); |
| 1681 | if (errOrWriter) { |
| 1682 | tar = std::move(*errOrWriter); |
| 1683 | tar->append(Path: "response.txt" , Data: createResponseFile(args)); |
| 1684 | tar->append(Path: "version.txt" , Data: getLLDVersion() + "\n" ); |
| 1685 | } else { |
| 1686 | error(msg: "--reproduce: " + toString(E: errOrWriter.takeError())); |
| 1687 | } |
| 1688 | } |
| 1689 | |
| 1690 | if (auto *arg = args.getLastArg(Ids: OPT_threads_eq)) { |
| 1691 | StringRef v(arg->getValue()); |
| 1692 | unsigned threads = 0; |
| 1693 | if (!llvm::to_integer(S: v, Num&: threads, Base: 0) || threads == 0) |
| 1694 | error(msg: arg->getSpelling() + ": expected a positive integer, but got '" + |
| 1695 | arg->getValue() + "'" ); |
| 1696 | parallel::strategy = hardware_concurrency(ThreadCount: threads); |
| 1697 | config->thinLTOJobs = v; |
| 1698 | } |
| 1699 | if (auto *arg = args.getLastArg(Ids: OPT_thinlto_jobs_eq)) |
| 1700 | config->thinLTOJobs = arg->getValue(); |
| 1701 | if (!get_threadpool_strategy(Num: config->thinLTOJobs)) |
| 1702 | error(msg: "--thinlto-jobs: invalid job count: " + config->thinLTOJobs); |
| 1703 | |
| 1704 | for (const Arg *arg : args.filtered(Ids: OPT_u)) { |
| 1705 | config->explicitUndefineds.push_back(x: symtab->addUndefined( |
| 1706 | name: arg->getValue(), /*file=*/nullptr, /*isWeakRef=*/false)); |
| 1707 | } |
| 1708 | |
| 1709 | for (const Arg *arg : args.filtered(Ids: OPT_U)) |
| 1710 | config->explicitDynamicLookups.insert(key: arg->getValue()); |
| 1711 | |
| 1712 | config->mapFile = args.getLastArgValue(Id: OPT_map); |
| 1713 | config->optimize = args::getInteger(args, key: OPT_O, Default: 1); |
| 1714 | config->outputFile = args.getLastArgValue(Id: OPT_o, Default: "a.out" ); |
| 1715 | config->finalOutput = |
| 1716 | args.getLastArgValue(Id: OPT_final_output, Default: config->outputFile); |
| 1717 | config->astPaths = args.getAllArgValues(Id: OPT_add_ast_path); |
| 1718 | config->headerPad = args::getHex(args, key: OPT_headerpad, /*Default=*/32); |
| 1719 | config->headerPadMaxInstallNames = |
| 1720 | args.hasArg(Ids: OPT_headerpad_max_install_names); |
| 1721 | config->printDylibSearch = |
| 1722 | args.hasArg(Ids: OPT_print_dylib_search) || getenv(name: "RC_TRACE_DYLIB_SEARCHING" ); |
| 1723 | config->printEachFile = args.hasArg(Ids: OPT_t); |
| 1724 | config->printWhyLoad = args.hasArg(Ids: OPT_why_load); |
| 1725 | config->omitDebugInfo = args.hasArg(Ids: OPT_S); |
| 1726 | config->errorForArchMismatch = args.hasArg(Ids: OPT_arch_errors_fatal); |
| 1727 | if (const Arg *arg = args.getLastArg(Ids: OPT_bundle_loader)) { |
| 1728 | if (config->outputType != MH_BUNDLE) |
| 1729 | error(msg: "-bundle_loader can only be used with MachO bundle output" ); |
| 1730 | addFile(path: arg->getValue(), loadType: LoadType::CommandLine, /*isLazy=*/false, |
| 1731 | /*isExplicit=*/false, /*isBundleLoader=*/true); |
| 1732 | } |
| 1733 | for (auto *arg : args.filtered(Ids: OPT_dyld_env)) { |
| 1734 | StringRef envPair(arg->getValue()); |
| 1735 | if (!envPair.contains(C: '=')) |
| 1736 | error(msg: "-dyld_env's argument is malformed. Expected " |
| 1737 | "-dyld_env <ENV_VAR>=<VALUE>, got `" + |
| 1738 | envPair + "`" ); |
| 1739 | config->dyldEnvs.push_back(x: envPair); |
| 1740 | } |
| 1741 | if (!config->dyldEnvs.empty() && config->outputType != MH_EXECUTE) |
| 1742 | error(msg: "-dyld_env can only be used when creating executable output" ); |
| 1743 | |
| 1744 | if (const Arg *arg = args.getLastArg(Ids: OPT_umbrella)) { |
| 1745 | if (config->outputType != MH_DYLIB) |
| 1746 | warn(msg: "-umbrella used, but not creating dylib" ); |
| 1747 | config->umbrella = arg->getValue(); |
| 1748 | } |
| 1749 | config->ltoObjPath = args.getLastArgValue(Id: OPT_object_path_lto); |
| 1750 | config->ltoNewPmPasses = args.getLastArgValue(Id: OPT_lto_newpm_passes); |
| 1751 | config->thinLTOCacheDir = args.getLastArgValue(Id: OPT_cache_path_lto); |
| 1752 | config->thinLTOCachePolicy = getLTOCachePolicy(args); |
| 1753 | config->thinLTOEmitImportsFiles = args.hasArg(Ids: OPT_thinlto_emit_imports_files); |
| 1754 | config->thinLTOEmitIndexFiles = args.hasArg(Ids: OPT_thinlto_emit_index_files) || |
| 1755 | args.hasArg(Ids: OPT_thinlto_index_only) || |
| 1756 | args.hasArg(Ids: OPT_thinlto_index_only_eq); |
| 1757 | config->thinLTOIndexOnly = args.hasArg(Ids: OPT_thinlto_index_only) || |
| 1758 | args.hasArg(Ids: OPT_thinlto_index_only_eq); |
| 1759 | config->thinLTOIndexOnlyArg = args.getLastArgValue(Id: OPT_thinlto_index_only_eq); |
| 1760 | config->thinLTOObjectSuffixReplace = |
| 1761 | getOldNewOptions(args, id: OPT_thinlto_object_suffix_replace_eq); |
| 1762 | std::tie(args&: config->thinLTOPrefixReplaceOld, args&: config->thinLTOPrefixReplaceNew, |
| 1763 | args&: config->thinLTOPrefixReplaceNativeObject) = |
| 1764 | getOldNewOptionsExtra(args, id: OPT_thinlto_prefix_replace_eq); |
| 1765 | if (config->thinLTOEmitIndexFiles && !config->thinLTOIndexOnly) { |
| 1766 | if (args.hasArg(Ids: OPT_thinlto_object_suffix_replace_eq)) |
| 1767 | error(msg: "--thinlto-object-suffix-replace is not supported with " |
| 1768 | "--thinlto-emit-index-files" ); |
| 1769 | else if (args.hasArg(Ids: OPT_thinlto_prefix_replace_eq)) |
| 1770 | error(msg: "--thinlto-prefix-replace is not supported with " |
| 1771 | "--thinlto-emit-index-files" ); |
| 1772 | } |
| 1773 | if (!config->thinLTOPrefixReplaceNativeObject.empty() && |
| 1774 | config->thinLTOIndexOnlyArg.empty()) { |
| 1775 | error(msg: "--thinlto-prefix-replace=old_dir;new_dir;obj_dir must be used with " |
| 1776 | "--thinlto-index-only=" ); |
| 1777 | } |
| 1778 | config->warnDuplicateRpath = |
| 1779 | args.hasFlag(Pos: OPT_warn_duplicate_rpath, Neg: OPT_no_warn_duplicate_rpath, Default: true); |
| 1780 | config->runtimePaths = getRuntimePaths(args); |
| 1781 | config->allowableClients = getAllowableClients(args); |
| 1782 | config->allLoad = args.hasFlag(Pos: OPT_all_load, Neg: OPT_noall_load, Default: false); |
| 1783 | config->archMultiple = args.hasArg(Ids: OPT_arch_multiple); |
| 1784 | config->applicationExtension = args.hasFlag( |
| 1785 | Pos: OPT_application_extension, Neg: OPT_no_application_extension, Default: false); |
| 1786 | config->exportDynamic = args.hasArg(Ids: OPT_export_dynamic); |
| 1787 | config->forceLoadObjC = args.hasArg(Ids: OPT_ObjC); |
| 1788 | config->forceLoadSwift = args.hasArg(Ids: OPT_force_load_swift_libs); |
| 1789 | config->deadStripDylibs = args.hasArg(Ids: OPT_dead_strip_dylibs); |
| 1790 | config->demangle = args.hasArg(Ids: OPT_demangle); |
| 1791 | config->implicitDylibs = !args.hasArg(Ids: OPT_no_implicit_dylibs); |
| 1792 | config->emitFunctionStarts = |
| 1793 | args.hasFlag(Pos: OPT_function_starts, Neg: OPT_no_function_starts, Default: true); |
| 1794 | config->emitDataInCodeInfo = |
| 1795 | args.hasFlag(Pos: OPT_data_in_code_info, Neg: OPT_no_data_in_code_info, Default: true); |
| 1796 | config->emitChainedFixups = shouldEmitChainedFixups(args); |
| 1797 | config->emitInitOffsets = |
| 1798 | config->emitChainedFixups || args.hasArg(Ids: OPT_init_offsets); |
| 1799 | config->emitRelativeMethodLists = shouldEmitRelativeMethodLists(args); |
| 1800 | config->icfLevel = getICFLevel(args); |
| 1801 | config->keepICFStabs = args.hasArg(Ids: OPT_keep_icf_stabs); |
| 1802 | config->dedupStrings = |
| 1803 | args.hasFlag(Pos: OPT_deduplicate_strings, Neg: OPT_no_deduplicate_strings, Default: true); |
| 1804 | config->dedupSymbolStrings = !args.hasArg(Ids: OPT_no_deduplicate_symbol_strings); |
| 1805 | config->deadStripDuplicates = args.hasArg(Ids: OPT_dead_strip_duplicates); |
| 1806 | config->warnDylibInstallName = args.hasFlag( |
| 1807 | Pos: OPT_warn_dylib_install_name, Neg: OPT_no_warn_dylib_install_name, Default: false); |
| 1808 | config->ignoreOptimizationHints = args.hasArg(Ids: OPT_ignore_optimization_hints); |
| 1809 | config->callGraphProfileSort = args.hasFlag( |
| 1810 | Pos: OPT_call_graph_profile_sort, Neg: OPT_no_call_graph_profile_sort, Default: true); |
| 1811 | config->printSymbolOrder = args.getLastArgValue(Id: OPT_print_symbol_order_eq); |
| 1812 | config->forceExactCpuSubtypeMatch = |
| 1813 | getenv(name: "LD_DYLIB_CPU_SUBTYPES_MUST_MATCH" ); |
| 1814 | config->objcStubsMode = getObjCStubsMode(args); |
| 1815 | config->ignoreAutoLink = args.hasArg(Ids: OPT_ignore_auto_link); |
| 1816 | for (const Arg *arg : args.filtered(Ids: OPT_ignore_auto_link_option)) |
| 1817 | config->ignoreAutoLinkOptions.insert(key: arg->getValue()); |
| 1818 | config->strictAutoLink = args.hasArg(Ids: OPT_strict_auto_link); |
| 1819 | config->ltoDebugPassManager = args.hasArg(Ids: OPT_lto_debug_pass_manager); |
| 1820 | config->codegenDataGeneratePath = |
| 1821 | args.getLastArgValue(Id: OPT_codegen_data_generate_path); |
| 1822 | config->csProfileGenerate = args.hasArg(Ids: OPT_cs_profile_generate); |
| 1823 | config->csProfilePath = args.getLastArgValue(Id: OPT_cs_profile_path); |
| 1824 | config->pgoWarnMismatch = |
| 1825 | args.hasFlag(Pos: OPT_pgo_warn_mismatch, Neg: OPT_no_pgo_warn_mismatch, Default: true); |
| 1826 | config->warnThinArchiveMissingMembers = |
| 1827 | args.hasFlag(Pos: OPT_warn_thin_archive_missing_members, |
| 1828 | Neg: OPT_no_warn_thin_archive_missing_members, Default: true); |
| 1829 | config->generateUuid = !args.hasArg(Ids: OPT_no_uuid); |
| 1830 | config->disableVerify = args.hasArg(Ids: OPT_disable_verify); |
| 1831 | |
| 1832 | auto IncompatWithCGSort = [&](StringRef firstArgStr) { |
| 1833 | // Throw an error only if --call-graph-profile-sort is explicitly specified |
| 1834 | if (config->callGraphProfileSort) |
| 1835 | if (const Arg *arg = args.getLastArgNoClaim(Ids: OPT_call_graph_profile_sort)) |
| 1836 | error(msg: firstArgStr + " is incompatible with " + arg->getSpelling()); |
| 1837 | }; |
| 1838 | if (args.hasArg(Ids: OPT_irpgo_profile_sort) || |
| 1839 | args.hasArg(Ids: OPT_irpgo_profile_sort_eq)) |
| 1840 | warn(msg: "--irpgo-profile-sort is deprecated. Please use " |
| 1841 | "--bp-startup-sort=function" ); |
| 1842 | if (const Arg *arg = args.getLastArg(Ids: OPT_irpgo_profile)) |
| 1843 | config->irpgoProfilePath = arg->getValue(); |
| 1844 | |
| 1845 | if (const Arg *arg = args.getLastArg(Ids: OPT_irpgo_profile_sort)) { |
| 1846 | config->irpgoProfilePath = arg->getValue(); |
| 1847 | config->bpStartupFunctionSort = true; |
| 1848 | IncompatWithCGSort(arg->getSpelling()); |
| 1849 | } |
| 1850 | config->bpCompressionSortStartupFunctions = |
| 1851 | args.hasFlag(Pos: OPT_bp_compression_sort_startup_functions, |
| 1852 | Neg: OPT_no_bp_compression_sort_startup_functions, Default: false); |
| 1853 | if (const Arg *arg = args.getLastArg(Ids: OPT_bp_startup_sort)) { |
| 1854 | StringRef startupSortStr = arg->getValue(); |
| 1855 | if (startupSortStr == "function" ) { |
| 1856 | config->bpStartupFunctionSort = true; |
| 1857 | } else if (startupSortStr != "none" ) { |
| 1858 | error(msg: "unknown value `" + startupSortStr + "` for " + arg->getSpelling()); |
| 1859 | } |
| 1860 | if (startupSortStr != "none" ) |
| 1861 | IncompatWithCGSort(arg->getSpelling()); |
| 1862 | } |
| 1863 | if (!config->bpStartupFunctionSort && |
| 1864 | config->bpCompressionSortStartupFunctions) |
| 1865 | error(msg: "--bp-compression-sort-startup-functions must be used with " |
| 1866 | "--bp-startup-sort=function" ); |
| 1867 | if (config->irpgoProfilePath.empty() && config->bpStartupFunctionSort) |
| 1868 | error(msg: "--bp-startup-sort=function must be used with " |
| 1869 | "--irpgo-profile" ); |
| 1870 | if (const Arg *arg = args.getLastArg(Ids: OPT_bp_compression_sort)) { |
| 1871 | StringRef compressionSortStr = arg->getValue(); |
| 1872 | if (compressionSortStr == "function" ) { |
| 1873 | config->bpFunctionOrderForCompression = true; |
| 1874 | } else if (compressionSortStr == "data" ) { |
| 1875 | config->bpDataOrderForCompression = true; |
| 1876 | } else if (compressionSortStr == "both" ) { |
| 1877 | config->bpFunctionOrderForCompression = true; |
| 1878 | config->bpDataOrderForCompression = true; |
| 1879 | } else if (compressionSortStr != "none" ) { |
| 1880 | error(msg: "unknown value `" + compressionSortStr + "` for " + |
| 1881 | arg->getSpelling()); |
| 1882 | } |
| 1883 | if (compressionSortStr != "none" ) |
| 1884 | IncompatWithCGSort(arg->getSpelling()); |
| 1885 | } |
| 1886 | config->bpVerboseSectionOrderer = args.hasArg(Ids: OPT_verbose_bp_section_orderer); |
| 1887 | |
| 1888 | for (const Arg *arg : args.filtered(Ids: OPT_alias)) { |
| 1889 | config->aliasedSymbols.push_back( |
| 1890 | x: std::make_pair(x: arg->getValue(N: 0), y: arg->getValue(N: 1))); |
| 1891 | } |
| 1892 | |
| 1893 | if (const char *zero = getenv(name: "ZERO_AR_DATE" )) |
| 1894 | config->zeroModTime = strcmp(s1: zero, s2: "0" ) != 0; |
| 1895 | if (args.getLastArg(Ids: OPT_reproducible)) |
| 1896 | config->zeroModTime = true; |
| 1897 | |
| 1898 | std::array<PlatformType, 4> encryptablePlatforms{ |
| 1899 | PLATFORM_IOS, PLATFORM_WATCHOS, PLATFORM_TVOS, PLATFORM_XROS}; |
| 1900 | config->emitEncryptionInfo = |
| 1901 | args.hasFlag(Pos: OPT_encryptable, Neg: OPT_no_encryption, |
| 1902 | Default: is_contained(Range&: encryptablePlatforms, Element: config->platform())); |
| 1903 | |
| 1904 | if (const Arg *arg = args.getLastArg(Ids: OPT_install_name)) { |
| 1905 | if (config->warnDylibInstallName && config->outputType != MH_DYLIB) |
| 1906 | warn( |
| 1907 | msg: arg->getAsString(Args: args) + |
| 1908 | ": ignored, only has effect with -dylib [--warn-dylib-install-name]" ); |
| 1909 | else |
| 1910 | config->installName = arg->getValue(); |
| 1911 | } else if (config->outputType == MH_DYLIB) { |
| 1912 | config->installName = config->finalOutput; |
| 1913 | } |
| 1914 | |
| 1915 | auto getClientName = [&]() { |
| 1916 | StringRef cn = path::filename(path: config->finalOutput); |
| 1917 | cn.consume_front(Prefix: "lib" ); |
| 1918 | auto firstDotOrUnderscore = cn.find_first_of(Chars: "._" ); |
| 1919 | cn = cn.take_front(N: firstDotOrUnderscore); |
| 1920 | return cn; |
| 1921 | }; |
| 1922 | config->clientName = args.getLastArgValue(Id: OPT_client_name, Default: getClientName()); |
| 1923 | |
| 1924 | if (args.hasArg(Ids: OPT_mark_dead_strippable_dylib)) { |
| 1925 | if (config->outputType != MH_DYLIB) |
| 1926 | warn(msg: "-mark_dead_strippable_dylib: ignored, only has effect with -dylib" ); |
| 1927 | else |
| 1928 | config->markDeadStrippableDylib = true; |
| 1929 | } |
| 1930 | |
| 1931 | if (const Arg *arg = args.getLastArg(Ids: OPT_static, Ids: OPT_dynamic)) |
| 1932 | config->staticLink = (arg->getOption().getID() == OPT_static); |
| 1933 | |
| 1934 | if (const Arg *arg = |
| 1935 | args.getLastArg(Ids: OPT_flat_namespace, Ids: OPT_twolevel_namespace)) |
| 1936 | config->namespaceKind = arg->getOption().getID() == OPT_twolevel_namespace |
| 1937 | ? NamespaceKind::twolevel |
| 1938 | : NamespaceKind::flat; |
| 1939 | |
| 1940 | config->undefinedSymbolTreatment = getUndefinedSymbolTreatment(args); |
| 1941 | |
| 1942 | if (config->outputType == MH_EXECUTE) |
| 1943 | config->entry = symtab->addUndefined(name: args.getLastArgValue(Id: OPT_e, Default: "_main" ), |
| 1944 | /*file=*/nullptr, |
| 1945 | /*isWeakRef=*/false); |
| 1946 | |
| 1947 | config->librarySearchPaths = |
| 1948 | getLibrarySearchPaths(args, roots: config->systemLibraryRoots); |
| 1949 | config->frameworkSearchPaths = |
| 1950 | getFrameworkSearchPaths(args, roots: config->systemLibraryRoots); |
| 1951 | if (const Arg *arg = |
| 1952 | args.getLastArg(Ids: OPT_search_paths_first, Ids: OPT_search_dylibs_first)) |
| 1953 | config->searchDylibsFirst = |
| 1954 | arg->getOption().getID() == OPT_search_dylibs_first; |
| 1955 | |
| 1956 | config->dylibCompatibilityVersion = |
| 1957 | parseDylibVersion(args, id: OPT_compatibility_version); |
| 1958 | config->dylibCurrentVersion = parseDylibVersion(args, id: OPT_current_version); |
| 1959 | |
| 1960 | config->dataConst = |
| 1961 | args.hasFlag(Pos: OPT_data_const, Neg: OPT_no_data_const, Default: dataConstDefault(args)); |
| 1962 | // Populate config->sectionRenameMap with builtin default renames. |
| 1963 | // Options -rename_section and -rename_segment are able to override. |
| 1964 | initializeSectionRenameMap(); |
| 1965 | // Reject every special character except '.' and '$' |
| 1966 | // TODO(gkm): verify that this is the proper set of invalid chars |
| 1967 | StringRef invalidNameChars("!\"#%&'()*+,-/:;<=>?@[\\]^`{|}~" ); |
| 1968 | auto validName = [invalidNameChars](StringRef s) { |
| 1969 | if (s.find_first_of(Chars: invalidNameChars) != StringRef::npos) |
| 1970 | error(msg: "invalid name for segment or section: " + s); |
| 1971 | return s; |
| 1972 | }; |
| 1973 | for (const Arg *arg : args.filtered(Ids: OPT_rename_section)) { |
| 1974 | config->sectionRenameMap[{validName(arg->getValue(N: 0)), |
| 1975 | validName(arg->getValue(N: 1))}] = { |
| 1976 | validName(arg->getValue(N: 2)), validName(arg->getValue(N: 3))}; |
| 1977 | } |
| 1978 | for (const Arg *arg : args.filtered(Ids: OPT_rename_segment)) { |
| 1979 | config->segmentRenameMap[validName(arg->getValue(N: 0))] = |
| 1980 | validName(arg->getValue(N: 1)); |
| 1981 | } |
| 1982 | |
| 1983 | config->sectionAlignments = parseSectAlign(args); |
| 1984 | |
| 1985 | for (const Arg *arg : args.filtered(Ids: OPT_segprot)) { |
| 1986 | StringRef segName = arg->getValue(N: 0); |
| 1987 | uint32_t maxProt = parseProtection(protStr: arg->getValue(N: 1)); |
| 1988 | uint32_t initProt = parseProtection(protStr: arg->getValue(N: 2)); |
| 1989 | |
| 1990 | // FIXME: Check if this works on more platforms. |
| 1991 | bool allowsDifferentInitAndMaxProt = |
| 1992 | config->platform() == PLATFORM_MACOS || |
| 1993 | config->platform() == PLATFORM_MACCATALYST; |
| 1994 | if (allowsDifferentInitAndMaxProt) { |
| 1995 | if (initProt > maxProt) |
| 1996 | error(msg: "invalid argument '" + arg->getAsString(Args: args) + |
| 1997 | "': init must not be more permissive than max" ); |
| 1998 | } else { |
| 1999 | if (maxProt != initProt && config->arch() != AK_i386) |
| 2000 | error(msg: "invalid argument '" + arg->getAsString(Args: args) + |
| 2001 | "': max and init must be the same for non-macOS non-i386 archs" ); |
| 2002 | } |
| 2003 | |
| 2004 | if (segName == segment_names::linkEdit) |
| 2005 | error(msg: "-segprot cannot be used to change __LINKEDIT's protections" ); |
| 2006 | config->segmentProtections.push_back(x: {.name: segName, .maxProt: maxProt, .initProt: initProt}); |
| 2007 | } |
| 2008 | |
| 2009 | config->hasExplicitExports = |
| 2010 | args.hasArg(Ids: OPT_no_exported_symbols) || |
| 2011 | args.hasArgNoClaim(Ids: OPT_exported_symbol, Ids: OPT_exported_symbols_list); |
| 2012 | handleSymbolPatterns(args, symbolPatterns&: config->exportedSymbols, singleOptionCode: OPT_exported_symbol, |
| 2013 | listFileOptionCode: OPT_exported_symbols_list); |
| 2014 | handleSymbolPatterns(args, symbolPatterns&: config->unexportedSymbols, singleOptionCode: OPT_unexported_symbol, |
| 2015 | listFileOptionCode: OPT_unexported_symbols_list); |
| 2016 | if (config->hasExplicitExports && !config->unexportedSymbols.empty()) |
| 2017 | error(msg: "cannot use both -exported_symbol* and -unexported_symbol* options" ); |
| 2018 | |
| 2019 | if (args.hasArg(Ids: OPT_no_exported_symbols) && !config->exportedSymbols.empty()) |
| 2020 | error(msg: "cannot use both -exported_symbol* and -no_exported_symbols options" ); |
| 2021 | |
| 2022 | // Imitating LD64's: |
| 2023 | // -non_global_symbols_no_strip_list and -non_global_symbols_strip_list can't |
| 2024 | // both be present. |
| 2025 | // But -x can be used with either of these two, in which case, the last arg |
| 2026 | // takes effect. |
| 2027 | // (TODO: This is kind of confusing - considering disallowing using them |
| 2028 | // together for a more straightforward behaviour) |
| 2029 | { |
| 2030 | bool includeLocal = false; |
| 2031 | bool excludeLocal = false; |
| 2032 | for (const Arg *arg : |
| 2033 | args.filtered(Ids: OPT_x, Ids: OPT_non_global_symbols_no_strip_list, |
| 2034 | Ids: OPT_non_global_symbols_strip_list)) { |
| 2035 | switch (arg->getOption().getID()) { |
| 2036 | case OPT_x: |
| 2037 | config->localSymbolsPresence = SymtabPresence::None; |
| 2038 | break; |
| 2039 | case OPT_non_global_symbols_no_strip_list: |
| 2040 | if (excludeLocal) { |
| 2041 | error(msg: "cannot use both -non_global_symbols_no_strip_list and " |
| 2042 | "-non_global_symbols_strip_list" ); |
| 2043 | } else { |
| 2044 | includeLocal = true; |
| 2045 | config->localSymbolsPresence = SymtabPresence::SelectivelyIncluded; |
| 2046 | parseSymbolPatternsFile(arg, symbolPatterns&: config->localSymbolPatterns); |
| 2047 | } |
| 2048 | break; |
| 2049 | case OPT_non_global_symbols_strip_list: |
| 2050 | if (includeLocal) { |
| 2051 | error(msg: "cannot use both -non_global_symbols_no_strip_list and " |
| 2052 | "-non_global_symbols_strip_list" ); |
| 2053 | } else { |
| 2054 | excludeLocal = true; |
| 2055 | config->localSymbolsPresence = SymtabPresence::SelectivelyExcluded; |
| 2056 | parseSymbolPatternsFile(arg, symbolPatterns&: config->localSymbolPatterns); |
| 2057 | } |
| 2058 | break; |
| 2059 | default: |
| 2060 | llvm_unreachable("unexpected option" ); |
| 2061 | } |
| 2062 | } |
| 2063 | } |
| 2064 | // Explicitly-exported literal symbols must be defined, but might |
| 2065 | // languish in an archive if unreferenced elsewhere or if they are in the |
| 2066 | // non-global strip list. Light a fire under those lazy symbols! |
| 2067 | for (const CachedHashStringRef &cachedName : config->exportedSymbols.literals) |
| 2068 | symtab->addUndefined(name: cachedName.val(), /*file=*/nullptr, |
| 2069 | /*isWeakRef=*/false); |
| 2070 | |
| 2071 | for (const Arg *arg : args.filtered(Ids: OPT_why_live)) |
| 2072 | config->whyLive.insert(symbolName: arg->getValue()); |
| 2073 | if (!config->whyLive.empty() && !config->deadStrip) |
| 2074 | warn(msg: "-why_live has no effect without -dead_strip, ignoring" ); |
| 2075 | |
| 2076 | config->saveTemps = args.hasArg(Ids: OPT_save_temps); |
| 2077 | |
| 2078 | config->adhocCodesign = args.hasFlag( |
| 2079 | Pos: OPT_adhoc_codesign, Neg: OPT_no_adhoc_codesign, |
| 2080 | Default: shouldAdhocSignByDefault(arch: config->arch(), platform: config->platform())); |
| 2081 | |
| 2082 | if (args.hasArg(Ids: OPT_v)) { |
| 2083 | message(msg: getLLDVersion(), s&: ctx->e.errs()); |
| 2084 | message(msg: StringRef("Library search paths:" ) + |
| 2085 | (config->librarySearchPaths.empty() |
| 2086 | ? "" |
| 2087 | : "\n\t" + join(R&: config->librarySearchPaths, Separator: "\n\t" )), |
| 2088 | s&: ctx->e.errs()); |
| 2089 | message(msg: StringRef("Framework search paths:" ) + |
| 2090 | (config->frameworkSearchPaths.empty() |
| 2091 | ? "" |
| 2092 | : "\n\t" + join(R&: config->frameworkSearchPaths, Separator: "\n\t" )), |
| 2093 | s&: ctx->e.errs()); |
| 2094 | } |
| 2095 | |
| 2096 | config->progName = argsArr[0]; |
| 2097 | |
| 2098 | config->timeTraceEnabled = args.hasArg(Ids: OPT_time_trace_eq); |
| 2099 | config->timeTraceGranularity = |
| 2100 | args::getInteger(args, key: OPT_time_trace_granularity_eq, Default: 500); |
| 2101 | |
| 2102 | // Initialize time trace profiler. |
| 2103 | if (config->timeTraceEnabled) |
| 2104 | timeTraceProfilerInitialize(TimeTraceGranularity: config->timeTraceGranularity, ProcName: config->progName); |
| 2105 | |
| 2106 | { |
| 2107 | TimeTraceScope timeScope("ExecuteLinker" ); |
| 2108 | |
| 2109 | initLLVM(); // must be run before any call to addFile() |
| 2110 | createFiles(args); |
| 2111 | |
| 2112 | // Now that all dylibs have been loaded, search for those that should be |
| 2113 | // re-exported. |
| 2114 | { |
| 2115 | auto reexportHandler = [](const Arg *arg, |
| 2116 | const std::vector<StringRef> &extensions) { |
| 2117 | config->hasReexports = true; |
| 2118 | StringRef searchName = arg->getValue(); |
| 2119 | if (!markReexport(searchName, extensions)) |
| 2120 | error(msg: arg->getSpelling() + " " + searchName + |
| 2121 | " does not match a supplied dylib" ); |
| 2122 | }; |
| 2123 | std::vector<StringRef> extensions = {".tbd" }; |
| 2124 | for (const Arg *arg : args.filtered(Ids: OPT_sub_umbrella)) |
| 2125 | reexportHandler(arg, extensions); |
| 2126 | |
| 2127 | extensions.push_back(x: ".dylib" ); |
| 2128 | for (const Arg *arg : args.filtered(Ids: OPT_sub_library)) |
| 2129 | reexportHandler(arg, extensions); |
| 2130 | } |
| 2131 | |
| 2132 | cl::ResetAllOptionOccurrences(); |
| 2133 | |
| 2134 | // Parse LTO options. |
| 2135 | if (const Arg *arg = args.getLastArg(Ids: OPT_mcpu)) |
| 2136 | parseClangOption(opt: saver().save(S: "-mcpu=" + StringRef(arg->getValue())), |
| 2137 | msg: arg->getSpelling()); |
| 2138 | |
| 2139 | for (const Arg *arg : args.filtered(Ids: OPT_mllvm)) { |
| 2140 | parseClangOption(opt: arg->getValue(), msg: arg->getSpelling()); |
| 2141 | config->mllvmOpts.emplace_back(Args: arg->getValue()); |
| 2142 | } |
| 2143 | |
| 2144 | config->passPlugins = args::getStrings(args, id: OPT_load_pass_plugins); |
| 2145 | |
| 2146 | createSyntheticSections(); |
| 2147 | createSyntheticSymbols(); |
| 2148 | addSynthenticMethnames(); |
| 2149 | |
| 2150 | createAliases(); |
| 2151 | // If we are in "explicit exports" mode, hide everything that isn't |
| 2152 | // explicitly exported. Do this before running LTO so that LTO can better |
| 2153 | // optimize. |
| 2154 | handleExplicitExports(); |
| 2155 | |
| 2156 | bool didCompileBitcodeFiles = compileBitcodeFiles(); |
| 2157 | |
| 2158 | resolveLCLinkerOptions(); |
| 2159 | |
| 2160 | // If --thinlto-index-only is given, we should create only "index |
| 2161 | // files" and not object files. Index file creation is already done |
| 2162 | // in compileBitcodeFiles, so we are done if that's the case. |
| 2163 | if (config->thinLTOIndexOnly) |
| 2164 | return errorCount() == 0; |
| 2165 | |
| 2166 | // LTO may emit a non-hidden (extern) object file symbol even if the |
| 2167 | // corresponding bitcode symbol is hidden. In particular, this happens for |
| 2168 | // cross-module references to hidden symbols under ThinLTO. Thus, if we |
| 2169 | // compiled any bitcode files, we must redo the symbol hiding. |
| 2170 | if (didCompileBitcodeFiles) |
| 2171 | handleExplicitExports(); |
| 2172 | replaceCommonSymbols(); |
| 2173 | |
| 2174 | StringRef orderFile = args.getLastArgValue(Id: OPT_order_file); |
| 2175 | if (!orderFile.empty()) |
| 2176 | priorityBuilder.parseOrderFile(path: orderFile); |
| 2177 | |
| 2178 | referenceStubBinder(); |
| 2179 | |
| 2180 | // FIXME: should terminate the link early based on errors encountered so |
| 2181 | // far? |
| 2182 | |
| 2183 | for (const Arg *arg : args.filtered(Ids: OPT_sectcreate)) { |
| 2184 | StringRef segName = arg->getValue(N: 0); |
| 2185 | StringRef sectName = arg->getValue(N: 1); |
| 2186 | StringRef fileName = arg->getValue(N: 2); |
| 2187 | std::optional<MemoryBufferRef> buffer = readFile(path: fileName); |
| 2188 | if (buffer) |
| 2189 | inputFiles.insert(X: make<OpaqueFile>(args&: *buffer, args&: segName, args&: sectName)); |
| 2190 | } |
| 2191 | |
| 2192 | for (const Arg *arg : args.filtered(Ids: OPT_add_empty_section)) { |
| 2193 | StringRef segName = arg->getValue(N: 0); |
| 2194 | StringRef sectName = arg->getValue(N: 1); |
| 2195 | inputFiles.insert(X: make<OpaqueFile>(args: MemoryBufferRef(), args&: segName, args&: sectName)); |
| 2196 | } |
| 2197 | |
| 2198 | gatherInputSections(); |
| 2199 | |
| 2200 | if (!config->codegenDataGeneratePath.empty()) |
| 2201 | codegenDataGenerate(); |
| 2202 | |
| 2203 | if (config->callGraphProfileSort) |
| 2204 | priorityBuilder.extractCallGraphProfile(); |
| 2205 | |
| 2206 | if (config->deadStrip) |
| 2207 | markLive(); |
| 2208 | |
| 2209 | // Ensure that no symbols point inside __mod_init_func sections if they are |
| 2210 | // removed due to -init_offsets. This must run after dead stripping. |
| 2211 | if (config->emitInitOffsets) |
| 2212 | eraseInitializerSymbols(); |
| 2213 | |
| 2214 | // Categories are not subject to dead-strip. The __objc_catlist section is |
| 2215 | // marked as NO_DEAD_STRIP and that propagates into all category data. |
| 2216 | if (args.hasArg(Ids: OPT_check_category_conflicts)) |
| 2217 | objc::checkCategories(); |
| 2218 | |
| 2219 | // Category merging uses "->live = false" to erase old category data, so |
| 2220 | // it has to run after dead-stripping (markLive). |
| 2221 | if (args.hasFlag(Pos: OPT_objc_category_merging, Neg: OPT_no_objc_category_merging, |
| 2222 | Default: false)) |
| 2223 | objc::mergeCategories(); |
| 2224 | |
| 2225 | // ICF assumes that all literals have been folded already, so we must run |
| 2226 | // foldIdenticalLiterals before foldIdenticalSections. |
| 2227 | foldIdenticalLiterals(); |
| 2228 | if (config->icfLevel != ICFLevel::none) { |
| 2229 | if (config->icfLevel == ICFLevel::safe || |
| 2230 | config->icfLevel == ICFLevel::safe_thunks) |
| 2231 | markAddrSigSymbols(); |
| 2232 | foldIdenticalSections(/*onlyCfStrings=*/false); |
| 2233 | } else if (config->dedupStrings) { |
| 2234 | foldIdenticalSections(/*onlyCfStrings=*/true); |
| 2235 | } |
| 2236 | |
| 2237 | // Write to an output file. |
| 2238 | if (target->wordSize == 8) |
| 2239 | writeResult<LP64>(); |
| 2240 | else |
| 2241 | writeResult<ILP32>(); |
| 2242 | |
| 2243 | depTracker->write(version: getLLDVersion(), inputs: inputFiles, output: config->outputFile); |
| 2244 | } |
| 2245 | |
| 2246 | if (config->timeTraceEnabled) { |
| 2247 | checkError(e: timeTraceProfilerWrite( |
| 2248 | PreferredFileName: args.getLastArgValue(Id: OPT_time_trace_eq).str(), FallbackFileName: config->outputFile)); |
| 2249 | |
| 2250 | timeTraceProfilerCleanup(); |
| 2251 | } |
| 2252 | |
| 2253 | if (errorCount() != 0 || config->strictAutoLink) |
| 2254 | for (const auto &warning : missingAutolinkWarnings) |
| 2255 | warn(msg: warning); |
| 2256 | |
| 2257 | return errorCount() == 0; |
| 2258 | } |
| 2259 | } // namespace macho |
| 2260 | } // namespace lld |
| 2261 | |