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