1 /* libunwind - a platform-independent unwind library
2 Copyright (c) 2003-2005 Hewlett-Packard Development Company, L.P.
3 Contributed by David Mosberger-Tang <davidm@hpl.hp.com>
4
5 This file is part of libunwind.
6
7 Permission is hereby granted, free of charge, to any person obtaining
8 a copy of this software and associated documentation files (the
9 "Software"), to deal in the Software without restriction, including
10 without limitation the rights to use, copy, modify, merge, publish,
11 distribute, sublicense, and/or sell copies of the Software, and to
12 permit persons to whom the Software is furnished to do so, subject to
13 the following conditions:
14
15 The above copyright notice and this permission notice shall be
16 included in all copies or substantial portions of the Software.
17
18 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
19 EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
20 MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
21 NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
22 LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
23 OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
24 WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
25
26 #include "dwarf_i.h"
27
28 static inline int
is_cie_id(unw_word_t val,int is_debug_frame)29 is_cie_id (unw_word_t val, int is_debug_frame)
30 {
31 /* The CIE ID is normally 0xffffffff (for 32-bit ELF) or
32 0xffffffffffffffff (for 64-bit ELF). However, .eh_frame
33 uses 0. */
34 if (is_debug_frame)
35 return (val == (uint32_t)(-1) || val == (uint64_t)(-1));
36 else
37 return (val == 0);
38 }
39
40 /* Note: we don't need to keep track of more than the first four
41 characters of the augmentation string, because we (a) ignore any
42 augmentation string contents once we find an unrecognized character
43 and (b) those characters that we do recognize, can't be
44 repeated. */
45 static inline int
parse_cie(unw_addr_space_t as,unw_accessors_t * a,unw_word_t addr,const unw_proc_info_t * pi,struct dwarf_cie_info * dci,int is_debug_frame,void * arg)46 parse_cie (unw_addr_space_t as, unw_accessors_t *a, unw_word_t addr,
47 const unw_proc_info_t *pi, struct dwarf_cie_info *dci,
48 int is_debug_frame, void *arg)
49 {
50 uint8_t version, ch, augstr[5], fde_encoding, handler_encoding;
51 unw_word_t len, cie_end_addr, aug_size;
52 uint32_t u32val;
53 uint64_t u64val;
54 size_t i;
55 int ret;
56 # define STR2(x) #x
57 # define STR(x) STR2(x)
58
59 /* Pick appropriate default for FDE-encoding. DWARF spec says
60 start-IP (initial_location) and the code-size (address_range) are
61 "address-unit sized constants". The `R' augmentation can be used
62 to override this, but by default, we pick an address-sized unit
63 for fde_encoding. */
64 switch (dwarf_addr_size (as))
65 {
66 case 4: fde_encoding = DW_EH_PE_udata4; break;
67 case 8: fde_encoding = DW_EH_PE_udata8; break;
68 default: fde_encoding = DW_EH_PE_omit; break;
69 }
70
71 dci->lsda_encoding = DW_EH_PE_omit;
72 dci->handler = 0;
73
74 if ((ret = dwarf_readu32 (as, a, &addr, &u32val, arg)) < 0)
75 return ret;
76
77 if (u32val != 0xffffffff)
78 {
79 /* the CIE is in the 32-bit DWARF format */
80 uint32_t cie_id;
81 /* DWARF says CIE id should be 0xffffffff, but in .eh_frame, it's 0 */
82 const uint32_t expected_id = (is_debug_frame) ? 0xffffffff : 0;
83
84 len = u32val;
85 cie_end_addr = addr + len;
86 if ((ret = dwarf_readu32 (as, a, &addr, &cie_id, arg)) < 0)
87 return ret;
88 if (cie_id != expected_id)
89 {
90 Debug (1, "Unexpected CIE id %x\n", cie_id);
91 return -UNW_EINVAL;
92 }
93 }
94 else
95 {
96 /* the CIE is in the 64-bit DWARF format */
97 uint64_t cie_id;
98 /* DWARF says CIE id should be 0xffffffffffffffff, but in
99 .eh_frame, it's 0 */
100 const uint64_t expected_id = (is_debug_frame) ? 0xffffffffffffffffull : 0;
101
102 if ((ret = dwarf_readu64 (as, a, &addr, &u64val, arg)) < 0)
103 return ret;
104 len = u64val;
105 cie_end_addr = addr + len;
106 if ((ret = dwarf_readu64 (as, a, &addr, &cie_id, arg)) < 0)
107 return ret;
108 if (cie_id != expected_id)
109 {
110 Debug (1, "Unexpected CIE id %llx\n", (long long) cie_id);
111 return -UNW_EINVAL;
112 }
113 }
114 dci->cie_instr_end = cie_end_addr;
115
116 if ((ret = dwarf_readu8 (as, a, &addr, &version, arg)) < 0)
117 return ret;
118
119 /* GCC emits version 1??? */
120 if (version != 1 && (version < DWARF_CIE_VERSION || version > DWARF_CIE_VERSION_MAX))
121 {
122 Debug (1, "Got CIE version %u, expected version 1 or between "
123 STR (DWARF_CIE_VERSION) " and " STR (DWARF_CIE_VERSION_MAX) "\n", version);
124 return -UNW_EBADVERSION;
125 }
126
127 /* read and parse the augmentation string: */
128 memset (augstr, 0, sizeof (augstr));
129 for (i = 0;;)
130 {
131 if ((ret = dwarf_readu8 (as, a, &addr, &ch, arg)) < 0)
132 return ret;
133
134 if (!ch)
135 break; /* end of augmentation string */
136
137 if (i < sizeof (augstr) - 1)
138 augstr[i++] = ch;
139 }
140
141 if ((ret = dwarf_read_uleb128 (as, a, &addr, &dci->code_align, arg)) < 0
142 || (ret = dwarf_read_sleb128 (as, a, &addr, &dci->data_align, arg)) < 0)
143 return ret;
144
145 /* Read the return-address column either as a u8 or as a uleb128. */
146 if (version == 1)
147 {
148 if ((ret = dwarf_readu8 (as, a, &addr, &ch, arg)) < 0)
149 return ret;
150 dci->ret_addr_column = ch;
151 }
152 else if ((ret = dwarf_read_uleb128 (as, a, &addr, &dci->ret_addr_column,
153 arg)) < 0)
154 return ret;
155
156 i = 0;
157 if (augstr[0] == 'z')
158 {
159 dci->sized_augmentation = 1;
160 if ((ret = dwarf_read_uleb128 (as, a, &addr, &aug_size, arg)) < 0)
161 return ret;
162 i++;
163 }
164
165 for (; i < sizeof (augstr) && augstr[i]; ++i)
166 switch (augstr[i])
167 {
168 case 'L':
169 /* read the LSDA pointer-encoding format. */
170 if ((ret = dwarf_readu8 (as, a, &addr, &ch, arg)) < 0)
171 return ret;
172 dci->lsda_encoding = ch;
173 break;
174
175 case 'R':
176 /* read the FDE pointer-encoding format. */
177 if ((ret = dwarf_readu8 (as, a, &addr, &fde_encoding, arg)) < 0)
178 return ret;
179 break;
180
181 case 'P':
182 /* read the personality-routine pointer-encoding format. */
183 if ((ret = dwarf_readu8 (as, a, &addr, &handler_encoding, arg)) < 0)
184 return ret;
185 if ((ret = dwarf_read_encoded_pointer (as, a, &addr, handler_encoding,
186 pi, &dci->handler, arg)) < 0)
187 return ret;
188 break;
189
190 case 'S':
191 /* This is a signal frame. */
192 dci->signal_frame = 1;
193
194 /* Temporarily set it to one so dwarf_parse_fde() knows that
195 it should fetch the actual ABI/TAG pair from the FDE. */
196 dci->have_abi_marker = 1;
197 break;
198
199 default:
200 Debug (1, "Unexpected augmentation string `%s'\n", augstr);
201 if (dci->sized_augmentation)
202 /* If we have the size of the augmentation body, we can skip
203 over the parts that we don't understand, so we're OK. */
204 goto done;
205 else
206 return -UNW_EINVAL;
207 }
208 done:
209 dci->fde_encoding = fde_encoding;
210 dci->cie_instr_start = addr;
211 Debug (15, "CIE parsed OK, augmentation = \"%s\", handler=0x%lx\n",
212 augstr, (long) dci->handler);
213 return 0;
214 }
215
216 /* Extract proc-info from the FDE starting at adress ADDR.
217
218 Pass BASE as zero for eh_frame behaviour, or a pointer to
219 debug_frame base for debug_frame behaviour. */
220
221 HIDDEN int
dwarf_extract_proc_info_from_fde(unw_addr_space_t as,unw_accessors_t * a,unw_word_t * addrp,unw_proc_info_t * pi,unw_word_t base,int need_unwind_info,int is_debug_frame,void * arg)222 dwarf_extract_proc_info_from_fde (unw_addr_space_t as, unw_accessors_t *a,
223 unw_word_t *addrp, unw_proc_info_t *pi,
224 unw_word_t base,
225 int need_unwind_info, int is_debug_frame,
226 void *arg)
227 {
228 unw_word_t fde_end_addr, cie_addr, cie_offset_addr, aug_end_addr = 0;
229 unw_word_t start_ip, ip_range, aug_size, addr = *addrp;
230 int ret, ip_range_encoding;
231 struct dwarf_cie_info dci;
232 uint64_t u64val;
233 uint32_t u32val;
234
235 Debug (12, "FDE @ 0x%lx\n", (long) addr);
236
237 memset (&dci, 0, sizeof (dci));
238
239 if ((ret = dwarf_readu32 (as, a, &addr, &u32val, arg)) < 0)
240 return ret;
241
242 if (u32val != 0xffffffff)
243 {
244 int32_t cie_offset = 0;
245
246 /* In some configurations, an FDE with a 0 length indicates the
247 end of the FDE-table. */
248 if (u32val == 0)
249 return -UNW_ENOINFO;
250
251 /* the FDE is in the 32-bit DWARF format */
252
253 *addrp = fde_end_addr = addr + u32val;
254 cie_offset_addr = addr;
255
256 if ((ret = dwarf_reads32 (as, a, &addr, &cie_offset, arg)) < 0)
257 return ret;
258
259 if (is_cie_id (cie_offset, is_debug_frame))
260 /* ignore CIEs (happens during linear searches) */
261 return 0;
262
263 if (is_debug_frame)
264 cie_addr = base + cie_offset;
265 else
266 /* DWARF says that the CIE_pointer in the FDE is a
267 .debug_frame-relative offset, but the GCC-generated .eh_frame
268 sections instead store a "pcrelative" offset, which is just
269 as fine as it's self-contained. */
270 cie_addr = cie_offset_addr - cie_offset;
271 }
272 else
273 {
274 int64_t cie_offset = 0;
275
276 /* the FDE is in the 64-bit DWARF format */
277
278 if ((ret = dwarf_readu64 (as, a, &addr, &u64val, arg)) < 0)
279 return ret;
280
281 *addrp = fde_end_addr = addr + u64val;
282 cie_offset_addr = addr;
283
284 if ((ret = dwarf_reads64 (as, a, &addr, &cie_offset, arg)) < 0)
285 return ret;
286
287 if (is_cie_id (cie_offset, is_debug_frame))
288 /* ignore CIEs (happens during linear searches) */
289 return 0;
290
291 if (is_debug_frame)
292 cie_addr = base + cie_offset;
293 else
294 /* DWARF says that the CIE_pointer in the FDE is a
295 .debug_frame-relative offset, but the GCC-generated .eh_frame
296 sections instead store a "pcrelative" offset, which is just
297 as fine as it's self-contained. */
298 cie_addr = (unw_word_t) ((uint64_t) cie_offset_addr - cie_offset);
299 }
300
301 Debug (15, "looking for CIE at address %lx\n", (long) cie_addr);
302
303 if ((ret = parse_cie (as, a, cie_addr, pi, &dci, is_debug_frame, arg)) < 0)
304 return ret;
305
306 /* IP-range has same encoding as FDE pointers, except that it's
307 always an absolute value: */
308 ip_range_encoding = dci.fde_encoding & DW_EH_PE_FORMAT_MASK;
309
310 if ((ret = dwarf_read_encoded_pointer (as, a, &addr, dci.fde_encoding,
311 pi, &start_ip, arg)) < 0
312 || (ret = dwarf_read_encoded_pointer (as, a, &addr, ip_range_encoding,
313 pi, &ip_range, arg)) < 0)
314 return ret;
315 pi->start_ip = start_ip;
316 pi->end_ip = start_ip + ip_range;
317 pi->handler = dci.handler;
318
319 if (dci.sized_augmentation)
320 {
321 if ((ret = dwarf_read_uleb128 (as, a, &addr, &aug_size, arg)) < 0)
322 return ret;
323 aug_end_addr = addr + aug_size;
324 }
325
326 if ((ret = dwarf_read_encoded_pointer (as, a, &addr, dci.lsda_encoding,
327 pi, &pi->lsda, arg)) < 0)
328 return ret;
329
330 Debug (15, "FDE covers IP 0x%lx-0x%lx, LSDA=0x%lx\n",
331 (long) pi->start_ip, (long) pi->end_ip, (long) pi->lsda);
332
333 if (need_unwind_info)
334 {
335 pi->format = UNW_INFO_FORMAT_TABLE;
336 pi->unwind_info_size = sizeof (dci);
337 pi->unwind_info = mempool_alloc (&dwarf_cie_info_pool);
338 if (!pi->unwind_info)
339 return -UNW_ENOMEM;
340
341 if (dci.have_abi_marker)
342 {
343 if ((ret = dwarf_readu16 (as, a, &addr, &dci.abi, arg)) < 0
344 || (ret = dwarf_readu16 (as, a, &addr, &dci.tag, arg)) < 0)
345 return ret;
346 Debug (13, "Found ABI marker = (abi=%u, tag=%u)\n",
347 dci.abi, dci.tag);
348 }
349
350 if (dci.sized_augmentation)
351 dci.fde_instr_start = aug_end_addr;
352 else
353 dci.fde_instr_start = addr;
354 dci.fde_instr_end = fde_end_addr;
355
356 memcpy (pi->unwind_info, &dci, sizeof (dci));
357 }
358 return 0;
359 }
360