Skip to content

Commit

Permalink
Merge pull request #3 from no1wudi/dev
Browse files Browse the repository at this point in the history
Implement native symbol section loader for AOT mode
  • Loading branch information
wenyongh committed Jan 14, 2021
2 parents e8105d8 + e89290a commit cae61bf
Show file tree
Hide file tree
Showing 5 changed files with 97 additions and 11 deletions.
69 changes: 68 additions & 1 deletion core/iwasm/aot/aot_loader.c
Original file line number Diff line number Diff line change
Expand Up @@ -362,6 +362,65 @@ load_target_info_section(const uint8 *buf, const uint8 *buf_end,
return false;
}

static void *
get_native_symbol_by_name(const char *name)
{
void *func = NULL;
uint32 symnum = 0;
SymbolMap *sym = NULL;

sym = get_target_symbol_map(&symnum);

while (symnum--) {
if (strcmp(sym->symbol_name, name) == 0) {
func = sym->symbol_addr;
break;
}
sym++;
}

return func;
}

static bool
load_native_symbol_section(const uint8 *buf,
const uint8 *buf_end,
AOTModule *module,
char *error_buf,
uint32 error_buf_size)
{
const uint8 *p = buf, *p_end = buf_end;
uint32 cnt;
const char *symbol;

read_uint32(p, p_end, cnt);

module->native_symbol_count = cnt;

if (cnt > 0) {
module->native_symbol_list = wasm_runtime_malloc(cnt * sizeof(void *));
if (module->native_symbol_list == NULL) {
set_error_buf(error_buf, error_buf_size,
"malloc native symbol list failed");
goto fail;
}

for (uint32 i = 0; i < cnt; i++) {
read_string(p, p_end, symbol);
module->native_symbol_list[i] = get_native_symbol_by_name(symbol);
if (module->native_symbol_list[i] == NULL) {
set_error_buf_v(error_buf, error_buf_size,
"missing native symbol: %s", symbol);
goto fail;
}
}
}

return true;
fail:
return false;
}

static void
destroy_import_memories(AOTImportMemory *import_memories,
bool is_jit_mode)
Expand Down Expand Up @@ -1661,6 +1720,11 @@ load_from_sections(AOTModule *module, AOTSection *sections,
error_buf, error_buf_size))
return false;
break;
case AOT_SECTION_TYPE_NATIVE_SYMBOL:
if (!load_native_symbol_section(buf, buf_end, module,
error_buf, error_buf_size))
return false;
break;
default:
set_error_buf(error_buf, error_buf_size,
"invalid aot section type");
Expand All @@ -1670,7 +1734,7 @@ load_from_sections(AOTModule *module, AOTSection *sections,
section = section->next;
}

