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