Skip to content

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

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

Open
wants to merge 2 commits into
base: main
Choose a base branch
from

Conversation

Il-Capitano
Copy link
Contributor

@Il-Capitano Il-Capitano commented May 19, 2025

For a full toolchain supporting execute-only code generation the runtime libraries also need to be pre-compiled with it enabled. The generic LLVM_EXECUTE_ONLY_CODE CMake option can now be used during build configuration to enable execute-only code generation in libunwind.

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

…n 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
@Il-Capitano Il-Capitano requested a review from a team as a code owner May 19, 2025 14:57
@llvmbot
Copy link
Member

llvmbot commented May 19, 2025

@llvm/pr-subscribers-libunwind

Author: Csanád Hajdú (Il-Capitano)

Changes

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


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

3 Files Affected:

  • (modified) libunwind/CMakeLists.txt (+18)
  • (modified) libunwind/src/UnwindRegistersRestore.S (+2)
  • (modified) libunwind/src/UnwindRegistersSave.S (+2)
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",@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",@progbits,unique,0
 #else
     .text
 #endif

@Il-Capitano
Copy link
Contributor Author

I simplified the configuration based on the discussion in the RFC. C and C++ compiler flags will be handled by the change in #143698.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants