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