summaryrefslogtreecommitdiff
path: root/src/binary-reader.cc
diff options
context:
space:
mode:
Diffstat (limited to 'src/binary-reader.cc')
-rw-r--r--src/binary-reader.cc40
1 files changed, 35 insertions, 5 deletions
diff --git a/src/binary-reader.cc b/src/binary-reader.cc
index 36bd4252..85a6e258 100644
--- a/src/binary-reader.cc
+++ b/src/binary-reader.cc
@@ -116,6 +116,9 @@ class BinaryReader {
[[nodiscard]] Result ReadBytes(const void** out_data,
Address* out_data_size,
const char* desc);
+ [[nodiscard]] Result ReadBytesWithSize(const void** out_data,
+ Offset size,
+ const char* desc);
[[nodiscard]] Result ReadIndex(Index* index, const char* desc);
[[nodiscard]] Result ReadOffset(Offset* offset, const char* desc);
[[nodiscard]] Result ReadAlignment(Address* align_log2, const char* desc);
@@ -153,6 +156,8 @@ class BinaryReader {
[[nodiscard]] Result ReadNameSection(Offset section_size);
[[nodiscard]] Result ReadRelocSection(Offset section_size);
[[nodiscard]] Result ReadDylinkSection(Offset section_size);
+ [[nodiscard]] Result ReadGenericCustomSection(std::string_view name,
+ Offset section_size);
[[nodiscard]] Result ReadDylink0Section(Offset section_size);
[[nodiscard]] Result ReadTargetFeaturesSections(Offset section_size);
[[nodiscard]] Result ReadLinkingSection(Offset section_size);
@@ -394,13 +399,19 @@ Result BinaryReader::ReadBytes(const void** out_data,
const char* desc) {
uint32_t data_size = 0;
CHECK_RESULT(ReadU32Leb128(&data_size, "data size"));
+ CHECK_RESULT(ReadBytesWithSize(out_data, data_size, desc));
+ *out_data_size = data_size;
+ return Result::Ok;
+}
- ERROR_UNLESS(state_.offset + data_size <= read_end_,
- "unable to read data: %s", desc);
+Result BinaryReader::ReadBytesWithSize(const void** out_data,
+ Offset size,
+ const char* desc) {
+ ERROR_UNLESS(state_.offset + size <= read_end_, "unable to read data: %s",
+ desc);
*out_data = static_cast<const uint8_t*>(state_.data) + state_.offset;
- *out_data_size = data_size;
- state_.offset += data_size;
+ state_.offset += size;
return Result::Ok;
}
@@ -2155,6 +2166,18 @@ Result BinaryReader::ReadTargetFeaturesSections(Offset section_size) {
return Result::Ok;
}
+Result BinaryReader::ReadGenericCustomSection(std::string_view name,
+ Offset section_size) {
+ CALLBACK(BeginGenericCustomSection, section_size);
+ const void* data;
+ Offset custom_data_size = read_end_ - state_.offset;
+ CHECK_RESULT(
+ ReadBytesWithSize(&data, custom_data_size, "custom section data"));
+ CALLBACK(OnGenericCustomSection, name, data, custom_data_size);
+ CALLBACK0(EndGenericCustomSection);
+ return Result::Ok;
+}
+
Result BinaryReader::ReadLinkingSection(Offset section_size) {
CALLBACK(BeginLinkingSection, section_size);
uint32_t version;
@@ -2375,6 +2398,13 @@ Result BinaryReader::ReadCustomSection(Index section_index,
ValueRestoreGuard<bool, &BinaryReader::reading_custom_section_> guard(this);
reading_custom_section_ = true;
+ {
+ // Backtrack parser when scope ends
+ ValueRestoreGuard<BinaryReaderDelegate::State, &BinaryReader::state_> guard(
+ this);
+ CHECK_RESULT(ReadGenericCustomSection(section_name, section_size));
+ }
+
if (options_.read_debug_names && section_name == WABT_BINARY_SECTION_NAME) {
CHECK_RESULT(ReadNameSection(section_size));
did_read_names_section_ = true;
@@ -2395,7 +2425,7 @@ Result BinaryReader::ReadCustomSection(Index section_index,
metadata_name.remove_prefix(sizeof(WABT_BINARY_SECTION_CODE_METADATA) - 1);
CHECK_RESULT(ReadCodeMetadataSection(metadata_name, section_size));
} else {
- // This is an unknown custom section, skip it.
+ // Skip. This is a generic custom section, and is handled above.
state_.offset = read_end_;
}
CALLBACK0(EndCustomSection);