Coverage Report

Created: 2021-01-19 06:58

/Users/buildslave/jenkins/workspace/coverage/llvm-project/clang/lib/Driver/ToolChains/PS4CPU.cpp
Line
Count
Source (jump to first uncovered line)
1
//===--- PS4CPU.cpp - PS4CPU ToolChain Implementations ----------*- C++ -*-===//
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 "PS4CPU.h"
10
#include "FreeBSD.h"
11
#include "CommonArgs.h"
12
#include "clang/Driver/Compilation.h"
13
#include "clang/Driver/Driver.h"
14
#include "clang/Driver/DriverDiagnostic.h"
15
#include "clang/Driver/Options.h"
16
#include "clang/Driver/SanitizerArgs.h"
17
#include "llvm/Option/ArgList.h"
18
#include "llvm/Support/FileSystem.h"
19
#include "llvm/Support/Path.h"
20
#include <cstdlib> // ::getenv
21
22
using namespace clang::driver;
23
using namespace clang;
24
using namespace llvm::opt;
25
26
using clang::driver::tools::AddLinkerInputs;
27
28
void tools::PS4cpu::addProfileRTArgs(const ToolChain &TC, const ArgList &Args,
29
129
                                     ArgStringList &CmdArgs) {
30
129
  if ((Args.hasFlag(options::OPT_fprofile_arcs, options::OPT_fno_profile_arcs,
31
129
                    false) ||
32
127
       Args.hasFlag(options::OPT_fprofile_generate,
33
127
                    options::OPT_fno_profile_generate, false) ||
34
125
       Args.hasFlag(options::OPT_fprofile_generate_EQ,
35
125
                    options::OPT_fno_profile_generate, false) ||
36
124
       Args.hasFlag(options::OPT_fprofile_instr_generate,
37
124
                    options::OPT_fno_profile_instr_generate, false) ||
38
123
       Args.hasFlag(options::OPT_fprofile_instr_generate_EQ,
39
123
                    options::OPT_fno_profile_instr_generate, false) ||
40
122
       Args.hasFlag(options::OPT_fcs_profile_generate,
41
122
                    options::OPT_fno_profile_generate, false) ||
42
121
       Args.hasFlag(options::OPT_fcs_profile_generate_EQ,
43
121
                    options::OPT_fno_profile_generate, false) ||
44
121
       Args.hasArg(options::OPT_fcreate_profile) ||
45
121
       Args.hasArg(options::OPT_coverage)))
46
9
    CmdArgs.push_back("--dependent-lib=libclang_rt.profile-x86_64.a");
47
129
}
48
49
void tools::PS4cpu::Assemble::ConstructJob(Compilation &C, const JobAction &JA,
50
                                           const InputInfo &Output,
51
                                           const InputInfoList &Inputs,
52
                                           const ArgList &Args,
53
0
                                           const char *LinkingOutput) const {
54
0
  claimNoWarnArgs(Args);
55
0
  ArgStringList CmdArgs;
56
57
0
  Args.AddAllArgValues(CmdArgs, options::OPT_Wa_COMMA, options::OPT_Xassembler);
58
59
0
  CmdArgs.push_back("-o");
60
0
  CmdArgs.push_back(Output.getFilename());
61
62
0
  assert(Inputs.size() == 1 && "Unexpected number of inputs.");
63
0
  const InputInfo &Input = Inputs[0];
64
0
  assert(Input.isFilename() && "Invalid input.");
65
0
  CmdArgs.push_back(Input.getFilename());
66
67
0
  const char *Exec =
68
0
      Args.MakeArgString(getToolChain().GetProgramPath("orbis-as"));
69
0
  C.addCommand(std::make_unique<Command>(JA, *this,
70
0
                                         ResponseFileSupport::AtFileUTF8(),
71
0
                                         Exec, CmdArgs, Inputs, Output));
72
0
}
73
74
56
static void AddPS4SanitizerArgs(const ToolChain &TC, ArgStringList &CmdArgs) {
75
56
  const SanitizerArgs &SanArgs = TC.getSanitizerArgs();
76
56
  if (SanArgs.needsUbsanRt()) {
77
2
    CmdArgs.push_back("-lSceDbgUBSanitizer_stub_weak");
78
2
  }
79
56
  if (SanArgs.needsAsanRt()) {
80
4
    CmdArgs.push_back("-lSceDbgAddressSanitizer_stub_weak");
81
4
  }
82
56
}
83
84
void tools::PS4cpu::addSanitizerArgs(const ToolChain &TC,
85
129
                                     ArgStringList &CmdArgs) {
86
129
  const SanitizerArgs &SanArgs = TC.getSanitizerArgs();
87
129
  if (SanArgs.needsUbsanRt())
88
2
    CmdArgs.push_back("--dependent-lib=libSceDbgUBSanitizer_stub_weak.a");
89
129
  if (SanArgs.needsAsanRt())
90
4
    CmdArgs.push_back("--dependent-lib=libSceDbgAddressSanitizer_stub_weak.a");
91
129
}
92
93
void tools::PS4cpu::Link::ConstructJob(Compilation &C, const JobAction &JA,
94
                                       const InputInfo &Output,
95
                                       const InputInfoList &Inputs,
96
                                       const ArgList &Args,
97
62
                                       const char *LinkingOutput) const {
98
62
  const toolchains::FreeBSD &ToolChain =
99
62
      static_cast<const toolchains::FreeBSD &>(getToolChain());
100
62
  const Driver &D = ToolChain.getDriver();
101
62
  ArgStringList CmdArgs;
102
103
  // Silence warning for "clang -g foo.o -o foo"
104
62
  Args.ClaimAllArgs(options::OPT_g_Group);
105
  // and "clang -emit-llvm foo.o -o foo"
106
62
  Args.ClaimAllArgs(options::OPT_emit_llvm);
107
  // and for "clang -w foo.o -o foo". Other warning options are already
108
  // handled somewhere else.
109
62
  Args.ClaimAllArgs(options::OPT_w);
110
111
62
  if (!D.SysRoot.empty())
112
1
    CmdArgs.push_back(Args.MakeArgString("--sysroot=" + D.SysRoot));
113
114
62
  if (Args.hasArg(options::OPT_pie))
115
0
    CmdArgs.push_back("-pie");
116
117
62
  if (Args.hasArg(options::OPT_rdynamic))
118
0
    CmdArgs.push_back("-export-dynamic");
119
62
  if (Args.hasArg(options::OPT_shared))
120
5
    CmdArgs.push_back("--oformat=so");
121
122
62
  if (Output.isFilename()) {
123
62
    CmdArgs.push_back("-o");
124
62
    CmdArgs.push_back(Output.getFilename());
125
0
  } else {
126
0
    assert(Output.isNothing() && "Invalid output.");
127
0
  }
128
129
62
  if(!Args.hasArg(options::OPT_nostdlib, options::OPT_nodefaultlibs))
130
56
    AddPS4SanitizerArgs(ToolChain, CmdArgs);
131
132
62
  Args.AddAllArgs(CmdArgs, options::OPT_L);
133
62
  Args.AddAllArgs(CmdArgs, options::OPT_T_Group);
134
62
  Args.AddAllArgs(CmdArgs, options::OPT_e);
135
62
  Args.AddAllArgs(CmdArgs, options::OPT_s);
136
62
  Args.AddAllArgs(CmdArgs, options::OPT_t);
137
62
  Args.AddAllArgs(CmdArgs, options::OPT_r);
138
139
62
  if (Args.hasArg(options::OPT_Z_Xlinker__no_demangle))
140
0
    CmdArgs.push_back("--no-demangle");
141
142
62
  AddLinkerInputs(ToolChain, Inputs, Args, CmdArgs, JA);
143
144
62
  if (Args.hasArg(options::OPT_pthread)) {
145
0
    CmdArgs.push_back("-lpthread");
146
0
  }
147
148
62
  if (Args.hasArg(options::OPT_fuse_ld_EQ)) {
149
5
    D.Diag(diag::err_drv_unsupported_opt_for_target)
150
5
        << "-fuse-ld" << getToolChain().getTriple().str();
151
5
  }
152
153
62
  const char *Exec =
154
62
      Args.MakeArgString(ToolChain.GetProgramPath("orbis-ld"));
155
156
62
  C.addCommand(std::make_unique<Command>(JA, *this,
157
62
                                         ResponseFileSupport::AtFileUTF8(),
158
62
                                         Exec, CmdArgs, Inputs, Output));
159
62
}
160
161
toolchains::PS4CPU::PS4CPU(const Driver &D, const llvm::Triple &Triple,
162
                           const ArgList &Args)
