xref: /openbsd-src/gnu/llvm/lld/tools/lld/lld.cpp (revision 05edf1c10c70aea023b49c9190728f24a4673803)
1 //===- tools/lld/lld.cpp - Linker Driver Dispatcher -----------------------===//
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 // This file contains the main function of the lld executable. The main
10 // function is a thin wrapper which dispatches to the platform specific
11 // driver.
12 //
13 // lld is a single executable that contains four different linkers for ELF,
14 // COFF, WebAssembly and Mach-O. The main function dispatches according to
15 // argv[0] (i.e. command name). The most common name for each target is shown
16 // below:
17 //
18 //  - ld.lld:    ELF (Unix)
19 //  - ld64:      Mach-O (macOS)
20 //  - lld-link:  COFF (Windows)
21 //  - ld-wasm:   WebAssembly
22 //
23 // lld can be invoked as "lld" along with "-flavor" option. This is for
24 // backward compatibility and not recommended.
25 //
26 //===----------------------------------------------------------------------===//
27 
28 #include "lld/Common/CommonLinkerContext.h"
29 #include "lld/Common/Driver.h"
30 #include "lld/Common/ErrorHandler.h"
31 #include "lld/Common/Memory.h"
32 #include "llvm/ADT/STLExtras.h"
33 #include "llvm/ADT/SmallVector.h"
34 #include "llvm/ADT/StringSwitch.h"
35 #include "llvm/ADT/Triple.h"
36 #include "llvm/ADT/Twine.h"
37 #include "llvm/Support/CommandLine.h"
38 #include "llvm/Support/CrashRecoveryContext.h"
39 #include "llvm/Support/Host.h"
40 #include "llvm/Support/InitLLVM.h"
41 #include "llvm/Support/Path.h"
42 #include "llvm/Support/PluginLoader.h"
43 #include "llvm/Support/Process.h"
44 #include <cstdlib>
45 #include <optional>
46 
47 using namespace lld;
48 using namespace llvm;
49 using namespace llvm::sys;
50 
51 enum Flavor {
52   Invalid,
53   Gnu,     // -flavor gnu
54   WinLink, // -flavor link
55   Darwin,  // -flavor darwin
56   Wasm,    // -flavor wasm
57 };
58 
die(const Twine & s)59 [[noreturn]] static void die(const Twine &s) {
60   llvm::errs() << s << "\n";
61   exit(1);
62 }
63 
getFlavor(StringRef s)64 static Flavor getFlavor(StringRef s) {
65   return StringSwitch<Flavor>(s)
66       .CasesLower("ld", "ld.lld", "gnu", Gnu)
67       .CasesLower("wasm", "ld-wasm", Wasm)
68       .CaseLower("link", WinLink)
69       .CasesLower("ld64", "ld64.lld", "darwin", Darwin)
70       .Default(Invalid);
71 }
72 
73 #ifdef LLD_ENABLE_MINGW
getDefaultQuotingStyle()74 static cl::TokenizerCallback getDefaultQuotingStyle() {
75   if (Triple(sys::getProcessTriple()).getOS() == Triple::Win32)
76     return cl::TokenizeWindowsCommandLine;
77   return cl::TokenizeGNUCommandLine;
78 }
79 
isPETargetName(StringRef s)80 static bool isPETargetName(StringRef s) {
81   return s == "i386pe" || s == "i386pep" || s == "thumb2pe" || s == "arm64pe";
82 }
83 
isPETarget(std::vector<const char * > & v)84 static bool isPETarget(std::vector<const char *> &v) {
85   for (auto it = v.begin(); it + 1 != v.end(); ++it) {
86     if (StringRef(*it) != "-m")
87       continue;
88     return isPETargetName(*(it + 1));
89   }
90   // Expand response files (arguments in the form of @<filename>)
91   // to allow detecting the -m argument from arguments in them.
92   SmallVector<const char *, 256> expandedArgs(v.data(), v.data() + v.size());
93   BumpPtrAllocator a;
94   StringSaver saver(a);
95   cl::ExpansionContext ECtx(saver.getAllocator(), getDefaultQuotingStyle());
96   if (Error Err = ECtx.expandResponseFiles(expandedArgs))
97     die(toString(std::move(Err)));
98   for (auto it = expandedArgs.begin(); it + 1 != expandedArgs.end(); ++it) {
99     if (StringRef(*it) != "-m")
100       continue;
101     return isPETargetName(*(it + 1));
102   }
103 
104 #ifdef LLD_DEFAULT_LD_LLD_IS_MINGW
105   return true;
106 #else
107   return false;
108 #endif
109 }
110 #endif
111 
parseProgname(StringRef progname)112 static Flavor parseProgname(StringRef progname) {
113   // Use GNU driver for "ld" by default.
114   if (progname == "ld")
115     return Gnu;
116 
117   // Progname may be something like "lld-gnu". Parse it.
118   SmallVector<StringRef, 3> v;
119   progname.split(v, "-");
120   for (StringRef s : v)
121     if (Flavor f = getFlavor(s))
122       return f;
123   return Invalid;
124 }
125 
parseFlavor(std::vector<const char * > & v)126 static Flavor parseFlavor(std::vector<const char *> &v) {
127   // Parse -flavor option.
128   if (v.size() > 1 && v[1] == StringRef("-flavor")) {
129     if (v.size() <= 2)
130       die("missing arg value for '-flavor'");
131     Flavor f = getFlavor(v[2]);
132     if (f == Invalid)
133       die("Unknown flavor: " + StringRef(v[2]));
134     v.erase(v.begin() + 1, v.begin() + 3);
135     return f;
136   }
137 
138   // Deduct the flavor from argv[0].
139   StringRef arg0 = path::filename(v[0]);
140   if (arg0.endswith_insensitive(".exe"))
141     arg0 = arg0.drop_back(4);
142   return parseProgname(arg0);
143 }
144 
145 bool inTestOutputDisabled = false;
146 
147 /// Universal linker main(). This linker emulates the gnu, darwin, or
148 /// windows linker based on the argv[0] or -flavor option.
lldMain(int argc,const char ** argv,llvm::raw_ostream & stdoutOS,llvm::raw_ostream & stderrOS,bool exitEarly=true)149 static int lldMain(int argc, const char **argv, llvm::raw_ostream &stdoutOS,
150                    llvm::raw_ostream &stderrOS, bool exitEarly = true) {
151   std::vector<const char *> args(argv, argv + argc);
152   auto link = [&args]() {
153     Flavor f = parseFlavor(args);
154 #ifdef LLD_ENABLE_MINGW
155     if (f == Gnu && isPETarget(args))
156       return mingw::link;
157     else if (f == Gnu)
158 #else
159     if (f == Gnu)
160 #endif
161       return elf::link;
162 #ifdef LLD_ENABLE_COFF
163     else if (f == WinLink)
164       return coff::link;
165 #endif
166 #ifdef LLD_ENABLE_MACHO
167     else if (f == Darwin)
168       return macho::link;
169 #endif
170 #ifdef LLD_ENABLE_WASM
171     else if (f == Wasm)
172       return lld::wasm::link;
173 #endif
174     else
175       die("lld is a generic driver.\n"
176           "Invoke ld.lld (Unix), ld64.lld (macOS), lld-link (Windows), wasm-ld"
177           " (WebAssembly) instead");
178   }();
179   // Run the driver. If an error occurs, false will be returned.
180   bool r = link(args, stdoutOS, stderrOS, exitEarly, inTestOutputDisabled);
181 
182   // Call exit() if we can to avoid calling destructors.
183   if (exitEarly)
184     exitLld(!r ? 1 : 0);
185 
186   // Delete the global context and clear the global context pointer, so that it
187   // cannot be accessed anymore.
188   CommonLinkerContext::destroy();
189 
190   return !r ? 1 : 0;
191 }
192 
193 // Similar to lldMain except that exceptions are caught.
safeLldMain(int argc,const char ** argv,llvm::raw_ostream & stdoutOS,llvm::raw_ostream & stderrOS)194 SafeReturn lld::safeLldMain(int argc, const char **argv,
195                             llvm::raw_ostream &stdoutOS,
196                             llvm::raw_ostream &stderrOS) {
197   int r = 0;
198   {
199     // The crash recovery is here only to be able to recover from arbitrary
200     // control flow when fatal() is called (through setjmp/longjmp or
201     // __try/__except).
202     llvm::CrashRecoveryContext crc;
203     if (!crc.RunSafely([&]() {
204           r = lldMain(argc, argv, stdoutOS, stderrOS, /*exitEarly=*/false);
205         }))
206       return {crc.RetCode, /*canRunAgain=*/false};
207   }
208 
209   // Cleanup memory and reset everything back in pristine condition. This path
210   // is only taken when LLD is in test, or when it is used as a library.
211   llvm::CrashRecoveryContext crc;
212   if (!crc.RunSafely([&]() { CommonLinkerContext::destroy(); })) {
213     // The memory is corrupted beyond any possible recovery.
214     return {r, /*canRunAgain=*/false};
215   }
216   return {r, /*canRunAgain=*/true};
217 }
218 
219 // When in lit tests, tells how many times the LLD tool should re-execute the
220 // main loop with the same inputs. When not in test, returns a value of 0 which
221 // signifies that LLD shall not release any memory after execution, to speed up
222 // process destruction.
inTestVerbosity()223 static unsigned inTestVerbosity() {
224   unsigned v = 0;
225   StringRef(getenv("LLD_IN_TEST")).getAsInteger(10, v);
226   return v;
227 }
228 
lld_main(int argc,char ** argv)229 int lld_main(int argc, char **argv) {
230   InitLLVM x(argc, argv);
231   sys::Process::UseANSIEscapeCodes(true);
232 
233   if (::getenv("FORCE_LLD_DIAGNOSTICS_CRASH")) {
234     llvm::errs()
235         << "crashing due to environment variable FORCE_LLD_DIAGNOSTICS_CRASH\n";
236     LLVM_BUILTIN_TRAP;
237   }
238 
239   // Not running in lit tests, just take the shortest codepath with global
240   // exception handling and no memory cleanup on exit.
241   if (!inTestVerbosity())
242     return lldMain(argc, const_cast<const char **>(argv), llvm::outs(),
243                    llvm::errs());
244 
245   std::optional<int> mainRet;
246   CrashRecoveryContext::Enable();
247 
248   for (unsigned i = inTestVerbosity(); i > 0; --i) {
249     // Disable stdout/stderr for all iterations but the last one.
250     inTestOutputDisabled = (i != 1);
251 
252     // Execute one iteration.
253     auto r = safeLldMain(argc, const_cast<const char **>(argv), llvm::outs(),
254                          llvm::errs());
255     if (!r.canRunAgain)
256       exitLld(r.ret); // Exit now, can't re-execute again.
257 
258     if (!mainRet) {
259       mainRet = r.ret;
260     } else if (r.ret != *mainRet) {
261       // Exit now, to fail the tests if the result is different between runs.
262       return r.ret;
263     }
264   }
265   return *mainRet;
266 }
267