1 /* DWARF 2 support. 2 Copyright (C) 1994-2018 Free Software Foundation, Inc. 3 4 Adapted from gdb/dwarf2read.c by Gavin Koch of Cygnus Solutions 5 (gavin@cygnus.com). 6 7 From the dwarf2read.c header: 8 Adapted by Gary Funck (gary@intrepid.com), Intrepid Technology, 9 Inc. with support from Florida State University (under contract 10 with the Ada Joint Program Office), and Silicon Graphics, Inc. 11 Initial contribution by Brent Benson, Harris Computer Systems, Inc., 12 based on Fred Fish's (Cygnus Support) implementation of DWARF 1 13 support in dwarfread.c 14 15 This file is part of BFD. 16 17 This program is free software; you can redistribute it and/or modify 18 it under the terms of the GNU General Public License as published by 19 the Free Software Foundation; either version 3 of the License, or (at 20 your option) any later version. 21 22 This program is distributed in the hope that it will be useful, but 23 WITHOUT ANY WARRANTY; without even the implied warranty of 24 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 25 General Public License for more details. 26 27 You should have received a copy of the GNU General Public License 28 along with this program; if not, write to the Free Software 29 Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, 30 MA 02110-1301, USA. */ 31 32 #include "sysdep.h" 33 #include "bfd.h" 34 #include "libiberty.h" 35 #include "libbfd.h" 36 #include "elf-bfd.h" 37 #include "dwarf2.h" 38 39 /* The data in the .debug_line statement prologue looks like this. */ 40 41 struct line_head 42 { 43 bfd_vma total_length; 44 unsigned short version; 45 bfd_vma prologue_length; 46 unsigned char minimum_instruction_length; 47 unsigned char maximum_ops_per_insn; 48 unsigned char default_is_stmt; 49 int line_base; 50 unsigned char line_range; 51 unsigned char opcode_base; 52 unsigned char *standard_opcode_lengths; 53 }; 54 55 /* Attributes have a name and a value. */ 56 57 struct attribute 58 { 59 enum dwarf_attribute name; 60 enum dwarf_form form; 61 union 62 { 63 char *str; 64 struct dwarf_block *blk; 65 bfd_uint64_t val; 66 bfd_int64_t sval; 67 } 68 u; 69 }; 70 71 /* Blocks are a bunch of untyped bytes. */ 72 struct dwarf_block 73 { 74 unsigned int size; 75 bfd_byte *data; 76 }; 77 78 struct adjusted_section 79 { 80 asection *section; 81 bfd_vma adj_vma; 82 }; 83 84 struct dwarf2_debug 85 { 86 /* A list of all previously read comp_units. */ 87 struct comp_unit *all_comp_units; 88 89 /* Last comp unit in list above. */ 90 struct comp_unit *last_comp_unit; 91 92 /* Names of the debug sections. */ 93 const struct dwarf_debug_section *debug_sections; 94 95 /* The next unread compilation unit within the .debug_info section. 96 Zero indicates that the .debug_info section has not been loaded 97 into a buffer yet. */ 98 bfd_byte *info_ptr; 99 100 /* Pointer to the end of the .debug_info section memory buffer. */ 101 bfd_byte *info_ptr_end; 102 103 /* Pointer to the original bfd for which debug was loaded. This is what 104 we use to compare and so check that the cached debug data is still 105 valid - it saves having to possibly dereference the gnu_debuglink each 106 time. */ 107 bfd *orig_bfd; 108 109 /* Pointer to the bfd, section and address of the beginning of the 110 section. The bfd might be different than expected because of 111 gnu_debuglink sections. */ 112 bfd *bfd_ptr; 113 asection *sec; 114 bfd_byte *sec_info_ptr; 115 116 /* Support for alternate debug info sections created by the DWZ utility: 117 This includes a pointer to an alternate bfd which contains *extra*, 118 possibly duplicate debug sections, and pointers to the loaded 119 .debug_str and .debug_info sections from this bfd. */ 120 bfd * alt_bfd_ptr; 121 bfd_byte * alt_dwarf_str_buffer; 122 bfd_size_type alt_dwarf_str_size; 123 bfd_byte * alt_dwarf_info_buffer; 124 bfd_size_type alt_dwarf_info_size; 125 126 /* A pointer to the memory block allocated for info_ptr. Neither 127 info_ptr nor sec_info_ptr are guaranteed to stay pointing to the 128 beginning of the malloc block. */ 129 bfd_byte *info_ptr_memory; 130 131 /* Pointer to the symbol table. */ 132 asymbol **syms; 133 134 /* Pointer to the .debug_abbrev section loaded into memory. */ 135 bfd_byte *dwarf_abbrev_buffer; 136 137 /* Length of the loaded .debug_abbrev section. */ 138 bfd_size_type dwarf_abbrev_size; 139 140 /* Buffer for decode_line_info. */ 141 bfd_byte *dwarf_line_buffer; 142 143 /* Length of the loaded .debug_line section. */ 144 bfd_size_type dwarf_line_size; 145 146 /* Pointer to the .debug_str section loaded into memory. */ 147 bfd_byte *dwarf_str_buffer; 148 149 /* Length of the loaded .debug_str section. */ 150 bfd_size_type dwarf_str_size; 151 152 /* Pointer to the .debug_line_str section loaded into memory. */ 153 bfd_byte *dwarf_line_str_buffer; 154 155 /* Length of the loaded .debug_line_str section. */ 156 bfd_size_type dwarf_line_str_size; 157 158 /* Pointer to the .debug_ranges section loaded into memory. */ 159 bfd_byte *dwarf_ranges_buffer; 160 161 /* Length of the loaded .debug_ranges section. */ 162 bfd_size_type dwarf_ranges_size; 163 164 /* If the most recent call to bfd_find_nearest_line was given an 165 address in an inlined function, preserve a pointer into the 166 calling chain for subsequent calls to bfd_find_inliner_info to 167 use. */ 168 struct funcinfo *inliner_chain; 169 170 /* Section VMAs at the time the stash was built. */ 171 bfd_vma *sec_vma; 172 173 /* Number of sections whose VMA we must adjust. */ 174 int adjusted_section_count; 175 176 /* Array of sections with adjusted VMA. */ 177 struct adjusted_section *adjusted_sections; 178 179 /* Number of times find_line is called. This is used in 180 the heuristic for enabling the info hash tables. */ 181 int info_hash_count; 182 183 #define STASH_INFO_HASH_TRIGGER 100 184 185 /* Hash table mapping symbol names to function infos. */ 186 struct info_hash_table *funcinfo_hash_table; 187 188 /* Hash table mapping symbol names to variable infos. */ 189 struct info_hash_table *varinfo_hash_table; 190 191 /* Head of comp_unit list in the last hash table update. */ 192 struct comp_unit *hash_units_head; 193 194 /* Status of info hash. */ 195 int info_hash_status; 196 #define STASH_INFO_HASH_OFF 0 197 #define STASH_INFO_HASH_ON 1 198 #define STASH_INFO_HASH_DISABLED 2 199 200 /* True if we opened bfd_ptr. */ 201 bfd_boolean close_on_cleanup; 202 }; 203 204 struct arange 205 { 206 struct arange *next; 207 bfd_vma low; 208 bfd_vma high; 209 }; 210 211 /* A minimal decoding of DWARF2 compilation units. We only decode 212 what's needed to get to the line number information. */ 213 214 struct comp_unit 215 { 216 /* Chain the previously read compilation units. */ 217 struct comp_unit *next_unit; 218 219 /* Likewise, chain the compilation unit read after this one. 220 The comp units are stored in reversed reading order. */ 221 struct comp_unit *prev_unit; 222 223 /* Keep the bfd convenient (for memory allocation). */ 224 bfd *abfd; 225 226 /* The lowest and highest addresses contained in this compilation 227 unit as specified in the compilation unit header. */ 228 struct arange arange; 229 230 /* The DW_AT_name attribute (for error messages). */ 231 char *name; 232 233 /* The abbrev hash table. */ 234 struct abbrev_info **abbrevs; 235 236 /* DW_AT_language. */ 237 int lang; 238 239 /* Note that an error was found by comp_unit_find_nearest_line. */ 240 int error; 241 242 /* The DW_AT_comp_dir attribute. */ 243 char *comp_dir; 244 245 /* TRUE if there is a line number table associated with this comp. unit. */ 246 int stmtlist; 247 248 /* Pointer to the current comp_unit so that we can find a given entry 249 by its reference. */ 250 bfd_byte *info_ptr_unit; 251 252 /* The offset into .debug_line of the line number table. */ 253 unsigned long line_offset; 254 255 /* Pointer to the first child die for the comp unit. */ 256 bfd_byte *first_child_die_ptr; 257 258 /* The end of the comp unit. */ 259 bfd_byte *end_ptr; 260 261 /* The decoded line number, NULL if not yet decoded. */ 262 struct line_info_table *line_table; 263 264 /* A list of the functions found in this comp. unit. */ 265 struct funcinfo *function_table; 266 267 /* A table of function information references searchable by address. */ 268 struct lookup_funcinfo *lookup_funcinfo_table; 269 270 /* Number of functions in the function_table and sorted_function_table. */ 271 bfd_size_type number_of_functions; 272 273 /* A list of the variables found in this comp. unit. */ 274 struct varinfo *variable_table; 275 276 /* Pointer to dwarf2_debug structure. */ 277 struct dwarf2_debug *stash; 278 279 /* DWARF format version for this unit - from unit header. */ 280 int version; 281 282 /* Address size for this unit - from unit header. */ 283 unsigned char addr_size; 284 285 /* Offset size for this unit - from unit header. */ 286 unsigned char offset_size; 287 288 /* Base address for this unit - from DW_AT_low_pc attribute of 289 DW_TAG_compile_unit DIE */ 290 bfd_vma base_address; 291 292 /* TRUE if symbols are cached in hash table for faster lookup by name. */ 293 bfd_boolean cached; 294 }; 295 296 /* This data structure holds the information of an abbrev. */ 297 struct abbrev_info 298 { 299 unsigned int number; /* Number identifying abbrev. */ 300 enum dwarf_tag tag; /* DWARF tag. */ 301 int has_children; /* Boolean. */ 302 unsigned int num_attrs; /* Number of attributes. */ 303 struct attr_abbrev *attrs; /* An array of attribute descriptions. */ 304 struct abbrev_info *next; /* Next in chain. */ 305 }; 306 307 struct attr_abbrev 308 { 309 enum dwarf_attribute name; 310 enum dwarf_form form; 311 bfd_vma implicit_const; 312 }; 313 314 /* Map of uncompressed DWARF debug section name to compressed one. It 315 is terminated by NULL uncompressed_name. */ 316 317 const struct dwarf_debug_section dwarf_debug_sections[] = 318 { 319 { ".debug_abbrev", ".zdebug_abbrev" }, 320 { ".debug_aranges", ".zdebug_aranges" }, 321 { ".debug_frame", ".zdebug_frame" }, 322 { ".debug_info", ".zdebug_info" }, 323 { ".debug_info", ".zdebug_info" }, 324 { ".debug_line", ".zdebug_line" }, 325 { ".debug_loc", ".zdebug_loc" }, 326 { ".debug_macinfo", ".zdebug_macinfo" }, 327 { ".debug_macro", ".zdebug_macro" }, 328 { ".debug_pubnames", ".zdebug_pubnames" }, 329 { ".debug_pubtypes", ".zdebug_pubtypes" }, 330 { ".debug_ranges", ".zdebug_ranges" }, 331 { ".debug_static_func", ".zdebug_static_func" }, 332 { ".debug_static_vars", ".zdebug_static_vars" }, 333 { ".debug_str", ".zdebug_str", }, 334 { ".debug_str", ".zdebug_str", }, 335 { ".debug_line_str", ".zdebug_line_str", }, 336 { ".debug_types", ".zdebug_types" }, 337 /* GNU DWARF 1 extensions */ 338 { ".debug_sfnames", ".zdebug_sfnames" }, 339 { ".debug_srcinfo", ".zebug_srcinfo" }, 340 /* SGI/MIPS DWARF 2 extensions */ 341 { ".debug_funcnames", ".zdebug_funcnames" }, 342 { ".debug_typenames", ".zdebug_typenames" }, 343 { ".debug_varnames", ".zdebug_varnames" }, 344 { ".debug_weaknames", ".zdebug_weaknames" }, 345 { NULL, NULL }, 346 }; 347 348 /* NB/ Numbers in this enum must match up with indicies 349 into the dwarf_debug_sections[] array above. */ 350 enum dwarf_debug_section_enum 351 { 352 debug_abbrev = 0, 353 debug_aranges, 354 debug_frame, 355 debug_info, 356 debug_info_alt, 357 debug_line, 358 debug_loc, 359 debug_macinfo, 360 debug_macro, 361 debug_pubnames, 362 debug_pubtypes, 363 debug_ranges, 364 debug_static_func, 365 debug_static_vars, 366 debug_str, 367 debug_str_alt, 368 debug_line_str, 369 debug_types, 370 debug_sfnames, 371 debug_srcinfo, 372 debug_funcnames, 373 debug_typenames, 374 debug_varnames, 375 debug_weaknames, 376 debug_max 377 }; 378 379 /* A static assertion. */ 380 extern int dwarf_debug_section_assert[ARRAY_SIZE (dwarf_debug_sections) 381 == debug_max + 1 ? 1 : -1]; 382 383 #ifndef ABBREV_HASH_SIZE 384 #define ABBREV_HASH_SIZE 121 385 #endif 386 #ifndef ATTR_ALLOC_CHUNK 387 #define ATTR_ALLOC_CHUNK 4 388 #endif 389 390 /* Variable and function hash tables. This is used to speed up look-up 391 in lookup_symbol_in_var_table() and lookup_symbol_in_function_table(). 392 In order to share code between variable and function infos, we use 393 a list of untyped pointer for all variable/function info associated with 394 a symbol. We waste a bit of memory for list with one node but that 395 simplifies the code. */ 396 397 struct info_list_node 398 { 399 struct info_list_node *next; 400 void *info; 401 }; 402 403 /* Info hash entry. */ 404 struct info_hash_entry 405 { 406 struct bfd_hash_entry root; 407 struct info_list_node *head; 408 }; 409 410 struct info_hash_table 411 { 412 struct bfd_hash_table base; 413 }; 414 415 /* Function to create a new entry in info hash table. */ 416 417 static struct bfd_hash_entry * 418 info_hash_table_newfunc (struct bfd_hash_entry *entry, 419 struct bfd_hash_table *table, 420 const char *string) 421 { 422 struct info_hash_entry *ret = (struct info_hash_entry *) entry; 423 424 /* Allocate the structure if it has not already been allocated by a 425 derived class. */ 426 if (ret == NULL) 427 { 428 ret = (struct info_hash_entry *) bfd_hash_allocate (table, 429 sizeof (* ret)); 430 if (ret == NULL) 431 return NULL; 432 } 433 434 /* Call the allocation method of the base class. */ 435 ret = ((struct info_hash_entry *) 436 bfd_hash_newfunc ((struct bfd_hash_entry *) ret, table, string)); 437 438 /* Initialize the local fields here. */ 439 if (ret) 440 ret->head = NULL; 441 442 return (struct bfd_hash_entry *) ret; 443 } 444 445 /* Function to create a new info hash table. It returns a pointer to the 446 newly created table or NULL if there is any error. We need abfd 447 solely for memory allocation. */ 448 449 static struct info_hash_table * 450 create_info_hash_table (bfd *abfd) 451 { 452 struct info_hash_table *hash_table; 453 454 hash_table = ((struct info_hash_table *) 455 bfd_alloc (abfd, sizeof (struct info_hash_table))); 456 if (!hash_table) 457 return hash_table; 458 459 if (!bfd_hash_table_init (&hash_table->base, info_hash_table_newfunc, 460 sizeof (struct info_hash_entry))) 461 { 462 bfd_release (abfd, hash_table); 463 return NULL; 464 } 465 466 return hash_table; 467 } 468 469 /* Insert an info entry into an info hash table. We do not check of 470 duplicate entries. Also, the caller need to guarantee that the 471 right type of info in inserted as info is passed as a void* pointer. 472 This function returns true if there is no error. */ 473 474 static bfd_boolean 475 insert_info_hash_table (struct info_hash_table *hash_table, 476 const char *key, 477 void *info, 478 bfd_boolean copy_p) 479 { 480 struct info_hash_entry *entry; 481 struct info_list_node *node; 482 483 entry = (struct info_hash_entry*) bfd_hash_lookup (&hash_table->base, 484 key, TRUE, copy_p); 485 if (!entry) 486 return FALSE; 487 488 node = (struct info_list_node *) bfd_hash_allocate (&hash_table->base, 489 sizeof (*node)); 490 if (!node) 491 return FALSE; 492 493 node->info = info; 494 node->next = entry->head; 495 entry->head = node; 496 497 return TRUE; 498 } 499 500 /* Look up an info entry list from an info hash table. Return NULL 501 if there is none. */ 502 503 static struct info_list_node * 504 lookup_info_hash_table (struct info_hash_table *hash_table, const char *key) 505 { 506 struct info_hash_entry *entry; 507 508 entry = (struct info_hash_entry*) bfd_hash_lookup (&hash_table->base, key, 509 FALSE, FALSE); 510 return entry ? entry->head : NULL; 511 } 512 513 /* Read a section into its appropriate place in the dwarf2_debug 514 struct (indicated by SECTION_BUFFER and SECTION_SIZE). If SYMS is 515 not NULL, use bfd_simple_get_relocated_section_contents to read the 516 section contents, otherwise use bfd_get_section_contents. Fail if 517 the located section does not contain at least OFFSET bytes. */ 518 519 static bfd_boolean 520 read_section (bfd * abfd, 521 const struct dwarf_debug_section *sec, 522 asymbol ** syms, 523 bfd_uint64_t offset, 524 bfd_byte ** section_buffer, 525 bfd_size_type * section_size) 526 { 527 asection *msec; 528 const char *section_name = sec->uncompressed_name; 529 bfd_byte *contents = *section_buffer; 530 531 /* The section may have already been read. */ 532 if (contents == NULL) 533 { 534 msec = bfd_get_section_by_name (abfd, section_name); 535 if (! msec) 536 { 537 section_name = sec->compressed_name; 538 if (section_name != NULL) 539 msec = bfd_get_section_by_name (abfd, section_name); 540 } 541 if (! msec) 542 { 543 _bfd_error_handler (_("Dwarf Error: Can't find %s section."), 544 sec->uncompressed_name); 545 bfd_set_error (bfd_error_bad_value); 546 return FALSE; 547 } 548 549 *section_size = msec->rawsize ? msec->rawsize : msec->size; 550 /* Paranoia - alloc one extra so that we can make sure a string 551 section is NUL terminated. */ 552 contents = (bfd_byte *) bfd_malloc (*section_size + 1); 553 if (contents == NULL) 554 return FALSE; 555 if (syms 556 ? !bfd_simple_get_relocated_section_contents (abfd, msec, contents, 557 syms) 558 : !bfd_get_section_contents (abfd, msec, contents, 0, *section_size)) 559 { 560 free (contents); 561 return FALSE; 562 } 563 contents[*section_size] = 0; 564 *section_buffer = contents; 565 } 566 567 /* It is possible to get a bad value for the offset into the section 568 that the client wants. Validate it here to avoid trouble later. */ 569 if (offset != 0 && offset >= *section_size) 570 { 571 /* xgettext: c-format */ 572 _bfd_error_handler (_("Dwarf Error: Offset (%llu)" 573 " greater than or equal to %s size (%Lu)."), 574 (long long) offset, section_name, *section_size); 575 bfd_set_error (bfd_error_bad_value); 576 return FALSE; 577 } 578 579 return TRUE; 580 } 581 582 /* Read dwarf information from a buffer. */ 583 584 static unsigned int 585 read_1_byte (bfd *abfd ATTRIBUTE_UNUSED, bfd_byte *buf, bfd_byte *end) 586 { 587 if (buf + 1 > end) 588 return 0; 589 return bfd_get_8 (abfd, buf); 590 } 591 592 static int 593 read_1_signed_byte (bfd *abfd ATTRIBUTE_UNUSED, bfd_byte *buf, bfd_byte *end) 594 { 595 if (buf + 1 > end) 596 return 0; 597 return bfd_get_signed_8 (abfd, buf); 598 } 599 600 static unsigned int 601 read_2_bytes (bfd *abfd, bfd_byte *buf, bfd_byte *end) 602 { 603 if (buf + 2 > end) 604 return 0; 605 return bfd_get_16 (abfd, buf); 606 } 607 608 static unsigned int 609 read_4_bytes (bfd *abfd, bfd_byte *buf, bfd_byte *end) 610 { 611 if (buf + 4 > end) 612 return 0; 613 return bfd_get_32 (abfd, buf); 614 } 615 616 static bfd_uint64_t 617 read_8_bytes (bfd *abfd, bfd_byte *buf, bfd_byte *end) 618 { 619 if (buf + 8 > end) 620 return 0; 621 return bfd_get_64 (abfd, buf); 622 } 623 624 static bfd_byte * 625 read_n_bytes (bfd *abfd ATTRIBUTE_UNUSED, 626 bfd_byte *buf, 627 bfd_byte *end, 628 unsigned int size ATTRIBUTE_UNUSED) 629 { 630 if (buf + size > end) 631 return NULL; 632 return buf; 633 } 634 635 /* Scans a NUL terminated string starting at BUF, returning a pointer to it. 636 Returns the number of characters in the string, *including* the NUL byte, 637 in BYTES_READ_PTR. This value is set even if the function fails. Bytes 638 at or beyond BUF_END will not be read. Returns NULL if there was a 639 problem, or if the string is empty. */ 640 641 static char * 642 read_string (bfd * abfd ATTRIBUTE_UNUSED, 643 bfd_byte * buf, 644 bfd_byte * buf_end, 645 unsigned int * bytes_read_ptr) 646 { 647 bfd_byte *str = buf; 648 649 if (buf >= buf_end) 650 { 651 * bytes_read_ptr = 0; 652 return NULL; 653 } 654 655 if (*str == '\0') 656 { 657 * bytes_read_ptr = 1; 658 return NULL; 659 } 660 661 while (buf < buf_end) 662 if (* buf ++ == 0) 663 { 664 * bytes_read_ptr = buf - str; 665 return (char *) str; 666 } 667 668 * bytes_read_ptr = buf - str; 669 return NULL; 670 } 671 672 /* Reads an offset from BUF and then locates the string at this offset 673 inside the debug string section. Returns a pointer to the string. 674 Returns the number of bytes read from BUF, *not* the length of the string, 675 in BYTES_READ_PTR. This value is set even if the function fails. Bytes 676 at or beyond BUF_END will not be read from BUF. Returns NULL if there was 677 a problem, or if the string is empty. Does not check for NUL termination 678 of the string. */ 679 680 static char * 681 read_indirect_string (struct comp_unit * unit, 682 bfd_byte * buf, 683 bfd_byte * buf_end, 684 unsigned int * bytes_read_ptr) 685 { 686 bfd_uint64_t offset; 687 struct dwarf2_debug *stash = unit->stash; 688 char *str; 689 690 if (buf + unit->offset_size > buf_end) 691 { 692 * bytes_read_ptr = 0; 693 return NULL; 694 } 695 696 if (unit->offset_size == 4) 697 offset = read_4_bytes (unit->abfd, buf, buf_end); 698 else 699 offset = read_8_bytes (unit->abfd, buf, buf_end); 700 701 *bytes_read_ptr = unit->offset_size; 702 703 if (! read_section (unit->abfd, &stash->debug_sections[debug_str], 704 stash->syms, offset, 705 &stash->dwarf_str_buffer, &stash->dwarf_str_size)) 706 return NULL; 707 708 if (offset >= stash->dwarf_str_size) 709 return NULL; 710 str = (char *) stash->dwarf_str_buffer + offset; 711 if (*str == '\0') 712 return NULL; 713 return str; 714 } 715 716 /* Like read_indirect_string but from .debug_line_str section. */ 717 718 static char * 719 read_indirect_line_string (struct comp_unit * unit, 720 bfd_byte * buf, 721 bfd_byte * buf_end, 722 unsigned int * bytes_read_ptr) 723 { 724 bfd_uint64_t offset; 725 struct dwarf2_debug *stash = unit->stash; 726 char *str; 727 728 if (buf + unit->offset_size > buf_end) 729 { 730 * bytes_read_ptr = 0; 731 return NULL; 732 } 733 734 if (unit->offset_size == 4) 735 offset = read_4_bytes (unit->abfd, buf, buf_end); 736 else 737 offset = read_8_bytes (unit->abfd, buf, buf_end); 738 739 *bytes_read_ptr = unit->offset_size; 740 741 if (! read_section (unit->abfd, &stash->debug_sections[debug_line_str], 742 stash->syms, offset, 743 &stash->dwarf_line_str_buffer, 744 &stash->dwarf_line_str_size)) 745 return NULL; 746 747 if (offset >= stash->dwarf_line_str_size) 748 return NULL; 749 str = (char *) stash->dwarf_line_str_buffer + offset; 750 if (*str == '\0') 751 return NULL; 752 return str; 753 } 754 755 /* Like read_indirect_string but uses a .debug_str located in 756 an alternate file pointed to by the .gnu_debugaltlink section. 757 Used to impement DW_FORM_GNU_strp_alt. */ 758 759 static char * 760 read_alt_indirect_string (struct comp_unit * unit, 761 bfd_byte * buf, 762 bfd_byte * buf_end, 763 unsigned int * bytes_read_ptr) 764 { 765 bfd_uint64_t offset; 766 struct dwarf2_debug *stash = unit->stash; 767 char *str; 768 769 if (buf + unit->offset_size > buf_end) 770 { 771 * bytes_read_ptr = 0; 772 return NULL; 773 } 774 775 if (unit->offset_size == 4) 776 offset = read_4_bytes (unit->abfd, buf, buf_end); 777 else 778 offset = read_8_bytes (unit->abfd, buf, buf_end); 779 780 *bytes_read_ptr = unit->offset_size; 781 782 if (stash->alt_bfd_ptr == NULL) 783 { 784 bfd * debug_bfd; 785 char * debug_filename = bfd_follow_gnu_debugaltlink (unit->abfd, DEBUGDIR); 786 787 if (debug_filename == NULL) 788 return NULL; 789 790 if ((debug_bfd = bfd_openr (debug_filename, NULL)) == NULL 791 || ! bfd_check_format (debug_bfd, bfd_object)) 792 { 793 if (debug_bfd) 794 bfd_close (debug_bfd); 795 796 /* FIXME: Should we report our failure to follow the debuglink ? */ 797 free (debug_filename); 798 return NULL; 799 } 800 stash->alt_bfd_ptr = debug_bfd; 801 } 802 803 if (! read_section (unit->stash->alt_bfd_ptr, 804 stash->debug_sections + debug_str_alt, 805 NULL, /* FIXME: Do we need to load alternate symbols ? */ 806 offset, 807 &stash->alt_dwarf_str_buffer, 808 &stash->alt_dwarf_str_size)) 809 return NULL; 810 811 if (offset >= stash->alt_dwarf_str_size) 812 return NULL; 813 str = (char *) stash->alt_dwarf_str_buffer + offset; 814 if (*str == '\0') 815 return NULL; 816 817 return str; 818 } 819 820 /* Resolve an alternate reference from UNIT at OFFSET. 821 Returns a pointer into the loaded alternate CU upon success 822 or NULL upon failure. */ 823 824 static bfd_byte * 825 read_alt_indirect_ref (struct comp_unit * unit, 826 bfd_uint64_t offset) 827 { 828 struct dwarf2_debug *stash = unit->stash; 829 830 if (stash->alt_bfd_ptr == NULL) 831 { 832 bfd * debug_bfd; 833 char * debug_filename = bfd_follow_gnu_debugaltlink (unit->abfd, DEBUGDIR); 834 835 if (debug_filename == NULL) 836 return FALSE; 837 838 if ((debug_bfd = bfd_openr (debug_filename, NULL)) == NULL 839 || ! bfd_check_format (debug_bfd, bfd_object)) 840 { 841 if (debug_bfd) 842 bfd_close (debug_bfd); 843 844 /* FIXME: Should we report our failure to follow the debuglink ? */ 845 free (debug_filename); 846 return NULL; 847 } 848 stash->alt_bfd_ptr = debug_bfd; 849 } 850 851 if (! read_section (unit->stash->alt_bfd_ptr, 852 stash->debug_sections + debug_info_alt, 853 NULL, /* FIXME: Do we need to load alternate symbols ? */ 854 offset, 855 &stash->alt_dwarf_info_buffer, 856 &stash->alt_dwarf_info_size)) 857 return NULL; 858 859 if (offset >= stash->alt_dwarf_info_size) 860 return NULL; 861 return stash->alt_dwarf_info_buffer + offset; 862 } 863 864 static bfd_uint64_t 865 read_address (struct comp_unit *unit, bfd_byte *buf, bfd_byte * buf_end) 866 { 867 int signed_vma = 0; 868 869 if (bfd_get_flavour (unit->abfd) == bfd_target_elf_flavour) 870 signed_vma = get_elf_backend_data (unit->abfd)->sign_extend_vma; 871 872 if (buf + unit->addr_size > buf_end) 873 return 0; 874 875 if (signed_vma) 876 { 877 switch (unit->addr_size) 878 { 879 case 8: 880 return bfd_get_signed_64 (unit->abfd, buf); 881 case 4: 882 return bfd_get_signed_32 (unit->abfd, buf); 883 case 2: 884 return bfd_get_signed_16 (unit->abfd, buf); 885 default: 886 abort (); 887 } 888 } 889 else 890 { 891 switch (unit->addr_size) 892 { 893 case 8: 894 return bfd_get_64 (unit->abfd, buf); 895 case 4: 896 return bfd_get_32 (unit->abfd, buf); 897 case 2: 898 return bfd_get_16 (unit->abfd, buf); 899 default: 900 abort (); 901 } 902 } 903 } 904 905 /* Lookup an abbrev_info structure in the abbrev hash table. */ 906 907 static struct abbrev_info * 908 lookup_abbrev (unsigned int number, struct abbrev_info **abbrevs) 909 { 910 unsigned int hash_number; 911 struct abbrev_info *abbrev; 912 913 hash_number = number % ABBREV_HASH_SIZE; 914 abbrev = abbrevs[hash_number]; 915 916 while (abbrev) 917 { 918 if (abbrev->number == number) 919 return abbrev; 920 else 921 abbrev = abbrev->next; 922 } 923 924 return NULL; 925 } 926 927 /* In DWARF version 2, the description of the debugging information is 928 stored in a separate .debug_abbrev section. Before we read any 929 dies from a section we read in all abbreviations and install them 930 in a hash table. */ 931 932 static struct abbrev_info** 933 read_abbrevs (bfd *abfd, bfd_uint64_t offset, struct dwarf2_debug *stash) 934 { 935 struct abbrev_info **abbrevs; 936 bfd_byte *abbrev_ptr; 937 bfd_byte *abbrev_end; 938 struct abbrev_info *cur_abbrev; 939 unsigned int abbrev_number, bytes_read, abbrev_name; 940 unsigned int abbrev_form, hash_number; 941 bfd_size_type amt; 942 943 if (! read_section (abfd, &stash->debug_sections[debug_abbrev], 944 stash->syms, offset, 945 &stash->dwarf_abbrev_buffer, &stash->dwarf_abbrev_size)) 946 return NULL; 947 948 if (offset >= stash->dwarf_abbrev_size) 949 return NULL; 950 951 amt = sizeof (struct abbrev_info*) * ABBREV_HASH_SIZE; 952 abbrevs = (struct abbrev_info **) bfd_zalloc (abfd, amt); 953 if (abbrevs == NULL) 954 return NULL; 955 956 abbrev_ptr = stash->dwarf_abbrev_buffer + offset; 957 abbrev_end = stash->dwarf_abbrev_buffer + stash->dwarf_abbrev_size; 958 abbrev_number = _bfd_safe_read_leb128 (abfd, abbrev_ptr, &bytes_read, 959 FALSE, abbrev_end); 960 abbrev_ptr += bytes_read; 961 962 /* Loop until we reach an abbrev number of 0. */ 963 while (abbrev_number) 964 { 965 amt = sizeof (struct abbrev_info); 966 cur_abbrev = (struct abbrev_info *) bfd_zalloc (abfd, amt); 967 if (cur_abbrev == NULL) 968 return NULL; 969 970 /* Read in abbrev header. */ 971 cur_abbrev->number = abbrev_number; 972 cur_abbrev->tag = (enum dwarf_tag) 973 _bfd_safe_read_leb128 (abfd, abbrev_ptr, &bytes_read, 974 FALSE, abbrev_end); 975 abbrev_ptr += bytes_read; 976 cur_abbrev->has_children = read_1_byte (abfd, abbrev_ptr, abbrev_end); 977 abbrev_ptr += 1; 978 979 /* Now read in declarations. */ 980 for (;;) 981 { 982 /* Initialize it just to avoid a GCC false warning. */ 983 bfd_vma implicit_const = -1; 984 985 abbrev_name = _bfd_safe_read_leb128 (abfd, abbrev_ptr, &bytes_read, 986 FALSE, abbrev_end); 987 abbrev_ptr += bytes_read; 988 abbrev_form = _bfd_safe_read_leb128 (abfd, abbrev_ptr, &bytes_read, 989 FALSE, abbrev_end); 990 abbrev_ptr += bytes_read; 991 if (abbrev_form == DW_FORM_implicit_const) 992 { 993 implicit_const = _bfd_safe_read_leb128 (abfd, abbrev_ptr, 994 &bytes_read, TRUE, 995 abbrev_end); 996 abbrev_ptr += bytes_read; 997 } 998 999 if (abbrev_name == 0) 1000 break; 1001 1002 if ((cur_abbrev->num_attrs % ATTR_ALLOC_CHUNK) == 0) 1003 { 1004 struct attr_abbrev *tmp; 1005 1006 amt = cur_abbrev->num_attrs + ATTR_ALLOC_CHUNK; 1007 amt *= sizeof (struct attr_abbrev); 1008 tmp = (struct attr_abbrev *) bfd_realloc (cur_abbrev->attrs, amt); 1009 if (tmp == NULL) 1010 { 1011 size_t i; 1012 1013 for (i = 0; i < ABBREV_HASH_SIZE; i++) 1014 { 1015 struct abbrev_info *abbrev = abbrevs[i]; 1016 1017 while (abbrev) 1018 { 1019 free (abbrev->attrs); 1020 abbrev = abbrev->next; 1021 } 1022 } 1023 return NULL; 1024 } 1025 cur_abbrev->attrs = tmp; 1026 } 1027 1028 cur_abbrev->attrs[cur_abbrev->num_attrs].name 1029 = (enum dwarf_attribute) abbrev_name; 1030 cur_abbrev->attrs[cur_abbrev->num_attrs].form 1031 = (enum dwarf_form) abbrev_form; 1032 cur_abbrev->attrs[cur_abbrev->num_attrs].implicit_const 1033 = implicit_const; 1034 ++cur_abbrev->num_attrs; 1035 } 1036 1037 hash_number = abbrev_number % ABBREV_HASH_SIZE; 1038 cur_abbrev->next = abbrevs[hash_number]; 1039 abbrevs[hash_number] = cur_abbrev; 1040 1041 /* Get next abbreviation. 1042 Under Irix6 the abbreviations for a compilation unit are not 1043 always properly terminated with an abbrev number of 0. 1044 Exit loop if we encounter an abbreviation which we have 1045 already read (which means we are about to read the abbreviations 1046 for the next compile unit) or if the end of the abbreviation 1047 table is reached. */ 1048 if ((unsigned int) (abbrev_ptr - stash->dwarf_abbrev_buffer) 1049 >= stash->dwarf_abbrev_size) 1050 break; 1051 abbrev_number = _bfd_safe_read_leb128 (abfd, abbrev_ptr, 1052 &bytes_read, FALSE, abbrev_end); 1053 abbrev_ptr += bytes_read; 1054 if (lookup_abbrev (abbrev_number, abbrevs) != NULL) 1055 break; 1056 } 1057 1058 return abbrevs; 1059 } 1060 1061 /* Returns true if the form is one which has a string value. */ 1062 1063 static inline bfd_boolean 1064 is_str_attr (enum dwarf_form form) 1065 { 1066 return (form == DW_FORM_string || form == DW_FORM_strp 1067 || form == DW_FORM_line_strp || form == DW_FORM_GNU_strp_alt); 1068 } 1069 1070 /* Read and fill in the value of attribute ATTR as described by FORM. 1071 Read data starting from INFO_PTR, but never at or beyond INFO_PTR_END. 1072 Returns an updated INFO_PTR taking into account the amount of data read. */ 1073 1074 static bfd_byte * 1075 read_attribute_value (struct attribute * attr, 1076 unsigned form, 1077 bfd_vma implicit_const, 1078 struct comp_unit * unit, 1079 bfd_byte * info_ptr, 1080 bfd_byte * info_ptr_end) 1081 { 1082 bfd *abfd = unit->abfd; 1083 unsigned int bytes_read; 1084 struct dwarf_block *blk; 1085 bfd_size_type amt; 1086 1087 if (info_ptr >= info_ptr_end && form != DW_FORM_flag_present) 1088 { 1089 _bfd_error_handler (_("Dwarf Error: Info pointer extends beyond end of attributes")); 1090 bfd_set_error (bfd_error_bad_value); 1091 return info_ptr; 1092 } 1093 1094 attr->form = (enum dwarf_form) form; 1095 1096 switch (form) 1097 { 1098 case DW_FORM_ref_addr: 1099 /* DW_FORM_ref_addr is an address in DWARF2, and an offset in 1100 DWARF3. */ 1101 if (unit->version == 3 || unit->version == 4) 1102 { 1103 if (unit->offset_size == 4) 1104 attr->u.val = read_4_bytes (unit->abfd, info_ptr, info_ptr_end); 1105 else 1106 attr->u.val = read_8_bytes (unit->abfd, info_ptr, info_ptr_end); 1107 info_ptr += unit->offset_size; 1108 break; 1109 } 1110 /* FALLTHROUGH */ 1111 case DW_FORM_addr: 1112 attr->u.val = read_address (unit, info_ptr, info_ptr_end); 1113 info_ptr += unit->addr_size; 1114 break; 1115 case DW_FORM_GNU_ref_alt: 1116 case DW_FORM_sec_offset: 1117 if (unit->offset_size == 4) 1118 attr->u.val = read_4_bytes (unit->abfd, info_ptr, info_ptr_end); 1119 else 1120 attr->u.val = read_8_bytes (unit->abfd, info_ptr, info_ptr_end); 1121 info_ptr += unit->offset_size; 1122 break; 1123 case DW_FORM_block2: 1124 amt = sizeof (struct dwarf_block); 1125 blk = (struct dwarf_block *) bfd_alloc (abfd, amt); 1126 if (blk == NULL) 1127 return NULL; 1128 blk->size = read_2_bytes (abfd, info_ptr, info_ptr_end); 1129 info_ptr += 2; 1130 blk->data = read_n_bytes (abfd, info_ptr, info_ptr_end, blk->size); 1131 info_ptr += blk->size; 1132 attr->u.blk = blk; 1133 break; 1134 case DW_FORM_block4: 1135 amt = sizeof (struct dwarf_block); 1136 blk = (struct dwarf_block *) bfd_alloc (abfd, amt); 1137 if (blk == NULL) 1138 return NULL; 1139 blk->size = read_4_bytes (abfd, info_ptr, info_ptr_end); 1140 info_ptr += 4; 1141 blk->data = read_n_bytes (abfd, info_ptr, info_ptr_end, blk->size); 1142 info_ptr += blk->size; 1143 attr->u.blk = blk; 1144 break; 1145 case DW_FORM_data2: 1146 attr->u.val = read_2_bytes (abfd, info_ptr, info_ptr_end); 1147 info_ptr += 2; 1148 break; 1149 case DW_FORM_data4: 1150 attr->u.val = read_4_bytes (abfd, info_ptr, info_ptr_end); 1151 info_ptr += 4; 1152 break; 1153 case DW_FORM_data8: 1154 attr->u.val = read_8_bytes (abfd, info_ptr, info_ptr_end); 1155 info_ptr += 8; 1156 break; 1157 case DW_FORM_string: 1158 attr->u.str = read_string (abfd, info_ptr, info_ptr_end, &bytes_read); 1159 info_ptr += bytes_read; 1160 break; 1161 case DW_FORM_strp: 1162 attr->u.str = read_indirect_string (unit, info_ptr, info_ptr_end, &bytes_read); 1163 info_ptr += bytes_read; 1164 break; 1165 case DW_FORM_line_strp: 1166 attr->u.str = read_indirect_line_string (unit, info_ptr, info_ptr_end, &bytes_read); 1167 info_ptr += bytes_read; 1168 break; 1169 case DW_FORM_GNU_strp_alt: 1170 attr->u.str = read_alt_indirect_string (unit, info_ptr, info_ptr_end, &bytes_read); 1171 info_ptr += bytes_read; 1172 break; 1173 case DW_FORM_exprloc: 1174 case DW_FORM_block: 1175 amt = sizeof (struct dwarf_block); 1176 blk = (struct dwarf_block *) bfd_alloc (abfd, amt); 1177 if (blk == NULL) 1178 return NULL; 1179 blk->size = _bfd_safe_read_leb128 (abfd, info_ptr, &bytes_read, 1180 FALSE, info_ptr_end); 1181 info_ptr += bytes_read; 1182 blk->data = read_n_bytes (abfd, info_ptr, info_ptr_end, blk->size); 1183 info_ptr += blk->size; 1184 attr->u.blk = blk; 1185 break; 1186 case DW_FORM_block1: 1187 amt = sizeof (struct dwarf_block); 1188 blk = (struct dwarf_block *) bfd_alloc (abfd, amt); 1189 if (blk == NULL) 1190 return NULL; 1191 blk->size = read_1_byte (abfd, info_ptr, info_ptr_end); 1192 info_ptr += 1; 1193 blk->data = read_n_bytes (abfd, info_ptr, info_ptr_end, blk->size); 1194 info_ptr += blk->size; 1195 attr->u.blk = blk; 1196 break; 1197 case DW_FORM_data1: 1198 attr->u.val = read_1_byte (abfd, info_ptr, info_ptr_end); 1199 info_ptr += 1; 1200 break; 1201 case DW_FORM_flag: 1202 attr->u.val = read_1_byte (abfd, info_ptr, info_ptr_end); 1203 info_ptr += 1; 1204 break; 1205 case DW_FORM_flag_present: 1206 attr->u.val = 1; 1207 break; 1208 case DW_FORM_sdata: 1209 attr->u.sval = _bfd_safe_read_leb128 (abfd, info_ptr, &bytes_read, 1210 TRUE, info_ptr_end); 1211 info_ptr += bytes_read; 1212 break; 1213 case DW_FORM_udata: 1214 attr->u.val = _bfd_safe_read_leb128 (abfd, info_ptr, &bytes_read, 1215 FALSE, info_ptr_end); 1216 info_ptr += bytes_read; 1217 break; 1218 case DW_FORM_ref1: 1219 attr->u.val = read_1_byte (abfd, info_ptr, info_ptr_end); 1220 info_ptr += 1; 1221 break; 1222 case DW_FORM_ref2: 1223 attr->u.val = read_2_bytes (abfd, info_ptr, info_ptr_end); 1224 info_ptr += 2; 1225 break; 1226 case DW_FORM_ref4: 1227 attr->u.val = read_4_bytes (abfd, info_ptr, info_ptr_end); 1228 info_ptr += 4; 1229 break; 1230 case DW_FORM_ref8: 1231 attr->u.val = read_8_bytes (abfd, info_ptr, info_ptr_end); 1232 info_ptr += 8; 1233 break; 1234 case DW_FORM_ref_sig8: 1235 attr->u.val = read_8_bytes (abfd, info_ptr, info_ptr_end); 1236 info_ptr += 8; 1237 break; 1238 case DW_FORM_ref_udata: 1239 attr->u.val = _bfd_safe_read_leb128 (abfd, info_ptr, &bytes_read, 1240 FALSE, info_ptr_end); 1241 info_ptr += bytes_read; 1242 break; 1243 case DW_FORM_indirect: 1244 form = _bfd_safe_read_leb128 (abfd, info_ptr, &bytes_read, 1245 FALSE, info_ptr_end); 1246 info_ptr += bytes_read; 1247 if (form == DW_FORM_implicit_const) 1248 { 1249 implicit_const = _bfd_safe_read_leb128 (abfd, info_ptr, &bytes_read, 1250 TRUE, info_ptr_end); 1251 info_ptr += bytes_read; 1252 } 1253 info_ptr = read_attribute_value (attr, form, implicit_const, unit, 1254 info_ptr, info_ptr_end); 1255 break; 1256 case DW_FORM_implicit_const: 1257 attr->form = DW_FORM_sdata; 1258 attr->u.sval = implicit_const; 1259 break; 1260 default: 1261 _bfd_error_handler (_("Dwarf Error: Invalid or unhandled FORM value: %#x."), 1262 form); 1263 bfd_set_error (bfd_error_bad_value); 1264 return NULL; 1265 } 1266 return info_ptr; 1267 } 1268 1269 /* Read an attribute described by an abbreviated attribute. */ 1270 1271 static bfd_byte * 1272 read_attribute (struct attribute * attr, 1273 struct attr_abbrev * abbrev, 1274 struct comp_unit * unit, 1275 bfd_byte * info_ptr, 1276 bfd_byte * info_ptr_end) 1277 { 1278 attr->name = abbrev->name; 1279 info_ptr = read_attribute_value (attr, abbrev->form, abbrev->implicit_const, 1280 unit, info_ptr, info_ptr_end); 1281 return info_ptr; 1282 } 1283 1284 /* Return whether DW_AT_name will return the same as DW_AT_linkage_name 1285 for a function. */ 1286 1287 static bfd_boolean 1288 non_mangled (int lang) 1289 { 1290 switch (lang) 1291 { 1292 default: 1293 return FALSE; 1294 1295 case DW_LANG_C89: 1296 case DW_LANG_C: 1297 case DW_LANG_Ada83: 1298 case DW_LANG_Cobol74: 1299 case DW_LANG_Cobol85: 1300 case DW_LANG_Fortran77: 1301 case DW_LANG_Pascal83: 1302 case DW_LANG_C99: 1303 case DW_LANG_Ada95: 1304 case DW_LANG_PLI: 1305 case DW_LANG_UPC: 1306 case DW_LANG_C11: 1307 return TRUE; 1308 } 1309 } 1310 1311 /* Source line information table routines. */ 1312 1313 #define FILE_ALLOC_CHUNK 5 1314 #define DIR_ALLOC_CHUNK 5 1315 1316 struct line_info 1317 { 1318 struct line_info * prev_line; 1319 bfd_vma address; 1320 char * filename; 1321 unsigned int line; 1322 unsigned int column; 1323 unsigned int discriminator; 1324 unsigned char op_index; 1325 unsigned char end_sequence; /* End of (sequential) code sequence. */ 1326 }; 1327 1328 struct fileinfo 1329 { 1330 char * name; 1331 unsigned int dir; 1332 unsigned int time; 1333 unsigned int size; 1334 }; 1335 1336 struct line_sequence 1337 { 1338 bfd_vma low_pc; 1339 struct line_sequence* prev_sequence; 1340 struct line_info* last_line; /* Largest VMA. */ 1341 struct line_info** line_info_lookup; 1342 bfd_size_type num_lines; 1343 }; 1344 1345 struct line_info_table 1346 { 1347 bfd * abfd; 1348 unsigned int num_files; 1349 unsigned int num_dirs; 1350 unsigned int num_sequences; 1351 char * comp_dir; 1352 char ** dirs; 1353 struct fileinfo* files; 1354 struct line_sequence* sequences; 1355 struct line_info* lcl_head; /* Local head; used in 'add_line_info'. */ 1356 }; 1357 1358 /* Remember some information about each function. If the function is 1359 inlined (DW_TAG_inlined_subroutine) it may have two additional 1360 attributes, DW_AT_call_file and DW_AT_call_line, which specify the 1361 source code location where this function was inlined. */ 1362 1363 struct funcinfo 1364 { 1365 /* Pointer to previous function in list of all functions. */ 1366 struct funcinfo * prev_func; 1367 /* Pointer to function one scope higher. */ 1368 struct funcinfo * caller_func; 1369 /* Source location file name where caller_func inlines this func. */ 1370 char * caller_file; 1371 /* Source location file name. */ 1372 char * file; 1373 /* Source location line number where caller_func inlines this func. */ 1374 int caller_line; 1375 /* Source location line number. */ 1376 int line; 1377 int tag; 1378 bfd_boolean is_linkage; 1379 const char * name; 1380 struct arange arange; 1381 /* Where the symbol is defined. */ 1382 asection * sec; 1383 }; 1384 1385 struct lookup_funcinfo 1386 { 1387 /* Function information corresponding to this lookup table entry. */ 1388 struct funcinfo * funcinfo; 1389 1390 /* The lowest address for this specific function. */ 1391 bfd_vma low_addr; 1392 1393 /* The highest address of this function before the lookup table is sorted. 1394 The highest address of all prior functions after the lookup table is 1395 sorted, which is used for binary search. */ 1396 bfd_vma high_addr; 1397 }; 1398 1399 struct varinfo 1400 { 1401 /* Pointer to previous variable in list of all variables */ 1402 struct varinfo *prev_var; 1403 /* Source location file name */ 1404 char *file; 1405 /* Source location line number */ 1406 int line; 1407 int tag; 1408 char *name; 1409 bfd_vma addr; 1410 /* Where the symbol is defined */ 1411 asection *sec; 1412 /* Is this a stack variable? */ 1413 unsigned int stack: 1; 1414 }; 1415 1416 /* Return TRUE if NEW_LINE should sort after LINE. */ 1417 1418 static inline bfd_boolean 1419 new_line_sorts_after (struct line_info *new_line, struct line_info *line) 1420 { 1421 return (new_line->address > line->address 1422 || (new_line->address == line->address 1423 && new_line->op_index > line->op_index)); 1424 } 1425 1426 1427 /* Adds a new entry to the line_info list in the line_info_table, ensuring 1428 that the list is sorted. Note that the line_info list is sorted from 1429 highest to lowest VMA (with possible duplicates); that is, 1430 line_info->prev_line always accesses an equal or smaller VMA. */ 1431 1432 static bfd_boolean 1433 add_line_info (struct line_info_table *table, 1434 bfd_vma address, 1435 unsigned char op_index, 1436 char *filename, 1437 unsigned int line, 1438 unsigned int column, 1439 unsigned int discriminator, 1440 int end_sequence) 1441 { 1442 bfd_size_type amt = sizeof (struct line_info); 1443 struct line_sequence* seq = table->sequences; 1444 struct line_info* info = (struct line_info *) bfd_alloc (table->abfd, amt); 1445 1446 if (info == NULL) 1447 return FALSE; 1448 1449 /* Set member data of 'info'. */ 1450 info->prev_line = NULL; 1451 info->address = address; 1452 info->op_index = op_index; 1453 info->line = line; 1454 info->column = column; 1455 info->discriminator = discriminator; 1456 info->end_sequence = end_sequence; 1457 1458 if (filename && filename[0]) 1459 { 1460 info->filename = (char *) bfd_alloc (table->abfd, strlen (filename) + 1); 1461 if (info->filename == NULL) 1462 return FALSE; 1463 strcpy (info->filename, filename); 1464 } 1465 else 1466 info->filename = NULL; 1467 1468 /* Find the correct location for 'info'. Normally we will receive 1469 new line_info data 1) in order and 2) with increasing VMAs. 1470 However some compilers break the rules (cf. decode_line_info) and 1471 so we include some heuristics for quickly finding the correct 1472 location for 'info'. In particular, these heuristics optimize for 1473 the common case in which the VMA sequence that we receive is a 1474 list of locally sorted VMAs such as 1475 p...z a...j (where a < j < p < z) 1476 1477 Note: table->lcl_head is used to head an *actual* or *possible* 1478 sub-sequence within the list (such as a...j) that is not directly 1479 headed by table->last_line 1480 1481 Note: we may receive duplicate entries from 'decode_line_info'. */ 1482 1483 if (seq 1484 && seq->last_line->address == address 1485 && seq->last_line->op_index == op_index 1486 && seq->last_line->end_sequence == end_sequence) 1487 { 1488 /* We only keep the last entry with the same address and end 1489 sequence. See PR ld/4986. */ 1490 if (table->lcl_head == seq->last_line) 1491 table->lcl_head = info; 1492 info->prev_line = seq->last_line->prev_line; 1493 seq->last_line = info; 1494 } 1495 else if (!seq || seq->last_line->end_sequence) 1496 { 1497 /* Start a new line sequence. */ 1498 amt = sizeof (struct line_sequence); 1499 seq = (struct line_sequence *) bfd_malloc (amt); 1500 if (seq == NULL) 1501 return FALSE; 1502 seq->low_pc = address; 1503 seq->prev_sequence = table->sequences; 1504 seq->last_line = info; 1505 table->lcl_head = info; 1506 table->sequences = seq; 1507 table->num_sequences++; 1508 } 1509 else if (info->end_sequence 1510 || new_line_sorts_after (info, seq->last_line)) 1511 { 1512 /* Normal case: add 'info' to the beginning of the current sequence. */ 1513 info->prev_line = seq->last_line; 1514 seq->last_line = info; 1515 1516 /* lcl_head: initialize to head a *possible* sequence at the end. */ 1517 if (!table->lcl_head) 1518 table->lcl_head = info; 1519 } 1520 else if (!new_line_sorts_after (info, table->lcl_head) 1521 && (!table->lcl_head->prev_line 1522 || new_line_sorts_after (info, table->lcl_head->prev_line))) 1523 { 1524 /* Abnormal but easy: lcl_head is the head of 'info'. */ 1525 info->prev_line = table->lcl_head->prev_line; 1526 table->lcl_head->prev_line = info; 1527 } 1528 else 1529 { 1530 /* Abnormal and hard: Neither 'last_line' nor 'lcl_head' 1531 are valid heads for 'info'. Reset 'lcl_head'. */ 1532 struct line_info* li2 = seq->last_line; /* Always non-NULL. */ 1533 struct line_info* li1 = li2->prev_line; 1534 1535 while (li1) 1536 { 1537 if (!new_line_sorts_after (info, li2) 1538 && new_line_sorts_after (info, li1)) 1539 break; 1540 1541 li2 = li1; /* always non-NULL */ 1542 li1 = li1->prev_line; 1543 } 1544 table->lcl_head = li2; 1545 info->prev_line = table->lcl_head->prev_line; 1546 table->lcl_head->prev_line = info; 1547 if (address < seq->low_pc) 1548 seq->low_pc = address; 1549 } 1550 return TRUE; 1551 } 1552 1553 /* Extract a fully qualified filename from a line info table. 1554 The returned string has been malloc'ed and it is the caller's 1555 responsibility to free it. */ 1556 1557 static char * 1558 concat_filename (struct line_info_table *table, unsigned int file) 1559 { 1560 char *filename; 1561 1562 if (file - 1 >= table->num_files) 1563 { 1564 /* FILE == 0 means unknown. */ 1565 if (file) 1566 _bfd_error_handler 1567 (_("Dwarf Error: mangled line number section (bad file number).")); 1568 return strdup ("<unknown>"); 1569 } 1570 1571 filename = table->files[file - 1].name; 1572 if (filename == NULL) 1573 return strdup ("<unknown>"); 1574 1575 if (!IS_ABSOLUTE_PATH (filename)) 1576 { 1577 char *dir_name = NULL; 1578 char *subdir_name = NULL; 1579 char *name; 1580 size_t len; 1581 1582 if (table->files[file - 1].dir 1583 /* PR 17512: file: 0317e960. */ 1584 && table->files[file - 1].dir <= table->num_dirs 1585 /* PR 17512: file: 7f3d2e4b. */ 1586 && table->dirs != NULL) 1587 subdir_name = table->dirs[table->files[file - 1].dir - 1]; 1588 1589 if (!subdir_name || !IS_ABSOLUTE_PATH (subdir_name)) 1590 dir_name = table->comp_dir; 1591 1592 if (!dir_name) 1593 { 1594 dir_name = subdir_name; 1595 subdir_name = NULL; 1596 } 1597 1598 if (!dir_name) 1599 return strdup (filename); 1600 1601 len = strlen (dir_name) + strlen (filename) + 2; 1602 1603 if (subdir_name) 1604 { 1605 len += strlen (subdir_name) + 1; 1606 name = (char *) bfd_malloc (len); 1607 if (name) 1608 sprintf (name, "%s/%s/%s", dir_name, subdir_name, filename); 1609 } 1610 else 1611 { 1612 name = (char *) bfd_malloc (len); 1613 if (name) 1614 sprintf (name, "%s/%s", dir_name, filename); 1615 } 1616 1617 return name; 1618 } 1619 1620 return strdup (filename); 1621 } 1622 1623 static bfd_boolean 1624 arange_add (const struct comp_unit *unit, struct arange *first_arange, 1625 bfd_vma low_pc, bfd_vma high_pc) 1626 { 1627 struct arange *arange; 1628 1629 /* Ignore empty ranges. */ 1630 if (low_pc == high_pc) 1631 return TRUE; 1632 1633 /* If the first arange is empty, use it. */ 1634 if (first_arange->high == 0) 1635 { 1636 first_arange->low = low_pc; 1637 first_arange->high = high_pc; 1638 return TRUE; 1639 } 1640 1641 /* Next see if we can cheaply extend an existing range. */ 1642 arange = first_arange; 1643 do 1644 { 1645 if (low_pc == arange->high) 1646 { 1647 arange->high = high_pc; 1648 return TRUE; 1649 } 1650 if (high_pc == arange->low) 1651 { 1652 arange->low = low_pc; 1653 return TRUE; 1654 } 1655 arange = arange->next; 1656 } 1657 while (arange); 1658 1659 /* Need to allocate a new arange and insert it into the arange list. 1660 Order isn't significant, so just insert after the first arange. */ 1661 arange = (struct arange *) bfd_alloc (unit->abfd, sizeof (*arange)); 1662 if (arange == NULL) 1663 return FALSE; 1664 arange->low = low_pc; 1665 arange->high = high_pc; 1666 arange->next = first_arange->next; 1667 first_arange->next = arange; 1668 return TRUE; 1669 } 1670 1671 /* Compare function for line sequences. */ 1672 1673 static int 1674 compare_sequences (const void* a, const void* b) 1675 { 1676 const struct line_sequence* seq1 = a; 1677 const struct line_sequence* seq2 = b; 1678 1679 /* Sort by low_pc as the primary key. */ 1680 if (seq1->low_pc < seq2->low_pc) 1681 return -1; 1682 if (seq1->low_pc > seq2->low_pc) 1683 return 1; 1684 1685 /* If low_pc values are equal, sort in reverse order of 1686 high_pc, so that the largest region comes first. */ 1687 if (seq1->last_line->address < seq2->last_line->address) 1688 return 1; 1689 if (seq1->last_line->address > seq2->last_line->address) 1690 return -1; 1691 1692 if (seq1->last_line->op_index < seq2->last_line->op_index) 1693 return 1; 1694 if (seq1->last_line->op_index > seq2->last_line->op_index) 1695 return -1; 1696 1697 return 0; 1698 } 1699 1700 /* Construct the line information table for quick lookup. */ 1701 1702 static bfd_boolean 1703 build_line_info_table (struct line_info_table * table, 1704 struct line_sequence * seq) 1705 { 1706 bfd_size_type amt; 1707 struct line_info** line_info_lookup; 1708 struct line_info* each_line; 1709 unsigned int num_lines; 1710 unsigned int line_index; 1711 1712 if (seq->line_info_lookup != NULL) 1713 return TRUE; 1714 1715 /* Count the number of line information entries. We could do this while 1716 scanning the debug information, but some entries may be added via 1717 lcl_head without having a sequence handy to increment the number of 1718 lines. */ 1719 num_lines = 0; 1720 for (each_line = seq->last_line; each_line; each_line = each_line->prev_line) 1721 num_lines++; 1722 1723 if (num_lines == 0) 1724 return TRUE; 1725 1726 /* Allocate space for the line information lookup table. */ 1727 amt = sizeof (struct line_info*) * num_lines; 1728 line_info_lookup = (struct line_info**) bfd_alloc (table->abfd, amt); 1729 if (line_info_lookup == NULL) 1730 return FALSE; 1731 1732 /* Create the line information lookup table. */ 1733 line_index = num_lines; 1734 for (each_line = seq->last_line; each_line; each_line = each_line->prev_line) 1735 line_info_lookup[--line_index] = each_line; 1736 1737 BFD_ASSERT (line_index == 0); 1738 1739 seq->num_lines = num_lines; 1740 seq->line_info_lookup = line_info_lookup; 1741 1742 return TRUE; 1743 } 1744 1745 /* Sort the line sequences for quick lookup. */ 1746 1747 static bfd_boolean 1748 sort_line_sequences (struct line_info_table* table) 1749 { 1750 bfd_size_type amt; 1751 struct line_sequence* sequences; 1752 struct line_sequence* seq; 1753 unsigned int n = 0; 1754 unsigned int num_sequences = table->num_sequences; 1755 bfd_vma last_high_pc; 1756 1757 if (num_sequences == 0) 1758 return TRUE; 1759 1760 /* Allocate space for an array of sequences. */ 1761 amt = sizeof (struct line_sequence) * num_sequences; 1762 sequences = (struct line_sequence *) bfd_alloc (table->abfd, amt); 1763 if (sequences == NULL) 1764 return FALSE; 1765 1766 /* Copy the linked list into the array, freeing the original nodes. */ 1767 seq = table->sequences; 1768 for (n = 0; n < num_sequences; n++) 1769 { 1770 struct line_sequence* last_seq = seq; 1771 1772 BFD_ASSERT (seq); 1773 sequences[n].low_pc = seq->low_pc; 1774 sequences[n].prev_sequence = NULL; 1775 sequences[n].last_line = seq->last_line; 1776 sequences[n].line_info_lookup = NULL; 1777 sequences[n].num_lines = 0; 1778 seq = seq->prev_sequence; 1779 free (last_seq); 1780 } 1781 BFD_ASSERT (seq == NULL); 1782 1783 qsort (sequences, n, sizeof (struct line_sequence), compare_sequences); 1784 1785 /* Make the list binary-searchable by trimming overlapping entries 1786 and removing nested entries. */ 1787 num_sequences = 1; 1788 last_high_pc = sequences[0].last_line->address; 1789 for (n = 1; n < table->num_sequences; n++) 1790 { 1791 if (sequences[n].low_pc < last_high_pc) 1792 { 1793 if (sequences[n].last_line->address <= last_high_pc) 1794 /* Skip nested entries. */ 1795 continue; 1796 1797 /* Trim overlapping entries. */ 1798 sequences[n].low_pc = last_high_pc; 1799 } 1800 last_high_pc = sequences[n].last_line->address; 1801 if (n > num_sequences) 1802 { 1803 /* Close up the gap. */ 1804 sequences[num_sequences].low_pc = sequences[n].low_pc; 1805 sequences[num_sequences].last_line = sequences[n].last_line; 1806 } 1807 num_sequences++; 1808 } 1809 1810 table->sequences = sequences; 1811 table->num_sequences = num_sequences; 1812 return TRUE; 1813 } 1814 1815 /* Add directory to TABLE. CUR_DIR memory ownership is taken by TABLE. */ 1816 1817 static bfd_boolean 1818 line_info_add_include_dir (struct line_info_table *table, char *cur_dir) 1819 { 1820 if ((table->num_dirs % DIR_ALLOC_CHUNK) == 0) 1821 { 1822 char **tmp; 1823 bfd_size_type amt; 1824 1825 amt = table->num_dirs + DIR_ALLOC_CHUNK; 1826 amt *= sizeof (char *); 1827 1828 tmp = (char **) bfd_realloc (table->dirs, amt); 1829 if (tmp == NULL) 1830 return FALSE; 1831 table->dirs = tmp; 1832 } 1833 1834 table->dirs[table->num_dirs++] = cur_dir; 1835 return TRUE; 1836 } 1837 1838 static bfd_boolean 1839 line_info_add_include_dir_stub (struct line_info_table *table, char *cur_dir, 1840 unsigned int dir ATTRIBUTE_UNUSED, 1841 unsigned int xtime ATTRIBUTE_UNUSED, 1842 unsigned int size ATTRIBUTE_UNUSED) 1843 { 1844 return line_info_add_include_dir (table, cur_dir); 1845 } 1846 1847 /* Add file to TABLE. CUR_FILE memory ownership is taken by TABLE. */ 1848 1849 static bfd_boolean 1850 line_info_add_file_name (struct line_info_table *table, char *cur_file, 1851 unsigned int dir, unsigned int xtime, 1852 unsigned int size) 1853 { 1854 if ((table->num_files % FILE_ALLOC_CHUNK) == 0) 1855 { 1856 struct fileinfo *tmp; 1857 bfd_size_type amt; 1858 1859 amt = table->num_files + FILE_ALLOC_CHUNK; 1860 amt *= sizeof (struct fileinfo); 1861 1862 tmp = (struct fileinfo *) bfd_realloc (table->files, amt); 1863 if (tmp == NULL) 1864 return FALSE; 1865 table->files = tmp; 1866 } 1867 1868 table->files[table->num_files].name = cur_file; 1869 table->files[table->num_files].dir = dir; 1870 table->files[table->num_files].time = xtime; 1871 table->files[table->num_files].size = size; 1872 table->num_files++; 1873 return TRUE; 1874 } 1875 1876 /* Read directory or file name entry format, starting with byte of 1877 format count entries, ULEB128 pairs of entry formats, ULEB128 of 1878 entries count and the entries themselves in the described entry 1879 format. */ 1880 1881 static bfd_boolean 1882 read_formatted_entries (struct comp_unit *unit, bfd_byte **bufp, 1883 bfd_byte *buf_end, struct line_info_table *table, 1884 bfd_boolean (*callback) (struct line_info_table *table, 1885 char *cur_file, 1886 unsigned int dir, 1887 unsigned int time, 1888 unsigned int size)) 1889 { 1890 bfd *abfd = unit->abfd; 1891 bfd_byte format_count, formati; 1892 bfd_vma data_count, datai; 1893 bfd_byte *buf = *bufp; 1894 bfd_byte *format_header_data; 1895 unsigned int bytes_read; 1896 1897 format_count = read_1_byte (abfd, buf, buf_end); 1898 buf += 1; 1899 format_header_data = buf; 1900 for (formati = 0; formati < format_count; formati++) 1901 { 1902 _bfd_safe_read_leb128 (abfd, buf, &bytes_read, FALSE, buf_end); 1903 buf += bytes_read; 1904 _bfd_safe_read_leb128 (abfd, buf, &bytes_read, FALSE, buf_end); 1905 buf += bytes_read; 1906 } 1907 1908 data_count = _bfd_safe_read_leb128 (abfd, buf, &bytes_read, FALSE, buf_end); 1909 buf += bytes_read; 1910 if (format_count == 0 && data_count != 0) 1911 { 1912 _bfd_error_handler (_("Dwarf Error: Zero format count.")); 1913 bfd_set_error (bfd_error_bad_value); 1914 return FALSE; 1915 } 1916 1917 /* PR 22210. Paranoia check. Don't bother running the loop 1918 if we know that we are going to run out of buffer. */ 1919 if (data_count > (bfd_vma) (buf_end - buf)) 1920 { 1921 _bfd_error_handler (_("Dwarf Error: data count (%Lx) larger than buffer size."), 1922 data_count); 1923 bfd_set_error (bfd_error_bad_value); 1924 return FALSE; 1925 } 1926 1927 for (datai = 0; datai < data_count; datai++) 1928 { 1929 bfd_byte *format = format_header_data; 1930 struct fileinfo fe; 1931 1932 memset (&fe, 0, sizeof fe); 1933 for (formati = 0; formati < format_count; formati++) 1934 { 1935 bfd_vma content_type, form; 1936 char *string_trash; 1937 char **stringp = &string_trash; 1938 unsigned int uint_trash, *uintp = &uint_trash; 1939 struct attribute attr; 1940 1941 content_type = _bfd_safe_read_leb128 (abfd, format, &bytes_read, 1942 FALSE, buf_end); 1943 format += bytes_read; 1944 switch (content_type) 1945 { 1946 case DW_LNCT_path: 1947 stringp = &fe.name; 1948 break; 1949 case DW_LNCT_directory_index: 1950 uintp = &fe.dir; 1951 break; 1952 case DW_LNCT_timestamp: 1953 uintp = &fe.time; 1954 break; 1955 case DW_LNCT_size: 1956 uintp = &fe.size; 1957 break; 1958 case DW_LNCT_MD5: 1959 break; 1960 default: 1961 _bfd_error_handler 1962 (_("Dwarf Error: Unknown format content type %Lu."), 1963 content_type); 1964 bfd_set_error (bfd_error_bad_value); 1965 return FALSE; 1966 } 1967 1968 form = _bfd_safe_read_leb128 (abfd, format, &bytes_read, FALSE, 1969 buf_end); 1970 format += bytes_read; 1971 1972 buf = read_attribute_value (&attr, form, 0, unit, buf, buf_end); 1973 if (buf == NULL) 1974 return FALSE; 1975 switch (form) 1976 { 1977 case DW_FORM_string: 1978 case DW_FORM_line_strp: 1979 *stringp = attr.u.str; 1980 break; 1981 1982 case DW_FORM_data1: 1983 case DW_FORM_data2: 1984 case DW_FORM_data4: 1985 case DW_FORM_data8: 1986 case DW_FORM_udata: 1987 *uintp = attr.u.val; 1988 break; 1989 } 1990 } 1991 1992 if (!callback (table, fe.name, fe.dir, fe.time, fe.size)) 1993 return FALSE; 1994 } 1995 1996 *bufp = buf; 1997 return TRUE; 1998 } 1999 2000 /* Decode the line number information for UNIT. */ 2001 2002 static struct line_info_table* 2003 decode_line_info (struct comp_unit *unit, struct dwarf2_debug *stash) 2004 { 2005 bfd *abfd = unit->abfd; 2006 struct line_info_table* table; 2007 bfd_byte *line_ptr; 2008 bfd_byte *line_end; 2009 struct line_head lh; 2010 unsigned int i, bytes_read, offset_size; 2011 char *cur_file, *cur_dir; 2012 unsigned char op_code, extended_op, adj_opcode; 2013 unsigned int exop_len; 2014 bfd_size_type amt; 2015 2016 if (! read_section (abfd, &stash->debug_sections[debug_line], 2017 stash->syms, unit->line_offset, 2018 &stash->dwarf_line_buffer, &stash->dwarf_line_size)) 2019 return NULL; 2020 2021 amt = sizeof (struct line_info_table); 2022 table = (struct line_info_table *) bfd_alloc (abfd, amt); 2023 if (table == NULL) 2024 return NULL; 2025 table->abfd = abfd; 2026 table->comp_dir = unit->comp_dir; 2027 2028 table->num_files = 0; 2029 table->files = NULL; 2030 2031 table->num_dirs = 0; 2032 table->dirs = NULL; 2033 2034 table->num_sequences = 0; 2035 table->sequences = NULL; 2036 2037 table->lcl_head = NULL; 2038 2039 if (stash->dwarf_line_size < 16) 2040 { 2041 _bfd_error_handler 2042 (_("Dwarf Error: Line info section is too small (%Ld)"), 2043 stash->dwarf_line_size); 2044 bfd_set_error (bfd_error_bad_value); 2045 return NULL; 2046 } 2047 line_ptr = stash->dwarf_line_buffer + unit->line_offset; 2048 line_end = stash->dwarf_line_buffer + stash->dwarf_line_size; 2049 2050 /* Read in the prologue. */ 2051 lh.total_length = read_4_bytes (abfd, line_ptr, line_end); 2052 line_ptr += 4; 2053 offset_size = 4; 2054 if (lh.total_length == 0xffffffff) 2055 { 2056 lh.total_length = read_8_bytes (abfd, line_ptr, line_end); 2057 line_ptr += 8; 2058 offset_size = 8; 2059 } 2060 else if (lh.total_length == 0 && unit->addr_size == 8) 2061 { 2062 /* Handle (non-standard) 64-bit DWARF2 formats. */ 2063 lh.total_length = read_4_bytes (abfd, line_ptr, line_end); 2064 line_ptr += 4; 2065 offset_size = 8; 2066 } 2067 2068 if (lh.total_length > (size_t) (line_end - line_ptr)) 2069 { 2070 _bfd_error_handler 2071 /* xgettext: c-format */ 2072 (_("Dwarf Error: Line info data is bigger (%#Lx)" 2073 " than the space remaining in the section (%#lx)"), 2074 lh.total_length, (unsigned long) (line_end - line_ptr)); 2075 bfd_set_error (bfd_error_bad_value); 2076 return NULL; 2077 } 2078 2079 line_end = line_ptr + lh.total_length; 2080 2081 lh.version = read_2_bytes (abfd, line_ptr, line_end); 2082 if (lh.version < 2 || lh.version > 5) 2083 { 2084 _bfd_error_handler 2085 (_("Dwarf Error: Unhandled .debug_line version %d."), lh.version); 2086 bfd_set_error (bfd_error_bad_value); 2087 return NULL; 2088 } 2089 line_ptr += 2; 2090 2091 if (line_ptr + offset_size + (lh.version >= 5 ? 8 : (lh.version >= 4 ? 6 : 5)) 2092 >= line_end) 2093 { 2094 _bfd_error_handler 2095 (_("Dwarf Error: Ran out of room reading prologue")); 2096 bfd_set_error (bfd_error_bad_value); 2097 return NULL; 2098 } 2099 2100 if (lh.version >= 5) 2101 { 2102 unsigned int segment_selector_size; 2103 2104 /* Skip address size. */ 2105 read_1_byte (abfd, line_ptr, line_end); 2106 line_ptr += 1; 2107 2108 segment_selector_size = read_1_byte (abfd, line_ptr, line_end); 2109 line_ptr += 1; 2110 if (segment_selector_size != 0) 2111 { 2112 _bfd_error_handler 2113 (_("Dwarf Error: Line info unsupported segment selector size %u."), 2114 segment_selector_size); 2115 bfd_set_error (bfd_error_bad_value); 2116 return NULL; 2117 } 2118 } 2119 2120 if (offset_size == 4) 2121 lh.prologue_length = read_4_bytes (abfd, line_ptr, line_end); 2122 else 2123 lh.prologue_length = read_8_bytes (abfd, line_ptr, line_end); 2124 line_ptr += offset_size; 2125 2126 lh.minimum_instruction_length = read_1_byte (abfd, line_ptr, line_end); 2127 line_ptr += 1; 2128 2129 if (lh.version >= 4) 2130 { 2131 lh.maximum_ops_per_insn = read_1_byte (abfd, line_ptr, line_end); 2132 line_ptr += 1; 2133 } 2134 else 2135 lh.maximum_ops_per_insn = 1; 2136 2137 if (lh.maximum_ops_per_insn == 0) 2138 { 2139 _bfd_error_handler 2140 (_("Dwarf Error: Invalid maximum operations per instruction.")); 2141 bfd_set_error (bfd_error_bad_value); 2142 return NULL; 2143 } 2144 2145 lh.default_is_stmt = read_1_byte (abfd, line_ptr, line_end); 2146 line_ptr += 1; 2147 2148 lh.line_base = read_1_signed_byte (abfd, line_ptr, line_end); 2149 line_ptr += 1; 2150 2151 lh.line_range = read_1_byte (abfd, line_ptr, line_end); 2152 line_ptr += 1; 2153 2154 lh.opcode_base = read_1_byte (abfd, line_ptr, line_end); 2155 line_ptr += 1; 2156 2157 if (line_ptr + (lh.opcode_base - 1) >= line_end) 2158 { 2159 _bfd_error_handler (_("Dwarf Error: Ran out of room reading opcodes")); 2160 bfd_set_error (bfd_error_bad_value); 2161 return NULL; 2162 } 2163 2164 amt = lh.opcode_base * sizeof (unsigned char); 2165 lh.standard_opcode_lengths = (unsigned char *) bfd_alloc (abfd, amt); 2166 2167 lh.standard_opcode_lengths[0] = 1; 2168 2169 for (i = 1; i < lh.opcode_base; ++i) 2170 { 2171 lh.standard_opcode_lengths[i] = read_1_byte (abfd, line_ptr, line_end); 2172 line_ptr += 1; 2173 } 2174 2175 if (lh.version >= 5) 2176 { 2177 /* Read directory table. */ 2178 if (!read_formatted_entries (unit, &line_ptr, line_end, table, 2179 line_info_add_include_dir_stub)) 2180 goto fail; 2181 2182 /* Read file name table. */ 2183 if (!read_formatted_entries (unit, &line_ptr, line_end, table, 2184 line_info_add_file_name)) 2185 goto fail; 2186 } 2187 else 2188 { 2189 /* Read directory table. */ 2190 while ((cur_dir = read_string (abfd, line_ptr, line_end, &bytes_read)) != NULL) 2191 { 2192 line_ptr += bytes_read; 2193 2194 if (!line_info_add_include_dir (table, cur_dir)) 2195 goto fail; 2196 } 2197 2198 line_ptr += bytes_read; 2199 2200 /* Read file name table. */ 2201 while ((cur_file = read_string (abfd, line_ptr, line_end, &bytes_read)) != NULL) 2202 { 2203 unsigned int dir, xtime, size; 2204 2205 line_ptr += bytes_read; 2206 2207 dir = _bfd_safe_read_leb128 (abfd, line_ptr, &bytes_read, FALSE, line_end); 2208 line_ptr += bytes_read; 2209 xtime = _bfd_safe_read_leb128 (abfd, line_ptr, &bytes_read, FALSE, line_end); 2210 line_ptr += bytes_read; 2211 size = _bfd_safe_read_leb128 (abfd, line_ptr, &bytes_read, FALSE, line_end); 2212 line_ptr += bytes_read; 2213 2214 if (!line_info_add_file_name (table, cur_file, dir, xtime, size)) 2215 goto fail; 2216 } 2217 2218 line_ptr += bytes_read; 2219 } 2220 2221 /* Read the statement sequences until there's nothing left. */ 2222 while (line_ptr < line_end) 2223 { 2224 /* State machine registers. */ 2225 bfd_vma address = 0; 2226 unsigned char op_index = 0; 2227 char * filename = table->num_files ? concat_filename (table, 1) : NULL; 2228 unsigned int line = 1; 2229 unsigned int column = 0; 2230 unsigned int discriminator = 0; 2231 int is_stmt = lh.default_is_stmt; 2232 int end_sequence = 0; 2233 unsigned int dir, xtime, size; 2234 /* eraxxon@alumni.rice.edu: Against the DWARF2 specs, some 2235 compilers generate address sequences that are wildly out of 2236 order using DW_LNE_set_address (e.g. Intel C++ 6.0 compiler 2237 for ia64-Linux). Thus, to determine the low and high 2238 address, we must compare on every DW_LNS_copy, etc. */ 2239 bfd_vma low_pc = (bfd_vma) -1; 2240 bfd_vma high_pc = 0; 2241 2242 /* Decode the table. */ 2243 while (!end_sequence && line_ptr < line_end) 2244 { 2245 op_code = read_1_byte (abfd, line_ptr, line_end); 2246 line_ptr += 1; 2247 2248 if (op_code >= lh.opcode_base) 2249 { 2250 /* Special operand. */ 2251 adj_opcode = op_code - lh.opcode_base; 2252 if (lh.line_range == 0) 2253 goto line_fail; 2254 if (lh.maximum_ops_per_insn == 1) 2255 address += (adj_opcode / lh.line_range 2256 * lh.minimum_instruction_length); 2257 else 2258 { 2259 address += ((op_index + adj_opcode / lh.line_range) 2260 / lh.maximum_ops_per_insn 2261 * lh.minimum_instruction_length); 2262 op_index = ((op_index + adj_opcode / lh.line_range) 2263 % lh.maximum_ops_per_insn); 2264 } 2265 line += lh.line_base + (adj_opcode % lh.line_range); 2266 /* Append row to matrix using current values. */ 2267 if (!add_line_info (table, address, op_index, filename, 2268 line, column, discriminator, 0)) 2269 goto line_fail; 2270 discriminator = 0; 2271 if (address < low_pc) 2272 low_pc = address; 2273 if (address > high_pc) 2274 high_pc = address; 2275 } 2276 else switch (op_code) 2277 { 2278 case DW_LNS_extended_op: 2279 exop_len = _bfd_safe_read_leb128 (abfd, line_ptr, &bytes_read, 2280 FALSE, line_end); 2281 line_ptr += bytes_read; 2282 extended_op = read_1_byte (abfd, line_ptr, line_end); 2283 line_ptr += 1; 2284 2285 switch (extended_op) 2286 { 2287 case DW_LNE_end_sequence: 2288 end_sequence = 1; 2289 if (!add_line_info (table, address, op_index, filename, line, 2290 column, discriminator, end_sequence)) 2291 goto line_fail; 2292 discriminator = 0; 2293 if (address < low_pc) 2294 low_pc = address; 2295 if (address > high_pc) 2296 high_pc = address; 2297 if (!arange_add (unit, &unit->arange, low_pc, high_pc)) 2298 goto line_fail; 2299 break; 2300 case DW_LNE_set_address: 2301 address = read_address (unit, line_ptr, line_end); 2302 op_index = 0; 2303 line_ptr += unit->addr_size; 2304 break; 2305 case DW_LNE_define_file: 2306 cur_file = read_string (abfd, line_ptr, line_end, &bytes_read); 2307 line_ptr += bytes_read; 2308 dir = _bfd_safe_read_leb128 (abfd, line_ptr, &bytes_read, 2309 FALSE, line_end); 2310 line_ptr += bytes_read; 2311 xtime = _bfd_safe_read_leb128 (abfd, line_ptr, &bytes_read, 2312 FALSE, line_end); 2313 line_ptr += bytes_read; 2314 size = _bfd_safe_read_leb128 (abfd, line_ptr, &bytes_read, 2315 FALSE, line_end); 2316 line_ptr += bytes_read; 2317 if (!line_info_add_file_name (table, cur_file, dir, 2318 xtime, size)) 2319 goto line_fail; 2320 break; 2321 case DW_LNE_set_discriminator: 2322 discriminator = 2323 _bfd_safe_read_leb128 (abfd, line_ptr, &bytes_read, 2324 FALSE, line_end); 2325 line_ptr += bytes_read; 2326 break; 2327 case DW_LNE_HP_source_file_correlation: 2328 line_ptr += exop_len - 1; 2329 break; 2330 default: 2331 _bfd_error_handler 2332 (_("Dwarf Error: mangled line number section.")); 2333 bfd_set_error (bfd_error_bad_value); 2334 line_fail: 2335 if (filename != NULL) 2336 free (filename); 2337 goto fail; 2338 } 2339 break; 2340 case DW_LNS_copy: 2341 if (!add_line_info (table, address, op_index, 2342 filename, line, column, discriminator, 0)) 2343 goto line_fail; 2344 discriminator = 0; 2345 if (address < low_pc) 2346 low_pc = address; 2347 if (address > high_pc) 2348 high_pc = address; 2349 break; 2350 case DW_LNS_advance_pc: 2351 if (lh.maximum_ops_per_insn == 1) 2352 address += (lh.minimum_instruction_length 2353 * _bfd_safe_read_leb128 (abfd, line_ptr, 2354 &bytes_read, 2355 FALSE, line_end)); 2356 else 2357 { 2358 bfd_vma adjust = _bfd_safe_read_leb128 (abfd, line_ptr, 2359 &bytes_read, 2360 FALSE, line_end); 2361 address = ((op_index + adjust) / lh.maximum_ops_per_insn 2362 * lh.minimum_instruction_length); 2363 op_index = (op_index + adjust) % lh.maximum_ops_per_insn; 2364 } 2365 line_ptr += bytes_read; 2366 break; 2367 case DW_LNS_advance_line: 2368 line += _bfd_safe_read_leb128 (abfd, line_ptr, &bytes_read, 2369 TRUE, line_end); 2370 line_ptr += bytes_read; 2371 break; 2372 case DW_LNS_set_file: 2373 { 2374 unsigned int file; 2375 2376 /* The file and directory tables are 0 2377 based, the references are 1 based. */ 2378 file = _bfd_safe_read_leb128 (abfd, line_ptr, &bytes_read, 2379 FALSE, line_end); 2380 line_ptr += bytes_read; 2381 if (filename) 2382 free (filename); 2383 filename = concat_filename (table, file); 2384 break; 2385 } 2386 case DW_LNS_set_column: 2387 column = _bfd_safe_read_leb128 (abfd, line_ptr, &bytes_read, 2388 FALSE, line_end); 2389 line_ptr += bytes_read; 2390 break; 2391 case DW_LNS_negate_stmt: 2392 is_stmt = (!is_stmt); 2393 break; 2394 case DW_LNS_set_basic_block: 2395 break; 2396 case DW_LNS_const_add_pc: 2397 if (lh.line_range == 0) 2398 goto line_fail; 2399 if (lh.maximum_ops_per_insn == 1) 2400 address += (lh.minimum_instruction_length 2401 * ((255 - lh.opcode_base) / lh.line_range)); 2402 else 2403 { 2404 bfd_vma adjust = ((255 - lh.opcode_base) / lh.line_range); 2405 address += (lh.minimum_instruction_length 2406 * ((op_index + adjust) 2407 / lh.maximum_ops_per_insn)); 2408 op_index = (op_index + adjust) % lh.maximum_ops_per_insn; 2409 } 2410 break; 2411 case DW_LNS_fixed_advance_pc: 2412 address += read_2_bytes (abfd, line_ptr, line_end); 2413 op_index = 0; 2414 line_ptr += 2; 2415 break; 2416 default: 2417 /* Unknown standard opcode, ignore it. */ 2418 for (i = 0; i < lh.standard_opcode_lengths[op_code]; i++) 2419 { 2420 (void) _bfd_safe_read_leb128 (abfd, line_ptr, &bytes_read, 2421 FALSE, line_end); 2422 line_ptr += bytes_read; 2423 } 2424 break; 2425 } 2426 } 2427 2428 if (filename) 2429 free (filename); 2430 } 2431 2432 if (sort_line_sequences (table)) 2433 return table; 2434 2435 fail: 2436 while (table->sequences != NULL) 2437 { 2438 struct line_sequence* seq = table->sequences; 2439 table->sequences = table->sequences->prev_sequence; 2440 free (seq); 2441 } 2442 if (table->files != NULL) 2443 free (table->files); 2444 if (table->dirs != NULL) 2445 free (table->dirs); 2446 return NULL; 2447 } 2448 2449 /* If ADDR is within TABLE set the output parameters and return the 2450 range of addresses covered by the entry used to fill them out. 2451 Otherwise set * FILENAME_PTR to NULL and return 0. 2452 The parameters FILENAME_PTR, LINENUMBER_PTR and DISCRIMINATOR_PTR 2453 are pointers to the objects to be filled in. */ 2454 2455 static bfd_vma 2456 lookup_address_in_line_info_table (struct line_info_table *table, 2457 bfd_vma addr, 2458 const char **filename_ptr, 2459 unsigned int *linenumber_ptr, 2460 unsigned int *discriminator_ptr) 2461 { 2462 struct line_sequence *seq = NULL; 2463 struct line_info *info; 2464 int low, high, mid; 2465 2466 /* Binary search the array of sequences. */ 2467 low = 0; 2468 high = table->num_sequences; 2469 while (low < high) 2470 { 2471 mid = (low + high) / 2; 2472 seq = &table->sequences[mid]; 2473 if (addr < seq->low_pc) 2474 high = mid; 2475 else if (addr >= seq->last_line->address) 2476 low = mid + 1; 2477 else 2478 break; 2479 } 2480 2481 /* Check for a valid sequence. */ 2482 if (!seq || addr < seq->low_pc || addr >= seq->last_line->address) 2483 goto fail; 2484 2485 if (!build_line_info_table (table, seq)) 2486 goto fail; 2487 2488 /* Binary search the array of line information. */ 2489 low = 0; 2490 high = seq->num_lines; 2491 info = NULL; 2492 while (low < high) 2493 { 2494 mid = (low + high) / 2; 2495 info = seq->line_info_lookup[mid]; 2496 if (addr < info->address) 2497 high = mid; 2498 else if (addr >= seq->line_info_lookup[mid + 1]->address) 2499 low = mid + 1; 2500 else 2501 break; 2502 } 2503 2504 /* Check for a valid line information entry. */ 2505 if (info 2506 && addr >= info->address 2507 && addr < seq->line_info_lookup[mid + 1]->address 2508 && !(info->end_sequence || info == seq->last_line)) 2509 { 2510 *filename_ptr = info->filename; 2511 *linenumber_ptr = info->line; 2512 if (discriminator_ptr) 2513 *discriminator_ptr = info->discriminator; 2514 return seq->last_line->address - seq->low_pc; 2515 } 2516 2517 fail: 2518 *filename_ptr = NULL; 2519 return 0; 2520 } 2521 2522 /* Read in the .debug_ranges section for future reference. */ 2523 2524 static bfd_boolean 2525 read_debug_ranges (struct comp_unit * unit) 2526 { 2527 struct dwarf2_debug * stash = unit->stash; 2528 2529 return read_section (unit->abfd, &stash->debug_sections[debug_ranges], 2530 stash->syms, 0, 2531 &stash->dwarf_ranges_buffer, 2532 &stash->dwarf_ranges_size); 2533 } 2534 2535 /* Function table functions. */ 2536 2537 static int 2538 compare_lookup_funcinfos (const void * a, const void * b) 2539 { 2540 const struct lookup_funcinfo * lookup1 = a; 2541 const struct lookup_funcinfo * lookup2 = b; 2542 2543 if (lookup1->low_addr < lookup2->low_addr) 2544 return -1; 2545 if (lookup1->low_addr > lookup2->low_addr) 2546 return 1; 2547 if (lookup1->high_addr < lookup2->high_addr) 2548 return -1; 2549 if (lookup1->high_addr > lookup2->high_addr) 2550 return 1; 2551 2552 return 0; 2553 } 2554 2555 static bfd_boolean 2556 build_lookup_funcinfo_table (struct comp_unit * unit) 2557 { 2558 struct lookup_funcinfo *lookup_funcinfo_table = unit->lookup_funcinfo_table; 2559 unsigned int number_of_functions = unit->number_of_functions; 2560 struct funcinfo *each; 2561 struct lookup_funcinfo *entry; 2562 size_t func_index; 2563 struct arange *range; 2564 bfd_vma low_addr, high_addr; 2565 2566 if (lookup_funcinfo_table || number_of_functions == 0) 2567 return TRUE; 2568 2569 /* Create the function info lookup table. */ 2570 lookup_funcinfo_table = (struct lookup_funcinfo *) 2571 bfd_malloc (number_of_functions * sizeof (struct lookup_funcinfo)); 2572 if (lookup_funcinfo_table == NULL) 2573 return FALSE; 2574 2575 /* Populate the function info lookup table. */ 2576 func_index = number_of_functions; 2577 for (each = unit->function_table; each; each = each->prev_func) 2578 { 2579 entry = &lookup_funcinfo_table[--func_index]; 2580 entry->funcinfo = each; 2581 2582 /* Calculate the lowest and highest address for this function entry. */ 2583 low_addr = entry->funcinfo->arange.low; 2584 high_addr = entry->funcinfo->arange.high; 2585 2586 for (range = entry->funcinfo->arange.next; range; range = range->next) 2587 { 2588 if (range->low < low_addr) 2589 low_addr = range->low; 2590 if (range->high > high_addr) 2591 high_addr = range->high; 2592 } 2593 2594 entry->low_addr = low_addr; 2595 entry->high_addr = high_addr; 2596 } 2597 2598 BFD_ASSERT (func_index == 0); 2599 2600 /* Sort the function by address. */ 2601 qsort (lookup_funcinfo_table, 2602 number_of_functions, 2603 sizeof (struct lookup_funcinfo), 2604 compare_lookup_funcinfos); 2605 2606 /* Calculate the high watermark for each function in the lookup table. */ 2607 high_addr = lookup_funcinfo_table[0].high_addr; 2608 for (func_index = 1; func_index < number_of_functions; func_index++) 2609 { 2610 entry = &lookup_funcinfo_table[func_index]; 2611 if (entry->high_addr > high_addr) 2612 high_addr = entry->high_addr; 2613 else 2614 entry->high_addr = high_addr; 2615 } 2616 2617 unit->lookup_funcinfo_table = lookup_funcinfo_table; 2618 return TRUE; 2619 } 2620 2621 /* If ADDR is within UNIT's function tables, set FUNCTION_PTR, and return 2622 TRUE. Note that we need to find the function that has the smallest range 2623 that contains ADDR, to handle inlined functions without depending upon 2624 them being ordered in TABLE by increasing range. */ 2625 2626 static bfd_boolean 2627 lookup_address_in_function_table (struct comp_unit *unit, 2628 bfd_vma addr, 2629 struct funcinfo **function_ptr) 2630 { 2631 unsigned int number_of_functions = unit->number_of_functions; 2632 struct lookup_funcinfo* lookup_funcinfo = NULL; 2633 struct funcinfo* funcinfo = NULL; 2634 struct funcinfo* best_fit = NULL; 2635 bfd_vma best_fit_len = 0; 2636 bfd_size_type low, high, mid, first; 2637 struct arange *arange; 2638 2639 if (number_of_functions == 0) 2640 return FALSE; 2641 2642 if (!build_lookup_funcinfo_table (unit)) 2643 return FALSE; 2644 2645 if (unit->lookup_funcinfo_table[number_of_functions - 1].high_addr < addr) 2646 return FALSE; 2647 2648 /* Find the first function in the lookup table which may contain the 2649 specified address. */ 2650 low = 0; 2651 high = number_of_functions; 2652 first = high; 2653 while (low < high) 2654 { 2655 mid = (low + high) / 2; 2656 lookup_funcinfo = &unit->lookup_funcinfo_table[mid]; 2657 if (addr < lookup_funcinfo->low_addr) 2658 high = mid; 2659 else if (addr >= lookup_funcinfo->high_addr) 2660 low = mid + 1; 2661 else 2662 high = first = mid; 2663 } 2664 2665 /* Find the 'best' match for the address. The prior algorithm defined the 2666 best match as the function with the smallest address range containing 2667 the specified address. This definition should probably be changed to the 2668 innermost inline routine containing the address, but right now we want 2669 to get the same results we did before. */ 2670 while (first < number_of_functions) 2671 { 2672 if (addr < unit->lookup_funcinfo_table[first].low_addr) 2673 break; 2674 funcinfo = unit->lookup_funcinfo_table[first].funcinfo; 2675 2676 for (arange = &funcinfo->arange; arange; arange = arange->next) 2677 { 2678 if (addr < arange->low || addr >= arange->high) 2679 continue; 2680 2681 if (!best_fit 2682 || arange->high - arange->low < best_fit_len 2683 /* The following comparison is designed to return the same 2684 match as the previous algorithm for routines which have the 2685 same best fit length. */ 2686 || (arange->high - arange->low == best_fit_len 2687 && funcinfo > best_fit)) 2688 { 2689 best_fit = funcinfo; 2690 best_fit_len = arange->high - arange->low; 2691 } 2692 } 2693 2694 first++; 2695 } 2696 2697 if (!best_fit) 2698 return FALSE; 2699 2700 *function_ptr = best_fit; 2701 return TRUE; 2702 } 2703 2704 /* If SYM at ADDR is within function table of UNIT, set FILENAME_PTR 2705 and LINENUMBER_PTR, and return TRUE. */ 2706 2707 static bfd_boolean 2708 lookup_symbol_in_function_table (struct comp_unit *unit, 2709 asymbol *sym, 2710 bfd_vma addr, 2711 const char **filename_ptr, 2712 unsigned int *linenumber_ptr) 2713 { 2714 struct funcinfo* each_func; 2715 struct funcinfo* best_fit = NULL; 2716 bfd_vma best_fit_len = 0; 2717 struct arange *arange; 2718 const char *name = bfd_asymbol_name (sym); 2719 asection *sec = bfd_get_section (sym); 2720 2721 for (each_func = unit->function_table; 2722 each_func; 2723 each_func = each_func->prev_func) 2724 { 2725 for (arange = &each_func->arange; 2726 arange; 2727 arange = arange->next) 2728 { 2729 if ((!each_func->sec || each_func->sec == sec) 2730 && addr >= arange->low 2731 && addr < arange->high 2732 && each_func->name 2733 && strcmp (name, each_func->name) == 0 2734 && (!best_fit 2735 || arange->high - arange->low < best_fit_len)) 2736 { 2737 best_fit = each_func; 2738 best_fit_len = arange->high - arange->low; 2739 } 2740 } 2741 } 2742 2743 if (best_fit) 2744 { 2745 best_fit->sec = sec; 2746 *filename_ptr = best_fit->file; 2747 *linenumber_ptr = best_fit->line; 2748 return TRUE; 2749 } 2750 else 2751 return FALSE; 2752 } 2753 2754 /* Variable table functions. */ 2755 2756 /* If SYM is within variable table of UNIT, set FILENAME_PTR and 2757 LINENUMBER_PTR, and return TRUE. */ 2758 2759 static bfd_boolean 2760 lookup_symbol_in_variable_table (struct comp_unit *unit, 2761 asymbol *sym, 2762 bfd_vma addr, 2763 const char **filename_ptr, 2764 unsigned int *linenumber_ptr) 2765 { 2766 const char *name = bfd_asymbol_name (sym); 2767 asection *sec = bfd_get_section (sym); 2768 struct varinfo* each; 2769 2770 for (each = unit->variable_table; each; each = each->prev_var) 2771 if (each->stack == 0 2772 && each->file != NULL 2773 && each->name != NULL 2774 && each->addr == addr 2775 && (!each->sec || each->sec == sec) 2776 && strcmp (name, each->name) == 0) 2777 break; 2778 2779 if (each) 2780 { 2781 each->sec = sec; 2782 *filename_ptr = each->file; 2783 *linenumber_ptr = each->line; 2784 return TRUE; 2785 } 2786 2787 return FALSE; 2788 } 2789 2790 static bfd_boolean 2791 find_abstract_instance_name (struct comp_unit *unit, 2792 bfd_byte *orig_info_ptr, 2793 struct attribute *attr_ptr, 2794 const char **pname, 2795 bfd_boolean *is_linkage) 2796 { 2797 bfd *abfd = unit->abfd; 2798 bfd_byte *info_ptr; 2799 bfd_byte *info_ptr_end; 2800 unsigned int abbrev_number, bytes_read, i; 2801 struct abbrev_info *abbrev; 2802 bfd_uint64_t die_ref = attr_ptr->u.val; 2803 struct attribute attr; 2804 const char *name = NULL; 2805 2806 /* DW_FORM_ref_addr can reference an entry in a different CU. It 2807 is an offset from the .debug_info section, not the current CU. */ 2808 if (attr_ptr->form == DW_FORM_ref_addr) 2809 { 2810 /* We only support DW_FORM_ref_addr within the same file, so 2811 any relocations should be resolved already. Check this by 2812 testing for a zero die_ref; There can't be a valid reference 2813 to the header of a .debug_info section. 2814 DW_FORM_ref_addr is an offset relative to .debug_info. 2815 Normally when using the GNU linker this is accomplished by 2816 emitting a symbolic reference to a label, because .debug_info 2817 sections are linked at zero. When there are multiple section 2818 groups containing .debug_info, as there might be in a 2819 relocatable object file, it would be reasonable to assume that 2820 a symbolic reference to a label in any .debug_info section 2821 might be used. Since we lay out multiple .debug_info 2822 sections at non-zero VMAs (see place_sections), and read 2823 them contiguously into stash->info_ptr_memory, that means 2824 the reference is relative to stash->info_ptr_memory. */ 2825 size_t total; 2826 2827 info_ptr = unit->stash->info_ptr_memory; 2828 info_ptr_end = unit->stash->info_ptr_end; 2829 total = info_ptr_end - info_ptr; 2830 if (!die_ref || die_ref >= total) 2831 { 2832 _bfd_error_handler 2833 (_("Dwarf Error: Invalid abstract instance DIE ref.")); 2834 bfd_set_error (bfd_error_bad_value); 2835 return FALSE; 2836 } 2837 info_ptr += die_ref; 2838 2839 /* Now find the CU containing this pointer. */ 2840 if (info_ptr >= unit->info_ptr_unit && info_ptr < unit->end_ptr) 2841 info_ptr_end = unit->end_ptr; 2842 else 2843 { 2844 /* Check other CUs to see if they contain the abbrev. */ 2845 struct comp_unit * u; 2846 2847 for (u = unit->prev_unit; u != NULL; u = u->prev_unit) 2848 if (info_ptr >= u->info_ptr_unit && info_ptr < u->end_ptr) 2849 break; 2850 2851 if (u == NULL) 2852 for (u = unit->next_unit; u != NULL; u = u->next_unit) 2853 if (info_ptr >= u->info_ptr_unit && info_ptr < u->end_ptr) 2854 break; 2855 2856 if (u) 2857 { 2858 unit = u; 2859 info_ptr_end = unit->end_ptr; 2860 } 2861 /* else FIXME: What do we do now ? */ 2862 } 2863 } 2864 else if (attr_ptr->form == DW_FORM_GNU_ref_alt) 2865 { 2866 info_ptr = read_alt_indirect_ref (unit, die_ref); 2867 if (info_ptr == NULL) 2868 { 2869 _bfd_error_handler 2870 (_("Dwarf Error: Unable to read alt ref %llu."), 2871 (long long) die_ref); 2872 bfd_set_error (bfd_error_bad_value); 2873 return FALSE; 2874 } 2875 info_ptr_end = (unit->stash->alt_dwarf_info_buffer 2876 + unit->stash->alt_dwarf_info_size); 2877 2878 /* FIXME: Do we need to locate the correct CU, in a similar 2879 fashion to the code in the DW_FORM_ref_addr case above ? */ 2880 } 2881 else 2882 { 2883 /* DW_FORM_ref1, DW_FORM_ref2, DW_FORM_ref4, DW_FORM_ref8 or 2884 DW_FORM_ref_udata. These are all references relative to the 2885 start of the current CU. */ 2886 size_t total; 2887 2888 info_ptr = unit->info_ptr_unit; 2889 info_ptr_end = unit->end_ptr; 2890 total = info_ptr_end - info_ptr; 2891 if (!die_ref || die_ref >= total) 2892 { 2893 _bfd_error_handler 2894 (_("Dwarf Error: Invalid abstract instance DIE ref.")); 2895 bfd_set_error (bfd_error_bad_value); 2896 return FALSE; 2897 } 2898 info_ptr += die_ref; 2899 } 2900 2901 abbrev_number = _bfd_safe_read_leb128 (abfd, info_ptr, &bytes_read, 2902 FALSE, info_ptr_end); 2903 info_ptr += bytes_read; 2904 2905 if (abbrev_number) 2906 { 2907 abbrev = lookup_abbrev (abbrev_number, unit->abbrevs); 2908 if (! abbrev) 2909 { 2910 _bfd_error_handler 2911 (_("Dwarf Error: Could not find abbrev number %u."), abbrev_number); 2912 bfd_set_error (bfd_error_bad_value); 2913 return FALSE; 2914 } 2915 else 2916 { 2917 for (i = 0; i < abbrev->num_attrs; ++i) 2918 { 2919 info_ptr = read_attribute (&attr, &abbrev->attrs[i], unit, 2920 info_ptr, info_ptr_end); 2921 if (info_ptr == NULL) 2922 break; 2923 /* It doesn't ever make sense for DW_AT_specification to 2924 refer to the same DIE. Stop simple recursion. */ 2925 if (info_ptr == orig_info_ptr) 2926 { 2927 _bfd_error_handler 2928 (_("Dwarf Error: Abstract instance recursion detected.")); 2929 bfd_set_error (bfd_error_bad_value); 2930 return FALSE; 2931 } 2932 switch (attr.name) 2933 { 2934 case DW_AT_name: 2935 /* Prefer DW_AT_MIPS_linkage_name or DW_AT_linkage_name 2936 over DW_AT_name. */ 2937 if (name == NULL && is_str_attr (attr.form)) 2938 { 2939 name = attr.u.str; 2940 if (non_mangled (unit->lang)) 2941 *is_linkage = TRUE; 2942 } 2943 break; 2944 case DW_AT_specification: 2945 if (!find_abstract_instance_name (unit, info_ptr, &attr, 2946 pname, is_linkage)) 2947 return FALSE; 2948 break; 2949 case DW_AT_linkage_name: 2950 case DW_AT_MIPS_linkage_name: 2951 /* PR 16949: Corrupt debug info can place 2952 non-string forms into these attributes. */ 2953 if (is_str_attr (attr.form)) 2954 { 2955 name = attr.u.str; 2956 *is_linkage = TRUE; 2957 } 2958 break; 2959 default: 2960 break; 2961 } 2962 } 2963 } 2964 } 2965 *pname = name; 2966 return TRUE; 2967 } 2968 2969 static bfd_boolean 2970 read_rangelist (struct comp_unit *unit, struct arange *arange, 2971 bfd_uint64_t offset) 2972 { 2973 bfd_byte *ranges_ptr; 2974 bfd_byte *ranges_end; 2975 bfd_vma base_address = unit->base_address; 2976 2977 if (! unit->stash->dwarf_ranges_buffer) 2978 { 2979 if (! read_debug_ranges (unit)) 2980 return FALSE; 2981 } 2982 2983 ranges_ptr = unit->stash->dwarf_ranges_buffer + offset; 2984 if (ranges_ptr < unit->stash->dwarf_ranges_buffer) 2985 return FALSE; 2986 ranges_end = unit->stash->dwarf_ranges_buffer + unit->stash->dwarf_ranges_size; 2987 2988 for (;;) 2989 { 2990 bfd_vma low_pc; 2991 bfd_vma high_pc; 2992 2993 /* PR 17512: file: 62cada7d. */ 2994 if (ranges_ptr + 2 * unit->addr_size > ranges_end) 2995 return FALSE; 2996 2997 low_pc = read_address (unit, ranges_ptr, ranges_end); 2998 ranges_ptr += unit->addr_size; 2999 high_pc = read_address (unit, ranges_ptr, ranges_end); 3000 ranges_ptr += unit->addr_size; 3001 3002 if (low_pc == 0 && high_pc == 0) 3003 break; 3004 if (low_pc == -1UL && high_pc != -1UL) 3005 base_address = high_pc; 3006 else 3007 { 3008 if (!arange_add (unit, arange, 3009 base_address + low_pc, base_address + high_pc)) 3010 return FALSE; 3011 } 3012 } 3013 return TRUE; 3014 } 3015 3016 /* DWARF2 Compilation unit functions. */ 3017 3018 /* Scan over each die in a comp. unit looking for functions to add 3019 to the function table and variables to the variable table. */ 3020 3021 static bfd_boolean 3022 scan_unit_for_symbols (struct comp_unit *unit) 3023 { 3024 bfd *abfd = unit->abfd; 3025 bfd_byte *info_ptr = unit->first_child_die_ptr; 3026 bfd_byte *info_ptr_end = unit->stash->info_ptr_end; 3027 int nesting_level = 0; 3028 struct nest_funcinfo { 3029 struct funcinfo *func; 3030 } *nested_funcs; 3031 int nested_funcs_size; 3032 3033 /* Maintain a stack of in-scope functions and inlined functions, which we 3034 can use to set the caller_func field. */ 3035 nested_funcs_size = 32; 3036 nested_funcs = (struct nest_funcinfo *) 3037 bfd_malloc (nested_funcs_size * sizeof (*nested_funcs)); 3038 if (nested_funcs == NULL) 3039 return FALSE; 3040 nested_funcs[nesting_level].func = 0; 3041 3042 while (nesting_level >= 0) 3043 { 3044 unsigned int abbrev_number, bytes_read, i; 3045 struct abbrev_info *abbrev; 3046 struct attribute attr; 3047 struct funcinfo *func; 3048 struct varinfo *var; 3049 bfd_vma low_pc = 0; 3050 bfd_vma high_pc = 0; 3051 bfd_boolean high_pc_relative = FALSE; 3052 3053 /* PR 17512: file: 9f405d9d. */ 3054 if (info_ptr >= info_ptr_end) 3055 goto fail; 3056 3057 abbrev_number = _bfd_safe_read_leb128 (abfd, info_ptr, &bytes_read, 3058 FALSE, info_ptr_end); 3059 info_ptr += bytes_read; 3060 3061 if (! abbrev_number) 3062 { 3063 nesting_level--; 3064 continue; 3065 } 3066 3067 abbrev = lookup_abbrev (abbrev_number, unit->abbrevs); 3068 if (! abbrev) 3069 { 3070 static unsigned int previous_failed_abbrev = -1U; 3071 3072 /* Avoid multiple reports of the same missing abbrev. */ 3073 if (abbrev_number != previous_failed_abbrev) 3074 { 3075 _bfd_error_handler 3076 (_("Dwarf Error: Could not find abbrev number %u."), 3077 abbrev_number); 3078 previous_failed_abbrev = abbrev_number; 3079 } 3080 bfd_set_error (bfd_error_bad_value); 3081 goto fail; 3082 } 3083 3084 var = NULL; 3085 if (abbrev->tag == DW_TAG_subprogram 3086 || abbrev->tag == DW_TAG_entry_point 3087 || abbrev->tag == DW_TAG_inlined_subroutine) 3088 { 3089 bfd_size_type amt = sizeof (struct funcinfo); 3090 func = (struct funcinfo *) bfd_zalloc (abfd, amt); 3091 if (func == NULL) 3092 goto fail; 3093 func->tag = abbrev->tag; 3094 func->prev_func = unit->function_table; 3095 unit->function_table = func; 3096 unit->number_of_functions++; 3097 BFD_ASSERT (!unit->cached); 3098 3099 if (func->tag == DW_TAG_inlined_subroutine) 3100 for (i = nesting_level; i-- != 0; ) 3101 if (nested_funcs[i].func) 3102 { 3103 func->caller_func = nested_funcs[i].func; 3104 break; 3105 } 3106 nested_funcs[nesting_level].func = func; 3107 } 3108 else 3109 { 3110 func = NULL; 3111 if (abbrev->tag == DW_TAG_variable) 3112 { 3113 bfd_size_type amt = sizeof (struct varinfo); 3114 var = (struct varinfo *) bfd_zalloc (abfd, amt); 3115 if (var == NULL) 3116 goto fail; 3117 var->tag = abbrev->tag; 3118 var->stack = 1; 3119 var->prev_var = unit->variable_table; 3120 unit->variable_table = var; 3121 /* PR 18205: Missing debug information can cause this 3122 var to be attached to an already cached unit. */ 3123 } 3124 3125 /* No inline function in scope at this nesting level. */ 3126 nested_funcs[nesting_level].func = 0; 3127 } 3128 3129 for (i = 0; i < abbrev->num_attrs; ++i) 3130 { 3131 info_ptr = read_attribute (&attr, &abbrev->attrs[i], 3132 unit, info_ptr, info_ptr_end); 3133 if (info_ptr == NULL) 3134 goto fail; 3135 3136 if (func) 3137 { 3138 switch (attr.name) 3139 { 3140 case DW_AT_call_file: 3141 func->caller_file = concat_filename (unit->line_table, 3142 attr.u.val); 3143 break; 3144 3145 case DW_AT_call_line: 3146 func->caller_line = attr.u.val; 3147 break; 3148 3149 case DW_AT_abstract_origin: 3150 case DW_AT_specification: 3151 if (!find_abstract_instance_name (unit, info_ptr, &attr, 3152 &func->name, 3153 &func->is_linkage)) 3154 goto fail; 3155 break; 3156 3157 case DW_AT_name: 3158 /* Prefer DW_AT_MIPS_linkage_name or DW_AT_linkage_name 3159 over DW_AT_name. */ 3160 if (func->name == NULL && is_str_attr (attr.form)) 3161 { 3162 func->name = attr.u.str; 3163 if (non_mangled (unit->lang)) 3164 func->is_linkage = TRUE; 3165 } 3166 break; 3167 3168 case DW_AT_linkage_name: 3169 case DW_AT_MIPS_linkage_name: 3170 /* PR 16949: Corrupt debug info can place 3171 non-string forms into these attributes. */ 3172 if (is_str_attr (attr.form)) 3173 { 3174 func->name = attr.u.str; 3175 func->is_linkage = TRUE; 3176 } 3177 break; 3178 3179 case DW_AT_low_pc: 3180 low_pc = attr.u.val; 3181 break; 3182 3183 case DW_AT_high_pc: 3184 high_pc = attr.u.val; 3185 high_pc_relative = attr.form != DW_FORM_addr; 3186 break; 3187 3188 case DW_AT_ranges: 3189 if (!read_rangelist (unit, &func->arange, attr.u.val)) 3190 goto fail; 3191 break; 3192 3193 case DW_AT_decl_file: 3194 func->file = concat_filename (unit->line_table, 3195 attr.u.val); 3196 break; 3197 3198 case DW_AT_decl_line: 3199 func->line = attr.u.val; 3200 break; 3201 3202 default: 3203 break; 3204 } 3205 } 3206 else if (var) 3207 { 3208 switch (attr.name) 3209 { 3210 case DW_AT_name: 3211 if (is_str_attr (attr.form)) 3212 var->name = attr.u.str; 3213 break; 3214 3215 case DW_AT_decl_file: 3216 var->file = concat_filename (unit->line_table, 3217 attr.u.val); 3218 break; 3219 3220 case DW_AT_decl_line: 3221 var->line = attr.u.val; 3222 break; 3223 3224 case DW_AT_external: 3225 if (attr.u.val != 0) 3226 var->stack = 0; 3227 break; 3228 3229 case DW_AT_location: 3230 switch (attr.form) 3231 { 3232 case DW_FORM_block: 3233 case DW_FORM_block1: 3234 case DW_FORM_block2: 3235 case DW_FORM_block4: 3236 case DW_FORM_exprloc: 3237 if (attr.u.blk->data != NULL 3238 && *attr.u.blk->data == DW_OP_addr) 3239 { 3240 var->stack = 0; 3241 3242 /* Verify that DW_OP_addr is the only opcode in the 3243 location, in which case the block size will be 1 3244 plus the address size. */ 3245 /* ??? For TLS variables, gcc can emit 3246 DW_OP_addr <addr> DW_OP_GNU_push_tls_address 3247 which we don't handle here yet. */ 3248 if (attr.u.blk->size == unit->addr_size + 1U) 3249 var->addr = bfd_get (unit->addr_size * 8, 3250 unit->abfd, 3251 attr.u.blk->data + 1); 3252 } 3253 break; 3254 3255 default: 3256 break; 3257 } 3258 break; 3259 3260 default: 3261 break; 3262 } 3263 } 3264 } 3265 3266 if (high_pc_relative) 3267 high_pc += low_pc; 3268 3269 if (func && high_pc != 0) 3270 { 3271 if (!arange_add (unit, &func->arange, low_pc, high_pc)) 3272 goto fail; 3273 } 3274 3275 if (abbrev->has_children) 3276 { 3277 nesting_level++; 3278 3279 if (nesting_level >= nested_funcs_size) 3280 { 3281 struct nest_funcinfo *tmp; 3282 3283 nested_funcs_size *= 2; 3284 tmp = (struct nest_funcinfo *) 3285 bfd_realloc (nested_funcs, 3286 nested_funcs_size * sizeof (*nested_funcs)); 3287 if (tmp == NULL) 3288 goto fail; 3289 nested_funcs = tmp; 3290 } 3291 nested_funcs[nesting_level].func = 0; 3292 } 3293 } 3294 3295 free (nested_funcs); 3296 return TRUE; 3297 3298 fail: 3299 free (nested_funcs); 3300 return FALSE; 3301 } 3302 3303 /* Parse a DWARF2 compilation unit starting at INFO_PTR. This 3304 includes the compilation unit header that proceeds the DIE's, but 3305 does not include the length field that precedes each compilation 3306 unit header. END_PTR points one past the end of this comp unit. 3307 OFFSET_SIZE is the size of DWARF2 offsets (either 4 or 8 bytes). 3308 3309 This routine does not read the whole compilation unit; only enough 3310 to get to the line number information for the compilation unit. */ 3311 3312 static struct comp_unit * 3313 parse_comp_unit (struct dwarf2_debug *stash, 3314 bfd_vma unit_length, 3315 bfd_byte *info_ptr_unit, 3316 unsigned int offset_size) 3317 { 3318 struct comp_unit* unit; 3319 unsigned int version; 3320 bfd_uint64_t abbrev_offset = 0; 3321 /* Initialize it just to avoid a GCC false warning. */ 3322 unsigned int addr_size = -1; 3323 struct abbrev_info** abbrevs; 3324 unsigned int abbrev_number, bytes_read, i; 3325 struct abbrev_info *abbrev; 3326 struct attribute attr; 3327 bfd_byte *info_ptr = stash->info_ptr; 3328 bfd_byte *end_ptr = info_ptr + unit_length; 3329 bfd_size_type amt; 3330 bfd_vma low_pc = 0; 3331 bfd_vma high_pc = 0; 3332 bfd *abfd = stash->bfd_ptr; 3333 bfd_boolean high_pc_relative = FALSE; 3334 enum dwarf_unit_type unit_type; 3335 3336 version = read_2_bytes (abfd, info_ptr, end_ptr); 3337 info_ptr += 2; 3338 if (version < 2 || version > 5) 3339 { 3340 /* PR 19872: A version number of 0 probably means that there is padding 3341 at the end of the .debug_info section. Gold puts it there when 3342 performing an incremental link, for example. So do not generate 3343 an error, just return a NULL. */ 3344 if (version) 3345 { 3346 _bfd_error_handler 3347 (_("Dwarf Error: found dwarf version '%u', this reader" 3348 " only handles version 2, 3, 4 and 5 information."), version); 3349 bfd_set_error (bfd_error_bad_value); 3350 } 3351 return NULL; 3352 } 3353 3354 if (version < 5) 3355 unit_type = DW_UT_compile; 3356 else 3357 { 3358 unit_type = read_1_byte (abfd, info_ptr, end_ptr); 3359 info_ptr += 1; 3360 3361 addr_size = read_1_byte (abfd, info_ptr, end_ptr); 3362 info_ptr += 1; 3363 } 3364 3365 BFD_ASSERT (offset_size == 4 || offset_size == 8); 3366 if (offset_size == 4) 3367 abbrev_offset = read_4_bytes (abfd, info_ptr, end_ptr); 3368 else 3369 abbrev_offset = read_8_bytes (abfd, info_ptr, end_ptr); 3370 info_ptr += offset_size; 3371 3372 if (version < 5) 3373 { 3374 addr_size = read_1_byte (abfd, info_ptr, end_ptr); 3375 info_ptr += 1; 3376 } 3377 3378 if (unit_type == DW_UT_type) 3379 { 3380 /* Skip type signature. */ 3381 info_ptr += 8; 3382 3383 /* Skip type offset. */ 3384 info_ptr += offset_size; 3385 } 3386 3387 if (addr_size > sizeof (bfd_vma)) 3388 { 3389 _bfd_error_handler 3390 /* xgettext: c-format */ 3391 (_("Dwarf Error: found address size '%u', this reader" 3392 " can not handle sizes greater than '%u'."), 3393 addr_size, 3394 (unsigned int) sizeof (bfd_vma)); 3395 bfd_set_error (bfd_error_bad_value); 3396 return NULL; 3397 } 3398 3399 if (addr_size != 2 && addr_size != 4 && addr_size != 8) 3400 { 3401 _bfd_error_handler 3402 ("Dwarf Error: found address size '%u', this reader" 3403 " can only handle address sizes '2', '4' and '8'.", addr_size); 3404 bfd_set_error (bfd_error_bad_value); 3405 return NULL; 3406 } 3407 3408 /* Read the abbrevs for this compilation unit into a table. */ 3409 abbrevs = read_abbrevs (abfd, abbrev_offset, stash); 3410 if (! abbrevs) 3411 return NULL; 3412 3413 abbrev_number = _bfd_safe_read_leb128 (abfd, info_ptr, &bytes_read, 3414 FALSE, end_ptr); 3415 info_ptr += bytes_read; 3416 if (! abbrev_number) 3417 { 3418 /* PR 19872: An abbrev number of 0 probably means that there is padding 3419 at the end of the .debug_abbrev section. Gold puts it there when 3420 performing an incremental link, for example. So do not generate 3421 an error, just return a NULL. */ 3422 return NULL; 3423 } 3424 3425 abbrev = lookup_abbrev (abbrev_number, abbrevs); 3426 if (! abbrev) 3427 { 3428 _bfd_error_handler (_("Dwarf Error: Could not find abbrev number %u."), 3429 abbrev_number); 3430 bfd_set_error (bfd_error_bad_value); 3431 return NULL; 3432 } 3433 3434 amt = sizeof (struct comp_unit); 3435 unit = (struct comp_unit *) bfd_zalloc (abfd, amt); 3436 if (unit == NULL) 3437 return NULL; 3438 unit->abfd = abfd; 3439 unit->version = version; 3440 unit->addr_size = addr_size; 3441 unit->offset_size = offset_size; 3442 unit->abbrevs = abbrevs; 3443 unit->end_ptr = end_ptr; 3444 unit->stash = stash; 3445 unit->info_ptr_unit = info_ptr_unit; 3446 3447 for (i = 0; i < abbrev->num_attrs; ++i) 3448 { 3449 info_ptr = read_attribute (&attr, &abbrev->attrs[i], unit, info_ptr, end_ptr); 3450 if (info_ptr == NULL) 3451 return NULL; 3452 3453 /* Store the data if it is of an attribute we want to keep in a 3454 partial symbol table. */ 3455 switch (attr.name) 3456 { 3457 case DW_AT_stmt_list: 3458 unit->stmtlist = 1; 3459 unit->line_offset = attr.u.val; 3460 break; 3461 3462 case DW_AT_name: 3463 if (is_str_attr (attr.form)) 3464 unit->name = attr.u.str; 3465 break; 3466 3467 case DW_AT_low_pc: 3468 low_pc = attr.u.val; 3469 /* If the compilation unit DIE has a DW_AT_low_pc attribute, 3470 this is the base address to use when reading location 3471 lists or range lists. */ 3472 if (abbrev->tag == DW_TAG_compile_unit) 3473 unit->base_address = low_pc; 3474 break; 3475 3476 case DW_AT_high_pc: 3477 high_pc = attr.u.val; 3478 high_pc_relative = attr.form != DW_FORM_addr; 3479 break; 3480 3481 case DW_AT_ranges: 3482 if (!read_rangelist (unit, &unit->arange, attr.u.val)) 3483 return NULL; 3484 break; 3485 3486 case DW_AT_comp_dir: 3487 { 3488 char *comp_dir = attr.u.str; 3489 3490 /* PR 17512: file: 1fe726be. */ 3491 if (! is_str_attr (attr.form)) 3492 { 3493 _bfd_error_handler 3494 (_("Dwarf Error: DW_AT_comp_dir attribute encountered with a non-string form.")); 3495 comp_dir = NULL; 3496 } 3497 3498 if (comp_dir) 3499 { 3500 /* Irix 6.2 native cc prepends <machine>.: to the compilation 3501 directory, get rid of it. */ 3502 char *cp = strchr (comp_dir, ':'); 3503 3504 if (cp && cp != comp_dir && cp[-1] == '.' && cp[1] == '/') 3505 comp_dir = cp + 1; 3506 } 3507 unit->comp_dir = comp_dir; 3508 break; 3509 } 3510 3511 case DW_AT_language: 3512 unit->lang = attr.u.val; 3513 break; 3514 3515 default: 3516 break; 3517 } 3518 } 3519 if (high_pc_relative) 3520 high_pc += low_pc; 3521 if (high_pc != 0) 3522 { 3523 if (!arange_add (unit, &unit->arange, low_pc, high_pc)) 3524 return NULL; 3525 } 3526 3527 unit->first_child_die_ptr = info_ptr; 3528 return unit; 3529 } 3530 3531 /* Return TRUE if UNIT may contain the address given by ADDR. When 3532 there are functions written entirely with inline asm statements, the 3533 range info in the compilation unit header may not be correct. We 3534 need to consult the line info table to see if a compilation unit 3535 really contains the given address. */ 3536 3537 static bfd_boolean 3538 comp_unit_contains_address (struct comp_unit *unit, bfd_vma addr) 3539 { 3540 struct arange *arange; 3541 3542 if (unit->error) 3543 return FALSE; 3544 3545 arange = &unit->arange; 3546 do 3547 { 3548 if (addr >= arange->low && addr < arange->high) 3549 return TRUE; 3550 arange = arange->next; 3551 } 3552 while (arange); 3553 3554 return FALSE; 3555 } 3556 3557 /* If UNIT contains ADDR, set the output parameters to the values for 3558 the line containing ADDR. The output parameters, FILENAME_PTR, 3559 FUNCTION_PTR, and LINENUMBER_PTR, are pointers to the objects 3560 to be filled in. 3561 3562 Returns the range of addresses covered by the entry that was used 3563 to fill in *LINENUMBER_PTR or 0 if it was not filled in. */ 3564 3565 static bfd_vma 3566 comp_unit_find_nearest_line (struct comp_unit *unit, 3567 bfd_vma addr, 3568 const char **filename_ptr, 3569 struct funcinfo **function_ptr, 3570 unsigned int *linenumber_ptr, 3571 unsigned int *discriminator_ptr, 3572 struct dwarf2_debug *stash) 3573 { 3574 bfd_boolean func_p; 3575 3576 if (unit->error) 3577 return FALSE; 3578 3579 if (! unit->line_table) 3580 { 3581 if (! unit->stmtlist) 3582 { 3583 unit->error = 1; 3584 return FALSE; 3585 } 3586 3587 unit->line_table = decode_line_info (unit, stash); 3588 3589 if (! unit->line_table) 3590 { 3591 unit->error = 1; 3592 return FALSE; 3593 } 3594 3595 if (unit->first_child_die_ptr < unit->end_ptr 3596 && ! scan_unit_for_symbols (unit)) 3597 { 3598 unit->error = 1; 3599 return FALSE; 3600 } 3601 } 3602 3603 *function_ptr = NULL; 3604 func_p = lookup_address_in_function_table (unit, addr, function_ptr); 3605 if (func_p && (*function_ptr)->tag == DW_TAG_inlined_subroutine) 3606 stash->inliner_chain = *function_ptr; 3607 3608 return lookup_address_in_line_info_table (unit->line_table, addr, 3609 filename_ptr, 3610 linenumber_ptr, 3611 discriminator_ptr); 3612 } 3613 3614 /* Check to see if line info is already decoded in a comp_unit. 3615 If not, decode it. Returns TRUE if no errors were encountered; 3616 FALSE otherwise. */ 3617 3618 static bfd_boolean 3619 comp_unit_maybe_decode_line_info (struct comp_unit *unit, 3620 struct dwarf2_debug *stash) 3621 { 3622 if (unit->error) 3623 return FALSE; 3624 3625 if (! unit->line_table) 3626 { 3627 if (! unit->stmtlist) 3628 { 3629 unit->error = 1; 3630 return FALSE; 3631 } 3632 3633 unit->line_table = decode_line_info (unit, stash); 3634 3635 if (! unit->line_table) 3636 { 3637 unit->error = 1; 3638 return FALSE; 3639 } 3640 3641 if (unit->first_child_die_ptr < unit->end_ptr 3642 && ! scan_unit_for_symbols (unit)) 3643 { 3644 unit->error = 1; 3645 return FALSE; 3646 } 3647 } 3648 3649 return TRUE; 3650 } 3651 3652 /* If UNIT contains SYM at ADDR, set the output parameters to the 3653 values for the line containing SYM. The output parameters, 3654 FILENAME_PTR, and LINENUMBER_PTR, are pointers to the objects to be 3655 filled in. 3656 3657 Return TRUE if UNIT contains SYM, and no errors were encountered; 3658 FALSE otherwise. */ 3659 3660 static bfd_boolean 3661 comp_unit_find_line (struct comp_unit *unit, 3662 asymbol *sym, 3663 bfd_vma addr, 3664 const char **filename_ptr, 3665 unsigned int *linenumber_ptr, 3666 struct dwarf2_debug *stash) 3667 { 3668 if (!comp_unit_maybe_decode_line_info (unit, stash)) 3669 return FALSE; 3670 3671 if (sym->flags & BSF_FUNCTION) 3672 return lookup_symbol_in_function_table (unit, sym, addr, 3673 filename_ptr, 3674 linenumber_ptr); 3675 3676 return lookup_symbol_in_variable_table (unit, sym, addr, 3677 filename_ptr, 3678 linenumber_ptr); 3679 } 3680 3681 static struct funcinfo * 3682 reverse_funcinfo_list (struct funcinfo *head) 3683 { 3684 struct funcinfo *rhead; 3685 struct funcinfo *temp; 3686 3687 for (rhead = NULL; head; head = temp) 3688 { 3689 temp = head->prev_func; 3690 head->prev_func = rhead; 3691 rhead = head; 3692 } 3693 return rhead; 3694 } 3695 3696 static struct varinfo * 3697 reverse_varinfo_list (struct varinfo *head) 3698 { 3699 struct varinfo *rhead; 3700 struct varinfo *temp; 3701 3702 for (rhead = NULL; head; head = temp) 3703 { 3704 temp = head->prev_var; 3705 head->prev_var = rhead; 3706 rhead = head; 3707 } 3708 return rhead; 3709 } 3710 3711 /* Extract all interesting funcinfos and varinfos of a compilation 3712 unit into hash tables for faster lookup. Returns TRUE if no 3713 errors were enountered; FALSE otherwise. */ 3714 3715 static bfd_boolean 3716 comp_unit_hash_info (struct dwarf2_debug *stash, 3717 struct comp_unit *unit, 3718 struct info_hash_table *funcinfo_hash_table, 3719 struct info_hash_table *varinfo_hash_table) 3720 { 3721 struct funcinfo* each_func; 3722 struct varinfo* each_var; 3723 bfd_boolean okay = TRUE; 3724 3725 BFD_ASSERT (stash->info_hash_status != STASH_INFO_HASH_DISABLED); 3726 3727 if (!comp_unit_maybe_decode_line_info (unit, stash)) 3728 return FALSE; 3729 3730 BFD_ASSERT (!unit->cached); 3731 3732 /* To preserve the original search order, we went to visit the function 3733 infos in the reversed order of the list. However, making the list 3734 bi-directional use quite a bit of extra memory. So we reverse 3735 the list first, traverse the list in the now reversed order and 3736 finally reverse the list again to get back the original order. */ 3737 unit->function_table = reverse_funcinfo_list (unit->function_table); 3738 for (each_func = unit->function_table; 3739 each_func && okay; 3740 each_func = each_func->prev_func) 3741 { 3742 /* Skip nameless functions. */ 3743 if (each_func->name) 3744 /* There is no need to copy name string into hash table as 3745 name string is either in the dwarf string buffer or 3746 info in the stash. */ 3747 okay = insert_info_hash_table (funcinfo_hash_table, each_func->name, 3748 (void*) each_func, FALSE); 3749 } 3750 unit->function_table = reverse_funcinfo_list (unit->function_table); 3751 if (!okay) 3752 return FALSE; 3753 3754 /* We do the same for variable infos. */ 3755 unit->variable_table = reverse_varinfo_list (unit->variable_table); 3756 for (each_var = unit->variable_table; 3757 each_var && okay; 3758 each_var = each_var->prev_var) 3759 { 3760 /* Skip stack vars and vars with no files or names. */ 3761 if (each_var->stack == 0 3762 && each_var->file != NULL 3763 && each_var->name != NULL) 3764 /* There is no need to copy name string into hash table as 3765 name string is either in the dwarf string buffer or 3766 info in the stash. */ 3767 okay = insert_info_hash_table (varinfo_hash_table, each_var->name, 3768 (void*) each_var, FALSE); 3769 } 3770 3771 unit->variable_table = reverse_varinfo_list (unit->variable_table); 3772 unit->cached = TRUE; 3773 return okay; 3774 } 3775 3776 /* Locate a section in a BFD containing debugging info. The search starts 3777 from the section after AFTER_SEC, or from the first section in the BFD if 3778 AFTER_SEC is NULL. The search works by examining the names of the 3779 sections. There are three permissiable names. The first two are given 3780 by DEBUG_SECTIONS[debug_info] (whose standard DWARF2 names are .debug_info 3781 and .zdebug_info). The third is a prefix .gnu.linkonce.wi. 3782 This is a variation on the .debug_info section which has a checksum 3783 describing the contents appended onto the name. This allows the linker to 3784 identify and discard duplicate debugging sections for different 3785 compilation units. */ 3786 #define GNU_LINKONCE_INFO ".gnu.linkonce.wi." 3787 3788 static asection * 3789 find_debug_info (bfd *abfd, const struct dwarf_debug_section *debug_sections, 3790 asection *after_sec) 3791 { 3792 asection *msec; 3793 const char *look; 3794 3795 if (after_sec == NULL) 3796 { 3797 look = debug_sections[debug_info].uncompressed_name; 3798 msec = bfd_get_section_by_name (abfd, look); 3799 if (msec != NULL) 3800 return msec; 3801 3802 look = debug_sections[debug_info].compressed_name; 3803 if (look != NULL) 3804 { 3805 msec = bfd_get_section_by_name (abfd, look); 3806 if (msec != NULL) 3807 return msec; 3808 } 3809 3810 for (msec = abfd->sections; msec != NULL; msec = msec->next) 3811 if (CONST_STRNEQ (msec->name, GNU_LINKONCE_INFO)) 3812 return msec; 3813 3814 return NULL; 3815 } 3816 3817 for (msec = after_sec->next; msec != NULL; msec = msec->next) 3818 { 3819 look = debug_sections[debug_info].uncompressed_name; 3820 if (strcmp (msec->name, look) == 0) 3821 return msec; 3822 3823 look = debug_sections[debug_info].compressed_name; 3824 if (look != NULL && strcmp (msec->name, look) == 0) 3825 return msec; 3826 3827 if (CONST_STRNEQ (msec->name, GNU_LINKONCE_INFO)) 3828 return msec; 3829 } 3830 3831 return NULL; 3832 } 3833 3834 /* Transfer VMAs from object file to separate debug file. */ 3835 3836 static void 3837 set_debug_vma (bfd *orig_bfd, bfd *debug_bfd) 3838 { 3839 asection *s, *d; 3840 3841 for (s = orig_bfd->sections, d = debug_bfd->sections; 3842 s != NULL && d != NULL; 3843 s = s->next, d = d->next) 3844 { 3845 if ((d->flags & SEC_DEBUGGING) != 0) 3846 break; 3847 /* ??? Assumes 1-1 correspondence between sections in the 3848 two files. */ 3849 if (strcmp (s->name, d->name) == 0) 3850 { 3851 d->output_section = s->output_section; 3852 d->output_offset = s->output_offset; 3853 d->vma = s->vma; 3854 } 3855 } 3856 } 3857 3858 /* Unset vmas for adjusted sections in STASH. */ 3859 3860 static void 3861 unset_sections (struct dwarf2_debug *stash) 3862 { 3863 int i; 3864 struct adjusted_section *p; 3865 3866 i = stash->adjusted_section_count; 3867 p = stash->adjusted_sections; 3868 for (; i > 0; i--, p++) 3869 p->section->vma = 0; 3870 } 3871 3872 /* Set VMAs for allocated and .debug_info sections in ORIG_BFD, a 3873 relocatable object file. VMAs are normally all zero in relocatable 3874 object files, so if we want to distinguish locations in sections by 3875 address we need to set VMAs so the sections do not overlap. We 3876 also set VMA on .debug_info so that when we have multiple 3877 .debug_info sections (or the linkonce variant) they also do not 3878 overlap. The multiple .debug_info sections make up a single 3879 logical section. ??? We should probably do the same for other 3880 debug sections. */ 3881 3882 static bfd_boolean 3883 place_sections (bfd *orig_bfd, struct dwarf2_debug *stash) 3884 { 3885 bfd *abfd; 3886 struct adjusted_section *p; 3887 int i; 3888 const char *debug_info_name; 3889 3890 if (stash->adjusted_section_count != 0) 3891 { 3892 i = stash->adjusted_section_count; 3893 p = stash->adjusted_sections; 3894 for (; i > 0; i--, p++) 3895 p->section->vma = p->adj_vma; 3896 return TRUE; 3897 } 3898 3899 debug_info_name = stash->debug_sections[debug_info].uncompressed_name; 3900 i = 0; 3901 abfd = orig_bfd; 3902 while (1) 3903 { 3904 asection *sect; 3905 3906 for (sect = abfd->sections; sect != NULL; sect = sect->next) 3907 { 3908 int is_debug_info; 3909 3910 if ((sect->output_section != NULL 3911 && sect->output_section != sect 3912 && (sect->flags & SEC_DEBUGGING) == 0) 3913 || sect->vma != 0) 3914 continue; 3915 3916 is_debug_info = (strcmp (sect->name, debug_info_name) == 0 3917 || CONST_STRNEQ (sect->name, GNU_LINKONCE_INFO)); 3918 3919 if (!((sect->flags & SEC_ALLOC) != 0 && abfd == orig_bfd) 3920 && !is_debug_info) 3921 continue; 3922 3923 i++; 3924 } 3925 if (abfd == stash->bfd_ptr) 3926 break; 3927 abfd = stash->bfd_ptr; 3928 } 3929 3930 if (i <= 1) 3931 stash->adjusted_section_count = -1; 3932 else 3933 { 3934 bfd_vma last_vma = 0, last_dwarf = 0; 3935 bfd_size_type amt = i * sizeof (struct adjusted_section); 3936 3937 p = (struct adjusted_section *) bfd_malloc (amt); 3938 if (p == NULL) 3939 return FALSE; 3940 3941 stash->adjusted_sections = p; 3942 stash->adjusted_section_count = i; 3943 3944 abfd = orig_bfd; 3945 while (1) 3946 { 3947 asection *sect; 3948 3949 for (sect = abfd->sections; sect != NULL; sect = sect->next) 3950 { 3951 bfd_size_type sz; 3952 int is_debug_info; 3953 3954 if ((sect->output_section != NULL 3955 && sect->output_section != sect 3956 && (sect->flags & SEC_DEBUGGING) == 0) 3957 || sect->vma != 0) 3958 continue; 3959 3960 is_debug_info = (strcmp (sect->name, debug_info_name) == 0 3961 || CONST_STRNEQ (sect->name, GNU_LINKONCE_INFO)); 3962 3963 if (!((sect->flags & SEC_ALLOC) != 0 && abfd == orig_bfd) 3964 && !is_debug_info) 3965 continue; 3966 3967 sz = sect->rawsize ? sect->rawsize : sect->size; 3968 3969 if (is_debug_info) 3970 { 3971 BFD_ASSERT (sect->alignment_power == 0); 3972 sect->vma = last_dwarf; 3973 last_dwarf += sz; 3974 } 3975 else 3976 { 3977 /* Align the new address to the current section 3978 alignment. */ 3979 last_vma = ((last_vma 3980 + ~(-((bfd_vma) 1 << sect->alignment_power))) 3981 & (-((bfd_vma) 1 << sect->alignment_power))); 3982 sect->vma = last_vma; 3983 last_vma += sz; 3984 } 3985 3986 p->section = sect; 3987 p->adj_vma = sect->vma; 3988 p++; 3989 } 3990 if (abfd == stash->bfd_ptr) 3991 break; 3992 abfd = stash->bfd_ptr; 3993 } 3994 } 3995 3996 if (orig_bfd != stash->bfd_ptr) 3997 set_debug_vma (orig_bfd, stash->bfd_ptr); 3998 3999 return TRUE; 4000 } 4001 4002 /* Look up a funcinfo by name using the given info hash table. If found, 4003 also update the locations pointed to by filename_ptr and linenumber_ptr. 4004 4005 This function returns TRUE if a funcinfo that matches the given symbol 4006 and address is found with any error; otherwise it returns FALSE. */ 4007 4008 static bfd_boolean 4009 info_hash_lookup_funcinfo (struct info_hash_table *hash_table, 4010 asymbol *sym, 4011 bfd_vma addr, 4012 const char **filename_ptr, 4013 unsigned int *linenumber_ptr) 4014 { 4015 struct funcinfo* each_func; 4016 struct funcinfo* best_fit = NULL; 4017 bfd_vma best_fit_len = 0; 4018 struct info_list_node *node; 4019 struct arange *arange; 4020 const char *name = bfd_asymbol_name (sym); 4021 asection *sec = bfd_get_section (sym); 4022 4023 for (node = lookup_info_hash_table (hash_table, name); 4024 node; 4025 node = node->next) 4026 { 4027 each_func = (struct funcinfo *) node->info; 4028 for (arange = &each_func->arange; 4029 arange; 4030 arange = arange->next) 4031 { 4032 if ((!each_func->sec || each_func->sec == sec) 4033 && addr >= arange->low 4034 && addr < arange->high 4035 && (!best_fit 4036 || arange->high - arange->low < best_fit_len)) 4037 { 4038 best_fit = each_func; 4039 best_fit_len = arange->high - arange->low; 4040 } 4041 } 4042 } 4043 4044 if (best_fit) 4045 { 4046 best_fit->sec = sec; 4047 *filename_ptr = best_fit->file; 4048 *linenumber_ptr = best_fit->line; 4049 return TRUE; 4050 } 4051 4052 return FALSE; 4053 } 4054 4055 /* Look up a varinfo by name using the given info hash table. If found, 4056 also update the locations pointed to by filename_ptr and linenumber_ptr. 4057 4058 This function returns TRUE if a varinfo that matches the given symbol 4059 and address is found with any error; otherwise it returns FALSE. */ 4060 4061 static bfd_boolean 4062 info_hash_lookup_varinfo (struct info_hash_table *hash_table, 4063 asymbol *sym, 4064 bfd_vma addr, 4065 const char **filename_ptr, 4066 unsigned int *linenumber_ptr) 4067 { 4068 const char *name = bfd_asymbol_name (sym); 4069 asection *sec = bfd_get_section (sym); 4070 struct varinfo* each; 4071 struct info_list_node *node; 4072 4073 for (node = lookup_info_hash_table (hash_table, name); 4074 node; 4075 node = node->next) 4076 { 4077 each = (struct varinfo *) node->info; 4078 if (each->addr == addr 4079 && (!each->sec || each->sec == sec)) 4080 { 4081 each->sec = sec; 4082 *filename_ptr = each->file; 4083 *linenumber_ptr = each->line; 4084 return TRUE; 4085 } 4086 } 4087 4088 return FALSE; 4089 } 4090 4091 /* Update the funcinfo and varinfo info hash tables if they are 4092 not up to date. Returns TRUE if there is no error; otherwise 4093 returns FALSE and disable the info hash tables. */ 4094 4095 static bfd_boolean 4096 stash_maybe_update_info_hash_tables (struct dwarf2_debug *stash) 4097 { 4098 struct comp_unit *each; 4099 4100 /* Exit if hash tables are up-to-date. */ 4101 if (stash->all_comp_units == stash->hash_units_head) 4102 return TRUE; 4103 4104 if (stash->hash_units_head) 4105 each = stash->hash_units_head->prev_unit; 4106 else 4107 each = stash->last_comp_unit; 4108 4109 while (each) 4110 { 4111 if (!comp_unit_hash_info (stash, each, stash->funcinfo_hash_table, 4112 stash->varinfo_hash_table)) 4113 { 4114 stash->info_hash_status = STASH_INFO_HASH_DISABLED; 4115 return FALSE; 4116 } 4117 each = each->prev_unit; 4118 } 4119 4120 stash->hash_units_head = stash->all_comp_units; 4121 return TRUE; 4122 } 4123 4124 /* Check consistency of info hash tables. This is for debugging only. */ 4125 4126 static void ATTRIBUTE_UNUSED 4127 stash_verify_info_hash_table (struct dwarf2_debug *stash) 4128 { 4129 struct comp_unit *each_unit; 4130 struct funcinfo *each_func; 4131 struct varinfo *each_var; 4132 struct info_list_node *node; 4133 bfd_boolean found; 4134 4135 for (each_unit = stash->all_comp_units; 4136 each_unit; 4137 each_unit = each_unit->next_unit) 4138 { 4139 for (each_func = each_unit->function_table; 4140 each_func; 4141 each_func = each_func->prev_func) 4142 { 4143 if (!each_func->name) 4144 continue; 4145 node = lookup_info_hash_table (stash->funcinfo_hash_table, 4146 each_func->name); 4147 BFD_ASSERT (node); 4148 found = FALSE; 4149 while (node && !found) 4150 { 4151 found = node->info == each_func; 4152 node = node->next; 4153 } 4154 BFD_ASSERT (found); 4155 } 4156 4157 for (each_var = each_unit->variable_table; 4158 each_var; 4159 each_var = each_var->prev_var) 4160 { 4161 if (!each_var->name || !each_var->file || each_var->stack) 4162 continue; 4163 node = lookup_info_hash_table (stash->varinfo_hash_table, 4164 each_var->name); 4165 BFD_ASSERT (node); 4166 found = FALSE; 4167 while (node && !found) 4168 { 4169 found = node->info == each_var; 4170 node = node->next; 4171 } 4172 BFD_ASSERT (found); 4173 } 4174 } 4175 } 4176 4177 /* Check to see if we want to enable the info hash tables, which consume 4178 quite a bit of memory. Currently we only check the number times 4179 bfd_dwarf2_find_line is called. In the future, we may also want to 4180 take the number of symbols into account. */ 4181 4182 static void 4183 stash_maybe_enable_info_hash_tables (bfd *abfd, struct dwarf2_debug *stash) 4184 { 4185 BFD_ASSERT (stash->info_hash_status == STASH_INFO_HASH_OFF); 4186 4187 if (stash->info_hash_count++ < STASH_INFO_HASH_TRIGGER) 4188 return; 4189 4190 /* FIXME: Maybe we should check the reduce_memory_overheads 4191 and optimize fields in the bfd_link_info structure ? */ 4192 4193 /* Create hash tables. */ 4194 stash->funcinfo_hash_table = create_info_hash_table (abfd); 4195 stash->varinfo_hash_table = create_info_hash_table (abfd); 4196 if (!stash->funcinfo_hash_table || !stash->varinfo_hash_table) 4197 { 4198 /* Turn off info hashes if any allocation above fails. */ 4199 stash->info_hash_status = STASH_INFO_HASH_DISABLED; 4200 return; 4201 } 4202 /* We need a forced update so that the info hash tables will 4203 be created even though there is no compilation unit. That 4204 happens if STASH_INFO_HASH_TRIGGER is 0. */ 4205 stash_maybe_update_info_hash_tables (stash); 4206 stash->info_hash_status = STASH_INFO_HASH_ON; 4207 } 4208 4209 /* Find the file and line associated with a symbol and address using the 4210 info hash tables of a stash. If there is a match, the function returns 4211 TRUE and update the locations pointed to by filename_ptr and linenumber_ptr; 4212 otherwise it returns FALSE. */ 4213 4214 static bfd_boolean 4215 stash_find_line_fast (struct dwarf2_debug *stash, 4216 asymbol *sym, 4217 bfd_vma addr, 4218 const char **filename_ptr, 4219 unsigned int *linenumber_ptr) 4220 { 4221 BFD_ASSERT (stash->info_hash_status == STASH_INFO_HASH_ON); 4222 4223 if (sym->flags & BSF_FUNCTION) 4224 return info_hash_lookup_funcinfo (stash->funcinfo_hash_table, sym, addr, 4225 filename_ptr, linenumber_ptr); 4226 return info_hash_lookup_varinfo (stash->varinfo_hash_table, sym, addr, 4227 filename_ptr, linenumber_ptr); 4228 } 4229 4230 /* Save current section VMAs. */ 4231 4232 static bfd_boolean 4233 save_section_vma (const bfd *abfd, struct dwarf2_debug *stash) 4234 { 4235 asection *s; 4236 unsigned int i; 4237 4238 if (abfd->section_count == 0) 4239 return TRUE; 4240 stash->sec_vma = bfd_malloc (sizeof (*stash->sec_vma) * abfd->section_count); 4241 if (stash->sec_vma == NULL) 4242 return FALSE; 4243 for (i = 0, s = abfd->sections; i < abfd->section_count; i++, s = s->next) 4244 { 4245 if (s->output_section != NULL) 4246 stash->sec_vma[i] = s->output_section->vma + s->output_offset; 4247 else 4248 stash->sec_vma[i] = s->vma; 4249 } 4250 return TRUE; 4251 } 4252 4253 /* Compare current section VMAs against those at the time the stash 4254 was created. If find_nearest_line is used in linker warnings or 4255 errors early in the link process, the debug info stash will be 4256 invalid for later calls. This is because we relocate debug info 4257 sections, so the stashed section contents depend on symbol values, 4258 which in turn depend on section VMAs. */ 4259 4260 static bfd_boolean 4261 section_vma_same (const bfd *abfd, const struct dwarf2_debug *stash) 4262 { 4263 asection *s; 4264 unsigned int i; 4265 4266 for (i = 0, s = abfd->sections; i < abfd->section_count; i++, s = s->next) 4267 { 4268 bfd_vma vma; 4269 4270 if (s->output_section != NULL) 4271 vma = s->output_section->vma + s->output_offset; 4272 else 4273 vma = s->vma; 4274 if (vma != stash->sec_vma[i]) 4275 return FALSE; 4276 } 4277 return TRUE; 4278 } 4279 4280 /* Read debug information from DEBUG_BFD when DEBUG_BFD is specified. 4281 If DEBUG_BFD is not specified, we read debug information from ABFD 4282 or its gnu_debuglink. The results will be stored in PINFO. 4283 The function returns TRUE iff debug information is ready. */ 4284 4285 bfd_boolean 4286 _bfd_dwarf2_slurp_debug_info (bfd *abfd, bfd *debug_bfd, 4287 const struct dwarf_debug_section *debug_sections, 4288 asymbol **symbols, 4289 void **pinfo, 4290 bfd_boolean do_place) 4291 { 4292 bfd_size_type amt = sizeof (struct dwarf2_debug); 4293 bfd_size_type total_size; 4294 asection *msec; 4295 struct dwarf2_debug *stash = (struct dwarf2_debug *) *pinfo; 4296 4297 if (stash != NULL) 4298 { 4299 if (stash->orig_bfd == abfd 4300 && section_vma_same (abfd, stash)) 4301 { 4302 /* Check that we did previously find some debug information 4303 before attempting to make use of it. */ 4304 if (stash->bfd_ptr != NULL) 4305 { 4306 if (do_place && !place_sections (abfd, stash)) 4307 return FALSE; 4308 return TRUE; 4309 } 4310 4311 return FALSE; 4312 } 4313 _bfd_dwarf2_cleanup_debug_info (abfd, pinfo); 4314 memset (stash, 0, amt); 4315 } 4316 else 4317 { 4318 stash = (struct dwarf2_debug *) bfd_zalloc (abfd, amt); 4319 if (! stash) 4320 return FALSE; 4321 } 4322 stash->orig_bfd = abfd; 4323 stash->debug_sections = debug_sections; 4324 stash->syms = symbols; 4325 if (!save_section_vma (abfd, stash)) 4326 return FALSE; 4327 4328 *pinfo = stash; 4329 4330 if (debug_bfd == NULL) 4331 debug_bfd = abfd; 4332 4333 msec = find_debug_info (debug_bfd, debug_sections, NULL); 4334 if (msec == NULL && abfd == debug_bfd) 4335 { 4336 char * debug_filename; 4337 4338 debug_filename = bfd_follow_build_id_debuglink (abfd, DEBUGDIR); 4339 if (debug_filename == NULL) 4340 debug_filename = bfd_follow_gnu_debuglink (abfd, DEBUGDIR); 4341 4342 if (debug_filename == NULL) 4343 /* No dwarf2 info, and no gnu_debuglink to follow. 4344 Note that at this point the stash has been allocated, but 4345 contains zeros. This lets future calls to this function 4346 fail more quickly. */ 4347 return FALSE; 4348 4349 /* Set BFD_DECOMPRESS to decompress debug sections. */ 4350 if ((debug_bfd = bfd_openr (debug_filename, NULL)) == NULL 4351 || !(debug_bfd->flags |= BFD_DECOMPRESS, 4352 bfd_check_format (debug_bfd, bfd_object)) 4353 || (msec = find_debug_info (debug_bfd, 4354 debug_sections, NULL)) == NULL 4355 || !bfd_generic_link_read_symbols (debug_bfd)) 4356 { 4357 if (debug_bfd) 4358 bfd_close (debug_bfd); 4359 /* FIXME: Should we report our failure to follow the debuglink ? */ 4360 free (debug_filename); 4361 return FALSE; 4362 } 4363 4364 symbols = bfd_get_outsymbols (debug_bfd); 4365 stash->syms = symbols; 4366 stash->close_on_cleanup = TRUE; 4367 } 4368 stash->bfd_ptr = debug_bfd; 4369 4370 if (do_place 4371 && !place_sections (abfd, stash)) 4372 return FALSE; 4373 4374 /* There can be more than one DWARF2 info section in a BFD these 4375 days. First handle the easy case when there's only one. If 4376 there's more than one, try case two: none of the sections is 4377 compressed. In that case, read them all in and produce one 4378 large stash. We do this in two passes - in the first pass we 4379 just accumulate the section sizes, and in the second pass we 4380 read in the section's contents. (The allows us to avoid 4381 reallocing the data as we add sections to the stash.) If 4382 some or all sections are compressed, then do things the slow 4383 way, with a bunch of reallocs. */ 4384 4385 if (! find_debug_info (debug_bfd, debug_sections, msec)) 4386 { 4387 /* Case 1: only one info section. */ 4388 total_size = msec->size; 4389 if (! read_section (debug_bfd, &stash->debug_sections[debug_info], 4390 symbols, 0, 4391 &stash->info_ptr_memory, &total_size)) 4392 return FALSE; 4393 } 4394 else 4395 { 4396 /* Case 2: multiple sections. */ 4397 for (total_size = 0; 4398 msec; 4399 msec = find_debug_info (debug_bfd, debug_sections, msec)) 4400 total_size += msec->size; 4401 4402 stash->info_ptr_memory = (bfd_byte *) bfd_malloc (total_size); 4403 if (stash->info_ptr_memory == NULL) 4404 return FALSE; 4405 4406 total_size = 0; 4407 for (msec = find_debug_info (debug_bfd, debug_sections, NULL); 4408 msec; 4409 msec = find_debug_info (debug_bfd, debug_sections, msec)) 4410 { 4411 bfd_size_type size; 4412 4413 size = msec->size; 4414 if (size == 0) 4415 continue; 4416 4417 if (!(bfd_simple_get_relocated_section_contents 4418 (debug_bfd, msec, stash->info_ptr_memory + total_size, 4419 symbols))) 4420 return FALSE; 4421 4422 total_size += size; 4423 } 4424 } 4425 4426 stash->info_ptr = stash->info_ptr_memory; 4427 stash->info_ptr_end = stash->info_ptr + total_size; 4428 stash->sec = find_debug_info (debug_bfd, debug_sections, NULL); 4429 stash->sec_info_ptr = stash->info_ptr; 4430 return TRUE; 4431 } 4432 4433 /* Scan the debug information in PINFO looking for a DW_TAG_subprogram 4434 abbrev with a DW_AT_low_pc attached to it. Then lookup that same 4435 symbol in SYMBOLS and return the difference between the low_pc and 4436 the symbol's address. Returns 0 if no suitable symbol could be found. */ 4437 4438 bfd_signed_vma 4439 _bfd_dwarf2_find_symbol_bias (asymbol ** symbols, void ** pinfo) 4440 { 4441 struct dwarf2_debug *stash; 4442 struct comp_unit * unit; 4443 4444 stash = (struct dwarf2_debug *) *pinfo; 4445 4446 if (stash == NULL) 4447 return 0; 4448 4449 for (unit = stash->all_comp_units; unit; unit = unit->next_unit) 4450 { 4451 struct funcinfo * func; 4452 4453 if (unit->function_table == NULL) 4454 { 4455 if (unit->line_table == NULL) 4456 unit->line_table = decode_line_info (unit, stash); 4457 if (unit->line_table != NULL) 4458 scan_unit_for_symbols (unit); 4459 } 4460 4461 for (func = unit->function_table; func != NULL; func = func->prev_func) 4462 if (func->name && func->arange.low) 4463 { 4464 asymbol ** psym; 4465 4466 /* FIXME: Do we need to scan the aranges looking for the lowest pc value ? */ 4467 4468 for (psym = symbols; * psym != NULL; psym++) 4469 { 4470 asymbol * sym = * psym; 4471 4472 if (sym->flags & BSF_FUNCTION 4473 && sym->section != NULL 4474 && strcmp (sym->name, func->name) == 0) 4475 return ((bfd_signed_vma) func->arange.low) - 4476 ((bfd_signed_vma) (sym->value + sym->section->vma)); 4477 } 4478 } 4479 } 4480 4481 return 0; 4482 } 4483 4484 /* Find the source code location of SYMBOL. If SYMBOL is NULL 4485 then find the nearest source code location corresponding to 4486 the address SECTION + OFFSET. 4487 Returns TRUE if the line is found without error and fills in 4488 FILENAME_PTR and LINENUMBER_PTR. In the case where SYMBOL was 4489 NULL the FUNCTIONNAME_PTR is also filled in. 4490 SYMBOLS contains the symbol table for ABFD. 4491 DEBUG_SECTIONS contains the name of the dwarf debug sections. 4492 ADDR_SIZE is the number of bytes in the initial .debug_info length 4493 field and in the abbreviation offset, or zero to indicate that the 4494 default value should be used. */ 4495 4496 bfd_boolean 4497 _bfd_dwarf2_find_nearest_line (bfd *abfd, 4498 asymbol **symbols, 4499 asymbol *symbol, 4500 asection *section, 4501 bfd_vma offset, 4502 const char **filename_ptr, 4503 const char **functionname_ptr, 4504 unsigned int *linenumber_ptr, 4505 unsigned int *discriminator_ptr, 4506 const struct dwarf_debug_section *debug_sections, 4507 unsigned int addr_size, 4508 void **pinfo) 4509 { 4510 /* Read each compilation unit from the section .debug_info, and check 4511 to see if it contains the address we are searching for. If yes, 4512 lookup the address, and return the line number info. If no, go 4513 on to the next compilation unit. 4514 4515 We keep a list of all the previously read compilation units, and 4516 a pointer to the next un-read compilation unit. Check the 4517 previously read units before reading more. */ 4518 struct dwarf2_debug *stash; 4519 /* What address are we looking for? */ 4520 bfd_vma addr; 4521 struct comp_unit* each; 4522 struct funcinfo *function = NULL; 4523 bfd_boolean found = FALSE; 4524 bfd_boolean do_line; 4525 4526 *filename_ptr = NULL; 4527 if (functionname_ptr != NULL) 4528 *functionname_ptr = NULL; 4529 *linenumber_ptr = 0; 4530 if (discriminator_ptr) 4531 *discriminator_ptr = 0; 4532 4533 if (! _bfd_dwarf2_slurp_debug_info (abfd, NULL, debug_sections, 4534 symbols, pinfo, 4535 (abfd->flags & (EXEC_P | DYNAMIC)) == 0)) 4536 return FALSE; 4537 4538 stash = (struct dwarf2_debug *) *pinfo; 4539 4540 do_line = symbol != NULL; 4541 if (do_line) 4542 { 4543 BFD_ASSERT (section == NULL && offset == 0 && functionname_ptr == NULL); 4544 section = bfd_get_section (symbol); 4545 addr = symbol->value; 4546 } 4547 else 4548 { 4549 BFD_ASSERT (section != NULL && functionname_ptr != NULL); 4550 addr = offset; 4551 4552 /* If we have no SYMBOL but the section we're looking at is not a 4553 code section, then take a look through the list of symbols to see 4554 if we have a symbol at the address we're looking for. If we do 4555 then use this to look up line information. This will allow us to 4556 give file and line results for data symbols. We exclude code 4557 symbols here, if we look up a function symbol and then look up the 4558 line information we'll actually return the line number for the 4559 opening '{' rather than the function definition line. This is 4560 because looking up by symbol uses the line table, in which the 4561 first line for a function is usually the opening '{', while 4562 looking up the function by section + offset uses the 4563 DW_AT_decl_line from the function DW_TAG_subprogram for the line, 4564 which will be the line of the function name. */ 4565 if (symbols != NULL && (section->flags & SEC_CODE) == 0) 4566 { 4567 asymbol **tmp; 4568 4569 for (tmp = symbols; (*tmp) != NULL; ++tmp) 4570 if ((*tmp)->the_bfd == abfd 4571 && (*tmp)->section == section 4572 && (*tmp)->value == offset 4573 && ((*tmp)->flags & BSF_SECTION_SYM) == 0) 4574 { 4575 symbol = *tmp; 4576 do_line = TRUE; 4577 /* For local symbols, keep going in the hope we find a 4578 global. */ 4579 if ((symbol->flags & BSF_GLOBAL) != 0) 4580 break; 4581 } 4582 } 4583 } 4584 4585 if (section->output_section) 4586 addr += section->output_section->vma + section->output_offset; 4587 else 4588 addr += section->vma; 4589 4590 /* A null info_ptr indicates that there is no dwarf2 info 4591 (or that an error occured while setting up the stash). */ 4592 if (! stash->info_ptr) 4593 return FALSE; 4594 4595 stash->inliner_chain = NULL; 4596 4597 /* Check the previously read comp. units first. */ 4598 if (do_line) 4599 { 4600 /* The info hash tables use quite a bit of memory. We may not want to 4601 always use them. We use some heuristics to decide if and when to 4602 turn it on. */ 4603 if (stash->info_hash_status == STASH_INFO_HASH_OFF) 4604 stash_maybe_enable_info_hash_tables (abfd, stash); 4605 4606 /* Keep info hash table up to date if they are available. Note that we 4607 may disable the hash tables if there is any error duing update. */ 4608 if (stash->info_hash_status == STASH_INFO_HASH_ON) 4609 stash_maybe_update_info_hash_tables (stash); 4610 4611 if (stash->info_hash_status == STASH_INFO_HASH_ON) 4612 { 4613 found = stash_find_line_fast (stash, symbol, addr, filename_ptr, 4614 linenumber_ptr); 4615 if (found) 4616 goto done; 4617 } 4618 else 4619 { 4620 /* Check the previously read comp. units first. */ 4621 for (each = stash->all_comp_units; each; each = each->next_unit) 4622 if ((symbol->flags & BSF_FUNCTION) == 0 4623 || each->arange.high == 0 4624 || comp_unit_contains_address (each, addr)) 4625 { 4626 found = comp_unit_find_line (each, symbol, addr, filename_ptr, 4627 linenumber_ptr, stash); 4628 if (found) 4629 goto done; 4630 } 4631 } 4632 } 4633 else 4634 { 4635 bfd_vma min_range = (bfd_vma) -1; 4636 const char * local_filename = NULL; 4637 struct funcinfo *local_function = NULL; 4638 unsigned int local_linenumber = 0; 4639 unsigned int local_discriminator = 0; 4640 4641 for (each = stash->all_comp_units; each; each = each->next_unit) 4642 { 4643 bfd_vma range = (bfd_vma) -1; 4644 4645 found = ((each->arange.high == 0 4646 || comp_unit_contains_address (each, addr)) 4647 && (range = comp_unit_find_nearest_line (each, addr, 4648 & local_filename, 4649 & local_function, 4650 & local_linenumber, 4651 & local_discriminator, 4652 stash)) != 0); 4653 if (found) 4654 { 4655 /* PRs 15935 15994: Bogus debug information may have provided us 4656 with an erroneous match. We attempt to counter this by 4657 selecting the match that has the smallest address range 4658 associated with it. (We are assuming that corrupt debug info 4659 will tend to result in extra large address ranges rather than 4660 extra small ranges). 4661 4662 This does mean that we scan through all of the CUs associated 4663 with the bfd each time this function is called. But this does 4664 have the benefit of producing consistent results every time the 4665 function is called. */ 4666 if (range <= min_range) 4667 { 4668 if (filename_ptr && local_filename) 4669 * filename_ptr = local_filename; 4670 if (local_function) 4671 function = local_function; 4672 if (discriminator_ptr && local_discriminator) 4673 * discriminator_ptr = local_discriminator; 4674 if (local_linenumber) 4675 * linenumber_ptr = local_linenumber; 4676 min_range = range; 4677 } 4678 } 4679 } 4680 4681 if (* linenumber_ptr) 4682 { 4683 found = TRUE; 4684 goto done; 4685 } 4686 } 4687 4688 /* The DWARF2 spec says that the initial length field, and the 4689 offset of the abbreviation table, should both be 4-byte values. 4690 However, some compilers do things differently. */ 4691 if (addr_size == 0) 4692 addr_size = 4; 4693 BFD_ASSERT (addr_size == 4 || addr_size == 8); 4694 4695 /* Read each remaining comp. units checking each as they are read. */ 4696 while (stash->info_ptr < stash->info_ptr_end) 4697 { 4698 bfd_vma length; 4699 unsigned int offset_size = addr_size; 4700 bfd_byte *info_ptr_unit = stash->info_ptr; 4701 4702 length = read_4_bytes (stash->bfd_ptr, stash->info_ptr, stash->info_ptr_end); 4703 /* A 0xffffff length is the DWARF3 way of indicating 4704 we use 64-bit offsets, instead of 32-bit offsets. */ 4705 if (length == 0xffffffff) 4706 { 4707 offset_size = 8; 4708 length = read_8_bytes (stash->bfd_ptr, stash->info_ptr + 4, stash->info_ptr_end); 4709 stash->info_ptr += 12; 4710 } 4711 /* A zero length is the IRIX way of indicating 64-bit offsets, 4712 mostly because the 64-bit length will generally fit in 32 4713 bits, and the endianness helps. */ 4714 else if (length == 0) 4715 { 4716 offset_size = 8; 4717 length = read_4_bytes (stash->bfd_ptr, stash->info_ptr + 4, stash->info_ptr_end); 4718 stash->info_ptr += 8; 4719 } 4720 /* In the absence of the hints above, we assume 32-bit DWARF2 4721 offsets even for targets with 64-bit addresses, because: 4722 a) most of the time these targets will not have generated 4723 more than 2Gb of debug info and so will not need 64-bit 4724 offsets, 4725 and 4726 b) if they do use 64-bit offsets but they are not using 4727 the size hints that are tested for above then they are 4728 not conforming to the DWARF3 standard anyway. */ 4729 else if (addr_size == 8) 4730 { 4731 offset_size = 4; 4732 stash->info_ptr += 4; 4733 } 4734 else 4735 stash->info_ptr += 4; 4736 4737 if (length > 0) 4738 { 4739 bfd_byte * new_ptr; 4740 4741 /* PR 21151 */ 4742 if (stash->info_ptr + length > stash->info_ptr_end) 4743 return FALSE; 4744 4745 each = parse_comp_unit (stash, length, info_ptr_unit, 4746 offset_size); 4747 if (!each) 4748 /* The dwarf information is damaged, don't trust it any 4749 more. */ 4750 break; 4751 4752 new_ptr = stash->info_ptr + length; 4753 /* PR 17512: file: 1500698c. */ 4754 if (new_ptr < stash->info_ptr) 4755 { 4756 /* A corrupt length value - do not trust the info any more. */ 4757 found = FALSE; 4758 break; 4759 } 4760 else 4761 stash->info_ptr = new_ptr; 4762 4763 if (stash->all_comp_units) 4764 stash->all_comp_units->prev_unit = each; 4765 else 4766 stash->last_comp_unit = each; 4767 4768 each->next_unit = stash->all_comp_units; 4769 stash->all_comp_units = each; 4770 4771 /* DW_AT_low_pc and DW_AT_high_pc are optional for 4772 compilation units. If we don't have them (i.e., 4773 unit->high == 0), we need to consult the line info table 4774 to see if a compilation unit contains the given 4775 address. */ 4776 if (do_line) 4777 found = (((symbol->flags & BSF_FUNCTION) == 0 4778 || each->arange.high == 0 4779 || comp_unit_contains_address (each, addr)) 4780 && comp_unit_find_line (each, symbol, addr, 4781 filename_ptr, 4782 linenumber_ptr, 4783 stash)); 4784 else 4785 found = ((each->arange.high == 0 4786 || comp_unit_contains_address (each, addr)) 4787 && comp_unit_find_nearest_line (each, addr, 4788 filename_ptr, 4789 &function, 4790 linenumber_ptr, 4791 discriminator_ptr, 4792 stash) != 0); 4793 4794 if ((bfd_vma) (stash->info_ptr - stash->sec_info_ptr) 4795 == stash->sec->size) 4796 { 4797 stash->sec = find_debug_info (stash->bfd_ptr, debug_sections, 4798 stash->sec); 4799 stash->sec_info_ptr = stash->info_ptr; 4800 } 4801 4802 if (found) 4803 goto done; 4804 } 4805 } 4806 4807 done: 4808 if (function) 4809 { 4810 if (!function->is_linkage) 4811 { 4812 asymbol *fun; 4813 bfd_vma sec_vma; 4814 4815 fun = _bfd_elf_find_function (abfd, symbols, section, offset, 4816 *filename_ptr ? NULL : filename_ptr, 4817 functionname_ptr); 4818 sec_vma = section->vma; 4819 if (section->output_section != NULL) 4820 sec_vma = section->output_section->vma + section->output_offset; 4821 if (fun != NULL 4822 && fun->value + sec_vma == function->arange.low) 4823 function->name = *functionname_ptr; 4824 /* Even if we didn't find a linkage name, say that we have 4825 to stop a repeated search of symbols. */ 4826 function->is_linkage = TRUE; 4827 } 4828 *functionname_ptr = function->name; 4829 } 4830 if ((abfd->flags & (EXEC_P | DYNAMIC)) == 0) 4831 unset_sections (stash); 4832 4833 return found; 4834 } 4835 4836 bfd_boolean 4837 _bfd_dwarf2_find_inliner_info (bfd *abfd ATTRIBUTE_UNUSED, 4838 const char **filename_ptr, 4839 const char **functionname_ptr, 4840 unsigned int *linenumber_ptr, 4841 void **pinfo) 4842 { 4843 struct dwarf2_debug *stash; 4844 4845 stash = (struct dwarf2_debug *) *pinfo; 4846 if (stash) 4847 { 4848 struct funcinfo *func = stash->inliner_chain; 4849 4850 if (func && func->caller_func) 4851 { 4852 *filename_ptr = func->caller_file; 4853 *functionname_ptr = func->caller_func->name; 4854 *linenumber_ptr = func->caller_line; 4855 stash->inliner_chain = func->caller_func; 4856 return TRUE; 4857 } 4858 } 4859 4860 return FALSE; 4861 } 4862 4863 void 4864 _bfd_dwarf2_cleanup_debug_info (bfd *abfd, void **pinfo) 4865 { 4866 struct dwarf2_debug *stash = (struct dwarf2_debug *) *pinfo; 4867 struct comp_unit *each; 4868 4869 if (abfd == NULL || stash == NULL) 4870 return; 4871 4872 for (each = stash->all_comp_units; each; each = each->next_unit) 4873 { 4874 struct abbrev_info **abbrevs = each->abbrevs; 4875 struct funcinfo *function_table = each->function_table; 4876 struct varinfo *variable_table = each->variable_table; 4877 size_t i; 4878 4879 for (i = 0; i < ABBREV_HASH_SIZE; i++) 4880 { 4881 struct abbrev_info *abbrev = abbrevs[i]; 4882 4883 while (abbrev) 4884 { 4885 free (abbrev->attrs); 4886 abbrev = abbrev->next; 4887 } 4888 } 4889 4890 if (each->line_table) 4891 { 4892 free (each->line_table->dirs); 4893 free (each->line_table->files); 4894 } 4895 4896 while (function_table) 4897 { 4898 if (function_table->file) 4899 { 4900 free (function_table->file); 4901 function_table->file = NULL; 4902 } 4903 4904 if (function_table->caller_file) 4905 { 4906 free (function_table->caller_file); 4907 function_table->caller_file = NULL; 4908 } 4909 function_table = function_table->prev_func; 4910 } 4911 4912 if (each->lookup_funcinfo_table) 4913 { 4914 free (each->lookup_funcinfo_table); 4915 each->lookup_funcinfo_table = NULL; 4916 } 4917 4918 while (variable_table) 4919 { 4920 if (variable_table->file) 4921 { 4922 free (variable_table->file); 4923 variable_table->file = NULL; 4924 } 4925 4926 variable_table = variable_table->prev_var; 4927 } 4928 } 4929 4930 if (stash->funcinfo_hash_table) 4931 bfd_hash_table_free (&stash->funcinfo_hash_table->base); 4932 if (stash->varinfo_hash_table) 4933 bfd_hash_table_free (&stash->varinfo_hash_table->base); 4934 if (stash->dwarf_abbrev_buffer) 4935 free (stash->dwarf_abbrev_buffer); 4936 if (stash->dwarf_line_buffer) 4937 free (stash->dwarf_line_buffer); 4938 if (stash->dwarf_str_buffer) 4939 free (stash->dwarf_str_buffer); 4940 if (stash->dwarf_line_str_buffer) 4941 free (stash->dwarf_line_str_buffer); 4942 if (stash->dwarf_ranges_buffer) 4943 free (stash->dwarf_ranges_buffer); 4944 if (stash->info_ptr_memory) 4945 free (stash->info_ptr_memory); 4946 if (stash->close_on_cleanup) 4947 bfd_close (stash->bfd_ptr); 4948 if (stash->alt_dwarf_str_buffer) 4949 free (stash->alt_dwarf_str_buffer); 4950 if (stash->alt_dwarf_info_buffer) 4951 free (stash->alt_dwarf_info_buffer); 4952 if (stash->sec_vma) 4953 free (stash->sec_vma); 4954 if (stash->adjusted_sections) 4955 free (stash->adjusted_sections); 4956 if (stash->alt_bfd_ptr) 4957 bfd_close (stash->alt_bfd_ptr); 4958 } 4959 4960 /* Find the function to a particular section and offset, 4961 for error reporting. */ 4962 4963 asymbol * 4964 _bfd_elf_find_function (bfd *abfd, 4965 asymbol **symbols, 4966 asection *section, 4967 bfd_vma offset, 4968 const char **filename_ptr, 4969 const char **functionname_ptr) 4970 { 4971 struct elf_find_function_cache 4972 { 4973 asection *last_section; 4974 asymbol *func; 4975 const char *filename; 4976 bfd_size_type func_size; 4977 } *cache; 4978 4979 if (symbols == NULL) 4980 return NULL; 4981 4982 if (bfd_get_flavour (abfd) != bfd_target_elf_flavour) 4983 return NULL; 4984 4985 cache = elf_tdata (abfd)->elf_find_function_cache; 4986 if (cache == NULL) 4987 { 4988 cache = bfd_zalloc (abfd, sizeof (*cache)); 4989 elf_tdata (abfd)->elf_find_function_cache = cache; 4990 if (cache == NULL) 4991 return NULL; 4992 } 4993 if (cache->last_section != section 4994 || cache->func == NULL 4995 || offset < cache->func->value 4996 || offset >= cache->func->value + cache->func_size) 4997 { 4998 asymbol *file; 4999 bfd_vma low_func; 5000 asymbol **p; 5001 /* ??? Given multiple file symbols, it is impossible to reliably 5002 choose the right file name for global symbols. File symbols are 5003 local symbols, and thus all file symbols must sort before any 5004 global symbols. The ELF spec may be interpreted to say that a 5005 file symbol must sort before other local symbols, but currently 5006 ld -r doesn't do this. So, for ld -r output, it is possible to 5007 make a better choice of file name for local symbols by ignoring 5008 file symbols appearing after a given local symbol. */ 5009 enum { nothing_seen, symbol_seen, file_after_symbol_seen } state; 5010 const struct elf_backend_data *bed = get_elf_backend_data (abfd); 5011 5012 file = NULL; 5013 low_func = 0; 5014 state = nothing_seen; 5015 cache->filename = NULL; 5016 cache->func = NULL; 5017 cache->func_size = 0; 5018 cache->last_section = section; 5019 5020 for (p = symbols; *p != NULL; p++) 5021 { 5022 asymbol *sym = *p; 5023 bfd_vma code_off; 5024 bfd_size_type size; 5025 5026 if ((sym->flags & BSF_FILE) != 0) 5027 { 5028 file = sym; 5029 if (state == symbol_seen) 5030 state = file_after_symbol_seen; 5031 continue; 5032 } 5033 5034 size = bed->maybe_function_sym (sym, section, &code_off); 5035 if (size != 0 5036 && code_off <= offset 5037 && (code_off > low_func 5038 || (code_off == low_func 5039 && size > cache->func_size))) 5040 { 5041 cache->func = sym; 5042 cache->func_size = size; 5043 cache->filename = NULL; 5044 low_func = code_off; 5045 if (file != NULL 5046 && ((sym->flags & BSF_LOCAL) != 0 5047 || state != file_after_symbol_seen)) 5048 cache->filename = bfd_asymbol_name (file); 5049 } 5050 if (state == nothing_seen) 5051 state = symbol_seen; 5052 } 5053 } 5054 5055 if (cache->func == NULL) 5056 return NULL; 5057 5058 if (filename_ptr) 5059 *filename_ptr = cache->filename; 5060 if (functionname_ptr) 5061 *functionname_ptr = bfd_asymbol_name (cache->func); 5062 5063 return cache->func; 5064 } 5065