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