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