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