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/Driver.h" 29 #include "lld/Common/Memory.h" 30 #include "llvm/ADT/STLExtras.h" 31 #include "llvm/ADT/SmallVector.h" 32 #include "llvm/ADT/StringSwitch.h" 33 #include "llvm/ADT/Triple.h" 34 #include "llvm/ADT/Twine.h" 35 #include "llvm/Support/CommandLine.h" 36 #include "llvm/Support/InitLLVM.h" 37 #include "llvm/Support/Path.h" 38 #include <cstdlib> 39 40 using namespace lld; 41 using namespace llvm; 42 using namespace llvm::sys; 43 44 enum Flavor { 45 Invalid, 46 Gnu, // -flavor gnu 47 WinLink, // -flavor link 48 Darwin, // -flavor darwin 49 Wasm, // -flavor wasm 50 }; 51 52 LLVM_ATTRIBUTE_NORETURN static void die(const Twine &s) { 53 errs() << s << "\n"; 54 exit(1); 55 } 56 57 static Flavor getFlavor(StringRef s) { 58 return StringSwitch<Flavor>(s) 59 .CasesLower("ld", "ld.lld", "gnu", Gnu) 60 .CasesLower("wasm", "ld-wasm", Wasm) 61 .CaseLower("link", WinLink) 62 .CasesLower("ld64", "ld64.lld", "darwin", Darwin) 63 .Default(Invalid); 64 } 65 66 static cl::TokenizerCallback getDefaultQuotingStyle() { 67 if (Triple(sys::getProcessTriple()).getOS() == Triple::Win32) 68 return cl::TokenizeWindowsCommandLine; 69 return cl::TokenizeGNUCommandLine; 70 } 71 72 static bool isPETargetName(StringRef s) { 73 return s == "i386pe" || s == "i386pep" || s == "thumb2pe" || s == "arm64pe"; 74 } 75 76 static bool isPETarget(std::vector<const char *> &v) { 77 for (auto it = v.begin(); it + 1 != v.end(); ++it) { 78 if (StringRef(*it) != "-m") 79 continue; 80 return isPETargetName(*(it + 1)); 81 } 82 // Expand response files (arguments in the form of @<filename>) 83 // to allow detecting the -m argument from arguments in them. 84 SmallVector<const char *, 256> expandedArgs(v.data(), v.data() + v.size()); 85 cl::ExpandResponseFiles(saver, getDefaultQuotingStyle(), expandedArgs); 86 for (auto it = expandedArgs.begin(); it + 1 != expandedArgs.end(); ++it) { 87 if (StringRef(*it) != "-m") 88 continue; 89 return isPETargetName(*(it + 1)); 90 } 91 return false; 92 } 93 94 static Flavor parseProgname(StringRef progname) { 95 #if __APPLE__ 96 // Use Darwin driver for "ld" on Darwin. 97 if (progname == "ld") 98 return Darwin; 99 #endif 100 101 #if LLVM_ON_UNIX 102 // Use GNU driver for "ld" on other Unix-like system. 103 if (progname == "ld") 104 return Gnu; 105 #endif 106 107 // Progname may be something like "lld-gnu". Parse it. 108 SmallVector<StringRef, 3> v; 109 progname.split(v, "-"); 110 for (StringRef s : v) 111 if (Flavor f = getFlavor(s)) 112 return f; 113 return Invalid; 114 } 115 116 static Flavor parseFlavor(std::vector<const char *> &v) { 117 // Parse -flavor option. 118 if (v.size() > 1 && v[1] == StringRef("-flavor")) { 119 if (v.size() <= 2) 120 die("missing arg value for '-flavor'"); 121 Flavor f = getFlavor(v[2]); 122 if (f == Invalid) 123 die("Unknown flavor: " + StringRef(v[2])); 124 v.erase(v.begin() + 1, v.begin() + 3); 125 return f; 126 } 127 128 // Deduct the flavor from argv[0]. 129 StringRef arg0 = path::filename(v[0]); 130 if (arg0.endswith_lower(".exe")) 131 arg0 = arg0.drop_back(4); 132 return parseProgname(arg0); 133 } 134 135 // If this function returns true, lld calls _exit() so that it quickly 136 // exits without invoking destructors of globally allocated objects. 137 // 138 // We don't want to do that if we are running tests though, because 139 // doing that breaks leak sanitizer. So, lit sets this environment variable, 140 // and we use it to detect whether we are running tests or not. 141 static bool canExitEarly() { return StringRef(getenv("LLD_IN_TEST")) != "1"; } 142 143 /// Universal linker main(). This linker emulates the gnu, darwin, or 144 /// windows linker based on the argv[0] or -flavor option. 145 int main(int argc, const char **argv) { 146 InitLLVM x(argc, argv); 147 148 std::vector<const char *> args(argv, argv + argc); 149 #ifdef __FreeBSD__ 150 return !elf::link(args, true); 151 #else 152 switch (parseFlavor(args)) { 153 case Gnu: 154 if (isPETarget(args)) 155 return !mingw::link(args); 156 return !elf::link(args, canExitEarly()); 157 case WinLink: 158 return !coff::link(args, canExitEarly()); 159 case Darwin: 160 return !mach_o::link(args, canExitEarly()); 161 case Wasm: 162 return !wasm::link(args, canExitEarly()); 163 default: 164 die("lld is a generic driver.\n" 165 "Invoke ld.lld (Unix), ld64.lld (macOS), lld-link (Windows), wasm-ld" 166 " (WebAssembly) instead"); 167 } 168 #endif 169 } 170