xref: /llvm-project/lld/MachO/DriverUtils.cpp (revision dd647e3e608ed0b2bac7c588d5859b80ef4a5976)
1 //===- DriverUtils.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 "Config.h"
10 #include "Driver.h"
11 #include "InputFiles.h"
12 #include "ObjC.h"
13 #include "Target.h"
14 
15 #include "lld/Common/Args.h"
16 #include "lld/Common/CommonLinkerContext.h"
17 #include "lld/Common/Reproduce.h"
18 #include "llvm/ADT/CachedHashString.h"
19 #include "llvm/ADT/DenseMap.h"
20 #include "llvm/LTO/LTO.h"
21 #include "llvm/Option/Arg.h"
22 #include "llvm/Option/ArgList.h"
23 #include "llvm/Option/Option.h"
24 #include "llvm/Support/CommandLine.h"
25 #include "llvm/Support/FileSystem.h"
26 #include "llvm/Support/Path.h"
27 #include "llvm/TextAPI/InterfaceFile.h"
28 #include "llvm/TextAPI/TextAPIReader.h"
29 
30 using namespace llvm;
31 using namespace llvm::MachO;
32 using namespace llvm::opt;
33 using namespace llvm::sys;
34 using namespace lld;
35 using namespace lld::macho;
36 
37 #define OPTTABLE_STR_TABLE_CODE
38 #include "Options.inc"
39 #undef OPTTABLE_STR_TABLE_CODE
40 
41 // Create prefix string literals used in Options.td
42 #define OPTTABLE_PREFIXES_TABLE_CODE
43 #include "Options.inc"
44 #undef OPTTABLE_PREFIXES_TABLE_CODE
45 
46 // Create table mapping all options defined in Options.td
47 static constexpr OptTable::Info optInfo[] = {
48 #define OPTION(PREFIX, NAME, ID, KIND, GROUP, ALIAS, ALIASARGS, FLAGS,         \
49                VISIBILITY, PARAM, HELPTEXT, HELPTEXTSFORVARIANTS, METAVAR,     \
50                VALUES)                                                         \
51   {PREFIX,                                                                     \
52    NAME,                                                                       \
53    HELPTEXT,                                                                   \
54    HELPTEXTSFORVARIANTS,                                                       \
55    METAVAR,                                                                    \
56    OPT_##ID,                                                                   \
57    opt::Option::KIND##Class,                                                   \
58    PARAM,                                                                      \
59    FLAGS,                                                                      \
60    VISIBILITY,                                                                 \
61    OPT_##GROUP,                                                                \
62    OPT_##ALIAS,                                                                \
63    ALIASARGS,                                                                  \
64    VALUES},
65 #include "Options.inc"
66 #undef OPTION
67 };
68 
69 MachOOptTable::MachOOptTable()
70     : GenericOptTable(OptionStrTable, OptionPrefixesTable, optInfo) {}
71 
72 // Set color diagnostics according to --color-diagnostics={auto,always,never}
73 // or --no-color-diagnostics flags.
74 static void handleColorDiagnostics(CommonLinkerContext &ctx,
75                                    InputArgList &args) {
76   const Arg *arg =
77       args.getLastArg(OPT_color_diagnostics, OPT_color_diagnostics_eq,
78                       OPT_no_color_diagnostics);
79   if (!arg)
80     return;
81   auto &errs = ctx.e.errs();
82   if (arg->getOption().getID() == OPT_color_diagnostics) {
83     errs.enable_colors(true);
84   } else if (arg->getOption().getID() == OPT_no_color_diagnostics) {
85     errs.enable_colors(false);
86   } else {
87     StringRef s = arg->getValue();
88     if (s == "always")
89       errs.enable_colors(true);
90     else if (s == "never")
91       errs.enable_colors(false);
92     else if (s != "auto")
93       error("unknown option: --color-diagnostics=" + s);
94   }
95 }
96 
97 InputArgList MachOOptTable::parse(CommonLinkerContext &ctx,
98                                   ArrayRef<const char *> argv) {
99   // Make InputArgList from string vectors.
100   unsigned missingIndex;
101   unsigned missingCount;
102   SmallVector<const char *, 256> vec(argv.data(), argv.data() + argv.size());
103 
104   // Expand response files (arguments in the form of @<filename>)
105   // and then parse the argument again.
106   cl::ExpandResponseFiles(saver(), cl::TokenizeGNUCommandLine, vec);
107   InputArgList args = ParseArgs(vec, missingIndex, missingCount);
108 
109   // Handle -fatal_warnings early since it converts missing argument warnings
110   // to errors.
111   errorHandler().fatalWarnings = args.hasArg(OPT_fatal_warnings);
112   errorHandler().suppressWarnings = args.hasArg(OPT_w);
113 
114   if (missingCount)
115     error(Twine(args.getArgString(missingIndex)) + ": missing argument");
116 
117   handleColorDiagnostics(ctx, args);
118 
119   for (const Arg *arg : args.filtered(OPT_UNKNOWN)) {
120     std::string nearest;
121     if (findNearest(arg->getAsString(args), nearest) > 1)
122       error("unknown argument '" + arg->getAsString(args) + "'");
123     else
124       error("unknown argument '" + arg->getAsString(args) +
125             "', did you mean '" + nearest + "'");
126   }
127   return args;
128 }
129 
130 void MachOOptTable::printHelp(CommonLinkerContext &ctx, const char *argv0,
131                               bool showHidden) const {
132   auto &outs = ctx.e.outs();
133   OptTable::printHelp(outs, (std::string(argv0) + " [options] file...").c_str(),
134                       "LLVM Linker", showHidden);
135   outs << '\n';
136 }
137 
138 static std::string rewritePath(StringRef s) {
139   if (fs::exists(s))
140     return relativeToRoot(s);
141   return std::string(s);
142 }
143 
144 static std::string rewriteInputPath(StringRef s) {
145   // Don't bother rewriting "absolute" paths that are actually under the
146   // syslibroot; simply rewriting the syslibroot is sufficient.
147   if (rerootPath(s) == s && fs::exists(s))
148     return relativeToRoot(s);
149   return std::string(s);
150 }
151 
152 // Reconstructs command line arguments so that so that you can re-run
153 // the same command with the same inputs. This is for --reproduce.
154 std::string macho::createResponseFile(const InputArgList &args) {
155   SmallString<0> data;
156   raw_svector_ostream os(data);
157 
158   // Copy the command line to the output while rewriting paths.
159   for (const Arg *arg : args) {
160     switch (arg->getOption().getID()) {
161     case OPT_reproduce:
162       break;
163     case OPT_INPUT:
164       os << quote(rewriteInputPath(arg->getValue())) << "\n";
165       break;
166     case OPT_o:
167       os << "-o " << quote(path::filename(arg->getValue())) << "\n";
168       break;
169     case OPT_filelist:
170       if (std::optional<MemoryBufferRef> buffer = readFile(arg->getValue()))
171         for (StringRef path : args::getLines(*buffer))
172           os << quote(rewriteInputPath(path)) << "\n";
173       break;
174     case OPT_force_load:
175     case OPT_weak_library:
176     case OPT_load_hidden:
177       os << arg->getSpelling() << " "
178          << quote(rewriteInputPath(arg->getValue())) << "\n";
179       break;
180     case OPT_F:
181     case OPT_L:
182     case OPT_bundle_loader:
183     case OPT_exported_symbols_list:
184     case OPT_order_file:
185     case OPT_syslibroot:
186     case OPT_unexported_symbols_list:
187       os << arg->getSpelling() << " " << quote(rewritePath(arg->getValue()))
188          << "\n";
189       break;
190     case OPT_sectcreate:
191       os << arg->getSpelling() << " " << quote(arg->getValue(0)) << " "
192          << quote(arg->getValue(1)) << " "
193          << quote(rewritePath(arg->getValue(2))) << "\n";
194       break;
195     default:
196       os << toString(*arg) << "\n";
197     }
198   }
199   return std::string(data);
200 }
201 
202 static void searchedDylib(const Twine &path, bool found) {
203   if (config->printDylibSearch)
204     message("searched " + path + (found ? ", found " : ", not found"));
205   if (!found)
206     depTracker->logFileNotFound(path);
207 }
208 
209 std::optional<StringRef> macho::resolveDylibPath(StringRef dylibPath) {
210   // TODO: if a tbd and dylib are both present, we should check to make sure
211   // they are consistent.
212   SmallString<261> tbdPath = dylibPath;
213   path::replace_extension(tbdPath, ".tbd");
214   bool tbdExists = fs::exists(tbdPath);
215   searchedDylib(tbdPath, tbdExists);
216   if (tbdExists)
217     return saver().save(tbdPath.str());
218 
219   bool dylibExists = fs::exists(dylibPath);
220   searchedDylib(dylibPath, dylibExists);
221   if (dylibExists)
222     return saver().save(dylibPath);
223   return {};
224 }
225 
226 // It's not uncommon to have multiple attempts to load a single dylib,
227 // especially if it's a commonly re-exported core library.
228 static DenseMap<CachedHashStringRef, DylibFile *> loadedDylibs;
229 
230 DylibFile *macho::loadDylib(MemoryBufferRef mbref, DylibFile *umbrella,
231                             bool isBundleLoader, bool explicitlyLinked) {
232   CachedHashStringRef path(mbref.getBufferIdentifier());
233   DylibFile *&file = loadedDylibs[path];
234   if (file) {
235     if (explicitlyLinked)
236       file->setExplicitlyLinked();
237     return file;
238   }
239 
240   DylibFile *newFile;
241   file_magic magic = identify_magic(mbref.getBuffer());
242   if (magic == file_magic::tapi_file) {
243     Expected<std::unique_ptr<InterfaceFile>> result = TextAPIReader::get(mbref);
244     if (!result) {
245       error("could not load TAPI file at " + mbref.getBufferIdentifier() +
246             ": " + toString(result.takeError()));
247       return nullptr;
248     }
249     file =
250         make<DylibFile>(**result, umbrella, isBundleLoader, explicitlyLinked);
251 
252     // parseReexports() can recursively call loadDylib(). That's fine since
253     // we wrote the DylibFile we just loaded to the loadDylib cache via the
254     // `file` reference. But the recursive load can grow loadDylibs, so the
255     // `file` reference might become invalid after parseReexports() -- so copy
256     // the pointer it refers to before continuing.
257     newFile = file;
258     if (newFile->exportingFile)
259       newFile->parseReexports(**result);
260   } else {
261     assert(magic == file_magic::macho_dynamically_linked_shared_lib ||
262            magic == file_magic::macho_dynamically_linked_shared_lib_stub ||
263            magic == file_magic::macho_executable ||
264            magic == file_magic::macho_bundle);
265     file = make<DylibFile>(mbref, umbrella, isBundleLoader, explicitlyLinked);
266 
267     // parseLoadCommands() can also recursively call loadDylib(). See comment
268     // in previous block for why this means we must copy `file` here.
269     newFile = file;
270     if (newFile->exportingFile)
271       newFile->parseLoadCommands(mbref);
272   }
273 
274   if (explicitlyLinked && !newFile->allowableClients.empty()) {
275     bool allowed = std::any_of(
276         newFile->allowableClients.begin(), newFile->allowableClients.end(),
277         [&](StringRef allowableClient) {
278           // We only do a prefix match to match LD64's behaviour.
279           return allowableClient.starts_with(config->clientName);
280         });
281 
282     // TODO: This behaviour doesn't quite match the latest available source
283     // release of LD64 (ld64-951.9), which allows "parents" and "siblings"
284     // to link to libraries even when they're not explicitly named as
285     // allowable clients. However, behaviour around this seems to have
286     // changed in the latest release of Xcode (ld64-1115.7.3), so it's not
287     // clear what the correct thing to do is yet.
288     if (!allowed)
289       error("cannot link directly with '" +
290             sys::path::filename(newFile->installName) + "' because " +
291             config->clientName + " is not an allowed client");
292   }
293   return newFile;
294 }
295 
296 void macho::resetLoadedDylibs() { loadedDylibs.clear(); }
297 
298 std::optional<StringRef>
299 macho::findPathCombination(const Twine &name,
300                            const std::vector<StringRef> &roots,
301                            ArrayRef<StringRef> extensions) {
302   SmallString<261> base;
303   for (StringRef dir : roots) {
304     base = dir;
305     path::append(base, name);
306     for (StringRef ext : extensions) {
307       Twine location = base + ext;
308       bool exists = fs::exists(location);
309       searchedDylib(location, exists);
310       if (exists)
311         return saver().save(location.str());
312     }
313   }
314   return {};
315 }
316 
317 StringRef macho::rerootPath(StringRef path) {
318   if (!path::is_absolute(path, path::Style::posix) || path.ends_with(".o"))
319     return path;
320 
321   if (std::optional<StringRef> rerootedPath =
322           findPathCombination(path, config->systemLibraryRoots))
323     return *rerootedPath;
324 
325   return path;
326 }
327 
328 uint32_t macho::getModTime(StringRef path) {
329   if (config->zeroModTime)
330     return 0;
331 
332   fs::file_status stat;
333   if (!fs::status(path, stat))
334     if (fs::exists(stat))
335       return toTimeT(stat.getLastModificationTime());
336 
337   warn("failed to get modification time of " + path);
338   return 0;
339 }
340 
341 void macho::printArchiveMemberLoad(StringRef reason, const InputFile *f) {
342   if (config->printEachFile)
343     message(toString(f));
344   if (config->printWhyLoad)
345     message(reason + " forced load of " + toString(f));
346 }
347 
348 macho::DependencyTracker::DependencyTracker(StringRef path)
349     : path(path), active(!path.empty()) {
350   if (active && fs::exists(path) && !fs::can_write(path)) {
351     warn("Ignoring dependency_info option since specified path is not "
352          "writeable.");
353     active = false;
354   }
355 }
356 
357 void macho::DependencyTracker::write(StringRef version,
358                                      const SetVector<InputFile *> &inputs,
359                                      StringRef output) {
360   if (!active)
361     return;
362 
363   std::error_code ec;
364   raw_fd_ostream os(path, ec, fs::OF_None);
365   if (ec) {
366     warn("Error writing dependency info to file");
367     return;
368   }
369 
370   auto addDep = [&os](DepOpCode opcode, const StringRef &path) {
371     // XXX: Even though DepOpCode's underlying type is uint8_t,
372     // this cast is still needed because Clang older than 10.x has a bug,
373     // where it doesn't know to cast the enum to its underlying type.
374     // Hence `<< DepOpCode` is ambiguous to it.
375     os << static_cast<uint8_t>(opcode);
376     os << path;
377     os << '\0';
378   };
379 
380   addDep(DepOpCode::Version, version);
381 
382   // Sort the input by its names.
383   std::vector<StringRef> inputNames;
384   inputNames.reserve(inputs.size());
385   for (InputFile *f : inputs)
386     inputNames.push_back(f->getName());
387   llvm::sort(inputNames);
388 
389   for (const StringRef &in : inputNames)
390     addDep(DepOpCode::Input, in);
391 
392   for (const std::string &f : notFounds)
393     addDep(DepOpCode::NotFound, f);
394 
395   addDep(DepOpCode::Output, output);
396 }
397