1 //===- DriverUtils.cpp ----------------------------------------------------===//
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
7 //===----------------------------------------------------------------------===//
11 #include "InputFiles.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"
31 using namespace llvm::MachO
;
32 using namespace llvm::opt
;
33 using namespace llvm::sys
;
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"
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, \
53 HELPTEXTSFORVARIANTS, \
56 opt::Option::KIND##Class, \
64 #include "Options.inc"
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
,
75 args
.getLastArg(OPT_color_diagnostics
, OPT_color_diagnostics_eq
,
76 OPT_no_color_diagnostics
);
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);
85 StringRef s
= arg
->getValue();
87 errs
.enable_colors(true);
88 else if (s
== "never")
89 errs
.enable_colors(false);
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
109 errorHandler().fatalWarnings
= args
.hasArg(OPT_fatal_warnings
);
110 errorHandler().suppressWarnings
= args
.hasArg(OPT_w
);
113 error(Twine(args
.getArgString(missingIndex
)) + ": missing argument");
115 handleColorDiagnostics(ctx
, args
);
117 for (const Arg
*arg
: args
.filtered(OPT_UNKNOWN
)) {
119 if (findNearest(arg
->getAsString(args
), nearest
) > 1)
120 error("unknown argument '" + arg
->getAsString(args
) + "'");
122 error("unknown argument '" + arg
->getAsString(args
) +
123 "', did you mean '" + nearest
+ "'");
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
);
136 static std::string
rewritePath(StringRef 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
) {
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()) {
162 os
<< quote(rewriteInputPath(arg
->getValue())) << "\n";
165 os
<< "-o " << quote(path::filename(arg
->getValue())) << "\n";
168 if (std::optional
<MemoryBufferRef
> buffer
= readFile(arg
->getValue()))
169 for (StringRef path
: args::getLines(*buffer
))
170 os
<< quote(rewriteInputPath(path
)) << "\n";
173 case OPT_weak_library
:
174 case OPT_load_hidden
:
175 os
<< arg
->getSpelling() << " "
176 << quote(rewriteInputPath(arg
->getValue())) << "\n";
180 case OPT_bundle_loader
:
181 case OPT_exported_symbols_list
:
184 case OPT_unexported_symbols_list
:
185 os
<< arg
->getSpelling() << " " << quote(rewritePath(arg
->getValue()))
189 os
<< arg
->getSpelling() << " " << quote(arg
->getValue(0)) << " "
190 << quote(arg
->getValue(1)) << " "
191 << quote(rewritePath(arg
->getValue(2))) << "\n";
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"));
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
);
215 return saver().save(tbdPath
.str());
217 bool dylibExists
= fs::exists(dylibPath
);
218 searchedDylib(dylibPath
, dylibExists
);
220 return saver().save(dylibPath
);
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
];
233 if (explicitlyLinked
)
234 file
->setExplicitlyLinked();
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
);
243 error("could not load TAPI file at " + mbref
.getBufferIdentifier() +
244 ": " + toString(result
.takeError()));
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.
256 if (newFile
->exportingFile
)
257 newFile
->parseReexports(**result
);
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.
268 if (newFile
->exportingFile
)
269 newFile
->parseLoadCommands(mbref
);
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
) {
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
);
289 return saver().save(location
.str());
295 StringRef
macho::rerootPath(StringRef path
) {
296 if (!path::is_absolute(path
, path::Style::posix
) || path
.ends_with(".o"))
299 if (std::optional
<StringRef
> rerootedPath
=
300 findPathCombination(path
, config
->systemLibraryRoots
))
301 return *rerootedPath
;
306 uint32_t macho::getModTime(StringRef path
) {
307 if (config
->zeroModTime
)
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
);
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 "
335 void macho::DependencyTracker::write(StringRef version
,
336 const SetVector
<InputFile
*> &inputs
,
342 raw_fd_ostream
os(path
, ec
, fs::OF_None
);
344 warn("Error writing dependency info to file");
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
);
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
);