PS4CPU.cpp 11 KB

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