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