]> git.lizzy.rs Git - rust.git/blob - compiler/rustc_codegen_cranelift/src/debuginfo/unwind.rs
Auto merge of #104334 - compiler-errors:ufcs-sugg-wrong-def-id, r=estebank
[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) =
43             context.compiled_code().unwrap().create_unwind_info(isa).unwrap()
44         {
45             unwind_info
46         } else {
47             return;
48         };
49
50         match unwind_info {
51             UnwindInfo::SystemV(unwind_info) => {
52                 self.frame_table.add_fde(
53                     self.cie_id.unwrap(),
54                     unwind_info
55                         .to_fde(Address::Symbol { symbol: func_id.as_u32() as usize, addend: 0 }),
56                 );
57             }
58             UnwindInfo::WindowsX64(_) => {
59                 // FIXME implement this
60             }
61             unwind_info => unimplemented!("{:?}", unwind_info),
62         }
63     }
64
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();
68
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);
74
75             for reloc in &eh_frame.0.relocs {
76                 product.add_debug_reloc(&section_map, &section_id, reloc);
77             }
78         }
79     }
80
81     #[cfg(all(feature = "jit", windows))]
82     pub(crate) unsafe fn register_jit(self, _jit_module: &cranelift_jit::JITModule) {}
83
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;
87
88         let mut eh_frame = EhFrame::from(super::emit::WriterRelocate::new(self.endian));
89         self.frame_table.write_eh_frame(&mut eh_frame).unwrap();
90
91         if eh_frame.0.writer.slice().is_empty() {
92             return;
93         }
94
95         let mut eh_frame = eh_frame.0.relocate_for_jit(jit_module);
96
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]);
99
100         // FIXME support unregistering unwind tables once cranelift-jit supports deallocating
101         // individual functions
102         let eh_frame = ManuallyDrop::new(eh_frame);
103
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")]
108         {
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;
113
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;
117
118                 // Skip over the CIE
119                 if current != start {
120                     __register_frame(current);
121                 }
122
123                 // Move to the next table entry (+4 because the length itself is not inclusive)
124                 current = current.add(len + 4);
125             }
126         }
127         #[cfg(not(target_os = "macos"))]
128         {
129             // On other platforms, `__register_frame` will walk the FDEs until an entry of length 0
130             __register_frame(eh_frame.as_ptr());
131         }
132     }
133 }
134
135 extern "C" {
136     // libunwind import
137     fn __register_frame(fde: *const u8);
138 }