[libc-commits] [libc] [libc] Fix gnu::naked and return-type errors with gcc on aarch64 (PR #120525)

Tristan Ross via libc-commits libc-commits at lists.llvm.org
Wed Dec 18 22:07:17 PST 2024


https://github.com/RossComputerGuy created https://github.com/llvm/llvm-project/pull/120525

This fixes `gnu::naked` from being used on aarch64 which GCC does not support. It also uses the unreachable builtin so GCC stops failing with `-Werror=return-type`.

>From 270635502cf07f48c221739c777cfb3bead41db0 Mon Sep 17 00:00:00 2001
From: Tristan Ross <tristan.ross at midstall.com>
Date: Wed, 18 Dec 2024 22:05:31 -0800
Subject: [PATCH] [libc] Fix gnu::naked and return-type errors with gcc on
 aarch64

---
 libc/src/setjmp/aarch64/longjmp.cpp | 13 ++++++++++---
 libc/src/setjmp/aarch64/setjmp.cpp  | 10 +++++++++-
 2 files changed, 19 insertions(+), 4 deletions(-)

diff --git a/libc/src/setjmp/aarch64/longjmp.cpp b/libc/src/setjmp/aarch64/longjmp.cpp
index 80c97c721cc4f6..3adbd063a0d62b 100644
--- a/libc/src/setjmp/aarch64/longjmp.cpp
+++ b/libc/src/setjmp/aarch64/longjmp.cpp
@@ -22,9 +22,11 @@ namespace LIBC_NAMESPACE_DECL {
 // supports the MTE instructions, not whether the compiler is configured to use
 // them.)
 
-[[gnu::naked]] LLVM_LIBC_FUNCTION(void, longjmp,
-                                  ([[maybe_unused]] jmp_buf buf,
-                                   [[maybe_unused]] int val)) {
+#ifndef __GNUC__
+[[gnu::naked]]
+#endif
+LLVM_LIBC_FUNCTION(void, longjmp,
+                   ([[maybe_unused]] jmp_buf buf, [[maybe_unused]] int val)) {
   // If BTI branch protection is in use, the compiler will automatically insert
   // a BTI here, so we don't need to make any extra effort to do so.
 
@@ -87,6 +89,11 @@ namespace LIBC_NAMESPACE_DECL {
       R"(
         ret
       )");
+
+#ifdef __GNUC__
+  // GCC fails here because it doesn't recognize a value is returned.
+  __builtin_unreachable();
+#endif
 }
 
 } // namespace LIBC_NAMESPACE_DECL
diff --git a/libc/src/setjmp/aarch64/setjmp.cpp b/libc/src/setjmp/aarch64/setjmp.cpp
index 8dd1eb342c0973..35f2b34e586ad9 100644
--- a/libc/src/setjmp/aarch64/setjmp.cpp
+++ b/libc/src/setjmp/aarch64/setjmp.cpp
@@ -12,7 +12,10 @@
 
 namespace LIBC_NAMESPACE_DECL {
 
-[[gnu::naked]] LLVM_LIBC_FUNCTION(int, setjmp, ([[maybe_unused]] jmp_buf buf)) {
+#ifndef __GNUC__
+[[gnu::naked]]
+#endif
+LLVM_LIBC_FUNCTION(int, setjmp, ([[maybe_unused]] jmp_buf buf)) {
   // If BTI branch protection is in use, the compiler will automatically insert
   // a BTI here, so we don't need to make any extra effort to do so.
 
@@ -88,6 +91,11 @@ namespace LIBC_NAMESPACE_DECL {
       R"(
         ret
       )");
+
+#ifdef __GNUC__
+  // GCC fails here because it doesn't recognize a value is returned.
+  __builtin_unreachable();
+#endif
 }
 
 } // namespace LIBC_NAMESPACE_DECL



More information about the libc-commits mailing list