1 /* elf.c -- Get debug data from an ELF file for backtraces. 2 Copyright (C) 2012-2017 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 <stdlib.h> 36 #include <string.h> 37 #include <sys/types.h> 38 39 #ifdef HAVE_DL_ITERATE_PHDR 40 #include <link.h> 41 #endif 42 43 #include "backtrace.h" 44 #include "internal.h" 45 46 #ifndef HAVE_DL_ITERATE_PHDR 47 48 /* Dummy version of dl_iterate_phdr for systems that don't have it. */ 49 50 #define dl_phdr_info x_dl_phdr_info 51 #define dl_iterate_phdr x_dl_iterate_phdr 52 53 struct dl_phdr_info 54 { 55 uintptr_t dlpi_addr; 56 const char *dlpi_name; 57 }; 58 59 static int 60 dl_iterate_phdr (int (*callback) (struct dl_phdr_info *, 61 size_t, void *) ATTRIBUTE_UNUSED, 62 void *data ATTRIBUTE_UNUSED) 63 { 64 return 0; 65 } 66 67 #endif /* ! defined (HAVE_DL_ITERATE_PHDR) */ 68 69 /* The configure script must tell us whether we are 32-bit or 64-bit 70 ELF. We could make this code test and support either possibility, 71 but there is no point. This code only works for the currently 72 running executable, which means that we know the ELF mode at 73 configure mode. */ 74 75 #if BACKTRACE_ELF_SIZE != 32 && BACKTRACE_ELF_SIZE != 64 76 #error "Unknown BACKTRACE_ELF_SIZE" 77 #endif 78 79 /* <link.h> might #include <elf.h> which might define our constants 80 with slightly different values. Undefine them to be safe. */ 81 82 #undef EI_NIDENT 83 #undef EI_MAG0 84 #undef EI_MAG1 85 #undef EI_MAG2 86 #undef EI_MAG3 87 #undef EI_CLASS 88 #undef EI_DATA 89 #undef EI_VERSION 90 #undef ELF_MAG0 91 #undef ELF_MAG1 92 #undef ELF_MAG2 93 #undef ELF_MAG3 94 #undef ELFCLASS32 95 #undef ELFCLASS64 96 #undef ELFDATA2LSB 97 #undef ELFDATA2MSB 98 #undef EV_CURRENT 99 #undef ET_DYN 100 #undef SHN_LORESERVE 101 #undef SHN_XINDEX 102 #undef SHN_UNDEF 103 #undef SHT_SYMTAB 104 #undef SHT_STRTAB 105 #undef SHT_DYNSYM 106 #undef SHF_COMPRESSED 107 #undef STT_OBJECT 108 #undef STT_FUNC 109 110 /* Basic types. */ 111 112 typedef uint16_t b_elf_half; /* Elf_Half. */ 113 typedef uint32_t b_elf_word; /* Elf_Word. */ 114 typedef int32_t b_elf_sword; /* Elf_Sword. */ 115 116 #if BACKTRACE_ELF_SIZE == 32 117 118 typedef uint32_t b_elf_addr; /* Elf_Addr. */ 119 typedef uint32_t b_elf_off; /* Elf_Off. */ 120 121 typedef uint32_t b_elf_wxword; /* 32-bit Elf_Word, 64-bit ELF_Xword. */ 122 123 #else 124 125 typedef uint64_t b_elf_addr; /* Elf_Addr. */ 126 typedef uint64_t b_elf_off; /* Elf_Off. */ 127 typedef uint64_t b_elf_xword; /* Elf_Xword. */ 128 typedef int64_t b_elf_sxword; /* Elf_Sxword. */ 129 130 typedef uint64_t b_elf_wxword; /* 32-bit Elf_Word, 64-bit ELF_Xword. */ 131 132 #endif 133 134 /* Data structures and associated constants. */ 135 136 #define EI_NIDENT 16 137 138 typedef struct { 139 unsigned char e_ident[EI_NIDENT]; /* ELF "magic number" */ 140 b_elf_half e_type; /* Identifies object file type */ 141 b_elf_half e_machine; /* Specifies required architecture */ 142 b_elf_word e_version; /* Identifies object file version */ 143 b_elf_addr e_entry; /* Entry point virtual address */ 144 b_elf_off e_phoff; /* Program header table file offset */ 145 b_elf_off e_shoff; /* Section header table file offset */ 146 b_elf_word e_flags; /* Processor-specific flags */ 147 b_elf_half e_ehsize; /* ELF header size in bytes */ 148 b_elf_half e_phentsize; /* Program header table entry size */ 149 b_elf_half e_phnum; /* Program header table entry count */ 150 b_elf_half e_shentsize; /* Section header table entry size */ 151 b_elf_half e_shnum; /* Section header table entry count */ 152 b_elf_half e_shstrndx; /* Section header string table index */ 153 } b_elf_ehdr; /* Elf_Ehdr. */ 154 155 #define EI_MAG0 0 156 #define EI_MAG1 1 157 #define EI_MAG2 2 158 #define EI_MAG3 3 159 #define EI_CLASS 4 160 #define EI_DATA 5 161 #define EI_VERSION 6 162 163 #define ELFMAG0 0x7f 164 #define ELFMAG1 'E' 165 #define ELFMAG2 'L' 166 #define ELFMAG3 'F' 167 168 #define ELFCLASS32 1 169 #define ELFCLASS64 2 170 171 #define ELFDATA2LSB 1 172 #define ELFDATA2MSB 2 173 174 #define EV_CURRENT 1 175 176 #define ET_DYN 3 177 178 typedef struct { 179 b_elf_word sh_name; /* Section name, index in string tbl */ 180 b_elf_word sh_type; /* Type of section */ 181 b_elf_wxword sh_flags; /* Miscellaneous section attributes */ 182 b_elf_addr sh_addr; /* Section virtual addr at execution */ 183 b_elf_off sh_offset; /* Section file offset */ 184 b_elf_wxword sh_size; /* Size of section in bytes */ 185 b_elf_word sh_link; /* Index of another section */ 186 b_elf_word sh_info; /* Additional section information */ 187 b_elf_wxword sh_addralign; /* Section alignment */ 188 b_elf_wxword sh_entsize; /* Entry size if section holds table */ 189 } b_elf_shdr; /* Elf_Shdr. */ 190 191 #define SHN_UNDEF 0x0000 /* Undefined section */ 192 #define SHN_LORESERVE 0xFF00 /* Begin range of reserved indices */ 193 #define SHN_XINDEX 0xFFFF /* Section index is held elsewhere */ 194 195 #define SHT_SYMTAB 2 196 #define SHT_STRTAB 3 197 #define SHT_DYNSYM 11 198 199 #define SHF_COMPRESSED 0x800 200 201 #if BACKTRACE_ELF_SIZE == 32 202 203 typedef struct 204 { 205 b_elf_word st_name; /* Symbol name, index in string tbl */ 206 b_elf_addr st_value; /* Symbol value */ 207 b_elf_word st_size; /* Symbol size */ 208 unsigned char st_info; /* Symbol binding and type */ 209 unsigned char st_other; /* Visibility and other data */ 210 b_elf_half st_shndx; /* Symbol section index */ 211 } b_elf_sym; /* Elf_Sym. */ 212 213 #else /* BACKTRACE_ELF_SIZE != 32 */ 214 215 typedef struct 216 { 217 b_elf_word st_name; /* Symbol name, index in string tbl */ 218 unsigned char st_info; /* Symbol binding and type */ 219 unsigned char st_other; /* Visibility and other data */ 220 b_elf_half st_shndx; /* Symbol section index */ 221 b_elf_addr st_value; /* Symbol value */ 222 b_elf_xword st_size; /* Symbol size */ 223 } b_elf_sym; /* Elf_Sym. */ 224 225 #endif /* BACKTRACE_ELF_SIZE != 32 */ 226 227 #define STT_OBJECT 1 228 #define STT_FUNC 2 229 230 /* An index of ELF sections we care about. */ 231 232 enum debug_section 233 { 234 DEBUG_INFO, 235 DEBUG_LINE, 236 DEBUG_ABBREV, 237 DEBUG_RANGES, 238 DEBUG_STR, 239 DEBUG_MAX 240 }; 241 242 /* Names of sections, indexed by enum elf_section. */ 243 244 static const char * const debug_section_names[DEBUG_MAX] = 245 { 246 ".debug_info", 247 ".debug_line", 248 ".debug_abbrev", 249 ".debug_ranges", 250 ".debug_str" 251 }; 252 253 /* Information we gather for the sections we care about. */ 254 255 struct debug_section_info 256 { 257 /* Section file offset. */ 258 off_t offset; 259 /* Section size. */ 260 size_t size; 261 /* Section contents, after read from file. */ 262 const unsigned char *data; 263 }; 264 265 /* Information we keep for an ELF symbol. */ 266 267 struct elf_symbol 268 { 269 /* The name of the symbol. */ 270 const char *name; 271 /* The address of the symbol. */ 272 uintptr_t address; 273 /* The size of the symbol. */ 274 size_t size; 275 }; 276 277 /* Information to pass to elf_syminfo. */ 278 279 struct elf_syminfo_data 280 { 281 /* Symbols for the next module. */ 282 struct elf_syminfo_data *next; 283 /* The ELF symbols, sorted by address. */ 284 struct elf_symbol *symbols; 285 /* The number of symbols. */ 286 size_t count; 287 }; 288 289 /* A dummy callback function used when we can't find any debug info. */ 290 291 static int 292 elf_nodebug (struct backtrace_state *state ATTRIBUTE_UNUSED, 293 uintptr_t pc ATTRIBUTE_UNUSED, 294 backtrace_full_callback callback ATTRIBUTE_UNUSED, 295 backtrace_error_callback error_callback, void *data) 296 { 297 error_callback (data, "no debug info in ELF executable", -1); 298 return 0; 299 } 300 301 /* A dummy callback function used when we can't find a symbol 302 table. */ 303 304 static void 305 elf_nosyms (struct backtrace_state *state ATTRIBUTE_UNUSED, 306 uintptr_t addr ATTRIBUTE_UNUSED, 307 backtrace_syminfo_callback callback ATTRIBUTE_UNUSED, 308 backtrace_error_callback error_callback, void *data) 309 { 310 error_callback (data, "no symbol table in ELF executable", -1); 311 } 312 313 /* Compare struct elf_symbol for qsort. */ 314 315 static int 316 elf_symbol_compare (const void *v1, const void *v2) 317 { 318 const struct elf_symbol *e1 = (const struct elf_symbol *) v1; 319 const struct elf_symbol *e2 = (const struct elf_symbol *) v2; 320 321 if (e1->address < e2->address) 322 return -1; 323 else if (e1->address > e2->address) 324 return 1; 325 else 326 return 0; 327 } 328 329 /* Compare an ADDR against an elf_symbol for bsearch. We allocate one 330 extra entry in the array so that this can look safely at the next 331 entry. */ 332 333 static int 334 elf_symbol_search (const void *vkey, const void *ventry) 335 { 336 const uintptr_t *key = (const uintptr_t *) vkey; 337 const struct elf_symbol *entry = (const struct elf_symbol *) ventry; 338 uintptr_t addr; 339 340 addr = *key; 341 if (addr < entry->address) 342 return -1; 343 else if (addr >= entry->address + entry->size) 344 return 1; 345 else 346 return 0; 347 } 348 349 /* Initialize the symbol table info for elf_syminfo. */ 350 351 static int 352 elf_initialize_syminfo (struct backtrace_state *state, 353 uintptr_t base_address, 354 const unsigned char *symtab_data, size_t symtab_size, 355 const unsigned char *strtab, size_t strtab_size, 356 backtrace_error_callback error_callback, 357 void *data, struct elf_syminfo_data *sdata) 358 { 359 size_t sym_count; 360 const b_elf_sym *sym; 361 size_t elf_symbol_count; 362 size_t elf_symbol_size; 363 struct elf_symbol *elf_symbols; 364 size_t i; 365 unsigned int j; 366 367 sym_count = symtab_size / sizeof (b_elf_sym); 368 369 /* We only care about function symbols. Count them. */ 370 sym = (const b_elf_sym *) symtab_data; 371 elf_symbol_count = 0; 372 for (i = 0; i < sym_count; ++i, ++sym) 373 { 374 int info; 375 376 info = sym->st_info & 0xf; 377 if ((info == STT_FUNC || info == STT_OBJECT) 378 && sym->st_shndx != SHN_UNDEF) 379 ++elf_symbol_count; 380 } 381 382 elf_symbol_size = elf_symbol_count * sizeof (struct elf_symbol); 383 elf_symbols = ((struct elf_symbol *) 384 backtrace_alloc (state, elf_symbol_size, error_callback, 385 data)); 386 if (elf_symbols == NULL) 387 return 0; 388 389 sym = (const b_elf_sym *) symtab_data; 390 j = 0; 391 for (i = 0; i < sym_count; ++i, ++sym) 392 { 393 int info; 394 395 info = sym->st_info & 0xf; 396 if (info != STT_FUNC && info != STT_OBJECT) 397 continue; 398 if (sym->st_shndx == SHN_UNDEF) 399 continue; 400 if (sym->st_name >= strtab_size) 401 { 402 error_callback (data, "symbol string index out of range", 0); 403 backtrace_free (state, elf_symbols, elf_symbol_size, error_callback, 404 data); 405 return 0; 406 } 407 elf_symbols[j].name = (const char *) strtab + sym->st_name; 408 elf_symbols[j].address = sym->st_value + base_address; 409 elf_symbols[j].size = sym->st_size; 410 ++j; 411 } 412 413 backtrace_qsort (elf_symbols, elf_symbol_count, sizeof (struct elf_symbol), 414 elf_symbol_compare); 415 416 sdata->next = NULL; 417 sdata->symbols = elf_symbols; 418 sdata->count = elf_symbol_count; 419 420 return 1; 421 } 422 423 /* Add EDATA to the list in STATE. */ 424 425 static void 426 elf_add_syminfo_data (struct backtrace_state *state, 427 struct elf_syminfo_data *edata) 428 { 429 if (!state->threaded) 430 { 431 struct elf_syminfo_data **pp; 432 433 for (pp = (struct elf_syminfo_data **) (void *) &state->syminfo_data; 434 *pp != NULL; 435 pp = &(*pp)->next) 436 ; 437 *pp = edata; 438 } 439 else 440 { 441 while (1) 442 { 443 struct elf_syminfo_data **pp; 444 445 pp = (struct elf_syminfo_data **) (void *) &state->syminfo_data; 446 447 while (1) 448 { 449 struct elf_syminfo_data *p; 450 451 p = backtrace_atomic_load_pointer (pp); 452 453 if (p == NULL) 454 break; 455 456 pp = &p->next; 457 } 458 459 if (__sync_bool_compare_and_swap (pp, NULL, edata)) 460 break; 461 } 462 } 463 } 464 465 /* Return the symbol name and value for an ADDR. */ 466 467 static void 468 elf_syminfo (struct backtrace_state *state, uintptr_t addr, 469 backtrace_syminfo_callback callback, 470 backtrace_error_callback error_callback ATTRIBUTE_UNUSED, 471 void *data) 472 { 473 struct elf_syminfo_data *edata; 474 struct elf_symbol *sym = NULL; 475 476 if (!state->threaded) 477 { 478 for (edata = (struct elf_syminfo_data *) state->syminfo_data; 479 edata != NULL; 480 edata = edata->next) 481 { 482 sym = ((struct elf_symbol *) 483 bsearch (&addr, edata->symbols, edata->count, 484 sizeof (struct elf_symbol), elf_symbol_search)); 485 if (sym != NULL) 486 break; 487 } 488 } 489 else 490 { 491 struct elf_syminfo_data **pp; 492 493 pp = (struct elf_syminfo_data **) (void *) &state->syminfo_data; 494 while (1) 495 { 496 edata = backtrace_atomic_load_pointer (pp); 497 if (edata == NULL) 498 break; 499 500 sym = ((struct elf_symbol *) 501 bsearch (&addr, edata->symbols, edata->count, 502 sizeof (struct elf_symbol), elf_symbol_search)); 503 if (sym != NULL) 504 break; 505 506 pp = &edata->next; 507 } 508 } 509 510 if (sym == NULL) 511 callback (data, addr, NULL, 0, 0); 512 else 513 callback (data, addr, sym->name, sym->address, sym->size); 514 } 515 516 /* Add the backtrace data for one ELF file. Returns 1 on success, 517 0 on failure (in both cases descriptor is closed) or -1 if exe 518 is non-zero and the ELF file is ET_DYN, which tells the caller that 519 elf_add will need to be called on the descriptor again after 520 base_address is determined. */ 521 522 static int 523 elf_add (struct backtrace_state *state, int descriptor, uintptr_t base_address, 524 backtrace_error_callback error_callback, void *data, 525 fileline *fileline_fn, int *found_sym, int *found_dwarf, int exe) 526 { 527 struct backtrace_view ehdr_view; 528 b_elf_ehdr ehdr; 529 off_t shoff; 530 unsigned int shnum; 531 unsigned int shstrndx; 532 struct backtrace_view shdrs_view; 533 int shdrs_view_valid; 534 const b_elf_shdr *shdrs; 535 const b_elf_shdr *shstrhdr; 536 size_t shstr_size; 537 off_t shstr_off; 538 struct backtrace_view names_view; 539 int names_view_valid; 540 const char *names; 541 unsigned int symtab_shndx; 542 unsigned int dynsym_shndx; 543 unsigned int i; 544 struct debug_section_info sections[DEBUG_MAX]; 545 struct backtrace_view symtab_view; 546 int symtab_view_valid; 547 struct backtrace_view strtab_view; 548 int strtab_view_valid; 549 off_t min_offset; 550 off_t max_offset; 551 struct backtrace_view debug_view; 552 int debug_view_valid; 553 554 *found_sym = 0; 555 *found_dwarf = 0; 556 557 shdrs_view_valid = 0; 558 names_view_valid = 0; 559 symtab_view_valid = 0; 560 strtab_view_valid = 0; 561 debug_view_valid = 0; 562 563 if (!backtrace_get_view (state, descriptor, 0, sizeof ehdr, error_callback, 564 data, &ehdr_view)) 565 goto fail; 566 567 memcpy (&ehdr, ehdr_view.data, sizeof ehdr); 568 569 backtrace_release_view (state, &ehdr_view, error_callback, data); 570 571 if (ehdr.e_ident[EI_MAG0] != ELFMAG0 572 || ehdr.e_ident[EI_MAG1] != ELFMAG1 573 || ehdr.e_ident[EI_MAG2] != ELFMAG2 574 || ehdr.e_ident[EI_MAG3] != ELFMAG3) 575 { 576 error_callback (data, "executable file is not ELF", 0); 577 goto fail; 578 } 579 if (ehdr.e_ident[EI_VERSION] != EV_CURRENT) 580 { 581 error_callback (data, "executable file is unrecognized ELF version", 0); 582 goto fail; 583 } 584 585 #if BACKTRACE_ELF_SIZE == 32 586 #define BACKTRACE_ELFCLASS ELFCLASS32 587 #else 588 #define BACKTRACE_ELFCLASS ELFCLASS64 589 #endif 590 591 if (ehdr.e_ident[EI_CLASS] != BACKTRACE_ELFCLASS) 592 { 593 error_callback (data, "executable file is unexpected ELF class", 0); 594 goto fail; 595 } 596 597 if (ehdr.e_ident[EI_DATA] != ELFDATA2LSB 598 && ehdr.e_ident[EI_DATA] != ELFDATA2MSB) 599 { 600 error_callback (data, "executable file has unknown endianness", 0); 601 goto fail; 602 } 603 604 /* If the executable is ET_DYN, it is either a PIE, or we are running 605 directly a shared library with .interp. We need to wait for 606 dl_iterate_phdr in that case to determine the actual base_address. */ 607 if (exe && ehdr.e_type == ET_DYN) 608 return -1; 609 610 shoff = ehdr.e_shoff; 611 shnum = ehdr.e_shnum; 612 shstrndx = ehdr.e_shstrndx; 613 614 if ((shnum == 0 || shstrndx == SHN_XINDEX) 615 && shoff != 0) 616 { 617 struct backtrace_view shdr_view; 618 const b_elf_shdr *shdr; 619 620 if (!backtrace_get_view (state, descriptor, shoff, sizeof shdr, 621 error_callback, data, &shdr_view)) 622 goto fail; 623 624 shdr = (const b_elf_shdr *) shdr_view.data; 625 626 if (shnum == 0) 627 shnum = shdr->sh_size; 628 629 if (shstrndx == SHN_XINDEX) 630 { 631 shstrndx = shdr->sh_link; 632 633 /* Versions of the GNU binutils between 2.12 and 2.18 did 634 not handle objects with more than SHN_LORESERVE sections 635 correctly. All large section indexes were offset by 636 0x100. There is more information at 637 http://sourceware.org/bugzilla/show_bug.cgi?id-5900 . 638 Fortunately these object files are easy to detect, as the 639 GNU binutils always put the section header string table 640 near the end of the list of sections. Thus if the 641 section header string table index is larger than the 642 number of sections, then we know we have to subtract 643 0x100 to get the real section index. */ 644 if (shstrndx >= shnum && shstrndx >= SHN_LORESERVE + 0x100) 645 shstrndx -= 0x100; 646 } 647 648 backtrace_release_view (state, &shdr_view, error_callback, data); 649 } 650 651 /* To translate PC to file/line when using DWARF, we need to find 652 the .debug_info and .debug_line sections. */ 653 654 /* Read the section headers, skipping the first one. */ 655 656 if (!backtrace_get_view (state, descriptor, shoff + sizeof (b_elf_shdr), 657 (shnum - 1) * sizeof (b_elf_shdr), 658 error_callback, data, &shdrs_view)) 659 goto fail; 660 shdrs_view_valid = 1; 661 shdrs = (const b_elf_shdr *) shdrs_view.data; 662 663 /* Read the section names. */ 664 665 shstrhdr = &shdrs[shstrndx - 1]; 666 shstr_size = shstrhdr->sh_size; 667 shstr_off = shstrhdr->sh_offset; 668 669 if (!backtrace_get_view (state, descriptor, shstr_off, shstr_size, 670 error_callback, data, &names_view)) 671 goto fail; 672 names_view_valid = 1; 673 names = (const char *) names_view.data; 674 675 symtab_shndx = 0; 676 dynsym_shndx = 0; 677 678 memset (sections, 0, sizeof sections); 679 680 /* Look for the symbol table. */ 681 for (i = 1; i < shnum; ++i) 682 { 683 const b_elf_shdr *shdr; 684 unsigned int sh_name; 685 const char *name; 686 int j; 687 688 shdr = &shdrs[i - 1]; 689 690 if (shdr->sh_type == SHT_SYMTAB) 691 symtab_shndx = i; 692 else if (shdr->sh_type == SHT_DYNSYM) 693 dynsym_shndx = i; 694 695 sh_name = shdr->sh_name; 696 if (sh_name >= shstr_size) 697 { 698 error_callback (data, "ELF section name out of range", 0); 699 goto fail; 700 } 701 702 name = names + sh_name; 703 704 for (j = 0; j < (int) DEBUG_MAX; ++j) 705 { 706 if (strcmp (name, debug_section_names[j]) == 0 707 && (shdr->sh_flags & SHF_COMPRESSED) == 0) 708 { 709 sections[j].offset = shdr->sh_offset; 710 sections[j].size = shdr->sh_size; 711 break; 712 } 713 } 714 } 715 716 if (symtab_shndx == 0) 717 symtab_shndx = dynsym_shndx; 718 if (symtab_shndx != 0) 719 { 720 const b_elf_shdr *symtab_shdr; 721 unsigned int strtab_shndx; 722 const b_elf_shdr *strtab_shdr; 723 struct elf_syminfo_data *sdata; 724 725 symtab_shdr = &shdrs[symtab_shndx - 1]; 726 strtab_shndx = symtab_shdr->sh_link; 727 if (strtab_shndx >= shnum) 728 { 729 error_callback (data, 730 "ELF symbol table strtab link out of range", 0); 731 goto fail; 732 } 733 strtab_shdr = &shdrs[strtab_shndx - 1]; 734 735 if (!backtrace_get_view (state, descriptor, symtab_shdr->sh_offset, 736 symtab_shdr->sh_size, error_callback, data, 737 &symtab_view)) 738 goto fail; 739 symtab_view_valid = 1; 740 741 if (!backtrace_get_view (state, descriptor, strtab_shdr->sh_offset, 742 strtab_shdr->sh_size, error_callback, data, 743 &strtab_view)) 744 goto fail; 745 strtab_view_valid = 1; 746 747 sdata = ((struct elf_syminfo_data *) 748 backtrace_alloc (state, sizeof *sdata, error_callback, data)); 749 if (sdata == NULL) 750 goto fail; 751 752 if (!elf_initialize_syminfo (state, base_address, 753 symtab_view.data, symtab_shdr->sh_size, 754 strtab_view.data, strtab_shdr->sh_size, 755 error_callback, data, sdata)) 756 { 757 backtrace_free (state, sdata, sizeof *sdata, error_callback, data); 758 goto fail; 759 } 760 761 /* We no longer need the symbol table, but we hold on to the 762 string table permanently. */ 763 backtrace_release_view (state, &symtab_view, error_callback, data); 764 765 *found_sym = 1; 766 767 elf_add_syminfo_data (state, sdata); 768 } 769 770 /* FIXME: Need to handle compressed debug sections. */ 771 772 backtrace_release_view (state, &shdrs_view, error_callback, data); 773 shdrs_view_valid = 0; 774 backtrace_release_view (state, &names_view, error_callback, data); 775 names_view_valid = 0; 776 777 /* Read all the debug sections in a single view, since they are 778 probably adjacent in the file. We never release this view. */ 779 780 min_offset = 0; 781 max_offset = 0; 782 for (i = 0; i < (int) DEBUG_MAX; ++i) 783 { 784 off_t end; 785 786 if (sections[i].size == 0) 787 continue; 788 if (min_offset == 0 || sections[i].offset < min_offset) 789 min_offset = sections[i].offset; 790 end = sections[i].offset + sections[i].size; 791 if (end > max_offset) 792 max_offset = end; 793 } 794 if (min_offset == 0 || max_offset == 0) 795 { 796 if (!backtrace_close (descriptor, error_callback, data)) 797 goto fail; 798 return 1; 799 } 800 801 if (!backtrace_get_view (state, descriptor, min_offset, 802 max_offset - min_offset, 803 error_callback, data, &debug_view)) 804 goto fail; 805 debug_view_valid = 1; 806 807 /* We've read all we need from the executable. */ 808 if (!backtrace_close (descriptor, error_callback, data)) 809 goto fail; 810 descriptor = -1; 811 812 for (i = 0; i < (int) DEBUG_MAX; ++i) 813 { 814 if (sections[i].size == 0) 815 sections[i].data = NULL; 816 else 817 sections[i].data = ((const unsigned char *) debug_view.data 818 + (sections[i].offset - min_offset)); 819 } 820 821 if (!backtrace_dwarf_add (state, base_address, 822 sections[DEBUG_INFO].data, 823 sections[DEBUG_INFO].size, 824 sections[DEBUG_LINE].data, 825 sections[DEBUG_LINE].size, 826 sections[DEBUG_ABBREV].data, 827 sections[DEBUG_ABBREV].size, 828 sections[DEBUG_RANGES].data, 829 sections[DEBUG_RANGES].size, 830 sections[DEBUG_STR].data, 831 sections[DEBUG_STR].size, 832 ehdr.e_ident[EI_DATA] == ELFDATA2MSB, 833 error_callback, data, fileline_fn)) 834 goto fail; 835 836 *found_dwarf = 1; 837 838 return 1; 839 840 fail: 841 if (shdrs_view_valid) 842 backtrace_release_view (state, &shdrs_view, error_callback, data); 843 if (names_view_valid) 844 backtrace_release_view (state, &names_view, error_callback, data); 845 if (symtab_view_valid) 846 backtrace_release_view (state, &symtab_view, error_callback, data); 847 if (strtab_view_valid) 848 backtrace_release_view (state, &strtab_view, error_callback, data); 849 if (debug_view_valid) 850 backtrace_release_view (state, &debug_view, error_callback, data); 851 if (descriptor != -1) 852 backtrace_close (descriptor, error_callback, data); 853 return 0; 854 } 855 856 /* Data passed to phdr_callback. */ 857 858 struct phdr_data 859 { 860 struct backtrace_state *state; 861 backtrace_error_callback error_callback; 862 void *data; 863 fileline *fileline_fn; 864 int *found_sym; 865 int *found_dwarf; 866 int exe_descriptor; 867 }; 868 869 /* Callback passed to dl_iterate_phdr. Load debug info from shared 870 libraries. */ 871 872 static int 873 #ifdef __i386__ 874 __attribute__ ((__force_align_arg_pointer__)) 875 #endif 876 phdr_callback (struct dl_phdr_info *info, size_t size ATTRIBUTE_UNUSED, 877 void *pdata) 878 { 879 struct phdr_data *pd = (struct phdr_data *) pdata; 880 int descriptor; 881 int does_not_exist; 882 fileline elf_fileline_fn; 883 int found_dwarf; 884 885 /* There is not much we can do if we don't have the module name, 886 unless executable is ET_DYN, where we expect the very first 887 phdr_callback to be for the PIE. */ 888 if (info->dlpi_name == NULL || info->dlpi_name[0] == '\0') 889 { 890 if (pd->exe_descriptor == -1) 891 return 0; 892 descriptor = pd->exe_descriptor; 893 pd->exe_descriptor = -1; 894 } 895 else 896 { 897 if (pd->exe_descriptor != -1) 898 { 899 backtrace_close (pd->exe_descriptor, pd->error_callback, pd->data); 900 pd->exe_descriptor = -1; 901 } 902 903 descriptor = backtrace_open (info->dlpi_name, pd->error_callback, 904 pd->data, &does_not_exist); 905 if (descriptor < 0) 906 return 0; 907 } 908 909 if (elf_add (pd->state, descriptor, info->dlpi_addr, pd->error_callback, 910 pd->data, &elf_fileline_fn, pd->found_sym, &found_dwarf, 0)) 911 { 912 if (found_dwarf) 913 { 914 *pd->found_dwarf = 1; 915 *pd->fileline_fn = elf_fileline_fn; 916 } 917 } 918 919 return 0; 920 } 921 922 /* Initialize the backtrace data we need from an ELF executable. At 923 the ELF level, all we need to do is find the debug info 924 sections. */ 925 926 int 927 backtrace_initialize (struct backtrace_state *state, int descriptor, 928 backtrace_error_callback error_callback, 929 void *data, fileline *fileline_fn) 930 { 931 int ret; 932 int found_sym; 933 int found_dwarf; 934 fileline elf_fileline_fn = elf_nodebug; 935 struct phdr_data pd; 936 937 ret = elf_add (state, descriptor, 0, error_callback, data, &elf_fileline_fn, 938 &found_sym, &found_dwarf, 1); 939 if (!ret) 940 return 0; 941 942 pd.state = state; 943 pd.error_callback = error_callback; 944 pd.data = data; 945 pd.fileline_fn = &elf_fileline_fn; 946 pd.found_sym = &found_sym; 947 pd.found_dwarf = &found_dwarf; 948 pd.exe_descriptor = ret < 0 ? descriptor : -1; 949 950 dl_iterate_phdr (phdr_callback, (void *) &pd); 951 952 if (!state->threaded) 953 { 954 if (found_sym) 955 state->syminfo_fn = elf_syminfo; 956 else if (state->syminfo_fn == NULL) 957 state->syminfo_fn = elf_nosyms; 958 } 959 else 960 { 961 if (found_sym) 962 backtrace_atomic_store_pointer (&state->syminfo_fn, elf_syminfo); 963 else 964 (void) __sync_bool_compare_and_swap (&state->syminfo_fn, NULL, 965 elf_nosyms); 966 } 967 968 if (!state->threaded) 969 { 970 if (state->fileline_fn == NULL || state->fileline_fn == elf_nodebug) 971 *fileline_fn = elf_fileline_fn; 972 } 973 else 974 { 975 fileline current_fn; 976 977 current_fn = backtrace_atomic_load_pointer (&state->fileline_fn); 978 if (current_fn == NULL || current_fn == elf_nodebug) 979 *fileline_fn = elf_fileline_fn; 980 } 981 982 return 1; 983 } 984