Coverage Report

Created: 2021-01-26 06:56

/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
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
49
0
  Args.AddAllArgValues(CmdArgs, options::OPT_Wa_COMMA, options::OPT_Xassembler);
50
51
0
  CmdArgs.push_back("-o");
52
0
  CmdArgs.push_back(Output.getFilename());
53
54
0
  for (const auto &Input : Inputs)
55
0
    CmdArgs.push_back(Input.getFilename());
56
57
0
  const std::string Assembler = TC.GetProgramPath("as");
58
0
  Exec = Args.MakeArgString(Assembler);
59
60
0
  C.addCommand(std::make_unique<Command>(JA, *this, ResponseFileSupport::None(),
61
0
                                         Exec, CmdArgs, Inputs, Output));
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
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
84
32
  if (!D.SysRoot.empty())
85
13
    CmdArgs.push_back(Args.MakeArgString("--sysroot=" + D.SysRoot));
86
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
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
115
32
  if (Args.hasArg(options::OPT_shared)) {
116
7
    switch (T.getArch()) {
117
0
    default:
118
0
      llvm_unreachable("unsupported architecture");
119
0
    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
130
7
    CmdArgs.push_back("-shared");
131
1
    CmdArgs.push_back(Args.hasArg(options::OPT_static) ? "-Bstatic"
132
6
                                                       : "-Bdynamic");
133
134
7
    CmdArgs.push_back("--enable-auto-image-base");
135
136
7
    CmdArgs.push_back("--entry");
137
7
    CmdArgs.push_back(Args.MakeArgString(EntryPoint));
138
25
  } else {
139
25
    EntryPoint.append("mainCRTStartup");
140
141
1
    CmdArgs.push_back(Args.hasArg(options::OPT_static) ? "-Bstatic"
142
24
                                                       : "-Bdynamic");
143
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
149
    // FIXME: handle subsystem
150
25
  }
151
152
  // NOTE: deal with multiple definitions on Windows (e.g. COMDAT)
153
32
  CmdArgs.push_back("--allow-multiple-definition");
154
155
32
  CmdArgs.push_back("-o");
156
32
  CmdArgs.push_back(Output.getFilename());
157
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
162
7
    CmdArgs.push_back("--out-implib");
163
7
    CmdArgs.push_back(Args.MakeArgString(ImpLib));
164
7
  }
165
166
32
  Args.AddAllArgs(CmdArgs, options::OPT_L);
167
32
  TC.AddFilePathLibArgs(Args, CmdArgs);
168
32
  AddLinkerInputs(TC, Inputs, Args, CmdArgs, JA);
169
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
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
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
204
32
  Exec = Args.MakeArgString(TC.GetLinkerPath());
205
206
32
  C.addCommand(std::make_unique<Command>(JA, *this,
207
32
                                         ResponseFileSupport::AtFileUTF8(),
208
32
                                         Exec, CmdArgs, Inputs, Output));
209
32
}
210
211
CrossWindowsToolChain::CrossWindowsToolChain(const Driver &D,
212
                                             const llvm::Triple &T,
213
                                             const llvm::opt::ArgList &Args)
214
58
    : Generic_GCC(D, T, Args) {}
215
216
58
bool CrossWindowsToolChain::IsUnwindTablesDefault(const ArgList &Args) const {
217
  // FIXME: all non-x86 targets need unwind tables, however, LLVM currently does
218
  // not know how to emit them.
219
58
  return getArch() == llvm::Triple::x86_64;
220
58
}
221
222
43
bool CrossWindowsToolChain::isPICDefault() const {
223
43
  return getArch() == llvm::Triple::x86_64;
224
43
}
225
226
58
bool CrossWindowsToolChain::isPIEDefault() const {
227
58
  return getArch() == llvm::Triple::x86_64;
228
58
}
229
230
50
bool CrossWindowsToolChain::isPICDefaultForced() const {
231
50
  return getArch() == llvm::Triple::x86_64;
232
50
}
233
234
void CrossWindowsToolChain::
235
AddClangSystemIncludeArgs(const llvm::opt::ArgList &DriverArgs,
236
58
                          llvm::opt::ArgStringList &CC1Args) const {
237
58
  const Driver &D = getDriver();
238
58
  const std::string &SysRoot = D.SysRoot;
239
240
58
  auto AddSystemAfterIncludes = [&]() {
241
58
    for (const auto &P : DriverArgs.getAllArgValues(options::OPT_isystem_after))
242
4
      addSystemInclude(DriverArgs, CC1Args, P);
243
58
  };
244
245
58
  if (DriverArgs.hasArg(options::OPT_nostdinc)) {
246
1
    AddSystemAfterIncludes();
247
1
    return;
248
1
  }
249
250
57
  addSystemInclude(DriverArgs, CC1Args, SysRoot + "/usr/local/include");
251
57
  if (!DriverArgs.hasArg(options::OPT_nobuiltininc)) {
252
57
    SmallString<128> ResourceDir(D.ResourceDir);
253
57
    llvm::sys::path::append(ResourceDir, "include");
254
57
    addSystemInclude(DriverArgs, CC1Args, ResourceDir);
255
57
  }
256
57
  AddSystemAfterIncludes();
257
57
  addExternCSystemInclude(DriverArgs, CC1Args, SysRoot + "/usr/include");
258
57
}
259
260
void CrossWindowsToolChain::
261
AddClangCXXStdlibIncludeArgs(const llvm::opt::ArgList &DriverArgs,
262
25
                             llvm::opt::ArgStringList &CC1Args) const {
263
25
  const std::string &SysRoot = getDriver().SysRoot;
264
265
25
  if (DriverArgs.hasArg(options::OPT_nostdinc) ||
266
25
      DriverArgs.hasArg(options::OPT_nostdincxx))
267
0
    return;
268
269
25
  if (GetCXXStdlibType(DriverArgs) == ToolChain::CST_Libcxx)
270
1
    addSystemInclude(DriverArgs, CC1Args, SysRoot + "/usr/include/c++/v1");
271
25
}
272
273
void CrossWindowsToolChain::
274
AddCXXStdlibLibArgs(const llvm::opt::ArgList &Args,
275
1
                    llvm::opt::ArgStringList &CmdArgs) const {
276
1
  if (GetCXXStdlibType(Args) == ToolChain::CST_Libcxx)
277
1
    CmdArgs.push_back("-lc++");
278
1
}
279
280
58
clang::SanitizerMask CrossWindowsToolChain::getSupportedSanitizers() const {
281
58
  SanitizerMask Res = ToolChain::getSupportedSanitizers();
282
58
  Res |= SanitizerKind::Address;
283
58
  Res |= SanitizerKind::PointerCompare;
284
58
  Res |= SanitizerKind::PointerSubtract;
285
58
  return Res;
286
58
}
287
288
32
Tool *CrossWindowsToolChain::buildLinker() const {
289
32
  return new tools::CrossWindows::Linker(*this);
290
32
}
291
292
0
Tool *CrossWindowsToolChain::buildAssembler() const {
293
0
  return new tools::CrossWindows::Assembler(*this);
294
0
}