wasmparser/readers/core/
dylink0.rs

1use crate::prelude::*;
2use crate::{BinaryReader, Result, Subsection, Subsections, SymbolFlags};
3use core::ops::Range;
4
5/// Parser for the dynamic linking `dylink.0` custom section.
6///
7/// This format is currently defined upstream at
8/// <https://github.com/WebAssembly/tool-conventions/blob/main/DynamicLinking.md>.
9pub type Dylink0SectionReader<'a> = Subsections<'a, Dylink0Subsection<'a>>;
10
11const WASM_DYLINK_MEM_INFO: u8 = 1;
12const WASM_DYLINK_NEEDED: u8 = 2;
13const WASM_DYLINK_EXPORT_INFO: u8 = 3;
14const WASM_DYLINK_IMPORT_INFO: u8 = 4;
15const WASM_DYLINK_RUNTIME_PATH: u8 = 5;
16
17/// Represents a `WASM_DYLINK_MEM_INFO` field
18#[derive(Debug, Copy, Clone)]
19pub struct MemInfo {
20    /// Size of the memory area the loader should reserve for the module, which
21    /// will begin at `env.__memory_base`.
22    pub memory_size: u32,
23
24    /// The required alignment of the memory area, in bytes, encoded as a power
25    /// of 2.
26    pub memory_alignment: u32,
27
28    /// Size of the table area the loader should reserve for the module, which
29    /// will begin at `env.__table_base`.
30    pub table_size: u32,
31
32    /// The required alignment of the table area, in elements, encoded as a
33    /// power of 2.
34    pub table_alignment: u32,
35}
36
37#[allow(missing_docs)]
38#[derive(Debug)]
39pub struct ExportInfo<'a> {
40    pub name: &'a str,
41    pub flags: SymbolFlags,
42}
43
44#[allow(missing_docs)]
45#[derive(Debug)]
46pub struct ImportInfo<'a> {
47    pub module: &'a str,
48    pub field: &'a str,
49    pub flags: SymbolFlags,
50}
51
52/// Possible subsections of the `dylink.0` custom section.
53#[derive(Debug)]
54#[allow(missing_docs)]
55pub enum Dylink0Subsection<'a> {
56    MemInfo(MemInfo),
57    Needed(Vec<&'a str>),
58    ExportInfo(Vec<ExportInfo<'a>>),
59    ImportInfo(Vec<ImportInfo<'a>>),
60    RuntimePath(Vec<&'a str>),
61    Unknown {
62        ty: u8,
63        data: &'a [u8],
64        range: Range<usize>,
65    },
66}
67
68impl<'a> Subsection<'a> for Dylink0Subsection<'a> {
69    fn from_reader(id: u8, mut reader: BinaryReader<'a>) -> Result<Self> {
70        let data = reader.remaining_buffer();
71        let offset = reader.original_position();
72        Ok(match id {
73            WASM_DYLINK_MEM_INFO => Self::MemInfo(MemInfo {
74                memory_size: reader.read_var_u32()?,
75                memory_alignment: reader.read_var_u32()?,
76                table_size: reader.read_var_u32()?,
77                table_alignment: reader.read_var_u32()?,
78            }),
79            WASM_DYLINK_NEEDED => Self::Needed(
80                (0..reader.read_var_u32()?)
81                    .map(|_| reader.read_string())
82                    .collect::<Result<_, _>>()?,
83            ),
84            WASM_DYLINK_EXPORT_INFO => Self::ExportInfo(
85                (0..reader.read_var_u32()?)
86                    .map(|_| {
87                        Ok(ExportInfo {
88                            name: reader.read_string()?,
89                            flags: reader.read()?,
90                        })
91                    })
92                    .collect::<Result<_, _>>()?,
93            ),
94            WASM_DYLINK_IMPORT_INFO => Self::ImportInfo(
95                (0..reader.read_var_u32()?)
96                    .map(|_| {
97                        Ok(ImportInfo {
98                            module: reader.read_string()?,
99                            field: reader.read_string()?,
100                            flags: reader.read()?,
101                        })
102                    })
103                    .collect::<Result<_, _>>()?,
104            ),
105            WASM_DYLINK_RUNTIME_PATH => Self::RuntimePath(
106                (0..reader.read_var_u32()?)
107                    .map(|_| reader.read_string())
108                    .collect::<Result<_, _>>()?,
109            ),
110            ty => Self::Unknown {
111                ty,
112                data,
113                range: offset..offset + data.len(),
114            },
115        })
116    }
117}