Skip to content

Commit 143aacb

Browse files
authored
Remove duplicate dylink.0 section parser (#1546)
This one predates the one in wasmparser and forgot to remove this one when the other was added.
1 parent 58194f4 commit 143aacb

1 file changed

Lines changed: 15 additions & 101 deletions

File tree

crates/wit-component/src/linking/metadata.rs

Lines changed: 15 additions & 101 deletions
Original file line numberDiff line numberDiff line change
@@ -8,16 +8,11 @@ use {
88
fmt,
99
},
1010
wasmparser::{
11-
BinaryReader, BinaryReaderError, ExternalKind, FuncType, KnownCustom, Parser, Payload,
12-
RefType, Subsection, Subsections, SymbolFlags, TableType, TypeRef, ValType,
11+
Dylink0Subsection, ExternalKind, FuncType, KnownCustom, MemInfo, Parser, Payload, RefType,
12+
SymbolFlags, TableType, TypeRef, ValType,
1313
},
1414
};
1515

16-
pub const WASM_DYLINK_MEM_INFO: u8 = 1;
17-
pub const WASM_DYLINK_NEEDED: u8 = 2;
18-
pub const WASM_DYLINK_EXPORT_INFO: u8 = 3;
19-
pub const WASM_DYLINK_IMPORT_INFO: u8 = 4;
20-
2116
/// Represents a core Wasm value type (not including V128 or reference types, which are not yet supported)
2217
#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash, PartialOrd, Ord)]
2318
pub enum ValueType {
@@ -155,26 +150,6 @@ pub struct Export<'a> {
155150
pub flags: SymbolFlags,
156151
}
157152

158-
/// Represents a `WASM_DYLINK_MEM_INFO` value
159-
#[derive(Debug, Copy, Clone)]
160-
pub struct MemInfo {
161-
pub memory_size: u32,
162-
pub memory_alignment: u32,
163-
pub table_size: u32,
164-
pub table_alignment: u32,
165-
}
166-
167-
impl Default for MemInfo {
168-
fn default() -> Self {
169-
Self {
170-
memory_size: 0,
171-
memory_alignment: 1,
172-
table_size: 0,
173-
table_alignment: 1,
174-
}
175-
}
176-
}
177-
178153
/// Metadata extracted from a dynamic library module
179154
#[derive(Debug)]
180155
pub struct Metadata<'a> {
@@ -232,70 +207,6 @@ pub struct Metadata<'a> {
232207
pub imports: BTreeSet<Import<'a>>,
233208
}
234209

235-
#[derive(Debug)]
236-
struct ExportInfo<'a> {
237-
name: &'a str,
238-
flags: SymbolFlags,
239-
}
240-
241-
#[derive(Debug)]
242-
struct ImportInfo<'a> {
243-
module: &'a str,
244-
field: &'a str,
245-
flags: SymbolFlags,
246-
}
247-
248-
#[derive(Debug)]
249-
enum DylinkSubsection<'a> {
250-
MemInfo(MemInfo),
251-
Needed(Vec<&'a str>),
252-
ExportInfo(Vec<ExportInfo<'a>>),
253-
ImportInfo(Vec<ImportInfo<'a>>),
254-
Unknown(u8),
255-
}
256-
257-
type DylinkSectionReader<'a> = Subsections<'a, DylinkSubsection<'a>>;
258-
259-
impl<'a> Subsection<'a> for DylinkSubsection<'a> {
260-
fn from_reader(id: u8, mut reader: BinaryReader<'a>) -> Result<Self, BinaryReaderError> {
261-
Ok(match id {
262-
WASM_DYLINK_MEM_INFO => Self::MemInfo(MemInfo {
263-
memory_size: reader.read_var_u32()?,
264-
memory_alignment: reader.read_var_u32()?,
265-
table_size: reader.read_var_u32()?,
266-
table_alignment: reader.read_var_u32()?,
267-
}),
268-
WASM_DYLINK_NEEDED => Self::Needed(
269-
(0..reader.read_var_u32()?)
270-
.map(|_| reader.read_string())
271-
.collect::<Result<_, _>>()?,
272-
),
273-
WASM_DYLINK_EXPORT_INFO => Self::ExportInfo(
274-
(0..reader.read_var_u32()?)
275-
.map(|_| {
276-
Ok(ExportInfo {
277-
name: reader.read_string()?,
278-
flags: reader.read()?,
279-
})
280-
})
281-
.collect::<Result<_, _>>()?,
282-
),
283-
WASM_DYLINK_IMPORT_INFO => Self::ImportInfo(
284-
(0..reader.read_var_u32()?)
285-
.map(|_| {
286-
Ok(ImportInfo {
287-
module: reader.read_string()?,
288-
field: reader.read_string()?,
289-
flags: reader.read()?,
290-
})
291-
})
292-
.collect::<Result<_, _>>()?,
293-
),
294-
_ => Self::Unknown(id),
295-
})
296-
}
297-
}
298-
299210
impl<'a> Metadata<'a> {
300211
/// Parse the specified module and extract its metadata.
301212
pub fn try_new(
@@ -310,7 +221,12 @@ impl<'a> Metadata<'a> {
310221
let mut result = Self {
311222
name,
312223
dl_openable,
313-
mem_info: MemInfo::default(),
224+
mem_info: MemInfo {
225+
memory_size: 0,
226+
memory_alignment: 1,
227+
table_size: 0,
228+
table_alignment: 1,
229+
},
314230
needed_libs: Vec::new(),
315231
has_data_relocs: false,
316232
has_ctors: false,
@@ -334,27 +250,25 @@ impl<'a> Metadata<'a> {
334250
for payload in Parser::new(0).parse_all(module) {
335251
match payload? {
336252
Payload::CustomSection(section) => {
337-
if let KnownCustom::Dylink0(_) = section.as_known() {
338-
let reader =
339-
DylinkSectionReader::new(section.data(), section.data_offset());
253+
if let KnownCustom::Dylink0(reader) = section.as_known() {
340254
for subsection in reader {
341255
match subsection.context("failed to parse `dylink.0` subsection")? {
342-
DylinkSubsection::MemInfo(info) => result.mem_info = info,
343-
DylinkSubsection::Needed(needed) => {
256+
Dylink0Subsection::MemInfo(info) => result.mem_info = info,
257+
Dylink0Subsection::Needed(needed) => {
344258
result.needed_libs = needed.clone()
345259
}
346-
DylinkSubsection::ExportInfo(info) => {
260+
Dylink0Subsection::ExportInfo(info) => {
347261
export_info
348262
.extend(info.iter().map(|info| (info.name, info.flags)));
349263
}
350-
DylinkSubsection::ImportInfo(info) => {
264+
Dylink0Subsection::ImportInfo(info) => {
351265
import_info.extend(
352266
info.iter()
353267
.map(|info| ((info.module, info.field), info.flags)),
354268
);
355269
}
356-
DylinkSubsection::Unknown(index) => {
357-
bail!("unrecognized `dylink.0` subsection: {index}")
270+
Dylink0Subsection::Unknown { ty, .. } => {
271+
bail!("unrecognized `dylink.0` subsection: {ty}")
358272
}
359273
}
360274
}

0 commit comments

Comments
 (0)