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