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
use anyhow::Result;
use object::write::{Object, StandardSection, Symbol, SymbolSection};
use object::{SymbolFlags, SymbolKind, SymbolScope};
use wasmtime_environ::DataInitializer;

/// Declares data segment symbol
pub fn declare_data_segment(
    obj: &mut Object,
    _data_initaliazer: &DataInitializer,
    index: usize,
) -> Result<()> {
    let name = format!("_memory_{}", index);
    let _symbol_id = obj.add_symbol(Symbol {
        name: name.as_bytes().to_vec(),
        value: 0,
        size: 0,
        kind: SymbolKind::Data,
        scope: SymbolScope::Linkage,
        weak: false,
        section: SymbolSection::Undefined,
        flags: SymbolFlags::None,
    });
    Ok(())
}

/// Emit segment data and initialization location
pub fn emit_data_segment(
    obj: &mut Object,
    data_initaliazer: &DataInitializer,
    index: usize,
) -> Result<()> {
    let name = format!("_memory_{}", index);
    let symbol_id = obj.symbol_id(name.as_bytes()).unwrap();
    let section_id = obj.section_id(StandardSection::Data);
    obj.add_symbol_data(symbol_id, section_id, data_initaliazer.data, 1);
    Ok(())
}