[libunwind] [libunwind] Add CMake option to enable execute-only code generation on AArch64 (PR #140554)

Csanád Hajdú via cfe-commits cfe-commits at lists.llvm.org
Mon May 19 07:57:16 PDT 2025


https://github.com/Il-Capitano created https://github.com/llvm/llvm-project/pull/140554

For a full toolchain supporting execute-only code generation the runtime libraries also need to be pre-compiled with it enabled. For libunwind this can now be enabled with the `LIBUNWIND_EXECUTE_ONLY_CODE` CMake option during build configuration.

The build option can only be enabled for a runtimes build of libunwind, because a recent version of Clang is needed to correctly compile assembly files with execute-only code support.

Related RFC: https://discourse.llvm.org/t/rfc-execute-only-code-support-for-runtime-libraries-on-aarch64/86180

>From c69f17edaceb95154e25f0e9062363d88498a775 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Csan=C3=A1d=20Hajd=C3=BA?= <csanad.hajdu at arm.com>
Date: Mon, 19 May 2025 12:29:43 +0200
Subject: [PATCH] [libunwind] Add CMake option to enable execute-only code
 generation on AArch64

For a full toolchain supporting execute-only code generation the runtime
libraries also need to be pre-compiled with it enabled. For libunwind
this can now be enabled with the `LIBUNWIND_EXECUTE_ONLY_CODE` CMake
option during build configuration.

The build option can only be enabled for a runtimes build of libunwind,
because a recent version of Clang is needed to correctly compile
assembly files with execute-only code support.

Related RFC: https://discourse.llvm.org/t/rfc-execute-only-code-support-for-runtime-libraries-on-aarch64/86180
---
 libunwind/CMakeLists.txt               | 18 ++++++++++++++++++
 libunwind/src/UnwindRegistersRestore.S |  2 ++
 libunwind/src/UnwindRegistersSave.S    |  2 ++
 3 files changed, 22 insertions(+)

diff --git a/libunwind/CMakeLists.txt b/libunwind/CMakeLists.txt
index e27f3c2e2fc17..75195fe022d4b 100644
--- a/libunwind/CMakeLists.txt
+++ b/libunwind/CMakeLists.txt
@@ -55,6 +55,7 @@ option(LIBUNWIND_USE_FRAME_HEADER_CACHE "Cache frame headers for unwinding. Requ
 option(LIBUNWIND_REMEMBER_HEAP_ALLOC "Use heap instead of the stack for .cfi_remember_state." OFF)
 option(LIBUNWIND_INSTALL_HEADERS "Install the libunwind headers." ON)
 option(LIBUNWIND_ENABLE_FRAME_APIS "Include libgcc-compatible frame apis." OFF)
+option(LIBUNWIND_EXECUTE_ONLY_CODE "Compile libunwind as execute-only." OFF)
 
 set(LIBUNWIND_LIBDIR_SUFFIX "${LLVM_LIBDIR_SUFFIX}" CACHE STRING
     "Define suffix of library directory name (32/64)")
@@ -109,6 +110,11 @@ endif()
 option(LIBUNWIND_HIDE_SYMBOLS
   "Do not export any symbols from the static library." ${LIBUNWIND_DEFAULT_HIDE_SYMBOLS})
 
+if (LIBUNWIND_EXECUTE_ONLY_CODE AND NOT LLVM_RUNTIMES_BUILD)
+  message(SEND_ERROR "LIBUNWIND_EXECUTE_ONLY_CODE is only supported "
+                     "for runtimes build of libunwind.")
+endif()
+
 # If toolchain is FPXX, we switch to FP64 to save the full FPRs. See:
 # https://web.archive.org/web/20180828210612/https://dmz-portal.mips.com/wiki/MIPS_O32_ABI_-_FR0_and_FR1_Interlinking
 check_symbol_exists(__mips_hard_float "" __MIPSHF)
@@ -330,6 +336,18 @@ if (C_SUPPORTS_COMMENT_LIB_PRAGMA)
   endif()
 endif()
 
+if (LIBUNWIND_EXECUTE_ONLY_CODE)
+  add_compile_flags_if_supported(-mexecute-only)
+  if (NOT CXX_SUPPORTS_MEXECUTE_ONLY_FLAG)
+    add_compile_flags_if_supported(-mpure-code)
+    if (NOT CXX_SUPPORTS_MPURE_CODE_FLAG)
+      message(SEND_ERROR
+        "Compiler doesn't support -mexecute-only or -mpure-code option!")
+    endif()
+  endif()
+  add_compile_definitions(_LIBUNWIND_EXECUTE_ONLY_CODE)
+endif()
+
 #===============================================================================
 # Setup Source Code
 #===============================================================================
diff --git a/libunwind/src/UnwindRegistersRestore.S b/libunwind/src/UnwindRegistersRestore.S
index 5e199188945df..2a0e81a9794eb 100644
--- a/libunwind/src/UnwindRegistersRestore.S
+++ b/libunwind/src/UnwindRegistersRestore.S
@@ -16,6 +16,8 @@
 
 #if defined(_AIX)
   .toc
+#elif defined(__aarch64__) && defined(__ELF__) && defined(_LIBUNWIND_EXECUTE_ONLY_CODE)
+  .section .text,"axy", at progbits,unique,0
 #else
   .text
 #endif
diff --git a/libunwind/src/UnwindRegistersSave.S b/libunwind/src/UnwindRegistersSave.S
index 5139a551ad245..1ff14cf272b61 100644
--- a/libunwind/src/UnwindRegistersSave.S
+++ b/libunwind/src/UnwindRegistersSave.S
@@ -16,6 +16,8 @@
 
 #if defined(_AIX)
     .toc
+#elif defined(__aarch64__) && defined(__ELF__) && defined(_LIBUNWIND_EXECUTE_ONLY_CODE)
+    .section .text,"axy", at progbits,unique,0
 #else
     .text
 #endif



More information about the cfe-commits mailing list