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