Skip to content

[PS5][Driver] Pass default -z options to PS5 linker #113162

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Oct 23, 2024

Conversation

playstation-edd
Copy link
Contributor

Until now, these options have been hardcoded as downstream patches in LLD. Add them to the driver so that the private patches can be removed.

PS5 only. These implementation of these behaviours will remain in the proprietary linker on PS4.

SIE tracker: TOOLCHAIN-16704

Until now, these options have been hardcoded as downstream patches in
LLD. Add them to the driver so that the private patches can be removed.

PS5 only. These implementation of these behaviours will remain in the
proprietary linker on PS4.

SIE tracker: TOOLCHAIN-16704
@llvmbot llvmbot added clang Clang issues not falling into any other category clang:driver 'clang' and 'clang++' user-facing binaries. Not 'clang-cl' labels Oct 21, 2024
@llvmbot
Copy link
Member

llvmbot commented Oct 21, 2024

@llvm/pr-subscribers-clang

@llvm/pr-subscribers-clang-driver

Author: Edd Dawson (playstation-edd)

Changes

Until now, these options have been hardcoded as downstream patches in LLD. Add them to the driver so that the private patches can be removed.

PS5 only. These implementation of these behaviours will remain in the proprietary linker on PS4.

SIE tracker: TOOLCHAIN-16704


Full diff: https://github.com/llvm/llvm-project/pull/113162.diff

2 Files Affected:

  • (modified) clang/lib/Driver/ToolChains/PS4CPU.cpp (+28-5)
  • (modified) clang/test/Driver/ps5-linker.c (+16)
