1 //===- tools/lld/lld.cpp - Linker Driver Dispatcher -----------------------===//
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This is the entry point to the lld driver. This is a thin wrapper which
11 // dispatches to the given platform specific driver.
13 // If there is -flavor option, it is dispatched according to the arguments.
14 // If the flavor parameter is not present, then it is dispatched according
17 //===----------------------------------------------------------------------===//
19 #include "lld/Common/Driver.h"
20 #include "llvm/ADT/STLExtras.h"
21 #include "llvm/ADT/StringSwitch.h"
22 #include "llvm/ADT/Twine.h"
23 #include "llvm/Support/ManagedStatic.h"
24 #include "llvm/Support/Path.h"
25 #include "llvm/Support/PrettyStackTrace.h"
26 #include "llvm/Support/Signals.h"
30 using namespace llvm::sys;
35 WinLink, // -flavor link
36 Darwin, // -flavor darwin
40 LLVM_ATTRIBUTE_NORETURN static void die(const Twine &S) {
45 static Flavor getFlavor(StringRef S) {
46 return StringSwitch<Flavor>(S)
47 .CasesLower("ld", "ld.lld", "gnu", Gnu)
48 .CasesLower("wasm", "ld-wasm", Wasm)
49 .CaseLower("link", WinLink)
50 .CasesLower("ld64", "ld64.lld", "darwin", Darwin)
54 static bool isPETarget(const std::vector<const char *> &V) {
55 for (auto It = V.begin(); It + 1 != V.end(); ++It) {
56 if (StringRef(*It) != "-m")
58 StringRef S = *(It + 1);
59 return S == "i386pe" || S == "i386pep" || S == "thumb2pe" || S == "arm64pe";
64 static Flavor parseProgname(StringRef Progname) {
66 // Use Darwin driver for "ld" on Darwin.
72 // Use GNU driver for "ld" on other Unix-like system.
77 // Progname may be something like "lld-gnu". Parse it.
78 SmallVector<StringRef, 3> V;
79 Progname.split(V, "-");
81 if (Flavor F = getFlavor(S))
86 static Flavor parseFlavor(std::vector<const char *> &V) {
87 // Parse -flavor option.
88 if (V.size() > 1 && V[1] == StringRef("-flavor")) {
90 die("missing arg value for '-flavor'");
91 Flavor F = getFlavor(V[2]);
93 die("Unknown flavor: " + StringRef(V[2]));
94 V.erase(V.begin() + 1, V.begin() + 3);
98 // Deduct the flavor from argv[0].
99 StringRef Arg0 = path::filename(V[0]);
100 if (Arg0.endswith_lower(".exe"))
101 Arg0 = Arg0.drop_back(4);
102 return parseProgname(Arg0);
105 /// Universal linker main(). This linker emulates the gnu, darwin, or
106 /// windows linker based on the argv[0] or -flavor option.
107 int main(int Argc, const char **Argv) {
108 // Standard set up, so program fails gracefully.
109 sys::PrintStackTraceOnErrorSignal(Argv[0]);
110 PrettyStackTraceProgram StackPrinter(Argc, Argv);
111 llvm_shutdown_obj Shutdown;
113 std::vector<const char *> Args(Argv, Argv + Argc);
115 return !elf::link(Args, true);
117 switch (parseFlavor(Args)) {
119 if (isPETarget(Args))
120 return !mingw::link(Args);
121 return !elf::link(Args, true);
123 return !coff::link(Args, true);
125 return !mach_o::link(Args);
127 return !wasm::link(Args, true);
129 die("lld is a generic driver.\n"
130 "Invoke ld.lld (Unix), ld (macOS) or lld-link (Windows) instead.");