Coverage Report

Created: 2019-05-19 14:56

/Users/buildslave/jenkins/workspace/clang-stage2-coverage-R/llvm/tools/lld/tools/lld/lld.cpp
Line
Count
Source (jump to first uncovered line)
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 "llvm/ADT/STLExtras.h"
30
#include "llvm/ADT/StringSwitch.h"
31
#include "llvm/ADT/Twine.h"
32
#include "llvm/Support/InitLLVM.h"
33
#include "llvm/Support/Path.h"
34
#include <cstdlib>
35
36
using namespace lld;
37
using namespace llvm;
38
using namespace llvm::sys;
39
40
enum Flavor {
41
  Invalid,
42
  Gnu,     // -flavor gnu
43
  WinLink, // -flavor link
44
  Darwin,  // -flavor darwin
45
  Wasm,    // -flavor wasm
46
};
47
48
0
LLVM_ATTRIBUTE_NORETURN static void die(const Twine &S) {
49
0
  errs() << S << "\n";
50
0
  exit(1);
51
0
}
52
53
4.55k
static Flavor getFlavor(StringRef S) {
54
4.55k
  return StringSwitch<Flavor>(S)
55
4.55k
      .CasesLower("ld", "ld.lld", "gnu", Gnu)
56
4.55k
      .CasesLower("wasm", "ld-wasm", Wasm)
57
4.55k
      .CaseLower("link", WinLink)
58
4.55k
      .CasesLower("ld64", "ld64.lld", "darwin", Darwin)
59
4.55k
      .Default(Invalid);
60
4.55k
}
61
62
2.87k
static bool isPETarget(const std::vector<const char *> &V) {
63
17.0k
  for (auto It = V.begin(); It + 1 != V.end(); 
++It14.1k
) {
64
14.2k
    if (StringRef(*It) != "-m")
65
14.1k
      continue;
66
118
    StringRef S = *(It + 1);
67
118
    return S == "i386pe" || 
S == "i386pep"111
||
S == "thumb2pe"43
||
S == "arm64pe"41
;
68
118
  }
69
2.87k
  
return false2.75k
;
70
2.87k
}
71
72
3.91k
static Flavor parseProgname(StringRef Progname) {
73
3.91k
#if __APPLE__
74
3.91k
  // Use Darwin driver for "ld" on Darwin.
75
3.91k
  if (Progname == "ld")
76
1
    return Darwin;
77
3.91k
#endif
78
3.91k
79
3.91k
#if LLVM_ON_UNIX
80
3.91k
  // Use GNU driver for "ld" on other Unix-like system.
81
3.91k
  if (Progname == "ld")
82
0
    return Gnu;
83
3.91k
#endif
84
3.91k
85
3.91k
  // Progname may be something like "lld-gnu". Parse it.
86
3.91k
  SmallVector<StringRef, 3> V;
87
3.91k
  Progname.split(V, "-");
88
3.91k
  for (StringRef S : V)
89
4.55k
    if (Flavor F = getFlavor(S))
90
3.91k
      return F;
91
3.91k
  
return Invalid0
;
92
3.91k
}
93
94
3.91k
static Flavor parseFlavor(std::vector<const char *> &V) {
95
3.91k
  // Parse -flavor option.
96
3.91k
  if (V.size() > 1 && 
V[1] == StringRef("-flavor")3.90k
) {
97
0
    if (V.size() <= 2)
98
0
      die("missing arg value for '-flavor'");
99
0
    Flavor F = getFlavor(V[2]);
100
0
    if (F == Invalid)
101
0
      die("Unknown flavor: " + StringRef(V[2]));
102
0
    V.erase(V.begin() + 1, V.begin() + 3);
103
0
    return F;
104
0
  }
105
3.91k
106
3.91k
  // Deduct the flavor from argv[0].
107
3.91k
  StringRef Arg0 = path::filename(V[0]);
108
3.91k
  if (Arg0.endswith_lower(".exe"))
109
0
    Arg0 = Arg0.drop_back(4);
110
3.91k
  return parseProgname(Arg0);
111
3.91k
}
112
113
// If this function returns true, lld calls _exit() so that it quickly
114
// exits without invoking destructors of globally allocated objects.
115
//
116
// We don't want to do that if we are running tests though, because
117
// doing that breaks leak sanitizer. So, lit sets this environment variable,
118
// and we use it to detect whether we are running tests or not.
119
3.83k
static bool canExitEarly() { return StringRef(getenv("LLD_IN_TEST")) != "1"; }
120
121
/// Universal linker main(). This linker emulates the gnu, darwin, or
122
/// windows linker based on the argv[0] or -flavor option.
123
3.91k
int main(int Argc, const char **Argv) {
124
3.91k
  InitLLVM X(Argc, Argv);
125
3.91k
126
3.91k
  std::vector<const char *> Args(Argv, Argv + Argc);
127
3.91k
  switch (parseFlavor(Args)) {
128
3.91k
  case Gnu:
129
2.87k
    if (isPETarget(Args))
130
79
      return !mingw::link(Args);
131
2.79k
    return !elf::link(Args, canExitEarly());
132
2.79k
  case WinLink:
133
642
    return !coff::link(Args, canExitEarly());
134
2.79k
  case Darwin:
135
208
    return !mach_o::link(Args, canExitEarly());
136
2.79k
  case Wasm:
137
190
    return !wasm::link(Args, canExitEarly());
138
2.79k
  default:
139
0
    die("lld is a generic driver.\n"
140
0
        "Invoke ld.lld (Unix), ld64.lld (macOS), lld-link (Windows), wasm-ld"
141
0
        " (WebAssembly) instead");
142
3.91k
  }
143
3.91k
}