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>
5 This file is part of libunwind.
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:
15 The above copyright notice and this permission notice shall be
16 included in all copies or substantial portions of the Software.
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. */
29 is_cie_id (unw_word_t val
, int is_debug_frame
)
31 /* The CIE ID is normally 0xffffffff (for 32-bit ELF) or
32 0xffffffffffffffff (for 64-bit ELF). However, .eh_frame
35 return (val
== - (uint32_t) 1 || val
== - (uint64_t) 1);
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
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
)
50 uint8_t version
, ch
, augstr
[5], fde_encoding
, handler_encoding
;
51 unw_word_t len
, cie_end_addr
, aug_size
;
57 # define STR(x) STR2(x)
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
64 switch (dwarf_addr_size (as
))
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;
71 dci
->lsda_encoding
= DW_EH_PE_omit
;
74 if ((ret
= dwarf_readu32 (as
, a
, &addr
, &u32val
, arg
)) < 0)
77 if (u32val
!= 0xffffffff)
79 /* the CIE is in the 32-bit DWARF format */
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;
85 cie_end_addr
= addr
+ len
;
86 if ((ret
= dwarf_readu32 (as
, a
, &addr
, &cie_id
, arg
)) < 0)
88 if (cie_id
!= expected_id
)
90 Debug (1, "Unexpected CIE id %x\n", cie_id
);
96 /* the CIE is in the 64-bit DWARF format */
98 /* DWARF says CIE id should be 0xffffffffffffffff, but in
100 const uint64_t expected_id
= (is_debug_frame
) ? 0xffffffffffffffffull
: 0;
102 if ((ret
= dwarf_readu64 (as
, a
, &addr
, &u64val
, arg
)) < 0)
105 cie_end_addr
= addr
+ len
;
106 if ((ret
= dwarf_readu64 (as
, a
, &addr
, &cie_id
, arg
)) < 0)
108 if (cie_id
!= expected_id
)
110 Debug (1, "Unexpected CIE id %llx\n", (long long) cie_id
);
114 dci
->cie_instr_end
= cie_end_addr
;
116 if ((ret
= dwarf_readu8 (as
, a
, &addr
, &version
, arg
)) < 0)
119 if (version
!= 1 && version
!= DWARF_CIE_VERSION
)
121 Debug (1, "Got CIE version %u, expected version 1 or "
122 STR (DWARF_CIE_VERSION
) "\n", version
);
123 return -UNW_EBADVERSION
;
126 /* read and parse the augmentation string: */
127 memset (augstr
, 0, sizeof (augstr
));
130 if ((ret
= dwarf_readu8 (as
, a
, &addr
, &ch
, arg
)) < 0)
134 break; /* end of augmentation string */
136 if (i
< sizeof (augstr
) - 1)
140 if ((ret
= dwarf_read_uleb128 (as
, a
, &addr
, &dci
->code_align
, arg
)) < 0
141 || (ret
= dwarf_read_sleb128 (as
, a
, &addr
, &dci
->data_align
, arg
)) < 0)
144 /* Read the return-address column either as a u8 or as a uleb128. */
147 if ((ret
= dwarf_readu8 (as
, a
, &addr
, &ch
, arg
)) < 0)
149 dci
->ret_addr_column
= ch
;
151 else if ((ret
= dwarf_read_uleb128 (as
, a
, &addr
, &dci
->ret_addr_column
,
156 if (augstr
[0] == 'z')
158 dci
->sized_augmentation
= 1;
159 if ((ret
= dwarf_read_uleb128 (as
, a
, &addr
, &aug_size
, arg
)) < 0)
164 for (; i
< sizeof (augstr
) && augstr
[i
]; ++i
)
168 /* read the LSDA pointer-encoding format. */
169 if ((ret
= dwarf_readu8 (as
, a
, &addr
, &ch
, arg
)) < 0)
171 dci
->lsda_encoding
= ch
;
175 /* read the FDE pointer-encoding format. */
176 if ((ret
= dwarf_readu8 (as
, a
, &addr
, &fde_encoding
, arg
)) < 0)
181 /* read the personality-routine pointer-encoding format. */
182 if ((ret
= dwarf_readu8 (as
, a
, &addr
, &handler_encoding
, arg
)) < 0)
184 if ((ret
= dwarf_read_encoded_pointer (as
, a
, &addr
, handler_encoding
,
185 pi
->gp
, pi
->start_ip
, &dci
->handler
, arg
)) < 0)
190 /* This is a signal frame. */
191 dci
->signal_frame
= 1;
193 /* Temporarily set it to one so dwarf_parse_fde() knows that
194 it should fetch the actual ABI/TAG pair from the FDE. */
195 dci
->have_abi_marker
= 1;
199 Debug (1, "Unexpected augmentation string `%s'\n", augstr
);
200 if (dci
->sized_augmentation
)
201 /* If we have the size of the augmentation body, we can skip
202 over the parts that we don't understand, so we're OK. */
208 dci
->fde_encoding
= fde_encoding
;
209 dci
->cie_instr_start
= addr
;
210 Debug (15, "CIE parsed OK, augmentation = \"%s\", handler=0x%lx\n",
211 augstr
, (long) dci
->handler
);
215 /* Extract proc-info from the FDE starting at adress ADDR.
217 Pass BASE as zero for eh_frame behaviour, or a pointer to
218 debug_frame base for debug_frame behaviour. */
221 dwarf_extract_proc_info_from_fde (unw_addr_space_t as
, unw_accessors_t
*a
,
222 unw_word_t
*addrp
, unw_proc_info_t
*pi
,
224 int need_unwind_info
, int is_debug_frame
,
227 unw_word_t fde_end_addr
, cie_addr
, cie_offset_addr
, aug_end_addr
= 0;
228 unw_word_t start_ip
, ip_range
, aug_size
, addr
= *addrp
;
229 int ret
, ip_range_encoding
;
230 struct dwarf_cie_info dci
;
234 Debug (12, "FDE @ 0x%lx\n", (long) addr
);
236 memset (&dci
, 0, sizeof (dci
));
238 if ((ret
= dwarf_readu32 (as
, a
, &addr
, &u32val
, arg
)) < 0)
241 if (u32val
!= 0xffffffff)
245 /* In some configurations, an FDE with a 0 length indicates the
246 end of the FDE-table. */
250 /* the FDE is in the 32-bit DWARF format */
252 *addrp
= fde_end_addr
= addr
+ u32val
;
253 cie_offset_addr
= addr
;
255 /* CIE must be within the segment. */
256 if (cie_offset_addr
< base
)
259 if ((ret
= dwarf_reads32 (as
, a
, &addr
, &cie_offset
, arg
)) < 0)
262 if (is_cie_id (cie_offset
, is_debug_frame
))
263 /* ignore CIEs (happens during linear searches) */
267 cie_addr
= base
+ cie_offset
;
269 /* DWARF says that the CIE_pointer in the FDE is a
270 .debug_frame-relative offset, but the GCC-generated .eh_frame
271 sections instead store a "pcrelative" offset, which is just
272 as fine as it's self-contained. */
273 cie_addr
= cie_offset_addr
- cie_offset
;
279 /* the FDE is in the 64-bit DWARF format */
281 if ((ret
= dwarf_readu64 (as
, a
, &addr
, &u64val
, arg
)) < 0)
284 *addrp
= fde_end_addr
= addr
+ u64val
;
285 cie_offset_addr
= addr
;
287 /* CIE must be within the segment. */
288 if (cie_offset_addr
< base
)
291 if ((ret
= dwarf_reads64 (as
, a
, &addr
, &cie_offset
, arg
)) < 0)
294 if (is_cie_id (cie_offset
, is_debug_frame
))
295 /* ignore CIEs (happens during linear searches) */
299 cie_addr
= base
+ cie_offset
;
301 /* DWARF says that the CIE_pointer in the FDE is a
302 .debug_frame-relative offset, but the GCC-generated .eh_frame
303 sections instead store a "pcrelative" offset, which is just
304 as fine as it's self-contained. */
305 cie_addr
= (unw_word_t
) ((uint64_t) cie_offset_addr
- cie_offset
);
308 Debug (15, "looking for CIE at address %lx\n", (long) cie_addr
);
310 if ((ret
= parse_cie (as
, a
, cie_addr
, pi
, &dci
, is_debug_frame
, arg
)) < 0)
313 /* IP-range has same encoding as FDE pointers, except that it's
314 always an absolute value: */
315 ip_range_encoding
= dci
.fde_encoding
& DW_EH_PE_FORMAT_MASK
;
317 if ((ret
= dwarf_read_encoded_pointer (as
, a
, &addr
, dci
.fde_encoding
,
318 pi
->gp
, pi
->start_ip
, &start_ip
, arg
)) < 0
319 || (ret
= dwarf_read_encoded_pointer (as
, a
, &addr
, ip_range_encoding
,
320 pi
->gp
, pi
->start_ip
, &ip_range
, arg
)) < 0)
322 pi
->start_ip
= start_ip
;
323 pi
->end_ip
= start_ip
+ ip_range
;
324 pi
->handler
= dci
.handler
;
326 if (dci
.sized_augmentation
)
328 if ((ret
= dwarf_read_uleb128 (as
, a
, &addr
, &aug_size
, arg
)) < 0)
330 aug_end_addr
= addr
+ aug_size
;
333 if ((ret
= dwarf_read_encoded_pointer (as
, a
, &addr
, dci
.lsda_encoding
,
334 pi
->gp
, pi
->start_ip
, &pi
->lsda
, arg
)) < 0)
337 Debug (15, "FDE covers IP 0x%lx-0x%lx, LSDA=0x%lx\n",
338 (long) pi
->start_ip
, (long) pi
->end_ip
, (long) pi
->lsda
);
340 if (need_unwind_info
)
342 pi
->format
= UNW_INFO_FORMAT_TABLE
;
343 pi
->unwind_info_size
= sizeof (dci
);
344 pi
->unwind_info
= mempool_alloc (&dwarf_cie_info_pool
);
345 if (!pi
->unwind_info
)
348 if (dci
.have_abi_marker
)
350 if ((ret
= dwarf_readu16 (as
, a
, &addr
, &dci
.abi
, arg
)) < 0
351 || (ret
= dwarf_readu16 (as
, a
, &addr
, &dci
.tag
, arg
)) < 0)
353 Debug (13, "Found ABI marker = (abi=%u, tag=%u)\n",
357 if (dci
.sized_augmentation
)
358 dci
.fde_instr_start
= aug_end_addr
;
360 dci
.fde_instr_start
= addr
;
361 dci
.fde_instr_end
= fde_end_addr
;
363 memcpy (pi
->unwind_info
, &dci
, sizeof (dci
));