Wasm: Parse the Name section, export init_refcount_test, and don't DCE exports

This commit is contained in:
Brian Carroll 2022-01-14 13:38:55 +00:00
parent 3d00217b53
commit c7da7ca689
6 changed files with 242 additions and 71 deletions

View file

@ -11,13 +11,17 @@ pub use linking::SymInfo;
use roc_reporting::internal_error;
pub use sections::{ConstExpr, Export, ExportType, Global, GlobalType, Signature};
use crate::wasm_module::serialize::SkipBytes;
use self::linking::{LinkingSection, RelocationSection};
use self::sections::{
CodeSection, DataSection, ExportSection, FunctionSection, GlobalSection, ImportSection,
MemorySection, OpaqueSection, Section, SectionId, TypeSection,
MemorySection, NameSection, OpaqueSection, Section, SectionId, TypeSection,
};
use self::serialize::{SerialBuffer, Serialize};
/// A representation of the WebAssembly binary file format
/// https://webassembly.github.io/spec/core/binary/modules.html
#[derive(Debug)]
pub struct WasmModule<'a> {
pub types: TypeSection<'a>,
@ -31,6 +35,7 @@ pub struct WasmModule<'a> {
pub element: OpaqueSection<'a>,
pub code: CodeSection<'a>,
pub data: DataSection<'a>,
pub names: NameSection<'a>,
pub linking: LinkingSection<'a>,
pub relocations: RelocationSection<'a>,
}
@ -128,15 +133,23 @@ impl<'a> WasmModule<'a> {
let ret_types = types.parse_preloaded_data(arena);
let import = ImportSection::preload(arena, bytes, &mut cursor);
let function = FunctionSection::preload(arena, bytes, &mut cursor);
let signature_ids = function.parse_preloaded_data(arena);
let table = OpaqueSection::preload(SectionId::Table, arena, bytes, &mut cursor);
let memory = MemorySection::preload(arena, bytes, &mut cursor);
let global = GlobalSection::preload(arena, bytes, &mut cursor);
let export = ExportSection::preload(arena, bytes, &mut cursor);
ExportSection::skip_bytes(bytes, &mut cursor);
let export = ExportSection::empty(arena);
let start = OpaqueSection::preload(SectionId::Start, arena, bytes, &mut cursor);
let element = OpaqueSection::preload(SectionId::Element, arena, bytes, &mut cursor);
let code = CodeSection::preload(
arena,
bytes,
@ -145,7 +158,11 @@ impl<'a> WasmModule<'a> {
signature_ids,
import.function_count,
);
let data = DataSection::preload(arena, bytes, &mut cursor);
// Metadata sections
let names = NameSection::parse(arena, bytes, &mut cursor);
let linking = LinkingSection::new(arena);
let relocations = RelocationSection::new(arena, "reloc.CODE");
@ -161,10 +178,24 @@ impl<'a> WasmModule<'a> {
element,
code,
data,
names,
linking,
relocations,
}
}
pub fn remove_dead_preloads<T: IntoIterator<Item = u32>>(
&mut self,
arena: &'a Bump,
called_preload_fns: T,
) {
self.code.remove_dead_preloads(
arena,
self.import.function_count,
&self.export.function_indices,
called_preload_fns,
)
}
}
/// Helper struct to count non-empty sections.