1 //! Unwind info generation (`.eh_frame`)
5 use cranelift_codegen::ir::Endianness;
6 use cranelift_codegen::isa::{unwind::UnwindInfo, TargetIsa};
8 use cranelift_object::ObjectProduct;
9 use gimli::write::{Address, CieId, EhFrame, FrameTable, Section};
10 use gimli::RunTimeEndian;
12 use super::object::WriteDebugInfo;
14 pub(crate) struct UnwindContext {
15 endian: RunTimeEndian,
16 frame_table: FrameTable,
17 cie_id: Option<CieId>,
21 pub(crate) fn new(isa: &dyn TargetIsa, pic_eh_frame: bool) -> Self {
22 let endian = match isa.endianness() {
23 Endianness::Little => RunTimeEndian::Little,
24 Endianness::Big => RunTimeEndian::Big,
26 let mut frame_table = FrameTable::default();
28 let cie_id = if let Some(mut cie) = isa.create_systemv_cie() {
30 cie.fde_address_encoding =
31 gimli::DwEhPe(gimli::DW_EH_PE_pcrel.0 | gimli::DW_EH_PE_sdata4.0);
33 Some(frame_table.add_cie(cie))
38 UnwindContext { endian, frame_table, cie_id }
41 pub(crate) fn add_function(&mut self, func_id: FuncId, context: &Context, isa: &dyn TargetIsa) {
42 let unwind_info = if let Some(unwind_info) =
43 context.compiled_code().unwrap().create_unwind_info(isa).unwrap()
51 UnwindInfo::SystemV(unwind_info) => {
52 self.frame_table.add_fde(
55 .to_fde(Address::Symbol { symbol: func_id.as_u32() as usize, addend: 0 }),
58 UnwindInfo::WindowsX64(_) => {
59 // FIXME implement this
61 unwind_info => unimplemented!("{:?}", unwind_info),
65 pub(crate) fn emit(self, product: &mut ObjectProduct) {
66 let mut eh_frame = EhFrame::from(super::emit::WriterRelocate::new(self.endian));
67 self.frame_table.write_eh_frame(&mut eh_frame).unwrap();
69 if !eh_frame.0.writer.slice().is_empty() {
70 let id = eh_frame.id();
71 let section_id = product.add_debug_section(id, eh_frame.0.writer.into_vec());
72 let mut section_map = FxHashMap::default();
73 section_map.insert(id, section_id);
75 for reloc in &eh_frame.0.relocs {
76 product.add_debug_reloc(§ion_map, §ion_id, reloc);
81 #[cfg(all(feature = "jit", windows))]
82 pub(crate) unsafe fn register_jit(self, _jit_module: &cranelift_jit::JITModule) {}
84 #[cfg(all(feature = "jit", not(windows)))]
85 pub(crate) unsafe fn register_jit(self, jit_module: &cranelift_jit::JITModule) {
86 use std::mem::ManuallyDrop;
88 let mut eh_frame = EhFrame::from(super::emit::WriterRelocate::new(self.endian));
89 self.frame_table.write_eh_frame(&mut eh_frame).unwrap();
91 if eh_frame.0.writer.slice().is_empty() {
95 let mut eh_frame = eh_frame.0.relocate_for_jit(jit_module);
97 // GCC expects a terminating "empty" length, so write a 0 length at the end of the table.
98 eh_frame.extend(&[0, 0, 0, 0]);
100 // FIXME support unregistering unwind tables once cranelift-jit supports deallocating
101 // individual functions
102 let eh_frame = ManuallyDrop::new(eh_frame);
104 // =======================================================================
105 // Everything after this line up to the end of the file is loosely based on
106 // https://github.com/bytecodealliance/wasmtime/blob/4471a82b0c540ff48960eca6757ccce5b1b5c3e4/crates/jit/src/unwind/systemv.rs
107 #[cfg(target_os = "macos")]
109 // On macOS, `__register_frame` takes a pointer to a single FDE
110 let start = eh_frame.as_ptr();
111 let end = start.add(eh_frame.len());
112 let mut current = start;
114 // Walk all of the entries in the frame table and register them
115 while current < end {
116 let len = std::ptr::read::<u32>(current as *const u32) as usize;
119 if current != start {
120 __register_frame(current);
123 // Move to the next table entry (+4 because the length itself is not inclusive)
124 current = current.add(len + 4);
127 #[cfg(not(target_os = "macos"))]
129 // On other platforms, `__register_frame` will walk the FDEs until an entry of length 0
130 __register_frame(eh_frame.as_ptr());
137 fn __register_frame(fde: *const u8);