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