xref: /llvm-project-15.0.7/lld/MachO/Driver.cpp (revision fd941036)
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->target = MachO::Target(getArchitectureFromName(archName), platform);
620 
621   switch (getCPUTypeFromArchitecture(config->target.Arch).first) {
622   case CPU_TYPE_X86_64:
623     return createX86_64TargetInfo();
624   case CPU_TYPE_ARM64:
625     return createARM64TargetInfo();
626   case CPU_TYPE_ARM64_32:
627     return createARM64_32TargetInfo();
628   default:
629     fatal("missing or unsupported -arch " + archName);
630   }
631 }
632 
633 static UndefinedSymbolTreatment
634 getUndefinedSymbolTreatment(const ArgList &args) {
635   StringRef treatmentStr = args.getLastArgValue(OPT_undefined);
636   auto treatment =
637       StringSwitch<UndefinedSymbolTreatment>(treatmentStr)
638           .Cases("error", "", UndefinedSymbolTreatment::error)
639           .Case("warning", UndefinedSymbolTreatment::warning)
640           .Case("suppress", UndefinedSymbolTreatment::suppress)
641           .Case("dynamic_lookup", UndefinedSymbolTreatment::dynamic_lookup)
642           .Default(UndefinedSymbolTreatment::unknown);
643   if (treatment == UndefinedSymbolTreatment::unknown) {
644     warn(Twine("unknown -undefined TREATMENT '") + treatmentStr +
645          "', defaulting to 'error'");
646     treatment = UndefinedSymbolTreatment::error;
647   } else if (config->namespaceKind == NamespaceKind::twolevel &&
648              (treatment == UndefinedSymbolTreatment::warning ||
649               treatment == UndefinedSymbolTreatment::suppress)) {
650     if (treatment == UndefinedSymbolTreatment::warning)
651       error("'-undefined warning' only valid with '-flat_namespace'");
652     else
653       error("'-undefined suppress' only valid with '-flat_namespace'");
654     treatment = UndefinedSymbolTreatment::error;
655   }
656   return treatment;
657 }
658 
659 static void warnIfDeprecatedOption(const Option &opt) {
660   if (!opt.getGroup().isValid())
661     return;
662   if (opt.getGroup().getID() == OPT_grp_deprecated) {
663     warn("Option `" + opt.getPrefixedName() + "' is deprecated in ld64:");
664     warn(opt.getHelpText());
665   }
666 }
667 
668 static void warnIfUnimplementedOption(const Option &opt) {
669   if (!opt.getGroup().isValid() || !opt.hasFlag(DriverFlag::HelpHidden))
670     return;
671   switch (opt.getGroup().getID()) {
672   case OPT_grp_deprecated:
673     // warn about deprecated options elsewhere
674     break;
675   case OPT_grp_undocumented:
676     warn("Option `" + opt.getPrefixedName() +
677          "' is undocumented. Should lld implement it?");
678     break;
679   case OPT_grp_obsolete:
680     warn("Option `" + opt.getPrefixedName() +
681          "' is obsolete. Please modernize your usage.");
682     break;
683   case OPT_grp_ignored:
684     warn("Option `" + opt.getPrefixedName() + "' is ignored.");
685     break;
686   default:
687     warn("Option `" + opt.getPrefixedName() +
688          "' is not yet implemented. Stay tuned...");
689     break;
690   }
691 }
692 
693 static const char *getReproduceOption(InputArgList &args) {
694   if (const Arg *arg = args.getLastArg(OPT_reproduce))
695     return arg->getValue();
696   return getenv("LLD_REPRODUCE");
697 }
698 
699 static bool isPie(InputArgList &args) {
700   if (config->outputType != MH_EXECUTE || args.hasArg(OPT_no_pie))
701     return false;
702   if (config->target.Arch == AK_arm64 || config->target.Arch == AK_arm64e ||
703       config->target.Arch == AK_arm64_32)
704     return true;
705 
706   // TODO: add logic here as we support more archs. E.g. i386 should default
707   // to PIE from 10.7
708   assert(config->target.Arch == AK_x86_64 ||
709          config->target.Arch == AK_x86_64h ||
710          config->target.Arch == AK_arm64_32);
711 
712   PlatformKind kind = config->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   if (args.hasArg(OPT_help_hidden)) {
880     parser.printHelp(argsArr[0], /*showHidden=*/true);
881     return true;
882   }
883   if (args.hasArg(OPT_help)) {
884     parser.printHelp(argsArr[0], /*showHidden=*/false);
885     return true;
886   }
887   if (args.hasArg(OPT_version)) {
888     message(getLLDVersion());
889     return true;
890   }
891 
892   if (const char *path = getReproduceOption(args)) {
893     // Note that --reproduce is a debug option so you can ignore it
894     // if you are trying to understand the whole picture of the code.
895     Expected<std::unique_ptr<TarWriter>> errOrWriter =
896         TarWriter::create(path, path::stem(path));
897     if (errOrWriter) {
898       tar = std::move(*errOrWriter);
899       tar->append("response.txt", createResponseFile(args));
900       tar->append("version.txt", getLLDVersion() + "\n");
901     } else {
902       error("--reproduce: " + toString(errOrWriter.takeError()));
903     }
904   }
905 
906   config = make<Configuration>();
907   symtab = make<SymbolTable>();
908   target = createTargetInfo(args);
909 
910   depTracker =
911       make<DependencyTracker>(args.getLastArgValue(OPT_dependency_info, ""));
912 
913   if (auto *arg = args.getLastArg(OPT_threads_eq)) {
914     StringRef v(arg->getValue());
915     unsigned threads = 0;
916     if (!llvm::to_integer(v, threads, 0) || threads == 0)
917       error(arg->getSpelling() + ": expected a positive integer, but got '" +
918             arg->getValue() + "'");
919     parallel::strategy = hardware_concurrency(threads);
920     config->thinLTOJobs = v;
921   }
922   if (auto *arg = args.getLastArg(OPT_thinlto_jobs_eq))
923     config->thinLTOJobs = arg->getValue();
924   if (!get_threadpool_strategy(config->thinLTOJobs))
925     error("--thinlto-jobs: invalid job count: " + config->thinLTOJobs);
926 
927   config->entry = symtab->addUndefined(args.getLastArgValue(OPT_e, "_main"),
928                                        /*file=*/nullptr,
929                                        /*isWeakRef=*/false);
930   for (const Arg *arg : args.filtered(OPT_u)) {
931     config->explicitUndefineds.push_back(symtab->addUndefined(
932         arg->getValue(), /*file=*/nullptr, /*isWeakRef=*/false));
933   }
934 
935   for (const Arg *arg : args.filtered(OPT_U))
936     symtab->addDynamicLookup(arg->getValue());
937 
938   config->mapFile = args.getLastArgValue(OPT_map);
939   config->outputFile = args.getLastArgValue(OPT_o, "a.out");
940   config->astPaths = args.getAllArgValues(OPT_add_ast_path);
941   config->headerPad = args::getHex(args, OPT_headerpad, /*Default=*/32);
942   config->headerPadMaxInstallNames =
943       args.hasArg(OPT_headerpad_max_install_names);
944   config->printEachFile = args.hasArg(OPT_t);
945   config->printWhyLoad = args.hasArg(OPT_why_load);
946   config->outputType = getOutputType(args);
947   if (const Arg *arg = args.getLastArg(OPT_bundle_loader)) {
948     if (config->outputType != MH_BUNDLE)
949       error("-bundle_loader can only be used with MachO bundle output");
950     addFile(arg->getValue(), false, true);
951   }
952   config->ltoObjPath = args.getLastArgValue(OPT_object_path_lto);
953   config->ltoNewPassManager =
954       args.hasFlag(OPT_no_lto_legacy_pass_manager, OPT_lto_legacy_pass_manager,
955                    LLVM_ENABLE_NEW_PASS_MANAGER);
956   config->runtimePaths = args::getStrings(args, OPT_rpath);
957   config->allLoad = args.hasArg(OPT_all_load);
958   config->forceLoadObjC = args.hasArg(OPT_ObjC);
959   config->demangle = args.hasArg(OPT_demangle);
960   config->implicitDylibs = !args.hasArg(OPT_no_implicit_dylibs);
961   config->emitFunctionStarts = !args.hasArg(OPT_no_function_starts);
962   config->emitBitcodeBundle = args.hasArg(OPT_bitcode_bundle);
963 
964 #ifndef HAVE_LIBXAR
965   if (config->emitBitcodeBundle)
966     error("-bitcode_bundle unsupported because LLD wasn't built with libxar");
967 #endif
968 
969   if (const Arg *arg = args.getLastArg(OPT_install_name)) {
970     if (config->outputType != MH_DYLIB)
971       warn(arg->getAsString(args) + ": ignored, only has effect with -dylib");
972     else
973       config->installName = arg->getValue();
974   } else if (config->outputType == MH_DYLIB) {
975     config->installName = config->outputFile;
976   }
977 
978   if (args.hasArg(OPT_mark_dead_strippable_dylib)) {
979     if (config->outputType != MH_DYLIB)
980       warn("-mark_dead_strippable_dylib: ignored, only has effect with -dylib");
981     else
982       config->markDeadStrippableDylib = true;
983   }
984 
985   if (const Arg *arg = args.getLastArg(OPT_static, OPT_dynamic))
986     config->staticLink = (arg->getOption().getID() == OPT_static);
987 
988   if (const Arg *arg =
989           args.getLastArg(OPT_flat_namespace, OPT_twolevel_namespace))
990     config->namespaceKind = arg->getOption().getID() == OPT_twolevel_namespace
991                                 ? NamespaceKind::twolevel
992                                 : NamespaceKind::flat;
993 
994   config->undefinedSymbolTreatment = getUndefinedSymbolTreatment(args);
995 
996   config->systemLibraryRoots = getSystemLibraryRoots(args);
997   config->librarySearchPaths =
998       getLibrarySearchPaths(args, config->systemLibraryRoots);
999   config->frameworkSearchPaths =
1000       getFrameworkSearchPaths(args, config->systemLibraryRoots);
1001   if (const Arg *arg =
1002           args.getLastArg(OPT_search_paths_first, OPT_search_dylibs_first))
1003     config->searchDylibsFirst =
1004         arg->getOption().getID() == OPT_search_dylibs_first;
1005 
1006   config->dylibCompatibilityVersion =
1007       parseDylibVersion(args, OPT_compatibility_version);
1008   config->dylibCurrentVersion = parseDylibVersion(args, OPT_current_version);
1009 
1010   // Reject every special character except '.' and '$'
1011   // TODO(gkm): verify that this is the proper set of invalid chars
1012   StringRef invalidNameChars("!\"#%&'()*+,-/:;<=>?@[\\]^`{|}~");
1013   auto validName = [invalidNameChars](StringRef s) {
1014     if (s.find_first_of(invalidNameChars) != StringRef::npos)
1015       error("invalid name for segment or section: " + s);
1016     return s;
1017   };
1018   for (const Arg *arg : args.filtered(OPT_rename_section)) {
1019     config->sectionRenameMap[{validName(arg->getValue(0)),
1020                               validName(arg->getValue(1))}] = {
1021         validName(arg->getValue(2)), validName(arg->getValue(3))};
1022   }
1023   for (const Arg *arg : args.filtered(OPT_rename_segment)) {
1024     config->segmentRenameMap[validName(arg->getValue(0))] =
1025         validName(arg->getValue(1));
1026   }
1027 
1028   for (const Arg *arg : args.filtered(OPT_segprot)) {
1029     StringRef segName = arg->getValue(0);
1030     uint32_t maxProt = parseProtection(arg->getValue(1));
1031     uint32_t initProt = parseProtection(arg->getValue(2));
1032     if (maxProt != initProt && config->target.Arch != AK_i386)
1033       error("invalid argument '" + arg->getAsString(args) +
1034             "': max and init must be the same for non-i386 archs");
1035     if (segName == segment_names::linkEdit)
1036       error("-segprot cannot be used to change __LINKEDIT's protections");
1037     config->segmentProtections.push_back({segName, maxProt, initProt});
1038   }
1039 
1040   handleSymbolPatterns(args, config->exportedSymbols, OPT_exported_symbol,
1041                        OPT_exported_symbols_list);
1042   handleSymbolPatterns(args, config->unexportedSymbols, OPT_unexported_symbol,
1043                        OPT_unexported_symbols_list);
1044   if (!config->exportedSymbols.empty() && !config->unexportedSymbols.empty()) {
1045     error("cannot use both -exported_symbol* and -unexported_symbol* options\n"
1046           ">>> ignoring unexports");
1047     config->unexportedSymbols.clear();
1048   }
1049 
1050   config->saveTemps = args.hasArg(OPT_save_temps);
1051 
1052   config->adhocCodesign = args.hasFlag(
1053       OPT_adhoc_codesign, OPT_no_adhoc_codesign,
1054       (config->target.Arch == AK_arm64 || config->target.Arch == AK_arm64e) &&
1055           config->target.Platform == PlatformKind::macOS);
1056 
1057   if (args.hasArg(OPT_v)) {
1058     message(getLLDVersion());
1059     message(StringRef("Library search paths:") +
1060             (config->librarySearchPaths.empty()
1061                  ? ""
1062                  : "\n\t" + join(config->librarySearchPaths, "\n\t")));
1063     message(StringRef("Framework search paths:") +
1064             (config->frameworkSearchPaths.empty()
1065                  ? ""
1066                  : "\n\t" + join(config->frameworkSearchPaths, "\n\t")));
1067   }
1068 
1069   config->progName = argsArr[0];
1070 
1071   config->timeTraceEnabled = args.hasArg(
1072       OPT_time_trace, OPT_time_trace_granularity_eq, OPT_time_trace_file_eq);
1073   config->timeTraceGranularity =
1074       args::getInteger(args, OPT_time_trace_granularity_eq, 500);
1075 
1076   // Initialize time trace profiler.
1077   if (config->timeTraceEnabled)
1078     timeTraceProfilerInitialize(config->timeTraceGranularity, config->progName);
1079 
1080   {
1081     TimeTraceScope timeScope("ExecuteLinker");
1082 
1083     initLLVM(); // must be run before any call to addFile()
1084     createFiles(args);
1085 
1086     config->isPic = config->outputType == MH_DYLIB ||
1087                     config->outputType == MH_BUNDLE || isPie(args);
1088 
1089     // Now that all dylibs have been loaded, search for those that should be
1090     // re-exported.
1091     {
1092       auto reexportHandler = [](const Arg *arg,
1093                                 const std::vector<StringRef> &extensions) {
1094         config->hasReexports = true;
1095         StringRef searchName = arg->getValue();
1096         if (!markReexport(searchName, extensions))
1097           error(arg->getSpelling() + " " + searchName +
1098                 " does not match a supplied dylib");
1099       };
1100       std::vector<StringRef> extensions = {".tbd"};
1101       for (const Arg *arg : args.filtered(OPT_sub_umbrella))
1102         reexportHandler(arg, extensions);
1103 
1104       extensions.push_back(".dylib");
1105       for (const Arg *arg : args.filtered(OPT_sub_library))
1106         reexportHandler(arg, extensions);
1107     }
1108 
1109     // Parse LTO options.
1110     if (const Arg *arg = args.getLastArg(OPT_mcpu))
1111       parseClangOption(saver.save("-mcpu=" + StringRef(arg->getValue())),
1112                        arg->getSpelling());
1113 
1114     for (const Arg *arg : args.filtered(OPT_mllvm))
1115       parseClangOption(arg->getValue(), arg->getSpelling());
1116 
1117     compileBitcodeFiles();
1118     replaceCommonSymbols();
1119 
1120     StringRef orderFile = args.getLastArgValue(OPT_order_file);
1121     if (!orderFile.empty())
1122       parseOrderFile(orderFile);
1123 
1124     if (config->outputType == MH_EXECUTE && isa<Undefined>(config->entry)) {
1125       error("undefined symbol: " + toString(*config->entry));
1126       return false;
1127     }
1128     // FIXME: This prints symbols that are undefined both in input files and
1129     // via -u flag twice.
1130     for (const Symbol *undefined : config->explicitUndefineds) {
1131       if (isa<Undefined>(undefined)) {
1132         error("undefined symbol: " + toString(*undefined) +
1133               "\n>>> referenced by flag -u " + toString(*undefined));
1134         return false;
1135       }
1136     }
1137     // Literal exported-symbol names must be defined, but glob
1138     // patterns need not match.
1139     for (const CachedHashStringRef &cachedName :
1140          config->exportedSymbols.literals) {
1141       if (const Symbol *sym = symtab->find(cachedName))
1142         if (isa<Defined>(sym))
1143           continue;
1144       error("undefined symbol " + cachedName.val() +
1145             "\n>>> referenced from option -exported_symbol(s_list)");
1146     }
1147 
1148     if (target->wordSize == 8)
1149       createSyntheticSections<LP64>();
1150     else
1151       createSyntheticSections<ILP32>();
1152 
1153     createSyntheticSymbols();
1154 
1155     for (const Arg *arg : args.filtered(OPT_sectcreate)) {
1156       StringRef segName = arg->getValue(0);
1157       StringRef sectName = arg->getValue(1);
1158       StringRef fileName = arg->getValue(2);
1159       Optional<MemoryBufferRef> buffer = readFile(fileName);
1160       if (buffer)
1161         inputFiles.insert(make<OpaqueFile>(*buffer, segName, sectName));
1162     }
1163 
1164     {
1165       TimeTraceScope timeScope("Gathering input sections");
1166       // Gather all InputSections into one vector.
1167       for (const InputFile *file : inputFiles) {
1168         for (const SubsectionMap &map : file->subsections)
1169           for (const SubsectionEntry &subsectionEntry : map)
1170             inputSections.push_back(subsectionEntry.isec);
1171       }
1172     }
1173 
1174     // Write to an output file.
1175     if (target->wordSize == 8)
1176       writeResult<LP64>();
1177     else
1178       writeResult<ILP32>();
1179 
1180     depTracker->write(getLLDVersion(), inputFiles, config->outputFile);
1181   }
1182 
1183   if (config->timeTraceEnabled) {
1184     if (auto E = timeTraceProfilerWrite(
1185             args.getLastArgValue(OPT_time_trace_file_eq).str(),
1186             config->outputFile)) {
1187       handleAllErrors(std::move(E),
1188                       [&](const StringError &SE) { error(SE.getMessage()); });
1189     }
1190 
1191     timeTraceProfilerCleanup();
1192   }
1193 
1194   if (canExitEarly)
1195     exitLld(errorCount() ? 1 : 0);
1196 
1197   return !errorCount();
1198 }
1199