]> git.proxmox.com Git - rustc.git/blob - src/libpanic_unwind/dwarf/eh.rs
New upstream version 1.12.0+dfsg1
[rustc.git] / 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.
4 //
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.
10
11 //! Parsing of GCC-style Language-Specific Data Area (LSDA)
12 //! For details see:
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
17 //!
18 //! A reference implementation may be found in the GCC source tree
19 //! (<root>/libgcc/unwind-c.c as of this writing)
20
21 #![allow(non_upper_case_globals)]
22 #![allow(unused)]
23
24 use dwarf::DwarfReader;
25 use core::mem;
26
27 pub const DW_EH_PE_omit: u8 = 0xFF;
28 pub const DW_EH_PE_absptr: u8 = 0x00;
29
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;
38
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;
44
45 pub const DW_EH_PE_indirect: u8 = 0x80;
46
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
53 }
54
55 pub enum EHAction {
56 None,
57 Cleanup(usize),
58 Catch(usize),
59 Terminate,
60 }
61
62 pub const USING_SJLJ_EXCEPTIONS: bool = cfg!(all(target_os = "ios", target_arch = "arm"));
63
64 pub unsafe fn find_eh_action(lsda: *const u8, context: &EHContext) -> EHAction {
65 if lsda.is_null() {
66 return EHAction::None;
67 }
68
69 let func_start = context.func_start;
70 let mut reader = DwarfReader::new(lsda);
71
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)
76 } else {
77 func_start
78 };
79
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();
84 }
85
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);
89 let ip = context.ip;
90
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 {
100 break;
101 }
102 if ip < func_start + cs_start + cs_len {
103 if cs_lpad == 0 {
104 return EHAction::None;
105 } else {
106 let lpad = lpad_base + cs_lpad;
107 return interpret_cs_action(cs_action, lpad);
108 }
109 }
110 }
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.
113 EHAction::None
114 } else {
115 // SjLj version:
116 // The "IP" is an index into the call-site table, with two exceptions:
117 // -1 means 'no-action', and 0 means 'terminate'.
118 match ip as isize {
119 -1 => return EHAction::None,
120 0 => return EHAction::Terminate,
121 _ => (),
122 }
123 let mut idx = ip;
124 loop {
125 let cs_lpad = reader.read_uleb128();
126 let cs_action = reader.read_uleb128();
127 idx -= 1;
128 if idx == 0 {
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);
133 }
134 }
135 }
136 }
137
138 fn interpret_cs_action(cs_action: u64, lpad: usize) -> EHAction {
139 if cs_action == 0 {
140 EHAction::Cleanup(lpad)
141 } else {
142 EHAction::Catch(lpad)
143 }
144 }
145
146 #[inline]
147 fn round_up(unrounded: usize, align: usize) -> usize {
148 assert!(align.is_power_of_two());
149 (unrounded + align - 1) & !(align - 1)
150 }
151
152 unsafe fn read_encoded_pointer(reader: &mut DwarfReader,
153 context: &EHContext,
154 encoding: u8)
155 -> usize {
156 assert!(encoding != DW_EH_PE_omit);
157
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>();
162 }
163
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,
174 _ => panic!(),
175 };
176
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);
183 context.func_start
184 }
185 DW_EH_PE_textrel => {
186 (*context.get_text_start)()
187 }
188 DW_EH_PE_datarel => {
189 (*context.get_data_start)()
190 }
191 _ => panic!(),
192 };
193
194 if encoding & DW_EH_PE_indirect != 0 {
195 result = *(result as *const usize);
196 }
197
198 result
199 }