]>
git.proxmox.com Git - rustc.git/blob - src/libpanic_unwind/dwarf/eh.rs
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
) -> EHAction
{
66 return EHAction
::None
;
69 let func_start
= context
.func_start
;
70 let mut reader
= DwarfReader
::new(lsda
);
72 let start_encoding
= reader
.read
::<u8>();
73 // base address for landing pad offsets
74 let lpad_base
= if start_encoding
!= DW_EH_PE_omit
{
75 read_encoded_pointer(&mut reader
, context
, start_encoding
)
80 let ttype_encoding
= reader
.read
::<u8>();
81 if ttype_encoding
!= DW_EH_PE_omit
{
82 // Rust doesn't analyze exception types, so we don't care about the type table
83 reader
.read_uleb128();
86 let call_site_encoding
= reader
.read
::<u8>();
87 let call_site_table_length
= reader
.read_uleb128();
88 let action_table
= reader
.ptr
.offset(call_site_table_length
as isize);
91 if !USING_SJLJ_EXCEPTIONS
{
92 while reader
.ptr
< action_table
{
93 let cs_start
= read_encoded_pointer(&mut reader
, context
, call_site_encoding
);
94 let cs_len
= read_encoded_pointer(&mut reader
, context
, call_site_encoding
);
95 let cs_lpad
= read_encoded_pointer(&mut reader
, context
, call_site_encoding
);
96 let cs_action
= reader
.read_uleb128();
97 // Callsite table is sorted by cs_start, so if we've passed the ip, we
98 // may stop searching.
99 if ip
< func_start
+ cs_start
{
102 if ip
< func_start
+ cs_start
+ cs_len
{
104 return EHAction
::None
;
106 let lpad
= lpad_base
+ cs_lpad
;
107 return interpret_cs_action(cs_action
, lpad
);
111 // Ip is not present in the table. This should not happen... but it does: issue #35011.
112 // So rather than returning EHAction::Terminate, we do this.
116 // The "IP" is an index into the call-site table, with two exceptions:
117 // -1 means 'no-action', and 0 means 'terminate'.
119 -1 => return EHAction
::None
,
120 0 => return EHAction
::Terminate
,
125 let cs_lpad
= reader
.read_uleb128();
126 let cs_action
= reader
.read_uleb128();
129 // Can never have null landing pad for sjlj -- that would have
130 // been indicated by a -1 call site index.
131 let lpad
= (cs_lpad
+ 1) as usize;
132 return interpret_cs_action(cs_action
, lpad
);
138 fn interpret_cs_action(cs_action
: u64, lpad
: usize) -> EHAction
{
140 EHAction
::Cleanup(lpad
)
142 EHAction
::Catch(lpad
)
147 fn round_up(unrounded
: usize, align
: usize) -> usize {
148 assert
!(align
.is_power_of_two());
149 (unrounded
+ align
- 1) & !(align
- 1)
152 unsafe fn read_encoded_pointer(reader
: &mut DwarfReader
,
156 assert
!(encoding
!= DW_EH_PE_omit
);
158 // DW_EH_PE_aligned implies it's an absolute pointer value
159 if encoding
== DW_EH_PE_aligned
{
160 reader
.ptr
= round_up(reader
.ptr
as usize, mem
::size_of
::<usize>()) as *const u8;
161 return reader
.read
::<usize>();
164 let mut result
= match encoding
& 0x0F {
165 DW_EH_PE_absptr
=> reader
.read
::<usize>(),
166 DW_EH_PE_uleb128
=> reader
.read_uleb128() as usize,
167 DW_EH_PE_udata2
=> reader
.read
::<u16>() as usize,
168 DW_EH_PE_udata4
=> reader
.read
::<u32>() as usize,
169 DW_EH_PE_udata8
=> reader
.read
::<u64>() as usize,
170 DW_EH_PE_sleb128
=> reader
.read_sleb128() as usize,
171 DW_EH_PE_sdata2
=> reader
.read
::<i16>() as usize,
172 DW_EH_PE_sdata4
=> reader
.read
::<i32>() as usize,
173 DW_EH_PE_sdata8
=> reader
.read
::<i64>() as usize,
177 result
+= match encoding
& 0x70 {
178 DW_EH_PE_absptr
=> 0,
179 // relative to address of the encoded value, despite the name
180 DW_EH_PE_pcrel
=> reader
.ptr
as usize,
181 DW_EH_PE_funcrel
=> {
182 assert
!(context
.func_start
!= 0);
185 DW_EH_PE_textrel
=> {
186 (*context
.get_text_start
)()
188 DW_EH_PE_datarel
=> {
189 (*context
.get_data_start
)()
194 if encoding
& DW_EH_PE_indirect
!= 0 {
195 result
= *(result
as *const usize);