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