1 //! Unwind info generation (`.eh_frame`)
5 use cranelift_codegen::isa::{unwind::UnwindInfo, TargetIsa};
7 use gimli::write::{Address, CieId, EhFrame, FrameTable, Section};
8 use gimli::RunTimeEndian;
10 use crate::backend::WriteDebugInfo;
12 pub(crate) struct UnwindContext {
13 endian: RunTimeEndian,
14 frame_table: FrameTable,
15 cie_id: Option<CieId>,
19 pub(crate) fn new(tcx: TyCtxt<'_>, isa: &dyn TargetIsa, pic_eh_frame: bool) -> Self {
20 let endian = super::target_endian(tcx);
21 let mut frame_table = FrameTable::default();
23 let cie_id = if let Some(mut cie) = isa.create_systemv_cie() {
25 cie.fde_address_encoding =
26 gimli::DwEhPe(gimli::DW_EH_PE_pcrel.0 | gimli::DW_EH_PE_sdata4.0);
28 Some(frame_table.add_cie(cie))
33 UnwindContext { endian, frame_table, cie_id }
36 pub(crate) fn add_function(&mut self, func_id: FuncId, context: &Context, isa: &dyn TargetIsa) {
37 let unwind_info = if let Some(unwind_info) = context.create_unwind_info(isa).unwrap() {
44 UnwindInfo::SystemV(unwind_info) => {
45 self.frame_table.add_fde(
48 .to_fde(Address::Symbol { symbol: func_id.as_u32() as usize, addend: 0 }),
51 UnwindInfo::WindowsX64(_) => {
52 // FIXME implement this
54 unwind_info => unimplemented!("{:?}", unwind_info),
58 pub(crate) fn emit<P: WriteDebugInfo>(self, product: &mut P) {
59 let mut eh_frame = EhFrame::from(super::emit::WriterRelocate::new(self.endian));
60 self.frame_table.write_eh_frame(&mut eh_frame).unwrap();
62 if !eh_frame.0.writer.slice().is_empty() {
63 let id = eh_frame.id();
64 let section_id = product.add_debug_section(id, eh_frame.0.writer.into_vec());
65 let mut section_map = FxHashMap::default();
66 section_map.insert(id, section_id);
68 for reloc in &eh_frame.0.relocs {
69 product.add_debug_reloc(§ion_map, §ion_id, reloc);
74 #[cfg(all(feature = "jit", windows))]
75 pub(crate) unsafe fn register_jit(self, _jit_module: &cranelift_jit::JITModule) {}
77 #[cfg(all(feature = "jit", not(windows)))]
78 pub(crate) unsafe fn register_jit(self, jit_module: &cranelift_jit::JITModule) {
79 let mut eh_frame = EhFrame::from(super::emit::WriterRelocate::new(self.endian));
80 self.frame_table.write_eh_frame(&mut eh_frame).unwrap();
82 if eh_frame.0.writer.slice().is_empty() {
86 let mut eh_frame = eh_frame.0.relocate_for_jit(jit_module);
88 // GCC expects a terminating "empty" length, so write a 0 length at the end of the table.
89 eh_frame.extend(&[0, 0, 0, 0]);
91 // FIXME support unregistering unwind tables once cranelift-jit supports deallocating
92 // individual functions
93 #[allow(unused_variables)]
94 let (eh_frame, eh_frame_len, _) = Vec::into_raw_parts(eh_frame);
96 // =======================================================================
97 // Everything after this line up to the end of the file is loosly based on
98 // https://github.com/bytecodealliance/wasmtime/blob/4471a82b0c540ff48960eca6757ccce5b1b5c3e4/crates/jit/src/unwind/systemv.rs
99 #[cfg(target_os = "macos")]
101 // On macOS, `__register_frame` takes a pointer to a single FDE
102 let start = eh_frame;
103 let end = start.add(eh_frame_len);
104 let mut current = start;
106 // Walk all of the entries in the frame table and register them
107 while current < end {
108 let len = std::ptr::read::<u32>(current as *const u32) as usize;
111 if current != start {
112 __register_frame(current);
115 // Move to the next table entry (+4 because the length itself is not inclusive)
116 current = current.add(len + 4);
119 #[cfg(not(target_os = "macos"))]
121 // On other platforms, `__register_frame` will walk the FDEs until an entry of length 0
122 __register_frame(eh_frame);
129 fn __register_frame(fde: *const u8);