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