Skip to main content

luaur_code_gen/methods/
unwind_builder_win_finalize.rs

1use crate::records::unwind_builder_win::UnwindBuilderWin;
2use crate::records::unwind_function_win::UnwindFunctionWin;
3use core::ffi::c_char;
4use core::ptr;
5
6impl UnwindBuilderWin {
7    pub fn finalize(
8        &self,
9        target: *mut c_char,
10        offset: usize,
11        _func_address: *mut core::ffi::c_void,
12        block_size: usize,
13    ) -> usize {
14        let mut current_target = target as *mut u8;
15        let k_full_block_function: u32 = 0xFFFFFFFF;
16
17        for func in &self.unwind_functions {
18            let mut adjusted_func = *func;
19
20            adjusted_func.begin_offset += offset as u32;
21
22            if adjusted_func.end_offset == k_full_block_function {
23                adjusted_func.end_offset = block_size as u32;
24            } else {
25                adjusted_func.end_offset += offset as u32;
26            }
27
28            adjusted_func.unwind_info_offset +=
29                (core::mem::size_of::<UnwindFunctionWin>() * self.unwind_functions.len()) as u32;
30
31            unsafe {
32                ptr::copy_nonoverlapping(
33                    &adjusted_func as *const UnwindFunctionWin as *const u8,
34                    current_target,
35                    core::mem::size_of::<UnwindFunctionWin>(),
36                );
37                current_target = current_target.add(core::mem::size_of::<UnwindFunctionWin>());
38            }
39        }
40
41        let raw_data_len =
42            unsafe { self.raw_data_pos.offset_from(self.raw_data.as_ptr()) } as usize;
43        unsafe {
44            ptr::copy_nonoverlapping(self.raw_data.as_ptr(), current_target, raw_data_len);
45        }
46
47        self.unwind_functions.len()
48    }
49}