[clang] [HIP] fix stack marking for -fgpu-rdc (PR #72782)

Yaxun Liu via cfe-commits cfe-commits at lists.llvm.org
Sat Nov 18 21:31:03 PST 2023


https://github.com/yxsamliu created https://github.com/llvm/llvm-project/pull/72782

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: https://github.com/llvm/llvm-project/issues/71711

>From adf19a8c09fc4ed82e4b42c735452e8c4ed8d84f Mon Sep 17 00:00:00 2001
From: "Yaxun (Sam) Liu" <yaxun.liu at amd.com>
Date: Fri, 17 Nov 2023 18:35:36 -0500
Subject: [PATCH] [HIP] fix stack marking for -fgpu-rdc

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: https://github.com/llvm/llvm-project/issues/71711
---
 clang/lib/Driver/ToolChains/HIPUtility.cpp | 2 ++
 clang/test/Driver/hip-toolchain-rdc.hip    | 2 ++
 2 files changed, 4 insertions(+)

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-[^"]+"]]



More information about the cfe-commits mailing list