summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--src/binary-writer.cc22
-rw-r--r--test/dump/elem-mvp-compat.txt1
-rw-r--r--test/dump/relocs-section-target.txt41
3 files changed, 61 insertions, 3 deletions
diff --git a/src/binary-writer.cc b/src/binary-writer.cc
index 13ead3c0..c0a841e7 100644
--- a/src/binary-writer.cc
+++ b/src/binary-writer.cc
@@ -369,7 +369,8 @@ class BinaryWriter {
size_t last_subsection_payload_offset_ = 0;
// Information about the data count section, so it can be removed if it is
- // not needed.
+ // not needed, and relocs relative to the code section patched up.
+ size_t code_start_ = 0;
size_t data_count_start_ = 0;
size_t data_count_end_ = 0;
bool has_data_segment_instruction_ = false;
@@ -1387,6 +1388,7 @@ Result BinaryWriter::WriteModule() {
}
if (num_funcs) {
+ code_start_ = stream_->offset();
BeginKnownSection(BinarySection::Code);
WriteU32Leb128(stream_, num_funcs, "num functions");
@@ -1409,10 +1411,26 @@ Result BinaryWriter::WriteModule() {
if (options_.features.bulk_memory_enabled() &&
!has_data_segment_instruction_) {
Offset size = stream_->offset() - data_count_end_;
- if (data_count_start_ != data_count_end_) {
+ if (size) {
+ // If the DataCount section was followed by anything, assert that it's
+ // only the Code section. This limits the amount of fixing-up that we
+ // need to do.
+ assert(data_count_end_ == code_start_);
+ assert(last_section_type_ == BinarySection::Code);
stream_->MoveData(data_count_start_, data_count_end_, size);
}
stream_->Truncate(data_count_start_ + size);
+
+ --section_count_;
+
+ // We just effectively decremented the code section's index; adjust anything
+ // that might have captured it.
+ for (RelocSection& section : reloc_sections_) {
+ if (section.section_index == section_count_) {
+ assert(last_section_type_ == BinarySection::Code);
+ --section.section_index;
+ }
+ }
}
if (module_->data_segments.size()) {
diff --git a/test/dump/elem-mvp-compat.txt b/test/dump/elem-mvp-compat.txt
index 4b111cdf..8f90c9c0 100644
--- a/test/dump/elem-mvp-compat.txt
+++ b/test/dump/elem-mvp-compat.txt
@@ -32,7 +32,6 @@
0000017: 00 ; section size (guess)
0000018: 00 ; data count
0000017: 01 ; FIXUP section size
-; move data: [19, 19) -> [16, 16)
; truncate to 22 (0x16)
elem-mvp-compat.wasm: file format wasm 0x1
diff --git a/test/dump/relocs-section-target.txt b/test/dump/relocs-section-target.txt
new file mode 100644
index 00000000..d3073df2
--- /dev/null
+++ b/test/dump/relocs-section-target.txt
@@ -0,0 +1,41 @@
+;;; TOOL: run-objdump
+;;; ARGS0: -r --enable-bulk-memory
+;;; ARGS1: -x
+(module
+ (type (;0;) (func))
+ (import "env" "b" (func (;0;) (type 0)))
+ (func $a (type 0)
+ call 0)
+ (export "a" (func $a)))
+(;; STDOUT ;;;
+
+relocs-section-target.wasm: file format wasm 0x1
+
+Section Details:
+
+Type[1]:
+ - type[0] () -> nil
+Import[1]:
+ - func[0] sig=0 <env.b> <- env.b
+Function[1]:
+ - func[1] sig=0 <a>
+Export[1]:
+ - func[1] <a> -> "a"
+Code[1]:
+ - func[1] size=8 <a>
+Custom:
+ - name: "linking"
+ - symbol table [count=1]
+ - 0: F <env.b> func=0 undefined binding=global vis=default
+Custom:
+ - name: "reloc.Code"
+ - relocations for section: 4 (Code) [1]
+ - R_WASM_FUNCTION_INDEX_LEB offset=0x000004(file=0x00002a) symbol=0 <env.b>
+
+Code Disassembly:
+
+000028 func[1] <a>:
+ 000029: 10 80 80 80 80 00 | call 0 <env.b>
+ 00002a: R_WASM_FUNCTION_INDEX_LEB 0 <env.b>
+ 00002f: 0b | end
+;;; STDOUT ;;)