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