1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
use crate::prelude::*;
use crate::{BinaryReader, Result, Subsection, Subsections, SymbolFlags};
use core::ops::Range;

/// Parser for the dynamic linking `dylink.0` custom section.
///
/// This format is currently defined upstream at
/// <https://github.com/WebAssembly/tool-conventions/blob/main/DynamicLinking.md>.
pub type Dylink0SectionReader<'a> = Subsections<'a, Dylink0Subsection<'a>>;

const WASM_DYLINK_MEM_INFO: u8 = 1;
const WASM_DYLINK_NEEDED: u8 = 2;
const WASM_DYLINK_EXPORT_INFO: u8 = 3;
const WASM_DYLINK_IMPORT_INFO: u8 = 4;

/// Represents a `WASM_DYLINK_MEM_INFO` field
#[derive(Debug, Copy, Clone)]
pub struct MemInfo {
    /// Size of the memory area the loader should reserve for the module, which
    /// will begin at `env.__memory_base`.
    pub memory_size: u32,

    /// The required alignment of the memory area, in bytes, encoded as a power
    /// of 2.
    pub memory_alignment: u32,

    /// Size of the table area the loader should reserve for the module, which
    /// will begin at `env.__table_base`.
    pub table_size: u32,

    /// The required alignment of the table area, in elements, encoded as a
    /// power of 2.
    pub table_alignment: u32,
}

#[allow(missing_docs)]
#[derive(Debug)]
pub struct ExportInfo<'a> {
    pub name: &'a str,
    pub flags: SymbolFlags,
}

#[allow(missing_docs)]
#[derive(Debug)]
pub struct ImportInfo<'a> {
    pub module: &'a str,
    pub field: &'a str,
    pub flags: SymbolFlags,
}

/// Possible subsections of the `dylink.0` custom section.
#[derive(Debug)]
#[allow(missing_docs)]
pub enum Dylink0Subsection<'a> {
    MemInfo(MemInfo),
    Needed(Vec<&'a str>),
    ExportInfo(Vec<ExportInfo<'a>>),
    ImportInfo(Vec<ImportInfo<'a>>),
    Unknown {
        ty: u8,
        data: &'a [u8],
        range: Range<usize>,
    },
}

impl<'a> Subsection<'a> for Dylink0Subsection<'a> {
    fn from_reader(id: u8, mut reader: BinaryReader<'a>) -> Result<Self> {
        let data = reader.remaining_buffer();
        let offset = reader.original_position();
        Ok(match id {
            WASM_DYLINK_MEM_INFO => Self::MemInfo(MemInfo {
                memory_size: reader.read_var_u32()?,
                memory_alignment: reader.read_var_u32()?,
                table_size: reader.read_var_u32()?,
                table_alignment: reader.read_var_u32()?,
            }),
            WASM_DYLINK_NEEDED => Self::Needed(
                (0..reader.read_var_u32()?)
                    .map(|_| reader.read_string())
                    .collect::<Result<_, _>>()?,
            ),
            WASM_DYLINK_EXPORT_INFO => Self::ExportInfo(
                (0..reader.read_var_u32()?)
                    .map(|_| {
                        Ok(ExportInfo {
                            name: reader.read_string()?,
                            flags: reader.read()?,
                        })
                    })
                    .collect::<Result<_, _>>()?,
            ),
            WASM_DYLINK_IMPORT_INFO => Self::ImportInfo(
                (0..reader.read_var_u32()?)
                    .map(|_| {
                        Ok(ImportInfo {
                            module: reader.read_string()?,
                            field: reader.read_string()?,
                            flags: reader.read()?,
                        })
                    })
                    .collect::<Result<_, _>>()?,
            ),
            ty => Self::Unknown {
                ty,
                data,
                range: offset..offset + data.len(),
            },
        })
    }
}