Skip to content

Commit fc78d67

Browse files
w4454962lum1n0us
andauthored
Fix the error of AOT mode on the "i386-windows-msvc" platform (#4183)
* Fix errors on the "i386-windows-msvc" platform * Refactor symbol name handling for AOT COFF32 binary format * Fix preprocessor directive placement for Windows compatibility in aot_reloc_x86_32.c --------- Co-authored-by: [email protected] <[email protected]>
1 parent d085d1c commit fc78d67

File tree

3 files changed

+89
-34
lines changed

3 files changed

+89
-34
lines changed

core/iwasm/aot/aot_runtime.c

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3285,8 +3285,25 @@ aot_call_indirect(WASMExecEnv *exec_env, uint32 tbl_idx, uint32 table_elem_idx,
32853285
cell_num += wasm_value_type_cell_num(ext_ret_types[i]);
32863286
}
32873287

3288+
#if WASM_ENABLE_AOT_STACK_FRAME != 0
3289+
void *prev_frame = get_top_frame(exec_env);
3290+
if (!is_frame_per_function(exec_env)
3291+
&& !aot_alloc_frame(exec_env, func_idx)) {
3292+
if (argv1 != argv1_buf)
3293+
wasm_runtime_free(argv1);
3294+
return false;
3295+
}
3296+
#endif
32883297
ret = invoke_native_internal(exec_env, func_ptr, func_type, signature,
32893298
attachment, argv1, argc, argv);
3299+
#if WASM_ENABLE_AOT_STACK_FRAME != 0
3300+
/* Free all frames allocated, note that some frames
3301+
may be allocated in AOT code and haven't been
3302+
freed if exception occurred */
3303+
while (get_top_frame(exec_env) != prev_frame)
3304+
aot_free_frame(exec_env);
3305+
#endif
3306+
32903307
if (!ret) {
32913308
if (argv1 != argv1_buf)
32923309
wasm_runtime_free(argv1);
@@ -3327,8 +3344,25 @@ aot_call_indirect(WASMExecEnv *exec_env, uint32 tbl_idx, uint32 table_elem_idx,
33273344
return true;
33283345
}
33293346
else {
3347+
#if WASM_ENABLE_AOT_STACK_FRAME != 0
3348+
void *prev_frame = get_top_frame(exec_env);
3349+
/* Only allocate frame for frame-per-call mode; in the
3350+
frame-per-function mode the frame is allocated at the
3351+
beginning of the function. */
3352+
if (!is_frame_per_function(exec_env)
3353+
&& !aot_alloc_frame(exec_env, func_idx)) {
3354+
return false;
3355+
}
3356+
#endif
33303357
ret = invoke_native_internal(exec_env, func_ptr, func_type, signature,
33313358
attachment, argv, argc, argv);
3359+
#if WASM_ENABLE_AOT_STACK_FRAME != 0
3360+
/* Free all frames allocated, note that some frames
3361+
may be allocated in AOT code and haven't been
3362+
freed if exception occurred */
3363+
while (get_top_frame(exec_env) != prev_frame)
3364+
aot_free_frame(exec_env);
3365+
#endif
33323366
if (!ret)
33333367
goto fail;
33343368

core/iwasm/aot/arch/aot_reloc_x86_32.c

Lines changed: 19 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -30,36 +30,39 @@ void __umoddi3();
3030
#pragma function(floor)
3131
#pragma function(ceil)
3232

33-
static int64
34-
__divdi3(int64 a, int64 b)
33+
static int64 __stdcall __divdi3(int64 a, int64 b)
3534
{
3635
return a / b;
3736
}
3837

39-
static uint64
40-
__udivdi3(uint64 a, uint64 b)
38+
static uint64 __stdcall __udivdi3(uint64 a, uint64 b)
4139
{
4240
return a / b;
4341
}
4442

45-
static int64
46-
__moddi3(int64 a, int64 b)
43+
static int64 __stdcall __moddi3(int64 a, int64 b)
4744
{
4845
return a % b;
4946
}
5047

51-
static uint64
52-
__umoddi3(uint64 a, uint64 b)
48+
static uint64 __stdcall __umoddi3(uint64 a, uint64 b)
5349
{
5450
return a % b;
5551
}
56-
#endif
5752

58-
static uint64
59-
__aulldiv(uint64 a, uint64 b)
53+
static uint64 __stdcall __aulldiv(uint64 a, uint64 b)
54+
{
55+
return a / b;
56+
}
57+
static int64 __stdcall __alldiv(int64 a, int64 b)
6058
{
6159
return a / b;
6260
}
61+
static int64 __stdcall __allrem(int64 a, int64 b)
62+
{
63+
return a % b;
64+
}
65+
#endif /* !defined(_WIN32) && !defined(_WIN32_) */
6366

6467
/* clang-format off */
6568
static SymbolMap target_sym_map[] = {
@@ -69,7 +72,11 @@ static SymbolMap target_sym_map[] = {
6972
REG_SYM(__udivdi3),
7073
REG_SYM(__moddi3),
7174
REG_SYM(__umoddi3),
72-
REG_SYM(__aulldiv)
75+
#if defined(_WIN32) || defined(_WIN32_)
76+
REG_SYM(__aulldiv),
77+
REG_SYM(__alldiv),
78+
REG_SYM(__allrem)
79+
#endif /* defined(_WIN32) || defined(_WIN32_) */
7380
};
7481
/* clang-format on */
7582

core/iwasm/compilation/aot_emit_aot_file.c

Lines changed: 36 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -920,9 +920,11 @@ get_relocations_size(AOTObjectData *obj_data,
920920
/* ignore the relocations to aot_func_internal#n in text section
921921
for windows platform since they will be applied in
922922
aot_emit_text_section */
923+
924+
const char *name = relocation->symbol_name;
923925
if ((!strcmp(relocation_group->section_name, ".text")
924926
|| !strcmp(relocation_group->section_name, ".ltext"))
925-
&& !strncmp(relocation->symbol_name, AOT_FUNC_INTERNAL_PREFIX,
927+
&& !strncmp(name, AOT_FUNC_INTERNAL_PREFIX,
926928
strlen(AOT_FUNC_INTERNAL_PREFIX))
927929
&& ((!strncmp(obj_data->comp_ctx->target_arch, "x86_64", 6)
928930
/* Windows AOT_COFF64_BIN_TYPE */
@@ -2489,8 +2491,8 @@ aot_emit_text_section(uint8 *buf, uint8 *buf_end, uint32 *p_offset,
24892491
relocation_count = relocation_group->relocation_count;
24902492
for (j = 0; j < relocation_count; j++) {
24912493
/* relocation to aot_func_internal#n */
2492-
if (str_starts_with(relocation->symbol_name,
2493-
AOT_FUNC_INTERNAL_PREFIX)
2494+
const char *name = relocation->symbol_name;
2495+
if (str_starts_with(name, AOT_FUNC_INTERNAL_PREFIX)
24942496
&& ((obj_data->target_info.bin_type
24952497
== 6 /* AOT_COFF64_BIN_TYPE */
24962498
&& relocation->relocation_type
@@ -2500,8 +2502,7 @@ aot_emit_text_section(uint8 *buf, uint8 *buf_end, uint32 *p_offset,
25002502
&& relocation->relocation_type
25012503
== 20 /* IMAGE_REL_I386_REL32 */))) {
25022504
uint32 func_idx =
2503-
atoi(relocation->symbol_name
2504-
+ strlen(AOT_FUNC_INTERNAL_PREFIX));
2505+
atoi(name + strlen(AOT_FUNC_INTERNAL_PREFIX));
25052506
uint64 text_offset, reloc_offset, reloc_addend;
25062507

25072508
bh_assert(func_idx < obj_data->func_count);
@@ -3052,6 +3053,27 @@ typedef struct elf64_rela {
30523053
#define SET_TARGET_INFO_FIELD(f, v, type, little) \
30533054
SET_TARGET_INFO_VALUE(f, elf_header->v, type, little)
30543055

3056+
/* in windows 32, the symbol name may start with '_' */
3057+
static char *
3058+
LLVMGetSymbolNameAndUnDecorate(LLVMSymbolIteratorRef si,
3059+
AOTTargetInfo target_info)
3060+
{
3061+
char *original_name = (char *)LLVMGetSymbolName(si);
3062+
if (!original_name) {
3063+
return NULL;
3064+
}
3065+
3066+
if (target_info.bin_type != AOT_COFF32_BIN_TYPE) {
3067+
return original_name;
3068+
}
3069+
3070+
if (*original_name == '_') {
3071+
return ++original_name;
3072+
}
3073+
3074+
return original_name;
3075+
}
3076+
30553077
static bool
30563078
aot_resolve_target_info(AOTCompContext *comp_ctx, AOTObjectData *obj_data)
30573079
{
@@ -3526,12 +3548,9 @@ aot_resolve_stack_sizes(AOTCompContext *comp_ctx, AOTObjectData *obj_data)
35263548
}
35273549

35283550
while (!LLVMObjectFileIsSymbolIteratorAtEnd(obj_data->binary, sym_itr)) {
3529-
if ((name = LLVMGetSymbolName(sym_itr))
3530-
&& (!strcmp(name, aot_stack_sizes_alias_name)
3531-
/* symbol of COFF32 starts with "_" */
3532-
|| (obj_data->target_info.bin_type == AOT_COFF32_BIN_TYPE
3533-
&& !strncmp(name, "_", 1)
3534-
&& !strcmp(name + 1, aot_stack_sizes_alias_name)))) {
3551+
if ((name =
3552+
LLVMGetSymbolNameAndUnDecorate(sym_itr, obj_data->target_info))
3553+
&& (!strcmp(name, aot_stack_sizes_alias_name))) {
35353554
#if 0 /* cf. https://github.com/llvm/llvm-project/issues/67765 */
35363555
uint64 sz = LLVMGetSymbolSize(sym_itr);
35373556
if (sz != sizeof(uint32) * obj_data->func_count
@@ -3695,8 +3714,8 @@ aot_resolve_functions(AOTCompContext *comp_ctx, AOTObjectData *obj_data)
36953714
}
36963715

36973716
while (!LLVMObjectFileIsSymbolIteratorAtEnd(obj_data->binary, sym_itr)) {
3698-
if ((name = (char *)LLVMGetSymbolName(sym_itr))
3699-
&& str_starts_with(name, prefix)) {
3717+
name = LLVMGetSymbolNameAndUnDecorate(sym_itr, obj_data->target_info);
3718+
if (name && str_starts_with(name, prefix)) {
37003719
/* symbol aot_func#n */
37013720
func_index = (uint32)atoi(name + strlen(prefix));
37023721
if (func_index < obj_data->func_count) {
@@ -3734,8 +3753,7 @@ aot_resolve_functions(AOTCompContext *comp_ctx, AOTObjectData *obj_data)
37343753
}
37353754
}
37363755
}
3737-
else if ((name = (char *)LLVMGetSymbolName(sym_itr))
3738-
&& str_starts_with(name, AOT_FUNC_INTERNAL_PREFIX)) {
3756+
else if (name && str_starts_with(name, AOT_FUNC_INTERNAL_PREFIX)) {
37393757
/* symbol aot_func_internal#n */
37403758
func_index = (uint32)atoi(name + strlen(AOT_FUNC_INTERNAL_PREFIX));
37413759
if (func_index < obj_data->func_count) {
@@ -3883,7 +3901,8 @@ aot_resolve_object_relocation_group(AOTObjectData *obj_data,
38833901

38843902
/* set relocation fields */
38853903
relocation->relocation_type = (uint32)type;
3886-
relocation->symbol_name = (char *)LLVMGetSymbolName(rel_sym);
3904+
relocation->symbol_name =
3905+
LLVMGetSymbolNameAndUnDecorate(rel_sym, obj_data->target_info);
38873906
relocation->relocation_offset = offset;
38883907
if (!strcmp(group->section_name, ".rela.text.unlikely.")
38893908
|| !strcmp(group->section_name, ".rel.text.unlikely.")) {
@@ -3910,12 +3929,7 @@ aot_resolve_object_relocation_group(AOTObjectData *obj_data,
39103929
* Note: aot_stack_sizes_section_name section only contains
39113930
* stack_sizes table.
39123931
*/
3913-
if (!strcmp(relocation->symbol_name, aot_stack_sizes_name)
3914-
/* in windows 32, the symbol name may start with '_' */
3915-
|| (strlen(relocation->symbol_name) > 0
3916-
&& relocation->symbol_name[0] == '_'
3917-
&& !strcmp(relocation->symbol_name + 1,
3918-
aot_stack_sizes_name))) {
3932+
if (!strcmp(relocation->symbol_name, aot_stack_sizes_name)) {
39193933
/* discard const */
39203934
relocation->symbol_name = (char *)aot_stack_sizes_section_name;
39213935
}

0 commit comments

Comments
 (0)