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