Support emit specified custom sections into AoT file (#1207)

And add API to get the content of custom section with
section name for both wasm file and aot file.
This commit is contained in:
Xu Jun
2022-06-10 21:51:13 +08:00
committed by GitHub
parent d404107d85
commit 77595c9560
22 changed files with 420 additions and 31 deletions

View File

@ -877,11 +877,15 @@ get_native_symbol_list_size(AOTCompContext *comp_ctx)
static uint32
get_name_section_size(AOTCompData *comp_data);
static uint32
get_custom_sections_size(AOTCompContext *comp_ctx, AOTCompData *comp_data);
static uint32
get_aot_file_size(AOTCompContext *comp_ctx, AOTCompData *comp_data,
AOTObjectData *obj_data)
{
uint32 size = 0;
uint32 size_custom_section = 0;
/* aot file header */
size += get_file_header_size();
@ -939,6 +943,12 @@ get_aot_file_size(AOTCompContext *comp_ctx, AOTCompData *comp_data,
get_name_section_size(comp_data));
}
size_custom_section = get_custom_sections_size(comp_ctx, comp_data);
if (size_custom_section > 0) {
size = align_uint(size, 4);
size += size_custom_section;
}
return size;
}
@ -1274,6 +1284,36 @@ fail:
return 0;
}
static uint32
get_custom_sections_size(AOTCompContext *comp_ctx, AOTCompData *comp_data)
{
uint32 size = 0, i;
for (i = 0; i < comp_ctx->custom_sections_count; i++) {
const char *section_name = comp_ctx->custom_sections_wp[i];
const uint8 *content = NULL;
uint32 length = 0;
content = wasm_loader_get_custom_section(comp_data->wasm_module,
section_name, &length);
if (!content) {
LOG_WARNING("Can't find custom section [%s], ignore it",
section_name);
continue;
}
size = align_uint(size, 4);
/* section id + section size + sub section id */
size += (uint32)sizeof(uint32) * 3;
/* section name and len */
size += get_string_size(comp_ctx, section_name);
/* section content */
size += length;
}
return size;
}
static bool
aot_emit_file_header(uint8 *buf, uint8 *buf_end, uint32 *p_offset,
AOTCompData *comp_data, AOTObjectData *obj_data)
@ -1897,6 +1937,41 @@ aot_emit_name_section(uint8 *buf, uint8 *buf_end, uint32 *p_offset,
return true;
}
static bool
aot_emit_custom_sections(uint8 *buf, uint8 *buf_end, uint32 *p_offset,
AOTCompData *comp_data, AOTCompContext *comp_ctx)
{
uint32 offset = *p_offset, i;
for (i = 0; i < comp_ctx->custom_sections_count; i++) {
const char *section_name = comp_ctx->custom_sections_wp[i];
const uint8 *content = NULL;
uint32 length = 0;
content = wasm_loader_get_custom_section(comp_data->wasm_module,
section_name, &length);
if (!content) {
/* Warning has been reported during calculating size */
continue;
}
offset = align_uint(offset, 4);
EMIT_U32(AOT_SECTION_TYPE_CUSTOM);
/* sub section id + content */
EMIT_U32(sizeof(uint32) * 1 + get_string_size(comp_ctx, section_name)
+ length);
EMIT_U32(AOT_CUSTOM_SECTION_RAW);
EMIT_STR(section_name);
bh_memcpy_s((uint8 *)(buf + offset), (uint32)(buf_end - buf), content,
length);
offset += length;
}
*p_offset = offset;
return true;
}
typedef uint32 U32;
typedef int32 I32;
typedef uint16 U16;
@ -2751,7 +2826,9 @@ aot_emit_aot_file_buf(AOTCompContext *comp_ctx, AOTCompData *comp_data,
|| !aot_emit_relocation_section(buf, buf_end, &offset, comp_ctx,
comp_data, obj_data)
|| !aot_emit_native_symbol(buf, buf_end, &offset, comp_ctx)
|| !aot_emit_name_section(buf, buf_end, &offset, comp_data, comp_ctx))
|| !aot_emit_name_section(buf, buf_end, &offset, comp_data, comp_ctx)
|| !aot_emit_custom_sections(buf, buf_end, &offset, comp_data,
comp_ctx))
goto fail2;
#if 0

View File

@ -1587,6 +1587,9 @@ aot_create_comp_context(AOTCompData *comp_data, aot_comp_option_t option)
comp_ctx->opt_level = option->opt_level;
comp_ctx->size_level = option->size_level;
comp_ctx->custom_sections_wp = option->custom_sections;
comp_ctx->custom_sections_count = option->custom_sections_count;
if (option->is_jit_mode) {
char *triple_jit = NULL;

View File

@ -348,6 +348,8 @@ typedef struct AOTCompContext {
/* Function contexts */
AOTFuncContext **func_ctxes;
uint32 func_ctx_count;
char **custom_sections_wp;
uint32 custom_sections_count;
} AOTCompContext;
enum {
@ -378,6 +380,8 @@ typedef struct AOTCompOption {
uint32 size_level;
uint32 output_format;
uint32 bounds_checks;
char **custom_sections;
uint32 custom_sections_count;
} AOTCompOption, *aot_comp_option_t;
AOTCompContext *