163
138
    : Generic_ELF(D, Triple, Args) {
164
138
  if (Args.hasArg(clang::driver::options::OPT_static))
165
1
    D.Diag(clang::diag::err_drv_unsupported_opt_for_target) << "-static"
166
1
                                                            << "PS4";
167
168
  // Determine where to find the PS4 libraries. We use SCE_ORBIS_SDK_DIR
169
  // if it exists; otherwise use the driver's installation path, which
170
  // should be <SDK_DIR>/host_tools/bin.
171
172
138
  SmallString<512> PS4SDKDir;
173
138
  if (const char *EnvValue = getenv("SCE_ORBIS_SDK_DIR")) {
174
22
    if (!llvm::sys::fs::exists(EnvValue))
175
0
      getDriver().Diag(clang::diag::warn_drv_ps4_sdk_dir) << EnvValue;
176
22
    PS4SDKDir = EnvValue;
177
116
  } else {
178
116
    PS4SDKDir = getDriver().Dir;
179
116
    llvm::sys::path::append(PS4SDKDir, "/../../");
180
116
  }
181
182
  // By default, the driver won't report a warning if it can't find
183
  // PS4's include or lib directories. This behavior could be changed if
184
  // -Weverything or -Winvalid-or-nonexistent-directory options are passed.
185
  // If -isysroot was passed, use that as the SDK base path.
186
138
  std::string PrefixDir;
187
138
  if (const Arg *A = Args.getLastArg(options::OPT_isysroot)) {
188
7
    PrefixDir = A->getValue();
189
7
    if (!llvm::sys::fs::exists(PrefixDir))
190
6
      getDriver().Diag(clang::diag::warn_missing_sysroot) << PrefixDir;
191
7
  } else
192
131
    PrefixDir = std::string(PS4SDKDir.str());
193
194
138
  SmallString<512> PS4SDKIncludeDir(PrefixDir);
195
138
  llvm::sys::path::append(PS4SDKIncludeDir, "target/include");
196
138
  if (!Args.hasArg(options::OPT_nostdinc) &&
197
134
      !Args.hasArg(options::OPT_nostdlibinc) &&
198
134
      !Args.hasArg(options::OPT_isysroot) &&
199
127
      !Args.hasArg(options::OPT__sysroot_EQ) &&
200
123
      !llvm::sys::fs::exists(PS4SDKIncludeDir)) {
201
122
    getDriver().Diag(clang::diag::warn_drv_unable_to_find_directory_expected)
202
122
        << "PS4 system headers" << PS4SDKIncludeDir;
203
122
  }
204
205
138
  SmallString<512> PS4SDKLibDir(PS4SDKDir);
206
138
  llvm::sys::path::append(PS4SDKLibDir, "target/lib");
207
138
  if (!Args.hasArg(options::OPT_nostdlib) &&
208
134
      !Args.hasArg(options::OPT_nodefaultlibs) &&
209
132
      !Args.hasArg(options::OPT__sysroot_EQ) && 
!Args.hasArg(options::OPT_E)127
&&
210
122
      !Args.hasArg(options::OPT_c) && 
!Args.hasArg(options::OPT_S)70
&&
211
63
      !Args.hasArg(options::OPT_emit_ast) &&
212
60
      !llvm::sys::fs::exists(PS4SDKLibDir)) {
213
60
    getDriver().Diag(clang::diag::warn_drv_unable_to_find_directory_expected)
214
60
        << "PS4 system libraries" << PS4SDKLibDir;
215
60
    return;
216
60
  }
217
78
  getFilePaths().push_back(std::string(PS4SDKLibDir.str()));
218
78
}
219
220
0
Tool *toolchains::PS4CPU::buildAssembler() const {
221
0
  return new tools::PS4cpu::Assemble(*this);
222
0
}
223
224
62
Tool *toolchains::PS4CPU::buildLinker() const {
225
62
  return new tools::PS4cpu::Link(*this);
226
62
}
227
228
273
bool toolchains::PS4CPU::isPICDefault() const { return true; }
229
230
62
bool toolchains::PS4CPU::HasNativeLLVMSupport() const { return true; }
231
232
138
SanitizerMask toolchains::PS4CPU::getSupportedSanitizers() const {
233
138
  SanitizerMask Res = ToolChain::getSupportedSanitizers();
234
138
  Res |= SanitizerKind::Address;
235
138
  Res |= SanitizerKind::PointerCompare;
236
138
  Res |= SanitizerKind::PointerSubtract;
237
138
  Res |= SanitizerKind::Vptr;
238
138
  return Res;
239
138
}
240
241
void toolchains::PS4CPU::addClangTargetOptions(
242
    const ArgList &DriverArgs, ArgStringList &CC1Args,
243
135
    Action::OffloadKind DeviceOffloadingKind) const {
244
  // PS4 does not use init arrays.
245
135
  if (DriverArgs.hasArg(options::OPT_fuse_init_array)) {
246
1
    Arg *A = DriverArgs.getLastArg(options::OPT_fuse_init_array);
247
1
    getDriver().Diag(clang::diag::err_drv_unsupported_opt_for_target)
248
1
        << A->getAsString(DriverArgs) << getTriple().str();
249
1
  }
250
251
135
  CC1Args.push_back("-fno-use-init-array");
252
253
135
  const Arg *A =
254
135
      DriverArgs.getLastArg(options::OPT_fvisibility_from_dllstorageclass,
255
135
                            options::OPT_fno_visibility_from_dllstorageclass);
256
135
  if (!A ||
257
134
      
A->getOption().matches(options::OPT_fvisibility_from_dllstorageclass)4
) {
258
134
    CC1Args.push_back("-fvisibility-from-dllstorageclass");
259
260
134
    if (DriverArgs.hasArg(options::OPT_fvisibility_dllexport_EQ))
261
2
      DriverArgs.AddLastArg(CC1Args, options::OPT_fvisibility_dllexport_EQ);
262
132
    else
263
132
      CC1Args.push_back("-fvisibility-dllexport=protected");
264
265
134
    if (DriverArgs.hasArg(options::OPT_fvisibility_nodllstorageclass_EQ))
266
4
      DriverArgs.AddLastArg(CC1Args,
267
4
                            options::OPT_fvisibility_nodllstorageclass_EQ);
268
130
    else
269
130
      CC1Args.push_back("-fvisibility-nodllstorageclass=hidden");
270
271
134
    if (DriverArgs.hasArg(options::OPT_fvisibility_externs_dllimport_EQ))
272
4
      DriverArgs.AddLastArg(CC1Args,
273
4
                            options::OPT_fvisibility_externs_dllimport_EQ);
274
130
    else
275
130
      CC1Args.push_back("-fvisibility-externs-dllimport=default");
276
277
134
    if (DriverArgs.hasArg(
278
134
            options::OPT_fvisibility_externs_nodllstorageclass_EQ))
279
2
      DriverArgs.AddLastArg(
280
2
          CC1Args, options::OPT_fvisibility_externs_nodllstorageclass_EQ);
281
132
    else
282
132
      CC1Args.push_back("-fvisibility-externs-nodllstorageclass=default");
283
134
  }
284
135
}