| 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 "Arch/X86.h" |
| 11 | #include "clang/Config/config.h" |
| 12 | #include "clang/Driver/CommonArgs.h" |
| 13 | #include "clang/Driver/Compilation.h" |
| 14 | #include "clang/Driver/Driver.h" |
| 15 | #include "clang/Driver/SanitizerArgs.h" |
| 16 | #include "clang/Options/Options.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 | // Helper to paste bits of an option together and return a saved string. |
| 27 | static const char *makeArgString(const ArgList &Args, const char *Prefix, |
| 28 | const char *Base, const char *Suffix) { |
| 29 | // Basically "Prefix + Base + Suffix" all converted to Twine then saved. |
| 30 | return Args.MakeArgString(Str: Twine(StringRef(Prefix), Base) + Suffix); |
| 31 | } |
| 32 | |
| 33 | void tools::PScpu::addProfileRTArgs(const ToolChain &TC, const ArgList &Args, |
| 34 | ArgStringList &CmdArgs) { |
| 35 | assert(TC.getTriple().isPS()); |
| 36 | auto &PSTC = static_cast<const toolchains::PS4PS5Base &>(TC); |
| 37 | |
| 38 | if ((Args.hasFlag(Pos: options::OPT_fprofile_arcs, Neg: options::OPT_fno_profile_arcs, |
| 39 | Default: false) || |
| 40 | Args.hasFlag(Pos: options::OPT_fprofile_generate, |
| 41 | Neg: options::OPT_fno_profile_generate, Default: false) || |
| 42 | Args.hasFlag(Pos: options::OPT_fprofile_generate_EQ, |
| 43 | Neg: options::OPT_fno_profile_generate, Default: false) || |
| 44 | Args.hasFlag(Pos: options::OPT_fprofile_instr_generate, |
| 45 | Neg: options::OPT_fno_profile_instr_generate, Default: false) || |
| 46 | Args.hasFlag(Pos: options::OPT_fprofile_instr_generate_EQ, |
| 47 | Neg: options::OPT_fno_profile_instr_generate, Default: false) || |
| 48 | Args.hasFlag(Pos: options::OPT_fcs_profile_generate, |
| 49 | Neg: options::OPT_fno_profile_generate, Default: false) || |
| 50 | Args.hasFlag(Pos: options::OPT_fcs_profile_generate_EQ, |
| 51 | Neg: options::OPT_fno_profile_generate, Default: false) || |
| 52 | Args.hasArg(Ids: options::OPT_fcreate_profile) || |
| 53 | Args.hasArg(Ids: options::OPT_coverage))) |
| 54 | CmdArgs.push_back(Elt: makeArgString( |
| 55 | Args, Prefix: "--dependent-lib=" , Base: PSTC.getProfileRTLibName(), Suffix: "" )); |
| 56 | } |
| 57 | |
| 58 | void tools::PScpu::Assembler::ConstructJob(Compilation &C, const JobAction &JA, |
| 59 | const InputInfo &Output, |
| 60 | const InputInfoList &Inputs, |
| 61 | const ArgList &Args, |
| 62 | const char *LinkingOutput) const { |
| 63 | auto &TC = static_cast<const toolchains::PS4PS5Base &>(getToolChain()); |
| 64 | claimNoWarnArgs(Args); |
| 65 | ArgStringList CmdArgs; |
| 66 | |
| 67 | Args.AddAllArgValues(Output&: CmdArgs, Id0: options::OPT_Wa_COMMA, Id1: options::OPT_Xassembler); |
| 68 | |
| 69 | CmdArgs.push_back(Elt: "-o" ); |
| 70 | CmdArgs.push_back(Elt: Output.getFilename()); |
| 71 | |
| 72 | assert(Inputs.size() == 1 && "Unexpected number of inputs." ); |
| 73 | const InputInfo &Input = Inputs[0]; |
| 74 | assert(Input.isFilename() && "Invalid input." ); |
| 75 | CmdArgs.push_back(Elt: Input.getFilename()); |
| 76 | |
| 77 | std::string AsName = TC.qualifyPSCmdName(CmdName: "as" ); |
| 78 | const char *Exec = Args.MakeArgString(Str: TC.GetProgramPath(Name: AsName.c_str())); |
| 79 | C.addCommand(Cmd: std::make_unique<Command>(args: JA, args: *this, |
| 80 | args: ResponseFileSupport::AtFileUTF8(), |
| 81 | args&: Exec, args&: CmdArgs, args: Inputs, args: Output)); |
| 82 | } |
| 83 | |
| 84 | void tools::PScpu::addSanitizerArgs(const ToolChain &TC, const ArgList &Args, |
| 85 | ArgStringList &CmdArgs) { |
| 86 | assert(TC.getTriple().isPS()); |
| 87 | auto &PSTC = static_cast<const toolchains::PS4PS5Base &>(TC); |
| 88 | PSTC.addSanitizerArgs(Args, CmdArgs, Prefix: "--dependent-lib=lib" , Suffix: ".a" ); |
| 89 | } |
| 90 | |
| 91 | void toolchains::PS4CPU::addSanitizerArgs(const ArgList &Args, |
| 92 | ArgStringList &CmdArgs, |
| 93 | const char *Prefix, |
| 94 | const char *Suffix) const { |
| 95 | auto arg = [&](const char *Name) -> const char * { |
| 96 | return makeArgString(Args, Prefix, Base: Name, Suffix); |
| 97 | }; |
| 98 | const SanitizerArgs &SanArgs = getSanitizerArgs(JobArgs: Args); |
| 99 | if (SanArgs.needsUbsanRt()) |
| 100 | CmdArgs.push_back(Elt: arg("SceDbgUBSanitizer_stub_weak" )); |
| 101 | if (SanArgs.needsAsanRt()) |
| 102 | CmdArgs.push_back(Elt: arg("SceDbgAddressSanitizer_stub_weak" )); |
| 103 | } |
| 104 | |
| 105 | void toolchains::PS5CPU::addSanitizerArgs(const ArgList &Args, |
| 106 | ArgStringList &CmdArgs, |
| 107 | const char *Prefix, |
| 108 | const char *Suffix) const { |
| 109 | auto arg = [&](const char *Name) -> const char * { |
| 110 | return makeArgString(Args, Prefix, Base: Name, Suffix); |
| 111 | }; |
| 112 | const SanitizerArgs &SanArgs = getSanitizerArgs(JobArgs: Args); |
| 113 | if (SanArgs.needsUbsanRt()) |
| 114 | CmdArgs.push_back(Elt: arg("SceUBSanitizer_nosubmission_stub_weak" )); |
| 115 | if (SanArgs.needsAsanRt()) |
| 116 | CmdArgs.push_back(Elt: arg("SceAddressSanitizer_nosubmission_stub_weak" )); |
| 117 | if (SanArgs.needsRtsanRt()) |
| 118 | CmdArgs.push_back(Elt: arg("SceRealtimeSanitizer_nosubmission_stub_weak" )); |
| 119 | if (SanArgs.needsTsanRt()) |
| 120 | CmdArgs.push_back(Elt: arg("SceThreadSanitizer_nosubmission_stub_weak" )); |
| 121 | if (SanArgs.needsTysanRt()) |
| 122 | CmdArgs.push_back(Elt: arg("SceTypeSanitizer_nosubmission_stub_weak" )); |
| 123 | } |
| 124 | |
| 125 | void tools::PS4cpu::Linker::ConstructJob(Compilation &C, const JobAction &JA, |
| 126 | const InputInfo &Output, |
| 127 | const InputInfoList &Inputs, |
| 128 | const ArgList &Args, |
| 129 | const char *LinkingOutput) const { |
| 130 | auto &TC = static_cast<const toolchains::PS4PS5Base &>(getToolChain()); |
| 131 | const Driver &D = TC.getDriver(); |
| 132 | ArgStringList CmdArgs; |
| 133 | |
| 134 | // Silence warning for "clang -g foo.o -o foo" |
| 135 | Args.ClaimAllArgs(Id0: options::OPT_g_Group); |
| 136 | // and "clang -emit-llvm foo.o -o foo" |
| 137 | Args.ClaimAllArgs(Id0: options::OPT_emit_llvm); |
| 138 | // and for "clang -w foo.o -o foo". Other warning options are already |
| 139 | // handled somewhere else. |
| 140 | Args.ClaimAllArgs(Id0: options::OPT_w); |
| 141 | |
| 142 | CmdArgs.push_back( |
| 143 | Elt: Args.MakeArgString(Str: "--sysroot=" + TC.getSDKLibraryRootDir())); |
| 144 | |
| 145 | if (Args.hasArg(Ids: options::OPT_pie)) |
| 146 | CmdArgs.push_back(Elt: "-pie" ); |
| 147 | |
| 148 | if (Args.hasArg(Ids: options::OPT_static)) |
| 149 | CmdArgs.push_back(Elt: "-static" ); |
| 150 | if (Args.hasArg(Ids: options::OPT_rdynamic)) |
| 151 | CmdArgs.push_back(Elt: "-export-dynamic" ); |
| 152 | if (Args.hasArg(Ids: options::OPT_shared)) |
| 153 | CmdArgs.push_back(Elt: "--shared" ); |
| 154 | |
| 155 | assert((Output.isFilename() || Output.isNothing()) && "Invalid output." ); |
| 156 | if (Output.isFilename()) { |
| 157 | CmdArgs.push_back(Elt: "-o" ); |
| 158 | CmdArgs.push_back(Elt: Output.getFilename()); |
| 159 | } |
| 160 | |
| 161 | const bool UseJMC = |
| 162 | Args.hasFlag(Pos: options::OPT_fjmc, Neg: options::OPT_fno_jmc, Default: false); |
| 163 | |
| 164 | const char *LTOArgs = "" ; |
| 165 | auto AddLTOFlag = [&](Twine Flag) { |
| 166 | LTOArgs = Args.MakeArgString(Str: Twine(LTOArgs) + " " + Flag); |
| 167 | }; |
| 168 | |
| 169 | // If the linker sees bitcode objects it will perform LTO. We can't tell |
| 170 | // whether or not that will be the case at this point. So, unconditionally |
| 171 | // pass LTO options to ensure proper codegen, metadata production, etc if |
| 172 | // LTO indeed occurs. |
| 173 | if (Args.hasFlag(Pos: options::OPT_funified_lto, Neg: options::OPT_fno_unified_lto, |
| 174 | Default: true)) |
| 175 | CmdArgs.push_back(Elt: TC.getLTOMode(Args) == LTOK_Thin ? "--lto=thin" |
| 176 | : "--lto=full" ); |
| 177 | if (UseJMC) |
| 178 | AddLTOFlag("-enable-jmc-instrument" ); |
| 179 | |
| 180 | if (Arg *A = Args.getLastArg(Ids: options::OPT_fcrash_diagnostics_dir)) |
| 181 | AddLTOFlag(Twine("-crash-diagnostics-dir=" ) + A->getValue()); |
| 182 | |
| 183 | if (StringRef Threads = getLTOParallelism(Args, D); !Threads.empty()) |
| 184 | AddLTOFlag(Twine("-threads=" ) + Threads); |
| 185 | |
| 186 | if (*LTOArgs) |
| 187 | CmdArgs.push_back( |
| 188 | Elt: Args.MakeArgString(Str: Twine("-lto-debug-options=" ) + LTOArgs)); |
| 189 | |
| 190 | // Sanitizer runtimes must be supplied before all other objects and libs. |
| 191 | if (!Args.hasArg(Ids: options::OPT_nostdlib, Ids: options::OPT_nodefaultlibs)) |
| 192 | TC.addSanitizerArgs(Args, CmdArgs, Prefix: "-l" , Suffix: "" ); |
| 193 | |
| 194 | // Other drivers typically add library search paths (`-L`) here via |
| 195 | // TC.AddFilePathLibArgs(). We don't do that on PS4 as the PS4 linker |
| 196 | // searches those locations by default. |
| 197 | Args.addAllArgs(Output&: CmdArgs, Ids: {options::OPT_L, options::OPT_T_Group, |
| 198 | options::OPT_s, options::OPT_t}); |
| 199 | |
| 200 | if (Args.hasArg(Ids: options::OPT_Z_Xlinker__no_demangle)) |
| 201 | CmdArgs.push_back(Elt: "--no-demangle" ); |
| 202 | |
| 203 | AddLinkerInputs(TC, Inputs, Args, CmdArgs, JA); |
| 204 | |
| 205 | if (Args.hasArg(Ids: options::OPT_pthread)) { |
| 206 | CmdArgs.push_back(Elt: "-lpthread" ); |
| 207 | } |
| 208 | |
| 209 | if (UseJMC) { |
| 210 | CmdArgs.push_back(Elt: "--whole-archive" ); |
| 211 | CmdArgs.push_back(Elt: "-lSceDbgJmc" ); |
| 212 | CmdArgs.push_back(Elt: "--no-whole-archive" ); |
| 213 | } |
| 214 | |
| 215 | if (Args.hasArg(Ids: options::OPT_fuse_ld_EQ)) { |
| 216 | D.Diag(DiagID: diag::err_drv_unsupported_opt_for_target) |
| 217 | << "-fuse-ld" << TC.getTriple().str(); |
| 218 | } |
| 219 | |
| 220 | std::string LdName = TC.qualifyPSCmdName(CmdName: TC.getLinkerBaseName()); |
| 221 | const char *Exec = Args.MakeArgString(Str: TC.GetProgramPath(Name: LdName.c_str())); |
| 222 | |
| 223 | C.addCommand(Cmd: std::make_unique<Command>(args: JA, args: *this, |
| 224 | args: ResponseFileSupport::AtFileUTF8(), |
| 225 | args&: Exec, args&: CmdArgs, args: Inputs, args: Output)); |
| 226 | } |
| 227 | |
| 228 | void tools::PS5cpu::Linker::ConstructJob(Compilation &C, const JobAction &JA, |
| 229 | const InputInfo &Output, |
| 230 | const InputInfoList &Inputs, |
| 231 | const ArgList &Args, |
| 232 | const char *LinkingOutput) const { |
| 233 | auto &TC = static_cast<const toolchains::PS4PS5Base &>(getToolChain()); |
| 234 | const Driver &D = TC.getDriver(); |
| 235 | ArgStringList CmdArgs; |
| 236 | |
| 237 | const bool Relocatable = Args.hasArg(Ids: options::OPT_r); |
| 238 | const bool Shared = Args.hasArg(Ids: options::OPT_shared); |
| 239 | const bool Static = Args.hasArg(Ids: options::OPT_static); |
| 240 | |
| 241 | // Silence warning for "clang -g foo.o -o foo" |
| 242 | Args.ClaimAllArgs(Id0: options::OPT_g_Group); |
| 243 | // and "clang -emit-llvm foo.o -o foo" |
| 244 | Args.ClaimAllArgs(Id0: options::OPT_emit_llvm); |
| 245 | // and for "clang -w foo.o -o foo". Other warning options are already |
| 246 | // handled somewhere else. |
| 247 | Args.ClaimAllArgs(Id0: options::OPT_w); |
| 248 | |
| 249 | CmdArgs.push_back(Elt: "-m" ); |
| 250 | CmdArgs.push_back(Elt: "elf_x86_64_fbsd" ); |
| 251 | |
| 252 | CmdArgs.push_back( |
| 253 | Elt: Args.MakeArgString(Str: "--sysroot=" + TC.getSDKLibraryRootDir())); |
| 254 | |
| 255 | // Default to PIE for non-static executables. |
| 256 | const bool PIE = Args.hasFlag(Pos: options::OPT_pie, Neg: options::OPT_no_pie, |
| 257 | Default: !Relocatable && !Shared && !Static); |
| 258 | if (PIE) |
| 259 | CmdArgs.push_back(Elt: "-pie" ); |
| 260 | |
| 261 | if (!Relocatable) { |
| 262 | CmdArgs.push_back(Elt: "--eh-frame-hdr" ); |
| 263 | CmdArgs.push_back(Elt: "--hash-style=sysv" ); |
| 264 | |
| 265 | // Add a build-id by default to allow the PlayStation symbol server to |
| 266 | // index the symbols. `uuid` is the cheapest fool-proof method. |
| 267 | // (The non-determinism and alternative methods are noted in the downstream |
| 268 | // PlayStation docs). |
| 269 | // Static executables are only used for a handful of specialized components, |
| 270 | // where the extra section is not wanted. |
| 271 | if (!Static) |
| 272 | CmdArgs.push_back(Elt: "--build-id=uuid" ); |
| 273 | |
| 274 | // All references are expected to be resolved at static link time for both |
| 275 | // executables and dynamic libraries. This has been the default linking |
| 276 | // behaviour for numerous PlayStation generations. |
| 277 | CmdArgs.push_back(Elt: "--unresolved-symbols=report-all" ); |
| 278 | |
| 279 | // Lazy binding of PLTs is not supported on PlayStation. They are placed in |
| 280 | // the RelRo segment. |
| 281 | CmdArgs.push_back(Elt: "-z" ); |
| 282 | CmdArgs.push_back(Elt: "now" ); |
| 283 | |
| 284 | // Don't export linker-generated __start/stop... section bookends. |
| 285 | CmdArgs.push_back(Elt: "-z" ); |
| 286 | CmdArgs.push_back(Elt: "start-stop-visibility=hidden" ); |
| 287 | |
| 288 | // DT_DEBUG is not supported on PlayStation. |
| 289 | CmdArgs.push_back(Elt: "-z" ); |
| 290 | CmdArgs.push_back(Elt: "rodynamic" ); |
| 291 | |
| 292 | CmdArgs.push_back(Elt: "-z" ); |
| 293 | CmdArgs.push_back(Elt: "common-page-size=0x4000" ); |
| 294 | |
| 295 | CmdArgs.push_back(Elt: "-z" ); |
| 296 | CmdArgs.push_back(Elt: "max-page-size=0x4000" ); |
| 297 | |
| 298 | // Patch relocated regions of DWARF whose targets are eliminated at link |
| 299 | // time with specific tombstones, such that they're recognisable by the |
| 300 | // PlayStation debugger. |
| 301 | CmdArgs.push_back(Elt: "-z" ); |
| 302 | CmdArgs.push_back(Elt: "dead-reloc-in-nonalloc=.debug_*=0xffffffffffffffff" ); |
| 303 | CmdArgs.push_back(Elt: "-z" ); |
| 304 | CmdArgs.push_back( |
| 305 | Elt: "dead-reloc-in-nonalloc=.debug_ranges=0xfffffffffffffffe" ); |
| 306 | CmdArgs.push_back(Elt: "-z" ); |
| 307 | CmdArgs.push_back(Elt: "dead-reloc-in-nonalloc=.debug_loc=0xfffffffffffffffe" ); |
| 308 | |
| 309 | // The PlayStation loader expects linked objects to be laid out in a |
| 310 | // particular way. This is achieved by linker scripts that are supplied |
| 311 | // with the SDK. The scripts are inside <sdkroot>/target/lib, which is |
| 312 | // added as a search path elsewhere. |
| 313 | // "PRX" has long stood for "PlayStation Relocatable eXecutable". |
| 314 | if (!Args.hasArgNoClaim(Ids: options::OPT_T)) { |
| 315 | CmdArgs.push_back(Elt: "--default-script" ); |
| 316 | CmdArgs.push_back(Elt: Static ? "static.script" |
| 317 | : Shared ? "prx.script" |
| 318 | : "main.script" ); |
| 319 | } |
| 320 | } |
| 321 | |
| 322 | if (Static) |
| 323 | CmdArgs.push_back(Elt: "-static" ); |
| 324 | if (Args.hasArg(Ids: options::OPT_rdynamic)) |
| 325 | CmdArgs.push_back(Elt: "-export-dynamic" ); |
| 326 | if (Shared) |
| 327 | CmdArgs.push_back(Elt: "--shared" ); |
| 328 | |
| 329 | // Provide a base address for non-PIE executables. This includes cases where |
| 330 | // -static is supplied without -pie. |
| 331 | if (!Relocatable && !Shared && !PIE) |
| 332 | CmdArgs.push_back(Elt: "--image-base=0x400000" ); |
| 333 | |
| 334 | assert((Output.isFilename() || Output.isNothing()) && "Invalid output." ); |
| 335 | if (Output.isFilename()) { |
| 336 | CmdArgs.push_back(Elt: "-o" ); |
| 337 | CmdArgs.push_back(Elt: Output.getFilename()); |
| 338 | } |
| 339 | |
| 340 | const bool UseJMC = |
| 341 | Args.hasFlag(Pos: options::OPT_fjmc, Neg: options::OPT_fno_jmc, Default: false); |
| 342 | |
| 343 | auto AddLTOFlag = [&](Twine Flag) { |
| 344 | CmdArgs.push_back(Elt: Args.MakeArgString(Str: Twine("-plugin-opt=" ) + Flag)); |
| 345 | }; |
| 346 | |
| 347 | // If the linker sees bitcode objects it will perform LTO. We can't tell |
| 348 | // whether or not that will be the case at this point. So, unconditionally |
| 349 | // pass LTO options to ensure proper codegen, metadata production, etc if |
| 350 | // LTO indeed occurs. |
| 351 | |
| 352 | tools::addDTLTOOptions(ToolChain: TC, Args, CmdArgs); |
| 353 | |
| 354 | if (Args.hasFlag(Pos: options::OPT_funified_lto, Neg: options::OPT_fno_unified_lto, |
| 355 | Default: true)) |
| 356 | CmdArgs.push_back(Elt: TC.getLTOMode(Args) == LTOK_Thin ? "--lto=thin" |
| 357 | : "--lto=full" ); |
| 358 | |
| 359 | if (Args.hasFlag(Pos: options::OPT_ffat_lto_objects, |
| 360 | Neg: options::OPT_fno_fat_lto_objects, Default: false)) |
| 361 | CmdArgs.push_back(Elt: "--fat-lto-objects" ); |
| 362 | |
| 363 | AddLTOFlag("-emit-jump-table-sizes-section" ); |
| 364 | |
| 365 | if (UseJMC) |
| 366 | AddLTOFlag("-enable-jmc-instrument" ); |
| 367 | |
| 368 | if (Args.hasFlag(Pos: options::OPT_fstack_size_section, |
| 369 | Neg: options::OPT_fno_stack_size_section, Default: false)) |
| 370 | AddLTOFlag("-stack-size-section" ); |
| 371 | |
| 372 | if (Arg *A = Args.getLastArg(Ids: options::OPT_fcrash_diagnostics_dir)) |
| 373 | AddLTOFlag(Twine("-crash-diagnostics-dir=" ) + A->getValue()); |
| 374 | |
| 375 | if (StringRef Jobs = getLTOParallelism(Args, D); !Jobs.empty()) |
| 376 | AddLTOFlag(Twine("jobs=" ) + Jobs); |
| 377 | |
| 378 | std::string CPU = tools::x86::getX86TargetCPU(D, Args, Triple: TC.getTriple()); |
| 379 | AddLTOFlag(Twine("mcpu=" + CPU)); |
| 380 | |
| 381 | Args.AddAllArgs(Output&: CmdArgs, Id0: options::OPT_L); |
| 382 | TC.AddFilePathLibArgs(Args, CmdArgs); |
| 383 | Args.addAllArgs(Output&: CmdArgs, |
| 384 | Ids: {options::OPT_T_Group, options::OPT_s, options::OPT_t}); |
| 385 | |
| 386 | if (Args.hasArg(Ids: options::OPT_Z_Xlinker__no_demangle)) |
| 387 | CmdArgs.push_back(Elt: "--no-demangle" ); |
| 388 | |
| 389 | // Sanitizer runtimes must be supplied before all other objects and libs. |
| 390 | if (!Args.hasArg(Ids: options::OPT_nostdlib, Ids: options::OPT_nodefaultlibs)) |
| 391 | TC.addSanitizerArgs(Args, CmdArgs, Prefix: "-l" , Suffix: "" ); |
| 392 | |
| 393 | const bool AddStartFiles = |
| 394 | !Relocatable && |
| 395 | !Args.hasArg(Ids: options::OPT_nostartfiles, Ids: options::OPT_nostdlib); |
| 396 | |
| 397 | auto AddCRTObject = [&](StringRef Name) { |
| 398 | // CRT objects can be found on user supplied library paths. This is |
| 399 | // an entrenched expectation on PlayStation. |
| 400 | CmdArgs.push_back(Elt: Args.MakeArgString(Str: "-l:" + Name)); |
| 401 | }; |
| 402 | |
| 403 | if (AddStartFiles) { |
| 404 | if (!Shared) |
| 405 | AddCRTObject("crt1.o" ); |
| 406 | AddCRTObject("crti.o" ); |
| 407 | AddCRTObject(Shared ? "crtbeginS.o" |
| 408 | : Static ? "crtbeginT.o" |
| 409 | : "crtbegin.o" ); |
| 410 | } |
| 411 | |
| 412 | AddLinkerInputs(TC, Inputs, Args, CmdArgs, JA); |
| 413 | |
| 414 | if (!Relocatable && |
| 415 | !Args.hasArg(Ids: options::OPT_nodefaultlibs, Ids: options::OPT_nostdlib)) { |
| 416 | |
| 417 | if (UseJMC) { |
| 418 | CmdArgs.push_back(Elt: "--push-state" ); |
| 419 | CmdArgs.push_back(Elt: "--whole-archive" ); |
| 420 | CmdArgs.push_back(Elt: "-lSceJmc_nosubmission" ); |
| 421 | CmdArgs.push_back(Elt: "--pop-state" ); |
| 422 | } |
| 423 | |
| 424 | if (Args.hasArg(Ids: options::OPT_pthread)) |
| 425 | CmdArgs.push_back(Elt: "-lpthread" ); |
| 426 | |
| 427 | if (Static) { |
| 428 | if (!Args.hasArg(Ids: options::OPT_nostdlibxx)) |
| 429 | CmdArgs.push_back(Elt: "-lstdc++" ); |
| 430 | if (!Args.hasArg(Ids: options::OPT_nolibc)) { |
| 431 | CmdArgs.push_back(Elt: "-lm" ); |
| 432 | CmdArgs.push_back(Elt: "-lc" ); |
| 433 | } |
| 434 | |
| 435 | CmdArgs.push_back(Elt: "-lcompiler_rt" ); |
| 436 | CmdArgs.push_back(Elt: "-lkernel" ); |
| 437 | } else { |
| 438 | // The C and C++ libraries are combined. |
| 439 | if (!Args.hasArg(Ids: options::OPT_nolibc, Ids: options::OPT_nostdlibxx)) |
| 440 | CmdArgs.push_back(Elt: "-lc_stub_weak" ); |
| 441 | |
| 442 | CmdArgs.push_back(Elt: "-lkernel_stub_weak" ); |
| 443 | } |
| 444 | } |
| 445 | if (AddStartFiles) { |
| 446 | AddCRTObject(Shared ? "crtendS.o" : "crtend.o" ); |
| 447 | AddCRTObject("crtn.o" ); |
| 448 | } |
| 449 | |
| 450 | if (Args.hasArg(Ids: options::OPT_fuse_ld_EQ)) { |
| 451 | D.Diag(DiagID: diag::err_drv_unsupported_opt_for_target) |
| 452 | << "-fuse-ld" << TC.getTriple().str(); |
| 453 | } |
| 454 | |
| 455 | std::string LdName = TC.qualifyPSCmdName(CmdName: TC.getLinkerBaseName()); |
| 456 | const char *Exec = Args.MakeArgString(Str: TC.GetProgramPath(Name: LdName.c_str())); |
| 457 | |
| 458 | C.addCommand(Cmd: std::make_unique<Command>(args: JA, args: *this, |
| 459 | args: ResponseFileSupport::AtFileUTF8(), |
| 460 | args&: Exec, args&: CmdArgs, args: Inputs, args: Output)); |
| 461 | } |
| 462 | |
| 463 | toolchains::PS4PS5Base::PS4PS5Base(const Driver &D, const llvm::Triple &Triple, |
| 464 | const ArgList &Args, StringRef Platform, |
| 465 | const char *EnvVar) |
| 466 | : Generic_ELF(D, Triple, Args) { |
| 467 | // Determine the baseline SDK directory from the environment, else |
| 468 | // the driver's location, which should be <SDK_DIR>/host_tools/bin. |
| 469 | SmallString<128> SDKRootDir; |
| 470 | SmallString<80> Whence; |
| 471 | if (const char *EnvValue = getenv(name: EnvVar)) { |
| 472 | SDKRootDir = EnvValue; |
| 473 | Whence = {"environment variable '" , EnvVar, "'" }; |
| 474 | } else { |
| 475 | SDKRootDir = D.Dir + "/../../" ; |
| 476 | Whence = "compiler's location" ; |
| 477 | } |
| 478 | |
| 479 | // Allow --sysroot= to override the root directory for header and library |
| 480 | // search, and -isysroot to override header search. If both are specified, |
| 481 | // -isysroot overrides --sysroot for header search. |
| 482 | auto OverrideRoot = [&](const options::ID &Opt, std::string &Root, |
| 483 | StringRef Default) { |
| 484 | if (const Arg *A = Args.getLastArg(Ids: Opt)) { |
| 485 | Root = A->getValue(); |
| 486 | if (!llvm::sys::fs::exists(Path: Root)) |
| 487 | D.Diag(DiagID: clang::diag::warn_missing_sysroot) << Root; |
| 488 | return true; |
| 489 | } |
| 490 | Root = Default.str(); |
| 491 | return false; |
| 492 | }; |
| 493 | |
| 494 | bool CustomSysroot = |
| 495 | OverrideRoot(options::OPT__sysroot_EQ, SDKLibraryRootDir, SDKRootDir); |
| 496 | bool CustomISysroot = |
| 497 | OverrideRoot(options::OPT_isysroot, SDKHeaderRootDir, SDKLibraryRootDir); |
| 498 | |
| 499 | // Emit warnings if parts of the SDK are missing, unless the user has taken |
| 500 | // control of header or library search. If we're not linking, don't check |
| 501 | // for missing libraries. |
| 502 | auto CheckSDKPartExists = [&](StringRef Dir, StringRef Desc) { |
| 503 | // In ThinLTO code generation mode SDK files are not required. |
| 504 | if (Args.hasArgNoClaim(Ids: options::OPT_fthinlto_index_EQ)) |
| 505 | return true; |
| 506 | if (llvm::sys::fs::exists(Path: Dir)) |
| 507 | return true; |
| 508 | D.Diag(DiagID: clang::diag::warn_drv_unable_to_find_directory_expected) |
| 509 | << (Twine(Platform) + " " + Desc).str() << Dir << Whence; |
| 510 | return false; |
| 511 | }; |
| 512 | |
| 513 | bool Linking = !Args.hasArg(Ids: options::OPT_E, Ids: options::OPT_c, Ids: options::OPT_S, |
| 514 | Ids: options::OPT_emit_ast); |
| 515 | if (Linking) { |
| 516 | SmallString<128> Dir(SDKLibraryRootDir); |
| 517 | llvm::sys::path::append(path&: Dir, a: "target/lib" ); |
| 518 | if (CheckSDKPartExists(Dir, "system libraries" )) |
| 519 | getFilePaths().push_back(Elt: std::string(Dir)); |
| 520 | } |
| 521 | if (!CustomSysroot && !CustomISysroot && |
| 522 | !Args.hasArg(Ids: options::OPT_nostdinc, Ids: options::OPT_nostdlibinc)) { |
| 523 | SmallString<128> Dir(SDKHeaderRootDir); |
| 524 | llvm::sys::path::append(path&: Dir, a: "target/include" ); |
| 525 | CheckSDKPartExists(Dir, "system headers" ); |
| 526 | } |
| 527 | |
| 528 | getFilePaths().push_back(Elt: "." ); |
| 529 | } |
| 530 | |
| 531 | void toolchains::PS4PS5Base::AddClangSystemIncludeArgs( |
| 532 | const ArgList &DriverArgs, ArgStringList &CC1Args) const { |
| 533 | const Driver &D = getDriver(); |
| 534 | |
| 535 | if (DriverArgs.hasArg(Ids: options::OPT_nostdinc)) |
| 536 | return; |
| 537 | |
| 538 | if (!DriverArgs.hasArg(Ids: options::OPT_nobuiltininc)) { |
| 539 | SmallString<128> Dir(D.ResourceDir); |
| 540 | llvm::sys::path::append(path&: Dir, a: "include" ); |
| 541 | addSystemInclude(DriverArgs, CC1Args, Path: Dir.str()); |
| 542 | } |
| 543 | |
| 544 | if (DriverArgs.hasArg(Ids: options::OPT_nostdlibinc)) |
| 545 | return; |
| 546 | |
| 547 | addExternCSystemInclude(DriverArgs, CC1Args, |
| 548 | Path: SDKHeaderRootDir + "/target/include" ); |
| 549 | addExternCSystemInclude(DriverArgs, CC1Args, |
| 550 | Path: SDKHeaderRootDir + "/target/include_common" ); |
| 551 | } |
| 552 | |
| 553 | Tool *toolchains::PS4CPU::buildAssembler() const { |
| 554 | return new tools::PScpu::Assembler(*this); |
| 555 | } |
| 556 | |
| 557 | Tool *toolchains::PS4CPU::buildLinker() const { |
| 558 | return new tools::PS4cpu::Linker(*this); |
| 559 | } |
| 560 | |
| 561 | Tool *toolchains::PS5CPU::buildAssembler() const { |
| 562 | // PS5 does not support an external assembler. |
| 563 | getDriver().Diag(DiagID: clang::diag::err_no_external_assembler); |
| 564 | return nullptr; |
| 565 | } |
| 566 | |
| 567 | Tool *toolchains::PS5CPU::buildLinker() const { |
| 568 | return new tools::PS5cpu::Linker(*this); |
| 569 | } |
| 570 | |
| 571 | SanitizerMask toolchains::PS4PS5Base::getSupportedSanitizers( |
| 572 | BoundArch BA, Action::OffloadKind DeviceOffloadKind) const { |
| 573 | SanitizerMask Res = ToolChain::getSupportedSanitizers(BA, DeviceOffloadKind); |
| 574 | Res |= SanitizerKind::Address; |
| 575 | Res |= SanitizerKind::PointerCompare; |
| 576 | Res |= SanitizerKind::PointerSubtract; |
| 577 | Res |= SanitizerKind::Vptr; |
| 578 | return Res; |
| 579 | } |
| 580 | |
| 581 | SanitizerMask toolchains::PS5CPU::getSupportedSanitizers( |
| 582 | BoundArch BA, Action::OffloadKind DeviceOffloadKind) const { |
| 583 | SanitizerMask Res = PS4PS5Base::getSupportedSanitizers(BA, DeviceOffloadKind); |
| 584 | Res |= SanitizerKind::Realtime; |
| 585 | Res |= SanitizerKind::Thread; |
| 586 | Res |= SanitizerKind::Type; |
| 587 | return Res; |
| 588 | } |
| 589 | |
| 590 | void toolchains::PS4PS5Base::addClangTargetOptions( |
| 591 | const ArgList &DriverArgs, ArgStringList &CC1Args, BoundArch BA, |
| 592 | Action::OffloadKind DeviceOffloadingKind) const { |
| 593 | // PS4/PS5 do not use init arrays. |
| 594 | if (DriverArgs.hasArg(Ids: options::OPT_fuse_init_array)) { |
| 595 | Arg *A = DriverArgs.getLastArg(Ids: options::OPT_fuse_init_array); |
| 596 | getDriver().Diag(DiagID: clang::diag::err_drv_unsupported_opt_for_target) |
| 597 | << A->getAsString(Args: DriverArgs) << getTriple().str(); |
| 598 | } |
| 599 | |
| 600 | CC1Args.push_back(Elt: "-fno-use-init-array" ); |
| 601 | |
| 602 | // Default to `hidden` visibility for PS5. |
| 603 | if (getTriple().isPS5() && |
| 604 | !DriverArgs.hasArg(Ids: options::OPT_fvisibility_EQ, |
| 605 | Ids: options::OPT_fvisibility_ms_compat)) |
| 606 | CC1Args.push_back(Elt: "-fvisibility=hidden" ); |
| 607 | |
| 608 | // Default to -fvisibility-global-new-delete=source for PS5. |
| 609 | if (getTriple().isPS5() && |
| 610 | !DriverArgs.hasArg(Ids: options::OPT_fvisibility_global_new_delete_EQ, |
| 611 | Ids: options::OPT_fvisibility_global_new_delete_hidden)) |
| 612 | CC1Args.push_back(Elt: "-fvisibility-global-new-delete=source" ); |
| 613 | |
| 614 | const Arg *A = |
| 615 | DriverArgs.getLastArg(Ids: options::OPT_fvisibility_from_dllstorageclass, |
| 616 | Ids: options::OPT_fno_visibility_from_dllstorageclass); |
| 617 | if (!A || |
| 618 | A->getOption().matches(ID: options::OPT_fvisibility_from_dllstorageclass)) { |
| 619 | CC1Args.push_back(Elt: "-fvisibility-from-dllstorageclass" ); |
| 620 | |
| 621 | if (DriverArgs.hasArg(Ids: options::OPT_fvisibility_dllexport_EQ)) |
| 622 | DriverArgs.AddLastArg(Output&: CC1Args, Ids: options::OPT_fvisibility_dllexport_EQ); |
| 623 | else |
| 624 | CC1Args.push_back(Elt: "-fvisibility-dllexport=protected" ); |
| 625 | |
| 626 | // For PS4 we override the visibilty of globals definitions without |
| 627 | // dllimport or dllexport annotations. |
| 628 | if (DriverArgs.hasArg(Ids: options::OPT_fvisibility_nodllstorageclass_EQ)) |
| 629 | DriverArgs.AddLastArg(Output&: CC1Args, |
| 630 | Ids: options::OPT_fvisibility_nodllstorageclass_EQ); |
| 631 | else if (getTriple().isPS4()) |
| 632 | CC1Args.push_back(Elt: "-fvisibility-nodllstorageclass=hidden" ); |
| 633 | else |
| 634 | CC1Args.push_back(Elt: "-fvisibility-nodllstorageclass=keep" ); |
| 635 | |
| 636 | if (DriverArgs.hasArg(Ids: options::OPT_fvisibility_externs_dllimport_EQ)) |
| 637 | DriverArgs.AddLastArg(Output&: CC1Args, |
| 638 | Ids: options::OPT_fvisibility_externs_dllimport_EQ); |
| 639 | else |
| 640 | CC1Args.push_back(Elt: "-fvisibility-externs-dllimport=default" ); |
| 641 | |
| 642 | // For PS4 we override the visibilty of external globals without |
| 643 | // dllimport or dllexport annotations. |
| 644 | if (DriverArgs.hasArg( |
| 645 | Ids: options::OPT_fvisibility_externs_nodllstorageclass_EQ)) |
| 646 | DriverArgs.AddLastArg( |
| 647 | Output&: CC1Args, Ids: options::OPT_fvisibility_externs_nodllstorageclass_EQ); |
| 648 | else if (getTriple().isPS4()) |
| 649 | CC1Args.push_back(Elt: "-fvisibility-externs-nodllstorageclass=default" ); |
| 650 | else |
| 651 | CC1Args.push_back(Elt: "-fvisibility-externs-nodllstorageclass=keep" ); |
| 652 | } |
| 653 | |
| 654 | // Enable jump table sizes section for PS5. |
| 655 | if (getTriple().isPS5()) { |
| 656 | CC1Args.push_back(Elt: "-mllvm" ); |
| 657 | CC1Args.push_back(Elt: "-emit-jump-table-sizes-section" ); |
| 658 | } |
| 659 | } |
| 660 | |
| 661 | void toolchains::PS4PS5Base::addClangWarningOptions( |
| 662 | ArgStringList &CC1Args) const { |
| 663 | CC1Args.push_back(Elt: "-Wnonportable-include-path-separator" ); |
| 664 | CC1Args.push_back(Elt: "-Wnonportable-system-include-path" ); |
| 665 | } |
| 666 | |
| 667 | // PS4 toolchain. |
| 668 | toolchains::PS4CPU::PS4CPU(const Driver &D, const llvm::Triple &Triple, |
| 669 | const llvm::opt::ArgList &Args) |
| 670 | : PS4PS5Base(D, Triple, Args, "PS4" , "SCE_ORBIS_SDK_DIR" ) {} |
| 671 | |
| 672 | // PS5 toolchain. |
| 673 | toolchains::PS5CPU::PS5CPU(const Driver &D, const llvm::Triple &Triple, |
| 674 | const llvm::opt::ArgList &Args) |
| 675 | : PS4PS5Base(D, Triple, Args, "PS5" , "SCE_PROSPERO_SDK_DIR" ) {} |
| 676 | |