2 Copyright (C) 1994-2016 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);
559 /* Read dwarf information from a buffer. */
562 read_1_byte (bfd *abfd ATTRIBUTE_UNUSED, bfd_byte *buf, bfd_byte *end)
566 return bfd_get_8 (abfd, buf);
570 read_1_signed_byte (bfd *abfd ATTRIBUTE_UNUSED, bfd_byte *buf, bfd_byte *end)
574 return bfd_get_signed_8 (abfd, buf);
578 read_2_bytes (bfd *abfd, bfd_byte *buf, bfd_byte *end)
582 return bfd_get_16 (abfd, buf);
586 read_4_bytes (bfd *abfd, bfd_byte *buf, bfd_byte *end)
590 return bfd_get_32 (abfd, buf);
594 read_8_bytes (bfd *abfd, bfd_byte *buf, bfd_byte *end)
598 return bfd_get_64 (abfd, buf);
602 read_n_bytes (bfd *abfd ATTRIBUTE_UNUSED,
605 unsigned int size ATTRIBUTE_UNUSED)
607 if (buf + size > end)
612 /* Scans a NUL terminated string starting at BUF, returning a pointer to it.
613 Returns the number of characters in the string, *including* the NUL byte,
614 in BYTES_READ_PTR. This value is set even if the function fails. Bytes
615 at or beyond BUF_END will not be read. Returns NULL if there was a
616 problem, or if the string is empty. */
619 read_string (bfd * abfd ATTRIBUTE_UNUSED,
622 unsigned int * bytes_read_ptr)
628 * bytes_read_ptr = 0;
634 * bytes_read_ptr = 1;
638 while (buf < buf_end)
641 * bytes_read_ptr = buf - str;
645 * bytes_read_ptr = buf - str;
649 /* Reads an offset from BUF and then locates the string at this offset
650 inside the debug string section. Returns a pointer to the string.
651 Returns the number of bytes read from BUF, *not* the length of the string,
652 in BYTES_READ_PTR. This value is set even if the function fails. Bytes
653 at or beyond BUF_END will not be read from BUF. Returns NULL if there was
654 a problem, or if the string is empty. Does not check for NUL termination
658 read_indirect_string (struct comp_unit * unit,
661 unsigned int * bytes_read_ptr)
664 struct dwarf2_debug *stash = unit->stash;
667 if (buf + unit->offset_size > buf_end)
669 * bytes_read_ptr = 0;
673 if (unit->offset_size == 4)
674 offset = read_4_bytes (unit->abfd, buf, buf_end);
676 offset = read_8_bytes (unit->abfd, buf, buf_end);
678 *bytes_read_ptr = unit->offset_size;
680 if (! read_section (unit->abfd, &stash->debug_sections[debug_str],
682 &stash->dwarf_str_buffer, &stash->dwarf_str_size))
685 if (offset >= stash->dwarf_str_size)
687 str = (char *) stash->dwarf_str_buffer + offset;
693 /* Like read_indirect_string but uses a .debug_str located in
694 an alternate file pointed to by the .gnu_debugaltlink section.
695 Used to impement DW_FORM_GNU_strp_alt. */
698 read_alt_indirect_string (struct comp_unit * unit,
701 unsigned int * bytes_read_ptr)
704 struct dwarf2_debug *stash = unit->stash;
707 if (buf + unit->offset_size > buf_end)
709 * bytes_read_ptr = 0;
713 if (unit->offset_size == 4)
714 offset = read_4_bytes (unit->abfd, buf, buf_end);
716 offset = read_8_bytes (unit->abfd, buf, buf_end);
718 *bytes_read_ptr = unit->offset_size;
720 if (stash->alt_bfd_ptr == NULL)
723 char * debug_filename = bfd_follow_gnu_debugaltlink (unit->abfd, DEBUGDIR);
725 if (debug_filename == NULL)
728 if ((debug_bfd = bfd_openr (debug_filename, NULL)) == NULL
729 || ! bfd_check_format (debug_bfd, bfd_object))
732 bfd_close (debug_bfd);
734 /* FIXME: Should we report our failure to follow the debuglink ? */
735 free (debug_filename);
738 stash->alt_bfd_ptr = debug_bfd;
741 if (! read_section (unit->stash->alt_bfd_ptr,
742 stash->debug_sections + debug_str_alt,
743 NULL, /* FIXME: Do we need to load alternate symbols ? */
745 &stash->alt_dwarf_str_buffer,
746 &stash->alt_dwarf_str_size))
749 if (offset >= stash->alt_dwarf_str_size)
751 str = (char *) stash->alt_dwarf_str_buffer + offset;
758 /* Resolve an alternate reference from UNIT at OFFSET.
759 Returns a pointer into the loaded alternate CU upon success
760 or NULL upon failure. */
763 read_alt_indirect_ref (struct comp_unit * unit,
766 struct dwarf2_debug *stash = unit->stash;
768 if (stash->alt_bfd_ptr == NULL)
771 char * debug_filename = bfd_follow_gnu_debugaltlink (unit->abfd, DEBUGDIR);
773 if (debug_filename == NULL)
776 if ((debug_bfd = bfd_openr (debug_filename, NULL)) == NULL
777 || ! bfd_check_format (debug_bfd, bfd_object))
780 bfd_close (debug_bfd);
782 /* FIXME: Should we report our failure to follow the debuglink ? */
783 free (debug_filename);
786 stash->alt_bfd_ptr = debug_bfd;
789 if (! read_section (unit->stash->alt_bfd_ptr,
790 stash->debug_sections + debug_info_alt,
791 NULL, /* FIXME: Do we need to load alternate symbols ? */
793 &stash->alt_dwarf_info_buffer,
794 &stash->alt_dwarf_info_size))
797 if (offset >= stash->alt_dwarf_info_size)
799 return stash->alt_dwarf_info_buffer + offset;
803 read_address (struct comp_unit *unit, bfd_byte *buf, bfd_byte * buf_end)
807 if (bfd_get_flavour (unit->abfd) == bfd_target_elf_flavour)
808 signed_vma = get_elf_backend_data (unit->abfd)->sign_extend_vma;
810 if (buf + unit->addr_size > buf_end)
815 switch (unit->addr_size)
818 return bfd_get_signed_64 (unit->abfd, buf);
820 return bfd_get_signed_32 (unit->abfd, buf);
822 return bfd_get_signed_16 (unit->abfd, buf);
829 switch (unit->addr_size)
832 return bfd_get_64 (unit->abfd, buf);
834 return bfd_get_32 (unit->abfd, buf);
836 return bfd_get_16 (unit->abfd, buf);
843 /* Lookup an abbrev_info structure in the abbrev hash table. */
845 static struct abbrev_info *
846 lookup_abbrev (unsigned int number, struct abbrev_info **abbrevs)
848 unsigned int hash_number;
849 struct abbrev_info *abbrev;
851 hash_number = number % ABBREV_HASH_SIZE;
852 abbrev = abbrevs[hash_number];
856 if (abbrev->number == number)
859 abbrev = abbrev->next;
865 /* In DWARF version 2, the description of the debugging information is
866 stored in a separate .debug_abbrev section. Before we read any
867 dies from a section we read in all abbreviations and install them
870 static struct abbrev_info**
871 read_abbrevs (bfd *abfd, bfd_uint64_t offset, struct dwarf2_debug *stash)
873 struct abbrev_info **abbrevs;
874 bfd_byte *abbrev_ptr;
875 bfd_byte *abbrev_end;
876 struct abbrev_info *cur_abbrev;
877 unsigned int abbrev_number, bytes_read, abbrev_name;
878 unsigned int abbrev_form, hash_number;
881 if (! read_section (abfd, &stash->debug_sections[debug_abbrev],
883 &stash->dwarf_abbrev_buffer, &stash->dwarf_abbrev_size))
886 if (offset >= stash->dwarf_abbrev_size)
889 amt = sizeof (struct abbrev_info*) * ABBREV_HASH_SIZE;
890 abbrevs = (struct abbrev_info **) bfd_zalloc (abfd, amt);
894 abbrev_ptr = stash->dwarf_abbrev_buffer + offset;
895 abbrev_end = stash->dwarf_abbrev_buffer + stash->dwarf_abbrev_size;
896 abbrev_number = safe_read_leb128 (abfd, abbrev_ptr, &bytes_read, FALSE, abbrev_end);
897 abbrev_ptr += bytes_read;
899 /* Loop until we reach an abbrev number of 0. */
900 while (abbrev_number)
902 amt = sizeof (struct abbrev_info);
903 cur_abbrev = (struct abbrev_info *) bfd_zalloc (abfd, amt);
904 if (cur_abbrev == NULL)
907 /* Read in abbrev header. */
908 cur_abbrev->number = abbrev_number;
909 cur_abbrev->tag = (enum dwarf_tag)
910 safe_read_leb128 (abfd, abbrev_ptr, &bytes_read, FALSE, abbrev_end);
911 abbrev_ptr += bytes_read;
912 cur_abbrev->has_children = read_1_byte (abfd, abbrev_ptr, abbrev_end);
915 /* Now read in declarations. */
916 abbrev_name = safe_read_leb128 (abfd, abbrev_ptr, &bytes_read, FALSE, abbrev_end);
917 abbrev_ptr += bytes_read;
918 abbrev_form = safe_read_leb128 (abfd, abbrev_ptr, &bytes_read, FALSE, abbrev_end);
919 abbrev_ptr += bytes_read;
923 if ((cur_abbrev->num_attrs % ATTR_ALLOC_CHUNK) == 0)
925 struct attr_abbrev *tmp;
927 amt = cur_abbrev->num_attrs + ATTR_ALLOC_CHUNK;
928 amt *= sizeof (struct attr_abbrev);
929 tmp = (struct attr_abbrev *) bfd_realloc (cur_abbrev->attrs, amt);
934 for (i = 0; i < ABBREV_HASH_SIZE; i++)
936 struct abbrev_info *abbrev = abbrevs[i];
940 free (abbrev->attrs);
941 abbrev = abbrev->next;
946 cur_abbrev->attrs = tmp;
949 cur_abbrev->attrs[cur_abbrev->num_attrs].name
950 = (enum dwarf_attribute) abbrev_name;
951 cur_abbrev->attrs[cur_abbrev->num_attrs++].form
952 = (enum dwarf_form) abbrev_form;
953 abbrev_name = safe_read_leb128 (abfd, abbrev_ptr, &bytes_read, FALSE, abbrev_end);
954 abbrev_ptr += bytes_read;
955 abbrev_form = safe_read_leb128 (abfd, abbrev_ptr, &bytes_read, FALSE, abbrev_end);
956 abbrev_ptr += bytes_read;
959 hash_number = abbrev_number % ABBREV_HASH_SIZE;
960 cur_abbrev->next = abbrevs[hash_number];
961 abbrevs[hash_number] = cur_abbrev;
963 /* Get next abbreviation.
964 Under Irix6 the abbreviations for a compilation unit are not
965 always properly terminated with an abbrev number of 0.
966 Exit loop if we encounter an abbreviation which we have
967 already read (which means we are about to read the abbreviations
968 for the next compile unit) or if the end of the abbreviation
970 if ((unsigned int) (abbrev_ptr - stash->dwarf_abbrev_buffer)
971 >= stash->dwarf_abbrev_size)
973 abbrev_number = safe_read_leb128 (abfd, abbrev_ptr, &bytes_read, FALSE, abbrev_end);
974 abbrev_ptr += bytes_read;
975 if (lookup_abbrev (abbrev_number, abbrevs) != NULL)
982 /* Returns true if the form is one which has a string value. */
984 static inline bfd_boolean
985 is_str_attr (enum dwarf_form form)
987 return form == DW_FORM_string || form == DW_FORM_strp || form == DW_FORM_GNU_strp_alt;
990 /* Read and fill in the value of attribute ATTR as described by FORM.
991 Read data starting from INFO_PTR, but never at or beyond INFO_PTR_END.
992 Returns an updated INFO_PTR taking into account the amount of data read. */
995 read_attribute_value (struct attribute * attr,
997 struct comp_unit * unit,
999 bfd_byte * info_ptr_end)
1001 bfd *abfd = unit->abfd;
1002 unsigned int bytes_read;
1003 struct dwarf_block *blk;
1006 if (info_ptr >= info_ptr_end && form != DW_FORM_flag_present)
1008 (*_bfd_error_handler) (_("Dwarf Error: Info pointer extends beyond end of attributes"));
1009 bfd_set_error (bfd_error_bad_value);
1013 attr->form = (enum dwarf_form) form;
1017 case DW_FORM_ref_addr:
1018 /* DW_FORM_ref_addr is an address in DWARF2, and an offset in
1020 if (unit->version == 3 || unit->version == 4)
1022 if (unit->offset_size == 4)
1023 attr->u.val = read_4_bytes (unit->abfd, info_ptr, info_ptr_end);
1025 attr->u.val = read_8_bytes (unit->abfd, info_ptr, info_ptr_end);
1026 info_ptr += unit->offset_size;
1031 attr->u.val = read_address (unit, info_ptr, info_ptr_end);
1032 info_ptr += unit->addr_size;
1034 case DW_FORM_GNU_ref_alt:
1035 case DW_FORM_sec_offset:
1036 if (unit->offset_size == 4)
1037 attr->u.val = read_4_bytes (unit->abfd, info_ptr, info_ptr_end);
1039 attr->u.val = read_8_bytes (unit->abfd, info_ptr, info_ptr_end);
1040 info_ptr += unit->offset_size;
1042 case DW_FORM_block2:
1043 amt = sizeof (struct dwarf_block);
1044 blk = (struct dwarf_block *) bfd_alloc (abfd, amt);
1047 blk->size = read_2_bytes (abfd, info_ptr, info_ptr_end);
1049 blk->data = read_n_bytes (abfd, info_ptr, info_ptr_end, blk->size);
1050 info_ptr += blk->size;
1053 case DW_FORM_block4:
1054 amt = sizeof (struct dwarf_block);
1055 blk = (struct dwarf_block *) bfd_alloc (abfd, amt);
1058 blk->size = read_4_bytes (abfd, info_ptr, info_ptr_end);
1060 blk->data = read_n_bytes (abfd, info_ptr, info_ptr_end, blk->size);
1061 info_ptr += blk->size;
1065 attr->u.val = read_2_bytes (abfd, info_ptr, info_ptr_end);
1069 attr->u.val = read_4_bytes (abfd, info_ptr, info_ptr_end);
1073 attr->u.val = read_8_bytes (abfd, info_ptr, info_ptr_end);
1076 case DW_FORM_string:
1077 attr->u.str = read_string (abfd, info_ptr, info_ptr_end, &bytes_read);
1078 info_ptr += bytes_read;
1081 attr->u.str = read_indirect_string (unit, info_ptr, info_ptr_end, &bytes_read);
1082 info_ptr += bytes_read;
1084 case DW_FORM_GNU_strp_alt:
1085 attr->u.str = read_alt_indirect_string (unit, info_ptr, info_ptr_end, &bytes_read);
1086 info_ptr += bytes_read;
1088 case DW_FORM_exprloc:
1090 amt = sizeof (struct dwarf_block);
1091 blk = (struct dwarf_block *) bfd_alloc (abfd, amt);
1094 blk->size = safe_read_leb128 (abfd, info_ptr, &bytes_read, FALSE, info_ptr_end);
1095 info_ptr += bytes_read;
1096 blk->data = read_n_bytes (abfd, info_ptr, info_ptr_end, blk->size);
1097 info_ptr += blk->size;
1100 case DW_FORM_block1:
1101 amt = sizeof (struct dwarf_block);
1102 blk = (struct dwarf_block *) bfd_alloc (abfd, amt);
1105 blk->size = read_1_byte (abfd, info_ptr, info_ptr_end);
1107 blk->data = read_n_bytes (abfd, info_ptr, info_ptr_end, blk->size);
1108 info_ptr += blk->size;
1112 attr->u.val = read_1_byte (abfd, info_ptr, info_ptr_end);
1116 attr->u.val = read_1_byte (abfd, info_ptr, info_ptr_end);
1119 case DW_FORM_flag_present:
1123 attr->u.sval = safe_read_leb128 (abfd, info_ptr, &bytes_read, TRUE, info_ptr_end);
1124 info_ptr += bytes_read;
1127 attr->u.val = safe_read_leb128 (abfd, info_ptr, &bytes_read, FALSE, info_ptr_end);
1128 info_ptr += bytes_read;
1131 attr->u.val = read_1_byte (abfd, info_ptr, info_ptr_end);
1135 attr->u.val = read_2_bytes (abfd, info_ptr, info_ptr_end);
1139 attr->u.val = read_4_bytes (abfd, info_ptr, info_ptr_end);
1143 attr->u.val = read_8_bytes (abfd, info_ptr, info_ptr_end);
1146 case DW_FORM_ref_sig8:
1147 attr->u.val = read_8_bytes (abfd, info_ptr, info_ptr_end);
1150 case DW_FORM_ref_udata:
1151 attr->u.val = safe_read_leb128 (abfd, info_ptr, &bytes_read, FALSE, info_ptr_end);
1152 info_ptr += bytes_read;
1154 case DW_FORM_indirect:
1155 form = safe_read_leb128 (abfd, info_ptr, &bytes_read, FALSE, info_ptr_end);
1156 info_ptr += bytes_read;
1157 info_ptr = read_attribute_value (attr, form, unit, info_ptr, info_ptr_end);
1160 (*_bfd_error_handler) (_("Dwarf Error: Invalid or unhandled FORM value: %#x."),
1162 bfd_set_error (bfd_error_bad_value);
1168 /* Read an attribute described by an abbreviated attribute. */
1171 read_attribute (struct attribute * attr,
1172 struct attr_abbrev * abbrev,
1173 struct comp_unit * unit,
1174 bfd_byte * info_ptr,
1175 bfd_byte * info_ptr_end)
1177 attr->name = abbrev->name;
1178 info_ptr = read_attribute_value (attr, abbrev->form, unit, info_ptr, info_ptr_end);
1182 /* Return whether DW_AT_name will return the same as DW_AT_linkage_name
1186 non_mangled (int lang)
1196 case DW_LANG_Cobol74:
1197 case DW_LANG_Cobol85:
1198 case DW_LANG_Fortran77:
1199 case DW_LANG_Pascal83:
1209 /* Source line information table routines. */
1211 #define FILE_ALLOC_CHUNK 5
1212 #define DIR_ALLOC_CHUNK 5
1216 struct line_info* prev_line;
1220 unsigned int column;
1221 unsigned int discriminator;
1222 unsigned char op_index;
1223 unsigned char end_sequence; /* End of (sequential) code sequence. */
1234 struct line_sequence
1237 struct line_sequence* prev_sequence;
1238 struct line_info* last_line; /* Largest VMA. */
1241 struct line_info_table
1244 unsigned int num_files;
1245 unsigned int num_dirs;
1246 unsigned int num_sequences;
1249 struct fileinfo* files;
1250 struct line_sequence* sequences;
1251 struct line_info* lcl_head; /* Local head; used in 'add_line_info'. */
1254 /* Remember some information about each function. If the function is
1255 inlined (DW_TAG_inlined_subroutine) it may have two additional
1256 attributes, DW_AT_call_file and DW_AT_call_line, which specify the
1257 source code location where this function was inlined. */
1261 /* Pointer to previous function in list of all functions. */
1262 struct funcinfo *prev_func;
1263 /* Pointer to function one scope higher. */
1264 struct funcinfo *caller_func;
1265 /* Source location file name where caller_func inlines this func. */
1267 /* Source location file name. */
1269 /* Source location line number where caller_func inlines this func. */
1271 /* Source location line number. */
1274 bfd_boolean is_linkage;
1276 struct arange arange;
1277 /* Where the symbol is defined. */
1283 /* Pointer to previous variable in list of all variables */
1284 struct varinfo *prev_var;
1285 /* Source location file name */
1287 /* Source location line number */
1292 /* Where the symbol is defined */
1294 /* Is this a stack variable? */
1295 unsigned int stack: 1;
1298 /* Return TRUE if NEW_LINE should sort after LINE. */
1300 static inline bfd_boolean
1301 new_line_sorts_after (struct line_info *new_line, struct line_info *line)
1303 return (new_line->address > line->address
1304 || (new_line->address == line->address
1305 && (new_line->op_index > line->op_index
1306 || (new_line->op_index == line->op_index
1307 && new_line->end_sequence < line->end_sequence))));
1311 /* Adds a new entry to the line_info list in the line_info_table, ensuring
1312 that the list is sorted. Note that the line_info list is sorted from
1313 highest to lowest VMA (with possible duplicates); that is,
1314 line_info->prev_line always accesses an equal or smaller VMA. */
1317 add_line_info (struct line_info_table *table,
1319 unsigned char op_index,
1322 unsigned int column,
1323 unsigned int discriminator,
1326 bfd_size_type amt = sizeof (struct line_info);
1327 struct line_sequence* seq = table->sequences;
1328 struct line_info* info = (struct line_info *) bfd_alloc (table->abfd, amt);
1333 /* Set member data of 'info'. */
1334 info->prev_line = NULL;
1335 info->address = address;
1336 info->op_index = op_index;
1338 info->column = column;
1339 info->discriminator = discriminator;
1340 info->end_sequence = end_sequence;
1342 if (filename && filename[0])
1344 info->filename = (char *) bfd_alloc (table->abfd, strlen (filename) + 1);
1345 if (info->filename == NULL)
1347 strcpy (info->filename, filename);
1350 info->filename = NULL;
1352 /* Find the correct location for 'info'. Normally we will receive
1353 new line_info data 1) in order and 2) with increasing VMAs.
1354 However some compilers break the rules (cf. decode_line_info) and
1355 so we include some heuristics for quickly finding the correct
1356 location for 'info'. In particular, these heuristics optimize for
1357 the common case in which the VMA sequence that we receive is a
1358 list of locally sorted VMAs such as
1359 p...z a...j (where a < j < p < z)
1361 Note: table->lcl_head is used to head an *actual* or *possible*
1362 sub-sequence within the list (such as a...j) that is not directly
1363 headed by table->last_line
1365 Note: we may receive duplicate entries from 'decode_line_info'. */
1368 && seq->last_line->address == address
1369 && seq->last_line->op_index == op_index
1370 && seq->last_line->end_sequence == end_sequence)
1372 /* We only keep the last entry with the same address and end
1373 sequence. See PR ld/4986. */
1374 if (table->lcl_head == seq->last_line)
1375 table->lcl_head = info;
1376 info->prev_line = seq->last_line->prev_line;
1377 seq->last_line = info;
1379 else if (!seq || seq->last_line->end_sequence)
1381 /* Start a new line sequence. */
1382 amt = sizeof (struct line_sequence);
1383 seq = (struct line_sequence *) bfd_malloc (amt);
1386 seq->low_pc = address;
1387 seq->prev_sequence = table->sequences;
1388 seq->last_line = info;
1389 table->lcl_head = info;
1390 table->sequences = seq;
1391 table->num_sequences++;
1393 else if (new_line_sorts_after (info, seq->last_line))
1395 /* Normal case: add 'info' to the beginning of the current sequence. */
1396 info->prev_line = seq->last_line;
1397 seq->last_line = info;
1399 /* lcl_head: initialize to head a *possible* sequence at the end. */
1400 if (!table->lcl_head)
1401 table->lcl_head = info;
1403 else if (!new_line_sorts_after (info, table->lcl_head)
1404 && (!table->lcl_head->prev_line
1405 || new_line_sorts_after (info, table->lcl_head->prev_line)))
1407 /* Abnormal but easy: lcl_head is the head of 'info'. */
1408 info->prev_line = table->lcl_head->prev_line;
1409 table->lcl_head->prev_line = info;
1413 /* Abnormal and hard: Neither 'last_line' nor 'lcl_head'
1414 are valid heads for 'info'. Reset 'lcl_head'. */
1415 struct line_info* li2 = seq->last_line; /* Always non-NULL. */
1416 struct line_info* li1 = li2->prev_line;
1420 if (!new_line_sorts_after (info, li2)
1421 && new_line_sorts_after (info, li1))
1424 li2 = li1; /* always non-NULL */
1425 li1 = li1->prev_line;
1427 table->lcl_head = li2;
1428 info->prev_line = table->lcl_head->prev_line;
1429 table->lcl_head->prev_line = info;
1430 if (address < seq->low_pc)
1431 seq->low_pc = address;
1436 /* Extract a fully qualified filename from a line info table.
1437 The returned string has been malloc'ed and it is the caller's
1438 responsibility to free it. */
1441 concat_filename (struct line_info_table *table, unsigned int file)
1445 if (file - 1 >= table->num_files)
1447 /* FILE == 0 means unknown. */
1449 (*_bfd_error_handler)
1450 (_("Dwarf Error: mangled line number section (bad file number)."));
1451 return strdup ("<unknown>");
1454 filename = table->files[file - 1].name;
1456 if (!IS_ABSOLUTE_PATH (filename))
1458 char *dir_name = NULL;
1459 char *subdir_name = NULL;
1463 if (table->files[file - 1].dir
1464 /* PR 17512: file: 0317e960. */
1465 && table->files[file - 1].dir <= table->num_dirs
1466 /* PR 17512: file: 7f3d2e4b. */
1467 && table->dirs != NULL)
1468 subdir_name = table->dirs[table->files[file - 1].dir - 1];
1470 if (!subdir_name || !IS_ABSOLUTE_PATH (subdir_name))
1471 dir_name = table->comp_dir;
1475 dir_name = subdir_name;
1480 return strdup (filename);
1482 len = strlen (dir_name) + strlen (filename) + 2;
1486 len += strlen (subdir_name) + 1;
1487 name = (char *) bfd_malloc (len);
1489 sprintf (name, "%s/%s/%s", dir_name, subdir_name, filename);
1493 name = (char *) bfd_malloc (len);
1495 sprintf (name, "%s/%s", dir_name, filename);
1501 return strdup (filename);
1505 arange_add (const struct comp_unit *unit, struct arange *first_arange,
1506 bfd_vma low_pc, bfd_vma high_pc)
1508 struct arange *arange;
1510 /* Ignore empty ranges. */
1511 if (low_pc == high_pc)
1514 /* If the first arange is empty, use it. */
1515 if (first_arange->high == 0)
1517 first_arange->low = low_pc;
1518 first_arange->high = high_pc;
1522 /* Next see if we can cheaply extend an existing range. */
1523 arange = first_arange;
1526 if (low_pc == arange->high)
1528 arange->high = high_pc;
1531 if (high_pc == arange->low)
1533 arange->low = low_pc;
1536 arange = arange->next;
1540 /* Need to allocate a new arange and insert it into the arange list.
1541 Order isn't significant, so just insert after the first arange. */
1542 arange = (struct arange *) bfd_alloc (unit->abfd, sizeof (*arange));
1545 arange->low = low_pc;
1546 arange->high = high_pc;
1547 arange->next = first_arange->next;
1548 first_arange->next = arange;
1552 /* Compare function for line sequences. */
1555 compare_sequences (const void* a, const void* b)
1557 const struct line_sequence* seq1 = a;
1558 const struct line_sequence* seq2 = b;
1560 /* Sort by low_pc as the primary key. */
1561 if (seq1->low_pc < seq2->low_pc)
1563 if (seq1->low_pc > seq2->low_pc)
1566 /* If low_pc values are equal, sort in reverse order of
1567 high_pc, so that the largest region comes first. */
1568 if (seq1->last_line->address < seq2->last_line->address)
1570 if (seq1->last_line->address > seq2->last_line->address)
1573 if (seq1->last_line->op_index < seq2->last_line->op_index)
1575 if (seq1->last_line->op_index > seq2->last_line->op_index)
1581 /* Sort the line sequences for quick lookup. */
1584 sort_line_sequences (struct line_info_table* table)
1587 struct line_sequence* sequences;
1588 struct line_sequence* seq;
1590 unsigned int num_sequences = table->num_sequences;
1591 bfd_vma last_high_pc;
1593 if (num_sequences == 0)
1596 /* Allocate space for an array of sequences. */
1597 amt = sizeof (struct line_sequence) * num_sequences;
1598 sequences = (struct line_sequence *) bfd_alloc (table->abfd, amt);
1599 if (sequences == NULL)
1602 /* Copy the linked list into the array, freeing the original nodes. */
1603 seq = table->sequences;
1604 for (n = 0; n < num_sequences; n++)
1606 struct line_sequence* last_seq = seq;
1609 sequences[n].low_pc = seq->low_pc;
1610 sequences[n].prev_sequence = NULL;
1611 sequences[n].last_line = seq->last_line;
1612 seq = seq->prev_sequence;
1615 BFD_ASSERT (seq == NULL);
1617 qsort (sequences, n, sizeof (struct line_sequence), compare_sequences);
1619 /* Make the list binary-searchable by trimming overlapping entries
1620 and removing nested entries. */
1622 last_high_pc = sequences[0].last_line->address;
1623 for (n = 1; n < table->num_sequences; n++)
1625 if (sequences[n].low_pc < last_high_pc)
1627 if (sequences[n].last_line->address <= last_high_pc)
1628 /* Skip nested entries. */
1631 /* Trim overlapping entries. */
1632 sequences[n].low_pc = last_high_pc;
1634 last_high_pc = sequences[n].last_line->address;
1635 if (n > num_sequences)
1637 /* Close up the gap. */
1638 sequences[num_sequences].low_pc = sequences[n].low_pc;
1639 sequences[num_sequences].last_line = sequences[n].last_line;
1644 table->sequences = sequences;
1645 table->num_sequences = num_sequences;
1649 /* Decode the line number information for UNIT. */
1651 static struct line_info_table*
1652 decode_line_info (struct comp_unit *unit, struct dwarf2_debug *stash)
1654 bfd *abfd = unit->abfd;
1655 struct line_info_table* table;
1658 struct line_head lh;
1659 unsigned int i, bytes_read, offset_size;
1660 char *cur_file, *cur_dir;
1661 unsigned char op_code, extended_op, adj_opcode;
1662 unsigned int exop_len;
1665 if (! read_section (abfd, &stash->debug_sections[debug_line],
1666 stash->syms, unit->line_offset,
1667 &stash->dwarf_line_buffer, &stash->dwarf_line_size))
1670 amt = sizeof (struct line_info_table);
1671 table = (struct line_info_table *) bfd_alloc (abfd, amt);
1675 table->comp_dir = unit->comp_dir;
1677 table->num_files = 0;
1678 table->files = NULL;
1680 table->num_dirs = 0;
1683 table->num_sequences = 0;
1684 table->sequences = NULL;
1686 table->lcl_head = NULL;
1688 if (stash->dwarf_line_size < 16)
1690 (*_bfd_error_handler)
1691 (_("Dwarf Error: Line info section is too small (%ld)"),
1692 (long) stash->dwarf_line_size);
1693 bfd_set_error (bfd_error_bad_value);
1696 line_ptr = stash->dwarf_line_buffer + unit->line_offset;
1697 line_end = stash->dwarf_line_buffer + stash->dwarf_line_size;
1699 /* Read in the prologue. */
1700 lh.total_length = read_4_bytes (abfd, line_ptr, line_end);
1703 if (lh.total_length == 0xffffffff)
1705 lh.total_length = read_8_bytes (abfd, line_ptr, line_end);
1709 else if (lh.total_length == 0 && unit->addr_size == 8)
1711 /* Handle (non-standard) 64-bit DWARF2 formats. */
1712 lh.total_length = read_4_bytes (abfd, line_ptr, line_end);
1717 if (lh.total_length > stash->dwarf_line_size)
1719 (*_bfd_error_handler)
1720 (_("Dwarf Error: Line info data is bigger (0x%lx) than the section (0x%lx)"),
1721 (long) lh.total_length, (long) stash->dwarf_line_size);
1722 bfd_set_error (bfd_error_bad_value);
1726 line_end = line_ptr + lh.total_length;
1728 lh.version = read_2_bytes (abfd, line_ptr, line_end);
1729 if (lh.version < 2 || lh.version > 4)
1731 (*_bfd_error_handler)
1732 (_("Dwarf Error: Unhandled .debug_line version %d."), lh.version);
1733 bfd_set_error (bfd_error_bad_value);
1738 if (line_ptr + offset_size + (lh.version >=4 ? 6 : 5) >= line_end)
1740 (*_bfd_error_handler)
1741 (_("Dwarf Error: Ran out of room reading prologue"));
1742 bfd_set_error (bfd_error_bad_value);
1746 if (offset_size == 4)
1747 lh.prologue_length = read_4_bytes (abfd, line_ptr, line_end);
1749 lh.prologue_length = read_8_bytes (abfd, line_ptr, line_end);
1750 line_ptr += offset_size;
1752 lh.minimum_instruction_length = read_1_byte (abfd, line_ptr, line_end);
1755 if (lh.version >= 4)
1757 lh.maximum_ops_per_insn = read_1_byte (abfd, line_ptr, line_end);
1761 lh.maximum_ops_per_insn = 1;
1763 if (lh.maximum_ops_per_insn == 0)
1765 (*_bfd_error_handler)
1766 (_("Dwarf Error: Invalid maximum operations per instruction."));
1767 bfd_set_error (bfd_error_bad_value);
1771 lh.default_is_stmt = read_1_byte (abfd, line_ptr, line_end);
1774 lh.line_base = read_1_signed_byte (abfd, line_ptr, line_end);
1777 lh.line_range = read_1_byte (abfd, line_ptr, line_end);
1780 lh.opcode_base = read_1_byte (abfd, line_ptr, line_end);
1783 if (line_ptr + (lh.opcode_base - 1) >= line_end)
1785 (*_bfd_error_handler) (_("Dwarf Error: Ran out of room reading opcodes"));
1786 bfd_set_error (bfd_error_bad_value);
1790 amt = lh.opcode_base * sizeof (unsigned char);
1791 lh.standard_opcode_lengths = (unsigned char *) bfd_alloc (abfd, amt);
1793 lh.standard_opcode_lengths[0] = 1;
1795 for (i = 1; i < lh.opcode_base; ++i)
1797 lh.standard_opcode_lengths[i] = read_1_byte (abfd, line_ptr, line_end);
1801 /* Read directory table. */
1802 while ((cur_dir = read_string (abfd, line_ptr, line_end, &bytes_read)) != NULL)
1804 line_ptr += bytes_read;
1806 if ((table->num_dirs % DIR_ALLOC_CHUNK) == 0)
1810 amt = table->num_dirs + DIR_ALLOC_CHUNK;
1811 amt *= sizeof (char *);
1813 tmp = (char **) bfd_realloc (table->dirs, amt);
1819 table->dirs[table->num_dirs++] = cur_dir;
1822 line_ptr += bytes_read;
1824 /* Read file name table. */
1825 while ((cur_file = read_string (abfd, line_ptr, line_end, &bytes_read)) != NULL)
1827 line_ptr += bytes_read;
1829 if ((table->num_files % FILE_ALLOC_CHUNK) == 0)
1831 struct fileinfo *tmp;
1833 amt = table->num_files + FILE_ALLOC_CHUNK;
1834 amt *= sizeof (struct fileinfo);
1836 tmp = (struct fileinfo *) bfd_realloc (table->files, amt);
1842 table->files[table->num_files].name = cur_file;
1843 table->files[table->num_files].dir =
1844 safe_read_leb128 (abfd, line_ptr, &bytes_read, FALSE, line_end);
1845 line_ptr += bytes_read;
1846 table->files[table->num_files].time = safe_read_leb128 (abfd, line_ptr, &bytes_read, FALSE, line_end);
1847 line_ptr += bytes_read;
1848 table->files[table->num_files].size = safe_read_leb128 (abfd, line_ptr, &bytes_read, FALSE, line_end);
1849 line_ptr += bytes_read;
1853 line_ptr += bytes_read;
1855 /* Read the statement sequences until there's nothing left. */
1856 while (line_ptr < line_end)
1858 /* State machine registers. */
1859 bfd_vma address = 0;
1860 unsigned char op_index = 0;
1861 char * filename = table->num_files ? concat_filename (table, 1) : NULL;
1862 unsigned int line = 1;
1863 unsigned int column = 0;
1864 unsigned int discriminator = 0;
1865 int is_stmt = lh.default_is_stmt;
1866 int end_sequence = 0;
1867 /* eraxxon@alumni.rice.edu: Against the DWARF2 specs, some
1868 compilers generate address sequences that are wildly out of
1869 order using DW_LNE_set_address (e.g. Intel C++ 6.0 compiler
1870 for ia64-Linux). Thus, to determine the low and high
1871 address, we must compare on every DW_LNS_copy, etc. */
1872 bfd_vma low_pc = (bfd_vma) -1;
1873 bfd_vma high_pc = 0;
1875 /* Decode the table. */
1876 while (! end_sequence)
1878 op_code = read_1_byte (abfd, line_ptr, line_end);
1881 if (op_code >= lh.opcode_base)
1883 /* Special operand. */
1884 adj_opcode = op_code - lh.opcode_base;
1885 if (lh.line_range == 0)
1887 if (lh.maximum_ops_per_insn == 1)
1888 address += (adj_opcode / lh.line_range
1889 * lh.minimum_instruction_length);
1892 address += ((op_index + adj_opcode / lh.line_range)
1893 / lh.maximum_ops_per_insn
1894 * lh.minimum_instruction_length);
1895 op_index = ((op_index + adj_opcode / lh.line_range)
1896 % lh.maximum_ops_per_insn);
1898 line += lh.line_base + (adj_opcode % lh.line_range);
1899 /* Append row to matrix using current values. */
1900 if (!add_line_info (table, address, op_index, filename,
1901 line, column, discriminator, 0))
1904 if (address < low_pc)
1906 if (address > high_pc)
1909 else switch (op_code)
1911 case DW_LNS_extended_op:
1912 exop_len = safe_read_leb128 (abfd, line_ptr, &bytes_read, FALSE, line_end);
1913 line_ptr += bytes_read;
1914 extended_op = read_1_byte (abfd, line_ptr, line_end);
1917 switch (extended_op)
1919 case DW_LNE_end_sequence:
1921 if (!add_line_info (table, address, op_index, filename, line,
1922 column, discriminator, end_sequence))
1925 if (address < low_pc)
1927 if (address > high_pc)
1929 if (!arange_add (unit, &unit->arange, low_pc, high_pc))
1932 case DW_LNE_set_address:
1933 address = read_address (unit, line_ptr, line_end);
1935 line_ptr += unit->addr_size;
1937 case DW_LNE_define_file:
1938 cur_file = read_string (abfd, line_ptr, line_end, &bytes_read);
1939 line_ptr += bytes_read;
1940 if ((table->num_files % FILE_ALLOC_CHUNK) == 0)
1942 struct fileinfo *tmp;
1944 amt = table->num_files + FILE_ALLOC_CHUNK;
1945 amt *= sizeof (struct fileinfo);
1946 tmp = (struct fileinfo *) bfd_realloc (table->files, amt);
1951 table->files[table->num_files].name = cur_file;
1952 table->files[table->num_files].dir =
1953 safe_read_leb128 (abfd, line_ptr, &bytes_read, FALSE, line_end);
1954 line_ptr += bytes_read;
1955 table->files[table->num_files].time =
1956 safe_read_leb128 (abfd, line_ptr, &bytes_read, FALSE, line_end);
1957 line_ptr += bytes_read;
1958 table->files[table->num_files].size =
1959 safe_read_leb128 (abfd, line_ptr, &bytes_read, FALSE, line_end);
1960 line_ptr += bytes_read;
1963 case DW_LNE_set_discriminator:
1965 safe_read_leb128 (abfd, line_ptr, &bytes_read, FALSE, line_end);
1966 line_ptr += bytes_read;
1968 case DW_LNE_HP_source_file_correlation:
1969 line_ptr += exop_len - 1;
1972 (*_bfd_error_handler)
1973 (_("Dwarf Error: mangled line number section."));
1974 bfd_set_error (bfd_error_bad_value);
1976 if (filename != NULL)
1982 if (!add_line_info (table, address, op_index,
1983 filename, line, column, discriminator, 0))
1986 if (address < low_pc)
1988 if (address > high_pc)
1991 case DW_LNS_advance_pc:
1992 if (lh.maximum_ops_per_insn == 1)
1993 address += (lh.minimum_instruction_length
1994 * safe_read_leb128 (abfd, line_ptr, &bytes_read,
1998 bfd_vma adjust = safe_read_leb128 (abfd, line_ptr, &bytes_read,
2000 address = ((op_index + adjust) / lh.maximum_ops_per_insn
2001 * lh.minimum_instruction_length);
2002 op_index = (op_index + adjust) % lh.maximum_ops_per_insn;
2004 line_ptr += bytes_read;
2006 case DW_LNS_advance_line:
2007 line += safe_read_leb128 (abfd, line_ptr, &bytes_read, TRUE, line_end);
2008 line_ptr += bytes_read;
2010 case DW_LNS_set_file:
2014 /* The file and directory tables are 0
2015 based, the references are 1 based. */
2016 file = safe_read_leb128 (abfd, line_ptr, &bytes_read, FALSE, line_end);
2017 line_ptr += bytes_read;
2020 filename = concat_filename (table, file);
2023 case DW_LNS_set_column:
2024 column = safe_read_leb128 (abfd, line_ptr, &bytes_read, FALSE, line_end);
2025 line_ptr += bytes_read;
2027 case DW_LNS_negate_stmt:
2028 is_stmt = (!is_stmt);
2030 case DW_LNS_set_basic_block:
2032 case DW_LNS_const_add_pc:
2033 if (lh.maximum_ops_per_insn == 1)
2034 address += (lh.minimum_instruction_length
2035 * ((255 - lh.opcode_base) / lh.line_range));
2038 bfd_vma adjust = ((255 - lh.opcode_base) / lh.line_range);
2039 address += (lh.minimum_instruction_length
2040 * ((op_index + adjust)
2041 / lh.maximum_ops_per_insn));
2042 op_index = (op_index + adjust) % lh.maximum_ops_per_insn;
2045 case DW_LNS_fixed_advance_pc:
2046 address += read_2_bytes (abfd, line_ptr, line_end);
2051 /* Unknown standard opcode, ignore it. */
2052 for (i = 0; i < lh.standard_opcode_lengths[op_code]; i++)
2054 (void) safe_read_leb128 (abfd, line_ptr, &bytes_read, FALSE, line_end);
2055 line_ptr += bytes_read;
2065 if (sort_line_sequences (table))
2069 if (table->sequences != NULL)
2070 free (table->sequences);
2071 if (table->files != NULL)
2072 free (table->files);
2073 if (table->dirs != NULL)
2078 /* If ADDR is within TABLE set the output parameters and return the
2079 range of addresses covered by the entry used to fill them out.
2080 Otherwise set * FILENAME_PTR to NULL and return 0.
2081 The parameters FILENAME_PTR, LINENUMBER_PTR and DISCRIMINATOR_PTR
2082 are pointers to the objects to be filled in. */
2085 lookup_address_in_line_info_table (struct line_info_table *table,
2087 const char **filename_ptr,
2088 unsigned int *linenumber_ptr,
2089 unsigned int *discriminator_ptr)
2091 struct line_sequence *seq = NULL;
2092 struct line_info *each_line;
2095 /* Binary search the array of sequences. */
2097 high = table->num_sequences;
2100 mid = (low + high) / 2;
2101 seq = &table->sequences[mid];
2102 if (addr < seq->low_pc)
2104 else if (addr >= seq->last_line->address)
2110 if (seq && addr >= seq->low_pc && addr < seq->last_line->address)
2112 /* Note: seq->last_line should be a descendingly sorted list. */
2113 for (each_line = seq->last_line;
2115 each_line = each_line->prev_line)
2116 if (addr >= each_line->address)
2120 && !(each_line->end_sequence || each_line == seq->last_line))
2122 *filename_ptr = each_line->filename;
2123 *linenumber_ptr = each_line->line;
2124 if (discriminator_ptr)
2125 *discriminator_ptr = each_line->discriminator;
2126 return seq->last_line->address - seq->low_pc;
2130 *filename_ptr = NULL;
2134 /* Read in the .debug_ranges section for future reference. */
2137 read_debug_ranges (struct comp_unit *unit)
2139 struct dwarf2_debug *stash = unit->stash;
2140 return read_section (unit->abfd, &stash->debug_sections[debug_ranges],
2142 &stash->dwarf_ranges_buffer, &stash->dwarf_ranges_size);
2145 /* Function table functions. */
2147 /* If ADDR is within UNIT's function tables, set FUNCTION_PTR, and return
2148 TRUE. Note that we need to find the function that has the smallest range
2149 that contains ADDR, to handle inlined functions without depending upon
2150 them being ordered in TABLE by increasing range. */
2153 lookup_address_in_function_table (struct comp_unit *unit,
2155 struct funcinfo **function_ptr)
2157 struct funcinfo* each_func;
2158 struct funcinfo* best_fit = NULL;
2159 bfd_vma best_fit_len = 0;
2160 struct arange *arange;
2162 for (each_func = unit->function_table;
2164 each_func = each_func->prev_func)
2166 for (arange = &each_func->arange;
2168 arange = arange->next)
2170 if (addr >= arange->low && addr < arange->high)
2173 || arange->high - arange->low < best_fit_len)
2175 best_fit = each_func;
2176 best_fit_len = arange->high - arange->low;
2184 *function_ptr = best_fit;
2190 /* If SYM at ADDR is within function table of UNIT, set FILENAME_PTR
2191 and LINENUMBER_PTR, and return TRUE. */
2194 lookup_symbol_in_function_table (struct comp_unit *unit,
2197 const char **filename_ptr,
2198 unsigned int *linenumber_ptr)
2200 struct funcinfo* each_func;
2201 struct funcinfo* best_fit = NULL;
2202 bfd_vma best_fit_len = 0;
2203 struct arange *arange;
2204 const char *name = bfd_asymbol_name (sym);
2205 asection *sec = bfd_get_section (sym);
2207 for (each_func = unit->function_table;
2209 each_func = each_func->prev_func)
2211 for (arange = &each_func->arange;
2213 arange = arange->next)
2215 if ((!each_func->sec || each_func->sec == sec)
2216 && addr >= arange->low
2217 && addr < arange->high
2219 && strcmp (name, each_func->name) == 0
2221 || arange->high - arange->low < best_fit_len))
2223 best_fit = each_func;
2224 best_fit_len = arange->high - arange->low;
2231 best_fit->sec = sec;
2232 *filename_ptr = best_fit->file;
2233 *linenumber_ptr = best_fit->line;
2240 /* Variable table functions. */
2242 /* If SYM is within variable table of UNIT, set FILENAME_PTR and
2243 LINENUMBER_PTR, and return TRUE. */
2246 lookup_symbol_in_variable_table (struct comp_unit *unit,
2249 const char **filename_ptr,
2250 unsigned int *linenumber_ptr)
2252 const char *name = bfd_asymbol_name (sym);
2253 asection *sec = bfd_get_section (sym);
2254 struct varinfo* each;
2256 for (each = unit->variable_table; each; each = each->prev_var)
2257 if (each->stack == 0
2258 && each->file != NULL
2259 && each->name != NULL
2260 && each->addr == addr
2261 && (!each->sec || each->sec == sec)
2262 && strcmp (name, each->name) == 0)
2268 *filename_ptr = each->file;
2269 *linenumber_ptr = each->line;
2277 find_abstract_instance_name (struct comp_unit *unit,
2278 struct attribute *attr_ptr,
2279 bfd_boolean *is_linkage)
2281 bfd *abfd = unit->abfd;
2283 bfd_byte *info_ptr_end;
2284 unsigned int abbrev_number, bytes_read, i;
2285 struct abbrev_info *abbrev;
2286 bfd_uint64_t die_ref = attr_ptr->u.val;
2287 struct attribute attr;
2290 /* DW_FORM_ref_addr can reference an entry in a different CU. It
2291 is an offset from the .debug_info section, not the current CU. */
2292 if (attr_ptr->form == DW_FORM_ref_addr)
2294 /* We only support DW_FORM_ref_addr within the same file, so
2295 any relocations should be resolved already. */
2299 info_ptr = unit->sec_info_ptr + die_ref;
2300 info_ptr_end = unit->end_ptr;
2302 /* Now find the CU containing this pointer. */
2303 if (info_ptr >= unit->info_ptr_unit && info_ptr < unit->end_ptr)
2307 /* Check other CUs to see if they contain the abbrev. */
2308 struct comp_unit * u;
2310 for (u = unit->prev_unit; u != NULL; u = u->prev_unit)
2311 if (info_ptr >= u->info_ptr_unit && info_ptr < u->end_ptr)
2315 for (u = unit->next_unit; u != NULL; u = u->next_unit)
2316 if (info_ptr >= u->info_ptr_unit && info_ptr < u->end_ptr)
2321 /* else FIXME: What do we do now ? */
2324 else if (attr_ptr->form == DW_FORM_GNU_ref_alt)
2326 info_ptr = read_alt_indirect_ref (unit, die_ref);
2327 if (info_ptr == NULL)
2329 (*_bfd_error_handler)
2330 (_("Dwarf Error: Unable to read alt ref %u."), die_ref);
2331 bfd_set_error (bfd_error_bad_value);
2334 info_ptr_end = unit->stash->alt_dwarf_info_buffer + unit->stash->alt_dwarf_info_size;
2336 /* FIXME: Do we need to locate the correct CU, in a similar
2337 fashion to the code in the DW_FORM_ref_addr case above ? */
2341 info_ptr = unit->info_ptr_unit + die_ref;
2342 info_ptr_end = unit->end_ptr;
2345 abbrev_number = safe_read_leb128 (abfd, info_ptr, &bytes_read, FALSE, info_ptr_end);
2346 info_ptr += bytes_read;
2350 abbrev = lookup_abbrev (abbrev_number, unit->abbrevs);
2353 (*_bfd_error_handler)
2354 (_("Dwarf Error: Could not find abbrev number %u."), abbrev_number);
2355 bfd_set_error (bfd_error_bad_value);
2359 for (i = 0; i < abbrev->num_attrs; ++i)
2361 info_ptr = read_attribute (&attr, &abbrev->attrs[i], unit,
2362 info_ptr, info_ptr_end);
2363 if (info_ptr == NULL)
2368 /* Prefer DW_AT_MIPS_linkage_name or DW_AT_linkage_name
2370 if (name == NULL && is_str_attr (attr.form))
2373 if (non_mangled (unit->lang))
2377 case DW_AT_specification:
2378 name = find_abstract_instance_name (unit, &attr, is_linkage);
2380 case DW_AT_linkage_name:
2381 case DW_AT_MIPS_linkage_name:
2382 /* PR 16949: Corrupt debug info can place
2383 non-string forms into these attributes. */
2384 if (is_str_attr (attr.form))
2400 read_rangelist (struct comp_unit *unit, struct arange *arange,
2401 bfd_uint64_t offset)
2403 bfd_byte *ranges_ptr;
2404 bfd_byte *ranges_end;
2405 bfd_vma base_address = unit->base_address;
2407 if (! unit->stash->dwarf_ranges_buffer)
2409 if (! read_debug_ranges (unit))
2413 ranges_ptr = unit->stash->dwarf_ranges_buffer + offset;
2414 if (ranges_ptr < unit->stash->dwarf_ranges_buffer)
2416 ranges_end = unit->stash->dwarf_ranges_buffer + unit->stash->dwarf_ranges_size;
2423 /* PR 17512: file: 62cada7d. */
2424 if (ranges_ptr + 2 * unit->addr_size > ranges_end)
2427 low_pc = read_address (unit, ranges_ptr, ranges_end);
2428 ranges_ptr += unit->addr_size;
2429 high_pc = read_address (unit, ranges_ptr, ranges_end);
2430 ranges_ptr += unit->addr_size;
2432 if (low_pc == 0 && high_pc == 0)
2434 if (low_pc == -1UL && high_pc != -1UL)
2435 base_address = high_pc;
2438 if (!arange_add (unit, arange,
2439 base_address + low_pc, base_address + high_pc))
2446 /* DWARF2 Compilation unit functions. */
2448 /* Scan over each die in a comp. unit looking for functions to add
2449 to the function table and variables to the variable table. */
2452 scan_unit_for_symbols (struct comp_unit *unit)
2454 bfd *abfd = unit->abfd;
2455 bfd_byte *info_ptr = unit->first_child_die_ptr;
2456 bfd_byte *info_ptr_end = unit->stash->info_ptr_end;
2457 int nesting_level = 1;
2458 struct funcinfo **nested_funcs;
2459 int nested_funcs_size;
2461 /* Maintain a stack of in-scope functions and inlined functions, which we
2462 can use to set the caller_func field. */
2463 nested_funcs_size = 32;
2464 nested_funcs = (struct funcinfo **)
2465 bfd_malloc (nested_funcs_size * sizeof (struct funcinfo *));
2466 if (nested_funcs == NULL)
2468 nested_funcs[nesting_level] = 0;
2470 while (nesting_level)
2472 unsigned int abbrev_number, bytes_read, i;
2473 struct abbrev_info *abbrev;
2474 struct attribute attr;
2475 struct funcinfo *func;
2476 struct varinfo *var;
2478 bfd_vma high_pc = 0;
2479 bfd_boolean high_pc_relative = FALSE;
2481 /* PR 17512: file: 9f405d9d. */
2482 if (info_ptr >= info_ptr_end)
2485 abbrev_number = safe_read_leb128 (abfd, info_ptr, &bytes_read, FALSE, info_ptr_end);
2486 info_ptr += bytes_read;
2488 if (! abbrev_number)
2494 abbrev = lookup_abbrev (abbrev_number,unit->abbrevs);
2497 (*_bfd_error_handler)
2498 (_("Dwarf Error: Could not find abbrev number %u."),
2500 bfd_set_error (bfd_error_bad_value);
2505 if (abbrev->tag == DW_TAG_subprogram
2506 || abbrev->tag == DW_TAG_entry_point
2507 || abbrev->tag == DW_TAG_inlined_subroutine)
2509 bfd_size_type amt = sizeof (struct funcinfo);
2510 func = (struct funcinfo *) bfd_zalloc (abfd, amt);
2513 func->tag = abbrev->tag;
2514 func->prev_func = unit->function_table;
2515 unit->function_table = func;
2516 BFD_ASSERT (!unit->cached);
2518 if (func->tag == DW_TAG_inlined_subroutine)
2519 for (i = nesting_level - 1; i >= 1; i--)
2520 if (nested_funcs[i])
2522 func->caller_func = nested_funcs[i];
2525 nested_funcs[nesting_level] = func;
2530 if (abbrev->tag == DW_TAG_variable)
2532 bfd_size_type amt = sizeof (struct varinfo);
2533 var = (struct varinfo *) bfd_zalloc (abfd, amt);
2536 var->tag = abbrev->tag;
2538 var->prev_var = unit->variable_table;
2539 unit->variable_table = var;
2540 BFD_ASSERT (!unit->cached);
2543 /* No inline function in scope at this nesting level. */
2544 nested_funcs[nesting_level] = 0;
2547 for (i = 0; i < abbrev->num_attrs; ++i)
2549 info_ptr = read_attribute (&attr, &abbrev->attrs[i], unit, info_ptr, info_ptr_end);
2550 if (info_ptr == NULL)
2557 case DW_AT_call_file:
2558 func->caller_file = concat_filename (unit->line_table,
2562 case DW_AT_call_line:
2563 func->caller_line = attr.u.val;
2566 case DW_AT_abstract_origin:
2567 case DW_AT_specification:
2568 func->name = find_abstract_instance_name (unit, &attr,
2573 /* Prefer DW_AT_MIPS_linkage_name or DW_AT_linkage_name
2575 if (func->name == NULL && is_str_attr (attr.form))
2577 func->name = attr.u.str;
2578 if (non_mangled (unit->lang))
2579 func->is_linkage = TRUE;
2583 case DW_AT_linkage_name:
2584 case DW_AT_MIPS_linkage_name:
2585 /* PR 16949: Corrupt debug info can place
2586 non-string forms into these attributes. */
2587 if (is_str_attr (attr.form))
2589 func->name = attr.u.str;
2590 func->is_linkage = TRUE;
2595 low_pc = attr.u.val;
2599 high_pc = attr.u.val;
2600 high_pc_relative = attr.form != DW_FORM_addr;
2604 if (!read_rangelist (unit, &func->arange, attr.u.val))
2608 case DW_AT_decl_file:
2609 func->file = concat_filename (unit->line_table,
2613 case DW_AT_decl_line:
2614 func->line = attr.u.val;
2626 var->name = attr.u.str;
2629 case DW_AT_decl_file:
2630 var->file = concat_filename (unit->line_table,
2634 case DW_AT_decl_line:
2635 var->line = attr.u.val;
2638 case DW_AT_external:
2639 if (attr.u.val != 0)
2643 case DW_AT_location:
2647 case DW_FORM_block1:
2648 case DW_FORM_block2:
2649 case DW_FORM_block4:
2650 case DW_FORM_exprloc:
2651 if (*attr.u.blk->data == DW_OP_addr)
2655 /* Verify that DW_OP_addr is the only opcode in the
2656 location, in which case the block size will be 1
2657 plus the address size. */
2658 /* ??? For TLS variables, gcc can emit
2659 DW_OP_addr <addr> DW_OP_GNU_push_tls_address
2660 which we don't handle here yet. */
2661 if (attr.u.blk->size == unit->addr_size + 1U)
2662 var->addr = bfd_get (unit->addr_size * 8,
2664 attr.u.blk->data + 1);
2679 if (high_pc_relative)
2682 if (func && high_pc != 0)
2684 if (!arange_add (unit, &func->arange, low_pc, high_pc))
2688 if (abbrev->has_children)
2692 if (nesting_level >= nested_funcs_size)
2694 struct funcinfo **tmp;
2696 nested_funcs_size *= 2;
2697 tmp = (struct funcinfo **)
2698 bfd_realloc (nested_funcs,
2699 nested_funcs_size * sizeof (struct funcinfo *));
2704 nested_funcs[nesting_level] = 0;
2708 free (nested_funcs);
2712 free (nested_funcs);
2716 /* Parse a DWARF2 compilation unit starting at INFO_PTR. This
2717 includes the compilation unit header that proceeds the DIE's, but
2718 does not include the length field that precedes each compilation
2719 unit header. END_PTR points one past the end of this comp unit.
2720 OFFSET_SIZE is the size of DWARF2 offsets (either 4 or 8 bytes).
2722 This routine does not read the whole compilation unit; only enough
2723 to get to the line number information for the compilation unit. */
2725 static struct comp_unit *
2726 parse_comp_unit (struct dwarf2_debug *stash,
2727 bfd_vma unit_length,
2728 bfd_byte *info_ptr_unit,
2729 unsigned int offset_size)
2731 struct comp_unit* unit;
2732 unsigned int version;
2733 bfd_uint64_t abbrev_offset = 0;
2734 unsigned int addr_size;
2735 struct abbrev_info** abbrevs;
2736 unsigned int abbrev_number, bytes_read, i;
2737 struct abbrev_info *abbrev;
2738 struct attribute attr;
2739 bfd_byte *info_ptr = stash->info_ptr;
2740 bfd_byte *end_ptr = info_ptr + unit_length;
2743 bfd_vma high_pc = 0;
2744 bfd *abfd = stash->bfd_ptr;
2745 bfd_boolean high_pc_relative = FALSE;
2747 version = read_2_bytes (abfd, info_ptr, end_ptr);
2749 BFD_ASSERT (offset_size == 4 || offset_size == 8);
2750 if (offset_size == 4)
2751 abbrev_offset = read_4_bytes (abfd, info_ptr, end_ptr);
2753 abbrev_offset = read_8_bytes (abfd, info_ptr, end_ptr);
2754 info_ptr += offset_size;
2755 addr_size = read_1_byte (abfd, info_ptr, end_ptr);
2758 if (version != 2 && version != 3 && version != 4)
2760 /* PR 19872: A version number of 0 probably means that there is padding
2761 at the end of the .debug_info section. Gold puts it there when
2762 performing an incremental link, for example. So do not generate
2763 an error, just return a NULL. */
2766 (*_bfd_error_handler)
2767 (_("Dwarf Error: found dwarf version '%u', this reader"
2768 " only handles version 2, 3 and 4 information."), version);
2769 bfd_set_error (bfd_error_bad_value);
2774 if (addr_size > sizeof (bfd_vma))
2776 (*_bfd_error_handler)
2777 (_("Dwarf Error: found address size '%u', this reader"
2778 " can not handle sizes greater than '%u'."),
2780 (unsigned int) sizeof (bfd_vma));
2781 bfd_set_error (bfd_error_bad_value);
2785 if (addr_size != 2 && addr_size != 4 && addr_size != 8)
2787 (*_bfd_error_handler)
2788 ("Dwarf Error: found address size '%u', this reader"
2789 " can only handle address sizes '2', '4' and '8'.", addr_size);
2790 bfd_set_error (bfd_error_bad_value);
2794 /* Read the abbrevs for this compilation unit into a table. */
2795 abbrevs = read_abbrevs (abfd, abbrev_offset, stash);
2799 abbrev_number = safe_read_leb128 (abfd, info_ptr, &bytes_read, FALSE, end_ptr);
2800 info_ptr += bytes_read;
2801 if (! abbrev_number)
2803 /* PR 19872: An abbrev number of 0 probably means that there is padding
2804 at the end of the .debug_abbrev section. Gold puts it there when
2805 performing an incremental link, for example. So do not generate
2806 an error, just return a NULL. */
2810 abbrev = lookup_abbrev (abbrev_number, abbrevs);
2813 (*_bfd_error_handler) (_("Dwarf Error: Could not find abbrev number %u."),
2815 bfd_set_error (bfd_error_bad_value);
2819 amt = sizeof (struct comp_unit);
2820 unit = (struct comp_unit *) bfd_zalloc (abfd, amt);
2824 unit->version = version;
2825 unit->addr_size = addr_size;
2826 unit->offset_size = offset_size;
2827 unit->abbrevs = abbrevs;
2828 unit->end_ptr = end_ptr;
2829 unit->stash = stash;
2830 unit->info_ptr_unit = info_ptr_unit;
2831 unit->sec_info_ptr = stash->sec_info_ptr;
2833 for (i = 0; i < abbrev->num_attrs; ++i)
2835 info_ptr = read_attribute (&attr, &abbrev->attrs[i], unit, info_ptr, end_ptr);
2836 if (info_ptr == NULL)
2839 /* Store the data if it is of an attribute we want to keep in a
2840 partial symbol table. */
2843 case DW_AT_stmt_list:
2845 unit->line_offset = attr.u.val;
2849 unit->name = attr.u.str;
2853 low_pc = attr.u.val;
2854 /* If the compilation unit DIE has a DW_AT_low_pc attribute,
2855 this is the base address to use when reading location
2856 lists or range lists. */
2857 if (abbrev->tag == DW_TAG_compile_unit)
2858 unit->base_address = low_pc;
2862 high_pc = attr.u.val;
2863 high_pc_relative = attr.form != DW_FORM_addr;
2867 if (!read_rangelist (unit, &unit->arange, attr.u.val))
2871 case DW_AT_comp_dir:
2873 char *comp_dir = attr.u.str;
2875 /* PR 17512: file: 1fe726be. */
2876 if (! is_str_attr (attr.form))
2878 (*_bfd_error_handler)
2879 (_("Dwarf Error: DW_AT_comp_dir attribute encountered with a non-string form."));
2885 /* Irix 6.2 native cc prepends <machine>.: to the compilation
2886 directory, get rid of it. */
2887 char *cp = strchr (comp_dir, ':');
2889 if (cp && cp != comp_dir && cp[-1] == '.' && cp[1] == '/')
2892 unit->comp_dir = comp_dir;
2896 case DW_AT_language:
2897 unit->lang = attr.u.val;
2904 if (high_pc_relative)
2908 if (!arange_add (unit, &unit->arange, low_pc, high_pc))
2912 unit->first_child_die_ptr = info_ptr;
2916 /* Return TRUE if UNIT may contain the address given by ADDR. When
2917 there are functions written entirely with inline asm statements, the
2918 range info in the compilation unit header may not be correct. We
2919 need to consult the line info table to see if a compilation unit
2920 really contains the given address. */
2923 comp_unit_contains_address (struct comp_unit *unit, bfd_vma addr)
2925 struct arange *arange;
2930 arange = &unit->arange;
2933 if (addr >= arange->low && addr < arange->high)
2935 arange = arange->next;
2942 /* If UNIT contains ADDR, set the output parameters to the values for
2943 the line containing ADDR. The output parameters, FILENAME_PTR,
2944 FUNCTION_PTR, and LINENUMBER_PTR, are pointers to the objects
2947 Returns the range of addresses covered by the entry that was used
2948 to fill in *LINENUMBER_PTR or 0 if it was not filled in. */
2951 comp_unit_find_nearest_line (struct comp_unit *unit,
2953 const char **filename_ptr,
2954 struct funcinfo **function_ptr,
2955 unsigned int *linenumber_ptr,
2956 unsigned int *discriminator_ptr,
2957 struct dwarf2_debug *stash)
2964 if (! unit->line_table)
2966 if (! unit->stmtlist)
2972 unit->line_table = decode_line_info (unit, stash);
2974 if (! unit->line_table)
2980 if (unit->first_child_die_ptr < unit->end_ptr
2981 && ! scan_unit_for_symbols (unit))
2988 *function_ptr = NULL;
2989 func_p = lookup_address_in_function_table (unit, addr, function_ptr);
2990 if (func_p && (*function_ptr)->tag == DW_TAG_inlined_subroutine)
2991 stash->inliner_chain = *function_ptr;
2993 return lookup_address_in_line_info_table (unit->line_table, addr,
2999 /* Check to see if line info is already decoded in a comp_unit.
3000 If not, decode it. Returns TRUE if no errors were encountered;
3004 comp_unit_maybe_decode_line_info (struct comp_unit *unit,
3005 struct dwarf2_debug *stash)
3010 if (! unit->line_table)
3012 if (! unit->stmtlist)
3018 unit->line_table = decode_line_info (unit, stash);
3020 if (! unit->line_table)
3026 if (unit->first_child_die_ptr < unit->end_ptr
3027 && ! scan_unit_for_symbols (unit))
3037 /* If UNIT contains SYM at ADDR, set the output parameters to the
3038 values for the line containing SYM. The output parameters,
3039 FILENAME_PTR, and LINENUMBER_PTR, are pointers to the objects to be
3042 Return TRUE if UNIT contains SYM, and no errors were encountered;
3046 comp_unit_find_line (struct comp_unit *unit,
3049 const char **filename_ptr,
3050 unsigned int *linenumber_ptr,
3051 struct dwarf2_debug *stash)
3053 if (!comp_unit_maybe_decode_line_info (unit, stash))
3056 if (sym->flags & BSF_FUNCTION)
3057 return lookup_symbol_in_function_table (unit, sym, addr,
3061 return lookup_symbol_in_variable_table (unit, sym, addr,
3066 static struct funcinfo *
3067 reverse_funcinfo_list (struct funcinfo *head)
3069 struct funcinfo *rhead;
3070 struct funcinfo *temp;
3072 for (rhead = NULL; head; head = temp)
3074 temp = head->prev_func;
3075 head->prev_func = rhead;
3081 static struct varinfo *
3082 reverse_varinfo_list (struct varinfo *head)
3084 struct varinfo *rhead;
3085 struct varinfo *temp;
3087 for (rhead = NULL; head; head = temp)
3089 temp = head->prev_var;
3090 head->prev_var = rhead;
3096 /* Extract all interesting funcinfos and varinfos of a compilation
3097 unit into hash tables for faster lookup. Returns TRUE if no
3098 errors were enountered; FALSE otherwise. */
3101 comp_unit_hash_info (struct dwarf2_debug *stash,
3102 struct comp_unit *unit,
3103 struct info_hash_table *funcinfo_hash_table,
3104 struct info_hash_table *varinfo_hash_table)
3106 struct funcinfo* each_func;
3107 struct varinfo* each_var;
3108 bfd_boolean okay = TRUE;
3110 BFD_ASSERT (stash->info_hash_status != STASH_INFO_HASH_DISABLED);
3112 if (!comp_unit_maybe_decode_line_info (unit, stash))
3115 BFD_ASSERT (!unit->cached);
3117 /* To preserve the original search order, we went to visit the function
3118 infos in the reversed order of the list. However, making the list
3119 bi-directional use quite a bit of extra memory. So we reverse
3120 the list first, traverse the list in the now reversed order and
3121 finally reverse the list again to get back the original order. */
3122 unit->function_table = reverse_funcinfo_list (unit->function_table);
3123 for (each_func = unit->function_table;
3125 each_func = each_func->prev_func)
3127 /* Skip nameless functions. */
3128 if (each_func->name)
3129 /* There is no need to copy name string into hash table as
3130 name string is either in the dwarf string buffer or
3131 info in the stash. */
3132 okay = insert_info_hash_table (funcinfo_hash_table, each_func->name,
3133 (void*) each_func, FALSE);
3135 unit->function_table = reverse_funcinfo_list (unit->function_table);
3139 /* We do the same for variable infos. */
3140 unit->variable_table = reverse_varinfo_list (unit->variable_table);
3141 for (each_var = unit->variable_table;
3143 each_var = each_var->prev_var)
3145 /* Skip stack vars and vars with no files or names. */
3146 if (each_var->stack == 0
3147 && each_var->file != NULL
3148 && each_var->name != NULL)
3149 /* There is no need to copy name string into hash table as
3150 name string is either in the dwarf string buffer or
3151 info in the stash. */
3152 okay = insert_info_hash_table (varinfo_hash_table, each_var->name,
3153 (void*) each_var, FALSE);
3156 unit->variable_table = reverse_varinfo_list (unit->variable_table);
3157 unit->cached = TRUE;
3161 /* Locate a section in a BFD containing debugging info. The search starts
3162 from the section after AFTER_SEC, or from the first section in the BFD if
3163 AFTER_SEC is NULL. The search works by examining the names of the
3164 sections. There are three permissiable names. The first two are given
3165 by DEBUG_SECTIONS[debug_info] (whose standard DWARF2 names are .debug_info
3166 and .zdebug_info). The third is a prefix .gnu.linkonce.wi.
3167 This is a variation on the .debug_info section which has a checksum
3168 describing the contents appended onto the name. This allows the linker to
3169 identify and discard duplicate debugging sections for different
3170 compilation units. */
3171 #define GNU_LINKONCE_INFO ".gnu.linkonce.wi."
3174 find_debug_info (bfd *abfd, const struct dwarf_debug_section *debug_sections,
3175 asection *after_sec)
3180 if (after_sec == NULL)
3182 look = debug_sections[debug_info].uncompressed_name;
3183 msec = bfd_get_section_by_name (abfd, look);
3187 look = debug_sections[debug_info].compressed_name;
3190 msec = bfd_get_section_by_name (abfd, look);
3195 for (msec = abfd->sections; msec != NULL; msec = msec->next)
3196 if (CONST_STRNEQ (msec->name, GNU_LINKONCE_INFO))
3202 for (msec = after_sec->next; msec != NULL; msec = msec->next)
3204 look = debug_sections[debug_info].uncompressed_name;
3205 if (strcmp (msec->name, look) == 0)
3208 look = debug_sections[debug_info].compressed_name;
3209 if (look != NULL && strcmp (msec->name, look) == 0)
3212 if (CONST_STRNEQ (msec->name, GNU_LINKONCE_INFO))
3219 /* Transfer VMAs from object file to separate debug file. */
3222 set_debug_vma (bfd *orig_bfd, bfd *debug_bfd)
3226 for (s = orig_bfd->sections, d = debug_bfd->sections;
3227 s != NULL && d != NULL;
3228 s = s->next, d = d->next)
3230 if ((d->flags & SEC_DEBUGGING) != 0)
3232 /* ??? Assumes 1-1 correspondence between sections in the
3234 if (strcmp (s->name, d->name) == 0)
3236 d->output_section = s->output_section;
3237 d->output_offset = s->output_offset;
3243 /* Unset vmas for adjusted sections in STASH. */
3246 unset_sections (struct dwarf2_debug *stash)
3249 struct adjusted_section *p;
3251 i = stash->adjusted_section_count;
3252 p = stash->adjusted_sections;
3253 for (; i > 0; i--, p++)
3254 p->section->vma = 0;
3257 /* Set VMAs for allocated and .debug_info sections in ORIG_BFD, a
3258 relocatable object file. VMAs are normally all zero in relocatable
3259 object files, so if we want to distinguish locations in sections by
3260 address we need to set VMAs so the sections do not overlap. We
3261 also set VMA on .debug_info so that when we have multiple
3262 .debug_info sections (or the linkonce variant) they also do not
3263 overlap. The multiple .debug_info sections make up a single
3264 logical section. ??? We should probably do the same for other
3268 place_sections (bfd *orig_bfd, struct dwarf2_debug *stash)
3271 struct adjusted_section *p;
3273 const char *debug_info_name;
3275 if (stash->adjusted_section_count != 0)
3277 i = stash->adjusted_section_count;
3278 p = stash->adjusted_sections;
3279 for (; i > 0; i--, p++)
3280 p->section->vma = p->adj_vma;
3284 debug_info_name = stash->debug_sections[debug_info].uncompressed_name;
3291 for (sect = abfd->sections; sect != NULL; sect = sect->next)
3295 if ((sect->output_section != NULL
3296 && sect->output_section != sect
3297 && (sect->flags & SEC_DEBUGGING) == 0)
3301 is_debug_info = (strcmp (sect->name, debug_info_name) == 0
3302 || CONST_STRNEQ (sect->name, GNU_LINKONCE_INFO));
3304 if (!((sect->flags & SEC_ALLOC) != 0 && abfd == orig_bfd)
3310 if (abfd == stash->bfd_ptr)
3312 abfd = stash->bfd_ptr;
3316 stash->adjusted_section_count = -1;
3319 bfd_vma last_vma = 0, last_dwarf = 0;
3320 bfd_size_type amt = i * sizeof (struct adjusted_section);
3322 p = (struct adjusted_section *) bfd_malloc (amt);
3326 stash->adjusted_sections = p;
3327 stash->adjusted_section_count = i;
3334 for (sect = abfd->sections; sect != NULL; sect = sect->next)
3339 if ((sect->output_section != NULL
3340 && sect->output_section != sect
3341 && (sect->flags & SEC_DEBUGGING) == 0)
3345 is_debug_info = (strcmp (sect->name, debug_info_name) == 0
3346 || CONST_STRNEQ (sect->name, GNU_LINKONCE_INFO));
3348 if (!((sect->flags & SEC_ALLOC) != 0 && abfd == orig_bfd)
3352 sz = sect->rawsize ? sect->rawsize : sect->size;
3356 BFD_ASSERT (sect->alignment_power == 0);
3357 sect->vma = last_dwarf;
3362 /* Align the new address to the current section
3364 last_vma = ((last_vma
3365 + ~(-((bfd_vma) 1 << sect->alignment_power)))
3366 & (-((bfd_vma) 1 << sect->alignment_power)));
3367 sect->vma = last_vma;
3372 p->adj_vma = sect->vma;
3375 if (abfd == stash->bfd_ptr)
3377 abfd = stash->bfd_ptr;
3381 if (orig_bfd != stash->bfd_ptr)
3382 set_debug_vma (orig_bfd, stash->bfd_ptr);
3387 /* Look up a funcinfo by name using the given info hash table. If found,
3388 also update the locations pointed to by filename_ptr and linenumber_ptr.
3390 This function returns TRUE if a funcinfo that matches the given symbol
3391 and address is found with any error; otherwise it returns FALSE. */
3394 info_hash_lookup_funcinfo (struct info_hash_table *hash_table,
3397 const char **filename_ptr,
3398 unsigned int *linenumber_ptr)
3400 struct funcinfo* each_func;
3401 struct funcinfo* best_fit = NULL;
3402 bfd_vma best_fit_len = 0;
3403 struct info_list_node *node;
3404 struct arange *arange;
3405 const char *name = bfd_asymbol_name (sym);
3406 asection *sec = bfd_get_section (sym);
3408 for (node = lookup_info_hash_table (hash_table, name);
3412 each_func = (struct funcinfo *) node->info;
3413 for (arange = &each_func->arange;
3415 arange = arange->next)
3417 if ((!each_func->sec || each_func->sec == sec)
3418 && addr >= arange->low
3419 && addr < arange->high
3421 || arange->high - arange->low < best_fit_len))
3423 best_fit = each_func;
3424 best_fit_len = arange->high - arange->low;
3431 best_fit->sec = sec;
3432 *filename_ptr = best_fit->file;
3433 *linenumber_ptr = best_fit->line;
3440 /* Look up a varinfo by name using the given info hash table. If found,
3441 also update the locations pointed to by filename_ptr and linenumber_ptr.
3443 This function returns TRUE if a varinfo that matches the given symbol
3444 and address is found with any error; otherwise it returns FALSE. */
3447 info_hash_lookup_varinfo (struct info_hash_table *hash_table,
3450 const char **filename_ptr,
3451 unsigned int *linenumber_ptr)
3453 const char *name = bfd_asymbol_name (sym);
3454 asection *sec = bfd_get_section (sym);
3455 struct varinfo* each;
3456 struct info_list_node *node;
3458 for (node = lookup_info_hash_table (hash_table, name);
3462 each = (struct varinfo *) node->info;
3463 if (each->addr == addr
3464 && (!each->sec || each->sec == sec))
3467 *filename_ptr = each->file;
3468 *linenumber_ptr = each->line;
3476 /* Update the funcinfo and varinfo info hash tables if they are
3477 not up to date. Returns TRUE if there is no error; otherwise
3478 returns FALSE and disable the info hash tables. */
3481 stash_maybe_update_info_hash_tables (struct dwarf2_debug *stash)
3483 struct comp_unit *each;
3485 /* Exit if hash tables are up-to-date. */
3486 if (stash->all_comp_units == stash->hash_units_head)
3489 if (stash->hash_units_head)
3490 each = stash->hash_units_head->prev_unit;
3492 each = stash->last_comp_unit;
3496 if (!comp_unit_hash_info (stash, each, stash->funcinfo_hash_table,
3497 stash->varinfo_hash_table))
3499 stash->info_hash_status = STASH_INFO_HASH_DISABLED;
3502 each = each->prev_unit;
3505 stash->hash_units_head = stash->all_comp_units;
3509 /* Check consistency of info hash tables. This is for debugging only. */
3511 static void ATTRIBUTE_UNUSED
3512 stash_verify_info_hash_table (struct dwarf2_debug *stash)
3514 struct comp_unit *each_unit;
3515 struct funcinfo *each_func;
3516 struct varinfo *each_var;
3517 struct info_list_node *node;
3520 for (each_unit = stash->all_comp_units;
3522 each_unit = each_unit->next_unit)
3524 for (each_func = each_unit->function_table;
3526 each_func = each_func->prev_func)
3528 if (!each_func->name)
3530 node = lookup_info_hash_table (stash->funcinfo_hash_table,
3534 while (node && !found)
3536 found = node->info == each_func;
3542 for (each_var = each_unit->variable_table;
3544 each_var = each_var->prev_var)
3546 if (!each_var->name || !each_var->file || each_var->stack)
3548 node = lookup_info_hash_table (stash->varinfo_hash_table,
3552 while (node && !found)
3554 found = node->info == each_var;
3562 /* Check to see if we want to enable the info hash tables, which consume
3563 quite a bit of memory. Currently we only check the number times
3564 bfd_dwarf2_find_line is called. In the future, we may also want to
3565 take the number of symbols into account. */
3568 stash_maybe_enable_info_hash_tables (bfd *abfd, struct dwarf2_debug *stash)
3570 BFD_ASSERT (stash->info_hash_status == STASH_INFO_HASH_OFF);
3572 if (stash->info_hash_count++ < STASH_INFO_HASH_TRIGGER)
3575 /* FIXME: Maybe we should check the reduce_memory_overheads
3576 and optimize fields in the bfd_link_info structure ? */
3578 /* Create hash tables. */
3579 stash->funcinfo_hash_table = create_info_hash_table (abfd);
3580 stash->varinfo_hash_table = create_info_hash_table (abfd);
3581 if (!stash->funcinfo_hash_table || !stash->varinfo_hash_table)
3583 /* Turn off info hashes if any allocation above fails. */
3584 stash->info_hash_status = STASH_INFO_HASH_DISABLED;
3587 /* We need a forced update so that the info hash tables will
3588 be created even though there is no compilation unit. That
3589 happens if STASH_INFO_HASH_TRIGGER is 0. */
3590 stash_maybe_update_info_hash_tables (stash);
3591 stash->info_hash_status = STASH_INFO_HASH_ON;
3594 /* Find the file and line associated with a symbol and address using the
3595 info hash tables of a stash. If there is a match, the function returns
3596 TRUE and update the locations pointed to by filename_ptr and linenumber_ptr;
3597 otherwise it returns FALSE. */
3600 stash_find_line_fast (struct dwarf2_debug *stash,
3603 const char **filename_ptr,
3604 unsigned int *linenumber_ptr)
3606 BFD_ASSERT (stash->info_hash_status == STASH_INFO_HASH_ON);
3608 if (sym->flags & BSF_FUNCTION)
3609 return info_hash_lookup_funcinfo (stash->funcinfo_hash_table, sym, addr,
3610 filename_ptr, linenumber_ptr);
3611 return info_hash_lookup_varinfo (stash->varinfo_hash_table, sym, addr,
3612 filename_ptr, linenumber_ptr);
3615 /* Save current section VMAs. */
3618 save_section_vma (const bfd *abfd, struct dwarf2_debug *stash)
3623 if (abfd->section_count == 0)
3625 stash->sec_vma = bfd_malloc (sizeof (*stash->sec_vma) * abfd->section_count);
3626 if (stash->sec_vma == NULL)
3628 for (i = 0, s = abfd->sections; i < abfd->section_count; i++, s = s->next)
3630 if (s->output_section != NULL)
3631 stash->sec_vma[i] = s->output_section->vma + s->output_offset;
3633 stash->sec_vma[i] = s->vma;
3638 /* Compare current section VMAs against those at the time the stash
3639 was created. If find_nearest_line is used in linker warnings or
3640 errors early in the link process, the debug info stash will be
3641 invalid for later calls. This is because we relocate debug info
3642 sections, so the stashed section contents depend on symbol values,
3643 which in turn depend on section VMAs. */
3646 section_vma_same (const bfd *abfd, const struct dwarf2_debug *stash)
3651 for (i = 0, s = abfd->sections; i < abfd->section_count; i++, s = s->next)
3655 if (s->output_section != NULL)
3656 vma = s->output_section->vma + s->output_offset;
3659 if (vma != stash->sec_vma[i])
3665 /* Read debug information from DEBUG_BFD when DEBUG_BFD is specified.
3666 If DEBUG_BFD is not specified, we read debug information from ABFD
3667 or its gnu_debuglink. The results will be stored in PINFO.
3668 The function returns TRUE iff debug information is ready. */
3671 _bfd_dwarf2_slurp_debug_info (bfd *abfd, bfd *debug_bfd,
3672 const struct dwarf_debug_section *debug_sections,
3675 bfd_boolean do_place)
3677 bfd_size_type amt = sizeof (struct dwarf2_debug);
3678 bfd_size_type total_size;
3680 struct dwarf2_debug *stash = (struct dwarf2_debug *) *pinfo;
3684 if (section_vma_same (abfd, stash))
3686 _bfd_dwarf2_cleanup_debug_info (abfd, pinfo);
3687 memset (stash, 0, amt);
3691 stash = (struct dwarf2_debug *) bfd_zalloc (abfd, amt);
3695 stash->debug_sections = debug_sections;
3696 stash->syms = symbols;
3697 if (!save_section_vma (abfd, stash))
3702 if (debug_bfd == NULL)
3705 msec = find_debug_info (debug_bfd, debug_sections, NULL);
3706 if (msec == NULL && abfd == debug_bfd)
3708 char * debug_filename = bfd_follow_gnu_debuglink (abfd, DEBUGDIR);
3710 if (debug_filename == NULL)
3711 /* No dwarf2 info, and no gnu_debuglink to follow.
3712 Note that at this point the stash has been allocated, but
3713 contains zeros. This lets future calls to this function
3714 fail more quickly. */
3717 /* Set BFD_DECOMPRESS to decompress debug sections. */
3718 if ((debug_bfd = bfd_openr (debug_filename, NULL)) == NULL
3719 || !(debug_bfd->flags |= BFD_DECOMPRESS,
3720 bfd_check_format (debug_bfd, bfd_object))
3721 || (msec = find_debug_info (debug_bfd,
3722 debug_sections, NULL)) == NULL
3723 || !bfd_generic_link_read_symbols (debug_bfd))
3726 bfd_close (debug_bfd);
3727 /* FIXME: Should we report our failure to follow the debuglink ? */
3728 free (debug_filename);
3732 symbols = bfd_get_outsymbols (debug_bfd);
3733 stash->syms = symbols;
3734 stash->close_on_cleanup = TRUE;
3736 stash->bfd_ptr = debug_bfd;
3739 && !place_sections (abfd, stash))
3742 /* There can be more than one DWARF2 info section in a BFD these
3743 days. First handle the easy case when there's only one. If
3744 there's more than one, try case two: none of the sections is
3745 compressed. In that case, read them all in and produce one
3746 large stash. We do this in two passes - in the first pass we
3747 just accumulate the section sizes, and in the second pass we
3748 read in the section's contents. (The allows us to avoid
3749 reallocing the data as we add sections to the stash.) If
3750 some or all sections are compressed, then do things the slow
3751 way, with a bunch of reallocs. */
3753 if (! find_debug_info (debug_bfd, debug_sections, msec))
3755 /* Case 1: only one info section. */
3756 total_size = msec->size;
3757 if (! read_section (debug_bfd, &stash->debug_sections[debug_info],
3759 &stash->info_ptr_memory, &total_size))
3764 /* Case 2: multiple sections. */
3765 for (total_size = 0;
3767 msec = find_debug_info (debug_bfd, debug_sections, msec))
3768 total_size += msec->size;
3770 stash->info_ptr_memory = (bfd_byte *) bfd_malloc (total_size);
3771 if (stash->info_ptr_memory == NULL)
3775 for (msec = find_debug_info (debug_bfd, debug_sections, NULL);
3777 msec = find_debug_info (debug_bfd, debug_sections, msec))
3785 if (!(bfd_simple_get_relocated_section_contents
3786 (debug_bfd, msec, stash->info_ptr_memory + total_size,
3794 stash->info_ptr = stash->info_ptr_memory;
3795 stash->info_ptr_end = stash->info_ptr + total_size;
3796 stash->sec = find_debug_info (debug_bfd, debug_sections, NULL);
3797 stash->sec_info_ptr = stash->info_ptr;
3801 /* Scan the debug information in PINFO looking for a DW_TAG_subprogram
3802 abbrev with a DW_AT_low_pc attached to it. Then lookup that same
3803 symbol in SYMBOLS and return the difference between the low_pc and
3804 the symbol's address. Returns 0 if no suitable symbol could be found. */
3807 _bfd_dwarf2_find_symbol_bias (asymbol ** symbols, void ** pinfo)
3809 struct dwarf2_debug *stash;
3810 struct comp_unit * unit;
3812 stash = (struct dwarf2_debug *) *pinfo;
3817 for (unit = stash->all_comp_units; unit; unit = unit->next_unit)
3819 struct funcinfo * func;
3821 if (unit->function_table == NULL)
3823 if (unit->line_table == NULL)
3824 unit->line_table = decode_line_info (unit, stash);
3825 if (unit->line_table != NULL)
3826 scan_unit_for_symbols (unit);
3829 for (func = unit->function_table; func != NULL; func = func->prev_func)
3830 if (func->name && func->arange.low)
3834 /* FIXME: Do we need to scan the aranges looking for the lowest pc value ? */
3836 for (psym = symbols; * psym != NULL; psym++)
3838 asymbol * sym = * psym;
3840 if (sym->flags & BSF_FUNCTION
3841 && sym->section != NULL
3842 && strcmp (sym->name, func->name) == 0)
3843 return ((bfd_signed_vma) func->arange.low) -
3844 ((bfd_signed_vma) (sym->value + sym->section->vma));
3852 /* Find the source code location of SYMBOL. If SYMBOL is NULL
3853 then find the nearest source code location corresponding to
3854 the address SECTION + OFFSET.
3855 Returns TRUE if the line is found without error and fills in
3856 FILENAME_PTR and LINENUMBER_PTR. In the case where SYMBOL was
3857 NULL the FUNCTIONNAME_PTR is also filled in.
3858 SYMBOLS contains the symbol table for ABFD.
3859 DEBUG_SECTIONS contains the name of the dwarf debug sections.
3860 ADDR_SIZE is the number of bytes in the initial .debug_info length
3861 field and in the abbreviation offset, or zero to indicate that the
3862 default value should be used. */
3865 _bfd_dwarf2_find_nearest_line (bfd *abfd,
3870 const char **filename_ptr,
3871 const char **functionname_ptr,
3872 unsigned int *linenumber_ptr,
3873 unsigned int *discriminator_ptr,
3874 const struct dwarf_debug_section *debug_sections,
3875 unsigned int addr_size,
3878 /* Read each compilation unit from the section .debug_info, and check
3879 to see if it contains the address we are searching for. If yes,
3880 lookup the address, and return the line number info. If no, go
3881 on to the next compilation unit.
3883 We keep a list of all the previously read compilation units, and
3884 a pointer to the next un-read compilation unit. Check the
3885 previously read units before reading more. */
3886 struct dwarf2_debug *stash;
3887 /* What address are we looking for? */
3889 struct comp_unit* each;
3890 struct funcinfo *function = NULL;
3891 bfd_boolean found = FALSE;
3892 bfd_boolean do_line;
3894 *filename_ptr = NULL;
3895 if (functionname_ptr != NULL)
3896 *functionname_ptr = NULL;
3897 *linenumber_ptr = 0;
3898 if (discriminator_ptr)
3899 *discriminator_ptr = 0;
3901 if (! _bfd_dwarf2_slurp_debug_info (abfd, NULL, debug_sections,
3903 (abfd->flags & (EXEC_P | DYNAMIC)) == 0))
3906 stash = (struct dwarf2_debug *) *pinfo;
3908 do_line = symbol != NULL;
3911 BFD_ASSERT (section == NULL && offset == 0 && functionname_ptr == NULL);
3912 section = bfd_get_section (symbol);
3913 addr = symbol->value;
3917 BFD_ASSERT (section != NULL && functionname_ptr != NULL);
3921 if (section->output_section)
3922 addr += section->output_section->vma + section->output_offset;
3924 addr += section->vma;
3926 /* A null info_ptr indicates that there is no dwarf2 info
3927 (or that an error occured while setting up the stash). */
3928 if (! stash->info_ptr)
3931 stash->inliner_chain = NULL;
3933 /* Check the previously read comp. units first. */
3936 /* The info hash tables use quite a bit of memory. We may not want to
3937 always use them. We use some heuristics to decide if and when to
3939 if (stash->info_hash_status == STASH_INFO_HASH_OFF)
3940 stash_maybe_enable_info_hash_tables (abfd, stash);
3942 /* Keep info hash table up to date if they are available. Note that we
3943 may disable the hash tables if there is any error duing update. */
3944 if (stash->info_hash_status == STASH_INFO_HASH_ON)
3945 stash_maybe_update_info_hash_tables (stash);
3947 if (stash->info_hash_status == STASH_INFO_HASH_ON)
3949 found = stash_find_line_fast (stash, symbol, addr, filename_ptr,
3956 /* Check the previously read comp. units first. */
3957 for (each = stash->all_comp_units; each; each = each->next_unit)
3958 if ((symbol->flags & BSF_FUNCTION) == 0
3959 || each->arange.high == 0
3960 || comp_unit_contains_address (each, addr))
3962 found = comp_unit_find_line (each, symbol, addr, filename_ptr,
3963 linenumber_ptr, stash);
3971 bfd_vma min_range = (bfd_vma) -1;
3972 const char * local_filename = NULL;
3973 struct funcinfo *local_function = NULL;
3974 unsigned int local_linenumber = 0;
3975 unsigned int local_discriminator = 0;
3977 for (each = stash->all_comp_units; each; each = each->next_unit)
3979 bfd_vma range = (bfd_vma) -1;
3981 found = ((each->arange.high == 0
3982 || comp_unit_contains_address (each, addr))
3983 && (range = comp_unit_find_nearest_line (each, addr,
3987 & local_discriminator,
3991 /* PRs 15935 15994: Bogus debug information may have provided us
3992 with an erroneous match. We attempt to counter this by
3993 selecting the match that has the smallest address range
3994 associated with it. (We are assuming that corrupt debug info
3995 will tend to result in extra large address ranges rather than
3996 extra small ranges).
3998 This does mean that we scan through all of the CUs associated
3999 with the bfd each time this function is called. But this does
4000 have the benefit of producing consistent results every time the
4001 function is called. */
4002 if (range <= min_range)
4004 if (filename_ptr && local_filename)
4005 * filename_ptr = local_filename;
4007 function = local_function;
4008 if (discriminator_ptr && local_discriminator)
4009 * discriminator_ptr = local_discriminator;
4010 if (local_linenumber)
4011 * linenumber_ptr = local_linenumber;
4017 if (* linenumber_ptr)
4024 /* The DWARF2 spec says that the initial length field, and the
4025 offset of the abbreviation table, should both be 4-byte values.
4026 However, some compilers do things differently. */
4029 BFD_ASSERT (addr_size == 4 || addr_size == 8);
4031 /* Read each remaining comp. units checking each as they are read. */
4032 while (stash->info_ptr < stash->info_ptr_end)
4035 unsigned int offset_size = addr_size;
4036 bfd_byte *info_ptr_unit = stash->info_ptr;
4038 length = read_4_bytes (stash->bfd_ptr, stash->info_ptr, stash->info_ptr_end);
4039 /* A 0xffffff length is the DWARF3 way of indicating
4040 we use 64-bit offsets, instead of 32-bit offsets. */
4041 if (length == 0xffffffff)
4044 length = read_8_bytes (stash->bfd_ptr, stash->info_ptr + 4, stash->info_ptr_end);
4045 stash->info_ptr += 12;
4047 /* A zero length is the IRIX way of indicating 64-bit offsets,
4048 mostly because the 64-bit length will generally fit in 32
4049 bits, and the endianness helps. */
4050 else if (length == 0)
4053 length = read_4_bytes (stash->bfd_ptr, stash->info_ptr + 4, stash->info_ptr_end);
4054 stash->info_ptr += 8;
4056 /* In the absence of the hints above, we assume 32-bit DWARF2
4057 offsets even for targets with 64-bit addresses, because:
4058 a) most of the time these targets will not have generated
4059 more than 2Gb of debug info and so will not need 64-bit
4062 b) if they do use 64-bit offsets but they are not using
4063 the size hints that are tested for above then they are
4064 not conforming to the DWARF3 standard anyway. */
4065 else if (addr_size == 8)
4068 stash->info_ptr += 4;
4071 stash->info_ptr += 4;
4077 each = parse_comp_unit (stash, length, info_ptr_unit,
4080 /* The dwarf information is damaged, don't trust it any
4084 new_ptr = stash->info_ptr + length;
4085 /* PR 17512: file: 1500698c. */
4086 if (new_ptr < stash->info_ptr)
4088 /* A corrupt length value - do not trust the info any more. */
4093 stash->info_ptr = new_ptr;
4095 if (stash->all_comp_units)
4096 stash->all_comp_units->prev_unit = each;
4098 stash->last_comp_unit = each;
4100 each->next_unit = stash->all_comp_units;
4101 stash->all_comp_units = each;
4103 /* DW_AT_low_pc and DW_AT_high_pc are optional for
4104 compilation units. If we don't have them (i.e.,
4105 unit->high == 0), we need to consult the line info table
4106 to see if a compilation unit contains the given
4109 found = (((symbol->flags & BSF_FUNCTION) == 0
4110 || each->arange.high == 0
4111 || comp_unit_contains_address (each, addr))
4112 && comp_unit_find_line (each, symbol, addr,
4117 found = ((each->arange.high == 0
4118 || comp_unit_contains_address (each, addr))
4119 && comp_unit_find_nearest_line (each, addr,
4126 if ((bfd_vma) (stash->info_ptr - stash->sec_info_ptr)
4127 == stash->sec->size)
4129 stash->sec = find_debug_info (stash->bfd_ptr, debug_sections,
4131 stash->sec_info_ptr = stash->info_ptr;
4142 if (!function->is_linkage)
4147 fun = _bfd_elf_find_function (abfd, symbols, section, offset,
4148 *filename_ptr ? NULL : filename_ptr,
4150 sec_vma = section->vma;
4151 if (section->output_section != NULL)
4152 sec_vma = section->output_section->vma + section->output_offset;
4154 && fun->value + sec_vma == function->arange.low)
4155 function->name = *functionname_ptr;
4156 /* Even if we didn't find a linkage name, say that we have
4157 to stop a repeated search of symbols. */
4158 function->is_linkage = TRUE;
4160 *functionname_ptr = function->name;
4162 if ((abfd->flags & (EXEC_P | DYNAMIC)) == 0)
4163 unset_sections (stash);
4169 _bfd_dwarf2_find_inliner_info (bfd *abfd ATTRIBUTE_UNUSED,
4170 const char **filename_ptr,
4171 const char **functionname_ptr,
4172 unsigned int *linenumber_ptr,
4175 struct dwarf2_debug *stash;
4177 stash = (struct dwarf2_debug *) *pinfo;
4180 struct funcinfo *func = stash->inliner_chain;
4182 if (func && func->caller_func)
4184 *filename_ptr = func->caller_file;
4185 *functionname_ptr = func->caller_func->name;
4186 *linenumber_ptr = func->caller_line;
4187 stash->inliner_chain = func->caller_func;
4196 _bfd_dwarf2_cleanup_debug_info (bfd *abfd, void **pinfo)
4198 struct dwarf2_debug *stash = (struct dwarf2_debug *) *pinfo;
4199 struct comp_unit *each;
4201 if (abfd == NULL || stash == NULL)
4204 for (each = stash->all_comp_units; each; each = each->next_unit)
4206 struct abbrev_info **abbrevs = each->abbrevs;
4207 struct funcinfo *function_table = each->function_table;
4208 struct varinfo *variable_table = each->variable_table;
4211 for (i = 0; i < ABBREV_HASH_SIZE; i++)
4213 struct abbrev_info *abbrev = abbrevs[i];
4217 free (abbrev->attrs);
4218 abbrev = abbrev->next;
4222 if (each->line_table)
4224 free (each->line_table->dirs);
4225 free (each->line_table->files);
4228 while (function_table)
4230 if (function_table->file)
4232 free (function_table->file);
4233 function_table->file = NULL;
4236 if (function_table->caller_file)
4238 free (function_table->caller_file);
4239 function_table->caller_file = NULL;
4241 function_table = function_table->prev_func;
4244 while (variable_table)
4246 if (variable_table->file)
4248 free (variable_table->file);
4249 variable_table->file = NULL;
4252 variable_table = variable_table->prev_var;
4256 if (stash->dwarf_abbrev_buffer)
4257 free (stash->dwarf_abbrev_buffer);
4258 if (stash->dwarf_line_buffer)
4259 free (stash->dwarf_line_buffer);
4260 if (stash->dwarf_str_buffer)
4261 free (stash->dwarf_str_buffer);
4262 if (stash->dwarf_ranges_buffer)
4263 free (stash->dwarf_ranges_buffer);
4264 if (stash->info_ptr_memory)
4265 free (stash->info_ptr_memory);
4266 if (stash->close_on_cleanup)
4267 bfd_close (stash->bfd_ptr);
4268 if (stash->alt_dwarf_str_buffer)
4269 free (stash->alt_dwarf_str_buffer);
4270 if (stash->alt_dwarf_info_buffer)
4271 free (stash->alt_dwarf_info_buffer);
4273 free (stash->sec_vma);
4274 if (stash->adjusted_sections)
4275 free (stash->adjusted_sections);
4276 if (stash->alt_bfd_ptr)
4277 bfd_close (stash->alt_bfd_ptr);
4280 /* Find the function to a particular section and offset,
4281 for error reporting. */
4284 _bfd_elf_find_function (bfd *abfd,
4288 const char **filename_ptr,
4289 const char **functionname_ptr)
4291 struct elf_find_function_cache
4293 asection *last_section;
4295 const char *filename;
4296 bfd_size_type func_size;
4299 if (symbols == NULL)
4302 if (bfd_get_flavour (abfd) != bfd_target_elf_flavour)
4305 cache = elf_tdata (abfd)->elf_find_function_cache;
4308 cache = bfd_zalloc (abfd, sizeof (*cache));
4309 elf_tdata (abfd)->elf_find_function_cache = cache;
4313 if (cache->last_section != section
4314 || cache->func == NULL
4315 || offset < cache->func->value
4316 || offset >= cache->func->value + cache->func_size)
4321 /* ??? Given multiple file symbols, it is impossible to reliably
4322 choose the right file name for global symbols. File symbols are
4323 local symbols, and thus all file symbols must sort before any
4324 global symbols. The ELF spec may be interpreted to say that a
4325 file symbol must sort before other local symbols, but currently
4326 ld -r doesn't do this. So, for ld -r output, it is possible to
4327 make a better choice of file name for local symbols by ignoring
4328 file symbols appearing after a given local symbol. */
4329 enum { nothing_seen, symbol_seen, file_after_symbol_seen } state;
4330 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
4334 state = nothing_seen;
4335 cache->filename = NULL;
4337 cache->func_size = 0;
4338 cache->last_section = section;
4340 for (p = symbols; *p != NULL; p++)
4346 if ((sym->flags & BSF_FILE) != 0)
4349 if (state == symbol_seen)
4350 state = file_after_symbol_seen;
4354 size = bed->maybe_function_sym (sym, section, &code_off);
4356 && code_off <= offset
4357 && (code_off > low_func
4358 || (code_off == low_func
4359 && size > cache->func_size)))
4362 cache->func_size = size;
4363 cache->filename = NULL;
4364 low_func = code_off;
4366 && ((sym->flags & BSF_LOCAL) != 0
4367 || state != file_after_symbol_seen))
4368 cache->filename = bfd_asymbol_name (file);
4370 if (state == nothing_seen)
4371 state = symbol_seen;
4375 if (cache->func == NULL)
4379 *filename_ptr = cache->filename;
4380 if (functionname_ptr)
4381 *functionname_ptr = bfd_asymbol_name (cache->func);