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) = context.create_unwind_info(isa).unwrap() {
49 UnwindInfo::SystemV(unwind_info) => {
50 self.frame_table.add_fde(
53 .to_fde(Address::Symbol { symbol: func_id.as_u32() as usize, addend: 0 }),
56 UnwindInfo::WindowsX64(_) => {
57 // FIXME implement this
59 unwind_info => unimplemented!("{:?}", unwind_info),
63 pub(crate) fn emit(self, product: &mut ObjectProduct) {
64 let mut eh_frame = EhFrame::from(super::emit::WriterRelocate::new(self.endian));
65 self.frame_table.write_eh_frame(&mut eh_frame).unwrap();
67 if !eh_frame.0.writer.slice().is_empty() {
68 let id = eh_frame.id();
69 let section_id = product.add_debug_section(id, eh_frame.0.writer.into_vec());
70 let mut section_map = FxHashMap::default();
71 section_map.insert(id, section_id);
73 for reloc in &eh_frame.0.relocs {
74 product.add_debug_reloc(§ion_map, §ion_id, reloc);
79 #[cfg(all(feature = "jit", windows))]
80 pub(crate) unsafe fn register_jit(self, _jit_module: &cranelift_jit::JITModule) {}
82 #[cfg(all(feature = "jit", not(windows)))]
83 pub(crate) unsafe fn register_jit(self, jit_module: &cranelift_jit::JITModule) {
84 use std::mem::ManuallyDrop;
86 let mut eh_frame = EhFrame::from(super::emit::WriterRelocate::new(self.endian));
87 self.frame_table.write_eh_frame(&mut eh_frame).unwrap();
89 if eh_frame.0.writer.slice().is_empty() {
93 let mut eh_frame = eh_frame.0.relocate_for_jit(jit_module);
95 // GCC expects a terminating "empty" length, so write a 0 length at the end of the table.
96 eh_frame.extend(&[0, 0, 0, 0]);
98 // FIXME support unregistering unwind tables once cranelift-jit supports deallocating
99 // individual functions
100 let eh_frame = ManuallyDrop::new(eh_frame);
102 // =======================================================================
103 // Everything after this line up to the end of the file is loosely based on
104 // https://github.com/bytecodealliance/wasmtime/blob/4471a82b0c540ff48960eca6757ccce5b1b5c3e4/crates/jit/src/unwind/systemv.rs
105 #[cfg(target_os = "macos")]
107 // On macOS, `__register_frame` takes a pointer to a single FDE
108 let start = eh_frame.as_ptr();
109 let end = start.add(eh_frame.len());
110 let mut current = start;
112 // Walk all of the entries in the frame table and register them
113 while current < end {
114 let len = std::ptr::read::<u32>(current as *const u32) as usize;
117 if current != start {
118 __register_frame(current);
121 // Move to the next table entry (+4 because the length itself is not inclusive)
122 current = current.add(len + 4);
125 #[cfg(not(target_os = "macos"))]
127 // On other platforms, `__register_frame` will walk the FDEs until an entry of length 0
128 __register_frame(eh_frame.as_ptr());
135 fn __register_frame(fde: *const u8);