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   errorHandler().suppressWarnings = args.hasArg(OPT_w);
91 
92   if (missingCount)
93     error(Twine(args.getArgString(missingIndex)) + ": missing argument");
94 
95   handleColorDiagnostics(args);
96 
97   for (const Arg *arg : args.filtered(OPT_UNKNOWN)) {
98     std::string nearest;
99     if (findNearest(arg->getAsString(args), nearest) > 1)
100       error("unknown argument '" + arg->getAsString(args) + "'");
101     else
102       error("unknown argument '" + arg->getAsString(args) +
103             "', did you mean '" + nearest + "'");
104   }
105   return args;
106 }
107 
108 void MachOOptTable::printHelp(const char *argv0, bool showHidden) const {
109   OptTable::printHelp(lld::outs(),
110                       (std::string(argv0) + " [options] file...").c_str(),
111                       "LLVM Linker", showHidden);
112   lld::outs() << "\n";
113 }
114 
115 static std::string rewritePath(StringRef s) {
116   if (fs::exists(s))
117     return relativeToRoot(s);
118   return std::string(s);
119 }
120 
121 static std::string rewriteInputPath(StringRef s) {
122   // Don't bother rewriting "absolute" paths that are actually under the
123   // syslibroot; simply rewriting the syslibroot is sufficient.
124   if (rerootPath(s) == s && fs::exists(s))
125     return relativeToRoot(s);
126   return std::string(s);
127 }
128 
129 // Reconstructs command line arguments so that so that you can re-run
130 // the same command with the same inputs. This is for --reproduce.
131 std::string macho::createResponseFile(const InputArgList &args) {
132   SmallString<0> data;
133   raw_svector_ostream os(data);
134 
135   // Copy the command line to the output while rewriting paths.
136   for (const Arg *arg : args) {
137     switch (arg->getOption().getID()) {
138     case OPT_reproduce:
139       break;
140     case OPT_INPUT:
141       os << quote(rewriteInputPath(arg->getValue())) << "\n";
142       break;
143     case OPT_o:
144       os << "-o " << quote(path::filename(arg->getValue())) << "\n";
145       break;
146     case OPT_filelist:
147       if (Optional<MemoryBufferRef> buffer = readFile(arg->getValue()))
148         for (StringRef path : args::getLines(*buffer))
149           os << quote(rewriteInputPath(path)) << "\n";
150       break;
151     case OPT_force_load:
152     case OPT_weak_library:
153     case OPT_load_hidden:
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<StringRef> 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   SmallString<261> tbdPath = dylibPath;
191   path::replace_extension(tbdPath, ".tbd");
192   bool tbdExists = fs::exists(tbdPath);
193   searchedDylib(tbdPath, tbdExists);
194   if (tbdExists)
195     return saver().save(tbdPath.str());
196 
197   bool dylibExists = fs::exists(dylibPath);
198   searchedDylib(dylibPath, dylibExists);
199   if (dylibExists)
200     return saver().save(dylibPath);
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, bool explicitlyLinked) {
210   CachedHashStringRef path(mbref.getBufferIdentifier());
211   DylibFile *&file = loadedDylibs[path];
212   if (file) {
213     if (explicitlyLinked)
214       file->setExplicitlyLinked();
215     return file;
216   }
217 
218   DylibFile *newFile;
219   file_magic magic = identify_magic(mbref.getBuffer());
220   if (magic == file_magic::tapi_file) {
221     Expected<std::unique_ptr<InterfaceFile>> result = TextAPIReader::get(mbref);
222     if (!result) {
223       error("could not load TAPI file at " + mbref.getBufferIdentifier() +
224             ": " + toString(result.takeError()));
225       return nullptr;
226     }
227     file =
228         make<DylibFile>(**result, umbrella, isBundleLoader, explicitlyLinked);
229 
230     // parseReexports() can recursively call loadDylib(). That's fine since
231     // we wrote the DylibFile we just loaded to the loadDylib cache via the
232     // `file` reference. But the recursive load can grow loadDylibs, so the
233     // `file` reference might become invalid after parseReexports() -- so copy
234     // the pointer it refers to before continuing.
235     newFile = file;
236     if (newFile->exportingFile)
237       newFile->parseReexports(**result);
238   } else {
239     assert(magic == file_magic::macho_dynamically_linked_shared_lib ||
240            magic == file_magic::macho_dynamically_linked_shared_lib_stub ||
241            magic == file_magic::macho_executable ||
242            magic == file_magic::macho_bundle);
243     file = make<DylibFile>(mbref, umbrella, isBundleLoader, explicitlyLinked);
244 
245     // parseLoadCommands() can also recursively call loadDylib(). See comment
246     // in previous block for why this means we must copy `file` here.
247     newFile = file;
248     if (newFile->exportingFile)
249       newFile->parseLoadCommands(mbref);
250   }
251   return newFile;
252 }
253 
254 void macho::resetLoadedDylibs() { loadedDylibs.clear(); }
255 
256 Optional<StringRef>
257 macho::findPathCombination(const Twine &name,
258                            const std::vector<StringRef> &roots,
259                            ArrayRef<StringRef> extensions) {
260   SmallString<261> base;
261   for (StringRef dir : roots) {
262     base = dir;
263     path::append(base, name);
264     for (StringRef ext : extensions) {
265       Twine location = base + ext;
266       bool exists = fs::exists(location);
267       searchedDylib(location, exists);
268       if (exists)
269         return saver().save(location.str());
270     }
271   }
272   return {};
273 }
274 
275 StringRef macho::rerootPath(StringRef path) {
276   if (!path::is_absolute(path, path::Style::posix) || path.endswith(".o"))
277     return path;
278 
279   if (Optional<StringRef> rerootedPath =
280           findPathCombination(path, config->systemLibraryRoots))
281     return *rerootedPath;
282 
283   return path;
284 }
285 
286 uint32_t macho::getModTime(StringRef path) {
287   if (config->zeroModTime)
288     return 0;
289 
290   fs::file_status stat;
291   if (!fs::status(path, stat))
292     if (fs::exists(stat))
293       return toTimeT(stat.getLastModificationTime());
294 
295   warn("failed to get modification time of " + path);
296   return 0;
297 }
298 
299 void macho::printArchiveMemberLoad(StringRef reason, const InputFile *f) {
300   if (config->printEachFile)
301     message(toString(f));
302   if (config->printWhyLoad)
303     message(reason + " forced load of " + toString(f));
304 }
305 
306 macho::DependencyTracker::DependencyTracker(StringRef path)
307     : path(path), active(!path.empty()) {
308   if (active && fs::exists(path) && !fs::can_write(path)) {
309     warn("Ignoring dependency_info option since specified path is not "
310          "writeable.");
311     active = false;
312   }
313 }
314 
315 void macho::DependencyTracker::write(StringRef version,
316                                      const SetVector<InputFile *> &inputs,
317                                      StringRef output) {
318   if (!active)
319     return;
320 
321   std::error_code ec;
322   raw_fd_ostream os(path, ec, fs::OF_None);
323   if (ec) {
324     warn("Error writing dependency info to file");
325     return;
326   }
327 
328   auto addDep = [&os](DepOpCode opcode, const StringRef &path) {
329     // XXX: Even though DepOpCode's underlying type is uint8_t,
330     // this cast is still needed because Clang older than 10.x has a bug,
331     // where it doesn't know to cast the enum to its underlying type.
332     // Hence `<< DepOpCode` is ambiguous to it.
333     os << static_cast<uint8_t>(opcode);
334     os << path;
335     os << '\0';
336   };
337 
338   addDep(DepOpCode::Version, version);
339 
340   // Sort the input by its names.
341   std::vector<StringRef> inputNames;
342   inputNames.reserve(inputs.size());
343   for (InputFile *f : inputs)
344     inputNames.push_back(f->getName());
345   llvm::sort(inputNames);
346 
347   for (const StringRef &in : inputNames)
348     addDep(DepOpCode::Input, in);
349 
350   for (const std::string &f : notFounds)
351     addDep(DepOpCode::NotFound, f);
352 
353   addDep(DepOpCode::Output, output);
354 }
355