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
28using namespace llvm;
29using namespace llvm::MachO;
30using namespace llvm::opt;
31using namespace llvm::sys;
32using namespace lld;
33using 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
45static 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
67MachOOptTable::MachOOptTable()
68 : GenericOptTable(OptionStrTable, OptionPrefixesTable, optInfo) {}
69
70// Set color diagnostics according to --color-diagnostics={auto,always,never}
71// or --no-color-diagnostics flags.
72static void handleColorDiagnostics(CommonLinkerContext &ctx,
73 InputArgList &args) {
74 const Arg *arg =
75 args.getLastArg(Ids: OPT_color_diagnostics, Ids: OPT_color_diagnostics_eq,
76 Ids: 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(enable: true);
82 } else if (arg->getOption().getID() == OPT_no_color_diagnostics) {
83 errs.enable_colors(enable: false);
84 } else {
85 StringRef s = arg->getValue();
86 if (s == "always")
87 errs.enable_colors(enable: true);
88 else if (s == "never")
89 errs.enable_colors(enable: false);
90 else if (s != "auto")
91 error(msg: "unknown option: --color-diagnostics=" + s);
92 }
93}
94
95InputArgList 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&: saver(), Tokenizer: cl::TokenizeGNUCommandLine, Argv&: vec);
105 InputArgList args = ParseArgs(Args: vec, MissingArgIndex&: missingIndex, MissingArgCount&: missingCount);
106
107 // Handle -fatal_warnings early since it converts missing argument warnings
108 // to errors.
109 errorHandler().fatalWarnings = args.hasArg(Ids: OPT_fatal_warnings);
110 errorHandler().suppressWarnings = args.hasArg(Ids: OPT_w);
111
112 if (missingCount)
113 error(msg: Twine(args.getArgString(Index: missingIndex)) + ": missing argument");
114
115 handleColorDiagnostics(ctx, args);
116
117 for (const Arg *arg : args.filtered(Ids: OPT_UNKNOWN)) {
118 std::string nearest;
119 if (findNearest(Option: arg->getAsString(Args: args), NearestString&: nearest) > 1)
120 error(msg: "unknown argument '" + arg->getAsString(Args: args) + "'");
121 else
122 error(msg: "unknown argument '" + arg->getAsString(Args: args) +
123 "', did you mean '" + nearest + "'");
124 }
125 return args;
126}
127
128void MachOOptTable::printHelp(CommonLinkerContext &ctx, const char *argv0,
129 bool showHidden) const {
130 auto &outs = ctx.e.outs();
131 OptTable::printHelp(OS&: outs, Usage: (std::string(argv0) + " [options] file...").c_str(),
132 Title: "LLVM Linker", ShowHidden: showHidden);
133 outs << '\n';
134}
135
136static std::string rewritePath(StringRef s) {
137 if (fs::exists(Path: s))
138 return relativeToRoot(path: s);
139 return std::string(s);
140}
141
142static 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(path: s) == s && fs::exists(Path: s))
146 return relativeToRoot(path: 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.
152std::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(s: rewriteInputPath(s: arg->getValue())) << "\n";
163 break;
164 case OPT_o:
165 os << "-o " << quote(s: path::filename(path: arg->getValue())) << "\n";
166 break;
167 case OPT_filelist:
168 if (std::optional<MemoryBufferRef> buffer = readFile(path: arg->getValue()))
169 for (StringRef path : args::getLines(mb: *buffer))
170 os << quote(s: rewriteInputPath(s: 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(s: rewriteInputPath(s: 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(s: rewritePath(s: arg->getValue()))
186 << "\n";
187 break;
188 case OPT_sectcreate:
189 os << arg->getSpelling() << " " << quote(s: arg->getValue(N: 0)) << " "
190 << quote(s: arg->getValue(N: 1)) << " "
191 << quote(s: rewritePath(s: arg->getValue(N: 2))) << "\n";
192 break;
193 default:
194 os << toString(arg: *arg) << "\n";
195 }
196 }
197 return std::string(data);
198}
199
200static void searchedDylib(const Twine &path, bool found) {
201 if (config->printDylibSearch)
202 message(msg: "searched " + path + (found ? ", found " : ", not found"));
203 if (!found)
204 depTracker->logFileNotFound(path);
205}
206
207std::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(path&: tbdPath, extension: ".tbd");
212 bool tbdExists = fs::exists(Path: tbdPath);
213 searchedDylib(path: tbdPath, found: tbdExists);
214 if (tbdExists)
215 return saver().save(S: tbdPath.str());
216
217 bool dylibExists = fs::exists(Path: dylibPath);
218 searchedDylib(path: dylibPath, found: dylibExists);
219 if (dylibExists)
220 return saver().save(S: 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.
226static DenseMap<CachedHashStringRef, DylibFile *> loadedDylibs;
227
228static StringRef realPathIfDifferent(StringRef path) {
229 SmallString<128> realPathBuf;
230 if (fs::real_path(path, output&: realPathBuf))
231 return StringRef();
232
233 SmallString<128> absPathBuf = path;
234 if (!fs::make_absolute(path&: absPathBuf) && realPathBuf == absPathBuf)
235 return StringRef();
236
237 return uniqueSaver().save(S: StringRef(realPathBuf));
238}
239
240DylibFile *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(path: 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(Val: 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(magic: mbref.getBuffer());
268 if (magic == file_magic::tapi_file) {
269 Expected<std::unique_ptr<InterfaceFile>> result = TextAPIReader::get(InputBuffer: mbref);
270 if (!result) {
271 error(msg: "could not load TAPI file at " + mbref.getBufferIdentifier() +
272 ": " + toString(E: result.takeError()));
273 return nullptr;
274 }
275 file =
276 make<DylibFile>(args&: **result, args&: umbrella, args&: isBundleLoader, args&: 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(interface: **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>(args&: mbref, args&: umbrella, args&: isBundleLoader, args&: 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(mb: mbref);
298 }
299
300 if (explicitlyLinked && !newFile->allowableClients.empty()) {
301 bool allowed =
302 llvm::any_of(Range&: newFile->allowableClients, P: [&](StringRef allowableClient) {
303 // We only do a prefix match to match LD64's behaviour.
304 return allowableClient.starts_with(Prefix: 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(msg: "cannot link directly with '" +
315 sys::path::filename(path: 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
326void macho::resetLoadedDylibs() { loadedDylibs.clear(); }
327
328std::optional<StringRef>
329macho::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(path&: base, a: name);
336 for (StringRef ext : extensions) {
337 Twine location = base + ext;
338 bool exists = fs::exists(Path: location);
339 searchedDylib(path: location, found: exists);
340 if (exists)
341 return saver().save(S: location.str());
342 }
343 }
344 return {};
345}
346
347StringRef macho::rerootPath(StringRef path) {
348 if (!path::is_absolute(path, style: path::Style::posix) || path.ends_with(Suffix: ".o"))
349 return path;
350
351 if (std::optional<StringRef> rerootedPath =
352 findPathCombination(name: path, roots: config->systemLibraryRoots))
353 return *rerootedPath;
354
355 return path;
356}
357
358uint32_t macho::getModTime(StringRef path) {
359 if (config->zeroModTime)
360 return 0;
361
362 fs::file_status stat;
363 if (!fs::status(path, result&: stat))
364 if (fs::exists(status: stat))
365 return toTimeT(TP: stat.getLastModificationTime());
366
367 warn(msg: "failed to get modification time of " + path);
368 return 0;
369}
370
371void macho::printArchiveMemberLoad(StringRef reason, const InputFile *f) {
372 if (config->printEachFile)
373 message(msg: toString(file: f));
374 if (config->printWhyLoad)
375 message(msg: reason + " forced load of " + toString(file: f));
376}
377
378macho::DependencyTracker::DependencyTracker(StringRef path)
379 : path(path), active(!path.empty()) {
380 if (active && fs::exists(Path: path) && !fs::can_write(Path: path)) {
381 warn(msg: "Ignoring dependency_info option since specified path is not "
382 "writeable.");
383 active = false;
384 }
385}
386
387void 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(msg: "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(n: inputs.size());
415 for (InputFile *f : inputs)
416 inputNames.push_back(x: f->getName());
417 llvm::sort(C&: 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