2 Copyright 1994, 1995, 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003,
3 2004, 2005, 2006, 2007, 2008, 2009, 2010 Free Software Foundation, Inc.
5 Adapted from gdb/dwarf2read.c by Gavin Koch of Cygnus Solutions
8 From the dwarf2read.c header:
9 Adapted by Gary Funck (gary@intrepid.com), Intrepid Technology,
10 Inc. with support from Florida State University (under contract
11 with the Ada Joint Program Office), and Silicon Graphics, Inc.
12 Initial contribution by Brent Benson, Harris Computer Systems, Inc.,
13 based on Fred Fish's (Cygnus Support) implementation of DWARF 1
14 support in dwarfread.c
16 This file is part of BFD.
18 This program is free software; you can redistribute it and/or modify
19 it under the terms of the GNU General Public License as published by
20 the Free Software Foundation; either version 3 of the License, or (at
21 your option) any later version.
23 This program is distributed in the hope that it will be useful, but
24 WITHOUT ANY WARRANTY; without even the implied warranty of
25 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
26 General Public License for more details.
28 You should have received a copy of the GNU General Public License
29 along with this program; if not, write to the Free Software
30 Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
31 MA 02110-1301, USA. */
35 #include "libiberty.h"
40 /* The data in the .debug_line statement prologue looks like this. */
45 unsigned short version;
46 bfd_vma prologue_length;
47 unsigned char minimum_instruction_length;
48 unsigned char maximum_ops_per_insn;
49 unsigned char default_is_stmt;
51 unsigned char line_range;
52 unsigned char opcode_base;
53 unsigned char *standard_opcode_lengths;
56 /* Attributes have a name and a value. */
60 enum dwarf_attribute name;
65 struct dwarf_block *blk;
72 /* Blocks are a bunch of untyped bytes. */
79 struct adjusted_section
87 /* A list of all previously read comp_units. */
88 struct comp_unit *all_comp_units;
90 /* Last comp unit in list above. */
91 struct comp_unit *last_comp_unit;
93 /* Names of the debug sections. */
94 const struct dwarf_debug_section *debug_sections;
96 /* The next unread compilation unit within the .debug_info section.
97 Zero indicates that the .debug_info section has not been loaded
101 /* Pointer to the end of the .debug_info section memory buffer. */
102 bfd_byte *info_ptr_end;
104 /* Pointer to the bfd, section and address of the beginning of the
105 section. The bfd might be different than expected because of
106 gnu_debuglink sections. */
109 bfd_byte *sec_info_ptr;
111 /* A pointer to the memory block allocated for info_ptr. Neither
112 info_ptr nor sec_info_ptr are guaranteed to stay pointing to the
113 beginning of the malloc block. This is used only to free the
115 bfd_byte *info_ptr_memory;
117 /* Pointer to the symbol table. */
120 /* Pointer to the .debug_abbrev section loaded into memory. */
121 bfd_byte *dwarf_abbrev_buffer;
123 /* Length of the loaded .debug_abbrev section. */
124 bfd_size_type dwarf_abbrev_size;
126 /* Buffer for decode_line_info. */
127 bfd_byte *dwarf_line_buffer;
129 /* Length of the loaded .debug_line section. */
130 bfd_size_type dwarf_line_size;
132 /* Pointer to the .debug_str section loaded into memory. */
133 bfd_byte *dwarf_str_buffer;
135 /* Length of the loaded .debug_str section. */
136 bfd_size_type dwarf_str_size;
138 /* Pointer to the .debug_ranges section loaded into memory. */
139 bfd_byte *dwarf_ranges_buffer;
141 /* Length of the loaded .debug_ranges section. */
142 bfd_size_type dwarf_ranges_size;
144 /* If the most recent call to bfd_find_nearest_line was given an
145 address in an inlined function, preserve a pointer into the
146 calling chain for subsequent calls to bfd_find_inliner_info to
148 struct funcinfo *inliner_chain;
150 /* Number of sections whose VMA we must adjust. */
151 unsigned int adjusted_section_count;
153 /* Array of sections with adjusted VMA. */
154 struct adjusted_section *adjusted_sections;
156 /* Number of times find_line is called. This is used in
157 the heuristic for enabling the info hash tables. */
160 #define STASH_INFO_HASH_TRIGGER 100
162 /* Hash table mapping symbol names to function infos. */
163 struct info_hash_table *funcinfo_hash_table;
165 /* Hash table mapping symbol names to variable infos. */
166 struct info_hash_table *varinfo_hash_table;
168 /* Head of comp_unit list in the last hash table update. */
169 struct comp_unit *hash_units_head;
171 /* Status of info hash. */
172 int info_hash_status;
173 #define STASH_INFO_HASH_OFF 0
174 #define STASH_INFO_HASH_ON 1
175 #define STASH_INFO_HASH_DISABLED 2
185 /* A minimal decoding of DWARF2 compilation units. We only decode
186 what's needed to get to the line number information. */
190 /* Chain the previously read compilation units. */
191 struct comp_unit *next_unit;
193 /* Likewise, chain the compilation unit read after this one.
194 The comp units are stored in reversed reading order. */
195 struct comp_unit *prev_unit;
197 /* Keep the bfd convenient (for memory allocation). */
200 /* The lowest and highest addresses contained in this compilation
201 unit as specified in the compilation unit header. */
202 struct arange arange;
204 /* The DW_AT_name attribute (for error messages). */
207 /* The abbrev hash table. */
208 struct abbrev_info **abbrevs;
210 /* Note that an error was found by comp_unit_find_nearest_line. */
213 /* The DW_AT_comp_dir attribute. */
216 /* TRUE if there is a line number table associated with this comp. unit. */
219 /* Pointer to the current comp_unit so that we can find a given entry
221 bfd_byte *info_ptr_unit;
223 /* Pointer to the start of the debug section, for DW_FORM_ref_addr. */
224 bfd_byte *sec_info_ptr;
226 /* The offset into .debug_line of the line number table. */
227 unsigned long line_offset;
229 /* Pointer to the first child die for the comp unit. */
230 bfd_byte *first_child_die_ptr;
232 /* The end of the comp unit. */
235 /* The decoded line number, NULL if not yet decoded. */
236 struct line_info_table *line_table;
238 /* A list of the functions found in this comp. unit. */
239 struct funcinfo *function_table;
241 /* A list of the variables found in this comp. unit. */
242 struct varinfo *variable_table;
244 /* Pointer to dwarf2_debug structure. */
245 struct dwarf2_debug *stash;
247 /* DWARF format version for this unit - from unit header. */
250 /* Address size for this unit - from unit header. */
251 unsigned char addr_size;
253 /* Offset size for this unit - from unit header. */
254 unsigned char offset_size;
256 /* Base address for this unit - from DW_AT_low_pc attribute of
257 DW_TAG_compile_unit DIE */
258 bfd_vma base_address;
260 /* TRUE if symbols are cached in hash table for faster lookup by name. */
264 /* This data structure holds the information of an abbrev. */
267 unsigned int number; /* Number identifying abbrev. */
268 enum dwarf_tag tag; /* DWARF tag. */
269 int has_children; /* Boolean. */
270 unsigned int num_attrs; /* Number of attributes. */
271 struct attr_abbrev *attrs; /* An array of attribute descriptions. */
272 struct abbrev_info *next; /* Next in chain. */
277 enum dwarf_attribute name;
278 enum dwarf_form form;
281 /* Map of uncompressed DWARF debug section name to compressed one. It
282 is terminated by NULL uncompressed_name. */
284 const struct dwarf_debug_section dwarf_debug_sections[] =
286 { ".debug_abbrev", ".zdebug_abbrev" },
287 { ".debug_aranges", ".zdebug_aranges" },
288 { ".debug_frame", ".zdebug_frame" },
289 { ".debug_info", ".zdebug_info" },
290 { ".debug_line", ".zdebug_line" },
291 { ".debug_loc", ".zdebug_loc" },
292 { ".debug_macinfo", ".zdebug_macinfo" },
293 { ".debug_macro", ".zdebug_macro" },
294 { ".debug_pubnames", ".zdebug_pubnames" },
295 { ".debug_pubtypes", ".zdebug_pubtypes" },
296 { ".debug_ranges", ".zdebug_ranges" },
297 { ".debug_static_func", ".zdebug_static_func" },
298 { ".debug_static_vars", ".zdebug_static_vars" },
299 { ".debug_str", ".zdebug_str", },
300 { ".debug_types", ".zdebug_types" },
301 /* GNU DWARF 1 extensions */
302 { ".debug_sfnames", ".zdebug_sfnames" },
303 { ".debug_srcinfo", ".zebug_srcinfo" },
304 /* SGI/MIPS DWARF 2 extensions */
305 { ".debug_funcnames", ".zdebug_funcnames" },
306 { ".debug_typenames", ".zdebug_typenames" },
307 { ".debug_varnames", ".zdebug_varnames" },
308 { ".debug_weaknames", ".zdebug_weaknames" },
312 enum dwarf_debug_section_enum
337 #ifndef ABBREV_HASH_SIZE
338 #define ABBREV_HASH_SIZE 121
340 #ifndef ATTR_ALLOC_CHUNK
341 #define ATTR_ALLOC_CHUNK 4
344 /* Variable and function hash tables. This is used to speed up look-up
345 in lookup_symbol_in_var_table() and lookup_symbol_in_function_table().
346 In order to share code between variable and function infos, we use
347 a list of untyped pointer for all variable/function info associated with
348 a symbol. We waste a bit of memory for list with one node but that
349 simplifies the code. */
351 struct info_list_node
353 struct info_list_node *next;
357 /* Info hash entry. */
358 struct info_hash_entry
360 struct bfd_hash_entry root;
361 struct info_list_node *head;
364 struct info_hash_table
366 struct bfd_hash_table base;
369 /* Function to create a new entry in info hash table. */
371 static struct bfd_hash_entry *
372 info_hash_table_newfunc (struct bfd_hash_entry *entry,
373 struct bfd_hash_table *table,
376 struct info_hash_entry *ret = (struct info_hash_entry *) entry;
378 /* Allocate the structure if it has not already been allocated by a
382 ret = (struct info_hash_entry *) bfd_hash_allocate (table,
388 /* Call the allocation method of the base class. */
389 ret = ((struct info_hash_entry *)
390 bfd_hash_newfunc ((struct bfd_hash_entry *) ret, table, string));
392 /* Initialize the local fields here. */
396 return (struct bfd_hash_entry *) ret;
399 /* Function to create a new info hash table. It returns a pointer to the
400 newly created table or NULL if there is any error. We need abfd
401 solely for memory allocation. */
403 static struct info_hash_table *
404 create_info_hash_table (bfd *abfd)
406 struct info_hash_table *hash_table;
408 hash_table = (struct info_hash_table *)
409 bfd_alloc (abfd, sizeof (struct info_hash_table));
413 if (!bfd_hash_table_init (&hash_table->base, info_hash_table_newfunc,
414 sizeof (struct info_hash_entry)))
416 bfd_release (abfd, hash_table);
423 /* Insert an info entry into an info hash table. We do not check of
424 duplicate entries. Also, the caller need to guarantee that the
425 right type of info in inserted as info is passed as a void* pointer.
426 This function returns true if there is no error. */
429 insert_info_hash_table (struct info_hash_table *hash_table,
434 struct info_hash_entry *entry;
435 struct info_list_node *node;
437 entry = (struct info_hash_entry*) bfd_hash_lookup (&hash_table->base,
442 node = (struct info_list_node *) bfd_hash_allocate (&hash_table->base,
448 node->next = entry->head;
454 /* Look up an info entry list from an info hash table. Return NULL
457 static struct info_list_node *
458 lookup_info_hash_table (struct info_hash_table *hash_table, const char *key)
460 struct info_hash_entry *entry;
462 entry = (struct info_hash_entry*) bfd_hash_lookup (&hash_table->base, key,
464 return entry ? entry->head : NULL;
467 /* Read a section into its appropriate place in the dwarf2_debug
468 struct (indicated by SECTION_BUFFER and SECTION_SIZE). If SYMS is
469 not NULL, use bfd_simple_get_relocated_section_contents to read the
470 section contents, otherwise use bfd_get_section_contents. Fail if
471 the located section does not contain at least OFFSET bytes. */
474 read_section (bfd * abfd,
475 const struct dwarf_debug_section *sec,
478 bfd_byte ** section_buffer,
479 bfd_size_type * section_size)
482 const char *section_name = sec->uncompressed_name;
484 /* read_section is a noop if the section has already been read. */
485 if (!*section_buffer)
487 msec = bfd_get_section_by_name (abfd, section_name);
490 section_name = sec->compressed_name;
491 if (section_name != NULL)
492 msec = bfd_get_section_by_name (abfd, section_name);
496 (*_bfd_error_handler) (_("Dwarf Error: Can't find %s section."),
497 sec->uncompressed_name);
498 bfd_set_error (bfd_error_bad_value);
502 *section_size = msec->rawsize ? msec->rawsize : msec->size;
506 = bfd_simple_get_relocated_section_contents (abfd, msec, NULL, syms);
507 if (! *section_buffer)
512 *section_buffer = (bfd_byte *) bfd_malloc (*section_size);
513 if (! *section_buffer)
515 if (! bfd_get_section_contents (abfd, msec, *section_buffer,
521 /* It is possible to get a bad value for the offset into the section
522 that the client wants. Validate it here to avoid trouble later. */
523 if (offset != 0 && offset >= *section_size)
525 (*_bfd_error_handler) (_("Dwarf Error: Offset (%lu) greater than or equal to %s size (%lu)."),
526 (long) offset, section_name, *section_size);
527 bfd_set_error (bfd_error_bad_value);
535 The following function up to the END VERBATIM mark are
536 copied directly from dwarf2read.c. */
538 /* Read dwarf information from a buffer. */
541 read_1_byte (bfd *abfd ATTRIBUTE_UNUSED, bfd_byte *buf)
543 return bfd_get_8 (abfd, buf);
547 read_1_signed_byte (bfd *abfd ATTRIBUTE_UNUSED, bfd_byte *buf)
549 return bfd_get_signed_8 (abfd, buf);
553 read_2_bytes (bfd *abfd, bfd_byte *buf)
555 return bfd_get_16 (abfd, buf);
559 read_4_bytes (bfd *abfd, bfd_byte *buf)
561 return bfd_get_32 (abfd, buf);
565 read_8_bytes (bfd *abfd, bfd_byte *buf)
567 return bfd_get_64 (abfd, buf);
571 read_n_bytes (bfd *abfd ATTRIBUTE_UNUSED,
573 unsigned int size ATTRIBUTE_UNUSED)
579 read_string (bfd *abfd ATTRIBUTE_UNUSED,
581 unsigned int *bytes_read_ptr)
583 /* Return a pointer to the embedded string. */
584 char *str = (char *) buf;
592 *bytes_read_ptr = strlen (str) + 1;
599 read_indirect_string (struct comp_unit * unit,
601 unsigned int * bytes_read_ptr)
604 struct dwarf2_debug *stash = unit->stash;
607 if (unit->offset_size == 4)
608 offset = read_4_bytes (unit->abfd, buf);
610 offset = read_8_bytes (unit->abfd, buf);
612 *bytes_read_ptr = unit->offset_size;
614 if (! read_section (unit->abfd, &stash->debug_sections[debug_str],
616 &stash->dwarf_str_buffer, &stash->dwarf_str_size))
619 str = (char *) stash->dwarf_str_buffer + offset;
626 read_address (struct comp_unit *unit, bfd_byte *buf)
628 int signed_vma = get_elf_backend_data (unit->abfd)->sign_extend_vma;
632 switch (unit->addr_size)
635 return bfd_get_signed_64 (unit->abfd, buf);
637 return bfd_get_signed_32 (unit->abfd, buf);
639 return bfd_get_signed_16 (unit->abfd, buf);
646 switch (unit->addr_size)
649 return bfd_get_64 (unit->abfd, buf);
651 return bfd_get_32 (unit->abfd, buf);
653 return bfd_get_16 (unit->abfd, buf);
660 /* Lookup an abbrev_info structure in the abbrev hash table. */
662 static struct abbrev_info *
663 lookup_abbrev (unsigned int number, struct abbrev_info **abbrevs)
665 unsigned int hash_number;
666 struct abbrev_info *abbrev;
668 hash_number = number % ABBREV_HASH_SIZE;
669 abbrev = abbrevs[hash_number];
673 if (abbrev->number == number)
676 abbrev = abbrev->next;
682 /* In DWARF version 2, the description of the debugging information is
683 stored in a separate .debug_abbrev section. Before we read any
684 dies from a section we read in all abbreviations and install them
687 static struct abbrev_info**
688 read_abbrevs (bfd *abfd, bfd_uint64_t offset, struct dwarf2_debug *stash)
690 struct abbrev_info **abbrevs;
691 bfd_byte *abbrev_ptr;
692 struct abbrev_info *cur_abbrev;
693 unsigned int abbrev_number, bytes_read, abbrev_name;
694 unsigned int abbrev_form, hash_number;
697 if (! read_section (abfd, &stash->debug_sections[debug_abbrev],
699 &stash->dwarf_abbrev_buffer, &stash->dwarf_abbrev_size))
702 amt = sizeof (struct abbrev_info*) * ABBREV_HASH_SIZE;
703 abbrevs = (struct abbrev_info **) bfd_zalloc (abfd, amt);
707 abbrev_ptr = stash->dwarf_abbrev_buffer + offset;
708 abbrev_number = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
709 abbrev_ptr += bytes_read;
711 /* Loop until we reach an abbrev number of 0. */
712 while (abbrev_number)
714 amt = sizeof (struct abbrev_info);
715 cur_abbrev = (struct abbrev_info *) bfd_zalloc (abfd, amt);
716 if (cur_abbrev == NULL)
719 /* Read in abbrev header. */
720 cur_abbrev->number = abbrev_number;
721 cur_abbrev->tag = (enum dwarf_tag)
722 read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
723 abbrev_ptr += bytes_read;
724 cur_abbrev->has_children = read_1_byte (abfd, abbrev_ptr);
727 /* Now read in declarations. */
728 abbrev_name = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
729 abbrev_ptr += bytes_read;
730 abbrev_form = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
731 abbrev_ptr += bytes_read;
735 if ((cur_abbrev->num_attrs % ATTR_ALLOC_CHUNK) == 0)
737 struct attr_abbrev *tmp;
739 amt = cur_abbrev->num_attrs + ATTR_ALLOC_CHUNK;
740 amt *= sizeof (struct attr_abbrev);
741 tmp = (struct attr_abbrev *) bfd_realloc (cur_abbrev->attrs, amt);
746 for (i = 0; i < ABBREV_HASH_SIZE; i++)
748 struct abbrev_info *abbrev = abbrevs[i];
752 free (abbrev->attrs);
753 abbrev = abbrev->next;
758 cur_abbrev->attrs = tmp;
761 cur_abbrev->attrs[cur_abbrev->num_attrs].name
762 = (enum dwarf_attribute) abbrev_name;
763 cur_abbrev->attrs[cur_abbrev->num_attrs++].form
764 = (enum dwarf_form) abbrev_form;
765 abbrev_name = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
766 abbrev_ptr += bytes_read;
767 abbrev_form = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
768 abbrev_ptr += bytes_read;
771 hash_number = abbrev_number % ABBREV_HASH_SIZE;
772 cur_abbrev->next = abbrevs[hash_number];
773 abbrevs[hash_number] = cur_abbrev;
775 /* Get next abbreviation.
776 Under Irix6 the abbreviations for a compilation unit are not
777 always properly terminated with an abbrev number of 0.
778 Exit loop if we encounter an abbreviation which we have
779 already read (which means we are about to read the abbreviations
780 for the next compile unit) or if the end of the abbreviation
782 if ((unsigned int) (abbrev_ptr - stash->dwarf_abbrev_buffer)
783 >= stash->dwarf_abbrev_size)
785 abbrev_number = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
786 abbrev_ptr += bytes_read;
787 if (lookup_abbrev (abbrev_number,abbrevs) != NULL)
794 /* Read an attribute value described by an attribute form. */
797 read_attribute_value (struct attribute *attr,
799 struct comp_unit *unit,
802 bfd *abfd = unit->abfd;
803 unsigned int bytes_read;
804 struct dwarf_block *blk;
807 attr->form = (enum dwarf_form) form;
811 case DW_FORM_ref_addr:
812 /* DW_FORM_ref_addr is an address in DWARF2, and an offset in
814 if (unit->version == 3 || unit->version == 4)
816 if (unit->offset_size == 4)
817 attr->u.val = read_4_bytes (unit->abfd, info_ptr);
819 attr->u.val = read_8_bytes (unit->abfd, info_ptr);
820 info_ptr += unit->offset_size;
825 attr->u.val = read_address (unit, info_ptr);
826 info_ptr += unit->addr_size;
828 case DW_FORM_sec_offset:
829 if (unit->offset_size == 4)
830 attr->u.val = read_4_bytes (unit->abfd, info_ptr);
832 attr->u.val = read_8_bytes (unit->abfd, info_ptr);
833 info_ptr += unit->offset_size;
836 amt = sizeof (struct dwarf_block);
837 blk = (struct dwarf_block *) bfd_alloc (abfd, amt);
840 blk->size = read_2_bytes (abfd, info_ptr);
842 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
843 info_ptr += blk->size;
847 amt = sizeof (struct dwarf_block);
848 blk = (struct dwarf_block *) bfd_alloc (abfd, amt);
851 blk->size = read_4_bytes (abfd, info_ptr);
853 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
854 info_ptr += blk->size;
858 attr->u.val = read_2_bytes (abfd, info_ptr);
862 attr->u.val = read_4_bytes (abfd, info_ptr);
866 attr->u.val = read_8_bytes (abfd, info_ptr);
870 attr->u.str = read_string (abfd, info_ptr, &bytes_read);
871 info_ptr += bytes_read;
874 attr->u.str = read_indirect_string (unit, info_ptr, &bytes_read);
875 info_ptr += bytes_read;
877 case DW_FORM_exprloc:
879 amt = sizeof (struct dwarf_block);
880 blk = (struct dwarf_block *) bfd_alloc (abfd, amt);
883 blk->size = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
884 info_ptr += bytes_read;
885 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
886 info_ptr += blk->size;
890 amt = sizeof (struct dwarf_block);
891 blk = (struct dwarf_block *) bfd_alloc (abfd, amt);
894 blk->size = read_1_byte (abfd, info_ptr);
896 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
897 info_ptr += blk->size;
901 attr->u.val = read_1_byte (abfd, info_ptr);
905 attr->u.val = read_1_byte (abfd, info_ptr);
908 case DW_FORM_flag_present:
912 attr->u.sval = read_signed_leb128 (abfd, info_ptr, &bytes_read);
913 info_ptr += bytes_read;
916 attr->u.val = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
917 info_ptr += bytes_read;
920 attr->u.val = read_1_byte (abfd, info_ptr);
924 attr->u.val = read_2_bytes (abfd, info_ptr);
928 attr->u.val = read_4_bytes (abfd, info_ptr);
932 attr->u.val = read_8_bytes (abfd, info_ptr);
935 case DW_FORM_ref_sig8:
936 attr->u.val = read_8_bytes (abfd, info_ptr);
939 case DW_FORM_ref_udata:
940 attr->u.val = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
941 info_ptr += bytes_read;
943 case DW_FORM_indirect:
944 form = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
945 info_ptr += bytes_read;
946 info_ptr = read_attribute_value (attr, form, unit, info_ptr);
949 (*_bfd_error_handler) (_("Dwarf Error: Invalid or unhandled FORM value: %u."),
951 bfd_set_error (bfd_error_bad_value);
957 /* Read an attribute described by an abbreviated attribute. */
960 read_attribute (struct attribute *attr,
961 struct attr_abbrev *abbrev,
962 struct comp_unit *unit,
965 attr->name = abbrev->name;
966 info_ptr = read_attribute_value (attr, abbrev->form, unit, info_ptr);
970 /* Source line information table routines. */
972 #define FILE_ALLOC_CHUNK 5
973 #define DIR_ALLOC_CHUNK 5
977 struct line_info* prev_line;
982 unsigned char op_index;
983 unsigned char end_sequence; /* End of (sequential) code sequence. */
997 struct line_sequence* prev_sequence;
998 struct line_info* last_line; /* Largest VMA. */
1001 struct line_info_table
1004 unsigned int num_files;
1005 unsigned int num_dirs;
1006 unsigned int num_sequences;
1009 struct fileinfo* files;
1010 struct line_sequence* sequences;
1011 struct line_info* lcl_head; /* Local head; used in 'add_line_info'. */
1014 /* Remember some information about each function. If the function is
1015 inlined (DW_TAG_inlined_subroutine) it may have two additional
1016 attributes, DW_AT_call_file and DW_AT_call_line, which specify the
1017 source code location where this function was inlined. */
1021 struct funcinfo *prev_func; /* Pointer to previous function in list of all functions */
1022 struct funcinfo *caller_func; /* Pointer to function one scope higher */
1023 char *caller_file; /* Source location file name where caller_func inlines this func */
1024 int caller_line; /* Source location line number where caller_func inlines this func */
1025 char *file; /* Source location file name */
1026 int line; /* Source location line number */
1029 struct arange arange;
1030 asection *sec; /* Where the symbol is defined */
1035 /* Pointer to previous variable in list of all variables */
1036 struct varinfo *prev_var;
1037 /* Source location file name */
1039 /* Source location line number */
1044 /* Where the symbol is defined */
1046 /* Is this a stack variable? */
1047 unsigned int stack: 1;
1050 /* Return TRUE if NEW_LINE should sort after LINE. */
1052 static inline bfd_boolean
1053 new_line_sorts_after (struct line_info *new_line, struct line_info *line)
1055 return (new_line->address > line->address
1056 || (new_line->address == line->address
1057 && (new_line->op_index > line->op_index
1058 || (new_line->op_index == line->op_index
1059 && new_line->end_sequence < line->end_sequence))));
1063 /* Adds a new entry to the line_info list in the line_info_table, ensuring
1064 that the list is sorted. Note that the line_info list is sorted from
1065 highest to lowest VMA (with possible duplicates); that is,
1066 line_info->prev_line always accesses an equal or smaller VMA. */
1069 add_line_info (struct line_info_table *table,
1071 unsigned char op_index,
1074 unsigned int column,
1077 bfd_size_type amt = sizeof (struct line_info);
1078 struct line_sequence* seq = table->sequences;
1079 struct line_info* info = (struct line_info *) bfd_alloc (table->abfd, amt);
1084 /* Set member data of 'info'. */
1085 info->prev_line = NULL;
1086 info->address = address;
1087 info->op_index = op_index;
1089 info->column = column;
1090 info->end_sequence = end_sequence;
1092 if (filename && filename[0])
1094 info->filename = (char *) bfd_alloc (table->abfd, strlen (filename) + 1);
1095 if (info->filename == NULL)
1097 strcpy (info->filename, filename);
1100 info->filename = NULL;
1102 /* Find the correct location for 'info'. Normally we will receive
1103 new line_info data 1) in order and 2) with increasing VMAs.
1104 However some compilers break the rules (cf. decode_line_info) and
1105 so we include some heuristics for quickly finding the correct
1106 location for 'info'. In particular, these heuristics optimize for
1107 the common case in which the VMA sequence that we receive is a
1108 list of locally sorted VMAs such as
1109 p...z a...j (where a < j < p < z)
1111 Note: table->lcl_head is used to head an *actual* or *possible*
1112 sub-sequence within the list (such as a...j) that is not directly
1113 headed by table->last_line
1115 Note: we may receive duplicate entries from 'decode_line_info'. */
1118 && seq->last_line->address == address
1119 && seq->last_line->op_index == op_index
1120 && seq->last_line->end_sequence == end_sequence)
1122 /* We only keep the last entry with the same address and end
1123 sequence. See PR ld/4986. */
1124 if (table->lcl_head == seq->last_line)
1125 table->lcl_head = info;
1126 info->prev_line = seq->last_line->prev_line;
1127 seq->last_line = info;
1129 else if (!seq || seq->last_line->end_sequence)
1131 /* Start a new line sequence. */
1132 amt = sizeof (struct line_sequence);
1133 seq = (struct line_sequence *) bfd_malloc (amt);
1136 seq->low_pc = address;
1137 seq->prev_sequence = table->sequences;
1138 seq->last_line = info;
1139 table->lcl_head = info;
1140 table->sequences = seq;
1141 table->num_sequences++;
1143 else if (new_line_sorts_after (info, seq->last_line))
1145 /* Normal case: add 'info' to the beginning of the current sequence. */
1146 info->prev_line = seq->last_line;
1147 seq->last_line = info;
1149 /* lcl_head: initialize to head a *possible* sequence at the end. */
1150 if (!table->lcl_head)
1151 table->lcl_head = info;
1153 else if (!new_line_sorts_after (info, table->lcl_head)
1154 && (!table->lcl_head->prev_line
1155 || new_line_sorts_after (info, table->lcl_head->prev_line)))
1157 /* Abnormal but easy: lcl_head is the head of 'info'. */
1158 info->prev_line = table->lcl_head->prev_line;
1159 table->lcl_head->prev_line = info;
1163 /* Abnormal and hard: Neither 'last_line' nor 'lcl_head'
1164 are valid heads for 'info'. Reset 'lcl_head'. */
1165 struct line_info* li2 = seq->last_line; /* Always non-NULL. */
1166 struct line_info* li1 = li2->prev_line;
1170 if (!new_line_sorts_after (info, li2)
1171 && new_line_sorts_after (info, li1))
1174 li2 = li1; /* always non-NULL */
1175 li1 = li1->prev_line;
1177 table->lcl_head = li2;
1178 info->prev_line = table->lcl_head->prev_line;
1179 table->lcl_head->prev_line = info;
1180 if (address < seq->low_pc)
1181 seq->low_pc = address;
1186 /* Extract a fully qualified filename from a line info table.
1187 The returned string has been malloc'ed and it is the caller's
1188 responsibility to free it. */
1191 concat_filename (struct line_info_table *table, unsigned int file)
1195 if (file - 1 >= table->num_files)
1197 /* FILE == 0 means unknown. */
1199 (*_bfd_error_handler)
1200 (_("Dwarf Error: mangled line number section (bad file number)."));
1201 return strdup ("<unknown>");
1204 filename = table->files[file - 1].name;
1206 if (!IS_ABSOLUTE_PATH (filename))
1208 char *dir_name = NULL;
1209 char *subdir_name = NULL;
1213 if (table->files[file - 1].dir)
1214 subdir_name = table->dirs[table->files[file - 1].dir - 1];
1216 if (!subdir_name || !IS_ABSOLUTE_PATH (subdir_name))
1217 dir_name = table->comp_dir;
1221 dir_name = subdir_name;
1226 return strdup (filename);
1228 len = strlen (dir_name) + strlen (filename) + 2;
1232 len += strlen (subdir_name) + 1;
1233 name = (char *) bfd_malloc (len);
1235 sprintf (name, "%s/%s/%s", dir_name, subdir_name, filename);
1239 name = (char *) bfd_malloc (len);
1241 sprintf (name, "%s/%s", dir_name, filename);
1247 return strdup (filename);
1251 arange_add (bfd *abfd, struct arange *first_arange,
1252 bfd_vma low_pc, bfd_vma high_pc)
1254 struct arange *arange;
1256 /* If the first arange is empty, use it. */
1257 if (first_arange->high == 0)
1259 first_arange->low = low_pc;
1260 first_arange->high = high_pc;
1264 /* Next see if we can cheaply extend an existing range. */
1265 arange = first_arange;
1268 if (low_pc == arange->high)
1270 arange->high = high_pc;
1273 if (high_pc == arange->low)
1275 arange->low = low_pc;
1278 arange = arange->next;
1282 /* Need to allocate a new arange and insert it into the arange list.
1283 Order isn't significant, so just insert after the first arange. */
1284 arange = (struct arange *) bfd_zalloc (abfd, sizeof (*arange));
1287 arange->low = low_pc;
1288 arange->high = high_pc;
1289 arange->next = first_arange->next;
1290 first_arange->next = arange;
1294 /* Compare function for line sequences. */
1297 compare_sequences (const void* a, const void* b)
1299 const struct line_sequence* seq1 = a;
1300 const struct line_sequence* seq2 = b;
1302 /* Sort by low_pc as the primary key. */
1303 if (seq1->low_pc < seq2->low_pc)
1305 if (seq1->low_pc > seq2->low_pc)
1308 /* If low_pc values are equal, sort in reverse order of
1309 high_pc, so that the largest region comes first. */
1310 if (seq1->last_line->address < seq2->last_line->address)
1312 if (seq1->last_line->address > seq2->last_line->address)
1315 if (seq1->last_line->op_index < seq2->last_line->op_index)
1317 if (seq1->last_line->op_index > seq2->last_line->op_index)
1323 /* Sort the line sequences for quick lookup. */
1326 sort_line_sequences (struct line_info_table* table)
1329 struct line_sequence* sequences;
1330 struct line_sequence* seq;
1332 unsigned int num_sequences = table->num_sequences;
1333 bfd_vma last_high_pc;
1335 if (num_sequences == 0)
1338 /* Allocate space for an array of sequences. */
1339 amt = sizeof (struct line_sequence) * num_sequences;
1340 sequences = (struct line_sequence *) bfd_alloc (table->abfd, amt);
1341 if (sequences == NULL)
1344 /* Copy the linked list into the array, freeing the original nodes. */
1345 seq = table->sequences;
1346 for (n = 0; n < num_sequences; n++)
1348 struct line_sequence* last_seq = seq;
1351 sequences[n].low_pc = seq->low_pc;
1352 sequences[n].prev_sequence = NULL;
1353 sequences[n].last_line = seq->last_line;
1354 seq = seq->prev_sequence;
1357 BFD_ASSERT (seq == NULL);
1359 qsort (sequences, n, sizeof (struct line_sequence), compare_sequences);
1361 /* Make the list binary-searchable by trimming overlapping entries
1362 and removing nested entries. */
1364 last_high_pc = sequences[0].last_line->address;
1365 for (n = 1; n < table->num_sequences; n++)
1367 if (sequences[n].low_pc < last_high_pc)
1369 if (sequences[n].last_line->address <= last_high_pc)
1370 /* Skip nested entries. */
1373 /* Trim overlapping entries. */
1374 sequences[n].low_pc = last_high_pc;
1376 last_high_pc = sequences[n].last_line->address;
1377 if (n > num_sequences)
1379 /* Close up the gap. */
1380 sequences[num_sequences].low_pc = sequences[n].low_pc;
1381 sequences[num_sequences].last_line = sequences[n].last_line;
1386 table->sequences = sequences;
1387 table->num_sequences = num_sequences;
1391 /* Decode the line number information for UNIT. */
1393 static struct line_info_table*
1394 decode_line_info (struct comp_unit *unit, struct dwarf2_debug *stash)
1396 bfd *abfd = unit->abfd;
1397 struct line_info_table* table;
1400 struct line_head lh;
1401 unsigned int i, bytes_read, offset_size;
1402 char *cur_file, *cur_dir;
1403 unsigned char op_code, extended_op, adj_opcode;
1406 if (! read_section (abfd, &stash->debug_sections[debug_line],
1407 stash->syms, unit->line_offset,
1408 &stash->dwarf_line_buffer, &stash->dwarf_line_size))
1411 amt = sizeof (struct line_info_table);
1412 table = (struct line_info_table *) bfd_alloc (abfd, amt);
1416 table->comp_dir = unit->comp_dir;
1418 table->num_files = 0;
1419 table->files = NULL;
1421 table->num_dirs = 0;
1424 table->num_sequences = 0;
1425 table->sequences = NULL;
1427 table->lcl_head = NULL;
1429 line_ptr = stash->dwarf_line_buffer + unit->line_offset;
1431 /* Read in the prologue. */
1432 lh.total_length = read_4_bytes (abfd, line_ptr);
1435 if (lh.total_length == 0xffffffff)
1437 lh.total_length = read_8_bytes (abfd, line_ptr);
1441 else if (lh.total_length == 0 && unit->addr_size == 8)
1443 /* Handle (non-standard) 64-bit DWARF2 formats. */
1444 lh.total_length = read_4_bytes (abfd, line_ptr);
1448 line_end = line_ptr + lh.total_length;
1449 lh.version = read_2_bytes (abfd, line_ptr);
1450 if (lh.version < 2 || lh.version > 4)
1452 (*_bfd_error_handler)
1453 (_("Dwarf Error: Unhandled .debug_line version %d."), lh.version);
1454 bfd_set_error (bfd_error_bad_value);
1458 if (offset_size == 4)
1459 lh.prologue_length = read_4_bytes (abfd, line_ptr);
1461 lh.prologue_length = read_8_bytes (abfd, line_ptr);
1462 line_ptr += offset_size;
1463 lh.minimum_instruction_length = read_1_byte (abfd, line_ptr);
1465 if (lh.version >= 4)
1467 lh.maximum_ops_per_insn = read_1_byte (abfd, line_ptr);
1471 lh.maximum_ops_per_insn = 1;
1472 if (lh.maximum_ops_per_insn == 0)
1474 (*_bfd_error_handler)
1475 (_("Dwarf Error: Invalid maximum operations per instruction."));
1476 bfd_set_error (bfd_error_bad_value);
1479 lh.default_is_stmt = read_1_byte (abfd, line_ptr);
1481 lh.line_base = read_1_signed_byte (abfd, line_ptr);
1483 lh.line_range = read_1_byte (abfd, line_ptr);
1485 lh.opcode_base = read_1_byte (abfd, line_ptr);
1487 amt = lh.opcode_base * sizeof (unsigned char);
1488 lh.standard_opcode_lengths = (unsigned char *) bfd_alloc (abfd, amt);
1490 lh.standard_opcode_lengths[0] = 1;
1492 for (i = 1; i < lh.opcode_base; ++i)
1494 lh.standard_opcode_lengths[i] = read_1_byte (abfd, line_ptr);
1498 /* Read directory table. */
1499 while ((cur_dir = read_string (abfd, line_ptr, &bytes_read)) != NULL)
1501 line_ptr += bytes_read;
1503 if ((table->num_dirs % DIR_ALLOC_CHUNK) == 0)
1507 amt = table->num_dirs + DIR_ALLOC_CHUNK;
1508 amt *= sizeof (char *);
1510 tmp = (char **) bfd_realloc (table->dirs, amt);
1516 table->dirs[table->num_dirs++] = cur_dir;
1519 line_ptr += bytes_read;
1521 /* Read file name table. */
1522 while ((cur_file = read_string (abfd, line_ptr, &bytes_read)) != NULL)
1524 line_ptr += bytes_read;
1526 if ((table->num_files % FILE_ALLOC_CHUNK) == 0)
1528 struct fileinfo *tmp;
1530 amt = table->num_files + FILE_ALLOC_CHUNK;
1531 amt *= sizeof (struct fileinfo);
1533 tmp = (struct fileinfo *) bfd_realloc (table->files, amt);
1539 table->files[table->num_files].name = cur_file;
1540 table->files[table->num_files].dir =
1541 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
1542 line_ptr += bytes_read;
1543 table->files[table->num_files].time =
1544 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
1545 line_ptr += bytes_read;
1546 table->files[table->num_files].size =
1547 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
1548 line_ptr += bytes_read;
1552 line_ptr += bytes_read;
1554 /* Read the statement sequences until there's nothing left. */
1555 while (line_ptr < line_end)
1557 /* State machine registers. */
1558 bfd_vma address = 0;
1559 unsigned char op_index = 0;
1560 char * filename = table->num_files ? concat_filename (table, 1) : NULL;
1561 unsigned int line = 1;
1562 unsigned int column = 0;
1563 int is_stmt = lh.default_is_stmt;
1564 int end_sequence = 0;
1565 /* eraxxon@alumni.rice.edu: Against the DWARF2 specs, some
1566 compilers generate address sequences that are wildly out of
1567 order using DW_LNE_set_address (e.g. Intel C++ 6.0 compiler
1568 for ia64-Linux). Thus, to determine the low and high
1569 address, we must compare on every DW_LNS_copy, etc. */
1570 bfd_vma low_pc = (bfd_vma) -1;
1571 bfd_vma high_pc = 0;
1573 /* Decode the table. */
1574 while (! end_sequence)
1576 op_code = read_1_byte (abfd, line_ptr);
1579 if (op_code >= lh.opcode_base)
1581 /* Special operand. */
1582 adj_opcode = op_code - lh.opcode_base;
1583 if (lh.maximum_ops_per_insn == 1)
1584 address += (adj_opcode / lh.line_range)
1585 * lh.minimum_instruction_length;
1588 address += ((op_index + (adj_opcode / lh.line_range))
1589 / lh.maximum_ops_per_insn)
1590 * lh.minimum_instruction_length;
1591 op_index = (op_index + (adj_opcode / lh.line_range))
1592 % lh.maximum_ops_per_insn;
1594 line += lh.line_base + (adj_opcode % lh.line_range);
1595 /* Append row to matrix using current values. */
1596 if (!add_line_info (table, address, op_index, filename,
1599 if (address < low_pc)
1601 if (address > high_pc)
1604 else switch (op_code)
1606 case DW_LNS_extended_op:
1607 /* Ignore length. */
1609 extended_op = read_1_byte (abfd, line_ptr);
1612 switch (extended_op)
1614 case DW_LNE_end_sequence:
1616 if (!add_line_info (table, address, op_index, filename,
1617 line, column, end_sequence))
1619 if (address < low_pc)
1621 if (address > high_pc)
1623 if (!arange_add (unit->abfd, &unit->arange, low_pc, high_pc))
1626 case DW_LNE_set_address:
1627 address = read_address (unit, line_ptr);
1629 line_ptr += unit->addr_size;
1631 case DW_LNE_define_file:
1632 cur_file = read_string (abfd, line_ptr, &bytes_read);
1633 line_ptr += bytes_read;
1634 if ((table->num_files % FILE_ALLOC_CHUNK) == 0)
1636 struct fileinfo *tmp;
1638 amt = table->num_files + FILE_ALLOC_CHUNK;
1639 amt *= sizeof (struct fileinfo);
1640 tmp = (struct fileinfo *) bfd_realloc (table->files, amt);
1645 table->files[table->num_files].name = cur_file;
1646 table->files[table->num_files].dir =
1647 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
1648 line_ptr += bytes_read;
1649 table->files[table->num_files].time =
1650 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
1651 line_ptr += bytes_read;
1652 table->files[table->num_files].size =
1653 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
1654 line_ptr += bytes_read;
1657 case DW_LNE_set_discriminator:
1658 (void) read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
1659 line_ptr += bytes_read;
1662 (*_bfd_error_handler) (_("Dwarf Error: mangled line number section."));
1663 bfd_set_error (bfd_error_bad_value);
1665 if (filename != NULL)
1671 if (!add_line_info (table, address, op_index,
1672 filename, line, column, 0))
1674 if (address < low_pc)
1676 if (address > high_pc)
1679 case DW_LNS_advance_pc:
1680 if (lh.maximum_ops_per_insn == 1)
1681 address += lh.minimum_instruction_length
1682 * read_unsigned_leb128 (abfd, line_ptr,
1686 bfd_vma adjust = read_unsigned_leb128 (abfd, line_ptr,
1688 address = ((op_index + adjust) / lh.maximum_ops_per_insn)
1689 * lh.minimum_instruction_length;
1690 op_index = (op_index + adjust) % lh.maximum_ops_per_insn;
1692 line_ptr += bytes_read;
1694 case DW_LNS_advance_line:
1695 line += read_signed_leb128 (abfd, line_ptr, &bytes_read);
1696 line_ptr += bytes_read;
1698 case DW_LNS_set_file:
1702 /* The file and directory tables are 0
1703 based, the references are 1 based. */
1704 file = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
1705 line_ptr += bytes_read;
1708 filename = concat_filename (table, file);
1711 case DW_LNS_set_column:
1712 column = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
1713 line_ptr += bytes_read;
1715 case DW_LNS_negate_stmt:
1716 is_stmt = (!is_stmt);
1718 case DW_LNS_set_basic_block:
1720 case DW_LNS_const_add_pc:
1721 if (lh.maximum_ops_per_insn == 1)
1722 address += lh.minimum_instruction_length
1723 * ((255 - lh.opcode_base) / lh.line_range);
1726 bfd_vma adjust = ((255 - lh.opcode_base) / lh.line_range);
1727 address += lh.minimum_instruction_length
1728 * ((op_index + adjust) / lh.maximum_ops_per_insn);
1729 op_index = (op_index + adjust) % lh.maximum_ops_per_insn;
1732 case DW_LNS_fixed_advance_pc:
1733 address += read_2_bytes (abfd, line_ptr);
1738 /* Unknown standard opcode, ignore it. */
1739 for (i = 0; i < lh.standard_opcode_lengths[op_code]; i++)
1741 (void) read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
1742 line_ptr += bytes_read;
1752 if (sort_line_sequences (table))
1756 if (table->sequences != NULL)
1757 free (table->sequences);
1758 if (table->files != NULL)
1759 free (table->files);
1760 if (table->dirs != NULL)
1765 /* If ADDR is within TABLE set the output parameters and return TRUE,
1766 otherwise return FALSE. The output parameters, FILENAME_PTR and
1767 LINENUMBER_PTR, are pointers to the objects to be filled in. */
1770 lookup_address_in_line_info_table (struct line_info_table *table,
1772 const char **filename_ptr,
1773 unsigned int *linenumber_ptr)
1775 struct line_sequence *seq = NULL;
1776 struct line_info *each_line;
1779 /* Binary search the array of sequences. */
1781 high = table->num_sequences;
1784 mid = (low + high) / 2;
1785 seq = &table->sequences[mid];
1786 if (addr < seq->low_pc)
1788 else if (addr >= seq->last_line->address)
1794 if (seq && addr >= seq->low_pc && addr < seq->last_line->address)
1796 /* Note: seq->last_line should be a descendingly sorted list. */
1797 for (each_line = seq->last_line;
1799 each_line = each_line->prev_line)
1800 if (addr >= each_line->address)
1804 && !(each_line->end_sequence || each_line == seq->last_line))
1806 *filename_ptr = each_line->filename;
1807 *linenumber_ptr = each_line->line;
1812 *filename_ptr = NULL;
1816 /* Read in the .debug_ranges section for future reference. */
1819 read_debug_ranges (struct comp_unit *unit)
1821 struct dwarf2_debug *stash = unit->stash;
1822 return read_section (unit->abfd, &stash->debug_sections[debug_ranges],
1824 &stash->dwarf_ranges_buffer, &stash->dwarf_ranges_size);
1827 /* Function table functions. */
1829 /* If ADDR is within TABLE, set FUNCTIONNAME_PTR, and return TRUE.
1830 Note that we need to find the function that has the smallest
1831 range that contains ADDR, to handle inlined functions without
1832 depending upon them being ordered in TABLE by increasing range. */
1835 lookup_address_in_function_table (struct comp_unit *unit,
1837 struct funcinfo **function_ptr,
1838 const char **functionname_ptr)
1840 struct funcinfo* each_func;
1841 struct funcinfo* best_fit = NULL;
1842 struct arange *arange;
1844 for (each_func = unit->function_table;
1846 each_func = each_func->prev_func)
1848 for (arange = &each_func->arange;
1850 arange = arange->next)
1852 if (addr >= arange->low && addr < arange->high)
1855 ((arange->high - arange->low) < (best_fit->arange.high - best_fit->arange.low)))
1856 best_fit = each_func;
1863 *functionname_ptr = best_fit->name;
1864 *function_ptr = best_fit;
1873 /* If SYM at ADDR is within function table of UNIT, set FILENAME_PTR
1874 and LINENUMBER_PTR, and return TRUE. */
1877 lookup_symbol_in_function_table (struct comp_unit *unit,
1880 const char **filename_ptr,
1881 unsigned int *linenumber_ptr)
1883 struct funcinfo* each_func;
1884 struct funcinfo* best_fit = NULL;
1885 struct arange *arange;
1886 const char *name = bfd_asymbol_name (sym);
1887 asection *sec = bfd_get_section (sym);
1889 for (each_func = unit->function_table;
1891 each_func = each_func->prev_func)
1893 for (arange = &each_func->arange;
1895 arange = arange->next)
1897 if ((!each_func->sec || each_func->sec == sec)
1898 && addr >= arange->low
1899 && addr < arange->high
1901 && strcmp (name, each_func->name) == 0
1903 || ((arange->high - arange->low)
1904 < (best_fit->arange.high - best_fit->arange.low))))
1905 best_fit = each_func;
1911 best_fit->sec = sec;
1912 *filename_ptr = best_fit->file;
1913 *linenumber_ptr = best_fit->line;
1920 /* Variable table functions. */
1922 /* If SYM is within variable table of UNIT, set FILENAME_PTR and
1923 LINENUMBER_PTR, and return TRUE. */
1926 lookup_symbol_in_variable_table (struct comp_unit *unit,
1929 const char **filename_ptr,
1930 unsigned int *linenumber_ptr)
1932 const char *name = bfd_asymbol_name (sym);
1933 asection *sec = bfd_get_section (sym);
1934 struct varinfo* each;
1936 for (each = unit->variable_table; each; each = each->prev_var)
1937 if (each->stack == 0
1938 && each->file != NULL
1939 && each->name != NULL
1940 && each->addr == addr
1941 && (!each->sec || each->sec == sec)
1942 && strcmp (name, each->name) == 0)
1948 *filename_ptr = each->file;
1949 *linenumber_ptr = each->line;
1957 find_abstract_instance_name (struct comp_unit *unit,
1958 struct attribute *attr_ptr)
1960 bfd *abfd = unit->abfd;
1962 unsigned int abbrev_number, bytes_read, i;
1963 struct abbrev_info *abbrev;
1964 bfd_uint64_t die_ref = attr_ptr->u.val;
1965 struct attribute attr;
1968 /* DW_FORM_ref_addr can reference an entry in a different CU. It
1969 is an offset from the .debug_info section, not the current CU. */
1970 if (attr_ptr->form == DW_FORM_ref_addr)
1972 /* We only support DW_FORM_ref_addr within the same file, so
1973 any relocations should be resolved already. */
1977 info_ptr = unit->sec_info_ptr + die_ref;
1980 info_ptr = unit->info_ptr_unit + die_ref;
1981 abbrev_number = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
1982 info_ptr += bytes_read;
1986 abbrev = lookup_abbrev (abbrev_number, unit->abbrevs);
1989 (*_bfd_error_handler) (_("Dwarf Error: Could not find abbrev number %u."),
1991 bfd_set_error (bfd_error_bad_value);
1995 for (i = 0; i < abbrev->num_attrs; ++i)
1997 info_ptr = read_attribute (&attr, &abbrev->attrs[i], unit,
1999 if (info_ptr == NULL)
2004 /* Prefer DW_AT_MIPS_linkage_name or DW_AT_linkage_name
2009 case DW_AT_specification:
2010 name = find_abstract_instance_name (unit, &attr);
2012 case DW_AT_linkage_name:
2013 case DW_AT_MIPS_linkage_name:
2026 read_rangelist (struct comp_unit *unit, struct arange *arange,
2027 bfd_uint64_t offset)
2029 bfd_byte *ranges_ptr;
2030 bfd_vma base_address = unit->base_address;
2032 if (! unit->stash->dwarf_ranges_buffer)
2034 if (! read_debug_ranges (unit))
2037 ranges_ptr = unit->stash->dwarf_ranges_buffer + offset;
2044 low_pc = read_address (unit, ranges_ptr);
2045 ranges_ptr += unit->addr_size;
2046 high_pc = read_address (unit, ranges_ptr);
2047 ranges_ptr += unit->addr_size;
2049 if (low_pc == 0 && high_pc == 0)
2051 if (low_pc == -1UL && high_pc != -1UL)
2052 base_address = high_pc;
2055 if (!arange_add (unit->abfd, arange,
2056 base_address + low_pc, base_address + high_pc))
2063 /* DWARF2 Compilation unit functions. */
2065 /* Scan over each die in a comp. unit looking for functions to add
2066 to the function table and variables to the variable table. */
2069 scan_unit_for_symbols (struct comp_unit *unit)
2071 bfd *abfd = unit->abfd;
2072 bfd_byte *info_ptr = unit->first_child_die_ptr;
2073 int nesting_level = 1;
2074 struct funcinfo **nested_funcs;
2075 int nested_funcs_size;
2077 /* Maintain a stack of in-scope functions and inlined functions, which we
2078 can use to set the caller_func field. */
2079 nested_funcs_size = 32;
2080 nested_funcs = (struct funcinfo **)
2081 bfd_malloc (nested_funcs_size * sizeof (struct funcinfo *));
2082 if (nested_funcs == NULL)
2084 nested_funcs[nesting_level] = 0;
2086 while (nesting_level)
2088 unsigned int abbrev_number, bytes_read, i;
2089 struct abbrev_info *abbrev;
2090 struct attribute attr;
2091 struct funcinfo *func;
2092 struct varinfo *var;
2094 bfd_vma high_pc = 0;
2096 abbrev_number = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
2097 info_ptr += bytes_read;
2099 if (! abbrev_number)
2105 abbrev = lookup_abbrev (abbrev_number,unit->abbrevs);
2108 (*_bfd_error_handler)
2109 (_("Dwarf Error: Could not find abbrev number %u."),
2111 bfd_set_error (bfd_error_bad_value);
2116 if (abbrev->tag == DW_TAG_subprogram
2117 || abbrev->tag == DW_TAG_entry_point
2118 || abbrev->tag == DW_TAG_inlined_subroutine)
2120 bfd_size_type amt = sizeof (struct funcinfo);
2121 func = (struct funcinfo *) bfd_zalloc (abfd, amt);
2124 func->tag = abbrev->tag;
2125 func->prev_func = unit->function_table;
2126 unit->function_table = func;
2127 BFD_ASSERT (!unit->cached);
2129 if (func->tag == DW_TAG_inlined_subroutine)
2130 for (i = nesting_level - 1; i >= 1; i--)
2131 if (nested_funcs[i])
2133 func->caller_func = nested_funcs[i];
2136 nested_funcs[nesting_level] = func;
2141 if (abbrev->tag == DW_TAG_variable)
2143 bfd_size_type amt = sizeof (struct varinfo);
2144 var = (struct varinfo *) bfd_zalloc (abfd, amt);
2147 var->tag = abbrev->tag;
2149 var->prev_var = unit->variable_table;
2150 unit->variable_table = var;
2151 BFD_ASSERT (!unit->cached);
2154 /* No inline function in scope at this nesting level. */
2155 nested_funcs[nesting_level] = 0;
2158 for (i = 0; i < abbrev->num_attrs; ++i)
2160 info_ptr = read_attribute (&attr, &abbrev->attrs[i], unit, info_ptr);
2161 if (info_ptr == NULL)
2168 case DW_AT_call_file:
2169 func->caller_file = concat_filename (unit->line_table,
2173 case DW_AT_call_line:
2174 func->caller_line = attr.u.val;
2177 case DW_AT_abstract_origin:
2178 case DW_AT_specification:
2179 func->name = find_abstract_instance_name (unit, &attr);
2183 /* Prefer DW_AT_MIPS_linkage_name or DW_AT_linkage_name
2185 if (func->name == NULL)
2186 func->name = attr.u.str;
2189 case DW_AT_linkage_name:
2190 case DW_AT_MIPS_linkage_name:
2191 func->name = attr.u.str;
2195 low_pc = attr.u.val;
2199 high_pc = attr.u.val;
2203 if (!read_rangelist (unit, &func->arange, attr.u.val))
2207 case DW_AT_decl_file:
2208 func->file = concat_filename (unit->line_table,
2212 case DW_AT_decl_line:
2213 func->line = attr.u.val;
2225 var->name = attr.u.str;
2228 case DW_AT_decl_file:
2229 var->file = concat_filename (unit->line_table,
2233 case DW_AT_decl_line:
2234 var->line = attr.u.val;
2237 case DW_AT_external:
2238 if (attr.u.val != 0)
2242 case DW_AT_location:
2246 case DW_FORM_block1:
2247 case DW_FORM_block2:
2248 case DW_FORM_block4:
2249 case DW_FORM_exprloc:
2250 if (*attr.u.blk->data == DW_OP_addr)
2254 /* Verify that DW_OP_addr is the only opcode in the
2255 location, in which case the block size will be 1
2256 plus the address size. */
2257 /* ??? For TLS variables, gcc can emit
2258 DW_OP_addr <addr> DW_OP_GNU_push_tls_address
2259 which we don't handle here yet. */
2260 if (attr.u.blk->size == unit->addr_size + 1U)
2261 var->addr = bfd_get (unit->addr_size * 8,
2263 attr.u.blk->data + 1);
2278 if (func && high_pc != 0)
2280 if (!arange_add (unit->abfd, &func->arange, low_pc, high_pc))
2284 if (abbrev->has_children)
2288 if (nesting_level >= nested_funcs_size)
2290 struct funcinfo **tmp;
2292 nested_funcs_size *= 2;
2293 tmp = (struct funcinfo **)
2294 bfd_realloc (nested_funcs,
2295 (nested_funcs_size * sizeof (struct funcinfo *)));
2300 nested_funcs[nesting_level] = 0;
2304 free (nested_funcs);
2308 free (nested_funcs);
2312 /* Parse a DWARF2 compilation unit starting at INFO_PTR. This
2313 includes the compilation unit header that proceeds the DIE's, but
2314 does not include the length field that precedes each compilation
2315 unit header. END_PTR points one past the end of this comp unit.
2316 OFFSET_SIZE is the size of DWARF2 offsets (either 4 or 8 bytes).
2318 This routine does not read the whole compilation unit; only enough
2319 to get to the line number information for the compilation unit. */
2321 static struct comp_unit *
2322 parse_comp_unit (struct dwarf2_debug *stash,
2323 bfd_vma unit_length,
2324 bfd_byte *info_ptr_unit,
2325 unsigned int offset_size)
2327 struct comp_unit* unit;
2328 unsigned int version;
2329 bfd_uint64_t abbrev_offset = 0;
2330 unsigned int addr_size;
2331 struct abbrev_info** abbrevs;
2332 unsigned int abbrev_number, bytes_read, i;
2333 struct abbrev_info *abbrev;
2334 struct attribute attr;
2335 bfd_byte *info_ptr = stash->info_ptr;
2336 bfd_byte *end_ptr = info_ptr + unit_length;
2339 bfd_vma high_pc = 0;
2340 bfd *abfd = stash->bfd_ptr;
2342 version = read_2_bytes (abfd, info_ptr);
2344 BFD_ASSERT (offset_size == 4 || offset_size == 8);
2345 if (offset_size == 4)
2346 abbrev_offset = read_4_bytes (abfd, info_ptr);
2348 abbrev_offset = read_8_bytes (abfd, info_ptr);
2349 info_ptr += offset_size;
2350 addr_size = read_1_byte (abfd, info_ptr);
2353 if (version != 2 && version != 3 && version != 4)
2355 (*_bfd_error_handler) (_("Dwarf Error: found dwarf version '%u', this reader only handles version 2, 3 and 4 information."), version);
2356 bfd_set_error (bfd_error_bad_value);
2360 if (addr_size > sizeof (bfd_vma))
2362 (*_bfd_error_handler) (_("Dwarf Error: found address size '%u', this reader can not handle sizes greater than '%u'."),
2364 (unsigned int) sizeof (bfd_vma));
2365 bfd_set_error (bfd_error_bad_value);
2369 if (addr_size != 2 && addr_size != 4 && addr_size != 8)
2371 (*_bfd_error_handler) ("Dwarf Error: found address size '%u', this reader can only handle address sizes '2', '4' and '8'.", addr_size);
2372 bfd_set_error (bfd_error_bad_value);
2376 /* Read the abbrevs for this compilation unit into a table. */
2377 abbrevs = read_abbrevs (abfd, abbrev_offset, stash);
2381 abbrev_number = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
2382 info_ptr += bytes_read;
2383 if (! abbrev_number)
2385 (*_bfd_error_handler) (_("Dwarf Error: Bad abbrev number: %u."),
2387 bfd_set_error (bfd_error_bad_value);
2391 abbrev = lookup_abbrev (abbrev_number, abbrevs);
2394 (*_bfd_error_handler) (_("Dwarf Error: Could not find abbrev number %u."),
2396 bfd_set_error (bfd_error_bad_value);
2400 amt = sizeof (struct comp_unit);
2401 unit = (struct comp_unit *) bfd_zalloc (abfd, amt);
2405 unit->version = version;
2406 unit->addr_size = addr_size;
2407 unit->offset_size = offset_size;
2408 unit->abbrevs = abbrevs;
2409 unit->end_ptr = end_ptr;
2410 unit->stash = stash;
2411 unit->info_ptr_unit = info_ptr_unit;
2412 unit->sec_info_ptr = stash->sec_info_ptr;
2414 for (i = 0; i < abbrev->num_attrs; ++i)
2416 info_ptr = read_attribute (&attr, &abbrev->attrs[i], unit, info_ptr);
2417 if (info_ptr == NULL)
2420 /* Store the data if it is of an attribute we want to keep in a
2421 partial symbol table. */
2424 case DW_AT_stmt_list:
2426 unit->line_offset = attr.u.val;
2430 unit->name = attr.u.str;
2434 low_pc = attr.u.val;
2435 /* If the compilation unit DIE has a DW_AT_low_pc attribute,
2436 this is the base address to use when reading location
2437 lists or range lists. */
2438 unit->base_address = low_pc;
2442 high_pc = attr.u.val;
2446 if (!read_rangelist (unit, &unit->arange, attr.u.val))
2450 case DW_AT_comp_dir:
2452 char *comp_dir = attr.u.str;
2455 /* Irix 6.2 native cc prepends <machine>.: to the compilation
2456 directory, get rid of it. */
2457 char *cp = strchr (comp_dir, ':');
2459 if (cp && cp != comp_dir && cp[-1] == '.' && cp[1] == '/')
2462 unit->comp_dir = comp_dir;
2472 if (!arange_add (unit->abfd, &unit->arange, low_pc, high_pc))
2476 unit->first_child_die_ptr = info_ptr;
2480 /* Return TRUE if UNIT may contain the address given by ADDR. When
2481 there are functions written entirely with inline asm statements, the
2482 range info in the compilation unit header may not be correct. We
2483 need to consult the line info table to see if a compilation unit
2484 really contains the given address. */
2487 comp_unit_contains_address (struct comp_unit *unit, bfd_vma addr)
2489 struct arange *arange;
2494 arange = &unit->arange;
2497 if (addr >= arange->low && addr < arange->high)
2499 arange = arange->next;
2506 /* If UNIT contains ADDR, set the output parameters to the values for
2507 the line containing ADDR. The output parameters, FILENAME_PTR,
2508 FUNCTIONNAME_PTR, and LINENUMBER_PTR, are pointers to the objects
2511 Return TRUE if UNIT contains ADDR, and no errors were encountered;
2515 comp_unit_find_nearest_line (struct comp_unit *unit,
2517 const char **filename_ptr,
2518 const char **functionname_ptr,
2519 unsigned int *linenumber_ptr,
2520 struct dwarf2_debug *stash)
2524 struct funcinfo *function;
2529 if (! unit->line_table)
2531 if (! unit->stmtlist)
2537 unit->line_table = decode_line_info (unit, stash);
2539 if (! unit->line_table)
2545 if (unit->first_child_die_ptr < unit->end_ptr
2546 && ! scan_unit_for_symbols (unit))
2554 func_p = lookup_address_in_function_table (unit, addr,
2555 &function, functionname_ptr);
2556 if (func_p && (function->tag == DW_TAG_inlined_subroutine))
2557 stash->inliner_chain = function;
2558 line_p = lookup_address_in_line_info_table (unit->line_table, addr,
2561 return line_p || func_p;
2564 /* Check to see if line info is already decoded in a comp_unit.
2565 If not, decode it. Returns TRUE if no errors were encountered;
2569 comp_unit_maybe_decode_line_info (struct comp_unit *unit,
2570 struct dwarf2_debug *stash)
2575 if (! unit->line_table)
2577 if (! unit->stmtlist)
2583 unit->line_table = decode_line_info (unit, stash);
2585 if (! unit->line_table)
2591 if (unit->first_child_die_ptr < unit->end_ptr
2592 && ! scan_unit_for_symbols (unit))
2602 /* If UNIT contains SYM at ADDR, set the output parameters to the
2603 values for the line containing SYM. The output parameters,
2604 FILENAME_PTR, and LINENUMBER_PTR, are pointers to the objects to be
2607 Return TRUE if UNIT contains SYM, and no errors were encountered;
2611 comp_unit_find_line (struct comp_unit *unit,
2614 const char **filename_ptr,
2615 unsigned int *linenumber_ptr,
2616 struct dwarf2_debug *stash)
2618 if (!comp_unit_maybe_decode_line_info (unit, stash))
2621 if (sym->flags & BSF_FUNCTION)
2622 return lookup_symbol_in_function_table (unit, sym, addr,
2626 return lookup_symbol_in_variable_table (unit, sym, addr,
2631 static struct funcinfo *
2632 reverse_funcinfo_list (struct funcinfo *head)
2634 struct funcinfo *rhead;
2635 struct funcinfo *temp;
2637 for (rhead = NULL; head; head = temp)
2639 temp = head->prev_func;
2640 head->prev_func = rhead;
2646 static struct varinfo *
2647 reverse_varinfo_list (struct varinfo *head)
2649 struct varinfo *rhead;
2650 struct varinfo *temp;
2652 for (rhead = NULL; head; head = temp)
2654 temp = head->prev_var;
2655 head->prev_var = rhead;
2661 /* Extract all interesting funcinfos and varinfos of a compilation
2662 unit into hash tables for faster lookup. Returns TRUE if no
2663 errors were enountered; FALSE otherwise. */
2666 comp_unit_hash_info (struct dwarf2_debug *stash,
2667 struct comp_unit *unit,
2668 struct info_hash_table *funcinfo_hash_table,
2669 struct info_hash_table *varinfo_hash_table)
2671 struct funcinfo* each_func;
2672 struct varinfo* each_var;
2673 bfd_boolean okay = TRUE;
2675 BFD_ASSERT (stash->info_hash_status != STASH_INFO_HASH_DISABLED);
2677 if (!comp_unit_maybe_decode_line_info (unit, stash))
2680 BFD_ASSERT (!unit->cached);
2682 /* To preserve the original search order, we went to visit the function
2683 infos in the reversed order of the list. However, making the list
2684 bi-directional use quite a bit of extra memory. So we reverse
2685 the list first, traverse the list in the now reversed order and
2686 finally reverse the list again to get back the original order. */
2687 unit->function_table = reverse_funcinfo_list (unit->function_table);
2688 for (each_func = unit->function_table;
2690 each_func = each_func->prev_func)
2692 /* Skip nameless functions. */
2693 if (each_func->name)
2694 /* There is no need to copy name string into hash table as
2695 name string is either in the dwarf string buffer or
2696 info in the stash. */
2697 okay = insert_info_hash_table (funcinfo_hash_table, each_func->name,
2698 (void*) each_func, FALSE);
2700 unit->function_table = reverse_funcinfo_list (unit->function_table);
2704 /* We do the same for variable infos. */
2705 unit->variable_table = reverse_varinfo_list (unit->variable_table);
2706 for (each_var = unit->variable_table;
2708 each_var = each_var->prev_var)
2710 /* Skip stack vars and vars with no files or names. */
2711 if (each_var->stack == 0
2712 && each_var->file != NULL
2713 && each_var->name != NULL)
2714 /* There is no need to copy name string into hash table as
2715 name string is either in the dwarf string buffer or
2716 info in the stash. */
2717 okay = insert_info_hash_table (varinfo_hash_table, each_var->name,
2718 (void*) each_var, FALSE);
2721 unit->variable_table = reverse_varinfo_list (unit->variable_table);
2722 unit->cached = TRUE;
2726 /* Locate a section in a BFD containing debugging info. The search starts
2727 from the section after AFTER_SEC, or from the first section in the BFD if
2728 AFTER_SEC is NULL. The search works by examining the names of the
2729 sections. There are three permissiable names. The first two are given
2730 by DEBUG_SECTIONS[debug_info] (whose standard DWARF2 names are .debug_info
2731 and .zdebug_info). The third is a prefix .gnu.linkonce.wi.
2732 This is a variation on the .debug_info section which has a checksum
2733 describing the contents appended onto the name. This allows the linker to
2734 identify and discard duplicate debugging sections for different
2735 compilation units. */
2736 #define GNU_LINKONCE_INFO ".gnu.linkonce.wi."
2739 find_debug_info (bfd *abfd, const struct dwarf_debug_section *debug_sections,
2740 asection *after_sec)
2744 msec = after_sec != NULL ? after_sec->next : abfd->sections;
2748 if (strcmp (msec->name,
2749 debug_sections[debug_info].uncompressed_name) == 0)
2752 if (debug_sections[debug_info].compressed_name != NULL
2753 && strcmp (msec->name,
2754 debug_sections[debug_info].compressed_name) == 0)
2757 if (CONST_STRNEQ (msec->name, GNU_LINKONCE_INFO))
2766 /* Unset vmas for adjusted sections in STASH. */
2769 unset_sections (struct dwarf2_debug *stash)
2772 struct adjusted_section *p;
2774 i = stash->adjusted_section_count;
2775 p = stash->adjusted_sections;
2776 for (; i > 0; i--, p++)
2777 p->section->vma = 0;
2780 /* Set unique VMAs for loadable and DWARF sections in ABFD and save
2781 VMAs in STASH for unset_sections. */
2784 place_sections (bfd *abfd, struct dwarf2_debug *stash)
2786 struct adjusted_section *p;
2789 if (stash->adjusted_section_count != 0)
2791 i = stash->adjusted_section_count;
2792 p = stash->adjusted_sections;
2793 for (; i > 0; i--, p++)
2794 p->section->vma = p->adj_vma;
2799 bfd_vma last_vma = 0, last_dwarf = 0;
2801 const char *debug_info_name;
2803 debug_info_name = stash->debug_sections[debug_info].uncompressed_name;
2805 for (sect = abfd->sections; sect != NULL; sect = sect->next)
2813 /* We need to adjust the VMAs of any .debug_info sections.
2814 Skip compressed ones, since no relocations could target
2815 them - they should not appear in object files anyway. */
2816 if (strcmp (sect->name, debug_info_name) == 0)
2818 else if (CONST_STRNEQ (sect->name, GNU_LINKONCE_INFO))
2823 if (!is_debug_info && (sect->flags & SEC_LOAD) == 0)
2826 sz = sect->rawsize ? sect->rawsize : sect->size;
2833 amt = i * sizeof (struct adjusted_section);
2834 p = (struct adjusted_section *) bfd_zalloc (abfd, amt);
2838 stash->adjusted_sections = p;
2839 stash->adjusted_section_count = i;
2841 for (sect = abfd->sections; sect != NULL; sect = sect->next)
2849 /* We need to adjust the VMAs of any .debug_info sections.
2850 Skip compressed ones, since no relocations could target
2851 them - they should not appear in object files anyway. */
2852 if (strcmp (sect->name, debug_info_name) == 0)
2854 else if (CONST_STRNEQ (sect->name, GNU_LINKONCE_INFO))
2859 if (!is_debug_info && (sect->flags & SEC_LOAD) == 0)
2862 sz = sect->rawsize ? sect->rawsize : sect->size;
2869 BFD_ASSERT (sect->alignment_power == 0);
2870 sect->vma = last_dwarf;
2873 else if (last_vma != 0)
2875 /* Align the new address to the current section
2877 last_vma = ((last_vma
2878 + ~((bfd_vma) -1 << sect->alignment_power))
2879 & ((bfd_vma) -1 << sect->alignment_power));
2880 sect->vma = last_vma;
2881 last_vma += sect->vma + sz;
2884 last_vma += sect->vma + sz;
2886 p->adj_vma = sect->vma;
2895 /* Look up a funcinfo by name using the given info hash table. If found,
2896 also update the locations pointed to by filename_ptr and linenumber_ptr.
2898 This function returns TRUE if a funcinfo that matches the given symbol
2899 and address is found with any error; otherwise it returns FALSE. */
2902 info_hash_lookup_funcinfo (struct info_hash_table *hash_table,
2905 const char **filename_ptr,
2906 unsigned int *linenumber_ptr)
2908 struct funcinfo* each_func;
2909 struct funcinfo* best_fit = NULL;
2910 struct info_list_node *node;
2911 struct arange *arange;
2912 const char *name = bfd_asymbol_name (sym);
2913 asection *sec = bfd_get_section (sym);
2915 for (node = lookup_info_hash_table (hash_table, name);
2919 each_func = (struct funcinfo *) node->info;
2920 for (arange = &each_func->arange;
2922 arange = arange->next)
2924 if ((!each_func->sec || each_func->sec == sec)
2925 && addr >= arange->low
2926 && addr < arange->high
2928 || ((arange->high - arange->low)
2929 < (best_fit->arange.high - best_fit->arange.low))))
2930 best_fit = each_func;
2936 best_fit->sec = sec;
2937 *filename_ptr = best_fit->file;
2938 *linenumber_ptr = best_fit->line;
2945 /* Look up a varinfo by name using the given info hash table. If found,
2946 also update the locations pointed to by filename_ptr and linenumber_ptr.
2948 This function returns TRUE if a varinfo that matches the given symbol
2949 and address is found with any error; otherwise it returns FALSE. */
2952 info_hash_lookup_varinfo (struct info_hash_table *hash_table,
2955 const char **filename_ptr,
2956 unsigned int *linenumber_ptr)
2958 const char *name = bfd_asymbol_name (sym);
2959 asection *sec = bfd_get_section (sym);
2960 struct varinfo* each;
2961 struct info_list_node *node;
2963 for (node = lookup_info_hash_table (hash_table, name);
2967 each = (struct varinfo *) node->info;
2968 if (each->addr == addr
2969 && (!each->sec || each->sec == sec))
2972 *filename_ptr = each->file;
2973 *linenumber_ptr = each->line;
2981 /* Update the funcinfo and varinfo info hash tables if they are
2982 not up to date. Returns TRUE if there is no error; otherwise
2983 returns FALSE and disable the info hash tables. */
2986 stash_maybe_update_info_hash_tables (struct dwarf2_debug *stash)
2988 struct comp_unit *each;
2990 /* Exit if hash tables are up-to-date. */
2991 if (stash->all_comp_units == stash->hash_units_head)
2994 if (stash->hash_units_head)
2995 each = stash->hash_units_head->prev_unit;
2997 each = stash->last_comp_unit;
3001 if (!comp_unit_hash_info (stash, each, stash->funcinfo_hash_table,
3002 stash->varinfo_hash_table))
3004 stash->info_hash_status = STASH_INFO_HASH_DISABLED;
3007 each = each->prev_unit;
3010 stash->hash_units_head = stash->all_comp_units;
3014 /* Check consistency of info hash tables. This is for debugging only. */
3016 static void ATTRIBUTE_UNUSED
3017 stash_verify_info_hash_table (struct dwarf2_debug *stash)
3019 struct comp_unit *each_unit;
3020 struct funcinfo *each_func;
3021 struct varinfo *each_var;
3022 struct info_list_node *node;
3025 for (each_unit = stash->all_comp_units;
3027 each_unit = each_unit->next_unit)
3029 for (each_func = each_unit->function_table;
3031 each_func = each_func->prev_func)
3033 if (!each_func->name)
3035 node = lookup_info_hash_table (stash->funcinfo_hash_table,
3039 while (node && !found)
3041 found = node->info == each_func;
3047 for (each_var = each_unit->variable_table;
3049 each_var = each_var->prev_var)
3051 if (!each_var->name || !each_var->file || each_var->stack)
3053 node = lookup_info_hash_table (stash->varinfo_hash_table,
3057 while (node && !found)
3059 found = node->info == each_var;
3067 /* Check to see if we want to enable the info hash tables, which consume
3068 quite a bit of memory. Currently we only check the number times
3069 bfd_dwarf2_find_line is called. In the future, we may also want to
3070 take the number of symbols into account. */
3073 stash_maybe_enable_info_hash_tables (bfd *abfd, struct dwarf2_debug *stash)
3075 BFD_ASSERT (stash->info_hash_status == STASH_INFO_HASH_OFF);
3077 if (stash->info_hash_count++ < STASH_INFO_HASH_TRIGGER)
3080 /* FIXME: Maybe we should check the reduce_memory_overheads
3081 and optimize fields in the bfd_link_info structure ? */
3083 /* Create hash tables. */
3084 stash->funcinfo_hash_table = create_info_hash_table (abfd);
3085 stash->varinfo_hash_table = create_info_hash_table (abfd);
3086 if (!stash->funcinfo_hash_table || !stash->varinfo_hash_table)
3088 /* Turn off info hashes if any allocation above fails. */
3089 stash->info_hash_status = STASH_INFO_HASH_DISABLED;
3092 /* We need a forced update so that the info hash tables will
3093 be created even though there is no compilation unit. That
3094 happens if STASH_INFO_HASH_TRIGGER is 0. */
3095 stash_maybe_update_info_hash_tables (stash);
3096 stash->info_hash_status = STASH_INFO_HASH_ON;
3099 /* Find the file and line associated with a symbol and address using the
3100 info hash tables of a stash. If there is a match, the function returns
3101 TRUE and update the locations pointed to by filename_ptr and linenumber_ptr;
3102 otherwise it returns FALSE. */
3105 stash_find_line_fast (struct dwarf2_debug *stash,
3108 const char **filename_ptr,
3109 unsigned int *linenumber_ptr)
3111 BFD_ASSERT (stash->info_hash_status == STASH_INFO_HASH_ON);
3113 if (sym->flags & BSF_FUNCTION)
3114 return info_hash_lookup_funcinfo (stash->funcinfo_hash_table, sym, addr,
3115 filename_ptr, linenumber_ptr);
3116 return info_hash_lookup_varinfo (stash->varinfo_hash_table, sym, addr,
3117 filename_ptr, linenumber_ptr);
3120 /* Read debug information from DEBUG_BFD when DEBUG_BFD is specified.
3121 If DEBUG_BFD is not specified, we read debug information from ABFD
3122 or its gnu_debuglink. The results will be stored in PINFO.
3123 The function returns TRUE iff debug information is ready. */
3126 _bfd_dwarf2_slurp_debug_info (bfd *abfd, bfd *debug_bfd,
3127 const struct dwarf_debug_section *debug_sections,
3131 bfd_size_type amt = sizeof (struct dwarf2_debug);
3132 bfd_size_type total_size;
3134 struct dwarf2_debug *stash = (struct dwarf2_debug *) *pinfo;
3139 stash = (struct dwarf2_debug *) bfd_zalloc (abfd, amt);
3142 stash->debug_sections = debug_sections;
3146 if (debug_bfd == NULL)
3149 msec = find_debug_info (debug_bfd, debug_sections, NULL);
3150 if (msec == NULL && abfd == debug_bfd)
3152 char * debug_filename = bfd_follow_gnu_debuglink (abfd, DEBUGDIR);
3154 if (debug_filename == NULL)
3155 /* No dwarf2 info, and no gnu_debuglink to follow.
3156 Note that at this point the stash has been allocated, but
3157 contains zeros. This lets future calls to this function
3158 fail more quickly. */
3161 if ((debug_bfd = bfd_openr (debug_filename, NULL)) == NULL
3162 || ! bfd_check_format (debug_bfd, bfd_object)
3163 || (msec = find_debug_info (debug_bfd,
3164 debug_sections, NULL)) == NULL)
3167 bfd_close (debug_bfd);
3168 /* FIXME: Should we report our failure to follow the debuglink ? */
3169 free (debug_filename);
3174 /* There can be more than one DWARF2 info section in a BFD these
3175 days. First handle the easy case when there's only one. If
3176 there's more than one, try case two: none of the sections is
3177 compressed. In that case, read them all in and produce one
3178 large stash. We do this in two passes - in the first pass we
3179 just accumulate the section sizes, and in the second pass we
3180 read in the section's contents. (The allows us to avoid
3181 reallocing the data as we add sections to the stash.) If
3182 some or all sections are compressed, then do things the slow
3183 way, with a bunch of reallocs. */
3185 if (! find_debug_info (debug_bfd, debug_sections, msec))
3187 /* Case 1: only one info section. */
3188 total_size = msec->size;
3189 if (! read_section (debug_bfd, &stash->debug_sections[debug_info],
3191 &stash->info_ptr_memory, &total_size))
3196 /* Case 2: multiple sections. */
3197 for (total_size = 0;
3199 msec = find_debug_info (debug_bfd, debug_sections, msec))
3200 total_size += msec->size;
3202 stash->info_ptr_memory = (bfd_byte *) bfd_malloc (total_size);
3203 if (stash->info_ptr_memory == NULL)
3207 for (msec = find_debug_info (debug_bfd, debug_sections, NULL);
3209 msec = find_debug_info (debug_bfd, debug_sections, msec))
3217 if (!(bfd_simple_get_relocated_section_contents
3218 (debug_bfd, msec, stash->info_ptr_memory + total_size,
3226 stash->info_ptr = stash->info_ptr_memory;
3227 stash->info_ptr_end = stash->info_ptr + total_size;
3228 stash->sec = find_debug_info (debug_bfd, debug_sections, NULL);
3229 stash->sec_info_ptr = stash->info_ptr;
3230 stash->syms = symbols;
3231 stash->bfd_ptr = debug_bfd;
3236 /* Find the source code location of SYMBOL. If SYMBOL is NULL
3237 then find the nearest source code location corresponding to
3238 the address SECTION + OFFSET.
3239 Returns TRUE if the line is found without error and fills in
3240 FILENAME_PTR and LINENUMBER_PTR. In the case where SYMBOL was
3241 NULL the FUNCTIONNAME_PTR is also filled in.
3242 SYMBOLS contains the symbol table for ABFD.
3243 DEBUG_SECTIONS contains the name of the dwarf debug sections.
3244 ADDR_SIZE is the number of bytes in the initial .debug_info length
3245 field and in the abbreviation offset, or zero to indicate that the
3246 default value should be used. */
3249 find_line (bfd *abfd,
3250 const struct dwarf_debug_section *debug_sections,
3255 const char **filename_ptr,
3256 const char **functionname_ptr,
3257 unsigned int *linenumber_ptr,
3258 unsigned int addr_size,
3261 /* Read each compilation unit from the section .debug_info, and check
3262 to see if it contains the address we are searching for. If yes,
3263 lookup the address, and return the line number info. If no, go
3264 on to the next compilation unit.
3266 We keep a list of all the previously read compilation units, and
3267 a pointer to the next un-read compilation unit. Check the
3268 previously read units before reading more. */
3269 struct dwarf2_debug *stash;
3270 /* What address are we looking for? */
3272 struct comp_unit* each;
3273 bfd_vma found = FALSE;
3274 bfd_boolean do_line;
3276 *filename_ptr = NULL;
3277 if (functionname_ptr != NULL)
3278 *functionname_ptr = NULL;
3279 *linenumber_ptr = 0;
3281 if (! _bfd_dwarf2_slurp_debug_info (abfd, NULL,
3282 debug_sections, symbols, pinfo))
3285 stash = (struct dwarf2_debug *) *pinfo;
3287 /* In a relocatable file, 2 functions may have the same address.
3288 We change the section vma so that they won't overlap. */
3289 if ((abfd->flags & (EXEC_P | DYNAMIC)) == 0)
3291 if (! place_sections (abfd, stash))
3295 do_line = (section == NULL
3297 && functionname_ptr == NULL
3301 addr = symbol->value;
3302 section = bfd_get_section (symbol);
3304 else if (section != NULL
3305 && functionname_ptr != NULL
3311 if (section->output_section)
3312 addr += section->output_section->vma + section->output_offset;
3314 addr += section->vma;
3316 /* A null info_ptr indicates that there is no dwarf2 info
3317 (or that an error occured while setting up the stash). */
3318 if (! stash->info_ptr)
3321 stash->inliner_chain = NULL;
3323 /* Check the previously read comp. units first. */
3326 /* The info hash tables use quite a bit of memory. We may not want to
3327 always use them. We use some heuristics to decide if and when to
3329 if (stash->info_hash_status == STASH_INFO_HASH_OFF)
3330 stash_maybe_enable_info_hash_tables (abfd, stash);
3332 /* Keep info hash table up to date if they are available. Note that we
3333 may disable the hash tables if there is any error duing update. */
3334 if (stash->info_hash_status == STASH_INFO_HASH_ON)
3335 stash_maybe_update_info_hash_tables (stash);
3337 if (stash->info_hash_status == STASH_INFO_HASH_ON)
3339 found = stash_find_line_fast (stash, symbol, addr, filename_ptr,
3346 /* Check the previously read comp. units first. */
3347 for (each = stash->all_comp_units; each; each = each->next_unit)
3348 if ((symbol->flags & BSF_FUNCTION) == 0
3349 || comp_unit_contains_address (each, addr))
3351 found = comp_unit_find_line (each, symbol, addr, filename_ptr,
3352 linenumber_ptr, stash);
3360 for (each = stash->all_comp_units; each; each = each->next_unit)
3362 found = (comp_unit_contains_address (each, addr)
3363 && comp_unit_find_nearest_line (each, addr,
3373 /* The DWARF2 spec says that the initial length field, and the
3374 offset of the abbreviation table, should both be 4-byte values.
3375 However, some compilers do things differently. */
3378 BFD_ASSERT (addr_size == 4 || addr_size == 8);
3380 /* Read each remaining comp. units checking each as they are read. */
3381 while (stash->info_ptr < stash->info_ptr_end)
3384 unsigned int offset_size = addr_size;
3385 bfd_byte *info_ptr_unit = stash->info_ptr;
3387 length = read_4_bytes (stash->bfd_ptr, stash->info_ptr);
3388 /* A 0xffffff length is the DWARF3 way of indicating
3389 we use 64-bit offsets, instead of 32-bit offsets. */
3390 if (length == 0xffffffff)
3393 length = read_8_bytes (stash->bfd_ptr, stash->info_ptr + 4);
3394 stash->info_ptr += 12;
3396 /* A zero length is the IRIX way of indicating 64-bit offsets,
3397 mostly because the 64-bit length will generally fit in 32
3398 bits, and the endianness helps. */
3399 else if (length == 0)
3402 length = read_4_bytes (stash->bfd_ptr, stash->info_ptr + 4);
3403 stash->info_ptr += 8;
3405 /* In the absence of the hints above, we assume 32-bit DWARF2
3406 offsets even for targets with 64-bit addresses, because:
3407 a) most of the time these targets will not have generated
3408 more than 2Gb of debug info and so will not need 64-bit
3411 b) if they do use 64-bit offsets but they are not using
3412 the size hints that are tested for above then they are
3413 not conforming to the DWARF3 standard anyway. */
3414 else if (addr_size == 8)
3417 stash->info_ptr += 4;
3420 stash->info_ptr += 4;
3424 each = parse_comp_unit (stash, length, info_ptr_unit,
3427 /* The dwarf information is damaged, don't trust it any
3430 stash->info_ptr += length;
3432 if (stash->all_comp_units)
3433 stash->all_comp_units->prev_unit = each;
3435 stash->last_comp_unit = each;
3437 each->next_unit = stash->all_comp_units;
3438 stash->all_comp_units = each;
3440 /* DW_AT_low_pc and DW_AT_high_pc are optional for
3441 compilation units. If we don't have them (i.e.,
3442 unit->high == 0), we need to consult the line info table
3443 to see if a compilation unit contains the given
3446 found = (((symbol->flags & BSF_FUNCTION) == 0
3447 || each->arange.high == 0
3448 || comp_unit_contains_address (each, addr))
3449 && comp_unit_find_line (each, symbol, addr,
3454 found = ((each->arange.high == 0
3455 || comp_unit_contains_address (each, addr))
3456 && comp_unit_find_nearest_line (each, addr,
3462 if ((bfd_vma) (stash->info_ptr - stash->sec_info_ptr)
3463 == stash->sec->size)
3465 stash->sec = find_debug_info (stash->bfd_ptr, debug_sections,
3467 stash->sec_info_ptr = stash->info_ptr;
3476 if ((abfd->flags & (EXEC_P | DYNAMIC)) == 0)
3477 unset_sections (stash);
3482 /* The DWARF2 version of find_nearest_line.
3483 Return TRUE if the line is found without error. */
3486 _bfd_dwarf2_find_nearest_line (bfd *abfd,
3487 const struct dwarf_debug_section *debug_sections,
3491 const char **filename_ptr,
3492 const char **functionname_ptr,
3493 unsigned int *linenumber_ptr,
3494 unsigned int addr_size,
3497 return find_line (abfd, debug_sections, section, offset, NULL, symbols,
3498 filename_ptr, functionname_ptr, linenumber_ptr, addr_size,
3502 /* The DWARF2 version of find_line.
3503 Return TRUE if the line is found without error. */
3506 _bfd_dwarf2_find_line (bfd *abfd,
3509 const char **filename_ptr,
3510 unsigned int *linenumber_ptr,
3511 unsigned int addr_size,
3514 return find_line (abfd, dwarf_debug_sections, NULL, 0, symbol, symbols,
3515 filename_ptr, NULL, linenumber_ptr, addr_size, pinfo);
3519 _bfd_dwarf2_find_inliner_info (bfd *abfd ATTRIBUTE_UNUSED,
3520 const char **filename_ptr,
3521 const char **functionname_ptr,
3522 unsigned int *linenumber_ptr,
3525 struct dwarf2_debug *stash;
3527 stash = (struct dwarf2_debug *) *pinfo;
3530 struct funcinfo *func = stash->inliner_chain;
3532 if (func && func->caller_func)
3534 *filename_ptr = func->caller_file;
3535 *functionname_ptr = func->caller_func->name;
3536 *linenumber_ptr = func->caller_line;
3537 stash->inliner_chain = func->caller_func;
3546 _bfd_dwarf2_cleanup_debug_info (bfd *abfd, void **pinfo)
3548 struct dwarf2_debug *stash = (struct dwarf2_debug *) *pinfo;;
3549 struct comp_unit *each;
3551 if (abfd == NULL || stash == NULL)
3554 for (each = stash->all_comp_units; each; each = each->next_unit)
3556 struct abbrev_info **abbrevs = each->abbrevs;
3557 struct funcinfo *function_table = each->function_table;
3558 struct varinfo *variable_table = each->variable_table;
3561 for (i = 0; i < ABBREV_HASH_SIZE; i++)
3563 struct abbrev_info *abbrev = abbrevs[i];
3567 free (abbrev->attrs);
3568 abbrev = abbrev->next;
3572 if (each->line_table)
3574 free (each->line_table->dirs);
3575 free (each->line_table->files);
3578 while (function_table)
3580 if (function_table->file)
3582 free (function_table->file);
3583 function_table->file = NULL;
3586 if (function_table->caller_file)
3588 free (function_table->caller_file);
3589 function_table->caller_file = NULL;
3591 function_table = function_table->prev_func;
3594 while (variable_table)
3596 if (variable_table->file)
3598 free (variable_table->file);
3599 variable_table->file = NULL;
3602 variable_table = variable_table->prev_var;
3606 if (stash->dwarf_abbrev_buffer)
3607 free (stash->dwarf_abbrev_buffer);
3608 if (stash->dwarf_line_buffer)
3609 free (stash->dwarf_line_buffer);
3610 if (stash->dwarf_str_buffer)
3611 free (stash->dwarf_str_buffer);
3612 if (stash->dwarf_ranges_buffer)
3613 free (stash->dwarf_ranges_buffer);
3614 if (stash->info_ptr_memory)
3615 free (stash->info_ptr_memory);