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