xref: /llvm-project/lld/MachO/Driver.cpp (revision c676104875f34a87051b446469cc395932bc1f13)
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/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"
55 
56 #include <algorithm>
57 
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;
65 
66 std::unique_ptr<Configuration> macho::config;
67 std::unique_ptr<DependencyTracker> macho::depTracker;
68 
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;
74 
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");
84   }
85 }
86 
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;
93 
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"});
106     }
107     return findPathCombination("lib" + name, config->librarySearchPaths,
108                                {".tbd", ".dylib", ".so", ".a"});
109   };
110 
111   std::optional<StringRef> path = doFind();
112   if (path)
113     resolvedLibraries[key] = *path;
114 
115   return path;
116 }
117 
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;
124 
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);
131 
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());
141       }
142       // Suffix lookup failed, fall through to the no-suffix case.
143     }
144 
145     if (std::optional<StringRef> path = resolveDylibPath(symlink.str()))
146       return resolvedFrameworks[key] = *path;
147   }
148   return {};
149 }
150 
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;
158   }
159   return true;
160 }
161 
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;
179         }
180       }
181     }
182     if (!found && warnIfNotDirectory(optionLetter, path))
183       paths.push_back(path);
184   }
185 
186   // `-Z` suppresses the standard "system" search paths.
187   if (args.hasArg(OPT_Z))
188     return paths;
189 
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()));
196     }
197   }
198   return paths;
199 }
200 
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;
213 }
214 
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"});
218 }
219 
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"});
225 }
226 
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);
233   };
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;
253     }
254   }
255   return CHECK(parseCachePruningPolicy(ltoPolicy), "invalid LTO cache policy");
256 }
257 
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
266 };
267 
268 struct ArchiveFileInfo {
269   ArchiveFile *file;
270   bool isCommandLineLoad;
271 };
272 
273 static DenseMap<StringRef, ArchiveFileInfo> loadedArchives;
274 
275 static void saveThinArchiveToRepro(ArchiveFile const *file) {
276   assert(tar && file->getArchive().isThin());
277 
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());
283   }
284   if (e)
285     error(toString(file) +
286           ": Archive::children failed: " + toString(std::move(e)));
287 }
288 
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;
298 
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);
310 
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");
316 
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);
320 
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;
330     }
331 
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;
351           }
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));
358           }
359         }
360         if (e)
361           error(toString(file) +
362                 ": Archive::children failed: " + toString(std::move(e)));
363       }
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);
368 
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;
384           }
385 
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)));
391         }
392         if (e)
393           error(toString(file) +
394                 ": Archive::children failed: " + toString(std::move(e)));
395       }
396     }
397 
398     file->addLazySymbols();
399     loadedArchives[path] = ArchiveFileInfo{file, isCommandLineLoad};
400     newFile = file;
401     break;
402   }
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");
427   }
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;
435         }
436       if (newFile->lazy && hasObjCSection(mbref))
437         extract(*newFile, "-ObjC");
438     }
439 
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);
445   }
446   return newFile;
447 }
448 
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;
464       }
465     }
466     return;
467   }
468   if (loadType == LoadType::LCLinkerOption) {
469     missingAutolinkWarnings.push_back(
470         saver().save("auto-linked library not found for -l" + name));
471     return;
472   }
473   error("library not found for -l" + name);
474 }
475 
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;
482 
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;
493       }
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);
502     }
503     return;
504   }
505   if (loadType == LoadType::LCLinkerOption) {
506     missingAutolinkWarnings.push_back(
507         saver().save("auto-linked framework not found for -framework " + name));
508     return;
509   }
510   error("framework not found for -framework " + name);
511 }
512 
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;
521 
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;
527   }
528   if (argv.size() != argc || offset > data.size())
529     fatal(toString(f) + ": invalid LC_LINKER_OPTION");
530 
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");
542   }
543 
544   LCLinkerOptions.append(argv);
545 }
546 
547 void macho::resolveLCLinkerOptions() {
548   while (!unprocessedLCLinkerOptions.empty()) {
549     SmallVector<StringRef> LCLinkerOptions(unprocessedLCLinkerOptions);
550     unprocessedLCLinkerOptions.clear();
551 
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");
567       }
568     }
569   }
570 }
571 
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);
579 }
580 
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;
593       }
594     }
595   }
596   return false;
597 }
598 
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();
608 }
609 
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);
617 
618   std::vector<ObjFile *> compiled = lto->compile();
619   for (ObjFile *file : compiled)
620     inputFiles.insert(file);
621 
622   return !compiled.empty();
623 }
624 
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;
636 
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);
650 
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);
658   }
659 }
660 
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};
680   }
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};
686 }
687 
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;
694 }
695 
696 static std::string lowerDash(StringRef s) {
697   return std::string(map_iterator(s.begin(), toLowerDash),
698                      map_iterator(s.end(), toLowerDash));
699 }
700 
701 struct PlatformVersion {
702   PlatformType platform = PLATFORM_UNKNOWN;
703   llvm::VersionTuple minimum;
704   llvm::VersionTuple sdk;
705 };
706 
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);
712 
713   PlatformVersion platformVersion;
714 
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;
742 }
743 
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);
751 
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];
758   }
759 
760   if (platformVersions.empty()) {
761     error("must specify -platform_version");
762     return;
763   }
764   if (platformVersions.size() > 2) {
765     error("must specify -platform_version at most twice");
766     return;
767   }
768   if (platformVersions.size() == 2) {
769     bool isZipperedCatalyst = platformVersions.count(PLATFORM_MACOS) &&
770                               platformVersions.count(PLATFORM_MACCATALYST);
771 
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");
778     }
779 
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;
789   }
790 
791   config->platformInfo = {MachO::Target(getArchitectureFromName(archName),
792                                         lastVersionInfo->platform,
793                                         lastVersionInfo->minimum),
794                           lastVersionInfo->sdk};
795 }
796 
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;
803   }
804 
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;
817   }
818 }
819 
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;
842   }
843   return treatment;
844 }
845 
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);
854 
855   if ((icfLevel == ICFLevel::safe_thunks) && (config->arch() != AK_arm64)) {
856     error("--icf=safe_thunks is only supported on arm64 targets");
857   }
858 
859   if (icfLevel == ICFLevel::unknown) {
860     warn(Twine("unknown --icf=OPTION `") + icfLevelStr +
861          "', defaulting to `none'");
862     icfLevel = ICFLevel::none;
863   }
864   return icfLevel;
865 }
866 
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;
871 
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");
878   }
879   return ObjCStubsMode::fast;
880 }
881 
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());
888   }
889 }
890 
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;
915   }
916 }
917 
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");
922 }
923 
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 {"", ""};
930 
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;
936 }
937 
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};
944 }
945 
946 static void parseClangOption(StringRef opt, const Twine &msg) {
947   std::string err;
948   raw_string_ostream os(err);
949 
950   const char *argv[] = {"lld", opt.data()};
951   if (cl::ParseCommandLineOptions(2, argv, "", &os))
952     return;
953   error(msg + ": " + StringRef(err).trim());
954 }
955 
956 static uint32_t parseDylibVersion(const ArgList &args, unsigned id) {
957   const Arg *arg = args.getLastArg(id);
958   if (!arg)
959     return 0;
960 
961   if (config->outputType != MH_DYLIB) {
962     error(arg->getAsString(args) + ": only valid with -dylib");
963     return 0;
964   }
965 
966   PackedVersion version;
967   if (!version.parse32(arg->getValue())) {
968     error(arg->getAsString(args) + ": malformed version");
969     return 0;
970   }
971 
972   return version.rawValue();
973 }
974 
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;
993     }
994   }
995   return prot;
996 }
997 
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;
1010     }
1011     if (!isPowerOf2_32(align)) {
1012       error("-sectalign: '" + StringRef(arg->getValue(2)) +
1013             "' (in base 16) not a power of two");
1014       continue;
1015     }
1016     sectAligns.push_back({segName, sectName, align});
1017   }
1018   return sectAligns;
1019 }
1020 
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;
1033   }
1034 }
1035 
1036 static bool supportsNoPie() {
1037   return !(config->arch() == AK_arm64 || config->arch() == AK_arm64e ||
1038            config->arch() == AK_arm64_32);
1039 }
1040 
1041 static bool shouldAdhocSignByDefault(Architecture arch, PlatformType platform) {
1042   if (arch != AK_arm64 && arch != AK_arm64e)
1043     return false;
1044 
1045   return platform == PLATFORM_MACOS || platform == PLATFORM_IOSSIMULATOR ||
1046          platform == PLATFORM_TVOSSIMULATOR ||
1047          platform == PLATFORM_WATCHOSSIMULATOR ||
1048          platform == PLATFORM_XROS_SIMULATOR;
1049 }
1050 
1051 template <std::size_t N>
1052 using MinVersions = std::array<std::pair<PlatformType, VersionTuple>, N>;
1053 
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;
1068 }
1069 
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)},
1078   }};
1079   if (!greaterEqMinVersion(minVersion, true))
1080     return false;
1081 
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");
1097   }
1098   return false;
1099 }
1100 
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;
1105 
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");
1110 
1111     return false;
1112   }
1113 
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");
1117 
1118     return false;
1119   }
1120 
1121   if (args.hasArg(OPT_preload)) {
1122     if (requested)
1123       error("-fixup_chains is incompatible with -preload");
1124 
1125     return false;
1126   }
1127 
1128   if (requested)
1129     return true;
1130 
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)},
1141   }};
1142   return greaterEqMinVersion(minVersion, false);
1143 }
1144 
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;
1152 
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)},
1165   }};
1166   return greaterEqMinVersion(minVersion, true);
1167 }
1168 
1169 void SymbolPatterns::clear() {
1170   literals.clear();
1171   globs.clear();
1172 }
1173 
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);
1181 }
1182 
1183 bool SymbolPatterns::matchLiteral(StringRef symbolName) const {
1184   return literals.contains(CachedHashStringRef(symbolName));
1185 }
1186 
1187 bool SymbolPatterns::matchGlob(StringRef symbolName) const {
1188   for (const GlobPattern &glob : globs)
1189     if (glob.match(symbolName))
1190       return true;
1191   return false;
1192 }
1193 
1194 bool SymbolPatterns::match(StringRef symbolName) const {
1195   return matchLiteral(symbolName) || matchGlob(symbolName);
1196 }
1197 
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;
1205   }
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);
1211   }
1212 }
1213 
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);
1222 }
1223 
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);
1235 
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;
1250       }
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;
1302     }
1303   }
1304 }
1305 
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);
1319     }
1320     if (!file->objCImageInfo.empty())
1321       in.objCImageInfo->addFile(file);
1322   }
1323 }
1324 
1325 static void codegenDataGenerate() {
1326   TimeTraceScope timeScope("Generating codegen data");
1327 
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);
1339 
1340       // Merge it to the global hash tree.
1341       globalOutlineRecord.merge(localOutlineRecord);
1342     }
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);
1349 
1350       // Merge it to the global function map.
1351       globalMergeRecord.merge(localMergeRecord);
1352     }
1353   }
1354 
1355   globalMergeRecord.finalize();
1356 
1357   CodeGenDataWriter Writer;
1358   if (!globalOutlineRecord.empty())
1359     Writer.addRecord(globalOutlineRecord);
1360   if (!globalMergeRecord.empty())
1361     Writer.addRecord(globalMergeRecord);
1362 
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());
1369 
1370   if (auto E = Writer.write(Output))
1371     error("fail to write CGData: " + toString(std::move(E)));
1372 }
1373 
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();
1382 }
1383 
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';
1391 
1392   if (data.empty())
1393     return;
1394 
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);
1399 
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);
1409 }
1410 
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;
1417 
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);
1426 }
1427 
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()));
1437       }
1438     } else {
1439       warn("undefined base symbol '" + pair.first + "' for alias '" +
1440            pair.second + "'\n");
1441     }
1442   }
1443 
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()));
1457           }
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());
1464         }
1465       }
1466     }
1467   }
1468 }
1469 
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()));
1491             }
1492           }
1493         } else {
1494           defined->privateExtern = true;
1495         }
1496       } else if (auto *dysym = dyn_cast<DylibSymbol>(sym)) {
1497         dysym->shouldReexport = config->exportedSymbols.match(sym->getName());
1498       }
1499     });
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;
1508     });
1509   }
1510 }
1511 
1512 static void eraseInitializerSymbols() {
1513   for (ConcatInputSection *isec : in.initOffsets->inputs())
1514     for (Defined *sym : isec->symbols)
1515       sym->used = false;
1516 }
1517 
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]");
1527   }
1528   return vals;
1529 }
1530 
1531 static SmallVector<StringRef, 0> getAllowableClients(opt::InputArgList &args) {
1532   SmallVector<StringRef, 0> vals;
1533   DenseSet<StringRef> seen;
1534   for (const Arg *arg : args.filtered(OPT_allowable_client)) {
1535     StringRef val = arg->getValue();
1536     if (seen.insert(val).second)
1537       vals.push_back(val);
1538   }
1539   return vals;
1540 }
1541 
1542 namespace lld {
1543 namespace macho {
1544 bool link(ArrayRef<const char *> argsArr, llvm::raw_ostream &stdoutOS,
1545           llvm::raw_ostream &stderrOS, bool exitEarly, bool disableOutput) {
1546   // This driver-specific context will be freed later by lldMain().
1547   auto *ctx = new CommonLinkerContext;
1548 
1549   ctx->e.initialize(stdoutOS, stderrOS, exitEarly, disableOutput);
1550   ctx->e.cleanupCallback = []() {
1551     resolvedFrameworks.clear();
1552     resolvedLibraries.clear();
1553     cachedReads.clear();
1554     concatOutputSections.clear();
1555     inputFiles.clear();
1556     inputSections.clear();
1557     inputSectionsOrder = 0;
1558     loadedArchives.clear();
1559     loadedObjectFrameworks.clear();
1560     missingAutolinkWarnings.clear();
1561     syntheticSections.clear();
1562     thunkMap.clear();
1563     unprocessedLCLinkerOptions.clear();
1564     ObjCSelRefsHelper::cleanup();
1565 
1566     firstTLVDataSection = nullptr;
1567     tar = nullptr;
1568     memset(&in, 0, sizeof(in));
1569 
1570     resetLoadedDylibs();
1571     resetOutputSegments();
1572     resetWriter();
1573     InputFile::resetIdCount();
1574 
1575     objc::doCleanup();
1576   };
1577 
1578   ctx->e.logName = args::getFilenameWithoutExe(argsArr[0]);
1579 
1580   MachOOptTable parser;
1581   InputArgList args = parser.parse(*ctx, argsArr.slice(1));
1582 
1583   ctx->e.errorLimitExceededMsg = "too many errors emitted, stopping now "
1584                                  "(use --error-limit=0 to see all errors)";
1585   ctx->e.errorLimit = args::getInteger(args, OPT_error_limit_eq, 20);
1586   ctx->e.verbose = args.hasArg(OPT_verbose);
1587 
1588   if (args.hasArg(OPT_help_hidden)) {
1589     parser.printHelp(*ctx, argsArr[0], /*showHidden=*/true);
1590     return true;
1591   }
1592   if (args.hasArg(OPT_help)) {
1593     parser.printHelp(*ctx, argsArr[0], /*showHidden=*/false);
1594     return true;
1595   }
1596   if (args.hasArg(OPT_version)) {
1597     message(getLLDVersion());
1598     return true;
1599   }
1600 
1601   config = std::make_unique<Configuration>();
1602   symtab = std::make_unique<SymbolTable>();
1603   config->outputType = getOutputType(args);
1604   target = createTargetInfo(args);
1605   depTracker = std::make_unique<DependencyTracker>(
1606       args.getLastArgValue(OPT_dependency_info));
1607 
1608   config->ltoo = args::getInteger(args, OPT_lto_O, 2);
1609   if (config->ltoo > 3)
1610     error("--lto-O: invalid optimization level: " + Twine(config->ltoo));
1611   unsigned ltoCgo =
1612       args::getInteger(args, OPT_lto_CGO, args::getCGOptLevel(config->ltoo));
1613   if (auto level = CodeGenOpt::getLevel(ltoCgo))
1614     config->ltoCgo = *level;
1615   else
1616     error("--lto-CGO: invalid codegen optimization level: " + Twine(ltoCgo));
1617 
1618   if (errorCount())
1619     return false;
1620 
1621   if (args.hasArg(OPT_pagezero_size)) {
1622     uint64_t pagezeroSize = args::getHex(args, OPT_pagezero_size, 0);
1623 
1624     // ld64 does something really weird. It attempts to realign the value to the
1625     // page size, but assumes the page size is 4K. This doesn't work with most
1626     // of Apple's ARM64 devices, which use a page size of 16K. This means that
1627     // it will first 4K align it by rounding down, then round up to 16K.  This
1628     // probably only happened because no one using this arg with anything other
1629     // then 0, so no one checked if it did what is what it says it does.
1630 
1631     // So we are not copying this weird behavior and doing the it in a logical
1632     // way, by always rounding down to page size.
1633     if (!isAligned(Align(target->getPageSize()), pagezeroSize)) {
1634       pagezeroSize -= pagezeroSize % target->getPageSize();
1635       warn("__PAGEZERO size is not page aligned, rounding down to 0x" +
1636            Twine::utohexstr(pagezeroSize));
1637     }
1638 
1639     target->pageZeroSize = pagezeroSize;
1640   }
1641 
1642   config->osoPrefix = args.getLastArgValue(OPT_oso_prefix);
1643   if (!config->osoPrefix.empty()) {
1644     // Expand special characters, such as ".", "..", or  "~", if present.
1645     // Note: LD64 only expands "." and not other special characters.
1646     // That seems silly to imitate so we will not try to follow it, but rather
1647     // just use real_path() to do it.
1648 
1649     // The max path length is 4096, in theory. However that seems quite long
1650     // and seems unlikely that any one would want to strip everything from the
1651     // path. Hence we've picked a reasonably large number here.
1652     SmallString<1024> expanded;
1653     if (!fs::real_path(config->osoPrefix, expanded,
1654                        /*expand_tilde=*/true)) {
1655       // Note: LD64 expands "." to be `<current_dir>/`
1656       // (ie., it has a slash suffix) whereas real_path() doesn't.
1657       // So we have to append '/' to be consistent.
1658       StringRef sep = sys::path::get_separator();
1659       // real_path removes trailing slashes as part of the normalization, but
1660       // these are meaningful for our text based stripping
1661       if (config->osoPrefix == "." || config->osoPrefix.ends_with(sep))
1662         expanded += sep;
1663       config->osoPrefix = saver().save(expanded.str());
1664     }
1665   }
1666 
1667   bool pie = args.hasFlag(OPT_pie, OPT_no_pie, true);
1668   if (!supportsNoPie() && !pie) {
1669     warn("-no_pie ignored for arm64");
1670     pie = true;
1671   }
1672 
1673   config->isPic = config->outputType == MH_DYLIB ||
1674                   config->outputType == MH_BUNDLE ||
1675                   (config->outputType == MH_EXECUTE && pie);
1676 
1677   // Must be set before any InputSections and Symbols are created.
1678   config->deadStrip = args.hasArg(OPT_dead_strip);
1679 
1680   config->systemLibraryRoots = getSystemLibraryRoots(args);
1681   if (const char *path = getReproduceOption(args)) {
1682     // Note that --reproduce is a debug option so you can ignore it
1683     // if you are trying to understand the whole picture of the code.
1684     Expected<std::unique_ptr<TarWriter>> errOrWriter =
1685         TarWriter::create(path, path::stem(path));
1686     if (errOrWriter) {
1687       tar = std::move(*errOrWriter);
1688       tar->append("response.txt", createResponseFile(args));
1689       tar->append("version.txt", getLLDVersion() + "\n");
1690     } else {
1691       error("--reproduce: " + toString(errOrWriter.takeError()));
1692     }
1693   }
1694 
1695   if (auto *arg = args.getLastArg(OPT_threads_eq)) {
1696     StringRef v(arg->getValue());
1697     unsigned threads = 0;
1698     if (!llvm::to_integer(v, threads, 0) || threads == 0)
1699       error(arg->getSpelling() + ": expected a positive integer, but got '" +
1700             arg->getValue() + "'");
1701     parallel::strategy = hardware_concurrency(threads);
1702     config->thinLTOJobs = v;
1703   }
1704   if (auto *arg = args.getLastArg(OPT_thinlto_jobs_eq))
1705     config->thinLTOJobs = arg->getValue();
1706   if (!get_threadpool_strategy(config->thinLTOJobs))
1707     error("--thinlto-jobs: invalid job count: " + config->thinLTOJobs);
1708 
1709   for (const Arg *arg : args.filtered(OPT_u)) {
1710     config->explicitUndefineds.push_back(symtab->addUndefined(
1711         arg->getValue(), /*file=*/nullptr, /*isWeakRef=*/false));
1712   }
1713 
1714   for (const Arg *arg : args.filtered(OPT_U))
1715     config->explicitDynamicLookups.insert(arg->getValue());
1716 
1717   config->mapFile = args.getLastArgValue(OPT_map);
1718   config->optimize = args::getInteger(args, OPT_O, 1);
1719   config->outputFile = args.getLastArgValue(OPT_o, "a.out");
1720   config->finalOutput =
1721       args.getLastArgValue(OPT_final_output, config->outputFile);
1722   config->astPaths = args.getAllArgValues(OPT_add_ast_path);
1723   config->headerPad = args::getHex(args, OPT_headerpad, /*Default=*/32);
1724   config->headerPadMaxInstallNames =
1725       args.hasArg(OPT_headerpad_max_install_names);
1726   config->printDylibSearch =
1727       args.hasArg(OPT_print_dylib_search) || getenv("RC_TRACE_DYLIB_SEARCHING");
1728   config->printEachFile = args.hasArg(OPT_t);
1729   config->printWhyLoad = args.hasArg(OPT_why_load);
1730   config->omitDebugInfo = args.hasArg(OPT_S);
1731   config->errorForArchMismatch = args.hasArg(OPT_arch_errors_fatal);
1732   if (const Arg *arg = args.getLastArg(OPT_bundle_loader)) {
1733     if (config->outputType != MH_BUNDLE)
1734       error("-bundle_loader can only be used with MachO bundle output");
1735     addFile(arg->getValue(), LoadType::CommandLine, /*isLazy=*/false,
1736             /*isExplicit=*/false, /*isBundleLoader=*/true);
1737   }
1738   for (auto *arg : args.filtered(OPT_dyld_env)) {
1739     StringRef envPair(arg->getValue());
1740     if (!envPair.contains('='))
1741       error("-dyld_env's argument is  malformed. Expected "
1742             "-dyld_env <ENV_VAR>=<VALUE>, got `" +
1743             envPair + "`");
1744     config->dyldEnvs.push_back(envPair);
1745   }
1746   if (!config->dyldEnvs.empty() && config->outputType != MH_EXECUTE)
1747     error("-dyld_env can only be used when creating executable output");
1748 
1749   if (const Arg *arg = args.getLastArg(OPT_umbrella)) {
1750     if (config->outputType != MH_DYLIB)
1751       warn("-umbrella used, but not creating dylib");
1752     config->umbrella = arg->getValue();
1753   }
1754   config->ltoObjPath = args.getLastArgValue(OPT_object_path_lto);
1755   config->ltoNewPmPasses = args.getLastArgValue(OPT_lto_newpm_passes);
1756   config->thinLTOCacheDir = args.getLastArgValue(OPT_cache_path_lto);
1757   config->thinLTOCachePolicy = getLTOCachePolicy(args);
1758   config->thinLTOEmitImportsFiles = args.hasArg(OPT_thinlto_emit_imports_files);
1759   config->thinLTOEmitIndexFiles = args.hasArg(OPT_thinlto_emit_index_files) ||
1760                                   args.hasArg(OPT_thinlto_index_only) ||
1761                                   args.hasArg(OPT_thinlto_index_only_eq);
1762   config->thinLTOIndexOnly = args.hasArg(OPT_thinlto_index_only) ||
1763                              args.hasArg(OPT_thinlto_index_only_eq);
1764   config->thinLTOIndexOnlyArg = args.getLastArgValue(OPT_thinlto_index_only_eq);
1765   config->thinLTOObjectSuffixReplace =
1766       getOldNewOptions(args, OPT_thinlto_object_suffix_replace_eq);
1767   std::tie(config->thinLTOPrefixReplaceOld, config->thinLTOPrefixReplaceNew,
1768            config->thinLTOPrefixReplaceNativeObject) =
1769       getOldNewOptionsExtra(args, OPT_thinlto_prefix_replace_eq);
1770   if (config->thinLTOEmitIndexFiles && !config->thinLTOIndexOnly) {
1771     if (args.hasArg(OPT_thinlto_object_suffix_replace_eq))
1772       error("--thinlto-object-suffix-replace is not supported with "
1773             "--thinlto-emit-index-files");
1774     else if (args.hasArg(OPT_thinlto_prefix_replace_eq))
1775       error("--thinlto-prefix-replace is not supported with "
1776             "--thinlto-emit-index-files");
1777   }
1778   if (!config->thinLTOPrefixReplaceNativeObject.empty() &&
1779       config->thinLTOIndexOnlyArg.empty()) {
1780     error("--thinlto-prefix-replace=old_dir;new_dir;obj_dir must be used with "
1781           "--thinlto-index-only=");
1782   }
1783   config->warnDuplicateRpath =
1784       args.hasFlag(OPT_warn_duplicate_rpath, OPT_no_warn_duplicate_rpath, true);
1785   config->runtimePaths = getRuntimePaths(args);
1786   config->allowableClients = getAllowableClients(args);
1787   config->allLoad = args.hasFlag(OPT_all_load, OPT_noall_load, false);
1788   config->archMultiple = args.hasArg(OPT_arch_multiple);
1789   config->applicationExtension = args.hasFlag(
1790       OPT_application_extension, OPT_no_application_extension, false);
1791   config->exportDynamic = args.hasArg(OPT_export_dynamic);
1792   config->forceLoadObjC = args.hasArg(OPT_ObjC);
1793   config->forceLoadSwift = args.hasArg(OPT_force_load_swift_libs);
1794   config->deadStripDylibs = args.hasArg(OPT_dead_strip_dylibs);
1795   config->demangle = args.hasArg(OPT_demangle);
1796   config->implicitDylibs = !args.hasArg(OPT_no_implicit_dylibs);
1797   config->emitFunctionStarts =
1798       args.hasFlag(OPT_function_starts, OPT_no_function_starts, true);
1799   config->emitDataInCodeInfo =
1800       args.hasFlag(OPT_data_in_code_info, OPT_no_data_in_code_info, true);
1801   config->emitChainedFixups = shouldEmitChainedFixups(args);
1802   config->emitInitOffsets =
1803       config->emitChainedFixups || args.hasArg(OPT_init_offsets);
1804   config->emitRelativeMethodLists = shouldEmitRelativeMethodLists(args);
1805   config->icfLevel = getICFLevel(args);
1806   config->keepICFStabs = args.hasArg(OPT_keep_icf_stabs);
1807   config->dedupStrings =
1808       args.hasFlag(OPT_deduplicate_strings, OPT_no_deduplicate_strings, true);
1809   config->dedupSymbolStrings = !args.hasArg(OPT_no_deduplicate_symbol_strings);
1810   config->deadStripDuplicates = args.hasArg(OPT_dead_strip_duplicates);
1811   config->warnDylibInstallName = args.hasFlag(
1812       OPT_warn_dylib_install_name, OPT_no_warn_dylib_install_name, false);
1813   config->ignoreOptimizationHints = args.hasArg(OPT_ignore_optimization_hints);
1814   config->callGraphProfileSort = args.hasFlag(
1815       OPT_call_graph_profile_sort, OPT_no_call_graph_profile_sort, true);
1816   config->printSymbolOrder = args.getLastArgValue(OPT_print_symbol_order_eq);
1817   config->forceExactCpuSubtypeMatch =
1818       getenv("LD_DYLIB_CPU_SUBTYPES_MUST_MATCH");
1819   config->objcStubsMode = getObjCStubsMode(args);
1820   config->ignoreAutoLink = args.hasArg(OPT_ignore_auto_link);
1821   for (const Arg *arg : args.filtered(OPT_ignore_auto_link_option))
1822     config->ignoreAutoLinkOptions.insert(arg->getValue());
1823   config->strictAutoLink = args.hasArg(OPT_strict_auto_link);
1824   config->ltoDebugPassManager = args.hasArg(OPT_lto_debug_pass_manager);
1825   config->codegenDataGeneratePath =
1826       args.getLastArgValue(OPT_codegen_data_generate_path);
1827   config->csProfileGenerate = args.hasArg(OPT_cs_profile_generate);
1828   config->csProfilePath = args.getLastArgValue(OPT_cs_profile_path);
1829   config->pgoWarnMismatch =
1830       args.hasFlag(OPT_pgo_warn_mismatch, OPT_no_pgo_warn_mismatch, true);
1831   config->warnThinArchiveMissingMembers =
1832       args.hasFlag(OPT_warn_thin_archive_missing_members,
1833                    OPT_no_warn_thin_archive_missing_members, true);
1834   config->generateUuid = !args.hasArg(OPT_no_uuid);
1835 
1836   auto IncompatWithCGSort = [&](StringRef firstArgStr) {
1837     // Throw an error only if --call-graph-profile-sort is explicitly specified
1838     if (config->callGraphProfileSort)
1839       if (const Arg *arg = args.getLastArgNoClaim(OPT_call_graph_profile_sort))
1840         error(firstArgStr + " is incompatible with " + arg->getSpelling());
1841   };
1842   if (args.hasArg(OPT_irpgo_profile_sort) ||
1843       args.hasArg(OPT_irpgo_profile_sort_eq))
1844     warn("--irpgo-profile-sort is deprecated. Please use "
1845          "--bp-startup-sort=function");
1846   if (const Arg *arg = args.getLastArg(OPT_irpgo_profile))
1847     config->irpgoProfilePath = arg->getValue();
1848 
1849   if (const Arg *arg = args.getLastArg(OPT_irpgo_profile_sort)) {
1850     config->irpgoProfilePath = arg->getValue();
1851     config->bpStartupFunctionSort = true;
1852     IncompatWithCGSort(arg->getSpelling());
1853   }
1854   config->bpCompressionSortStartupFunctions =
1855       args.hasFlag(OPT_bp_compression_sort_startup_functions,
1856                    OPT_no_bp_compression_sort_startup_functions, false);
1857   if (const Arg *arg = args.getLastArg(OPT_bp_startup_sort)) {
1858     StringRef startupSortStr = arg->getValue();
1859     if (startupSortStr == "function") {
1860       config->bpStartupFunctionSort = true;
1861     } else if (startupSortStr != "none") {
1862       error("unknown value `" + startupSortStr + "` for " + arg->getSpelling());
1863     }
1864     if (startupSortStr != "none")
1865       IncompatWithCGSort(arg->getSpelling());
1866   }
1867   if (!config->bpStartupFunctionSort &&
1868       config->bpCompressionSortStartupFunctions)
1869     error("--bp-compression-sort-startup-functions must be used with "
1870           "--bp-startup-sort=function");
1871   if (config->irpgoProfilePath.empty() && config->bpStartupFunctionSort)
1872     error("--bp-startup-sort=function must be used with "
1873           "--irpgo-profile");
1874   if (const Arg *arg = args.getLastArg(OPT_bp_compression_sort)) {
1875     StringRef compressionSortStr = arg->getValue();
1876     if (compressionSortStr == "function") {
1877       config->bpFunctionOrderForCompression = true;
1878     } else if (compressionSortStr == "data") {
1879       config->bpDataOrderForCompression = true;
1880     } else if (compressionSortStr == "both") {
1881       config->bpFunctionOrderForCompression = true;
1882       config->bpDataOrderForCompression = true;
1883     } else if (compressionSortStr != "none") {
1884       error("unknown value `" + compressionSortStr + "` for " +
1885             arg->getSpelling());
1886     }
1887     if (compressionSortStr != "none")
1888       IncompatWithCGSort(arg->getSpelling());
1889   }
1890   config->bpVerboseSectionOrderer = args.hasArg(OPT_verbose_bp_section_orderer);
1891 
1892   for (const Arg *arg : args.filtered(OPT_alias)) {
1893     config->aliasedSymbols.push_back(
1894         std::make_pair(arg->getValue(0), arg->getValue(1)));
1895   }
1896 
1897   if (const char *zero = getenv("ZERO_AR_DATE"))
1898     config->zeroModTime = strcmp(zero, "0") != 0;
1899   if (args.getLastArg(OPT_reproducible))
1900     config->zeroModTime = true;
1901 
1902   std::array<PlatformType, 4> encryptablePlatforms{
1903       PLATFORM_IOS, PLATFORM_WATCHOS, PLATFORM_TVOS, PLATFORM_XROS};
1904   config->emitEncryptionInfo =
1905       args.hasFlag(OPT_encryptable, OPT_no_encryption,
1906                    is_contained(encryptablePlatforms, config->platform()));
1907 
1908   if (const Arg *arg = args.getLastArg(OPT_install_name)) {
1909     if (config->warnDylibInstallName && config->outputType != MH_DYLIB)
1910       warn(
1911           arg->getAsString(args) +
1912           ": ignored, only has effect with -dylib [--warn-dylib-install-name]");
1913     else
1914       config->installName = arg->getValue();
1915   } else if (config->outputType == MH_DYLIB) {
1916     config->installName = config->finalOutput;
1917   }
1918 
1919   auto getClientName = [&]() {
1920     StringRef cn = path::filename(config->finalOutput);
1921     cn.consume_front("lib");
1922     auto firstDotOrUnderscore = cn.find_first_of("._");
1923     cn = cn.take_front(firstDotOrUnderscore);
1924     return cn;
1925   };
1926   config->clientName = args.getLastArgValue(OPT_client_name, getClientName());
1927 
1928   if (args.hasArg(OPT_mark_dead_strippable_dylib)) {
1929     if (config->outputType != MH_DYLIB)
1930       warn("-mark_dead_strippable_dylib: ignored, only has effect with -dylib");
1931     else
1932       config->markDeadStrippableDylib = true;
1933   }
1934 
1935   if (const Arg *arg = args.getLastArg(OPT_static, OPT_dynamic))
1936     config->staticLink = (arg->getOption().getID() == OPT_static);
1937 
1938   if (const Arg *arg =
1939           args.getLastArg(OPT_flat_namespace, OPT_twolevel_namespace))
1940     config->namespaceKind = arg->getOption().getID() == OPT_twolevel_namespace
1941                                 ? NamespaceKind::twolevel
1942                                 : NamespaceKind::flat;
1943 
1944   config->undefinedSymbolTreatment = getUndefinedSymbolTreatment(args);
1945 
1946   if (config->outputType == MH_EXECUTE)
1947     config->entry = symtab->addUndefined(args.getLastArgValue(OPT_e, "_main"),
1948                                          /*file=*/nullptr,
1949                                          /*isWeakRef=*/false);
1950 
1951   config->librarySearchPaths =
1952       getLibrarySearchPaths(args, config->systemLibraryRoots);
1953   config->frameworkSearchPaths =
1954       getFrameworkSearchPaths(args, config->systemLibraryRoots);
1955   if (const Arg *arg =
1956           args.getLastArg(OPT_search_paths_first, OPT_search_dylibs_first))
1957     config->searchDylibsFirst =
1958         arg->getOption().getID() == OPT_search_dylibs_first;
1959 
1960   config->dylibCompatibilityVersion =
1961       parseDylibVersion(args, OPT_compatibility_version);
1962   config->dylibCurrentVersion = parseDylibVersion(args, OPT_current_version);
1963 
1964   config->dataConst =
1965       args.hasFlag(OPT_data_const, OPT_no_data_const, dataConstDefault(args));
1966   // Populate config->sectionRenameMap with builtin default renames.
1967   // Options -rename_section and -rename_segment are able to override.
1968   initializeSectionRenameMap();
1969   // Reject every special character except '.' and '$'
1970   // TODO(gkm): verify that this is the proper set of invalid chars
1971   StringRef invalidNameChars("!\"#%&'()*+,-/:;<=>?@[\\]^`{|}~");
1972   auto validName = [invalidNameChars](StringRef s) {
1973     if (s.find_first_of(invalidNameChars) != StringRef::npos)
1974       error("invalid name for segment or section: " + s);
1975     return s;
1976   };
1977   for (const Arg *arg : args.filtered(OPT_rename_section)) {
1978     config->sectionRenameMap[{validName(arg->getValue(0)),
1979                               validName(arg->getValue(1))}] = {
1980         validName(arg->getValue(2)), validName(arg->getValue(3))};
1981   }
1982   for (const Arg *arg : args.filtered(OPT_rename_segment)) {
1983     config->segmentRenameMap[validName(arg->getValue(0))] =
1984         validName(arg->getValue(1));
1985   }
1986 
1987   config->sectionAlignments = parseSectAlign(args);
1988 
1989   for (const Arg *arg : args.filtered(OPT_segprot)) {
1990     StringRef segName = arg->getValue(0);
1991     uint32_t maxProt = parseProtection(arg->getValue(1));
1992     uint32_t initProt = parseProtection(arg->getValue(2));
1993 
1994     // FIXME: Check if this works on more platforms.
1995     bool allowsDifferentInitAndMaxProt =
1996         config->platform() == PLATFORM_MACOS ||
1997         config->platform() == PLATFORM_MACCATALYST;
1998     if (allowsDifferentInitAndMaxProt) {
1999       if (initProt > maxProt)
2000         error("invalid argument '" + arg->getAsString(args) +
2001               "': init must not be more permissive than max");
2002     } else {
2003       if (maxProt != initProt && config->arch() != AK_i386)
2004         error("invalid argument '" + arg->getAsString(args) +
2005               "': max and init must be the same for non-macOS non-i386 archs");
2006     }
2007 
2008     if (segName == segment_names::linkEdit)
2009       error("-segprot cannot be used to change __LINKEDIT's protections");
2010     config->segmentProtections.push_back({segName, maxProt, initProt});
2011   }
2012 
2013   config->hasExplicitExports =
2014       args.hasArg(OPT_no_exported_symbols) ||
2015       args.hasArgNoClaim(OPT_exported_symbol, OPT_exported_symbols_list);
2016   handleSymbolPatterns(args, config->exportedSymbols, OPT_exported_symbol,
2017                        OPT_exported_symbols_list);
2018   handleSymbolPatterns(args, config->unexportedSymbols, OPT_unexported_symbol,
2019                        OPT_unexported_symbols_list);
2020   if (config->hasExplicitExports && !config->unexportedSymbols.empty())
2021     error("cannot use both -exported_symbol* and -unexported_symbol* options");
2022 
2023   if (args.hasArg(OPT_no_exported_symbols) && !config->exportedSymbols.empty())
2024     error("cannot use both -exported_symbol* and -no_exported_symbols options");
2025 
2026   // Imitating LD64's:
2027   // -non_global_symbols_no_strip_list and -non_global_symbols_strip_list can't
2028   // both be present.
2029   // But -x can be used with either of these two, in which case, the last arg
2030   // takes effect.
2031   // (TODO: This is kind of confusing - considering disallowing using them
2032   // together for a more straightforward behaviour)
2033   {
2034     bool includeLocal = false;
2035     bool excludeLocal = false;
2036     for (const Arg *arg :
2037          args.filtered(OPT_x, OPT_non_global_symbols_no_strip_list,
2038                        OPT_non_global_symbols_strip_list)) {
2039       switch (arg->getOption().getID()) {
2040       case OPT_x:
2041         config->localSymbolsPresence = SymtabPresence::None;
2042         break;
2043       case OPT_non_global_symbols_no_strip_list:
2044         if (excludeLocal) {
2045           error("cannot use both -non_global_symbols_no_strip_list and "
2046                 "-non_global_symbols_strip_list");
2047         } else {
2048           includeLocal = true;
2049           config->localSymbolsPresence = SymtabPresence::SelectivelyIncluded;
2050           parseSymbolPatternsFile(arg, config->localSymbolPatterns);
2051         }
2052         break;
2053       case OPT_non_global_symbols_strip_list:
2054         if (includeLocal) {
2055           error("cannot use both -non_global_symbols_no_strip_list and "
2056                 "-non_global_symbols_strip_list");
2057         } else {
2058           excludeLocal = true;
2059           config->localSymbolsPresence = SymtabPresence::SelectivelyExcluded;
2060           parseSymbolPatternsFile(arg, config->localSymbolPatterns);
2061         }
2062         break;
2063       default:
2064         llvm_unreachable("unexpected option");
2065       }
2066     }
2067   }
2068   // Explicitly-exported literal symbols must be defined, but might
2069   // languish in an archive if unreferenced elsewhere or if they are in the
2070   // non-global strip list. Light a fire under those lazy symbols!
2071   for (const CachedHashStringRef &cachedName : config->exportedSymbols.literals)
2072     symtab->addUndefined(cachedName.val(), /*file=*/nullptr,
2073                          /*isWeakRef=*/false);
2074 
2075   for (const Arg *arg : args.filtered(OPT_why_live))
2076     config->whyLive.insert(arg->getValue());
2077   if (!config->whyLive.empty() && !config->deadStrip)
2078     warn("-why_live has no effect without -dead_strip, ignoring");
2079 
2080   config->saveTemps = args.hasArg(OPT_save_temps);
2081 
2082   config->adhocCodesign = args.hasFlag(
2083       OPT_adhoc_codesign, OPT_no_adhoc_codesign,
2084       shouldAdhocSignByDefault(config->arch(), config->platform()));
2085 
2086   if (args.hasArg(OPT_v)) {
2087     message(getLLDVersion(), ctx->e.errs());
2088     message(StringRef("Library search paths:") +
2089                 (config->librarySearchPaths.empty()
2090                      ? ""
2091                      : "\n\t" + join(config->librarySearchPaths, "\n\t")),
2092             ctx->e.errs());
2093     message(StringRef("Framework search paths:") +
2094                 (config->frameworkSearchPaths.empty()
2095                      ? ""
2096                      : "\n\t" + join(config->frameworkSearchPaths, "\n\t")),
2097             ctx->e.errs());
2098   }
2099 
2100   config->progName = argsArr[0];
2101 
2102   config->timeTraceEnabled = args.hasArg(OPT_time_trace_eq);
2103   config->timeTraceGranularity =
2104       args::getInteger(args, OPT_time_trace_granularity_eq, 500);
2105 
2106   // Initialize time trace profiler.
2107   if (config->timeTraceEnabled)
2108     timeTraceProfilerInitialize(config->timeTraceGranularity, config->progName);
2109 
2110   {
2111     TimeTraceScope timeScope("ExecuteLinker");
2112 
2113     initLLVM(); // must be run before any call to addFile()
2114     createFiles(args);
2115 
2116     // Now that all dylibs have been loaded, search for those that should be
2117     // re-exported.
2118     {
2119       auto reexportHandler = [](const Arg *arg,
2120                                 const std::vector<StringRef> &extensions) {
2121         config->hasReexports = true;
2122         StringRef searchName = arg->getValue();
2123         if (!markReexport(searchName, extensions))
2124           error(arg->getSpelling() + " " + searchName +
2125                 " does not match a supplied dylib");
2126       };
2127       std::vector<StringRef> extensions = {".tbd"};
2128       for (const Arg *arg : args.filtered(OPT_sub_umbrella))
2129         reexportHandler(arg, extensions);
2130 
2131       extensions.push_back(".dylib");
2132       for (const Arg *arg : args.filtered(OPT_sub_library))
2133         reexportHandler(arg, extensions);
2134     }
2135 
2136     cl::ResetAllOptionOccurrences();
2137 
2138     // Parse LTO options.
2139     if (const Arg *arg = args.getLastArg(OPT_mcpu))
2140       parseClangOption(saver().save("-mcpu=" + StringRef(arg->getValue())),
2141                        arg->getSpelling());
2142 
2143     for (const Arg *arg : args.filtered(OPT_mllvm)) {
2144       parseClangOption(arg->getValue(), arg->getSpelling());
2145       config->mllvmOpts.emplace_back(arg->getValue());
2146     }
2147 
2148     config->passPlugins = args::getStrings(args, OPT_load_pass_plugins);
2149 
2150     createSyntheticSections();
2151     createSyntheticSymbols();
2152     addSynthenticMethnames();
2153 
2154     createAliases();
2155     // If we are in "explicit exports" mode, hide everything that isn't
2156     // explicitly exported. Do this before running LTO so that LTO can better
2157     // optimize.
2158     handleExplicitExports();
2159 
2160     bool didCompileBitcodeFiles = compileBitcodeFiles();
2161 
2162     resolveLCLinkerOptions();
2163 
2164     // If --thinlto-index-only is given, we should create only "index
2165     // files" and not object files. Index file creation is already done
2166     // in compileBitcodeFiles, so we are done if that's the case.
2167     if (config->thinLTOIndexOnly)
2168       return errorCount() == 0;
2169 
2170     // LTO may emit a non-hidden (extern) object file symbol even if the
2171     // corresponding bitcode symbol is hidden. In particular, this happens for
2172     // cross-module references to hidden symbols under ThinLTO. Thus, if we
2173     // compiled any bitcode files, we must redo the symbol hiding.
2174     if (didCompileBitcodeFiles)
2175       handleExplicitExports();
2176     replaceCommonSymbols();
2177 
2178     StringRef orderFile = args.getLastArgValue(OPT_order_file);
2179     if (!orderFile.empty())
2180       priorityBuilder.parseOrderFile(orderFile);
2181 
2182     referenceStubBinder();
2183 
2184     // FIXME: should terminate the link early based on errors encountered so
2185     // far?
2186 
2187     for (const Arg *arg : args.filtered(OPT_sectcreate)) {
2188       StringRef segName = arg->getValue(0);
2189       StringRef sectName = arg->getValue(1);
2190       StringRef fileName = arg->getValue(2);
2191       std::optional<MemoryBufferRef> buffer = readFile(fileName);
2192       if (buffer)
2193         inputFiles.insert(make<OpaqueFile>(*buffer, segName, sectName));
2194     }
2195 
2196     for (const Arg *arg : args.filtered(OPT_add_empty_section)) {
2197       StringRef segName = arg->getValue(0);
2198       StringRef sectName = arg->getValue(1);
2199       inputFiles.insert(make<OpaqueFile>(MemoryBufferRef(), segName, sectName));
2200     }
2201 
2202     gatherInputSections();
2203 
2204     if (!config->codegenDataGeneratePath.empty())
2205       codegenDataGenerate();
2206 
2207     if (config->callGraphProfileSort)
2208       priorityBuilder.extractCallGraphProfile();
2209 
2210     if (config->deadStrip)
2211       markLive();
2212 
2213     // Ensure that no symbols point inside __mod_init_func sections if they are
2214     // removed due to -init_offsets. This must run after dead stripping.
2215     if (config->emitInitOffsets)
2216       eraseInitializerSymbols();
2217 
2218     // Categories are not subject to dead-strip. The __objc_catlist section is
2219     // marked as NO_DEAD_STRIP and that propagates into all category data.
2220     if (args.hasArg(OPT_check_category_conflicts))
2221       objc::checkCategories();
2222 
2223     // Category merging uses "->live = false" to erase old category data, so
2224     // it has to run after dead-stripping (markLive).
2225     if (args.hasFlag(OPT_objc_category_merging, OPT_no_objc_category_merging,
2226                      false))
2227       objc::mergeCategories();
2228 
2229     // ICF assumes that all literals have been folded already, so we must run
2230     // foldIdenticalLiterals before foldIdenticalSections.
2231     foldIdenticalLiterals();
2232     if (config->icfLevel != ICFLevel::none) {
2233       if (config->icfLevel == ICFLevel::safe ||
2234           config->icfLevel == ICFLevel::safe_thunks)
2235         markAddrSigSymbols();
2236       foldIdenticalSections(/*onlyCfStrings=*/false);
2237     } else if (config->dedupStrings) {
2238       foldIdenticalSections(/*onlyCfStrings=*/true);
2239     }
2240 
2241     // Write to an output file.
2242     if (target->wordSize == 8)
2243       writeResult<LP64>();
2244     else
2245       writeResult<ILP32>();
2246 
2247     depTracker->write(getLLDVersion(), inputFiles, config->outputFile);
2248   }
2249 
2250   if (config->timeTraceEnabled) {
2251     checkError(timeTraceProfilerWrite(
2252         args.getLastArgValue(OPT_time_trace_eq).str(), config->outputFile));
2253 
2254     timeTraceProfilerCleanup();
2255   }
2256 
2257   if (errorCount() != 0 || config->strictAutoLink)
2258     for (const auto &warning : missingAutolinkWarnings)
2259       warn(warning);
2260 
2261   return errorCount() == 0;
2262 }
2263 } // namespace macho
2264 } // namespace lld
2265