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