Coverage Report

Created: 2020-09-19 12:23

/Users/buildslave/jenkins/workspace/coverage/llvm-project/clang/lib/Driver/ToolChains/CrossWindows.cpp
Line
Count
Source (jump to first uncovered line)
1
//===-- CrossWindows.cpp - Cross Windows Tool Chain -----------------------===//
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
#include "CrossWindows.h"
10
#include "CommonArgs.h"
11
#include "clang/Driver/Compilation.h"
12
#include "clang/Driver/Driver.h"
13
#include "clang/Driver/Options.h"
14
#include "clang/Driver/SanitizerArgs.h"
15
#include "llvm/Option/ArgList.h"
16
#include "llvm/Support/Path.h"
17
18
using namespace clang::driver;
19
using namespace clang::driver::toolchains;
20
21
using llvm::opt::ArgList;
22
using llvm::opt::ArgStringList;
23
24
void tools::CrossWindows::Assembler::ConstructJob(
25
    Compilation &C, const JobAction &JA, const InputInfo &Output,
26
    const InputInfoList &Inputs, const ArgList &Args,
27
0
    const char *LinkingOutput) const {
28
0
  claimNoWarnArgs(Args);
29
0
  const auto &TC =
30
0
      static_cast<const toolchains::CrossWindowsToolChain &>(getToolChain());
31
0
  ArgStringList CmdArgs;
32
0
  const char *Exec;
33
0
34
0
  switch (TC.getArch()) {
35
0
  default:
36
0
    llvm_unreachable("unsupported architecture");
37
0
  case llvm::Triple::arm:
38
0
  case llvm::Triple::thumb:
39
0
  case llvm::Triple::aarch64:
40
0
    break;
41
0
  case llvm::Triple::x86:
42
0
    CmdArgs.push_back("--32");
43
0
    break;
44
0
  case llvm::Triple::x86_64:
45
0
    CmdArgs.push_back("--64");
46
0
    break;
47
0
  }
48
0
49
0
  Args.AddAllArgValues(CmdArgs, options::OPT_Wa_COMMA, options::OPT_Xassembler);
50
0
51
0
  CmdArgs.push_back("-o");
52
0
  CmdArgs.push_back(Output.getFilename());
53
0
54
0
  for (const auto &Input : Inputs)
55
0
    CmdArgs.push_back(Input.getFilename());
56
0
57
0
  const std::string Assembler = TC.GetProgramPath("as");
58
0
  Exec = Args.MakeArgString(Assembler);
59
0
60
0
  C.addCommand(std::make_unique<Command>(JA, *this, ResponseFileSupport::None(),
61
0
                                         Exec, CmdArgs, Inputs));
62
0
}
63
64
void tools::CrossWindows::Linker::ConstructJob(
65
    Compilation &C, const JobAction &JA, const InputInfo &Output,
66
    const InputInfoList &Inputs, const ArgList &Args,
67
32
    const char *LinkingOutput) const {
68
32
  const auto &TC =
69
32
      static_cast<const toolchains::CrossWindowsToolChain &>(getToolChain());
70
32
  const llvm::Triple &T = TC.getTriple();
71
32
  const Driver &D = TC.getDriver();
72
32
  SmallString<128> EntryPoint;
73
32
  ArgStringList CmdArgs;
74
32
  const char *Exec;
75
32
76
  // Silence warning for "clang -g foo.o -o foo"
77
32
  Args.ClaimAllArgs(options::OPT_g_Group);
78
  // and "clang -emit-llvm foo.o -o foo"
79
32
  Args.ClaimAllArgs(options::OPT_emit_llvm);
80
  // and for "clang -w foo.o -o foo"
81
32
  Args.ClaimAllArgs(options::OPT_w);
82
  // Other warning options are already handled somewhere else.
83
32
84
32
  if (!D.SysRoot.empty())
85
13
    CmdArgs.push_back(Args.MakeArgString("--sysroot=" + D.SysRoot));
86
32
87
32
  if (Args.hasArg(options::OPT_pie))
88
0
    CmdArgs.push_back("-pie");
89
32
  if (Args.hasArg(options::OPT_rdynamic))
90
0
    CmdArgs.push_back("-export-dynamic");
91
32
  if (Args.hasArg(options::OPT_s))
92
0
    CmdArgs.push_back("--strip-all");
93
32
94
32
  CmdArgs.push_back("-m");
95
32
  switch (TC.getArch()) {
96
0
  default:
97
0
    llvm_unreachable("unsupported architecture");
98
14
  case llvm::Triple::arm:
99
14
  case llvm::Triple::thumb:
100
    // FIXME: this is incorrect for WinCE
101
14
    CmdArgs.push_back("thumb2pe");
102
14
    break;
103
0
  case llvm::Triple::aarch64:
104
0
    CmdArgs.push_back("arm64pe");
105
0
    break;
106
10
  case llvm::Triple::x86:
107
10
    CmdArgs.push_back("i386pe");
108
10
    EntryPoint.append("_");
109
10
    break;
110
8
  case llvm::Triple::x86_64:
111
8
    CmdArgs.push_back("i386pep");
112
8
    break;
113
32
  }
114
32
115
32
  if (Args.hasArg(options::OPT_shared)) {
116
7
    switch (T.getArch()) {
117
0
    default:
118
0
      llvm_unreachable("unsupported architecture");
119
7
    case llvm::Triple::aarch64:
120
7
    case llvm::Triple::arm:
121
7
    case llvm::Triple::thumb:
122
7
    case llvm::Triple::x86_64:
123
7
      EntryPoint.append("_DllMainCRTStartup");
124
7
      break;
125
0
    case llvm::Triple::x86:
126
0
      EntryPoint.append("_DllMainCRTStartup@12");
127
0
      break;
128
7
    }
129
7
130
7
    CmdArgs.push_back("-shared");
131
1
    CmdArgs.push_back(Args.hasArg(options::OPT_static) ? "-Bstatic"
132
6
                                                       : "-Bdynamic");
133
7
134
7
    CmdArgs.push_back("--enable-auto-image-base");
135
7
136
7
    CmdArgs.push_back("--entry");
137
7
    CmdArgs.push_back(Args.MakeArgString(EntryPoint));
138
25
  } else {
139
25
    EntryPoint.append("mainCRTStartup");
140
25
141
1
    CmdArgs.push_back(Args.hasArg(options::OPT_static) ? "-Bstatic"
142
24
                                                       : "-Bdynamic");
143
25
144
25
    if (!Args.hasArg(options::OPT_nostdlib, options::OPT_nostartfiles)) {
145
25
      CmdArgs.push_back("--entry");
146
25
      CmdArgs.push_back(Args.MakeArgString(EntryPoint));
147
25
    }
148
25
149
    // FIXME: handle subsystem
150
25
  }
151
32
152
  // NOTE: deal with multiple definitions on Windows (e.g. COMDAT)
153
32
  CmdArgs.push_back("--allow-multiple-definition");
154
32
155
32
  CmdArgs.push_back("-o");
156
32
  CmdArgs.push_back(Output.getFilename());
157
32
158
32
  if (Args.hasArg(options::OPT_shared) || 
Args.hasArg(options::OPT_rdynamic)25
) {
159
7
    SmallString<261> ImpLib(Output.getFilename());
160
7
    llvm::sys::path::replace_extension(ImpLib, ".lib");
161
7
162
7
    CmdArgs.push_back("--out-implib");
163
7
    CmdArgs.push_back(Args.MakeArgString(ImpLib));
164
7
  }
165
32
166
32
  Args.AddAllArgs(CmdArgs, options::OPT_L);
167
32
  TC.AddFilePathLibArgs(Args, CmdArgs);
168
32
  AddLinkerInputs(TC, Inputs, Args, CmdArgs, JA);
169
32
170
32
  if (TC.ShouldLinkCXXStdlib(Args)) {
171
1
    bool StaticCXX = Args.hasArg(options::OPT_static_libstdcxx) &&
172
0
                     !Args.hasArg(options::OPT_static);
173
1
    if (StaticCXX)
174
0
      CmdArgs.push_back("-Bstatic");
175
1
    TC.AddCXXStdlibLibArgs(Args, CmdArgs);
176
1
    if (StaticCXX)
177
0
      CmdArgs.push_back("-Bdynamic");
178
1
  }
179
32
180
32
  if (!Args.hasArg(options::OPT_nostdlib)) {
181
32
    if (!Args.hasArg(options::OPT_nodefaultlibs)) {
182
      // TODO handle /MT[d] /MD[d]
183
31
      CmdArgs.push_back("-lmsvcrt");
184
31
      AddRunTimeLibs(TC, D, CmdArgs, Args);
185
31
    }
186
32
  }
187
32
188
32
  if (TC.getSanitizerArgs().needsAsanRt()) {
189
    // TODO handle /MT[d] /MD[d]
190
3
    if (Args.hasArg(options::OPT_shared)) {
191
1
      CmdArgs.push_back(TC.getCompilerRTArgString(Args, "asan_dll_thunk"));
192
2
    } else {
193
2
      for (const auto &Lib : {"asan_dynamic", "asan_dynamic_runtime_thunk"})
194
4
        CmdArgs.push_back(TC.getCompilerRTArgString(Args, Lib));
195
      // Make sure the dynamic runtime thunk is not optimized out at link time
196
      // to ensure proper SEH handling.
197
2
      CmdArgs.push_back(Args.MakeArgString("--undefined"));
198
2
      CmdArgs.push_back(Args.MakeArgString(TC.getArch() == llvm::Triple::x86
199
1
                                               ? "___asan_seh_interceptor"
200
1
                                               : "__asan_seh_interceptor"));
201
2
    }
202
3
  }
203
32
204
32
  Exec = Args.MakeArgString(TC.GetLinkerPath());
205
32
206
32
  C.addCommand(std::make_unique<Command>(
207
32
      JA, *this, ResponseFileSupport::AtFileUTF8(), Exec, CmdArgs, Inputs));
208
32
}
209
210
CrossWindowsToolChain::CrossWindowsToolChain(const Driver &D,
211
                                             const llvm::Triple &T,
212
                                             const llvm::opt::ArgList &Args)
