xref: /llvm-project-15.0.7/lld/MachO/Driver.cpp (revision 2ea7fb7b)
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   switch (getCPUTypeFromArchitecture(config->arch()).first) {
623   case CPU_TYPE_X86_64:
624     return createX86_64TargetInfo();
625   case CPU_TYPE_ARM64:
626     return createARM64TargetInfo();
627   case CPU_TYPE_ARM64_32:
628     return createARM64_32TargetInfo();
629   default:
630     fatal("missing or unsupported -arch " + archName);
631   }
632 }
633 
634 static UndefinedSymbolTreatment
635 getUndefinedSymbolTreatment(const ArgList &args) {
636   StringRef treatmentStr = args.getLastArgValue(OPT_undefined);
637   auto treatment =
638       StringSwitch<UndefinedSymbolTreatment>(treatmentStr)
639           .Cases("error", "", UndefinedSymbolTreatment::error)
640           .Case("warning", UndefinedSymbolTreatment::warning)
641           .Case("suppress", UndefinedSymbolTreatment::suppress)
642           .Case("dynamic_lookup", UndefinedSymbolTreatment::dynamic_lookup)
643           .Default(UndefinedSymbolTreatment::unknown);
644   if (treatment == UndefinedSymbolTreatment::unknown) {
645     warn(Twine("unknown -undefined TREATMENT '") + treatmentStr +
646          "', defaulting to 'error'");
647     treatment = UndefinedSymbolTreatment::error;
648   } else if (config->namespaceKind == NamespaceKind::twolevel &&
649              (treatment == UndefinedSymbolTreatment::warning ||
650               treatment == UndefinedSymbolTreatment::suppress)) {
651     if (treatment == UndefinedSymbolTreatment::warning)
652       error("'-undefined warning' only valid with '-flat_namespace'");
653     else
654       error("'-undefined suppress' only valid with '-flat_namespace'");
655     treatment = UndefinedSymbolTreatment::error;
656   }
657   return treatment;
658 }
659 
660 static void warnIfDeprecatedOption(const Option &opt) {
661   if (!opt.getGroup().isValid())
662     return;
663   if (opt.getGroup().getID() == OPT_grp_deprecated) {
664     warn("Option `" + opt.getPrefixedName() + "' is deprecated in ld64:");
665     warn(opt.getHelpText());
666   }
667 }
668 
669 static void warnIfUnimplementedOption(const Option &opt) {
670   if (!opt.getGroup().isValid() || !opt.hasFlag(DriverFlag::HelpHidden))
671     return;
672   switch (opt.getGroup().getID()) {
673   case OPT_grp_deprecated:
674     // warn about deprecated options elsewhere
675     break;
676   case OPT_grp_undocumented:
677     warn("Option `" + opt.getPrefixedName() +
678          "' is undocumented. Should lld implement it?");
679     break;
680   case OPT_grp_obsolete:
681     warn("Option `" + opt.getPrefixedName() +
682          "' is obsolete. Please modernize your usage.");
683     break;
684   case OPT_grp_ignored:
685     warn("Option `" + opt.getPrefixedName() + "' is ignored.");
686     break;
687   default:
688     warn("Option `" + opt.getPrefixedName() +
689          "' is not yet implemented. Stay tuned...");
690     break;
691   }
692 }
693 
694 static const char *getReproduceOption(InputArgList &args) {
695   if (const Arg *arg = args.getLastArg(OPT_reproduce))
696     return arg->getValue();
697   return getenv("LLD_REPRODUCE");
698 }
699 
700 static bool isPie(InputArgList &args) {
701   if (config->outputType != MH_EXECUTE || args.hasArg(OPT_no_pie))
702     return false;
703   if (config->arch() == AK_arm64 || config->arch() == AK_arm64e ||
704       config->arch() == AK_arm64_32)
705     return true;
706 
707   // TODO: add logic here as we support more archs. E.g. i386 should default
708   // to PIE from 10.7
709   assert(config->arch() == AK_x86_64 || config->arch() == AK_x86_64h ||
710          config->arch() == AK_arm64_32);
711 
712   PlatformKind kind = config->platformInfo.target.Platform;
713   if (kind == PlatformKind::macOS &&
714       config->platformInfo.minimum >= VersionTuple(10, 6))
715     return true;
716 
717   if (kind == PlatformKind::iOSSimulator || kind == PlatformKind::driverKit)
718     return true;
719 
720   return args.hasArg(OPT_pie);
721 }
722 
723 static void parseClangOption(StringRef opt, const Twine &msg) {
724   std::string err;
725   raw_string_ostream os(err);
726 
727   const char *argv[] = {"lld", opt.data()};
728   if (cl::ParseCommandLineOptions(2, argv, "", &os))
729     return;
730   os.flush();
731   error(msg + ": " + StringRef(err).trim());
732 }
733 
734 static uint32_t parseDylibVersion(const ArgList &args, unsigned id) {
735   const Arg *arg = args.getLastArg(id);
736   if (!arg)
737     return 0;
738 
739   if (config->outputType != MH_DYLIB) {
740     error(arg->getAsString(args) + ": only valid with -dylib");
741     return 0;
742   }
743 
744   PackedVersion version;
745   if (!version.parse32(arg->getValue())) {
746     error(arg->getAsString(args) + ": malformed version");
747     return 0;
748   }
749 
750   return version.rawValue();
751 }
752 
753 static uint32_t parseProtection(StringRef protStr) {
754   uint32_t prot = 0;
755   for (char c : protStr) {
756     switch (c) {
757     case 'r':
758       prot |= VM_PROT_READ;
759       break;
760     case 'w':
761       prot |= VM_PROT_WRITE;
762       break;
763     case 'x':
764       prot |= VM_PROT_EXECUTE;
765       break;
766     case '-':
767       break;
768     default:
769       error("unknown -segprot letter '" + Twine(c) + "' in " + protStr);
770       return 0;
771     }
772   }
773   return prot;
774 }
775 
776 void SymbolPatterns::clear() {
777   literals.clear();
778   globs.clear();
779 }
780 
781 void SymbolPatterns::insert(StringRef symbolName) {
782   if (symbolName.find_first_of("*?[]") == StringRef::npos)
783     literals.insert(CachedHashStringRef(symbolName));
784   else if (Expected<GlobPattern> pattern = GlobPattern::create(symbolName))
785     globs.emplace_back(*pattern);
786   else
787     error("invalid symbol-name pattern: " + symbolName);
788 }
789 
790 bool SymbolPatterns::matchLiteral(StringRef symbolName) const {
791   return literals.contains(CachedHashStringRef(symbolName));
792 }
793 
794 bool SymbolPatterns::matchGlob(StringRef symbolName) const {
795   for (const llvm::GlobPattern &glob : globs)
796     if (glob.match(symbolName))
797       return true;
798   return false;
799 }
800 
801 bool SymbolPatterns::match(StringRef symbolName) const {
802   return matchLiteral(symbolName) || matchGlob(symbolName);
803 }
804 
805 static void handleSymbolPatterns(InputArgList &args,
806                                  SymbolPatterns &symbolPatterns,
807                                  unsigned singleOptionCode,
808                                  unsigned listFileOptionCode) {
809   for (const Arg *arg : args.filtered(singleOptionCode))
810     symbolPatterns.insert(arg->getValue());
811   for (const Arg *arg : args.filtered(listFileOptionCode)) {
812     StringRef path = arg->getValue();
813     Optional<MemoryBufferRef> buffer = readFile(path);
814     if (!buffer) {
815       error("Could not read symbol file: " + path);
816       continue;
817     }
818     MemoryBufferRef mbref = *buffer;
819     for (StringRef line : args::getLines(mbref)) {
820       line = line.take_until([](char c) { return c == '#'; }).trim();
821       if (!line.empty())
822         symbolPatterns.insert(line);
823     }
824   }
825 }
826 
827 void createFiles(const InputArgList &args) {
828   TimeTraceScope timeScope("Load input files");
829   // This loop should be reserved for options whose exact ordering matters.
830   // Other options should be handled via filtered() and/or getLastArg().
831   for (const Arg *arg : args) {
832     const Option &opt = arg->getOption();
833     warnIfDeprecatedOption(opt);
834     warnIfUnimplementedOption(opt);
835 
836     switch (opt.getID()) {
837     case OPT_INPUT:
838       addFile(rerootPath(arg->getValue()), false);
839       break;
840     case OPT_weak_library:
841       if (auto *dylibFile = dyn_cast_or_null<DylibFile>(
842               addFile(rerootPath(arg->getValue()), false)))
843         dylibFile->forceWeakImport = true;
844       break;
845     case OPT_filelist:
846       addFileList(arg->getValue());
847       break;
848     case OPT_force_load:
849       addFile(rerootPath(arg->getValue()), true);
850       break;
851     case OPT_l:
852     case OPT_weak_l:
853       addLibrary(arg->getValue(), opt.getID() == OPT_weak_l);
854       break;
855     case OPT_framework:
856     case OPT_weak_framework:
857       addFramework(arg->getValue(), opt.getID() == OPT_weak_framework);
858       break;
859     default:
860       break;
861     }
862   }
863 }
864 
865 bool macho::link(ArrayRef<const char *> argsArr, bool canExitEarly,
866                  raw_ostream &stdoutOS, raw_ostream &stderrOS) {
867   lld::stdoutOS = &stdoutOS;
868   lld::stderrOS = &stderrOS;
869 
870   errorHandler().cleanupCallback = []() { freeArena(); };
871 
872   errorHandler().logName = args::getFilenameWithoutExe(argsArr[0]);
873   stderrOS.enable_colors(stderrOS.has_colors());
874   // TODO: Set up error handler properly, e.g. the errorLimitExceededMsg
875 
876   MachOOptTable parser;
877   InputArgList args = parser.parse(argsArr.slice(1));
878 
879   errorHandler().errorLimitExceededMsg =
880       "too many errors emitted, stopping now "
881       "(use --error-limit=0 to see all errors)";
882   errorHandler().errorLimit = args::getInteger(args, OPT_error_limit_eq, 20);
883 
884   if (args.hasArg(OPT_help_hidden)) {
885     parser.printHelp(argsArr[0], /*showHidden=*/true);
886     return true;
887   }
888   if (args.hasArg(OPT_help)) {
889     parser.printHelp(argsArr[0], /*showHidden=*/false);
890     return true;
891   }
892   if (args.hasArg(OPT_version)) {
893     message(getLLDVersion());
894     return true;
895   }
896 
897   if (const char *path = getReproduceOption(args)) {
898     // Note that --reproduce is a debug option so you can ignore it
899     // if you are trying to understand the whole picture of the code.
900     Expected<std::unique_ptr<TarWriter>> errOrWriter =
901         TarWriter::create(path, path::stem(path));
902     if (errOrWriter) {
903       tar = std::move(*errOrWriter);
904       tar->append("response.txt", createResponseFile(args));
905       tar->append("version.txt", getLLDVersion() + "\n");
906     } else {
907       error("--reproduce: " + toString(errOrWriter.takeError()));
908     }
909   }
910 
911   config = make<Configuration>();
912   symtab = make<SymbolTable>();
913   target = createTargetInfo(args);
914 
915   depTracker =
916       make<DependencyTracker>(args.getLastArgValue(OPT_dependency_info, ""));
917 
918   if (auto *arg = args.getLastArg(OPT_threads_eq)) {
919     StringRef v(arg->getValue());
920     unsigned threads = 0;
921     if (!llvm::to_integer(v, threads, 0) || threads == 0)
922       error(arg->getSpelling() + ": expected a positive integer, but got '" +
923             arg->getValue() + "'");
924     parallel::strategy = hardware_concurrency(threads);
925     config->thinLTOJobs = v;
926   }
927   if (auto *arg = args.getLastArg(OPT_thinlto_jobs_eq))
928     config->thinLTOJobs = arg->getValue();
929   if (!get_threadpool_strategy(config->thinLTOJobs))
930     error("--thinlto-jobs: invalid job count: " + config->thinLTOJobs);
931 
932   config->entry = symtab->addUndefined(args.getLastArgValue(OPT_e, "_main"),
933                                        /*file=*/nullptr,
934                                        /*isWeakRef=*/false);
935   for (const Arg *arg : args.filtered(OPT_u)) {
936     config->explicitUndefineds.push_back(symtab->addUndefined(
937         arg->getValue(), /*file=*/nullptr, /*isWeakRef=*/false));
938   }
939 
940   for (const Arg *arg : args.filtered(OPT_U))
941     symtab->addDynamicLookup(arg->getValue());
942 
943   config->mapFile = args.getLastArgValue(OPT_map);
944   config->outputFile = args.getLastArgValue(OPT_o, "a.out");
945   config->astPaths = args.getAllArgValues(OPT_add_ast_path);
946   config->headerPad = args::getHex(args, OPT_headerpad, /*Default=*/32);
947   config->headerPadMaxInstallNames =
948       args.hasArg(OPT_headerpad_max_install_names);
949   config->printEachFile = args.hasArg(OPT_t);
950   config->printWhyLoad = args.hasArg(OPT_why_load);
951   config->outputType = getOutputType(args);
952   if (const Arg *arg = args.getLastArg(OPT_bundle_loader)) {
953     if (config->outputType != MH_BUNDLE)
954       error("-bundle_loader can only be used with MachO bundle output");
955     addFile(arg->getValue(), false, true);
956   }
957   config->ltoObjPath = args.getLastArgValue(OPT_object_path_lto);
958   config->ltoNewPassManager =
959       args.hasFlag(OPT_no_lto_legacy_pass_manager, OPT_lto_legacy_pass_manager,
960                    LLVM_ENABLE_NEW_PASS_MANAGER);
961   config->runtimePaths = args::getStrings(args, OPT_rpath);
962   config->allLoad = args.hasArg(OPT_all_load);
963   config->forceLoadObjC = args.hasArg(OPT_ObjC);
964   config->demangle = args.hasArg(OPT_demangle);
965   config->implicitDylibs = !args.hasArg(OPT_no_implicit_dylibs);
966   config->emitFunctionStarts = !args.hasArg(OPT_no_function_starts);
967   config->emitBitcodeBundle = args.hasArg(OPT_bitcode_bundle);
968 
969   std::array<PlatformKind, 3> encryptablePlatforms{
970       PlatformKind::iOS, PlatformKind::watchOS, PlatformKind::tvOS};
971   config->emitEncryptionInfo =
972       args.hasFlag(OPT_encryptable, OPT_no_encryption,
973                    is_contained(encryptablePlatforms, config->platform()));
974 
975 #ifndef HAVE_LIBXAR
976   if (config->emitBitcodeBundle)
977     error("-bitcode_bundle unsupported because LLD wasn't built with libxar");
978 #endif
979 
980   if (const Arg *arg = args.getLastArg(OPT_install_name)) {
981     if (config->outputType != MH_DYLIB)
982       warn(arg->getAsString(args) + ": ignored, only has effect with -dylib");
983     else
984       config->installName = arg->getValue();
985   } else if (config->outputType == MH_DYLIB) {
986     config->installName = config->outputFile;
987   }
988 
989   if (args.hasArg(OPT_mark_dead_strippable_dylib)) {
990     if (config->outputType != MH_DYLIB)
991       warn("-mark_dead_strippable_dylib: ignored, only has effect with -dylib");
992     else
993       config->markDeadStrippableDylib = true;
994   }
995 
996   if (const Arg *arg = args.getLastArg(OPT_static, OPT_dynamic))
997     config->staticLink = (arg->getOption().getID() == OPT_static);
998 
999   if (const Arg *arg =
1000           args.getLastArg(OPT_flat_namespace, OPT_twolevel_namespace))
1001     config->namespaceKind = arg->getOption().getID() == OPT_twolevel_namespace
1002                                 ? NamespaceKind::twolevel
1003                                 : NamespaceKind::flat;
1004 
1005   config->undefinedSymbolTreatment = getUndefinedSymbolTreatment(args);
1006 
1007   config->systemLibraryRoots = getSystemLibraryRoots(args);
1008   config->librarySearchPaths =
1009       getLibrarySearchPaths(args, config->systemLibraryRoots);
1010   config->frameworkSearchPaths =
1011       getFrameworkSearchPaths(args, config->systemLibraryRoots);
1012   if (const Arg *arg =
1013           args.getLastArg(OPT_search_paths_first, OPT_search_dylibs_first))
1014     config->searchDylibsFirst =
1015         arg->getOption().getID() == OPT_search_dylibs_first;
1016 
1017   config->dylibCompatibilityVersion =
1018       parseDylibVersion(args, OPT_compatibility_version);
1019   config->dylibCurrentVersion = parseDylibVersion(args, OPT_current_version);
1020 
1021   // Reject every special character except '.' and '$'
1022   // TODO(gkm): verify that this is the proper set of invalid chars
1023   StringRef invalidNameChars("!\"#%&'()*+,-/:;<=>?@[\\]^`{|}~");
1024   auto validName = [invalidNameChars](StringRef s) {
1025     if (s.find_first_of(invalidNameChars) != StringRef::npos)
1026       error("invalid name for segment or section: " + s);
1027     return s;
1028   };
1029   for (const Arg *arg : args.filtered(OPT_rename_section)) {
1030     config->sectionRenameMap[{validName(arg->getValue(0)),
1031                               validName(arg->getValue(1))}] = {
1032         validName(arg->getValue(2)), validName(arg->getValue(3))};
1033   }
1034   for (const Arg *arg : args.filtered(OPT_rename_segment)) {
1035     config->segmentRenameMap[validName(arg->getValue(0))] =
1036         validName(arg->getValue(1));
1037   }
1038 
1039   for (const Arg *arg : args.filtered(OPT_segprot)) {
1040     StringRef segName = arg->getValue(0);
1041     uint32_t maxProt = parseProtection(arg->getValue(1));
1042     uint32_t initProt = parseProtection(arg->getValue(2));
1043     if (maxProt != initProt && config->arch() != AK_i386)
1044       error("invalid argument '" + arg->getAsString(args) +
1045             "': max and init must be the same for non-i386 archs");
1046     if (segName == segment_names::linkEdit)
1047       error("-segprot cannot be used to change __LINKEDIT's protections");
1048     config->segmentProtections.push_back({segName, maxProt, initProt});
1049   }
1050 
1051   handleSymbolPatterns(args, config->exportedSymbols, OPT_exported_symbol,
1052                        OPT_exported_symbols_list);
1053   handleSymbolPatterns(args, config->unexportedSymbols, OPT_unexported_symbol,
1054                        OPT_unexported_symbols_list);
1055   if (!config->exportedSymbols.empty() && !config->unexportedSymbols.empty()) {
1056     error("cannot use both -exported_symbol* and -unexported_symbol* options\n"
1057           ">>> ignoring unexports");
1058     config->unexportedSymbols.clear();
1059   }
1060 
1061   config->saveTemps = args.hasArg(OPT_save_temps);
1062 
1063   config->adhocCodesign = args.hasFlag(
1064       OPT_adhoc_codesign, OPT_no_adhoc_codesign,
1065       (config->arch() == AK_arm64 || config->arch() == AK_arm64e) &&
1066           config->platform() == PlatformKind::macOS);
1067 
1068   if (args.hasArg(OPT_v)) {
1069     message(getLLDVersion());
1070     message(StringRef("Library search paths:") +
1071             (config->librarySearchPaths.empty()
1072                  ? ""
1073                  : "\n\t" + join(config->librarySearchPaths, "\n\t")));
1074     message(StringRef("Framework search paths:") +
1075             (config->frameworkSearchPaths.empty()
1076                  ? ""
1077                  : "\n\t" + join(config->frameworkSearchPaths, "\n\t")));
1078   }
1079 
1080   config->progName = argsArr[0];
1081 
1082   config->timeTraceEnabled = args.hasArg(
1083       OPT_time_trace, OPT_time_trace_granularity_eq, OPT_time_trace_file_eq);
1084   config->timeTraceGranularity =
1085       args::getInteger(args, OPT_time_trace_granularity_eq, 500);
1086 
1087   // Initialize time trace profiler.
1088   if (config->timeTraceEnabled)
1089     timeTraceProfilerInitialize(config->timeTraceGranularity, config->progName);
1090 
1091   {
1092     TimeTraceScope timeScope("ExecuteLinker");
1093 
1094     initLLVM(); // must be run before any call to addFile()
1095     createFiles(args);
1096 
1097     config->isPic = config->outputType == MH_DYLIB ||
1098                     config->outputType == MH_BUNDLE || isPie(args);
1099 
1100     // Now that all dylibs have been loaded, search for those that should be
1101     // re-exported.
1102     {
1103       auto reexportHandler = [](const Arg *arg,
1104                                 const std::vector<StringRef> &extensions) {
1105         config->hasReexports = true;
1106         StringRef searchName = arg->getValue();
1107         if (!markReexport(searchName, extensions))
1108           error(arg->getSpelling() + " " + searchName +
1109                 " does not match a supplied dylib");
1110       };
1111       std::vector<StringRef> extensions = {".tbd"};
1112       for (const Arg *arg : args.filtered(OPT_sub_umbrella))
1113         reexportHandler(arg, extensions);
1114 
1115       extensions.push_back(".dylib");
1116       for (const Arg *arg : args.filtered(OPT_sub_library))
1117         reexportHandler(arg, extensions);
1118     }
1119 
1120     // Parse LTO options.
1121     if (const Arg *arg = args.getLastArg(OPT_mcpu))
1122       parseClangOption(saver.save("-mcpu=" + StringRef(arg->getValue())),
1123                        arg->getSpelling());
1124 
1125     for (const Arg *arg : args.filtered(OPT_mllvm))
1126       parseClangOption(arg->getValue(), arg->getSpelling());
1127 
1128     compileBitcodeFiles();
1129     replaceCommonSymbols();
1130 
1131     StringRef orderFile = args.getLastArgValue(OPT_order_file);
1132     if (!orderFile.empty())
1133       parseOrderFile(orderFile);
1134 
1135     if (config->outputType == MH_EXECUTE && isa<Undefined>(config->entry)) {
1136       error("undefined symbol: " + toString(*config->entry));
1137       return false;
1138     }
1139     // FIXME: This prints symbols that are undefined both in input files and
1140     // via -u flag twice.
1141     for (const Symbol *undefined : config->explicitUndefineds) {
1142       if (isa<Undefined>(undefined)) {
1143         error("undefined symbol: " + toString(*undefined) +
1144               "\n>>> referenced by flag -u " + toString(*undefined));
1145         return false;
1146       }
1147     }
1148     // Literal exported-symbol names must be defined, but glob
1149     // patterns need not match.
1150     for (const CachedHashStringRef &cachedName :
1151          config->exportedSymbols.literals) {
1152       if (const Symbol *sym = symtab->find(cachedName))
1153         if (isa<Defined>(sym))
1154           continue;
1155       error("undefined symbol " + cachedName.val() +
1156             "\n>>> referenced from option -exported_symbol(s_list)");
1157     }
1158 
1159     if (target->wordSize == 8)
1160       createSyntheticSections<LP64>();
1161     else
1162       createSyntheticSections<ILP32>();
1163 
1164     createSyntheticSymbols();
1165 
1166     for (const Arg *arg : args.filtered(OPT_sectcreate)) {
1167       StringRef segName = arg->getValue(0);
1168       StringRef sectName = arg->getValue(1);
1169       StringRef fileName = arg->getValue(2);
1170       Optional<MemoryBufferRef> buffer = readFile(fileName);
1171       if (buffer)
1172         inputFiles.insert(make<OpaqueFile>(*buffer, segName, sectName));
1173     }
1174 
1175     {
1176       TimeTraceScope timeScope("Gathering input sections");
1177       // Gather all InputSections into one vector.
1178       for (const InputFile *file : inputFiles) {
1179         for (const SubsectionMap &map : file->subsections)
1180           for (const SubsectionEntry &subsectionEntry : map)
1181             inputSections.push_back(subsectionEntry.isec);
1182       }
1183     }
1184 
1185     // Write to an output file.
1186     if (target->wordSize == 8)
1187       writeResult<LP64>();
1188     else
1189       writeResult<ILP32>();
1190 
1191     depTracker->write(getLLDVersion(), inputFiles, config->outputFile);
1192   }
1193 
1194   if (config->timeTraceEnabled) {
1195     if (auto E = timeTraceProfilerWrite(
1196             args.getLastArgValue(OPT_time_trace_file_eq).str(),
1197             config->outputFile)) {
1198       handleAllErrors(std::move(E),
1199                       [&](const StringError &SE) { error(SE.getMessage()); });
1200     }
1201 
1202     timeTraceProfilerCleanup();
1203   }
1204 
1205   if (canExitEarly)
1206     exitLld(errorCount() ? 1 : 0);
1207 
1208   return !errorCount();
1209 }
1210