2 Copyright (C) 1994-2019 Free Software Foundation, Inc.
4 Adapted from gdb/dwarf2read.c by Gavin Koch of Cygnus Solutions
7 From the dwarf2read.c header:
8 Adapted by Gary Funck (gary@intrepid.com), Intrepid Technology,
9 Inc. with support from Florida State University (under contract
10 with the Ada Joint Program Office), and Silicon Graphics, Inc.
11 Initial contribution by Brent Benson, Harris Computer Systems, Inc.,
12 based on Fred Fish's (Cygnus Support) implementation of DWARF 1
13 support in dwarfread.c
15 This file is part of BFD.
17 This program is free software; you can redistribute it and/or modify
18 it under the terms of the GNU General Public License as published by
19 the Free Software Foundation; either version 3 of the License, or (at
20 your option) any later version.
22 This program is distributed in the hope that it will be useful, but
23 WITHOUT ANY WARRANTY; without even the implied warranty of
24 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
25 General Public License for more details.
27 You should have received a copy of the GNU General Public License
28 along with this program; if not, write to the Free Software
29 Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
30 MA 02110-1301, USA. */
34 #include "libiberty.h"
39 /* The data in the .debug_line statement prologue looks like this. */
44 unsigned short version;
45 bfd_vma prologue_length;
46 unsigned char minimum_instruction_length;
47 unsigned char maximum_ops_per_insn;
48 unsigned char default_is_stmt;
50 unsigned char line_range;
51 unsigned char opcode_base;
52 unsigned char *standard_opcode_lengths;
55 /* Attributes have a name and a value. */
59 enum dwarf_attribute name;
64 struct dwarf_block *blk;
71 /* Blocks are a bunch of untyped bytes. */
78 struct adjusted_section
86 /* A list of all previously read comp_units. */
87 struct comp_unit *all_comp_units;
89 /* Last comp unit in list above. */
90 struct comp_unit *last_comp_unit;
92 /* Names of the debug sections. */
93 const struct dwarf_debug_section *debug_sections;
95 /* The next unread compilation unit within the .debug_info section.
96 Zero indicates that the .debug_info section has not been loaded
100 /* Pointer to the end of the .debug_info section memory buffer. */
101 bfd_byte *info_ptr_end;
103 /* Pointer to the 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. */
129 bfd_byte *info_ptr_memory;
131 /* Pointer to the symbol table. */
134 /* Pointer to the .debug_abbrev section loaded into memory. */
135 bfd_byte *dwarf_abbrev_buffer;
137 /* Length of the loaded .debug_abbrev section. */
138 bfd_size_type dwarf_abbrev_size;
140 /* Buffer for decode_line_info. */
141 bfd_byte *dwarf_line_buffer;
143 /* Length of the loaded .debug_line section. */
144 bfd_size_type dwarf_line_size;
146 /* Pointer to the .debug_str section loaded into memory. */
147 bfd_byte *dwarf_str_buffer;
149 /* Length of the loaded .debug_str section. */
150 bfd_size_type dwarf_str_size;
152 /* Pointer to the .debug_line_str section loaded into memory. */
153 bfd_byte *dwarf_line_str_buffer;
155 /* Length of the loaded .debug_line_str section. */
156 bfd_size_type dwarf_line_str_size;
158 /* Pointer to the .debug_ranges section loaded into memory. */
159 bfd_byte *dwarf_ranges_buffer;
161 /* Length of the loaded .debug_ranges section. */
162 bfd_size_type dwarf_ranges_size;
164 /* If the most recent call to bfd_find_nearest_line was given an
165 address in an inlined function, preserve a pointer into the
166 calling chain for subsequent calls to bfd_find_inliner_info to
168 struct funcinfo *inliner_chain;
170 /* Section VMAs at the time the stash was built. */
172 /* Number of sections in the SEC_VMA table. */
173 unsigned int sec_vma_count;
175 /* Number of sections whose VMA we must adjust. */
176 int adjusted_section_count;
178 /* Array of sections with adjusted VMA. */
179 struct adjusted_section *adjusted_sections;
181 /* Number of times find_line is called. This is used in
182 the heuristic for enabling the info hash tables. */
185 #define STASH_INFO_HASH_TRIGGER 100
187 /* Hash table mapping symbol names to function infos. */
188 struct info_hash_table *funcinfo_hash_table;
190 /* Hash table mapping symbol names to variable infos. */
191 struct info_hash_table *varinfo_hash_table;
193 /* Head of comp_unit list in the last hash table update. */
194 struct comp_unit *hash_units_head;
196 /* Status of info hash. */
197 int info_hash_status;
198 #define STASH_INFO_HASH_OFF 0
199 #define STASH_INFO_HASH_ON 1
200 #define STASH_INFO_HASH_DISABLED 2
202 /* True if we opened bfd_ptr. */
203 bfd_boolean close_on_cleanup;
213 /* A minimal decoding of DWARF2 compilation units. We only decode
214 what's needed to get to the line number information. */
218 /* Chain the previously read compilation units. */
219 struct comp_unit *next_unit;
221 /* Likewise, chain the compilation unit read after this one.
222 The comp units are stored in reversed reading order. */
223 struct comp_unit *prev_unit;
225 /* Keep the bfd convenient (for memory allocation). */
228 /* The lowest and highest addresses contained in this compilation
229 unit as specified in the compilation unit header. */
230 struct arange arange;
232 /* The DW_AT_name attribute (for error messages). */
235 /* The abbrev hash table. */
236 struct abbrev_info **abbrevs;
238 /* DW_AT_language. */
241 /* Note that an error was found by comp_unit_find_nearest_line. */
244 /* The DW_AT_comp_dir attribute. */
247 /* TRUE if there is a line number table associated with this comp. unit. */
250 /* Pointer to the current comp_unit so that we can find a given entry
252 bfd_byte *info_ptr_unit;
254 /* The offset into .debug_line of the line number table. */
255 unsigned long line_offset;
257 /* Pointer to the first child die for the comp unit. */
258 bfd_byte *first_child_die_ptr;
260 /* The end of the comp unit. */
263 /* The decoded line number, NULL if not yet decoded. */
264 struct line_info_table *line_table;
266 /* A list of the functions found in this comp. unit. */
267 struct funcinfo *function_table;
269 /* A table of function information references searchable by address. */
270 struct lookup_funcinfo *lookup_funcinfo_table;
272 /* Number of functions in the function_table and sorted_function_table. */
273 bfd_size_type number_of_functions;
275 /* A list of the variables found in this comp. unit. */
276 struct varinfo *variable_table;
278 /* Pointer to dwarf2_debug structure. */
279 struct dwarf2_debug *stash;
281 /* DWARF format version for this unit - from unit header. */
284 /* Address size for this unit - from unit header. */
285 unsigned char addr_size;
287 /* Offset size for this unit - from unit header. */
288 unsigned char offset_size;
290 /* Base address for this unit - from DW_AT_low_pc attribute of
291 DW_TAG_compile_unit DIE */
292 bfd_vma base_address;
294 /* TRUE if symbols are cached in hash table for faster lookup by name. */
298 /* This data structure holds the information of an abbrev. */
301 unsigned int number; /* Number identifying abbrev. */
302 enum dwarf_tag tag; /* DWARF tag. */
303 int has_children; /* Boolean. */
304 unsigned int num_attrs; /* Number of attributes. */
305 struct attr_abbrev *attrs; /* An array of attribute descriptions. */
306 struct abbrev_info *next; /* Next in chain. */
311 enum dwarf_attribute name;
312 enum dwarf_form form;
313 bfd_vma implicit_const;
316 /* Map of uncompressed DWARF debug section name to compressed one. It
317 is terminated by NULL uncompressed_name. */
319 const struct dwarf_debug_section dwarf_debug_sections[] =
321 { ".debug_abbrev", ".zdebug_abbrev" },
322 { ".debug_aranges", ".zdebug_aranges" },
323 { ".debug_frame", ".zdebug_frame" },
324 { ".debug_info", ".zdebug_info" },
325 { ".debug_info", ".zdebug_info" },
326 { ".debug_line", ".zdebug_line" },
327 { ".debug_loc", ".zdebug_loc" },
328 { ".debug_macinfo", ".zdebug_macinfo" },
329 { ".debug_macro", ".zdebug_macro" },
330 { ".debug_pubnames", ".zdebug_pubnames" },
331 { ".debug_pubtypes", ".zdebug_pubtypes" },
332 { ".debug_ranges", ".zdebug_ranges" },
333 { ".debug_static_func", ".zdebug_static_func" },
334 { ".debug_static_vars", ".zdebug_static_vars" },
335 { ".debug_str", ".zdebug_str", },
336 { ".debug_str", ".zdebug_str", },
337 { ".debug_line_str", ".zdebug_line_str", },
338 { ".debug_types", ".zdebug_types" },
339 /* GNU DWARF 1 extensions */
340 { ".debug_sfnames", ".zdebug_sfnames" },
341 { ".debug_srcinfo", ".zebug_srcinfo" },
342 /* SGI/MIPS DWARF 2 extensions */
343 { ".debug_funcnames", ".zdebug_funcnames" },
344 { ".debug_typenames", ".zdebug_typenames" },
345 { ".debug_varnames", ".zdebug_varnames" },
346 { ".debug_weaknames", ".zdebug_weaknames" },
350 /* NB/ Numbers in this enum must match up with indices
351 into the dwarf_debug_sections[] array above. */
352 enum dwarf_debug_section_enum
381 /* A static assertion. */
382 extern int dwarf_debug_section_assert[ARRAY_SIZE (dwarf_debug_sections)
383 == debug_max + 1 ? 1 : -1];
385 #ifndef ABBREV_HASH_SIZE
386 #define ABBREV_HASH_SIZE 121
388 #ifndef ATTR_ALLOC_CHUNK
389 #define ATTR_ALLOC_CHUNK 4
392 /* Variable and function hash tables. This is used to speed up look-up
393 in lookup_symbol_in_var_table() and lookup_symbol_in_function_table().
394 In order to share code between variable and function infos, we use
395 a list of untyped pointer for all variable/function info associated with
396 a symbol. We waste a bit of memory for list with one node but that
397 simplifies the code. */
399 struct info_list_node
401 struct info_list_node *next;
405 /* Info hash entry. */
406 struct info_hash_entry
408 struct bfd_hash_entry root;
409 struct info_list_node *head;
412 struct info_hash_table
414 struct bfd_hash_table base;
417 /* Function to create a new entry in info hash table. */
419 static struct bfd_hash_entry *
420 info_hash_table_newfunc (struct bfd_hash_entry *entry,
421 struct bfd_hash_table *table,
424 struct info_hash_entry *ret = (struct info_hash_entry *) entry;
426 /* Allocate the structure if it has not already been allocated by a
430 ret = (struct info_hash_entry *) bfd_hash_allocate (table,
436 /* Call the allocation method of the base class. */
437 ret = ((struct info_hash_entry *)
438 bfd_hash_newfunc ((struct bfd_hash_entry *) ret, table, string));
440 /* Initialize the local fields here. */
444 return (struct bfd_hash_entry *) ret;
447 /* Function to create a new info hash table. It returns a pointer to the
448 newly created table or NULL if there is any error. We need abfd
449 solely for memory allocation. */
451 static struct info_hash_table *
452 create_info_hash_table (bfd *abfd)
454 struct info_hash_table *hash_table;
456 hash_table = ((struct info_hash_table *)
457 bfd_alloc (abfd, sizeof (struct info_hash_table)));
461 if (!bfd_hash_table_init (&hash_table->base, info_hash_table_newfunc,
462 sizeof (struct info_hash_entry)))
464 bfd_release (abfd, hash_table);
471 /* Insert an info entry into an info hash table. We do not check of
472 duplicate entries. Also, the caller need to guarantee that the
473 right type of info in inserted as info is passed as a void* pointer.
474 This function returns true if there is no error. */
477 insert_info_hash_table (struct info_hash_table *hash_table,
482 struct info_hash_entry *entry;
483 struct info_list_node *node;
485 entry = (struct info_hash_entry*) bfd_hash_lookup (&hash_table->base,
490 node = (struct info_list_node *) bfd_hash_allocate (&hash_table->base,
496 node->next = entry->head;
502 /* Look up an info entry list from an info hash table. Return NULL
505 static struct info_list_node *
506 lookup_info_hash_table (struct info_hash_table *hash_table, const char *key)
508 struct info_hash_entry *entry;
510 entry = (struct info_hash_entry*) bfd_hash_lookup (&hash_table->base, key,
512 return entry ? entry->head : NULL;
515 /* Read a section into its appropriate place in the dwarf2_debug
516 struct (indicated by SECTION_BUFFER and SECTION_SIZE). If SYMS is
517 not NULL, use bfd_simple_get_relocated_section_contents to read the
518 section contents, otherwise use bfd_get_section_contents. Fail if
519 the located section does not contain at least OFFSET bytes. */
522 read_section (bfd * abfd,
523 const struct dwarf_debug_section *sec,
526 bfd_byte ** section_buffer,
527 bfd_size_type * section_size)
530 const char *section_name = sec->uncompressed_name;
531 bfd_byte *contents = *section_buffer;
534 /* The section may have already been read. */
535 if (contents == 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;
553 /* Paranoia - alloc one extra so that we can make sure a string
554 section is NUL terminated. */
555 amt = *section_size + 1;
558 bfd_set_error (bfd_error_no_memory);
561 contents = (bfd_byte *) bfd_malloc (amt);
562 if (contents == NULL)
565 ? !bfd_simple_get_relocated_section_contents (abfd, msec, contents,
567 : !bfd_get_section_contents (abfd, msec, contents, 0, *section_size))
572 contents[*section_size] = 0;
573 *section_buffer = contents;
576 /* It is possible to get a bad value for the offset into the section
577 that the client wants. Validate it here to avoid trouble later. */
578 if (offset != 0 && offset >= *section_size)
580 /* xgettext: c-format */
581 _bfd_error_handler (_("DWARF error: offset (%" PRIu64 ")"
582 " greater than or equal to %s size (%" PRIu64 ")"),
583 (uint64_t) offset, section_name,
584 (uint64_t) *section_size);
585 bfd_set_error (bfd_error_bad_value);
592 /* Read dwarf information from a buffer. */
595 read_1_byte (bfd *abfd ATTRIBUTE_UNUSED, bfd_byte *buf, bfd_byte *end)
599 return bfd_get_8 (abfd, buf);
603 read_1_signed_byte (bfd *abfd ATTRIBUTE_UNUSED, bfd_byte *buf, bfd_byte *end)
607 return bfd_get_signed_8 (abfd, buf);
611 read_2_bytes (bfd *abfd, bfd_byte *buf, bfd_byte *end)
615 return bfd_get_16 (abfd, buf);
619 read_4_bytes (bfd *abfd, bfd_byte *buf, bfd_byte *end)
623 return bfd_get_32 (abfd, buf);
627 read_8_bytes (bfd *abfd, bfd_byte *buf, bfd_byte *end)
631 return bfd_get_64 (abfd, buf);
635 read_n_bytes (bfd_byte * buf,
637 struct dwarf_block * block)
639 unsigned int size = block->size;
640 bfd_byte * block_end = buf + size;
642 if (block_end > end || block_end < buf)
655 /* Scans a NUL terminated string starting at BUF, returning a pointer to it.
656 Returns the number of characters in the string, *including* the NUL byte,
657 in BYTES_READ_PTR. This value is set even if the function fails. Bytes
658 at or beyond BUF_END will not be read. Returns NULL if there was a
659 problem, or if the string is empty. */
662 read_string (bfd * abfd ATTRIBUTE_UNUSED,
665 unsigned int * bytes_read_ptr)
671 * bytes_read_ptr = 0;
677 * bytes_read_ptr = 1;
681 while (buf < buf_end)
684 * bytes_read_ptr = buf - str;
688 * bytes_read_ptr = buf - str;
692 /* Reads an offset from BUF and then locates the string at this offset
693 inside the debug string section. Returns a pointer to the string.
694 Returns the number of bytes read from BUF, *not* the length of the string,
695 in BYTES_READ_PTR. This value is set even if the function fails. Bytes
696 at or beyond BUF_END will not be read from BUF. Returns NULL if there was
697 a problem, or if the string is empty. Does not check for NUL termination
701 read_indirect_string (struct comp_unit * unit,
704 unsigned int * bytes_read_ptr)
707 struct dwarf2_debug *stash = unit->stash;
710 if (buf + unit->offset_size > buf_end)
712 * bytes_read_ptr = 0;
716 if (unit->offset_size == 4)
717 offset = read_4_bytes (unit->abfd, buf, buf_end);
719 offset = read_8_bytes (unit->abfd, buf, buf_end);
721 *bytes_read_ptr = unit->offset_size;
723 if (! read_section (unit->abfd, &stash->debug_sections[debug_str],
725 &stash->dwarf_str_buffer, &stash->dwarf_str_size))
728 if (offset >= stash->dwarf_str_size)
730 str = (char *) stash->dwarf_str_buffer + offset;
736 /* Like read_indirect_string but from .debug_line_str section. */
739 read_indirect_line_string (struct comp_unit * unit,
742 unsigned int * bytes_read_ptr)
745 struct dwarf2_debug *stash = unit->stash;
748 if (buf + unit->offset_size > buf_end)
750 * bytes_read_ptr = 0;
754 if (unit->offset_size == 4)
755 offset = read_4_bytes (unit->abfd, buf, buf_end);
757 offset = read_8_bytes (unit->abfd, buf, buf_end);
759 *bytes_read_ptr = unit->offset_size;
761 if (! read_section (unit->abfd, &stash->debug_sections[debug_line_str],
763 &stash->dwarf_line_str_buffer,
764 &stash->dwarf_line_str_size))
767 if (offset >= stash->dwarf_line_str_size)
769 str = (char *) stash->dwarf_line_str_buffer + offset;
775 /* Like read_indirect_string but uses a .debug_str located in
776 an alternate file pointed to by the .gnu_debugaltlink section.
777 Used to impement DW_FORM_GNU_strp_alt. */
780 read_alt_indirect_string (struct comp_unit * unit,
783 unsigned int * bytes_read_ptr)
786 struct dwarf2_debug *stash = unit->stash;
789 if (buf + unit->offset_size > buf_end)
791 * bytes_read_ptr = 0;
795 if (unit->offset_size == 4)
796 offset = read_4_bytes (unit->abfd, buf, buf_end);
798 offset = read_8_bytes (unit->abfd, buf, buf_end);
800 *bytes_read_ptr = unit->offset_size;
802 if (stash->alt_bfd_ptr == NULL)
805 char * debug_filename = bfd_follow_gnu_debugaltlink (unit->abfd, DEBUGDIR);
807 if (debug_filename == NULL)
810 if ((debug_bfd = bfd_openr (debug_filename, NULL)) == NULL
811 || ! bfd_check_format (debug_bfd, bfd_object))
814 bfd_close (debug_bfd);
816 /* FIXME: Should we report our failure to follow the debuglink ? */
817 free (debug_filename);
820 stash->alt_bfd_ptr = debug_bfd;
823 if (! read_section (unit->stash->alt_bfd_ptr,
824 stash->debug_sections + debug_str_alt,
825 NULL, /* FIXME: Do we need to load alternate symbols ? */
827 &stash->alt_dwarf_str_buffer,
828 &stash->alt_dwarf_str_size))
831 if (offset >= stash->alt_dwarf_str_size)
833 str = (char *) stash->alt_dwarf_str_buffer + offset;
840 /* Resolve an alternate reference from UNIT at OFFSET.
841 Returns a pointer into the loaded alternate CU upon success
842 or NULL upon failure. */
845 read_alt_indirect_ref (struct comp_unit * unit,
848 struct dwarf2_debug *stash = unit->stash;
850 if (stash->alt_bfd_ptr == NULL)
853 char * debug_filename = bfd_follow_gnu_debugaltlink (unit->abfd, DEBUGDIR);
855 if (debug_filename == NULL)
858 if ((debug_bfd = bfd_openr (debug_filename, NULL)) == NULL
859 || ! bfd_check_format (debug_bfd, bfd_object))
862 bfd_close (debug_bfd);
864 /* FIXME: Should we report our failure to follow the debuglink ? */
865 free (debug_filename);
868 stash->alt_bfd_ptr = debug_bfd;
871 if (! read_section (unit->stash->alt_bfd_ptr,
872 stash->debug_sections + debug_info_alt,
873 NULL, /* FIXME: Do we need to load alternate symbols ? */
875 &stash->alt_dwarf_info_buffer,
876 &stash->alt_dwarf_info_size))
879 if (offset >= stash->alt_dwarf_info_size)
881 return stash->alt_dwarf_info_buffer + offset;
885 read_address (struct comp_unit *unit, bfd_byte *buf, bfd_byte * buf_end)
889 if (bfd_get_flavour (unit->abfd) == bfd_target_elf_flavour)
890 signed_vma = get_elf_backend_data (unit->abfd)->sign_extend_vma;
892 if (buf + unit->addr_size > buf_end)
897 switch (unit->addr_size)
900 return bfd_get_signed_64 (unit->abfd, buf);
902 return bfd_get_signed_32 (unit->abfd, buf);
904 return bfd_get_signed_16 (unit->abfd, buf);
911 switch (unit->addr_size)
914 return bfd_get_64 (unit->abfd, buf);
916 return bfd_get_32 (unit->abfd, buf);
918 return bfd_get_16 (unit->abfd, buf);
925 /* Lookup an abbrev_info structure in the abbrev hash table. */
927 static struct abbrev_info *
928 lookup_abbrev (unsigned int number, struct abbrev_info **abbrevs)
930 unsigned int hash_number;
931 struct abbrev_info *abbrev;
933 hash_number = number % ABBREV_HASH_SIZE;
934 abbrev = abbrevs[hash_number];
938 if (abbrev->number == number)
941 abbrev = abbrev->next;
947 /* In DWARF version 2, the description of the debugging information is
948 stored in a separate .debug_abbrev section. Before we read any
949 dies from a section we read in all abbreviations and install them
952 static struct abbrev_info**
953 read_abbrevs (bfd *abfd, bfd_uint64_t offset, struct dwarf2_debug *stash)
955 struct abbrev_info **abbrevs;
956 bfd_byte *abbrev_ptr;
957 bfd_byte *abbrev_end;
958 struct abbrev_info *cur_abbrev;
959 unsigned int abbrev_number, bytes_read, abbrev_name;
960 unsigned int abbrev_form, hash_number;
963 if (! read_section (abfd, &stash->debug_sections[debug_abbrev],
965 &stash->dwarf_abbrev_buffer, &stash->dwarf_abbrev_size))
968 if (offset >= stash->dwarf_abbrev_size)
971 amt = sizeof (struct abbrev_info*) * ABBREV_HASH_SIZE;
972 abbrevs = (struct abbrev_info **) bfd_zalloc (abfd, amt);
976 abbrev_ptr = stash->dwarf_abbrev_buffer + offset;
977 abbrev_end = stash->dwarf_abbrev_buffer + stash->dwarf_abbrev_size;
978 abbrev_number = _bfd_safe_read_leb128 (abfd, abbrev_ptr, &bytes_read,
980 abbrev_ptr += bytes_read;
982 /* Loop until we reach an abbrev number of 0. */
983 while (abbrev_number)
985 amt = sizeof (struct abbrev_info);
986 cur_abbrev = (struct abbrev_info *) bfd_zalloc (abfd, amt);
987 if (cur_abbrev == NULL)
990 /* Read in abbrev header. */
991 cur_abbrev->number = abbrev_number;
992 cur_abbrev->tag = (enum dwarf_tag)
993 _bfd_safe_read_leb128 (abfd, abbrev_ptr, &bytes_read,
995 abbrev_ptr += bytes_read;
996 cur_abbrev->has_children = read_1_byte (abfd, abbrev_ptr, abbrev_end);
999 /* Now read in declarations. */
1002 /* Initialize it just to avoid a GCC false warning. */
1003 bfd_vma implicit_const = -1;
1005 abbrev_name = _bfd_safe_read_leb128 (abfd, abbrev_ptr, &bytes_read,
1007 abbrev_ptr += bytes_read;
1008 abbrev_form = _bfd_safe_read_leb128 (abfd, abbrev_ptr, &bytes_read,
1010 abbrev_ptr += bytes_read;
1011 if (abbrev_form == DW_FORM_implicit_const)
1013 implicit_const = _bfd_safe_read_leb128 (abfd, abbrev_ptr,
1016 abbrev_ptr += bytes_read;
1019 if (abbrev_name == 0)
1022 if ((cur_abbrev->num_attrs % ATTR_ALLOC_CHUNK) == 0)
1024 struct attr_abbrev *tmp;
1026 amt = cur_abbrev->num_attrs + ATTR_ALLOC_CHUNK;
1027 amt *= sizeof (struct attr_abbrev);
1028 tmp = (struct attr_abbrev *) bfd_realloc (cur_abbrev->attrs, amt);
1033 for (i = 0; i < ABBREV_HASH_SIZE; i++)
1035 struct abbrev_info *abbrev = abbrevs[i];
1039 free (abbrev->attrs);
1040 abbrev = abbrev->next;
1045 cur_abbrev->attrs = tmp;
1048 cur_abbrev->attrs[cur_abbrev->num_attrs].name
1049 = (enum dwarf_attribute) abbrev_name;
1050 cur_abbrev->attrs[cur_abbrev->num_attrs].form
1051 = (enum dwarf_form) abbrev_form;
1052 cur_abbrev->attrs[cur_abbrev->num_attrs].implicit_const
1054 ++cur_abbrev->num_attrs;
1057 hash_number = abbrev_number % ABBREV_HASH_SIZE;
1058 cur_abbrev->next = abbrevs[hash_number];
1059 abbrevs[hash_number] = cur_abbrev;
1061 /* Get next abbreviation.
1062 Under Irix6 the abbreviations for a compilation unit are not
1063 always properly terminated with an abbrev number of 0.
1064 Exit loop if we encounter an abbreviation which we have
1065 already read (which means we are about to read the abbreviations
1066 for the next compile unit) or if the end of the abbreviation
1067 table is reached. */
1068 if ((unsigned int) (abbrev_ptr - stash->dwarf_abbrev_buffer)
1069 >= stash->dwarf_abbrev_size)
1071 abbrev_number = _bfd_safe_read_leb128 (abfd, abbrev_ptr,
1072 &bytes_read, FALSE, abbrev_end);
1073 abbrev_ptr += bytes_read;
1074 if (lookup_abbrev (abbrev_number, abbrevs) != NULL)
1081 /* Returns true if the form is one which has a string value. */
1083 static inline bfd_boolean
1084 is_str_attr (enum dwarf_form form)
1086 return (form == DW_FORM_string || form == DW_FORM_strp
1087 || form == DW_FORM_line_strp || form == DW_FORM_GNU_strp_alt);
1090 /* Read and fill in the value of attribute ATTR as described by FORM.
1091 Read data starting from INFO_PTR, but never at or beyond INFO_PTR_END.
1092 Returns an updated INFO_PTR taking into account the amount of data read. */
1095 read_attribute_value (struct attribute * attr,
1097 bfd_vma implicit_const,
1098 struct comp_unit * unit,
1099 bfd_byte * info_ptr,
1100 bfd_byte * info_ptr_end)
1102 bfd *abfd = unit->abfd;
1103 unsigned int bytes_read;
1104 struct dwarf_block *blk;
1107 if (info_ptr >= info_ptr_end && form != DW_FORM_flag_present)
1109 _bfd_error_handler (_("DWARF error: info pointer extends beyond end of attributes"));
1110 bfd_set_error (bfd_error_bad_value);
1114 attr->form = (enum dwarf_form) form;
1118 case DW_FORM_ref_addr:
1119 /* DW_FORM_ref_addr is an address in DWARF2, and an offset in
1121 if (unit->version == 3 || unit->version == 4)
1123 if (unit->offset_size == 4)
1124 attr->u.val = read_4_bytes (unit->abfd, info_ptr, info_ptr_end);
1126 attr->u.val = read_8_bytes (unit->abfd, info_ptr, info_ptr_end);
1127 info_ptr += unit->offset_size;
1132 attr->u.val = read_address (unit, info_ptr, info_ptr_end);
1133 info_ptr += unit->addr_size;
1135 case DW_FORM_GNU_ref_alt:
1136 case DW_FORM_sec_offset:
1137 if (unit->offset_size == 4)
1138 attr->u.val = read_4_bytes (unit->abfd, info_ptr, info_ptr_end);
1140 attr->u.val = read_8_bytes (unit->abfd, info_ptr, info_ptr_end);
1141 info_ptr += unit->offset_size;
1143 case DW_FORM_block2:
1144 amt = sizeof (struct dwarf_block);
1145 blk = (struct dwarf_block *) bfd_alloc (abfd, amt);
1148 blk->size = read_2_bytes (abfd, info_ptr, info_ptr_end);
1150 info_ptr = read_n_bytes (info_ptr, info_ptr_end, blk);
1153 case DW_FORM_block4:
1154 amt = sizeof (struct dwarf_block);
1155 blk = (struct dwarf_block *) bfd_alloc (abfd, amt);
1158 blk->size = read_4_bytes (abfd, info_ptr, info_ptr_end);
1160 info_ptr = read_n_bytes (info_ptr, info_ptr_end, blk);
1164 attr->u.val = read_2_bytes (abfd, info_ptr, info_ptr_end);
1168 attr->u.val = read_4_bytes (abfd, info_ptr, info_ptr_end);
1172 attr->u.val = read_8_bytes (abfd, info_ptr, info_ptr_end);
1175 case DW_FORM_string:
1176 attr->u.str = read_string (abfd, info_ptr, info_ptr_end, &bytes_read);
1177 info_ptr += bytes_read;
1180 attr->u.str = read_indirect_string (unit, info_ptr, info_ptr_end, &bytes_read);
1181 info_ptr += bytes_read;
1183 case DW_FORM_line_strp:
1184 attr->u.str = read_indirect_line_string (unit, info_ptr, info_ptr_end, &bytes_read);
1185 info_ptr += bytes_read;
1187 case DW_FORM_GNU_strp_alt:
1188 attr->u.str = read_alt_indirect_string (unit, info_ptr, info_ptr_end, &bytes_read);
1189 info_ptr += bytes_read;
1191 case DW_FORM_exprloc:
1193 amt = sizeof (struct dwarf_block);
1194 blk = (struct dwarf_block *) bfd_alloc (abfd, amt);
1197 blk->size = _bfd_safe_read_leb128 (abfd, info_ptr, &bytes_read,
1198 FALSE, info_ptr_end);
1199 info_ptr += bytes_read;
1200 info_ptr = read_n_bytes (info_ptr, info_ptr_end, blk);
1203 case DW_FORM_block1:
1204 amt = sizeof (struct dwarf_block);
1205 blk = (struct dwarf_block *) bfd_alloc (abfd, amt);
1208 blk->size = read_1_byte (abfd, info_ptr, info_ptr_end);
1210 info_ptr = read_n_bytes (info_ptr, info_ptr_end, blk);
1214 attr->u.val = read_1_byte (abfd, info_ptr, info_ptr_end);
1218 attr->u.val = read_1_byte (abfd, info_ptr, info_ptr_end);
1221 case DW_FORM_flag_present:
1225 attr->u.sval = _bfd_safe_read_leb128 (abfd, info_ptr, &bytes_read,
1226 TRUE, info_ptr_end);
1227 info_ptr += bytes_read;
1230 attr->u.val = _bfd_safe_read_leb128 (abfd, info_ptr, &bytes_read,
1231 FALSE, info_ptr_end);
1232 info_ptr += bytes_read;
1235 attr->u.val = read_1_byte (abfd, info_ptr, info_ptr_end);
1239 attr->u.val = read_2_bytes (abfd, info_ptr, info_ptr_end);
1243 attr->u.val = read_4_bytes (abfd, info_ptr, info_ptr_end);
1247 attr->u.val = read_8_bytes (abfd, info_ptr, info_ptr_end);
1250 case DW_FORM_ref_sig8:
1251 attr->u.val = read_8_bytes (abfd, info_ptr, info_ptr_end);
1254 case DW_FORM_ref_udata:
1255 attr->u.val = _bfd_safe_read_leb128 (abfd, info_ptr, &bytes_read,
1256 FALSE, info_ptr_end);
1257 info_ptr += bytes_read;
1259 case DW_FORM_indirect:
1260 form = _bfd_safe_read_leb128 (abfd, info_ptr, &bytes_read,
1261 FALSE, info_ptr_end);
1262 info_ptr += bytes_read;
1263 if (form == DW_FORM_implicit_const)
1265 implicit_const = _bfd_safe_read_leb128 (abfd, info_ptr, &bytes_read,
1266 TRUE, info_ptr_end);
1267 info_ptr += bytes_read;
1269 info_ptr = read_attribute_value (attr, form, implicit_const, unit,
1270 info_ptr, info_ptr_end);
1272 case DW_FORM_implicit_const:
1273 attr->form = DW_FORM_sdata;
1274 attr->u.sval = implicit_const;
1277 _bfd_error_handler (_("DWARF error: invalid or unhandled FORM value: %#x"),
1279 bfd_set_error (bfd_error_bad_value);
1285 /* Read an attribute described by an abbreviated attribute. */
1288 read_attribute (struct attribute * attr,
1289 struct attr_abbrev * abbrev,
1290 struct comp_unit * unit,
1291 bfd_byte * info_ptr,
1292 bfd_byte * info_ptr_end)
1294 attr->name = abbrev->name;
1295 info_ptr = read_attribute_value (attr, abbrev->form, abbrev->implicit_const,
1296 unit, info_ptr, info_ptr_end);
1300 /* Return whether DW_AT_name will return the same as DW_AT_linkage_name
1304 non_mangled (int lang)
1314 case DW_LANG_Cobol74:
1315 case DW_LANG_Cobol85:
1316 case DW_LANG_Fortran77:
1317 case DW_LANG_Pascal83:
1327 /* Source line information table routines. */
1329 #define FILE_ALLOC_CHUNK 5
1330 #define DIR_ALLOC_CHUNK 5
1334 struct line_info * prev_line;
1338 unsigned int column;
1339 unsigned int discriminator;
1340 unsigned char op_index;
1341 unsigned char end_sequence; /* End of (sequential) code sequence. */
1352 struct line_sequence
1355 struct line_sequence* prev_sequence;
1356 struct line_info* last_line; /* Largest VMA. */
1357 struct line_info** line_info_lookup;
1358 bfd_size_type num_lines;
1361 struct line_info_table
1364 unsigned int num_files;
1365 unsigned int num_dirs;
1366 unsigned int num_sequences;
1369 struct fileinfo* files;
1370 struct line_sequence* sequences;
1371 struct line_info* lcl_head; /* Local head; used in 'add_line_info'. */
1374 /* Remember some information about each function. If the function is
1375 inlined (DW_TAG_inlined_subroutine) it may have two additional
1376 attributes, DW_AT_call_file and DW_AT_call_line, which specify the
1377 source code location where this function was inlined. */
1381 /* Pointer to previous function in list of all functions. */
1382 struct funcinfo * prev_func;
1383 /* Pointer to function one scope higher. */
1384 struct funcinfo * caller_func;
1385 /* Source location file name where caller_func inlines this func. */
1387 /* Source location file name. */
1389 /* Source location line number where caller_func inlines this func. */
1391 /* Source location line number. */
1394 bfd_boolean is_linkage;
1396 struct arange arange;
1397 /* Where the symbol is defined. */
1401 struct lookup_funcinfo
1403 /* Function information corresponding to this lookup table entry. */
1404 struct funcinfo * funcinfo;
1406 /* The lowest address for this specific function. */
1409 /* The highest address of this function before the lookup table is sorted.
1410 The highest address of all prior functions after the lookup table is
1411 sorted, which is used for binary search. */
1417 /* Pointer to previous variable in list of all variables */
1418 struct varinfo *prev_var;
1419 /* Source location file name */
1421 /* Source location line number */
1426 /* Where the symbol is defined */
1428 /* Is this a stack variable? */
1429 unsigned int stack: 1;
1432 /* Return TRUE if NEW_LINE should sort after LINE. */
1434 static inline bfd_boolean
1435 new_line_sorts_after (struct line_info *new_line, struct line_info *line)
1437 return (new_line->address > line->address
1438 || (new_line->address == line->address
1439 && new_line->op_index > line->op_index));
1443 /* Adds a new entry to the line_info list in the line_info_table, ensuring
1444 that the list is sorted. Note that the line_info list is sorted from
1445 highest to lowest VMA (with possible duplicates); that is,
1446 line_info->prev_line always accesses an equal or smaller VMA. */
1449 add_line_info (struct line_info_table *table,
1451 unsigned char op_index,
1454 unsigned int column,
1455 unsigned int discriminator,
1458 bfd_size_type amt = sizeof (struct line_info);
1459 struct line_sequence* seq = table->sequences;
1460 struct line_info* info = (struct line_info *) bfd_alloc (table->abfd, amt);
1465 /* Set member data of 'info'. */
1466 info->prev_line = NULL;
1467 info->address = address;
1468 info->op_index = op_index;
1470 info->column = column;
1471 info->discriminator = discriminator;
1472 info->end_sequence = end_sequence;
1474 if (filename && filename[0])
1476 info->filename = (char *) bfd_alloc (table->abfd, strlen (filename) + 1);
1477 if (info->filename == NULL)
1479 strcpy (info->filename, filename);
1482 info->filename = NULL;
1484 /* Find the correct location for 'info'. Normally we will receive
1485 new line_info data 1) in order and 2) with increasing VMAs.
1486 However some compilers break the rules (cf. decode_line_info) and
1487 so we include some heuristics for quickly finding the correct
1488 location for 'info'. In particular, these heuristics optimize for
1489 the common case in which the VMA sequence that we receive is a
1490 list of locally sorted VMAs such as
1491 p...z a...j (where a < j < p < z)
1493 Note: table->lcl_head is used to head an *actual* or *possible*
1494 sub-sequence within the list (such as a...j) that is not directly
1495 headed by table->last_line
1497 Note: we may receive duplicate entries from 'decode_line_info'. */
1500 && seq->last_line->address == address
1501 && seq->last_line->op_index == op_index
1502 && seq->last_line->end_sequence == end_sequence)
1504 /* We only keep the last entry with the same address and end
1505 sequence. See PR ld/4986. */
1506 if (table->lcl_head == seq->last_line)
1507 table->lcl_head = info;
1508 info->prev_line = seq->last_line->prev_line;
1509 seq->last_line = info;
1511 else if (!seq || seq->last_line->end_sequence)
1513 /* Start a new line sequence. */
1514 amt = sizeof (struct line_sequence);
1515 seq = (struct line_sequence *) bfd_malloc (amt);
1518 seq->low_pc = address;
1519 seq->prev_sequence = table->sequences;
1520 seq->last_line = info;
1521 table->lcl_head = info;
1522 table->sequences = seq;
1523 table->num_sequences++;
1525 else if (info->end_sequence
1526 || new_line_sorts_after (info, seq->last_line))
1528 /* Normal case: add 'info' to the beginning of the current sequence. */
1529 info->prev_line = seq->last_line;
1530 seq->last_line = info;
1532 /* lcl_head: initialize to head a *possible* sequence at the end. */
1533 if (!table->lcl_head)
1534 table->lcl_head = info;
1536 else if (!new_line_sorts_after (info, table->lcl_head)
1537 && (!table->lcl_head->prev_line
1538 || new_line_sorts_after (info, table->lcl_head->prev_line)))
1540 /* Abnormal but easy: lcl_head is the head of 'info'. */
1541 info->prev_line = table->lcl_head->prev_line;
1542 table->lcl_head->prev_line = info;
1546 /* Abnormal and hard: Neither 'last_line' nor 'lcl_head'
1547 are valid heads for 'info'. Reset 'lcl_head'. */
1548 struct line_info* li2 = seq->last_line; /* Always non-NULL. */
1549 struct line_info* li1 = li2->prev_line;
1553 if (!new_line_sorts_after (info, li2)
1554 && new_line_sorts_after (info, li1))
1557 li2 = li1; /* always non-NULL */
1558 li1 = li1->prev_line;
1560 table->lcl_head = li2;
1561 info->prev_line = table->lcl_head->prev_line;
1562 table->lcl_head->prev_line = info;
1563 if (address < seq->low_pc)
1564 seq->low_pc = address;
1569 /* Extract a fully qualified filename from a line info table.
1570 The returned string has been malloc'ed and it is the caller's
1571 responsibility to free it. */
1574 concat_filename (struct line_info_table *table, unsigned int file)
1578 if (table == NULL || file - 1 >= table->num_files)
1580 /* FILE == 0 means unknown. */
1583 (_("DWARF error: mangled line number section (bad file number)"));
1584 return strdup ("<unknown>");
1587 filename = table->files[file - 1].name;
1588 if (filename == NULL)
1589 return strdup ("<unknown>");
1591 if (!IS_ABSOLUTE_PATH (filename))
1593 char *dir_name = NULL;
1594 char *subdir_name = NULL;
1598 if (table->files[file - 1].dir
1599 /* PR 17512: file: 0317e960. */
1600 && table->files[file - 1].dir <= table->num_dirs
1601 /* PR 17512: file: 7f3d2e4b. */
1602 && table->dirs != NULL)
1603 subdir_name = table->dirs[table->files[file - 1].dir - 1];
1605 if (!subdir_name || !IS_ABSOLUTE_PATH (subdir_name))
1606 dir_name = table->comp_dir;
1610 dir_name = subdir_name;
1615 return strdup (filename);
1617 len = strlen (dir_name) + strlen (filename) + 2;
1621 len += strlen (subdir_name) + 1;
1622 name = (char *) bfd_malloc (len);
1624 sprintf (name, "%s/%s/%s", dir_name, subdir_name, filename);
1628 name = (char *) bfd_malloc (len);
1630 sprintf (name, "%s/%s", dir_name, filename);
1636 return strdup (filename);
1640 arange_add (const struct comp_unit *unit, struct arange *first_arange,
1641 bfd_vma low_pc, bfd_vma high_pc)
1643 struct arange *arange;
1645 /* Ignore empty ranges. */
1646 if (low_pc == high_pc)
1649 /* If the first arange is empty, use it. */
1650 if (first_arange->high == 0)
1652 first_arange->low = low_pc;
1653 first_arange->high = high_pc;
1657 /* Next see if we can cheaply extend an existing range. */
1658 arange = first_arange;
1661 if (low_pc == arange->high)
1663 arange->high = high_pc;
1666 if (high_pc == arange->low)
1668 arange->low = low_pc;
1671 arange = arange->next;
1675 /* Need to allocate a new arange and insert it into the arange list.
1676 Order isn't significant, so just insert after the first arange. */
1677 arange = (struct arange *) bfd_alloc (unit->abfd, sizeof (*arange));
1680 arange->low = low_pc;
1681 arange->high = high_pc;
1682 arange->next = first_arange->next;
1683 first_arange->next = arange;
1687 /* Compare function for line sequences. */
1690 compare_sequences (const void* a, const void* b)
1692 const struct line_sequence* seq1 = a;
1693 const struct line_sequence* seq2 = b;
1695 /* Sort by low_pc as the primary key. */
1696 if (seq1->low_pc < seq2->low_pc)
1698 if (seq1->low_pc > seq2->low_pc)
1701 /* If low_pc values are equal, sort in reverse order of
1702 high_pc, so that the largest region comes first. */
1703 if (seq1->last_line->address < seq2->last_line->address)
1705 if (seq1->last_line->address > seq2->last_line->address)
1708 if (seq1->last_line->op_index < seq2->last_line->op_index)
1710 if (seq1->last_line->op_index > seq2->last_line->op_index)
1716 /* Construct the line information table for quick lookup. */
1719 build_line_info_table (struct line_info_table * table,
1720 struct line_sequence * seq)
1723 struct line_info** line_info_lookup;
1724 struct line_info* each_line;
1725 unsigned int num_lines;
1726 unsigned int line_index;
1728 if (seq->line_info_lookup != NULL)
1731 /* Count the number of line information entries. We could do this while
1732 scanning the debug information, but some entries may be added via
1733 lcl_head without having a sequence handy to increment the number of
1736 for (each_line = seq->last_line; each_line; each_line = each_line->prev_line)
1742 /* Allocate space for the line information lookup table. */
1743 amt = sizeof (struct line_info*) * num_lines;
1744 line_info_lookup = (struct line_info**) bfd_alloc (table->abfd, amt);
1745 if (line_info_lookup == NULL)
1748 /* Create the line information lookup table. */
1749 line_index = num_lines;
1750 for (each_line = seq->last_line; each_line; each_line = each_line->prev_line)
1751 line_info_lookup[--line_index] = each_line;
1753 BFD_ASSERT (line_index == 0);
1755 seq->num_lines = num_lines;
1756 seq->line_info_lookup = line_info_lookup;
1761 /* Sort the line sequences for quick lookup. */
1764 sort_line_sequences (struct line_info_table* table)
1767 struct line_sequence* sequences;
1768 struct line_sequence* seq;
1770 unsigned int num_sequences = table->num_sequences;
1771 bfd_vma last_high_pc;
1773 if (num_sequences == 0)
1776 /* Allocate space for an array of sequences. */
1777 amt = sizeof (struct line_sequence) * num_sequences;
1778 sequences = (struct line_sequence *) bfd_alloc (table->abfd, amt);
1779 if (sequences == NULL)
1782 /* Copy the linked list into the array, freeing the original nodes. */
1783 seq = table->sequences;
1784 for (n = 0; n < num_sequences; n++)
1786 struct line_sequence* last_seq = seq;
1789 sequences[n].low_pc = seq->low_pc;
1790 sequences[n].prev_sequence = NULL;
1791 sequences[n].last_line = seq->last_line;
1792 sequences[n].line_info_lookup = NULL;
1793 sequences[n].num_lines = 0;
1794 seq = seq->prev_sequence;
1797 BFD_ASSERT (seq == NULL);
1799 qsort (sequences, n, sizeof (struct line_sequence), compare_sequences);
1801 /* Make the list binary-searchable by trimming overlapping entries
1802 and removing nested entries. */
1804 last_high_pc = sequences[0].last_line->address;
1805 for (n = 1; n < table->num_sequences; n++)
1807 if (sequences[n].low_pc < last_high_pc)
1809 if (sequences[n].last_line->address <= last_high_pc)
1810 /* Skip nested entries. */
1813 /* Trim overlapping entries. */
1814 sequences[n].low_pc = last_high_pc;
1816 last_high_pc = sequences[n].last_line->address;
1817 if (n > num_sequences)
1819 /* Close up the gap. */
1820 sequences[num_sequences].low_pc = sequences[n].low_pc;
1821 sequences[num_sequences].last_line = sequences[n].last_line;
1826 table->sequences = sequences;
1827 table->num_sequences = num_sequences;
1831 /* Add directory to TABLE. CUR_DIR memory ownership is taken by TABLE. */
1834 line_info_add_include_dir (struct line_info_table *table, char *cur_dir)
1836 if ((table->num_dirs % DIR_ALLOC_CHUNK) == 0)
1841 amt = table->num_dirs + DIR_ALLOC_CHUNK;
1842 amt *= sizeof (char *);
1844 tmp = (char **) bfd_realloc (table->dirs, amt);
1850 table->dirs[table->num_dirs++] = cur_dir;
1855 line_info_add_include_dir_stub (struct line_info_table *table, char *cur_dir,
1856 unsigned int dir ATTRIBUTE_UNUSED,
1857 unsigned int xtime ATTRIBUTE_UNUSED,
1858 unsigned int size ATTRIBUTE_UNUSED)
1860 return line_info_add_include_dir (table, cur_dir);
1863 /* Add file to TABLE. CUR_FILE memory ownership is taken by TABLE. */
1866 line_info_add_file_name (struct line_info_table *table, char *cur_file,
1867 unsigned int dir, unsigned int xtime,
1870 if ((table->num_files % FILE_ALLOC_CHUNK) == 0)
1872 struct fileinfo *tmp;
1875 amt = table->num_files + FILE_ALLOC_CHUNK;
1876 amt *= sizeof (struct fileinfo);
1878 tmp = (struct fileinfo *) bfd_realloc (table->files, amt);
1884 table->files[table->num_files].name = cur_file;
1885 table->files[table->num_files].dir = dir;
1886 table->files[table->num_files].time = xtime;
1887 table->files[table->num_files].size = size;
1892 /* Read directory or file name entry format, starting with byte of
1893 format count entries, ULEB128 pairs of entry formats, ULEB128 of
1894 entries count and the entries themselves in the described entry
1898 read_formatted_entries (struct comp_unit *unit, bfd_byte **bufp,
1899 bfd_byte *buf_end, struct line_info_table *table,
1900 bfd_boolean (*callback) (struct line_info_table *table,
1906 bfd *abfd = unit->abfd;
1907 bfd_byte format_count, formati;
1908 bfd_vma data_count, datai;
1909 bfd_byte *buf = *bufp;
1910 bfd_byte *format_header_data;
1911 unsigned int bytes_read;
1913 format_count = read_1_byte (abfd, buf, buf_end);
1915 format_header_data = buf;
1916 for (formati = 0; formati < format_count; formati++)
1918 _bfd_safe_read_leb128 (abfd, buf, &bytes_read, FALSE, buf_end);
1920 _bfd_safe_read_leb128 (abfd, buf, &bytes_read, FALSE, buf_end);
1924 data_count = _bfd_safe_read_leb128 (abfd, buf, &bytes_read, FALSE, buf_end);
1926 if (format_count == 0 && data_count != 0)
1928 _bfd_error_handler (_("DWARF error: zero format count"));
1929 bfd_set_error (bfd_error_bad_value);
1933 /* PR 22210. Paranoia check. Don't bother running the loop
1934 if we know that we are going to run out of buffer. */
1935 if (data_count > (bfd_vma) (buf_end - buf))
1938 (_("DWARF error: data count (%" PRIx64 ") larger than buffer size"),
1939 (uint64_t) data_count);
1940 bfd_set_error (bfd_error_bad_value);
1944 for (datai = 0; datai < data_count; datai++)
1946 bfd_byte *format = format_header_data;
1949 memset (&fe, 0, sizeof fe);
1950 for (formati = 0; formati < format_count; formati++)
1952 bfd_vma content_type, form;
1954 char **stringp = &string_trash;
1955 unsigned int uint_trash, *uintp = &uint_trash;
1956 struct attribute attr;
1958 content_type = _bfd_safe_read_leb128 (abfd, format, &bytes_read,
1960 format += bytes_read;
1961 switch (content_type)
1966 case DW_LNCT_directory_index:
1969 case DW_LNCT_timestamp:
1979 (_("DWARF error: unknown format content type %" PRIu64),
1980 (uint64_t) content_type);
1981 bfd_set_error (bfd_error_bad_value);
1985 form = _bfd_safe_read_leb128 (abfd, format, &bytes_read, FALSE,
1987 format += bytes_read;
1989 buf = read_attribute_value (&attr, form, 0, unit, buf, buf_end);
1994 case DW_FORM_string:
1995 case DW_FORM_line_strp:
1996 *stringp = attr.u.str;
2004 *uintp = attr.u.val;
2009 if (!callback (table, fe.name, fe.dir, fe.time, fe.size))
2017 /* Decode the line number information for UNIT. */
2019 static struct line_info_table*
2020 decode_line_info (struct comp_unit *unit, struct dwarf2_debug *stash)
2022 bfd *abfd = unit->abfd;
2023 struct line_info_table* table;
2026 struct line_head lh;
2027 unsigned int i, bytes_read, offset_size;
2028 char *cur_file, *cur_dir;
2029 unsigned char op_code, extended_op, adj_opcode;
2030 unsigned int exop_len;
2033 if (! read_section (abfd, &stash->debug_sections[debug_line],
2034 stash->syms, unit->line_offset,
2035 &stash->dwarf_line_buffer, &stash->dwarf_line_size))
2038 amt = sizeof (struct line_info_table);
2039 table = (struct line_info_table *) bfd_alloc (abfd, amt);
2043 table->comp_dir = unit->comp_dir;
2045 table->num_files = 0;
2046 table->files = NULL;
2048 table->num_dirs = 0;
2051 table->num_sequences = 0;
2052 table->sequences = NULL;
2054 table->lcl_head = NULL;
2056 if (stash->dwarf_line_size < 16)
2059 (_("DWARF error: line info section is too small (%" PRId64 ")"),
2060 (int64_t) stash->dwarf_line_size);
2061 bfd_set_error (bfd_error_bad_value);
2064 line_ptr = stash->dwarf_line_buffer + unit->line_offset;
2065 line_end = stash->dwarf_line_buffer + stash->dwarf_line_size;
2067 /* Read in the prologue. */
2068 lh.total_length = read_4_bytes (abfd, line_ptr, line_end);
2071 if (lh.total_length == 0xffffffff)
2073 lh.total_length = read_8_bytes (abfd, line_ptr, line_end);
2077 else if (lh.total_length == 0 && unit->addr_size == 8)
2079 /* Handle (non-standard) 64-bit DWARF2 formats. */
2080 lh.total_length = read_4_bytes (abfd, line_ptr, line_end);
2085 if (lh.total_length > (size_t) (line_end - line_ptr))
2088 /* xgettext: c-format */
2089 (_("DWARF error: line info data is bigger (%#" PRIx64 ")"
2090 " than the space remaining in the section (%#lx)"),
2091 (uint64_t) lh.total_length, (unsigned long) (line_end - line_ptr));
2092 bfd_set_error (bfd_error_bad_value);
2096 line_end = line_ptr + lh.total_length;
2098 lh.version = read_2_bytes (abfd, line_ptr, line_end);
2099 if (lh.version < 2 || lh.version > 5)
2102 (_("DWARF error: unhandled .debug_line version %d"), lh.version);
2103 bfd_set_error (bfd_error_bad_value);
2108 if (line_ptr + offset_size + (lh.version >= 5 ? 8 : (lh.version >= 4 ? 6 : 5))
2112 (_("DWARF error: ran out of room reading prologue"));
2113 bfd_set_error (bfd_error_bad_value);
2117 if (lh.version >= 5)
2119 unsigned int segment_selector_size;
2121 /* Skip address size. */
2122 read_1_byte (abfd, line_ptr, line_end);
2125 segment_selector_size = read_1_byte (abfd, line_ptr, line_end);
2127 if (segment_selector_size != 0)
2130 (_("DWARF error: line info unsupported segment selector size %u"),
2131 segment_selector_size);
2132 bfd_set_error (bfd_error_bad_value);
2137 if (offset_size == 4)
2138 lh.prologue_length = read_4_bytes (abfd, line_ptr, line_end);
2140 lh.prologue_length = read_8_bytes (abfd, line_ptr, line_end);
2141 line_ptr += offset_size;
2143 lh.minimum_instruction_length = read_1_byte (abfd, line_ptr, line_end);
2146 if (lh.version >= 4)
2148 lh.maximum_ops_per_insn = read_1_byte (abfd, line_ptr, line_end);
2152 lh.maximum_ops_per_insn = 1;
2154 if (lh.maximum_ops_per_insn == 0)
2157 (_("DWARF error: invalid maximum operations per instruction"));
2158 bfd_set_error (bfd_error_bad_value);
2162 lh.default_is_stmt = read_1_byte (abfd, line_ptr, line_end);
2165 lh.line_base = read_1_signed_byte (abfd, line_ptr, line_end);
2168 lh.line_range = read_1_byte (abfd, line_ptr, line_end);
2171 lh.opcode_base = read_1_byte (abfd, line_ptr, line_end);
2174 if (line_ptr + (lh.opcode_base - 1) >= line_end)
2176 _bfd_error_handler (_("DWARF error: ran out of room reading opcodes"));
2177 bfd_set_error (bfd_error_bad_value);
2181 amt = lh.opcode_base * sizeof (unsigned char);
2182 lh.standard_opcode_lengths = (unsigned char *) bfd_alloc (abfd, amt);
2184 lh.standard_opcode_lengths[0] = 1;
2186 for (i = 1; i < lh.opcode_base; ++i)
2188 lh.standard_opcode_lengths[i] = read_1_byte (abfd, line_ptr, line_end);
2192 if (lh.version >= 5)
2194 /* Read directory table. */
2195 if (!read_formatted_entries (unit, &line_ptr, line_end, table,
2196 line_info_add_include_dir_stub))
2199 /* Read file name table. */
2200 if (!read_formatted_entries (unit, &line_ptr, line_end, table,
2201 line_info_add_file_name))
2206 /* Read directory table. */
2207 while ((cur_dir = read_string (abfd, line_ptr, line_end, &bytes_read)) != NULL)
2209 line_ptr += bytes_read;
2211 if (!line_info_add_include_dir (table, cur_dir))
2215 line_ptr += bytes_read;
2217 /* Read file name table. */
2218 while ((cur_file = read_string (abfd, line_ptr, line_end, &bytes_read)) != NULL)
2220 unsigned int dir, xtime, size;
2222 line_ptr += bytes_read;
2224 dir = _bfd_safe_read_leb128 (abfd, line_ptr, &bytes_read, FALSE, line_end);
2225 line_ptr += bytes_read;
2226 xtime = _bfd_safe_read_leb128 (abfd, line_ptr, &bytes_read, FALSE, line_end);
2227 line_ptr += bytes_read;
2228 size = _bfd_safe_read_leb128 (abfd, line_ptr, &bytes_read, FALSE, line_end);
2229 line_ptr += bytes_read;
2231 if (!line_info_add_file_name (table, cur_file, dir, xtime, size))
2235 line_ptr += bytes_read;
2238 /* Read the statement sequences until there's nothing left. */
2239 while (line_ptr < line_end)
2241 /* State machine registers. */
2242 bfd_vma address = 0;
2243 unsigned char op_index = 0;
2244 char * filename = table->num_files ? concat_filename (table, 1) : NULL;
2245 unsigned int line = 1;
2246 unsigned int column = 0;
2247 unsigned int discriminator = 0;
2248 int is_stmt = lh.default_is_stmt;
2249 int end_sequence = 0;
2250 unsigned int dir, xtime, size;
2251 /* eraxxon@alumni.rice.edu: Against the DWARF2 specs, some
2252 compilers generate address sequences that are wildly out of
2253 order using DW_LNE_set_address (e.g. Intel C++ 6.0 compiler
2254 for ia64-Linux). Thus, to determine the low and high
2255 address, we must compare on every DW_LNS_copy, etc. */
2256 bfd_vma low_pc = (bfd_vma) -1;
2257 bfd_vma high_pc = 0;
2259 /* Decode the table. */
2260 while (!end_sequence && line_ptr < line_end)
2262 op_code = read_1_byte (abfd, line_ptr, line_end);
2265 if (op_code >= lh.opcode_base)
2267 /* Special operand. */
2268 adj_opcode = op_code - lh.opcode_base;
2269 if (lh.line_range == 0)
2271 if (lh.maximum_ops_per_insn == 1)
2272 address += (adj_opcode / lh.line_range
2273 * lh.minimum_instruction_length);
2276 address += ((op_index + adj_opcode / lh.line_range)
2277 / lh.maximum_ops_per_insn
2278 * lh.minimum_instruction_length);
2279 op_index = ((op_index + adj_opcode / lh.line_range)
2280 % lh.maximum_ops_per_insn);
2282 line += lh.line_base + (adj_opcode % lh.line_range);
2283 /* Append row to matrix using current values. */
2284 if (!add_line_info (table, address, op_index, filename,
2285 line, column, discriminator, 0))
2288 if (address < low_pc)
2290 if (address > high_pc)
2293 else switch (op_code)
2295 case DW_LNS_extended_op:
2296 exop_len = _bfd_safe_read_leb128 (abfd, line_ptr, &bytes_read,
2298 line_ptr += bytes_read;
2299 extended_op = read_1_byte (abfd, line_ptr, line_end);
2302 switch (extended_op)
2304 case DW_LNE_end_sequence:
2306 if (!add_line_info (table, address, op_index, filename, line,
2307 column, discriminator, end_sequence))
2310 if (address < low_pc)
2312 if (address > high_pc)
2314 if (!arange_add (unit, &unit->arange, low_pc, high_pc))
2317 case DW_LNE_set_address:
2318 address = read_address (unit, line_ptr, line_end);
2320 line_ptr += unit->addr_size;
2322 case DW_LNE_define_file:
2323 cur_file = read_string (abfd, line_ptr, line_end, &bytes_read);
2324 line_ptr += bytes_read;
2325 dir = _bfd_safe_read_leb128 (abfd, line_ptr, &bytes_read,
2327 line_ptr += bytes_read;
2328 xtime = _bfd_safe_read_leb128 (abfd, line_ptr, &bytes_read,
2330 line_ptr += bytes_read;
2331 size = _bfd_safe_read_leb128 (abfd, line_ptr, &bytes_read,
2333 line_ptr += bytes_read;
2334 if (!line_info_add_file_name (table, cur_file, dir,
2338 case DW_LNE_set_discriminator:
2340 _bfd_safe_read_leb128 (abfd, line_ptr, &bytes_read,
2342 line_ptr += bytes_read;
2344 case DW_LNE_HP_source_file_correlation:
2345 line_ptr += exop_len - 1;
2349 (_("DWARF error: mangled line number section"));
2350 bfd_set_error (bfd_error_bad_value);
2352 if (filename != NULL)
2358 if (!add_line_info (table, address, op_index,
2359 filename, line, column, discriminator, 0))
2362 if (address < low_pc)
2364 if (address > high_pc)
2367 case DW_LNS_advance_pc:
2368 if (lh.maximum_ops_per_insn == 1)
2369 address += (lh.minimum_instruction_length
2370 * _bfd_safe_read_leb128 (abfd, line_ptr,
2375 bfd_vma adjust = _bfd_safe_read_leb128 (abfd, line_ptr,
2378 address = ((op_index + adjust) / lh.maximum_ops_per_insn
2379 * lh.minimum_instruction_length);
2380 op_index = (op_index + adjust) % lh.maximum_ops_per_insn;
2382 line_ptr += bytes_read;
2384 case DW_LNS_advance_line:
2385 line += _bfd_safe_read_leb128 (abfd, line_ptr, &bytes_read,
2387 line_ptr += bytes_read;
2389 case DW_LNS_set_file:
2393 /* The file and directory tables are 0
2394 based, the references are 1 based. */
2395 file = _bfd_safe_read_leb128 (abfd, line_ptr, &bytes_read,
2397 line_ptr += bytes_read;
2400 filename = concat_filename (table, file);
2403 case DW_LNS_set_column:
2404 column = _bfd_safe_read_leb128 (abfd, line_ptr, &bytes_read,
2406 line_ptr += bytes_read;
2408 case DW_LNS_negate_stmt:
2409 is_stmt = (!is_stmt);
2411 case DW_LNS_set_basic_block:
2413 case DW_LNS_const_add_pc:
2414 if (lh.line_range == 0)
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 while (table->sequences != NULL)
2455 struct line_sequence* seq = table->sequences;
2456 table->sequences = table->sequences->prev_sequence;
2459 if (table->files != NULL)
2460 free (table->files);
2461 if (table->dirs != NULL)
2466 /* If ADDR is within TABLE set the output parameters and return the
2467 range of addresses covered by the entry used to fill them out.
2468 Otherwise set * FILENAME_PTR to NULL and return 0.
2469 The parameters FILENAME_PTR, LINENUMBER_PTR and DISCRIMINATOR_PTR
2470 are pointers to the objects to be filled in. */
2473 lookup_address_in_line_info_table (struct line_info_table *table,
2475 const char **filename_ptr,
2476 unsigned int *linenumber_ptr,
2477 unsigned int *discriminator_ptr)
2479 struct line_sequence *seq = NULL;
2480 struct line_info *info;
2483 /* Binary search the array of sequences. */
2485 high = table->num_sequences;
2488 mid = (low + high) / 2;
2489 seq = &table->sequences[mid];
2490 if (addr < seq->low_pc)
2492 else if (addr >= seq->last_line->address)
2498 /* Check for a valid sequence. */
2499 if (!seq || addr < seq->low_pc || addr >= seq->last_line->address)
2502 if (!build_line_info_table (table, seq))
2505 /* Binary search the array of line information. */
2507 high = seq->num_lines;
2511 mid = (low + high) / 2;
2512 info = seq->line_info_lookup[mid];
2513 if (addr < info->address)
2515 else if (addr >= seq->line_info_lookup[mid + 1]->address)
2521 /* Check for a valid line information entry. */
2523 && addr >= info->address
2524 && addr < seq->line_info_lookup[mid + 1]->address
2525 && !(info->end_sequence || info == seq->last_line))
2527 *filename_ptr = info->filename;
2528 *linenumber_ptr = info->line;
2529 if (discriminator_ptr)
2530 *discriminator_ptr = info->discriminator;
2531 return seq->last_line->address - seq->low_pc;
2535 *filename_ptr = NULL;
2539 /* Read in the .debug_ranges section for future reference. */
2542 read_debug_ranges (struct comp_unit * unit)
2544 struct dwarf2_debug * stash = unit->stash;
2546 return read_section (unit->abfd, &stash->debug_sections[debug_ranges],
2548 &stash->dwarf_ranges_buffer,
2549 &stash->dwarf_ranges_size);
2552 /* Function table functions. */
2555 compare_lookup_funcinfos (const void * a, const void * b)
2557 const struct lookup_funcinfo * lookup1 = a;
2558 const struct lookup_funcinfo * lookup2 = b;
2560 if (lookup1->low_addr < lookup2->low_addr)
2562 if (lookup1->low_addr > lookup2->low_addr)
2564 if (lookup1->high_addr < lookup2->high_addr)
2566 if (lookup1->high_addr > lookup2->high_addr)
2573 build_lookup_funcinfo_table (struct comp_unit * unit)
2575 struct lookup_funcinfo *lookup_funcinfo_table = unit->lookup_funcinfo_table;
2576 unsigned int number_of_functions = unit->number_of_functions;
2577 struct funcinfo *each;
2578 struct lookup_funcinfo *entry;
2580 struct arange *range;
2581 bfd_vma low_addr, high_addr;
2583 if (lookup_funcinfo_table || number_of_functions == 0)
2586 /* Create the function info lookup table. */
2587 lookup_funcinfo_table = (struct lookup_funcinfo *)
2588 bfd_malloc (number_of_functions * sizeof (struct lookup_funcinfo));
2589 if (lookup_funcinfo_table == NULL)
2592 /* Populate the function info lookup table. */
2593 func_index = number_of_functions;
2594 for (each = unit->function_table; each; each = each->prev_func)
2596 entry = &lookup_funcinfo_table[--func_index];
2597 entry->funcinfo = each;
2599 /* Calculate the lowest and highest address for this function entry. */
2600 low_addr = entry->funcinfo->arange.low;
2601 high_addr = entry->funcinfo->arange.high;
2603 for (range = entry->funcinfo->arange.next; range; range = range->next)
2605 if (range->low < low_addr)
2606 low_addr = range->low;
2607 if (range->high > high_addr)
2608 high_addr = range->high;
2611 entry->low_addr = low_addr;
2612 entry->high_addr = high_addr;
2615 BFD_ASSERT (func_index == 0);
2617 /* Sort the function by address. */
2618 qsort (lookup_funcinfo_table,
2619 number_of_functions,
2620 sizeof (struct lookup_funcinfo),
2621 compare_lookup_funcinfos);
2623 /* Calculate the high watermark for each function in the lookup table. */
2624 high_addr = lookup_funcinfo_table[0].high_addr;
2625 for (func_index = 1; func_index < number_of_functions; func_index++)
2627 entry = &lookup_funcinfo_table[func_index];
2628 if (entry->high_addr > high_addr)
2629 high_addr = entry->high_addr;
2631 entry->high_addr = high_addr;
2634 unit->lookup_funcinfo_table = lookup_funcinfo_table;
2638 /* If ADDR is within UNIT's function tables, set FUNCTION_PTR, and return
2639 TRUE. Note that we need to find the function that has the smallest range
2640 that contains ADDR, to handle inlined functions without depending upon
2641 them being ordered in TABLE by increasing range. */
2644 lookup_address_in_function_table (struct comp_unit *unit,
2646 struct funcinfo **function_ptr)
2648 unsigned int number_of_functions = unit->number_of_functions;
2649 struct lookup_funcinfo* lookup_funcinfo = NULL;
2650 struct funcinfo* funcinfo = NULL;
2651 struct funcinfo* best_fit = NULL;
2652 bfd_vma best_fit_len = 0;
2653 bfd_size_type low, high, mid, first;
2654 struct arange *arange;
2656 if (number_of_functions == 0)
2659 if (!build_lookup_funcinfo_table (unit))
2662 if (unit->lookup_funcinfo_table[number_of_functions - 1].high_addr < addr)
2665 /* Find the first function in the lookup table which may contain the
2666 specified address. */
2668 high = number_of_functions;
2672 mid = (low + high) / 2;
2673 lookup_funcinfo = &unit->lookup_funcinfo_table[mid];
2674 if (addr < lookup_funcinfo->low_addr)
2676 else if (addr >= lookup_funcinfo->high_addr)
2682 /* Find the 'best' match for the address. The prior algorithm defined the
2683 best match as the function with the smallest address range containing
2684 the specified address. This definition should probably be changed to the
2685 innermost inline routine containing the address, but right now we want
2686 to get the same results we did before. */
2687 while (first < number_of_functions)
2689 if (addr < unit->lookup_funcinfo_table[first].low_addr)
2691 funcinfo = unit->lookup_funcinfo_table[first].funcinfo;
2693 for (arange = &funcinfo->arange; arange; arange = arange->next)
2695 if (addr < arange->low || addr >= arange->high)
2699 || arange->high - arange->low < best_fit_len
2700 /* The following comparison is designed to return the same
2701 match as the previous algorithm for routines which have the
2702 same best fit length. */
2703 || (arange->high - arange->low == best_fit_len
2704 && funcinfo > best_fit))
2706 best_fit = funcinfo;
2707 best_fit_len = arange->high - arange->low;
2717 *function_ptr = best_fit;
2721 /* If SYM at ADDR is within function table of UNIT, set FILENAME_PTR
2722 and LINENUMBER_PTR, and return TRUE. */
2725 lookup_symbol_in_function_table (struct comp_unit *unit,
2728 const char **filename_ptr,
2729 unsigned int *linenumber_ptr)
2731 struct funcinfo* each_func;
2732 struct funcinfo* best_fit = NULL;
2733 bfd_vma best_fit_len = 0;
2734 struct arange *arange;
2735 const char *name = bfd_asymbol_name (sym);
2736 asection *sec = bfd_get_section (sym);
2738 for (each_func = unit->function_table;
2740 each_func = each_func->prev_func)
2742 for (arange = &each_func->arange;
2744 arange = arange->next)
2746 if ((!each_func->sec || each_func->sec == sec)
2747 && addr >= arange->low
2748 && addr < arange->high
2750 && strcmp (name, each_func->name) == 0
2752 || arange->high - arange->low < best_fit_len))
2754 best_fit = each_func;
2755 best_fit_len = arange->high - arange->low;
2762 best_fit->sec = sec;
2763 *filename_ptr = best_fit->file;
2764 *linenumber_ptr = best_fit->line;
2771 /* Variable table functions. */
2773 /* If SYM is within variable table of UNIT, set FILENAME_PTR and
2774 LINENUMBER_PTR, and return TRUE. */
2777 lookup_symbol_in_variable_table (struct comp_unit *unit,
2780 const char **filename_ptr,
2781 unsigned int *linenumber_ptr)
2783 const char *name = bfd_asymbol_name (sym);
2784 asection *sec = bfd_get_section (sym);
2785 struct varinfo* each;
2787 for (each = unit->variable_table; each; each = each->prev_var)
2788 if (each->stack == 0
2789 && each->file != NULL
2790 && each->name != NULL
2791 && each->addr == addr
2792 && (!each->sec || each->sec == sec)
2793 && strcmp (name, each->name) == 0)
2799 *filename_ptr = each->file;
2800 *linenumber_ptr = each->line;
2807 static bfd_boolean comp_unit_maybe_decode_line_info (struct comp_unit *,
2808 struct dwarf2_debug *);
2811 find_abstract_instance (struct comp_unit * unit,
2812 bfd_byte * orig_info_ptr,
2813 struct attribute * attr_ptr,
2814 const char ** pname,
2815 bfd_boolean * is_linkage,
2816 char ** filename_ptr,
2817 int * linenumber_ptr)
2819 bfd *abfd = unit->abfd;
2821 bfd_byte *info_ptr_end;
2822 unsigned int abbrev_number, bytes_read, i;
2823 struct abbrev_info *abbrev;
2824 bfd_uint64_t die_ref = attr_ptr->u.val;
2825 struct attribute attr;
2826 const char *name = NULL;
2828 /* DW_FORM_ref_addr can reference an entry in a different CU. It
2829 is an offset from the .debug_info section, not the current CU. */
2830 if (attr_ptr->form == DW_FORM_ref_addr)
2832 /* We only support DW_FORM_ref_addr within the same file, so
2833 any relocations should be resolved already. Check this by
2834 testing for a zero die_ref; There can't be a valid reference
2835 to the header of a .debug_info section.
2836 DW_FORM_ref_addr is an offset relative to .debug_info.
2837 Normally when using the GNU linker this is accomplished by
2838 emitting a symbolic reference to a label, because .debug_info
2839 sections are linked at zero. When there are multiple section
2840 groups containing .debug_info, as there might be in a
2841 relocatable object file, it would be reasonable to assume that
2842 a symbolic reference to a label in any .debug_info section
2843 might be used. Since we lay out multiple .debug_info
2844 sections at non-zero VMAs (see place_sections), and read
2845 them contiguously into stash->info_ptr_memory, that means
2846 the reference is relative to stash->info_ptr_memory. */
2849 info_ptr = unit->stash->info_ptr_memory;
2850 info_ptr_end = unit->stash->info_ptr_end;
2851 total = info_ptr_end - info_ptr;
2854 else if (die_ref >= total)
2857 (_("DWARF error: invalid abstract instance DIE ref"));
2858 bfd_set_error (bfd_error_bad_value);
2861 info_ptr += die_ref;
2863 /* Now find the CU containing this pointer. */
2864 if (info_ptr >= unit->info_ptr_unit && info_ptr < unit->end_ptr)
2865 info_ptr_end = unit->end_ptr;
2868 /* Check other CUs to see if they contain the abbrev. */
2869 struct comp_unit * u;
2871 for (u = unit->prev_unit; u != NULL; u = u->prev_unit)
2872 if (info_ptr >= u->info_ptr_unit && info_ptr < u->end_ptr)
2876 for (u = unit->next_unit; u != NULL; u = u->next_unit)
2877 if (info_ptr >= u->info_ptr_unit && info_ptr < u->end_ptr)
2883 info_ptr_end = unit->end_ptr;
2885 /* else FIXME: What do we do now ? */
2888 else if (attr_ptr->form == DW_FORM_GNU_ref_alt)
2890 info_ptr = read_alt_indirect_ref (unit, die_ref);
2891 if (info_ptr == NULL)
2894 (_("DWARF error: unable to read alt ref %" PRIu64),
2895 (uint64_t) die_ref);
2896 bfd_set_error (bfd_error_bad_value);
2899 info_ptr_end = (unit->stash->alt_dwarf_info_buffer
2900 + unit->stash->alt_dwarf_info_size);
2902 /* FIXME: Do we need to locate the correct CU, in a similar
2903 fashion to the code in the DW_FORM_ref_addr case above ? */
2907 /* DW_FORM_ref1, DW_FORM_ref2, DW_FORM_ref4, DW_FORM_ref8 or
2908 DW_FORM_ref_udata. These are all references relative to the
2909 start of the current CU. */
2912 info_ptr = unit->info_ptr_unit;
2913 info_ptr_end = unit->end_ptr;
2914 total = info_ptr_end - info_ptr;
2915 if (!die_ref || die_ref >= total)
2918 (_("DWARF error: invalid abstract instance DIE ref"));
2919 bfd_set_error (bfd_error_bad_value);
2922 info_ptr += die_ref;
2925 abbrev_number = _bfd_safe_read_leb128 (abfd, info_ptr, &bytes_read,
2926 FALSE, info_ptr_end);
2927 info_ptr += bytes_read;
2931 abbrev = lookup_abbrev (abbrev_number, unit->abbrevs);
2935 (_("DWARF error: could not find abbrev number %u"), abbrev_number);
2936 bfd_set_error (bfd_error_bad_value);
2941 for (i = 0; i < abbrev->num_attrs; ++i)
2943 info_ptr = read_attribute (&attr, &abbrev->attrs[i], unit,
2944 info_ptr, info_ptr_end);
2945 if (info_ptr == NULL)
2947 /* It doesn't ever make sense for DW_AT_specification to
2948 refer to the same DIE. Stop simple recursion. */
2949 if (info_ptr == orig_info_ptr)
2952 (_("DWARF error: abstract instance recursion detected"));
2953 bfd_set_error (bfd_error_bad_value);
2959 /* Prefer DW_AT_MIPS_linkage_name or DW_AT_linkage_name
2961 if (name == NULL && is_str_attr (attr.form))
2964 if (non_mangled (unit->lang))
2968 case DW_AT_specification:
2969 if (!find_abstract_instance (unit, info_ptr, &attr,
2971 filename_ptr, linenumber_ptr))
2974 case DW_AT_linkage_name:
2975 case DW_AT_MIPS_linkage_name:
2976 /* PR 16949: Corrupt debug info can place
2977 non-string forms into these attributes. */
2978 if (is_str_attr (attr.form))
2984 case DW_AT_decl_file:
2985 *filename_ptr = concat_filename (unit->line_table,
2988 case DW_AT_decl_line:
2989 *linenumber_ptr = attr.u.val;
3002 read_rangelist (struct comp_unit *unit, struct arange *arange,
3003 bfd_uint64_t offset)
3005 bfd_byte *ranges_ptr;
3006 bfd_byte *ranges_end;
3007 bfd_vma base_address = unit->base_address;
3009 if (! unit->stash->dwarf_ranges_buffer)
3011 if (! read_debug_ranges (unit))
3015 ranges_ptr = unit->stash->dwarf_ranges_buffer + offset;
3016 if (ranges_ptr < unit->stash->dwarf_ranges_buffer)
3018 ranges_end = unit->stash->dwarf_ranges_buffer + unit->stash->dwarf_ranges_size;
3025 /* PR 17512: file: 62cada7d. */
3026 if (ranges_ptr + 2 * unit->addr_size > ranges_end)
3029 low_pc = read_address (unit, ranges_ptr, ranges_end);
3030 ranges_ptr += unit->addr_size;
3031 high_pc = read_address (unit, ranges_ptr, ranges_end);
3032 ranges_ptr += unit->addr_size;
3034 if (low_pc == 0 && high_pc == 0)
3036 if (low_pc == -1UL && high_pc != -1UL)
3037 base_address = high_pc;
3040 if (!arange_add (unit, arange,
3041 base_address + low_pc, base_address + high_pc))
3048 /* DWARF2 Compilation unit functions. */
3050 /* Scan over each die in a comp. unit looking for functions to add
3051 to the function table and variables to the variable table. */
3054 scan_unit_for_symbols (struct comp_unit *unit)
3056 bfd *abfd = unit->abfd;
3057 bfd_byte *info_ptr = unit->first_child_die_ptr;
3058 bfd_byte *info_ptr_end = unit->stash->info_ptr_end;
3059 int nesting_level = 0;
3060 struct nest_funcinfo {
3061 struct funcinfo *func;
3063 int nested_funcs_size;
3065 /* Maintain a stack of in-scope functions and inlined functions, which we
3066 can use to set the caller_func field. */
3067 nested_funcs_size = 32;
3068 nested_funcs = (struct nest_funcinfo *)
3069 bfd_malloc (nested_funcs_size * sizeof (*nested_funcs));
3070 if (nested_funcs == NULL)
3072 nested_funcs[nesting_level].func = 0;
3074 while (nesting_level >= 0)
3076 unsigned int abbrev_number, bytes_read, i;
3077 struct abbrev_info *abbrev;
3078 struct attribute attr;
3079 struct funcinfo *func;
3080 struct varinfo *var;
3082 bfd_vma high_pc = 0;
3083 bfd_boolean high_pc_relative = FALSE;
3085 /* PR 17512: file: 9f405d9d. */
3086 if (info_ptr >= info_ptr_end)
3089 abbrev_number = _bfd_safe_read_leb128 (abfd, info_ptr, &bytes_read,
3090 FALSE, info_ptr_end);
3091 info_ptr += bytes_read;
3093 if (! abbrev_number)
3099 abbrev = lookup_abbrev (abbrev_number, unit->abbrevs);
3102 static unsigned int previous_failed_abbrev = -1U;
3104 /* Avoid multiple reports of the same missing abbrev. */
3105 if (abbrev_number != previous_failed_abbrev)
3108 (_("DWARF error: could not find abbrev number %u"),
3110 previous_failed_abbrev = abbrev_number;
3112 bfd_set_error (bfd_error_bad_value);
3117 if (abbrev->tag == DW_TAG_subprogram
3118 || abbrev->tag == DW_TAG_entry_point
3119 || abbrev->tag == DW_TAG_inlined_subroutine)
3121 bfd_size_type amt = sizeof (struct funcinfo);
3122 func = (struct funcinfo *) bfd_zalloc (abfd, amt);
3125 func->tag = abbrev->tag;
3126 func->prev_func = unit->function_table;
3127 unit->function_table = func;
3128 unit->number_of_functions++;
3129 BFD_ASSERT (!unit->cached);
3131 if (func->tag == DW_TAG_inlined_subroutine)
3132 for (i = nesting_level; i-- != 0; )
3133 if (nested_funcs[i].func)
3135 func->caller_func = nested_funcs[i].func;
3138 nested_funcs[nesting_level].func = func;
3143 if (abbrev->tag == DW_TAG_variable)
3145 bfd_size_type amt = sizeof (struct varinfo);
3146 var = (struct varinfo *) bfd_zalloc (abfd, amt);
3149 var->tag = abbrev->tag;
3151 var->prev_var = unit->variable_table;
3152 unit->variable_table = var;
3153 /* PR 18205: Missing debug information can cause this
3154 var to be attached to an already cached unit. */
3157 /* No inline function in scope at this nesting level. */
3158 nested_funcs[nesting_level].func = 0;
3161 for (i = 0; i < abbrev->num_attrs; ++i)
3163 info_ptr = read_attribute (&attr, &abbrev->attrs[i],
3164 unit, info_ptr, info_ptr_end);
3165 if (info_ptr == NULL)
3172 case DW_AT_call_file:
3173 func->caller_file = concat_filename (unit->line_table,
3177 case DW_AT_call_line:
3178 func->caller_line = attr.u.val;
3181 case DW_AT_abstract_origin:
3182 case DW_AT_specification:
3183 if (!find_abstract_instance (unit, info_ptr, &attr,
3192 /* Prefer DW_AT_MIPS_linkage_name or DW_AT_linkage_name
3194 if (func->name == NULL && is_str_attr (attr.form))
3196 func->name = attr.u.str;
3197 if (non_mangled (unit->lang))
3198 func->is_linkage = TRUE;
3202 case DW_AT_linkage_name:
3203 case DW_AT_MIPS_linkage_name:
3204 /* PR 16949: Corrupt debug info can place
3205 non-string forms into these attributes. */
3206 if (is_str_attr (attr.form))
3208 func->name = attr.u.str;
3209 func->is_linkage = TRUE;
3214 low_pc = attr.u.val;
3218 high_pc = attr.u.val;
3219 high_pc_relative = attr.form != DW_FORM_addr;
3223 if (!read_rangelist (unit, &func->arange, attr.u.val))
3227 case DW_AT_decl_file:
3228 func->file = concat_filename (unit->line_table,
3232 case DW_AT_decl_line:
3233 func->line = attr.u.val;
3245 if (is_str_attr (attr.form))
3246 var->name = attr.u.str;
3249 case DW_AT_decl_file:
3250 var->file = concat_filename (unit->line_table,
3254 case DW_AT_decl_line:
3255 var->line = attr.u.val;
3258 case DW_AT_external:
3259 if (attr.u.val != 0)
3263 case DW_AT_location:
3267 case DW_FORM_block1:
3268 case DW_FORM_block2:
3269 case DW_FORM_block4:
3270 case DW_FORM_exprloc:
3271 if (attr.u.blk->data != NULL
3272 && *attr.u.blk->data == DW_OP_addr)
3276 /* Verify that DW_OP_addr is the only opcode in the
3277 location, in which case the block size will be 1
3278 plus the address size. */
3279 /* ??? For TLS variables, gcc can emit
3280 DW_OP_addr <addr> DW_OP_GNU_push_tls_address
3281 which we don't handle here yet. */
3282 if (attr.u.blk->size == unit->addr_size + 1U)
3283 var->addr = bfd_get (unit->addr_size * 8,
3285 attr.u.blk->data + 1);
3300 if (high_pc_relative)
3303 if (func && high_pc != 0)
3305 if (!arange_add (unit, &func->arange, low_pc, high_pc))
3309 if (abbrev->has_children)
3313 if (nesting_level >= nested_funcs_size)
3315 struct nest_funcinfo *tmp;
3317 nested_funcs_size *= 2;
3318 tmp = (struct nest_funcinfo *)
3319 bfd_realloc (nested_funcs,
3320 nested_funcs_size * sizeof (*nested_funcs));
3325 nested_funcs[nesting_level].func = 0;
3329 free (nested_funcs);
3333 free (nested_funcs);
3337 /* Parse a DWARF2 compilation unit starting at INFO_PTR. This
3338 includes the compilation unit header that proceeds the DIE's, but
3339 does not include the length field that precedes each compilation
3340 unit header. END_PTR points one past the end of this comp unit.
3341 OFFSET_SIZE is the size of DWARF2 offsets (either 4 or 8 bytes).
3343 This routine does not read the whole compilation unit; only enough
3344 to get to the line number information for the compilation unit. */
3346 static struct comp_unit *
3347 parse_comp_unit (struct dwarf2_debug *stash,
3348 bfd_vma unit_length,
3349 bfd_byte *info_ptr_unit,
3350 unsigned int offset_size)
3352 struct comp_unit* unit;
3353 unsigned int version;
3354 bfd_uint64_t abbrev_offset = 0;
3355 /* Initialize it just to avoid a GCC false warning. */
3356 unsigned int addr_size = -1;
3357 struct abbrev_info** abbrevs;
3358 unsigned int abbrev_number, bytes_read, i;
3359 struct abbrev_info *abbrev;
3360 struct attribute attr;
3361 bfd_byte *info_ptr = stash->info_ptr;
3362 bfd_byte *end_ptr = info_ptr + unit_length;
3365 bfd_vma high_pc = 0;
3366 bfd *abfd = stash->bfd_ptr;
3367 bfd_boolean high_pc_relative = FALSE;
3368 enum dwarf_unit_type unit_type;
3370 version = read_2_bytes (abfd, info_ptr, end_ptr);
3372 if (version < 2 || version > 5)
3374 /* PR 19872: A version number of 0 probably means that there is padding
3375 at the end of the .debug_info section. Gold puts it there when
3376 performing an incremental link, for example. So do not generate
3377 an error, just return a NULL. */
3381 (_("DWARF error: found dwarf version '%u', this reader"
3382 " only handles version 2, 3, 4 and 5 information"), version);
3383 bfd_set_error (bfd_error_bad_value);
3389 unit_type = DW_UT_compile;
3392 unit_type = read_1_byte (abfd, info_ptr, end_ptr);
3395 addr_size = read_1_byte (abfd, info_ptr, end_ptr);
3399 BFD_ASSERT (offset_size == 4 || offset_size == 8);
3400 if (offset_size == 4)
3401 abbrev_offset = read_4_bytes (abfd, info_ptr, end_ptr);
3403 abbrev_offset = read_8_bytes (abfd, info_ptr, end_ptr);
3404 info_ptr += offset_size;
3408 addr_size = read_1_byte (abfd, info_ptr, end_ptr);
3412 if (unit_type == DW_UT_type)
3414 /* Skip type signature. */
3417 /* Skip type offset. */
3418 info_ptr += offset_size;
3421 if (addr_size > sizeof (bfd_vma))
3424 /* xgettext: c-format */
3425 (_("DWARF error: found address size '%u', this reader"
3426 " can not handle sizes greater than '%u'"),
3428 (unsigned int) sizeof (bfd_vma));
3429 bfd_set_error (bfd_error_bad_value);
3433 if (addr_size != 2 && addr_size != 4 && addr_size != 8)
3436 ("DWARF error: found address size '%u', this reader"
3437 " can only handle address sizes '2', '4' and '8'", addr_size);
3438 bfd_set_error (bfd_error_bad_value);
3442 /* Read the abbrevs for this compilation unit into a table. */
3443 abbrevs = read_abbrevs (abfd, abbrev_offset, stash);
3447 abbrev_number = _bfd_safe_read_leb128 (abfd, info_ptr, &bytes_read,
3449 info_ptr += bytes_read;
3450 if (! abbrev_number)
3452 /* PR 19872: An abbrev number of 0 probably means that there is padding
3453 at the end of the .debug_abbrev section. Gold puts it there when
3454 performing an incremental link, for example. So do not generate
3455 an error, just return a NULL. */
3459 abbrev = lookup_abbrev (abbrev_number, abbrevs);
3462 _bfd_error_handler (_("DWARF error: could not find abbrev number %u"),
3464 bfd_set_error (bfd_error_bad_value);
3468 amt = sizeof (struct comp_unit);
3469 unit = (struct comp_unit *) bfd_zalloc (abfd, amt);
3473 unit->version = version;
3474 unit->addr_size = addr_size;
3475 unit->offset_size = offset_size;
3476 unit->abbrevs = abbrevs;
3477 unit->end_ptr = end_ptr;
3478 unit->stash = stash;
3479 unit->info_ptr_unit = info_ptr_unit;
3481 for (i = 0; i < abbrev->num_attrs; ++i)
3483 info_ptr = read_attribute (&attr, &abbrev->attrs[i], unit, info_ptr, end_ptr);
3484 if (info_ptr == NULL)
3487 /* Store the data if it is of an attribute we want to keep in a
3488 partial symbol table. */
3491 case DW_AT_stmt_list:
3493 unit->line_offset = attr.u.val;
3497 if (is_str_attr (attr.form))
3498 unit->name = attr.u.str;
3502 low_pc = attr.u.val;
3503 /* If the compilation unit DIE has a DW_AT_low_pc attribute,
3504 this is the base address to use when reading location
3505 lists or range lists. */
3506 if (abbrev->tag == DW_TAG_compile_unit)
3507 unit->base_address = low_pc;
3511 high_pc = attr.u.val;
3512 high_pc_relative = attr.form != DW_FORM_addr;
3516 if (!read_rangelist (unit, &unit->arange, attr.u.val))
3520 case DW_AT_comp_dir:
3522 char *comp_dir = attr.u.str;
3524 /* PR 17512: file: 1fe726be. */
3525 if (! is_str_attr (attr.form))
3528 (_("DWARF error: DW_AT_comp_dir attribute encountered with a non-string form"));
3534 /* Irix 6.2 native cc prepends <machine>.: to the compilation
3535 directory, get rid of it. */
3536 char *cp = strchr (comp_dir, ':');
3538 if (cp && cp != comp_dir && cp[-1] == '.' && cp[1] == '/')
3541 unit->comp_dir = comp_dir;
3545 case DW_AT_language:
3546 unit->lang = attr.u.val;
3553 if (high_pc_relative)
3557 if (!arange_add (unit, &unit->arange, low_pc, high_pc))
3561 unit->first_child_die_ptr = info_ptr;
3565 /* Return TRUE if UNIT may contain the address given by ADDR. When
3566 there are functions written entirely with inline asm statements, the
3567 range info in the compilation unit header may not be correct. We
3568 need to consult the line info table to see if a compilation unit
3569 really contains the given address. */
3572 comp_unit_contains_address (struct comp_unit *unit, bfd_vma addr)
3574 struct arange *arange;
3579 arange = &unit->arange;
3582 if (addr >= arange->low && addr < arange->high)
3584 arange = arange->next;
3591 /* If UNIT contains ADDR, set the output parameters to the values for
3592 the line containing ADDR. The output parameters, FILENAME_PTR,
3593 FUNCTION_PTR, and LINENUMBER_PTR, are pointers to the objects
3596 Returns the range of addresses covered by the entry that was used
3597 to fill in *LINENUMBER_PTR or 0 if it was not filled in. */
3600 comp_unit_find_nearest_line (struct comp_unit *unit,
3602 const char **filename_ptr,
3603 struct funcinfo **function_ptr,
3604 unsigned int *linenumber_ptr,
3605 unsigned int *discriminator_ptr,
3606 struct dwarf2_debug *stash)
3610 if (!comp_unit_maybe_decode_line_info (unit, stash))
3613 *function_ptr = NULL;
3614 func_p = lookup_address_in_function_table (unit, addr, function_ptr);
3615 if (func_p && (*function_ptr)->tag == DW_TAG_inlined_subroutine)
3616 stash->inliner_chain = *function_ptr;
3618 return lookup_address_in_line_info_table (unit->line_table, addr,
3624 /* Check to see if line info is already decoded in a comp_unit.
3625 If not, decode it. Returns TRUE if no errors were encountered;
3629 comp_unit_maybe_decode_line_info (struct comp_unit *unit,
3630 struct dwarf2_debug *stash)
3635 if (! unit->line_table)
3637 if (! unit->stmtlist)
3643 unit->line_table = decode_line_info (unit, stash);
3645 if (! unit->line_table)
3651 if (unit->first_child_die_ptr < unit->end_ptr
3652 && ! scan_unit_for_symbols (unit))
3662 /* If UNIT contains SYM at ADDR, set the output parameters to the
3663 values for the line containing SYM. The output parameters,
3664 FILENAME_PTR, and LINENUMBER_PTR, are pointers to the objects to be
3667 Return TRUE if UNIT contains SYM, and no errors were encountered;
3671 comp_unit_find_line (struct comp_unit *unit,
3674 const char **filename_ptr,
3675 unsigned int *linenumber_ptr,
3676 struct dwarf2_debug *stash)
3678 if (!comp_unit_maybe_decode_line_info (unit, stash))
3681 if (sym->flags & BSF_FUNCTION)
3682 return lookup_symbol_in_function_table (unit, sym, addr,
3686 return lookup_symbol_in_variable_table (unit, sym, addr,
3691 static struct funcinfo *
3692 reverse_funcinfo_list (struct funcinfo *head)
3694 struct funcinfo *rhead;
3695 struct funcinfo *temp;
3697 for (rhead = NULL; head; head = temp)
3699 temp = head->prev_func;
3700 head->prev_func = rhead;
3706 static struct varinfo *
3707 reverse_varinfo_list (struct varinfo *head)
3709 struct varinfo *rhead;
3710 struct varinfo *temp;
3712 for (rhead = NULL; head; head = temp)
3714 temp = head->prev_var;
3715 head->prev_var = rhead;
3721 /* Extract all interesting funcinfos and varinfos of a compilation
3722 unit into hash tables for faster lookup. Returns TRUE if no
3723 errors were enountered; FALSE otherwise. */
3726 comp_unit_hash_info (struct dwarf2_debug *stash,
3727 struct comp_unit *unit,
3728 struct info_hash_table *funcinfo_hash_table,
3729 struct info_hash_table *varinfo_hash_table)
3731 struct funcinfo* each_func;
3732 struct varinfo* each_var;
3733 bfd_boolean okay = TRUE;
3735 BFD_ASSERT (stash->info_hash_status != STASH_INFO_HASH_DISABLED);
3737 if (!comp_unit_maybe_decode_line_info (unit, stash))
3740 BFD_ASSERT (!unit->cached);
3742 /* To preserve the original search order, we went to visit the function
3743 infos in the reversed order of the list. However, making the list
3744 bi-directional use quite a bit of extra memory. So we reverse
3745 the list first, traverse the list in the now reversed order and
3746 finally reverse the list again to get back the original order. */
3747 unit->function_table = reverse_funcinfo_list (unit->function_table);
3748 for (each_func = unit->function_table;
3750 each_func = each_func->prev_func)
3752 /* Skip nameless functions. */
3753 if (each_func->name)
3754 /* There is no need to copy name string into hash table as
3755 name string is either in the dwarf string buffer or
3756 info in the stash. */
3757 okay = insert_info_hash_table (funcinfo_hash_table, each_func->name,
3758 (void*) each_func, FALSE);
3760 unit->function_table = reverse_funcinfo_list (unit->function_table);
3764 /* We do the same for variable infos. */
3765 unit->variable_table = reverse_varinfo_list (unit->variable_table);
3766 for (each_var = unit->variable_table;
3768 each_var = each_var->prev_var)
3770 /* Skip stack vars and vars with no files or names. */
3771 if (each_var->stack == 0
3772 && each_var->file != NULL
3773 && each_var->name != NULL)
3774 /* There is no need to copy name string into hash table as
3775 name string is either in the dwarf string buffer or
3776 info in the stash. */
3777 okay = insert_info_hash_table (varinfo_hash_table, each_var->name,
3778 (void*) each_var, FALSE);
3781 unit->variable_table = reverse_varinfo_list (unit->variable_table);
3782 unit->cached = TRUE;
3786 /* Locate a section in a BFD containing debugging info. The search starts
3787 from the section after AFTER_SEC, or from the first section in the BFD if
3788 AFTER_SEC is NULL. The search works by examining the names of the
3789 sections. There are three permissiable names. The first two are given
3790 by DEBUG_SECTIONS[debug_info] (whose standard DWARF2 names are .debug_info
3791 and .zdebug_info). The third is a prefix .gnu.linkonce.wi.
3792 This is a variation on the .debug_info section which has a checksum
3793 describing the contents appended onto the name. This allows the linker to
3794 identify and discard duplicate debugging sections for different
3795 compilation units. */
3796 #define GNU_LINKONCE_INFO ".gnu.linkonce.wi."
3799 find_debug_info (bfd *abfd, const struct dwarf_debug_section *debug_sections,
3800 asection *after_sec)
3805 if (after_sec == NULL)
3807 look = debug_sections[debug_info].uncompressed_name;
3808 msec = bfd_get_section_by_name (abfd, look);
3812 look = debug_sections[debug_info].compressed_name;
3815 msec = bfd_get_section_by_name (abfd, look);
3820 for (msec = abfd->sections; msec != NULL; msec = msec->next)
3821 if (CONST_STRNEQ (msec->name, GNU_LINKONCE_INFO))
3827 for (msec = after_sec->next; msec != NULL; msec = msec->next)
3829 look = debug_sections[debug_info].uncompressed_name;
3830 if (strcmp (msec->name, look) == 0)
3833 look = debug_sections[debug_info].compressed_name;
3834 if (look != NULL && strcmp (msec->name, look) == 0)
3837 if (CONST_STRNEQ (msec->name, GNU_LINKONCE_INFO))
3844 /* Transfer VMAs from object file to separate debug file. */
3847 set_debug_vma (bfd *orig_bfd, bfd *debug_bfd)
3851 for (s = orig_bfd->sections, d = debug_bfd->sections;
3852 s != NULL && d != NULL;
3853 s = s->next, d = d->next)
3855 if ((d->flags & SEC_DEBUGGING) != 0)
3857 /* ??? Assumes 1-1 correspondence between sections in the
3859 if (strcmp (s->name, d->name) == 0)
3861 d->output_section = s->output_section;
3862 d->output_offset = s->output_offset;
3868 /* Unset vmas for adjusted sections in STASH. */
3871 unset_sections (struct dwarf2_debug *stash)
3874 struct adjusted_section *p;
3876 i = stash->adjusted_section_count;
3877 p = stash->adjusted_sections;
3878 for (; i > 0; i--, p++)
3879 p->section->vma = 0;
3882 /* Set VMAs for allocated and .debug_info sections in ORIG_BFD, a
3883 relocatable object file. VMAs are normally all zero in relocatable
3884 object files, so if we want to distinguish locations in sections by
3885 address we need to set VMAs so the sections do not overlap. We
3886 also set VMA on .debug_info so that when we have multiple
3887 .debug_info sections (or the linkonce variant) they also do not
3888 overlap. The multiple .debug_info sections make up a single
3889 logical section. ??? We should probably do the same for other
3893 place_sections (bfd *orig_bfd, struct dwarf2_debug *stash)
3896 struct adjusted_section *p;
3898 const char *debug_info_name;
3900 if (stash->adjusted_section_count != 0)
3902 i = stash->adjusted_section_count;
3903 p = stash->adjusted_sections;
3904 for (; i > 0; i--, p++)
3905 p->section->vma = p->adj_vma;
3909 debug_info_name = stash->debug_sections[debug_info].uncompressed_name;
3916 for (sect = abfd->sections; sect != NULL; sect = sect->next)
3920 if ((sect->output_section != NULL
3921 && sect->output_section != sect
3922 && (sect->flags & SEC_DEBUGGING) == 0)
3926 is_debug_info = (strcmp (sect->name, debug_info_name) == 0
3927 || CONST_STRNEQ (sect->name, GNU_LINKONCE_INFO));
3929 if (!((sect->flags & SEC_ALLOC) != 0 && abfd == orig_bfd)
3935 if (abfd == stash->bfd_ptr)
3937 abfd = stash->bfd_ptr;
3941 stash->adjusted_section_count = -1;
3944 bfd_vma last_vma = 0, last_dwarf = 0;
3945 bfd_size_type amt = i * sizeof (struct adjusted_section);
3947 p = (struct adjusted_section *) bfd_malloc (amt);
3951 stash->adjusted_sections = p;
3952 stash->adjusted_section_count = i;
3959 for (sect = abfd->sections; sect != NULL; sect = sect->next)
3964 if ((sect->output_section != NULL
3965 && sect->output_section != sect
3966 && (sect->flags & SEC_DEBUGGING) == 0)
3970 is_debug_info = (strcmp (sect->name, debug_info_name) == 0
3971 || CONST_STRNEQ (sect->name, GNU_LINKONCE_INFO));
3973 if (!((sect->flags & SEC_ALLOC) != 0 && abfd == orig_bfd)
3977 sz = sect->rawsize ? sect->rawsize : sect->size;
3981 BFD_ASSERT (sect->alignment_power == 0);
3982 sect->vma = last_dwarf;
3987 /* Align the new address to the current section
3989 last_vma = ((last_vma
3990 + ~(-((bfd_vma) 1 << sect->alignment_power)))
3991 & (-((bfd_vma) 1 << sect->alignment_power)));
3992 sect->vma = last_vma;
3997 p->adj_vma = sect->vma;
4000 if (abfd == stash->bfd_ptr)
4002 abfd = stash->bfd_ptr;
4006 if (orig_bfd != stash->bfd_ptr)
4007 set_debug_vma (orig_bfd, stash->bfd_ptr);
4012 /* Look up a funcinfo by name using the given info hash table. If found,
4013 also update the locations pointed to by filename_ptr and linenumber_ptr.
4015 This function returns TRUE if a funcinfo that matches the given symbol
4016 and address is found with any error; otherwise it returns FALSE. */
4019 info_hash_lookup_funcinfo (struct info_hash_table *hash_table,
4022 const char **filename_ptr,
4023 unsigned int *linenumber_ptr)
4025 struct funcinfo* each_func;
4026 struct funcinfo* best_fit = NULL;
4027 bfd_vma best_fit_len = 0;
4028 struct info_list_node *node;
4029 struct arange *arange;
4030 const char *name = bfd_asymbol_name (sym);
4031 asection *sec = bfd_get_section (sym);
4033 for (node = lookup_info_hash_table (hash_table, name);
4037 each_func = (struct funcinfo *) node->info;
4038 for (arange = &each_func->arange;
4040 arange = arange->next)
4042 if ((!each_func->sec || each_func->sec == sec)
4043 && addr >= arange->low
4044 && addr < arange->high
4046 || arange->high - arange->low < best_fit_len))
4048 best_fit = each_func;
4049 best_fit_len = arange->high - arange->low;
4056 best_fit->sec = sec;
4057 *filename_ptr = best_fit->file;
4058 *linenumber_ptr = best_fit->line;
4065 /* Look up a varinfo by name using the given info hash table. If found,
4066 also update the locations pointed to by filename_ptr and linenumber_ptr.
4068 This function returns TRUE if a varinfo that matches the given symbol
4069 and address is found with any error; otherwise it returns FALSE. */
4072 info_hash_lookup_varinfo (struct info_hash_table *hash_table,
4075 const char **filename_ptr,
4076 unsigned int *linenumber_ptr)
4078 const char *name = bfd_asymbol_name (sym);
4079 asection *sec = bfd_get_section (sym);
4080 struct varinfo* each;
4081 struct info_list_node *node;
4083 for (node = lookup_info_hash_table (hash_table, name);
4087 each = (struct varinfo *) node->info;
4088 if (each->addr == addr
4089 && (!each->sec || each->sec == sec))
4092 *filename_ptr = each->file;
4093 *linenumber_ptr = each->line;
4101 /* Update the funcinfo and varinfo info hash tables if they are
4102 not up to date. Returns TRUE if there is no error; otherwise
4103 returns FALSE and disable the info hash tables. */
4106 stash_maybe_update_info_hash_tables (struct dwarf2_debug *stash)
4108 struct comp_unit *each;
4110 /* Exit if hash tables are up-to-date. */
4111 if (stash->all_comp_units == stash->hash_units_head)
4114 if (stash->hash_units_head)
4115 each = stash->hash_units_head->prev_unit;
4117 each = stash->last_comp_unit;
4121 if (!comp_unit_hash_info (stash, each, stash->funcinfo_hash_table,
4122 stash->varinfo_hash_table))
4124 stash->info_hash_status = STASH_INFO_HASH_DISABLED;
4127 each = each->prev_unit;
4130 stash->hash_units_head = stash->all_comp_units;
4134 /* Check consistency of info hash tables. This is for debugging only. */
4136 static void ATTRIBUTE_UNUSED
4137 stash_verify_info_hash_table (struct dwarf2_debug *stash)
4139 struct comp_unit *each_unit;
4140 struct funcinfo *each_func;
4141 struct varinfo *each_var;
4142 struct info_list_node *node;
4145 for (each_unit = stash->all_comp_units;
4147 each_unit = each_unit->next_unit)
4149 for (each_func = each_unit->function_table;
4151 each_func = each_func->prev_func)
4153 if (!each_func->name)
4155 node = lookup_info_hash_table (stash->funcinfo_hash_table,
4159 while (node && !found)
4161 found = node->info == each_func;
4167 for (each_var = each_unit->variable_table;
4169 each_var = each_var->prev_var)
4171 if (!each_var->name || !each_var->file || each_var->stack)
4173 node = lookup_info_hash_table (stash->varinfo_hash_table,
4177 while (node && !found)
4179 found = node->info == each_var;
4187 /* Check to see if we want to enable the info hash tables, which consume
4188 quite a bit of memory. Currently we only check the number times
4189 bfd_dwarf2_find_line is called. In the future, we may also want to
4190 take the number of symbols into account. */
4193 stash_maybe_enable_info_hash_tables (bfd *abfd, struct dwarf2_debug *stash)
4195 BFD_ASSERT (stash->info_hash_status == STASH_INFO_HASH_OFF);
4197 if (stash->info_hash_count++ < STASH_INFO_HASH_TRIGGER)
4200 /* FIXME: Maybe we should check the reduce_memory_overheads
4201 and optimize fields in the bfd_link_info structure ? */
4203 /* Create hash tables. */
4204 stash->funcinfo_hash_table = create_info_hash_table (abfd);
4205 stash->varinfo_hash_table = create_info_hash_table (abfd);
4206 if (!stash->funcinfo_hash_table || !stash->varinfo_hash_table)
4208 /* Turn off info hashes if any allocation above fails. */
4209 stash->info_hash_status = STASH_INFO_HASH_DISABLED;
4212 /* We need a forced update so that the info hash tables will
4213 be created even though there is no compilation unit. That
4214 happens if STASH_INFO_HASH_TRIGGER is 0. */
4215 stash_maybe_update_info_hash_tables (stash);
4216 stash->info_hash_status = STASH_INFO_HASH_ON;
4219 /* Find the file and line associated with a symbol and address using the
4220 info hash tables of a stash. If there is a match, the function returns
4221 TRUE and update the locations pointed to by filename_ptr and linenumber_ptr;
4222 otherwise it returns FALSE. */
4225 stash_find_line_fast (struct dwarf2_debug *stash,
4228 const char **filename_ptr,
4229 unsigned int *linenumber_ptr)
4231 BFD_ASSERT (stash->info_hash_status == STASH_INFO_HASH_ON);
4233 if (sym->flags & BSF_FUNCTION)
4234 return info_hash_lookup_funcinfo (stash->funcinfo_hash_table, sym, addr,
4235 filename_ptr, linenumber_ptr);
4236 return info_hash_lookup_varinfo (stash->varinfo_hash_table, sym, addr,
4237 filename_ptr, linenumber_ptr);
4240 /* Save current section VMAs. */
4243 save_section_vma (const bfd *abfd, struct dwarf2_debug *stash)
4248 if (abfd->section_count == 0)
4250 stash->sec_vma = bfd_malloc (sizeof (*stash->sec_vma) * abfd->section_count);
4251 if (stash->sec_vma == NULL)
4253 stash->sec_vma_count = abfd->section_count;
4254 for (i = 0, s = abfd->sections;
4255 s != NULL && i < abfd->section_count;
4258 if (s->output_section != NULL)
4259 stash->sec_vma[i] = s->output_section->vma + s->output_offset;
4261 stash->sec_vma[i] = s->vma;
4266 /* Compare current section VMAs against those at the time the stash
4267 was created. If find_nearest_line is used in linker warnings or
4268 errors early in the link process, the debug info stash will be
4269 invalid for later calls. This is because we relocate debug info
4270 sections, so the stashed section contents depend on symbol values,
4271 which in turn depend on section VMAs. */
4274 section_vma_same (const bfd *abfd, const struct dwarf2_debug *stash)
4279 /* PR 24334: If the number of sections in ABFD has changed between
4280 when the stash was created and now, then we cannot trust the
4281 stashed vma information. */
4282 if (abfd->section_count != stash->sec_vma_count)
4285 for (i = 0, s = abfd->sections;
4286 s != NULL && i < abfd->section_count;
4291 if (s->output_section != NULL)
4292 vma = s->output_section->vma + s->output_offset;
4295 if (vma != stash->sec_vma[i])
4301 /* Read debug information from DEBUG_BFD when DEBUG_BFD is specified.
4302 If DEBUG_BFD is not specified, we read debug information from ABFD
4303 or its gnu_debuglink. The results will be stored in PINFO.
4304 The function returns TRUE iff debug information is ready. */
4307 _bfd_dwarf2_slurp_debug_info (bfd *abfd, bfd *debug_bfd,
4308 const struct dwarf_debug_section *debug_sections,
4311 bfd_boolean do_place)
4313 bfd_size_type amt = sizeof (struct dwarf2_debug);
4314 bfd_size_type total_size;
4316 struct dwarf2_debug *stash = (struct dwarf2_debug *) *pinfo;
4320 if (stash->orig_bfd == abfd
4321 && section_vma_same (abfd, stash))
4323 /* Check that we did previously find some debug information
4324 before attempting to make use of it. */
4325 if (stash->bfd_ptr != NULL)
4327 if (do_place && !place_sections (abfd, stash))
4334 _bfd_dwarf2_cleanup_debug_info (abfd, pinfo);
4335 memset (stash, 0, amt);
4339 stash = (struct dwarf2_debug *) bfd_zalloc (abfd, amt);
4343 stash->orig_bfd = abfd;
4344 stash->debug_sections = debug_sections;
4345 stash->syms = symbols;
4346 if (!save_section_vma (abfd, stash))
4351 if (debug_bfd == NULL)
4354 msec = find_debug_info (debug_bfd, debug_sections, NULL);
4355 if (msec == NULL && abfd == debug_bfd)
4357 char * debug_filename;
4359 debug_filename = bfd_follow_build_id_debuglink (abfd, DEBUGDIR);
4360 if (debug_filename == NULL)
4361 debug_filename = bfd_follow_gnu_debuglink (abfd, DEBUGDIR);
4363 if (debug_filename == NULL)
4364 /* No dwarf2 info, and no gnu_debuglink to follow.
4365 Note that at this point the stash has been allocated, but
4366 contains zeros. This lets future calls to this function
4367 fail more quickly. */
4370 /* Set BFD_DECOMPRESS to decompress debug sections. */
4371 if ((debug_bfd = bfd_openr (debug_filename, NULL)) == NULL
4372 || !(debug_bfd->flags |= BFD_DECOMPRESS,
4373 bfd_check_format (debug_bfd, bfd_object))
4374 || (msec = find_debug_info (debug_bfd,
4375 debug_sections, NULL)) == NULL
4376 || !bfd_generic_link_read_symbols (debug_bfd))
4379 bfd_close (debug_bfd);
4380 /* FIXME: Should we report our failure to follow the debuglink ? */
4381 free (debug_filename);
4385 symbols = bfd_get_outsymbols (debug_bfd);
4386 stash->syms = symbols;
4387 stash->close_on_cleanup = TRUE;
4389 stash->bfd_ptr = debug_bfd;
4392 && !place_sections (abfd, stash))
4395 /* There can be more than one DWARF2 info section in a BFD these
4396 days. First handle the easy case when there's only one. If
4397 there's more than one, try case two: none of the sections is
4398 compressed. In that case, read them all in and produce one
4399 large stash. We do this in two passes - in the first pass we
4400 just accumulate the section sizes, and in the second pass we
4401 read in the section's contents. (The allows us to avoid
4402 reallocing the data as we add sections to the stash.) If
4403 some or all sections are compressed, then do things the slow
4404 way, with a bunch of reallocs. */
4406 if (! find_debug_info (debug_bfd, debug_sections, msec))
4408 /* Case 1: only one info section. */
4409 total_size = msec->size;
4410 if (! read_section (debug_bfd, &stash->debug_sections[debug_info],
4412 &stash->info_ptr_memory, &total_size))
4417 /* Case 2: multiple sections. */
4418 for (total_size = 0;
4420 msec = find_debug_info (debug_bfd, debug_sections, msec))
4421 total_size += msec->size;
4423 stash->info_ptr_memory = (bfd_byte *) bfd_malloc (total_size);
4424 if (stash->info_ptr_memory == NULL)
4428 for (msec = find_debug_info (debug_bfd, debug_sections, NULL);
4430 msec = find_debug_info (debug_bfd, debug_sections, msec))
4438 if (!(bfd_simple_get_relocated_section_contents
4439 (debug_bfd, msec, stash->info_ptr_memory + total_size,
4447 stash->info_ptr = stash->info_ptr_memory;
4448 stash->info_ptr_end = stash->info_ptr + total_size;
4449 stash->sec = find_debug_info (debug_bfd, debug_sections, NULL);
4450 stash->sec_info_ptr = stash->info_ptr;
4454 /* Scan the debug information in PINFO looking for a DW_TAG_subprogram
4455 abbrev with a DW_AT_low_pc attached to it. Then lookup that same
4456 symbol in SYMBOLS and return the difference between the low_pc and
4457 the symbol's address. Returns 0 if no suitable symbol could be found. */
4460 _bfd_dwarf2_find_symbol_bias (asymbol ** symbols, void ** pinfo)
4462 struct dwarf2_debug *stash;
4463 struct comp_unit * unit;
4465 stash = (struct dwarf2_debug *) *pinfo;
4467 if (stash == NULL || symbols == NULL)
4470 for (unit = stash->all_comp_units; unit; unit = unit->next_unit)
4472 struct funcinfo * func;
4474 comp_unit_maybe_decode_line_info (unit, stash);
4476 for (func = unit->function_table; func != NULL; func = func->prev_func)
4477 if (func->name && func->arange.low)
4481 /* FIXME: Do we need to scan the aranges looking for the lowest pc value ? */
4483 for (psym = symbols; * psym != NULL; psym++)
4485 asymbol * sym = * psym;
4487 if (sym->flags & BSF_FUNCTION
4488 && sym->section != NULL
4489 && strcmp (sym->name, func->name) == 0)
4490 return ((bfd_signed_vma) func->arange.low) -
4491 ((bfd_signed_vma) (sym->value + sym->section->vma));
4499 /* Find the source code location of SYMBOL. If SYMBOL is NULL
4500 then find the nearest source code location corresponding to
4501 the address SECTION + OFFSET.
4502 Returns TRUE if the line is found without error and fills in
4503 FILENAME_PTR and LINENUMBER_PTR. In the case where SYMBOL was
4504 NULL the FUNCTIONNAME_PTR is also filled in.
4505 SYMBOLS contains the symbol table for ABFD.
4506 DEBUG_SECTIONS contains the name of the dwarf debug sections.
4507 field and in the abbreviation offset, or zero to indicate that the
4508 default value should be used. */
4511 _bfd_dwarf2_find_nearest_line (bfd *abfd,
4516 const char **filename_ptr,
4517 const char **functionname_ptr,
4518 unsigned int *linenumber_ptr,
4519 unsigned int *discriminator_ptr,
4520 const struct dwarf_debug_section *debug_sections,
4523 /* Read each compilation unit from the section .debug_info, and check
4524 to see if it contains the address we are searching for. If yes,
4525 lookup the address, and return the line number info. If no, go
4526 on to the next compilation unit.
4528 We keep a list of all the previously read compilation units, and
4529 a pointer to the next un-read compilation unit. Check the
4530 previously read units before reading more. */
4531 struct dwarf2_debug *stash;
4532 /* What address are we looking for? */
4534 struct comp_unit* each;
4535 struct funcinfo *function = NULL;
4536 bfd_boolean found = FALSE;
4537 bfd_boolean do_line;
4539 *filename_ptr = NULL;
4540 if (functionname_ptr != NULL)
4541 *functionname_ptr = NULL;
4542 *linenumber_ptr = 0;
4543 if (discriminator_ptr)
4544 *discriminator_ptr = 0;
4546 if (! _bfd_dwarf2_slurp_debug_info (abfd, NULL, debug_sections,
4548 (abfd->flags & (EXEC_P | DYNAMIC)) == 0))
4551 stash = (struct dwarf2_debug *) *pinfo;
4553 do_line = symbol != NULL;
4556 BFD_ASSERT (section == NULL && offset == 0 && functionname_ptr == NULL);
4557 section = bfd_get_section (symbol);
4558 addr = symbol->value;
4562 BFD_ASSERT (section != NULL && functionname_ptr != NULL);
4565 /* If we have no SYMBOL but the section we're looking at is not a
4566 code section, then take a look through the list of symbols to see
4567 if we have a symbol at the address we're looking for. If we do
4568 then use this to look up line information. This will allow us to
4569 give file and line results for data symbols. We exclude code
4570 symbols here, if we look up a function symbol and then look up the
4571 line information we'll actually return the line number for the
4572 opening '{' rather than the function definition line. This is
4573 because looking up by symbol uses the line table, in which the
4574 first line for a function is usually the opening '{', while
4575 looking up the function by section + offset uses the
4576 DW_AT_decl_line from the function DW_TAG_subprogram for the line,
4577 which will be the line of the function name. */
4578 if (symbols != NULL && (section->flags & SEC_CODE) == 0)
4582 for (tmp = symbols; (*tmp) != NULL; ++tmp)
4583 if ((*tmp)->the_bfd == abfd
4584 && (*tmp)->section == section
4585 && (*tmp)->value == offset
4586 && ((*tmp)->flags & BSF_SECTION_SYM) == 0)
4590 /* For local symbols, keep going in the hope we find a
4592 if ((symbol->flags & BSF_GLOBAL) != 0)
4598 if (section->output_section)
4599 addr += section->output_section->vma + section->output_offset;
4601 addr += section->vma;
4603 /* A null info_ptr indicates that there is no dwarf2 info
4604 (or that an error occured while setting up the stash). */
4605 if (! stash->info_ptr)
4608 stash->inliner_chain = NULL;
4610 /* Check the previously read comp. units first. */
4613 /* The info hash tables use quite a bit of memory. We may not want to
4614 always use them. We use some heuristics to decide if and when to
4616 if (stash->info_hash_status == STASH_INFO_HASH_OFF)
4617 stash_maybe_enable_info_hash_tables (abfd, stash);
4619 /* Keep info hash table up to date if they are available. Note that we
4620 may disable the hash tables if there is any error duing update. */
4621 if (stash->info_hash_status == STASH_INFO_HASH_ON)
4622 stash_maybe_update_info_hash_tables (stash);
4624 if (stash->info_hash_status == STASH_INFO_HASH_ON)
4626 found = stash_find_line_fast (stash, symbol, addr, filename_ptr,
4633 /* Check the previously read comp. units first. */
4634 for (each = stash->all_comp_units; each; each = each->next_unit)
4635 if ((symbol->flags & BSF_FUNCTION) == 0
4636 || each->arange.high == 0
4637 || comp_unit_contains_address (each, addr))
4639 found = comp_unit_find_line (each, symbol, addr, filename_ptr,
4640 linenumber_ptr, stash);
4648 bfd_vma min_range = (bfd_vma) -1;
4649 const char * local_filename = NULL;
4650 struct funcinfo *local_function = NULL;
4651 unsigned int local_linenumber = 0;
4652 unsigned int local_discriminator = 0;
4654 for (each = stash->all_comp_units; each; each = each->next_unit)
4656 bfd_vma range = (bfd_vma) -1;
4658 found = ((each->arange.high == 0
4659 || comp_unit_contains_address (each, addr))
4660 && (range = comp_unit_find_nearest_line (each, addr,
4664 & local_discriminator,
4668 /* PRs 15935 15994: Bogus debug information may have provided us
4669 with an erroneous match. We attempt to counter this by
4670 selecting the match that has the smallest address range
4671 associated with it. (We are assuming that corrupt debug info
4672 will tend to result in extra large address ranges rather than
4673 extra small ranges).
4675 This does mean that we scan through all of the CUs associated
4676 with the bfd each time this function is called. But this does
4677 have the benefit of producing consistent results every time the
4678 function is called. */
4679 if (range <= min_range)
4681 if (filename_ptr && local_filename)
4682 * filename_ptr = local_filename;
4684 function = local_function;
4685 if (discriminator_ptr && local_discriminator)
4686 * discriminator_ptr = local_discriminator;
4687 if (local_linenumber)
4688 * linenumber_ptr = local_linenumber;
4694 if (* linenumber_ptr)
4701 /* Read each remaining comp. units checking each as they are read. */
4702 while (stash->info_ptr < stash->info_ptr_end)
4705 unsigned int offset_size;
4706 bfd_byte *info_ptr_unit = stash->info_ptr;
4708 length = read_4_bytes (stash->bfd_ptr, stash->info_ptr, stash->info_ptr_end);
4709 /* A 0xffffff length is the DWARF3 way of indicating
4710 we use 64-bit offsets, instead of 32-bit offsets. */
4711 if (length == 0xffffffff)
4714 length = read_8_bytes (stash->bfd_ptr, stash->info_ptr + 4, stash->info_ptr_end);
4715 stash->info_ptr += 12;
4717 /* A zero length is the IRIX way of indicating 64-bit offsets,
4718 mostly because the 64-bit length will generally fit in 32
4719 bits, and the endianness helps. */
4720 else if (length == 0)
4723 length = read_4_bytes (stash->bfd_ptr, stash->info_ptr + 4, stash->info_ptr_end);
4724 stash->info_ptr += 8;
4726 /* In the absence of the hints above, we assume 32-bit DWARF2
4727 offsets even for targets with 64-bit addresses, because:
4728 a) most of the time these targets will not have generated
4729 more than 2Gb of debug info and so will not need 64-bit
4732 b) if they do use 64-bit offsets but they are not using
4733 the size hints that are tested for above then they are
4734 not conforming to the DWARF3 standard anyway. */
4738 stash->info_ptr += 4;
4746 if (stash->info_ptr + length > stash->info_ptr_end)
4749 each = parse_comp_unit (stash, length, info_ptr_unit,
4752 /* The dwarf information is damaged, don't trust it any
4756 new_ptr = stash->info_ptr + length;
4757 /* PR 17512: file: 1500698c. */
4758 if (new_ptr < stash->info_ptr)
4760 /* A corrupt length value - do not trust the info any more. */
4765 stash->info_ptr = new_ptr;
4767 if (stash->all_comp_units)
4768 stash->all_comp_units->prev_unit = each;
4770 stash->last_comp_unit = each;
4772 each->next_unit = stash->all_comp_units;
4773 stash->all_comp_units = each;
4775 /* DW_AT_low_pc and DW_AT_high_pc are optional for
4776 compilation units. If we don't have them (i.e.,
4777 unit->high == 0), we need to consult the line info table
4778 to see if a compilation unit contains the given
4781 found = (((symbol->flags & BSF_FUNCTION) == 0
4782 || each->arange.high == 0
4783 || comp_unit_contains_address (each, addr))
4784 && comp_unit_find_line (each, symbol, addr,
4789 found = ((each->arange.high == 0
4790 || comp_unit_contains_address (each, addr))
4791 && comp_unit_find_nearest_line (each, addr,
4798 if ((bfd_vma) (stash->info_ptr - stash->sec_info_ptr)
4799 == stash->sec->size)
4801 stash->sec = find_debug_info (stash->bfd_ptr, debug_sections,
4803 stash->sec_info_ptr = stash->info_ptr;
4814 if (!function->is_linkage)
4819 fun = _bfd_elf_find_function (abfd, symbols, section, offset,
4820 *filename_ptr ? NULL : filename_ptr,
4822 sec_vma = section->vma;
4823 if (section->output_section != NULL)
4824 sec_vma = section->output_section->vma + section->output_offset;
4826 && fun->value + sec_vma == function->arange.low)
4827 function->name = *functionname_ptr;
4828 /* Even if we didn't find a linkage name, say that we have
4829 to stop a repeated search of symbols. */
4830 function->is_linkage = TRUE;
4832 *functionname_ptr = function->name;
4834 if ((abfd->flags & (EXEC_P | DYNAMIC)) == 0)
4835 unset_sections (stash);
4841 _bfd_dwarf2_find_inliner_info (bfd *abfd ATTRIBUTE_UNUSED,
4842 const char **filename_ptr,
4843 const char **functionname_ptr,
4844 unsigned int *linenumber_ptr,
4847 struct dwarf2_debug *stash;
4849 stash = (struct dwarf2_debug *) *pinfo;
4852 struct funcinfo *func = stash->inliner_chain;
4854 if (func && func->caller_func)
4856 *filename_ptr = func->caller_file;
4857 *functionname_ptr = func->caller_func->name;
4858 *linenumber_ptr = func->caller_line;
4859 stash->inliner_chain = func->caller_func;
4868 _bfd_dwarf2_cleanup_debug_info (bfd *abfd, void **pinfo)
4870 struct dwarf2_debug *stash = (struct dwarf2_debug *) *pinfo;
4871 struct comp_unit *each;
4873 if (abfd == NULL || stash == NULL)
4876 for (each = stash->all_comp_units; each; each = each->next_unit)
4878 struct abbrev_info **abbrevs = each->abbrevs;
4879 struct funcinfo *function_table = each->function_table;
4880 struct varinfo *variable_table = each->variable_table;
4883 for (i = 0; i < ABBREV_HASH_SIZE; i++)
4885 struct abbrev_info *abbrev = abbrevs[i];
4889 free (abbrev->attrs);
4890 abbrev = abbrev->next;
4894 if (each->line_table)
4896 free (each->line_table->dirs);
4897 free (each->line_table->files);
4900 while (function_table)
4902 if (function_table->file)
4904 free (function_table->file);
4905 function_table->file = NULL;
4908 if (function_table->caller_file)
4910 free (function_table->caller_file);
4911 function_table->caller_file = NULL;
4913 function_table = function_table->prev_func;
4916 if (each->lookup_funcinfo_table)
4918 free (each->lookup_funcinfo_table);
4919 each->lookup_funcinfo_table = NULL;
4922 while (variable_table)
4924 if (variable_table->file)
4926 free (variable_table->file);
4927 variable_table->file = NULL;
4930 variable_table = variable_table->prev_var;
4934 if (stash->funcinfo_hash_table)
4935 bfd_hash_table_free (&stash->funcinfo_hash_table->base);
4936 if (stash->varinfo_hash_table)
4937 bfd_hash_table_free (&stash->varinfo_hash_table->base);
4938 if (stash->dwarf_abbrev_buffer)
4939 free (stash->dwarf_abbrev_buffer);
4940 if (stash->dwarf_line_buffer)
4941 free (stash->dwarf_line_buffer);
4942 if (stash->dwarf_str_buffer)
4943 free (stash->dwarf_str_buffer);
4944 if (stash->dwarf_line_str_buffer)
4945 free (stash->dwarf_line_str_buffer);
4946 if (stash->dwarf_ranges_buffer)
4947 free (stash->dwarf_ranges_buffer);
4948 if (stash->info_ptr_memory)
4949 free (stash->info_ptr_memory);
4950 if (stash->close_on_cleanup)
4951 bfd_close (stash->bfd_ptr);
4952 if (stash->alt_dwarf_str_buffer)
4953 free (stash->alt_dwarf_str_buffer);
4954 if (stash->alt_dwarf_info_buffer)
4955 free (stash->alt_dwarf_info_buffer);
4957 free (stash->sec_vma);
4958 if (stash->adjusted_sections)
4959 free (stash->adjusted_sections);
4960 if (stash->alt_bfd_ptr)
4961 bfd_close (stash->alt_bfd_ptr);
4964 /* Find the function to a particular section and offset,
4965 for error reporting. */
4968 _bfd_elf_find_function (bfd *abfd,
4972 const char **filename_ptr,
4973 const char **functionname_ptr)
4975 struct elf_find_function_cache
4977 asection *last_section;
4979 const char *filename;
4980 bfd_size_type func_size;
4983 if (symbols == NULL)
4986 if (bfd_get_flavour (abfd) != bfd_target_elf_flavour)
4989 cache = elf_tdata (abfd)->elf_find_function_cache;
4992 cache = bfd_zalloc (abfd, sizeof (*cache));
4993 elf_tdata (abfd)->elf_find_function_cache = cache;
4997 if (cache->last_section != section
4998 || cache->func == NULL
4999 || offset < cache->func->value
5000 || offset >= cache->func->value + cache->func_size)
5005 /* ??? Given multiple file symbols, it is impossible to reliably
5006 choose the right file name for global symbols. File symbols are
5007 local symbols, and thus all file symbols must sort before any
5008 global symbols. The ELF spec may be interpreted to say that a
5009 file symbol must sort before other local symbols, but currently
5010 ld -r doesn't do this. So, for ld -r output, it is possible to
5011 make a better choice of file name for local symbols by ignoring
5012 file symbols appearing after a given local symbol. */
5013 enum { nothing_seen, symbol_seen, file_after_symbol_seen } state;
5014 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
5018 state = nothing_seen;
5019 cache->filename = NULL;
5021 cache->func_size = 0;
5022 cache->last_section = section;
5024 for (p = symbols; *p != NULL; p++)
5030 if ((sym->flags & BSF_FILE) != 0)
5033 if (state == symbol_seen)
5034 state = file_after_symbol_seen;
5038 size = bed->maybe_function_sym (sym, section, &code_off);
5040 && code_off <= offset
5041 && (code_off > low_func
5042 || (code_off == low_func
5043 && size > cache->func_size)))
5046 cache->func_size = size;
5047 cache->filename = NULL;
5048 low_func = code_off;
5050 && ((sym->flags & BSF_LOCAL) != 0
5051 || state != file_after_symbol_seen))
5052 cache->filename = bfd_asymbol_name (file);
5054 if (state == nothing_seen)
5055 state = symbol_seen;
5059 if (cache->func == NULL)
5063 *filename_ptr = cache->filename;
5064 if (functionname_ptr)
5065 *functionname_ptr = bfd_asymbol_name (cache->func);