Skip to main content

wamex_cli/read/
linking.rs

1use std::fmt::Debug;
2
3use anyhow::Result;
4use wasmparser::{Comdat, InitFunc, Segment};
5
6use super::CustomSectionReader;
7
8#[allow(dead_code)]
9pub mod section {
10    use std::ops::Range;
11
12    #[derive(Default, Debug)]
13    pub struct UnknownInfo<'a> {
14        /// The identifier for this subsection.
15        pub ty: u8,
16        /// The contents of this subsection.
17        pub data: &'a [u8],
18        /// The range of bytes, relative to the start of the original data
19        /// stream, that the contents of this subsection reside in.
20        pub range: Range<usize>,
21    }
22}
23
24/// Store information by symbol type
25#[derive(Debug, Default)]
26pub struct LinkingSymbolsInfo<'a> {
27    pub symbols: Vec<wasmparser::SymbolInfo<'a>>,
28}
29
30impl<'a> LinkingSymbolsInfo<'a> {
31    fn try_from_reader(map: wasmparser::SymbolInfoMap<'a>) -> Result<Self> {
32        let mut info = Self::default();
33        for sym_info in map.into_iter() {
34            info.symbols.push(sym_info?);
35        }
36        Ok(info)
37    }
38}
39
40#[derive(Default, Debug)]
41pub struct LinkingInfo<'a> {
42    pub segments_info: Vec<Segment<'a>>,
43    pub init_funcs: Vec<InitFunc>,
44    pub comdat_info: Vec<Comdat<'a>>,
45    pub linking_symbols: LinkingSymbolsInfo<'a>,
46    pub unknown_linking: Vec<section::UnknownInfo<'a>>,
47}
48
49impl<'a> CustomSectionReader<'a> for LinkingInfo<'a> {
50    type Reader = wasmparser::LinkingSectionReader<'a>;
51
52    fn read(reader: Self::Reader) -> Result<Self> {
53        use wasmparser::Linking;
54        let mut linking = LinkingInfo::default();
55        for subsection in reader.subsections() {
56            match subsection? {
57                Linking::SegmentInfo(s) => {
58                    linking
59                        .segments_info
60                        .extend(&s.into_iter().collect::<Result<Vec<_>, _>>()?);
61                }
62                Linking::InitFuncs(i) => {
63                    linking
64                        .init_funcs
65                        .extend(&i.into_iter().collect::<Result<Vec<_>, _>>()?);
66                }
67                Linking::ComdatInfo(c) => {
68                    linking
69                        .comdat_info
70                        .extend(c.into_iter().collect::<Result<Vec<_>, _>>()?);
71                }
72                Linking::SymbolTable(map) => {
73                    linking.linking_symbols = LinkingSymbolsInfo::try_from_reader(map)?;
74                }
75                Linking::Unknown { ty, data, range } => {
76                    linking
77                        .unknown_linking
78                        .push(section::UnknownInfo { ty, data, range });
79                }
80            }
81        }
82
83        Ok(linking)
84    }
85}