]> git.lizzy.rs Git - rust.git/blob - compiler/rustc_codegen_cranelift/src/debuginfo/unwind.rs
Auto merge of #95254 - jyn514:fix-windows-builds, r=Mark-Simulacrum
[rust.git] / compiler / rustc_codegen_cranelift / src / debuginfo / unwind.rs
1 //! Unwind info generation (`.eh_frame`)
2
3 use crate::prelude::*;
4
5 use cranelift_codegen::ir::Endianness;
6 use cranelift_codegen::isa::{unwind::UnwindInfo, TargetIsa};
7
8 use cranelift_object::ObjectProduct;
9 use gimli::write::{Address, CieId, EhFrame, FrameTable, Section};
10 use gimli::RunTimeEndian;
11
12 use super::object::WriteDebugInfo;
13
14 pub(crate) struct UnwindContext {
15     endian: RunTimeEndian,
16     frame_table: FrameTable,
17     cie_id: Option<CieId>,
18 }
19
20 impl UnwindContext {
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,
25         };
26         let mut frame_table = FrameTable::default();
27
28         let cie_id = if let Some(mut cie) = isa.create_systemv_cie() {
29             if pic_eh_frame {
30                 cie.fde_address_encoding =
31                     gimli::DwEhPe(gimli::DW_EH_PE_pcrel.0 | gimli::DW_EH_PE_sdata4.0);
32             }
33             Some(frame_table.add_cie(cie))
34         } else {
35             None
36         };
37
38         UnwindContext { endian, frame_table, cie_id }
39     }
40
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() {
43             unwind_info
44         } else {
45             return;
46         };
47
48         match unwind_info {
49             UnwindInfo::SystemV(unwind_info) => {
50                 self.frame_table.add_fde(
51                     self.cie_id.unwrap(),
52                     unwind_info
53                         .to_fde(Address::Symbol { symbol: func_id.as_u32() as usize, addend: 0 }),
54                 );
55             }
56             UnwindInfo::WindowsX64(_) => {
57                 // FIXME implement this
58             }
59             unwind_info => unimplemented!("{:?}", unwind_info),
60         }
61     }
62
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();
66
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);
72
73             for reloc in &eh_frame.0.relocs {
74                 product.add_debug_reloc(&section_map, &section_id, reloc);
75             }
76         }
77     }
78
79     #[cfg(all(feature = "jit", windows))]
80     pub(crate) unsafe fn register_jit(self, _jit_module: &cranelift_jit::JITModule) {}
81
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;
85
86         let mut eh_frame = EhFrame::from(super::emit::WriterRelocate::new(self.endian));
87         self.frame_table.write_eh_frame(&mut eh_frame).unwrap();
88
89         if eh_frame.0.writer.slice().is_empty() {
90             return;
91         }
92
93         let mut eh_frame = eh_frame.0.relocate_for_jit(jit_module);
94
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]);
97
98         // FIXME support unregistering unwind tables once cranelift-jit supports deallocating
99         // individual functions
100         let eh_frame = ManuallyDrop::new(eh_frame);
101
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")]
106         {
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;
111
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;
115
116                 // Skip over the CIE
117                 if current != start {
118                     __register_frame(current);
119                 }
120
121                 // Move to the next table entry (+4 because the length itself is not inclusive)
122                 current = current.add(len + 4);
123             }
124         }
125         #[cfg(not(target_os = "macos"))]
126         {
127             // On other platforms, `__register_frame` will walk the FDEs until an entry of length 0
128             __register_frame(eh_frame.as_ptr());
129         }
130     }
131 }
132
133 extern "C" {
134     // libunwind import
135     fn __register_frame(fde: *const u8);
136 }