Skip to content

[libc++] Add CMake option to enable execute-only code generation on AArch64 #140552

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 1 commit into
base: main
Choose a base branch
from

Conversation

Il-Capitano
Copy link
Contributor

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

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

…Arch64

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

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:55
@llvmbot llvmbot added the libc++ libc++ C++ Standard Library. Not GNU libstdc++. Not libc++abi. label May 19, 2025
@llvmbot
Copy link
Member

llvmbot commented May 19, 2025

@llvm/pr-subscribers-libcxx

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 libc++ this can now be enabled with the LIBCXX_EXECUTE_ONLY_CODE CMake option during build configuration.

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/140552.diff

1 Files Affected:

  • (modified) libcxx/CMakeLists.txt (+13)
diff --git a/libcxx/CMakeLists.txt b/libcxx/CMakeLists.txt
index dffdd7a3c70a6..a3e0d3e909255 100644
--- a/libcxx/CMakeLists.txt
+++ b/libcxx/CMakeLists.txt
@@ -318,6 +318,8 @@ endif()
 option(LIBCXX_HERMETIC_STATIC_LIBRARY
   "Do not export any symbols from the static library." ${LIBCXX_HERMETIC_STATIC_LIBRARY_DEFAULT})
 
+option(LIBCXX_EXECUTE_ONLY_CODE "Compile libc++ as execute-only." OFF)
+
 #===============================================================================
 # Check option configurations
 #===============================================================================
@@ -538,6 +540,17 @@ function(cxx_add_basic_build_flags target)
   # errors.
   target_compile_definitions(${target} PRIVATE -D_LIBCPP_REMOVE_TRANSITIVE_INCLUDES)
 
+  if (LIBCXX_EXECUTE_ONLY_CODE)
+    target_add_compile_flags_if_supported(${target} PRIVATE -mexecute-only)
+    if (NOT CXX_SUPPORTS_MEXECUTE_ONLY_FLAG)
+      target_add_compile_flags_if_supported(${target} PRIVATE -mpure-code)
+      if (NOT CXX_SUPPORTS_MPURE_CODE_FLAG)
+        message(SEND_ERROR "Compiler doesn't support -mexecute-only or "
+                           "-mpure-code option for target ${target}!")
+      endif()
+    endif()
+  endif()
+
   if (C_SUPPORTS_COMMENT_LIB_PRAGMA)
     if (LIBCXX_HAS_PTHREAD_LIB)
       target_compile_definitions(${target} PRIVATE -D_LIBCPP_LINK_PTHREAD_LIB)

@philnik777 philnik777 requested a review from ldionne May 19, 2025 15:03
@philnik777
Copy link
Contributor

Is there a reason to not do this unconditionally?

@Il-Capitano
Copy link
Contributor Author

Is there a reason to not do this unconditionally?

Execute-only code generation for AArch64 is fairly new, and currently planned to have limited use. Also older tools may not correctly handle merging the SHF_AARCH64_PURECODE ELF section flag that LLVM puts on .text (LLD 20 and before doesn't for example. It produces a valid non-execute-only output though, just that the section flag may be incorrectly kept).

If you don't use a very recent version of Clang, LLD, and a compatible libc implementation, you won't be able to take advantage of this feature, and may get unexpected behaviour from tools. In general, this configuration is intended for assembling a complete toolchain, where you control everything.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
libc++ libc++ C++ Standard Library. Not GNU libstdc++. Not libc++abi.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants