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)]
10pub struct Relocation {
12 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}