Coverage Report

Created: 2019-07-24 05:18

/Users/buildslave/jenkins/workspace/clang-stage2-coverage-R/llvm/tools/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
110
                                     ArgStringList &CmdArgs) {
30
110
  if ((Args.hasFlag(options::OPT_fprofile_arcs, options::OPT_fno_profile_arcs,
31
110
                    false) ||
32
110
       Args.hasFlag(options::OPT_fprofile_generate,
33
108
                    options::OPT_fno_profile_instr_generate, false) ||
34
110
       Args.hasFlag(options::OPT_fprofile_generate_EQ,
35
107
                    options::OPT_fno_profile_instr_generate, false) ||
36
110
       Args.hasFlag(options::OPT_fprofile_instr_generate,
37
106
                    options::OPT_fno_profile_instr_generate, false) ||
38
110
       Args.hasFlag(options::OPT_fprofile_instr_generate_EQ,
39
105
                    options::OPT_fno_profile_instr_generate, false) ||
40
110
       
Args.hasArg(options::OPT_fcreate_profile)104
||
41
110
       
Args.hasArg(options::OPT_coverage)104
))
42
7
    CmdArgs.push_back("--dependent-lib=libclang_rt.profile-x86_64.a");
43
110
}
44
45
void tools::PS4cpu::Assemble::ConstructJob(Compilation &C, const JobAction &JA,
46
                                           const InputInfo &Output,
47
                                           const InputInfoList &Inputs,
48
                                           const ArgList &Args,
49
0
                                           const char *LinkingOutput) const {
50
0
  claimNoWarnArgs(Args);
51
0
  ArgStringList CmdArgs;
52
0
53
0
  Args.AddAllArgValues(CmdArgs, options::OPT_Wa_COMMA, options::OPT_Xassembler);
54
0
55
0
  CmdArgs.push_back("-o");
56
0
  CmdArgs.push_back(Output.getFilename());
57
0
58
0
  assert(Inputs.size() == 1 && "Unexpected number of inputs.");
59
0
  const InputInfo &Input = Inputs[0];
60
0
  assert(Input.isFilename() && "Invalid input.");
61
0
  CmdArgs.push_back(Input.getFilename());
62
0
63
0
  const char *Exec =
64
0
      Args.MakeArgString(getToolChain().GetProgramPath("orbis-as"));
65
0
  C.addCommand(llvm::make_unique<Command>(JA, *this, Exec, CmdArgs, Inputs));
66
0
}
67
68
38
static void AddPS4SanitizerArgs(const ToolChain &TC, ArgStringList &CmdArgs) {
69
38
  const SanitizerArgs &SanArgs = TC.getSanitizerArgs();
70
38
  if (SanArgs.needsUbsanRt()) {
71
2
    CmdArgs.push_back("-lSceDbgUBSanitizer_stub_weak");
72
2
  }
73
38
  if (SanArgs.needsAsanRt()) {
74
4
    CmdArgs.push_back("-lSceDbgAddressSanitizer_stub_weak");
75
4
  }
76
38
}
77
78
void tools::PS4cpu::addSanitizerArgs(const ToolChain &TC,
79
110
                                     ArgStringList &CmdArgs) {
80
110
  const SanitizerArgs &SanArgs = TC.getSanitizerArgs();
81
110
  if (SanArgs.needsUbsanRt())
82
2
    CmdArgs.push_back("--dependent-lib=libSceDbgUBSanitizer_stub_weak.a");
83
110
  if (SanArgs.needsAsanRt())
84
4
    CmdArgs.push_back("--dependent-lib=libSceDbgAddressSanitizer_stub_weak.a");
85
110
}
86
87
static void ConstructPS4LinkJob(const Tool &T, Compilation &C,
88
                                const JobAction &JA, const InputInfo &Output,
89
                                const InputInfoList &Inputs,
90
                                const ArgList &Args,
91
38
                                const char *LinkingOutput) {
92
38
  const toolchains::FreeBSD &ToolChain =
93
38
      static_cast<const toolchains::FreeBSD &>(T.getToolChain());
94
38
  const Driver &D = ToolChain.getDriver();
95
38
  ArgStringList CmdArgs;
96
38
97
38
  // Silence warning for "clang -g foo.o -o foo"
98
38
  Args.ClaimAllArgs(options::OPT_g_Group);
99
38
  // and "clang -emit-llvm foo.o -o foo"
100
38
  Args.ClaimAllArgs(options::OPT_emit_llvm);
101
38
  // and for "clang -w foo.o -o foo". Other warning options are already
102
38
  // handled somewhere else.
103
38
  Args.ClaimAllArgs(options::OPT_w);
104
38
105
38
  if (!D.SysRoot.empty())
106
1
    CmdArgs.push_back(Args.MakeArgString("--sysroot=" + D.SysRoot));
107
38
108
38
  if (Args.hasArg(options::OPT_pie))
109
0
    CmdArgs.push_back("-pie");
110
38
111
38
  if (Args.hasArg(options::OPT_rdynamic))
112
0
    CmdArgs.push_back("-export-dynamic");
113
38
  if (Args.hasArg(options::OPT_shared))
114
1
    CmdArgs.push_back("--oformat=so");
115
38
116
38
  if (Output.isFilename()) {
117
38
    CmdArgs.push_back("-o");
118
38
    CmdArgs.push_back(Output.getFilename());
119
38
  } else {
120
0
    assert(Output.isNothing() && "Invalid output.");
121
0
  }
122
38
123
38
  if(!Args.hasArg(options::OPT_nostdlib, options::OPT_nodefaultlibs))
124
33
    AddPS4SanitizerArgs(ToolChain, CmdArgs);
125
38
126
38
  Args.AddAllArgs(CmdArgs, options::OPT_L);
127
38
  Args.AddAllArgs(CmdArgs, options::OPT_T_Group);
128
38
  Args.AddAllArgs(CmdArgs, options::OPT_e);
129
38
  Args.AddAllArgs(CmdArgs, options::OPT_s);
130
38
  Args.AddAllArgs(CmdArgs, options::OPT_t);
131
38
  Args.AddAllArgs(CmdArgs, options::OPT_r);
132
38
133
38
  if (Args.hasArg(options::OPT_Z_Xlinker__no_demangle))
134
0
    CmdArgs.push_back("--no-demangle");
135
38
136
38
  AddLinkerInputs(ToolChain, Inputs, Args, CmdArgs, JA);
137
38
138
38
  if (Args.hasArg(options::OPT_pthread)) {
139
0
    CmdArgs.push_back("-lpthread");
140
0
  }
141
38
142
38
  const char *Exec = Args.MakeArgString(ToolChain.GetProgramPath("orbis-ld"));
143
38
144
38
  C.addCommand(llvm::make_unique<Command>(JA, T, Exec, CmdArgs, Inputs));
145
38
}
146
147
static void ConstructGoldLinkJob(const Tool &T, Compilation &C,
148
                                 const JobAction &JA, const InputInfo &Output,
149
                                 const InputInfoList &Inputs,
150
                                 const ArgList &Args,
151
6
                                 const char *LinkingOutput) {
152
6
  const toolchains::FreeBSD &ToolChain =
153
6
      static_cast<const toolchains::FreeBSD &>(T.getToolChain());
154
6
  const Driver &D = ToolChain.getDriver();
155
6
  ArgStringList CmdArgs;
156
6
157
6
  // Silence warning for "clang -g foo.o -o foo"
158
6
  Args.ClaimAllArgs(options::OPT_g_Group);
159
6
  // and "clang -emit-llvm foo.o -o foo"
160
6
  Args.ClaimAllArgs(options::OPT_emit_llvm);
161
6
  // and for "clang -w foo.o -o foo". Other warning options are already
162
6
  // handled somewhere else.
163
6
  Args.ClaimAllArgs(options::OPT_w);
164
6
165
6
  if (!D.SysRoot.empty())
166
0
    CmdArgs.push_back(Args.MakeArgString("--sysroot=" + D.SysRoot));
167
6
168
6
  if (Args.hasArg(options::OPT_pie))
169
0
    CmdArgs.push_back("-pie");
170
6
171
6
  if (Args.hasArg(options::OPT_static)) {
172
0
    CmdArgs.push_back("-Bstatic");
173
6
  } else {
174
6
    if (Args.hasArg(options::OPT_rdynamic))
175
0
      CmdArgs.push_back("-export-dynamic");
176
6
    CmdArgs.push_back("--eh-frame-hdr");
177
6
    if (Args.hasArg(options::OPT_shared)) {
178
5
      CmdArgs.push_back("-Bshareable");
179
5
    } else {
180
1
      CmdArgs.push_back("-dynamic-linker");
181
1
      CmdArgs.push_back("/libexec/ld-elf.so.1");
182
1
    }
183
6
    CmdArgs.push_back("--enable-new-dtags");
184
6
  }
185
6
186
6
  if (Output.isFilename()) {
187
6
    CmdArgs.push_back("-o");
188
6
    CmdArgs.push_back(Output.getFilename());
189
6
  } else {
190
0
    assert(Output.isNothing() && "Invalid output.");
191
0
  }
192
6
193
6
  if(!Args.hasArg(options::OPT_nostdlib, options::OPT_nodefaultlibs))
194
5
    AddPS4SanitizerArgs(ToolChain, CmdArgs);
195
6
196
6
  if (!Args.hasArg(options::OPT_nostdlib, options::OPT_nostartfiles)) {
197
5
    const char *crt1 = nullptr;
198
5
    if (!Args.hasArg(options::OPT_shared)) {
199
1
      if (Args.hasArg(options::OPT_pg))
200
0
        crt1 = "gcrt1.o";
201
1
      else if (Args.hasArg(options::OPT_pie))
202
0
        crt1 = "Scrt1.o";
203
1
      else
204
1
        crt1 = "crt1.o";
205
1
    }
206
5
    if (crt1)
207
1
      CmdArgs.push_back(Args.MakeArgString(ToolChain.GetFilePath(crt1)));
208
5
209
5
    CmdArgs.push_back(Args.MakeArgString(ToolChain.GetFilePath("crti.o")));
210
5
211
5
    const char *crtbegin = nullptr;
212
5
    if (Args.hasArg(options::OPT_static))
213
0
      crtbegin = "crtbeginT.o";
214
5
    else if (Args.hasArg(options::OPT_shared) || 
Args.hasArg(options::OPT_pie)1
)
215
4
      crtbegin = "crtbeginS.o";
216
1
    else
217
1
      crtbegin = "crtbegin.o";
218
5
219
5
    CmdArgs.push_back(Args.MakeArgString(ToolChain.GetFilePath(crtbegin)));
220
5
  }
221
6
222
6
  Args.AddAllArgs(CmdArgs, options::OPT_L);
223
6
  ToolChain.AddFilePathLibArgs(Args, CmdArgs);
224
6
  Args.AddAllArgs(CmdArgs, options::OPT_T_Group);
225
6
  Args.AddAllArgs(CmdArgs, options::OPT_e);
226
6
  Args.AddAllArgs(CmdArgs, options::OPT_s);
227
6
  Args.AddAllArgs(CmdArgs, options::OPT_t);
228
6
  Args.AddAllArgs(CmdArgs, options::OPT_r);
229
6
230
6
  if (Args.hasArg(options::OPT_Z_Xlinker__no_demangle))
231
0
    CmdArgs.push_back("--no-demangle");
232
6
233
6
  AddLinkerInputs(ToolChain, Inputs, Args, CmdArgs, JA);
234
6
235
6
  if (!Args.hasArg(options::OPT_nostdlib, options::OPT_nodefaultlibs)) {
236
5
    // For PS4, we always want to pass libm, libstdc++ and libkernel
237
5
    // libraries for both C and C++ compilations.
238
5
    CmdArgs.push_back("-lkernel");
239
5
    if (D.CCCIsCXX()) {
240
0
      if (ToolChain.ShouldLinkCXXStdlib(Args))
241
0
        ToolChain.AddCXXStdlibLibArgs(Args, CmdArgs);
242
0
      if (Args.hasArg(options::OPT_pg))
243
0
        CmdArgs.push_back("-lm_p");
244
0
      else
245
0
        CmdArgs.push_back("-lm");
246
0
    }
247
5
    // FIXME: For some reason GCC passes -lgcc and -lgcc_s before adding
248
5
    // the default system libraries. Just mimic this for now.
249
5
    if (Args.hasArg(options::OPT_pg))
250
0
      CmdArgs.push_back("-lgcc_p");
251
5
    else
252
5
      CmdArgs.push_back("-lcompiler_rt");
253
5
    if (Args.hasArg(options::OPT_static)) {
254
0
      CmdArgs.push_back("-lstdc++");
255
5
    } else if (Args.hasArg(options::OPT_pg)) {
256
0
      CmdArgs.push_back("-lgcc_eh_p");
257
5
    } else {
258
5
      CmdArgs.push_back("--as-needed");
259
5
      CmdArgs.push_back("-lstdc++");
260
5
      CmdArgs.push_back("--no-as-needed");
261
5
    }
262
5
263
5
    if (Args.hasArg(options::OPT_pthread)) {
264
0
      if (Args.hasArg(options::OPT_pg))
265
0
        CmdArgs.push_back("-lpthread_p");
266
0
      else
267
0
        CmdArgs.push_back("-lpthread");
268
0
    }
269
5
270
5
    if (Args.hasArg(options::OPT_pg)) {
271
0
      if (Args.hasArg(options::OPT_shared))
272
0
        CmdArgs.push_back("-lc");
273
0
      else {
274
0
        if (Args.hasArg(options::OPT_static)) {
275
0
          CmdArgs.push_back("--start-group");
276
0
          CmdArgs.push_back("-lc_p");
277
0
          CmdArgs.push_back("-lpthread_p");
278
0
          CmdArgs.push_back("--end-group");
279
0
        } else {
280
0
          CmdArgs.push_back("-lc_p");
281
0
        }
282
0
      }
283
0
      CmdArgs.push_back("-lgcc_p");
284
5
    } else {
285
5
      if (Args.hasArg(options::OPT_static)) {
286
0
        CmdArgs.push_back("--start-group");
287
0
        CmdArgs.push_back("-lc");
288
0
        CmdArgs.push_back("-lpthread");
289
0
        CmdArgs.push_back("--end-group");
290
5
      } else {
291
5
        CmdArgs.push_back("-lc");
292
5
      }
293
5
      CmdArgs.push_back("-lcompiler_rt");
294
5
    }
295
5
296
5
    if (Args.hasArg(options::OPT_static)) {
297
0
      CmdArgs.push_back("-lstdc++");
298
5
    } else if (Args.hasArg(options::OPT_pg)) {
299
0
      CmdArgs.push_back("-lgcc_eh_p");
300
5
    } else {
301
5
      CmdArgs.push_back("--as-needed");
302
5
      CmdArgs.push_back("-lstdc++");
303
5
      CmdArgs.push_back("--no-as-needed");
304
5
    }
305
5
  }
306
6
307
6
  if (!Args.hasArg(options::OPT_nostdlib, options::OPT_nostartfiles)) {
308
5
    if (Args.hasArg(options::OPT_shared) || 
Args.hasArg(options::OPT_pie)1
)
309
4
      CmdArgs.push_back(Args.MakeArgString(ToolChain.GetFilePath("crtendS.o")));
310
1
    else
311
1
      CmdArgs.push_back(Args.MakeArgString(ToolChain.GetFilePath("crtend.o")));
312
5
    CmdArgs.push_back(Args.MakeArgString(ToolChain.GetFilePath("crtn.o")));
313
5
  }
314
6
315
6
  const char *Exec =
316
#ifdef _WIN32
317
      Args.MakeArgString(ToolChain.GetProgramPath("orbis-ld.gold"));
318
#else
319
      Args.MakeArgString(ToolChain.GetProgramPath("orbis-ld"));
320
6
#endif
321
6
322
6
  C.addCommand(llvm::make_unique<Command>(JA, T, Exec, CmdArgs, Inputs));
323
6
}
324
325
void tools::PS4cpu::Link::ConstructJob(Compilation &C, const JobAction &JA,
326
                                       const InputInfo &Output,
327
                                       const InputInfoList &Inputs,
328
                                       const ArgList &Args,
329
44
                                       const char *LinkingOutput) const {
330
44
  const toolchains::FreeBSD &ToolChain =
331
44
      static_cast<const toolchains::FreeBSD &>(getToolChain());
332
44
  const Driver &D = ToolChain.getDriver();
333
44
  bool PS4Linker;
334
44
  StringRef LinkerOptName;
335
44
  if (const Arg *A = Args.getLastArg(options::OPT_fuse_ld_EQ)) {
336
7
    LinkerOptName = A->getValue();
337
7
    if (LinkerOptName != "ps4" && 
LinkerOptName != "gold"5
)
338
4
      D.Diag(diag::err_drv_unsupported_linker) << LinkerOptName;
339
7
  }
340
44
341
44
  if (LinkerOptName == "gold")
342
1
    PS4Linker = false;
343
43
  else if (LinkerOptName == "ps4")
344
2
    PS4Linker = true;
345
41
  else
346
41
    PS4Linker = !Args.hasArg(options::OPT_shared);
347
44
348
44
  if (PS4Linker)
349
38
    ConstructPS4LinkJob(*this, C, JA, Output, Inputs, Args, LinkingOutput);
350
6
  else
351
6
    ConstructGoldLinkJob(*this, C, JA, Output, Inputs, Args, LinkingOutput);
352
44
}
353
354
toolchains::PS4CPU::PS4CPU(const Driver &D, const llvm::Triple &Triple,
355
                           const ArgList &Args)
