Stream: cranelift

Topic: ✔ How to use declare_data_in_data and write_data_addr?


view this post on Zulip Adel Prokurov (Aug 29 2025 at 05:30):

Hello! I am writing a compiler for Scheme and have to initialize GLOBALS array for GC to trace globals. This array is initialized like this:

        let globals_array = self
            .module
            .declare_data("CAPY_GLOBALS", Linkage::Export, false, false)
            .unwrap();
        let mut desc = DataDescription::new();
        desc.set_align(size_of::<usize>() as _);
        let datas = self
            .constants
            .values()
            .copied()
            .chain(self.cache_cells.values().copied());
        let mut offset = 0;
        for data in datas {
            let gv = self.module.declare_data_in_data(data, &mut desc);

            desc.write_data_addr(offset, gv, 0);
            offset += 8;
        }
        desc.define_zeroinit(offset as usize);

        self.module.define_data(globals_array, &desc).unwrap();

but when I load produced shared-library using dlopen and then dlsym the array it is not populated with anyfing but fully zeroed, is that expected?

view this post on Zulip Adel Prokurov (Aug 29 2025 at 05:30):

what would be the correct way to put addresses of all constants into that array?

view this post on Zulip Adel Prokurov (Aug 29 2025 at 05:34):

Oh apparently I had to use .define(vec![0; offset]) instead of zeroinit, that makes this thing work properly.

view this post on Zulip Notification Bot (Aug 29 2025 at 05:34):

Adel Prokurov has marked this topic as resolved.


Last updated: Dec 06 2025 at 07:03 UTC