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