Coverage Report

Created: 2018-01-17 21:32

/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
//                             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/Common/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
  Wasm,    // -flavor wasm
38
};
39
40
0
LLVM_ATTRIBUTE_NORETURN static void die(const Twine &S) {
41
0
  errs() << S << "\n";
42
0
  exit(1);
43
0
}
44
45
1.15k
static Flavor getFlavor(StringRef S) {
46
1.15k
  return StringSwitch<Flavor>(S)
47
1.15k
      .CasesLower("ld", "ld.lld", "gnu", Gnu)
48
1.15k
      .CasesLower("wasm", "ld-wasm", Wasm)
49
1.15k
      .CaseLower("link", WinLink)
50
1.15k
      .CasesLower("ld64", "ld64.lld", "darwin", Darwin)
51
1.15k
      .Default(Invalid);
52
1.15k
}
53
54
1.05k
static bool isPETarget(const std::vector<const char *> &V) {
55
7.09k
  for (auto It = V.begin(); It + 1 != V.end(); 
++It6.04k
) {
56
6.11k
    if (StringRef(*It) != "-m")
57
6.04k
      continue;
58
74
    StringRef S = *(It + 1);
59
74
    return S == "i386pe" || 
S == "i386pep"73
||
S == "thumb2pe"52
||
S == "arm64pe"51
;
60
74
  }
61
1.05k
  
return false980
;
62
1.05k
}
63
64
1.05k
static Flavor parseProgname(StringRef Progname) {
65
1.05k
#if __APPLE__
66
1.05k
  // Use Darwin driver for "ld" on Darwin.
67
1.05k
  if (Progname == "ld")
68
0
    return Darwin;
69
1.05k
#endif
70
1.05k
71
1.05k
#if LLVM_ON_UNIX
72
1.05k
  // Use GNU driver for "ld" on other Unix-like system.
73
1.05k
  if (Progname == "ld")
74
0
    return Gnu;
75
1.05k
#endif
76
1.05k
77
1.05k
  // Progname may be something like "lld-gnu". Parse it.
78
1.05k
  SmallVector<StringRef, 3> V;
79
1.05k
  Progname.split(V, "-");
80
1.05k
  for (StringRef S : V)
81
1.05k
    if (Flavor F = getFlavor(S))
82
1.05k
      return F;
83
1.05k
  
return Invalid0
;
84
1.05k
}
85
86
1.15k
static Flavor parseFlavor(std::vector<const char *> &V) {
87
1.15k
  // Parse -flavor option.
88
1.15k
  if (V.size() > 1 && V[1] == StringRef("-flavor")) {
89
102
    if (V.size() <= 2)
90
0
      die("missing arg value for '-flavor'");
91
102
    Flavor F = getFlavor(V[2]);
92
102
    if (F == Invalid)
93
0
      die("Unknown flavor: " + StringRef(V[2]));
94
102
    V.erase(V.begin() + 1, V.begin() + 3);
95
102
    return F;
96
102
  }
97
1.05k
98
1.05k
  // Deduct the flavor from argv[0].
99
1.05k
  StringRef Arg0 = path::filename(V[0]);
100
1.05k
  if (Arg0.endswith_lower(".exe"))
101
0
    Arg0 = Arg0.drop_back(4);
102
1.05k
  return parseProgname(Arg0);
103
1.05k
}
104
105
/// Universal linker main(). This linker emulates the gnu, darwin, or
106
/// windows linker based on the argv[0] or -flavor option.
107
1.15k
int main(int Argc, const char **Argv) {
108
1.15k
  // Standard set up, so program fails gracefully.
109
1.15k
  sys::PrintStackTraceOnErrorSignal(Argv[0]);
110
1.15k
  PrettyStackTraceProgram StackPrinter(Argc, Argv);
111
1.15k
  llvm_shutdown_obj Shutdown;
112
1.15k
113
1.15k
  std::vector<const char *> Args(Argv, Argv + Argc);
114
1.15k
  switch (parseFlavor(Args)) {
115
1.15k
  case Gnu:
116
1.05k
    if (isPETarget(Args))
117
24
      return !mingw::link(Args);
118
1.03k
    return !elf::link(Args, true);
119
1.03k
  case WinLink:
120
0
    return !coff::link(Args, true);
121
1.03k
  case Darwin:
122
102
    return !mach_o::link(Args);
123
1.03k
  case Wasm:
124
0
    return !wasm::link(Args, true);
125
1.03k
  default:
126
0
    die("lld is a generic driver.\n"
127
0
        "Invoke ld.lld (Unix), ld (macOS) or lld-link (Windows) instead.");
128
0
  }
129
0
}