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