diff options
Diffstat (limited to 'src')
-rw-r--r-- | src/binary-reader-linker.cc | 80 | ||||
-rw-r--r-- | src/binary-reader.cc | 2 | ||||
-rw-r--r-- | src/binary-writer.cc | 34 |
3 files changed, 40 insertions, 76 deletions
diff --git a/src/binary-reader-linker.cc b/src/binary-reader-linker.cc index 40d3c420..0da7b9c2 100644 --- a/src/binary-reader-linker.cc +++ b/src/binary-reader-linker.cc @@ -34,8 +34,6 @@ class BinaryReaderLinker : public BinaryReaderNop { virtual Result BeginSection(BinarySection section_type, uint32_t size); - virtual Result BeginCustomSection(uint32_t size, StringSlice section_name); - virtual Result OnImport(uint32_t index, StringSlice module_name, StringSlice field_name); @@ -51,6 +49,8 @@ class BinaryReaderLinker : public BinaryReaderNop { Type type, bool mutable_); + virtual Result OnFunctionCount(uint32_t count); + virtual Result OnTable(uint32_t index, Type elem_type, const Limits* elem_limits); @@ -70,6 +70,8 @@ class BinaryReaderLinker : public BinaryReaderNop { const void* data, uint32_t size); + virtual Result BeginNamesSection(uint32_t size); + virtual Result OnFunctionName(uint32_t function_index, StringSlice function_name); @@ -88,6 +90,7 @@ class BinaryReaderLinker : public BinaryReaderNop { Section* reloc_section = nullptr; Section* current_section = nullptr; + uint32_t function_count = 0; }; BinaryReaderLinker::BinaryReaderLinker(LinkerInputBinary* binary) @@ -163,6 +166,11 @@ Result BinaryReaderLinker::OnImportGlobal(uint32_t import_index, return Result::Ok; } +Result BinaryReaderLinker::OnFunctionCount(uint32_t count) { + function_count = count; + return Result::Ok; +} + Result BinaryReaderLinker::BeginSection(BinarySection section_code, uint32_t size) { Section* sec = new Section(); @@ -185,62 +193,6 @@ Result BinaryReaderLinker::BeginSection(BinarySection section_code, return Result::Ok; } -Result BinaryReaderLinker::BeginCustomSection(uint32_t size, - StringSlice section_name) { - Section* sec = current_section; - sec->data.custom.name = section_name; - - /* Modify section size and offset to not include the name itself. */ - size_t delta = state->offset - sec->offset; - sec->offset = sec->offset + delta; - sec->size = sec->size - delta; - sec->payload_offset = sec->offset; - sec->payload_size = sec->size; - - /* Special handling for certain CUSTOM sections */ - if (string_slice_eq_cstr(§ion_name, "name")) { - uint32_t name_type; - size_t bytes_read = read_u32_leb128( - &binary->data[sec->offset], &binary->data[binary->size], &name_type); - - if (static_cast<NameSectionSubsection>(name_type) != - NameSectionSubsection::Function) { - WABT_FATAL("no function name section"); - } - - sec->payload_offset += bytes_read; - sec->payload_size -= bytes_read; - - uint32_t subsection_size; - bytes_read = read_u32_leb128(&binary->data[sec->offset], - &binary->data[binary->size], &subsection_size); - - sec->payload_offset += bytes_read; - sec->payload_size -= bytes_read; - - bytes_read = read_u32_leb128(&binary->data[sec->payload_offset], - &binary->data[binary->size], &sec->count); - sec->payload_offset += bytes_read; - sec->payload_size -= bytes_read; - - /* We don't currently support merging name sections unless they contain - * a name for every function. */ - uint32_t total_funcs = binary->function_imports.size(); - for (const std::unique_ptr<Section>& section : binary->sections) { - if (section->section_code == BinarySection::Function) { - total_funcs += section->count; - break; - } - } - if (total_funcs != sec->count) { - WABT_FATAL("name section count (%d) does not match function count (%d)\n", - sec->count, total_funcs); - } - } - - return Result::Ok; -} - Result BinaryReaderLinker::OnTable(uint32_t index, Type elem_type, const Limits* elem_limits) { @@ -313,13 +265,13 @@ Result BinaryReaderLinker::OnExport(uint32_t index, return Result::Ok; } +Result BinaryReaderLinker::BeginNamesSection(uint32_t size) { + binary->debug_names.resize(function_count + binary->function_imports.size()); + return Result::Ok; +} + Result BinaryReaderLinker::OnFunctionName(uint32_t index, StringSlice name) { - while (binary->debug_names.size() < index) { - binary->debug_names.emplace_back(); - } - if (binary->debug_names.size() == index) { - binary->debug_names.push_back(string_slice_to_string(name)); - } + binary->debug_names[index] = string_slice_to_string(name); return Result::Ok; } diff --git a/src/binary-reader.cc b/src/binary-reader.cc index 4ac9c746..136b4c4a 100644 --- a/src/binary-reader.cc +++ b/src/binary-reader.cc @@ -943,6 +943,8 @@ static void read_custom_section(Context* ctx, uint32_t section_size) { StringSlice function_name; in_u32_leb128(ctx, &function_index, "function index"); + RAISE_ERROR_UNLESS(function_index < num_total_funcs(ctx), + "invalid function index: %u", function_index); in_str(ctx, &function_name, "function name"); CALLBACK(OnFunctionName, function_index, function_name); } diff --git a/src/binary-writer.cc b/src/binary-writer.cc index 839752d7..8c7d9be4 100644 --- a/src/binary-writer.cc +++ b/src/binary-writer.cc @@ -315,8 +315,6 @@ static void begin_subsection(Context* ctx, const char* name, size_t leb_size_guess) { assert(ctx->last_subsection_leb_size_guess == 0); - char desc[100]; - wabt_snprintf(desc, sizeof(desc), "subsection \"%s\"", name); ctx->last_subsection_leb_size_guess = leb_size_guess; ctx->last_subsection_offset = write_u32_leb128_space(ctx, leb_size_guess, "subsection size (guess)"); @@ -880,17 +878,29 @@ static Result write_module(Context* ctx, const Module* module) { char desc[100]; begin_custom_section(ctx, WABT_BINARY_SECTION_NAME, LEB_SECTION_SIZE_GUESS); - write_u32_leb128(&ctx->stream, 1, "function name type"); - begin_subsection(ctx, "function name subsection", LEB_SECTION_SIZE_GUESS); - write_u32_leb128(&ctx->stream, module->funcs.size(), "num functions"); - for (size_t i = 0; i < module->funcs.size(); ++i) { - const Func* func = module->funcs[i]; - write_u32_leb128(&ctx->stream, i, "function index"); - wabt_snprintf(desc, sizeof(desc), "func name %" PRIzd, i); - write_str(&ctx->stream, func->name.start, func->name.length, - PrintChars::Yes, desc); + + size_t named_functions = 0; + for (const Func* func: module->funcs) { + if (func->name.length > 0) + named_functions++; + } + + if (named_functions > 0) { + write_u32_leb128(&ctx->stream, 1, "function name type"); + begin_subsection(ctx, "function name subsection", LEB_SECTION_SIZE_GUESS); + + write_u32_leb128(&ctx->stream, named_functions, "num functions"); + for (size_t i = 0; i < module->funcs.size(); ++i) { + const Func* func = module->funcs[i]; + if (func->name.length == 0) + continue; + write_u32_leb128(&ctx->stream, i, "function index"); + wabt_snprintf(desc, sizeof(desc), "func name %" PRIzd, i); + write_str(&ctx->stream, func->name.start, func->name.length, + PrintChars::Yes, desc); + } + end_subsection(ctx); } - end_subsection(ctx); write_u32_leb128(&ctx->stream, 2, "local name type"); |