]> git.proxmox.com Git - rustc.git/blame - compiler/rustc_codegen_cranelift/src/debuginfo/unwind.rs
Merge tag 'debian/1.52.1+dfsg1-1_exp2' into proxmox/buster
[rustc.git] / compiler / rustc_codegen_cranelift / src / debuginfo / unwind.rs
CommitLineData
29967ef6
XL
1//! Unwind info generation (`.eh_frame`)
2
3use crate::prelude::*;
4
5use cranelift_codegen::isa::{unwind::UnwindInfo, TargetIsa};
6
7use gimli::write::{Address, CieId, EhFrame, FrameTable, Section};
8
9use crate::backend::WriteDebugInfo;
10
11pub(crate) struct UnwindContext<'tcx> {
12 tcx: TyCtxt<'tcx>,
13 frame_table: FrameTable,
14 cie_id: Option<CieId>,
15}
16
17impl<'tcx> UnwindContext<'tcx> {
5869c6ff 18 pub(crate) fn new(tcx: TyCtxt<'tcx>, isa: &dyn TargetIsa, pic_eh_frame: bool) -> Self {
29967ef6
XL
19 let mut frame_table = FrameTable::default();
20
21 let cie_id = if let Some(mut cie) = isa.create_systemv_cie() {
5869c6ff 22 if pic_eh_frame {
29967ef6
XL
23 cie.fde_address_encoding =
24 gimli::DwEhPe(gimli::DW_EH_PE_pcrel.0 | gimli::DW_EH_PE_sdata4.0);
25 }
26 Some(frame_table.add_cie(cie))
27 } else {
28 None
29 };
30
6a06907d 31 UnwindContext { tcx, frame_table, cie_id }
29967ef6
XL
32 }
33
34 pub(crate) fn add_function(&mut self, func_id: FuncId, context: &Context, isa: &dyn TargetIsa) {
35 let unwind_info = if let Some(unwind_info) = context.create_unwind_info(isa).unwrap() {
36 unwind_info
37 } else {
38 return;
39 };
40
41 match unwind_info {
42 UnwindInfo::SystemV(unwind_info) => {
43 self.frame_table.add_fde(
44 self.cie_id.unwrap(),
6a06907d
XL
45 unwind_info
46 .to_fde(Address::Symbol { symbol: func_id.as_u32() as usize, addend: 0 }),
29967ef6
XL
47 );
48 }
49 UnwindInfo::WindowsX64(_) => {
50 // FIXME implement this
51 }
52 unwind_info => unimplemented!("{:?}", unwind_info),
53 }
54 }
55
56 pub(crate) fn emit<P: WriteDebugInfo>(self, product: &mut P) {
6a06907d
XL
57 let mut eh_frame =
58 EhFrame::from(super::emit::WriterRelocate::new(super::target_endian(self.tcx)));
29967ef6
XL
59 self.frame_table.write_eh_frame(&mut eh_frame).unwrap();
60
61 if !eh_frame.0.writer.slice().is_empty() {
62 let id = eh_frame.id();
63 let section_id = product.add_debug_section(id, eh_frame.0.writer.into_vec());
64 let mut section_map = FxHashMap::default();
65 section_map.insert(id, section_id);
66
67 for reloc in &eh_frame.0.relocs {
68 product.add_debug_reloc(&section_map, &section_id, reloc);
69 }
70 }
71 }
72
73 #[cfg(feature = "jit")]
74 pub(crate) unsafe fn register_jit(
75 self,
5869c6ff 76 jit_module: &cranelift_jit::JITModule,
29967ef6 77 ) -> Option<UnwindRegistry> {
6a06907d
XL
78 let mut eh_frame =
79 EhFrame::from(super::emit::WriterRelocate::new(super::target_endian(self.tcx)));
29967ef6
XL
80 self.frame_table.write_eh_frame(&mut eh_frame).unwrap();
81
82 if eh_frame.0.writer.slice().is_empty() {
83 return None;
84 }
85
fc512014 86 let mut eh_frame = eh_frame.0.relocate_for_jit(jit_module);
29967ef6
XL
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 let mut registrations = Vec::new();
92
93 // =======================================================================
94 // Everything after this line up to the end of the file is loosly based on
95 // https://github.com/bytecodealliance/wasmtime/blob/4471a82b0c540ff48960eca6757ccce5b1b5c3e4/crates/jit/src/unwind/systemv.rs
96 #[cfg(target_os = "macos")]
97 {
98 // On macOS, `__register_frame` takes a pointer to a single FDE
99 let start = eh_frame.as_ptr();
100 let end = start.add(eh_frame.len());
101 let mut current = start;
102
103 // Walk all of the entries in the frame table and register them
104 while current < end {
105 let len = std::ptr::read::<u32>(current as *const u32) as usize;
106
107 // Skip over the CIE
108 if current != start {
109 __register_frame(current);
110 registrations.push(current as usize);
111 }
112
113 // Move to the next table entry (+4 because the length itself is not inclusive)
114 current = current.add(len + 4);
115 }
116 }
117 #[cfg(not(target_os = "macos"))]
118 {
119 // On other platforms, `__register_frame` will walk the FDEs until an entry of length 0
120 let ptr = eh_frame.as_ptr();
121 __register_frame(ptr);
122 registrations.push(ptr as usize);
123 }
124
6a06907d 125 Some(UnwindRegistry { _frame_table: eh_frame, registrations })
29967ef6
XL
126 }
127}
128
129/// Represents a registry of function unwind information for System V ABI.
130pub(crate) struct UnwindRegistry {
131 _frame_table: Vec<u8>,
132 registrations: Vec<usize>,
133}
134
135extern "C" {
136 // libunwind import
137 fn __register_frame(fde: *const u8);
138 fn __deregister_frame(fde: *const u8);
139}
140
141impl Drop for UnwindRegistry {
142 fn drop(&mut self) {
143 unsafe {
144 // libgcc stores the frame entries as a linked list in decreasing sort order
145 // based on the PC value of the registered entry.
146 //
147 // As we store the registrations in increasing order, it would be O(N^2) to
148 // deregister in that order.
149 //
150 // To ensure that we just pop off the first element in the list upon every
151 // deregistration, walk our list of registrations backwards.
152 for fde in self.registrations.iter().rev() {
153 __deregister_frame(*fde as *const _);
154 }
155 }
156 }
157}