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