diff options
Diffstat (limited to 'src/tools/wasm-link.cc')
-rw-r--r-- | src/tools/wasm-link.cc | 70 |
1 files changed, 37 insertions, 33 deletions
diff --git a/src/tools/wasm-link.cc b/src/tools/wasm-link.cc index 6d4b1fbd..b69f2b5a 100644 --- a/src/tools/wasm-link.cc +++ b/src/tools/wasm-link.cc @@ -154,17 +154,29 @@ LinkerInputBinary::~LinkerInputBinary() { delete[] data; } -static Index relocate_func_index(LinkerInputBinary* binary, - Index function_index) { +bool LinkerInputBinary::IsFunctionImport(Index index) { + assert(IsValidFunctionIndex(index)); + return index < function_imports.size(); +} + +bool LinkerInputBinary::IsInactiveFunctionImport(Index index){ + return IsFunctionImport(index) && !function_imports[index].active; +} + +bool LinkerInputBinary::IsValidFunctionIndex(Index index) { + return index < function_imports.size() + function_count; +} + +Index LinkerInputBinary::RelocateFuncIndex(Index function_index) { Index offset; - if (function_index >= binary->function_imports.size()) { + if (!IsFunctionImport(function_index)) { /* locally declared function call */ - offset = binary->function_index_offset; + offset = function_index_offset; if (s_debug) s_log_stream->Writef("func reloc %d + %d\n", function_index, offset); } else { /* imported function call */ - FunctionImport* import = &binary->function_imports[function_index]; + FunctionImport* import = &function_imports[function_index]; if (!import->active) { function_index = import->foreign_index; offset = import->foreign_binary->function_index_offset; @@ -183,18 +195,16 @@ static Index relocate_func_index(LinkerInputBinary* binary, return function_index + offset; } -static Index relocate_type_index(LinkerInputBinary* binary, - Index type_index) { - return type_index + binary->type_index_offset; +Index LinkerInputBinary::RelocateTypeIndex(Index type_index) { + return type_index + type_index_offset; } -static Index relocate_global_index(LinkerInputBinary* binary, - Index global_index) { +Index LinkerInputBinary::RelocateGlobalIndex(Index global_index) { Index offset; - if (global_index >= binary->global_imports.size()) { - offset = binary->global_index_offset; + if (global_index >= global_imports.size()) { + offset = global_index_offset; } else { - offset = binary->imported_global_index_offset; + offset = imported_global_index_offset; } return global_index + offset; } @@ -210,16 +220,16 @@ static void apply_relocation(Section* section, Reloc* r) { switch (r->type) { case RelocType::FuncIndexLEB: - new_value = relocate_func_index(binary, cur_value); + new_value = binary->RelocateFuncIndex(cur_value); break; case RelocType::TypeIndexLEB: - new_value = relocate_type_index(binary, cur_value); + new_value = binary->RelocateTypeIndex(cur_value); break; case RelocType::TableIndexSLEB: new_value = cur_value + binary->table_index_offset; break; case RelocType::GlobalIndexLEB: - new_value = relocate_global_index(binary, cur_value); + new_value = binary->RelocateGlobalIndex(cur_value); break; default: WABT_FATAL("unhandled relocation type: %s\n", @@ -321,7 +331,7 @@ static void write_export_section(Context* ctx) { Index index = export_.index; switch (export_.kind) { case ExternalKind::Func: - index = relocate_func_index(binary.get(), index); + index = binary->RelocateFuncIndex(index); break; default: WABT_FATAL("unsupport export type: %d\n", @@ -451,7 +461,8 @@ static void write_function_section(Context* ctx, &sec->binary->data[sec->payload_offset + sec->payload_size]; while (count--) { input_offset += read_u32_leb128(start + input_offset, end, &sig_index); - write_u32_leb128(stream, relocate_type_index(sec->binary, sig_index), "sig"); + write_u32_leb128(stream, sec->binary->RelocateTypeIndex(sig_index), + "sig"); } } @@ -495,10 +506,8 @@ static void write_names_section(Context* ctx) { for (size_t i = 0; i < binary->debug_names.size(); i++) { if (binary->debug_names[i].empty()) continue; - if (i < binary->function_imports.size()) { - if (!binary->function_imports[i].active) - continue; - } + if (binary->IsInactiveFunctionImport(i)) + continue; total_count++; } } @@ -516,16 +525,11 @@ static void write_names_section(Context* ctx) { write_u32_leb128(stream, total_count, "element count"); for (const std::unique_ptr<LinkerInputBinary>& binary: ctx->inputs) { for (size_t i = 0; i < binary->debug_names.size(); i++) { - if (i < binary->function_imports.size()) { - if (!binary->function_imports[i].active) { - continue; - } - } - if (binary->debug_names[i].empty()) continue; - write_u32_leb128(stream, relocate_func_index(&*binary, i), - "function index"); + if (binary->IsInactiveFunctionImport(i)) + continue; + write_u32_leb128(stream, binary->RelocateFuncIndex(i), "function index"); write_string(stream, binary->debug_names[i], "function name"); } } @@ -566,13 +570,13 @@ static void write_reloc_section(Context* ctx, Index relocated_index; switch (reloc.type) { case RelocType::FuncIndexLEB: - relocated_index = relocate_func_index(sec->binary, reloc.index); + relocated_index = sec->binary->RelocateFuncIndex(reloc.index); break; case RelocType::TypeIndexLEB: - relocated_index = relocate_type_index(sec->binary, reloc.index); + relocated_index = sec->binary->RelocateTypeIndex(reloc.index); break; case RelocType::GlobalIndexLEB: - relocated_index = relocate_global_index(sec->binary, reloc.index); + relocated_index = sec->binary->RelocateGlobalIndex(reloc.index); break; // TODO(sbc): Handle other relocation types. default: |