Skip to main content

wamex_cli/read/
relocs.rs

1use anyhow::Result;
2use vec_map::VecMap;
3pub use wasmparser::RelocationEntry;
4use wasmparser::SectionLimited;
5
6use super::CustomSectionReader;
7use crate::index::SectionId;
8
9#[derive(Default, Debug)]
10/// Information stored in "reloc.*" sections
11pub struct Relocation {
12    ///
13    /// "reloc.*" sections.
14    /// The key is an index to which relocation entry corresponds
15    pub relocs: VecMap<RelocationSection>,
16}
17impl Relocation {
18    pub(super) fn push_section(&mut self, reader: wasmparser::RelocSectionReader) -> Result<()> {
19        let exist = self.relocs.insert(
20            reader.section_index() as SectionId,
21            RelocationSection::read(reader.entries())?,
22        );
23        if exist.is_some() {
24            log::error!(
25                "duplicate reloc section for index {}",
26                reader.section_index()
27            );
28        }
29        Ok(())
30    }
31    pub fn get_section(&self, index: SectionId) -> Option<&RelocationSection> {
32        self.relocs.get(index)
33    }
34}
35
36#[derive(Default, Debug)]
37pub struct RelocationSection {
38    pub entries: Vec<RelocationEntry>,
39}
40
41impl<'a> CustomSectionReader<'a> for RelocationSection {
42    type Reader = SectionLimited<'a, RelocationEntry>;
43
44    fn read(reader: Self::Reader) -> Result<Self> {
45        let entries = reader.into_iter().collect::<Result<Vec<_>, _>>()?;
46        Ok(RelocationSection { entries })
47    }
48}