diff options
Diffstat (limited to 'src/tools/wasm-link.c')
-rw-r--r-- | src/tools/wasm-link.c | 518 |
1 files changed, 259 insertions, 259 deletions
diff --git a/src/tools/wasm-link.c b/src/tools/wasm-link.c index 5f8ced18..07bd20c8 100644 --- a/src/tools/wasm-link.c +++ b/src/tools/wasm-link.c @@ -27,9 +27,9 @@ #include "binary-reader-linker.h" #define PROGRAM_NAME "wasm-link" -#define NOPE WASM_OPTION_NO_ARGUMENT -#define YEP WASM_OPTION_HAS_ARGUMENT -#define FIRST_KNOWN_SECTION WASM_BINARY_SECTION_TYPE +#define NOPE WABT_OPTION_NO_ARGUMENT +#define YEP WABT_OPTION_HAS_ARGUMENT +#define FIRST_KNOWN_SECTION WABT_BINARY_SECTION_TYPE enum { FLAG_VERBOSE, FLAG_OUTPUT, FLAG_RELOCATABLE, FLAG_HELP, NUM_FLAGS }; @@ -38,7 +38,7 @@ static const char s_description[] = "\n" " $ wasm-link m1.wasm m2.wasm -o out.wasm\n"; -static WasmOption s_options[] = { +static WabtOption s_options[] = { {FLAG_VERBOSE, 'v', "verbose", NULL, NOPE, "use multiple times for more info"}, {FLAG_OUTPUT, 'o', "output", "FILE", YEP, "output wasm binary file"}, @@ -46,33 +46,33 @@ static WasmOption s_options[] = { "output a relocatable object file"}, {FLAG_HELP, 'h', "help", NULL, NOPE, "print this help message"}, }; -WASM_STATIC_ASSERT(NUM_FLAGS == WASM_ARRAY_SIZE(s_options)); +WABT_STATIC_ASSERT(NUM_FLAGS == WABT_ARRAY_SIZE(s_options)); typedef const char* String; -WASM_DEFINE_VECTOR(string, String); +WABT_DEFINE_VECTOR(string, String); -static WasmBool s_verbose; -static WasmBool s_relocatable; +static WabtBool s_verbose; +static WabtBool s_relocatable; static const char* s_outfile = "a.wasm"; static StringVector s_infiles; -static WasmFileWriter s_log_stream_writer; -static WasmStream s_log_stream; +static WabtFileWriter s_log_stream_writer; +static WabtStream s_log_stream; typedef struct Context { - WasmStream stream; - WasmLinkerInputBinaryVector inputs; + WabtStream stream; + WabtLinkerInputBinaryVector inputs; ssize_t current_section_payload_offset; - WasmAllocator* allocator; + WabtAllocator* allocator; } Context; -static void on_option(struct WasmOptionParser* parser, - struct WasmOption* option, +static void on_option(struct WabtOptionParser* parser, + struct WabtOption* option, const char* argument) { switch (option->id) { case FLAG_VERBOSE: s_verbose++; - wasm_init_file_writer_existing(&s_log_stream_writer, stdout); - wasm_init_stream(&s_log_stream, &s_log_stream_writer.base, NULL); + wabt_init_file_writer_existing(&s_log_stream_writer, stdout); + wabt_init_stream(&s_log_stream, &s_log_stream_writer.base, NULL); break; case FLAG_OUTPUT: @@ -80,83 +80,83 @@ static void on_option(struct WasmOptionParser* parser, break; case FLAG_RELOCATABLE: - s_relocatable = WASM_TRUE; + s_relocatable = WABT_TRUE; break; case FLAG_HELP: - wasm_print_help(parser, PROGRAM_NAME); + wabt_print_help(parser, PROGRAM_NAME); exit(0); break; } } -static void on_argument(struct WasmOptionParser* parser, const char* argument) { - WasmAllocator* allocator = parser->user_data; - wasm_append_string_value(allocator, &s_infiles, &argument); +static void on_argument(struct WabtOptionParser* parser, const char* argument) { + WabtAllocator* allocator = parser->user_data; + wabt_append_string_value(allocator, &s_infiles, &argument); } -static void on_option_error(struct WasmOptionParser* parser, +static void on_option_error(struct WabtOptionParser* parser, const char* message) { - WASM_FATAL("%s\n", message); + WABT_FATAL("%s\n", message); } -static void parse_options(WasmAllocator* allocator, int argc, char** argv) { - WasmOptionParser parser; - WASM_ZERO_MEMORY(parser); +static void parse_options(WabtAllocator* allocator, int argc, char** argv) { + WabtOptionParser parser; + WABT_ZERO_MEMORY(parser); parser.description = s_description; parser.options = s_options; - parser.num_options = WASM_ARRAY_SIZE(s_options); + parser.num_options = WABT_ARRAY_SIZE(s_options); parser.on_option = on_option; parser.on_argument = on_argument; parser.on_error = on_option_error; parser.user_data = allocator; - wasm_parse_options(&parser, argc, argv); + wabt_parse_options(&parser, argc, argv); if (!s_infiles.size) { - wasm_print_help(&parser, PROGRAM_NAME); - WASM_FATAL("No inputs files specified.\n"); + wabt_print_help(&parser, PROGRAM_NAME); + WABT_FATAL("No inputs files specified.\n"); } } -void wasm_destroy_section(WasmAllocator* allocator, WasmSection* section) { - wasm_destroy_reloc_vector(allocator, §ion->relocations); +void wabt_destroy_section(WabtAllocator* allocator, WabtSection* section) { + wabt_destroy_reloc_vector(allocator, §ion->relocations); switch (section->section_code) { - case WASM_BINARY_SECTION_DATA: - wasm_destroy_data_segment_vector(allocator, §ion->data_segments); + case WABT_BINARY_SECTION_DATA: + wabt_destroy_data_segment_vector(allocator, §ion->data_segments); break; default: break; } } -void wasm_destroy_binary(WasmAllocator* allocator, - WasmLinkerInputBinary* binary) { - WASM_DESTROY_VECTOR_AND_ELEMENTS(allocator, binary->sections, section); - wasm_destroy_function_import_vector(allocator, &binary->function_imports); - wasm_destroy_global_import_vector(allocator, &binary->global_imports); - wasm_destroy_string_slice_vector(allocator, &binary->debug_names); - wasm_destroy_export_vector(allocator, &binary->exports); - wasm_free(allocator, binary->data); +void wabt_destroy_binary(WabtAllocator* allocator, + WabtLinkerInputBinary* binary) { + WABT_DESTROY_VECTOR_AND_ELEMENTS(allocator, binary->sections, section); + wabt_destroy_function_import_vector(allocator, &binary->function_imports); + wabt_destroy_global_import_vector(allocator, &binary->global_imports); + wabt_destroy_string_slice_vector(allocator, &binary->debug_names); + wabt_destroy_export_vector(allocator, &binary->exports); + wabt_free(allocator, binary->data); } -static uint32_t relocate_func_index(WasmLinkerInputBinary* binary, +static uint32_t relocate_func_index(WabtLinkerInputBinary* binary, uint32_t function_index) { uint32_t offset; if (function_index >= binary->function_imports.size) { /* locally declared function call */ offset = binary->function_index_offset; if (s_verbose) - wasm_writef(&s_log_stream, "func reloc %d + %d\n", function_index, + wabt_writef(&s_log_stream, "func reloc %d + %d\n", function_index, offset); } else { /* imported function call */ - WasmFunctionImport* import = &binary->function_imports.data[function_index]; + WabtFunctionImport* import = &binary->function_imports.data[function_index]; offset = binary->imported_function_index_offset; if (!import->active) { function_index = import->foreign_index; offset = import->foreign_binary->function_index_offset; if (s_verbose) - wasm_writef(&s_log_stream, + wabt_writef(&s_log_stream, "reloc for disabled import. new index = %d + %d\n", function_index, offset); } @@ -164,27 +164,27 @@ static uint32_t relocate_func_index(WasmLinkerInputBinary* binary, return function_index + offset; } -static void apply_relocation(WasmSection* section, WasmReloc* r) { - WasmLinkerInputBinary* binary = section->binary; +static void apply_relocation(WabtSection* section, WabtReloc* r) { + WabtLinkerInputBinary* binary = section->binary; uint8_t* section_data = &binary->data[section->offset]; size_t section_size = section->size; uint32_t cur_value = 0, new_value = 0; - wasm_read_u32_leb128(section_data + r->offset, section_data + section_size, + wabt_read_u32_leb128(section_data + r->offset, section_data + section_size, &cur_value); uint32_t offset = 0; switch (r->type) { - case WASM_RELOC_FUNC_INDEX_LEB: + case WABT_RELOC_FUNC_INDEX_LEB: new_value = relocate_func_index(binary, cur_value); break; - case WASM_RELOC_TABLE_INDEX_SLEB: + case WABT_RELOC_TABLE_INDEX_SLEB: printf("%s: table index reloc: %d offset=%d\n", binary->filename, cur_value, binary->table_index_offset); offset = binary->table_index_offset; new_value = cur_value + offset; break; - case WASM_RELOC_GLOBAL_INDEX_LEB: + case WABT_RELOC_GLOBAL_INDEX_LEB: if (cur_value >= binary->global_imports.size) { offset = binary->global_index_offset; } else { @@ -193,82 +193,82 @@ static void apply_relocation(WasmSection* section, WasmReloc* r) { new_value = cur_value + offset; break; default: - WASM_FATAL("unhandled relocation type: %s\n", - wasm_get_reloc_type_name(r->type)); + WABT_FATAL("unhandled relocation type: %s\n", + wabt_get_reloc_type_name(r->type)); break; } - wasm_write_fixed_u32_leb128_raw(section_data + r->offset, + wabt_write_fixed_u32_leb128_raw(section_data + r->offset, section_data + section_size, new_value); } -static void apply_relocations(WasmSection* section) { +static void apply_relocations(WabtSection* section) { if (!section->relocations.size) return; if (s_verbose) - wasm_writef(&s_log_stream, "apply_relocations: %s\n", - wasm_get_section_name(section->section_code)); + wabt_writef(&s_log_stream, "apply_relocations: %s\n", + wabt_get_section_name(section->section_code)); /* Perform relocations in-place */ size_t i; for (i = 0; i < section->relocations.size; i++) { - WasmReloc* reloc = §ion->relocations.data[i]; + WabtReloc* reloc = §ion->relocations.data[i]; apply_relocation(section, reloc); } } -static void write_section_payload(Context* ctx, WasmSection* sec) { +static void write_section_payload(Context* ctx, WabtSection* sec) { assert(ctx->current_section_payload_offset != -1); sec->output_payload_offset = ctx->stream.offset - ctx->current_section_payload_offset; uint8_t* payload = &sec->binary->data[sec->payload_offset]; - wasm_write_data(&ctx->stream, payload, sec->payload_size, "section content"); + wabt_write_data(&ctx->stream, payload, sec->payload_size, "section content"); } -static void write_c_str(WasmStream* stream, const char* str, const char* desc) { - wasm_write_str(stream, str, strlen(str), WASM_PRINT_CHARS, desc); +static void write_c_str(WabtStream* stream, const char* str, const char* desc) { + wabt_write_str(stream, str, strlen(str), WABT_PRINT_CHARS, desc); } -static void write_slice(WasmStream* stream, - WasmStringSlice str, +static void write_slice(WabtStream* stream, + WabtStringSlice str, const char* desc) { - wasm_write_str(stream, str.start, str.length, WASM_PRINT_CHARS, desc); + wabt_write_str(stream, str.start, str.length, WABT_PRINT_CHARS, desc); } #define WRITE_UNKNOWN_SIZE(STREAM) \ uint32_t fixup_offset = (STREAM)->offset; \ - wasm_write_fixed_u32_leb128(STREAM, 0, "unknown size"); \ + wabt_write_fixed_u32_leb128(STREAM, 0, "unknown size"); \ ctx->current_section_payload_offset = (STREAM)->offset; \ uint32_t start = (STREAM)->offset; #define FIXUP_SIZE(STREAM) \ - wasm_write_fixed_u32_leb128_at(STREAM, fixup_offset, \ + wabt_write_fixed_u32_leb128_at(STREAM, fixup_offset, \ (STREAM)->offset - start, "fixup size"); static void write_table_section(Context* ctx, - const WasmSectionPtrVector* sections) { + const WabtSectionPtrVector* sections) { /* Total section size includes the element count leb128 which is * always 1 in the current spec */ uint32_t table_count = 1; - uint32_t flags = WASM_BINARY_LIMITS_HAS_MAX_FLAG; + uint32_t flags = WABT_BINARY_LIMITS_HAS_MAX_FLAG; uint32_t elem_count = 0; size_t i; for (i = 0; i < sections->size; i++) { - WasmSection* sec = sections->data[i]; + WabtSection* sec = sections->data[i]; elem_count += sec->binary->table_elem_count; } - WasmStream* stream = &ctx->stream; + WabtStream* stream = &ctx->stream; WRITE_UNKNOWN_SIZE(stream); - wasm_write_u32_leb128(stream, table_count, "table count"); - wasm_write_type(stream, WASM_TYPE_ANYFUNC); - wasm_write_u32_leb128(stream, flags, "table elem flags"); - wasm_write_u32_leb128(stream, elem_count, "table initial length"); - wasm_write_u32_leb128(stream, elem_count, "table max length"); + wabt_write_u32_leb128(stream, table_count, "table count"); + wabt_write_type(stream, WABT_TYPE_ANYFUNC); + wabt_write_u32_leb128(stream, flags, "table elem flags"); + wabt_write_u32_leb128(stream, elem_count, "table initial length"); + wabt_write_u32_leb128(stream, elem_count, "table max length"); FIXUP_SIZE(stream); } @@ -276,58 +276,58 @@ static void write_export_section(Context* ctx) { size_t i, j; uint32_t total_exports = 0; for (i = 0; i < ctx->inputs.size; i++) { - WasmLinkerInputBinary* binary = &ctx->inputs.data[i]; + WabtLinkerInputBinary* binary = &ctx->inputs.data[i]; total_exports += binary->exports.size; } - WasmStream* stream = &ctx->stream; + WabtStream* stream = &ctx->stream; WRITE_UNKNOWN_SIZE(stream); - wasm_write_u32_leb128(stream, total_exports, "export count"); + wabt_write_u32_leb128(stream, total_exports, "export count"); for (i = 0; i < ctx->inputs.size; i++) { - WasmLinkerInputBinary* binary = &ctx->inputs.data[i]; + WabtLinkerInputBinary* binary = &ctx->inputs.data[i]; for (j = 0; j < binary->exports.size; j++) { - WasmExport* export = &binary->exports.data[j]; + WabtExport* export = &binary->exports.data[j]; write_slice(stream, export->name, "export name"); - wasm_write_u8(stream, export->kind, "export kind"); + wabt_write_u8(stream, export->kind, "export kind"); uint32_t index = export->index; switch (export->kind) { - case WASM_EXTERNAL_KIND_FUNC: + case WABT_EXTERNAL_KIND_FUNC: index = relocate_func_index(binary, index); break; default: - WASM_FATAL("unsupport export type: %d\n", export->kind); + WABT_FATAL("unsupport export type: %d\n", export->kind); break; } - wasm_write_u32_leb128(stream, index, "export index"); + wabt_write_u32_leb128(stream, index, "export index"); } } FIXUP_SIZE(stream); } static void write_elem_section(Context* ctx, - const WasmSectionPtrVector* sections) { - WasmStream* stream = &ctx->stream; + const WabtSectionPtrVector* sections) { + WabtStream* stream = &ctx->stream; WRITE_UNKNOWN_SIZE(stream); size_t i; uint32_t total_elem_count = 0; for (i = 0; i < sections->size; i++) { - WasmSection* sec = sections->data[i]; + WabtSection* sec = sections->data[i]; total_elem_count += sec->binary->table_elem_count; } - wasm_write_u32_leb128(stream, 1, "segment count"); - wasm_write_u32_leb128(stream, 0, "table index"); - wasm_write_opcode(&ctx->stream, WASM_OPCODE_I32_CONST); - wasm_write_i32_leb128(&ctx->stream, 0, "elem init literal"); - wasm_write_opcode(&ctx->stream, WASM_OPCODE_END); - wasm_write_u32_leb128(stream, total_elem_count, "num elements"); + wabt_write_u32_leb128(stream, 1, "segment count"); + wabt_write_u32_leb128(stream, 0, "table index"); + wabt_write_opcode(&ctx->stream, WABT_OPCODE_I32_CONST); + wabt_write_i32_leb128(&ctx->stream, 0, "elem init literal"); + wabt_write_opcode(&ctx->stream, WABT_OPCODE_END); + wabt_write_u32_leb128(stream, total_elem_count, "num elements"); ctx->current_section_payload_offset = stream->offset; for (i = 0; i < sections->size; i++) { - WasmSection* sec = sections->data[i]; + WabtSection* sec = sections->data[i]; apply_relocations(sec); write_section_payload(ctx, sec); } @@ -336,52 +336,52 @@ static void write_elem_section(Context* ctx, } static void write_memory_section(Context* ctx, - const WasmSectionPtrVector* sections) { - WasmStream* stream = &ctx->stream; + const WabtSectionPtrVector* sections) { + WabtStream* stream = &ctx->stream; WRITE_UNKNOWN_SIZE(stream); - wasm_write_u32_leb128(stream, 1, "memory count"); + wabt_write_u32_leb128(stream, 1, "memory count"); - WasmLimits limits; - WASM_ZERO_MEMORY(limits); - limits.has_max = WASM_TRUE; + WabtLimits limits; + WABT_ZERO_MEMORY(limits); + limits.has_max = WABT_TRUE; size_t i; for (i = 0; i < sections->size; i++) { - WasmSection* sec = sections->data[i]; + WabtSection* sec = sections->data[i]; limits.initial += sec->memory_limits.initial; } limits.max = limits.initial; - wasm_write_limits(stream, &limits); + wabt_write_limits(stream, &limits); FIXUP_SIZE(stream); } static void write_function_import(Context* ctx, - WasmFunctionImport* import, + WabtFunctionImport* import, uint32_t offset) { - write_c_str(&ctx->stream, WASM_LINK_MODULE_NAME, "import module name"); + write_c_str(&ctx->stream, WABT_LINK_MODULE_NAME, "import module name"); write_slice(&ctx->stream, import->name, "import field name"); - wasm_write_u8(&ctx->stream, WASM_EXTERNAL_KIND_FUNC, "import kind"); - wasm_write_u32_leb128(&ctx->stream, import->sig_index + offset, + wabt_write_u8(&ctx->stream, WABT_EXTERNAL_KIND_FUNC, "import kind"); + wabt_write_u32_leb128(&ctx->stream, import->sig_index + offset, "import signature index"); } -static void write_global_import(Context* ctx, WasmGlobalImport* import) { - write_c_str(&ctx->stream, WASM_LINK_MODULE_NAME, "import module name"); +static void write_global_import(Context* ctx, WabtGlobalImport* import) { + write_c_str(&ctx->stream, WABT_LINK_MODULE_NAME, "import module name"); write_slice(&ctx->stream, import->name, "import field name"); - wasm_write_u8(&ctx->stream, WASM_EXTERNAL_KIND_GLOBAL, "import kind"); - wasm_write_type(&ctx->stream, import->type); - wasm_write_u8(&ctx->stream, import->mutable, "global mutability"); + wabt_write_u8(&ctx->stream, WABT_EXTERNAL_KIND_GLOBAL, "import kind"); + wabt_write_type(&ctx->stream, import->type); + wabt_write_u8(&ctx->stream, import->mutable, "global mutability"); } static void write_import_section(Context* ctx) { uint32_t num_imports = 0; size_t i, j; for (i = 0; i < ctx->inputs.size; i++) { - WasmLinkerInputBinary* binary = &ctx->inputs.data[i]; - WasmFunctionImportVector* imports = &binary->function_imports; + WabtLinkerInputBinary* binary = &ctx->inputs.data[i]; + WabtFunctionImportVector* imports = &binary->function_imports; for (j = 0; j < imports->size; j++) { - WasmFunctionImport* import = &imports->data[j]; + WabtFunctionImport* import = &imports->data[j]; if (import->active) num_imports++; } @@ -389,18 +389,18 @@ static void write_import_section(Context* ctx) { } WRITE_UNKNOWN_SIZE(&ctx->stream); - wasm_write_u32_leb128(&ctx->stream, num_imports, "num imports"); + wabt_write_u32_leb128(&ctx->stream, num_imports, "num imports"); for (i = 0; i < ctx->inputs.size; i++) { - WasmLinkerInputBinary* binary = &ctx->inputs.data[i]; - WasmFunctionImportVector* imports = &binary->function_imports; + WabtLinkerInputBinary* binary = &ctx->inputs.data[i]; + WabtFunctionImportVector* imports = &binary->function_imports; for (j = 0; j < imports->size; j++) { - WasmFunctionImport* import = &imports->data[j]; + WabtFunctionImport* import = &imports->data[j]; if (import->active) write_function_import(ctx, import, binary->type_index_offset); } - WasmGlobalImportVector* globals = &binary->global_imports; + WabtGlobalImportVector* globals = &binary->global_imports; for (j = 0; j < globals->size; j++) { write_global_import(ctx, &globals->data[j]); } @@ -410,16 +410,16 @@ static void write_import_section(Context* ctx) { } static void write_function_section(Context* ctx, - const WasmSectionPtrVector* sections, + const WabtSectionPtrVector* sections, uint32_t total_count) { - WasmStream* stream = &ctx->stream; + WabtStream* stream = &ctx->stream; WRITE_UNKNOWN_SIZE(stream); - wasm_write_u32_leb128(stream, total_count, "function count"); + wabt_write_u32_leb128(stream, total_count, "function count"); size_t i; for (i = 0; i < sections->size; i++) { - WasmSection* sec = sections->data[i]; + WabtSection* sec = sections->data[i]; uint32_t count = sec->count; uint32_t input_offset = 0; uint32_t sig_index = 0; @@ -428,41 +428,41 @@ static void write_function_section(Context* ctx, &sec->binary->data[sec->payload_offset + sec->payload_size]; while (count--) { input_offset += - wasm_read_u32_leb128(start + input_offset, end, &sig_index); + wabt_read_u32_leb128(start + input_offset, end, &sig_index); sig_index += sec->binary->type_index_offset; - wasm_write_u32_leb128(stream, sig_index, "sig"); + wabt_write_u32_leb128(stream, sig_index, "sig"); } } FIXUP_SIZE(stream); } -static void write_data_segment(WasmStream* stream, - WasmDataSegment* segment, +static void write_data_segment(WabtStream* stream, + WabtDataSegment* segment, uint32_t offset) { assert(segment->memory_index == 0); - wasm_write_u32_leb128(stream, segment->memory_index, "memory index"); - wasm_write_opcode(stream, WASM_OPCODE_I32_CONST); - wasm_write_u32_leb128(stream, segment->offset + offset, "offset"); - wasm_write_opcode(stream, WASM_OPCODE_END); - wasm_write_u32_leb128(stream, segment->size, "segment size"); - wasm_write_data(stream, segment->data, segment->size, "segment data"); + wabt_write_u32_leb128(stream, segment->memory_index, "memory index"); + wabt_write_opcode(stream, WABT_OPCODE_I32_CONST); + wabt_write_u32_leb128(stream, segment->offset + offset, "offset"); + wabt_write_opcode(stream, WABT_OPCODE_END); + wabt_write_u32_leb128(stream, segment->size, "segment size"); + wabt_write_data(stream, segment->data, segment->size, "segment data"); } static void write_data_section(Context* ctx, - WasmSectionPtrVector* sections, + WabtSectionPtrVector* sections, uint32_t total_count) { - WasmStream* stream = &ctx->stream; + WabtStream* stream = &ctx->stream; WRITE_UNKNOWN_SIZE(stream); - wasm_write_u32_leb128(stream, total_count, "data segment count"); + wabt_write_u32_leb128(stream, total_count, "data segment count"); size_t i, j; for (i = 0; i < sections->size; i++) { - WasmSection* sec = sections->data[i]; + WabtSection* sec = sections->data[i]; for (j = 0; j < sec->data_segments.size; j++) { - WasmDataSegment* segment = &sec->data_segments.data[j]; + WabtDataSegment* segment = &sec->data_segments.data[j]; write_data_segment(stream, segment, - sec->binary->memory_page_offset * WASM_PAGE_SIZE); + sec->binary->memory_page_offset * WABT_PAGE_SIZE); } } @@ -473,7 +473,7 @@ static void write_names_section(Context* ctx) { uint32_t total_count = 0; size_t i, j; for (i = 0; i < ctx->inputs.size; i++) { - WasmLinkerInputBinary* binary = &ctx->inputs.data[i]; + WabtLinkerInputBinary* binary = &ctx->inputs.data[i]; for (j = 0; j < binary->debug_names.size; j++) { if (j < binary->function_imports.size) { if (!binary->function_imports.data[j].active) @@ -486,21 +486,21 @@ static void write_names_section(Context* ctx) { if (!total_count) return; - WasmStream* stream = &ctx->stream; - wasm_write_u8(stream, WASM_BINARY_SECTION_CUSTOM, "section code"); + WabtStream* stream = &ctx->stream; + wabt_write_u8(stream, WABT_BINARY_SECTION_CUSTOM, "section code"); WRITE_UNKNOWN_SIZE(stream); write_c_str(stream, "name", "custom section name"); - wasm_write_u32_leb128(stream, total_count, "element count"); + wabt_write_u32_leb128(stream, total_count, "element count"); for (i = 0; i < ctx->inputs.size; i++) { - WasmLinkerInputBinary* binary = &ctx->inputs.data[i]; + WabtLinkerInputBinary* binary = &ctx->inputs.data[i]; for (j = 0; j < binary->debug_names.size; j++) { if (j < binary->function_imports.size) { if (!binary->function_imports.data[j].active) continue; } write_slice(stream, binary->debug_names.data[j], "function name"); - wasm_write_u32_leb128(stream, 0, "local name count"); + wabt_write_u32_leb128(stream, 0, "local name count"); } } @@ -508,14 +508,14 @@ static void write_names_section(Context* ctx) { } static void write_reloc_section(Context* ctx, - WasmBinarySection section_code, - WasmSectionPtrVector* sections) { + WabtBinarySection section_code, + WabtSectionPtrVector* sections) { size_t i, j; uint32_t total_relocs = 0; /* First pass to know total reloc count */ for (i = 0; i < sections->size; i++) { - WasmSection* sec = sections->data[i]; + WabtSection* sec = sections->data[i]; total_relocs += sec->relocations.size; } @@ -523,38 +523,38 @@ static void write_reloc_section(Context* ctx, return; char section_name[128]; - wasm_snprintf(section_name, sizeof(section_name), "%s.%s", - WASM_BINARY_SECTION_RELOC, wasm_get_section_name(section_code)); + wabt_snprintf(section_name, sizeof(section_name), "%s.%s", + WABT_BINARY_SECTION_RELOC, wabt_get_section_name(section_code)); - WasmStream* stream = &ctx->stream; - wasm_write_u8(stream, WASM_BINARY_SECTION_CUSTOM, "section code"); + WabtStream* stream = &ctx->stream; + wabt_write_u8(stream, WABT_BINARY_SECTION_CUSTOM, "section code"); WRITE_UNKNOWN_SIZE(stream); write_c_str(stream, section_name, "reloc section name"); - wasm_write_u32_leb128(&ctx->stream, section_code, "reloc section"); - wasm_write_u32_leb128(&ctx->stream, total_relocs, "num relocs"); + wabt_write_u32_leb128(&ctx->stream, section_code, "reloc section"); + wabt_write_u32_leb128(&ctx->stream, total_relocs, "num relocs"); for (i = 0; i < sections->size; i++) { - WasmSection* sec = sections->data[i]; - WasmRelocVector* relocs = &sec->relocations; + WabtSection* sec = sections->data[i]; + WabtRelocVector* relocs = &sec->relocations; for (j = 0; j < relocs->size; j++) { - wasm_write_u32_leb128(&ctx->stream, relocs->data[j].type, "reloc type"); + wabt_write_u32_leb128(&ctx->stream, relocs->data[j].type, "reloc type"); uint32_t new_offset = relocs->data[j].offset + sec->output_payload_offset; - wasm_write_u32_leb128(&ctx->stream, new_offset, "reloc offset"); + wabt_write_u32_leb128(&ctx->stream, new_offset, "reloc offset"); } } FIXUP_SIZE(stream); } -static WasmBool write_combined_section(Context* ctx, - WasmBinarySection section_code, - WasmSectionPtrVector* sections) { +static WabtBool write_combined_section(Context* ctx, + WabtBinarySection section_code, + WabtSectionPtrVector* sections) { if (!sections->size) - return WASM_FALSE; + return WABT_FALSE; - if (section_code == WASM_BINARY_SECTION_START && sections->size > 1) { - WASM_FATAL("Don't know how to combine sections of type: %s\n", - wasm_get_section_name(section_code)); + if (section_code == WABT_BINARY_SECTION_START && sections->size > 1) { + WABT_FATAL("Don't know how to combine sections of type: %s\n", + wabt_get_section_name(section_code)); } size_t i; @@ -563,83 +563,83 @@ static WasmBool write_combined_section(Context* ctx, /* Sum section size and element count */ for (i = 0; i < sections->size; i++) { - WasmSection* sec = sections->data[i]; + WabtSection* sec = sections->data[i]; total_size += sec->payload_size; total_count += sec->count; } - wasm_write_u8(&ctx->stream, section_code, "section code"); + wabt_write_u8(&ctx->stream, section_code, "section code"); ctx->current_section_payload_offset = -1; switch (section_code) { - case WASM_BINARY_SECTION_IMPORT: + case WABT_BINARY_SECTION_IMPORT: write_import_section(ctx); break; - case WASM_BINARY_SECTION_FUNCTION: + case WABT_BINARY_SECTION_FUNCTION: write_function_section(ctx, sections, total_count); break; - case WASM_BINARY_SECTION_TABLE: + case WABT_BINARY_SECTION_TABLE: write_table_section(ctx, sections); break; - case WASM_BINARY_SECTION_EXPORT: + case WABT_BINARY_SECTION_EXPORT: write_export_section(ctx); break; - case WASM_BINARY_SECTION_ELEM: + case WABT_BINARY_SECTION_ELEM: write_elem_section(ctx, sections); break; - case WASM_BINARY_SECTION_MEMORY: + case WABT_BINARY_SECTION_MEMORY: write_memory_section(ctx, sections); break; - case WASM_BINARY_SECTION_DATA: + case WABT_BINARY_SECTION_DATA: write_data_section(ctx, sections, total_count); break; default: { /* Total section size includes the element count leb128. */ - total_size += wasm_u32_leb128_length(total_count); + total_size += wabt_u32_leb128_length(total_count); /* Write section to stream */ - WasmStream* stream = &ctx->stream; - wasm_write_u32_leb128(stream, total_size, "section size"); - wasm_write_u32_leb128(stream, total_count, "element count"); + WabtStream* stream = &ctx->stream; + wabt_write_u32_leb128(stream, total_size, "section size"); + wabt_write_u32_leb128(stream, total_count, "element count"); ctx->current_section_payload_offset = ctx->stream.offset; for (i = 0; i < sections->size; i++) { - WasmSection* sec = sections->data[i]; + WabtSection* sec = sections->data[i]; apply_relocations(sec); write_section_payload(ctx, sec); } } } - return WASM_TRUE; + return WABT_TRUE; } typedef struct ExportInfo { - WasmExport* export; - WasmLinkerInputBinary* binary; + WabtExport* export; + WabtLinkerInputBinary* binary; } ExportInfo; -WASM_DEFINE_VECTOR(export_info, ExportInfo); +WABT_DEFINE_VECTOR(export_info, ExportInfo); static void resolve_symbols(Context* ctx) { /* Create hashmap of all exported symbols from all inputs */ - WasmBindingHash export_map; - WASM_ZERO_MEMORY(export_map); + WabtBindingHash export_map; + WABT_ZERO_MEMORY(export_map); ExportInfoVector export_list; - WASM_ZERO_MEMORY(export_list); + WABT_ZERO_MEMORY(export_list); size_t i, j; for (i = 0; i < ctx->inputs.size; i++) { - WasmLinkerInputBinary* binary = &ctx->inputs.data[i]; + WabtLinkerInputBinary* binary = &ctx->inputs.data[i]; for (j = 0; j < binary->exports.size; j++) { - WasmExport* export = &binary->exports.data[j]; - ExportInfo* info = wasm_append_export_info(ctx->allocator, &export_list); + WabtExport* export = &binary->exports.data[j]; + ExportInfo* info = wabt_append_export_info(ctx->allocator, &export_list); info->export = export; info->binary = binary; /* TODO(sbc): Handle duplicate names */ - WasmStringSlice name = - wasm_dup_string_slice(ctx->allocator, export->name); - WasmBinding* binding = - wasm_insert_binding(ctx->allocator, &export_map, &name); + WabtStringSlice name = + wabt_dup_string_slice(ctx->allocator, export->name); + WabtBinding* binding = + wabt_insert_binding(ctx->allocator, &export_map, &name); binding->index = export_list.size - 1; } } @@ -649,15 +649,15 @@ static void resolve_symbols(Context* ctx) { * ones. */ for (i = 0; i < ctx->inputs.size; i++) { - WasmLinkerInputBinary* binary = &ctx->inputs.data[i]; + WabtLinkerInputBinary* binary = &ctx->inputs.data[i]; for (j = 0; j < binary->function_imports.size; j++) { - WasmFunctionImport* import = &binary->function_imports.data[j]; + WabtFunctionImport* import = &binary->function_imports.data[j]; int export_index = - wasm_find_binding_index_by_name(&export_map, &import->name); + wabt_find_binding_index_by_name(&export_map, &import->name); if (export_index == -1) { if (!s_relocatable) - WASM_FATAL("undefined symbol: " PRIstringslice "\n", - WASM_PRINTF_STRING_SLICE_ARG(import->name)); + WABT_FATAL("undefined symbol: " PRIstringslice "\n", + WABT_PRINTF_STRING_SLICE_ARG(import->name)); continue; } @@ -665,15 +665,15 @@ static void resolve_symbols(Context* ctx) { ExportInfo* export_info = &export_list.data[export_index]; /* TODO(sbc): verify the foriegn function has the correct signature */ - import->active = WASM_FALSE; + import->active = WABT_FALSE; import->foreign_binary = export_info->binary; import->foreign_index = export_info->export->index; binary->active_function_imports--; } } - wasm_destroy_export_info_vector(ctx->allocator, &export_list); - wasm_destroy_binding_hash(ctx->allocator, &export_map); + wabt_destroy_export_info_vector(ctx->allocator, &export_list); + wabt_destroy_binding_hash(ctx->allocator, &export_map); } static void calculate_reloc_offsets(Context* ctx) { @@ -686,7 +686,7 @@ static void calculate_reloc_offsets(Context* ctx) { uint32_t total_function_imports = 0; uint32_t total_global_imports = 0; for (i = 0; i < ctx->inputs.size; i++) { - WasmLinkerInputBinary* binary = &ctx->inputs.data[i]; + WabtLinkerInputBinary* binary = &ctx->inputs.data[i]; /* The imported_function_index_offset is the sum of all the function * imports from objects that precede this one. i.e. the current running * total */ @@ -699,23 +699,23 @@ static void calculate_reloc_offsets(Context* ctx) { } for (i = 0; i < ctx->inputs.size; i++) { - WasmLinkerInputBinary* binary = &ctx->inputs.data[i]; + WabtLinkerInputBinary* binary = &ctx->inputs.data[i]; binary->table_index_offset = table_elem_count; table_elem_count += binary->table_elem_count; for (j = 0; j < binary->sections.size; j++) { - WasmSection* sec = &binary->sections.data[j]; + WabtSection* sec = &binary->sections.data[j]; switch (sec->section_code) { - case WASM_BINARY_SECTION_TYPE: + case WABT_BINARY_SECTION_TYPE: binary->type_index_offset = type_count; type_count += sec->count; break; - case WASM_BINARY_SECTION_GLOBAL: + case WABT_BINARY_SECTION_GLOBAL: binary->global_index_offset = total_global_imports - sec->binary->global_imports.size + global_count; global_count += sec->count; break; - case WASM_BINARY_SECTION_FUNCTION: + case WABT_BINARY_SECTION_FUNCTION: binary->function_index_offset = total_function_imports - sec->binary->function_imports.size + function_count; @@ -730,37 +730,37 @@ static void calculate_reloc_offsets(Context* ctx) { static void write_binary(Context* ctx) { /* Find all the sections of each type */ - WasmSectionPtrVector sections[WASM_NUM_BINARY_SECTIONS]; - WASM_ZERO_MEMORY(sections); + WabtSectionPtrVector sections[WABT_NUM_BINARY_SECTIONS]; + WABT_ZERO_MEMORY(sections); size_t i, j; for (j = 0; j < ctx->inputs.size; j++) { - WasmLinkerInputBinary* binary = &ctx->inputs.data[j]; + WabtLinkerInputBinary* binary = &ctx->inputs.data[j]; for (i = 0; i < binary->sections.size; i++) { - WasmSection* s = &binary->sections.data[i]; - WasmSectionPtrVector* sec_list = §ions[s->section_code]; - wasm_append_section_ptr_value(ctx->allocator, sec_list, &s); + WabtSection* s = &binary->sections.data[i]; + WabtSectionPtrVector* sec_list = §ions[s->section_code]; + wabt_append_section_ptr_value(ctx->allocator, sec_list, &s); } } /* Write the final binary */ - wasm_write_u32(&ctx->stream, WASM_BINARY_MAGIC, "WASM_BINARY_MAGIC"); - wasm_write_u32(&ctx->stream, WASM_BINARY_VERSION, "WASM_BINARY_VERSION"); + wabt_write_u32(&ctx->stream, WABT_BINARY_MAGIC, "WABT_BINARY_MAGIC"); + wabt_write_u32(&ctx->stream, WABT_BINARY_VERSION, "WABT_BINARY_VERSION"); /* Write known sections first */ - for (i = FIRST_KNOWN_SECTION; i < WASM_NUM_BINARY_SECTIONS; i++) { + for (i = FIRST_KNOWN_SECTION; i < WABT_NUM_BINARY_SECTIONS; i++) { write_combined_section(ctx, i, §ions[i]); } write_names_section(ctx); /* Generate a new set of reloction sections */ - for (i = FIRST_KNOWN_SECTION; i < WASM_NUM_BINARY_SECTIONS; i++) { + for (i = FIRST_KNOWN_SECTION; i < WABT_NUM_BINARY_SECTIONS; i++) { write_reloc_section(ctx, i, §ions[i]); } - for (i = 0; i < WASM_NUM_BINARY_SECTIONS; i++) { - wasm_destroy_section_ptr_vector(ctx->allocator, §ions[i]); + for (i = 0; i < WABT_NUM_BINARY_SECTIONS; i++) { + wabt_destroy_section_ptr_vector(ctx->allocator, §ions[i]); } } @@ -768,90 +768,90 @@ static void dump_reloc_offsets(Context* ctx) { if (s_verbose) { uint32_t i; for (i = 0; i < ctx->inputs.size; i++) { - WasmLinkerInputBinary* binary = &ctx->inputs.data[i]; - wasm_writef(&s_log_stream, "Relocation info for: %s\n", binary->filename); - wasm_writef(&s_log_stream, " - type index offset : %d\n", + WabtLinkerInputBinary* binary = &ctx->inputs.data[i]; + wabt_writef(&s_log_stream, "Relocation info for: %s\n", binary->filename); + wabt_writef(&s_log_stream, " - type index offset : %d\n", binary->type_index_offset); - wasm_writef(&s_log_stream, " - mem page offset : %d\n", + wabt_writef(&s_log_stream, " - mem page offset : %d\n", binary->memory_page_offset); - wasm_writef(&s_log_stream, " - function index offset : %d\n", + wabt_writef(&s_log_stream, " - function index offset : %d\n", binary->function_index_offset); - wasm_writef(&s_log_stream, " - global index offset : %d\n", + wabt_writef(&s_log_stream, " - global index offset : %d\n", binary->global_index_offset); - wasm_writef(&s_log_stream, " - imported function offset: %d\n", + wabt_writef(&s_log_stream, " - imported function offset: %d\n", binary->imported_function_index_offset); - wasm_writef(&s_log_stream, " - imported global offset : %d\n", + wabt_writef(&s_log_stream, " - imported global offset : %d\n", binary->imported_global_index_offset); } } } -static WasmResult perform_link(Context* ctx) { - WasmMemoryWriter writer; - WASM_ZERO_MEMORY(writer); - if (WASM_FAILED(wasm_init_mem_writer(ctx->allocator, &writer))) - WASM_FATAL("unable to open memory writer for writing\n"); +static WabtResult perform_link(Context* ctx) { + WabtMemoryWriter writer; + WABT_ZERO_MEMORY(writer); + if (WABT_FAILED(wabt_init_mem_writer(ctx->allocator, &writer))) + WABT_FATAL("unable to open memory writer for writing\n"); - WasmStream* log_stream = NULL; + WabtStream* log_stream = NULL; if (s_verbose) log_stream = &s_log_stream; if (s_verbose) - wasm_writef(&s_log_stream, "writing file: %s\n", s_outfile); + wabt_writef(&s_log_stream, "writing file: %s\n", s_outfile); calculate_reloc_offsets(ctx); resolve_symbols(ctx); calculate_reloc_offsets(ctx); dump_reloc_offsets(ctx); - wasm_init_stream(&ctx->stream, &writer.base, log_stream); + wabt_init_stream(&ctx->stream, &writer.base, log_stream); write_binary(ctx); - if (WASM_FAILED(wasm_write_output_buffer_to_file(&writer.buf, s_outfile))) - WASM_FATAL("error writing linked output to file\n"); + if (WABT_FAILED(wabt_write_output_buffer_to_file(&writer.buf, s_outfile))) + WABT_FATAL("error writing linked output to file\n"); - wasm_close_mem_writer(&writer); - return WASM_OK; + wabt_close_mem_writer(&writer); + return WABT_OK; } int main(int argc, char** argv) { - wasm_init_stdio(); + wabt_init_stdio(); Context context; - WASM_ZERO_MEMORY(context); - context.allocator = &g_wasm_libc_allocator; + WABT_ZERO_MEMORY(context); + context.allocator = &g_wabt_libc_allocator; parse_options(context.allocator, argc, argv); - WasmResult result = WASM_OK; + WabtResult result = WABT_OK; size_t i; for (i = 0; i < s_infiles.size; i++) { const char* input_filename = s_infiles.data[i]; if (s_verbose) - wasm_writef(&s_log_stream, "reading file: %s\n", input_filename); + wabt_writef(&s_log_stream, "reading file: %s\n", input_filename); void* data; size_t size; - result = wasm_read_file(context.allocator, input_filename, &data, &size); - if (WASM_FAILED(result)) + result = wabt_read_file(context.allocator, input_filename, &data, &size); + if (WABT_FAILED(result)) return result; - WasmLinkerInputBinary* b = - wasm_append_binary(context.allocator, &context.inputs); + WabtLinkerInputBinary* b = + wabt_append_binary(context.allocator, &context.inputs); b->data = data; b->size = size; b->filename = input_filename; - result = wasm_read_binary_linker(context.allocator, b); - if (WASM_FAILED(result)) - WASM_FATAL("error parsing file: %s\n", input_filename); + result = wabt_read_binary_linker(context.allocator, b); + if (WABT_FAILED(result)) + WABT_FATAL("error parsing file: %s\n", input_filename); } result = perform_link(&context); - if (WASM_FAILED(result)) + if (WABT_FAILED(result)) return result; /* Cleanup */ - WASM_DESTROY_VECTOR_AND_ELEMENTS(context.allocator, context.inputs, binary); - wasm_destroy_string_vector(context.allocator, &s_infiles); + WABT_DESTROY_VECTOR_AND_ELEMENTS(context.allocator, context.inputs, binary); + wabt_destroy_string_vector(context.allocator, &s_infiles); - wasm_print_allocator_stats(context.allocator); - wasm_destroy_allocator(context.allocator); + wabt_print_allocator_stats(context.allocator); + wabt_destroy_allocator(context.allocator); return result; } |