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