]> git.lizzy.rs Git - rust.git/blob - compiler/rustc_codegen_cranelift/src/debuginfo/unwind.rs
Auto merge of #88454 - devnexen:sunos_asan, r=wesleywiser
[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::isa::{unwind::UnwindInfo, TargetIsa};
6
7 use gimli::write::{Address, CieId, EhFrame, FrameTable, Section};
8 use gimli::RunTimeEndian;
9
10 use crate::backend::WriteDebugInfo;
11
12 pub(crate) struct UnwindContext {
13     endian: RunTimeEndian,
14     frame_table: FrameTable,
15     cie_id: Option<CieId>,
16 }
17
18 impl UnwindContext {
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();
22
23         let cie_id = if let Some(mut cie) = isa.create_systemv_cie() {
24             if pic_eh_frame {
25                 cie.fde_address_encoding =
26                     gimli::DwEhPe(gimli::DW_EH_PE_pcrel.0 | gimli::DW_EH_PE_sdata4.0);
27             }
28             Some(frame_table.add_cie(cie))
29         } else {
30             None
31         };
32
33         UnwindContext { endian, frame_table, cie_id }
34     }
35
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() {
38             unwind_info
39         } else {
40             return;
41         };
42
43         match unwind_info {
44             UnwindInfo::SystemV(unwind_info) => {
45                 self.frame_table.add_fde(
46                     self.cie_id.unwrap(),
47                     unwind_info
48                         .to_fde(Address::Symbol { symbol: func_id.as_u32() as usize, addend: 0 }),
49                 );
50             }
51             UnwindInfo::WindowsX64(_) => {
52                 // FIXME implement this
53             }
54             unwind_info => unimplemented!("{:?}", unwind_info),
55         }
56     }
57
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();
61
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);
67
68             for reloc in &eh_frame.0.relocs {
69                 product.add_debug_reloc(&section_map, &section_id, reloc);
70             }
71         }
72     }
73
74     #[cfg(all(feature = "jit", windows))]
75     pub(crate) unsafe fn register_jit(self, _jit_module: &cranelift_jit::JITModule) {}
76
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();
81
82         if eh_frame.0.writer.slice().is_empty() {
83             return;
84         }
85
86         let mut eh_frame = eh_frame.0.relocate_for_jit(jit_module);
87
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]);
90
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);
95
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")]
100         {
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;
105
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;
109
110                 // Skip over the CIE
111                 if current != start {
112                     __register_frame(current);
113                 }
114
115                 // Move to the next table entry (+4 because the length itself is not inclusive)
116                 current = current.add(len + 4);
117             }
118         }
119         #[cfg(not(target_os = "macos"))]
120         {
121             // On other platforms, `__register_frame` will walk the FDEs until an entry of length 0
122             __register_frame(eh_frame);
123         }
124     }
125 }
126
127 extern "C" {
128     // libunwind import
129     fn __register_frame(fde: *const u8);
130 }