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