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