if (last_section_type != AOT_SECTION_TYPE_RELOCATION) {
if (last_section_type != AOT_SECTION_TYPE_NATIVE_SYMBOL) {
set_error_buf(error_buf, error_buf_size,
"section missing");
return false;
Expand Down Expand Up @@ -2232,6 +2296,9 @@ aot_unload(AOTModule *module)
module->mem_init_data_count,
module->is_jit_mode);

if (module->native_symbol_list)
wasm_runtime_free(module->native_symbol_list);

if (module->import_tables)
destroy_import_tables(module->import_tables,
module->is_jit_mode);
Expand Down
6 changes: 5 additions & 1 deletion core/iwasm/aot/aot_runtime.h
Original file line number Diff line number Diff line change
Expand Up @@ -38,11 +38,11 @@ typedef enum AOTExceptionID {
typedef enum AOTSectionType {
AOT_SECTION_TYPE_TARGET_INFO = 0,
AOT_SECTION_TYPE_INIT_DATA,
AOT_SECTION_TYPE_NATIVE_SYMBOL,
AOT_SECTION_TYPE_TEXT,
AOT_SECTION_TYPE_FUNCTION,
AOT_SECTION_TYPE_EXPORT,
AOT_SECTION_TYPE_RELOCATION,
AOT_SECTION_TYPE_NATIVE_SYMBOL,
AOT_SECTION_TYPE_SIGANATURE
} AOTSectionType;

Expand Down Expand Up @@ -101,6 +101,10 @@ typedef struct AOTModule {
uint32 mem_init_data_count;
AOTMemInitData **mem_init_data_list;

/* native symobl */
uint32 native_symbol_count;
void **native_symbol_list;

/* import tables */
uint32 import_table_count;
AOTImportTable *import_tables;
Expand Down
12 changes: 12 additions & 0 deletions core/iwasm/common/wasm_exec_env.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@
#include "wasm_exec_env.h"
#include "wasm_runtime_common.h"

#if WASM_ENABLE_AOT != 0
#include "aot_runtime.h"
#endif

#if WASM_ENABLE_THREAD_MGR != 0
#include "../libraries/thread-mgr/thread_manager.h"
#endif
Expand Down Expand Up @@ -44,6 +48,14 @@ wasm_exec_env_create_internal(struct WASMModuleInstanceCommon *module_inst,
exec_env->wasm_stack.s.bottom + stack_size;
exec_env->wasm_stack.s.top = exec_env->wasm_stack.s.bottom;

#if WASM_ENABLE_AOT != 0
if (module_inst->module_type == Wasm_Module_AoT) {
AOTModuleInstance *i = (AOTModuleInstance *)module_inst;
AOTModule *m = (AOTModule *)i->aot_module.ptr;
exec_env->native_symbol = m->native_symbol_list;
}
#endif

#if WASM_ENABLE_MEMORY_TRACING != 0
wasm_runtime_dump_exec_env_mem_consumption(exec_env);
#endif
Expand Down
7 changes: 5 additions & 2 deletions core/iwasm/common/wasm_exec_env.h
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,8 @@ typedef struct WASMExecEnv {
/* Previous thread's exec env of a WASM module instance. */
struct WASMExecEnv *prev;

/* Note: field module_inst, argv_buf and native_stack_boundary
are used by AOTed code, don't change the places of them */
/* Note: field module_inst, argv_buf, native_stack_boundary and
native_symbol are used by AOTed code, don't change the places of them */

/* The WASM module instance of current thread */
struct WASMModuleInstanceCommon *module_inst;
Expand All @@ -52,6 +52,9 @@ typedef struct WASMExecEnv {
exception. */
uint8 *native_stack_boundary;

/* Native symbol list*/
void **native_symbol;

#if WASM_ENABLE_THREAD_MGR != 0
/* Used to terminate or suspend the interpreter
bit 0: need terminate
Expand Down
14 changes: 7 additions & 7 deletions core/iwasm/compilation/aot_emit_aot_file.c
Original file line number Diff line number Diff line change
Expand Up @@ -769,11 +769,6 @@ get_aot_file_size(AOTCompContext *comp_ctx, AOTCompData *comp_data,
size += (uint32)sizeof(uint32) * 2;
size += get_init_data_section_size(comp_data, obj_data);

size = align_uint(size, 4);
/* section id + section size + symbol count */
size += (uint32)sizeof(uint32) * 3;
size += get_native_symbol_list_size(comp_ctx);

/* text section */
size = align_uint(size, 4);
/* section id + section size */
Expand All @@ -798,6 +793,11 @@ get_aot_file_size(AOTCompContext *comp_ctx, AOTCompData *comp_data,
size += (uint32)sizeof(uint32) * 2;
size += get_relocation_section_size(obj_data);

size = align_uint(size, 4);
/* section id + section size + symbol count */
size += (uint32)sizeof(uint32) * 3;
size += get_native_symbol_list_size(comp_ctx);

return size;
}

Expand Down Expand Up @@ -2200,11 +2200,11 @@ aot_emit_aot_file(AOTCompContext *comp_ctx, AOTCompData *comp_data,
if (!aot_emit_file_header(buf, buf_end, &offset, comp_data, obj_data)
|| !aot_emit_target_info_section(buf, buf_end, &offset, comp_data, obj_data)
|| !aot_emit_init_data_section(buf, buf_end, &offset, comp_ctx, comp_data, obj_data)
|| !aot_emit_native_symbol(buf, buf_end, &offset, comp_ctx)
|| !aot_emit_text_section(buf, buf_end, &offset, comp_data, obj_data)
|| !aot_emit_func_section(buf, buf_end, &offset, comp_data, obj_data)
|| !aot_emit_export_section(buf, buf_end, &offset, comp_data, obj_data)
|| !aot_emit_relocation_section(buf, buf_end, &offset, comp_data, obj_data))
|| !aot_emit_relocation_section(buf, buf_end, &offset, comp_data, obj_data)
|| !aot_emit_native_symbol(buf, buf_end, &offset, comp_ctx))
goto fail2;

#if 0
Expand Down

0 comments on commit cae61bf

Please sign in to comment.