Skip to content

Commit 1672518

Browse files
committed
Refactor symbol name handling for AOT COFF32 binary format
1 parent 28456a5 commit 1672518

File tree

2 files changed

+46
-28
lines changed

2 files changed

+46
-28
lines changed

core/iwasm/aot/aot_runtime.c

Lines changed: 18 additions & 1 deletion
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);
@@ -3333,7 +3350,7 @@ aot_call_indirect(WASMExecEnv *exec_env, uint32 tbl_idx, uint32 table_elem_idx,
33333350
frame-per-function mode the frame is allocated at the
33343351
beginning of the function. */
33353352
if (!is_frame_per_function(exec_env)
3336-
&& !aot_alloc_frame(exec_env, table_elem_idx)) {
3353+
&& !aot_alloc_frame(exec_env, func_idx)) {
33373354
return false;
33383355
}
33393356
#endif

core/iwasm/compilation/aot_emit_aot_file.c

Lines changed: 28 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -922,10 +922,6 @@ get_relocations_size(AOTObjectData *obj_data,
922922
aot_emit_text_section */
923923

924924
const char *name = relocation->symbol_name;
925-
if (name && obj_data->target_info.bin_type == AOT_COFF32_BIN_TYPE
926-
&& *name == '_') {
927-
name++;
928-
}
929925
if ((!strcmp(relocation_group->section_name, ".text")
930926
|| !strcmp(relocation_group->section_name, ".ltext"))
931927
&& !strncmp(name, AOT_FUNC_INTERNAL_PREFIX,
@@ -2496,11 +2492,6 @@ aot_emit_text_section(uint8 *buf, uint8 *buf_end, uint32 *p_offset,
24962492
for (j = 0; j < relocation_count; j++) {
24972493
/* relocation to aot_func_internal#n */
24982494
const char *name = relocation->symbol_name;
2499-
if (name
2500-
&& obj_data->target_info.bin_type == AOT_COFF32_BIN_TYPE
2501-
&& *name == '_') {
2502-
name++;
2503-
}
25042495
if (str_starts_with(name, AOT_FUNC_INTERNAL_PREFIX)
25052496
&& ((obj_data->target_info.bin_type
25062497
== 6 /* AOT_COFF64_BIN_TYPE */
@@ -3062,6 +3053,27 @@ typedef struct elf64_rela {
30623053
#define SET_TARGET_INFO_FIELD(f, v, type, little) \
30633054
SET_TARGET_INFO_VALUE(f, elf_header->v, type, little)
30643055

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+
30653077
static bool
30663078
aot_resolve_target_info(AOTCompContext *comp_ctx, AOTObjectData *obj_data)
30673079
{
@@ -3536,12 +3548,9 @@ aot_resolve_stack_sizes(AOTCompContext *comp_ctx, AOTObjectData *obj_data)
35363548
}
35373549

35383550
while (!LLVMObjectFileIsSymbolIteratorAtEnd(obj_data->binary, sym_itr)) {
3539-
if ((name = LLVMGetSymbolName(sym_itr))
3540-
&& (!strcmp(name, aot_stack_sizes_alias_name)
3541-
/* symbol of COFF32 starts with "_" */
3542-
|| (obj_data->target_info.bin_type == AOT_COFF32_BIN_TYPE
3543-
&& !strncmp(name, "_", 1)
3544-
&& !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))) {
35453554
#if 0 /* cf. https://github.com/llvm/llvm-project/issues/67765 */
35463555
uint64 sz = LLVMGetSymbolSize(sym_itr);
35473556
if (sz != sizeof(uint32) * obj_data->func_count
@@ -3705,11 +3714,7 @@ aot_resolve_functions(AOTCompContext *comp_ctx, AOTObjectData *obj_data)
37053714
}
37063715

37073716
while (!LLVMObjectFileIsSymbolIteratorAtEnd(obj_data->binary, sym_itr)) {
3708-
name = (char *)LLVMGetSymbolName(sym_itr);
3709-
if (name && obj_data->target_info.bin_type == AOT_COFF32_BIN_TYPE
3710-
&& *name == '_') {
3711-
name++;
3712-
}
3717+
name = LLVMGetSymbolNameAndUnDecorate(sym_itr, obj_data->target_info);
37133718
if (name && str_starts_with(name, prefix)) {
37143719
/* symbol aot_func#n */
37153720
func_index = (uint32)atoi(name + strlen(prefix));
@@ -3896,7 +3901,8 @@ aot_resolve_object_relocation_group(AOTObjectData *obj_data,
38963901

38973902
/* set relocation fields */
38983903
relocation->relocation_type = (uint32)type;
3899-
relocation->symbol_name = (char *)LLVMGetSymbolName(rel_sym);
3904+
relocation->symbol_name =
3905+
LLVMGetSymbolNameAndUnDecorate(rel_sym, obj_data->target_info);
39003906
relocation->relocation_offset = offset;
39013907
if (!strcmp(group->section_name, ".rela.text.unlikely.")
39023908
|| !strcmp(group->section_name, ".rel.text.unlikely.")) {
@@ -3923,12 +3929,7 @@ aot_resolve_object_relocation_group(AOTObjectData *obj_data,
39233929
* Note: aot_stack_sizes_section_name section only contains
39243930
* stack_sizes table.
39253931
*/
3926-
if (!strcmp(relocation->symbol_name, aot_stack_sizes_name)
3927-
/* in windows 32, the symbol name may start with '_' */
3928-
|| (strlen(relocation->symbol_name) > 0
3929-
&& relocation->symbol_name[0] == '_'
3930-
&& !strcmp(relocation->symbol_name + 1,
3931-
aot_stack_sizes_name))) {
3932+
if (!strcmp(relocation->symbol_name, aot_stack_sizes_name)) {
39323933
/* discard const */
39333934
relocation->symbol_name = (char *)aot_stack_sizes_section_name;
39343935
}

0 commit comments

Comments
 (0)