xref: /llvm-project-15.0.7/lld/MachO/Driver.cpp (revision 2d28100b)
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 "InputFiles.h"
12 #include "LTO.h"
13 #include "ObjC.h"
14 #include "OutputSection.h"
15 #include "OutputSegment.h"
16 #include "SymbolTable.h"
17 #include "Symbols.h"
18 #include "SyntheticSections.h"
19 #include "Target.h"
20 #include "Writer.h"
21 
22 #include "lld/Common/Args.h"
23 #include "lld/Common/Driver.h"
24 #include "lld/Common/ErrorHandler.h"
25 #include "lld/Common/LLVM.h"
26 #include "lld/Common/Memory.h"
27 #include "lld/Common/Reproduce.h"
28 #include "lld/Common/Version.h"
29 #include "llvm/ADT/DenseSet.h"
30 #include "llvm/ADT/StringExtras.h"
31 #include "llvm/ADT/StringRef.h"
32 #include "llvm/BinaryFormat/MachO.h"
33 #include "llvm/BinaryFormat/Magic.h"
34 #include "llvm/Config/config.h"
35 #include "llvm/LTO/LTO.h"
36 #include "llvm/Object/Archive.h"
37 #include "llvm/Option/ArgList.h"
38 #include "llvm/Support/CommandLine.h"
39 #include "llvm/Support/FileSystem.h"
40 #include "llvm/Support/Host.h"
41 #include "llvm/Support/MemoryBuffer.h"
42 #include "llvm/Support/Parallel.h"
43 #include "llvm/Support/Path.h"
44 #include "llvm/Support/TarWriter.h"
45 #include "llvm/Support/TargetSelect.h"
46 #include "llvm/Support/TimeProfiler.h"
47 #include "llvm/TextAPI/PackedVersion.h"
48 
49 #include <algorithm>
50 
51 using namespace llvm;
52 using namespace llvm::MachO;
53 using namespace llvm::object;
54 using namespace llvm::opt;
55 using namespace llvm::sys;
56 using namespace lld;
57 using namespace lld::macho;
58 
59 Configuration *macho::config;
60 DependencyTracker *macho::depTracker;
61 
62 static HeaderFileType getOutputType(const InputArgList &args) {
63   // TODO: -r, -dylinker, -preload...
64   Arg *outputArg = args.getLastArg(OPT_bundle, OPT_dylib, OPT_execute);
65   if (outputArg == nullptr)
66     return MH_EXECUTE;
67 
68   switch (outputArg->getOption().getID()) {
69   case OPT_bundle:
70     return MH_BUNDLE;
71   case OPT_dylib:
72     return MH_DYLIB;
73   case OPT_execute:
74     return MH_EXECUTE;
75   default:
76     llvm_unreachable("internal error");
77   }
78 }
79 
80 // Search for all possible combinations of `{root}/{name}.{extension}`.
81 // If \p extensions are not specified, then just search for `{root}/{name}`.
82 static Optional<StringRef>
83 findPathCombination(const Twine &name, const std::vector<StringRef> &roots,
84                     ArrayRef<StringRef> extensions = {""}) {
85   SmallString<261> base;
86   for (StringRef dir : roots) {
87     base = dir;
88     path::append(base, name);
89     for (StringRef ext : extensions) {
90       Twine location = base + ext;
91       if (fs::exists(location))
92         return saver.save(location.str());
93       else
94         depTracker->logFileNotFound(location);
95     }
96   }
97   return {};
98 }
99 
100 static Optional<StringRef> findLibrary(StringRef name) {
101   if (config->searchDylibsFirst) {
102     if (Optional<StringRef> path = findPathCombination(
103             "lib" + name, config->librarySearchPaths, {".tbd", ".dylib"}))
104       return path;
105     return findPathCombination("lib" + name, config->librarySearchPaths,
106                                {".a"});
107   }
108   return findPathCombination("lib" + name, config->librarySearchPaths,
109                              {".tbd", ".dylib", ".a"});
110 }
111 
112 // If -syslibroot is specified, absolute paths to non-object files may be
113 // rerooted.
114 static StringRef rerootPath(StringRef path) {
115   if (!path::is_absolute(path, path::Style::posix) || path.endswith(".o"))
116     return path;
117 
118   if (Optional<StringRef> rerootedPath =
119           findPathCombination(path, config->systemLibraryRoots))
120     return *rerootedPath;
121 
122   return path;
123 }
124 
125 static Optional<std::string> findFramework(StringRef name) {
126   SmallString<260> symlink;
127   StringRef suffix;
128   std::tie(name, suffix) = name.split(",");
129   for (StringRef dir : config->frameworkSearchPaths) {
130     symlink = dir;
131     path::append(symlink, name + ".framework", name);
132 
133     if (!suffix.empty()) {
134       // NOTE: we must resolve the symlink before trying the suffixes, because
135       // there are no symlinks for the suffixed paths.
136       SmallString<260> location;
137       if (!fs::real_path(symlink, location)) {
138         // only append suffix if realpath() succeeds
139         Twine suffixed = location + suffix;
140         if (fs::exists(suffixed))
141           return suffixed.str();
142       }
143       // Suffix lookup failed, fall through to the no-suffix case.
144     }
145 
146     if (Optional<std::string> path = resolveDylibPath(symlink))
147       return path;
148   }
149   return {};
150 }
151 
152 static bool warnIfNotDirectory(StringRef option, StringRef path) {
153   if (!fs::exists(path)) {
154     warn("directory not found for option -" + option + path);
155     return false;
156   } else if (!fs::is_directory(path)) {
157     warn("option -" + option + path + " references a non-directory path");
158     return false;
159   }
160   return true;
161 }
162 
163 static std::vector<StringRef>
164 getSearchPaths(unsigned optionCode, InputArgList &args,
165                const std::vector<StringRef> &roots,
166                const SmallVector<StringRef, 2> &systemPaths) {
167   std::vector<StringRef> paths;
168   StringRef optionLetter{optionCode == OPT_F ? "F" : "L"};
169   for (StringRef path : args::getStrings(args, optionCode)) {
170     // NOTE: only absolute paths are re-rooted to syslibroot(s)
171     bool found = false;
172     if (path::is_absolute(path, path::Style::posix)) {
173       for (StringRef root : roots) {
174         SmallString<261> buffer(root);
175         path::append(buffer, path);
176         // Do not warn about paths that are computed via the syslib roots
177         if (fs::is_directory(buffer)) {
178           paths.push_back(saver.save(buffer.str()));
179           found = true;
180         }
181       }
182     }
183     if (!found && warnIfNotDirectory(optionLetter, path))
184       paths.push_back(path);
185   }
186 
187   // `-Z` suppresses the standard "system" search paths.
188   if (args.hasArg(OPT_Z))
189     return paths;
190 
191   for (const StringRef &path : systemPaths) {
192     for (const StringRef &root : roots) {
193       SmallString<261> buffer(root);
194       path::append(buffer, path);
195       if (fs::is_directory(buffer))
196         paths.push_back(saver.save(buffer.str()));
197     }
198   }
199   return paths;
200 }
201 
202 static std::vector<StringRef> getSystemLibraryRoots(InputArgList &args) {
203   std::vector<StringRef> roots;
204   for (const Arg *arg : args.filtered(OPT_syslibroot))
205     roots.push_back(arg->getValue());
206   // NOTE: the final `-syslibroot` being `/` will ignore all roots
207   if (roots.size() && roots.back() == "/")
208     roots.clear();
209   // NOTE: roots can never be empty - add an empty root to simplify the library
210   // and framework search path computation.
211   if (roots.empty())
212     roots.emplace_back("");
213   return roots;
214 }
215 
216 static std::vector<StringRef>
217 getLibrarySearchPaths(InputArgList &args, const std::vector<StringRef> &roots) {
218   return getSearchPaths(OPT_L, args, roots, {"/usr/lib", "/usr/local/lib"});
219 }
220 
221 static std::vector<StringRef>
222 getFrameworkSearchPaths(InputArgList &args,
223                         const std::vector<StringRef> &roots) {
224   return getSearchPaths(OPT_F, args, roots,
225                         {"/Library/Frameworks", "/System/Library/Frameworks"});
226 }
227 
228 namespace {
229 struct ArchiveMember {
230   MemoryBufferRef mbref;
231   uint32_t modTime;
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 --reproduces 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});
262   }
263   if (err)
264     fatal(mb.getBufferIdentifier() +
265           ": Archive::children failed: " + toString(std::move(err)));
266 
267   return v;
268 }
269 
270 static InputFile *addFile(StringRef path, bool forceLoadArchive,
271                           bool isBundleLoader = false) {
272   Optional<MemoryBufferRef> buffer = readFile(path);
273   if (!buffer)
274     return nullptr;
275   MemoryBufferRef mbref = *buffer;
276   InputFile *newFile = nullptr;
277 
278   file_magic magic = identify_magic(mbref.getBuffer());
279   switch (magic) {
280   case file_magic::archive: {
281     std::unique_ptr<object::Archive> file = CHECK(
282         object::Archive::create(mbref), path + ": failed to parse archive");
283 
284     if (!file->isEmpty() && !file->hasSymbolTable())
285       error(path + ": archive has no index; run ranlib to add one");
286 
287     if (config->allLoad || forceLoadArchive) {
288       if (Optional<MemoryBufferRef> buffer = readFile(path)) {
289         for (const ArchiveMember &member : getArchiveMembers(*buffer)) {
290           if (Optional<InputFile *> file = loadArchiveMember(
291                   member.mbref, member.modTime, path, /*objCOnly=*/false)) {
292             inputFiles.insert(*file);
293             printArchiveMemberLoad(
294                 (forceLoadArchive ? "-force_load" : "-all_load"),
295                 inputFiles.back());
296           }
297         }
298       }
299     } else if (config->forceLoadObjC) {
300       for (const object::Archive::Symbol &sym : file->symbols())
301         if (sym.getName().startswith(objc::klass))
302           symtab->addUndefined(sym.getName(), /*file=*/nullptr,
303                                /*isWeakRef=*/false);
304 
305       // TODO: no need to look for ObjC sections for a given archive member if
306       // we already found that it contains an ObjC symbol. We should also
307       // consider creating a LazyObjFile class in order to avoid double-loading
308       // these files here and below (as part of the ArchiveFile).
309       if (Optional<MemoryBufferRef> buffer = readFile(path)) {
310         for (const ArchiveMember &member : getArchiveMembers(*buffer)) {
311           if (Optional<InputFile *> file = loadArchiveMember(
312                   member.mbref, member.modTime, path, /*objCOnly=*/true)) {
313             inputFiles.insert(*file);
314             printArchiveMemberLoad("-ObjC", inputFiles.back());
315           }
316         }
317       }
318     }
319 
320     newFile = make<ArchiveFile>(std::move(file));
321     break;
322   }
323   case file_magic::macho_object:
324     newFile = make<ObjFile>(mbref, getModTime(path), "");
325     break;
326   case file_magic::macho_dynamically_linked_shared_lib:
327   case file_magic::macho_dynamically_linked_shared_lib_stub:
328   case file_magic::tapi_file:
329     if (Optional<DylibFile *> dylibFile = loadDylib(mbref))
330       newFile = *dylibFile;
331     break;
332   case file_magic::bitcode:
333     newFile = make<BitcodeFile>(mbref);
334     break;
335   case file_magic::macho_executable:
336   case file_magic::macho_bundle:
337     // We only allow executable and bundle type here if it is used
338     // as a bundle loader.
339     if (!isBundleLoader)
340       error(path + ": unhandled file type");
341     if (Optional<DylibFile *> dylibFile =
342             loadDylib(mbref, nullptr, isBundleLoader))
343       newFile = *dylibFile;
344     break;
345   default:
346     error(path + ": unhandled file type");
347   }
348   if (newFile) {
349     // printArchiveMemberLoad() prints both .a and .o names, so no need to
350     // print the .a name here.
351     if (config->printEachFile && magic != file_magic::archive)
352       message(toString(newFile));
353     inputFiles.insert(newFile);
354   }
355   return newFile;
356 }
357 
358 static void addLibrary(StringRef name, bool isWeak) {
359   if (Optional<StringRef> path = findLibrary(name)) {
360     auto *dylibFile = dyn_cast_or_null<DylibFile>(addFile(*path, false));
361     if (isWeak && dylibFile)
362       dylibFile->forceWeakImport = true;
363     return;
364   }
365   error("library not found for -l" + name);
366 }
367 
368 static void addFramework(StringRef name, bool isWeak) {
369   if (Optional<std::string> path = findFramework(name)) {
370     auto *dylibFile = dyn_cast_or_null<DylibFile>(addFile(*path, false));
371     if (isWeak && dylibFile)
372       dylibFile->forceWeakImport = true;
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       addLibrary(arg->getValue(), false);
402       break;
403     case OPT_framework:
404       addFramework(arg->getValue(), false);
405       break;
406     default:
407       error(arg->getSpelling() + " is not allowed in LC_LINKER_OPTION");
408     }
409   }
410 }
411 
412 static void addFileList(StringRef path) {
413   Optional<MemoryBufferRef> buffer = readFile(path);
414   if (!buffer)
415     return;
416   MemoryBufferRef mbref = *buffer;
417   for (StringRef path : args::getLines(mbref))
418     addFile(rerootPath(path), false);
419 }
420 
421 // An order file has one entry per line, in the following format:
422 //
423 //   <cpu>:<object file>:<symbol name>
424 //
425 // <cpu> and <object file> are optional. If not specified, then that entry
426 // matches any symbol of that name. Parsing this format is not quite
427 // straightforward because the symbol name itself can contain colons, so when
428 // encountering a colon, we consider the preceding characters to decide if it
429 // can be a valid CPU type or file path.
430 //
431 // If a symbol is matched by multiple entries, then it takes the lowest-ordered
432 // entry (the one nearest to the front of the list.)
433 //
434 // The file can also have line comments that start with '#'.
435 static void parseOrderFile(StringRef path) {
436   Optional<MemoryBufferRef> buffer = readFile(path);
437   if (!buffer) {
438     error("Could not read order file at " + path);
439     return;
440   }
441 
442   MemoryBufferRef mbref = *buffer;
443   size_t priority = std::numeric_limits<size_t>::max();
444   for (StringRef line : args::getLines(mbref)) {
445     StringRef objectFile, symbol;
446     line = line.take_until([](char c) { return c == '#'; }); // ignore comments
447     line = line.ltrim();
448 
449     CPUType cpuType = StringSwitch<CPUType>(line)
450                           .StartsWith("i386:", CPU_TYPE_I386)
451                           .StartsWith("x86_64:", CPU_TYPE_X86_64)
452                           .StartsWith("arm:", CPU_TYPE_ARM)
453                           .StartsWith("arm64:", CPU_TYPE_ARM64)
454                           .StartsWith("ppc:", CPU_TYPE_POWERPC)
455                           .StartsWith("ppc64:", CPU_TYPE_POWERPC64)
456                           .Default(CPU_TYPE_ANY);
457     // Drop the CPU type as well as the colon
458     if (cpuType != CPU_TYPE_ANY)
459       line = line.drop_until([](char c) { return c == ':'; }).drop_front();
460     // TODO: Update when we extend support for other CPUs
461     if (cpuType != CPU_TYPE_ANY && cpuType != CPU_TYPE_X86_64 &&
462         cpuType != CPU_TYPE_ARM64)
463       continue;
464 
465     constexpr std::array<StringRef, 2> fileEnds = {".o:", ".o):"};
466     for (StringRef fileEnd : fileEnds) {
467       size_t pos = line.find(fileEnd);
468       if (pos != StringRef::npos) {
469         // Split the string around the colon
470         objectFile = line.take_front(pos + fileEnd.size() - 1);
471         line = line.drop_front(pos + fileEnd.size());
472         break;
473       }
474     }
475     symbol = line.trim();
476 
477     if (!symbol.empty()) {
478       SymbolPriorityEntry &entry = config->priorities[symbol];
479       if (!objectFile.empty())
480         entry.objectFiles.insert(std::make_pair(objectFile, priority));
481       else
482         entry.anyObjectFile = std::max(entry.anyObjectFile, priority);
483     }
484 
485     --priority;
486   }
487 }
488 
489 // We expect sub-library names of the form "libfoo", which will match a dylib
490 // with a path of .*/libfoo.{dylib, tbd}.
491 // XXX ld64 seems to ignore the extension entirely when matching sub-libraries;
492 // I'm not sure what the use case for that is.
493 static bool markReexport(StringRef searchName, ArrayRef<StringRef> extensions) {
494   for (InputFile *file : inputFiles) {
495     if (auto *dylibFile = dyn_cast<DylibFile>(file)) {
496       StringRef filename = path::filename(dylibFile->getName());
497       if (filename.consume_front(searchName) &&
498           (filename.empty() ||
499            find(extensions, filename) != extensions.end())) {
500         dylibFile->reexport = true;
501         return true;
502       }
503     }
504   }
505   return false;
506 }
507 
508 // This function is called on startup. We need this for LTO since
509 // LTO calls LLVM functions to compile bitcode files to native code.
510 // Technically this can be delayed until we read bitcode files, but
511 // we don't bother to do lazily because the initialization is fast.
512 static void initLLVM() {
513   InitializeAllTargets();
514   InitializeAllTargetMCs();
515   InitializeAllAsmPrinters();
516   InitializeAllAsmParsers();
517 }
518 
519 static void compileBitcodeFiles() {
520   TimeTraceScope timeScope("LTO");
521   auto *lto = make<BitcodeCompiler>();
522   for (InputFile *file : inputFiles)
523     if (auto *bitcodeFile = dyn_cast<BitcodeFile>(file))
524       lto->add(*bitcodeFile);
525 
526   for (ObjFile *file : lto->compile())
527     inputFiles.insert(file);
528 }
529 
530 // Replaces common symbols with defined symbols residing in __common sections.
531 // This function must be called after all symbol names are resolved (i.e. after
532 // all InputFiles have been loaded.) As a result, later operations won't see
533 // any CommonSymbols.
534 static void replaceCommonSymbols() {
535   TimeTraceScope timeScope("Replace common symbols");
536   for (Symbol *sym : symtab->getSymbols()) {
537     auto *common = dyn_cast<CommonSymbol>(sym);
538     if (common == nullptr)
539       continue;
540 
541     auto *isec = make<InputSection>();
542     isec->file = common->getFile();
543     isec->name = section_names::common;
544     isec->segname = segment_names::data;
545     isec->align = common->align;
546     // Casting to size_t will truncate large values on 32-bit architectures,
547     // but it's not really worth supporting the linking of 64-bit programs on
548     // 32-bit archs.
549     isec->data = {nullptr, static_cast<size_t>(common->size)};
550     isec->flags = S_ZEROFILL;
551     inputSections.push_back(isec);
552 
553     replaceSymbol<Defined>(sym, sym->getName(), isec->file, isec, /*value=*/0,
554                            /*size=*/0,
555                            /*isWeakDef=*/false,
556                            /*isExternal=*/true, common->privateExtern);
557   }
558 }
559 
560 static inline char toLowerDash(char x) {
561   if (x >= 'A' && x <= 'Z')
562     return x - 'A' + 'a';
563   else if (x == ' ')
564     return '-';
565   return x;
566 }
567 
568 static std::string lowerDash(StringRef s) {
569   return std::string(map_iterator(s.begin(), toLowerDash),
570                      map_iterator(s.end(), toLowerDash));
571 }
572 
573 // Has the side-effect of setting Config::platformInfo.
574 static PlatformKind parsePlatformVersion(const ArgList &args) {
575   const Arg *arg = args.getLastArg(OPT_platform_version);
576   if (!arg) {
577     error("must specify -platform_version");
578     return PlatformKind::unknown;
579   }
580 
581   StringRef platformStr = arg->getValue(0);
582   StringRef minVersionStr = arg->getValue(1);
583   StringRef sdkVersionStr = arg->getValue(2);
584 
585   // TODO(compnerd) see if we can generate this case list via XMACROS
586   PlatformKind platform =
587       StringSwitch<PlatformKind>(lowerDash(platformStr))
588           .Cases("macos", "1", PlatformKind::macOS)
589           .Cases("ios", "2", PlatformKind::iOS)
590           .Cases("tvos", "3", PlatformKind::tvOS)
591           .Cases("watchos", "4", PlatformKind::watchOS)
592           .Cases("bridgeos", "5", PlatformKind::bridgeOS)
593           .Cases("mac-catalyst", "6", PlatformKind::macCatalyst)
594           .Cases("ios-simulator", "7", PlatformKind::iOSSimulator)
595           .Cases("tvos-simulator", "8", PlatformKind::tvOSSimulator)
596           .Cases("watchos-simulator", "9", PlatformKind::watchOSSimulator)
597           .Cases("driverkit", "10", PlatformKind::driverKit)
598           .Default(PlatformKind::unknown);
599   if (platform == PlatformKind::unknown)
600     error(Twine("malformed platform: ") + platformStr);
601   // TODO: check validity of version strings, which varies by platform
602   // NOTE: ld64 accepts version strings with 5 components
603   // llvm::VersionTuple accepts no more than 4 components
604   // Has Apple ever published version strings with 5 components?
605   if (config->platformInfo.minimum.tryParse(minVersionStr))
606     error(Twine("malformed minimum version: ") + minVersionStr);
607   if (config->platformInfo.sdk.tryParse(sdkVersionStr))
608     error(Twine("malformed sdk version: ") + sdkVersionStr);
609   return platform;
610 }
611 
612 // Has the side-effect of setting Config::target.
613 static TargetInfo *createTargetInfo(InputArgList &args) {
614   StringRef archName = args.getLastArgValue(OPT_arch);
615   if (archName.empty())
616     fatal("must specify -arch");
617   PlatformKind platform = parsePlatformVersion(args);
618 
619   config->platformInfo.target =
620       MachO::Target(getArchitectureFromName(archName), platform);
621 
622   uint32_t cpuType;
623   uint32_t cpuSubtype;
624   std::tie(cpuType, cpuSubtype) = getCPUTypeFromArchitecture(config->arch());
625 
626   switch (cpuType) {
627   case CPU_TYPE_X86_64:
628     return createX86_64TargetInfo();
629   case CPU_TYPE_ARM64:
630     return createARM64TargetInfo();
631   case CPU_TYPE_ARM64_32:
632     return createARM64_32TargetInfo();
633   case CPU_TYPE_ARM:
634     return createARMTargetInfo(cpuSubtype);
635   default:
636     fatal("missing or unsupported -arch " + archName);
637   }
638 }
639 
640 static UndefinedSymbolTreatment
641 getUndefinedSymbolTreatment(const ArgList &args) {
642   StringRef treatmentStr = args.getLastArgValue(OPT_undefined);
643   auto treatment =
644       StringSwitch<UndefinedSymbolTreatment>(treatmentStr)
645           .Cases("error", "", UndefinedSymbolTreatment::error)
646           .Case("warning", UndefinedSymbolTreatment::warning)
647           .Case("suppress", UndefinedSymbolTreatment::suppress)
648           .Case("dynamic_lookup", UndefinedSymbolTreatment::dynamic_lookup)
649           .Default(UndefinedSymbolTreatment::unknown);
650   if (treatment == UndefinedSymbolTreatment::unknown) {
651     warn(Twine("unknown -undefined TREATMENT '") + treatmentStr +
652          "', defaulting to 'error'");
653     treatment = UndefinedSymbolTreatment::error;
654   } else if (config->namespaceKind == NamespaceKind::twolevel &&
655              (treatment == UndefinedSymbolTreatment::warning ||
656               treatment == UndefinedSymbolTreatment::suppress)) {
657     if (treatment == UndefinedSymbolTreatment::warning)
658       error("'-undefined warning' only valid with '-flat_namespace'");
659     else
660       error("'-undefined suppress' only valid with '-flat_namespace'");
661     treatment = UndefinedSymbolTreatment::error;
662   }
663   return treatment;
664 }
665 
666 static void warnIfDeprecatedOption(const Option &opt) {
667   if (!opt.getGroup().isValid())
668     return;
669   if (opt.getGroup().getID() == OPT_grp_deprecated) {
670     warn("Option `" + opt.getPrefixedName() + "' is deprecated in ld64:");
671     warn(opt.getHelpText());
672   }
673 }
674 
675 static void warnIfUnimplementedOption(const Option &opt) {
676   if (!opt.getGroup().isValid() || !opt.hasFlag(DriverFlag::HelpHidden))
677     return;
678   switch (opt.getGroup().getID()) {
679   case OPT_grp_deprecated:
680     // warn about deprecated options elsewhere
681     break;
682   case OPT_grp_undocumented:
683     warn("Option `" + opt.getPrefixedName() +
684          "' is undocumented. Should lld implement it?");
685     break;
686   case OPT_grp_obsolete:
687     warn("Option `" + opt.getPrefixedName() +
688          "' is obsolete. Please modernize your usage.");
689     break;
690   case OPT_grp_ignored:
691     warn("Option `" + opt.getPrefixedName() + "' is ignored.");
692     break;
693   default:
694     warn("Option `" + opt.getPrefixedName() +
695          "' is not yet implemented. Stay tuned...");
696     break;
697   }
698 }
699 
700 static const char *getReproduceOption(InputArgList &args) {
701   if (const Arg *arg = args.getLastArg(OPT_reproduce))
702     return arg->getValue();
703   return getenv("LLD_REPRODUCE");
704 }
705 
706 static void parseClangOption(StringRef opt, const Twine &msg) {
707   std::string err;
708   raw_string_ostream os(err);
709 
710   const char *argv[] = {"lld", opt.data()};
711   if (cl::ParseCommandLineOptions(2, argv, "", &os))
712     return;
713   os.flush();
714   error(msg + ": " + StringRef(err).trim());
715 }
716 
717 static uint32_t parseDylibVersion(const ArgList &args, unsigned id) {
718   const Arg *arg = args.getLastArg(id);
719   if (!arg)
720     return 0;
721 
722   if (config->outputType != MH_DYLIB) {
723     error(arg->getAsString(args) + ": only valid with -dylib");
724     return 0;
725   }
726 
727   PackedVersion version;
728   if (!version.parse32(arg->getValue())) {
729     error(arg->getAsString(args) + ": malformed version");
730     return 0;
731   }
732 
733   return version.rawValue();
734 }
735 
736 static uint32_t parseProtection(StringRef protStr) {
737   uint32_t prot = 0;
738   for (char c : protStr) {
739     switch (c) {
740     case 'r':
741       prot |= VM_PROT_READ;
742       break;
743     case 'w':
744       prot |= VM_PROT_WRITE;
745       break;
746     case 'x':
747       prot |= VM_PROT_EXECUTE;
748       break;
749     case '-':
750       break;
751     default:
752       error("unknown -segprot letter '" + Twine(c) + "' in " + protStr);
753       return 0;
754     }
755   }
756   return prot;
757 }
758 
759 void SymbolPatterns::clear() {
760   literals.clear();
761   globs.clear();
762 }
763 
764 void SymbolPatterns::insert(StringRef symbolName) {
765   if (symbolName.find_first_of("*?[]") == StringRef::npos)
766     literals.insert(CachedHashStringRef(symbolName));
767   else if (Expected<GlobPattern> pattern = GlobPattern::create(symbolName))
768     globs.emplace_back(*pattern);
769   else
770     error("invalid symbol-name pattern: " + symbolName);
771 }
772 
773 bool SymbolPatterns::matchLiteral(StringRef symbolName) const {
774   return literals.contains(CachedHashStringRef(symbolName));
775 }
776 
777 bool SymbolPatterns::matchGlob(StringRef symbolName) const {
778   for (const llvm::GlobPattern &glob : globs)
779     if (glob.match(symbolName))
780       return true;
781   return false;
782 }
783 
784 bool SymbolPatterns::match(StringRef symbolName) const {
785   return matchLiteral(symbolName) || matchGlob(symbolName);
786 }
787 
788 static void handleSymbolPatterns(InputArgList &args,
789                                  SymbolPatterns &symbolPatterns,
790                                  unsigned singleOptionCode,
791                                  unsigned listFileOptionCode) {
792   for (const Arg *arg : args.filtered(singleOptionCode))
793     symbolPatterns.insert(arg->getValue());
794   for (const Arg *arg : args.filtered(listFileOptionCode)) {
795     StringRef path = arg->getValue();
796     Optional<MemoryBufferRef> buffer = readFile(path);
797     if (!buffer) {
798       error("Could not read symbol file: " + path);
799       continue;
800     }
801     MemoryBufferRef mbref = *buffer;
802     for (StringRef line : args::getLines(mbref)) {
803       line = line.take_until([](char c) { return c == '#'; }).trim();
804       if (!line.empty())
805         symbolPatterns.insert(line);
806     }
807   }
808 }
809 
810 void createFiles(const InputArgList &args) {
811   TimeTraceScope timeScope("Load input files");
812   // This loop should be reserved for options whose exact ordering matters.
813   // Other options should be handled via filtered() and/or getLastArg().
814   for (const Arg *arg : args) {
815     const Option &opt = arg->getOption();
816     warnIfDeprecatedOption(opt);
817     warnIfUnimplementedOption(opt);
818 
819     switch (opt.getID()) {
820     case OPT_INPUT:
821       addFile(rerootPath(arg->getValue()), false);
822       break;
823     case OPT_weak_library:
824       if (auto *dylibFile = dyn_cast_or_null<DylibFile>(
825               addFile(rerootPath(arg->getValue()), false)))
826         dylibFile->forceWeakImport = true;
827       break;
828     case OPT_filelist:
829       addFileList(arg->getValue());
830       break;
831     case OPT_force_load:
832       addFile(rerootPath(arg->getValue()), true);
833       break;
834     case OPT_l:
835     case OPT_weak_l:
836       addLibrary(arg->getValue(), opt.getID() == OPT_weak_l);
837       break;
838     case OPT_framework:
839     case OPT_weak_framework:
840       addFramework(arg->getValue(), opt.getID() == OPT_weak_framework);
841       break;
842     default:
843       break;
844     }
845   }
846 }
847 
848 bool macho::link(ArrayRef<const char *> argsArr, bool canExitEarly,
849                  raw_ostream &stdoutOS, raw_ostream &stderrOS) {
850   lld::stdoutOS = &stdoutOS;
851   lld::stderrOS = &stderrOS;
852 
853   errorHandler().cleanupCallback = []() { freeArena(); };
854 
855   errorHandler().logName = args::getFilenameWithoutExe(argsArr[0]);
856   stderrOS.enable_colors(stderrOS.has_colors());
857   // TODO: Set up error handler properly, e.g. the errorLimitExceededMsg
858 
859   MachOOptTable parser;
860   InputArgList args = parser.parse(argsArr.slice(1));
861 
862   errorHandler().errorLimitExceededMsg =
863       "too many errors emitted, stopping now "
864       "(use --error-limit=0 to see all errors)";
865   errorHandler().errorLimit = args::getInteger(args, OPT_error_limit_eq, 20);
866 
867   if (args.hasArg(OPT_help_hidden)) {
868     parser.printHelp(argsArr[0], /*showHidden=*/true);
869     return true;
870   }
871   if (args.hasArg(OPT_help)) {
872     parser.printHelp(argsArr[0], /*showHidden=*/false);
873     return true;
874   }
875   if (args.hasArg(OPT_version)) {
876     message(getLLDVersion());
877     return true;
878   }
879 
880   if (const char *path = getReproduceOption(args)) {
881     // Note that --reproduce is a debug option so you can ignore it
882     // if you are trying to understand the whole picture of the code.
883     Expected<std::unique_ptr<TarWriter>> errOrWriter =
884         TarWriter::create(path, path::stem(path));
885     if (errOrWriter) {
886       tar = std::move(*errOrWriter);
887       tar->append("response.txt", createResponseFile(args));
888       tar->append("version.txt", getLLDVersion() + "\n");
889     } else {
890       error("--reproduce: " + toString(errOrWriter.takeError()));
891     }
892   }
893 
894   config = make<Configuration>();
895   symtab = make<SymbolTable>();
896   target = createTargetInfo(args);
897 
898   depTracker =
899       make<DependencyTracker>(args.getLastArgValue(OPT_dependency_info, ""));
900 
901   if (auto *arg = args.getLastArg(OPT_threads_eq)) {
902     StringRef v(arg->getValue());
903     unsigned threads = 0;
904     if (!llvm::to_integer(v, threads, 0) || threads == 0)
905       error(arg->getSpelling() + ": expected a positive integer, but got '" +
906             arg->getValue() + "'");
907     parallel::strategy = hardware_concurrency(threads);
908     config->thinLTOJobs = v;
909   }
910   if (auto *arg = args.getLastArg(OPT_thinlto_jobs_eq))
911     config->thinLTOJobs = arg->getValue();
912   if (!get_threadpool_strategy(config->thinLTOJobs))
913     error("--thinlto-jobs: invalid job count: " + config->thinLTOJobs);
914 
915   config->entry = symtab->addUndefined(args.getLastArgValue(OPT_e, "_main"),
916                                        /*file=*/nullptr,
917                                        /*isWeakRef=*/false);
918   for (const Arg *arg : args.filtered(OPT_u)) {
919     config->explicitUndefineds.push_back(symtab->addUndefined(
920         arg->getValue(), /*file=*/nullptr, /*isWeakRef=*/false));
921   }
922 
923   for (const Arg *arg : args.filtered(OPT_U))
924     symtab->addDynamicLookup(arg->getValue());
925 
926   config->mapFile = args.getLastArgValue(OPT_map);
927   config->outputFile = args.getLastArgValue(OPT_o, "a.out");
928   config->astPaths = args.getAllArgValues(OPT_add_ast_path);
929   config->headerPad = args::getHex(args, OPT_headerpad, /*Default=*/32);
930   config->headerPadMaxInstallNames =
931       args.hasArg(OPT_headerpad_max_install_names);
932   config->printEachFile = args.hasArg(OPT_t);
933   config->printWhyLoad = args.hasArg(OPT_why_load);
934   config->outputType = getOutputType(args);
935   if (const Arg *arg = args.getLastArg(OPT_bundle_loader)) {
936     if (config->outputType != MH_BUNDLE)
937       error("-bundle_loader can only be used with MachO bundle output");
938     addFile(arg->getValue(), false, true);
939   }
940   config->ltoObjPath = args.getLastArgValue(OPT_object_path_lto);
941   config->ltoNewPassManager =
942       args.hasFlag(OPT_no_lto_legacy_pass_manager, OPT_lto_legacy_pass_manager,
943                    LLVM_ENABLE_NEW_PASS_MANAGER);
944   config->runtimePaths = args::getStrings(args, OPT_rpath);
945   config->allLoad = args.hasArg(OPT_all_load);
946   config->forceLoadObjC = args.hasArg(OPT_ObjC);
947   config->demangle = args.hasArg(OPT_demangle);
948   config->implicitDylibs = !args.hasArg(OPT_no_implicit_dylibs);
949   config->emitFunctionStarts = !args.hasArg(OPT_no_function_starts);
950   config->emitBitcodeBundle = args.hasArg(OPT_bitcode_bundle);
951 
952   std::array<PlatformKind, 3> encryptablePlatforms{
953       PlatformKind::iOS, PlatformKind::watchOS, PlatformKind::tvOS};
954   config->emitEncryptionInfo =
955       args.hasFlag(OPT_encryptable, OPT_no_encryption,
956                    is_contained(encryptablePlatforms, config->platform()));
957 
958 #ifndef HAVE_LIBXAR
959   if (config->emitBitcodeBundle)
960     error("-bitcode_bundle unsupported because LLD wasn't built with libxar");
961 #endif
962 
963   if (const Arg *arg = args.getLastArg(OPT_install_name)) {
964     if (config->outputType != MH_DYLIB)
965       warn(arg->getAsString(args) + ": ignored, only has effect with -dylib");
966     else
967       config->installName = arg->getValue();
968   } else if (config->outputType == MH_DYLIB) {
969     config->installName = config->outputFile;
970   }
971 
972   if (args.hasArg(OPT_mark_dead_strippable_dylib)) {
973     if (config->outputType != MH_DYLIB)
974       warn("-mark_dead_strippable_dylib: ignored, only has effect with -dylib");
975     else
976       config->markDeadStrippableDylib = true;
977   }
978 
979   if (const Arg *arg = args.getLastArg(OPT_static, OPT_dynamic))
980     config->staticLink = (arg->getOption().getID() == OPT_static);
981 
982   if (const Arg *arg =
983           args.getLastArg(OPT_flat_namespace, OPT_twolevel_namespace))
984     config->namespaceKind = arg->getOption().getID() == OPT_twolevel_namespace
985                                 ? NamespaceKind::twolevel
986                                 : NamespaceKind::flat;
987 
988   config->undefinedSymbolTreatment = getUndefinedSymbolTreatment(args);
989 
990   config->systemLibraryRoots = getSystemLibraryRoots(args);
991   config->librarySearchPaths =
992       getLibrarySearchPaths(args, config->systemLibraryRoots);
993   config->frameworkSearchPaths =
994       getFrameworkSearchPaths(args, config->systemLibraryRoots);
995   if (const Arg *arg =
996           args.getLastArg(OPT_search_paths_first, OPT_search_dylibs_first))
997     config->searchDylibsFirst =
998         arg->getOption().getID() == OPT_search_dylibs_first;
999 
1000   config->dylibCompatibilityVersion =
1001       parseDylibVersion(args, OPT_compatibility_version);
1002   config->dylibCurrentVersion = parseDylibVersion(args, OPT_current_version);
1003 
1004   // Reject every special character except '.' and '$'
1005   // TODO(gkm): verify that this is the proper set of invalid chars
1006   StringRef invalidNameChars("!\"#%&'()*+,-/:;<=>?@[\\]^`{|}~");
1007   auto validName = [invalidNameChars](StringRef s) {
1008     if (s.find_first_of(invalidNameChars) != StringRef::npos)
1009       error("invalid name for segment or section: " + s);
1010     return s;
1011   };
1012   for (const Arg *arg : args.filtered(OPT_rename_section)) {
1013     config->sectionRenameMap[{validName(arg->getValue(0)),
1014                               validName(arg->getValue(1))}] = {
1015         validName(arg->getValue(2)), validName(arg->getValue(3))};
1016   }
1017   for (const Arg *arg : args.filtered(OPT_rename_segment)) {
1018     config->segmentRenameMap[validName(arg->getValue(0))] =
1019         validName(arg->getValue(1));
1020   }
1021 
1022   for (const Arg *arg : args.filtered(OPT_segprot)) {
1023     StringRef segName = arg->getValue(0);
1024     uint32_t maxProt = parseProtection(arg->getValue(1));
1025     uint32_t initProt = parseProtection(arg->getValue(2));
1026     if (maxProt != initProt && config->arch() != AK_i386)
1027       error("invalid argument '" + arg->getAsString(args) +
1028             "': max and init must be the same for non-i386 archs");
1029     if (segName == segment_names::linkEdit)
1030       error("-segprot cannot be used to change __LINKEDIT's protections");
1031     config->segmentProtections.push_back({segName, maxProt, initProt});
1032   }
1033 
1034   handleSymbolPatterns(args, config->exportedSymbols, OPT_exported_symbol,
1035                        OPT_exported_symbols_list);
1036   handleSymbolPatterns(args, config->unexportedSymbols, OPT_unexported_symbol,
1037                        OPT_unexported_symbols_list);
1038   if (!config->exportedSymbols.empty() && !config->unexportedSymbols.empty()) {
1039     error("cannot use both -exported_symbol* and -unexported_symbol* options\n"
1040           ">>> ignoring unexports");
1041     config->unexportedSymbols.clear();
1042   }
1043 
1044   config->saveTemps = args.hasArg(OPT_save_temps);
1045 
1046   config->adhocCodesign = args.hasFlag(
1047       OPT_adhoc_codesign, OPT_no_adhoc_codesign,
1048       (config->arch() == AK_arm64 || config->arch() == AK_arm64e) &&
1049           config->platform() == PlatformKind::macOS);
1050 
1051   if (args.hasArg(OPT_v)) {
1052     message(getLLDVersion());
1053     message(StringRef("Library search paths:") +
1054             (config->librarySearchPaths.empty()
1055                  ? ""
1056                  : "\n\t" + join(config->librarySearchPaths, "\n\t")));
1057     message(StringRef("Framework search paths:") +
1058             (config->frameworkSearchPaths.empty()
1059                  ? ""
1060                  : "\n\t" + join(config->frameworkSearchPaths, "\n\t")));
1061   }
1062 
1063   config->progName = argsArr[0];
1064 
1065   config->timeTraceEnabled = args.hasArg(
1066       OPT_time_trace, OPT_time_trace_granularity_eq, OPT_time_trace_file_eq);
1067   config->timeTraceGranularity =
1068       args::getInteger(args, OPT_time_trace_granularity_eq, 500);
1069 
1070   // Initialize time trace profiler.
1071   if (config->timeTraceEnabled)
1072     timeTraceProfilerInitialize(config->timeTraceGranularity, config->progName);
1073 
1074   {
1075     TimeTraceScope timeScope("ExecuteLinker");
1076 
1077     initLLVM(); // must be run before any call to addFile()
1078     createFiles(args);
1079 
1080     config->isPic = config->outputType == MH_DYLIB ||
1081                     config->outputType == MH_BUNDLE ||
1082                     (config->outputType == MH_EXECUTE &&
1083                      args.hasFlag(OPT_pie, OPT_no_pie, true));
1084 
1085     // Now that all dylibs have been loaded, search for those that should be
1086     // re-exported.
1087     {
1088       auto reexportHandler = [](const Arg *arg,
1089                                 const std::vector<StringRef> &extensions) {
1090         config->hasReexports = true;
1091         StringRef searchName = arg->getValue();
1092         if (!markReexport(searchName, extensions))
1093           error(arg->getSpelling() + " " + searchName +
1094                 " does not match a supplied dylib");
1095       };
1096       std::vector<StringRef> extensions = {".tbd"};
1097       for (const Arg *arg : args.filtered(OPT_sub_umbrella))
1098         reexportHandler(arg, extensions);
1099 
1100       extensions.push_back(".dylib");
1101       for (const Arg *arg : args.filtered(OPT_sub_library))
1102         reexportHandler(arg, extensions);
1103     }
1104 
1105     // Parse LTO options.
1106     if (const Arg *arg = args.getLastArg(OPT_mcpu))
1107       parseClangOption(saver.save("-mcpu=" + StringRef(arg->getValue())),
1108                        arg->getSpelling());
1109 
1110     for (const Arg *arg : args.filtered(OPT_mllvm))
1111       parseClangOption(arg->getValue(), arg->getSpelling());
1112 
1113     compileBitcodeFiles();
1114     replaceCommonSymbols();
1115 
1116     StringRef orderFile = args.getLastArgValue(OPT_order_file);
1117     if (!orderFile.empty())
1118       parseOrderFile(orderFile);
1119 
1120     if (config->outputType == MH_EXECUTE && isa<Undefined>(config->entry)) {
1121       error("undefined symbol: " + toString(*config->entry));
1122       return false;
1123     }
1124     // FIXME: This prints symbols that are undefined both in input files and
1125     // via -u flag twice.
1126     for (const Symbol *undefined : config->explicitUndefineds) {
1127       if (isa<Undefined>(undefined)) {
1128         error("undefined symbol: " + toString(*undefined) +
1129               "\n>>> referenced by flag -u " + toString(*undefined));
1130         return false;
1131       }
1132     }
1133     // Literal exported-symbol names must be defined, but glob
1134     // patterns need not match.
1135     for (const CachedHashStringRef &cachedName :
1136          config->exportedSymbols.literals) {
1137       if (const Symbol *sym = symtab->find(cachedName))
1138         if (isa<Defined>(sym))
1139           continue;
1140       error("undefined symbol " + cachedName.val() +
1141             "\n>>> referenced from option -exported_symbol(s_list)");
1142     }
1143 
1144     if (target->wordSize == 8)
1145       createSyntheticSections<LP64>();
1146     else
1147       createSyntheticSections<ILP32>();
1148 
1149     createSyntheticSymbols();
1150 
1151     for (const Arg *arg : args.filtered(OPT_sectcreate)) {
1152       StringRef segName = arg->getValue(0);
1153       StringRef sectName = arg->getValue(1);
1154       StringRef fileName = arg->getValue(2);
1155       Optional<MemoryBufferRef> buffer = readFile(fileName);
1156       if (buffer)
1157         inputFiles.insert(make<OpaqueFile>(*buffer, segName, sectName));
1158     }
1159 
1160     {
1161       TimeTraceScope timeScope("Gathering input sections");
1162       // Gather all InputSections into one vector.
1163       for (const InputFile *file : inputFiles) {
1164         for (const SubsectionMap &map : file->subsections)
1165           for (const SubsectionEntry &subsectionEntry : map)
1166             inputSections.push_back(subsectionEntry.isec);
1167       }
1168     }
1169 
1170     // Write to an output file.
1171     if (target->wordSize == 8)
1172       writeResult<LP64>();
1173     else
1174       writeResult<ILP32>();
1175 
1176     depTracker->write(getLLDVersion(), inputFiles, config->outputFile);
1177   }
1178 
1179   if (config->timeTraceEnabled) {
1180     if (auto E = timeTraceProfilerWrite(
1181             args.getLastArgValue(OPT_time_trace_file_eq).str(),
1182             config->outputFile)) {
1183       handleAllErrors(std::move(E),
1184                       [&](const StringError &SE) { error(SE.getMessage()); });
1185     }
1186 
1187     timeTraceProfilerCleanup();
1188   }
1189 
1190   if (canExitEarly)
1191     exitLld(errorCount() ? 1 : 0);
1192 
1193   return !errorCount();
1194 }
1195