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