1 //===- DriverDispatcher.cpp - Support using LLD as a library --------------===//
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 //===----------------------------------------------------------------------===//
9 #include "lld/Common/CommonLinkerContext.h"
10 #include "lld/Common/Driver.h"
11 #include "lld/Common/ErrorHandler.h"
12 #include "lld/Common/Memory.h"
13 #include "llvm/ADT/STLExtras.h"
14 #include "llvm/ADT/SmallVector.h"
15 #include "llvm/ADT/StringSwitch.h"
16 #include "llvm/ADT/Twine.h"
17 #include "llvm/Support/CommandLine.h"
18 #include "llvm/Support/CrashRecoveryContext.h"
19 #include "llvm/Support/InitLLVM.h"
20 #include "llvm/Support/Path.h"
21 #include "llvm/Support/Process.h"
22 #include "llvm/TargetParser/Host.h"
23 #include "llvm/TargetParser/Triple.h"
28 using namespace llvm::sys
;
30 static void err(const Twine
&s
) { llvm::errs() << s
<< "\n"; }
32 static Flavor
getFlavor(StringRef s
) {
33 return StringSwitch
<Flavor
>(s
)
34 .CasesLower("ld", "ld.lld", "gnu", Gnu
)
35 .CasesLower("wasm", "ld-wasm", Wasm
)
36 .CaseLower("link", WinLink
)
37 .CasesLower("ld64", "ld64.lld", "darwin", Darwin
)
41 static cl::TokenizerCallback
getDefaultQuotingStyle() {
42 if (Triple(sys::getProcessTriple()).getOS() == Triple::Win32
)
43 return cl::TokenizeWindowsCommandLine
;
44 return cl::TokenizeGNUCommandLine
;
47 static bool isPETargetName(StringRef s
) {
48 return s
== "i386pe" || s
== "i386pep" || s
== "thumb2pe" || s
== "arm64pe";
51 static std::optional
<bool> isPETarget(llvm::ArrayRef
<const char *> args
) {
52 for (auto it
= args
.begin(); it
+ 1 != args
.end(); ++it
) {
53 if (StringRef(*it
) != "-m")
55 return isPETargetName(*(it
+ 1));
58 // Expand response files (arguments in the form of @<filename>)
59 // to allow detecting the -m argument from arguments in them.
60 SmallVector
<const char *, 256> expandedArgs(args
.data(),
61 args
.data() + args
.size());
64 cl::ExpansionContext
ectx(saver
.getAllocator(), getDefaultQuotingStyle());
65 if (Error e
= ectx
.expandResponseFiles(expandedArgs
)) {
66 err(toString(std::move(e
)));
70 for (auto it
= expandedArgs
.begin(); it
+ 1 != expandedArgs
.end(); ++it
) {
71 if (StringRef(*it
) != "-m")
73 return isPETargetName(*(it
+ 1));
76 #ifdef LLD_DEFAULT_LD_LLD_IS_MINGW
83 static Flavor
parseProgname(StringRef progname
) {
84 // Use GNU driver for "ld" by default.
88 // Progname may be something like "lld-gnu". Parse it.
89 SmallVector
<StringRef
, 3> v
;
90 progname
.split(v
, "-");
92 if (Flavor f
= getFlavor(s
))
98 parseFlavorWithoutMinGW(llvm::SmallVectorImpl
<const char *> &argsV
) {
99 // Parse -flavor option.
100 if (argsV
.size() > 1 && argsV
[1] == StringRef("-flavor")) {
101 if (argsV
.size() <= 2) {
102 err("missing arg value for '-flavor'");
105 Flavor f
= getFlavor(argsV
[2]);
107 err("Unknown flavor: " + StringRef(argsV
[2]));
110 argsV
.erase(argsV
.begin() + 1, argsV
.begin() + 3);
114 // Deduct the flavor from argv[0].
115 StringRef arg0
= path::filename(argsV
[0]);
116 if (arg0
.ends_with_insensitive(".exe"))
117 arg0
= arg0
.drop_back(4);
118 Flavor f
= parseProgname(arg0
);
120 err("lld is a generic driver.\n"
121 "Invoke ld.lld (Unix), ld64.lld (macOS), lld-link (Windows), wasm-ld"
122 " (WebAssembly) instead");
128 static Flavor
parseFlavor(llvm::SmallVectorImpl
<const char *> &argsV
) {
129 Flavor f
= parseFlavorWithoutMinGW(argsV
);
131 auto isPE
= isPETarget(argsV
);
140 static Driver
whichDriver(llvm::SmallVectorImpl
<const char *> &argsV
,
141 llvm::ArrayRef
<DriverDef
> drivers
) {
142 Flavor f
= parseFlavor(argsV
);
144 llvm::find_if(drivers
, [=](auto &driverdef
) { return driverdef
.f
== f
; });
145 if (it
== drivers
.end()) {
146 // Driver is invalid or not available in this build.
147 return [](llvm::ArrayRef
<const char *>, llvm::raw_ostream
&,
148 llvm::raw_ostream
&, bool, bool) { return false; };
154 bool inTestOutputDisabled
= false;
156 /// Universal linker main(). This linker emulates the gnu, darwin, or
157 /// windows linker based on the argv[0] or -flavor option.
158 int unsafeLldMain(llvm::ArrayRef
<const char *> args
,
159 llvm::raw_ostream
&stdoutOS
, llvm::raw_ostream
&stderrOS
,
160 llvm::ArrayRef
<DriverDef
> drivers
, bool exitEarly
) {
161 SmallVector
<const char *, 256> argsV(args
);
162 Driver d
= whichDriver(argsV
, drivers
);
163 // Run the driver. If an error occurs, false will be returned.
164 int r
= !d(argsV
, stdoutOS
, stderrOS
, exitEarly
, inTestOutputDisabled
);
165 // At this point 'r' is either 1 for error, and 0 for no error.
167 // Call exit() if we can to avoid calling destructors.
171 // Delete the global context and clear the global context pointer, so that it
172 // cannot be accessed anymore.
173 CommonLinkerContext::destroy();
179 Result
lld::lldMain(llvm::ArrayRef
<const char *> args
,
180 llvm::raw_ostream
&stdoutOS
, llvm::raw_ostream
&stderrOS
,
181 llvm::ArrayRef
<DriverDef
> drivers
) {
184 // The crash recovery is here only to be able to recover from arbitrary
185 // control flow when fatal() is called (through setjmp/longjmp or
187 llvm::CrashRecoveryContext crc
;
188 if (!crc
.RunSafely([&]() {
189 r
= unsafeLldMain(args
, stdoutOS
, stderrOS
, drivers
,
190 /*exitEarly=*/false);
192 return {crc
.RetCode
, /*canRunAgain=*/false};
195 // Cleanup memory and reset everything back in pristine condition. This path
196 // is only taken when LLD is in test, or when it is used as a library.
197 llvm::CrashRecoveryContext crc
;
198 if (!crc
.RunSafely([&]() { CommonLinkerContext::destroy(); })) {
199 // The memory is corrupted beyond any possible recovery.
200 return {r
, /*canRunAgain=*/false};
202 return {r
, /*canRunAgain=*/true};