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