2 Copyright (C) 1994-2017 Free Software Foundation, Inc.
4 Adapted from gdb/dwarf2read.c by Gavin Koch of Cygnus Solutions
7 From the dwarf2read.c header:
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 original bfd for which debug was loaded. This is what
104 we use to compare and so check that the cached debug data is still
105 valid - it saves having to possibly dereference the gnu_debuglink each
109 /* Pointer to the bfd, section and address of the beginning of the
110 section. The bfd might be different than expected because of
111 gnu_debuglink sections. */
114 bfd_byte *sec_info_ptr;
116 /* Support for alternate debug info sections created by the DWZ utility:
117 This includes a pointer to an alternate bfd which contains *extra*,
118 possibly duplicate debug sections, and pointers to the loaded
119 .debug_str and .debug_info sections from this bfd. */
121 bfd_byte * alt_dwarf_str_buffer;
122 bfd_size_type alt_dwarf_str_size;
123 bfd_byte * alt_dwarf_info_buffer;
124 bfd_size_type alt_dwarf_info_size;
126 /* A pointer to the memory block allocated for info_ptr. Neither
127 info_ptr nor sec_info_ptr are guaranteed to stay pointing to the
128 beginning of the malloc block. This is used only to free the
130 bfd_byte *info_ptr_memory;
132 /* Pointer to the symbol table. */
135 /* Pointer to the .debug_abbrev section loaded into memory. */
136 bfd_byte *dwarf_abbrev_buffer;
138 /* Length of the loaded .debug_abbrev section. */
139 bfd_size_type dwarf_abbrev_size;
141 /* Buffer for decode_line_info. */
142 bfd_byte *dwarf_line_buffer;
144 /* Length of the loaded .debug_line section. */
145 bfd_size_type dwarf_line_size;
147 /* Pointer to the .debug_str section loaded into memory. */
148 bfd_byte *dwarf_str_buffer;
150 /* Length of the loaded .debug_str section. */
151 bfd_size_type dwarf_str_size;
153 /* Pointer to the .debug_line_str section loaded into memory. */
154 bfd_byte *dwarf_line_str_buffer;
156 /* Length of the loaded .debug_line_str section. */
157 bfd_size_type dwarf_line_str_size;
159 /* Pointer to the .debug_ranges section loaded into memory. */
160 bfd_byte *dwarf_ranges_buffer;
162 /* Length of the loaded .debug_ranges section. */
163 bfd_size_type dwarf_ranges_size;
165 /* If the most recent call to bfd_find_nearest_line was given an
166 address in an inlined function, preserve a pointer into the
167 calling chain for subsequent calls to bfd_find_inliner_info to
169 struct funcinfo *inliner_chain;
171 /* Section VMAs at the time the stash was built. */
174 /* Number of sections whose VMA we must adjust. */
175 int adjusted_section_count;
177 /* Array of sections with adjusted VMA. */
178 struct adjusted_section *adjusted_sections;
180 /* Number of times find_line is called. This is used in
181 the heuristic for enabling the info hash tables. */
184 #define STASH_INFO_HASH_TRIGGER 100
186 /* Hash table mapping symbol names to function infos. */
187 struct info_hash_table *funcinfo_hash_table;
189 /* Hash table mapping symbol names to variable infos. */
190 struct info_hash_table *varinfo_hash_table;
192 /* Head of comp_unit list in the last hash table update. */
193 struct comp_unit *hash_units_head;
195 /* Status of info hash. */
196 int info_hash_status;
197 #define STASH_INFO_HASH_OFF 0
198 #define STASH_INFO_HASH_ON 1
199 #define STASH_INFO_HASH_DISABLED 2
201 /* True if we opened bfd_ptr. */
202 bfd_boolean close_on_cleanup;
212 /* A minimal decoding of DWARF2 compilation units. We only decode
213 what's needed to get to the line number information. */
217 /* Chain the previously read compilation units. */
218 struct comp_unit *next_unit;
220 /* Likewise, chain the compilation unit read after this one.
221 The comp units are stored in reversed reading order. */
222 struct comp_unit *prev_unit;
224 /* Keep the bfd convenient (for memory allocation). */
227 /* The lowest and highest addresses contained in this compilation
228 unit as specified in the compilation unit header. */
229 struct arange arange;
231 /* The DW_AT_name attribute (for error messages). */
234 /* The abbrev hash table. */
235 struct abbrev_info **abbrevs;
237 /* DW_AT_language. */
240 /* Note that an error was found by comp_unit_find_nearest_line. */
243 /* The DW_AT_comp_dir attribute. */
246 /* TRUE if there is a line number table associated with this comp. unit. */
249 /* Pointer to the current comp_unit so that we can find a given entry
251 bfd_byte *info_ptr_unit;
253 /* Pointer to the start of the debug section, for DW_FORM_ref_addr. */
254 bfd_byte *sec_info_ptr;
256 /* The offset into .debug_line of the line number table. */
257 unsigned long line_offset;
259 /* Pointer to the first child die for the comp unit. */
260 bfd_byte *first_child_die_ptr;
262 /* The end of the comp unit. */
265 /* The decoded line number, NULL if not yet decoded. */
266 struct line_info_table *line_table;
268 /* A list of the functions found in this comp. unit. */
269 struct funcinfo *function_table;
271 /* A table of function information references searchable by address. */
272 struct lookup_funcinfo *lookup_funcinfo_table;
274 /* Number of functions in the function_table and sorted_function_table. */
275 bfd_size_type number_of_functions;
277 /* A list of the variables found in this comp. unit. */
278 struct varinfo *variable_table;
280 /* Pointer to dwarf2_debug structure. */
281 struct dwarf2_debug *stash;
283 /* DWARF format version for this unit - from unit header. */
286 /* Address size for this unit - from unit header. */
287 unsigned char addr_size;
289 /* Offset size for this unit - from unit header. */
290 unsigned char offset_size;
292 /* Base address for this unit - from DW_AT_low_pc attribute of
293 DW_TAG_compile_unit DIE */
294 bfd_vma base_address;
296 /* TRUE if symbols are cached in hash table for faster lookup by name. */
300 /* This data structure holds the information of an abbrev. */
303 unsigned int number; /* Number identifying abbrev. */
304 enum dwarf_tag tag; /* DWARF tag. */
305 int has_children; /* Boolean. */
306 unsigned int num_attrs; /* Number of attributes. */
307 struct attr_abbrev *attrs; /* An array of attribute descriptions. */
308 struct abbrev_info *next; /* Next in chain. */
313 enum dwarf_attribute name;
314 enum dwarf_form form;
315 bfd_vma implicit_const;
318 /* Map of uncompressed DWARF debug section name to compressed one. It
319 is terminated by NULL uncompressed_name. */
321 const struct dwarf_debug_section dwarf_debug_sections[] =
323 { ".debug_abbrev", ".zdebug_abbrev" },
324 { ".debug_aranges", ".zdebug_aranges" },
325 { ".debug_frame", ".zdebug_frame" },
326 { ".debug_info", ".zdebug_info" },
327 { ".debug_info", ".zdebug_info" },
328 { ".debug_line", ".zdebug_line" },
329 { ".debug_loc", ".zdebug_loc" },
330 { ".debug_macinfo", ".zdebug_macinfo" },
331 { ".debug_macro", ".zdebug_macro" },
332 { ".debug_pubnames", ".zdebug_pubnames" },
333 { ".debug_pubtypes", ".zdebug_pubtypes" },
334 { ".debug_ranges", ".zdebug_ranges" },
335 { ".debug_static_func", ".zdebug_static_func" },
336 { ".debug_static_vars", ".zdebug_static_vars" },
337 { ".debug_str", ".zdebug_str", },
338 { ".debug_str", ".zdebug_str", },
339 { ".debug_line_str", ".zdebug_line_str", },
340 { ".debug_types", ".zdebug_types" },
341 /* GNU DWARF 1 extensions */
342 { ".debug_sfnames", ".zdebug_sfnames" },
343 { ".debug_srcinfo", ".zebug_srcinfo" },
344 /* SGI/MIPS DWARF 2 extensions */
345 { ".debug_funcnames", ".zdebug_funcnames" },
346 { ".debug_typenames", ".zdebug_typenames" },
347 { ".debug_varnames", ".zdebug_varnames" },
348 { ".debug_weaknames", ".zdebug_weaknames" },
352 /* NB/ Numbers in this enum must match up with indicies
353 into the dwarf_debug_sections[] array above. */
354 enum dwarf_debug_section_enum
383 /* A static assertion. */
384 extern int dwarf_debug_section_assert[ARRAY_SIZE (dwarf_debug_sections)
385 == debug_max + 1 ? 1 : -1];
387 #ifndef ABBREV_HASH_SIZE
388 #define ABBREV_HASH_SIZE 121
390 #ifndef ATTR_ALLOC_CHUNK
391 #define ATTR_ALLOC_CHUNK 4
394 /* Variable and function hash tables. This is used to speed up look-up
395 in lookup_symbol_in_var_table() and lookup_symbol_in_function_table().
396 In order to share code between variable and function infos, we use
397 a list of untyped pointer for all variable/function info associated with
398 a symbol. We waste a bit of memory for list with one node but that
399 simplifies the code. */
401 struct info_list_node
403 struct info_list_node *next;
407 /* Info hash entry. */
408 struct info_hash_entry
410 struct bfd_hash_entry root;
411 struct info_list_node *head;
414 struct info_hash_table
416 struct bfd_hash_table base;
419 /* Function to create a new entry in info hash table. */
421 static struct bfd_hash_entry *
422 info_hash_table_newfunc (struct bfd_hash_entry *entry,
423 struct bfd_hash_table *table,
426 struct info_hash_entry *ret = (struct info_hash_entry *) entry;
428 /* Allocate the structure if it has not already been allocated by a
432 ret = (struct info_hash_entry *) bfd_hash_allocate (table,
438 /* Call the allocation method of the base class. */
439 ret = ((struct info_hash_entry *)
440 bfd_hash_newfunc ((struct bfd_hash_entry *) ret, table, string));
442 /* Initialize the local fields here. */
446 return (struct bfd_hash_entry *) ret;
449 /* Function to create a new info hash table. It returns a pointer to the
450 newly created table or NULL if there is any error. We need abfd
451 solely for memory allocation. */
453 static struct info_hash_table *
454 create_info_hash_table (bfd *abfd)
456 struct info_hash_table *hash_table;
458 hash_table = ((struct info_hash_table *)
459 bfd_alloc (abfd, sizeof (struct info_hash_table)));
463 if (!bfd_hash_table_init (&hash_table->base, info_hash_table_newfunc,
464 sizeof (struct info_hash_entry)))
466 bfd_release (abfd, hash_table);
473 /* Insert an info entry into an info hash table. We do not check of
474 duplicate entries. Also, the caller need to guarantee that the
475 right type of info in inserted as info is passed as a void* pointer.
476 This function returns true if there is no error. */
479 insert_info_hash_table (struct info_hash_table *hash_table,
484 struct info_hash_entry *entry;
485 struct info_list_node *node;
487 entry = (struct info_hash_entry*) bfd_hash_lookup (&hash_table->base,
492 node = (struct info_list_node *) bfd_hash_allocate (&hash_table->base,
498 node->next = entry->head;
504 /* Look up an info entry list from an info hash table. Return NULL
507 static struct info_list_node *
508 lookup_info_hash_table (struct info_hash_table *hash_table, const char *key)
510 struct info_hash_entry *entry;
512 entry = (struct info_hash_entry*) bfd_hash_lookup (&hash_table->base, key,
514 return entry ? entry->head : NULL;
517 /* Read a section into its appropriate place in the dwarf2_debug
518 struct (indicated by SECTION_BUFFER and SECTION_SIZE). If SYMS is
519 not NULL, use bfd_simple_get_relocated_section_contents to read the
520 section contents, otherwise use bfd_get_section_contents. Fail if
521 the located section does not contain at least OFFSET bytes. */
524 read_section (bfd * abfd,
525 const struct dwarf_debug_section *sec,
528 bfd_byte ** section_buffer,
529 bfd_size_type * section_size)
532 const char *section_name = sec->uncompressed_name;
534 /* The section may have already been read. */
535 if (*section_buffer == NULL)
537 msec = bfd_get_section_by_name (abfd, section_name);
540 section_name = sec->compressed_name;
541 if (section_name != NULL)
542 msec = bfd_get_section_by_name (abfd, section_name);
546 _bfd_error_handler (_("Dwarf Error: Can't find %s section."),
547 sec->uncompressed_name);
548 bfd_set_error (bfd_error_bad_value);
552 *section_size = msec->rawsize ? msec->rawsize : msec->size;
556 = bfd_simple_get_relocated_section_contents (abfd, msec, NULL, syms);
557 if (! *section_buffer)
562 *section_buffer = (bfd_byte *) bfd_malloc (*section_size);
563 if (! *section_buffer)
565 if (! bfd_get_section_contents (abfd, msec, *section_buffer,
571 /* It is possible to get a bad value for the offset into the section
572 that the client wants. Validate it here to avoid trouble later. */
573 if (offset != 0 && offset >= *section_size)
575 /* xgettext: c-format */
576 _bfd_error_handler (_("Dwarf Error: Offset (%llu)"
577 " greater than or equal to %s size (%Lu)."),
578 (long long) offset, section_name, *section_size);
579 bfd_set_error (bfd_error_bad_value);
586 /* Read dwarf information from a buffer. */
589 read_1_byte (bfd *abfd ATTRIBUTE_UNUSED, bfd_byte *buf, bfd_byte *end)
593 return bfd_get_8 (abfd, buf);
597 read_1_signed_byte (bfd *abfd ATTRIBUTE_UNUSED, bfd_byte *buf, bfd_byte *end)
601 return bfd_get_signed_8 (abfd, buf);
605 read_2_bytes (bfd *abfd, bfd_byte *buf, bfd_byte *end)
609 return bfd_get_16 (abfd, buf);
613 read_4_bytes (bfd *abfd, bfd_byte *buf, bfd_byte *end)
617 return bfd_get_32 (abfd, buf);
621 read_8_bytes (bfd *abfd, bfd_byte *buf, bfd_byte *end)
625 return bfd_get_64 (abfd, buf);
629 read_n_bytes (bfd *abfd ATTRIBUTE_UNUSED,
632 unsigned int size ATTRIBUTE_UNUSED)
634 if (buf + size > end)
639 /* Scans a NUL terminated string starting at BUF, returning a pointer to it.
640 Returns the number of characters in the string, *including* the NUL byte,
641 in BYTES_READ_PTR. This value is set even if the function fails. Bytes
642 at or beyond BUF_END will not be read. Returns NULL if there was a
643 problem, or if the string is empty. */
646 read_string (bfd * abfd ATTRIBUTE_UNUSED,
649 unsigned int * bytes_read_ptr)
655 * bytes_read_ptr = 0;
661 * bytes_read_ptr = 1;
665 while (buf < buf_end)
668 * bytes_read_ptr = buf - str;
672 * bytes_read_ptr = buf - str;
676 /* Reads an offset from BUF and then locates the string at this offset
677 inside the debug string section. Returns a pointer to the string.
678 Returns the number of bytes read from BUF, *not* the length of the string,
679 in BYTES_READ_PTR. This value is set even if the function fails. Bytes
680 at or beyond BUF_END will not be read from BUF. Returns NULL if there was
681 a problem, or if the string is empty. Does not check for NUL termination
685 read_indirect_string (struct comp_unit * unit,
688 unsigned int * bytes_read_ptr)
691 struct dwarf2_debug *stash = unit->stash;
694 if (buf + unit->offset_size > buf_end)
696 * bytes_read_ptr = 0;
700 if (unit->offset_size == 4)
701 offset = read_4_bytes (unit->abfd, buf, buf_end);
703 offset = read_8_bytes (unit->abfd, buf, buf_end);
705 *bytes_read_ptr = unit->offset_size;
707 if (! read_section (unit->abfd, &stash->debug_sections[debug_str],
709 &stash->dwarf_str_buffer, &stash->dwarf_str_size))
712 if (offset >= stash->dwarf_str_size)
714 str = (char *) stash->dwarf_str_buffer + offset;
720 /* Like read_indirect_string but from .debug_line_str section. */
723 read_indirect_line_string (struct comp_unit * unit,
726 unsigned int * bytes_read_ptr)
729 struct dwarf2_debug *stash = unit->stash;
732 if (buf + unit->offset_size > buf_end)
734 * bytes_read_ptr = 0;
738 if (unit->offset_size == 4)
739 offset = read_4_bytes (unit->abfd, buf, buf_end);
741 offset = read_8_bytes (unit->abfd, buf, buf_end);
743 *bytes_read_ptr = unit->offset_size;
745 if (! read_section (unit->abfd, &stash->debug_sections[debug_line_str],
747 &stash->dwarf_line_str_buffer,
748 &stash->dwarf_line_str_size))
751 if (offset >= stash->dwarf_line_str_size)
753 str = (char *) stash->dwarf_line_str_buffer + offset;
759 /* Like read_indirect_string but uses a .debug_str located in
760 an alternate file pointed to by the .gnu_debugaltlink section.
761 Used to impement DW_FORM_GNU_strp_alt. */
764 read_alt_indirect_string (struct comp_unit * unit,
767 unsigned int * bytes_read_ptr)
770 struct dwarf2_debug *stash = unit->stash;
773 if (buf + unit->offset_size > buf_end)
775 * bytes_read_ptr = 0;
779 if (unit->offset_size == 4)
780 offset = read_4_bytes (unit->abfd, buf, buf_end);
782 offset = read_8_bytes (unit->abfd, buf, buf_end);
784 *bytes_read_ptr = unit->offset_size;
786 if (stash->alt_bfd_ptr == NULL)
789 char * debug_filename = bfd_follow_gnu_debugaltlink (unit->abfd, DEBUGDIR);
791 if (debug_filename == NULL)
794 if ((debug_bfd = bfd_openr (debug_filename, NULL)) == NULL
795 || ! bfd_check_format (debug_bfd, bfd_object))
798 bfd_close (debug_bfd);
800 /* FIXME: Should we report our failure to follow the debuglink ? */
801 free (debug_filename);
804 stash->alt_bfd_ptr = debug_bfd;
807 if (! read_section (unit->stash->alt_bfd_ptr,
808 stash->debug_sections + debug_str_alt,
809 NULL, /* FIXME: Do we need to load alternate symbols ? */
811 &stash->alt_dwarf_str_buffer,
812 &stash->alt_dwarf_str_size))
815 if (offset >= stash->alt_dwarf_str_size)
817 str = (char *) stash->alt_dwarf_str_buffer + offset;
824 /* Resolve an alternate reference from UNIT at OFFSET.
825 Returns a pointer into the loaded alternate CU upon success
826 or NULL upon failure. */
829 read_alt_indirect_ref (struct comp_unit * unit,
832 struct dwarf2_debug *stash = unit->stash;
834 if (stash->alt_bfd_ptr == NULL)
837 char * debug_filename = bfd_follow_gnu_debugaltlink (unit->abfd, DEBUGDIR);
839 if (debug_filename == NULL)
842 if ((debug_bfd = bfd_openr (debug_filename, NULL)) == NULL
843 || ! bfd_check_format (debug_bfd, bfd_object))
846 bfd_close (debug_bfd);
848 /* FIXME: Should we report our failure to follow the debuglink ? */
849 free (debug_filename);
852 stash->alt_bfd_ptr = debug_bfd;
855 if (! read_section (unit->stash->alt_bfd_ptr,
856 stash->debug_sections + debug_info_alt,
857 NULL, /* FIXME: Do we need to load alternate symbols ? */
859 &stash->alt_dwarf_info_buffer,
860 &stash->alt_dwarf_info_size))
863 if (offset >= stash->alt_dwarf_info_size)
865 return stash->alt_dwarf_info_buffer + offset;
869 read_address (struct comp_unit *unit, bfd_byte *buf, bfd_byte * buf_end)
873 if (bfd_get_flavour (unit->abfd) == bfd_target_elf_flavour)
874 signed_vma = get_elf_backend_data (unit->abfd)->sign_extend_vma;
876 if (buf + unit->addr_size > buf_end)
881 switch (unit->addr_size)
884 return bfd_get_signed_64 (unit->abfd, buf);
886 return bfd_get_signed_32 (unit->abfd, buf);
888 return bfd_get_signed_16 (unit->abfd, buf);
895 switch (unit->addr_size)
898 return bfd_get_64 (unit->abfd, buf);
900 return bfd_get_32 (unit->abfd, buf);
902 return bfd_get_16 (unit->abfd, buf);
909 /* Lookup an abbrev_info structure in the abbrev hash table. */
911 static struct abbrev_info *
912 lookup_abbrev (unsigned int number, struct abbrev_info **abbrevs)
914 unsigned int hash_number;
915 struct abbrev_info *abbrev;
917 hash_number = number % ABBREV_HASH_SIZE;
918 abbrev = abbrevs[hash_number];
922 if (abbrev->number == number)
925 abbrev = abbrev->next;
931 /* In DWARF version 2, the description of the debugging information is
932 stored in a separate .debug_abbrev section. Before we read any
933 dies from a section we read in all abbreviations and install them
936 static struct abbrev_info**
937 read_abbrevs (bfd *abfd, bfd_uint64_t offset, struct dwarf2_debug *stash)
939 struct abbrev_info **abbrevs;
940 bfd_byte *abbrev_ptr;
941 bfd_byte *abbrev_end;
942 struct abbrev_info *cur_abbrev;
943 unsigned int abbrev_number, bytes_read, abbrev_name;
944 unsigned int abbrev_form, hash_number;
947 if (! read_section (abfd, &stash->debug_sections[debug_abbrev],
949 &stash->dwarf_abbrev_buffer, &stash->dwarf_abbrev_size))
952 if (offset >= stash->dwarf_abbrev_size)
955 amt = sizeof (struct abbrev_info*) * ABBREV_HASH_SIZE;
956 abbrevs = (struct abbrev_info **) bfd_zalloc (abfd, amt);
960 abbrev_ptr = stash->dwarf_abbrev_buffer + offset;
961 abbrev_end = stash->dwarf_abbrev_buffer + stash->dwarf_abbrev_size;
962 abbrev_number = _bfd_safe_read_leb128 (abfd, abbrev_ptr, &bytes_read,
964 abbrev_ptr += bytes_read;
966 /* Loop until we reach an abbrev number of 0. */
967 while (abbrev_number)
969 amt = sizeof (struct abbrev_info);
970 cur_abbrev = (struct abbrev_info *) bfd_zalloc (abfd, amt);
971 if (cur_abbrev == NULL)
974 /* Read in abbrev header. */
975 cur_abbrev->number = abbrev_number;
976 cur_abbrev->tag = (enum dwarf_tag)
977 _bfd_safe_read_leb128 (abfd, abbrev_ptr, &bytes_read,
979 abbrev_ptr += bytes_read;
980 cur_abbrev->has_children = read_1_byte (abfd, abbrev_ptr, abbrev_end);
983 /* Now read in declarations. */
986 /* Initialize it just to avoid a GCC false warning. */
987 bfd_vma implicit_const = -1;
989 abbrev_name = _bfd_safe_read_leb128 (abfd, abbrev_ptr, &bytes_read,
991 abbrev_ptr += bytes_read;
992 abbrev_form = _bfd_safe_read_leb128 (abfd, abbrev_ptr, &bytes_read,
994 abbrev_ptr += bytes_read;
995 if (abbrev_form == DW_FORM_implicit_const)
997 implicit_const = _bfd_safe_read_leb128 (abfd, abbrev_ptr,
1000 abbrev_ptr += bytes_read;
1003 if (abbrev_name == 0)
1006 if ((cur_abbrev->num_attrs % ATTR_ALLOC_CHUNK) == 0)
1008 struct attr_abbrev *tmp;
1010 amt = cur_abbrev->num_attrs + ATTR_ALLOC_CHUNK;
1011 amt *= sizeof (struct attr_abbrev);
1012 tmp = (struct attr_abbrev *) bfd_realloc (cur_abbrev->attrs, amt);
1017 for (i = 0; i < ABBREV_HASH_SIZE; i++)
1019 struct abbrev_info *abbrev = abbrevs[i];
1023 free (abbrev->attrs);
1024 abbrev = abbrev->next;
1029 cur_abbrev->attrs = tmp;
1032 cur_abbrev->attrs[cur_abbrev->num_attrs].name
1033 = (enum dwarf_attribute) abbrev_name;
1034 cur_abbrev->attrs[cur_abbrev->num_attrs].form
1035 = (enum dwarf_form) abbrev_form;
1036 cur_abbrev->attrs[cur_abbrev->num_attrs].implicit_const
1038 ++cur_abbrev->num_attrs;
1041 hash_number = abbrev_number % ABBREV_HASH_SIZE;
1042 cur_abbrev->next = abbrevs[hash_number];
1043 abbrevs[hash_number] = cur_abbrev;
1045 /* Get next abbreviation.
1046 Under Irix6 the abbreviations for a compilation unit are not
1047 always properly terminated with an abbrev number of 0.
1048 Exit loop if we encounter an abbreviation which we have
1049 already read (which means we are about to read the abbreviations
1050 for the next compile unit) or if the end of the abbreviation
1051 table is reached. */
1052 if ((unsigned int) (abbrev_ptr - stash->dwarf_abbrev_buffer)
1053 >= stash->dwarf_abbrev_size)
1055 abbrev_number = _bfd_safe_read_leb128 (abfd, abbrev_ptr,
1056 &bytes_read, FALSE, abbrev_end);
1057 abbrev_ptr += bytes_read;
1058 if (lookup_abbrev (abbrev_number, abbrevs) != NULL)
1065 /* Returns true if the form is one which has a string value. */
1067 static inline bfd_boolean
1068 is_str_attr (enum dwarf_form form)
1070 return (form == DW_FORM_string || form == DW_FORM_strp
1071 || form == DW_FORM_line_strp || form == DW_FORM_GNU_strp_alt);
1074 /* Read and fill in the value of attribute ATTR as described by FORM.
1075 Read data starting from INFO_PTR, but never at or beyond INFO_PTR_END.
1076 Returns an updated INFO_PTR taking into account the amount of data read. */
1079 read_attribute_value (struct attribute * attr,
1081 bfd_vma implicit_const,
1082 struct comp_unit * unit,
1083 bfd_byte * info_ptr,
1084 bfd_byte * info_ptr_end)
1086 bfd *abfd = unit->abfd;
1087 unsigned int bytes_read;
1088 struct dwarf_block *blk;
1091 if (info_ptr >= info_ptr_end && form != DW_FORM_flag_present)
1093 _bfd_error_handler (_("Dwarf Error: Info pointer extends beyond end of attributes"));
1094 bfd_set_error (bfd_error_bad_value);
1098 attr->form = (enum dwarf_form) form;
1102 case DW_FORM_ref_addr:
1103 /* DW_FORM_ref_addr is an address in DWARF2, and an offset in
1105 if (unit->version == 3 || unit->version == 4)
1107 if (unit->offset_size == 4)
1108 attr->u.val = read_4_bytes (unit->abfd, info_ptr, info_ptr_end);
1110 attr->u.val = read_8_bytes (unit->abfd, info_ptr, info_ptr_end);
1111 info_ptr += unit->offset_size;
1116 attr->u.val = read_address (unit, info_ptr, info_ptr_end);
1117 info_ptr += unit->addr_size;
1119 case DW_FORM_GNU_ref_alt:
1120 case DW_FORM_sec_offset:
1121 if (unit->offset_size == 4)
1122 attr->u.val = read_4_bytes (unit->abfd, info_ptr, info_ptr_end);
1124 attr->u.val = read_8_bytes (unit->abfd, info_ptr, info_ptr_end);
1125 info_ptr += unit->offset_size;
1127 case DW_FORM_block2:
1128 amt = sizeof (struct dwarf_block);
1129 blk = (struct dwarf_block *) bfd_alloc (abfd, amt);
1132 blk->size = read_2_bytes (abfd, info_ptr, info_ptr_end);
1134 blk->data = read_n_bytes (abfd, info_ptr, info_ptr_end, blk->size);
1135 info_ptr += blk->size;
1138 case DW_FORM_block4:
1139 amt = sizeof (struct dwarf_block);
1140 blk = (struct dwarf_block *) bfd_alloc (abfd, amt);
1143 blk->size = read_4_bytes (abfd, info_ptr, info_ptr_end);
1145 blk->data = read_n_bytes (abfd, info_ptr, info_ptr_end, blk->size);
1146 info_ptr += blk->size;
1150 attr->u.val = read_2_bytes (abfd, info_ptr, info_ptr_end);
1154 attr->u.val = read_4_bytes (abfd, info_ptr, info_ptr_end);
1158 attr->u.val = read_8_bytes (abfd, info_ptr, info_ptr_end);
1161 case DW_FORM_string:
1162 attr->u.str = read_string (abfd, info_ptr, info_ptr_end, &bytes_read);
1163 info_ptr += bytes_read;
1166 attr->u.str = read_indirect_string (unit, info_ptr, info_ptr_end, &bytes_read);
1167 info_ptr += bytes_read;
1169 case DW_FORM_line_strp:
1170 attr->u.str = read_indirect_line_string (unit, info_ptr, info_ptr_end, &bytes_read);
1171 info_ptr += bytes_read;
1173 case DW_FORM_GNU_strp_alt:
1174 attr->u.str = read_alt_indirect_string (unit, info_ptr, info_ptr_end, &bytes_read);
1175 info_ptr += bytes_read;
1177 case DW_FORM_exprloc:
1179 amt = sizeof (struct dwarf_block);
1180 blk = (struct dwarf_block *) bfd_alloc (abfd, amt);
1183 blk->size = _bfd_safe_read_leb128 (abfd, info_ptr, &bytes_read,
1184 FALSE, info_ptr_end);
1185 info_ptr += bytes_read;
1186 blk->data = read_n_bytes (abfd, info_ptr, info_ptr_end, blk->size);
1187 info_ptr += blk->size;
1190 case DW_FORM_block1:
1191 amt = sizeof (struct dwarf_block);
1192 blk = (struct dwarf_block *) bfd_alloc (abfd, amt);
1195 blk->size = read_1_byte (abfd, info_ptr, info_ptr_end);
1197 blk->data = read_n_bytes (abfd, info_ptr, info_ptr_end, blk->size);
1198 info_ptr += blk->size;
1202 attr->u.val = read_1_byte (abfd, info_ptr, info_ptr_end);
1206 attr->u.val = read_1_byte (abfd, info_ptr, info_ptr_end);
1209 case DW_FORM_flag_present:
1213 attr->u.sval = _bfd_safe_read_leb128 (abfd, info_ptr, &bytes_read,
1214 TRUE, info_ptr_end);
1215 info_ptr += bytes_read;
1218 attr->u.val = _bfd_safe_read_leb128 (abfd, info_ptr, &bytes_read,
1219 FALSE, info_ptr_end);
1220 info_ptr += bytes_read;
1223 attr->u.val = read_1_byte (abfd, info_ptr, info_ptr_end);
1227 attr->u.val = read_2_bytes (abfd, info_ptr, info_ptr_end);
1231 attr->u.val = read_4_bytes (abfd, info_ptr, info_ptr_end);
1235 attr->u.val = read_8_bytes (abfd, info_ptr, info_ptr_end);
1238 case DW_FORM_ref_sig8:
1239 attr->u.val = read_8_bytes (abfd, info_ptr, info_ptr_end);
1242 case DW_FORM_ref_udata:
1243 attr->u.val = _bfd_safe_read_leb128 (abfd, info_ptr, &bytes_read,
1244 FALSE, info_ptr_end);
1245 info_ptr += bytes_read;
1247 case DW_FORM_indirect:
1248 form = _bfd_safe_read_leb128 (abfd, info_ptr, &bytes_read,
1249 FALSE, info_ptr_end);
1250 info_ptr += bytes_read;
1251 if (form == DW_FORM_implicit_const)
1253 implicit_const = _bfd_safe_read_leb128 (abfd, info_ptr, &bytes_read,
1254 TRUE, info_ptr_end);
1255 info_ptr += bytes_read;
1257 info_ptr = read_attribute_value (attr, form, implicit_const, unit,
1258 info_ptr, info_ptr_end);
1260 case DW_FORM_implicit_const:
1261 attr->form = DW_FORM_sdata;
1262 attr->u.sval = implicit_const;
1265 _bfd_error_handler (_("Dwarf Error: Invalid or unhandled FORM value: %#x."),
1267 bfd_set_error (bfd_error_bad_value);
1273 /* Read an attribute described by an abbreviated attribute. */
1276 read_attribute (struct attribute * attr,
1277 struct attr_abbrev * abbrev,
1278 struct comp_unit * unit,
1279 bfd_byte * info_ptr,
1280 bfd_byte * info_ptr_end)
1282 attr->name = abbrev->name;
1283 info_ptr = read_attribute_value (attr, abbrev->form, abbrev->implicit_const,
1284 unit, info_ptr, info_ptr_end);
1288 /* Return whether DW_AT_name will return the same as DW_AT_linkage_name
1292 non_mangled (int lang)
1302 case DW_LANG_Cobol74:
1303 case DW_LANG_Cobol85:
1304 case DW_LANG_Fortran77:
1305 case DW_LANG_Pascal83:
1315 /* Source line information table routines. */
1317 #define FILE_ALLOC_CHUNK 5
1318 #define DIR_ALLOC_CHUNK 5
1322 struct line_info * prev_line;
1326 unsigned int column;
1327 unsigned int discriminator;
1328 unsigned char op_index;
1329 unsigned char end_sequence; /* End of (sequential) code sequence. */
1340 struct line_sequence
1343 struct line_sequence* prev_sequence;
1344 struct line_info* last_line; /* Largest VMA. */
1345 struct line_info** line_info_lookup;
1346 bfd_size_type num_lines;
1349 struct line_info_table
1352 unsigned int num_files;
1353 unsigned int num_dirs;
1354 unsigned int num_sequences;
1357 struct fileinfo* files;
1358 struct line_sequence* sequences;
1359 struct line_info* lcl_head; /* Local head; used in 'add_line_info'. */
1362 /* Remember some information about each function. If the function is
1363 inlined (DW_TAG_inlined_subroutine) it may have two additional
1364 attributes, DW_AT_call_file and DW_AT_call_line, which specify the
1365 source code location where this function was inlined. */
1369 /* Pointer to previous function in list of all functions. */
1370 struct funcinfo * prev_func;
1371 /* Pointer to function one scope higher. */
1372 struct funcinfo * caller_func;
1373 /* Source location file name where caller_func inlines this func. */
1375 /* Source location file name. */
1377 /* Source location line number where caller_func inlines this func. */
1379 /* Source location line number. */
1382 bfd_boolean is_linkage;
1384 struct arange arange;
1385 /* Where the symbol is defined. */
1389 struct lookup_funcinfo
1391 /* Function information corresponding to this lookup table entry. */
1392 struct funcinfo * funcinfo;
1394 /* The lowest address for this specific function. */
1397 /* The highest address of this function before the lookup table is sorted.
1398 The highest address of all prior functions after the lookup table is
1399 sorted, which is used for binary search. */
1405 /* Pointer to previous variable in list of all variables */
1406 struct varinfo *prev_var;
1407 /* Source location file name */
1409 /* Source location line number */
1414 /* Where the symbol is defined */
1416 /* Is this a stack variable? */
1417 unsigned int stack: 1;
1420 /* Return TRUE if NEW_LINE should sort after LINE. */
1422 static inline bfd_boolean
1423 new_line_sorts_after (struct line_info *new_line, struct line_info *line)
1425 return (new_line->address > line->address
1426 || (new_line->address == line->address
1427 && (new_line->op_index > line->op_index
1428 || (new_line->op_index == line->op_index
1429 && new_line->end_sequence < line->end_sequence))));
1433 /* Adds a new entry to the line_info list in the line_info_table, ensuring
1434 that the list is sorted. Note that the line_info list is sorted from
1435 highest to lowest VMA (with possible duplicates); that is,
1436 line_info->prev_line always accesses an equal or smaller VMA. */
1439 add_line_info (struct line_info_table *table,
1441 unsigned char op_index,
1444 unsigned int column,
1445 unsigned int discriminator,
1448 bfd_size_type amt = sizeof (struct line_info);
1449 struct line_sequence* seq = table->sequences;
1450 struct line_info* info = (struct line_info *) bfd_alloc (table->abfd, amt);
1455 /* Set member data of 'info'. */
1456 info->prev_line = NULL;
1457 info->address = address;
1458 info->op_index = op_index;
1460 info->column = column;
1461 info->discriminator = discriminator;
1462 info->end_sequence = end_sequence;
1464 if (filename && filename[0])
1466 info->filename = (char *) bfd_alloc (table->abfd, strlen (filename) + 1);
1467 if (info->filename == NULL)
1469 strcpy (info->filename, filename);
1472 info->filename = NULL;
1474 /* Find the correct location for 'info'. Normally we will receive
1475 new line_info data 1) in order and 2) with increasing VMAs.
1476 However some compilers break the rules (cf. decode_line_info) and
1477 so we include some heuristics for quickly finding the correct
1478 location for 'info'. In particular, these heuristics optimize for
1479 the common case in which the VMA sequence that we receive is a
1480 list of locally sorted VMAs such as
1481 p...z a...j (where a < j < p < z)
1483 Note: table->lcl_head is used to head an *actual* or *possible*
1484 sub-sequence within the list (such as a...j) that is not directly
1485 headed by table->last_line
1487 Note: we may receive duplicate entries from 'decode_line_info'. */
1490 && seq->last_line->address == address
1491 && seq->last_line->op_index == op_index
1492 && seq->last_line->end_sequence == end_sequence)
1494 /* We only keep the last entry with the same address and end
1495 sequence. See PR ld/4986. */
1496 if (table->lcl_head == seq->last_line)
1497 table->lcl_head = info;
1498 info->prev_line = seq->last_line->prev_line;
1499 seq->last_line = info;
1501 else if (!seq || seq->last_line->end_sequence)
1503 /* Start a new line sequence. */
1504 amt = sizeof (struct line_sequence);
1505 seq = (struct line_sequence *) bfd_malloc (amt);
1508 seq->low_pc = address;
1509 seq->prev_sequence = table->sequences;
1510 seq->last_line = info;
1511 table->lcl_head = info;
1512 table->sequences = seq;
1513 table->num_sequences++;
1515 else if (new_line_sorts_after (info, seq->last_line))
1517 /* Normal case: add 'info' to the beginning of the current sequence. */
1518 info->prev_line = seq->last_line;
1519 seq->last_line = info;
1521 /* lcl_head: initialize to head a *possible* sequence at the end. */
1522 if (!table->lcl_head)
1523 table->lcl_head = info;
1525 else if (!new_line_sorts_after (info, table->lcl_head)
1526 && (!table->lcl_head->prev_line
1527 || new_line_sorts_after (info, table->lcl_head->prev_line)))
1529 /* Abnormal but easy: lcl_head is the head of 'info'. */
1530 info->prev_line = table->lcl_head->prev_line;
1531 table->lcl_head->prev_line = info;
1535 /* Abnormal and hard: Neither 'last_line' nor 'lcl_head'
1536 are valid heads for 'info'. Reset 'lcl_head'. */
1537 struct line_info* li2 = seq->last_line; /* Always non-NULL. */
1538 struct line_info* li1 = li2->prev_line;
1542 if (!new_line_sorts_after (info, li2)
1543 && new_line_sorts_after (info, li1))
1546 li2 = li1; /* always non-NULL */
1547 li1 = li1->prev_line;
1549 table->lcl_head = li2;
1550 info->prev_line = table->lcl_head->prev_line;
1551 table->lcl_head->prev_line = info;
1552 if (address < seq->low_pc)
1553 seq->low_pc = address;
1558 /* Extract a fully qualified filename from a line info table.
1559 The returned string has been malloc'ed and it is the caller's
1560 responsibility to free it. */
1563 concat_filename (struct line_info_table *table, unsigned int file)
1567 if (file - 1 >= table->num_files)
1569 /* FILE == 0 means unknown. */
1572 (_("Dwarf Error: mangled line number section (bad file number)."));
1573 return strdup ("<unknown>");
1576 filename = table->files[file - 1].name;
1578 if (!IS_ABSOLUTE_PATH (filename))
1580 char *dir_name = NULL;
1581 char *subdir_name = NULL;
1585 if (table->files[file - 1].dir
1586 /* PR 17512: file: 0317e960. */
1587 && table->files[file - 1].dir <= table->num_dirs
1588 /* PR 17512: file: 7f3d2e4b. */
1589 && table->dirs != NULL)
1590 subdir_name = table->dirs[table->files[file - 1].dir - 1];
1592 if (!subdir_name || !IS_ABSOLUTE_PATH (subdir_name))
1593 dir_name = table->comp_dir;
1597 dir_name = subdir_name;
1602 return strdup (filename);
1604 len = strlen (dir_name) + strlen (filename) + 2;
1608 len += strlen (subdir_name) + 1;
1609 name = (char *) bfd_malloc (len);
1611 sprintf (name, "%s/%s/%s", dir_name, subdir_name, filename);
1615 name = (char *) bfd_malloc (len);
1617 sprintf (name, "%s/%s", dir_name, filename);
1623 return strdup (filename);
1627 arange_add (const struct comp_unit *unit, struct arange *first_arange,
1628 bfd_vma low_pc, bfd_vma high_pc)
1630 struct arange *arange;
1632 /* Ignore empty ranges. */
1633 if (low_pc == high_pc)
1636 /* If the first arange is empty, use it. */
1637 if (first_arange->high == 0)
1639 first_arange->low = low_pc;
1640 first_arange->high = high_pc;
1644 /* Next see if we can cheaply extend an existing range. */
1645 arange = first_arange;
1648 if (low_pc == arange->high)
1650 arange->high = high_pc;
1653 if (high_pc == arange->low)
1655 arange->low = low_pc;
1658 arange = arange->next;
1662 /* Need to allocate a new arange and insert it into the arange list.
1663 Order isn't significant, so just insert after the first arange. */
1664 arange = (struct arange *) bfd_alloc (unit->abfd, sizeof (*arange));
1667 arange->low = low_pc;
1668 arange->high = high_pc;
1669 arange->next = first_arange->next;
1670 first_arange->next = arange;
1674 /* Compare function for line sequences. */
1677 compare_sequences (const void* a, const void* b)
1679 const struct line_sequence* seq1 = a;
1680 const struct line_sequence* seq2 = b;
1682 /* Sort by low_pc as the primary key. */
1683 if (seq1->low_pc < seq2->low_pc)
1685 if (seq1->low_pc > seq2->low_pc)
1688 /* If low_pc values are equal, sort in reverse order of
1689 high_pc, so that the largest region comes first. */
1690 if (seq1->last_line->address < seq2->last_line->address)
1692 if (seq1->last_line->address > seq2->last_line->address)
1695 if (seq1->last_line->op_index < seq2->last_line->op_index)
1697 if (seq1->last_line->op_index > seq2->last_line->op_index)
1703 /* Construct the line information table for quick lookup. */
1706 build_line_info_table (struct line_info_table * table,
1707 struct line_sequence * seq)
1710 struct line_info** line_info_lookup;
1711 struct line_info* each_line;
1712 unsigned int num_lines;
1713 unsigned int line_index;
1715 if (seq->line_info_lookup != NULL)
1718 /* Count the number of line information entries. We could do this while
1719 scanning the debug information, but some entries may be added via
1720 lcl_head without having a sequence handy to increment the number of
1723 for (each_line = seq->last_line; each_line; each_line = each_line->prev_line)
1729 /* Allocate space for the line information lookup table. */
1730 amt = sizeof (struct line_info*) * num_lines;
1731 line_info_lookup = (struct line_info**) bfd_alloc (table->abfd, amt);
1732 if (line_info_lookup == NULL)
1735 /* Create the line information lookup table. */
1736 line_index = num_lines;
1737 for (each_line = seq->last_line; each_line; each_line = each_line->prev_line)
1738 line_info_lookup[--line_index] = each_line;
1740 BFD_ASSERT (line_index == 0);
1742 seq->num_lines = num_lines;
1743 seq->line_info_lookup = line_info_lookup;
1748 /* Sort the line sequences for quick lookup. */
1751 sort_line_sequences (struct line_info_table* table)
1754 struct line_sequence* sequences;
1755 struct line_sequence* seq;
1757 unsigned int num_sequences = table->num_sequences;
1758 bfd_vma last_high_pc;
1760 if (num_sequences == 0)
1763 /* Allocate space for an array of sequences. */
1764 amt = sizeof (struct line_sequence) * num_sequences;
1765 sequences = (struct line_sequence *) bfd_alloc (table->abfd, amt);
1766 if (sequences == NULL)
1769 /* Copy the linked list into the array, freeing the original nodes. */
1770 seq = table->sequences;
1771 for (n = 0; n < num_sequences; n++)
1773 struct line_sequence* last_seq = seq;
1776 sequences[n].low_pc = seq->low_pc;
1777 sequences[n].prev_sequence = NULL;
1778 sequences[n].last_line = seq->last_line;
1779 sequences[n].line_info_lookup = NULL;
1780 sequences[n].num_lines = 0;
1781 seq = seq->prev_sequence;
1784 BFD_ASSERT (seq == NULL);
1786 qsort (sequences, n, sizeof (struct line_sequence), compare_sequences);
1788 /* Make the list binary-searchable by trimming overlapping entries
1789 and removing nested entries. */
1791 last_high_pc = sequences[0].last_line->address;
1792 for (n = 1; n < table->num_sequences; n++)
1794 if (sequences[n].low_pc < last_high_pc)
1796 if (sequences[n].last_line->address <= last_high_pc)
1797 /* Skip nested entries. */
1800 /* Trim overlapping entries. */
1801 sequences[n].low_pc = last_high_pc;
1803 last_high_pc = sequences[n].last_line->address;
1804 if (n > num_sequences)
1806 /* Close up the gap. */
1807 sequences[num_sequences].low_pc = sequences[n].low_pc;
1808 sequences[num_sequences].last_line = sequences[n].last_line;
1813 table->sequences = sequences;
1814 table->num_sequences = num_sequences;
1818 /* Add directory to TABLE. CUR_DIR memory ownership is taken by TABLE. */
1821 line_info_add_include_dir (struct line_info_table *table, char *cur_dir)
1823 if ((table->num_dirs % DIR_ALLOC_CHUNK) == 0)
1828 amt = table->num_dirs + DIR_ALLOC_CHUNK;
1829 amt *= sizeof (char *);
1831 tmp = (char **) bfd_realloc (table->dirs, amt);
1837 table->dirs[table->num_dirs++] = cur_dir;
1842 line_info_add_include_dir_stub (struct line_info_table *table, char *cur_dir,
1843 unsigned int dir ATTRIBUTE_UNUSED,
1844 unsigned int xtime ATTRIBUTE_UNUSED,
1845 unsigned int size ATTRIBUTE_UNUSED)
1847 return line_info_add_include_dir (table, cur_dir);
1850 /* Add file to TABLE. CUR_FILE memory ownership is taken by TABLE. */
1853 line_info_add_file_name (struct line_info_table *table, char *cur_file,
1854 unsigned int dir, unsigned int xtime,
1857 if ((table->num_files % FILE_ALLOC_CHUNK) == 0)
1859 struct fileinfo *tmp;
1862 amt = table->num_files + FILE_ALLOC_CHUNK;
1863 amt *= sizeof (struct fileinfo);
1865 tmp = (struct fileinfo *) bfd_realloc (table->files, amt);
1871 table->files[table->num_files].name = cur_file;
1872 table->files[table->num_files].dir = dir;
1873 table->files[table->num_files].time = xtime;
1874 table->files[table->num_files].size = size;
1879 /* Read directory or file name entry format, starting with byte of
1880 format count entries, ULEB128 pairs of entry formats, ULEB128 of
1881 entries count and the entries themselves in the described entry
1885 read_formatted_entries (struct comp_unit *unit, bfd_byte **bufp,
1886 bfd_byte *buf_end, struct line_info_table *table,
1887 bfd_boolean (*callback) (struct line_info_table *table,
1893 bfd *abfd = unit->abfd;
1894 bfd_byte format_count, formati;
1895 bfd_vma data_count, datai;
1896 bfd_byte *buf = *bufp;
1897 bfd_byte *format_header_data;
1898 unsigned int bytes_read;
1900 format_count = read_1_byte (abfd, buf, buf_end);
1902 format_header_data = buf;
1903 for (formati = 0; formati < format_count; formati++)
1905 _bfd_safe_read_leb128 (abfd, buf, &bytes_read, FALSE, buf_end);
1907 _bfd_safe_read_leb128 (abfd, buf, &bytes_read, FALSE, buf_end);
1911 data_count = _bfd_safe_read_leb128 (abfd, buf, &bytes_read, FALSE, buf_end);
1913 for (datai = 0; datai < data_count; datai++)
1915 bfd_byte *format = format_header_data;
1918 for (formati = 0; formati < format_count; formati++)
1920 bfd_vma content_type, form;
1922 char **stringp = &string_trash;
1923 unsigned int uint_trash, *uintp = &uint_trash;
1925 content_type = _bfd_safe_read_leb128 (abfd, format, &bytes_read,
1927 format += bytes_read;
1928 switch (content_type)
1933 case DW_LNCT_directory_index:
1936 case DW_LNCT_timestamp:
1946 (_("Dwarf Error: Unknown format content type %Lu."),
1948 bfd_set_error (bfd_error_bad_value);
1952 form = _bfd_safe_read_leb128 (abfd, format, &bytes_read, FALSE,
1954 format += bytes_read;
1957 case DW_FORM_string:
1958 *stringp = read_string (abfd, buf, buf_end, &bytes_read);
1962 case DW_FORM_line_strp:
1963 *stringp = read_indirect_line_string (unit, buf, buf_end, &bytes_read);
1968 *uintp = read_1_byte (abfd, buf, buf_end);
1973 *uintp = read_2_bytes (abfd, buf, buf_end);
1978 *uintp = read_4_bytes (abfd, buf, buf_end);
1983 *uintp = read_8_bytes (abfd, buf, buf_end);
1988 *uintp = _bfd_safe_read_leb128 (abfd, buf, &bytes_read, FALSE,
1994 /* It is valid only for DW_LNCT_timestamp which is ignored by
2000 if (!callback (table, fe.name, fe.dir, fe.time, fe.size))
2008 /* Decode the line number information for UNIT. */
2010 static struct line_info_table*
2011 decode_line_info (struct comp_unit *unit, struct dwarf2_debug *stash)
2013 bfd *abfd = unit->abfd;
2014 struct line_info_table* table;
2017 struct line_head lh;
2018 unsigned int i, bytes_read, offset_size;
2019 char *cur_file, *cur_dir;
2020 unsigned char op_code, extended_op, adj_opcode;
2021 unsigned int exop_len;
2024 if (! read_section (abfd, &stash->debug_sections[debug_line],
2025 stash->syms, unit->line_offset,
2026 &stash->dwarf_line_buffer, &stash->dwarf_line_size))
2029 amt = sizeof (struct line_info_table);
2030 table = (struct line_info_table *) bfd_alloc (abfd, amt);
2034 table->comp_dir = unit->comp_dir;
2036 table->num_files = 0;
2037 table->files = NULL;
2039 table->num_dirs = 0;
2042 table->num_sequences = 0;
2043 table->sequences = NULL;
2045 table->lcl_head = NULL;
2047 if (stash->dwarf_line_size < 16)
2050 (_("Dwarf Error: Line info section is too small (%Ld)"),
2051 stash->dwarf_line_size);
2052 bfd_set_error (bfd_error_bad_value);
2055 line_ptr = stash->dwarf_line_buffer + unit->line_offset;
2056 line_end = stash->dwarf_line_buffer + stash->dwarf_line_size;
2058 /* Read in the prologue. */
2059 lh.total_length = read_4_bytes (abfd, line_ptr, line_end);
2062 if (lh.total_length == 0xffffffff)
2064 lh.total_length = read_8_bytes (abfd, line_ptr, line_end);
2068 else if (lh.total_length == 0 && unit->addr_size == 8)
2070 /* Handle (non-standard) 64-bit DWARF2 formats. */
2071 lh.total_length = read_4_bytes (abfd, line_ptr, line_end);
2076 if (lh.total_length > stash->dwarf_line_size)
2079 /* xgettext: c-format */
2080 (_("Dwarf Error: Line info data is bigger (%#Lx) than the section (%#Lx)"),
2081 lh.total_length, stash->dwarf_line_size);
2082 bfd_set_error (bfd_error_bad_value);
2086 line_end = line_ptr + lh.total_length;
2088 lh.version = read_2_bytes (abfd, line_ptr, line_end);
2089 if (lh.version < 2 || lh.version > 5)
2092 (_("Dwarf Error: Unhandled .debug_line version %d."), lh.version);
2093 bfd_set_error (bfd_error_bad_value);
2098 if (line_ptr + offset_size + (lh.version >= 5 ? 8 : (lh.version >= 4 ? 6 : 5))
2102 (_("Dwarf Error: Ran out of room reading prologue"));
2103 bfd_set_error (bfd_error_bad_value);
2107 if (lh.version >= 5)
2109 unsigned int segment_selector_size;
2111 /* Skip address size. */
2112 read_1_byte (abfd, line_ptr, line_end);
2115 segment_selector_size = read_1_byte (abfd, line_ptr, line_end);
2117 if (segment_selector_size != 0)
2120 (_("Dwarf Error: Line info unsupported segment selector size %u."),
2121 segment_selector_size);
2122 bfd_set_error (bfd_error_bad_value);
2127 if (offset_size == 4)
2128 lh.prologue_length = read_4_bytes (abfd, line_ptr, line_end);
2130 lh.prologue_length = read_8_bytes (abfd, line_ptr, line_end);
2131 line_ptr += offset_size;
2133 lh.minimum_instruction_length = read_1_byte (abfd, line_ptr, line_end);
2136 if (lh.version >= 4)
2138 lh.maximum_ops_per_insn = read_1_byte (abfd, line_ptr, line_end);
2142 lh.maximum_ops_per_insn = 1;
2144 if (lh.maximum_ops_per_insn == 0)
2147 (_("Dwarf Error: Invalid maximum operations per instruction."));
2148 bfd_set_error (bfd_error_bad_value);
2152 lh.default_is_stmt = read_1_byte (abfd, line_ptr, line_end);
2155 lh.line_base = read_1_signed_byte (abfd, line_ptr, line_end);
2158 lh.line_range = read_1_byte (abfd, line_ptr, line_end);
2161 lh.opcode_base = read_1_byte (abfd, line_ptr, line_end);
2164 if (line_ptr + (lh.opcode_base - 1) >= line_end)
2166 _bfd_error_handler (_("Dwarf Error: Ran out of room reading opcodes"));
2167 bfd_set_error (bfd_error_bad_value);
2171 amt = lh.opcode_base * sizeof (unsigned char);
2172 lh.standard_opcode_lengths = (unsigned char *) bfd_alloc (abfd, amt);
2174 lh.standard_opcode_lengths[0] = 1;
2176 for (i = 1; i < lh.opcode_base; ++i)
2178 lh.standard_opcode_lengths[i] = read_1_byte (abfd, line_ptr, line_end);
2182 if (lh.version >= 5)
2184 /* Read directory table. */
2185 if (!read_formatted_entries (unit, &line_ptr, line_end, table,
2186 line_info_add_include_dir_stub))
2189 /* Read file name table. */
2190 if (!read_formatted_entries (unit, &line_ptr, line_end, table,
2191 line_info_add_file_name))
2196 /* Read directory table. */
2197 while ((cur_dir = read_string (abfd, line_ptr, line_end, &bytes_read)) != NULL)
2199 line_ptr += bytes_read;
2201 if (!line_info_add_include_dir (table, cur_dir))
2205 line_ptr += bytes_read;
2207 /* Read file name table. */
2208 while ((cur_file = read_string (abfd, line_ptr, line_end, &bytes_read)) != NULL)
2210 unsigned int dir, xtime, size;
2212 line_ptr += bytes_read;
2214 dir = _bfd_safe_read_leb128 (abfd, line_ptr, &bytes_read, FALSE, line_end);
2215 line_ptr += bytes_read;
2216 xtime = _bfd_safe_read_leb128 (abfd, line_ptr, &bytes_read, FALSE, line_end);
2217 line_ptr += bytes_read;
2218 size = _bfd_safe_read_leb128 (abfd, line_ptr, &bytes_read, FALSE, line_end);
2219 line_ptr += bytes_read;
2221 if (!line_info_add_file_name (table, cur_file, dir, xtime, size))
2225 line_ptr += bytes_read;
2228 /* Read the statement sequences until there's nothing left. */
2229 while (line_ptr < line_end)
2231 /* State machine registers. */
2232 bfd_vma address = 0;
2233 unsigned char op_index = 0;
2234 char * filename = table->num_files ? concat_filename (table, 1) : NULL;
2235 unsigned int line = 1;
2236 unsigned int column = 0;
2237 unsigned int discriminator = 0;
2238 int is_stmt = lh.default_is_stmt;
2239 int end_sequence = 0;
2241 compilers generate address sequences that are wildly out of
2242 order using DW_LNE_set_address (e.g. Intel C++ 6.0 compiler
2243 for ia64-Linux). Thus, to determine the low and high
2244 address, we must compare on every DW_LNS_copy, etc. */
2245 bfd_vma low_pc = (bfd_vma) -1;
2246 bfd_vma high_pc = 0;
2248 /* Decode the table. */
2249 while (! end_sequence)
2251 op_code = read_1_byte (abfd, line_ptr, line_end);
2254 if (op_code >= lh.opcode_base)
2256 /* Special operand. */
2257 adj_opcode = op_code - lh.opcode_base;
2258 if (lh.line_range == 0)
2260 if (lh.maximum_ops_per_insn == 1)
2261 address += (adj_opcode / lh.line_range
2262 * lh.minimum_instruction_length);
2265 address += ((op_index + adj_opcode / lh.line_range)
2266 / lh.maximum_ops_per_insn
2267 * lh.minimum_instruction_length);
2268 op_index = ((op_index + adj_opcode / lh.line_range)
2269 % lh.maximum_ops_per_insn);
2271 line += lh.line_base + (adj_opcode % lh.line_range);
2272 /* Append row to matrix using current values. */
2273 if (!add_line_info (table, address, op_index, filename,
2274 line, column, discriminator, 0))
2277 if (address < low_pc)
2279 if (address > high_pc)
2282 else switch (op_code)
2284 case DW_LNS_extended_op:
2285 exop_len = _bfd_safe_read_leb128 (abfd, line_ptr, &bytes_read,
2287 line_ptr += bytes_read;
2288 extended_op = read_1_byte (abfd, line_ptr, line_end);
2291 switch (extended_op)
2293 case DW_LNE_end_sequence:
2295 if (!add_line_info (table, address, op_index, filename, line,
2296 column, discriminator, end_sequence))
2299 if (address < low_pc)
2301 if (address > high_pc)
2303 if (!arange_add (unit, &unit->arange, low_pc, high_pc))
2306 case DW_LNE_set_address:
2307 address = read_address (unit, line_ptr, line_end);
2309 line_ptr += unit->addr_size;
2311 case DW_LNE_define_file:
2312 cur_file = read_string (abfd, line_ptr, line_end, &bytes_read);
2313 line_ptr += bytes_read;
2314 if ((table->num_files % FILE_ALLOC_CHUNK) == 0)
2316 struct fileinfo *tmp;
2318 amt = table->num_files + FILE_ALLOC_CHUNK;
2319 amt *= sizeof (struct fileinfo);
2320 tmp = (struct fileinfo *) bfd_realloc (table->files, amt);
2325 table->files[table->num_files].name = cur_file;
2326 table->files[table->num_files].dir =
2327 _bfd_safe_read_leb128 (abfd, line_ptr, &bytes_read,
2329 line_ptr += bytes_read;
2330 table->files[table->num_files].time =
2331 _bfd_safe_read_leb128 (abfd, line_ptr, &bytes_read,
2333 line_ptr += bytes_read;
2334 table->files[table->num_files].size =
2335 _bfd_safe_read_leb128 (abfd, line_ptr, &bytes_read,
2337 line_ptr += bytes_read;
2340 case DW_LNE_set_discriminator:
2342 _bfd_safe_read_leb128 (abfd, line_ptr, &bytes_read,
2344 line_ptr += bytes_read;
2346 case DW_LNE_HP_source_file_correlation:
2347 line_ptr += exop_len - 1;
2351 (_("Dwarf Error: mangled line number section."));
2352 bfd_set_error (bfd_error_bad_value);
2354 if (filename != NULL)
2360 if (!add_line_info (table, address, op_index,
2361 filename, line, column, discriminator, 0))
2364 if (address < low_pc)
2366 if (address > high_pc)
2369 case DW_LNS_advance_pc:
2370 if (lh.maximum_ops_per_insn == 1)
2371 address += (lh.minimum_instruction_length
2372 * _bfd_safe_read_leb128 (abfd, line_ptr,
2377 bfd_vma adjust = _bfd_safe_read_leb128 (abfd, line_ptr,
2380 address = ((op_index + adjust) / lh.maximum_ops_per_insn
2381 * lh.minimum_instruction_length);
2382 op_index = (op_index + adjust) % lh.maximum_ops_per_insn;
2384 line_ptr += bytes_read;
2386 case DW_LNS_advance_line:
2387 line += _bfd_safe_read_leb128 (abfd, line_ptr, &bytes_read,
2389 line_ptr += bytes_read;
2391 case DW_LNS_set_file:
2395 /* The file and directory tables are 0
2396 based, the references are 1 based. */
2397 file = _bfd_safe_read_leb128 (abfd, line_ptr, &bytes_read,
2399 line_ptr += bytes_read;
2402 filename = concat_filename (table, file);
2405 case DW_LNS_set_column:
2406 column = _bfd_safe_read_leb128 (abfd, line_ptr, &bytes_read,
2408 line_ptr += bytes_read;
2410 case DW_LNS_negate_stmt:
2411 is_stmt = (!is_stmt);
2413 case DW_LNS_set_basic_block:
2415 case DW_LNS_const_add_pc:
2416 if (lh.maximum_ops_per_insn == 1)
2417 address += (lh.minimum_instruction_length
2418 * ((255 - lh.opcode_base) / lh.line_range));
2421 bfd_vma adjust = ((255 - lh.opcode_base) / lh.line_range);
2422 address += (lh.minimum_instruction_length
2423 * ((op_index + adjust)
2424 / lh.maximum_ops_per_insn));
2425 op_index = (op_index + adjust) % lh.maximum_ops_per_insn;
2428 case DW_LNS_fixed_advance_pc:
2429 address += read_2_bytes (abfd, line_ptr, line_end);
2434 /* Unknown standard opcode, ignore it. */
2435 for (i = 0; i < lh.standard_opcode_lengths[op_code]; i++)
2437 (void) _bfd_safe_read_leb128 (abfd, line_ptr, &bytes_read,
2439 line_ptr += bytes_read;
2449 if (sort_line_sequences (table))
2453 if (table->sequences != NULL)
2454 free (table->sequences);
2455 if (table->files != NULL)
2456 free (table->files);
2457 if (table->dirs != NULL)
2462 /* If ADDR is within TABLE set the output parameters and return the
2463 range of addresses covered by the entry used to fill them out.
2464 Otherwise set * FILENAME_PTR to NULL and return 0.
2465 The parameters FILENAME_PTR, LINENUMBER_PTR and DISCRIMINATOR_PTR
2466 are pointers to the objects to be filled in. */
2469 lookup_address_in_line_info_table (struct line_info_table *table,
2471 const char **filename_ptr,
2472 unsigned int *linenumber_ptr,
2473 unsigned int *discriminator_ptr)
2475 struct line_sequence *seq = NULL;
2476 struct line_info *info;
2479 /* Binary search the array of sequences. */
2481 high = table->num_sequences;
2484 mid = (low + high) / 2;
2485 seq = &table->sequences[mid];
2486 if (addr < seq->low_pc)
2488 else if (addr >= seq->last_line->address)
2494 /* Check for a valid sequence. */
2495 if (!seq || addr < seq->low_pc || addr >= seq->last_line->address)
2498 if (!build_line_info_table (table, seq))
2501 /* Binary search the array of line information. */
2503 high = seq->num_lines;
2507 mid = (low + high) / 2;
2508 info = seq->line_info_lookup[mid];
2509 if (addr < info->address)
2511 else if (addr >= seq->line_info_lookup[mid + 1]->address)
2517 /* Check for a valid line information entry. */
2519 && addr >= info->address
2520 && addr < seq->line_info_lookup[mid + 1]->address
2521 && !(info->end_sequence || info == seq->last_line))
2523 *filename_ptr = info->filename;
2524 *linenumber_ptr = info->line;
2525 if (discriminator_ptr)
2526 *discriminator_ptr = info->discriminator;
2527 return seq->last_line->address - seq->low_pc;
2531 *filename_ptr = NULL;
2535 /* Read in the .debug_ranges section for future reference. */
2538 read_debug_ranges (struct comp_unit * unit)
2540 struct dwarf2_debug * stash = unit->stash;
2542 return read_section (unit->abfd, &stash->debug_sections[debug_ranges],
2544 &stash->dwarf_ranges_buffer,
2545 &stash->dwarf_ranges_size);
2548 /* Function table functions. */
2551 compare_lookup_funcinfos (const void * a, const void * b)
2553 const struct lookup_funcinfo * lookup1 = a;
2554 const struct lookup_funcinfo * lookup2 = b;
2556 if (lookup1->low_addr < lookup2->low_addr)
2558 if (lookup1->low_addr > lookup2->low_addr)
2560 if (lookup1->high_addr < lookup2->high_addr)
2562 if (lookup1->high_addr > lookup2->high_addr)
2569 build_lookup_funcinfo_table (struct comp_unit * unit)
2571 struct lookup_funcinfo *lookup_funcinfo_table = unit->lookup_funcinfo_table;
2572 unsigned int number_of_functions = unit->number_of_functions;
2573 struct funcinfo *each;
2574 struct lookup_funcinfo *entry;
2576 struct arange *range;
2577 bfd_vma low_addr, high_addr;
2579 if (lookup_funcinfo_table || number_of_functions == 0)
2582 /* Create the function info lookup table. */
2583 lookup_funcinfo_table = (struct lookup_funcinfo *)
2584 bfd_malloc (number_of_functions * sizeof (struct lookup_funcinfo));
2585 if (lookup_funcinfo_table == NULL)
2588 /* Populate the function info lookup table. */
2589 func_index = number_of_functions;
2590 for (each = unit->function_table; each; each = each->prev_func)
2592 entry = &lookup_funcinfo_table[--func_index];
2593 entry->funcinfo = each;
2595 /* Calculate the lowest and highest address for this function entry. */
2596 low_addr = entry->funcinfo->arange.low;
2597 high_addr = entry->funcinfo->arange.high;
2599 for (range = entry->funcinfo->arange.next; range; range = range->next)
2601 if (range->low < low_addr)
2602 low_addr = range->low;
2603 if (range->high > high_addr)
2604 high_addr = range->high;
2607 entry->low_addr = low_addr;
2608 entry->high_addr = high_addr;
2611 BFD_ASSERT (func_index == 0);
2613 /* Sort the function by address. */
2614 qsort (lookup_funcinfo_table,
2615 number_of_functions,
2616 sizeof (struct lookup_funcinfo),
2617 compare_lookup_funcinfos);
2619 /* Calculate the high watermark for each function in the lookup table. */
2620 high_addr = lookup_funcinfo_table[0].high_addr;
2621 for (func_index = 1; func_index < number_of_functions; func_index++)
2623 entry = &lookup_funcinfo_table[func_index];
2624 if (entry->high_addr > high_addr)
2625 high_addr = entry->high_addr;
2627 entry->high_addr = high_addr;
2630 unit->lookup_funcinfo_table = lookup_funcinfo_table;
2634 /* If ADDR is within UNIT's function tables, set FUNCTION_PTR, and return
2635 TRUE. Note that we need to find the function that has the smallest range
2636 that contains ADDR, to handle inlined functions without depending upon
2637 them being ordered in TABLE by increasing range. */
2640 lookup_address_in_function_table (struct comp_unit *unit,
2642 struct funcinfo **function_ptr)
2644 unsigned int number_of_functions = unit->number_of_functions;
2645 struct lookup_funcinfo* lookup_funcinfo = NULL;
2646 struct funcinfo* funcinfo = NULL;
2647 struct funcinfo* best_fit = NULL;
2648 bfd_vma best_fit_len = 0;
2649 bfd_size_type low, high, mid, first;
2650 struct arange *arange;
2652 if (number_of_functions == 0)
2655 if (!build_lookup_funcinfo_table (unit))
2658 if (unit->lookup_funcinfo_table[number_of_functions - 1].high_addr < addr)
2661 /* Find the first function in the lookup table which may contain the
2662 specified address. */
2664 high = number_of_functions;
2668 mid = (low + high) / 2;
2669 lookup_funcinfo = &unit->lookup_funcinfo_table[mid];
2670 if (addr < lookup_funcinfo->low_addr)
2672 else if (addr >= lookup_funcinfo->high_addr)
2678 /* Find the 'best' match for the address. The prior algorithm defined the
2679 best match as the function with the smallest address range containing
2680 the specified address. This definition should probably be changed to the
2681 innermost inline routine containing the address, but right now we want
2682 to get the same results we did before. */
2683 while (first < number_of_functions)
2685 if (addr < unit->lookup_funcinfo_table[first].low_addr)
2687 funcinfo = unit->lookup_funcinfo_table[first].funcinfo;
2689 for (arange = &funcinfo->arange; arange; arange = arange->next)
2691 if (addr < arange->low || addr >= arange->high)
2695 || arange->high - arange->low < best_fit_len
2696 /* The following comparison is designed to return the same
2697 match as the previous algorithm for routines which have the
2698 same best fit length. */
2699 || (arange->high - arange->low == best_fit_len
2700 && funcinfo > best_fit))
2702 best_fit = funcinfo;
2703 best_fit_len = arange->high - arange->low;
2713 *function_ptr = best_fit;
2717 /* If SYM at ADDR is within function table of UNIT, set FILENAME_PTR
2718 and LINENUMBER_PTR, and return TRUE. */
2721 lookup_symbol_in_function_table (struct comp_unit *unit,
2724 const char **filename_ptr,
2725 unsigned int *linenumber_ptr)
2727 struct funcinfo* each_func;
2728 struct funcinfo* best_fit = NULL;
2729 bfd_vma best_fit_len = 0;
2730 struct arange *arange;
2731 const char *name = bfd_asymbol_name (sym);
2732 asection *sec = bfd_get_section (sym);
2734 for (each_func = unit->function_table;
2736 each_func = each_func->prev_func)
2738 for (arange = &each_func->arange;
2740 arange = arange->next)
2742 if ((!each_func->sec || each_func->sec == sec)
2743 && addr >= arange->low
2744 && addr < arange->high
2746 && strcmp (name, each_func->name) == 0
2748 || arange->high - arange->low < best_fit_len))
2750 best_fit = each_func;
2751 best_fit_len = arange->high - arange->low;
2758 best_fit->sec = sec;
2759 *filename_ptr = best_fit->file;
2760 *linenumber_ptr = best_fit->line;
2767 /* Variable table functions. */
2769 /* If SYM is within variable table of UNIT, set FILENAME_PTR and
2770 LINENUMBER_PTR, and return TRUE. */
2773 lookup_symbol_in_variable_table (struct comp_unit *unit,
2776 const char **filename_ptr,
2777 unsigned int *linenumber_ptr)
2779 const char *name = bfd_asymbol_name (sym);
2780 asection *sec = bfd_get_section (sym);
2781 struct varinfo* each;
2783 for (each = unit->variable_table; each; each = each->prev_var)
2784 if (each->stack == 0
2785 && each->file != NULL
2786 && each->name != NULL
2787 && each->addr == addr
2788 && (!each->sec || each->sec == sec)
2789 && strcmp (name, each->name) == 0)
2795 *filename_ptr = each->file;
2796 *linenumber_ptr = each->line;
2804 find_abstract_instance_name (struct comp_unit *unit,
2805 struct attribute *attr_ptr,
2806 bfd_boolean *is_linkage)
2808 bfd *abfd = unit->abfd;
2810 bfd_byte *info_ptr_end;
2811 unsigned int abbrev_number, bytes_read, i;
2812 struct abbrev_info *abbrev;
2813 bfd_uint64_t die_ref = attr_ptr->u.val;
2814 struct attribute attr;
2817 /* DW_FORM_ref_addr can reference an entry in a different CU. It
2818 is an offset from the .debug_info section, not the current CU. */
2819 if (attr_ptr->form == DW_FORM_ref_addr)
2821 /* We only support DW_FORM_ref_addr within the same file, so
2822 any relocations should be resolved already. */
2826 info_ptr = unit->sec_info_ptr + die_ref;
2827 info_ptr_end = unit->end_ptr;
2829 /* Now find the CU containing this pointer. */
2830 if (info_ptr >= unit->info_ptr_unit && info_ptr < unit->end_ptr)
2834 /* Check other CUs to see if they contain the abbrev. */
2835 struct comp_unit * u;
2837 for (u = unit->prev_unit; u != NULL; u = u->prev_unit)
2838 if (info_ptr >= u->info_ptr_unit && info_ptr < u->end_ptr)
2842 for (u = unit->next_unit; u != NULL; u = u->next_unit)
2843 if (info_ptr >= u->info_ptr_unit && info_ptr < u->end_ptr)
2848 /* else FIXME: What do we do now ? */
2851 else if (attr_ptr->form == DW_FORM_GNU_ref_alt)
2853 info_ptr = read_alt_indirect_ref (unit, die_ref);
2854 if (info_ptr == NULL)
2857 (_("Dwarf Error: Unable to read alt ref %llu."),
2858 (long long) die_ref);
2859 bfd_set_error (bfd_error_bad_value);
2862 info_ptr_end = unit->stash->alt_dwarf_info_buffer + unit->stash->alt_dwarf_info_size;
2864 /* FIXME: Do we need to locate the correct CU, in a similar
2865 fashion to the code in the DW_FORM_ref_addr case above ? */
2869 info_ptr = unit->info_ptr_unit + die_ref;
2870 info_ptr_end = unit->end_ptr;
2873 abbrev_number = _bfd_safe_read_leb128 (abfd, info_ptr, &bytes_read,
2874 FALSE, info_ptr_end);
2875 info_ptr += bytes_read;
2879 abbrev = lookup_abbrev (abbrev_number, unit->abbrevs);
2883 (_("Dwarf Error: Could not find abbrev number %u."), abbrev_number);
2884 bfd_set_error (bfd_error_bad_value);
2888 for (i = 0; i < abbrev->num_attrs; ++i)
2890 info_ptr = read_attribute (&attr, &abbrev->attrs[i], unit,
2891 info_ptr, info_ptr_end);
2892 if (info_ptr == NULL)
2897 /* Prefer DW_AT_MIPS_linkage_name or DW_AT_linkage_name
2899 if (name == NULL && is_str_attr (attr.form))
2902 if (non_mangled (unit->lang))
2906 case DW_AT_specification:
2907 name = find_abstract_instance_name (unit, &attr, is_linkage);
2909 case DW_AT_linkage_name:
2910 case DW_AT_MIPS_linkage_name:
2911 /* PR 16949: Corrupt debug info can place
2912 non-string forms into these attributes. */
2913 if (is_str_attr (attr.form))
2929 read_rangelist (struct comp_unit *unit, struct arange *arange,
2930 bfd_uint64_t offset)
2932 bfd_byte *ranges_ptr;
2933 bfd_byte *ranges_end;
2934 bfd_vma base_address = unit->base_address;
2936 if (! unit->stash->dwarf_ranges_buffer)
2938 if (! read_debug_ranges (unit))
2942 ranges_ptr = unit->stash->dwarf_ranges_buffer + offset;
2943 if (ranges_ptr < unit->stash->dwarf_ranges_buffer)
2945 ranges_end = unit->stash->dwarf_ranges_buffer + unit->stash->dwarf_ranges_size;
2952 /* PR 17512: file: 62cada7d. */
2953 if (ranges_ptr + 2 * unit->addr_size > ranges_end)
2956 low_pc = read_address (unit, ranges_ptr, ranges_end);
2957 ranges_ptr += unit->addr_size;
2958 high_pc = read_address (unit, ranges_ptr, ranges_end);
2959 ranges_ptr += unit->addr_size;
2961 if (low_pc == 0 && high_pc == 0)
2963 if (low_pc == -1UL && high_pc != -1UL)
2964 base_address = high_pc;
2967 if (!arange_add (unit, arange,
2968 base_address + low_pc, base_address + high_pc))
2975 /* DWARF2 Compilation unit functions. */
2977 /* Scan over each die in a comp. unit looking for functions to add
2978 to the function table and variables to the variable table. */
2981 scan_unit_for_symbols (struct comp_unit *unit)
2983 bfd *abfd = unit->abfd;
2984 bfd_byte *info_ptr = unit->first_child_die_ptr;
2985 bfd_byte *info_ptr_end = unit->stash->info_ptr_end;
2986 int nesting_level = 1;
2987 struct funcinfo **nested_funcs;
2988 int nested_funcs_size;
2990 /* Maintain a stack of in-scope functions and inlined functions, which we
2991 can use to set the caller_func field. */
2992 nested_funcs_size = 32;
2993 nested_funcs = (struct funcinfo **)
2994 bfd_malloc (nested_funcs_size * sizeof (struct funcinfo *));
2995 if (nested_funcs == NULL)
2997 nested_funcs[nesting_level] = 0;
2999 while (nesting_level)
3001 unsigned int abbrev_number, bytes_read, i;
3002 struct abbrev_info *abbrev;
3003 struct attribute attr;
3004 struct funcinfo *func;
3005 struct varinfo *var;
3007 bfd_vma high_pc = 0;
3008 bfd_boolean high_pc_relative = FALSE;
3010 /* PR 17512: file: 9f405d9d. */
3011 if (info_ptr >= info_ptr_end)
3014 abbrev_number = _bfd_safe_read_leb128 (abfd, info_ptr, &bytes_read,
3015 FALSE, info_ptr_end);
3016 info_ptr += bytes_read;
3018 if (! abbrev_number)
3024 abbrev = lookup_abbrev (abbrev_number, unit->abbrevs);
3027 static unsigned int previous_failed_abbrev = -1U;
3029 /* Avoid multiple reports of the same missing abbrev. */
3030 if (abbrev_number != previous_failed_abbrev)
3033 (_("Dwarf Error: Could not find abbrev number %u."),
3035 previous_failed_abbrev = abbrev_number;
3037 bfd_set_error (bfd_error_bad_value);
3042 if (abbrev->tag == DW_TAG_subprogram
3043 || abbrev->tag == DW_TAG_entry_point
3044 || abbrev->tag == DW_TAG_inlined_subroutine)
3046 bfd_size_type amt = sizeof (struct funcinfo);
3047 func = (struct funcinfo *) bfd_zalloc (abfd, amt);
3050 func->tag = abbrev->tag;
3051 func->prev_func = unit->function_table;
3052 unit->function_table = func;
3053 unit->number_of_functions++;
3054 BFD_ASSERT (!unit->cached);
3056 if (func->tag == DW_TAG_inlined_subroutine)
3057 for (i = nesting_level - 1; i >= 1; i--)
3058 if (nested_funcs[i])
3060 func->caller_func = nested_funcs[i];
3063 nested_funcs[nesting_level] = func;
3068 if (abbrev->tag == DW_TAG_variable)
3070 bfd_size_type amt = sizeof (struct varinfo);
3071 var = (struct varinfo *) bfd_zalloc (abfd, amt);
3074 var->tag = abbrev->tag;
3076 var->prev_var = unit->variable_table;
3077 unit->variable_table = var;
3078 /* PR 18205: Missing debug information can cause this
3079 var to be attached to an already cached unit. */
3082 /* No inline function in scope at this nesting level. */
3083 nested_funcs[nesting_level] = 0;
3086 for (i = 0; i < abbrev->num_attrs; ++i)
3088 info_ptr = read_attribute (&attr, &abbrev->attrs[i], unit, info_ptr, info_ptr_end);
3089 if (info_ptr == NULL)
3096 case DW_AT_call_file:
3097 func->caller_file = concat_filename (unit->line_table,
3101 case DW_AT_call_line:
3102 func->caller_line = attr.u.val;
3105 case DW_AT_abstract_origin:
3106 case DW_AT_specification:
3107 func->name = find_abstract_instance_name (unit, &attr,
3112 /* Prefer DW_AT_MIPS_linkage_name or DW_AT_linkage_name
3114 if (func->name == NULL && is_str_attr (attr.form))
3116 func->name = attr.u.str;
3117 if (non_mangled (unit->lang))
3118 func->is_linkage = TRUE;
3122 case DW_AT_linkage_name:
3123 case DW_AT_MIPS_linkage_name:
3124 /* PR 16949: Corrupt debug info can place
3125 non-string forms into these attributes. */
3126 if (is_str_attr (attr.form))
3128 func->name = attr.u.str;
3129 func->is_linkage = TRUE;
3134 low_pc = attr.u.val;
3138 high_pc = attr.u.val;
3139 high_pc_relative = attr.form != DW_FORM_addr;
3143 if (!read_rangelist (unit, &func->arange, attr.u.val))
3147 case DW_AT_decl_file:
3148 func->file = concat_filename (unit->line_table,
3152 case DW_AT_decl_line:
3153 func->line = attr.u.val;
3165 var->name = attr.u.str;
3168 case DW_AT_decl_file:
3169 var->file = concat_filename (unit->line_table,
3173 case DW_AT_decl_line:
3174 var->line = attr.u.val;
3177 case DW_AT_external:
3178 if (attr.u.val != 0)
3182 case DW_AT_location:
3186 case DW_FORM_block1:
3187 case DW_FORM_block2:
3188 case DW_FORM_block4:
3189 case DW_FORM_exprloc:
3190 if (*attr.u.blk->data == DW_OP_addr)
3194 /* Verify that DW_OP_addr is the only opcode in the
3195 location, in which case the block size will be 1
3196 plus the address size. */
3197 /* ??? For TLS variables, gcc can emit
3198 DW_OP_addr <addr> DW_OP_GNU_push_tls_address
3199 which we don't handle here yet. */
3200 if (attr.u.blk->size == unit->addr_size + 1U)
3201 var->addr = bfd_get (unit->addr_size * 8,
3203 attr.u.blk->data + 1);
3218 if (high_pc_relative)
3221 if (func && high_pc != 0)
3223 if (!arange_add (unit, &func->arange, low_pc, high_pc))
3227 if (abbrev->has_children)
3231 if (nesting_level >= nested_funcs_size)
3233 struct funcinfo **tmp;
3235 nested_funcs_size *= 2;
3236 tmp = (struct funcinfo **)
3237 bfd_realloc (nested_funcs,
3238 nested_funcs_size * sizeof (struct funcinfo *));
3243 nested_funcs[nesting_level] = 0;
3247 free (nested_funcs);
3251 free (nested_funcs);
3255 /* Parse a DWARF2 compilation unit starting at INFO_PTR. This
3256 includes the compilation unit header that proceeds the DIE's, but
3257 does not include the length field that precedes each compilation
3258 unit header. END_PTR points one past the end of this comp unit.
3259 OFFSET_SIZE is the size of DWARF2 offsets (either 4 or 8 bytes).
3261 This routine does not read the whole compilation unit; only enough
3262 to get to the line number information for the compilation unit. */
3264 static struct comp_unit *
3265 parse_comp_unit (struct dwarf2_debug *stash,
3266 bfd_vma unit_length,
3267 bfd_byte *info_ptr_unit,
3268 unsigned int offset_size)
3270 struct comp_unit* unit;
3271 unsigned int version;
3272 bfd_uint64_t abbrev_offset = 0;
3273 /* Initialize it just to avoid a GCC false warning. */
3274 unsigned int addr_size = -1;
3275 struct abbrev_info** abbrevs;
3276 unsigned int abbrev_number, bytes_read, i;
3277 struct abbrev_info *abbrev;
3278 struct attribute attr;
3279 bfd_byte *info_ptr = stash->info_ptr;
3280 bfd_byte *end_ptr = info_ptr + unit_length;
3283 bfd_vma high_pc = 0;
3284 bfd *abfd = stash->bfd_ptr;
3285 bfd_boolean high_pc_relative = FALSE;
3286 enum dwarf_unit_type unit_type;
3288 version = read_2_bytes (abfd, info_ptr, end_ptr);
3290 if (version < 2 || version > 5)
3292 /* PR 19872: A version number of 0 probably means that there is padding
3293 at the end of the .debug_info section. Gold puts it there when
3294 performing an incremental link, for example. So do not generate
3295 an error, just return a NULL. */
3299 (_("Dwarf Error: found dwarf version '%u', this reader"
3300 " only handles version 2, 3, 4 and 5 information."), version);
3301 bfd_set_error (bfd_error_bad_value);
3307 unit_type = DW_UT_compile;
3310 unit_type = read_1_byte (abfd, info_ptr, end_ptr);
3313 addr_size = read_1_byte (abfd, info_ptr, end_ptr);
3317 BFD_ASSERT (offset_size == 4 || offset_size == 8);
3318 if (offset_size == 4)
3319 abbrev_offset = read_4_bytes (abfd, info_ptr, end_ptr);
3321 abbrev_offset = read_8_bytes (abfd, info_ptr, end_ptr);
3322 info_ptr += offset_size;
3326 addr_size = read_1_byte (abfd, info_ptr, end_ptr);
3330 if (unit_type == DW_UT_type)
3332 /* Skip type signature. */
3335 /* Skip type offset. */
3336 info_ptr += offset_size;
3339 if (addr_size > sizeof (bfd_vma))
3342 /* xgettext: c-format */
3343 (_("Dwarf Error: found address size '%u', this reader"
3344 " can not handle sizes greater than '%u'."),
3346 (unsigned int) sizeof (bfd_vma));
3347 bfd_set_error (bfd_error_bad_value);
3351 if (addr_size != 2 && addr_size != 4 && addr_size != 8)
3354 ("Dwarf Error: found address size '%u', this reader"
3355 " can only handle address sizes '2', '4' and '8'.", addr_size);
3356 bfd_set_error (bfd_error_bad_value);
3360 /* Read the abbrevs for this compilation unit into a table. */
3361 abbrevs = read_abbrevs (abfd, abbrev_offset, stash);
3365 abbrev_number = _bfd_safe_read_leb128 (abfd, info_ptr, &bytes_read,
3367 info_ptr += bytes_read;
3368 if (! abbrev_number)
3370 /* PR 19872: An abbrev number of 0 probably means that there is padding
3371 at the end of the .debug_abbrev section. Gold puts it there when
3372 performing an incremental link, for example. So do not generate
3373 an error, just return a NULL. */
3377 abbrev = lookup_abbrev (abbrev_number, abbrevs);
3380 _bfd_error_handler (_("Dwarf Error: Could not find abbrev number %u."),
3382 bfd_set_error (bfd_error_bad_value);
3386 amt = sizeof (struct comp_unit);
3387 unit = (struct comp_unit *) bfd_zalloc (abfd, amt);
3391 unit->version = version;
3392 unit->addr_size = addr_size;
3393 unit->offset_size = offset_size;
3394 unit->abbrevs = abbrevs;
3395 unit->end_ptr = end_ptr;
3396 unit->stash = stash;
3397 unit->info_ptr_unit = info_ptr_unit;
3398 unit->sec_info_ptr = stash->sec_info_ptr;
3400 for (i = 0; i < abbrev->num_attrs; ++i)
3402 info_ptr = read_attribute (&attr, &abbrev->attrs[i], unit, info_ptr, end_ptr);
3403 if (info_ptr == NULL)
3406 /* Store the data if it is of an attribute we want to keep in a
3407 partial symbol table. */
3410 case DW_AT_stmt_list:
3412 unit->line_offset = attr.u.val;
3416 unit->name = attr.u.str;
3420 low_pc = attr.u.val;
3421 /* If the compilation unit DIE has a DW_AT_low_pc attribute,
3422 this is the base address to use when reading location
3423 lists or range lists. */
3424 if (abbrev->tag == DW_TAG_compile_unit)
3425 unit->base_address = low_pc;
3429 high_pc = attr.u.val;
3430 high_pc_relative = attr.form != DW_FORM_addr;
3434 if (!read_rangelist (unit, &unit->arange, attr.u.val))
3438 case DW_AT_comp_dir:
3440 char *comp_dir = attr.u.str;
3442 /* PR 17512: file: 1fe726be. */
3443 if (! is_str_attr (attr.form))
3446 (_("Dwarf Error: DW_AT_comp_dir attribute encountered with a non-string form."));
3452 /* Irix 6.2 native cc prepends <machine>.: to the compilation
3453 directory, get rid of it. */
3454 char *cp = strchr (comp_dir, ':');
3456 if (cp && cp != comp_dir && cp[-1] == '.' && cp[1] == '/')
3459 unit->comp_dir = comp_dir;
3463 case DW_AT_language:
3464 unit->lang = attr.u.val;
3471 if (high_pc_relative)
3475 if (!arange_add (unit, &unit->arange, low_pc, high_pc))
3479 unit->first_child_die_ptr = info_ptr;
3483 /* Return TRUE if UNIT may contain the address given by ADDR. When
3484 there are functions written entirely with inline asm statements, the
3485 range info in the compilation unit header may not be correct. We
3486 need to consult the line info table to see if a compilation unit
3487 really contains the given address. */
3490 comp_unit_contains_address (struct comp_unit *unit, bfd_vma addr)
3492 struct arange *arange;
3497 arange = &unit->arange;
3500 if (addr >= arange->low && addr < arange->high)
3502 arange = arange->next;
3509 /* If UNIT contains ADDR, set the output parameters to the values for
3510 the line containing ADDR. The output parameters, FILENAME_PTR,
3511 FUNCTION_PTR, and LINENUMBER_PTR, are pointers to the objects
3514 Returns the range of addresses covered by the entry that was used
3515 to fill in *LINENUMBER_PTR or 0 if it was not filled in. */
3518 comp_unit_find_nearest_line (struct comp_unit *unit,
3520 const char **filename_ptr,
3521 struct funcinfo **function_ptr,
3522 unsigned int *linenumber_ptr,
3523 unsigned int *discriminator_ptr,
3524 struct dwarf2_debug *stash)
3531 if (! unit->line_table)
3533 if (! unit->stmtlist)
3539 unit->line_table = decode_line_info (unit, stash);
3541 if (! unit->line_table)
3547 if (unit->first_child_die_ptr < unit->end_ptr
3548 && ! scan_unit_for_symbols (unit))
3555 *function_ptr = NULL;
3556 func_p = lookup_address_in_function_table (unit, addr, function_ptr);
3557 if (func_p && (*function_ptr)->tag == DW_TAG_inlined_subroutine)
3558 stash->inliner_chain = *function_ptr;
3560 return lookup_address_in_line_info_table (unit->line_table, addr,
3566 /* Check to see if line info is already decoded in a comp_unit.
3567 If not, decode it. Returns TRUE if no errors were encountered;
3571 comp_unit_maybe_decode_line_info (struct comp_unit *unit,
3572 struct dwarf2_debug *stash)
3577 if (! unit->line_table)
3579 if (! unit->stmtlist)
3585 unit->line_table = decode_line_info (unit, stash);
3587 if (! unit->line_table)
3593 if (unit->first_child_die_ptr < unit->end_ptr
3594 && ! scan_unit_for_symbols (unit))
3604 /* If UNIT contains SYM at ADDR, set the output parameters to the
3605 values for the line containing SYM. The output parameters,
3606 FILENAME_PTR, and LINENUMBER_PTR, are pointers to the objects to be
3609 Return TRUE if UNIT contains SYM, and no errors were encountered;
3613 comp_unit_find_line (struct comp_unit *unit,
3616 const char **filename_ptr,
3617 unsigned int *linenumber_ptr,
3618 struct dwarf2_debug *stash)
3620 if (!comp_unit_maybe_decode_line_info (unit, stash))
3623 if (sym->flags & BSF_FUNCTION)
3624 return lookup_symbol_in_function_table (unit, sym, addr,
3628 return lookup_symbol_in_variable_table (unit, sym, addr,
3633 static struct funcinfo *
3634 reverse_funcinfo_list (struct funcinfo *head)
3636 struct funcinfo *rhead;
3637 struct funcinfo *temp;
3639 for (rhead = NULL; head; head = temp)
3641 temp = head->prev_func;
3642 head->prev_func = rhead;
3648 static struct varinfo *
3649 reverse_varinfo_list (struct varinfo *head)
3651 struct varinfo *rhead;
3652 struct varinfo *temp;
3654 for (rhead = NULL; head; head = temp)
3656 temp = head->prev_var;
3657 head->prev_var = rhead;
3663 /* Extract all interesting funcinfos and varinfos of a compilation
3664 unit into hash tables for faster lookup. Returns TRUE if no
3665 errors were enountered; FALSE otherwise. */
3668 comp_unit_hash_info (struct dwarf2_debug *stash,
3669 struct comp_unit *unit,
3670 struct info_hash_table *funcinfo_hash_table,
3671 struct info_hash_table *varinfo_hash_table)
3673 struct funcinfo* each_func;
3674 struct varinfo* each_var;
3675 bfd_boolean okay = TRUE;
3677 BFD_ASSERT (stash->info_hash_status != STASH_INFO_HASH_DISABLED);
3679 if (!comp_unit_maybe_decode_line_info (unit, stash))
3682 BFD_ASSERT (!unit->cached);
3684 /* To preserve the original search order, we went to visit the function
3685 infos in the reversed order of the list. However, making the list
3686 bi-directional use quite a bit of extra memory. So we reverse
3687 the list first, traverse the list in the now reversed order and
3688 finally reverse the list again to get back the original order. */
3689 unit->function_table = reverse_funcinfo_list (unit->function_table);
3690 for (each_func = unit->function_table;
3692 each_func = each_func->prev_func)
3694 /* Skip nameless functions. */
3695 if (each_func->name)
3696 /* There is no need to copy name string into hash table as
3697 name string is either in the dwarf string buffer or
3698 info in the stash. */
3699 okay = insert_info_hash_table (funcinfo_hash_table, each_func->name,
3700 (void*) each_func, FALSE);
3702 unit->function_table = reverse_funcinfo_list (unit->function_table);
3706 /* We do the same for variable infos. */
3707 unit->variable_table = reverse_varinfo_list (unit->variable_table);
3708 for (each_var = unit->variable_table;
3710 each_var = each_var->prev_var)
3712 /* Skip stack vars and vars with no files or names. */
3713 if (each_var->stack == 0
3714 && each_var->file != NULL
3715 && each_var->name != NULL)
3716 /* There is no need to copy name string into hash table as
3717 name string is either in the dwarf string buffer or
3718 info in the stash. */
3719 okay = insert_info_hash_table (varinfo_hash_table, each_var->name,
3720 (void*) each_var, FALSE);
3723 unit->variable_table = reverse_varinfo_list (unit->variable_table);
3724 unit->cached = TRUE;
3728 /* Locate a section in a BFD containing debugging info. The search starts
3729 from the section after AFTER_SEC, or from the first section in the BFD if
3730 AFTER_SEC is NULL. The search works by examining the names of the
3731 sections. There are three permissiable names. The first two are given
3732 by DEBUG_SECTIONS[debug_info] (whose standard DWARF2 names are .debug_info
3733 and .zdebug_info). The third is a prefix .gnu.linkonce.wi.
3734 This is a variation on the .debug_info section which has a checksum
3735 describing the contents appended onto the name. This allows the linker to
3736 identify and discard duplicate debugging sections for different
3737 compilation units. */
3738 #define GNU_LINKONCE_INFO ".gnu.linkonce.wi."
3741 find_debug_info (bfd *abfd, const struct dwarf_debug_section *debug_sections,
3742 asection *after_sec)
3747 if (after_sec == NULL)
3749 look = debug_sections[debug_info].uncompressed_name;
3750 msec = bfd_get_section_by_name (abfd, look);
3754 look = debug_sections[debug_info].compressed_name;
3757 msec = bfd_get_section_by_name (abfd, look);
3762 for (msec = abfd->sections; msec != NULL; msec = msec->next)
3763 if (CONST_STRNEQ (msec->name, GNU_LINKONCE_INFO))
3769 for (msec = after_sec->next; msec != NULL; msec = msec->next)
3771 look = debug_sections[debug_info].uncompressed_name;
3772 if (strcmp (msec->name, look) == 0)
3775 look = debug_sections[debug_info].compressed_name;
3776 if (look != NULL && strcmp (msec->name, look) == 0)
3779 if (CONST_STRNEQ (msec->name, GNU_LINKONCE_INFO))
3786 /* Transfer VMAs from object file to separate debug file. */
3789 set_debug_vma (bfd *orig_bfd, bfd *debug_bfd)
3793 for (s = orig_bfd->sections, d = debug_bfd->sections;
3794 s != NULL && d != NULL;
3795 s = s->next, d = d->next)
3797 if ((d->flags & SEC_DEBUGGING) != 0)
3799 /* ??? Assumes 1-1 correspondence between sections in the
3801 if (strcmp (s->name, d->name) == 0)
3803 d->output_section = s->output_section;
3804 d->output_offset = s->output_offset;
3810 /* Unset vmas for adjusted sections in STASH. */
3813 unset_sections (struct dwarf2_debug *stash)
3816 struct adjusted_section *p;
3818 i = stash->adjusted_section_count;
3819 p = stash->adjusted_sections;
3820 for (; i > 0; i--, p++)
3821 p->section->vma = 0;
3824 /* Set VMAs for allocated and .debug_info sections in ORIG_BFD, a
3825 relocatable object file. VMAs are normally all zero in relocatable
3826 object files, so if we want to distinguish locations in sections by
3827 address we need to set VMAs so the sections do not overlap. We
3828 also set VMA on .debug_info so that when we have multiple
3829 .debug_info sections (or the linkonce variant) they also do not
3830 overlap. The multiple .debug_info sections make up a single
3831 logical section. ??? We should probably do the same for other
3835 place_sections (bfd *orig_bfd, struct dwarf2_debug *stash)
3838 struct adjusted_section *p;
3840 const char *debug_info_name;
3842 if (stash->adjusted_section_count != 0)
3844 i = stash->adjusted_section_count;
3845 p = stash->adjusted_sections;
3846 for (; i > 0; i--, p++)
3847 p->section->vma = p->adj_vma;
3851 debug_info_name = stash->debug_sections[debug_info].uncompressed_name;
3858 for (sect = abfd->sections; sect != NULL; sect = sect->next)
3862 if ((sect->output_section != NULL
3863 && sect->output_section != sect
3864 && (sect->flags & SEC_DEBUGGING) == 0)
3868 is_debug_info = (strcmp (sect->name, debug_info_name) == 0
3869 || CONST_STRNEQ (sect->name, GNU_LINKONCE_INFO));
3871 if (!((sect->flags & SEC_ALLOC) != 0 && abfd == orig_bfd)
3877 if (abfd == stash->bfd_ptr)
3879 abfd = stash->bfd_ptr;
3883 stash->adjusted_section_count = -1;
3886 bfd_vma last_vma = 0, last_dwarf = 0;
3887 bfd_size_type amt = i * sizeof (struct adjusted_section);
3889 p = (struct adjusted_section *) bfd_malloc (amt);
3893 stash->adjusted_sections = p;
3894 stash->adjusted_section_count = i;
3901 for (sect = abfd->sections; sect != NULL; sect = sect->next)
3906 if ((sect->output_section != NULL
3907 && sect->output_section != sect
3908 && (sect->flags & SEC_DEBUGGING) == 0)
3912 is_debug_info = (strcmp (sect->name, debug_info_name) == 0
3913 || CONST_STRNEQ (sect->name, GNU_LINKONCE_INFO));
3915 if (!((sect->flags & SEC_ALLOC) != 0 && abfd == orig_bfd)
3919 sz = sect->rawsize ? sect->rawsize : sect->size;
3923 BFD_ASSERT (sect->alignment_power == 0);
3924 sect->vma = last_dwarf;
3929 /* Align the new address to the current section
3931 last_vma = ((last_vma
3932 + ~(-((bfd_vma) 1 << sect->alignment_power)))
3933 & (-((bfd_vma) 1 << sect->alignment_power)));
3934 sect->vma = last_vma;
3939 p->adj_vma = sect->vma;
3942 if (abfd == stash->bfd_ptr)
3944 abfd = stash->bfd_ptr;
3948 if (orig_bfd != stash->bfd_ptr)
3949 set_debug_vma (orig_bfd, stash->bfd_ptr);
3954 /* Look up a funcinfo by name using the given info hash table. If found,
3955 also update the locations pointed to by filename_ptr and linenumber_ptr.
3957 This function returns TRUE if a funcinfo that matches the given symbol
3958 and address is found with any error; otherwise it returns FALSE. */
3961 info_hash_lookup_funcinfo (struct info_hash_table *hash_table,
3964 const char **filename_ptr,
3965 unsigned int *linenumber_ptr)
3967 struct funcinfo* each_func;
3968 struct funcinfo* best_fit = NULL;
3969 bfd_vma best_fit_len = 0;
3970 struct info_list_node *node;
3971 struct arange *arange;
3972 const char *name = bfd_asymbol_name (sym);
3973 asection *sec = bfd_get_section (sym);
3975 for (node = lookup_info_hash_table (hash_table, name);
3979 each_func = (struct funcinfo *) node->info;
3980 for (arange = &each_func->arange;
3982 arange = arange->next)
3984 if ((!each_func->sec || each_func->sec == sec)
3985 && addr >= arange->low
3986 && addr < arange->high
3988 || arange->high - arange->low < best_fit_len))
3990 best_fit = each_func;
3991 best_fit_len = arange->high - arange->low;
3998 best_fit->sec = sec;
3999 *filename_ptr = best_fit->file;
4000 *linenumber_ptr = best_fit->line;
4007 /* Look up a varinfo by name using the given info hash table. If found,
4008 also update the locations pointed to by filename_ptr and linenumber_ptr.
4010 This function returns TRUE if a varinfo that matches the given symbol
4011 and address is found with any error; otherwise it returns FALSE. */
4014 info_hash_lookup_varinfo (struct info_hash_table *hash_table,
4017 const char **filename_ptr,
4018 unsigned int *linenumber_ptr)
4020 const char *name = bfd_asymbol_name (sym);
4021 asection *sec = bfd_get_section (sym);
4022 struct varinfo* each;
4023 struct info_list_node *node;
4025 for (node = lookup_info_hash_table (hash_table, name);
4029 each = (struct varinfo *) node->info;
4030 if (each->addr == addr
4031 && (!each->sec || each->sec == sec))
4034 *filename_ptr = each->file;
4035 *linenumber_ptr = each->line;
4043 /* Update the funcinfo and varinfo info hash tables if they are
4044 not up to date. Returns TRUE if there is no error; otherwise
4045 returns FALSE and disable the info hash tables. */
4048 stash_maybe_update_info_hash_tables (struct dwarf2_debug *stash)
4050 struct comp_unit *each;
4052 /* Exit if hash tables are up-to-date. */
4053 if (stash->all_comp_units == stash->hash_units_head)
4056 if (stash->hash_units_head)
4057 each = stash->hash_units_head->prev_unit;
4059 each = stash->last_comp_unit;
4063 if (!comp_unit_hash_info (stash, each, stash->funcinfo_hash_table,
4064 stash->varinfo_hash_table))
4066 stash->info_hash_status = STASH_INFO_HASH_DISABLED;
4069 each = each->prev_unit;
4072 stash->hash_units_head = stash->all_comp_units;
4076 /* Check consistency of info hash tables. This is for debugging only. */
4078 static void ATTRIBUTE_UNUSED
4079 stash_verify_info_hash_table (struct dwarf2_debug *stash)
4081 struct comp_unit *each_unit;
4082 struct funcinfo *each_func;
4083 struct varinfo *each_var;
4084 struct info_list_node *node;
4087 for (each_unit = stash->all_comp_units;
4089 each_unit = each_unit->next_unit)
4091 for (each_func = each_unit->function_table;
4093 each_func = each_func->prev_func)
4095 if (!each_func->name)
4097 node = lookup_info_hash_table (stash->funcinfo_hash_table,
4101 while (node && !found)
4103 found = node->info == each_func;
4109 for (each_var = each_unit->variable_table;
4111 each_var = each_var->prev_var)
4113 if (!each_var->name || !each_var->file || each_var->stack)
4115 node = lookup_info_hash_table (stash->varinfo_hash_table,
4119 while (node && !found)
4121 found = node->info == each_var;
4129 /* Check to see if we want to enable the info hash tables, which consume
4130 quite a bit of memory. Currently we only check the number times
4131 bfd_dwarf2_find_line is called. In the future, we may also want to
4132 take the number of symbols into account. */
4135 stash_maybe_enable_info_hash_tables (bfd *abfd, struct dwarf2_debug *stash)
4137 BFD_ASSERT (stash->info_hash_status == STASH_INFO_HASH_OFF);
4139 if (stash->info_hash_count++ < STASH_INFO_HASH_TRIGGER)
4142 /* FIXME: Maybe we should check the reduce_memory_overheads
4143 and optimize fields in the bfd_link_info structure ? */
4145 /* Create hash tables. */
4146 stash->funcinfo_hash_table = create_info_hash_table (abfd);
4147 stash->varinfo_hash_table = create_info_hash_table (abfd);
4148 if (!stash->funcinfo_hash_table || !stash->varinfo_hash_table)
4150 /* Turn off info hashes if any allocation above fails. */
4151 stash->info_hash_status = STASH_INFO_HASH_DISABLED;
4154 /* We need a forced update so that the info hash tables will
4155 be created even though there is no compilation unit. That
4156 happens if STASH_INFO_HASH_TRIGGER is 0. */
4157 stash_maybe_update_info_hash_tables (stash);
4158 stash->info_hash_status = STASH_INFO_HASH_ON;
4161 /* Find the file and line associated with a symbol and address using the
4162 info hash tables of a stash. If there is a match, the function returns
4163 TRUE and update the locations pointed to by filename_ptr and linenumber_ptr;
4164 otherwise it returns FALSE. */
4167 stash_find_line_fast (struct dwarf2_debug *stash,
4170 const char **filename_ptr,
4171 unsigned int *linenumber_ptr)
4173 BFD_ASSERT (stash->info_hash_status == STASH_INFO_HASH_ON);
4175 if (sym->flags & BSF_FUNCTION)
4176 return info_hash_lookup_funcinfo (stash->funcinfo_hash_table, sym, addr,
4177 filename_ptr, linenumber_ptr);
4178 return info_hash_lookup_varinfo (stash->varinfo_hash_table, sym, addr,
4179 filename_ptr, linenumber_ptr);
4182 /* Save current section VMAs. */
4185 save_section_vma (const bfd *abfd, struct dwarf2_debug *stash)
4190 if (abfd->section_count == 0)
4192 stash->sec_vma = bfd_malloc (sizeof (*stash->sec_vma) * abfd->section_count);
4193 if (stash->sec_vma == NULL)
4195 for (i = 0, s = abfd->sections; i < abfd->section_count; i++, s = s->next)
4197 if (s->output_section != NULL)
4198 stash->sec_vma[i] = s->output_section->vma + s->output_offset;
4200 stash->sec_vma[i] = s->vma;
4205 /* Compare current section VMAs against those at the time the stash
4206 was created. If find_nearest_line is used in linker warnings or
4207 errors early in the link process, the debug info stash will be
4208 invalid for later calls. This is because we relocate debug info
4209 sections, so the stashed section contents depend on symbol values,
4210 which in turn depend on section VMAs. */
4213 section_vma_same (const bfd *abfd, const struct dwarf2_debug *stash)
4218 for (i = 0, s = abfd->sections; i < abfd->section_count; i++, s = s->next)
4222 if (s->output_section != NULL)
4223 vma = s->output_section->vma + s->output_offset;
4226 if (vma != stash->sec_vma[i])
4232 /* Read debug information from DEBUG_BFD when DEBUG_BFD is specified.
4233 If DEBUG_BFD is not specified, we read debug information from ABFD
4234 or its gnu_debuglink. The results will be stored in PINFO.
4235 The function returns TRUE iff debug information is ready. */
4238 _bfd_dwarf2_slurp_debug_info (bfd *abfd, bfd *debug_bfd,
4239 const struct dwarf_debug_section *debug_sections,
4242 bfd_boolean do_place)
4244 bfd_size_type amt = sizeof (struct dwarf2_debug);
4245 bfd_size_type total_size;
4247 struct dwarf2_debug *stash = (struct dwarf2_debug *) *pinfo;
4251 if (stash->orig_bfd == abfd
4252 && section_vma_same (abfd, stash))
4254 /* Check that we did previously find some debug information
4255 before attempting to make use of it. */
4256 if (stash->bfd_ptr != NULL)
4258 if (do_place && !place_sections (abfd, stash))
4265 _bfd_dwarf2_cleanup_debug_info (abfd, pinfo);
4266 memset (stash, 0, amt);
4270 stash = (struct dwarf2_debug *) bfd_zalloc (abfd, amt);
4274 stash->orig_bfd = abfd;
4275 stash->debug_sections = debug_sections;
4276 stash->syms = symbols;
4277 if (!save_section_vma (abfd, stash))
4282 if (debug_bfd == NULL)
4285 msec = find_debug_info (debug_bfd, debug_sections, NULL);
4286 if (msec == NULL && abfd == debug_bfd)
4288 char * debug_filename;
4290 debug_filename = bfd_follow_build_id_debuglink (abfd, DEBUGDIR);
4291 if (debug_filename == NULL)
4292 debug_filename = bfd_follow_gnu_debuglink (abfd, DEBUGDIR);
4294 if (debug_filename == NULL)
4295 /* No dwarf2 info, and no gnu_debuglink to follow.
4296 Note that at this point the stash has been allocated, but
4297 contains zeros. This lets future calls to this function
4298 fail more quickly. */
4301 /* Set BFD_DECOMPRESS to decompress debug sections. */
4302 if ((debug_bfd = bfd_openr (debug_filename, NULL)) == NULL
4303 || !(debug_bfd->flags |= BFD_DECOMPRESS,
4304 bfd_check_format (debug_bfd, bfd_object))
4305 || (msec = find_debug_info (debug_bfd,
4306 debug_sections, NULL)) == NULL
4307 || !bfd_generic_link_read_symbols (debug_bfd))
4310 bfd_close (debug_bfd);
4311 /* FIXME: Should we report our failure to follow the debuglink ? */
4312 free (debug_filename);
4316 symbols = bfd_get_outsymbols (debug_bfd);
4317 stash->syms = symbols;
4318 stash->close_on_cleanup = TRUE;
4320 stash->bfd_ptr = debug_bfd;
4323 && !place_sections (abfd, stash))
4326 /* There can be more than one DWARF2 info section in a BFD these
4327 days. First handle the easy case when there's only one. If
4328 there's more than one, try case two: none of the sections is
4329 compressed. In that case, read them all in and produce one
4330 large stash. We do this in two passes - in the first pass we
4331 just accumulate the section sizes, and in the second pass we
4332 read in the section's contents. (The allows us to avoid
4333 reallocing the data as we add sections to the stash.) If
4334 some or all sections are compressed, then do things the slow
4335 way, with a bunch of reallocs. */
4337 if (! find_debug_info (debug_bfd, debug_sections, msec))
4339 /* Case 1: only one info section. */
4340 total_size = msec->size;
4341 if (! read_section (debug_bfd, &stash->debug_sections[debug_info],
4343 &stash->info_ptr_memory, &total_size))
4348 /* Case 2: multiple sections. */
4349 for (total_size = 0;
4351 msec = find_debug_info (debug_bfd, debug_sections, msec))
4352 total_size += msec->size;
4354 stash->info_ptr_memory = (bfd_byte *) bfd_malloc (total_size);
4355 if (stash->info_ptr_memory == NULL)
4359 for (msec = find_debug_info (debug_bfd, debug_sections, NULL);
4361 msec = find_debug_info (debug_bfd, debug_sections, msec))
4369 if (!(bfd_simple_get_relocated_section_contents
4370 (debug_bfd, msec, stash->info_ptr_memory + total_size,
4378 stash->info_ptr = stash->info_ptr_memory;
4379 stash->info_ptr_end = stash->info_ptr + total_size;
4380 stash->sec = find_debug_info (debug_bfd, debug_sections, NULL);
4381 stash->sec_info_ptr = stash->info_ptr;
4385 /* Scan the debug information in PINFO looking for a DW_TAG_subprogram
4386 abbrev with a DW_AT_low_pc attached to it. Then lookup that same
4387 symbol in SYMBOLS and return the difference between the low_pc and
4388 the symbol's address. Returns 0 if no suitable symbol could be found. */
4391 _bfd_dwarf2_find_symbol_bias (asymbol ** symbols, void ** pinfo)
4393 struct dwarf2_debug *stash;
4394 struct comp_unit * unit;
4396 stash = (struct dwarf2_debug *) *pinfo;
4401 for (unit = stash->all_comp_units; unit; unit = unit->next_unit)
4403 struct funcinfo * func;
4405 if (unit->function_table == NULL)
4407 if (unit->line_table == NULL)
4408 unit->line_table = decode_line_info (unit, stash);
4409 if (unit->line_table != NULL)
4410 scan_unit_for_symbols (unit);
4413 for (func = unit->function_table; func != NULL; func = func->prev_func)
4414 if (func->name && func->arange.low)
4418 /* FIXME: Do we need to scan the aranges looking for the lowest pc value ? */
4420 for (psym = symbols; * psym != NULL; psym++)
4422 asymbol * sym = * psym;
4424 if (sym->flags & BSF_FUNCTION
4425 && sym->section != NULL
4426 && strcmp (sym->name, func->name) == 0)
4427 return ((bfd_signed_vma) func->arange.low) -
4428 ((bfd_signed_vma) (sym->value + sym->section->vma));
4436 /* Find the source code location of SYMBOL. If SYMBOL is NULL
4437 then find the nearest source code location corresponding to
4438 the address SECTION + OFFSET.
4439 Returns TRUE if the line is found without error and fills in
4440 FILENAME_PTR and LINENUMBER_PTR. In the case where SYMBOL was
4441 NULL the FUNCTIONNAME_PTR is also filled in.
4442 SYMBOLS contains the symbol table for ABFD.
4443 DEBUG_SECTIONS contains the name of the dwarf debug sections.
4444 ADDR_SIZE is the number of bytes in the initial .debug_info length
4445 field and in the abbreviation offset, or zero to indicate that the
4446 default value should be used. */
4449 _bfd_dwarf2_find_nearest_line (bfd *abfd,
4454 const char **filename_ptr,
4455 const char **functionname_ptr,
4456 unsigned int *linenumber_ptr,
4457 unsigned int *discriminator_ptr,
4458 const struct dwarf_debug_section *debug_sections,
4459 unsigned int addr_size,
4462 /* Read each compilation unit from the section .debug_info, and check
4463 to see if it contains the address we are searching for. If yes,
4464 lookup the address, and return the line number info. If no, go
4465 on to the next compilation unit.
4467 We keep a list of all the previously read compilation units, and
4468 a pointer to the next un-read compilation unit. Check the
4469 previously read units before reading more. */
4470 struct dwarf2_debug *stash;
4471 /* What address are we looking for? */
4473 struct comp_unit* each;
4474 struct funcinfo *function = NULL;
4475 bfd_boolean found = FALSE;
4476 bfd_boolean do_line;
4478 *filename_ptr = NULL;
4479 if (functionname_ptr != NULL)
4480 *functionname_ptr = NULL;
4481 *linenumber_ptr = 0;
4482 if (discriminator_ptr)
4483 *discriminator_ptr = 0;
4485 if (! _bfd_dwarf2_slurp_debug_info (abfd, NULL, debug_sections,
4487 (abfd->flags & (EXEC_P | DYNAMIC)) == 0))
4490 stash = (struct dwarf2_debug *) *pinfo;
4492 do_line = symbol != NULL;
4495 BFD_ASSERT (section == NULL && offset == 0 && functionname_ptr == NULL);
4496 section = bfd_get_section (symbol);
4497 addr = symbol->value;
4501 BFD_ASSERT (section != NULL && functionname_ptr != NULL);
4504 /* If we have no SYMBOL but the section we're looking at is not a
4505 code section, then take a look through the list of symbols to see
4506 if we have a symbol at the address we're looking for. If we do
4507 then use this to look up line information. This will allow us to
4508 give file and line results for data symbols. We exclude code
4509 symbols here, if we look up a function symbol and then look up the
4510 line information we'll actually return the line number for the
4511 opening '{' rather than the function definition line. This is
4512 because looking up by symbol uses the line table, in which the
4513 first line for a function is usually the opening '{', while
4514 looking up the function by section + offset uses the
4515 DW_AT_decl_line from the function DW_TAG_subprogram for the line,
4516 which will be the line of the function name. */
4517 if (symbols != NULL && (section->flags & SEC_CODE) == 0)
4521 for (tmp = symbols; (*tmp) != NULL; ++tmp)
4522 if ((*tmp)->the_bfd == abfd
4523 && (*tmp)->section == section
4524 && (*tmp)->value == offset
4525 && ((*tmp)->flags & BSF_SECTION_SYM) == 0)
4529 /* For local symbols, keep going in the hope we find a
4531 if ((symbol->flags & BSF_GLOBAL) != 0)
4537 if (section->output_section)
4538 addr += section->output_section->vma + section->output_offset;
4540 addr += section->vma;
4542 /* A null info_ptr indicates that there is no dwarf2 info
4543 (or that an error occured while setting up the stash). */
4544 if (! stash->info_ptr)
4547 stash->inliner_chain = NULL;
4549 /* Check the previously read comp. units first. */
4552 /* The info hash tables use quite a bit of memory. We may not want to
4553 always use them. We use some heuristics to decide if and when to
4555 if (stash->info_hash_status == STASH_INFO_HASH_OFF)
4556 stash_maybe_enable_info_hash_tables (abfd, stash);
4558 /* Keep info hash table up to date if they are available. Note that we
4559 may disable the hash tables if there is any error duing update. */
4560 if (stash->info_hash_status == STASH_INFO_HASH_ON)
4561 stash_maybe_update_info_hash_tables (stash);
4563 if (stash->info_hash_status == STASH_INFO_HASH_ON)
4565 found = stash_find_line_fast (stash, symbol, addr, filename_ptr,
4572 /* Check the previously read comp. units first. */
4573 for (each = stash->all_comp_units; each; each = each->next_unit)
4574 if ((symbol->flags & BSF_FUNCTION) == 0
4575 || each->arange.high == 0
4576 || comp_unit_contains_address (each, addr))
4578 found = comp_unit_find_line (each, symbol, addr, filename_ptr,
4579 linenumber_ptr, stash);
4587 bfd_vma min_range = (bfd_vma) -1;
4588 const char * local_filename = NULL;
4589 struct funcinfo *local_function = NULL;
4590 unsigned int local_linenumber = 0;
4591 unsigned int local_discriminator = 0;
4593 for (each = stash->all_comp_units; each; each = each->next_unit)
4595 bfd_vma range = (bfd_vma) -1;
4597 found = ((each->arange.high == 0
4598 || comp_unit_contains_address (each, addr))
4599 && (range = comp_unit_find_nearest_line (each, addr,
4603 & local_discriminator,
4607 /* PRs 15935 15994: Bogus debug information may have provided us
4608 with an erroneous match. We attempt to counter this by
4609 selecting the match that has the smallest address range
4610 associated with it. (We are assuming that corrupt debug info
4611 will tend to result in extra large address ranges rather than
4612 extra small ranges).
4614 This does mean that we scan through all of the CUs associated
4615 with the bfd each time this function is called. But this does
4616 have the benefit of producing consistent results every time the
4617 function is called. */
4618 if (range <= min_range)
4620 if (filename_ptr && local_filename)
4621 * filename_ptr = local_filename;
4623 function = local_function;
4624 if (discriminator_ptr && local_discriminator)
4625 * discriminator_ptr = local_discriminator;
4626 if (local_linenumber)
4627 * linenumber_ptr = local_linenumber;
4633 if (* linenumber_ptr)
4640 /* The DWARF2 spec says that the initial length field, and the
4641 offset of the abbreviation table, should both be 4-byte values.
4642 However, some compilers do things differently. */
4645 BFD_ASSERT (addr_size == 4 || addr_size == 8);
4647 /* Read each remaining comp. units checking each as they are read. */
4648 while (stash->info_ptr < stash->info_ptr_end)
4651 unsigned int offset_size = addr_size;
4652 bfd_byte *info_ptr_unit = stash->info_ptr;
4654 length = read_4_bytes (stash->bfd_ptr, stash->info_ptr, stash->info_ptr_end);
4655 /* A 0xffffff length is the DWARF3 way of indicating
4656 we use 64-bit offsets, instead of 32-bit offsets. */
4657 if (length == 0xffffffff)
4660 length = read_8_bytes (stash->bfd_ptr, stash->info_ptr + 4, stash->info_ptr_end);
4661 stash->info_ptr += 12;
4663 /* A zero length is the IRIX way of indicating 64-bit offsets,
4664 mostly because the 64-bit length will generally fit in 32
4665 bits, and the endianness helps. */
4666 else if (length == 0)
4669 length = read_4_bytes (stash->bfd_ptr, stash->info_ptr + 4, stash->info_ptr_end);
4670 stash->info_ptr += 8;
4672 /* In the absence of the hints above, we assume 32-bit DWARF2
4673 offsets even for targets with 64-bit addresses, because:
4674 a) most of the time these targets will not have generated
4675 more than 2Gb of debug info and so will not need 64-bit
4678 b) if they do use 64-bit offsets but they are not using
4679 the size hints that are tested for above then they are
4680 not conforming to the DWARF3 standard anyway. */
4681 else if (addr_size == 8)
4684 stash->info_ptr += 4;
4687 stash->info_ptr += 4;
4694 if (stash->info_ptr + length > stash->info_ptr_end)
4697 each = parse_comp_unit (stash, length, info_ptr_unit,
4700 /* The dwarf information is damaged, don't trust it any
4704 new_ptr = stash->info_ptr + length;
4705 /* PR 17512: file: 1500698c. */
4706 if (new_ptr < stash->info_ptr)
4708 /* A corrupt length value - do not trust the info any more. */
4713 stash->info_ptr = new_ptr;
4715 if (stash->all_comp_units)
4716 stash->all_comp_units->prev_unit = each;
4718 stash->last_comp_unit = each;
4720 each->next_unit = stash->all_comp_units;
4721 stash->all_comp_units = each;
4723 /* DW_AT_low_pc and DW_AT_high_pc are optional for
4724 compilation units. If we don't have them (i.e.,
4725 unit->high == 0), we need to consult the line info table
4726 to see if a compilation unit contains the given
4729 found = (((symbol->flags & BSF_FUNCTION) == 0
4730 || each->arange.high == 0
4731 || comp_unit_contains_address (each, addr))
4732 && comp_unit_find_line (each, symbol, addr,
4737 found = ((each->arange.high == 0
4738 || comp_unit_contains_address (each, addr))
4739 && comp_unit_find_nearest_line (each, addr,
4746 if ((bfd_vma) (stash->info_ptr - stash->sec_info_ptr)
4747 == stash->sec->size)
4749 stash->sec = find_debug_info (stash->bfd_ptr, debug_sections,
4751 stash->sec_info_ptr = stash->info_ptr;
4762 if (!function->is_linkage)
4767 fun = _bfd_elf_find_function (abfd, symbols, section, offset,
4768 *filename_ptr ? NULL : filename_ptr,
4770 sec_vma = section->vma;
4771 if (section->output_section != NULL)
4772 sec_vma = section->output_section->vma + section->output_offset;
4774 && fun->value + sec_vma == function->arange.low)
4775 function->name = *functionname_ptr;
4776 /* Even if we didn't find a linkage name, say that we have
4777 to stop a repeated search of symbols. */
4778 function->is_linkage = TRUE;
4780 *functionname_ptr = function->name;
4782 if ((abfd->flags & (EXEC_P | DYNAMIC)) == 0)
4783 unset_sections (stash);
4789 _bfd_dwarf2_find_inliner_info (bfd *abfd ATTRIBUTE_UNUSED,
4790 const char **filename_ptr,
4791 const char **functionname_ptr,
4792 unsigned int *linenumber_ptr,
4795 struct dwarf2_debug *stash;
4797 stash = (struct dwarf2_debug *) *pinfo;
4800 struct funcinfo *func = stash->inliner_chain;
4802 if (func && func->caller_func)
4804 *filename_ptr = func->caller_file;
4805 *functionname_ptr = func->caller_func->name;
4806 *linenumber_ptr = func->caller_line;
4807 stash->inliner_chain = func->caller_func;
4816 _bfd_dwarf2_cleanup_debug_info (bfd *abfd, void **pinfo)
4818 struct dwarf2_debug *stash = (struct dwarf2_debug *) *pinfo;
4819 struct comp_unit *each;
4821 if (abfd == NULL || stash == NULL)
4824 for (each = stash->all_comp_units; each; each = each->next_unit)
4826 struct abbrev_info **abbrevs = each->abbrevs;
4827 struct funcinfo *function_table = each->function_table;
4828 struct varinfo *variable_table = each->variable_table;
4831 for (i = 0; i < ABBREV_HASH_SIZE; i++)
4833 struct abbrev_info *abbrev = abbrevs[i];
4837 free (abbrev->attrs);
4838 abbrev = abbrev->next;
4842 if (each->line_table)
4844 free (each->line_table->dirs);
4845 free (each->line_table->files);
4848 while (function_table)
4850 if (function_table->file)
4852 free (function_table->file);
4853 function_table->file = NULL;
4856 if (function_table->caller_file)
4858 free (function_table->caller_file);
4859 function_table->caller_file = NULL;
4861 function_table = function_table->prev_func;
4864 if (each->lookup_funcinfo_table)
4866 free (each->lookup_funcinfo_table);
4867 each->lookup_funcinfo_table = NULL;
4870 while (variable_table)
4872 if (variable_table->file)
4874 free (variable_table->file);
4875 variable_table->file = NULL;
4878 variable_table = variable_table->prev_var;
4882 if (stash->dwarf_abbrev_buffer)
4883 free (stash->dwarf_abbrev_buffer);
4884 if (stash->dwarf_line_buffer)
4885 free (stash->dwarf_line_buffer);
4886 if (stash->dwarf_str_buffer)
4887 free (stash->dwarf_str_buffer);
4888 if (stash->dwarf_line_str_buffer)
4889 free (stash->dwarf_line_str_buffer);
4890 if (stash->dwarf_ranges_buffer)
4891 free (stash->dwarf_ranges_buffer);
4892 if (stash->info_ptr_memory)
4893 free (stash->info_ptr_memory);
4894 if (stash->close_on_cleanup)
4895 bfd_close (stash->bfd_ptr);
4896 if (stash->alt_dwarf_str_buffer)
4897 free (stash->alt_dwarf_str_buffer);
4898 if (stash->alt_dwarf_info_buffer)
4899 free (stash->alt_dwarf_info_buffer);
4901 free (stash->sec_vma);
4902 if (stash->adjusted_sections)
4903 free (stash->adjusted_sections);
4904 if (stash->alt_bfd_ptr)
4905 bfd_close (stash->alt_bfd_ptr);
4908 /* Find the function to a particular section and offset,
4909 for error reporting. */
4912 _bfd_elf_find_function (bfd *abfd,
4916 const char **filename_ptr,
4917 const char **functionname_ptr)
4919 struct elf_find_function_cache
4921 asection *last_section;
4923 const char *filename;
4924 bfd_size_type func_size;
4927 if (symbols == NULL)
4930 if (bfd_get_flavour (abfd) != bfd_target_elf_flavour)
4933 cache = elf_tdata (abfd)->elf_find_function_cache;
4936 cache = bfd_zalloc (abfd, sizeof (*cache));
4937 elf_tdata (abfd)->elf_find_function_cache = cache;
4941 if (cache->last_section != section
4942 || cache->func == NULL
4943 || offset < cache->func->value
4944 || offset >= cache->func->value + cache->func_size)
4949 /* ??? Given multiple file symbols, it is impossible to reliably
4950 choose the right file name for global symbols. File symbols are
4951 local symbols, and thus all file symbols must sort before any
4952 global symbols. The ELF spec may be interpreted to say that a
4953 file symbol must sort before other local symbols, but currently
4954 ld -r doesn't do this. So, for ld -r output, it is possible to
4955 make a better choice of file name for local symbols by ignoring
4956 file symbols appearing after a given local symbol. */
4957 enum { nothing_seen, symbol_seen, file_after_symbol_seen } state;
4958 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
4962 state = nothing_seen;
4963 cache->filename = NULL;
4965 cache->func_size = 0;
4966 cache->last_section = section;
4968 for (p = symbols; *p != NULL; p++)
4974 if ((sym->flags & BSF_FILE) != 0)
4977 if (state == symbol_seen)
4978 state = file_after_symbol_seen;
4982 size = bed->maybe_function_sym (sym, section, &code_off);
4984 && code_off <= offset
4985 && (code_off > low_func
4986 || (code_off == low_func
4987 && size > cache->func_size)))
4990 cache->func_size = size;
4991 cache->filename = NULL;
4992 low_func = code_off;
4994 && ((sym->flags & BSF_LOCAL) != 0
4995 || state != file_after_symbol_seen))
4996 cache->filename = bfd_asymbol_name (file);
4998 if (state == nothing_seen)
4999 state = symbol_seen;
5003 if (cache->func == NULL)
5007 *filename_ptr = cache->filename;
5008 if (functionname_ptr)
5009 *functionname_ptr = bfd_asymbol_name (cache->func);