]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - contrib/llvm/tools/lld/tools/lld/lld.cpp
Update lld to trunk r290819 and resolve conflicts.
[FreeBSD/FreeBSD.git] / contrib / llvm / tools / lld / tools / lld / lld.cpp
1 //===- tools/lld/lld.cpp - Linker Driver Dispatcher -----------------------===//
2 //
3 //                             The LLVM Linker
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // This is the entry point to the lld driver. This is a thin wrapper which
11 // dispatches to the given platform specific driver.
12 //
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
15 // to argv[0].
16 //
17 //===----------------------------------------------------------------------===//
18
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"
27
28 using namespace lld;
29 using namespace llvm;
30 using namespace llvm::sys;
31
32 enum Flavor {
33   Invalid,
34   Gnu,     // -flavor gnu
35   WinLink, // -flavor link
36   Darwin,  // -flavor darwin
37 };
38
39 LLVM_ATTRIBUTE_NORETURN static void die(const Twine &S) {
40   errs() << S << "\n";
41   exit(1);
42 }
43
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)
49       .Default(Invalid);
50 }
51
52 static Flavor parseProgname(StringRef Progname) {
53 #if __APPLE__
54   // Use Darwin driver for "ld" on Darwin.
55   if (Progname == "ld")
56     return Darwin;
57 #endif
58
59 #if LLVM_ON_UNIX
60   // Use GNU driver for "ld" on other Unix-like system.
61   if (Progname == "ld")
62     return Gnu;
63 #endif
64
65   // Progname may be something like "lld-gnu". Parse it.
66   SmallVector<StringRef, 3> V;
67   Progname.split(V, "-");
68   for (StringRef S : V)
69     if (Flavor F = getFlavor(S))
70       return F;
71   return Invalid;
72 }
73
74 static Flavor parseFlavor(std::vector<const char *> &V) {
75   // Parse -flavor option.
76   if (V.size() > 1 && V[1] == StringRef("-flavor")) {
77     if (V.size() <= 2)
78       die("missing arg value for '-flavor'");
79     Flavor F = getFlavor(V[2]);
80     if (F == Invalid)
81       die("Unknown flavor: " + StringRef(V[2]));
82     V.erase(V.begin() + 1, V.begin() + 3);
83     return F;
84   }
85
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);
91 }
92
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;
100
101   std::vector<const char *> Args(Argv, Argv + Argc);
102   return !elf::link(Args, true);
103 }