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