1 // Copyright 2015 The Rust Project Developers. See the COPYRIGHT
2 // file at the top-level directory of this distribution and at
3 // http://rust-lang.org/COPYRIGHT.
5 // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6 // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8 // option. This file may not be copied, modified, or distributed
9 // except according to those terms.
11 //! Parsing of GCC-style Language-Specific Data Area (LSDA)
13 //! http://refspecs.linuxfoundation.org/LSB_3.0.0/LSB-PDA/LSB-PDA/ehframechpt.html
14 //! http://mentorembedded.github.io/cxx-abi/exceptions.pdf
15 //! http://www.airs.com/blog/archives/460
16 //! http://www.airs.com/blog/archives/464
18 //! A reference implementation may be found in the GCC source tree
19 //! (<root>/libgcc/unwind-c.c as of this writing)
21 #![allow(non_upper_case_globals)]
24 use dwarf::DwarfReader;
27 pub const DW_EH_PE_omit: u8 = 0xFF;
28 pub const DW_EH_PE_absptr: u8 = 0x00;
30 pub const DW_EH_PE_uleb128: u8 = 0x01;
31 pub const DW_EH_PE_udata2: u8 = 0x02;
32 pub const DW_EH_PE_udata4: u8 = 0x03;
33 pub const DW_EH_PE_udata8: u8 = 0x04;
34 pub const DW_EH_PE_sleb128: u8 = 0x09;
35 pub const DW_EH_PE_sdata2: u8 = 0x0A;
36 pub const DW_EH_PE_sdata4: u8 = 0x0B;
37 pub const DW_EH_PE_sdata8: u8 = 0x0C;
39 pub const DW_EH_PE_pcrel: u8 = 0x10;
40 pub const DW_EH_PE_textrel: u8 = 0x20;
41 pub const DW_EH_PE_datarel: u8 = 0x30;
42 pub const DW_EH_PE_funcrel: u8 = 0x40;
43 pub const DW_EH_PE_aligned: u8 = 0x50;
45 pub const DW_EH_PE_indirect: u8 = 0x80;
47 #[derive(Copy, Clone)]
48 pub struct EHContext<'a> {
49 pub ip: usize, // Current instruction pointer
50 pub func_start: usize, // Address of the current function
51 pub get_text_start: &'a Fn() -> usize, // Get address of the code section
52 pub get_data_start: &'a Fn() -> usize, // Get address of the data section
62 pub const USING_SJLJ_EXCEPTIONS: bool = cfg!(all(target_os = "ios", target_arch = "arm"));
64 pub unsafe fn find_eh_action(lsda: *const u8, context: &EHContext)
65 -> Result<EHAction, ()>
68 return Ok(EHAction::None)
71 let func_start = context.func_start;
72 let mut reader = DwarfReader::new(lsda);
74 let start_encoding = reader.read::<u8>();
75 // base address for landing pad offsets
76 let lpad_base = if start_encoding != DW_EH_PE_omit {
77 read_encoded_pointer(&mut reader, context, start_encoding)?
82 let ttype_encoding = reader.read::<u8>();
83 if ttype_encoding != DW_EH_PE_omit {
84 // Rust doesn't analyze exception types, so we don't care about the type table
85 reader.read_uleb128();
88 let call_site_encoding = reader.read::<u8>();
89 let call_site_table_length = reader.read_uleb128();
90 let action_table = reader.ptr.offset(call_site_table_length as isize);
93 if !USING_SJLJ_EXCEPTIONS {
94 while reader.ptr < action_table {
95 let cs_start = read_encoded_pointer(&mut reader, context, call_site_encoding)?;
96 let cs_len = read_encoded_pointer(&mut reader, context, call_site_encoding)?;
97 let cs_lpad = read_encoded_pointer(&mut reader, context, call_site_encoding)?;
98 let cs_action = reader.read_uleb128();
99 // Callsite table is sorted by cs_start, so if we've passed the ip, we
100 // may stop searching.
101 if ip < func_start + cs_start {
104 if ip < func_start + cs_start + cs_len {
106 return Ok(EHAction::None)
108 let lpad = lpad_base + cs_lpad;
109 return Ok(interpret_cs_action(cs_action, lpad))
113 // Ip is not present in the table. This should not happen... but it does: issue #35011.
114 // So rather than returning EHAction::Terminate, we do this.
118 // The "IP" is an index into the call-site table, with two exceptions:
119 // -1 means 'no-action', and 0 means 'terminate'.
121 -1 => return Ok(EHAction::None),
122 0 => return Ok(EHAction::Terminate),
127 let cs_lpad = reader.read_uleb128();
128 let cs_action = reader.read_uleb128();
131 // Can never have null landing pad for sjlj -- that would have
132 // been indicated by a -1 call site index.
133 let lpad = (cs_lpad + 1) as usize;
134 return Ok(interpret_cs_action(cs_action, lpad))
140 fn interpret_cs_action(cs_action: u64, lpad: usize) -> EHAction {
142 EHAction::Cleanup(lpad)
144 EHAction::Catch(lpad)
149 fn round_up(unrounded: usize, align: usize) -> Result<usize, ()> {
150 if align.is_power_of_two() {
151 Ok((unrounded + align - 1) & !(align - 1))
157 unsafe fn read_encoded_pointer(reader: &mut DwarfReader,
160 -> Result<usize, ()> {
161 if encoding == DW_EH_PE_omit {
165 // DW_EH_PE_aligned implies it's an absolute pointer value
166 if encoding == DW_EH_PE_aligned {
167 reader.ptr = round_up(reader.ptr as usize, mem::size_of::<usize>())? as *const u8;
168 return Ok(reader.read::<usize>())
171 let mut result = match encoding & 0x0F {
172 DW_EH_PE_absptr => reader.read::<usize>(),
173 DW_EH_PE_uleb128 => reader.read_uleb128() as usize,
174 DW_EH_PE_udata2 => reader.read::<u16>() as usize,
175 DW_EH_PE_udata4 => reader.read::<u32>() as usize,
176 DW_EH_PE_udata8 => reader.read::<u64>() as usize,
177 DW_EH_PE_sleb128 => reader.read_sleb128() as usize,
178 DW_EH_PE_sdata2 => reader.read::<i16>() as usize,
179 DW_EH_PE_sdata4 => reader.read::<i32>() as usize,
180 DW_EH_PE_sdata8 => reader.read::<i64>() as usize,
184 result += match encoding & 0x70 {
185 DW_EH_PE_absptr => 0,
186 // relative to address of the encoded value, despite the name
187 DW_EH_PE_pcrel => reader.ptr as usize,
188 DW_EH_PE_funcrel => {
189 if context.func_start == 0 {
194 DW_EH_PE_textrel => (*context.get_text_start)(),
195 DW_EH_PE_datarel => (*context.get_data_start)(),
199 if encoding & DW_EH_PE_indirect != 0 {
200 result = *(result as *const usize);