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/Driver/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
39 LLVM_ATTRIBUTE_NORETURN static void die(const Twine &S) {
44 static Flavor getFlavor(StringRef S) {
45 return StringSwitch<Flavor>(S)
46 .Cases("ld", "ld.lld", "gnu", Gnu)
47 .Case("link", WinLink)
48 .Case("darwin", Darwin)
52 static Flavor parseProgname(StringRef Progname) {
54 // Use Darwin driver for "ld" on Darwin.
60 // Use GNU driver for "ld" on other Unix-like system.
65 // Progname may be something like "lld-gnu". Parse it.
66 SmallVector<StringRef, 3> V;
67 Progname.split(V, "-");
69 if (Flavor F = getFlavor(S))
74 static Flavor parseFlavor(std::vector<const char *> &V) {
75 // Parse -flavor option.
76 if (V.size() > 1 && V[1] == StringRef("-flavor")) {
78 die("missing arg value for '-flavor'");
79 Flavor F = getFlavor(V[2]);
81 die("Unknown flavor: " + StringRef(V[2]));
82 V.erase(V.begin() + 1, V.begin() + 3);
86 // Deduct the flavor from argv[0].
87 StringRef Arg0 = path::filename(V[0]);
88 if (Arg0.endswith_lower(".exe"))
89 Arg0 = Arg0.drop_back(4);
90 return parseProgname(Arg0);
93 /// Universal linker main(). This linker emulates the gnu, darwin, or
94 /// windows linker based on the argv[0] or -flavor option.
95 int main(int Argc, const char **Argv) {
96 // Standard set up, so program fails gracefully.
97 sys::PrintStackTraceOnErrorSignal(Argv[0]);
98 PrettyStackTraceProgram StackPrinter(Argc, Argv);
99 llvm_shutdown_obj Shutdown;
101 std::vector<const char *> Args(Argv, Argv + Argc);
102 return !elf::link(Args, true);