summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/binary-reader-linker.cc62
-rw-r--r--src/binary-reader-linker.h6
-rw-r--r--src/tools/wasm-link.cc70
-rw-r--r--src/wasm-link.h20
4 files changed, 86 insertions, 72 deletions
diff --git a/src/binary-reader-linker.cc b/src/binary-reader-linker.cc
index e85320b1..cb78f10f 100644
--- a/src/binary-reader-linker.cc
+++ b/src/binary-reader-linker.cc
@@ -83,15 +83,14 @@ class BinaryReaderLinker : public BinaryReaderNop {
Result OnInitExprI32ConstExpr(Index index, uint32_t value) override;
private:
- LinkerInputBinary* binary;
+ LinkerInputBinary* binary_;
- Section* reloc_section = nullptr;
- Section* current_section = nullptr;
- Index function_count = 0;
+ Section* reloc_section_ = nullptr;
+ Section* current_section_ = nullptr;
};
BinaryReaderLinker::BinaryReaderLinker(LinkerInputBinary* binary)
- : binary(binary) {}
+ : binary_(binary) {}
Result BinaryReaderLinker::OnRelocCount(Index count,
BinarySection section_code,
@@ -100,10 +99,10 @@ Result BinaryReaderLinker::OnRelocCount(Index count,
WABT_FATAL("relocation for custom sections not yet supported\n");
}
- for (const std::unique_ptr<Section>& section : binary->sections) {
+ for (const std::unique_ptr<Section>& section : binary_->sections) {
if (section->section_code != section_code)
continue;
- reloc_section = section.get();
+ reloc_section_ = section.get();
return Result::Ok;
}
@@ -115,11 +114,11 @@ Result BinaryReaderLinker::OnReloc(RelocType type,
Offset offset,
Index index,
uint32_t addend) {
- if (offset + RELOC_SIZE > reloc_section->size) {
+ if (offset + RELOC_SIZE > reloc_section_->size) {
WABT_FATAL("invalid relocation offset: %#" PRIoffset "\n", offset);
}
- reloc_section->relocations.emplace_back(type, offset, index, addend);
+ reloc_section_->relocations.emplace_back(type, offset, index, addend);
return Result::Ok;
}
@@ -129,13 +128,13 @@ Result BinaryReaderLinker::OnImportFunc(Index import_index,
StringSlice field_name,
Index global_index,
Index sig_index) {
- binary->function_imports.emplace_back();
- FunctionImport* import = &binary->function_imports.back();
+ binary_->function_imports.emplace_back();
+ FunctionImport* import = &binary_->function_imports.back();
import->module_name = module_name;
import->name = field_name;
import->sig_index = sig_index;
import->active = true;
- binary->active_function_imports++;
+ binary_->active_function_imports++;
return Result::Ok;
}
@@ -145,35 +144,35 @@ Result BinaryReaderLinker::OnImportGlobal(Index import_index,
Index global_index,
Type type,
bool mutable_) {
- binary->global_imports.emplace_back();
- GlobalImport* import = &binary->global_imports.back();
+ binary_->global_imports.emplace_back();
+ GlobalImport* import = &binary_->global_imports.back();
import->module_name = module_name;
import->name = field_name;
import->type = type;
import->mutable_ = mutable_;
- binary->active_global_imports++;
+ binary_->active_global_imports++;
return Result::Ok;
}
Result BinaryReaderLinker::OnFunctionCount(Index count) {
- function_count = count;
+ binary_->function_count = count;
return Result::Ok;
}
Result BinaryReaderLinker::BeginSection(BinarySection section_code,
Offset size) {
Section* sec = new Section();
- binary->sections.emplace_back(sec);
- current_section = sec;
+ binary_->sections.emplace_back(sec);
+ current_section_ = sec;
sec->section_code = section_code;
sec->size = size;
sec->offset = state->offset;
- sec->binary = binary;
+ sec->binary = binary_;
if (sec->section_code != BinarySection::Custom &&
sec->section_code != BinarySection::Start) {
size_t bytes_read = read_u32_leb128(
- &binary->data[sec->offset], &binary->data[binary->size], &sec->count);
+ &binary_->data[sec->offset], &binary_->data[binary_->size], &sec->count);
if (bytes_read == 0)
WABT_FATAL("error reading section element count\n");
sec->payload_offset = sec->offset + bytes_read;
@@ -188,13 +187,13 @@ Result BinaryReaderLinker::OnTable(Index index,
if (elem_limits->has_max && (elem_limits->max != elem_limits->initial))
WABT_FATAL("Tables with max != initial not supported by wabt-link\n");
- binary->table_elem_count = elem_limits->initial;
+ binary_->table_elem_count = elem_limits->initial;
return Result::Ok;
}
Result BinaryReaderLinker::OnElemSegmentFunctionIndexCount(Index index,
Index count) {
- Section* sec = current_section;
+ Section* sec = current_section_;
/* Modify the payload to include only the actual function indexes */
size_t delta = state->offset - sec->payload_offset;
@@ -204,14 +203,14 @@ Result BinaryReaderLinker::OnElemSegmentFunctionIndexCount(Index index,
}
Result BinaryReaderLinker::OnMemory(Index index, const Limits* page_limits) {
- Section* sec = current_section;
+ Section* sec = current_section_;
sec->data.memory_limits = *page_limits;
- binary->memory_page_count = page_limits->initial;
+ binary_->memory_page_count = page_limits->initial;
return Result::Ok;
}
Result BinaryReaderLinker::BeginDataSegment(Index index, Index memory_index) {
- Section* sec = current_section;
+ Section* sec = current_section_;
if (!sec->data.data_segments) {
sec->data.data_segments = new std::vector<DataSegment>();
}
@@ -222,7 +221,7 @@ Result BinaryReaderLinker::BeginDataSegment(Index index, Index memory_index) {
}
Result BinaryReaderLinker::OnInitExprI32ConstExpr(Index index, uint32_t value) {
- Section* sec = current_section;
+ Section* sec = current_section_;
if (sec->section_code != BinarySection::Data)
return Result::Ok;
DataSegment& segment = sec->data.data_segments->back();
@@ -233,7 +232,7 @@ Result BinaryReaderLinker::OnInitExprI32ConstExpr(Index index, uint32_t value) {
Result BinaryReaderLinker::OnDataSegmentData(Index index,
const void* src_data,
Address size) {
- Section* sec = current_section;
+ Section* sec = current_section_;
DataSegment& segment = sec->data.data_segments->back();
segment.data = static_cast<const uint8_t*>(src_data);
segment.size = size;
@@ -244,8 +243,8 @@ Result BinaryReaderLinker::OnExport(Index index,
ExternalKind kind,
Index item_index,
StringSlice name) {
- binary->exports.emplace_back();
- Export* export_ = &binary->exports.back();
+ binary_->exports.emplace_back();
+ Export* export_ = &binary_->exports.back();
export_->name = name;
export_->kind = kind;
export_->index = item_index;
@@ -253,12 +252,13 @@ Result BinaryReaderLinker::OnExport(Index index,
}
Result BinaryReaderLinker::BeginNamesSection(Offset size) {
- binary->debug_names.resize(function_count + binary->function_imports.size());
+ binary_->debug_names.resize(binary_->function_count +
+ binary_->function_imports.size());
return Result::Ok;
}
Result BinaryReaderLinker::OnFunctionName(Index index, StringSlice name) {
- binary->debug_names[index] = string_slice_to_string(name);
+ binary_->debug_names[index] = string_slice_to_string(name);
return Result::Ok;
}
diff --git a/src/binary-reader-linker.h b/src/binary-reader-linker.h
index 0ffb7e42..84b0f3c9 100644
--- a/src/binary-reader-linker.h
+++ b/src/binary-reader-linker.h
@@ -26,14 +26,14 @@ class Stream;
namespace link {
-struct LinkerInputBinary;
+class LinkerInputBinary;
struct LinkOptions {
Stream* log_stream;
};
-Result read_binary_linker(struct LinkerInputBinary* input_info,
- struct LinkOptions* options);
+Result read_binary_linker(LinkerInputBinary* input_info,
+ LinkOptions* options);
} // namespace link
} // namespace wabt
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:
diff --git a/src/wasm-link.h b/src/wasm-link.h
index 2fdc33ac..86cdecb5 100644
--- a/src/wasm-link.h
+++ b/src/wasm-link.h
@@ -26,7 +26,7 @@
namespace wabt {
namespace link {
-struct LinkerInputBinary;
+class LinkerInputBinary;
struct FunctionImport {
StringSlice module_name;
@@ -34,7 +34,7 @@ struct FunctionImport {
Index sig_index;
bool active; /* Is this import present in the linked binary */
Index relocated_function_index;
- struct LinkerInputBinary* foreign_binary;
+ LinkerInputBinary* foreign_binary;
Index foreign_index;
};
@@ -69,7 +69,7 @@ struct Section {
~Section();
/* The binary to which this section belongs */
- struct LinkerInputBinary* binary;
+ LinkerInputBinary* binary;
std::vector<Reloc> relocations; /* The relocations for this section */
BinarySection section_code;
@@ -98,11 +98,20 @@ struct Section {
typedef std::vector<Section*> SectionPtrVector;
-struct LinkerInputBinary {
+class LinkerInputBinary {
+ public:
WABT_DISALLOW_COPY_AND_ASSIGN(LinkerInputBinary);
LinkerInputBinary(const char* filename, uint8_t* data, size_t size);
~LinkerInputBinary();
+ Index RelocateFuncIndex(Index findex);
+ Index RelocateTypeIndex(Index index);
+ Index RelocateGlobalIndex(Index index);
+
+ bool IsValidFunctionIndex(Index index);
+ bool IsFunctionImport(Index index);
+ bool IsInactiveFunctionImport(Index index);
+
const char* filename;
uint8_t* data;
size_t size;
@@ -123,7 +132,8 @@ struct LinkerInputBinary {
Index memory_page_count;
Index memory_page_offset;
- Index table_elem_count;
+ Index table_elem_count = 0;
+ Index function_count = 0;
std::vector<std::string> debug_names;
};