Skip to content

Commit edbcbb7

Browse files
committed
fix(aot): reserve x18 register on macOS ARM64
Apple reserves CPU register x18 for TLS on ARM64. When generating AOT code for aarch64 on macOS, LLVM may use x18, causing crashes when the AOT code runs on macOS ARM64 (M1/M2/M3). This patch: 1. Detects darwin/macho ABI and sets correct vendor string 2. Detects darwin/apple in default triple for platform detection 3. Adds +reserve-x18 to LLVM target features for aarch64 on macOS The fix only applies when compiling on macOS ARM64 hosts, ensuring generated AOT code is compatible with Apple's platform requirements.
1 parent 2a2dd19 commit edbcbb7

1 file changed

Lines changed: 36 additions & 0 deletions

File tree

core/iwasm/compilation/aot_llvm.c

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3005,6 +3005,10 @@ aot_create_comp_context(const AOTCompData *comp_data, aot_comp_option_t option)
30053005
else
30063006
vendor_sys = "-pc-windows-";
30073007
}
3008+
else if (!strcmp(abi, "darwin") || !strcmp(abi, "macho")) {
3009+
/* macOS/Darwin: x18 is reserved by Apple */
3010+
vendor_sys = "-apple-";
3011+
}
30083012
else {
30093013
if (is_baremetal_target(arch, cpu, abi))
30103014
vendor_sys = "-unknown-none-";
@@ -3050,6 +3054,14 @@ aot_create_comp_context(const AOTCompData *comp_data, aot_comp_option_t option)
30503054
if (!abi)
30513055
abi = "gnu";
30523056
}
3057+
else if (strstr(default_triple, "darwin")
3058+
|| strstr(default_triple, "apple")) {
3059+
/* macOS/Darwin: x18 is reserved by Apple, must use correct
3060+
* triple to prevent LLVM from using it */
3061+
vendor_sys = "-apple-darwin";
3062+
if (!abi)
3063+
abi = "";
3064+
}
30533065
else {
30543066
vendor_sys = "-pc-linux-";
30553067
if (!abi)
@@ -3139,6 +3151,30 @@ aot_create_comp_context(const AOTCompData *comp_data, aot_comp_option_t option)
31393151
if (!features)
31403152
features = "";
31413153

3154+
#if (defined(__APPLE__) || defined(__MACH__)) && defined(BUILD_TARGET_AARCH64)
3155+
/* On macOS ARM64, x18 is reserved by Apple for TLS. Even though we're
3156+
* generating ELF (Linux-style) AOT files, we must tell LLVM to not
3157+
* use x18, otherwise the AOT code will crash when running on macOS. */
3158+
{
3159+
bool is_aarch64 = false;
3160+
if (arch && !strncmp(arch, "aarch64", 7))
3161+
is_aarch64 = true;
3162+
else if (triple_norm && strstr(triple_norm, "aarch64"))
3163+
is_aarch64 = true;
3164+
3165+
if (is_aarch64) {
3166+
if (features[0] != '\0') {
3167+
snprintf(features_buf, sizeof(features_buf), "%s,+reserve-x18",
3168+
features);
3169+
features = features_buf;
3170+
}
3171+
else {
3172+
features = "+reserve-x18";
3173+
}
3174+
}
3175+
}
3176+
#endif
3177+
31423178
/* Get target with triple, note that LLVMGetTargetFromTriple()
31433179
return 0 when success, but not true. */
31443180
if (LLVMGetTargetFromTriple(triple_norm, &target, &err) != 0) {

0 commit comments

Comments
 (0)