213
52
    : Generic_GCC(D, T, Args) {}
214
215
52
bool CrossWindowsToolChain::IsUnwindTablesDefault(const ArgList &Args) const {
216
  // FIXME: all non-x86 targets need unwind tables, however, LLVM currently does
217
  // not know how to emit them.
218
52
  return getArch() == llvm::Triple::x86_64;
219
52
}
220
221
43
bool CrossWindowsToolChain::isPICDefault() const {
222
43
  return getArch() == llvm::Triple::x86_64;
223
43
}
224
225
52
bool CrossWindowsToolChain::isPIEDefault() const {
226
52
  return getArch() == llvm::Triple::x86_64;
227
52
}
228
229
44
bool CrossWindowsToolChain::isPICDefaultForced() const {
230
44
  return getArch() == llvm::Triple::x86_64;
231
44
}
232
233
void CrossWindowsToolChain::
234
AddClangSystemIncludeArgs(const llvm::opt::ArgList &DriverArgs,
235
52
                          llvm::opt::ArgStringList &CC1Args) const {
236
52
  const Driver &D = getDriver();
237
52
  const std::string &SysRoot = D.SysRoot;
238
52
239
52
  auto AddSystemAfterIncludes = [&]() {
240
52
    for (const auto &P : DriverArgs.getAllArgValues(options::OPT_isystem_after))
241
4
      addSystemInclude(DriverArgs, CC1Args, P);
242
52
  };
243
52
244
52
  if (DriverArgs.hasArg(options::OPT_nostdinc)) {
245
1
    AddSystemAfterIncludes();
246
1
    return;
247
1
  }
248
51
249
51
  addSystemInclude(DriverArgs, CC1Args, SysRoot + "/usr/local/include");
250
51
  if (!DriverArgs.hasArg(options::OPT_nobuiltininc)) {
251
51
    SmallString<128> ResourceDir(D.ResourceDir);
252
51
    llvm::sys::path::append(ResourceDir, "include");
253
51
    addSystemInclude(DriverArgs, CC1Args, ResourceDir);
254
51
  }
255
51
  AddSystemAfterIncludes();
256
51
  addExternCSystemInclude(DriverArgs, CC1Args, SysRoot + "/usr/include");
257
51
}
258
259
void CrossWindowsToolChain::
260
AddClangCXXStdlibIncludeArgs(const llvm::opt::ArgList &DriverArgs,
261
25
                             llvm::opt::ArgStringList &CC1Args) const {
262
25
  const std::string &SysRoot = getDriver().SysRoot;
263
25
264
25
  if (DriverArgs.hasArg(options::OPT_nostdinc) ||
265
25
      DriverArgs.hasArg(options::OPT_nostdincxx))
266
0
    return;
267
25
268
25
  if (GetCXXStdlibType(DriverArgs) == ToolChain::CST_Libcxx)
269
1
    addSystemInclude(DriverArgs, CC1Args, SysRoot + "/usr/include/c++/v1");
270
25
}
271
272
void CrossWindowsToolChain::
273
AddCXXStdlibLibArgs(const llvm::opt::ArgList &DriverArgs,
274
1
                    llvm::opt::ArgStringList &CC1Args) const {
275
1
  if (GetCXXStdlibType(DriverArgs) == ToolChain::CST_Libcxx)
276
1
    CC1Args.push_back("-lc++");
277
1
}
278
279
52
clang::SanitizerMask CrossWindowsToolChain::getSupportedSanitizers() const {
280
52
  SanitizerMask Res = ToolChain::getSupportedSanitizers();
281
52
  Res |= SanitizerKind::Address;
282
52
  Res |= SanitizerKind::PointerCompare;
283
52
  Res |= SanitizerKind::PointerSubtract;
284
52
  return Res;
285
52
}
286
287
32
Tool *CrossWindowsToolChain::buildLinker() const {
288
32
  return new tools::CrossWindows::Linker(*this);
289
32
}
290
291
0
Tool *CrossWindowsToolChain::buildAssembler() const {
292
0
  return new tools::CrossWindows::Assembler(*this);
293
0
}