Skip to content

Commit ff3eb6f

Browse files
Il-Capitanovinay-deshmukh
authored andcommitted
[compiler-rt] Add CMake option to enable execute-only code generation on AArch64 (llvm#140555)
For a full toolchain supporting execute-only code generation the runtime libraries also need to be pre-compiled with it enabled. The generic `RUNTIMES_EXECUTE_ONLY_CODE` CMake option can now be used during build configuration to enable execute-only code generation in compiler-rt. The build option can only be enabled for a runtimes build of compiler-rt, 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
1 parent 0dcea13 commit ff3eb6f

File tree

12 files changed

+40
-10
lines changed

12 files changed

+40
-10
lines changed

compiler-rt/CMakeLists.txt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -605,6 +605,10 @@ string(REGEX REPLACE "-stdlib=[a-zA-Z+]*" "" CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS}
605605
list(APPEND COMPILER_RT_COMMON_CFLAGS ${stdlib_flag})
606606
list(APPEND COMPILER_RT_COMMON_LINK_FLAGS ${stdlib_flag})
607607

608+
# Add assembler flags for execute-only code generation. C and C++ flags should have already
609+
# been added to CMAKE_C_FLAGS and CMAKE_CXX_FLAGS.
610+
append_string_if(RUNTIMES_EXECUTE_ONLY_CODE -DCOMPILER_RT_EXECUTE_ONLY_CODE CMAKE_ASM_FLAGS)
611+
608612
# TODO: There's a lot of duplication across lib/*/tests/CMakeLists.txt files,
609613
# move some of the common flags to COMPILER_RT_UNITTEST_CFLAGS.
610614

compiler-rt/lib/builtins/assembly.h

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,9 +71,24 @@
7171

7272
#endif
7373

74+
#if defined(__aarch64__) && defined(__ELF__) && \
75+
defined(COMPILER_RT_EXECUTE_ONLY_CODE)
76+
// The assembler always creates an implicit '.text' section with default flags
77+
// (SHF_ALLOC | SHF_EXECINSTR), which is incompatible with the execute-only
78+
// '.text' section we want to create here because of the missing
79+
// SHF_AARCH64_PURECODE section flag. To solve this, we use 'unique,0' to
80+
// differentiate the two sections. The output will therefore have two separate
81+
// sections named '.text', where code will be placed into the execute-only
82+
// '.text' section, and the implicitly-created one will be empty.
83+
#define TEXT_SECTION \
84+
.section .text,"axy",@progbits,unique,0
85+
#else
86+
#define TEXT_SECTION \
87+
.text
88+
#endif
89+
7490
#if defined(__arm__) || defined(__aarch64__) || defined(__arm64ec__)
7591
#define FUNC_ALIGN \
76-
.text SEPARATOR \
7792
.balign 16 SEPARATOR
7893
#else
7994
#define FUNC_ALIGN
@@ -255,6 +270,7 @@
255270
#endif
256271

257272
#define DEFINE_COMPILERRT_FUNCTION(name) \
273+
TEXT_SECTION SEPARATOR \
258274
DEFINE_CODE_STATE \
259275
FILE_LEVEL_DIRECTIVE SEPARATOR \
260276
.globl FUNC_SYMBOL(SYMBOL_NAME(name)) SEPARATOR \
@@ -264,6 +280,7 @@
264280
FUNC_SYMBOL(SYMBOL_NAME(name)):
265281

266282
#define DEFINE_COMPILERRT_THUMB_FUNCTION(name) \
283+
TEXT_SECTION SEPARATOR \
267284
DEFINE_CODE_STATE \
268285
FILE_LEVEL_DIRECTIVE SEPARATOR \
269286
.globl FUNC_SYMBOL(SYMBOL_NAME(name)) SEPARATOR \
@@ -273,6 +290,7 @@
273290
FUNC_SYMBOL(SYMBOL_NAME(name)):
274291

275292
#define DEFINE_COMPILERRT_PRIVATE_FUNCTION(name) \
293+
TEXT_SECTION SEPARATOR \
276294
DEFINE_CODE_STATE \
277295
FILE_LEVEL_DIRECTIVE SEPARATOR \
278296
.globl FUNC_SYMBOL(SYMBOL_NAME(name)) SEPARATOR \
@@ -282,6 +300,7 @@
282300
FUNC_SYMBOL(SYMBOL_NAME(name)):
283301

284302
#define DEFINE_COMPILERRT_PRIVATE_FUNCTION_UNMANGLED(name) \
303+
TEXT_SECTION SEPARATOR \
285304
DEFINE_CODE_STATE \
286305
.globl FUNC_SYMBOL(name) SEPARATOR \
287306
SYMBOL_IS_FUNC(name) SEPARATOR \
@@ -290,6 +309,7 @@
290309
FUNC_SYMBOL(name):
291310

292311
#define DEFINE_COMPILERRT_OUTLINE_FUNCTION_UNMANGLED(name) \
312+
TEXT_SECTION SEPARATOR \
293313
DEFINE_CODE_STATE \
294314
FUNC_ALIGN \
295315
.globl FUNC_SYMBOL(name) SEPARATOR \

compiler-rt/lib/fuzzer/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -162,6 +162,7 @@ if(OS_NAME MATCHES "Android|Linux|Fuchsia" AND
162162
CFLAGS ${TARGET_CFLAGS}
163163
CMAKE_ARGS -DCMAKE_CXX_COMPILER_WORKS=ON
164164
-DCMAKE_POSITION_INDEPENDENT_CODE=ON
165+
-DRUNTIMES_EXECUTE_ONLY_CODE=${RUNTIMES_EXECUTE_ONLY_CODE}
165166
-DLIBCXXABI_ENABLE_EXCEPTIONS=OFF
166167
-DLIBCXX_ABI_NAMESPACE=__Fuzzer
167168
-DLIBCXX_ENABLE_EXCEPTIONS=OFF)

compiler-rt/lib/hwasan/hwasan_setjmp_aarch64.S

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@
2828
// stack pointer when compiling a C function.
2929
// Hence we have to write this function in assembly.
3030

31-
.section .text
31+
TEXT_SECTION
3232
.file "hwasan_setjmp_aarch64.S"
3333

3434
.global ASM_WRAPPER_NAME(setjmp)

compiler-rt/lib/hwasan/hwasan_tag_mismatch_aarch64.S

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@
7070
// clobbering the x17 register in error reports, and that the program will have
7171
// a runtime dependency on the __hwasan_tag_mismatch_v2 symbol therefore it will
7272
// fail to start up given an older (i.e. incompatible) runtime.
73-
.section .text
73+
TEXT_SECTION
7474
.file "hwasan_tag_mismatch_aarch64.S"
7575
.global __hwasan_tag_mismatch
7676
.type __hwasan_tag_mismatch, %function

compiler-rt/lib/msan/tests/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,7 @@ if(COMPILER_RT_CAN_EXECUTE_TESTS AND
139139
add_custom_libcxx(libcxx_msan_${arch} ${LIBCXX_PREFIX}
140140
DEPS ${MSAN_RUNTIME_LIBRARIES}
141141
CFLAGS ${MSAN_LIBCXX_CFLAGS} ${TARGET_CFLAGS}
142+
CMAKE_ARGS -DRUNTIMES_EXECUTE_ONLY_CODE=${RUNTIMES_EXECUTE_ONLY_CODE}
142143
USE_TOOLCHAIN)
143144
set(MSAN_LIBCXX_DIR ${LIBCXX_PREFIX}/lib/)
144145

compiler-rt/lib/orc/elfnix_tls.aarch64.S

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,11 @@
1313
// The content of this file is aarch64-only
1414
#if defined(__arm64__) || defined(__aarch64__)
1515

16+
#include "builtins/assembly.h"
17+
1618
#define REGISTER_SAVE_SPACE_SIZE 32 * 24
1719

18-
.text
20+
TEXT_SECTION
1921

2022
// returns address of TLV in x0, all other registers preserved
2123
// TODO: add fast-path for repeat access

compiler-rt/lib/orc/sysv_reenter.arm64.S

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,9 @@
1313
// The content of this file is arm64-only
1414
#if defined(__arm64__) || defined(__aarch64__)
1515

16-
.text
16+
#include "builtins/assembly.h"
17+
18+
TEXT_SECTION
1719

1820
// Saves GPRs, calls __orc_rt_resolve
1921
.globl __orc_rt_sysv_reenter

compiler-rt/lib/sanitizer_common/sanitizer_common_interceptors_vfork_aarch64.inc.S

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55

66
ASM_HIDDEN(COMMON_INTERCEPTOR_SPILL_AREA)
77

8+
TEXT_SECTION
89
.comm _ZN14__interception10real_vforkE,8,8
910
.globl ASM_WRAPPER_NAME(vfork)
1011
ASM_TYPE_FUNCTION(ASM_WRAPPER_NAME(vfork))

compiler-rt/lib/tsan/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ if(COMPILER_RT_LIBCXX_PATH AND
3030
add_custom_libcxx(libcxx_tsan_${arch} ${LIBCXX_PREFIX}
3131
DEPS ${TSAN_RUNTIME_LIBRARIES}
3232
CFLAGS ${TARGET_CFLAGS} -fsanitize=thread
33+
CMAKE_ARGS -DRUNTIMES_EXECUTE_ONLY_CODE=${RUNTIMES_EXECUTE_ONLY_CODE}
3334
USE_TOOLCHAIN)
3435
list(APPEND libcxx_tsan_deps libcxx_tsan_${arch}-install-cmake326-workaround)
3536
endforeach()

0 commit comments

Comments
 (0)