2 Copyright (C) 1994-2015 Free Software Foundation, Inc.
4 Adapted from gdb/dwarf2read.c by Gavin Koch of Cygnus Solutions
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
15 This file is part of BFD.
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.
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.
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. */
34 #include "libiberty.h"
39 /* The data in the .debug_line statement prologue looks like this. */
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;
50 unsigned char line_range;
51 unsigned char opcode_base;
52 unsigned char *standard_opcode_lengths;
55 /* Attributes have a name and a value. */
59 enum dwarf_attribute name;
64 struct dwarf_block *blk;
71 /* Blocks are a bunch of untyped bytes. */
78 struct adjusted_section
86 /* A list of all previously read comp_units. */
87 struct comp_unit *all_comp_units;
89 /* Last comp unit in list above. */
90 struct comp_unit *last_comp_unit;
92 /* Names of the debug sections. */
93 const struct dwarf_debug_section *debug_sections;
95 /* The next unread compilation unit within the .debug_info section.
96 Zero indicates that the .debug_info section has not been loaded
100 /* Pointer to the end of the .debug_info section memory buffer. */
101 bfd_byte *info_ptr_end;
103 /* Pointer to the bfd, section and address of the beginning of the
104 section. The bfd might be different than expected because of
105 gnu_debuglink sections. */
108 bfd_byte *sec_info_ptr;
110 /* Support for alternate debug info sections created by the DWZ utility:
111 This includes a pointer to an alternate bfd which contains *extra*,
112 possibly duplicate debug sections, and pointers to the loaded
113 .debug_str and .debug_info sections from this bfd. */
115 bfd_byte * alt_dwarf_str_buffer;
116 bfd_size_type alt_dwarf_str_size;
117 bfd_byte * alt_dwarf_info_buffer;
118 bfd_size_type alt_dwarf_info_size;
120 /* A pointer to the memory block allocated for info_ptr. Neither
121 info_ptr nor sec_info_ptr are guaranteed to stay pointing to the
122 beginning of the malloc block. This is used only to free the
124 bfd_byte *info_ptr_memory;
126 /* Pointer to the symbol table. */
129 /* Pointer to the .debug_abbrev section loaded into memory. */
130 bfd_byte *dwarf_abbrev_buffer;
132 /* Length of the loaded .debug_abbrev section. */
133 bfd_size_type dwarf_abbrev_size;
135 /* Buffer for decode_line_info. */
136 bfd_byte *dwarf_line_buffer;
138 /* Length of the loaded .debug_line section. */
139 bfd_size_type dwarf_line_size;
141 /* Pointer to the .debug_str section loaded into memory. */
142 bfd_byte *dwarf_str_buffer;
144 /* Length of the loaded .debug_str section. */
145 bfd_size_type dwarf_str_size;
147 /* Pointer to the .debug_ranges section loaded into memory. */
148 bfd_byte *dwarf_ranges_buffer;
150 /* Length of the loaded .debug_ranges section. */
151 bfd_size_type dwarf_ranges_size;
153 /* If the most recent call to bfd_find_nearest_line was given an
154 address in an inlined function, preserve a pointer into the
155 calling chain for subsequent calls to bfd_find_inliner_info to
157 struct funcinfo *inliner_chain;
159 /* Section VMAs at the time the stash was built. */
162 /* Number of sections whose VMA we must adjust. */
163 int adjusted_section_count;
165 /* Array of sections with adjusted VMA. */
166 struct adjusted_section *adjusted_sections;
168 /* Number of times find_line is called. This is used in
169 the heuristic for enabling the info hash tables. */
172 #define STASH_INFO_HASH_TRIGGER 100
174 /* Hash table mapping symbol names to function infos. */
175 struct info_hash_table *funcinfo_hash_table;
177 /* Hash table mapping symbol names to variable infos. */
178 struct info_hash_table *varinfo_hash_table;
180 /* Head of comp_unit list in the last hash table update. */
181 struct comp_unit *hash_units_head;
183 /* Status of info hash. */
184 int info_hash_status;
185 #define STASH_INFO_HASH_OFF 0
186 #define STASH_INFO_HASH_ON 1
187 #define STASH_INFO_HASH_DISABLED 2
189 /* True if we opened bfd_ptr. */
190 bfd_boolean close_on_cleanup;
200 /* A minimal decoding of DWARF2 compilation units. We only decode
201 what's needed to get to the line number information. */
205 /* Chain the previously read compilation units. */
206 struct comp_unit *next_unit;
208 /* Likewise, chain the compilation unit read after this one.
209 The comp units are stored in reversed reading order. */
210 struct comp_unit *prev_unit;
212 /* Keep the bfd convenient (for memory allocation). */
215 /* The lowest and highest addresses contained in this compilation
216 unit as specified in the compilation unit header. */
217 struct arange arange;
219 /* The DW_AT_name attribute (for error messages). */
222 /* The abbrev hash table. */
223 struct abbrev_info **abbrevs;
225 /* DW_AT_language. */
228 /* Note that an error was found by comp_unit_find_nearest_line. */
231 /* The DW_AT_comp_dir attribute. */
234 /* TRUE if there is a line number table associated with this comp. unit. */
237 /* Pointer to the current comp_unit so that we can find a given entry
239 bfd_byte *info_ptr_unit;
241 /* Pointer to the start of the debug section, for DW_FORM_ref_addr. */
242 bfd_byte *sec_info_ptr;
244 /* The offset into .debug_line of the line number table. */
245 unsigned long line_offset;
247 /* Pointer to the first child die for the comp unit. */
248 bfd_byte *first_child_die_ptr;
250 /* The end of the comp unit. */
253 /* The decoded line number, NULL if not yet decoded. */
254 struct line_info_table *line_table;
256 /* A list of the functions found in this comp. unit. */
257 struct funcinfo *function_table;
259 /* A list of the variables found in this comp. unit. */
260 struct varinfo *variable_table;
262 /* Pointer to dwarf2_debug structure. */
263 struct dwarf2_debug *stash;
265 /* DWARF format version for this unit - from unit header. */
268 /* Address size for this unit - from unit header. */
269 unsigned char addr_size;
271 /* Offset size for this unit - from unit header. */
272 unsigned char offset_size;
274 /* Base address for this unit - from DW_AT_low_pc attribute of
275 DW_TAG_compile_unit DIE */
276 bfd_vma base_address;
278 /* TRUE if symbols are cached in hash table for faster lookup by name. */
282 /* This data structure holds the information of an abbrev. */
285 unsigned int number; /* Number identifying abbrev. */
286 enum dwarf_tag tag; /* DWARF tag. */
287 int has_children; /* Boolean. */
288 unsigned int num_attrs; /* Number of attributes. */
289 struct attr_abbrev *attrs; /* An array of attribute descriptions. */
290 struct abbrev_info *next; /* Next in chain. */
295 enum dwarf_attribute name;
296 enum dwarf_form form;
299 /* Map of uncompressed DWARF debug section name to compressed one. It
300 is terminated by NULL uncompressed_name. */
302 const struct dwarf_debug_section dwarf_debug_sections[] =
304 { ".debug_abbrev", ".zdebug_abbrev" },
305 { ".debug_aranges", ".zdebug_aranges" },
306 { ".debug_frame", ".zdebug_frame" },
307 { ".debug_info", ".zdebug_info" },
308 { ".debug_info", ".zdebug_info" },
309 { ".debug_line", ".zdebug_line" },
310 { ".debug_loc", ".zdebug_loc" },
311 { ".debug_macinfo", ".zdebug_macinfo" },
312 { ".debug_macro", ".zdebug_macro" },
313 { ".debug_pubnames", ".zdebug_pubnames" },
314 { ".debug_pubtypes", ".zdebug_pubtypes" },
315 { ".debug_ranges", ".zdebug_ranges" },
316 { ".debug_static_func", ".zdebug_static_func" },
317 { ".debug_static_vars", ".zdebug_static_vars" },
318 { ".debug_str", ".zdebug_str", },
319 { ".debug_str", ".zdebug_str", },
320 { ".debug_types", ".zdebug_types" },
321 /* GNU DWARF 1 extensions */
322 { ".debug_sfnames", ".zdebug_sfnames" },
323 { ".debug_srcinfo", ".zebug_srcinfo" },
324 /* SGI/MIPS DWARF 2 extensions */
325 { ".debug_funcnames", ".zdebug_funcnames" },
326 { ".debug_typenames", ".zdebug_typenames" },
327 { ".debug_varnames", ".zdebug_varnames" },
328 { ".debug_weaknames", ".zdebug_weaknames" },
332 /* NB/ Numbers in this enum must match up with indicies
333 into the dwarf_debug_sections[] array above. */
334 enum dwarf_debug_section_enum
361 #ifndef ABBREV_HASH_SIZE
362 #define ABBREV_HASH_SIZE 121
364 #ifndef ATTR_ALLOC_CHUNK
365 #define ATTR_ALLOC_CHUNK 4
368 /* Variable and function hash tables. This is used to speed up look-up
369 in lookup_symbol_in_var_table() and lookup_symbol_in_function_table().
370 In order to share code between variable and function infos, we use
371 a list of untyped pointer for all variable/function info associated with
372 a symbol. We waste a bit of memory for list with one node but that
373 simplifies the code. */
375 struct info_list_node
377 struct info_list_node *next;
381 /* Info hash entry. */
382 struct info_hash_entry
384 struct bfd_hash_entry root;
385 struct info_list_node *head;
388 struct info_hash_table
390 struct bfd_hash_table base;
393 /* Function to create a new entry in info hash table. */
395 static struct bfd_hash_entry *
396 info_hash_table_newfunc (struct bfd_hash_entry *entry,
397 struct bfd_hash_table *table,
400 struct info_hash_entry *ret = (struct info_hash_entry *) entry;
402 /* Allocate the structure if it has not already been allocated by a
406 ret = (struct info_hash_entry *) bfd_hash_allocate (table,
412 /* Call the allocation method of the base class. */
413 ret = ((struct info_hash_entry *)
414 bfd_hash_newfunc ((struct bfd_hash_entry *) ret, table, string));
416 /* Initialize the local fields here. */
420 return (struct bfd_hash_entry *) ret;
423 /* Function to create a new info hash table. It returns a pointer to the
424 newly created table or NULL if there is any error. We need abfd
425 solely for memory allocation. */
427 static struct info_hash_table *
428 create_info_hash_table (bfd *abfd)
430 struct info_hash_table *hash_table;
432 hash_table = ((struct info_hash_table *)
433 bfd_alloc (abfd, sizeof (struct info_hash_table)));
437 if (!bfd_hash_table_init (&hash_table->base, info_hash_table_newfunc,
438 sizeof (struct info_hash_entry)))
440 bfd_release (abfd, hash_table);
447 /* Insert an info entry into an info hash table. We do not check of
448 duplicate entries. Also, the caller need to guarantee that the
449 right type of info in inserted as info is passed as a void* pointer.
450 This function returns true if there is no error. */
453 insert_info_hash_table (struct info_hash_table *hash_table,
458 struct info_hash_entry *entry;
459 struct info_list_node *node;
461 entry = (struct info_hash_entry*) bfd_hash_lookup (&hash_table->base,
466 node = (struct info_list_node *) bfd_hash_allocate (&hash_table->base,
472 node->next = entry->head;
478 /* Look up an info entry list from an info hash table. Return NULL
481 static struct info_list_node *
482 lookup_info_hash_table (struct info_hash_table *hash_table, const char *key)
484 struct info_hash_entry *entry;
486 entry = (struct info_hash_entry*) bfd_hash_lookup (&hash_table->base, key,
488 return entry ? entry->head : NULL;
491 /* Read a section into its appropriate place in the dwarf2_debug
492 struct (indicated by SECTION_BUFFER and SECTION_SIZE). If SYMS is
493 not NULL, use bfd_simple_get_relocated_section_contents to read the
494 section contents, otherwise use bfd_get_section_contents. Fail if
495 the located section does not contain at least OFFSET bytes. */
498 read_section (bfd * abfd,
499 const struct dwarf_debug_section *sec,
502 bfd_byte ** section_buffer,
503 bfd_size_type * section_size)
506 const char *section_name = sec->uncompressed_name;
508 /* The section may have already been read. */
509 if (*section_buffer == NULL)
511 msec = bfd_get_section_by_name (abfd, section_name);
514 section_name = sec->compressed_name;
515 if (section_name != NULL)
516 msec = bfd_get_section_by_name (abfd, section_name);
520 (*_bfd_error_handler) (_("Dwarf Error: Can't find %s section."),
521 sec->uncompressed_name);
522 bfd_set_error (bfd_error_bad_value);
526 *section_size = msec->rawsize ? msec->rawsize : msec->size;
530 = bfd_simple_get_relocated_section_contents (abfd, msec, NULL, syms);
531 if (! *section_buffer)
536 *section_buffer = (bfd_byte *) bfd_malloc (*section_size);
537 if (! *section_buffer)
539 if (! bfd_get_section_contents (abfd, msec, *section_buffer,
545 /* It is possible to get a bad value for the offset into the section
546 that the client wants. Validate it here to avoid trouble later. */
547 if (offset != 0 && offset >= *section_size)
549 (*_bfd_error_handler) (_("Dwarf Error: Offset (%lu)"
550 " greater than or equal to %s size (%lu)."),
551 (long) offset, section_name, *section_size);
552 bfd_set_error (bfd_error_bad_value);
560 The following function up to the END VERBATIM mark are
561 copied directly from dwarf2read.c. */
563 /* Read dwarf information from a buffer. */
566 read_1_byte (bfd *abfd ATTRIBUTE_UNUSED, bfd_byte *buf)
568 return bfd_get_8 (abfd, buf);
572 read_1_signed_byte (bfd *abfd ATTRIBUTE_UNUSED, bfd_byte *buf)
574 return bfd_get_signed_8 (abfd, buf);
578 read_2_bytes (bfd *abfd, bfd_byte *buf)
580 return bfd_get_16 (abfd, buf);
584 read_4_bytes (bfd *abfd, bfd_byte *buf)
586 return bfd_get_32 (abfd, buf);
590 read_8_bytes (bfd *abfd, bfd_byte *buf)
592 return bfd_get_64 (abfd, buf);
596 read_n_bytes (bfd *abfd ATTRIBUTE_UNUSED,
598 unsigned int size ATTRIBUTE_UNUSED)
604 read_string (bfd *abfd ATTRIBUTE_UNUSED,
606 unsigned int *bytes_read_ptr)
608 /* Return a pointer to the embedded string. */
609 char *str = (char *) buf;
617 *bytes_read_ptr = strlen (str) + 1;
624 read_indirect_string (struct comp_unit * unit,
626 unsigned int * bytes_read_ptr)
629 struct dwarf2_debug *stash = unit->stash;
632 if (unit->offset_size == 4)
633 offset = read_4_bytes (unit->abfd, buf);
635 offset = read_8_bytes (unit->abfd, buf);
637 *bytes_read_ptr = unit->offset_size;
639 if (! read_section (unit->abfd, &stash->debug_sections[debug_str],
641 &stash->dwarf_str_buffer, &stash->dwarf_str_size))
644 str = (char *) stash->dwarf_str_buffer + offset;
650 /* Like read_indirect_string but uses a .debug_str located in
651 an alternate file pointed to by the .gnu_debugaltlink section.
652 Used to impement DW_FORM_GNU_strp_alt. */
655 read_alt_indirect_string (struct comp_unit * unit,
657 unsigned int * bytes_read_ptr)
660 struct dwarf2_debug *stash = unit->stash;
663 if (unit->offset_size == 4)
664 offset = read_4_bytes (unit->abfd, buf);
666 offset = read_8_bytes (unit->abfd, buf);
668 *bytes_read_ptr = unit->offset_size;
670 if (stash->alt_bfd_ptr == NULL)
673 char * debug_filename = bfd_follow_gnu_debugaltlink (unit->abfd, DEBUGDIR);
675 if (debug_filename == NULL)
678 if ((debug_bfd = bfd_openr (debug_filename, NULL)) == NULL
679 || ! bfd_check_format (debug_bfd, bfd_object))
682 bfd_close (debug_bfd);
684 /* FIXME: Should we report our failure to follow the debuglink ? */
685 free (debug_filename);
688 stash->alt_bfd_ptr = debug_bfd;
691 if (! read_section (unit->stash->alt_bfd_ptr,
692 stash->debug_sections + debug_str_alt,
693 NULL, /* FIXME: Do we need to load alternate symbols ? */
695 &stash->alt_dwarf_str_buffer,
696 &stash->alt_dwarf_str_size))
699 str = (char *) stash->alt_dwarf_str_buffer + offset;
706 /* Resolve an alternate reference from UNIT at OFFSET.
707 Returns a pointer into the loaded alternate CU upon success
708 or NULL upon failure. */
711 read_alt_indirect_ref (struct comp_unit * unit,
714 struct dwarf2_debug *stash = unit->stash;
716 if (stash->alt_bfd_ptr == NULL)
719 char * debug_filename = bfd_follow_gnu_debugaltlink (unit->abfd, DEBUGDIR);
721 if (debug_filename == NULL)
724 if ((debug_bfd = bfd_openr (debug_filename, NULL)) == NULL
725 || ! bfd_check_format (debug_bfd, bfd_object))
728 bfd_close (debug_bfd);
730 /* FIXME: Should we report our failure to follow the debuglink ? */
731 free (debug_filename);
734 stash->alt_bfd_ptr = debug_bfd;
737 if (! read_section (unit->stash->alt_bfd_ptr,
738 stash->debug_sections + debug_info_alt,
739 NULL, /* FIXME: Do we need to load alternate symbols ? */
741 &stash->alt_dwarf_info_buffer,
742 &stash->alt_dwarf_info_size))
745 return stash->alt_dwarf_info_buffer + offset;
749 read_address (struct comp_unit *unit, bfd_byte *buf)
753 if (bfd_get_flavour (unit->abfd) == bfd_target_elf_flavour)
754 signed_vma = get_elf_backend_data (unit->abfd)->sign_extend_vma;
758 switch (unit->addr_size)
761 return bfd_get_signed_64 (unit->abfd, buf);
763 return bfd_get_signed_32 (unit->abfd, buf);
765 return bfd_get_signed_16 (unit->abfd, buf);
772 switch (unit->addr_size)
775 return bfd_get_64 (unit->abfd, buf);
777 return bfd_get_32 (unit->abfd, buf);
779 return bfd_get_16 (unit->abfd, buf);
786 /* Lookup an abbrev_info structure in the abbrev hash table. */
788 static struct abbrev_info *
789 lookup_abbrev (unsigned int number, struct abbrev_info **abbrevs)
791 unsigned int hash_number;
792 struct abbrev_info *abbrev;
794 hash_number = number % ABBREV_HASH_SIZE;
795 abbrev = abbrevs[hash_number];
799 if (abbrev->number == number)
802 abbrev = abbrev->next;
808 /* In DWARF version 2, the description of the debugging information is
809 stored in a separate .debug_abbrev section. Before we read any
810 dies from a section we read in all abbreviations and install them
813 static struct abbrev_info**
814 read_abbrevs (bfd *abfd, bfd_uint64_t offset, struct dwarf2_debug *stash)
816 struct abbrev_info **abbrevs;
817 bfd_byte *abbrev_ptr;
818 struct abbrev_info *cur_abbrev;
819 unsigned int abbrev_number, bytes_read, abbrev_name;
820 unsigned int abbrev_form, hash_number;
823 if (! read_section (abfd, &stash->debug_sections[debug_abbrev],
825 &stash->dwarf_abbrev_buffer, &stash->dwarf_abbrev_size))
828 amt = sizeof (struct abbrev_info*) * ABBREV_HASH_SIZE;
829 abbrevs = (struct abbrev_info **) bfd_zalloc (abfd, amt);
833 abbrev_ptr = stash->dwarf_abbrev_buffer + offset;
834 abbrev_number = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
835 abbrev_ptr += bytes_read;
837 /* Loop until we reach an abbrev number of 0. */
838 while (abbrev_number)
840 amt = sizeof (struct abbrev_info);
841 cur_abbrev = (struct abbrev_info *) bfd_zalloc (abfd, amt);
842 if (cur_abbrev == NULL)
845 /* Read in abbrev header. */
846 cur_abbrev->number = abbrev_number;
847 cur_abbrev->tag = (enum dwarf_tag)
848 read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
849 abbrev_ptr += bytes_read;
850 cur_abbrev->has_children = read_1_byte (abfd, abbrev_ptr);
853 /* Now read in declarations. */
854 abbrev_name = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
855 abbrev_ptr += bytes_read;
856 abbrev_form = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
857 abbrev_ptr += bytes_read;
861 if ((cur_abbrev->num_attrs % ATTR_ALLOC_CHUNK) == 0)
863 struct attr_abbrev *tmp;
865 amt = cur_abbrev->num_attrs + ATTR_ALLOC_CHUNK;
866 amt *= sizeof (struct attr_abbrev);
867 tmp = (struct attr_abbrev *) bfd_realloc (cur_abbrev->attrs, amt);
872 for (i = 0; i < ABBREV_HASH_SIZE; i++)
874 struct abbrev_info *abbrev = abbrevs[i];
878 free (abbrev->attrs);
879 abbrev = abbrev->next;
884 cur_abbrev->attrs = tmp;
887 cur_abbrev->attrs[cur_abbrev->num_attrs].name
888 = (enum dwarf_attribute) abbrev_name;
889 cur_abbrev->attrs[cur_abbrev->num_attrs++].form
890 = (enum dwarf_form) abbrev_form;
891 abbrev_name = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
892 abbrev_ptr += bytes_read;
893 abbrev_form = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
894 abbrev_ptr += bytes_read;
897 hash_number = abbrev_number % ABBREV_HASH_SIZE;
898 cur_abbrev->next = abbrevs[hash_number];
899 abbrevs[hash_number] = cur_abbrev;
901 /* Get next abbreviation.
902 Under Irix6 the abbreviations for a compilation unit are not
903 always properly terminated with an abbrev number of 0.
904 Exit loop if we encounter an abbreviation which we have
905 already read (which means we are about to read the abbreviations
906 for the next compile unit) or if the end of the abbreviation
908 if ((unsigned int) (abbrev_ptr - stash->dwarf_abbrev_buffer)
909 >= stash->dwarf_abbrev_size)
911 abbrev_number = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
912 abbrev_ptr += bytes_read;
913 if (lookup_abbrev (abbrev_number,abbrevs) != NULL)
920 /* Returns true if the form is one which has a string value. */
922 static inline bfd_boolean
923 is_str_attr (enum dwarf_form form)
925 return form == DW_FORM_string || form == DW_FORM_strp || form == DW_FORM_GNU_strp_alt;
928 /* Read an attribute value described by an attribute form. */
931 read_attribute_value (struct attribute *attr,
933 struct comp_unit *unit,
936 bfd *abfd = unit->abfd;
937 unsigned int bytes_read;
938 struct dwarf_block *blk;
941 attr->form = (enum dwarf_form) form;
945 case DW_FORM_ref_addr:
946 /* DW_FORM_ref_addr is an address in DWARF2, and an offset in
948 if (unit->version == 3 || unit->version == 4)
950 if (unit->offset_size == 4)
951 attr->u.val = read_4_bytes (unit->abfd, info_ptr);
953 attr->u.val = read_8_bytes (unit->abfd, info_ptr);
954 info_ptr += unit->offset_size;
959 attr->u.val = read_address (unit, info_ptr);
960 info_ptr += unit->addr_size;
962 case DW_FORM_GNU_ref_alt:
963 case DW_FORM_sec_offset:
964 if (unit->offset_size == 4)
965 attr->u.val = read_4_bytes (unit->abfd, info_ptr);
967 attr->u.val = read_8_bytes (unit->abfd, info_ptr);
968 info_ptr += unit->offset_size;
971 amt = sizeof (struct dwarf_block);
972 blk = (struct dwarf_block *) bfd_alloc (abfd, amt);
975 blk->size = read_2_bytes (abfd, info_ptr);
977 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
978 info_ptr += blk->size;
982 amt = sizeof (struct dwarf_block);
983 blk = (struct dwarf_block *) bfd_alloc (abfd, amt);
986 blk->size = read_4_bytes (abfd, info_ptr);
988 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
989 info_ptr += blk->size;
993 attr->u.val = read_2_bytes (abfd, info_ptr);
997 attr->u.val = read_4_bytes (abfd, info_ptr);
1001 attr->u.val = read_8_bytes (abfd, info_ptr);
1004 case DW_FORM_string:
1005 attr->u.str = read_string (abfd, info_ptr, &bytes_read);
1006 info_ptr += bytes_read;
1009 attr->u.str = read_indirect_string (unit, info_ptr, &bytes_read);
1010 info_ptr += bytes_read;
1012 case DW_FORM_GNU_strp_alt:
1013 attr->u.str = read_alt_indirect_string (unit, info_ptr, &bytes_read);
1014 info_ptr += bytes_read;
1016 case DW_FORM_exprloc:
1018 amt = sizeof (struct dwarf_block);
1019 blk = (struct dwarf_block *) bfd_alloc (abfd, amt);
1022 blk->size = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
1023 info_ptr += bytes_read;
1024 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
1025 info_ptr += blk->size;
1028 case DW_FORM_block1:
1029 amt = sizeof (struct dwarf_block);
1030 blk = (struct dwarf_block *) bfd_alloc (abfd, amt);
1033 blk->size = read_1_byte (abfd, info_ptr);
1035 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
1036 info_ptr += blk->size;
1040 attr->u.val = read_1_byte (abfd, info_ptr);
1044 attr->u.val = read_1_byte (abfd, info_ptr);
1047 case DW_FORM_flag_present:
1051 attr->u.sval = read_signed_leb128 (abfd, info_ptr, &bytes_read);
1052 info_ptr += bytes_read;
1055 attr->u.val = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
1056 info_ptr += bytes_read;
1059 attr->u.val = read_1_byte (abfd, info_ptr);
1063 attr->u.val = read_2_bytes (abfd, info_ptr);
1067 attr->u.val = read_4_bytes (abfd, info_ptr);
1071 attr->u.val = read_8_bytes (abfd, info_ptr);
1074 case DW_FORM_ref_sig8:
1075 attr->u.val = read_8_bytes (abfd, info_ptr);
1078 case DW_FORM_ref_udata:
1079 attr->u.val = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
1080 info_ptr += bytes_read;
1082 case DW_FORM_indirect:
1083 form = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
1084 info_ptr += bytes_read;
1085 info_ptr = read_attribute_value (attr, form, unit, info_ptr);
1088 (*_bfd_error_handler) (_("Dwarf Error: Invalid or unhandled FORM value: %#x."),
1090 bfd_set_error (bfd_error_bad_value);
1096 /* Read an attribute described by an abbreviated attribute. */
1099 read_attribute (struct attribute *attr,
1100 struct attr_abbrev *abbrev,
1101 struct comp_unit *unit,
1104 attr->name = abbrev->name;
1105 info_ptr = read_attribute_value (attr, abbrev->form, unit, info_ptr);
1109 /* Return whether DW_AT_name will return the same as DW_AT_linkage_name
1113 non_mangled (int lang)
1123 case DW_LANG_Cobol74:
1124 case DW_LANG_Cobol85:
1125 case DW_LANG_Fortran77:
1126 case DW_LANG_Pascal83:
1136 /* Source line information table routines. */
1138 #define FILE_ALLOC_CHUNK 5
1139 #define DIR_ALLOC_CHUNK 5
1143 struct line_info* prev_line;
1147 unsigned int column;
1148 unsigned int discriminator;
1149 unsigned char op_index;
1150 unsigned char end_sequence; /* End of (sequential) code sequence. */
1161 struct line_sequence
1164 struct line_sequence* prev_sequence;
1165 struct line_info* last_line; /* Largest VMA. */
1168 struct line_info_table
1171 unsigned int num_files;
1172 unsigned int num_dirs;
1173 unsigned int num_sequences;
1176 struct fileinfo* files;
1177 struct line_sequence* sequences;
1178 struct line_info* lcl_head; /* Local head; used in 'add_line_info'. */
1181 /* Remember some information about each function. If the function is
1182 inlined (DW_TAG_inlined_subroutine) it may have two additional
1183 attributes, DW_AT_call_file and DW_AT_call_line, which specify the
1184 source code location where this function was inlined. */
1188 /* Pointer to previous function in list of all functions. */
1189 struct funcinfo *prev_func;
1190 /* Pointer to function one scope higher. */
1191 struct funcinfo *caller_func;
1192 /* Source location file name where caller_func inlines this func. */
1194 /* Source location file name. */
1196 /* Source location line number where caller_func inlines this func. */
1198 /* Source location line number. */
1201 bfd_boolean is_linkage;
1203 struct arange arange;
1204 /* Where the symbol is defined. */
1210 /* Pointer to previous variable in list of all variables */
1211 struct varinfo *prev_var;
1212 /* Source location file name */
1214 /* Source location line number */
1219 /* Where the symbol is defined */
1221 /* Is this a stack variable? */
1222 unsigned int stack: 1;
1225 /* Return TRUE if NEW_LINE should sort after LINE. */
1227 static inline bfd_boolean
1228 new_line_sorts_after (struct line_info *new_line, struct line_info *line)
1230 return (new_line->address > line->address
1231 || (new_line->address == line->address
1232 && (new_line->op_index > line->op_index
1233 || (new_line->op_index == line->op_index
1234 && new_line->end_sequence < line->end_sequence))));
1238 /* Adds a new entry to the line_info list in the line_info_table, ensuring
1239 that the list is sorted. Note that the line_info list is sorted from
1240 highest to lowest VMA (with possible duplicates); that is,
1241 line_info->prev_line always accesses an equal or smaller VMA. */
1244 add_line_info (struct line_info_table *table,
1246 unsigned char op_index,
1249 unsigned int column,
1250 unsigned int discriminator,
1253 bfd_size_type amt = sizeof (struct line_info);
1254 struct line_sequence* seq = table->sequences;
1255 struct line_info* info = (struct line_info *) bfd_alloc (table->abfd, amt);
1260 /* Set member data of 'info'. */
1261 info->prev_line = NULL;
1262 info->address = address;
1263 info->op_index = op_index;
1265 info->column = column;
1266 info->discriminator = discriminator;
1267 info->end_sequence = end_sequence;
1269 if (filename && filename[0])
1271 info->filename = (char *) bfd_alloc (table->abfd, strlen (filename) + 1);
1272 if (info->filename == NULL)
1274 strcpy (info->filename, filename);
1277 info->filename = NULL;
1279 /* Find the correct location for 'info'. Normally we will receive
1280 new line_info data 1) in order and 2) with increasing VMAs.
1281 However some compilers break the rules (cf. decode_line_info) and
1282 so we include some heuristics for quickly finding the correct
1283 location for 'info'. In particular, these heuristics optimize for
1284 the common case in which the VMA sequence that we receive is a
1285 list of locally sorted VMAs such as
1286 p...z a...j (where a < j < p < z)
1288 Note: table->lcl_head is used to head an *actual* or *possible*
1289 sub-sequence within the list (such as a...j) that is not directly
1290 headed by table->last_line
1292 Note: we may receive duplicate entries from 'decode_line_info'. */
1295 && seq->last_line->address == address
1296 && seq->last_line->op_index == op_index
1297 && seq->last_line->end_sequence == end_sequence)
1299 /* We only keep the last entry with the same address and end
1300 sequence. See PR ld/4986. */
1301 if (table->lcl_head == seq->last_line)
1302 table->lcl_head = info;
1303 info->prev_line = seq->last_line->prev_line;
1304 seq->last_line = info;
1306 else if (!seq || seq->last_line->end_sequence)
1308 /* Start a new line sequence. */
1309 amt = sizeof (struct line_sequence);
1310 seq = (struct line_sequence *) bfd_malloc (amt);
1313 seq->low_pc = address;
1314 seq->prev_sequence = table->sequences;
1315 seq->last_line = info;
1316 table->lcl_head = info;
1317 table->sequences = seq;
1318 table->num_sequences++;
1320 else if (new_line_sorts_after (info, seq->last_line))
1322 /* Normal case: add 'info' to the beginning of the current sequence. */
1323 info->prev_line = seq->last_line;
1324 seq->last_line = info;
1326 /* lcl_head: initialize to head a *possible* sequence at the end. */
1327 if (!table->lcl_head)
1328 table->lcl_head = info;
1330 else if (!new_line_sorts_after (info, table->lcl_head)
1331 && (!table->lcl_head->prev_line
1332 || new_line_sorts_after (info, table->lcl_head->prev_line)))
1334 /* Abnormal but easy: lcl_head is the head of 'info'. */
1335 info->prev_line = table->lcl_head->prev_line;
1336 table->lcl_head->prev_line = info;
1340 /* Abnormal and hard: Neither 'last_line' nor 'lcl_head'
1341 are valid heads for 'info'. Reset 'lcl_head'. */
1342 struct line_info* li2 = seq->last_line; /* Always non-NULL. */
1343 struct line_info* li1 = li2->prev_line;
1347 if (!new_line_sorts_after (info, li2)
1348 && new_line_sorts_after (info, li1))
1351 li2 = li1; /* always non-NULL */
1352 li1 = li1->prev_line;
1354 table->lcl_head = li2;
1355 info->prev_line = table->lcl_head->prev_line;
1356 table->lcl_head->prev_line = info;
1357 if (address < seq->low_pc)
1358 seq->low_pc = address;
1363 /* Extract a fully qualified filename from a line info table.
1364 The returned string has been malloc'ed and it is the caller's
1365 responsibility to free it. */
1368 concat_filename (struct line_info_table *table, unsigned int file)
1372 if (file - 1 >= table->num_files)
1374 /* FILE == 0 means unknown. */
1376 (*_bfd_error_handler)
1377 (_("Dwarf Error: mangled line number section (bad file number)."));
1378 return strdup ("<unknown>");
1381 filename = table->files[file - 1].name;
1383 if (!IS_ABSOLUTE_PATH (filename))
1385 char *dir_name = NULL;
1386 char *subdir_name = NULL;
1390 if (table->files[file - 1].dir)
1391 subdir_name = table->dirs[table->files[file - 1].dir - 1];
1393 if (!subdir_name || !IS_ABSOLUTE_PATH (subdir_name))
1394 dir_name = table->comp_dir;
1398 dir_name = subdir_name;
1403 return strdup (filename);
1405 len = strlen (dir_name) + strlen (filename) + 2;
1409 len += strlen (subdir_name) + 1;
1410 name = (char *) bfd_malloc (len);
1412 sprintf (name, "%s/%s/%s", dir_name, subdir_name, filename);
1416 name = (char *) bfd_malloc (len);
1418 sprintf (name, "%s/%s", dir_name, filename);
1424 return strdup (filename);
1428 arange_add (const struct comp_unit *unit, struct arange *first_arange,
1429 bfd_vma low_pc, bfd_vma high_pc)
1431 struct arange *arange;
1433 /* Ignore empty ranges. */
1434 if (low_pc == high_pc)
1437 /* If the first arange is empty, use it. */
1438 if (first_arange->high == 0)
1440 first_arange->low = low_pc;
1441 first_arange->high = high_pc;
1445 /* Next see if we can cheaply extend an existing range. */
1446 arange = first_arange;
1449 if (low_pc == arange->high)
1451 arange->high = high_pc;
1454 if (high_pc == arange->low)
1456 arange->low = low_pc;
1459 arange = arange->next;
1463 /* Need to allocate a new arange and insert it into the arange list.
1464 Order isn't significant, so just insert after the first arange. */
1465 arange = (struct arange *) bfd_alloc (unit->abfd, sizeof (*arange));
1468 arange->low = low_pc;
1469 arange->high = high_pc;
1470 arange->next = first_arange->next;
1471 first_arange->next = arange;
1475 /* Compare function for line sequences. */
1478 compare_sequences (const void* a, const void* b)
1480 const struct line_sequence* seq1 = a;
1481 const struct line_sequence* seq2 = b;
1483 /* Sort by low_pc as the primary key. */
1484 if (seq1->low_pc < seq2->low_pc)
1486 if (seq1->low_pc > seq2->low_pc)
1489 /* If low_pc values are equal, sort in reverse order of
1490 high_pc, so that the largest region comes first. */
1491 if (seq1->last_line->address < seq2->last_line->address)
1493 if (seq1->last_line->address > seq2->last_line->address)
1496 if (seq1->last_line->op_index < seq2->last_line->op_index)
1498 if (seq1->last_line->op_index > seq2->last_line->op_index)
1504 /* Sort the line sequences for quick lookup. */
1507 sort_line_sequences (struct line_info_table* table)
1510 struct line_sequence* sequences;
1511 struct line_sequence* seq;
1513 unsigned int num_sequences = table->num_sequences;
1514 bfd_vma last_high_pc;
1516 if (num_sequences == 0)
1519 /* Allocate space for an array of sequences. */
1520 amt = sizeof (struct line_sequence) * num_sequences;
1521 sequences = (struct line_sequence *) bfd_alloc (table->abfd, amt);
1522 if (sequences == NULL)
1525 /* Copy the linked list into the array, freeing the original nodes. */
1526 seq = table->sequences;
1527 for (n = 0; n < num_sequences; n++)
1529 struct line_sequence* last_seq = seq;
1532 sequences[n].low_pc = seq->low_pc;
1533 sequences[n].prev_sequence = NULL;
1534 sequences[n].last_line = seq->last_line;
1535 seq = seq->prev_sequence;
1538 BFD_ASSERT (seq == NULL);
1540 qsort (sequences, n, sizeof (struct line_sequence), compare_sequences);
1542 /* Make the list binary-searchable by trimming overlapping entries
1543 and removing nested entries. */
1545 last_high_pc = sequences[0].last_line->address;
1546 for (n = 1; n < table->num_sequences; n++)
1548 if (sequences[n].low_pc < last_high_pc)
1550 if (sequences[n].last_line->address <= last_high_pc)
1551 /* Skip nested entries. */
1554 /* Trim overlapping entries. */
1555 sequences[n].low_pc = last_high_pc;
1557 last_high_pc = sequences[n].last_line->address;
1558 if (n > num_sequences)
1560 /* Close up the gap. */
1561 sequences[num_sequences].low_pc = sequences[n].low_pc;
1562 sequences[num_sequences].last_line = sequences[n].last_line;
1567 table->sequences = sequences;
1568 table->num_sequences = num_sequences;
1572 /* Decode the line number information for UNIT. */
1574 static struct line_info_table*
1575 decode_line_info (struct comp_unit *unit, struct dwarf2_debug *stash)
1577 bfd *abfd = unit->abfd;
1578 struct line_info_table* table;
1581 struct line_head lh;
1582 unsigned int i, bytes_read, offset_size;
1583 char *cur_file, *cur_dir;
1584 unsigned char op_code, extended_op, adj_opcode;
1585 unsigned int exop_len;
1588 if (! read_section (abfd, &stash->debug_sections[debug_line],
1589 stash->syms, unit->line_offset,
1590 &stash->dwarf_line_buffer, &stash->dwarf_line_size))
1593 amt = sizeof (struct line_info_table);
1594 table = (struct line_info_table *) bfd_alloc (abfd, amt);
1598 table->comp_dir = unit->comp_dir;
1600 table->num_files = 0;
1601 table->files = NULL;
1603 table->num_dirs = 0;
1606 table->num_sequences = 0;
1607 table->sequences = NULL;
1609 table->lcl_head = NULL;
1611 line_ptr = stash->dwarf_line_buffer + unit->line_offset;
1613 /* Read in the prologue. */
1614 lh.total_length = read_4_bytes (abfd, line_ptr);
1617 if (lh.total_length == 0xffffffff)
1619 lh.total_length = read_8_bytes (abfd, line_ptr);
1623 else if (lh.total_length == 0 && unit->addr_size == 8)
1625 /* Handle (non-standard) 64-bit DWARF2 formats. */
1626 lh.total_length = read_4_bytes (abfd, line_ptr);
1630 line_end = line_ptr + lh.total_length;
1631 lh.version = read_2_bytes (abfd, line_ptr);
1632 if (lh.version < 2 || lh.version > 4)
1634 (*_bfd_error_handler)
1635 (_("Dwarf Error: Unhandled .debug_line version %d."), lh.version);
1636 bfd_set_error (bfd_error_bad_value);
1640 if (offset_size == 4)
1641 lh.prologue_length = read_4_bytes (abfd, line_ptr);
1643 lh.prologue_length = read_8_bytes (abfd, line_ptr);
1644 line_ptr += offset_size;
1645 lh.minimum_instruction_length = read_1_byte (abfd, line_ptr);
1647 if (lh.version >= 4)
1649 lh.maximum_ops_per_insn = read_1_byte (abfd, line_ptr);
1653 lh.maximum_ops_per_insn = 1;
1654 if (lh.maximum_ops_per_insn == 0)
1656 (*_bfd_error_handler)
1657 (_("Dwarf Error: Invalid maximum operations per instruction."));
1658 bfd_set_error (bfd_error_bad_value);
1661 lh.default_is_stmt = read_1_byte (abfd, line_ptr);
1663 lh.line_base = read_1_signed_byte (abfd, line_ptr);
1665 lh.line_range = read_1_byte (abfd, line_ptr);
1667 lh.opcode_base = read_1_byte (abfd, line_ptr);
1669 amt = lh.opcode_base * sizeof (unsigned char);
1670 lh.standard_opcode_lengths = (unsigned char *) bfd_alloc (abfd, amt);
1672 lh.standard_opcode_lengths[0] = 1;
1674 for (i = 1; i < lh.opcode_base; ++i)
1676 lh.standard_opcode_lengths[i] = read_1_byte (abfd, line_ptr);
1680 /* Read directory table. */
1681 while ((cur_dir = read_string (abfd, line_ptr, &bytes_read)) != NULL)
1683 line_ptr += bytes_read;
1685 if ((table->num_dirs % DIR_ALLOC_CHUNK) == 0)
1689 amt = table->num_dirs + DIR_ALLOC_CHUNK;
1690 amt *= sizeof (char *);
1692 tmp = (char **) bfd_realloc (table->dirs, amt);
1698 table->dirs[table->num_dirs++] = cur_dir;
1701 line_ptr += bytes_read;
1703 /* Read file name table. */
1704 while ((cur_file = read_string (abfd, line_ptr, &bytes_read)) != NULL)
1706 line_ptr += bytes_read;
1708 if ((table->num_files % FILE_ALLOC_CHUNK) == 0)
1710 struct fileinfo *tmp;
1712 amt = table->num_files + FILE_ALLOC_CHUNK;
1713 amt *= sizeof (struct fileinfo);
1715 tmp = (struct fileinfo *) bfd_realloc (table->files, amt);
1721 table->files[table->num_files].name = cur_file;
1722 table->files[table->num_files].dir =
1723 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
1724 line_ptr += bytes_read;
1725 table->files[table->num_files].time =
1726 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
1727 line_ptr += bytes_read;
1728 table->files[table->num_files].size =
1729 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
1730 line_ptr += bytes_read;
1734 line_ptr += bytes_read;
1736 /* Read the statement sequences until there's nothing left. */
1737 while (line_ptr < line_end)
1739 /* State machine registers. */
1740 bfd_vma address = 0;
1741 unsigned char op_index = 0;
1742 char * filename = table->num_files ? concat_filename (table, 1) : NULL;
1743 unsigned int line = 1;
1744 unsigned int column = 0;
1745 unsigned int discriminator = 0;
1746 int is_stmt = lh.default_is_stmt;
1747 int end_sequence = 0;
1748 /* eraxxon@alumni.rice.edu: Against the DWARF2 specs, some
1749 compilers generate address sequences that are wildly out of
1750 order using DW_LNE_set_address (e.g. Intel C++ 6.0 compiler
1751 for ia64-Linux). Thus, to determine the low and high
1752 address, we must compare on every DW_LNS_copy, etc. */
1753 bfd_vma low_pc = (bfd_vma) -1;
1754 bfd_vma high_pc = 0;
1756 /* Decode the table. */
1757 while (! end_sequence)
1759 op_code = read_1_byte (abfd, line_ptr);
1762 if (op_code >= lh.opcode_base)
1764 /* Special operand. */
1765 adj_opcode = op_code - lh.opcode_base;
1766 if (lh.maximum_ops_per_insn == 1)
1767 address += (adj_opcode / lh.line_range
1768 * lh.minimum_instruction_length);
1771 address += ((op_index + adj_opcode / lh.line_range)
1772 / lh.maximum_ops_per_insn
1773 * lh.minimum_instruction_length);
1774 op_index = ((op_index + adj_opcode / lh.line_range)
1775 % lh.maximum_ops_per_insn);
1777 line += lh.line_base + (adj_opcode % lh.line_range);
1778 /* Append row to matrix using current values. */
1779 if (!add_line_info (table, address, op_index, filename,
1780 line, column, discriminator, 0))
1783 if (address < low_pc)
1785 if (address > high_pc)
1788 else switch (op_code)
1790 case DW_LNS_extended_op:
1791 exop_len = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
1792 line_ptr += bytes_read;
1793 extended_op = read_1_byte (abfd, line_ptr);
1796 switch (extended_op)
1798 case DW_LNE_end_sequence:
1800 if (!add_line_info (table, address, op_index, filename, line,
1801 column, discriminator, end_sequence))
1804 if (address < low_pc)
1806 if (address > high_pc)
1808 if (!arange_add (unit, &unit->arange, low_pc, high_pc))
1811 case DW_LNE_set_address:
1812 address = read_address (unit, line_ptr);
1814 line_ptr += unit->addr_size;
1816 case DW_LNE_define_file:
1817 cur_file = read_string (abfd, line_ptr, &bytes_read);
1818 line_ptr += bytes_read;
1819 if ((table->num_files % FILE_ALLOC_CHUNK) == 0)
1821 struct fileinfo *tmp;
1823 amt = table->num_files + FILE_ALLOC_CHUNK;
1824 amt *= sizeof (struct fileinfo);
1825 tmp = (struct fileinfo *) bfd_realloc (table->files, amt);
1830 table->files[table->num_files].name = cur_file;
1831 table->files[table->num_files].dir =
1832 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
1833 line_ptr += bytes_read;
1834 table->files[table->num_files].time =
1835 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
1836 line_ptr += bytes_read;
1837 table->files[table->num_files].size =
1838 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
1839 line_ptr += bytes_read;
1842 case DW_LNE_set_discriminator:
1844 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
1845 line_ptr += bytes_read;
1847 case DW_LNE_HP_source_file_correlation:
1848 line_ptr += exop_len - 1;
1851 (*_bfd_error_handler)
1852 (_("Dwarf Error: mangled line number section."));
1853 bfd_set_error (bfd_error_bad_value);
1855 if (filename != NULL)
1861 if (!add_line_info (table, address, op_index,
1862 filename, line, column, discriminator, 0))
1865 if (address < low_pc)
1867 if (address > high_pc)
1870 case DW_LNS_advance_pc:
1871 if (lh.maximum_ops_per_insn == 1)
1872 address += (lh.minimum_instruction_length
1873 * read_unsigned_leb128 (abfd, line_ptr,
1877 bfd_vma adjust = read_unsigned_leb128 (abfd, line_ptr,
1879 address = ((op_index + adjust) / lh.maximum_ops_per_insn
1880 * lh.minimum_instruction_length);
1881 op_index = (op_index + adjust) % lh.maximum_ops_per_insn;
1883 line_ptr += bytes_read;
1885 case DW_LNS_advance_line:
1886 line += read_signed_leb128 (abfd, line_ptr, &bytes_read);
1887 line_ptr += bytes_read;
1889 case DW_LNS_set_file:
1893 /* The file and directory tables are 0
1894 based, the references are 1 based. */
1895 file = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
1896 line_ptr += bytes_read;
1899 filename = concat_filename (table, file);
1902 case DW_LNS_set_column:
1903 column = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
1904 line_ptr += bytes_read;
1906 case DW_LNS_negate_stmt:
1907 is_stmt = (!is_stmt);
1909 case DW_LNS_set_basic_block:
1911 case DW_LNS_const_add_pc:
1912 if (lh.maximum_ops_per_insn == 1)
1913 address += (lh.minimum_instruction_length
1914 * ((255 - lh.opcode_base) / lh.line_range));
1917 bfd_vma adjust = ((255 - lh.opcode_base) / lh.line_range);
1918 address += (lh.minimum_instruction_length
1919 * ((op_index + adjust)
1920 / lh.maximum_ops_per_insn));
1921 op_index = (op_index + adjust) % lh.maximum_ops_per_insn;
1924 case DW_LNS_fixed_advance_pc:
1925 address += read_2_bytes (abfd, line_ptr);
1930 /* Unknown standard opcode, ignore it. */
1931 for (i = 0; i < lh.standard_opcode_lengths[op_code]; i++)
1933 (void) read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
1934 line_ptr += bytes_read;
1944 if (sort_line_sequences (table))
1948 if (table->sequences != NULL)
1949 free (table->sequences);
1950 if (table->files != NULL)
1951 free (table->files);
1952 if (table->dirs != NULL)
1957 /* If ADDR is within TABLE set the output parameters and return the
1958 range of addresses covered by the entry used to fill them out.
1959 Otherwise set * FILENAME_PTR to NULL and return 0.
1960 The parameters FILENAME_PTR, LINENUMBER_PTR and DISCRIMINATOR_PTR
1961 are pointers to the objects to be filled in. */
1964 lookup_address_in_line_info_table (struct line_info_table *table,
1966 const char **filename_ptr,
1967 unsigned int *linenumber_ptr,
1968 unsigned int *discriminator_ptr)
1970 struct line_sequence *seq = NULL;
1971 struct line_info *each_line;
1974 /* Binary search the array of sequences. */
1976 high = table->num_sequences;
1979 mid = (low + high) / 2;
1980 seq = &table->sequences[mid];
1981 if (addr < seq->low_pc)
1983 else if (addr >= seq->last_line->address)
1989 if (seq && addr >= seq->low_pc && addr < seq->last_line->address)
1991 /* Note: seq->last_line should be a descendingly sorted list. */
1992 for (each_line = seq->last_line;
1994 each_line = each_line->prev_line)
1995 if (addr >= each_line->address)
1999 && !(each_line->end_sequence || each_line == seq->last_line))
2001 *filename_ptr = each_line->filename;
2002 *linenumber_ptr = each_line->line;
2003 if (discriminator_ptr)
2004 *discriminator_ptr = each_line->discriminator;
2005 return seq->last_line->address - seq->low_pc;
2009 *filename_ptr = NULL;
2013 /* Read in the .debug_ranges section for future reference. */
2016 read_debug_ranges (struct comp_unit *unit)
2018 struct dwarf2_debug *stash = unit->stash;
2019 return read_section (unit->abfd, &stash->debug_sections[debug_ranges],
2021 &stash->dwarf_ranges_buffer, &stash->dwarf_ranges_size);
2024 /* Function table functions. */
2026 /* If ADDR is within UNIT's function tables, set FUNCTION_PTR, and return
2027 TRUE. Note that we need to find the function that has the smallest range
2028 that contains ADDR, to handle inlined functions without depending upon
2029 them being ordered in TABLE by increasing range. */
2032 lookup_address_in_function_table (struct comp_unit *unit,
2034 struct funcinfo **function_ptr)
2036 struct funcinfo* each_func;
2037 struct funcinfo* best_fit = NULL;
2038 bfd_vma best_fit_len = 0;
2039 struct arange *arange;
2041 for (each_func = unit->function_table;
2043 each_func = each_func->prev_func)
2045 for (arange = &each_func->arange;
2047 arange = arange->next)
2049 if (addr >= arange->low && addr < arange->high)
2052 || arange->high - arange->low < best_fit_len)
2054 best_fit = each_func;
2055 best_fit_len = arange->high - arange->low;
2063 *function_ptr = best_fit;
2069 /* If SYM at ADDR is within function table of UNIT, set FILENAME_PTR
2070 and LINENUMBER_PTR, and return TRUE. */
2073 lookup_symbol_in_function_table (struct comp_unit *unit,
2076 const char **filename_ptr,
2077 unsigned int *linenumber_ptr)
2079 struct funcinfo* each_func;
2080 struct funcinfo* best_fit = NULL;
2081 bfd_vma best_fit_len = 0;
2082 struct arange *arange;
2083 const char *name = bfd_asymbol_name (sym);
2084 asection *sec = bfd_get_section (sym);
2086 for (each_func = unit->function_table;
2088 each_func = each_func->prev_func)
2090 for (arange = &each_func->arange;
2092 arange = arange->next)
2094 if ((!each_func->sec || each_func->sec == sec)
2095 && addr >= arange->low
2096 && addr < arange->high
2098 && strcmp (name, each_func->name) == 0
2100 || arange->high - arange->low < best_fit_len))
2102 best_fit = each_func;
2103 best_fit_len = arange->high - arange->low;
2110 best_fit->sec = sec;
2111 *filename_ptr = best_fit->file;
2112 *linenumber_ptr = best_fit->line;
2119 /* Variable table functions. */
2121 /* If SYM is within variable table of UNIT, set FILENAME_PTR and
2122 LINENUMBER_PTR, and return TRUE. */
2125 lookup_symbol_in_variable_table (struct comp_unit *unit,
2128 const char **filename_ptr,
2129 unsigned int *linenumber_ptr)
2131 const char *name = bfd_asymbol_name (sym);
2132 asection *sec = bfd_get_section (sym);
2133 struct varinfo* each;
2135 for (each = unit->variable_table; each; each = each->prev_var)
2136 if (each->stack == 0
2137 && each->file != NULL
2138 && each->name != NULL
2139 && each->addr == addr
2140 && (!each->sec || each->sec == sec)
2141 && strcmp (name, each->name) == 0)
2147 *filename_ptr = each->file;
2148 *linenumber_ptr = each->line;
2156 find_abstract_instance_name (struct comp_unit *unit,
2157 struct attribute *attr_ptr,
2158 bfd_boolean *is_linkage)
2160 bfd *abfd = unit->abfd;
2162 unsigned int abbrev_number, bytes_read, i;
2163 struct abbrev_info *abbrev;
2164 bfd_uint64_t die_ref = attr_ptr->u.val;
2165 struct attribute attr;
2168 /* DW_FORM_ref_addr can reference an entry in a different CU. It
2169 is an offset from the .debug_info section, not the current CU. */
2170 if (attr_ptr->form == DW_FORM_ref_addr)
2172 /* We only support DW_FORM_ref_addr within the same file, so
2173 any relocations should be resolved already. */
2177 info_ptr = unit->sec_info_ptr + die_ref;
2179 /* Now find the CU containing this pointer. */
2180 if (info_ptr >= unit->info_ptr_unit && info_ptr < unit->end_ptr)
2184 /* Check other CUs to see if they contain the abbrev. */
2185 struct comp_unit * u;
2187 for (u = unit->prev_unit; u != NULL; u = u->prev_unit)
2188 if (info_ptr >= u->info_ptr_unit && info_ptr < u->end_ptr)
2192 for (u = unit->next_unit; u != NULL; u = u->next_unit)
2193 if (info_ptr >= u->info_ptr_unit && info_ptr < u->end_ptr)
2198 /* else FIXME: What do we do now ? */
2201 else if (attr_ptr->form == DW_FORM_GNU_ref_alt)
2203 info_ptr = read_alt_indirect_ref (unit, die_ref);
2204 if (info_ptr == NULL)
2206 (*_bfd_error_handler)
2207 (_("Dwarf Error: Unable to read alt ref %u."), die_ref);
2208 bfd_set_error (bfd_error_bad_value);
2211 /* FIXME: Do we need to locate the correct CU, in a similar
2212 fashion to the code in the DW_FORM_ref_addr case above ? */
2215 info_ptr = unit->info_ptr_unit + die_ref;
2217 abbrev_number = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
2218 info_ptr += bytes_read;
2222 abbrev = lookup_abbrev (abbrev_number, unit->abbrevs);
2225 (*_bfd_error_handler)
2226 (_("Dwarf Error: Could not find abbrev number %u."), abbrev_number);
2227 bfd_set_error (bfd_error_bad_value);
2231 for (i = 0; i < abbrev->num_attrs; ++i)
2233 info_ptr = read_attribute (&attr, &abbrev->attrs[i], unit,
2235 if (info_ptr == NULL)
2240 /* Prefer DW_AT_MIPS_linkage_name or DW_AT_linkage_name
2242 if (name == NULL && is_str_attr (attr.form))
2245 if (non_mangled (unit->lang))
2249 case DW_AT_specification:
2250 name = find_abstract_instance_name (unit, &attr, is_linkage);
2252 case DW_AT_linkage_name:
2253 case DW_AT_MIPS_linkage_name:
2254 /* PR 16949: Corrupt debug info can place
2255 non-string forms into these attributes. */
2256 if (is_str_attr (attr.form))
2272 read_rangelist (struct comp_unit *unit, struct arange *arange,
2273 bfd_uint64_t offset)
2275 bfd_byte *ranges_ptr;
2276 bfd_vma base_address = unit->base_address;
2278 if (! unit->stash->dwarf_ranges_buffer)
2280 if (! read_debug_ranges (unit))
2283 ranges_ptr = unit->stash->dwarf_ranges_buffer + offset;
2290 low_pc = read_address (unit, ranges_ptr);
2291 ranges_ptr += unit->addr_size;
2292 high_pc = read_address (unit, ranges_ptr);
2293 ranges_ptr += unit->addr_size;
2295 if (low_pc == 0 && high_pc == 0)
2297 if (low_pc == -1UL && high_pc != -1UL)
2298 base_address = high_pc;
2301 if (!arange_add (unit, arange,
2302 base_address + low_pc, base_address + high_pc))
2309 /* DWARF2 Compilation unit functions. */
2311 /* Scan over each die in a comp. unit looking for functions to add
2312 to the function table and variables to the variable table. */
2315 scan_unit_for_symbols (struct comp_unit *unit)
2317 bfd *abfd = unit->abfd;
2318 bfd_byte *info_ptr = unit->first_child_die_ptr;
2319 int nesting_level = 1;
2320 struct funcinfo **nested_funcs;
2321 int nested_funcs_size;
2323 /* Maintain a stack of in-scope functions and inlined functions, which we
2324 can use to set the caller_func field. */
2325 nested_funcs_size = 32;
2326 nested_funcs = (struct funcinfo **)
2327 bfd_malloc (nested_funcs_size * sizeof (struct funcinfo *));
2328 if (nested_funcs == NULL)
2330 nested_funcs[nesting_level] = 0;
2332 while (nesting_level)
2334 unsigned int abbrev_number, bytes_read, i;
2335 struct abbrev_info *abbrev;
2336 struct attribute attr;
2337 struct funcinfo *func;
2338 struct varinfo *var;
2340 bfd_vma high_pc = 0;
2341 bfd_boolean high_pc_relative = FALSE;
2343 abbrev_number = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
2344 info_ptr += bytes_read;
2346 if (! abbrev_number)
2352 abbrev = lookup_abbrev (abbrev_number,unit->abbrevs);
2355 (*_bfd_error_handler)
2356 (_("Dwarf Error: Could not find abbrev number %u."),
2358 bfd_set_error (bfd_error_bad_value);
2363 if (abbrev->tag == DW_TAG_subprogram
2364 || abbrev->tag == DW_TAG_entry_point
2365 || abbrev->tag == DW_TAG_inlined_subroutine)
2367 bfd_size_type amt = sizeof (struct funcinfo);
2368 func = (struct funcinfo *) bfd_zalloc (abfd, amt);
2371 func->tag = abbrev->tag;
2372 func->prev_func = unit->function_table;
2373 unit->function_table = func;
2374 BFD_ASSERT (!unit->cached);
2376 if (func->tag == DW_TAG_inlined_subroutine)
2377 for (i = nesting_level - 1; i >= 1; i--)
2378 if (nested_funcs[i])
2380 func->caller_func = nested_funcs[i];
2383 nested_funcs[nesting_level] = func;
2388 if (abbrev->tag == DW_TAG_variable)
2390 bfd_size_type amt = sizeof (struct varinfo);
2391 var = (struct varinfo *) bfd_zalloc (abfd, amt);
2394 var->tag = abbrev->tag;
2396 var->prev_var = unit->variable_table;
2397 unit->variable_table = var;
2398 BFD_ASSERT (!unit->cached);
2401 /* No inline function in scope at this nesting level. */
2402 nested_funcs[nesting_level] = 0;
2405 for (i = 0; i < abbrev->num_attrs; ++i)
2407 info_ptr = read_attribute (&attr, &abbrev->attrs[i], unit, info_ptr);
2408 if (info_ptr == NULL)
2415 case DW_AT_call_file:
2416 func->caller_file = concat_filename (unit->line_table,
2420 case DW_AT_call_line:
2421 func->caller_line = attr.u.val;
2424 case DW_AT_abstract_origin:
2425 case DW_AT_specification:
2426 func->name = find_abstract_instance_name (unit, &attr,
2431 /* Prefer DW_AT_MIPS_linkage_name or DW_AT_linkage_name
2433 if (func->name == NULL && is_str_attr (attr.form))
2435 func->name = attr.u.str;
2436 if (non_mangled (unit->lang))
2437 func->is_linkage = TRUE;
2441 case DW_AT_linkage_name:
2442 case DW_AT_MIPS_linkage_name:
2443 /* PR 16949: Corrupt debug info can place
2444 non-string forms into these attributes. */
2445 if (is_str_attr (attr.form))
2447 func->name = attr.u.str;
2448 func->is_linkage = TRUE;
2453 low_pc = attr.u.val;
2457 high_pc = attr.u.val;
2458 high_pc_relative = attr.form != DW_FORM_addr;
2462 if (!read_rangelist (unit, &func->arange, attr.u.val))
2466 case DW_AT_decl_file:
2467 func->file = concat_filename (unit->line_table,
2471 case DW_AT_decl_line:
2472 func->line = attr.u.val;
2484 var->name = attr.u.str;
2487 case DW_AT_decl_file:
2488 var->file = concat_filename (unit->line_table,
2492 case DW_AT_decl_line:
2493 var->line = attr.u.val;
2496 case DW_AT_external:
2497 if (attr.u.val != 0)
2501 case DW_AT_location:
2505 case DW_FORM_block1:
2506 case DW_FORM_block2:
2507 case DW_FORM_block4:
2508 case DW_FORM_exprloc:
2509 if (*attr.u.blk->data == DW_OP_addr)
2513 /* Verify that DW_OP_addr is the only opcode in the
2514 location, in which case the block size will be 1
2515 plus the address size. */
2516 /* ??? For TLS variables, gcc can emit
2517 DW_OP_addr <addr> DW_OP_GNU_push_tls_address
2518 which we don't handle here yet. */
2519 if (attr.u.blk->size == unit->addr_size + 1U)
2520 var->addr = bfd_get (unit->addr_size * 8,
2522 attr.u.blk->data + 1);
2537 if (high_pc_relative)
2540 if (func && high_pc != 0)
2542 if (!arange_add (unit, &func->arange, low_pc, high_pc))
2546 if (abbrev->has_children)
2550 if (nesting_level >= nested_funcs_size)
2552 struct funcinfo **tmp;
2554 nested_funcs_size *= 2;
2555 tmp = (struct funcinfo **)
2556 bfd_realloc (nested_funcs,
2557 nested_funcs_size * sizeof (struct funcinfo *));
2562 nested_funcs[nesting_level] = 0;
2566 free (nested_funcs);
2570 free (nested_funcs);
2574 /* Parse a DWARF2 compilation unit starting at INFO_PTR. This
2575 includes the compilation unit header that proceeds the DIE's, but
2576 does not include the length field that precedes each compilation
2577 unit header. END_PTR points one past the end of this comp unit.
2578 OFFSET_SIZE is the size of DWARF2 offsets (either 4 or 8 bytes).
2580 This routine does not read the whole compilation unit; only enough
2581 to get to the line number information for the compilation unit. */
2583 static struct comp_unit *
2584 parse_comp_unit (struct dwarf2_debug *stash,
2585 bfd_vma unit_length,
2586 bfd_byte *info_ptr_unit,
2587 unsigned int offset_size)
2589 struct comp_unit* unit;
2590 unsigned int version;
2591 bfd_uint64_t abbrev_offset = 0;
2592 unsigned int addr_size;
2593 struct abbrev_info** abbrevs;
2594 unsigned int abbrev_number, bytes_read, i;
2595 struct abbrev_info *abbrev;
2596 struct attribute attr;
2597 bfd_byte *info_ptr = stash->info_ptr;
2598 bfd_byte *end_ptr = info_ptr + unit_length;
2601 bfd_vma high_pc = 0;
2602 bfd *abfd = stash->bfd_ptr;
2603 bfd_boolean high_pc_relative = FALSE;
2605 version = read_2_bytes (abfd, info_ptr);
2607 BFD_ASSERT (offset_size == 4 || offset_size == 8);
2608 if (offset_size == 4)
2609 abbrev_offset = read_4_bytes (abfd, info_ptr);
2611 abbrev_offset = read_8_bytes (abfd, info_ptr);
2612 info_ptr += offset_size;
2613 addr_size = read_1_byte (abfd, info_ptr);
2616 if (version != 2 && version != 3 && version != 4)
2618 (*_bfd_error_handler)
2619 (_("Dwarf Error: found dwarf version '%u', this reader"
2620 " only handles version 2, 3 and 4 information."), version);
2621 bfd_set_error (bfd_error_bad_value);
2625 if (addr_size > sizeof (bfd_vma))
2627 (*_bfd_error_handler)
2628 (_("Dwarf Error: found address size '%u', this reader"
2629 " can not handle sizes greater than '%u'."),
2631 (unsigned int) sizeof (bfd_vma));
2632 bfd_set_error (bfd_error_bad_value);
2636 if (addr_size != 2 && addr_size != 4 && addr_size != 8)
2638 (*_bfd_error_handler)
2639 ("Dwarf Error: found address size '%u', this reader"
2640 " can only handle address sizes '2', '4' and '8'.", addr_size);
2641 bfd_set_error (bfd_error_bad_value);
2645 /* Read the abbrevs for this compilation unit into a table. */
2646 abbrevs = read_abbrevs (abfd, abbrev_offset, stash);
2650 abbrev_number = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
2651 info_ptr += bytes_read;
2652 if (! abbrev_number)
2654 (*_bfd_error_handler) (_("Dwarf Error: Bad abbrev number: %u."),
2656 bfd_set_error (bfd_error_bad_value);
2660 abbrev = lookup_abbrev (abbrev_number, abbrevs);
2663 (*_bfd_error_handler) (_("Dwarf Error: Could not find abbrev number %u."),
2665 bfd_set_error (bfd_error_bad_value);
2669 amt = sizeof (struct comp_unit);
2670 unit = (struct comp_unit *) bfd_zalloc (abfd, amt);
2674 unit->version = version;
2675 unit->addr_size = addr_size;
2676 unit->offset_size = offset_size;
2677 unit->abbrevs = abbrevs;
2678 unit->end_ptr = end_ptr;
2679 unit->stash = stash;
2680 unit->info_ptr_unit = info_ptr_unit;
2681 unit->sec_info_ptr = stash->sec_info_ptr;
2683 for (i = 0; i < abbrev->num_attrs; ++i)
2685 info_ptr = read_attribute (&attr, &abbrev->attrs[i], unit, info_ptr);
2686 if (info_ptr == NULL)
2689 /* Store the data if it is of an attribute we want to keep in a
2690 partial symbol table. */
2693 case DW_AT_stmt_list:
2695 unit->line_offset = attr.u.val;
2699 unit->name = attr.u.str;
2703 low_pc = attr.u.val;
2704 /* If the compilation unit DIE has a DW_AT_low_pc attribute,
2705 this is the base address to use when reading location
2706 lists or range lists. */
2707 if (abbrev->tag == DW_TAG_compile_unit)
2708 unit->base_address = low_pc;
2712 high_pc = attr.u.val;
2713 high_pc_relative = attr.form != DW_FORM_addr;
2717 if (!read_rangelist (unit, &unit->arange, attr.u.val))
2721 case DW_AT_comp_dir:
2723 char *comp_dir = attr.u.str;
2726 /* Irix 6.2 native cc prepends <machine>.: to the compilation
2727 directory, get rid of it. */
2728 char *cp = strchr (comp_dir, ':');
2730 if (cp && cp != comp_dir && cp[-1] == '.' && cp[1] == '/')
2733 unit->comp_dir = comp_dir;
2737 case DW_AT_language:
2738 unit->lang = attr.u.val;
2745 if (high_pc_relative)
2749 if (!arange_add (unit, &unit->arange, low_pc, high_pc))
2753 unit->first_child_die_ptr = info_ptr;
2757 /* Return TRUE if UNIT may contain the address given by ADDR. When
2758 there are functions written entirely with inline asm statements, the
2759 range info in the compilation unit header may not be correct. We
2760 need to consult the line info table to see if a compilation unit
2761 really contains the given address. */
2764 comp_unit_contains_address (struct comp_unit *unit, bfd_vma addr)
2766 struct arange *arange;
2771 arange = &unit->arange;
2774 if (addr >= arange->low && addr < arange->high)
2776 arange = arange->next;
2783 /* If UNIT contains ADDR, set the output parameters to the values for
2784 the line containing ADDR. The output parameters, FILENAME_PTR,
2785 FUNCTION_PTR, and LINENUMBER_PTR, are pointers to the objects
2788 Returns the range of addresses covered by the entry that was used
2789 to fill in *LINENUMBER_PTR or 0 if it was not filled in. */
2792 comp_unit_find_nearest_line (struct comp_unit *unit,
2794 const char **filename_ptr,
2795 struct funcinfo **function_ptr,
2796 unsigned int *linenumber_ptr,
2797 unsigned int *discriminator_ptr,
2798 struct dwarf2_debug *stash)
2805 if (! unit->line_table)
2807 if (! unit->stmtlist)
2813 unit->line_table = decode_line_info (unit, stash);
2815 if (! unit->line_table)
2821 if (unit->first_child_die_ptr < unit->end_ptr
2822 && ! scan_unit_for_symbols (unit))
2829 *function_ptr = NULL;
2830 func_p = lookup_address_in_function_table (unit, addr, function_ptr);
2831 if (func_p && (*function_ptr)->tag == DW_TAG_inlined_subroutine)
2832 stash->inliner_chain = *function_ptr;
2834 return lookup_address_in_line_info_table (unit->line_table, addr,
2840 /* Check to see if line info is already decoded in a comp_unit.
2841 If not, decode it. Returns TRUE if no errors were encountered;
2845 comp_unit_maybe_decode_line_info (struct comp_unit *unit,
2846 struct dwarf2_debug *stash)
2851 if (! unit->line_table)
2853 if (! unit->stmtlist)
2859 unit->line_table = decode_line_info (unit, stash);
2861 if (! unit->line_table)
2867 if (unit->first_child_die_ptr < unit->end_ptr
2868 && ! scan_unit_for_symbols (unit))
2878 /* If UNIT contains SYM at ADDR, set the output parameters to the
2879 values for the line containing SYM. The output parameters,
2880 FILENAME_PTR, and LINENUMBER_PTR, are pointers to the objects to be
2883 Return TRUE if UNIT contains SYM, and no errors were encountered;
2887 comp_unit_find_line (struct comp_unit *unit,
2890 const char **filename_ptr,
2891 unsigned int *linenumber_ptr,
2892 struct dwarf2_debug *stash)
2894 if (!comp_unit_maybe_decode_line_info (unit, stash))
2897 if (sym->flags & BSF_FUNCTION)
2898 return lookup_symbol_in_function_table (unit, sym, addr,
2902 return lookup_symbol_in_variable_table (unit, sym, addr,
2907 static struct funcinfo *
2908 reverse_funcinfo_list (struct funcinfo *head)
2910 struct funcinfo *rhead;
2911 struct funcinfo *temp;
2913 for (rhead = NULL; head; head = temp)
2915 temp = head->prev_func;
2916 head->prev_func = rhead;
2922 static struct varinfo *
2923 reverse_varinfo_list (struct varinfo *head)
2925 struct varinfo *rhead;
2926 struct varinfo *temp;
2928 for (rhead = NULL; head; head = temp)
2930 temp = head->prev_var;
2931 head->prev_var = rhead;
2937 /* Extract all interesting funcinfos and varinfos of a compilation
2938 unit into hash tables for faster lookup. Returns TRUE if no
2939 errors were enountered; FALSE otherwise. */
2942 comp_unit_hash_info (struct dwarf2_debug *stash,
2943 struct comp_unit *unit,
2944 struct info_hash_table *funcinfo_hash_table,
2945 struct info_hash_table *varinfo_hash_table)
2947 struct funcinfo* each_func;
2948 struct varinfo* each_var;
2949 bfd_boolean okay = TRUE;
2951 BFD_ASSERT (stash->info_hash_status != STASH_INFO_HASH_DISABLED);
2953 if (!comp_unit_maybe_decode_line_info (unit, stash))
2956 BFD_ASSERT (!unit->cached);
2958 /* To preserve the original search order, we went to visit the function
2959 infos in the reversed order of the list. However, making the list
2960 bi-directional use quite a bit of extra memory. So we reverse
2961 the list first, traverse the list in the now reversed order and
2962 finally reverse the list again to get back the original order. */
2963 unit->function_table = reverse_funcinfo_list (unit->function_table);
2964 for (each_func = unit->function_table;
2966 each_func = each_func->prev_func)
2968 /* Skip nameless functions. */
2969 if (each_func->name)
2970 /* There is no need to copy name string into hash table as
2971 name string is either in the dwarf string buffer or
2972 info in the stash. */
2973 okay = insert_info_hash_table (funcinfo_hash_table, each_func->name,
2974 (void*) each_func, FALSE);
2976 unit->function_table = reverse_funcinfo_list (unit->function_table);
2980 /* We do the same for variable infos. */
2981 unit->variable_table = reverse_varinfo_list (unit->variable_table);
2982 for (each_var = unit->variable_table;
2984 each_var = each_var->prev_var)
2986 /* Skip stack vars and vars with no files or names. */
2987 if (each_var->stack == 0
2988 && each_var->file != NULL
2989 && each_var->name != NULL)
2990 /* There is no need to copy name string into hash table as
2991 name string is either in the dwarf string buffer or
2992 info in the stash. */
2993 okay = insert_info_hash_table (varinfo_hash_table, each_var->name,
2994 (void*) each_var, FALSE);
2997 unit->variable_table = reverse_varinfo_list (unit->variable_table);
2998 unit->cached = TRUE;
3002 /* Locate a section in a BFD containing debugging info. The search starts
3003 from the section after AFTER_SEC, or from the first section in the BFD if
3004 AFTER_SEC is NULL. The search works by examining the names of the
3005 sections. There are three permissiable names. The first two are given
3006 by DEBUG_SECTIONS[debug_info] (whose standard DWARF2 names are .debug_info
3007 and .zdebug_info). The third is a prefix .gnu.linkonce.wi.
3008 This is a variation on the .debug_info section which has a checksum
3009 describing the contents appended onto the name. This allows the linker to
3010 identify and discard duplicate debugging sections for different
3011 compilation units. */
3012 #define GNU_LINKONCE_INFO ".gnu.linkonce.wi."
3015 find_debug_info (bfd *abfd, const struct dwarf_debug_section *debug_sections,
3016 asection *after_sec)
3021 if (after_sec == NULL)
3023 look = debug_sections[debug_info].uncompressed_name;
3024 msec = bfd_get_section_by_name (abfd, look);
3028 look = debug_sections[debug_info].compressed_name;
3031 msec = bfd_get_section_by_name (abfd, look);
3036 for (msec = abfd->sections; msec != NULL; msec = msec->next)
3037 if (CONST_STRNEQ (msec->name, GNU_LINKONCE_INFO))
3043 for (msec = after_sec->next; msec != NULL; msec = msec->next)
3045 look = debug_sections[debug_info].uncompressed_name;
3046 if (strcmp (msec->name, look) == 0)
3049 look = debug_sections[debug_info].compressed_name;
3050 if (look != NULL && strcmp (msec->name, look) == 0)
3053 if (CONST_STRNEQ (msec->name, GNU_LINKONCE_INFO))
3060 /* Transfer VMAs from object file to separate debug file. */
3063 set_debug_vma (bfd *orig_bfd, bfd *debug_bfd)
3067 for (s = orig_bfd->sections, d = debug_bfd->sections;
3068 s != NULL && d != NULL;
3069 s = s->next, d = d->next)
3071 if ((d->flags & SEC_DEBUGGING) != 0)
3073 /* ??? Assumes 1-1 correspondence between sections in the
3075 if (strcmp (s->name, d->name) == 0)
3077 d->output_section = s->output_section;
3078 d->output_offset = s->output_offset;
3084 /* Unset vmas for adjusted sections in STASH. */
3087 unset_sections (struct dwarf2_debug *stash)
3090 struct adjusted_section *p;
3092 i = stash->adjusted_section_count;
3093 p = stash->adjusted_sections;
3094 for (; i > 0; i--, p++)
3095 p->section->vma = 0;
3098 /* Set VMAs for allocated and .debug_info sections in ORIG_BFD, a
3099 relocatable object file. VMAs are normally all zero in relocatable
3100 object files, so if we want to distinguish locations in sections by
3101 address we need to set VMAs so the sections do not overlap. We
3102 also set VMA on .debug_info so that when we have multiple
3103 .debug_info sections (or the linkonce variant) they also do not
3104 overlap. The multiple .debug_info sections make up a single
3105 logical section. ??? We should probably do the same for other
3109 place_sections (bfd *orig_bfd, struct dwarf2_debug *stash)
3112 struct adjusted_section *p;
3114 const char *debug_info_name;
3116 if (stash->adjusted_section_count != 0)
3118 i = stash->adjusted_section_count;
3119 p = stash->adjusted_sections;
3120 for (; i > 0; i--, p++)
3121 p->section->vma = p->adj_vma;
3125 debug_info_name = stash->debug_sections[debug_info].uncompressed_name;
3132 for (sect = abfd->sections; sect != NULL; sect = sect->next)
3136 if ((sect->output_section != NULL
3137 && sect->output_section != sect
3138 && (sect->flags & SEC_DEBUGGING) == 0)
3142 is_debug_info = (strcmp (sect->name, debug_info_name) == 0
3143 || CONST_STRNEQ (sect->name, GNU_LINKONCE_INFO));
3145 if (!((sect->flags & SEC_ALLOC) != 0 && abfd == orig_bfd)
3151 if (abfd == stash->bfd_ptr)
3153 abfd = stash->bfd_ptr;
3157 stash->adjusted_section_count = -1;
3160 bfd_vma last_vma = 0, last_dwarf = 0;
3161 bfd_size_type amt = i * sizeof (struct adjusted_section);
3163 p = (struct adjusted_section *) bfd_malloc (amt);
3167 stash->adjusted_sections = p;
3168 stash->adjusted_section_count = i;
3175 for (sect = abfd->sections; sect != NULL; sect = sect->next)
3180 if ((sect->output_section != NULL
3181 && sect->output_section != sect
3182 && (sect->flags & SEC_DEBUGGING) == 0)
3186 is_debug_info = (strcmp (sect->name, debug_info_name) == 0
3187 || CONST_STRNEQ (sect->name, GNU_LINKONCE_INFO));
3189 if (!((sect->flags & SEC_ALLOC) != 0 && abfd == orig_bfd)
3193 sz = sect->rawsize ? sect->rawsize : sect->size;
3197 BFD_ASSERT (sect->alignment_power == 0);
3198 sect->vma = last_dwarf;
3203 /* Align the new address to the current section
3205 last_vma = ((last_vma
3206 + ~((bfd_vma) -1 << sect->alignment_power))
3207 & ((bfd_vma) -1 << sect->alignment_power));
3208 sect->vma = last_vma;
3213 p->adj_vma = sect->vma;
3216 if (abfd == stash->bfd_ptr)
3218 abfd = stash->bfd_ptr;
3222 if (orig_bfd != stash->bfd_ptr)
3223 set_debug_vma (orig_bfd, stash->bfd_ptr);
3228 /* Look up a funcinfo by name using the given info hash table. If found,
3229 also update the locations pointed to by filename_ptr and linenumber_ptr.
3231 This function returns TRUE if a funcinfo that matches the given symbol
3232 and address is found with any error; otherwise it returns FALSE. */
3235 info_hash_lookup_funcinfo (struct info_hash_table *hash_table,
3238 const char **filename_ptr,
3239 unsigned int *linenumber_ptr)
3241 struct funcinfo* each_func;
3242 struct funcinfo* best_fit = NULL;
3243 bfd_vma best_fit_len = 0;
3244 struct info_list_node *node;
3245 struct arange *arange;
3246 const char *name = bfd_asymbol_name (sym);
3247 asection *sec = bfd_get_section (sym);
3249 for (node = lookup_info_hash_table (hash_table, name);
3253 each_func = (struct funcinfo *) node->info;
3254 for (arange = &each_func->arange;
3256 arange = arange->next)
3258 if ((!each_func->sec || each_func->sec == sec)
3259 && addr >= arange->low
3260 && addr < arange->high
3262 || arange->high - arange->low < best_fit_len))
3264 best_fit = each_func;
3265 best_fit_len = arange->high - arange->low;
3272 best_fit->sec = sec;
3273 *filename_ptr = best_fit->file;
3274 *linenumber_ptr = best_fit->line;
3281 /* Look up a varinfo by name using the given info hash table. If found,
3282 also update the locations pointed to by filename_ptr and linenumber_ptr.
3284 This function returns TRUE if a varinfo that matches the given symbol
3285 and address is found with any error; otherwise it returns FALSE. */
3288 info_hash_lookup_varinfo (struct info_hash_table *hash_table,
3291 const char **filename_ptr,
3292 unsigned int *linenumber_ptr)
3294 const char *name = bfd_asymbol_name (sym);
3295 asection *sec = bfd_get_section (sym);
3296 struct varinfo* each;
3297 struct info_list_node *node;
3299 for (node = lookup_info_hash_table (hash_table, name);
3303 each = (struct varinfo *) node->info;
3304 if (each->addr == addr
3305 && (!each->sec || each->sec == sec))
3308 *filename_ptr = each->file;
3309 *linenumber_ptr = each->line;
3317 /* Update the funcinfo and varinfo info hash tables if they are
3318 not up to date. Returns TRUE if there is no error; otherwise
3319 returns FALSE and disable the info hash tables. */
3322 stash_maybe_update_info_hash_tables (struct dwarf2_debug *stash)
3324 struct comp_unit *each;
3326 /* Exit if hash tables are up-to-date. */
3327 if (stash->all_comp_units == stash->hash_units_head)
3330 if (stash->hash_units_head)
3331 each = stash->hash_units_head->prev_unit;
3333 each = stash->last_comp_unit;
3337 if (!comp_unit_hash_info (stash, each, stash->funcinfo_hash_table,
3338 stash->varinfo_hash_table))
3340 stash->info_hash_status = STASH_INFO_HASH_DISABLED;
3343 each = each->prev_unit;
3346 stash->hash_units_head = stash->all_comp_units;
3350 /* Check consistency of info hash tables. This is for debugging only. */
3352 static void ATTRIBUTE_UNUSED
3353 stash_verify_info_hash_table (struct dwarf2_debug *stash)
3355 struct comp_unit *each_unit;
3356 struct funcinfo *each_func;
3357 struct varinfo *each_var;
3358 struct info_list_node *node;
3361 for (each_unit = stash->all_comp_units;
3363 each_unit = each_unit->next_unit)
3365 for (each_func = each_unit->function_table;
3367 each_func = each_func->prev_func)
3369 if (!each_func->name)
3371 node = lookup_info_hash_table (stash->funcinfo_hash_table,
3375 while (node && !found)
3377 found = node->info == each_func;
3383 for (each_var = each_unit->variable_table;
3385 each_var = each_var->prev_var)
3387 if (!each_var->name || !each_var->file || each_var->stack)
3389 node = lookup_info_hash_table (stash->varinfo_hash_table,
3393 while (node && !found)
3395 found = node->info == each_var;
3403 /* Check to see if we want to enable the info hash tables, which consume
3404 quite a bit of memory. Currently we only check the number times
3405 bfd_dwarf2_find_line is called. In the future, we may also want to
3406 take the number of symbols into account. */
3409 stash_maybe_enable_info_hash_tables (bfd *abfd, struct dwarf2_debug *stash)
3411 BFD_ASSERT (stash->info_hash_status == STASH_INFO_HASH_OFF);
3413 if (stash->info_hash_count++ < STASH_INFO_HASH_TRIGGER)
3416 /* FIXME: Maybe we should check the reduce_memory_overheads
3417 and optimize fields in the bfd_link_info structure ? */
3419 /* Create hash tables. */
3420 stash->funcinfo_hash_table = create_info_hash_table (abfd);
3421 stash->varinfo_hash_table = create_info_hash_table (abfd);
3422 if (!stash->funcinfo_hash_table || !stash->varinfo_hash_table)
3424 /* Turn off info hashes if any allocation above fails. */
3425 stash->info_hash_status = STASH_INFO_HASH_DISABLED;
3428 /* We need a forced update so that the info hash tables will
3429 be created even though there is no compilation unit. That
3430 happens if STASH_INFO_HASH_TRIGGER is 0. */
3431 stash_maybe_update_info_hash_tables (stash);
3432 stash->info_hash_status = STASH_INFO_HASH_ON;
3435 /* Find the file and line associated with a symbol and address using the
3436 info hash tables of a stash. If there is a match, the function returns
3437 TRUE and update the locations pointed to by filename_ptr and linenumber_ptr;
3438 otherwise it returns FALSE. */
3441 stash_find_line_fast (struct dwarf2_debug *stash,
3444 const char **filename_ptr,
3445 unsigned int *linenumber_ptr)
3447 BFD_ASSERT (stash->info_hash_status == STASH_INFO_HASH_ON);
3449 if (sym->flags & BSF_FUNCTION)
3450 return info_hash_lookup_funcinfo (stash->funcinfo_hash_table, sym, addr,
3451 filename_ptr, linenumber_ptr);
3452 return info_hash_lookup_varinfo (stash->varinfo_hash_table, sym, addr,
3453 filename_ptr, linenumber_ptr);
3456 /* Save current section VMAs. */
3459 save_section_vma (const bfd *abfd, struct dwarf2_debug *stash)
3464 if (abfd->section_count == 0)
3466 stash->sec_vma = bfd_malloc (sizeof (*stash->sec_vma) * abfd->section_count);
3467 if (stash->sec_vma == NULL)
3469 for (i = 0, s = abfd->sections; i < abfd->section_count; i++, s = s->next)
3471 if (s->output_section != NULL)
3472 stash->sec_vma[i] = s->output_section->vma + s->output_offset;
3474 stash->sec_vma[i] = s->vma;
3479 /* Compare current section VMAs against those at the time the stash
3480 was created. If find_nearest_line is used in linker warnings or
3481 errors early in the link process, the debug info stash will be
3482 invalid for later calls. This is because we relocate debug info
3483 sections, so the stashed section contents depend on symbol values,
3484 which in turn depend on section VMAs. */
3487 section_vma_same (const bfd *abfd, const struct dwarf2_debug *stash)
3492 for (i = 0, s = abfd->sections; i < abfd->section_count; i++, s = s->next)
3496 if (s->output_section != NULL)
3497 vma = s->output_section->vma + s->output_offset;
3500 if (vma != stash->sec_vma[i])
3506 /* Read debug information from DEBUG_BFD when DEBUG_BFD is specified.
3507 If DEBUG_BFD is not specified, we read debug information from ABFD
3508 or its gnu_debuglink. The results will be stored in PINFO.
3509 The function returns TRUE iff debug information is ready. */
3512 _bfd_dwarf2_slurp_debug_info (bfd *abfd, bfd *debug_bfd,
3513 const struct dwarf_debug_section *debug_sections,
3516 bfd_boolean do_place)
3518 bfd_size_type amt = sizeof (struct dwarf2_debug);
3519 bfd_size_type total_size;
3521 struct dwarf2_debug *stash = (struct dwarf2_debug *) *pinfo;
3525 if (section_vma_same (abfd, stash))
3527 _bfd_dwarf2_cleanup_debug_info (abfd, pinfo);
3528 memset (stash, 0, amt);
3532 stash = (struct dwarf2_debug *) bfd_zalloc (abfd, amt);
3536 stash->debug_sections = debug_sections;
3537 stash->syms = symbols;
3538 if (!save_section_vma (abfd, stash))
3543 if (debug_bfd == NULL)
3546 msec = find_debug_info (debug_bfd, debug_sections, NULL);
3547 if (msec == NULL && abfd == debug_bfd)
3549 char * debug_filename = bfd_follow_gnu_debuglink (abfd, DEBUGDIR);
3551 if (debug_filename == NULL)
3552 /* No dwarf2 info, and no gnu_debuglink to follow.
3553 Note that at this point the stash has been allocated, but
3554 contains zeros. This lets future calls to this function
3555 fail more quickly. */
3558 if ((debug_bfd = bfd_openr (debug_filename, NULL)) == NULL
3559 || ! bfd_check_format (debug_bfd, bfd_object)
3560 || (msec = find_debug_info (debug_bfd,
3561 debug_sections, NULL)) == NULL
3562 || !bfd_generic_link_read_symbols (debug_bfd))
3565 bfd_close (debug_bfd);
3566 /* FIXME: Should we report our failure to follow the debuglink ? */
3567 free (debug_filename);
3571 symbols = bfd_get_outsymbols (debug_bfd);
3572 stash->syms = symbols;
3573 stash->close_on_cleanup = TRUE;
3575 stash->bfd_ptr = debug_bfd;
3578 && !place_sections (abfd, stash))
3581 /* There can be more than one DWARF2 info section in a BFD these
3582 days. First handle the easy case when there's only one. If
3583 there's more than one, try case two: none of the sections is
3584 compressed. In that case, read them all in and produce one
3585 large stash. We do this in two passes - in the first pass we
3586 just accumulate the section sizes, and in the second pass we
3587 read in the section's contents. (The allows us to avoid
3588 reallocing the data as we add sections to the stash.) If
3589 some or all sections are compressed, then do things the slow
3590 way, with a bunch of reallocs. */
3592 if (! find_debug_info (debug_bfd, debug_sections, msec))
3594 /* Case 1: only one info section. */
3595 total_size = msec->size;
3596 if (! read_section (debug_bfd, &stash->debug_sections[debug_info],
3598 &stash->info_ptr_memory, &total_size))
3603 /* Case 2: multiple sections. */
3604 for (total_size = 0;
3606 msec = find_debug_info (debug_bfd, debug_sections, msec))
3607 total_size += msec->size;
3609 stash->info_ptr_memory = (bfd_byte *) bfd_malloc (total_size);
3610 if (stash->info_ptr_memory == NULL)
3614 for (msec = find_debug_info (debug_bfd, debug_sections, NULL);
3616 msec = find_debug_info (debug_bfd, debug_sections, msec))
3624 if (!(bfd_simple_get_relocated_section_contents
3625 (debug_bfd, msec, stash->info_ptr_memory + total_size,
3633 stash->info_ptr = stash->info_ptr_memory;
3634 stash->info_ptr_end = stash->info_ptr + total_size;
3635 stash->sec = find_debug_info (debug_bfd, debug_sections, NULL);
3636 stash->sec_info_ptr = stash->info_ptr;
3640 /* Find the source code location of SYMBOL. If SYMBOL is NULL
3641 then find the nearest source code location corresponding to
3642 the address SECTION + OFFSET.
3643 Returns TRUE if the line is found without error and fills in
3644 FILENAME_PTR and LINENUMBER_PTR. In the case where SYMBOL was
3645 NULL the FUNCTIONNAME_PTR is also filled in.
3646 SYMBOLS contains the symbol table for ABFD.
3647 DEBUG_SECTIONS contains the name of the dwarf debug sections.
3648 ADDR_SIZE is the number of bytes in the initial .debug_info length
3649 field and in the abbreviation offset, or zero to indicate that the
3650 default value should be used. */
3653 _bfd_dwarf2_find_nearest_line (bfd *abfd,
3658 const char **filename_ptr,
3659 const char **functionname_ptr,
3660 unsigned int *linenumber_ptr,
3661 unsigned int *discriminator_ptr,
3662 const struct dwarf_debug_section *debug_sections,
3663 unsigned int addr_size,
3666 /* Read each compilation unit from the section .debug_info, and check
3667 to see if it contains the address we are searching for. If yes,
3668 lookup the address, and return the line number info. If no, go
3669 on to the next compilation unit.
3671 We keep a list of all the previously read compilation units, and
3672 a pointer to the next un-read compilation unit. Check the
3673 previously read units before reading more. */
3674 struct dwarf2_debug *stash;
3675 /* What address are we looking for? */
3677 struct comp_unit* each;
3678 struct funcinfo *function = NULL;
3679 bfd_boolean found = FALSE;
3680 bfd_boolean do_line;
3682 *filename_ptr = NULL;
3683 if (functionname_ptr != NULL)
3684 *functionname_ptr = NULL;
3685 *linenumber_ptr = 0;
3686 if (discriminator_ptr)
3687 *discriminator_ptr = 0;
3689 if (! _bfd_dwarf2_slurp_debug_info (abfd, NULL, debug_sections,
3691 (abfd->flags & (EXEC_P | DYNAMIC)) == 0))
3694 stash = (struct dwarf2_debug *) *pinfo;
3696 do_line = symbol != NULL;
3699 BFD_ASSERT (section == NULL && offset == 0 && functionname_ptr == NULL);
3700 section = bfd_get_section (symbol);
3701 addr = symbol->value;
3705 BFD_ASSERT (section != NULL && functionname_ptr != NULL);
3709 if (section->output_section)
3710 addr += section->output_section->vma + section->output_offset;
3712 addr += section->vma;
3714 /* A null info_ptr indicates that there is no dwarf2 info
3715 (or that an error occured while setting up the stash). */
3716 if (! stash->info_ptr)
3719 stash->inliner_chain = NULL;
3721 /* Check the previously read comp. units first. */
3724 /* The info hash tables use quite a bit of memory. We may not want to
3725 always use them. We use some heuristics to decide if and when to
3727 if (stash->info_hash_status == STASH_INFO_HASH_OFF)
3728 stash_maybe_enable_info_hash_tables (abfd, stash);
3730 /* Keep info hash table up to date if they are available. Note that we
3731 may disable the hash tables if there is any error duing update. */
3732 if (stash->info_hash_status == STASH_INFO_HASH_ON)
3733 stash_maybe_update_info_hash_tables (stash);
3735 if (stash->info_hash_status == STASH_INFO_HASH_ON)
3737 found = stash_find_line_fast (stash, symbol, addr, filename_ptr,
3744 /* Check the previously read comp. units first. */
3745 for (each = stash->all_comp_units; each; each = each->next_unit)
3746 if ((symbol->flags & BSF_FUNCTION) == 0
3747 || each->arange.high == 0
3748 || comp_unit_contains_address (each, addr))
3750 found = comp_unit_find_line (each, symbol, addr, filename_ptr,
3751 linenumber_ptr, stash);
3759 bfd_vma min_range = (bfd_vma) -1;
3760 const char * local_filename = NULL;
3761 struct funcinfo *local_function = NULL;
3762 unsigned int local_linenumber = 0;
3763 unsigned int local_discriminator = 0;
3765 for (each = stash->all_comp_units; each; each = each->next_unit)
3767 bfd_vma range = (bfd_vma) -1;
3769 found = ((each->arange.high == 0
3770 || comp_unit_contains_address (each, addr))
3771 && (range = comp_unit_find_nearest_line (each, addr,
3775 & local_discriminator,
3779 /* PRs 15935 15994: Bogus debug information may have provided us
3780 with an erroneous match. We attempt to counter this by
3781 selecting the match that has the smallest address range
3782 associated with it. (We are assuming that corrupt debug info
3783 will tend to result in extra large address ranges rather than
3784 extra small ranges).
3786 This does mean that we scan through all of the CUs associated
3787 with the bfd each time this function is called. But this does
3788 have the benefit of producing consistent results every time the
3789 function is called. */
3790 if (range <= min_range)
3792 if (filename_ptr && local_filename)
3793 * filename_ptr = local_filename;
3795 function = local_function;
3796 if (discriminator_ptr && local_discriminator)
3797 * discriminator_ptr = local_discriminator;
3798 if (local_linenumber)
3799 * linenumber_ptr = local_linenumber;
3805 if (* linenumber_ptr)
3812 /* The DWARF2 spec says that the initial length field, and the
3813 offset of the abbreviation table, should both be 4-byte values.
3814 However, some compilers do things differently. */
3817 BFD_ASSERT (addr_size == 4 || addr_size == 8);
3819 /* Read each remaining comp. units checking each as they are read. */
3820 while (stash->info_ptr < stash->info_ptr_end)
3823 unsigned int offset_size = addr_size;
3824 bfd_byte *info_ptr_unit = stash->info_ptr;
3826 length = read_4_bytes (stash->bfd_ptr, stash->info_ptr);
3827 /* A 0xffffff length is the DWARF3 way of indicating
3828 we use 64-bit offsets, instead of 32-bit offsets. */
3829 if (length == 0xffffffff)
3832 length = read_8_bytes (stash->bfd_ptr, stash->info_ptr + 4);
3833 stash->info_ptr += 12;
3835 /* A zero length is the IRIX way of indicating 64-bit offsets,
3836 mostly because the 64-bit length will generally fit in 32
3837 bits, and the endianness helps. */
3838 else if (length == 0)
3841 length = read_4_bytes (stash->bfd_ptr, stash->info_ptr + 4);
3842 stash->info_ptr += 8;
3844 /* In the absence of the hints above, we assume 32-bit DWARF2
3845 offsets even for targets with 64-bit addresses, because:
3846 a) most of the time these targets will not have generated
3847 more than 2Gb of debug info and so will not need 64-bit
3850 b) if they do use 64-bit offsets but they are not using
3851 the size hints that are tested for above then they are
3852 not conforming to the DWARF3 standard anyway. */
3853 else if (addr_size == 8)
3856 stash->info_ptr += 4;
3859 stash->info_ptr += 4;
3863 each = parse_comp_unit (stash, length, info_ptr_unit,
3866 /* The dwarf information is damaged, don't trust it any
3869 stash->info_ptr += length;
3871 if (stash->all_comp_units)
3872 stash->all_comp_units->prev_unit = each;
3874 stash->last_comp_unit = each;
3876 each->next_unit = stash->all_comp_units;
3877 stash->all_comp_units = each;
3879 /* DW_AT_low_pc and DW_AT_high_pc are optional for
3880 compilation units. If we don't have them (i.e.,
3881 unit->high == 0), we need to consult the line info table
3882 to see if a compilation unit contains the given
3885 found = (((symbol->flags & BSF_FUNCTION) == 0
3886 || each->arange.high == 0
3887 || comp_unit_contains_address (each, addr))
3888 && comp_unit_find_line (each, symbol, addr,
3893 found = ((each->arange.high == 0
3894 || comp_unit_contains_address (each, addr))
3895 && comp_unit_find_nearest_line (each, addr,
3902 if ((bfd_vma) (stash->info_ptr - stash->sec_info_ptr)
3903 == stash->sec->size)
3905 stash->sec = find_debug_info (stash->bfd_ptr, debug_sections,
3907 stash->sec_info_ptr = stash->info_ptr;
3918 if (!function->is_linkage
3919 && _bfd_elf_find_function (abfd, symbols, section, offset,
3920 *filename_ptr ? NULL : filename_ptr,
3923 function->name = *functionname_ptr;
3924 function->is_linkage = TRUE;
3927 *functionname_ptr = function->name;
3929 if ((abfd->flags & (EXEC_P | DYNAMIC)) == 0)
3930 unset_sections (stash);
3936 _bfd_dwarf2_find_inliner_info (bfd *abfd ATTRIBUTE_UNUSED,
3937 const char **filename_ptr,
3938 const char **functionname_ptr,
3939 unsigned int *linenumber_ptr,
3942 struct dwarf2_debug *stash;
3944 stash = (struct dwarf2_debug *) *pinfo;
3947 struct funcinfo *func = stash->inliner_chain;
3949 if (func && func->caller_func)
3951 *filename_ptr = func->caller_file;
3952 *functionname_ptr = func->caller_func->name;
3953 *linenumber_ptr = func->caller_line;
3954 stash->inliner_chain = func->caller_func;
3963 _bfd_dwarf2_cleanup_debug_info (bfd *abfd, void **pinfo)
3965 struct dwarf2_debug *stash = (struct dwarf2_debug *) *pinfo;
3966 struct comp_unit *each;
3968 if (abfd == NULL || stash == NULL)
3971 for (each = stash->all_comp_units; each; each = each->next_unit)
3973 struct abbrev_info **abbrevs = each->abbrevs;
3974 struct funcinfo *function_table = each->function_table;
3975 struct varinfo *variable_table = each->variable_table;
3978 for (i = 0; i < ABBREV_HASH_SIZE; i++)
3980 struct abbrev_info *abbrev = abbrevs[i];
3984 free (abbrev->attrs);
3985 abbrev = abbrev->next;
3989 if (each->line_table)
3991 free (each->line_table->dirs);
3992 free (each->line_table->files);
3995 while (function_table)
3997 if (function_table->file)
3999 free (function_table->file);
4000 function_table->file = NULL;
4003 if (function_table->caller_file)
4005 free (function_table->caller_file);
4006 function_table->caller_file = NULL;
4008 function_table = function_table->prev_func;
4011 while (variable_table)
4013 if (variable_table->file)
4015 free (variable_table->file);
4016 variable_table->file = NULL;
4019 variable_table = variable_table->prev_var;
4023 if (stash->dwarf_abbrev_buffer)
4024 free (stash->dwarf_abbrev_buffer);
4025 if (stash->dwarf_line_buffer)
4026 free (stash->dwarf_line_buffer);
4027 if (stash->dwarf_str_buffer)
4028 free (stash->dwarf_str_buffer);
4029 if (stash->dwarf_ranges_buffer)
4030 free (stash->dwarf_ranges_buffer);
4031 if (stash->info_ptr_memory)
4032 free (stash->info_ptr_memory);
4033 if (stash->close_on_cleanup)
4034 bfd_close (stash->bfd_ptr);
4035 if (stash->alt_dwarf_str_buffer)
4036 free (stash->alt_dwarf_str_buffer);
4037 if (stash->alt_dwarf_info_buffer)
4038 free (stash->alt_dwarf_info_buffer);
4040 free (stash->sec_vma);
4041 if (stash->adjusted_sections)
4042 free (stash->adjusted_sections);
4043 if (stash->alt_bfd_ptr)
4044 bfd_close (stash->alt_bfd_ptr);
4047 /* Find the function to a particular section and offset,
4048 for error reporting. */
4051 _bfd_elf_find_function (bfd *abfd,
4055 const char **filename_ptr,
4056 const char **functionname_ptr)
4058 struct elf_find_function_cache
4060 asection *last_section;
4062 const char *filename;
4063 bfd_size_type func_size;
4066 if (symbols == NULL)
4069 if (bfd_get_flavour (abfd) != bfd_target_elf_flavour)
4072 cache = elf_tdata (abfd)->elf_find_function_cache;
4075 cache = bfd_zalloc (abfd, sizeof (*cache));
4076 elf_tdata (abfd)->elf_find_function_cache = cache;
4080 if (cache->last_section != section
4081 || cache->func == NULL
4082 || offset < cache->func->value
4083 || offset >= cache->func->value + cache->func_size)
4088 /* ??? Given multiple file symbols, it is impossible to reliably
4089 choose the right file name for global symbols. File symbols are
4090 local symbols, and thus all file symbols must sort before any
4091 global symbols. The ELF spec may be interpreted to say that a
4092 file symbol must sort before other local symbols, but currently
4093 ld -r doesn't do this. So, for ld -r output, it is possible to
4094 make a better choice of file name for local symbols by ignoring
4095 file symbols appearing after a given local symbol. */
4096 enum { nothing_seen, symbol_seen, file_after_symbol_seen } state;
4097 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
4101 state = nothing_seen;
4102 cache->filename = NULL;
4104 cache->func_size = 0;
4105 cache->last_section = section;
4107 for (p = symbols; *p != NULL; p++)
4113 if ((sym->flags & BSF_FILE) != 0)
4116 if (state == symbol_seen)
4117 state = file_after_symbol_seen;
4121 size = bed->maybe_function_sym (sym, section, &code_off);
4123 && code_off <= offset
4124 && (code_off > low_func
4125 || (code_off == low_func
4126 && size > cache->func_size)))
4129 cache->func_size = size;
4130 cache->filename = NULL;
4131 low_func = code_off;
4133 && ((sym->flags & BSF_LOCAL) != 0
4134 || state != file_after_symbol_seen))
4135 cache->filename = bfd_asymbol_name (file);
4137 if (state == nothing_seen)
4138 state = symbol_seen;
4142 if (cache->func == NULL)
4146 *filename_ptr = cache->filename;
4147 if (functionname_ptr)
4148 *functionname_ptr = bfd_asymbol_name (cache->func);