Coverage Report

Created: 2019-07-24 05:18

/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
// 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
// MinGW is a GNU development environment for Windows. It consists of GNU
10
// tools such as GCC and GNU ld. Unlike Cygwin, there's no POSIX-compatible
11
// layer, as it aims to be a native development toolchain.
12
//
13
// lld/MinGW is a drop-in replacement for GNU ld/MinGW.
14
//
15
// Being a native development tool, a MinGW linker is not very different from
16
// Microsoft link.exe, so a MinGW linker can be implemented as a thin wrapper
17
// for lld/COFF. This driver takes Unix-ish command line options, translates
18
// them to Windows-ish ones, and then passes them to lld/COFF.
19
//
20
// When this driver calls the lld/COFF driver, it passes a hidden option
21
// "-lldmingw" along with other user-supplied options, to run the lld/COFF
22
// linker in "MinGW mode".
23
//
24
// There are subtle differences between MS link.exe and GNU ld/MinGW, and GNU
25
// ld/MinGW implements a few GNU-specific features. Such features are directly
26
// implemented in lld/COFF and enabled only when the linker is running in MinGW
27
// mode.
28
//
29
//===----------------------------------------------------------------------===//
30
31
#include "lld/Common/Driver.h"
32
#include "lld/Common/ErrorHandler.h"
33
#include "lld/Common/Memory.h"
34
#include "lld/Common/Version.h"
35
#include "llvm/ADT/ArrayRef.h"
36
#include "llvm/ADT/Optional.h"
37
#include "llvm/ADT/StringExtras.h"
38
#include "llvm/ADT/StringRef.h"
39
#include "llvm/ADT/Triple.h"
40
#include "llvm/Option/Arg.h"
41
#include "llvm/Option/ArgList.h"
42
#include "llvm/Option/Option.h"
43
#include "llvm/Support/CommandLine.h"
44
#include "llvm/Support/FileSystem.h"
45
#include "llvm/Support/Path.h"
46
47
#if !defined(_MSC_VER) && !defined(__MINGW32__)
48
#include <unistd.h>
49
#endif
50
51
using namespace lld;
52
using namespace llvm;
53
54
// Create OptTable
55
enum {
56
  OPT_INVALID = 0,
57
#define OPTION(_1, _2, ID, _4, _5, _6, _7, _8, _9, _10, _11, _12) OPT_##ID,
58
#include "Options.inc"
59
#undef OPTION
60
};
61
62
// Create prefix string literals used in Options.td
63
#define PREFIX(NAME, VALUE) static const char *const NAME[] = VALUE;
64
#include "Options.inc"
65
#undef PREFIX
66
67
// Create table mapping all options defined in Options.td
68
static const opt::OptTable::Info infoTable[] = {
69
#define OPTION(X1, X2, ID, KIND, GROUP, ALIAS, X7, X8, X9, X10, X11, X12)      \
70
  {X1, X2, X10,         X11,         OPT_##ID, opt::Option::KIND##Class,       \
71
   X9, X8, OPT_##GROUP, OPT_##ALIAS, X7,       X12},
72
#include "Options.inc"
73
#undef OPTION
74
};
75
76
namespace {
77
class MinGWOptTable : public opt::OptTable {
78
public:
79
90
  MinGWOptTable() : OptTable(infoTable, false) {}
80
  opt::InputArgList parse(ArrayRef<const char *> argv);
81
};
82
} // namespace
83
84
1
static void printHelp(const char *argv0) {
85
1
  MinGWOptTable().PrintHelp(
86
1
      outs(), (std::string(argv0) + " [options] file...").c_str(), "lld",
87
1
      false /*ShowHidden*/, true /*ShowAllAliases*/);
88
1
  outs() << "\n";
89
1
}
90
91
89
static cl::TokenizerCallback getQuotingStyle() {
92
89
  if (Triple(sys::getProcessTriple()).getOS() == Triple::Win32)
93
0
    return cl::TokenizeWindowsCommandLine;
94
89
  return cl::TokenizeGNUCommandLine;
95
89
}
96
97
89
opt::InputArgList MinGWOptTable::parse(ArrayRef<const char *> argv) {
98
89
  unsigned missingIndex;
99
89
  unsigned missingCount;
100
89
101
89
  SmallVector<const char *, 256> vec(argv.data(), argv.data() + argv.size());
102
89
  cl::ExpandResponseFiles(saver, getQuotingStyle(), vec);
103
89
  opt::InputArgList args = this->ParseArgs(vec, missingIndex, missingCount);
104
89
105
89
  if (missingCount)
106
0
    fatal(StringRef(args.getArgString(missingIndex)) + ": missing argument");
107
89
  for (auto *arg : args.filtered(OPT_UNKNOWN))
108
0
    fatal("unknown argument: " + arg->getAsString(args));
109
89
  return args;
110
89
}
111
112
// Find a file by concatenating given paths.
113
4
static Optional<std::string> findFile(StringRef path1, const Twine &path2) {
114
4
  SmallString<128> s;
115
4
  sys::path::append(s, path1, path2);
116
4
  if (sys::fs::exists(s))
117
4
    return s.str().str();
118
0
  return None;
119
0
}
120
121
// This is for -lfoo. We'll look for libfoo.dll.a or libfoo.a from search paths.
122
static std::string
123
4
searchLibrary(StringRef name, ArrayRef<StringRef> searchPaths, bool bStatic) {
124
4
  if (name.startswith(":")) {
125
0
    for (StringRef dir : searchPaths)
126
0
      if (Optional<std::string> s = findFile(dir, name.substr(1)))
127
0
        return *s;
128
0
    fatal("unable to find library -l" + name);
129
4
  }
130
4
131
4
  for (StringRef dir : searchPaths) {
132
4
    if (!bStatic)
133
2
      if (Optional<std::string> s = findFile(dir, "lib" + name + ".dll.a"))
134
2
        return *s;
135
2
    if (Optional<std::string> s = findFile(dir, "lib" + name + ".a"))
136
2
      return *s;
137
2
  }
138
4
  
fatal("unable to find library -l" + name)0
;
139
4
}
140
141
// Convert Unix-ish command line arguments to Windows-ish ones and
142
// then call coff::link.
143
89
bool mingw::link(ArrayRef<const char *> argsArr, raw_ostream &diag) {
144
89
  MinGWOptTable parser;
145
89
  opt::InputArgList args = parser.parse(argsArr.slice(1));
146
89
147
89
  if (args.hasArg(OPT_help)) {
148
1
    printHelp(argsArr[0]);
149
1
    return true;
150
1
  }
151
88
152
88
  // A note about "compatible with GNU linkers" message: this is a hack for
153
88
  // scripts generated by GNU Libtool 2.4.6 (released in February 2014 and
154
88
  // still the newest version in March 2017) or earlier to recognize LLD as
155
88
  // a GNU compatible linker. As long as an output for the -v option
156
88
  // contains "GNU" or "with BFD", they recognize us as GNU-compatible.
157
88
  if (args.hasArg(OPT_v) || 
args.hasArg(OPT_version)87
)
158
2
    message(getLLDVersion() + " (compatible with GNU linkers)");
159
88
160
88
  // The behavior of -v or --version is a bit strange, but this is
161
88
  // needed for compatibility with GNU linkers.
162
88
  if (args.hasArg(OPT_v) && 
!args.hasArg(OPT_INPUT)1
&&
!args.hasArg(OPT_l)1
)
163
1
    return true;
164
87
  if (args.hasArg(OPT_version))
165
1
    return true;
166
86
167
86
  if (!args.hasArg(OPT_INPUT) && 
!args.hasArg(OPT_l)3
)
168
0
    fatal("no input files");
169
86
170
86
  std::vector<std::string> linkArgs;
171
920
  auto add = [&](const Twine &s) { linkArgs.push_back(s.str()); };
172
86
173
86
  add("lld-link");
174
86
  add("-lldmingw");
175
86
176
86
  if (auto *a = args.getLastArg(OPT_entry)) {
177
4
    StringRef s = a->getValue();
178
4
    if (args.getLastArgValue(OPT_m) == "i386pe" && 
s.startswith("_")1
)
179
1
      add("-entry:" + s.substr(1));
180
3
    else
181
3
      add("-entry:" + s);
182
4
  }
183
86
184
86
  if (args.hasArg(OPT_major_os_version, OPT_minor_os_version,
185
86
                  OPT_major_subsystem_version, OPT_minor_subsystem_version)) {
186
6
    auto *majOSVer = args.getLastArg(OPT_major_os_version);
187
6
    auto *minOSVer = args.getLastArg(OPT_minor_os_version);
188
6
    auto *majSubSysVer = args.getLastArg(OPT_major_subsystem_version);
189
6
    auto *minSubSysVer = args.getLastArg(OPT_minor_subsystem_version);
190
6
    if (majOSVer && 
majSubSysVer3
&&
191
6
        
StringRef(majOSVer->getValue()) != StringRef(majSubSysVer->getValue())1
)
192
0
      warn("--major-os-version and --major-subsystem-version set to differing "
193
0
           "versions, not supported");
194
6
    if (minOSVer && 
minSubSysVer3
&&
195
6
        
StringRef(minOSVer->getValue()) != StringRef(minSubSysVer->getValue())1
)
196
0
      warn("--minor-os-version and --minor-subsystem-version set to differing "
197
0
           "versions, not supported");
198
6
    StringRef subSys = args.getLastArgValue(OPT_subs, "default");
199
6
    StringRef major = majOSVer ? 
majOSVer->getValue()3
200
6
                               : 
majSubSysVer 3
?
majSubSysVer->getValue()2
:
"6"1
;
201
6
    StringRef minor = minOSVer ? 
minOSVer->getValue()3
202
6
                               : 
minSubSysVer 3
?
minSubSysVer->getValue()2
:
""1
;
203
6
    StringRef sep = minor.empty() ? 
""1
:
"."5
;
204
6
    add("-subsystem:" + subSys + "," + major + sep + minor);
205
80
  } else if (auto *a = args.getLastArg(OPT_subs)) {
206
4
    add("-subsystem:" + StringRef(a->getValue()));
207
4
  }
208
86
209
86
  if (auto *a = args.getLastArg(OPT_out_implib))
210
2
    add("-implib:" + StringRef(a->getValue()));
211
86
  if (auto *a = args.getLastArg(OPT_stack))
212
2
    add("-stack:" + StringRef(a->getValue()));
213
86
  if (auto *a = args.getLastArg(OPT_output_def))
214
1
    add("-output-def:" + StringRef(a->getValue()));
215
86
  if (auto *a = args.getLastArg(OPT_image_base))
216
2
    add("-base:" + StringRef(a->getValue()));
217
86
  if (auto *a = args.getLastArg(OPT_map))
218
4
    add("-lldmap:" + StringRef(a->getValue()));
219
86
220
86
  if (auto *a = args.getLastArg(OPT_o))
221
3
    add("-out:" + StringRef(a->getValue()));
222
83
  else if (args.hasArg(OPT_shared))
223
7
    add("-out:a.dll");
224
76
  else
225
76
    add("-out:a.exe");
226
86
227
86
  if (auto *a = args.getLastArg(OPT_pdb)) {
228
3
    add("-debug");
229
3
    StringRef v = a->getValue();
230
3
    if (!v.empty())
231
2
      add("-pdb:" + v);
232
83
  } else if (args.hasArg(OPT_strip_debug)) {
233
2
    add("-debug:symtab");
234
81
  } else if (!args.hasArg(OPT_strip_all)) {
235
79
    add("-debug:dwarf");
236
79
  }
237
86
238
86
  if (args.hasArg(OPT_shared))
239
7
    add("-dll");
240
86
  if (args.hasArg(OPT_verbose))
241
2
    add("-verbose");
242
86
  if (args.hasArg(OPT_exclude_all_symbols))
243
1
    add("-exclude-all-symbols");
244
86
  if (args.hasArg(OPT_export_all_symbols))
245
1
    add("-export-all-symbols");
246
86
  if (args.hasArg(OPT_large_address_aware))
247
1
    add("-largeaddressaware");
248
86
  if (args.hasArg(OPT_kill_at))
249
2
    add("-kill-at");
250
86
  if (args.hasArg(OPT_appcontainer))
251
2
    add("-appcontainer");
252
86
253
86
  if (args.getLastArgValue(OPT_m) != "thumb2pe" &&
254
86
      
args.getLastArgValue(OPT_m) != "arm64pe"84
&&
!args.hasArg(OPT_dynamicbase)82
)
255
80
    add("-dynamicbase:no");
256
86
257
86
  if (args.hasFlag(OPT_no_insert_timestamp, OPT_insert_timestamp, false))
258
2
    add("-timestamp:0");
259
86
260
86
  if (args.hasFlag(OPT_gc_sections, OPT_no_gc_sections, false))
261
2
    add("-opt:ref");
262
84
  else
263
84
    add("-opt:noref");
264
86
265
86
  if (auto *a = args.getLastArg(OPT_icf)) {
266
6
    StringRef s = a->getValue();
267
6
    if (s == "all")
268
2
      add("-opt:icf");
269
4
    else if (s == "safe" || 
s == "none"2
)
270
4
      add("-opt:noicf");
271
0
    else
272
0
      fatal("unknown parameter: --icf=" + s);
273
80
  } else {
274
80
    add("-opt:noicf");
275
80
  }
276
86
277
86
  if (auto *a = args.getLastArg(OPT_m)) {
278
86
    StringRef s = a->getValue();
279
86
    if (s == "i386pe")
280
9
      add("-machine:x86");
281
77
    else if (s == "i386pep")
282
73
      add("-machine:x64");
283
4
    else if (s == "thumb2pe")
284
2
      add("-machine:arm");
285
2
    else if (s == "arm64pe")
286
2
      add("-machine:arm64");
287
0
    else
288
0
      fatal("unknown parameter: -m" + s);
289
86
  }
290
86
291
86
  for (auto *a : args.filtered(OPT_mllvm))
292
2
    add("-mllvm:" + StringRef(a->getValue()));
293
86
294
86
  for (auto *a : args.filtered(OPT_Xlink))
295
1
    add(a->getValue());
296
86
297
86
  if (args.getLastArgValue(OPT_m) == "i386pe")
298
9
    add("-alternatename:__image_base__=___ImageBase");
299
77
  else
300
77
    add("-alternatename:__image_base__=__ImageBase");
301
86
302
86
  for (auto *a : args.filtered(OPT_require_defined))
303
4
    add("-include:" + StringRef(a->getValue()));
304
86
  for (auto *a : args.filtered(OPT_undefined))
305
4
    add("-includeoptional:" + StringRef(a->getValue()));
306
86
307
86
  std::vector<StringRef> searchPaths;
308
86
  for (auto *a : args.filtered(OPT_L)) {
309
4
    searchPaths.push_back(a->getValue());
310
4
    add("-libpath:" + StringRef(a->getValue()));
311
4
  }
312
86
313
86
  StringRef prefix = "";
314
86
  bool isStatic = false;
315
365
  for (auto *a : args) {
316
365
    switch (a->getOption().getID()) {
317
365
    case OPT_INPUT:
318
90
      if (StringRef(a->getValue()).endswith_lower(".def"))
319
2
        add("-def:" + StringRef(a->getValue()));
320
88
      else
321
88
        add(prefix + StringRef(a->getValue()));
322
90
      break;
323
365
    case OPT_l:
324
4
      add(prefix + searchLibrary(a->getValue(), searchPaths, isStatic));
325
4
      break;
326
365
    case OPT_whole_archive:
327
2
      prefix = "-wholearchive:";
328
2
      break;
329
365
    case OPT_no_whole_archive:
330
2
      prefix = "";
331
2
      break;
332
365
    case OPT_Bstatic:
333
2
      isStatic = true;
334
2
      break;
335
365
    case OPT_Bdynamic:
336
1
      isStatic = false;
337
1
      break;
338
365
    }
339
365
  }
340
86
341
86
  if (args.hasArg(OPT_verbose) || 
args.hasArg(OPT__HASH_HASH_HASH)84
)
342
86
    outs() << llvm::join(linkArgs, " ") << "\n";
343
86
344
86
  if (args.hasArg(OPT__HASH_HASH_HASH))
345
86
    return true;
346
0
347
0
  // Repack vector of strings to vector of const char pointers for coff::link.
348
0
  std::vector<const char *> vec;
349
0
  for (const std::string &s : linkArgs)
350
0
    vec.push_back(s.c_str());
351
0
  return coff::link(vec, true);
352
0
}