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