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