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