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