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 
14 #include "lld/Common/Args.h"
15 #include "lld/Common/ErrorHandler.h"
16 #include "lld/Common/Memory.h"
17 #include "lld/Common/Reproduce.h"
18 #include "llvm/ADT/CachedHashString.h"
19 #include "llvm/ADT/DenseMap.h"
20 #include "llvm/Bitcode/BitcodeReader.h"
21 #include "llvm/LTO/LTO.h"
22 #include "llvm/Option/Arg.h"
23 #include "llvm/Option/ArgList.h"
24 #include "llvm/Option/Option.h"
25 #include "llvm/Support/CommandLine.h"
26 #include "llvm/Support/FileSystem.h"
27 #include "llvm/Support/Path.h"
28 #include "llvm/TextAPI/MachO/InterfaceFile.h"
29 #include "llvm/TextAPI/MachO/TextAPIReader.h"
30 
31 using namespace llvm;
32 using namespace llvm::MachO;
33 using namespace llvm::opt;
34 using namespace llvm::sys;
35 using namespace lld;
36 using namespace lld::macho;
37 
38 // Create prefix string literals used in Options.td
39 #define PREFIX(NAME, VALUE) const char *NAME[] = VALUE;
40 #include "Options.inc"
41 #undef PREFIX
42 
43 // Create table mapping all options defined in Options.td
44 static const OptTable::Info optInfo[] = {
45 #define OPTION(X1, X2, ID, KIND, GROUP, ALIAS, X7, X8, X9, X10, X11, X12)      \
46   {X1, X2, X10,         X11,         OPT_##ID, Option::KIND##Class,            \
47    X9, X8, OPT_##GROUP, OPT_##ALIAS, X7,       X12},
48 #include "Options.inc"
49 #undef OPTION
50 };
51 
52 MachOOptTable::MachOOptTable() : OptTable(optInfo) {}
53 
54 // Set color diagnostics according to --color-diagnostics={auto,always,never}
55 // or --no-color-diagnostics flags.
56 static void handleColorDiagnostics(InputArgList &args) {
57   const Arg *arg =
58       args.getLastArg(OPT_color_diagnostics, OPT_color_diagnostics_eq,
59                       OPT_no_color_diagnostics);
60   if (!arg)
61     return;
62   if (arg->getOption().getID() == OPT_color_diagnostics) {
63     lld::errs().enable_colors(true);
64   } else if (arg->getOption().getID() == OPT_no_color_diagnostics) {
65     lld::errs().enable_colors(false);
66   } else {
67     StringRef s = arg->getValue();
68     if (s == "always")
69       lld::errs().enable_colors(true);
70     else if (s == "never")
71       lld::errs().enable_colors(false);
72     else if (s != "auto")
73       error("unknown option: --color-diagnostics=" + s);
74   }
75 }
76 
77 InputArgList MachOOptTable::parse(ArrayRef<const char *> argv) {
78   // Make InputArgList from string vectors.
79   unsigned missingIndex;
80   unsigned missingCount;
81   SmallVector<const char *, 256> vec(argv.data(), argv.data() + argv.size());
82 
83   // Expand response files (arguments in the form of @<filename>)
84   // and then parse the argument again.
85   cl::ExpandResponseFiles(saver, cl::TokenizeGNUCommandLine, vec);
86   InputArgList args = ParseArgs(vec, missingIndex, missingCount);
87 
88   // Handle -fatal_warnings early since it converts missing argument warnings
89   // to errors.
90   errorHandler().fatalWarnings = args.hasArg(OPT_fatal_warnings);
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   PrintHelp(lld::outs(), (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 // Reconstructs command line arguments so that so that you can re-run
121 // the same command with the same inputs. This is for --reproduce.
122 std::string macho::createResponseFile(const InputArgList &args) {
123   SmallString<0> data;
124   raw_svector_ostream os(data);
125 
126   // Copy the command line to the output while rewriting paths.
127   for (const Arg *arg : args) {
128     switch (arg->getOption().getID()) {
129     case OPT_reproduce:
130       break;
131     case OPT_INPUT:
132       os << quote(rewritePath(arg->getValue())) << "\n";
133       break;
134     case OPT_o:
135       os << "-o " << quote(path::filename(arg->getValue())) << "\n";
136       break;
137     case OPT_filelist:
138       if (Optional<MemoryBufferRef> buffer = readFile(arg->getValue()))
139         for (StringRef path : args::getLines(*buffer))
140           os << quote(rewritePath(path)) << "\n";
141       break;
142     case OPT_force_load:
143     case OPT_rpath:
144     case OPT_syslibroot:
145     case OPT_F:
146     case OPT_L:
147     case OPT_order_file:
148       os << arg->getSpelling() << " " << quote(rewritePath(arg->getValue()))
149          << "\n";
150       break;
151     case OPT_sectcreate:
152       os << arg->getSpelling() << " " << quote(arg->getValue(0)) << " "
153          << quote(arg->getValue(1)) << " "
154          << quote(rewritePath(arg->getValue(2))) << "\n";
155       break;
156     default:
157       os << toString(*arg) << "\n";
158     }
159   }
160   return std::string(data.str());
161 }
162 
163 Optional<std::string> macho::resolveDylibPath(StringRef path) {
164   // TODO: if a tbd and dylib are both present, we should check to make sure
165   // they are consistent.
166   if (fs::exists(path))
167     return std::string(path);
168   else
169     depTracker->logFileNotFound(path);
170 
171   SmallString<261> location = path;
172   path::replace_extension(location, ".tbd");
173   if (fs::exists(location))
174     return std::string(location);
175   else
176     depTracker->logFileNotFound(location);
177   return {};
178 }
179 
180 // It's not uncommon to have multiple attempts to load a single dylib,
181 // especially if it's a commonly re-exported core library.
182 static DenseMap<CachedHashStringRef, DylibFile *> loadedDylibs;
183 
184 Optional<DylibFile *> macho::loadDylib(MemoryBufferRef mbref,
185                                        DylibFile *umbrella,
186                                        bool isBundleLoader) {
187   StringRef path = mbref.getBufferIdentifier();
188   DylibFile *&file = loadedDylibs[CachedHashStringRef(path)];
189   if (file)
190     return file;
191 
192   file_magic magic = identify_magic(mbref.getBuffer());
193   if (magic == file_magic::tapi_file) {
194     Expected<std::unique_ptr<InterfaceFile>> result = TextAPIReader::get(mbref);
195     if (!result) {
196       error("could not load TAPI file at " + mbref.getBufferIdentifier() +
197             ": " + toString(result.takeError()));
198       return {};
199     }
200     file = make<DylibFile>(**result, umbrella, isBundleLoader);
201   } else {
202     assert(magic == file_magic::macho_dynamically_linked_shared_lib ||
203            magic == file_magic::macho_dynamically_linked_shared_lib_stub ||
204            magic == file_magic::macho_executable ||
205            magic == file_magic::macho_bundle);
206     file = make<DylibFile>(mbref, umbrella, isBundleLoader);
207   }
208   return file;
209 }
210 
211 Optional<InputFile *> macho::loadArchiveMember(MemoryBufferRef mb,
212                                                uint32_t modTime,
213                                                StringRef archiveName,
214                                                bool objCOnly) {
215   switch (identify_magic(mb.getBuffer())) {
216   case file_magic::macho_object:
217     if (!objCOnly || hasObjCSection(mb))
218       return make<ObjFile>(mb, modTime, archiveName);
219     return None;
220   case file_magic::bitcode:
221     if (!objCOnly || check(isBitcodeContainingObjCCategory(mb)))
222       return make<BitcodeFile>(mb);
223     return None;
224   default:
225     error(archiveName + ": archive member " + mb.getBufferIdentifier() +
226           " has unhandled file type");
227     return None;
228   }
229 }
230 
231 uint32_t macho::getModTime(StringRef path) {
232   fs::file_status stat;
233   if (!fs::status(path, stat))
234     if (fs::exists(stat))
235       return toTimeT(stat.getLastModificationTime());
236 
237   warn("failed to get modification time of " + path);
238   return 0;
239 }
240 
241 void macho::printArchiveMemberLoad(StringRef reason, const InputFile *f) {
242   if (config->printEachFile)
243     message(toString(f));
244   if (config->printWhyLoad)
245     message(reason + " forced load of " + toString(f));
246 }
247 
248 macho::DependencyTracker::DependencyTracker(StringRef path)
249     : path(path), active(!path.empty()) {
250   if (active && fs::exists(path) && !fs::can_write(path)) {
251     warn("Ignoring dependency_info option since specified path is not "
252          "writeable.");
253     active = false;
254   }
255 }
256 
257 void macho::DependencyTracker::write(llvm::StringRef version,
258                                      const llvm::SetVector<InputFile *> &inputs,
259                                      llvm::StringRef output) {
260   if (!active)
261     return;
262 
263   std::error_code ec;
264   llvm::raw_fd_ostream os(path, ec, llvm::sys::fs::OF_None);
265   if (ec) {
266     warn("Error writing dependency info to file");
267     return;
268   }
269 
270   auto addDep = [&os](DepOpCode opcode, const StringRef &path) {
271     // XXX: Even though DepOpCode's underlying type is uint8_t,
272     // this cast is still needed because Clang older than 10.x has a bug,
273     // where it doesn't know to cast the enum to its underlying type.
274     // Hence `<< DepOpCode` is ambiguous to it.
275     os << static_cast<uint8_t>(opcode);
276     os << path;
277     os << '\0';
278   };
279 
280   addDep(DepOpCode::Version, version);
281 
282   // Sort the input by its names.
283   std::vector<StringRef> inputNames;
284   inputNames.reserve(inputs.size());
285   for (InputFile *f : inputs)
286     inputNames.push_back(f->getName());
287   llvm::sort(inputNames);
288 
289   for (const StringRef &in : inputNames)
290     addDep(DepOpCode::Input, in);
291 
292   for (const std::string &f : notFounds)
293     addDep(DepOpCode::NotFound, f);
294 
295   addDep(DepOpCode::Output, output);
296 }
297