]>
Commit | Line | Data |
---|---|---|
eff02e4f ILT |
1 | /* dwarf.c -- Get file/line information from DWARF for backtraces. |
2 | Copyright (C) 2012 Free Software Foundation, Inc. | |
3 | Written by Ian Lance Taylor, Google. | |
4 | ||
5 | Redistribution and use in source and binary forms, with or without | |
6 | modification, are permitted provided that the following conditions are | |
7 | met: | |
8 | ||
9 | (1) Redistributions of source code must retain the above copyright | |
10 | notice, this list of conditions and the following disclaimer. | |
11 | ||
12 | (2) Redistributions in binary form must reproduce the above copyright | |
13 | notice, this list of conditions and the following disclaimer in | |
14 | the documentation and/or other materials provided with the | |
15 | distribution. | |
16 | ||
17 | (3) The name of the author may not be used to | |
18 | endorse or promote products derived from this software without | |
19 | specific prior written permission. | |
20 | ||
21 | THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR | |
22 | IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED | |
23 | WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE | |
24 | DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, | |
25 | INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES | |
26 | (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR | |
27 | SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | |
28 | HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, | |
29 | STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING | |
30 | IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE | |
31 | POSSIBILITY OF SUCH DAMAGE. */ | |
32 | ||
33 | #include "config.h" | |
34 | ||
35 | #include <errno.h> | |
36 | #include <stdint.h> | |
37 | #include <stdlib.h> | |
38 | #include <string.h> | |
39 | #include <sys/types.h> | |
40 | ||
41 | #include "dwarf2.h" | |
42 | #include "filenames.h" | |
43 | ||
44 | #include "backtrace.h" | |
45 | #include "internal.h" | |
46 | ||
772a71a9 ILT |
47 | #ifndef HAVE_DECL_STRNLEN |
48 | /* The function is defined in libiberty if needed. */ | |
49 | extern size_t strnlen (const char *, size_t); | |
50 | #endif | |
51 | ||
eff02e4f ILT |
52 | /* A buffer to read DWARF info. */ |
53 | ||
54 | struct dwarf_buf | |
55 | { | |
56 | /* Buffer name for error messages. */ | |
57 | const char *name; | |
58 | /* Start of the buffer. */ | |
59 | const unsigned char *start; | |
60 | /* Next byte to read. */ | |
61 | const unsigned char *buf; | |
62 | /* The number of bytes remaining. */ | |
63 | size_t left; | |
64 | /* Whether the data is big-endian. */ | |
65 | int is_bigendian; | |
66 | /* Error callback routine. */ | |
67 | backtrace_error_callback error_callback; | |
68 | /* Data for error_callback. */ | |
69 | void *data; | |
70 | /* Non-zero if we've reported an underflow error. */ | |
71 | int reported_underflow; | |
72 | }; | |
73 | ||
74 | /* A single attribute in a DWARF abbreviation. */ | |
75 | ||
76 | struct attr | |
77 | { | |
78 | /* The attribute name. */ | |
79 | enum dwarf_attribute name; | |
80 | /* The attribute form. */ | |
81 | enum dwarf_form form; | |
82 | }; | |
83 | ||
84 | /* A single DWARF abbreviation. */ | |
85 | ||
86 | struct abbrev | |
87 | { | |
88 | /* The abbrev code--the number used to refer to the abbrev. */ | |
89 | uint64_t code; | |
90 | /* The entry tag. */ | |
91 | enum dwarf_tag tag; | |
92 | /* Non-zero if this abbrev has child entries. */ | |
93 | int has_children; | |
94 | /* The number of attributes. */ | |
95 | size_t num_attrs; | |
96 | /* The attributes. */ | |
97 | struct attr *attrs; | |
98 | }; | |
99 | ||
100 | /* The DWARF abbreviations for a compilation unit. This structure | |
101 | only exists while reading the compilation unit. Most DWARF readers | |
102 | seem to a hash table to map abbrev ID's to abbrev entries. | |
103 | However, we primarily care about GCC, and GCC simply issues ID's in | |
104 | numerical order starting at 1. So we simply keep a sorted vector, | |
105 | and try to just look up the code. */ | |
106 | ||
107 | struct abbrevs | |
108 | { | |
109 | /* The number of abbrevs in the vector. */ | |
110 | size_t num_abbrevs; | |
111 | /* The abbrevs, sorted by the code field. */ | |
112 | struct abbrev *abbrevs; | |
113 | }; | |
114 | ||
115 | /* The different kinds of attribute values. */ | |
116 | ||
117 | enum attr_val_encoding | |
118 | { | |
119 | /* An address. */ | |
120 | ATTR_VAL_ADDRESS, | |
121 | /* A unsigned integer. */ | |
122 | ATTR_VAL_UINT, | |
123 | /* A sigd integer. */ | |
124 | ATTR_VAL_SINT, | |
125 | /* A string. */ | |
126 | ATTR_VAL_STRING, | |
127 | /* An offset to other data in the containing unit. */ | |
128 | ATTR_VAL_REF_UNIT, | |
129 | /* An offset to other data within the .dwarf_info section. */ | |
130 | ATTR_VAL_REF_INFO, | |
131 | /* An offset to data in some other section. */ | |
132 | ATTR_VAL_REF_SECTION, | |
133 | /* A type signature. */ | |
134 | ATTR_VAL_REF_TYPE, | |
135 | /* A block of data (not represented). */ | |
136 | ATTR_VAL_BLOCK, | |
137 | /* An expression (not represented). */ | |
138 | ATTR_VAL_EXPR, | |
139 | }; | |
140 | ||
141 | /* An attribute value. */ | |
142 | ||
143 | struct attr_val | |
144 | { | |
145 | /* How the value is stored in the field u. */ | |
146 | enum attr_val_encoding encoding; | |
147 | union | |
148 | { | |
149 | /* ATTR_VAL_ADDRESS, ATTR_VAL_UINT, ATTR_VAL_REF*. */ | |
150 | uint64_t uint; | |
151 | /* ATTR_VAL_SINT. */ | |
152 | int64_t sint; | |
153 | /* ATTR_VAL_STRING. */ | |
154 | const char *string; | |
155 | /* ATTR_VAL_BLOCK not stored. */ | |
156 | } u; | |
157 | }; | |
158 | ||
159 | /* The line number program header. */ | |
160 | ||
161 | struct line_header | |
162 | { | |
163 | /* The version of the line number information. */ | |
164 | int version; | |
165 | /* The minimum instruction length. */ | |
166 | unsigned int min_insn_len; | |
167 | /* The maximum number of ops per instruction. */ | |
168 | unsigned int max_ops_per_insn; | |
169 | /* The line base for special opcodes. */ | |
170 | int line_base; | |
171 | /* The line range for special opcodes. */ | |
172 | unsigned int line_range; | |
173 | /* The opcode base--the first special opcode. */ | |
174 | unsigned int opcode_base; | |
175 | /* Opcode lengths, indexed by opcode - 1. */ | |
176 | const unsigned char *opcode_lengths; | |
177 | /* The number of directory entries. */ | |
178 | size_t dirs_count; | |
179 | /* The directory entries. */ | |
180 | const char **dirs; | |
181 | /* The number of filenames. */ | |
182 | size_t filenames_count; | |
183 | /* The filenames. */ | |
184 | const char **filenames; | |
185 | }; | |
186 | ||
187 | /* Map a single PC value to a file/line. We will keep a vector of | |
188 | these sorted by PC value. Each file/line will be correct from the | |
189 | PC up to the PC of the next entry if there is one. We allocate one | |
190 | extra entry at the end so that we can use bsearch. */ | |
191 | ||
192 | struct line | |
193 | { | |
194 | /* PC. */ | |
195 | uintptr_t pc; | |
196 | /* File name. Many entries in the array are expected to point to | |
197 | the same file name. */ | |
198 | const char *filename; | |
199 | /* Line number. */ | |
200 | int lineno; | |
201 | }; | |
202 | ||
203 | /* A growable vector of line number information. This is used while | |
204 | reading the line numbers. */ | |
205 | ||
206 | struct line_vector | |
207 | { | |
208 | /* Memory. This is an array of struct line. */ | |
209 | struct backtrace_vector vec; | |
210 | /* Number of valid mappings. */ | |
211 | size_t count; | |
212 | }; | |
213 | ||
214 | /* A function described in the debug info. */ | |
215 | ||
216 | struct function | |
217 | { | |
218 | /* The name of the function. */ | |
219 | const char *name; | |
220 | /* If this is an inlined function, the filename of the call | |
221 | site. */ | |
222 | const char *caller_filename; | |
223 | /* If this is an inlined function, the line number of the call | |
224 | site. */ | |
225 | int caller_lineno; | |
226 | /* Map PC ranges to inlined functions. */ | |
227 | struct function_addrs *function_addrs; | |
228 | size_t function_addrs_count; | |
229 | }; | |
230 | ||
231 | /* An address range for a function. This maps a PC value to a | |
232 | specific function. */ | |
233 | ||
234 | struct function_addrs | |
235 | { | |
236 | /* Range is LOW <= PC < HIGH. */ | |
237 | uint64_t low; | |
238 | uint64_t high; | |
239 | /* Function for this address range. */ | |
240 | struct function *function; | |
241 | }; | |
242 | ||
243 | /* A growable vector of function address ranges. */ | |
244 | ||
245 | struct function_vector | |
246 | { | |
247 | /* Memory. This is an array of struct function_addrs. */ | |
248 | struct backtrace_vector vec; | |
249 | /* Number of address ranges present. */ | |
250 | size_t count; | |
251 | }; | |
252 | ||
253 | /* A DWARF compilation unit. This only holds the information we need | |
254 | to map a PC to a file and line. */ | |
255 | ||
256 | struct unit | |
257 | { | |
258 | /* The first entry for this compilation unit. */ | |
259 | const unsigned char *unit_data; | |
260 | /* The length of the data for this compilation unit. */ | |
261 | size_t unit_data_len; | |
262 | /* The offset of UNIT_DATA from the start of the information for | |
263 | this compilation unit. */ | |
264 | size_t unit_data_offset; | |
265 | /* DWARF version. */ | |
266 | int version; | |
267 | /* Whether unit is DWARF64. */ | |
268 | int is_dwarf64; | |
269 | /* Address size. */ | |
270 | int addrsize; | |
271 | /* Offset into line number information. */ | |
272 | off_t lineoff; | |
273 | /* Compilation command working directory. */ | |
274 | const char *comp_dir; | |
275 | /* The abbreviations for this unit. */ | |
276 | struct abbrevs abbrevs; | |
277 | ||
278 | /* The fields above this point are read in during initialization and | |
279 | may be accessed freely. The fields below this point are read in | |
280 | as needed, and therefore require care, as different threads may | |
281 | try to initialize them simultaneously. */ | |
282 | ||
283 | /* PC to line number mapping. This is NULL if the values have not | |
284 | been read. This is (struct line *) -1 if there was an error | |
285 | reading the values. */ | |
286 | struct line *lines; | |
287 | /* Number of entries in lines. */ | |
288 | size_t lines_count; | |
289 | /* PC ranges to function. */ | |
290 | struct function_addrs *function_addrs; | |
291 | size_t function_addrs_count; | |
292 | }; | |
293 | ||
294 | /* An address range for a compilation unit. This maps a PC value to a | |
295 | specific compilation unit. Note that we invert the representation | |
296 | in DWARF: instead of listing the units and attaching a list of | |
297 | ranges, we list the ranges and have each one point to the unit. | |
298 | This lets us do a binary search to find the unit. */ | |
299 | ||
300 | struct unit_addrs | |
301 | { | |
302 | /* Range is LOW <= PC < HIGH. */ | |
303 | uint64_t low; | |
304 | uint64_t high; | |
305 | /* Compilation unit for this address range. */ | |
306 | struct unit *u; | |
307 | }; | |
308 | ||
309 | /* A growable vector of compilation unit address ranges. */ | |
310 | ||
311 | struct unit_addrs_vector | |
312 | { | |
313 | /* Memory. This is an array of struct unit_addrs. */ | |
314 | struct backtrace_vector vec; | |
315 | /* Number of address ranges present. */ | |
316 | size_t count; | |
317 | }; | |
318 | ||
319 | /* The information we need to map a PC to a file and line. */ | |
320 | ||
321 | struct dwarf_data | |
322 | { | |
323 | /* A sorted list of address ranges. */ | |
324 | struct unit_addrs *addrs; | |
325 | /* Number of address ranges in list. */ | |
326 | size_t addrs_count; | |
327 | /* The unparsed .debug_info section. */ | |
328 | const unsigned char *dwarf_info; | |
329 | size_t dwarf_info_size; | |
330 | /* The unparsed .debug_line section. */ | |
331 | const unsigned char *dwarf_line; | |
332 | size_t dwarf_line_size; | |
333 | /* The unparsed .debug_ranges section. */ | |
334 | const unsigned char *dwarf_ranges; | |
335 | size_t dwarf_ranges_size; | |
336 | /* The unparsed .debug_str section. */ | |
337 | const unsigned char *dwarf_str; | |
338 | size_t dwarf_str_size; | |
339 | /* Whether the data is big-endian or not. */ | |
340 | int is_bigendian; | |
341 | /* A vector used for function addresses. We keep this here so that | |
342 | we can grow the vector as we read more functions. */ | |
343 | struct function_vector fvec; | |
344 | }; | |
345 | ||
346 | /* Report an error for a DWARF buffer. */ | |
347 | ||
348 | static void | |
349 | dwarf_buf_error (struct dwarf_buf *buf, const char *msg) | |
350 | { | |
351 | char b[200]; | |
352 | ||
353 | snprintf (b, sizeof b, "%s in %s at %d", | |
354 | msg, buf->name, (int) (buf->buf - buf->start)); | |
355 | buf->error_callback (buf->data, b, 0); | |
356 | } | |
357 | ||
358 | /* Require at least COUNT bytes in BUF. Return 1 if all is well, 0 on | |
359 | error. */ | |
360 | ||
361 | static int | |
362 | require (struct dwarf_buf *buf, size_t count) | |
363 | { | |
364 | if (buf->left >= count) | |
365 | return 1; | |
366 | ||
367 | if (!buf->reported_underflow) | |
368 | { | |
369 | dwarf_buf_error (buf, "DWARF underflow"); | |
370 | buf->reported_underflow = 1; | |
371 | } | |
372 | ||
373 | return 0; | |
374 | } | |
375 | ||
376 | /* Advance COUNT bytes in BUF. Return 1 if all is well, 0 on | |
377 | error. */ | |
378 | ||
379 | static int | |
380 | advance (struct dwarf_buf *buf, size_t count) | |
381 | { | |
382 | if (!require (buf, count)) | |
383 | return 0; | |
384 | buf->buf += count; | |
385 | buf->left -= count; | |
386 | return 1; | |
387 | } | |
388 | ||
389 | /* Read one byte from BUF and advance 1 byte. */ | |
390 | ||
391 | static unsigned char | |
392 | read_byte (struct dwarf_buf *buf) | |
393 | { | |
394 | const unsigned char *p = buf->buf; | |
395 | ||
396 | if (!advance (buf, 1)) | |
397 | return 0; | |
398 | return p[0]; | |
399 | } | |
400 | ||
401 | /* Read a signed char from BUF and advance 1 byte. */ | |
402 | ||
403 | static signed char | |
404 | read_sbyte (struct dwarf_buf *buf) | |
405 | { | |
406 | const unsigned char *p = buf->buf; | |
407 | ||
408 | if (!advance (buf, 1)) | |
409 | return 0; | |
410 | return (*p ^ 0x80) - 0x80; | |
411 | } | |
412 | ||
413 | /* Read a uint16 from BUF and advance 2 bytes. */ | |
414 | ||
415 | static uint16_t | |
416 | read_uint16 (struct dwarf_buf *buf) | |
417 | { | |
418 | const unsigned char *p = buf->buf; | |
419 | ||
420 | if (!advance (buf, 2)) | |
421 | return 0; | |
422 | if (buf->is_bigendian) | |
423 | return ((uint16_t) p[0] << 8) | (uint16_t) p[1]; | |
424 | else | |
425 | return ((uint16_t) p[1] << 8) | (uint16_t) p[0]; | |
426 | } | |
427 | ||
428 | /* Read a uint32 from BUF and advance 4 bytes. */ | |
429 | ||
430 | static uint32_t | |
431 | read_uint32 (struct dwarf_buf *buf) | |
432 | { | |
433 | const unsigned char *p = buf->buf; | |
434 | ||
435 | if (!advance (buf, 4)) | |
436 | return 0; | |
437 | if (buf->is_bigendian) | |
438 | return (((uint32_t) p[0] << 24) | ((uint32_t) p[1] << 16) | |
439 | | ((uint32_t) p[2] << 8) | (uint32_t) p[3]); | |
440 | else | |
441 | return (((uint32_t) p[3] << 24) | ((uint32_t) p[2] << 16) | |
442 | | ((uint32_t) p[1] << 8) | (uint32_t) p[0]); | |
443 | } | |
444 | ||
445 | /* Read a uint64 from BUF and advance 8 bytes. */ | |
446 | ||
447 | static uint64_t | |
448 | read_uint64 (struct dwarf_buf *buf) | |
449 | { | |
450 | const unsigned char *p = buf->buf; | |
451 | ||
452 | if (!advance (buf, 8)) | |
453 | return 0; | |
454 | if (buf->is_bigendian) | |
455 | return (((uint64_t) p[0] << 56) | ((uint64_t) p[1] << 48) | |
456 | | ((uint64_t) p[2] << 40) | ((uint64_t) p[3] << 32) | |
457 | | ((uint64_t) p[4] << 24) | ((uint64_t) p[5] << 16) | |
458 | | ((uint64_t) p[6] << 8) | (uint64_t) p[7]); | |
459 | else | |
460 | return (((uint64_t) p[7] << 56) | ((uint64_t) p[6] << 48) | |
461 | | ((uint64_t) p[5] << 40) | ((uint64_t) p[4] << 32) | |
462 | | ((uint64_t) p[3] << 24) | ((uint64_t) p[2] << 16) | |
463 | | ((uint64_t) p[1] << 8) | (uint64_t) p[0]); | |
464 | } | |
465 | ||
466 | /* Read an offset from BUF and advance the appropriate number of | |
467 | bytes. */ | |
468 | ||
469 | static uint64_t | |
470 | read_offset (struct dwarf_buf *buf, int is_dwarf64) | |
471 | { | |
472 | if (is_dwarf64) | |
473 | return read_uint64 (buf); | |
474 | else | |
475 | return read_uint32 (buf); | |
476 | } | |
477 | ||
478 | /* Read an address from BUF and advance the appropriate number of | |
479 | bytes. */ | |
480 | ||
481 | static uint64_t | |
482 | read_address (struct dwarf_buf *buf, int addrsize) | |
483 | { | |
484 | switch (addrsize) | |
485 | { | |
486 | case 1: | |
487 | return read_byte (buf); | |
488 | case 2: | |
489 | return read_uint16 (buf); | |
490 | case 4: | |
491 | return read_uint32 (buf); | |
492 | case 8: | |
493 | return read_uint64 (buf); | |
494 | default: | |
495 | dwarf_buf_error (buf, "unrecognized address size"); | |
496 | return 0; | |
497 | } | |
498 | } | |
499 | ||
500 | /* Return whether a value is the highest possible address, given the | |
501 | address size. */ | |
502 | ||
503 | static int | |
504 | is_highest_address (uint64_t address, int addrsize) | |
505 | { | |
506 | switch (addrsize) | |
507 | { | |
508 | case 1: | |
509 | return address == (unsigned char) -1; | |
510 | case 2: | |
511 | return address == (uint16_t) -1; | |
512 | case 4: | |
513 | return address == (uint32_t) -1; | |
514 | case 8: | |
515 | return address == (uint64_t) -1; | |
516 | default: | |
517 | return 0; | |
518 | } | |
519 | } | |
520 | ||
521 | /* Read an unsigned LEB128 number. */ | |
522 | ||
523 | static uint64_t | |
524 | read_uleb128 (struct dwarf_buf *buf) | |
525 | { | |
526 | uint64_t ret; | |
527 | unsigned int shift; | |
528 | unsigned char b; | |
529 | ||
530 | ret = 0; | |
531 | shift = 0; | |
532 | do | |
533 | { | |
534 | const unsigned char *p; | |
535 | ||
536 | p = buf->buf; | |
537 | if (!advance (buf, 1)) | |
538 | return 0; | |
539 | b = *p; | |
540 | ret |= ((uint64_t) (b & 0x7f)) << shift; | |
541 | shift += 7; | |
542 | } | |
543 | while ((b & 0x80) != 0); | |
544 | ||
545 | if (shift > 64) | |
546 | dwarf_buf_error (buf, "LEB128 overflows uint64_5"); | |
547 | ||
548 | return ret; | |
549 | } | |
550 | ||
551 | /* Read a signed LEB128 number. */ | |
552 | ||
553 | static int64_t | |
554 | read_sleb128 (struct dwarf_buf *buf) | |
555 | { | |
556 | uint64_t val; | |
557 | unsigned int shift; | |
558 | unsigned char b; | |
559 | ||
560 | val = 0; | |
561 | shift = 0; | |
562 | do | |
563 | { | |
564 | const unsigned char *p; | |
565 | ||
566 | p = buf->buf; | |
567 | if (!advance (buf, 1)) | |
568 | return 0; | |
569 | b = *p; | |
570 | val |= ((uint64_t) (b & 0x7f)) << shift; | |
571 | shift += 7; | |
572 | } | |
573 | while ((b & 0x80) != 0); | |
574 | ||
575 | if (shift > 64) | |
576 | dwarf_buf_error (buf, "signed LEB128 overflows uint64_t"); | |
577 | ||
578 | if ((b & 0x40) != 0) | |
579 | val |= ((uint64_t) -1) << shift; | |
580 | ||
581 | return (int64_t) val; | |
582 | } | |
583 | ||
584 | /* Return the length of an LEB128 number. */ | |
585 | ||
586 | static size_t | |
587 | leb128_len (const unsigned char *p) | |
588 | { | |
589 | size_t ret; | |
590 | ||
591 | ret = 1; | |
592 | while ((*p & 0x80) != 0) | |
593 | { | |
594 | ++p; | |
595 | ++ret; | |
596 | } | |
597 | return ret; | |
598 | } | |
599 | ||
600 | /* Free an abbreviations structure. */ | |
601 | ||
602 | static void | |
603 | free_abbrevs (struct backtrace_state *state, struct abbrevs *abbrevs, | |
604 | backtrace_error_callback error_callback, void *data) | |
605 | { | |
606 | size_t i; | |
607 | ||
608 | for (i = 0; i < abbrevs->num_abbrevs; ++i) | |
609 | backtrace_free (state, abbrevs->abbrevs[i].attrs, | |
610 | abbrevs->abbrevs[i].num_attrs * sizeof (struct attr), | |
611 | error_callback, data); | |
612 | backtrace_free (state, abbrevs->abbrevs, | |
613 | abbrevs->num_abbrevs * sizeof (struct abbrev), | |
614 | error_callback, data); | |
615 | abbrevs->num_abbrevs = 0; | |
616 | abbrevs->abbrevs = NULL; | |
617 | } | |
618 | ||
619 | /* Read an attribute value. Returns 1 on success, 0 on failure. If | |
620 | the value can be represented as a uint64_t, sets *VAL and sets | |
621 | *IS_VALID to 1. We don't try to store the value of other attribute | |
622 | forms, because we don't care about them. */ | |
623 | ||
624 | static int | |
625 | read_attribute (enum dwarf_form form, struct dwarf_buf *buf, | |
626 | int is_dwarf64, int version, int addrsize, | |
627 | const unsigned char *dwarf_str, size_t dwarf_str_size, | |
628 | struct attr_val *val) | |
629 | { | |
630 | switch (form) | |
631 | { | |
632 | case DW_FORM_addr: | |
633 | val->encoding = ATTR_VAL_ADDRESS; | |
634 | val->u.uint = read_address (buf, addrsize); | |
635 | return 1; | |
636 | case DW_FORM_block2: | |
637 | val->encoding = ATTR_VAL_BLOCK; | |
638 | return advance (buf, read_uint16 (buf)); | |
639 | case DW_FORM_block4: | |
640 | val->encoding = ATTR_VAL_BLOCK; | |
641 | return advance (buf, read_uint32 (buf)); | |
642 | case DW_FORM_data2: | |
643 | val->encoding = ATTR_VAL_UINT; | |
644 | val->u.uint = read_uint16 (buf); | |
645 | return 1; | |
646 | case DW_FORM_data4: | |
647 | val->encoding = ATTR_VAL_UINT; | |
648 | val->u.uint = read_uint32 (buf); | |
649 | return 1; | |
650 | case DW_FORM_data8: | |
651 | val->encoding = ATTR_VAL_UINT; | |
652 | val->u.uint = read_uint64 (buf); | |
653 | return 1; | |
654 | case DW_FORM_string: | |
655 | val->encoding = ATTR_VAL_STRING; | |
656 | val->u.string = (const char *) buf->buf; | |
657 | return advance (buf, strnlen ((const char *) buf->buf, buf->left) + 1); | |
658 | case DW_FORM_block: | |
659 | val->encoding = ATTR_VAL_BLOCK; | |
660 | return advance (buf, read_uleb128 (buf)); | |
661 | case DW_FORM_block1: | |
662 | val->encoding = ATTR_VAL_BLOCK; | |
663 | return advance (buf, read_byte (buf)); | |
664 | case DW_FORM_data1: | |
665 | val->encoding = ATTR_VAL_UINT; | |
666 | val->u.uint = read_byte (buf); | |
667 | return 1; | |
668 | case DW_FORM_flag: | |
669 | val->encoding = ATTR_VAL_UINT; | |
670 | val->u.uint = read_byte (buf); | |
671 | return 1; | |
672 | case DW_FORM_sdata: | |
673 | val->encoding = ATTR_VAL_SINT; | |
674 | val->u.sint = read_sleb128 (buf); | |
675 | return 1; | |
676 | case DW_FORM_strp: | |
677 | { | |
678 | uint64_t offset; | |
679 | ||
680 | offset = read_offset (buf, is_dwarf64); | |
681 | if (offset >= dwarf_str_size) | |
682 | { | |
683 | dwarf_buf_error (buf, "DW_FORM_strp out of range"); | |
684 | return 0; | |
685 | } | |
686 | val->encoding = ATTR_VAL_STRING; | |
687 | val->u.string = (const char *) dwarf_str + offset; | |
688 | return 1; | |
689 | } | |
690 | case DW_FORM_udata: | |
691 | val->encoding = ATTR_VAL_UINT; | |
692 | val->u.uint = read_uleb128 (buf); | |
693 | return 1; | |
694 | case DW_FORM_ref_addr: | |
695 | val->encoding = ATTR_VAL_REF_INFO; | |
696 | if (version == 2) | |
697 | val->u.uint = read_address (buf, addrsize); | |
698 | else | |
699 | val->u.uint = read_offset (buf, is_dwarf64); | |
700 | return 1; | |
701 | case DW_FORM_ref1: | |
702 | val->encoding = ATTR_VAL_REF_UNIT; | |
703 | val->u.uint = read_byte (buf); | |
704 | return 1; | |
705 | case DW_FORM_ref2: | |
706 | val->encoding = ATTR_VAL_REF_UNIT; | |
707 | val->u.uint = read_uint16 (buf); | |
708 | return 1; | |
709 | case DW_FORM_ref4: | |
710 | val->encoding = ATTR_VAL_REF_UNIT; | |
711 | val->u.uint = read_uint32 (buf); | |
712 | return 1; | |
713 | case DW_FORM_ref8: | |
714 | val->encoding = ATTR_VAL_REF_UNIT; | |
715 | val->u.uint = read_uint64 (buf); | |
716 | return 1; | |
717 | case DW_FORM_ref_udata: | |
718 | val->encoding = ATTR_VAL_REF_UNIT; | |
719 | val->u.uint = read_uleb128 (buf); | |
720 | return 1; | |
721 | case DW_FORM_indirect: | |
722 | { | |
723 | uint64_t form; | |
724 | ||
725 | form = read_uleb128 (buf); | |
726 | return read_attribute ((enum dwarf_form) form, buf, is_dwarf64, | |
727 | version, addrsize, dwarf_str, dwarf_str_size, | |
728 | val); | |
729 | } | |
730 | case DW_FORM_sec_offset: | |
731 | val->encoding = ATTR_VAL_REF_SECTION; | |
732 | val->u.uint = read_offset (buf, is_dwarf64); | |
733 | return 1; | |
734 | case DW_FORM_exprloc: | |
735 | val->encoding = ATTR_VAL_EXPR; | |
736 | return advance (buf, read_uleb128 (buf)); | |
737 | case DW_FORM_flag_present: | |
738 | val->encoding = ATTR_VAL_UINT; | |
739 | val->u.uint = 1; | |
740 | return 1; | |
741 | case DW_FORM_ref_sig8: | |
742 | val->encoding = ATTR_VAL_REF_TYPE; | |
743 | val->u.uint = read_uint64 (buf); | |
744 | return 1; | |
745 | case DW_FORM_GNU_addr_index: | |
746 | val->encoding = ATTR_VAL_REF_SECTION; | |
747 | val->u.uint = read_uleb128 (buf); | |
748 | return 1; | |
749 | case DW_FORM_GNU_str_index: | |
750 | val->encoding = ATTR_VAL_REF_SECTION; | |
751 | val->u.uint = read_uleb128 (buf); | |
752 | return 1; | |
753 | case DW_FORM_GNU_ref_alt: | |
754 | val->encoding = ATTR_VAL_REF_SECTION; | |
755 | val->u.uint = read_offset (buf, is_dwarf64); | |
756 | return 1; | |
757 | case DW_FORM_GNU_strp_alt: | |
758 | val->encoding = ATTR_VAL_REF_SECTION; | |
759 | val->u.uint = read_offset (buf, is_dwarf64); | |
760 | return 1; | |
761 | default: | |
762 | dwarf_buf_error (buf, "unrecognized DWARF form"); | |
763 | return 0; | |
764 | } | |
765 | } | |
766 | ||
767 | /* Compare function_addrs for qsort. When ranges are nested, make the | |
768 | smallest one sort last. */ | |
769 | ||
770 | static int | |
771 | function_addrs_compare (const void *v1, const void *v2) | |
772 | { | |
773 | const struct function_addrs *a1 = (const struct function_addrs *) v1; | |
774 | const struct function_addrs *a2 = (const struct function_addrs *) v2; | |
775 | ||
776 | if (a1->low < a2->low) | |
777 | return -1; | |
778 | if (a1->low > a2->low) | |
779 | return 1; | |
780 | if (a1->high < a2->high) | |
781 | return 1; | |
782 | if (a1->high > a2->high) | |
783 | return -1; | |
784 | return strcmp (a1->function->name, a2->function->name); | |
785 | } | |
786 | ||
787 | /* Compare a PC against a function_addrs for bsearch. Note that if | |
788 | there are multiple ranges containing PC, which one will be returned | |
789 | is unpredictable. We compensate for that in dwarf_fileline. */ | |
790 | ||
791 | static int | |
792 | function_addrs_search (const void *vkey, const void *ventry) | |
793 | { | |
794 | const uintptr_t *key = (const uintptr_t *) vkey; | |
795 | const struct function_addrs *entry = (const struct function_addrs *) ventry; | |
796 | uintptr_t pc; | |
797 | ||
798 | pc = *key; | |
799 | if (pc < entry->low) | |
800 | return -1; | |
801 | else if (pc >= entry->high) | |
802 | return 1; | |
803 | else | |
804 | return 0; | |
805 | } | |
806 | ||
807 | /* Add a new compilation unit address range to a vector. Returns 1 on | |
808 | success, 0 on failure. */ | |
809 | ||
810 | static int | |
811 | add_unit_addr (struct backtrace_state *state, struct unit_addrs addrs, | |
812 | backtrace_error_callback error_callback, void *data, | |
813 | struct unit_addrs_vector *vec) | |
814 | { | |
815 | struct unit_addrs *p; | |
816 | ||
817 | /* Try to merge with the last entry. */ | |
818 | if (vec->count > 0) | |
819 | { | |
820 | p = (struct unit_addrs *) vec->vec.base + (vec->count - 1); | |
821 | if ((addrs.low == p->high || addrs.low == p->high + 1) | |
822 | && addrs.u == p->u) | |
823 | { | |
824 | if (addrs.high > p->high) | |
825 | p->high = addrs.high; | |
826 | return 1; | |
827 | } | |
828 | } | |
829 | ||
830 | p = ((struct unit_addrs *) | |
831 | backtrace_vector_grow (state, sizeof (struct unit_addrs), | |
832 | error_callback, data, &vec->vec)); | |
833 | if (p == NULL) | |
834 | return 0; | |
835 | ||
836 | *p = addrs; | |
837 | ++vec->count; | |
838 | return 1; | |
839 | } | |
840 | ||
841 | /* Free a unit address vector. */ | |
842 | ||
843 | static void | |
844 | free_unit_addrs_vector (struct backtrace_state *state, | |
845 | struct unit_addrs_vector *vec, | |
846 | backtrace_error_callback error_callback, void *data) | |
847 | { | |
848 | struct unit_addrs *addrs; | |
849 | size_t i; | |
850 | ||
851 | addrs = (struct unit_addrs *) vec->vec.base; | |
852 | for (i = 0; i < vec->count; ++i) | |
853 | free_abbrevs (state, &addrs[i].u->abbrevs, error_callback, data); | |
854 | } | |
855 | ||
856 | /* Compare unit_addrs for qsort. When ranges are nested, make the | |
857 | smallest one sort last. */ | |
858 | ||
859 | static int | |
860 | unit_addrs_compare (const void *v1, const void *v2) | |
861 | { | |
862 | const struct unit_addrs *a1 = (const struct unit_addrs *) v1; | |
863 | const struct unit_addrs *a2 = (const struct unit_addrs *) v2; | |
864 | ||
865 | if (a1->low < a2->low) | |
866 | return -1; | |
867 | if (a1->low > a2->low) | |
868 | return 1; | |
869 | if (a1->high < a2->high) | |
870 | return 1; | |
871 | if (a1->high > a2->high) | |
872 | return -1; | |
873 | if (a1->u->lineoff < a2->u->lineoff) | |
874 | return -1; | |
875 | if (a1->u->lineoff > a2->u->lineoff) | |
876 | return 1; | |
877 | return 0; | |
878 | } | |
879 | ||
880 | /* Compare a PC against a unit_addrs for bsearch. Note that if there | |
881 | are multiple ranges containing PC, which one will be returned is | |
882 | unpredictable. We compensate for that in dwarf_fileline. */ | |
883 | ||
884 | static int | |
885 | unit_addrs_search (const void *vkey, const void *ventry) | |
886 | { | |
887 | const uintptr_t *key = (const uintptr_t *) vkey; | |
888 | const struct unit_addrs *entry = (const struct unit_addrs *) ventry; | |
889 | uintptr_t pc; | |
890 | ||
891 | pc = *key; | |
892 | if (pc < entry->low) | |
893 | return -1; | |
894 | else if (pc >= entry->high) | |
895 | return 1; | |
896 | else | |
897 | return 0; | |
898 | } | |
899 | ||
900 | /* Sort the line vector by PC. We want a stable sort here. We know | |
901 | that the pointers are into the same array, so it is safe to compare | |
902 | them directly. */ | |
903 | ||
904 | static int | |
905 | line_compare (const void *v1, const void *v2) | |
906 | { | |
907 | const struct line *ln1 = (const struct line *) v1; | |
908 | const struct line *ln2 = (const struct line *) v2; | |
909 | ||
910 | if (ln1->pc < ln2->pc) | |
911 | return -1; | |
912 | else if (ln1->pc > ln2->pc) | |
913 | return 1; | |
914 | else if (ln1 < ln2) | |
915 | return -1; | |
916 | else if (ln1 > ln2) | |
917 | return 1; | |
918 | else | |
919 | return 0; | |
920 | } | |
921 | ||
922 | /* Find a PC in a line vector. We always allocate an extra entry at | |
923 | the end of the lines vector, so that this routine can safely look | |
924 | at the next entry. Note that when there are multiple mappings for | |
925 | the same PC value, this will return the last one. */ | |
926 | ||
927 | static int | |
928 | line_search (const void *vkey, const void *ventry) | |
929 | { | |
930 | const uintptr_t *key = (const uintptr_t *) vkey; | |
931 | const struct line *entry = (const struct line *) ventry; | |
932 | uintptr_t pc; | |
933 | ||
934 | pc = *key; | |
935 | if (pc < entry->pc) | |
936 | return -1; | |
937 | else if (pc >= (entry + 1)->pc) | |
938 | return 1; | |
939 | else | |
940 | return 0; | |
941 | } | |
942 | ||
943 | /* Sort the abbrevs by the abbrev code. This function is passed to | |
944 | both qsort and bsearch. */ | |
945 | ||
946 | static int | |
947 | abbrev_compare (const void *v1, const void *v2) | |
948 | { | |
949 | const struct abbrev *a1 = (const struct abbrev *) v1; | |
950 | const struct abbrev *a2 = (const struct abbrev *) v2; | |
951 | ||
952 | if (a1->code < a2->code) | |
953 | return -1; | |
954 | else if (a1->code > a2->code) | |
955 | return 1; | |
956 | else | |
957 | { | |
958 | /* This really shouldn't happen. It means there are two | |
959 | different abbrevs with the same code, and that means we don't | |
960 | know which one lookup_abbrev should return. */ | |
961 | return 0; | |
962 | } | |
963 | } | |
964 | ||
965 | /* Read the abbreviation table for a compilation unit. Returns 1 on | |
966 | success, 0 on failure. */ | |
967 | ||
968 | static int | |
969 | read_abbrevs (struct backtrace_state *state, uint64_t abbrev_offset, | |
970 | const unsigned char *dwarf_abbrev, size_t dwarf_abbrev_size, | |
971 | int is_bigendian, backtrace_error_callback error_callback, | |
972 | void *data, struct abbrevs *abbrevs) | |
973 | { | |
974 | struct dwarf_buf abbrev_buf; | |
975 | struct dwarf_buf count_buf; | |
976 | size_t num_abbrevs; | |
977 | ||
978 | abbrevs->num_abbrevs = 0; | |
979 | abbrevs->abbrevs = NULL; | |
980 | ||
981 | if (abbrev_offset >= dwarf_abbrev_size) | |
982 | { | |
983 | error_callback (data, "abbrev offset out of range", 0); | |
984 | return 0; | |
985 | } | |
986 | ||
987 | abbrev_buf.name = ".debug_abbrev"; | |
988 | abbrev_buf.start = dwarf_abbrev; | |
989 | abbrev_buf.buf = dwarf_abbrev + abbrev_offset; | |
990 | abbrev_buf.left = dwarf_abbrev_size - abbrev_offset; | |
991 | abbrev_buf.is_bigendian = is_bigendian; | |
992 | abbrev_buf.error_callback = error_callback; | |
993 | abbrev_buf.data = data; | |
994 | abbrev_buf.reported_underflow = 0; | |
995 | ||
996 | /* Count the number of abbrevs in this list. */ | |
997 | ||
998 | count_buf = abbrev_buf; | |
999 | num_abbrevs = 0; | |
1000 | while (read_uleb128 (&count_buf) != 0) | |
1001 | { | |
1002 | if (count_buf.reported_underflow) | |
1003 | return 0; | |
1004 | ++num_abbrevs; | |
1005 | // Skip tag. | |
1006 | read_uleb128 (&count_buf); | |
1007 | // Skip has_children. | |
1008 | read_byte (&count_buf); | |
1009 | // Skip attributes. | |
1010 | while (read_uleb128 (&count_buf) != 0) | |
1011 | read_uleb128 (&count_buf); | |
1012 | // Skip form of last attribute. | |
1013 | read_uleb128 (&count_buf); | |
1014 | } | |
1015 | ||
1016 | if (count_buf.reported_underflow) | |
1017 | return 0; | |
1018 | ||
1019 | if (num_abbrevs == 0) | |
1020 | return 1; | |
1021 | ||
1022 | abbrevs->num_abbrevs = num_abbrevs; | |
1023 | abbrevs->abbrevs = ((struct abbrev *) | |
1024 | backtrace_alloc (state, | |
1025 | num_abbrevs * sizeof (struct abbrev), | |
1026 | error_callback, data)); | |
1027 | if (abbrevs->abbrevs == NULL) | |
1028 | return 0; | |
1029 | memset (abbrevs->abbrevs, 0, num_abbrevs * sizeof (struct abbrev)); | |
1030 | ||
1031 | num_abbrevs = 0; | |
1032 | while (1) | |
1033 | { | |
1034 | uint64_t code; | |
1035 | struct abbrev a; | |
1036 | size_t num_attrs; | |
1037 | struct attr *attrs; | |
1038 | ||
1039 | if (abbrev_buf.reported_underflow) | |
1040 | goto fail; | |
1041 | ||
1042 | code = read_uleb128 (&abbrev_buf); | |
1043 | if (code == 0) | |
1044 | break; | |
1045 | ||
1046 | a.code = code; | |
1047 | a.tag = (enum dwarf_tag) read_uleb128 (&abbrev_buf); | |
1048 | a.has_children = read_byte (&abbrev_buf); | |
1049 | ||
1050 | count_buf = abbrev_buf; | |
1051 | num_attrs = 0; | |
1052 | while (read_uleb128 (&count_buf) != 0) | |
1053 | { | |
1054 | ++num_attrs; | |
1055 | read_uleb128 (&count_buf); | |
1056 | } | |
1057 | ||
1058 | if (num_attrs == 0) | |
1059 | { | |
1060 | attrs = NULL; | |
1061 | read_uleb128 (&abbrev_buf); | |
1062 | read_uleb128 (&abbrev_buf); | |
1063 | } | |
1064 | else | |
1065 | { | |
1066 | attrs = ((struct attr *) | |
1067 | backtrace_alloc (state, num_attrs * sizeof *attrs, | |
1068 | error_callback, data)); | |
1069 | if (attrs == NULL) | |
1070 | goto fail; | |
1071 | num_attrs = 0; | |
1072 | while (1) | |
1073 | { | |
1074 | uint64_t name; | |
1075 | uint64_t form; | |
1076 | ||
1077 | name = read_uleb128 (&abbrev_buf); | |
1078 | form = read_uleb128 (&abbrev_buf); | |
1079 | if (name == 0) | |
1080 | break; | |
1081 | attrs[num_attrs].name = (enum dwarf_attribute) name; | |
1082 | attrs[num_attrs].form = (enum dwarf_form) form; | |
1083 | ++num_attrs; | |
1084 | } | |
1085 | } | |
1086 | ||
1087 | a.num_attrs = num_attrs; | |
1088 | a.attrs = attrs; | |
1089 | ||
1090 | abbrevs->abbrevs[num_abbrevs] = a; | |
1091 | ++num_abbrevs; | |
1092 | } | |
1093 | ||
1094 | qsort (abbrevs->abbrevs, abbrevs->num_abbrevs, sizeof (struct abbrev), | |
1095 | abbrev_compare); | |
1096 | ||
1097 | return 1; | |
1098 | ||
1099 | fail: | |
1100 | free_abbrevs (state, abbrevs, error_callback, data); | |
1101 | return 0; | |
1102 | } | |
1103 | ||
1104 | /* Return the abbrev information for an abbrev code. */ | |
1105 | ||
1106 | static const struct abbrev * | |
1107 | lookup_abbrev (struct abbrevs *abbrevs, uint64_t code, | |
1108 | backtrace_error_callback error_callback, void *data) | |
1109 | { | |
1110 | struct abbrev key; | |
1111 | void *p; | |
1112 | ||
1113 | /* With GCC, where abbrevs are simply numbered in order, we should | |
1114 | be able to just look up the entry. */ | |
1115 | if (code - 1 < abbrevs->num_abbrevs | |
1116 | && abbrevs->abbrevs[code - 1].code == code) | |
1117 | return &abbrevs->abbrevs[code - 1]; | |
1118 | ||
1119 | /* Otherwise we have to search. */ | |
1120 | memset (&key, 0, sizeof key); | |
1121 | key.code = code; | |
1122 | p = bsearch (&key, abbrevs->abbrevs, abbrevs->num_abbrevs, | |
1123 | sizeof (struct abbrev), abbrev_compare); | |
1124 | if (p == NULL) | |
1125 | { | |
1126 | error_callback (data, "invalid abbreviation code", 0); | |
1127 | return NULL; | |
1128 | } | |
1129 | return (const struct abbrev *) p; | |
1130 | } | |
1131 | ||
1132 | /* Add non-contiguous address ranges for a compilation unit. Returns | |
1133 | 1 on success, 0 on failure. */ | |
1134 | ||
1135 | static int | |
1136 | add_unit_ranges (struct backtrace_state *state, struct unit *u, | |
1137 | uint64_t ranges, uint64_t base, int is_bigendian, | |
1138 | const unsigned char *dwarf_ranges, size_t dwarf_ranges_size, | |
1139 | backtrace_error_callback error_callback, void *data, | |
1140 | struct unit_addrs_vector *addrs) | |
1141 | { | |
1142 | struct dwarf_buf ranges_buf; | |
1143 | ||
1144 | if (ranges >= dwarf_ranges_size) | |
1145 | { | |
1146 | error_callback (data, "ranges offset out of range", 0); | |
1147 | return 0; | |
1148 | } | |
1149 | ||
1150 | ranges_buf.name = ".debug_ranges"; | |
1151 | ranges_buf.start = dwarf_ranges; | |
1152 | ranges_buf.buf = dwarf_ranges + ranges; | |
1153 | ranges_buf.left = dwarf_ranges_size - ranges; | |
1154 | ranges_buf.is_bigendian = is_bigendian; | |
1155 | ranges_buf.error_callback = error_callback; | |
1156 | ranges_buf.data = data; | |
1157 | ranges_buf.reported_underflow = 0; | |
1158 | ||
1159 | while (1) | |
1160 | { | |
1161 | uint64_t low; | |
1162 | uint64_t high; | |
1163 | ||
1164 | if (ranges_buf.reported_underflow) | |
1165 | return 0; | |
1166 | ||
1167 | low = read_address (&ranges_buf, u->addrsize); | |
1168 | high = read_address (&ranges_buf, u->addrsize); | |
1169 | ||
1170 | if (low == 0 && high == 0) | |
1171 | break; | |
1172 | ||
1173 | if (is_highest_address (low, u->addrsize)) | |
1174 | base = high; | |
1175 | else | |
1176 | { | |
1177 | struct unit_addrs a; | |
1178 | ||
1179 | a.low = low + base; | |
1180 | a.high = high + base; | |
1181 | a.u = u; | |
1182 | if (!add_unit_addr (state, a, error_callback, data, addrs)) | |
1183 | return 0; | |
1184 | } | |
1185 | } | |
1186 | ||
1187 | if (ranges_buf.reported_underflow) | |
1188 | return 0; | |
1189 | ||
1190 | return 1; | |
1191 | } | |
1192 | ||
1193 | /* Build a mapping from address ranges to the compilation units where | |
1194 | the line number information for that range can be found. Returns 1 | |
1195 | on success, 0 on failure. */ | |
1196 | ||
1197 | static int | |
1198 | build_address_map (struct backtrace_state *state, | |
1199 | const unsigned char *dwarf_info, size_t dwarf_info_size, | |
1200 | const unsigned char *dwarf_abbrev, size_t dwarf_abbrev_size, | |
1201 | const unsigned char *dwarf_ranges, size_t dwarf_ranges_size, | |
1202 | const unsigned char *dwarf_str, size_t dwarf_str_size, | |
1203 | int is_bigendian, backtrace_error_callback error_callback, | |
1204 | void *data, struct unit_addrs_vector *addrs) | |
1205 | { | |
1206 | struct dwarf_buf info; | |
1207 | struct abbrevs abbrevs; | |
1208 | ||
1209 | memset (&addrs->vec, 0, sizeof addrs->vec); | |
1210 | addrs->count = 0; | |
1211 | ||
1212 | /* Read through the .debug_info section. FIXME: Should we use the | |
1213 | .debug_aranges section? gdb and addr2line don't use it, but I'm | |
1214 | not sure why. */ | |
1215 | ||
1216 | info.name = ".debug_info"; | |
1217 | info.start = dwarf_info; | |
1218 | info.buf = dwarf_info; | |
1219 | info.left = dwarf_info_size; | |
1220 | info.is_bigendian = is_bigendian; | |
1221 | info.error_callback = error_callback; | |
1222 | info.data = data; | |
1223 | info.reported_underflow = 0; | |
1224 | ||
1225 | memset (&abbrevs, 0, sizeof abbrevs); | |
1226 | while (info.left > 0) | |
1227 | { | |
1228 | const unsigned char *unit_data_start; | |
1229 | uint64_t len; | |
1230 | int is_dwarf64; | |
1231 | struct dwarf_buf unit_buf; | |
1232 | int version; | |
1233 | uint64_t abbrev_offset; | |
1234 | const struct abbrev *abbrev; | |
1235 | int addrsize; | |
1236 | const unsigned char *unit_data; | |
1237 | size_t unit_data_len; | |
1238 | size_t unit_data_offset; | |
1239 | uint64_t code; | |
1240 | size_t i; | |
1241 | uint64_t lowpc; | |
1242 | int have_lowpc; | |
1243 | uint64_t highpc; | |
1244 | int have_highpc; | |
1245 | int highpc_is_relative; | |
1246 | uint64_t ranges; | |
1247 | int have_ranges; | |
1248 | uint64_t lineoff; | |
1249 | int have_lineoff; | |
1250 | const char *comp_dir; | |
1251 | ||
1252 | if (info.reported_underflow) | |
1253 | goto fail; | |
1254 | ||
1255 | unit_data_start = info.buf; | |
1256 | ||
1257 | is_dwarf64 = 0; | |
1258 | len = read_uint32 (&info); | |
1259 | if (len == 0xffffffff) | |
1260 | { | |
1261 | len = read_uint64 (&info); | |
1262 | is_dwarf64 = 1; | |
1263 | } | |
1264 | ||
1265 | unit_buf = info; | |
1266 | unit_buf.start = info.buf; | |
1267 | unit_buf.left = len; | |
1268 | ||
1269 | if (!advance (&info, len)) | |
1270 | goto fail; | |
1271 | ||
1272 | version = read_uint16 (&unit_buf); | |
1273 | if (version < 2 || version > 4) | |
1274 | { | |
1275 | dwarf_buf_error (&unit_buf, "unrecognized DWARF version"); | |
1276 | goto fail; | |
1277 | } | |
1278 | ||
1279 | abbrev_offset = read_offset (&unit_buf, is_dwarf64); | |
1280 | if (!read_abbrevs (state, abbrev_offset, dwarf_abbrev, dwarf_abbrev_size, | |
1281 | is_bigendian, error_callback, data, &abbrevs)) | |
1282 | goto fail; | |
1283 | ||
1284 | addrsize = read_byte (&unit_buf); | |
1285 | ||
1286 | unit_data = unit_buf.buf; | |
1287 | unit_data_len = unit_buf.left; | |
1288 | unit_data_offset = unit_buf.buf - unit_data_start; | |
1289 | ||
1290 | /* We only look at the first attribute in the compilation unit. | |
1291 | In practice this will be a DW_TAG_compile_unit which will | |
1292 | tell us the PC range and where to find the line number | |
1293 | information. */ | |
1294 | ||
1295 | code = read_uleb128 (&unit_buf); | |
1296 | abbrev = lookup_abbrev (&abbrevs, code, error_callback, data); | |
1297 | if (abbrev == NULL) | |
1298 | goto fail; | |
1299 | ||
1300 | lowpc = 0; | |
1301 | have_lowpc = 0; | |
1302 | highpc = 0; | |
1303 | have_highpc = 0; | |
1304 | highpc_is_relative = 0; | |
1305 | ranges = 0; | |
1306 | have_ranges = 0; | |
1307 | lineoff = 0; | |
1308 | have_lineoff = 0; | |
1309 | comp_dir = NULL; | |
1310 | for (i = 0; i < abbrev->num_attrs; ++i) | |
1311 | { | |
1312 | struct attr_val val; | |
1313 | ||
1314 | if (!read_attribute (abbrev->attrs[i].form, &unit_buf, is_dwarf64, | |
1315 | version, addrsize, dwarf_str, dwarf_str_size, | |
1316 | &val)) | |
1317 | goto fail; | |
1318 | ||
1319 | switch (abbrev->attrs[i].name) | |
1320 | { | |
1321 | case DW_AT_low_pc: | |
1322 | if (val.encoding == ATTR_VAL_ADDRESS) | |
1323 | { | |
1324 | lowpc = val.u.uint; | |
1325 | have_lowpc = 1; | |
1326 | } | |
1327 | break; | |
1328 | case DW_AT_high_pc: | |
1329 | if (val.encoding == ATTR_VAL_ADDRESS) | |
1330 | { | |
1331 | highpc = val.u.uint; | |
1332 | have_highpc = 1; | |
1333 | } | |
1334 | else if (val.encoding == ATTR_VAL_UINT) | |
1335 | { | |
1336 | highpc = val.u.uint; | |
1337 | have_highpc = 1; | |
1338 | highpc_is_relative = 1; | |
1339 | } | |
1340 | break; | |
1341 | case DW_AT_ranges: | |
1342 | if (val.encoding == ATTR_VAL_UINT | |
1343 | || val.encoding == ATTR_VAL_REF_SECTION) | |
1344 | { | |
1345 | ranges = val.u.uint; | |
1346 | have_ranges = 1; | |
1347 | } | |
1348 | break; | |
1349 | case DW_AT_stmt_list: | |
1350 | if (val.encoding == ATTR_VAL_UINT | |
1351 | || val.encoding == ATTR_VAL_REF_SECTION) | |
1352 | { | |
1353 | lineoff = val.u.uint; | |
1354 | have_lineoff = 1; | |
1355 | } | |
1356 | break; | |
1357 | case DW_AT_comp_dir: | |
1358 | if (val.encoding == ATTR_VAL_STRING) | |
1359 | comp_dir = val.u.string; | |
1360 | break; | |
1361 | default: | |
1362 | break; | |
1363 | } | |
1364 | } | |
1365 | ||
1366 | if (unit_buf.reported_underflow) | |
1367 | goto fail; | |
1368 | ||
1369 | if (((have_lowpc && have_highpc) || have_ranges) && have_lineoff) | |
1370 | { | |
1371 | struct unit *u; | |
1372 | struct unit_addrs a; | |
1373 | ||
1374 | u = ((struct unit *) | |
1375 | backtrace_alloc (state, sizeof *u, error_callback, data)); | |
1376 | if (u == NULL) | |
1377 | goto fail; | |
1378 | u->unit_data = unit_data; | |
1379 | u->unit_data_len = unit_data_len; | |
1380 | u->unit_data_offset = unit_data_offset; | |
1381 | u->version = version; | |
1382 | u->is_dwarf64 = is_dwarf64; | |
1383 | u->addrsize = addrsize; | |
1384 | u->comp_dir = comp_dir; | |
1385 | u->lineoff = lineoff; | |
1386 | u->abbrevs = abbrevs; | |
1387 | memset (&abbrevs, 0, sizeof abbrevs); | |
1388 | ||
1389 | /* The actual line number mappings will be read as | |
1390 | needed. */ | |
1391 | u->lines = NULL; | |
1392 | u->lines_count = 0; | |
1393 | u->function_addrs = NULL; | |
1394 | u->function_addrs_count = 0; | |
1395 | ||
1396 | if (have_ranges) | |
1397 | { | |
1398 | if (!add_unit_ranges (state, u, ranges, lowpc, is_bigendian, | |
1399 | dwarf_ranges, dwarf_ranges_size, | |
1400 | error_callback, data, addrs)) | |
1401 | { | |
1402 | free_abbrevs (state, &u->abbrevs, error_callback, data); | |
1403 | backtrace_free (state, u, sizeof *u, error_callback, data); | |
1404 | goto fail; | |
1405 | } | |
1406 | } | |
1407 | else | |
1408 | { | |
1409 | if (highpc_is_relative) | |
1410 | highpc += lowpc; | |
1411 | a.low = lowpc; | |
1412 | a.high = highpc; | |
1413 | a.u = u; | |
1414 | ||
1415 | if (!add_unit_addr (state, a, error_callback, data, addrs)) | |
1416 | { | |
1417 | free_abbrevs (state, &u->abbrevs, error_callback, data); | |
1418 | backtrace_free (state, u, sizeof *u, error_callback, data); | |
1419 | goto fail; | |
1420 | } | |
1421 | } | |
1422 | } | |
1423 | else | |
1424 | { | |
1425 | free_abbrevs (state, &abbrevs, error_callback, data); | |
1426 | memset (&abbrevs, 0, sizeof abbrevs); | |
1427 | } | |
1428 | } | |
1429 | if (info.reported_underflow) | |
1430 | goto fail; | |
1431 | ||
1432 | return 1; | |
1433 | ||
1434 | fail: | |
1435 | free_abbrevs (state, &abbrevs, error_callback, data); | |
1436 | free_unit_addrs_vector (state, addrs, error_callback, data); | |
1437 | return 0; | |
1438 | } | |
1439 | ||
1440 | /* Add a new mapping to the vector of line mappings that we are | |
1441 | building. Returns 1 on success, 0 on failure. */ | |
1442 | ||
1443 | static int | |
1444 | add_line (struct backtrace_state *state, uintptr_t pc, const char *filename, | |
1445 | int lineno, backtrace_error_callback error_callback, void *data, | |
1446 | struct line_vector *vec) | |
1447 | { | |
1448 | struct line *ln; | |
1449 | ||
1450 | /* If we are adding the same mapping, ignore it. This can happen | |
1451 | when using discriminators. */ | |
1452 | if (vec->count > 0) | |
1453 | { | |
1454 | ln = (struct line *) vec->vec.base + (vec->count - 1); | |
1455 | if (pc == ln->pc && filename == ln->filename && lineno == ln->lineno) | |
1456 | return 1; | |
1457 | } | |
1458 | ||
1459 | ln = ((struct line *) | |
1460 | backtrace_vector_grow (state, sizeof (struct line), error_callback, | |
1461 | data, &vec->vec)); | |
1462 | if (ln == NULL) | |
1463 | return 0; | |
1464 | ||
1465 | ln->pc = pc; | |
1466 | ln->filename = filename; | |
1467 | ln->lineno = lineno; | |
1468 | ||
1469 | ++vec->count; | |
1470 | ||
1471 | return 1; | |
1472 | } | |
1473 | ||
1474 | /* Free the line header information. If FREE_FILENAMES is true we | |
1475 | free the file names themselves, otherwise we leave them, as there | |
1476 | may be line structures pointing to them. */ | |
1477 | ||
1478 | static void | |
1479 | free_line_header (struct backtrace_state *state, struct line_header *hdr, | |
1480 | backtrace_error_callback error_callback, void *data) | |
1481 | { | |
1482 | backtrace_free (state, hdr->dirs, hdr->dirs_count * sizeof (const char *), | |
1483 | error_callback, data); | |
1484 | backtrace_free (state, hdr->filenames, | |
1485 | hdr->filenames_count * sizeof (char *), | |
1486 | error_callback, data); | |
1487 | } | |
1488 | ||
1489 | /* Read the line header. Return 1 on success, 0 on failure. */ | |
1490 | ||
1491 | static int | |
1492 | read_line_header (struct backtrace_state *state, struct unit *u, | |
1493 | int is_dwarf64, struct dwarf_buf *line_buf, | |
1494 | struct line_header *hdr) | |
1495 | { | |
1496 | uint64_t hdrlen; | |
1497 | struct dwarf_buf hdr_buf; | |
1498 | const unsigned char *p; | |
1499 | const unsigned char *pend; | |
1500 | size_t i; | |
1501 | ||
1502 | hdr->version = read_uint16 (line_buf); | |
1503 | if (hdr->version < 2 || hdr->version > 4) | |
1504 | { | |
1505 | dwarf_buf_error (line_buf, "unsupported line number version"); | |
1506 | return 0; | |
1507 | } | |
1508 | ||
1509 | hdrlen = read_offset (line_buf, is_dwarf64); | |
1510 | ||
1511 | hdr_buf = *line_buf; | |
1512 | hdr_buf.left = hdrlen; | |
1513 | ||
1514 | if (!advance (line_buf, hdrlen)) | |
1515 | return 0; | |
1516 | ||
1517 | hdr->min_insn_len = read_byte (&hdr_buf); | |
1518 | if (hdr->version < 4) | |
1519 | hdr->max_ops_per_insn = 1; | |
1520 | else | |
1521 | hdr->max_ops_per_insn = read_byte (&hdr_buf); | |
1522 | ||
1523 | /* We don't care about default_is_stmt. */ | |
1524 | read_byte (&hdr_buf); | |
1525 | ||
1526 | hdr->line_base = read_sbyte (&hdr_buf); | |
1527 | hdr->line_range = read_byte (&hdr_buf); | |
1528 | ||
1529 | hdr->opcode_base = read_byte (&hdr_buf); | |
1530 | hdr->opcode_lengths = hdr_buf.buf; | |
1531 | if (!advance (&hdr_buf, hdr->opcode_base - 1)) | |
1532 | return 0; | |
1533 | ||
1534 | /* Count the number of directory entries. */ | |
1535 | hdr->dirs_count = 0; | |
1536 | p = hdr_buf.buf; | |
1537 | pend = p + hdr_buf.left; | |
1538 | while (p < pend && *p != '\0') | |
1539 | { | |
1540 | p += strnlen((const char *) p, pend - p) + 1; | |
1541 | ++hdr->dirs_count; | |
1542 | } | |
1543 | ||
1544 | hdr->dirs = ((const char **) | |
1545 | backtrace_alloc (state, | |
1546 | hdr->dirs_count * sizeof (const char *), | |
1547 | line_buf->error_callback, line_buf->data)); | |
1548 | if (hdr->dirs == NULL) | |
1549 | return 0; | |
1550 | ||
1551 | i = 0; | |
1552 | while (*hdr_buf.buf != '\0') | |
1553 | { | |
1554 | if (hdr_buf.reported_underflow) | |
1555 | return 0; | |
1556 | ||
1557 | hdr->dirs[i] = (const char *) hdr_buf.buf; | |
1558 | ++i; | |
1559 | if (!advance (&hdr_buf, | |
1560 | strnlen ((const char *) hdr_buf.buf, hdr_buf.left) + 1)) | |
1561 | return 0; | |
1562 | } | |
1563 | if (!advance (&hdr_buf, 1)) | |
1564 | return 0; | |
1565 | ||
1566 | /* Count the number of file entries. */ | |
1567 | hdr->filenames_count = 0; | |
1568 | p = hdr_buf.buf; | |
1569 | pend = p + hdr_buf.left; | |
1570 | while (p < pend && *p != '\0') | |
1571 | { | |
1572 | p += strnlen ((const char *) p, pend - p) + 1; | |
1573 | p += leb128_len (p); | |
1574 | p += leb128_len (p); | |
1575 | p += leb128_len (p); | |
1576 | ++hdr->filenames_count; | |
1577 | } | |
1578 | ||
1579 | hdr->filenames = ((const char **) | |
1580 | backtrace_alloc (state, | |
1581 | hdr->filenames_count * sizeof (char *), | |
1582 | line_buf->error_callback, | |
1583 | line_buf->data)); | |
1584 | if (hdr->filenames == NULL) | |
1585 | return 0; | |
1586 | i = 0; | |
1587 | while (*hdr_buf.buf != '\0') | |
1588 | { | |
1589 | const char *filename; | |
1590 | uint64_t dir_index; | |
1591 | ||
1592 | if (hdr_buf.reported_underflow) | |
1593 | return 0; | |
1594 | ||
1595 | filename = (const char *) hdr_buf.buf; | |
1596 | if (!advance (&hdr_buf, | |
1597 | strnlen ((const char *) hdr_buf.buf, hdr_buf.left) + 1)) | |
1598 | return 0; | |
1599 | dir_index = read_uleb128 (&hdr_buf); | |
1600 | if (IS_ABSOLUTE_PATH (filename)) | |
1601 | hdr->filenames[i] = filename; | |
1602 | else | |
1603 | { | |
1604 | const char *dir; | |
1605 | size_t dir_len; | |
1606 | size_t filename_len; | |
1607 | char *s; | |
1608 | ||
1609 | if (dir_index == 0) | |
1610 | dir = u->comp_dir; | |
1611 | else if (dir_index - 1 < hdr->dirs_count) | |
1612 | dir = hdr->dirs[dir_index - 1]; | |
1613 | else | |
1614 | { | |
1615 | dwarf_buf_error (line_buf, | |
1616 | ("invalid directory index in " | |
1617 | "line number program header")); | |
1618 | return 0; | |
1619 | } | |
1620 | dir_len = strlen (dir); | |
1621 | filename_len = strlen (filename); | |
1622 | s = ((char *) | |
1623 | backtrace_alloc (state, dir_len + filename_len + 2, | |
1624 | line_buf->error_callback, line_buf->data)); | |
1625 | if (s == NULL) | |
1626 | return 0; | |
1627 | memcpy (s, dir, dir_len); | |
1628 | /* FIXME: If we are on a DOS-based file system, and the | |
1629 | directory or the file name use backslashes, then we | |
1630 | should use a backslash here. */ | |
1631 | s[dir_len] = '/'; | |
1632 | memcpy (s + dir_len + 1, filename, filename_len + 1); | |
1633 | hdr->filenames[i] = s; | |
1634 | } | |
1635 | ||
1636 | /* Ignore the modification time and size. */ | |
1637 | read_uleb128 (&hdr_buf); | |
1638 | read_uleb128 (&hdr_buf); | |
1639 | ||
1640 | ++i; | |
1641 | } | |
1642 | ||
1643 | if (hdr_buf.reported_underflow) | |
1644 | return 0; | |
1645 | ||
1646 | return 1; | |
1647 | } | |
1648 | ||
1649 | /* Read the line program, adding line mappings to VEC. Return 1 on | |
1650 | success, 0 on failure. */ | |
1651 | ||
1652 | static int | |
1653 | read_line_program (struct backtrace_state *state, struct unit *u, | |
1654 | const struct line_header *hdr, struct dwarf_buf *line_buf, | |
1655 | struct line_vector *vec) | |
1656 | { | |
1657 | uint64_t address; | |
1658 | unsigned int op_index; | |
1659 | const char *reset_filename; | |
1660 | const char *filename; | |
1661 | int lineno; | |
1662 | ||
1663 | address = 0; | |
1664 | op_index = 0; | |
1665 | if (hdr->filenames_count > 0) | |
1666 | reset_filename = hdr->filenames[0]; | |
1667 | else | |
1668 | reset_filename = ""; | |
1669 | filename = reset_filename; | |
1670 | lineno = 1; | |
1671 | while (line_buf->left > 0) | |
1672 | { | |
1673 | unsigned int op; | |
1674 | ||
1675 | op = read_byte (line_buf); | |
1676 | if (op >= hdr->opcode_base) | |
1677 | { | |
1678 | unsigned int advance; | |
1679 | ||
1680 | /* Special opcode. */ | |
1681 | op -= hdr->opcode_base; | |
1682 | advance = op / hdr->line_range; | |
1683 | address += (hdr->min_insn_len * (op_index + advance) | |
1684 | / hdr->max_ops_per_insn); | |
1685 | op_index = (op_index + advance) % hdr->max_ops_per_insn; | |
1686 | lineno += hdr->line_base + (int) (op % hdr->line_range); | |
1687 | add_line (state, address, filename, lineno, line_buf->error_callback, | |
1688 | line_buf->data, vec); | |
1689 | } | |
1690 | else if (op == DW_LNS_extended_op) | |
1691 | { | |
1692 | uint64_t len; | |
1693 | ||
1694 | len = read_uleb128 (line_buf); | |
1695 | op = read_byte (line_buf); | |
1696 | switch (op) | |
1697 | { | |
1698 | case DW_LNE_end_sequence: | |
1699 | /* FIXME: Should we mark the high PC here? It seems | |
1700 | that we already have that information from the | |
1701 | compilation unit. */ | |
1702 | address = 0; | |
1703 | op_index = 0; | |
1704 | filename = reset_filename; | |
1705 | lineno = 1; | |
1706 | break; | |
1707 | case DW_LNE_set_address: | |
1708 | address = read_address (line_buf, u->addrsize); | |
1709 | break; | |
1710 | case DW_LNE_define_file: | |
1711 | { | |
1712 | const char *f; | |
1713 | unsigned int dir_index; | |
1714 | ||
1715 | f = (const char *) line_buf->buf; | |
1716 | if (!advance (line_buf, strnlen (f, line_buf->left) + 1)) | |
1717 | return 0; | |
1718 | dir_index = read_uleb128 (line_buf); | |
1719 | /* Ignore that time and length. */ | |
1720 | read_uleb128 (line_buf); | |
1721 | read_uleb128 (line_buf); | |
1722 | if (IS_ABSOLUTE_PATH (f)) | |
1723 | filename = f; | |
1724 | else | |
1725 | { | |
1726 | const char *dir; | |
1727 | size_t dir_len; | |
1728 | size_t f_len; | |
1729 | char *p; | |
1730 | ||
1731 | if (dir_index == 0) | |
1732 | dir = u->comp_dir; | |
1733 | else if (dir_index - 1 < hdr->dirs_count) | |
1734 | dir = hdr->dirs[dir_index - 1]; | |
1735 | else | |
1736 | { | |
1737 | dwarf_buf_error (line_buf, | |
1738 | ("invalid directory index " | |
1739 | "in line number program")); | |
1740 | return 0; | |
1741 | } | |
1742 | dir_len = strlen (dir); | |
1743 | f_len = strlen (f); | |
1744 | p = ((char *) | |
1745 | backtrace_alloc (state, dir_len + f_len + 2, | |
1746 | line_buf->error_callback, | |
1747 | line_buf->data)); | |
1748 | if (p == NULL) | |
1749 | return 0; | |
1750 | memcpy (p, dir, dir_len); | |
1751 | /* FIXME: If we are on a DOS-based file system, | |
1752 | and the directory or the file name use | |
1753 | backslashes, then we should use a backslash | |
1754 | here. */ | |
1755 | p[dir_len] = '/'; | |
1756 | memcpy (p + dir_len + 1, f, f_len + 1); | |
1757 | filename = p; | |
1758 | } | |
1759 | } | |
1760 | break; | |
1761 | case DW_LNE_set_discriminator: | |
1762 | /* We don't care about discriminators. */ | |
1763 | read_uleb128 (line_buf); | |
1764 | break; | |
1765 | default: | |
1766 | if (!advance (line_buf, len - 1)) | |
1767 | return 0; | |
1768 | break; | |
1769 | } | |
1770 | } | |
1771 | else | |
1772 | { | |
1773 | switch (op) | |
1774 | { | |
1775 | case DW_LNS_copy: | |
1776 | add_line (state, address, filename, lineno, | |
1777 | line_buf->error_callback, line_buf->data, vec); | |
1778 | break; | |
1779 | case DW_LNS_advance_pc: | |
1780 | { | |
1781 | uint64_t advance; | |
1782 | ||
1783 | advance = read_uleb128 (line_buf); | |
1784 | address += (hdr->min_insn_len * (op_index + advance) | |
1785 | / hdr->max_ops_per_insn); | |
1786 | op_index = (op_index + advance) % hdr->max_ops_per_insn; | |
1787 | } | |
1788 | break; | |
1789 | case DW_LNS_advance_line: | |
1790 | lineno += (int) read_sleb128 (line_buf); | |
1791 | break; | |
1792 | case DW_LNS_set_file: | |
1793 | { | |
1794 | uint64_t fileno; | |
1795 | ||
1796 | fileno = read_uleb128 (line_buf); | |
1797 | if (fileno == 0) | |
1798 | filename = ""; | |
1799 | else | |
1800 | { | |
1801 | if (fileno - 1 >= hdr->filenames_count) | |
1802 | { | |
1803 | dwarf_buf_error (line_buf, | |
1804 | ("invalid file number in " | |
1805 | "line number program")); | |
1806 | return 0; | |
1807 | } | |
1808 | filename = hdr->filenames[fileno - 1]; | |
1809 | } | |
1810 | } | |
1811 | break; | |
1812 | case DW_LNS_set_column: | |
1813 | read_uleb128 (line_buf); | |
1814 | break; | |
1815 | case DW_LNS_negate_stmt: | |
1816 | break; | |
1817 | case DW_LNS_set_basic_block: | |
1818 | break; | |
1819 | case DW_LNS_const_add_pc: | |
1820 | { | |
1821 | unsigned int advance; | |
1822 | ||
1823 | op = 255 - hdr->opcode_base; | |
1824 | advance = op / hdr->line_range; | |
1825 | address += (hdr->min_insn_len * (op_index + advance) | |
1826 | / hdr->max_ops_per_insn); | |
1827 | op_index = (op_index + advance) % hdr->max_ops_per_insn; | |
1828 | } | |
1829 | break; | |
1830 | case DW_LNS_fixed_advance_pc: | |
1831 | address += read_uint16 (line_buf); | |
1832 | op_index = 0; | |
1833 | break; | |
1834 | case DW_LNS_set_prologue_end: | |
1835 | break; | |
1836 | case DW_LNS_set_epilogue_begin: | |
1837 | break; | |
1838 | case DW_LNS_set_isa: | |
1839 | read_uleb128 (line_buf); | |
1840 | break; | |
1841 | default: | |
1842 | { | |
1843 | unsigned int i; | |
1844 | ||
1845 | for (i = hdr->opcode_lengths[op - 1]; i > 0; --i) | |
1846 | read_uleb128 (line_buf); | |
1847 | } | |
1848 | break; | |
1849 | } | |
1850 | } | |
1851 | } | |
1852 | ||
1853 | return 1; | |
1854 | } | |
1855 | ||
1856 | /* Read the line number information for a compilation unit. Returns 1 | |
1857 | on success, 0 on failure. */ | |
1858 | ||
1859 | static int | |
1860 | read_line_info (struct backtrace_state *state, struct dwarf_data *ddata, | |
1861 | backtrace_error_callback error_callback, void *data, | |
1862 | struct unit *u, struct line_header *hdr, struct line **lines, | |
1863 | size_t *lines_count) | |
1864 | { | |
1865 | struct line_vector vec; | |
1866 | struct dwarf_buf line_buf; | |
1867 | uint64_t len; | |
1868 | int is_dwarf64; | |
1869 | struct line *ln; | |
1870 | ||
1871 | memset (&vec.vec, 0, sizeof vec.vec); | |
1872 | vec.count = 0; | |
1873 | ||
1874 | memset (hdr, 0, sizeof *hdr); | |
1875 | ||
1876 | if (u->lineoff != (off_t) (size_t) u->lineoff | |
1877 | || (size_t) u->lineoff >= ddata->dwarf_line_size) | |
1878 | { | |
1879 | error_callback (data, "unit line offset out of range", 0); | |
1880 | goto fail; | |
1881 | } | |
1882 | ||
1883 | line_buf.name = ".debug_line"; | |
1884 | line_buf.start = ddata->dwarf_line; | |
1885 | line_buf.buf = ddata->dwarf_line + u->lineoff; | |
1886 | line_buf.left = ddata->dwarf_line_size - u->lineoff; | |
1887 | line_buf.is_bigendian = ddata->is_bigendian; | |
1888 | line_buf.error_callback = error_callback; | |
1889 | line_buf.data = data; | |
1890 | line_buf.reported_underflow = 0; | |
1891 | ||
1892 | is_dwarf64 = 0; | |
1893 | len = read_uint32 (&line_buf); | |
1894 | if (len == 0xffffffff) | |
1895 | { | |
1896 | len = read_uint64 (&line_buf); | |
1897 | is_dwarf64 = 1; | |
1898 | } | |
1899 | line_buf.left = len; | |
1900 | ||
1901 | if (!read_line_header (state, u, is_dwarf64, &line_buf, hdr)) | |
1902 | goto fail; | |
1903 | ||
1904 | if (!read_line_program (state, u, hdr, &line_buf, &vec)) | |
1905 | goto fail; | |
1906 | ||
1907 | if (line_buf.reported_underflow) | |
1908 | goto fail; | |
1909 | ||
1910 | if (vec.count == 0) | |
1911 | { | |
1912 | /* This is not a failure in the sense of a generating an error, | |
1913 | but it is a failure in that sense that we have no useful | |
1914 | information. */ | |
1915 | goto fail; | |
1916 | } | |
1917 | ||
1918 | /* Allocate one extra entry at the end. */ | |
1919 | ln = ((struct line *) | |
1920 | backtrace_vector_grow (state, sizeof (struct line), error_callback, | |
1921 | data, &vec.vec)); | |
1922 | if (ln == NULL) | |
1923 | goto fail; | |
1924 | ln->pc = (uintptr_t) -1; | |
1925 | ln->filename = NULL; | |
1926 | ln->lineno = 0; | |
1927 | ||
1928 | if (!backtrace_vector_release (state, &vec.vec, error_callback, data)) | |
1929 | goto fail; | |
1930 | ||
1931 | ln = (struct line *) vec.vec.base; | |
1932 | qsort (ln, vec.count, sizeof (struct line), line_compare); | |
1933 | ||
1934 | *lines = ln; | |
1935 | *lines_count = vec.count; | |
1936 | ||
1937 | return 1; | |
1938 | ||
1939 | fail: | |
1940 | vec.vec.alc += vec.vec.size; | |
1941 | vec.vec.size = 0; | |
1942 | backtrace_vector_release (state, &vec.vec, error_callback, data); | |
1943 | free_line_header (state, hdr, error_callback, data); | |
1944 | *lines = (struct line *) (uintptr_t) -1; | |
1945 | *lines_count = 0; | |
1946 | return 0; | |
1947 | } | |
1948 | ||
1949 | /* Read the name of a function from a DIE referenced by a | |
1950 | DW_AT_abstract_origin or DW_AT_specification tag. OFFSET is within | |
1951 | the same compilation unit. */ | |
1952 | ||
1953 | static const char * | |
1954 | read_referenced_name (struct dwarf_data *ddata, struct unit *u, | |
1955 | uint64_t offset, backtrace_error_callback error_callback, | |
1956 | void *data) | |
1957 | { | |
1958 | struct dwarf_buf unit_buf; | |
1959 | uint64_t code; | |
1960 | const struct abbrev *abbrev; | |
1961 | const char *ret; | |
1962 | size_t i; | |
1963 | ||
1964 | /* OFFSET is from the start of the data for this compilation unit. | |
1965 | U->unit_data is the data, but it starts U->unit_data_offset bytes | |
1966 | from the beginning. */ | |
1967 | ||
1968 | if (offset < u->unit_data_offset | |
1969 | || offset - u->unit_data_offset >= u->unit_data_len) | |
1970 | { | |
1971 | error_callback (data, | |
1972 | "abstract origin or specification out of range", | |
1973 | 0); | |
1974 | return NULL; | |
1975 | } | |
1976 | ||
1977 | offset -= u->unit_data_offset; | |
1978 | ||
1979 | unit_buf.name = ".debug_info"; | |
1980 | unit_buf.start = ddata->dwarf_info; | |
1981 | unit_buf.buf = u->unit_data + offset; | |
1982 | unit_buf.left = u->unit_data_len - offset; | |
1983 | unit_buf.is_bigendian = ddata->is_bigendian; | |
1984 | unit_buf.error_callback = error_callback; | |
1985 | unit_buf.data = data; | |
1986 | unit_buf.reported_underflow = 0; | |
1987 | ||
1988 | code = read_uleb128 (&unit_buf); | |
1989 | if (code == 0) | |
1990 | { | |
1991 | dwarf_buf_error (&unit_buf, "invalid abstract origin or specification"); | |
1992 | return NULL; | |
1993 | } | |
1994 | ||
1995 | abbrev = lookup_abbrev (&u->abbrevs, code, error_callback, data); | |
1996 | if (abbrev == NULL) | |
1997 | return NULL; | |
1998 | ||
1999 | ret = NULL; | |
2000 | for (i = 0; i < abbrev->num_attrs; ++i) | |
2001 | { | |
2002 | struct attr_val val; | |
2003 | ||
2004 | if (!read_attribute (abbrev->attrs[i].form, &unit_buf, | |
2005 | u->is_dwarf64, u->version, u->addrsize, | |
2006 | ddata->dwarf_str, ddata->dwarf_str_size, | |
2007 | &val)) | |
2008 | return NULL; | |
2009 | ||
2010 | switch (abbrev->attrs[i].name) | |
2011 | { | |
2012 | case DW_AT_name: | |
2013 | /* We prefer the linkage name if get one. */ | |
2014 | if (val.encoding == ATTR_VAL_STRING) | |
2015 | ret = val.u.string; | |
2016 | break; | |
2017 | ||
2018 | case DW_AT_linkage_name: | |
2019 | case DW_AT_MIPS_linkage_name: | |
2020 | if (val.encoding == ATTR_VAL_STRING) | |
2021 | return val.u.string; | |
2022 | break; | |
2023 | ||
2024 | case DW_AT_specification: | |
2025 | if (abbrev->attrs[i].form == DW_FORM_ref_addr | |
2026 | || abbrev->attrs[i].form == DW_FORM_ref_sig8) | |
2027 | { | |
2028 | /* This refers to a specification defined in some other | |
2029 | compilation unit. We can handle this case if we | |
2030 | must, but it's harder. */ | |
2031 | break; | |
2032 | } | |
2033 | if (val.encoding == ATTR_VAL_UINT | |
2034 | || val.encoding == ATTR_VAL_REF_UNIT) | |
2035 | { | |
2036 | const char *name; | |
2037 | ||
2038 | name = read_referenced_name (ddata, u, val.u.uint, | |
2039 | error_callback, data); | |
2040 | if (name != NULL) | |
2041 | ret = name; | |
2042 | } | |
2043 | break; | |
2044 | ||
2045 | default: | |
2046 | break; | |
2047 | } | |
2048 | } | |
2049 | ||
2050 | return ret; | |
2051 | } | |
2052 | ||
2053 | /* Add a single range to U that maps to function. Returns 1 on | |
2054 | success, 0 on error. */ | |
2055 | ||
2056 | static int | |
2057 | add_function_range (struct backtrace_state *state, struct function *function, | |
2058 | uint64_t lowpc, uint64_t highpc, | |
2059 | backtrace_error_callback error_callback, | |
2060 | void *data, struct function_vector *vec) | |
2061 | { | |
2062 | struct function_addrs *p; | |
2063 | ||
2064 | if (vec->count > 0) | |
2065 | { | |
2066 | p = (struct function_addrs *) vec->vec.base + vec->count - 1; | |
2067 | if ((lowpc == p->high || lowpc == p->high + 1) | |
2068 | && function == p->function) | |
2069 | { | |
2070 | if (highpc > p->high) | |
2071 | p->high = highpc; | |
2072 | return 1; | |
2073 | } | |
2074 | } | |
2075 | ||
2076 | p = ((struct function_addrs *) | |
2077 | backtrace_vector_grow (state, sizeof (struct function_addrs), | |
2078 | error_callback, data, &vec->vec)); | |
2079 | if (p == NULL) | |
2080 | return 0; | |
2081 | ||
2082 | p->low = lowpc; | |
2083 | p->high = highpc; | |
2084 | p->function = function; | |
2085 | ++vec->count; | |
2086 | return 1; | |
2087 | } | |
2088 | ||
2089 | /* Add PC ranges to U that map to FUNCTION. Returns 1 on success, 0 | |
2090 | on error. */ | |
2091 | ||
2092 | static int | |
2093 | add_function_ranges (struct backtrace_state *state, struct dwarf_data *ddata, | |
2094 | struct unit *u, struct function *function, | |
2095 | uint64_t ranges, uint64_t base, | |
2096 | backtrace_error_callback error_callback, void *data, | |
2097 | struct function_vector *vec) | |
2098 | { | |
2099 | struct dwarf_buf ranges_buf; | |
2100 | ||
2101 | if (ranges >= ddata->dwarf_ranges_size) | |
2102 | { | |
2103 | error_callback (data, "function ranges offset out of range", 0); | |
2104 | return 0; | |
2105 | } | |
2106 | ||
2107 | ranges_buf.name = ".debug_ranges"; | |
2108 | ranges_buf.start = ddata->dwarf_ranges; | |
2109 | ranges_buf.buf = ddata->dwarf_ranges + ranges; | |
2110 | ranges_buf.left = ddata->dwarf_ranges_size - ranges; | |
2111 | ranges_buf.is_bigendian = ddata->is_bigendian; | |
2112 | ranges_buf.error_callback = error_callback; | |
2113 | ranges_buf.data = data; | |
2114 | ranges_buf.reported_underflow = 0; | |
2115 | ||
2116 | while (1) | |
2117 | { | |
2118 | uint64_t low; | |
2119 | uint64_t high; | |
2120 | ||
2121 | if (ranges_buf.reported_underflow) | |
2122 | return 0; | |
2123 | ||
2124 | low = read_address (&ranges_buf, u->addrsize); | |
2125 | high = read_address (&ranges_buf, u->addrsize); | |
2126 | ||
2127 | if (low == 0 && high == 0) | |
2128 | break; | |
2129 | ||
2130 | if (is_highest_address (low, u->addrsize)) | |
2131 | base = high; | |
2132 | else | |
2133 | { | |
2134 | if (!add_function_range (state, function, low + base, high + base, | |
2135 | error_callback, data, vec)) | |
2136 | return 0; | |
2137 | } | |
2138 | } | |
2139 | ||
2140 | if (ranges_buf.reported_underflow) | |
2141 | return 0; | |
2142 | ||
2143 | return 1; | |
2144 | } | |
2145 | ||
2146 | /* Read one entry plus all its children. Add function addresses to | |
2147 | VEC. Returns 1 on success, 0 on error. */ | |
2148 | ||
2149 | static int | |
2150 | read_function_entry (struct backtrace_state *state, struct dwarf_data *ddata, | |
2151 | struct unit *u, uint64_t base, struct dwarf_buf *unit_buf, | |
2152 | const struct line_header *lhdr, | |
2153 | backtrace_error_callback error_callback, void *data, | |
2154 | struct function_vector *vec) | |
2155 | { | |
2156 | while (unit_buf->left > 0) | |
2157 | { | |
2158 | uint64_t code; | |
2159 | const struct abbrev *abbrev; | |
2160 | int is_function; | |
2161 | struct function *function; | |
2162 | size_t i; | |
2163 | uint64_t lowpc; | |
2164 | int have_lowpc; | |
2165 | uint64_t highpc; | |
2166 | int have_highpc; | |
2167 | int highpc_is_relative; | |
2168 | uint64_t ranges; | |
2169 | int have_ranges; | |
2170 | ||
2171 | code = read_uleb128 (unit_buf); | |
2172 | if (code == 0) | |
2173 | return 1; | |
2174 | ||
2175 | abbrev = lookup_abbrev (&u->abbrevs, code, error_callback, data); | |
2176 | if (abbrev == NULL) | |
2177 | return 0; | |
2178 | ||
2179 | is_function = (abbrev->tag == DW_TAG_subprogram | |
2180 | || abbrev->tag == DW_TAG_entry_point | |
2181 | || abbrev->tag == DW_TAG_inlined_subroutine); | |
2182 | ||
2183 | function = NULL; | |
2184 | if (is_function) | |
2185 | { | |
2186 | function = ((struct function *) | |
2187 | backtrace_alloc (state, sizeof *function, | |
2188 | error_callback, data)); | |
2189 | if (function == NULL) | |
2190 | return 0; | |
2191 | memset (function, 0, sizeof *function); | |
2192 | } | |
2193 | ||
2194 | lowpc = 0; | |
2195 | have_lowpc = 0; | |
2196 | highpc = 0; | |
2197 | have_highpc = 0; | |
2198 | highpc_is_relative = 0; | |
2199 | ranges = 0; | |
2200 | have_ranges = 0; | |
2201 | for (i = 0; i < abbrev->num_attrs; ++i) | |
2202 | { | |
2203 | struct attr_val val; | |
2204 | ||
2205 | if (!read_attribute (abbrev->attrs[i].form, unit_buf, | |
2206 | u->is_dwarf64, u->version, u->addrsize, | |
2207 | ddata->dwarf_str, ddata->dwarf_str_size, | |
2208 | &val)) | |
2209 | return 0; | |
2210 | ||
2211 | /* The compile unit sets the base address for any address | |
2212 | ranges in the function entries. */ | |
2213 | if (abbrev->tag == DW_TAG_compile_unit | |
2214 | && abbrev->attrs[i].name == DW_AT_low_pc | |
2215 | && val.encoding == ATTR_VAL_ADDRESS) | |
2216 | base = val.u.uint; | |
2217 | ||
2218 | if (is_function) | |
2219 | { | |
2220 | switch (abbrev->attrs[i].name) | |
2221 | { | |
2222 | case DW_AT_call_file: | |
2223 | if (val.encoding == ATTR_VAL_UINT) | |
2224 | { | |
2225 | if (val.u.uint == 0) | |
2226 | function->caller_filename = ""; | |
2227 | else | |
2228 | { | |
2229 | if (val.u.uint - 1 >= lhdr->filenames_count) | |
2230 | { | |
2231 | dwarf_buf_error (unit_buf, | |
2232 | ("invalid file number in " | |
2233 | "DW_AT_call_file attribute")); | |
2234 | return 0; | |
2235 | } | |
2236 | function->caller_filename = | |
2237 | lhdr->filenames[val.u.uint - 1]; | |
2238 | } | |
2239 | } | |
2240 | break; | |
2241 | ||
2242 | case DW_AT_call_line: | |
2243 | if (val.encoding == ATTR_VAL_UINT) | |
2244 | function->caller_lineno = val.u.uint; | |
2245 | break; | |
2246 | ||
2247 | case DW_AT_abstract_origin: | |
2248 | case DW_AT_specification: | |
2249 | if (abbrev->attrs[i].form == DW_FORM_ref_addr | |
2250 | || abbrev->attrs[i].form == DW_FORM_ref_sig8) | |
2251 | { | |
2252 | /* This refers to an abstract origin defined in | |
2253 | some other compilation unit. We can handle | |
2254 | this case if we must, but it's harder. */ | |
2255 | break; | |
2256 | } | |
2257 | if (val.encoding == ATTR_VAL_UINT | |
2258 | || val.encoding == ATTR_VAL_REF_UNIT) | |
2259 | { | |
2260 | const char *name; | |
2261 | ||
2262 | name = read_referenced_name (ddata, u, val.u.uint, | |
2263 | error_callback, data); | |
2264 | if (name != NULL) | |
2265 | function->name = name; | |
2266 | } | |
2267 | break; | |
2268 | ||
2269 | case DW_AT_name: | |
2270 | if (val.encoding == ATTR_VAL_STRING) | |
2271 | { | |
2272 | /* Don't override a name we found in some other | |
2273 | way, as it will normally be more | |
2274 | useful--e.g., this name is normally not | |
2275 | mangled. */ | |
2276 | if (function->name == NULL) | |
2277 | function->name = val.u.string; | |
2278 | } | |
2279 | break; | |
2280 | ||
2281 | case DW_AT_linkage_name: | |
2282 | case DW_AT_MIPS_linkage_name: | |
2283 | if (val.encoding == ATTR_VAL_STRING) | |
2284 | function->name = val.u.string; | |
2285 | break; | |
2286 | ||
2287 | case DW_AT_low_pc: | |
2288 | if (val.encoding == ATTR_VAL_ADDRESS) | |
2289 | { | |
2290 | lowpc = val.u.uint; | |
2291 | have_lowpc = 1; | |
2292 | } | |
2293 | break; | |
2294 | ||
2295 | case DW_AT_high_pc: | |
2296 | if (val.encoding == ATTR_VAL_ADDRESS) | |
2297 | { | |
2298 | highpc = val.u.uint; | |
2299 | have_highpc = 1; | |
2300 | } | |
2301 | else if (val.encoding == ATTR_VAL_UINT) | |
2302 | { | |
2303 | highpc = val.u.uint; | |
2304 | have_highpc = 1; | |
2305 | highpc_is_relative = 1; | |
2306 | } | |
2307 | break; | |
2308 | ||
2309 | case DW_AT_ranges: | |
2310 | if (val.encoding == ATTR_VAL_UINT | |
2311 | || val.encoding == ATTR_VAL_REF_SECTION) | |
2312 | { | |
2313 | ranges = val.u.uint; | |
2314 | have_ranges = 1; | |
2315 | } | |
2316 | break; | |
2317 | ||
2318 | default: | |
2319 | break; | |
2320 | } | |
2321 | } | |
2322 | } | |
2323 | ||
2324 | /* If we couldn't find a name for the function, we have no use | |
2325 | for it. */ | |
2326 | if (is_function && function->name == NULL) | |
2327 | { | |
2328 | backtrace_free (state, function, sizeof *function, | |
2329 | error_callback, data); | |
2330 | is_function = 0; | |
2331 | } | |
2332 | ||
2333 | if (is_function) | |
2334 | { | |
2335 | if (have_ranges) | |
2336 | { | |
2337 | if (!add_function_ranges (state, ddata, u, function, ranges, | |
2338 | base, error_callback, data, vec)) | |
2339 | return 0; | |
2340 | } | |
2341 | else if (have_lowpc && have_highpc) | |
2342 | { | |
2343 | if (highpc_is_relative) | |
2344 | highpc += lowpc; | |
2345 | if (!add_function_range (state, function, lowpc, highpc, | |
2346 | error_callback, data, vec)) | |
2347 | return 0; | |
2348 | } | |
2349 | else | |
2350 | { | |
2351 | backtrace_free (state, function, sizeof *function, | |
2352 | error_callback, data); | |
2353 | is_function = 0; | |
2354 | } | |
2355 | } | |
2356 | ||
2357 | if (abbrev->has_children) | |
2358 | { | |
2359 | if (!is_function) | |
2360 | { | |
2361 | if (!read_function_entry (state, ddata, u, base, unit_buf, lhdr, | |
2362 | error_callback, data, vec)) | |
2363 | return 0; | |
2364 | } | |
2365 | else | |
2366 | { | |
2367 | struct function_vector fvec; | |
2368 | ||
2369 | /* Gather any information for inlined functions in | |
2370 | FVEC. */ | |
2371 | ||
2372 | memset (&fvec, 0, sizeof fvec); | |
2373 | ||
2374 | if (!read_function_entry (state, ddata, u, base, unit_buf, lhdr, | |
2375 | error_callback, data, &fvec)) | |
2376 | return 0; | |
2377 | ||
2378 | if (fvec.count > 0) | |
2379 | { | |
2380 | struct function_addrs *faddrs; | |
2381 | ||
2382 | if (!backtrace_vector_release (state, &fvec.vec, | |
2383 | error_callback, data)) | |
2384 | return 0; | |
2385 | ||
2386 | faddrs = (struct function_addrs *) fvec.vec.base; | |
2387 | qsort (faddrs, fvec.count, | |
2388 | sizeof (struct function_addrs), | |
2389 | function_addrs_compare); | |
2390 | ||
2391 | function->function_addrs = faddrs; | |
2392 | function->function_addrs_count = fvec.count; | |
2393 | } | |
2394 | } | |
2395 | } | |
2396 | } | |
2397 | ||
2398 | return 1; | |
2399 | } | |
2400 | ||
2401 | /* Read function name information for a compilation unit. We look | |
2402 | through the whole unit looking for function tags. */ | |
2403 | ||
2404 | static void | |
2405 | read_function_info (struct backtrace_state *state, struct dwarf_data *ddata, | |
2406 | const struct line_header *lhdr, | |
2407 | backtrace_error_callback error_callback, void *data, | |
2408 | struct unit *u, struct function_vector *fvec, | |
2409 | struct function_addrs **ret_addrs, | |
2410 | size_t *ret_addrs_count) | |
2411 | { | |
2412 | struct dwarf_buf unit_buf; | |
2413 | struct function_addrs *addrs; | |
2414 | size_t addrs_count; | |
2415 | ||
2416 | unit_buf.name = ".debug_info"; | |
2417 | unit_buf.start = ddata->dwarf_info; | |
2418 | unit_buf.buf = u->unit_data; | |
2419 | unit_buf.left = u->unit_data_len; | |
2420 | unit_buf.is_bigendian = ddata->is_bigendian; | |
2421 | unit_buf.error_callback = error_callback; | |
2422 | unit_buf.data = data; | |
2423 | unit_buf.reported_underflow = 0; | |
2424 | ||
2425 | while (unit_buf.left > 0) | |
2426 | { | |
2427 | if (!read_function_entry (state, ddata, u, 0, &unit_buf, lhdr, | |
2428 | error_callback, data, fvec)) | |
2429 | return; | |
2430 | } | |
2431 | ||
2432 | if (fvec->count == 0) | |
2433 | return; | |
2434 | ||
2435 | addrs = (struct function_addrs *) fvec->vec.base; | |
2436 | addrs_count = fvec->count; | |
2437 | ||
2438 | /* Finish this list of addresses, but leave the remaining space in | |
2439 | the vector available for the next function unit. */ | |
2440 | backtrace_vector_finish (state, &fvec->vec); | |
2441 | fvec->count = 0; | |
2442 | ||
2443 | qsort (addrs, addrs_count, sizeof (struct function_addrs), | |
2444 | function_addrs_compare); | |
2445 | ||
2446 | *ret_addrs = addrs; | |
2447 | *ret_addrs_count = addrs_count; | |
2448 | } | |
2449 | ||
2450 | /* See if PC is inlined in FUNCTION. If it is, print out the inlined | |
2451 | information, and update FILENAME and LINENO for the caller. | |
2452 | Returns whatever CALLBACK returns, or 0 to keep going. */ | |
2453 | ||
2454 | static int | |
2455 | report_inlined_functions (uintptr_t pc, struct function *function, | |
2456 | backtrace_full_callback callback, void *data, | |
2457 | const char **filename, int *lineno) | |
2458 | { | |
2459 | struct function_addrs *function_addrs; | |
2460 | struct function *inlined; | |
2461 | int ret; | |
2462 | ||
2463 | if (function->function_addrs_count == 0) | |
2464 | return 0; | |
2465 | ||
2466 | function_addrs = ((struct function_addrs *) | |
2467 | bsearch (&pc, function->function_addrs, | |
2468 | function->function_addrs_count, | |
2469 | sizeof (struct function_addrs), | |
2470 | function_addrs_search)); | |
2471 | if (function_addrs == NULL) | |
2472 | return 0; | |
2473 | ||
2474 | while (((size_t) (function_addrs - function->function_addrs) + 1 | |
2475 | < function->function_addrs_count) | |
2476 | && pc >= (function_addrs + 1)->low | |
2477 | && pc < (function_addrs + 1)->high) | |
2478 | ++function_addrs; | |
2479 | ||
2480 | /* We found an inlined call. */ | |
2481 | ||
2482 | inlined = function_addrs->function; | |
2483 | ||
2484 | /* Report any calls inlined into this one. */ | |
2485 | ret = report_inlined_functions (pc, inlined, callback, data, | |
2486 | filename, lineno); | |
2487 | if (ret != 0) | |
2488 | return ret; | |
2489 | ||
2490 | /* Report this inlined call. */ | |
2491 | ret = callback (data, pc, *filename, *lineno, inlined->name); | |
2492 | if (ret != 0) | |
2493 | return ret; | |
2494 | ||
2495 | /* Our caller will report the caller of the inlined function; tell | |
2496 | it the appropriate filename and line number. */ | |
2497 | *filename = inlined->caller_filename; | |
2498 | *lineno = inlined->caller_lineno; | |
2499 | ||
2500 | return 0; | |
2501 | } | |
2502 | ||
2503 | /* Return the file/line information for a PC using the DWARF mapping | |
2504 | we built earlier. */ | |
2505 | ||
2506 | static int | |
2507 | dwarf_fileline (struct backtrace_state *state, uintptr_t pc, | |
2508 | backtrace_full_callback callback, | |
2509 | backtrace_error_callback error_callback, void *data) | |
2510 | { | |
2511 | struct dwarf_data *ddata; | |
2512 | struct unit_addrs *entry; | |
2513 | struct unit *u; | |
2514 | int new_data; | |
2515 | struct line *lines; | |
2516 | struct line *ln; | |
2517 | struct function_addrs *function_addrs; | |
2518 | struct function *function; | |
2519 | const char *filename; | |
2520 | int lineno; | |
2521 | int ret; | |
2522 | ||
2523 | ddata = (struct dwarf_data *) state->fileline_data; | |
2524 | ||
2525 | /* Find an address range that includes PC. */ | |
2526 | entry = bsearch (&pc, ddata->addrs, ddata->addrs_count, | |
2527 | sizeof (struct unit_addrs), unit_addrs_search); | |
2528 | ||
2529 | if (entry == NULL) | |
2530 | return callback (data, pc, NULL, 0, NULL); | |
2531 | ||
2532 | /* If there are multiple ranges that contain PC, use the last one, | |
2533 | in order to produce predictable results. If we assume that all | |
2534 | ranges are properly nested, then the last range will be the | |
2535 | smallest one. */ | |
2536 | while ((size_t) (entry - ddata->addrs) + 1 < ddata->addrs_count | |
2537 | && pc >= (entry + 1)->low | |
2538 | && pc < (entry + 1)->high) | |
2539 | ++entry; | |
2540 | ||
2541 | /* We need the lines, lines_count, function_addrs, | |
2542 | function_addrs_count fields of u. If they are not set, we need | |
2543 | to set them. When running in threaded mode, we need to allow for | |
2544 | the possibility that some other thread is setting them | |
2545 | simultaneously. */ | |
2546 | ||
2547 | u = entry->u; | |
2548 | lines = u->lines; | |
2549 | ||
2550 | /* Skip units with no useful line number information by walking | |
2551 | backward. Useless line number information is marked by setting | |
2552 | lines == -1. */ | |
2553 | while (entry > ddata->addrs | |
2554 | && pc >= (entry - 1)->low | |
2555 | && pc < (entry - 1)->high) | |
2556 | { | |
2557 | if (state->threaded) | |
2558 | { | |
2559 | /* Use __sync_bool_compare_and_swap to do a | |
2560 | load-acquire. */ | |
2561 | while (!__sync_bool_compare_and_swap (&u->lines, lines, lines)) | |
2562 | lines = u->lines; | |
2563 | } | |
2564 | ||
2565 | if (lines != (struct line *) (uintptr_t) -1) | |
2566 | break; | |
2567 | ||
2568 | --entry; | |
2569 | ||
2570 | u = entry->u; | |
2571 | lines = u->lines; | |
2572 | } | |
2573 | ||
2574 | /* Do a load-acquire of u->lines. */ | |
2575 | if (state->threaded) | |
2576 | { | |
2577 | /* Use __sync_bool_compare_and_swap to do an atomic load. */ | |
2578 | while (!__sync_bool_compare_and_swap (&u->lines, lines, lines)) | |
2579 | lines = u->lines; | |
2580 | } | |
2581 | ||
2582 | new_data = 0; | |
2583 | if (lines == NULL) | |
2584 | { | |
2585 | size_t function_addrs_count; | |
2586 | struct line_header lhdr; | |
2587 | size_t count; | |
2588 | ||
2589 | /* We have never read the line information for this unit. Read | |
2590 | it now. */ | |
2591 | ||
2592 | function_addrs = NULL; | |
2593 | function_addrs_count = 0; | |
2594 | if (read_line_info (state, ddata, error_callback, data, entry->u, &lhdr, | |
2595 | &lines, &count)) | |
2596 | { | |
2597 | read_function_info (state, ddata, &lhdr, error_callback, data, | |
2598 | entry->u, &ddata->fvec, &function_addrs, | |
2599 | &function_addrs_count); | |
2600 | free_line_header (state, &lhdr, error_callback, data); | |
2601 | new_data = 1; | |
2602 | } | |
2603 | ||
2604 | /* Atomically store the information we just read into the unit. | |
2605 | If another thread is simultaneously writing, it presumably | |
2606 | read the same information, and we don't care which one we | |
2607 | wind up with; we just leak the other one. We do have to | |
2608 | write the lines field last, so that the acquire-loads above | |
2609 | ensure that the other fields are set. */ | |
2610 | ||
2611 | if (!state->threaded) | |
2612 | { | |
2613 | u->lines_count = count; | |
2614 | u->function_addrs = function_addrs; | |
2615 | u->function_addrs_count = function_addrs_count; | |
2616 | u->lines = lines; | |
2617 | } | |
2618 | else | |
2619 | { | |
2620 | __sync_bool_compare_and_swap (&u->lines_count, 0, count); | |
2621 | __sync_bool_compare_and_swap (&u->function_addrs, NULL, | |
2622 | function_addrs); | |
2623 | __sync_bool_compare_and_swap (&u->function_addrs_count, 0, | |
2624 | function_addrs_count); | |
2625 | __sync_bool_compare_and_swap (&u->lines, NULL, lines); | |
2626 | } | |
2627 | } | |
2628 | ||
2629 | /* Now all fields of U have been initialized. */ | |
2630 | ||
2631 | if (lines == (struct line *) (uintptr_t) -1) | |
2632 | { | |
2633 | /* If reading the line number information failed in some way, | |
2634 | try again to see if there is a better compilation unit for | |
2635 | this PC. */ | |
2636 | if (new_data) | |
2637 | dwarf_fileline (state, pc, callback, error_callback, data); | |
2638 | return callback (data, pc, NULL, 0, NULL); | |
2639 | } | |
2640 | ||
2641 | /* Search for PC within this unit. */ | |
2642 | ||
2643 | ln = (struct line *) bsearch (&pc, lines, entry->u->lines_count, | |
2644 | sizeof (struct line), line_search); | |
2645 | if (ln == NULL) | |
2646 | { | |
2647 | error_callback (data, "inconsistent DWARF line number info", 0); | |
2648 | return 0; | |
2649 | } | |
2650 | ||
2651 | /* Search for function name within this unit. */ | |
2652 | ||
2653 | if (entry->u->function_addrs_count == 0) | |
2654 | return callback (data, pc, ln->filename, ln->lineno, NULL); | |
2655 | ||
2656 | function_addrs = ((struct function_addrs *) | |
2657 | bsearch (&pc, entry->u->function_addrs, | |
2658 | entry->u->function_addrs_count, | |
2659 | sizeof (struct function_addrs), | |
2660 | function_addrs_search)); | |
2661 | if (function_addrs == NULL) | |
2662 | return callback (data, pc, ln->filename, ln->lineno, NULL); | |
2663 | ||
2664 | /* If there are multiple function ranges that contain PC, use the | |
2665 | last one, in order to produce predictable results. */ | |
2666 | ||
2667 | while (((size_t) (function_addrs - entry->u->function_addrs + 1) | |
2668 | < entry->u->function_addrs_count) | |
2669 | && pc >= (function_addrs + 1)->low | |
2670 | && pc < (function_addrs + 1)->high) | |
2671 | ++function_addrs; | |
2672 | ||
2673 | function = function_addrs->function; | |
2674 | ||
2675 | filename = ln->filename; | |
2676 | lineno = ln->lineno; | |
2677 | ||
2678 | ret = report_inlined_functions (pc, function, callback, data, | |
2679 | &filename, &lineno); | |
2680 | if (ret != 0) | |
2681 | return ret; | |
2682 | ||
2683 | return callback (data, pc, filename, lineno, function->name); | |
2684 | } | |
2685 | ||
2686 | /* Build our data structures from the .debug_info and .debug_line | |
2687 | sections. Set *FILELINE_FN and *FILELINE_DATA. Return 1 on | |
2688 | success, 0 on failure. */ | |
2689 | ||
2690 | int | |
2691 | backtrace_dwarf_initialize (struct backtrace_state *state, | |
2692 | const unsigned char *dwarf_info, | |
2693 | size_t dwarf_info_size, | |
2694 | const unsigned char *dwarf_line, | |
2695 | size_t dwarf_line_size, | |
2696 | const unsigned char *dwarf_abbrev, | |
2697 | size_t dwarf_abbrev_size, | |
2698 | const unsigned char *dwarf_ranges, | |
2699 | size_t dwarf_ranges_size, | |
2700 | const unsigned char *dwarf_str, | |
2701 | size_t dwarf_str_size, | |
2702 | int is_bigendian, | |
2703 | backtrace_error_callback error_callback, | |
2704 | void *data, fileline *fileline_fn) | |
2705 | { | |
2706 | struct unit_addrs_vector addrs_vec; | |
2707 | struct unit_addrs *addrs; | |
2708 | size_t addrs_count; | |
2709 | struct dwarf_data *fdata; | |
2710 | ||
2711 | if (!build_address_map (state, dwarf_info, dwarf_info_size, dwarf_abbrev, | |
2712 | dwarf_abbrev_size, dwarf_ranges, dwarf_ranges_size, | |
2713 | dwarf_str, dwarf_str_size, is_bigendian, | |
2714 | error_callback, data, &addrs_vec)) | |
2715 | return 0; | |
2716 | ||
2717 | if (!backtrace_vector_release (state, &addrs_vec.vec, error_callback, data)) | |
2718 | return 0; | |
2719 | addrs = (struct unit_addrs *) addrs_vec.vec.base; | |
2720 | addrs_count = addrs_vec.count; | |
2721 | qsort (addrs, addrs_count, sizeof (struct unit_addrs), unit_addrs_compare); | |
2722 | ||
2723 | fdata = ((struct dwarf_data *) | |
2724 | backtrace_alloc (state, sizeof (struct dwarf_data), | |
2725 | error_callback, data)); | |
2726 | if (fdata == NULL) | |
2727 | return 0; | |
2728 | ||
2729 | fdata->addrs = addrs; | |
2730 | fdata->addrs_count = addrs_count; | |
2731 | fdata->dwarf_info = dwarf_info; | |
2732 | fdata->dwarf_info_size = dwarf_info_size; | |
2733 | fdata->dwarf_line = dwarf_line; | |
2734 | fdata->dwarf_line_size = dwarf_line_size; | |
2735 | fdata->dwarf_ranges = dwarf_ranges; | |
2736 | fdata->dwarf_ranges_size = dwarf_ranges_size; | |
2737 | fdata->dwarf_str = dwarf_str; | |
2738 | fdata->dwarf_str_size = dwarf_str_size; | |
2739 | fdata->is_bigendian = is_bigendian; | |
2740 | memset (&fdata->fvec, 0, sizeof fdata->fvec); | |
2741 | ||
2742 | state->fileline_data = fdata; | |
2743 | ||
2744 | *fileline_fn = dwarf_fileline; | |
2745 | ||
2746 | return 1; | |
2747 | } |