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