summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--src/binary-reader-objdump.cc112
-rw-r--r--test/dump/table.txt96
2 files changed, 114 insertions, 94 deletions
diff --git a/src/binary-reader-objdump.cc b/src/binary-reader-objdump.cc
index aa8cfd9f..29117ad8 100644
--- a/src/binary-reader-objdump.cc
+++ b/src/binary-reader-objdump.cc
@@ -686,6 +686,15 @@ class BinaryReaderObjdump : public BinaryReaderObjdumpBase {
Result OnFunctionBodyCount(Index count) override;
+ Result BeginElemSection(Offset size) override {
+ in_elem_section_ = true;
+ return Result::Ok;
+ }
+ Result EndElemSection() override {
+ in_elem_section_ = false;
+ return Result::Ok;
+ }
+
Result OnElemSegmentCount(Index count) override;
Result BeginElemSegment(Index index, Index table_index) override;
Result OnElemSegmentFunctionIndexCount(Index index, Index count) override;
@@ -700,6 +709,7 @@ class BinaryReaderObjdump : public BinaryReaderObjdumpBase {
in_data_section_ = false;
return Result::Ok;
}
+
Result OnDataSegmentCount(Index count) override;
Result BeginDataSegment(Index index, Index memory_index) override;
Result OnDataSegmentData(Index index,
@@ -756,6 +766,7 @@ class BinaryReaderObjdump : public BinaryReaderObjdumpBase {
Result OnExceptionType(Index index, TypeVector& sig) override;
private:
+ Result HandleInitExpr(const InitExpr& expr);
bool ShouldPrintDetails();
void PrintDetails(const char* fmt, ...);
void PrintSymbolFlags(uint32_t flags);
@@ -767,7 +778,11 @@ class BinaryReaderObjdump : public BinaryReaderObjdumpBase {
Index table_index_ = 0;
Index next_data_reloc_ = 0;
bool in_data_section_ = false;
+ bool in_elem_section_ = false;
InitExpr data_init_expr_;
+ InitExpr elem_init_expr_;
+ uint32_t data_offset_ = 0;
+ uint32_t elem_offset_ = 0;
};
BinaryReaderObjdump::BinaryReaderObjdump(const uint8_t* data,
@@ -1061,8 +1076,8 @@ Result BinaryReaderObjdump::OnExport(Index index,
Result BinaryReaderObjdump::OnElemSegmentFunctionIndex(Index segment_index,
Index func_index) {
- PrintDetails(" - elem[%" PRIindex "] = func[%" PRIindex "]", elem_index_,
- func_index);
+ PrintDetails(" - elem[%" PRIindex "] = func[%" PRIindex "]",
+ elem_offset_ + elem_index_, func_index);
if (const char* name = GetFunctionName(func_index)) {
PrintDetails(" <%s>", name);
}
@@ -1083,9 +1098,9 @@ Result BinaryReaderObjdump::BeginElemSegment(Index index, Index table_index) {
Result BinaryReaderObjdump::OnElemSegmentFunctionIndexCount(Index index,
Index count) {
- PrintDetails(" - segment[%" PRIindex "] table=%" PRIindex " count=%" PRIindex
- "\n",
+ PrintDetails(" - segment[%" PRIindex "] table=%" PRIindex " count=%" PRIindex,
index, table_index_, count);
+ PrintInitExpr(elem_init_expr_);
return Result::Ok;
}
@@ -1131,29 +1146,54 @@ void BinaryReaderObjdump::PrintInitExpr(const InitExpr& expr) {
}
}
-Result BinaryReaderObjdump::OnInitExprF32ConstExpr(Index index,
- uint32_t value) {
- InitExpr expr;
- expr.type = InitExprType::F32;
- expr.value.f32 = value;
+static Result InitExprToConstOffset(const InitExpr& expr,
+ uint32_t* out_offset) {
+ switch (expr.type) {
+ case InitExprType::I32:
+ *out_offset = expr.value.i32;
+ break;
+ case InitExprType::Global:
+ *out_offset = 0;
+ break;
+ case InitExprType::I64:
+ case InitExprType::F32:
+ case InitExprType::F64:
+ case InitExprType::V128:
+ fprintf(stderr, "Segment/Elem offset must be an i32 init expr");
+ return Result::Error;
+ break;
+ }
+ return Result::Ok;
+}
+
+Result BinaryReaderObjdump::HandleInitExpr(const InitExpr& expr) {
if (in_data_section_) {
data_init_expr_ = expr;
+ return InitExprToConstOffset(expr, &data_offset_);
+ } else if (in_elem_section_) {
+ elem_init_expr_ = expr;
+ return InitExprToConstOffset(expr, &elem_offset_);
} else {
PrintInitExpr(expr);
}
return Result::Ok;
}
+Result BinaryReaderObjdump::OnInitExprF32ConstExpr(Index index,
+ uint32_t value) {
+ InitExpr expr;
+ expr.type = InitExprType::F32;
+ expr.value.f32 = value;
+ HandleInitExpr(expr);
+ return Result::Ok;
+}
+
Result BinaryReaderObjdump::OnInitExprF64ConstExpr(Index index,
uint64_t value) {
InitExpr expr;
expr.type = InitExprType::F64;
expr.value.f64 = value;
- if (in_data_section_) {
- data_init_expr_ = expr;
- } else {
- PrintInitExpr(expr);
- }
+ HandleInitExpr(expr);
return Result::Ok;
}
@@ -1161,11 +1201,7 @@ Result BinaryReaderObjdump::OnInitExprV128ConstExpr(Index index, v128 value) {
InitExpr expr;
expr.type = InitExprType::V128;
expr.value.v128_v = value;
- if (in_data_section_) {
- data_init_expr_ = expr;
- } else {
- PrintInitExpr(expr);
- }
+ HandleInitExpr(expr);
return Result::Ok;
}
@@ -1174,11 +1210,7 @@ Result BinaryReaderObjdump::OnInitExprGetGlobalExpr(Index index,
InitExpr expr;
expr.type = InitExprType::Global;
expr.value.global = global_index;
- if (in_data_section_) {
- data_init_expr_ = expr;
- } else {
- PrintInitExpr(expr);
- }
+ HandleInitExpr(expr);
return Result::Ok;
}
@@ -1187,11 +1219,7 @@ Result BinaryReaderObjdump::OnInitExprI32ConstExpr(Index index,
InitExpr expr;
expr.type = InitExprType::I32;
expr.value.i32 = value;
- if (in_data_section_) {
- data_init_expr_ = expr;
- } else {
- PrintInitExpr(expr);
- }
+ HandleInitExpr(expr);
return Result::Ok;
}
@@ -1200,11 +1228,7 @@ Result BinaryReaderObjdump::OnInitExprI64ConstExpr(Index index,
InitExpr expr;
expr.type = InitExprType::I64;
expr.value.i64 = value;
- if (in_data_section_) {
- data_init_expr_ = expr;
- } else {
- PrintInitExpr(expr);
- }
+ HandleInitExpr(expr);
return Result::Ok;
}
@@ -1251,23 +1275,7 @@ Result BinaryReaderObjdump::OnDataSegmentData(Index index,
PrintDetails(" - segment[%" PRIindex "] size=%" PRIaddress, index, size);
PrintInitExpr(data_init_expr_);
- size_t voffset = 0;
- switch (data_init_expr_.type) {
- case InitExprType::I32:
- voffset = data_init_expr_.value.i32;
- break;
- case InitExprType::Global:
- break;
- case InitExprType::I64:
- case InitExprType::F32:
- case InitExprType::F64:
- case InitExprType::V128:
- fprintf(stderr, "Segment offset must be an i32 init expr");
- return Result::Error;
- break;
- }
-
- out_stream_->WriteMemoryDump(src_data, size, voffset, PrintChars::Yes,
+ out_stream_->WriteMemoryDump(src_data, size, data_offset_, PrintChars::Yes,
" - ");
// Print relocations from this segment.
@@ -1284,7 +1292,7 @@ Result BinaryReaderObjdump::OnDataSegmentData(Index index,
if (abs_offset > state->offset) {
break;
}
- PrintRelocation(reloc, reloc.offset - segment_offset + voffset);
+ PrintRelocation(reloc, reloc.offset - segment_offset + data_offset_);
next_data_reloc_++;
}
diff --git a/test/dump/table.txt b/test/dump/table.txt
index 0846217a..d7d913eb 100644
--- a/test/dump/table.txt
+++ b/test/dump/table.txt
@@ -7,7 +7,9 @@
(func (param i32 i64))
(func (result f64)
f64.const 0)
- (table anyfunc (elem 0 0 1 2)))
+ (table 6 6 anyfunc)
+ (elem (i32.const 0) 1 1)
+ (elem (i32.const 2) 0 0 1 2))
(;; STDOUT ;;;
0000000: 0061 736d ; WASM_BINARY_MAGIC
0000004: 0100 0000 ; WASM_BINARY_VERSION
@@ -47,46 +49,54 @@
; table 0
0000021: 70 ; anyfunc
0000022: 01 ; limits: flags
-0000023: 04 ; limits: initial
-0000024: 04 ; limits: max
+0000023: 06 ; limits: initial
+0000024: 06 ; limits: max
000001f: 05 ; FIXUP section size
; section "Elem" (9)
0000025: 09 ; section code
0000026: 00 ; section size (guess)
-0000027: 01 ; num elem segments
+0000027: 02 ; num elem segments
; elem segment header 0
0000028: 00 ; table index
0000029: 41 ; i32.const
000002a: 00 ; i32 literal
000002b: 0b ; end
-000002c: 04 ; num function indices
-000002d: 00 ; function index
-000002e: 00 ; function index
-000002f: 01 ; function index
-0000030: 02 ; function index
-0000026: 0a ; FIXUP section size
+000002c: 02 ; num function indices
+000002d: 01 ; function index
+000002e: 01 ; function index
+; elem segment header 1
+000002f: 00 ; table index
+0000030: 41 ; i32.const
+0000031: 02 ; i32 literal
+0000032: 0b ; end
+0000033: 04 ; num function indices
+0000034: 00 ; function index
+0000035: 00 ; function index
+0000036: 01 ; function index
+0000037: 02 ; function index
+0000026: 11 ; FIXUP section size
; section "Code" (10)
-0000031: 0a ; section code
-0000032: 00 ; section size (guess)
-0000033: 03 ; num functions
+0000038: 0a ; section code
+0000039: 00 ; section size (guess)
+000003a: 03 ; num functions
; function body 0
-0000034: 00 ; func body size (guess)
-0000035: 00 ; local decl count
-0000036: 0b ; end
-0000034: 02 ; FIXUP func body size
+000003b: 00 ; func body size (guess)
+000003c: 00 ; local decl count
+000003d: 0b ; end
+000003b: 02 ; FIXUP func body size
; function body 1
-0000037: 00 ; func body size (guess)
-0000038: 00 ; local decl count
-0000039: 0b ; end
-0000037: 02 ; FIXUP func body size
+000003e: 00 ; func body size (guess)
+000003f: 00 ; local decl count
+0000040: 0b ; end
+000003e: 02 ; FIXUP func body size
; function body 2
-000003a: 00 ; func body size (guess)
-000003b: 00 ; local decl count
-000003c: 44 ; f64.const
-000003d: 0000 0000 0000 0000 ; f64 literal
-0000045: 0b ; end
-000003a: 0b ; FIXUP func body size
-0000032: 13 ; FIXUP section size
+0000041: 00 ; func body size (guess)
+0000042: 00 ; local decl count
+0000043: 44 ; f64.const
+0000044: 0000 0000 0000 0000 ; f64 literal
+000004c: 0b ; end
+0000041: 0b ; FIXUP func body size
+0000039: 13 ; FIXUP section size
table.wasm: file format wasm 0x1
@@ -101,22 +111,24 @@ Function:
- func[1] sig=1
- func[2] sig=2
Table:
- - table[0] type=anyfunc initial=4 max=4
+ - table[0] type=anyfunc initial=6 max=6
Elem:
- - init i32=0
- - segment[0] table=0 count=4
- - elem[0] = func[0]
- - elem[1] = func[0]
- - elem[2] = func[1]
- - elem[3] = func[2]
+ - segment[0] table=0 count=2 - init i32=0
+ - elem[0] = func[1]
+ - elem[1] = func[1]
+ - segment[1] table=0 count=4 - init i32=2
+ - elem[2] = func[0]
+ - elem[3] = func[0]
+ - elem[4] = func[1]
+ - elem[5] = func[2]
Code Disassembly:
-000034 func[0]:
- 000036: 0b | end
-000037 func[1]:
- 000039: 0b | end
-00003a func[2]:
- 00003c: 44 00 00 00 00 00 00 00 00 | f64.const 0x0p+0
- 000045: 0b | end
+00003b func[0]:
+ 00003d: 0b | end
+00003e func[1]:
+ 000040: 0b | end
+000041 func[2]:
+ 000043: 44 00 00 00 00 00 00 00 00 | f64.const 0x0p+0
+ 00004c: 0b | end
;;; STDOUT ;;)