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_CODE
36#include "Options.inc"
37
38MachOOptTable::MachOOptTable() : OptTable(optionTables()) {}
39
40// Set color diagnostics according to --color-diagnostics={auto,always,never}
41// or --no-color-diagnostics flags.
42static void handleColorDiagnostics(CommonLinkerContext &ctx,
43 InputArgList &args) {
44 const Arg *arg =
45 args.getLastArg(Ids: OPT_color_diagnostics, Ids: OPT_color_diagnostics_eq,
46 Ids: OPT_no_color_diagnostics);
47 if (!arg)
48 return;
49 auto &errs = ctx.e.errs();
50 if (arg->getOption().getID() == OPT_color_diagnostics) {
51 errs.enable_colors(enable: true);
52 } else if (arg->getOption().getID() == OPT_no_color_diagnostics) {
53 errs.enable_colors(enable: false);
54 } else {
55 StringRef s = arg->getValue();
56 if (s == "always")
57 errs.enable_colors(enable: true);
58 else if (s == "never")
59 errs.enable_colors(enable: false);
60 else if (s != "auto")
61 error(msg: "unknown option: --color-diagnostics=" + s);
62 }
63}
64
65InputArgList MachOOptTable::parse(CommonLinkerContext &ctx,
66 ArrayRef<const char *> argv) {
67 // Make InputArgList from string vectors.
68 unsigned missingIndex;
69 unsigned missingCount;
70 SmallVector<const char *, 256> vec(argv.data(), argv.data() + argv.size());
71
72 // Expand response files (arguments in the form of @<filename>)
73 // and then parse the argument again.
74 cl::ExpandResponseFiles(Saver&: saver(), Tokenizer: cl::TokenizeGNUCommandLine, Argv&: vec);
75 InputArgList args = ParseArgs(Args: vec, MissingArgIndex&: missingIndex, MissingArgCount&: missingCount);
76
77 // Handle -fatal_warnings early since it converts missing argument warnings
78 // to errors.
79 errorHandler().fatalWarnings = args.hasArg(Ids: OPT_fatal_warnings);
80 errorHandler().suppressWarnings = args.hasArg(Ids: OPT_w);
81
82 if (missingCount)
83 error(msg: Twine(args.getArgString(Index: missingIndex)) + ": missing argument");
84
85 handleColorDiagnostics(ctx, args);
86
87 for (const Arg *arg : args.filtered(Ids: OPT_UNKNOWN)) {
88 std::string nearest;
89 if (findNearest(Option: arg->getAsString(Args: args), NearestString&: nearest) > 1)
90 error(msg: "unknown argument '" + arg->getAsString(Args: args) + "'");
91 else
92 error(msg: "unknown argument '" + arg->getAsString(Args: args) +
93 "', did you mean '" + nearest + "'");
94 }
95 return args;
96}
97
98void MachOOptTable::printHelp(CommonLinkerContext &ctx, const char *argv0,
99 bool showHidden) const {
100 auto &outs = ctx.e.outs();
101 OptTable::printHelp(OS&: outs, Usage: (std::string(argv0) + " [options] file...").c_str(),
102 Title: "LLVM Linker", ShowHidden: showHidden);
103 outs << '\n';
104}
105
106// If any SDK contains the directory, use those directories in SDK order
107// instead of falling back to the host.
108SmallVector<StringRef>
109macho::getRerootedSearchPaths(StringRef searchPath, ArrayRef<StringRef> roots) {
110 SmallVector<StringRef> paths;
111 // NOTE: only absolute paths are re-rooted to syslibroot(s)
112 if (path::is_absolute(path: searchPath, style: path::Style::posix)) {
113 for (StringRef root : roots) {
114 SmallString<261> buffer(root);
115 path::append(path&: buffer, a: searchPath);
116 // Do not warn about paths that are computed via the syslib roots
117 if (fs::is_directory(Path: buffer))
118 paths.push_back(Elt: saver().save(S: buffer.str()));
119 }
120 }
121 if (paths.empty())
122 paths.push_back(Elt: searchPath);
123 return paths;
124}
125
126static std::string rewritePath(StringRef s) {
127 if (fs::exists(Path: s))
128 return relativeToRoot(path: s);
129 return std::string(s);
130}
131
132static std::string rewriteInputPath(StringRef s) {
133 // Don't bother rewriting "absolute" paths that are actually under the
134 // syslibroot; simply rewriting the syslibroot is sufficient.
135 if (rerootPath(path: s) == s && fs::exists(Path: s))
136 return relativeToRoot(path: s);
137 return std::string(s);
138}
139
140// Reconstructs command line arguments so that so that you can re-run
141// the same command with the same inputs. This is for --reproduce.
142std::string macho::createResponseFile(const InputArgList &args) {
143 SmallString<0> data;
144 raw_svector_ostream os(data);
145
146 // Copy the command line to the output while rewriting paths.
147 for (const Arg *arg : args) {
148 switch (arg->getOption().getID()) {
149 case OPT_reproduce:
150 break;
151 case OPT_INPUT:
152 os << quote(s: rewriteInputPath(s: arg->getValue())) << "\n";
153 break;
154 case OPT_o:
155 os << "-o " << quote(s: path::filename(path: arg->getValue())) << "\n";
156 break;
157 case OPT_filelist:
158 if (std::optional<MemoryBufferRef> buffer = readFile(path: arg->getValue()))
159 for (StringRef path : args::getLines(mb: *buffer))
160 os << quote(s: rewriteInputPath(s: path)) << "\n";
161 break;
162 case OPT_force_load:
163 case OPT_weak_library:
164 case OPT_load_hidden:
165 os << arg->getSpelling() << " "
166 << quote(s: rewriteInputPath(s: arg->getValue())) << "\n";
167 break;
168 case OPT_F:
169 case OPT_L:
170 // Resolve SDK prefixes before making search paths relative: relative
171 // search paths are not rerooted when the reproducer is replayed.
172 for (StringRef path :
173 getRerootedSearchPaths(searchPath: arg->getValue(), roots: config->systemLibraryRoots))
174 os << arg->getSpelling() << " " << quote(s: rewritePath(s: path)) << "\n";
175 break;
176 case OPT_non_global_symbols_strip_list:
177 case OPT_non_global_symbols_no_strip_list:
178 case OPT_bundle_loader:
179 case OPT_exported_symbols_list:
180 case OPT_order_file:
181 case OPT_syslibroot:
182 case OPT_unexported_symbols_list:
183 os << arg->getSpelling() << " " << quote(s: rewritePath(s: arg->getValue()))
184 << "\n";
185 break;
186 case OPT_sectcreate:
187 os << arg->getSpelling() << " " << quote(s: arg->getValue(N: 0)) << " "
188 << quote(s: arg->getValue(N: 1)) << " "
189 << quote(s: rewritePath(s: arg->getValue(N: 2))) << "\n";
190 break;
191 default:
192 os << toString(arg: *arg) << "\n";
193 }
194 }
195 return std::string(data);
196}
197
198static void searchedDylib(const Twine &path, bool found) {
199 if (config->printDylibSearch)
200 message(msg: "searched " + path + (found ? ", found " : ", not found"));
201 if (!found)
202 depTracker->logFileNotFound(path);
203}
204
205std::optional<StringRef> macho::resolveDylibPath(StringRef dylibPath) {
206 // TODO: if a tbd and dylib are both present, we should check to make sure
207 // they are consistent.
208 SmallString<261> tbdPath = dylibPath;
209 path::replace_extension(path&: tbdPath, extension: ".tbd");
210 bool tbdExists = fs::exists(Path: tbdPath);
211 searchedDylib(path: tbdPath, found: tbdExists);
212 if (tbdExists)
213 return saver().save(S: tbdPath.str());
214
215 bool dylibExists = fs::exists(Path: dylibPath);
216 searchedDylib(path: dylibPath, found: dylibExists);
217 if (dylibExists)
218 return saver().save(S: dylibPath);
219 return {};
220}
221
222// It's not uncommon to have multiple attempts to load a single dylib,
223// especially if it's a commonly re-exported core library.
224static DenseMap<CachedHashStringRef, DylibFile *> loadedDylibs;
225
226static StringRef realPathIfDifferent(StringRef path) {
227 SmallString<128> realPathBuf;
228 if (fs::real_path(path, output&: realPathBuf))
229 return StringRef();
230
231 SmallString<128> absPathBuf = path;
232 if (!fs::make_absolute(path&: absPathBuf) && realPathBuf == absPathBuf)
233 return StringRef();
234
235 return uniqueSaver().save(S: StringRef(realPathBuf));
236}
237
238DylibFile *macho::loadDylib(MemoryBufferRef mbref, DylibFile *umbrella,
239 bool isBundleLoader, bool explicitlyLinked) {
240 CachedHashStringRef path(mbref.getBufferIdentifier());
241 DylibFile *&file = loadedDylibs[path];
242 if (file) {
243 if (explicitlyLinked)
244 file->setExplicitlyLinked();
245 return file;
246 }
247
248 // Frameworks can be found from different symlink paths, so resolve
249 // symlinks and look up in the dylib cache.
250 CachedHashStringRef realPath(
251 realPathIfDifferent(path: mbref.getBufferIdentifier()));
252 if (!realPath.val().empty()) {
253 // Avoid map insertions here so that we do not invalidate the "file"
254 // reference.
255 auto it = loadedDylibs.find(Val: realPath);
256 if (it != loadedDylibs.end()) {
257 DylibFile *realfile = it->second;
258 if (explicitlyLinked)
259 realfile->setExplicitlyLinked();
260 return realfile;
261 }
262 }
263
264 DylibFile *newFile;
265 file_magic magic = identify_magic(magic: mbref.getBuffer());
266 if (magic == file_magic::tapi_file) {
267 Expected<std::unique_ptr<InterfaceFile>> result = TextAPIReader::get(InputBuffer: mbref);
268 if (!result) {
269 error(msg: "could not load TAPI file at " + mbref.getBufferIdentifier() +
270 ": " + toString(E: result.takeError()));
271 return nullptr;
272 }
273 file =
274 make<DylibFile>(args&: **result, args&: umbrella, args&: isBundleLoader, args&: explicitlyLinked);
275
276 // parseReexports() can recursively call loadDylib(). That's fine since
277 // we wrote the DylibFile we just loaded to the loadDylib cache via the
278 // `file` reference. But the recursive load can grow loadDylibs, so the
279 // `file` reference might become invalid after parseReexports() -- so copy
280 // the pointer it refers to before continuing.
281 newFile = file;
282 if (newFile->exportingFile)
283 newFile->parseReexports(interface: **result);
284 } else {
285 assert(magic == file_magic::macho_dynamically_linked_shared_lib ||
286 magic == file_magic::macho_dynamically_linked_shared_lib_stub ||
287 magic == file_magic::macho_executable ||
288 magic == file_magic::macho_bundle);
289 file = make<DylibFile>(args&: mbref, args&: umbrella, args&: isBundleLoader, args&: explicitlyLinked);
290
291 // parseLoadCommands() can also recursively call loadDylib(). See comment
292 // in previous block for why this means we must copy `file` here.
293 newFile = file;
294 if (newFile->exportingFile)
295 newFile->parseLoadCommands(mb: mbref);
296 }
297
298 if (explicitlyLinked && !newFile->allowableClients.empty()) {
299 bool allowed =
300 llvm::any_of(Range&: newFile->allowableClients, P: [&](StringRef allowableClient) {
301 // We only do a prefix match to match LD64's behaviour.
302 return allowableClient.starts_with(Prefix: config->clientName);
303 });
304
305 // TODO: This behaviour doesn't quite match the latest available source
306 // release of LD64 (ld64-951.9), which allows "parents" and "siblings"
307 // to link to libraries even when they're not explicitly named as
308 // allowable clients. However, behaviour around this seems to have
309 // changed in the latest release of Xcode (ld64-1115.7.3), so it's not
310 // clear what the correct thing to do is yet.
311 if (!allowed)
312 error(msg: "cannot link directly with '" +
313 sys::path::filename(path: newFile->installName) + "' because " +
314 config->clientName + " is not an allowed client");
315 }
316
317 // If the load path was a symlink, cache the real path too.
318 if (!realPath.val().empty())
319 loadedDylibs[realPath] = newFile;
320
321 return newFile;
322}
323
324void macho::resetLoadedDylibs() { loadedDylibs.clear(); }
325
326std::optional<StringRef>
327macho::findPathCombination(const Twine &name,
328 const std::vector<StringRef> &roots,
329 ArrayRef<StringRef> extensions) {
330 SmallString<261> base;
331 for (StringRef dir : roots) {
332 base = dir;
333 path::append(path&: base, a: name);
334 for (StringRef ext : extensions) {
335 Twine location = base + ext;
336 bool exists = fs::exists(Path: location);
337 searchedDylib(path: location, found: exists);
338 if (exists)
339 return saver().save(S: location.str());
340 }
341 }
342 return {};
343}
344
345StringRef macho::rerootPath(StringRef path) {
346 if (!path::is_absolute(path, style: path::Style::posix) || path.ends_with(Suffix: ".o"))
347 return path;
348
349 if (std::optional<StringRef> rerootedPath =
350 findPathCombination(name: path, roots: config->systemLibraryRoots))
351 return *rerootedPath;
352
353 return path;
354}
355
356uint32_t macho::getModTime(StringRef path) {
357 if (config->zeroModTime)
358 return 0;
359
360 fs::file_status stat;
361 if (!fs::status(path, result&: stat))
362 if (fs::exists(status: stat))
363 return toTimeT(TP: stat.getLastModificationTime());
364
365 warn(msg: "failed to get modification time of " + path);
366 return 0;
367}
368
369void macho::printArchiveMemberLoad(StringRef reason, const InputFile *f) {
370 if (config->printEachFile)
371 message(msg: toString(file: f));
372 if (config->printWhyLoad)
373 message(msg: reason + " forced load of " + toString(file: f));
374}
375
376macho::DependencyTracker::DependencyTracker(StringRef path)
377 : path(path), active(!path.empty()) {
378 if (active && fs::exists(Path: path) && !fs::can_write(Path: path)) {
379 warn(msg: "Ignoring dependency_info option since specified path is not "
380 "writeable.");
381 active = false;
382 }
383}
384
385void macho::DependencyTracker::write(StringRef version,
386 const SetVector<InputFile *> &inputs,
387 StringRef output) {
388 if (!active)
389 return;
390
391 std::error_code ec;
392 raw_fd_ostream os(path, ec, fs::OF_None);
393 if (ec) {
394 warn(msg: "Error writing dependency info to file");
395 return;
396 }
397
398 auto addDep = [&os](DepOpCode opcode, const StringRef &path) {
399 // XXX: Even though DepOpCode's underlying type is uint8_t,
400 // this cast is still needed because Clang older than 10.x has a bug,
401 // where it doesn't know to cast the enum to its underlying type.
402 // Hence `<< DepOpCode` is ambiguous to it.
403 os << static_cast<uint8_t>(opcode);
404 os << path;
405 os << '\0';
406 };
407
408 addDep(DepOpCode::Version, version);
409
410 // Sort the input by its names.
411 std::vector<StringRef> inputNames;
412 inputNames.reserve(n: inputs.size());
413 for (InputFile *f : inputs)
414 inputNames.push_back(x: f->getName());
415 llvm::sort(C&: inputNames);
416
417 for (const StringRef &in : inputNames)
418 addDep(DepOpCode::Input, in);
419
420 for (const std::string &f : notFounds)
421 addDep(DepOpCode::NotFound, f);
422
423 addDep(DepOpCode::Output, output);
424}
425