[MemProf] Templatize CallStackRadixTreeBuilder (NFC) (#117014)
[llvm-project.git] / lld / MachO / DriverUtils.cpp
blob858a4bb34029cfc0954a39c0b7bd76dcb1b0221d
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 //===----------------------------------------------------------------------===//
9 #include "Config.h"
10 #include "Driver.h"
11 #include "InputFiles.h"
12 #include "ObjC.h"
13 #include "Target.h"
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"
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;
37 // Create prefix string literals used in Options.td
38 #define PREFIX(NAME, VALUE) \
39 static constexpr StringLiteral NAME##_init[] = VALUE; \
40 static constexpr ArrayRef<StringLiteral> NAME(NAME##_init, \
41 std::size(NAME##_init) - 1);
42 #include "Options.inc"
43 #undef PREFIX
45 // Create table mapping all options defined in Options.td
46 static constexpr OptTable::Info optInfo[] = {
47 #define OPTION(PREFIX, NAME, ID, KIND, GROUP, ALIAS, ALIASARGS, FLAGS, \
48 VISIBILITY, PARAM, HELPTEXT, HELPTEXTSFORVARIANTS, METAVAR, \
49 VALUES) \
50 {PREFIX, \
51 NAME, \
52 HELPTEXT, \
53 HELPTEXTSFORVARIANTS, \
54 METAVAR, \
55 OPT_##ID, \
56 opt::Option::KIND##Class, \
57 PARAM, \
58 FLAGS, \
59 VISIBILITY, \
60 OPT_##GROUP, \
61 OPT_##ALIAS, \
62 ALIASARGS, \
63 VALUES},
64 #include "Options.inc"
65 #undef OPTION
68 MachOOptTable::MachOOptTable() : GenericOptTable(optInfo) {}
70 // Set color diagnostics according to --color-diagnostics={auto,always,never}
71 // or --no-color-diagnostics flags.
72 static void handleColorDiagnostics(CommonLinkerContext &ctx,
73 InputArgList &args) {
74 const Arg *arg =
75 args.getLastArg(OPT_color_diagnostics, OPT_color_diagnostics_eq,
76 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(true);
82 } else if (arg->getOption().getID() == OPT_no_color_diagnostics) {
83 errs.enable_colors(false);
84 } else {
85 StringRef s = arg->getValue();
86 if (s == "always")
87 errs.enable_colors(true);
88 else if (s == "never")
89 errs.enable_colors(false);
90 else if (s != "auto")
91 error("unknown option: --color-diagnostics=" + s);
95 InputArgList 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());
102 // Expand response files (arguments in the form of @<filename>)
103 // and then parse the argument again.
104 cl::ExpandResponseFiles(saver(), cl::TokenizeGNUCommandLine, vec);
105 InputArgList args = ParseArgs(vec, missingIndex, missingCount);
107 // Handle -fatal_warnings early since it converts missing argument warnings
108 // to errors.
109 errorHandler().fatalWarnings = args.hasArg(OPT_fatal_warnings);
110 errorHandler().suppressWarnings = args.hasArg(OPT_w);
112 if (missingCount)
113 error(Twine(args.getArgString(missingIndex)) + ": missing argument");
115 handleColorDiagnostics(ctx, args);
117 for (const Arg *arg : args.filtered(OPT_UNKNOWN)) {
118 std::string nearest;
119 if (findNearest(arg->getAsString(args), nearest) > 1)
120 error("unknown argument '" + arg->getAsString(args) + "'");
121 else
122 error("unknown argument '" + arg->getAsString(args) +
123 "', did you mean '" + nearest + "'");
125 return args;
128 void MachOOptTable::printHelp(CommonLinkerContext &ctx, const char *argv0,
129 bool showHidden) const {
130 auto &outs = ctx.e.outs();
131 OptTable::printHelp(outs, (std::string(argv0) + " [options] file...").c_str(),
132 "LLVM Linker", showHidden);
133 outs << '\n';
136 static std::string rewritePath(StringRef s) {
137 if (fs::exists(s))
138 return relativeToRoot(s);
139 return std::string(s);
142 static 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(s) == s && fs::exists(s))
146 return relativeToRoot(s);
147 return std::string(s);
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.
152 std::string macho::createResponseFile(const InputArgList &args) {
153 SmallString<0> data;
154 raw_svector_ostream os(data);
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(rewriteInputPath(arg->getValue())) << "\n";
163 break;
164 case OPT_o:
165 os << "-o " << quote(path::filename(arg->getValue())) << "\n";
166 break;
167 case OPT_filelist:
168 if (std::optional<MemoryBufferRef> buffer = readFile(arg->getValue()))
169 for (StringRef path : args::getLines(*buffer))
170 os << quote(rewriteInputPath(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(rewriteInputPath(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(rewritePath(arg->getValue()))
186 << "\n";
187 break;
188 case OPT_sectcreate:
189 os << arg->getSpelling() << " " << quote(arg->getValue(0)) << " "
190 << quote(arg->getValue(1)) << " "
191 << quote(rewritePath(arg->getValue(2))) << "\n";
192 break;
193 default:
194 os << toString(*arg) << "\n";
197 return std::string(data);
200 static void searchedDylib(const Twine &path, bool found) {
201 if (config->printDylibSearch)
202 message("searched " + path + (found ? ", found " : ", not found"));
203 if (!found)
204 depTracker->logFileNotFound(path);
207 std::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(tbdPath, ".tbd");
212 bool tbdExists = fs::exists(tbdPath);
213 searchedDylib(tbdPath, tbdExists);
214 if (tbdExists)
215 return saver().save(tbdPath.str());
217 bool dylibExists = fs::exists(dylibPath);
218 searchedDylib(dylibPath, dylibExists);
219 if (dylibExists)
220 return saver().save(dylibPath);
221 return {};
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.
226 static DenseMap<CachedHashStringRef, DylibFile *> loadedDylibs;
228 DylibFile *macho::loadDylib(MemoryBufferRef mbref, DylibFile *umbrella,
229 bool isBundleLoader, bool explicitlyLinked) {
230 CachedHashStringRef path(mbref.getBufferIdentifier());
231 DylibFile *&file = loadedDylibs[path];
232 if (file) {
233 if (explicitlyLinked)
234 file->setExplicitlyLinked();
235 return file;
238 DylibFile *newFile;
239 file_magic magic = identify_magic(mbref.getBuffer());
240 if (magic == file_magic::tapi_file) {
241 Expected<std::unique_ptr<InterfaceFile>> result = TextAPIReader::get(mbref);
242 if (!result) {
243 error("could not load TAPI file at " + mbref.getBufferIdentifier() +
244 ": " + toString(result.takeError()));
245 return nullptr;
247 file =
248 make<DylibFile>(**result, umbrella, isBundleLoader, explicitlyLinked);
250 // parseReexports() can recursively call loadDylib(). That's fine since
251 // we wrote the DylibFile we just loaded to the loadDylib cache via the
252 // `file` reference. But the recursive load can grow loadDylibs, so the
253 // `file` reference might become invalid after parseReexports() -- so copy
254 // the pointer it refers to before continuing.
255 newFile = file;
256 if (newFile->exportingFile)
257 newFile->parseReexports(**result);
258 } else {
259 assert(magic == file_magic::macho_dynamically_linked_shared_lib ||
260 magic == file_magic::macho_dynamically_linked_shared_lib_stub ||
261 magic == file_magic::macho_executable ||
262 magic == file_magic::macho_bundle);
263 file = make<DylibFile>(mbref, umbrella, isBundleLoader, explicitlyLinked);
265 // parseLoadCommands() can also recursively call loadDylib(). See comment
266 // in previous block for why this means we must copy `file` here.
267 newFile = file;
268 if (newFile->exportingFile)
269 newFile->parseLoadCommands(mbref);
271 return newFile;
274 void macho::resetLoadedDylibs() { loadedDylibs.clear(); }
276 std::optional<StringRef>
277 macho::findPathCombination(const Twine &name,
278 const std::vector<StringRef> &roots,
279 ArrayRef<StringRef> extensions) {
280 SmallString<261> base;
281 for (StringRef dir : roots) {
282 base = dir;
283 path::append(base, name);
284 for (StringRef ext : extensions) {
285 Twine location = base + ext;
286 bool exists = fs::exists(location);
287 searchedDylib(location, exists);
288 if (exists)
289 return saver().save(location.str());
292 return {};
295 StringRef macho::rerootPath(StringRef path) {
296 if (!path::is_absolute(path, path::Style::posix) || path.ends_with(".o"))
297 return path;
299 if (std::optional<StringRef> rerootedPath =
300 findPathCombination(path, config->systemLibraryRoots))
301 return *rerootedPath;
303 return path;
306 uint32_t macho::getModTime(StringRef path) {
307 if (config->zeroModTime)
308 return 0;
310 fs::file_status stat;
311 if (!fs::status(path, stat))
312 if (fs::exists(stat))
313 return toTimeT(stat.getLastModificationTime());
315 warn("failed to get modification time of " + path);
316 return 0;
319 void macho::printArchiveMemberLoad(StringRef reason, const InputFile *f) {
320 if (config->printEachFile)
321 message(toString(f));
322 if (config->printWhyLoad)
323 message(reason + " forced load of " + toString(f));
326 macho::DependencyTracker::DependencyTracker(StringRef path)
327 : path(path), active(!path.empty()) {
328 if (active && fs::exists(path) && !fs::can_write(path)) {
329 warn("Ignoring dependency_info option since specified path is not "
330 "writeable.");
331 active = false;
335 void macho::DependencyTracker::write(StringRef version,
336 const SetVector<InputFile *> &inputs,
337 StringRef output) {
338 if (!active)
339 return;
341 std::error_code ec;
342 raw_fd_ostream os(path, ec, fs::OF_None);
343 if (ec) {
344 warn("Error writing dependency info to file");
345 return;
348 auto addDep = [&os](DepOpCode opcode, const StringRef &path) {
349 // XXX: Even though DepOpCode's underlying type is uint8_t,
350 // this cast is still needed because Clang older than 10.x has a bug,
351 // where it doesn't know to cast the enum to its underlying type.
352 // Hence `<< DepOpCode` is ambiguous to it.
353 os << static_cast<uint8_t>(opcode);
354 os << path;
355 os << '\0';
358 addDep(DepOpCode::Version, version);
360 // Sort the input by its names.
361 std::vector<StringRef> inputNames;
362 inputNames.reserve(inputs.size());
363 for (InputFile *f : inputs)
364 inputNames.push_back(f->getName());
365 llvm::sort(inputNames);
367 for (const StringRef &in : inputNames)
368 addDep(DepOpCode::Input, in);
370 for (const std::string &f : notFounds)
371 addDep(DepOpCode::NotFound, f);
373 addDep(DepOpCode::Output, output);