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