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 /* ANDROID support update. */
36 return (val == (uint32_t) -1 || val == (unw_word_t) (uint64_t) -1);
37 /* End of ANDROID update. */
38 else
39 return (val == 0);
40 }
41
42 /* Note: we don't need to keep track of more than the first four
43 characters of the augmentation string, because we (a) ignore any
44 augmentation string contents once we find an unrecognized character
45 and (b) those characters that we do recognize, can't be
46 repeated. */
47 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,unw_word_t base,void * arg)48 parse_cie (unw_addr_space_t as, unw_accessors_t *a, unw_word_t addr,
49 const unw_proc_info_t *pi, struct dwarf_cie_info *dci,
50 unw_word_t base, void *arg)
51 {
52 uint8_t version, ch, augstr[5], fde_encoding, handler_encoding;
53 unw_word_t len, cie_end_addr, aug_size;
54 uint32_t u32val;
55 uint64_t u64val;
56 size_t i;
57 int ret;
58 # define STR2(x) #x
59 # define STR(x) STR2(x)
60
61 /* Pick appropriate default for FDE-encoding. DWARF spec says
62 start-IP (initial_location) and the code-size (address_range) are
63 "address-unit sized constants". The `R' augmentation can be used
64 to override this, but by default, we pick an address-sized unit
65 for fde_encoding. */
66 switch (dwarf_addr_size (as))
67 {
68 case 4: fde_encoding = DW_EH_PE_udata4; break;
69 case 8: fde_encoding = DW_EH_PE_udata8; break;
70 default: fde_encoding = DW_EH_PE_omit; break;
71 }
72
73 dci->lsda_encoding = DW_EH_PE_omit;
74 dci->handler = 0;
75
76 if ((ret = dwarf_readu32 (as, a, &addr, &u32val, arg)) < 0)
77 return ret;
78
79 if (u32val != 0xffffffff)
80 {
81 /* the CIE is in the 32-bit DWARF format */
82 uint32_t cie_id;
83 /* DWARF says CIE id should be 0xffffffff, but in .eh_frame, it's 0 */
84 const uint32_t expected_id = (base) ? 0xffffffff : 0;
85
86 len = u32val;
87 cie_end_addr = addr + len;
88 if ((ret = dwarf_readu32 (as, a, &addr, &cie_id, arg)) < 0)
89 return ret;
90 if (cie_id != expected_id)
91 {
92 Debug (1, "Unexpected CIE id %x\n", cie_id);
93 return -UNW_EINVAL;
94 }
95 }
96 else
97 {
98 /* the CIE is in the 64-bit DWARF format */
99 uint64_t cie_id;
100 /* DWARF says CIE id should be 0xffffffffffffffff, but in
101 .eh_frame, it's 0 */
102 const uint64_t expected_id = (base) ? 0xffffffffffffffffull : 0;
103
104 if ((ret = dwarf_readu64 (as, a, &addr, &u64val, arg)) < 0)
105 return ret;
106 len = u64val;
107 cie_end_addr = addr + len;
108 if ((ret = dwarf_readu64 (as, a, &addr, &cie_id, arg)) < 0)
109 return ret;
110 if (cie_id != expected_id)
111 {
112 Debug (1, "Unexpected CIE id %llx\n", (long long) cie_id);
113 return -UNW_EINVAL;
114 }
115 }
116 dci->cie_instr_end = cie_end_addr;
117
118 if ((ret = dwarf_readu8 (as, a, &addr, &version, arg)) < 0)
119 return ret;
120
121 if (version != 1 && version != 3 && version != 4)
122 {
123 Debug (1, "Got CIE version %u, expected version 1, 3 or 4\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 (version == 4) {
142 uint8_t address_size;
143 if ((ret = dwarf_readu8(as, a, &addr, &address_size, arg)) < 0) {
144 return ret;
145 }
146 if (address_size != sizeof(unw_word_t)) {
147 return -UNW_EBADVERSION;
148 }
149 uint8_t segment_size;
150 if ((ret = dwarf_readu8(as, a, &addr, &segment_size, arg)) < 0) {
151 return ret;
152 }
153 // We don't support non-zero segment size.
154 if (segment_size != 0) {
155 return -UNW_EBADVERSION;
156 }
157 }
158 if ((ret = dwarf_read_uleb128 (as, a, &addr, &dci->code_align, arg)) < 0
159 || (ret = dwarf_read_sleb128 (as, a, &addr, &dci->data_align, arg)) < 0)
160 return ret;
161
162 /* Read the return-address column either as a u8 or as a uleb128. */
163 if (version == 1)
164 {
165 if ((ret = dwarf_readu8 (as, a, &addr, &ch, arg)) < 0)
166 return ret;
167 dci->ret_addr_column = ch;
168 }
169 else if ((ret = dwarf_read_uleb128 (as, a, &addr, &dci->ret_addr_column,
170 arg)) < 0)
171 return ret;
172
173 i = 0;
174 if (augstr[0] == 'z')
175 {
176 dci->sized_augmentation = 1;
177 if ((ret = dwarf_read_uleb128 (as, a, &addr, &aug_size, arg)) < 0)
178 return ret;
179 i++;
180 }
181
182 for (; i < sizeof (augstr) && augstr[i]; ++i)
183 switch (augstr[i])
184 {
185 case 'L':
186 /* read the LSDA pointer-encoding format. */
187 if ((ret = dwarf_readu8 (as, a, &addr, &ch, arg)) < 0)
188 return ret;
189 dci->lsda_encoding = ch;
190 break;
191
192 case 'R':
193 /* read the FDE pointer-encoding format. */
194 if ((ret = dwarf_readu8 (as, a, &addr, &fde_encoding, arg)) < 0)
195 return ret;
196 break;
197
198 case 'P':
199 /* read the personality-routine pointer-encoding format. */
200 if ((ret = dwarf_readu8 (as, a, &addr, &handler_encoding, arg)) < 0)
201 return ret;
202 if ((ret = dwarf_read_encoded_pointer (as, a, &addr, handler_encoding,
203 pi, &dci->handler, arg)) < 0)
204 return ret;
205 break;
206
207 case 'S':
208 /* This is a signal frame. */
209 dci->signal_frame = 1;
210
211 /* Temporarily set it to one so dwarf_parse_fde() knows that
212 it should fetch the actual ABI/TAG pair from the FDE. */
213 dci->have_abi_marker = 1;
214 break;
215
216 default:
217 Debug (1, "Unexpected augmentation string `%s'\n", augstr);
218 if (dci->sized_augmentation)
219 /* If we have the size of the augmentation body, we can skip
220 over the parts that we don't understand, so we're OK. */
221 goto done;
222 else
223 return -UNW_EINVAL;
224 }
225 done:
226 dci->fde_encoding = fde_encoding;
227 dci->cie_instr_start = addr;
228 Debug (15, "CIE parsed OK, augmentation = \"%s\", handler=0x%lx\n",
229 augstr, (long) dci->handler);
230 return 0;
231 }
232
233 /* Extract proc-info from the FDE starting at adress ADDR.
234
235 Pass BASE as zero for eh_frame behaviour, or a pointer to
236 debug_frame base for debug_frame behaviour. */
237
238 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,int need_unwind_info,unw_word_t base,void * arg)239 dwarf_extract_proc_info_from_fde (unw_addr_space_t as, unw_accessors_t *a,
240 unw_word_t *addrp, unw_proc_info_t *pi,
241 int need_unwind_info, unw_word_t base,
242 void *arg)
243 {
244 unw_word_t fde_end_addr, cie_addr, cie_offset_addr, aug_end_addr = 0;
245 unw_word_t start_ip, ip_range, aug_size, addr = *addrp;
246 int ret, ip_range_encoding;
247 struct dwarf_cie_info dci;
248 uint64_t u64val;
249 uint32_t u32val;
250
251 Debug (12, "FDE @ 0x%lx\n", (long) addr);
252
253 memset (&dci, 0, sizeof (dci));
254
255 if ((ret = dwarf_readu32 (as, a, &addr, &u32val, arg)) < 0)
256 return ret;
257
258 if (u32val != 0xffffffff)
259 {
260 int32_t cie_offset;
261
262 /* In some configurations, an FDE with a 0 length indicates the
263 end of the FDE-table. */
264 if (u32val == 0)
265 return -UNW_ENOINFO;
266
267 /* the FDE is in the 32-bit DWARF format */
268
269 *addrp = fde_end_addr = addr + u32val;
270 cie_offset_addr = addr;
271
272 if ((ret = dwarf_reads32 (as, a, &addr, &cie_offset, arg)) < 0)
273 return ret;
274
275 if (is_cie_id (cie_offset, base != 0))
276 /* ignore CIEs (happens during linear searches) */
277 return 0;
278
279 if (base != 0)
280 cie_addr = base + cie_offset;
281 else
282 /* DWARF says that the CIE_pointer in the FDE is a
283 .debug_frame-relative offset, but the GCC-generated .eh_frame
284 sections instead store a "pcrelative" offset, which is just
285 as fine as it's self-contained. */
286 cie_addr = cie_offset_addr - cie_offset;
287 }
288 else
289 {
290 int64_t cie_offset;
291
292 /* the FDE is in the 64-bit DWARF format */
293
294 if ((ret = dwarf_readu64 (as, a, &addr, &u64val, arg)) < 0)
295 return ret;
296
297 *addrp = fde_end_addr = addr + u64val;
298 cie_offset_addr = addr;
299
300 if ((ret = dwarf_reads64 (as, a, &addr, &cie_offset, arg)) < 0)
301 return ret;
302
303 if (is_cie_id (cie_offset, base != 0))
304 /* ignore CIEs (happens during linear searches) */
305 return 0;
306
307 if (base != 0)
308 cie_addr = base + cie_offset;
309 else
310 /* DWARF says that the CIE_pointer in the FDE is a
311 .debug_frame-relative offset, but the GCC-generated .eh_frame
312 sections instead store a "pcrelative" offset, which is just
313 as fine as it's self-contained. */
314 cie_addr = (unw_word_t) ((uint64_t) cie_offset_addr - cie_offset);
315 }
316
317 Debug (15, "looking for CIE at address %lx\n", (long) cie_addr);
318
319 if ((ret = parse_cie (as, a, cie_addr, pi, &dci, base, arg)) < 0)
320 return ret;
321
322 /* IP-range has same encoding as FDE pointers, except that it's
323 always an absolute value: */
324 ip_range_encoding = dci.fde_encoding & DW_EH_PE_FORMAT_MASK;
325
326 if ((ret = dwarf_read_encoded_pointer (as, a, &addr, dci.fde_encoding,
327 pi, &start_ip, arg)) < 0
328 || (ret = dwarf_read_encoded_pointer (as, a, &addr, ip_range_encoding,
329 pi, &ip_range, arg)) < 0)
330 return ret;
331 pi->start_ip = start_ip;
332 pi->end_ip = start_ip + ip_range;
333 pi->handler = dci.handler;
334
335 if (dci.sized_augmentation)
336 {
337 if ((ret = dwarf_read_uleb128 (as, a, &addr, &aug_size, arg)) < 0)
338 return ret;
339 aug_end_addr = addr + aug_size;
340 }
341
342 if ((ret = dwarf_read_encoded_pointer (as, a, &addr, dci.lsda_encoding,
343 pi, &pi->lsda, arg)) < 0)
344 return ret;
345
346 Debug (15, "FDE covers IP 0x%lx-0x%lx, LSDA=0x%lx\n",
347 (long) pi->start_ip, (long) pi->end_ip, (long) pi->lsda);
348
349 if (need_unwind_info)
350 {
351 pi->format = UNW_INFO_FORMAT_TABLE;
352 pi->unwind_info_size = sizeof (dci);
353 pi->unwind_info = mempool_alloc (&dwarf_cie_info_pool);
354 if (!pi->unwind_info)
355 return -UNW_ENOMEM;
356
357 if (dci.have_abi_marker)
358 {
359 if ((ret = dwarf_readu16 (as, a, &addr, &dci.abi, arg)) < 0
360 || (ret = dwarf_readu16 (as, a, &addr, &dci.tag, arg)) < 0)
361 return ret;
362 Debug (13, "Found ABI marker = (abi=%u, tag=%u)\n",
363 dci.abi, dci.tag);
364 }
365
366 if (dci.sized_augmentation)
367 dci.fde_instr_start = aug_end_addr;
368 else
369 dci.fde_instr_start = addr;
370 dci.fde_instr_end = fde_end_addr;
371
372 memcpy (pi->unwind_info, &dci, sizeof (dci));
373 }
374 return 0;
375 }
376