Skip to content
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

[HIP] fix stack marking for -fgpu-rdc #72782

Merged
merged 1 commit into from
Dec 1, 2023
Merged

Conversation

yxsamliu
Copy link
Collaborator

HIP toolchain uses llvm-mc to generate a host object embedding device binary for -fgpu-rdc. Due to lack of .note.GNU-stack section, the generated relocatable has executable stack marking, which disables protection from executable stack for any HIP programs compiled with -fgpu-rdc.

This patch adds .note.GNU-stack section to the input to llvm-mc to fix the executable stack marking.

Fixes: #71711

@llvmbot llvmbot added clang Clang issues not falling into any other category clang:driver 'clang' and 'clang++' user-facing binaries. Not 'clang-cl' labels Nov 19, 2023
@llvmbot
Copy link
Collaborator

llvmbot commented Nov 19, 2023

@llvm/pr-subscribers-clang

@llvm/pr-subscribers-clang-driver

Author: Yaxun (Sam) Liu (yxsamliu)

Changes

HIP toolchain uses llvm-mc to generate a host object embedding device binary for -fgpu-rdc. Due to lack of .note.GNU-stack section, the generated relocatable has executable stack marking, which disables protection from executable stack for any HIP programs compiled with -fgpu-rdc.

This patch adds .note.GNU-stack section to the input to llvm-mc to fix the executable stack marking.

Fixes: #71711


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

2 Files Affected:

  • (modified) clang/lib/Driver/ToolChains/HIPUtility.cpp (+2)
  • (modified) clang/test/Driver/hip-toolchain-rdc.hip (+2)
diff --git a/clang/lib/Driver/ToolChains/HIPUtility.cpp b/clang/lib/Driver/ToolChains/HIPUtility.cpp
index 04efdcba20ea740..671cd060e0d810d 100644
--- a/clang/lib/Driver/ToolChains/HIPUtility.cpp
+++ b/clang/lib/Driver/ToolChains/HIPUtility.cpp
@@ -150,6 +150,8 @@ void HIP::constructGenerateObjFileFromHIPFatBinary(
   ObjStream << "  .incbin ";
   llvm::sys::printArg(ObjStream, BundleFile, /*Quote=*/true);
   ObjStream << "\n";
+  if (HostTriple.isOSLinux() && HostTriple.isOSBinFormatELF())
+	  ObjStream << "  .section .note.GNU-stack, \"\", @progbits\n";
   ObjStream.flush();
 
   // Dump the contents of the temp object file gen if the user requested that.
diff --git a/clang/test/Driver/hip-toolchain-rdc.hip b/clang/test/Driver/hip-toolchain-rdc.hip
index fa30bb8a9f5f576..1827531f9cab7a3 100644
--- a/clang/test/Driver/hip-toolchain-rdc.hip
+++ b/clang/test/Driver/hip-toolchain-rdc.hip
@@ -32,6 +32,8 @@
 // CHECK: .p2align 12
 // CHECK: __hip_fatbin:
 // CHECK: .incbin "[[BUNDLE:.*hipfb]]"
+// LNX: .section .note.GNU-stack, "", @progbits
+// MSVC-NOT: .note.GNU-stack
 
 // emit objects for host side path
 // CHECK: [[CLANG:".*clang.*"]] "-cc1" "-triple" [[HOST:"x86_64-[^"]+"]]

Copy link

github-actions bot commented Nov 19, 2023

✅ With the latest revision this PR passed the C/C++ code formatter.

HIP toolchain uses llvm-mc to generate a host object embedding
device binary for -fgpu-rdc. Due to lack of .note.GNU-stack
section, the generated relocatable has executable stack
marking, which disables protection from executable stack
for any HIP programs compiled with -fgpu-rdc.

This patch adds .note.GNU-stack section to the input
to llvm-mc to fix the executable stack marking.

Fixes: llvm#71711
@yxsamliu yxsamliu merged commit a4d8549 into llvm:main Dec 1, 2023
3 checks passed
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.

[AMDGPU] Linking with FatBinary files disables stack execution protection
3 participants