356
116
    : Generic_ELF(D, Triple, Args) {
357
116
  if (Args.hasArg(clang::driver::options::OPT_static))
358
1
    D.Diag(clang::diag::err_drv_unsupported_opt_for_target) << "-static"
359
1
                                                            << "PS4";
360
116
361
116
  // Determine where to find the PS4 libraries. We use SCE_ORBIS_SDK_DIR
362
116
  // if it exists; otherwise use the driver's installation path, which
363
116
  // should be <SDK_DIR>/host_tools/bin.
364
116
365
116
  SmallString<512> PS4SDKDir;
366
116
  if (const char *EnvValue = getenv("SCE_ORBIS_SDK_DIR")) {
367
22
    if (!llvm::sys::fs::exists(EnvValue))
368
0
      getDriver().Diag(clang::diag::warn_drv_ps4_sdk_dir) << EnvValue;
369
22
    PS4SDKDir = EnvValue;
370
94
  } else {
371
94
    PS4SDKDir = getDriver().Dir;
372
94
    llvm::sys::path::append(PS4SDKDir, "/../../");
373
94
  }
374
116
375
116
  // By default, the driver won't report a warning if it can't find
376
116
  // PS4's include or lib directories. This behavior could be changed if
377
116
  // -Weverything or -Winvalid-or-nonexistent-directory options are passed.
378
116
  // If -isysroot was passed, use that as the SDK base path.
379
116
  std::string PrefixDir;
380
116
  if (const Arg *A = Args.getLastArg(options::OPT_isysroot)) {
381
7
    PrefixDir = A->getValue();
382
7
    if (!llvm::sys::fs::exists(PrefixDir))
383
6
      getDriver().Diag(clang::diag::warn_missing_sysroot) << PrefixDir;
384
7
  } else
385
109
    PrefixDir = PS4SDKDir.str();
386
116
387
116
  SmallString<512> PS4SDKIncludeDir(PrefixDir);
388
116
  llvm::sys::path::append(PS4SDKIncludeDir, "target/include");
389
116
  if (!Args.hasArg(options::OPT_nostdinc) &&
390
116
      
!Args.hasArg(options::OPT_nostdlibinc)112
&&
391
116
      
!Args.hasArg(options::OPT_isysroot)112
&&
392
116
      
!Args.hasArg(options::OPT__sysroot_EQ)105
&&
393
116
      
!llvm::sys::fs::exists(PS4SDKIncludeDir)101
) {
394
100
    getDriver().Diag(clang::diag::warn_drv_unable_to_find_directory_expected)
395
100
        << "PS4 system headers" << PS4SDKIncludeDir;
396
100
  }
397
116
398
116
  SmallString<512> PS4SDKLibDir(PS4SDKDir);
399
116
  llvm::sys::path::append(PS4SDKLibDir, "target/lib");
400
116
  if (!Args.hasArg(options::OPT_nostdlib) &&
401
116
      
!Args.hasArg(options::OPT_nodefaultlibs)112
&&
402
116
      
!Args.hasArg(options::OPT__sysroot_EQ)110
&&
!Args.hasArg(options::OPT_E)105
&&
403
116
      
!Args.hasArg(options::OPT_c)100
&&
!Args.hasArg(options::OPT_S)52
&&
404
116
      
!Args.hasArg(options::OPT_emit_ast)45
&&
405
116
      
!llvm::sys::fs::exists(PS4SDKLibDir)42
) {
406
42
    getDriver().Diag(clang::diag::warn_drv_unable_to_find_directory_expected)
407
42
        << "PS4 system libraries" << PS4SDKLibDir;
408
42
    return;
409
42
  }
410
74
  getFilePaths().push_back(PS4SDKLibDir.str());
411
74
}
412
413
0
Tool *toolchains::PS4CPU::buildAssembler() const {
414
0
  return new tools::PS4cpu::Assemble(*this);
415
0
}
416
417
44
Tool *toolchains::PS4CPU::buildLinker() const {
418
44
  return new tools::PS4cpu::Link(*this);
419
44
}
420
421
229
bool toolchains::PS4CPU::isPICDefault() const { return true; }
422
423
44
bool toolchains::PS4CPU::HasNativeLLVMSupport() const { return true; }
424
425
116
SanitizerMask toolchains::PS4CPU::getSupportedSanitizers() const {
426
116
  SanitizerMask Res = ToolChain::getSupportedSanitizers();
427
116
  Res |= SanitizerKind::Address;
428
116
  Res |= SanitizerKind::PointerCompare;
429
116
  Res |= SanitizerKind::PointerSubtract;
430
116
  Res |= SanitizerKind::Vptr;
431
116
  return Res;
432
116
}