diff --git a/clang/lib/Driver/ToolChains/PS4CPU.cpp b/clang/lib/Driver/ToolChains/PS4CPU.cpp
index 7c028f18c0308f..a6a3501394afae 100644
--- a/clang/lib/Driver/ToolChains/PS4CPU.cpp
+++ b/clang/lib/Driver/ToolChains/PS4CPU.cpp
@@ -229,6 +229,8 @@ void tools::PS5cpu::Linker::ConstructJob(Compilation &C, const JobAction &JA,
   const Driver &D = TC.getDriver();
   ArgStringList CmdArgs;
 
+  const bool Relocatable = Args.hasArg(options::OPT_r);
+
   // Silence warning for "clang -g foo.o -o foo"
   Args.ClaimAllArgs(options::OPT_g_Group);
   // and "clang -emit-llvm foo.o -o foo"
@@ -240,11 +242,32 @@ void tools::PS5cpu::Linker::ConstructJob(Compilation &C, const JobAction &JA,
   CmdArgs.push_back(
       Args.MakeArgString("--sysroot=" + TC.getSDKLibraryRootDir()));
 
-  // Default to PIE for non-static executables.
-  const bool PIE =
-      !Args.hasArg(options::OPT_r, options::OPT_shared, options::OPT_static);
-  if (Args.hasFlag(options::OPT_pie, options::OPT_no_pie, PIE))
-    CmdArgs.push_back("-pie");
+  if (!Relocatable) {
+    // Default to PIE for non-static executables.
+    const bool PIE = !Args.hasArg(options::OPT_shared, options::OPT_static);
+    if (Args.hasFlag(options::OPT_pie, options::OPT_no_pie, PIE))
+      CmdArgs.push_back("-pie");
+
+    // Lazy binding of PLTs is not supported on PlayStation. They are placed in
+    // the RelRo segment.
+    CmdArgs.push_back("-z");
+    CmdArgs.push_back("now");
+
+    // Don't export linker-generated __start/stop... section bookends.
+    CmdArgs.push_back("-z");
+    CmdArgs.push_back("start-stop-visibility=hidden");
+
+    // Patch relocated regions of DWARF whose targets are eliminated at link
+    // time with specific tombstones, such that they're recognisable by the
+    // PlayStation debugger.
+    CmdArgs.push_back("-z");
+    CmdArgs.push_back("dead-reloc-in-nonalloc=.debug*=0xffffffffffffffff");
+    CmdArgs.push_back("-z");
+    CmdArgs.push_back(
+        "dead-reloc-in-nonalloc=.debug_ranges=0xfffffffffffffffe");
+    CmdArgs.push_back("-z");
+    CmdArgs.push_back("dead-reloc-in-nonalloc=.debug_loc=0xfffffffffffffffe");
+  }
 
   if (Args.hasArg(options::OPT_static))
     CmdArgs.push_back("-static");
diff --git a/clang/test/Driver/ps5-linker.c b/clang/test/Driver/ps5-linker.c
index 4ae65963e361aa..2b9e673c0a23e2 100644
--- a/clang/test/Driver/ps5-linker.c
+++ b/clang/test/Driver/ps5-linker.c
@@ -14,6 +14,22 @@
 // CHECK-NO-PIE-NOT: "-pie"
 // CHECK-SHARED: "--shared"
 
+// Test the driver passes PlayStation-specific -z options to the linker.
+
+// RUN: %clang --target=x86_64-sie-ps5 %s -### 2>&1 | FileCheck --check-prefixes=CHECK-Z %s
+
+// CHECK-Z: {{ld(\.exe)?}}"
+// CHECK-Z-SAME: "-z" "now"
+// CHECK-Z-SAME: "-z" "start-stop-visibility=hidden"
+// CHECK-Z-SAME: "-z" "dead-reloc-in-nonalloc=.debug*=0xffffffffffffffff"
+// CHECK-Z-SAME: "-z" "dead-reloc-in-nonalloc=.debug_ranges=0xfffffffffffffffe"
+// CHECK-Z-SAME: "-z" "dead-reloc-in-nonalloc=.debug_loc=0xfffffffffffffffe"
+
+// RUN: %clang --target=x86_64-sie-ps5 -r %s -### 2>&1 | FileCheck --check-prefixes=CHECK-NO-Z %s
+
+// CHECK-NO-Z: {{ld(\.exe)?}}"
+// CHECK-NO-Z-NOT: "-z"
+
 // Test that -static is forwarded to the linker
 
 // RUN: %clang --target=x86_64-sie-ps5 -static %s -### 2>&1 | FileCheck --check-prefixes=CHECK-STATIC %s

@playstation-edd
Copy link
Contributor Author

@pogo59 - I recall you expressed an interest in being subscribed to such PRs. Please do let me know if it gets tiresome!

@playstation-edd
Copy link
Contributor Author

Note to self: fix typo in comment on submission ("These implementation ...").

Copy link
Member

@jmorse jmorse left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

// Default to PIE for non-static executables.
const bool PIE = !Args.hasArg(options::OPT_shared, options::OPT_static);
if (Args.hasFlag(options::OPT_pie, options::OPT_no_pie, PIE))
CmdArgs.push_back("-pie");
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Did you mean to include these changes in the PR? It looks like the old code would pass through an explicit -pie even with -r; the new code will (probably) diagnose that -pie as unused. This is likely a good thing, but is not mentioned in the commit message.

Copy link
Contributor Author

@playstation-edd playstation-edd Oct 22, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The inclusion was deliberate. I took the opportunity to perform the check for OPT_r in one place. But I didn't spot the edge-case change in behaviour that you mention, forgetting that hasArg() had a side-effect - sorry!

Would we prefer that I undo this bit and defer to a separate PR? Or update the final commit message and perhaps add a test case for the behaviour change?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These are small things but I'd be inclined to separate it out. Apart from being "in the driver" it's not really related, functionally, to the -z changes.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have undone this part of the change. Thanks for spotting it.

@playstation-edd playstation-edd merged commit ac5a201 into llvm:main Oct 23, 2024
8 checks passed
@playstation-edd playstation-edd deleted the ps5-driver-z-opts branch October 23, 2024 09:14
@frobtech frobtech mentioned this pull request Oct 25, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
clang:driver 'clang' and 'clang++' user-facing binaries. Not 'clang-cl' clang Clang issues not falling into any other category
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants