Coverage Report

Created: 2018-06-25 02:00

/Users/buildslave/jenkins/workspace/clang-stage2-coverage-R/llvm/tools/lld/MinGW/Driver.cpp
Line
Count
Source (jump to first uncovered line)
1
//===- MinGW/Driver.cpp ---------------------------------------------------===//
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
/// GNU ld style linker driver for COFF currently supporting mingw-w64.
11
///
12
//===----------------------------------------------------------------------===//
13
14
#include "lld/Common/Driver.h"
15
#include "lld/Common/ErrorHandler.h"
16
#include "llvm/ADT/ArrayRef.h"
17
#include "llvm/ADT/Optional.h"
18
#include "llvm/ADT/StringExtras.h"
19
#include "llvm/ADT/StringRef.h"
20
#include "llvm/Option/Arg.h"
21
#include "llvm/Option/ArgList.h"
22
#include "llvm/Option/Option.h"
23
#include "llvm/Support/CommandLine.h"
24
#include "llvm/Support/FileSystem.h"
25
#include "llvm/Support/Path.h"
26
27
#if !defined(_MSC_VER) && !defined(__MINGW32__)
28
#include <unistd.h>
29
#endif
30
31
using namespace lld;
32
using namespace llvm;
33
34
// Create OptTable
35
enum {
36
  OPT_INVALID = 0,
37
#define OPTION(_1, _2, ID, _4, _5, _6, _7, _8, _9, _10, _11, _12) OPT_##ID,
38
#include "Options.inc"
39
#undef OPTION
40
};
41
42
// Create prefix string literals used in Options.td
43
#define PREFIX(NAME, VALUE) static const char *const NAME[] = VALUE;
44
#include "Options.inc"
45
#undef PREFIX
46
47
// Create table mapping all options defined in Options.td
48
static const opt::OptTable::Info InfoTable[] = {
49
#define OPTION(X1, X2, ID, KIND, GROUP, ALIAS, X7, X8, X9, X10, X11, X12)      \
50
  {X1, X2, X10,         X11,         OPT_##ID, opt::Option::KIND##Class,       \
51
   X9, X8, OPT_##GROUP, OPT_##ALIAS, X7,       X12},
52
#include "Options.inc"
53
#undef OPTION
54
};
55
56
namespace {
57
class MinGWOptTable : public opt::OptTable {
58
public:
59
64
  MinGWOptTable() : OptTable(InfoTable, false) {}
60
  opt::InputArgList parse(ArrayRef<const char *> Argv);
61
};
62
} // namespace
63
64
64
opt::InputArgList MinGWOptTable::parse(ArrayRef<const char *> Argv) {
65
64
  unsigned MissingIndex;
66
64
  unsigned MissingCount;
67
64
68
64
  SmallVector<const char *, 256> Vec(Argv.data(), Argv.data() + Argv.size());
69
64
  opt::InputArgList Args = this->ParseArgs(Vec, MissingIndex, MissingCount);
70
64
71
64
  if (MissingCount)
72
0
    fatal(StringRef(Args.getArgString(MissingIndex)) + ": missing argument");
73
64
  for (auto *Arg : Args.filtered(OPT_UNKNOWN))
74
0
    fatal("unknown argument: " + Arg->getSpelling());
75
64
  if (!Args.hasArg(OPT_INPUT) && 
!Args.hasArg(OPT_l)3
)
76
0
    fatal("no input files");
77
64
  return Args;
78
64
}
79
80
// Find a file by concatenating given paths.
81
4
static Optional<std::string> findFile(StringRef Path1, const Twine &Path2) {
82
4
  SmallString<128> S;
83
4
  sys::path::append(S, Path1, Path2);
84
4
  if (sys::fs::exists(S))
85
4
    return S.str().str();
86
0
  return None;
87
0
}
88
89
// This is for -lfoo. We'll look for libfoo.dll.a or libfoo.a from search paths.
90
static std::string
91
4
searchLibrary(StringRef Name, ArrayRef<StringRef> SearchPaths, bool BStatic) {
92
4
  if (Name.startswith(":")) {
93
0
    for (StringRef Dir : SearchPaths)
94
0
      if (Optional<std::string> S = findFile(Dir, Name.substr(1)))
95
0
        return *S;
96
0
    fatal("unable to find library -l" + Name);
97
4
  }
98
4
99
4
  for (StringRef Dir : SearchPaths) {
100
4
    if (!BStatic)
101
2
      if (Optional<std::string> S = findFile(Dir, "lib" + Name + ".dll.a"))
102
2
        return *S;
103
2
    if (Optional<std::string> S = findFile(Dir, "lib" + Name + ".a"))
104
2
      return *S;
105
2
  }
106
4
  
fatal("unable to find library -l" + Name)0
;
107
4
}
108
109
// Convert Unix-ish command line arguments to Windows-ish ones and
110
// then call coff::link.
111
64
bool mingw::link(ArrayRef<const char *> ArgsArr, raw_ostream &Diag) {
112
64
  MinGWOptTable Parser;
113
64
  opt::InputArgList Args = Parser.parse(ArgsArr.slice(1));
114
64
115
64
  std::vector<std::string> LinkArgs;
116
674
  auto Add = [&](const Twine &S) { LinkArgs.push_back(S.str()); };
117
64
118
64
  Add("lld-link");
119
64
  Add("-lldmingw");
120
64
121
64
  if (auto *A = Args.getLastArg(OPT_entry)) {
122
4
    StringRef S = A->getValue();
123
4
    if (Args.getLastArgValue(OPT_m) == "i386pe" && 
S.startswith("_")1
)
124
1
      Add("-entry:" + S.substr(1));
125
3
    else
126
3
      Add("-entry:" + S);
127
4
  }
128
64
129
64
  if (auto *A = Args.getLastArg(OPT_subs))
130
2
    Add("-subsystem:" + StringRef(A->getValue()));
131
64
  if (auto *A = Args.getLastArg(OPT_out_implib))
132
2
    Add("-implib:" + StringRef(A->getValue()));
133
64
  if (auto *A = Args.getLastArg(OPT_stack))
134
2
    Add("-stack:" + StringRef(A->getValue()));
135
64
  if (auto *A = Args.getLastArg(OPT_output_def))
136
1
    Add("-output-def:" + StringRef(A->getValue()));
137
64
  if (auto *A = Args.getLastArg(OPT_image_base))
138
2
    Add("-base:" + StringRef(A->getValue()));
139
64
  if (auto *A = Args.getLastArg(OPT_map))
140
4
    Add("-lldmap:" + StringRef(A->getValue()));
141
64
142
64
  if (auto *A = Args.getLastArg(OPT_o))
143
3
    Add("-out:" + StringRef(A->getValue()));
144
61
  else if (Args.hasArg(OPT_shared))
145
7
    Add("-out:a.dll");
146
54
  else
147
54
    Add("-out:a.exe");
148
64
149
64
  if (auto *A = Args.getLastArg(OPT_pdb)) {
150
1
    Add("-debug");
151
1
    Add("-pdb:" + StringRef(A->getValue()));
152
63
  } else if (!Args.hasArg(OPT_strip_all)) {
153
61
    Add("-debug:dwarf");
154
61
  }
155
64
156
64
  if (Args.hasArg(OPT_shared))
157
7
    Add("-dll");
158
64
  if (Args.hasArg(OPT_verbose))
159
2
    Add("-verbose");
160
64
  if (Args.hasArg(OPT_export_all_symbols))
161
1
    Add("-export-all-symbols");
162
64
  if (Args.hasArg(OPT_large_address_aware))
163
1
    Add("-largeaddressaware");
164
64
  if (Args.hasArg(OPT_kill_at))
165
2
    Add("-kill-at");
166
64
167
64
  if (Args.getLastArgValue(OPT_m) != "thumb2pe" &&
168
64
      
Args.getLastArgValue(OPT_m) != "arm64pe"62
&&
!Args.hasArg(OPT_dynamicbase)60
)
169
58
    Add("-dynamicbase:no");
170
64
171
64
  if (Args.hasFlag(OPT_gc_sections, OPT_no_gc_sections, false))
172
2
    Add("-opt:ref");
173
62
  else
174
62
    Add("-opt:noref");
175
64
176
64
  if (auto *A = Args.getLastArg(OPT_icf)) {
177
6
    StringRef S = A->getValue();
178
6
    if (S == "all")
179
2
      Add("-opt:icf");
180
4
    else if (S == "safe" || 
S == "none"2
)
181
4
      Add("-opt:noicf");
182
0
    else
183
0
      fatal("unknown parameter: --icf=" + S);
184
58
  } else {
185
58
    Add("-opt:noicf");
186
58
  }
187
64
188
64
  if (auto *A = Args.getLastArg(OPT_m)) {
189
64
    StringRef S = A->getValue();
190
64
    if (S == "i386pe")
191
6
      Add("-machine:x86");
192
58
    else if (S == "i386pep")
193
54
      Add("-machine:x64");
194
4
    else if (S == "thumb2pe")
195
2
      Add("-machine:arm");
196
2
    else if (S == "arm64pe")
197
2
      Add("-machine:arm64");
198
0
    else
199
0
      fatal("unknown parameter: -m" + S);
200
64
  }
201
64
202
64
  for (auto *A : Args.filtered(OPT_mllvm))
203
2
    Add("-mllvm:" + StringRef(A->getValue()));
204
64
205
64
  for (auto *A : Args.filtered(OPT_Xlink))
206
1
    Add(A->getValue());
207
64
208
64
  if (Args.getLastArgValue(OPT_m) == "i386pe")
209
6
    Add("-alternatename:__image_base__=___ImageBase");
210
58
  else
211
58
    Add("-alternatename:__image_base__=__ImageBase");
212
64
213
64
  std::vector<StringRef> SearchPaths;
214
64
  for (auto *A : Args.filtered(OPT_L))
215
3
    SearchPaths.push_back(A->getValue());
216
64
217
64
  StringRef Prefix = "";
218
64
  bool Static = false;
219
263
  for (auto *A : Args) {
220
263
    switch (A->getOption().getUnaliasedOption().getID()) {
221
263
    case OPT_INPUT:
222
68
      if (StringRef(A->getValue()).endswith_lower(".def"))
223
2
        Add("-def:" + StringRef(A->getValue()));
224
66
      else
225
66
        Add(Prefix + StringRef(A->getValue()));
226
68
      break;
227
263
    case OPT_l:
228
4
      Add(Prefix + searchLibrary(A->getValue(), SearchPaths, Static));
229
4
      break;
230
263
    case OPT_whole_archive:
231
2
      Prefix = "-wholearchive:";
232
2
      break;
233
263
    case OPT_no_whole_archive:
234
2
      Prefix = "";
235
2
      break;
236
263
    case OPT_Bstatic:
237
2
      Static = true;
238
2
      break;
239
263
    case OPT_Bdynamic:
240
1
      Static = false;
241
1
      break;
242
263
    }
243
263
  }
244
64
245
64
  if (Args.hasArg(OPT_verbose) || 
Args.hasArg(OPT__HASH_HASH_HASH)62
)
246
64
    outs() << llvm::join(LinkArgs, " ") << "\n";
247
64
248
64
  if (Args.hasArg(OPT__HASH_HASH_HASH))
249
64
    return true;
250
0
251
0
  // Repack vector of strings to vector of const char pointers for coff::link.
252
0
  std::vector<const char *> Vec;
253
0
  for (const std::string &S : LinkArgs)
254
0
    Vec.push_back(S.c_str());
255
0
  return coff::link(Vec, true);
256
0
}