xref: /llvm-project-15.0.7/lld/MachO/Driver.cpp (revision bb33f925)
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 "OutputSection.h"
13 #include "OutputSegment.h"
14 #include "SymbolTable.h"
15 #include "Symbols.h"
16 #include "SyntheticSections.h"
17 #include "Target.h"
18 #include "Writer.h"
19 
20 #include "lld/Common/Args.h"
21 #include "lld/Common/Driver.h"
22 #include "lld/Common/ErrorHandler.h"
23 #include "lld/Common/LLVM.h"
24 #include "lld/Common/Memory.h"
25 #include "lld/Common/Version.h"
26 #include "llvm/ADT/DenseSet.h"
27 #include "llvm/ADT/StringExtras.h"
28 #include "llvm/ADT/StringRef.h"
29 #include "llvm/BinaryFormat/MachO.h"
30 #include "llvm/BinaryFormat/Magic.h"
31 #include "llvm/Object/Archive.h"
32 #include "llvm/Option/ArgList.h"
33 #include "llvm/Option/Option.h"
34 #include "llvm/Support/FileSystem.h"
35 #include "llvm/Support/Host.h"
36 #include "llvm/Support/MemoryBuffer.h"
37 #include "llvm/Support/Path.h"
38 
39 using namespace llvm;
40 using namespace llvm::MachO;
41 using namespace llvm::sys;
42 using namespace llvm::opt;
43 using namespace lld;
44 using namespace lld::macho;
45 
46 Configuration *lld::macho::config;
47 
48 // Create prefix string literals used in Options.td
49 #define PREFIX(NAME, VALUE) const char *NAME[] = VALUE;
50 #include "Options.inc"
51 #undef PREFIX
52 
53 // Create table mapping all options defined in Options.td
54 static const opt::OptTable::Info optInfo[] = {
55 #define OPTION(X1, X2, ID, KIND, GROUP, ALIAS, X7, X8, X9, X10, X11, X12)      \
56   {X1, X2, X10,         X11,         OPT_##ID, opt::Option::KIND##Class,       \
57    X9, X8, OPT_##GROUP, OPT_##ALIAS, X7,       X12},
58 #include "Options.inc"
59 #undef OPTION
60 };
61 
62 MachOOptTable::MachOOptTable() : OptTable(optInfo) {}
63 
64 opt::InputArgList MachOOptTable::parse(ArrayRef<const char *> argv) {
65   // Make InputArgList from string vectors.
66   unsigned missingIndex;
67   unsigned missingCount;
68   SmallVector<const char *, 256> vec(argv.data(), argv.data() + argv.size());
69 
70   opt::InputArgList args = ParseArgs(vec, missingIndex, missingCount);
71 
72   if (missingCount)
73     error(Twine(args.getArgString(missingIndex)) + ": missing argument");
74 
75   for (opt::Arg *arg : args.filtered(OPT_UNKNOWN))
76     error("unknown argument: " + arg->getSpelling());
77   return args;
78 }
79 
80 void MachOOptTable::printHelp(const char *argv0, bool showHidden) const {
81   PrintHelp(lld::outs(), (std::string(argv0) + " [options] file...").c_str(),
82             "LLVM Linker", showHidden);
83   lld::outs() << "\n";
84 }
85 
86 static Optional<std::string> findLibrary(StringRef name) {
87   std::string stub = (llvm::Twine("lib") + name + ".tbd").str();
88   std::string shared = (llvm::Twine("lib") + name + ".dylib").str();
89   std::string archive = (llvm::Twine("lib") + name + ".a").str();
90   llvm::SmallString<260> location;
91 
92   for (StringRef dir : config->librarySearchPaths) {
93     for (StringRef library : {stub, shared, archive}) {
94       location = dir;
95       llvm::sys::path::append(location, library);
96       if (fs::exists(location))
97         return location.str().str();
98     }
99   }
100   return {};
101 }
102 
103 static Optional<std::string> findFramework(StringRef name) {
104   // TODO: support .tbd files
105   llvm::SmallString<260> symlink;
106   llvm::SmallString<260> location;
107   StringRef suffix;
108   std::tie(name, suffix) = name.split(",");
109   for (StringRef dir : config->frameworkSearchPaths) {
110     symlink = dir;
111     path::append(symlink, name + ".framework", name);
112     // If the symlink fails to resolve, skip to the next search path.
113     // NOTE: we must resolve the symlink before trying the suffixes, because
114     // there are no symlinks for the suffixed paths.
115     if (fs::real_path(symlink, location))
116       continue;
117     if (!suffix.empty()) {
118       llvm::Twine suffixed = location + suffix;
119       if (fs::exists(suffixed))
120         return suffixed.str();
121       // Suffix lookup failed, fall through to the no-suffix case.
122     }
123     if (fs::exists(location))
124       return location.str().str();
125   }
126   return {};
127 }
128 
129 static TargetInfo *createTargetInfo(opt::InputArgList &args) {
130   StringRef arch = args.getLastArgValue(OPT_arch, "x86_64");
131   config->arch = llvm::MachO::getArchitectureFromName(
132       args.getLastArgValue(OPT_arch, arch));
133   switch (config->arch) {
134   case llvm::MachO::AK_x86_64:
135   case llvm::MachO::AK_x86_64h:
136     return createX86_64TargetInfo();
137   default:
138     fatal("missing or unsupported -arch " + arch);
139   }
140 }
141 
142 static bool isDirectory(StringRef option, StringRef path) {
143   if (!fs::exists(path)) {
144     warn("directory not found for option -" + option + path);
145     return false;
146   } else if (!fs::is_directory(path)) {
147     warn("option -" + option + path + " references a non-directory path");
148     return false;
149   }
150   return true;
151 }
152 
153 static void getSearchPaths(std::vector<StringRef> &paths, unsigned optionCode,
154                            opt::InputArgList &args,
155                            const SmallVector<StringRef, 2> &systemPaths) {
156   StringRef optionLetter{(optionCode == OPT_F ? "F" : "L")};
157   for (auto const &path : args::getStrings(args, optionCode)) {
158     if (isDirectory(optionLetter, path))
159       paths.push_back(path);
160   }
161   if (!args.hasArg(OPT_Z) && Triple(sys::getProcessTriple()).isOSDarwin()) {
162     for (auto const &path : systemPaths) {
163       if (isDirectory(optionLetter, path))
164         paths.push_back(path);
165     }
166   }
167 }
168 
169 static void getLibrarySearchPaths(std::vector<StringRef> &paths,
170                                   opt::InputArgList &args) {
171   getSearchPaths(paths, OPT_L, args, {"/usr/lib", "/usr/local/lib"});
172 }
173 
174 static void getFrameworkSearchPaths(std::vector<StringRef> &paths,
175                                     opt::InputArgList &args) {
176   getSearchPaths(paths, OPT_F, args,
177                  {"/Library/Frameworks", "/System/Library/Frameworks"});
178 }
179 
180 static void addFile(StringRef path) {
181   Optional<MemoryBufferRef> buffer = readFile(path);
182   if (!buffer)
183     return;
184   MemoryBufferRef mbref = *buffer;
185 
186   switch (identify_magic(mbref.getBuffer())) {
187   case file_magic::archive: {
188     std::unique_ptr<object::Archive> file = CHECK(
189         object::Archive::create(mbref), path + ": failed to parse archive");
190 
191     if (!file->isEmpty() && !file->hasSymbolTable())
192       error(path + ": archive has no index; run ranlib to add one");
193 
194     inputFiles.push_back(make<ArchiveFile>(std::move(file)));
195     break;
196   }
197   case file_magic::macho_object:
198     inputFiles.push_back(make<ObjFile>(mbref));
199     break;
200   case file_magic::macho_dynamically_linked_shared_lib:
201     inputFiles.push_back(make<DylibFile>(mbref));
202     break;
203   case file_magic::tapi_file: {
204     llvm::Expected<std::unique_ptr<llvm::MachO::InterfaceFile>> result =
205         TextAPIReader::get(mbref);
206     if (!result)
207       return;
208 
209     inputFiles.push_back(make<DylibFile>(std::move(*result)));
210     break;
211   }
212   default:
213     error(path + ": unhandled file type");
214   }
215 }
216 
217 static void addFileList(StringRef path) {
218   Optional<MemoryBufferRef> buffer = readFile(path);
219   if (!buffer)
220     return;
221   MemoryBufferRef mbref = *buffer;
222   for (StringRef path : args::getLines(mbref))
223     addFile(path);
224 }
225 
226 static std::array<StringRef, 6> archNames{"arm",    "arm64", "i386",
227                                           "x86_64", "ppc",   "ppc64"};
228 static bool isArchString(StringRef s) {
229   static DenseSet<StringRef> archNamesSet(archNames.begin(), archNames.end());
230   return archNamesSet.find(s) != archNamesSet.end();
231 }
232 
233 // An order file has one entry per line, in the following format:
234 //
235 //   <arch>:<object file>:<symbol name>
236 //
237 // <arch> and <object file> are optional. If not specified, then that entry
238 // matches any symbol of that name.
239 //
240 // If a symbol is matched by multiple entries, then it takes the lowest-ordered
241 // entry (the one nearest to the front of the list.)
242 //
243 // The file can also have line comments that start with '#'.
244 static void parseOrderFile(StringRef path) {
245   Optional<MemoryBufferRef> buffer = readFile(path);
246   if (!buffer) {
247     error("Could not read order file at " + path);
248     return;
249   }
250 
251   MemoryBufferRef mbref = *buffer;
252   size_t priority = std::numeric_limits<size_t>::max();
253   for (StringRef rest : args::getLines(mbref)) {
254     StringRef arch, objectFile, symbol;
255 
256     std::array<StringRef, 3> fields;
257     uint8_t fieldCount = 0;
258     while (rest != "" && fieldCount < 3) {
259       std::pair<StringRef, StringRef> p = getToken(rest, ": \t\n\v\f\r");
260       StringRef tok = p.first;
261       rest = p.second;
262 
263       // Check if we have a comment
264       if (tok == "" || tok[0] == '#')
265         break;
266 
267       fields[fieldCount++] = tok;
268     }
269 
270     switch (fieldCount) {
271     case 3:
272       arch = fields[0];
273       objectFile = fields[1];
274       symbol = fields[2];
275       break;
276     case 2:
277       (isArchString(fields[0]) ? arch : objectFile) = fields[0];
278       symbol = fields[1];
279       break;
280     case 1:
281       symbol = fields[0];
282       break;
283     case 0:
284       break;
285     default:
286       llvm_unreachable("too many fields in order file");
287     }
288 
289     if (!arch.empty()) {
290       if (!isArchString(arch)) {
291         error("invalid arch \"" + arch + "\" in order file: expected one of " +
292               llvm::join(archNames, ", "));
293         continue;
294       }
295 
296       // TODO: Update when we extend support for other archs
297       if (arch != "x86_64")
298         continue;
299     }
300 
301     if (!objectFile.empty() && !objectFile.endswith(".o")) {
302       error("invalid object file name \"" + objectFile +
303             "\" in order file: should end with .o");
304       continue;
305     }
306 
307     if (!symbol.empty()) {
308       SymbolPriorityEntry &entry = config->priorities[symbol];
309       if (!objectFile.empty())
310         entry.objectFiles.insert(std::make_pair(objectFile, priority));
311       else
312         entry.anyObjectFile = std::max(entry.anyObjectFile, priority);
313     }
314 
315     --priority;
316   }
317 }
318 
319 // We expect sub-library names of the form "libfoo", which will match a dylib
320 // with a path of .*/libfoo.dylib.
321 static bool markSubLibrary(StringRef searchName) {
322   for (InputFile *file : inputFiles) {
323     if (auto *dylibFile = dyn_cast<DylibFile>(file)) {
324       StringRef filename = path::filename(dylibFile->getName());
325       if (filename.consume_front(searchName) && filename == ".dylib") {
326         dylibFile->reexport = true;
327         return true;
328       }
329     }
330   }
331   return false;
332 }
333 
334 static void handlePlatformVersion(const opt::Arg *arg) {
335   // TODO: implementation coming very soon ...
336 }
337 
338 static void warnIfDeprecatedOption(const opt::Option &opt) {
339   if (!opt.getGroup().isValid())
340     return;
341   if (opt.getGroup().getID() == OPT_grp_deprecated) {
342     warn("Option `" + opt.getPrefixedName() + "' is deprecated in ld64:");
343     warn(opt.getHelpText());
344   }
345 }
346 
347 static void warnIfUnimplementedOption(const opt::Option &opt) {
348   if (!opt.getGroup().isValid())
349     return;
350   switch (opt.getGroup().getID()) {
351   case OPT_grp_deprecated:
352     // warn about deprecated options elsewhere
353     break;
354   case OPT_grp_undocumented:
355     warn("Option `" + opt.getPrefixedName() +
356          "' is undocumented. Should lld implement it?");
357     break;
358   case OPT_grp_obsolete:
359     warn("Option `" + opt.getPrefixedName() +
360          "' is obsolete. Please modernize your usage.");
361     break;
362   case OPT_grp_ignored:
363     warn("Option `" + opt.getPrefixedName() + "' is ignored.");
364     break;
365   default:
366     warn("Option `" + opt.getPrefixedName() +
367          "' is not yet implemented. Stay tuned...");
368     break;
369   }
370 }
371 
372 bool macho::link(llvm::ArrayRef<const char *> argsArr, bool canExitEarly,
373                  raw_ostream &stdoutOS, raw_ostream &stderrOS) {
374   lld::stdoutOS = &stdoutOS;
375   lld::stderrOS = &stderrOS;
376 
377   stderrOS.enable_colors(stderrOS.has_colors());
378   // TODO: Set up error handler properly, e.g. the errorLimitExceededMsg
379 
380   MachOOptTable parser;
381   opt::InputArgList args = parser.parse(argsArr.slice(1));
382 
383   if (args.hasArg(OPT_help_hidden)) {
384     parser.printHelp(argsArr[0], /*showHidden=*/true);
385     return true;
386   } else if (args.hasArg(OPT_help)) {
387     parser.printHelp(argsArr[0], /*showHidden=*/false);
388     return true;
389   }
390 
391   config = make<Configuration>();
392   symtab = make<SymbolTable>();
393   target = createTargetInfo(args);
394 
395   config->entry = symtab->addUndefined(args.getLastArgValue(OPT_e, "_main"));
396   config->outputFile = args.getLastArgValue(OPT_o, "a.out");
397   config->installName =
398       args.getLastArgValue(OPT_install_name, config->outputFile);
399   config->headerPad = args::getHex(args, OPT_headerpad, /*Default=*/32);
400   getLibrarySearchPaths(config->librarySearchPaths, args);
401   getFrameworkSearchPaths(config->frameworkSearchPaths, args);
402   config->outputType = args.hasArg(OPT_dylib) ? MH_DYLIB : MH_EXECUTE;
403 
404   if (args.hasArg(OPT_v)) {
405     message(getLLDVersion());
406     message(StringRef("Library search paths:") +
407             (config->librarySearchPaths.size()
408                  ? "\n\t" + llvm::join(config->librarySearchPaths, "\n\t")
409                  : ""));
410     message(StringRef("Framework search paths:") +
411             (config->frameworkSearchPaths.size()
412                  ? "\n\t" + llvm::join(config->frameworkSearchPaths, "\n\t")
413                  : ""));
414     freeArena();
415     return !errorCount();
416   }
417 
418   for (const auto &arg : args) {
419     const auto &opt = arg->getOption();
420     warnIfDeprecatedOption(opt);
421     switch (arg->getOption().getID()) {
422     case OPT_INPUT:
423       addFile(arg->getValue());
424       break;
425     case OPT_filelist:
426       addFileList(arg->getValue());
427       break;
428     case OPT_l: {
429       StringRef name = arg->getValue();
430       if (Optional<std::string> path = findLibrary(name)) {
431         addFile(*path);
432         break;
433       }
434       error("library not found for -l" + name);
435       break;
436     }
437     case OPT_framework: {
438       StringRef name = arg->getValue();
439       if (Optional<std::string> path = findFramework(name)) {
440         addFile(*path);
441         break;
442       }
443       error("framework not found for -framework " + name);
444       break;
445     }
446     case OPT_platform_version:
447       handlePlatformVersion(arg);
448       break;
449     case OPT_o:
450     case OPT_dylib:
451     case OPT_e:
452     case OPT_F:
453     case OPT_L:
454     case OPT_headerpad:
455     case OPT_install_name:
456     case OPT_Z:
457     case OPT_arch:
458       // handled elsewhere
459       break;
460     default:
461       warnIfUnimplementedOption(opt);
462       break;
463     }
464   }
465 
466   // Now that all dylibs have been loaded, search for those that should be
467   // re-exported.
468   for (opt::Arg *arg : args.filtered(OPT_sub_library)) {
469     config->hasReexports = true;
470     StringRef searchName = arg->getValue();
471     if (!markSubLibrary(searchName))
472       error("-sub_library " + searchName + " does not match a supplied dylib");
473   }
474 
475   StringRef orderFile = args.getLastArgValue(OPT_order_file);
476   if (!orderFile.empty())
477     parseOrderFile(orderFile);
478 
479   if (config->outputType == MH_EXECUTE && !isa<Defined>(config->entry)) {
480     error("undefined symbol: " + config->entry->getName());
481     return false;
482   }
483 
484   createSyntheticSections();
485   symtab->addDSOHandle(in.header);
486 
487   // Initialize InputSections.
488   for (InputFile *file : inputFiles) {
489     for (SubsectionMap &map : file->subsections) {
490       for (auto &p : map) {
491         InputSection *isec = p.second;
492         inputSections.push_back(isec);
493       }
494     }
495   }
496 
497   // Write to an output file.
498   writeResult();
499 
500   if (canExitEarly)
501     exitLld(errorCount() ? 1 : 0);
502 
503   freeArena();
504   return !errorCount();
505 }
506