forked from llvm/llvm-project
- Notifications
You must be signed in to change notification settings - Fork 339
/
Copy pathDriverUtils.cpp
401 lines (354 loc) · 14 KB
/
DriverUtils.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
//===- DriverUtils.cpp ----------------------------------------------------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//
#include"Config.h"
#include"Driver.h"
#include"InputFiles.h"
#include"ObjC.h"
#include"Target.h"
#include"lld/Common/Args.h"
#include"lld/Common/CommonLinkerContext.h"
#include"lld/Common/Reproduce.h"
#include"llvm/ADT/CachedHashString.h"
#include"llvm/ADT/DenseMap.h"
#include"llvm/LTO/LTO.h"
#include"llvm/Option/Arg.h"
#include"llvm/Option/ArgList.h"
#include"llvm/Option/Option.h"
#include"llvm/Support/CommandLine.h"
#include"llvm/Support/FileSystem.h"
#include"llvm/Support/Path.h"
#include"llvm/TextAPI/InterfaceFile.h"
#include"llvm/TextAPI/TextAPIReader.h"
usingnamespacellvm;
usingnamespacellvm::MachO;
usingnamespacellvm::opt;
usingnamespacellvm::sys;
usingnamespacelld;
usingnamespacelld::macho;
#defineOPTTABLE_STR_TABLE_CODE
#include"Options.inc"
#undef OPTTABLE_STR_TABLE_CODE
// Create prefix string literals used in Options.td
#defineOPTTABLE_PREFIXES_TABLE_CODE
#include"Options.inc"
#undef OPTTABLE_PREFIXES_TABLE_CODE
// Create table mapping all options defined in Options.td
staticconstexpr OptTable::Info optInfo[] = {
#defineOPTION(PREFIX, NAME, ID, KIND, GROUP, ALIAS, ALIASARGS, FLAGS, \
VISIBILITY, PARAM, HELPTEXT, HELPTEXTSFORVARIANTS, METAVAR, \
VALUES) \
{PREFIX, \
NAME, \
HELPTEXT, \
HELPTEXTSFORVARIANTS, \
METAVAR, \
OPT_##ID, \
opt::Option::KIND##Class, \
PARAM, \
FLAGS, \
VISIBILITY, \
OPT_##GROUP, \
OPT_##ALIAS, \
ALIASARGS, \
VALUES},
#include"Options.inc"
#undef OPTION
};
MachOOptTable::MachOOptTable()
: GenericOptTable(OptionStrTable, OptionPrefixesTable, optInfo) {}
// Set color diagnostics according to --color-diagnostics={auto,always,never}
// or --no-color-diagnostics flags.
staticvoidhandleColorDiagnostics(CommonLinkerContext &ctx,
InputArgList &args) {
const Arg *arg =
args.getLastArg(OPT_color_diagnostics, OPT_color_diagnostics_eq,
OPT_no_color_diagnostics);
if (!arg)
return;
auto &errs = ctx.e.errs();
if (arg->getOption().getID() == OPT_color_diagnostics) {
errs.enable_colors(true);
} elseif (arg->getOption().getID() == OPT_no_color_diagnostics) {
errs.enable_colors(false);
} else {
StringRef s = arg->getValue();
if (s == "always")
errs.enable_colors(true);
elseif (s == "never")
errs.enable_colors(false);
elseif (s != "auto")
error("unknown option: --color-diagnostics=" + s);
}
}
InputArgList MachOOptTable::parse(CommonLinkerContext &ctx,
ArrayRef<constchar *> argv) {
// Make InputArgList from string vectors.
unsigned missingIndex;
unsigned missingCount;
SmallVector<constchar *, 256> vec(argv.data(), argv.data() + argv.size());
// Expand response files (arguments in the form of @<filename>)
// and then parse the argument again.
cl::ExpandResponseFiles(saver(), cl::TokenizeGNUCommandLine, vec);
InputArgList args = ParseArgs(vec, missingIndex, missingCount);
// Handle -fatal_warnings early since it converts missing argument warnings
// to errors.
errorHandler().fatalWarnings = args.hasArg(OPT_fatal_warnings);
errorHandler().suppressWarnings = args.hasArg(OPT_w);
if (missingCount)
error(Twine(args.getArgString(missingIndex)) + ": missing argument");
handleColorDiagnostics(ctx, args);
for (const Arg *arg : args.filtered(OPT_UNKNOWN)) {
std::string nearest;
if (findNearest(arg->getAsString(args), nearest) > 1)
error("unknown argument '" + arg->getAsString(args) + "'");
else
error("unknown argument '" + arg->getAsString(args) +
"', did you mean '" + nearest + "'");
}
return args;
}
voidMachOOptTable::printHelp(CommonLinkerContext &ctx, constchar *argv0,
bool showHidden) const {
auto &outs = ctx.e.outs();
OptTable::printHelp(outs, (std::string(argv0) + " [options] file...").c_str(),
"LLVM Linker", showHidden);
outs << '\n';
}
static std::string rewritePath(StringRef s) {
if (fs::exists(s))
returnrelativeToRoot(s);
returnstd::string(s);
}
static std::string rewriteInputPath(StringRef s) {
// Don't bother rewriting "absolute" paths that are actually under the
// syslibroot; simply rewriting the syslibroot is sufficient.
if (rerootPath(s) == s && fs::exists(s))
returnrelativeToRoot(s);
returnstd::string(s);
}
// Reconstructs command line arguments so that so that you can re-run
// the same command with the same inputs. This is for --reproduce.
std::string macho::createResponseFile(const InputArgList &args) {
SmallString<0> data;
raw_svector_ostream os(data);
// Copy the command line to the output while rewriting paths.
for (const Arg *arg : args) {
switch (arg->getOption().getID()) {
case OPT_reproduce:
break;
case OPT_INPUT:
os << quote(rewriteInputPath(arg->getValue())) << "\n";
break;
case OPT_o:
os << "-o " << quote(path::filename(arg->getValue())) << "\n";
break;
case OPT_filelist:
if (std::optional<MemoryBufferRef> buffer = readFile(arg->getValue()))
for (StringRef path : args::getLines(*buffer))
os << quote(rewriteInputPath(path)) << "\n";
break;
case OPT_force_load:
case OPT_weak_library:
case OPT_load_hidden:
os << arg->getSpelling() << ""
<< quote(rewriteInputPath(arg->getValue())) << "\n";
break;
case OPT_F:
case OPT_L:
case OPT_bundle_loader:
case OPT_exported_symbols_list:
case OPT_order_file:
case OPT_syslibroot:
case OPT_unexported_symbols_list:
os << arg->getSpelling() << "" << quote(rewritePath(arg->getValue()))
<< "\n";
break;
case OPT_sectcreate:
os << arg->getSpelling() << "" << quote(arg->getValue(0)) << ""
<< quote(arg->getValue(1)) << ""
<< quote(rewritePath(arg->getValue(2))) << "\n";
break;
default:
os << toString(*arg) << "\n";
}
}
returnstd::string(data);
}
staticvoidsearchedDylib(const Twine &path, bool found) {
if (config->printDylibSearch)
message("searched " + path + (found ? ", found " : ", not found"));
if (!found)
depTracker->logFileNotFound(path);
}
std::optional<StringRef> macho::resolveDylibPath(StringRef dylibPath) {
// TODO: if a tbd and dylib are both present, we should check to make sure
// they are consistent.
SmallString<261> tbdPath = dylibPath;
path::replace_extension(tbdPath, ".tbd");
bool tbdExists = fs::exists(tbdPath);
searchedDylib(tbdPath, tbdExists);
if (tbdExists)
returnsaver().save(tbdPath.str());
bool dylibExists = fs::exists(dylibPath);
searchedDylib(dylibPath, dylibExists);
if (dylibExists)
returnsaver().save(dylibPath);
return {};
}
// It's not uncommon to have multiple attempts to load a single dylib,
// especially if it's a commonly re-exported core library.
static DenseMap<CachedHashStringRef, DylibFile *> loadedDylibs;
DylibFile *macho::loadDylib(MemoryBufferRef mbref, DylibFile *umbrella,
bool isBundleLoader, bool explicitlyLinked) {
// Frameworks can be found from different symlink paths, so resolve
// symlinks before looking up in the dylib cache.
SmallString<128> realPath;
std::error_code err = fs::real_path(mbref.getBufferIdentifier(), realPath);
CachedHashStringRef path(!err ? uniqueSaver().save(StringRef(realPath))
: mbref.getBufferIdentifier());
DylibFile *&file = loadedDylibs[path];
if (file) {
if (explicitlyLinked)
file->setExplicitlyLinked();
return file;
}
DylibFile *newFile;
file_magic magic = identify_magic(mbref.getBuffer());
if (magic == file_magic::tapi_file) {
Expected<std::unique_ptr<InterfaceFile>> result = TextAPIReader::get(mbref);
if (!result) {
error("could not load TAPI file at " + mbref.getBufferIdentifier() +
": " + toString(result.takeError()));
returnnullptr;
}
file =
make<DylibFile>(**result, umbrella, isBundleLoader, explicitlyLinked);
// parseReexports() can recursively call loadDylib(). That's fine since
// we wrote the DylibFile we just loaded to the loadDylib cache via the
// `file` reference. But the recursive load can grow loadDylibs, so the
// `file` reference might become invalid after parseReexports() -- so copy
// the pointer it refers to before continuing.
newFile = file;
if (newFile->exportingFile)
newFile->parseReexports(**result);
} else {
assert(magic == file_magic::macho_dynamically_linked_shared_lib ||
magic == file_magic::macho_dynamically_linked_shared_lib_stub ||
magic == file_magic::macho_executable ||
magic == file_magic::macho_bundle);
file = make<DylibFile>(mbref, umbrella, isBundleLoader, explicitlyLinked);
// parseLoadCommands() can also recursively call loadDylib(). See comment
// in previous block for why this means we must copy `file` here.
newFile = file;
if (newFile->exportingFile)
newFile->parseLoadCommands(mbref);
}
if (explicitlyLinked && !newFile->allowableClients.empty()) {
bool allowed = std::any_of(
newFile->allowableClients.begin(), newFile->allowableClients.end(),
[&](StringRef allowableClient) {
// We only do a prefix match to match LD64's behaviour.
return allowableClient.starts_with(config->clientName);
});
// TODO: This behaviour doesn't quite match the latest available source
// release of LD64 (ld64-951.9), which allows "parents" and "siblings"
// to link to libraries even when they're not explicitly named as
// allowable clients. However, behaviour around this seems to have
// changed in the latest release of Xcode (ld64-1115.7.3), so it's not
// clear what the correct thing to do is yet.
if (!allowed)
error("cannot link directly with '" +
sys::path::filename(newFile->installName) + "' because " +
config->clientName + " is not an allowed client");
}
return newFile;
}
voidmacho::resetLoadedDylibs() { loadedDylibs.clear(); }
std::optional<StringRef>
macho::findPathCombination(const Twine &name,
const std::vector<StringRef> &roots,
ArrayRef<StringRef> extensions) {
SmallString<261> base;
for (StringRef dir : roots) {
base = dir;
path::append(base, name);
for (StringRef ext : extensions) {
Twine location = base + ext;
bool exists = fs::exists(location);
searchedDylib(location, exists);
if (exists)
returnsaver().save(location.str());
}
}
return {};
}
StringRef macho::rerootPath(StringRef path) {
if (!path::is_absolute(path, path::Style::posix) || path.ends_with(".o"))
return path;
if (std::optional<StringRef> rerootedPath =
findPathCombination(path, config->systemLibraryRoots))
return *rerootedPath;
return path;
}
uint32_tmacho::getModTime(StringRef path) {
if (config->zeroModTime)
return0;
fs::file_status stat;
if (!fs::status(path, stat))
if (fs::exists(stat))
returntoTimeT(stat.getLastModificationTime());
warn("failed to get modification time of " + path);
return0;
}
voidmacho::printArchiveMemberLoad(StringRef reason, const InputFile *f) {
if (config->printEachFile)
message(toString(f));
if (config->printWhyLoad)
message(reason + " forced load of " + toString(f));
}
macho::DependencyTracker::DependencyTracker(StringRef path)
: path(path), active(!path.empty()) {
if (active && fs::exists(path) && !fs::can_write(path)) {
warn("Ignoring dependency_info option since specified path is not "
"writeable.");
active = false;
}
}
voidmacho::DependencyTracker::write(StringRef version,
const SetVector<InputFile *> &inputs,
StringRef output) {
if (!active)
return;
std::error_code ec;
raw_fd_ostream os(path, ec, fs::OF_None);
if (ec) {
warn("Error writing dependency info to file");
return;
}
auto addDep = [&os](DepOpCode opcode, const StringRef &path) {
// XXX: Even though DepOpCode's underlying type is uint8_t,
// this cast is still needed because Clang older than 10.x has a bug,
// where it doesn't know to cast the enum to its underlying type.
// Hence `<< DepOpCode` is ambiguous to it.
os << static_cast<uint8_t>(opcode);
os << path;
os << '\0';
};
addDep(DepOpCode::Version, version);
// Sort the input by its names.
std::vector<StringRef> inputNames;
inputNames.reserve(inputs.size());
for (InputFile *f : inputs)
inputNames.push_back(f->getName());
llvm::sort(inputNames);
for (const StringRef &in : inputNames)
addDep(DepOpCode::Input, in);
for (const std::string &f : notFounds)
addDep(DepOpCode::NotFound, f);
addDep(DepOpCode::Output, output);
}