Coverage Report

Created: 2018-01-17 17:22

/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
56
  MinGWOptTable() : OptTable(InfoTable, false) {}
60
  opt::InputArgList parse(ArrayRef<const char *> Argv);
61
};
62
} // namespace
63
64
56
opt::InputArgList MinGWOptTable::parse(ArrayRef<const char *> Argv) {
65
56
  unsigned MissingIndex;
66
56
  unsigned MissingCount;
67
56
68
56
  SmallVector<const char *, 256> Vec(Argv.data(), Argv.data() + Argv.size());
69
56
  opt::InputArgList Args = this->ParseArgs(Vec, MissingIndex, MissingCount);
70
56
71
56
  if (MissingCount)
72
0
    fatal(StringRef(Args.getArgString(MissingIndex)) + ": missing argument");
73
56
  for (auto *Arg : Args.filtered(OPT_UNKNOWN))
74
0
    fatal("unknown argument: " + Arg->getSpelling());
75
56
  if (!Args.hasArg(OPT_INPUT) && 
!Args.hasArg(OPT_l)3
)
76
0
    fatal("no input files");
77
56
  return Args;
78
56
}
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
0
}
108
109
// Convert Unix-ish command line arguments to Windows-ish ones and
110
// then call coff::link.
111
56
bool mingw::link(ArrayRef<const char *> ArgsArr, raw_ostream &Diag) {
112
56
  MinGWOptTable Parser;
113
56
  opt::InputArgList Args = Parser.parse(ArgsArr.slice(1));
114
56
115
56
  std::vector<std::string> LinkArgs;
116
585
  auto Add = [&](const Twine &S) { LinkArgs.push_back(S.str()); };
117
56
118
56
  Add("lld-link");
119
56
  Add("-lldmingw");
120
56
121
56
  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
56
129
56
  if (auto *A = Args.getLastArg(OPT_subs))
130
2
    Add("-subsystem:" + StringRef(A->getValue()));
131
56
  if (auto *A = Args.getLastArg(OPT_out_implib))
132
2
    Add("-implib:" + StringRef(A->getValue()));
133
56
  if (auto *A = Args.getLastArg(OPT_stack))
134
2
    Add("-stack:" + StringRef(A->getValue()));
135
56
  if (auto *A = Args.getLastArg(OPT_output_def))
136
1
    Add("-output-def:" + StringRef(A->getValue()));
137
56
  if (auto *A = Args.getLastArg(OPT_image_base))
138
2
    Add("-base:" + StringRef(A->getValue()));
139
56
140
56
  if (auto *A = Args.getLastArg(OPT_o))
141
3
    Add("-out:" + StringRef(A->getValue()));
142
53
  else if (Args.hasArg(OPT_shared))
143
5
    Add("-out:a.dll");
144
48
  else
145
48
    Add("-out:a.exe");
146
56
147
56
  if (Args.hasArg(OPT_shared))
148
5
    Add("-dll");
149
56
  if (Args.hasArg(OPT_verbose))
150
2
    Add("-verbose");
151
56
  if (Args.hasArg(OPT_export_all_symbols))
152
1
    Add("-export-all-symbols");
153
56
  if (!Args.hasArg(OPT_strip_all))
154
54
    Add("-debug:dwarf");
155
56
  if (Args.hasArg(OPT_large_address_aware))
156
1
    Add("-largeaddressaware");
157
56
158
56
  if (Args.getLastArgValue(OPT_m) != "thumb2pe" &&
159
56
      
Args.getLastArgValue(OPT_m) != "arm64pe"54
&&
!Args.hasArg(OPT_dynamicbase)52
)
160
50
    Add("-dynamicbase:no");
161
56
162
56
  if (Args.hasFlag(OPT_gc_sections, OPT_no_gc_sections, false))
163
2
    Add("-opt:ref");
164
54
  else
165
54
    Add("-opt:noref");
166
56
167
56
  if (auto *A = Args.getLastArg(OPT_icf)) {
168
6
    StringRef S = A->getValue();
169
6
    if (S == "all")
170
2
      Add("-opt:icf");
171
4
    else if (S == "safe" || 
S == "none"2
)
172
4
      Add("-opt:noicf");
173
0
    else
174
0
      fatal("unknown parameter: --icf=" + S);
175
50
  } else {
176
50
    Add("-opt:noicf");
177
50
  }
178
56
179
56
  if (auto *A = Args.getLastArg(OPT_m)) {
180
56
    StringRef S = A->getValue();
181
56
    if (S == "i386pe")
182
4
      Add("-machine:x86");
183
52
    else if (S == "i386pep")
184
48
      Add("-machine:x64");
185
4
    else if (S == "thumb2pe")
186
2
      Add("-machine:arm");
187
2
    else if (S == "arm64pe")
188
2
      Add("-machine:arm64");
189
0
    else
190
0
      fatal("unknown parameter: -m" + S);
191
56
  }
192
56
193
56
  for (auto *A : Args.filtered(OPT_mllvm))
194
2
    Add("-mllvm:" + StringRef(A->getValue()));
195
56
196
56
  for (auto *A : Args.filtered(OPT_Xlink))
197
1
    Add(A->getValue());
198
56
199
56
  if (Args.getLastArgValue(OPT_m) == "i386pe")
200
4
    Add("-alternatename:__image_base__=___ImageBase");
201
52
  else
202
52
    Add("-alternatename:__image_base__=__ImageBase");
203
56
204
56
  std::vector<StringRef> SearchPaths;
205
56
  for (auto *A : Args.filtered(OPT_L))
206
3
    SearchPaths.push_back(A->getValue());
207
56
208
56
  StringRef Prefix = "";
209
56
  bool Static = false;
210
284
  for (auto *A : Args) {
211
284
    switch (A->getOption().getUnaliasedOption().getID()) {
212
284
    case OPT_INPUT:
213
60
      if (StringRef(A->getValue()).endswith_lower(".def"))
214
2
        Add("-def:" + StringRef(A->getValue()));
215
58
      else
216
58
        Add(Prefix + StringRef(A->getValue()));
217
60
      break;
218
284
    case OPT_l:
219
4
      Add(Prefix + searchLibrary(A->getValue(), SearchPaths, Static));
220
4
      break;
221
284
    case OPT_whole_archive:
222
2
      Prefix = "-wholearchive:";
223
2
      break;
224
284
    case OPT_no_whole_archive:
225
2
      Prefix = "";
226
2
      break;
227
284
    case OPT_Bstatic:
228
2
      Static = true;
229
2
      break;
230
284
    case OPT_Bdynamic:
231
1
      Static = false;
232
1
      break;
233
284
    }
234
284
  }
235
56
236
56
  if (Args.hasArg(OPT_verbose) || 
Args.hasArg(OPT__HASH_HASH_HASH)54
)
237
56
    outs() << llvm::join(LinkArgs, " ") << "\n";
238
56
239
56
  if (Args.hasArg(OPT__HASH_HASH_HASH))
240
56
    return true;
241
0
242
0
  // Repack vector of strings to vector of const char pointers for coff::link.
243
0
  std::vector<const char *> Vec;
244
0
  for (const std::string &S : LinkArgs)
245
0
    Vec.push_back(S.c_str());
246
0
  return coff::link(Vec, true);
247
0
}