1 //! Parsing of GCC-style Language-Specific Data Area (LSDA)
3 //! * <https://refspecs.linuxfoundation.org/LSB_3.0.0/LSB-PDA/LSB-PDA/ehframechpt.html>
4 //! * <https://itanium-cxx-abi.github.io/cxx-abi/exceptions.pdf>
5 //! * <https://www.airs.com/blog/archives/460>
6 //! * <https://www.airs.com/blog/archives/464>
8 //! A reference implementation may be found in the GCC source tree
9 //! (`<root>/libgcc/unwind-c.c` as of this writing).
11 #![allow(non_upper_case_globals)]
14 use super::DwarfReader;
18 pub const DW_EH_PE_omit: u8 = 0xFF;
19 pub const DW_EH_PE_absptr: u8 = 0x00;
21 pub const DW_EH_PE_uleb128: u8 = 0x01;
22 pub const DW_EH_PE_udata2: u8 = 0x02;
23 pub const DW_EH_PE_udata4: u8 = 0x03;
24 pub const DW_EH_PE_udata8: u8 = 0x04;
25 pub const DW_EH_PE_sleb128: u8 = 0x09;
26 pub const DW_EH_PE_sdata2: u8 = 0x0A;
27 pub const DW_EH_PE_sdata4: u8 = 0x0B;
28 pub const DW_EH_PE_sdata8: u8 = 0x0C;
30 pub const DW_EH_PE_pcrel: u8 = 0x10;
31 pub const DW_EH_PE_textrel: u8 = 0x20;
32 pub const DW_EH_PE_datarel: u8 = 0x30;
33 pub const DW_EH_PE_funcrel: u8 = 0x40;
34 pub const DW_EH_PE_aligned: u8 = 0x50;
36 pub const DW_EH_PE_indirect: u8 = 0x80;
38 #[derive(Copy, Clone)]
39 pub struct EHContext<'a> {
40 pub ip: usize, // Current instruction pointer
41 pub func_start: usize, // Address of the current function
42 pub get_text_start: &'a dyn Fn() -> usize, // Get address of the code section
43 pub get_data_start: &'a dyn Fn() -> usize, // Get address of the data section
53 pub const USING_SJLJ_EXCEPTIONS: bool = cfg!(all(target_os = "ios", target_arch = "arm"));
55 pub unsafe fn find_eh_action(lsda: *const u8, context: &EHContext<'_>) -> Result<EHAction, ()> {
57 return Ok(EHAction::None);
60 let func_start = context.func_start;
61 let mut reader = DwarfReader::new(lsda);
63 let start_encoding = reader.read::<u8>();
64 // base address for landing pad offsets
65 let lpad_base = if start_encoding != DW_EH_PE_omit {
66 read_encoded_pointer(&mut reader, context, start_encoding)?
71 let ttype_encoding = reader.read::<u8>();
72 if ttype_encoding != DW_EH_PE_omit {
73 // Rust doesn't analyze exception types, so we don't care about the type table
74 reader.read_uleb128();
77 let call_site_encoding = reader.read::<u8>();
78 let call_site_table_length = reader.read_uleb128();
79 let action_table = reader.ptr.add(call_site_table_length as usize);
82 if !USING_SJLJ_EXCEPTIONS {
83 while reader.ptr < action_table {
84 let cs_start = read_encoded_pointer(&mut reader, context, call_site_encoding)?;
85 let cs_len = read_encoded_pointer(&mut reader, context, call_site_encoding)?;
86 let cs_lpad = read_encoded_pointer(&mut reader, context, call_site_encoding)?;
87 let cs_action_entry = reader.read_uleb128();
88 // Callsite table is sorted by cs_start, so if we've passed the ip, we
89 // may stop searching.
90 if ip < func_start + cs_start {
93 if ip < func_start + cs_start + cs_len {
95 return Ok(EHAction::None);
97 let lpad = lpad_base + cs_lpad;
98 return Ok(interpret_cs_action(action_table as *mut u8, cs_action_entry, lpad));
102 // Ip is not present in the table. This indicates a nounwind call.
103 Ok(EHAction::Terminate)
106 // The "IP" is an index into the call-site table, with two exceptions:
107 // -1 means 'no-action', and 0 means 'terminate'.
109 -1 => return Ok(EHAction::None),
110 0 => return Ok(EHAction::Terminate),
115 let cs_lpad = reader.read_uleb128();
116 let cs_action_entry = reader.read_uleb128();
119 // Can never have null landing pad for sjlj -- that would have
120 // been indicated by a -1 call site index.
121 let lpad = (cs_lpad + 1) as usize;
122 return Ok(interpret_cs_action(action_table as *mut u8, cs_action_entry, lpad));
128 unsafe fn interpret_cs_action(
129 action_table: *mut u8,
130 cs_action_entry: u64,
133 if cs_action_entry == 0 {
134 // If cs_action is 0 then this is a cleanup (Drop::drop). We run these
135 // for both Rust panics and foreign exceptions.
136 EHAction::Cleanup(lpad)
138 let action_record = (action_table as *mut u8).offset(cs_action_entry as isize - 1);
139 let mut action_reader = DwarfReader::new(action_record);
140 let ttype_index = action_reader.read_sleb128();
141 if ttype_index == 0 {
142 EHAction::Cleanup(lpad)
144 // Stop unwinding Rust panics at catch_unwind.
145 EHAction::Catch(lpad)
151 fn round_up(unrounded: usize, align: usize) -> Result<usize, ()> {
152 if align.is_power_of_two() { Ok((unrounded + align - 1) & !(align - 1)) } else { Err(()) }
155 unsafe fn read_encoded_pointer(
156 reader: &mut DwarfReader,
157 context: &EHContext<'_>,
159 ) -> Result<usize, ()> {
160 if encoding == DW_EH_PE_omit {
164 // DW_EH_PE_aligned implies it's an absolute pointer value
165 if encoding == DW_EH_PE_aligned {
166 reader.ptr = reader.ptr.with_addr(round_up(reader.ptr.addr(), mem::size_of::<usize>())?);
167 return Ok(reader.read::<usize>());
170 let mut result = match encoding & 0x0F {
171 DW_EH_PE_absptr => reader.read::<usize>(),
172 DW_EH_PE_uleb128 => reader.read_uleb128() as usize,
173 DW_EH_PE_udata2 => reader.read::<u16>() as usize,
174 DW_EH_PE_udata4 => reader.read::<u32>() as usize,
175 DW_EH_PE_udata8 => reader.read::<u64>() as usize,
176 DW_EH_PE_sleb128 => reader.read_sleb128() as usize,
177 DW_EH_PE_sdata2 => reader.read::<i16>() as usize,
178 DW_EH_PE_sdata4 => reader.read::<i32>() as usize,
179 DW_EH_PE_sdata8 => reader.read::<i64>() as usize,
183 result += match encoding & 0x70 {
184 DW_EH_PE_absptr => 0,
185 // relative to address of the encoded value, despite the name
186 DW_EH_PE_pcrel => reader.ptr.expose_addr(),
187 DW_EH_PE_funcrel => {
188 if context.func_start == 0 {
193 DW_EH_PE_textrel => (*context.get_text_start)(),
194 DW_EH_PE_datarel => (*context.get_data_start)(),
198 if encoding & DW_EH_PE_indirect != 0 {
199 result = *ptr::from_exposed_addr::<usize>(result);