2 Copyright (C) 1994-2018 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. */
173 /* Number of sections whose VMA we must adjust. */
174 int adjusted_section_count;
176 /* Array of sections with adjusted VMA. */
177 struct adjusted_section *adjusted_sections;
179 /* Number of times find_line is called. This is used in
180 the heuristic for enabling the info hash tables. */
183 #define STASH_INFO_HASH_TRIGGER 100
185 /* Hash table mapping symbol names to function infos. */
186 struct info_hash_table *funcinfo_hash_table;
188 /* Hash table mapping symbol names to variable infos. */
189 struct info_hash_table *varinfo_hash_table;
191 /* Head of comp_unit list in the last hash table update. */
192 struct comp_unit *hash_units_head;
194 /* Status of info hash. */
195 int info_hash_status;
196 #define STASH_INFO_HASH_OFF 0
197 #define STASH_INFO_HASH_ON 1
198 #define STASH_INFO_HASH_DISABLED 2
200 /* True if we opened bfd_ptr. */
201 bfd_boolean close_on_cleanup;
211 /* A minimal decoding of DWARF2 compilation units. We only decode
212 what's needed to get to the line number information. */
216 /* Chain the previously read compilation units. */
217 struct comp_unit *next_unit;
219 /* Likewise, chain the compilation unit read after this one.
220 The comp units are stored in reversed reading order. */
221 struct comp_unit *prev_unit;
223 /* Keep the bfd convenient (for memory allocation). */
226 /* The lowest and highest addresses contained in this compilation
227 unit as specified in the compilation unit header. */
228 struct arange arange;
230 /* The DW_AT_name attribute (for error messages). */
233 /* The abbrev hash table. */
234 struct abbrev_info **abbrevs;
236 /* DW_AT_language. */
239 /* Note that an error was found by comp_unit_find_nearest_line. */
242 /* The DW_AT_comp_dir attribute. */
245 /* TRUE if there is a line number table associated with this comp. unit. */
248 /* Pointer to the current comp_unit so that we can find a given entry
250 bfd_byte *info_ptr_unit;
252 /* The offset into .debug_line of the line number table. */
253 unsigned long line_offset;
255 /* Pointer to the first child die for the comp unit. */
256 bfd_byte *first_child_die_ptr;
258 /* The end of the comp unit. */
261 /* The decoded line number, NULL if not yet decoded. */
262 struct line_info_table *line_table;
264 /* A list of the functions found in this comp. unit. */
265 struct funcinfo *function_table;
267 /* A table of function information references searchable by address. */
268 struct lookup_funcinfo *lookup_funcinfo_table;
270 /* Number of functions in the function_table and sorted_function_table. */
271 bfd_size_type number_of_functions;
273 /* A list of the variables found in this comp. unit. */
274 struct varinfo *variable_table;
276 /* Pointer to dwarf2_debug structure. */
277 struct dwarf2_debug *stash;
279 /* DWARF format version for this unit - from unit header. */
282 /* Address size for this unit - from unit header. */
283 unsigned char addr_size;
285 /* Offset size for this unit - from unit header. */
286 unsigned char offset_size;
288 /* Base address for this unit - from DW_AT_low_pc attribute of
289 DW_TAG_compile_unit DIE */
290 bfd_vma base_address;
292 /* TRUE if symbols are cached in hash table for faster lookup by name. */
296 /* This data structure holds the information of an abbrev. */
299 unsigned int number; /* Number identifying abbrev. */
300 enum dwarf_tag tag; /* DWARF tag. */
301 int has_children; /* Boolean. */
302 unsigned int num_attrs; /* Number of attributes. */
303 struct attr_abbrev *attrs; /* An array of attribute descriptions. */
304 struct abbrev_info *next; /* Next in chain. */
309 enum dwarf_attribute name;
310 enum dwarf_form form;
311 bfd_vma implicit_const;
314 /* Map of uncompressed DWARF debug section name to compressed one. It
315 is terminated by NULL uncompressed_name. */
317 const struct dwarf_debug_section dwarf_debug_sections[] =
319 { ".debug_abbrev", ".zdebug_abbrev" },
320 { ".debug_aranges", ".zdebug_aranges" },
321 { ".debug_frame", ".zdebug_frame" },
322 { ".debug_info", ".zdebug_info" },
323 { ".debug_info", ".zdebug_info" },
324 { ".debug_line", ".zdebug_line" },
325 { ".debug_loc", ".zdebug_loc" },
326 { ".debug_macinfo", ".zdebug_macinfo" },
327 { ".debug_macro", ".zdebug_macro" },
328 { ".debug_pubnames", ".zdebug_pubnames" },
329 { ".debug_pubtypes", ".zdebug_pubtypes" },
330 { ".debug_ranges", ".zdebug_ranges" },
331 { ".debug_static_func", ".zdebug_static_func" },
332 { ".debug_static_vars", ".zdebug_static_vars" },
333 { ".debug_str", ".zdebug_str", },
334 { ".debug_str", ".zdebug_str", },
335 { ".debug_line_str", ".zdebug_line_str", },
336 { ".debug_types", ".zdebug_types" },
337 /* GNU DWARF 1 extensions */
338 { ".debug_sfnames", ".zdebug_sfnames" },
339 { ".debug_srcinfo", ".zebug_srcinfo" },
340 /* SGI/MIPS DWARF 2 extensions */
341 { ".debug_funcnames", ".zdebug_funcnames" },
342 { ".debug_typenames", ".zdebug_typenames" },
343 { ".debug_varnames", ".zdebug_varnames" },
344 { ".debug_weaknames", ".zdebug_weaknames" },
348 /* NB/ Numbers in this enum must match up with indices
349 into the dwarf_debug_sections[] array above. */
350 enum dwarf_debug_section_enum
379 /* A static assertion. */
380 extern int dwarf_debug_section_assert[ARRAY_SIZE (dwarf_debug_sections)
381 == debug_max + 1 ? 1 : -1];
383 #ifndef ABBREV_HASH_SIZE
384 #define ABBREV_HASH_SIZE 121
386 #ifndef ATTR_ALLOC_CHUNK
387 #define ATTR_ALLOC_CHUNK 4
390 /* Variable and function hash tables. This is used to speed up look-up
391 in lookup_symbol_in_var_table() and lookup_symbol_in_function_table().
392 In order to share code between variable and function infos, we use
393 a list of untyped pointer for all variable/function info associated with
394 a symbol. We waste a bit of memory for list with one node but that
395 simplifies the code. */
397 struct info_list_node
399 struct info_list_node *next;
403 /* Info hash entry. */
404 struct info_hash_entry
406 struct bfd_hash_entry root;
407 struct info_list_node *head;
410 struct info_hash_table
412 struct bfd_hash_table base;
415 /* Function to create a new entry in info hash table. */
417 static struct bfd_hash_entry *
418 info_hash_table_newfunc (struct bfd_hash_entry *entry,
419 struct bfd_hash_table *table,
422 struct info_hash_entry *ret = (struct info_hash_entry *) entry;
424 /* Allocate the structure if it has not already been allocated by a
428 ret = (struct info_hash_entry *) bfd_hash_allocate (table,
434 /* Call the allocation method of the base class. */
435 ret = ((struct info_hash_entry *)
436 bfd_hash_newfunc ((struct bfd_hash_entry *) ret, table, string));
438 /* Initialize the local fields here. */
442 return (struct bfd_hash_entry *) ret;
445 /* Function to create a new info hash table. It returns a pointer to the
446 newly created table or NULL if there is any error. We need abfd
447 solely for memory allocation. */
449 static struct info_hash_table *
450 create_info_hash_table (bfd *abfd)
452 struct info_hash_table *hash_table;
454 hash_table = ((struct info_hash_table *)
455 bfd_alloc (abfd, sizeof (struct info_hash_table)));
459 if (!bfd_hash_table_init (&hash_table->base, info_hash_table_newfunc,
460 sizeof (struct info_hash_entry)))
462 bfd_release (abfd, hash_table);
469 /* Insert an info entry into an info hash table. We do not check of
470 duplicate entries. Also, the caller need to guarantee that the
471 right type of info in inserted as info is passed as a void* pointer.
472 This function returns true if there is no error. */
475 insert_info_hash_table (struct info_hash_table *hash_table,
480 struct info_hash_entry *entry;
481 struct info_list_node *node;
483 entry = (struct info_hash_entry*) bfd_hash_lookup (&hash_table->base,
488 node = (struct info_list_node *) bfd_hash_allocate (&hash_table->base,
494 node->next = entry->head;
500 /* Look up an info entry list from an info hash table. Return NULL
503 static struct info_list_node *
504 lookup_info_hash_table (struct info_hash_table *hash_table, const char *key)
506 struct info_hash_entry *entry;
508 entry = (struct info_hash_entry*) bfd_hash_lookup (&hash_table->base, key,
510 return entry ? entry->head : NULL;
513 /* Read a section into its appropriate place in the dwarf2_debug
514 struct (indicated by SECTION_BUFFER and SECTION_SIZE). If SYMS is
515 not NULL, use bfd_simple_get_relocated_section_contents to read the
516 section contents, otherwise use bfd_get_section_contents. Fail if
517 the located section does not contain at least OFFSET bytes. */
520 read_section (bfd * abfd,
521 const struct dwarf_debug_section *sec,
524 bfd_byte ** section_buffer,
525 bfd_size_type * section_size)
528 const char *section_name = sec->uncompressed_name;
529 bfd_byte *contents = *section_buffer;
532 /* The section may have already been read. */
533 if (contents == NULL)
535 msec = bfd_get_section_by_name (abfd, section_name);
538 section_name = sec->compressed_name;
539 if (section_name != NULL)
540 msec = bfd_get_section_by_name (abfd, section_name);
544 _bfd_error_handler (_("DWARF error: can't find %s section."),
545 sec->uncompressed_name);
546 bfd_set_error (bfd_error_bad_value);
550 *section_size = msec->rawsize ? msec->rawsize : msec->size;
551 /* Paranoia - alloc one extra so that we can make sure a string
552 section is NUL terminated. */
553 amt = *section_size + 1;
556 bfd_set_error (bfd_error_no_memory);
559 contents = (bfd_byte *) bfd_malloc (amt);
560 if (contents == NULL)
563 ? !bfd_simple_get_relocated_section_contents (abfd, msec, contents,
565 : !bfd_get_section_contents (abfd, msec, contents, 0, *section_size))
570 contents[*section_size] = 0;
571 *section_buffer = contents;
574 /* It is possible to get a bad value for the offset into the section
575 that the client wants. Validate it here to avoid trouble later. */
576 if (offset != 0 && offset >= *section_size)
578 /* xgettext: c-format */
579 _bfd_error_handler (_("DWARF error: offset (%" PRIu64 ")"
580 " greater than or equal to %s size (%" PRIu64 ")"),
581 (uint64_t) offset, section_name,
582 (uint64_t) *section_size);
583 bfd_set_error (bfd_error_bad_value);
590 /* Read dwarf information from a buffer. */
593 read_1_byte (bfd *abfd ATTRIBUTE_UNUSED, bfd_byte *buf, bfd_byte *end)
597 return bfd_get_8 (abfd, buf);
601 read_1_signed_byte (bfd *abfd ATTRIBUTE_UNUSED, bfd_byte *buf, bfd_byte *end)
605 return bfd_get_signed_8 (abfd, buf);
609 read_2_bytes (bfd *abfd, bfd_byte *buf, bfd_byte *end)
613 return bfd_get_16 (abfd, buf);
617 read_4_bytes (bfd *abfd, bfd_byte *buf, bfd_byte *end)
621 return bfd_get_32 (abfd, buf);
625 read_8_bytes (bfd *abfd, bfd_byte *buf, bfd_byte *end)
629 return bfd_get_64 (abfd, buf);
633 read_n_bytes (bfd_byte * buf,
635 struct dwarf_block * block)
637 unsigned int size = block->size;
638 bfd_byte * block_end = buf + size;
640 if (block_end > end || block_end < buf)
653 /* Scans a NUL terminated string starting at BUF, returning a pointer to it.
654 Returns the number of characters in the string, *including* the NUL byte,
655 in BYTES_READ_PTR. This value is set even if the function fails. Bytes
656 at or beyond BUF_END will not be read. Returns NULL if there was a
657 problem, or if the string is empty. */
660 read_string (bfd * abfd ATTRIBUTE_UNUSED,
663 unsigned int * bytes_read_ptr)
669 * bytes_read_ptr = 0;
675 * bytes_read_ptr = 1;
679 while (buf < buf_end)
682 * bytes_read_ptr = buf - str;
686 * bytes_read_ptr = buf - str;
690 /* Reads an offset from BUF and then locates the string at this offset
691 inside the debug string section. Returns a pointer to the string.
692 Returns the number of bytes read from BUF, *not* the length of the string,
693 in BYTES_READ_PTR. This value is set even if the function fails. Bytes
694 at or beyond BUF_END will not be read from BUF. Returns NULL if there was
695 a problem, or if the string is empty. Does not check for NUL termination
699 read_indirect_string (struct comp_unit * unit,
702 unsigned int * bytes_read_ptr)
705 struct dwarf2_debug *stash = unit->stash;
708 if (buf + unit->offset_size > buf_end)
710 * bytes_read_ptr = 0;
714 if (unit->offset_size == 4)
715 offset = read_4_bytes (unit->abfd, buf, buf_end);
717 offset = read_8_bytes (unit->abfd, buf, buf_end);
719 *bytes_read_ptr = unit->offset_size;
721 if (! read_section (unit->abfd, &stash->debug_sections[debug_str],
723 &stash->dwarf_str_buffer, &stash->dwarf_str_size))
726 if (offset >= stash->dwarf_str_size)
728 str = (char *) stash->dwarf_str_buffer + offset;
734 /* Like read_indirect_string but from .debug_line_str section. */
737 read_indirect_line_string (struct comp_unit * unit,
740 unsigned int * bytes_read_ptr)
743 struct dwarf2_debug *stash = unit->stash;
746 if (buf + unit->offset_size > buf_end)
748 * bytes_read_ptr = 0;
752 if (unit->offset_size == 4)
753 offset = read_4_bytes (unit->abfd, buf, buf_end);
755 offset = read_8_bytes (unit->abfd, buf, buf_end);
757 *bytes_read_ptr = unit->offset_size;
759 if (! read_section (unit->abfd, &stash->debug_sections[debug_line_str],
761 &stash->dwarf_line_str_buffer,
762 &stash->dwarf_line_str_size))
765 if (offset >= stash->dwarf_line_str_size)
767 str = (char *) stash->dwarf_line_str_buffer + offset;
773 /* Like read_indirect_string but uses a .debug_str located in
774 an alternate file pointed to by the .gnu_debugaltlink section.
775 Used to impement DW_FORM_GNU_strp_alt. */
778 read_alt_indirect_string (struct comp_unit * unit,
781 unsigned int * bytes_read_ptr)
784 struct dwarf2_debug *stash = unit->stash;
787 if (buf + unit->offset_size > buf_end)
789 * bytes_read_ptr = 0;
793 if (unit->offset_size == 4)
794 offset = read_4_bytes (unit->abfd, buf, buf_end);
796 offset = read_8_bytes (unit->abfd, buf, buf_end);
798 *bytes_read_ptr = unit->offset_size;
800 if (stash->alt_bfd_ptr == NULL)
803 char * debug_filename = bfd_follow_gnu_debugaltlink (unit->abfd, DEBUGDIR);
805 if (debug_filename == NULL)
808 if ((debug_bfd = bfd_openr (debug_filename, NULL)) == NULL
809 || ! bfd_check_format (debug_bfd, bfd_object))
812 bfd_close (debug_bfd);
814 /* FIXME: Should we report our failure to follow the debuglink ? */
815 free (debug_filename);
818 stash->alt_bfd_ptr = debug_bfd;
821 if (! read_section (unit->stash->alt_bfd_ptr,
822 stash->debug_sections + debug_str_alt,
823 NULL, /* FIXME: Do we need to load alternate symbols ? */
825 &stash->alt_dwarf_str_buffer,
826 &stash->alt_dwarf_str_size))
829 if (offset >= stash->alt_dwarf_str_size)
831 str = (char *) stash->alt_dwarf_str_buffer + offset;
838 /* Resolve an alternate reference from UNIT at OFFSET.
839 Returns a pointer into the loaded alternate CU upon success
840 or NULL upon failure. */
843 read_alt_indirect_ref (struct comp_unit * unit,
846 struct dwarf2_debug *stash = unit->stash;
848 if (stash->alt_bfd_ptr == NULL)
851 char * debug_filename = bfd_follow_gnu_debugaltlink (unit->abfd, DEBUGDIR);
853 if (debug_filename == NULL)
856 if ((debug_bfd = bfd_openr (debug_filename, NULL)) == NULL
857 || ! bfd_check_format (debug_bfd, bfd_object))
860 bfd_close (debug_bfd);
862 /* FIXME: Should we report our failure to follow the debuglink ? */
863 free (debug_filename);
866 stash->alt_bfd_ptr = debug_bfd;
869 if (! read_section (unit->stash->alt_bfd_ptr,
870 stash->debug_sections + debug_info_alt,
871 NULL, /* FIXME: Do we need to load alternate symbols ? */
873 &stash->alt_dwarf_info_buffer,
874 &stash->alt_dwarf_info_size))
877 if (offset >= stash->alt_dwarf_info_size)
879 return stash->alt_dwarf_info_buffer + offset;
883 read_address (struct comp_unit *unit, bfd_byte *buf, bfd_byte * buf_end)
887 if (bfd_get_flavour (unit->abfd) == bfd_target_elf_flavour)
888 signed_vma = get_elf_backend_data (unit->abfd)->sign_extend_vma;
890 if (buf + unit->addr_size > buf_end)
895 switch (unit->addr_size)
898 return bfd_get_signed_64 (unit->abfd, buf);
900 return bfd_get_signed_32 (unit->abfd, buf);
902 return bfd_get_signed_16 (unit->abfd, buf);
909 switch (unit->addr_size)
912 return bfd_get_64 (unit->abfd, buf);
914 return bfd_get_32 (unit->abfd, buf);
916 return bfd_get_16 (unit->abfd, buf);
923 /* Lookup an abbrev_info structure in the abbrev hash table. */
925 static struct abbrev_info *
926 lookup_abbrev (unsigned int number, struct abbrev_info **abbrevs)
928 unsigned int hash_number;
929 struct abbrev_info *abbrev;
931 hash_number = number % ABBREV_HASH_SIZE;
932 abbrev = abbrevs[hash_number];
936 if (abbrev->number == number)
939 abbrev = abbrev->next;
945 /* In DWARF version 2, the description of the debugging information is
946 stored in a separate .debug_abbrev section. Before we read any
947 dies from a section we read in all abbreviations and install them
950 static struct abbrev_info**
951 read_abbrevs (bfd *abfd, bfd_uint64_t offset, struct dwarf2_debug *stash)
953 struct abbrev_info **abbrevs;
954 bfd_byte *abbrev_ptr;
955 bfd_byte *abbrev_end;
956 struct abbrev_info *cur_abbrev;
957 unsigned int abbrev_number, bytes_read, abbrev_name;
958 unsigned int abbrev_form, hash_number;
961 if (! read_section (abfd, &stash->debug_sections[debug_abbrev],
963 &stash->dwarf_abbrev_buffer, &stash->dwarf_abbrev_size))
966 if (offset >= stash->dwarf_abbrev_size)
969 amt = sizeof (struct abbrev_info*) * ABBREV_HASH_SIZE;
970 abbrevs = (struct abbrev_info **) bfd_zalloc (abfd, amt);
974 abbrev_ptr = stash->dwarf_abbrev_buffer + offset;
975 abbrev_end = stash->dwarf_abbrev_buffer + stash->dwarf_abbrev_size;
976 abbrev_number = _bfd_safe_read_leb128 (abfd, abbrev_ptr, &bytes_read,
978 abbrev_ptr += bytes_read;
980 /* Loop until we reach an abbrev number of 0. */
981 while (abbrev_number)
983 amt = sizeof (struct abbrev_info);
984 cur_abbrev = (struct abbrev_info *) bfd_zalloc (abfd, amt);
985 if (cur_abbrev == NULL)
988 /* Read in abbrev header. */
989 cur_abbrev->number = abbrev_number;
990 cur_abbrev->tag = (enum dwarf_tag)
991 _bfd_safe_read_leb128 (abfd, abbrev_ptr, &bytes_read,
993 abbrev_ptr += bytes_read;
994 cur_abbrev->has_children = read_1_byte (abfd, abbrev_ptr, abbrev_end);
997 /* Now read in declarations. */
1000 /* Initialize it just to avoid a GCC false warning. */
1001 bfd_vma implicit_const = -1;
1003 abbrev_name = _bfd_safe_read_leb128 (abfd, abbrev_ptr, &bytes_read,
1005 abbrev_ptr += bytes_read;
1006 abbrev_form = _bfd_safe_read_leb128 (abfd, abbrev_ptr, &bytes_read,
1008 abbrev_ptr += bytes_read;
1009 if (abbrev_form == DW_FORM_implicit_const)
1011 implicit_const = _bfd_safe_read_leb128 (abfd, abbrev_ptr,
1014 abbrev_ptr += bytes_read;
1017 if (abbrev_name == 0)
1020 if ((cur_abbrev->num_attrs % ATTR_ALLOC_CHUNK) == 0)
1022 struct attr_abbrev *tmp;
1024 amt = cur_abbrev->num_attrs + ATTR_ALLOC_CHUNK;
1025 amt *= sizeof (struct attr_abbrev);
1026 tmp = (struct attr_abbrev *) bfd_realloc (cur_abbrev->attrs, amt);
1031 for (i = 0; i < ABBREV_HASH_SIZE; i++)
1033 struct abbrev_info *abbrev = abbrevs[i];
1037 free (abbrev->attrs);
1038 abbrev = abbrev->next;
1043 cur_abbrev->attrs = tmp;
1046 cur_abbrev->attrs[cur_abbrev->num_attrs].name
1047 = (enum dwarf_attribute) abbrev_name;
1048 cur_abbrev->attrs[cur_abbrev->num_attrs].form
1049 = (enum dwarf_form) abbrev_form;
1050 cur_abbrev->attrs[cur_abbrev->num_attrs].implicit_const
1052 ++cur_abbrev->num_attrs;
1055 hash_number = abbrev_number % ABBREV_HASH_SIZE;
1056 cur_abbrev->next = abbrevs[hash_number];
1057 abbrevs[hash_number] = cur_abbrev;
1059 /* Get next abbreviation.
1060 Under Irix6 the abbreviations for a compilation unit are not
1061 always properly terminated with an abbrev number of 0.
1062 Exit loop if we encounter an abbreviation which we have
1063 already read (which means we are about to read the abbreviations
1064 for the next compile unit) or if the end of the abbreviation
1065 table is reached. */
1066 if ((unsigned int) (abbrev_ptr - stash->dwarf_abbrev_buffer)
1067 >= stash->dwarf_abbrev_size)
1069 abbrev_number = _bfd_safe_read_leb128 (abfd, abbrev_ptr,
1070 &bytes_read, FALSE, abbrev_end);
1071 abbrev_ptr += bytes_read;
1072 if (lookup_abbrev (abbrev_number, abbrevs) != NULL)
1079 /* Returns true if the form is one which has a string value. */
1081 static inline bfd_boolean
1082 is_str_attr (enum dwarf_form form)
1084 return (form == DW_FORM_string || form == DW_FORM_strp
1085 || form == DW_FORM_line_strp || form == DW_FORM_GNU_strp_alt);
1088 /* Read and fill in the value of attribute ATTR as described by FORM.
1089 Read data starting from INFO_PTR, but never at or beyond INFO_PTR_END.
1090 Returns an updated INFO_PTR taking into account the amount of data read. */
1093 read_attribute_value (struct attribute * attr,
1095 bfd_vma implicit_const,
1096 struct comp_unit * unit,
1097 bfd_byte * info_ptr,
1098 bfd_byte * info_ptr_end)
1100 bfd *abfd = unit->abfd;
1101 unsigned int bytes_read;
1102 struct dwarf_block *blk;
1105 if (info_ptr >= info_ptr_end && form != DW_FORM_flag_present)
1107 _bfd_error_handler (_("DWARF error: info pointer extends beyond end of attributes"));
1108 bfd_set_error (bfd_error_bad_value);
1112 attr->form = (enum dwarf_form) form;
1116 case DW_FORM_ref_addr:
1117 /* DW_FORM_ref_addr is an address in DWARF2, and an offset in
1119 if (unit->version == 3 || unit->version == 4)
1121 if (unit->offset_size == 4)
1122 attr->u.val = read_4_bytes (unit->abfd, info_ptr, info_ptr_end);
1124 attr->u.val = read_8_bytes (unit->abfd, info_ptr, info_ptr_end);
1125 info_ptr += unit->offset_size;
1130 attr->u.val = read_address (unit, info_ptr, info_ptr_end);
1131 info_ptr += unit->addr_size;
1133 case DW_FORM_GNU_ref_alt:
1134 case DW_FORM_sec_offset:
1135 if (unit->offset_size == 4)
1136 attr->u.val = read_4_bytes (unit->abfd, info_ptr, info_ptr_end);
1138 attr->u.val = read_8_bytes (unit->abfd, info_ptr, info_ptr_end);
1139 info_ptr += unit->offset_size;
1141 case DW_FORM_block2:
1142 amt = sizeof (struct dwarf_block);
1143 blk = (struct dwarf_block *) bfd_alloc (abfd, amt);
1146 blk->size = read_2_bytes (abfd, info_ptr, info_ptr_end);
1148 info_ptr = read_n_bytes (info_ptr, info_ptr_end, blk);
1151 case DW_FORM_block4:
1152 amt = sizeof (struct dwarf_block);
1153 blk = (struct dwarf_block *) bfd_alloc (abfd, amt);
1156 blk->size = read_4_bytes (abfd, info_ptr, info_ptr_end);
1158 info_ptr = read_n_bytes (info_ptr, info_ptr_end, blk);
1162 attr->u.val = read_2_bytes (abfd, info_ptr, info_ptr_end);
1166 attr->u.val = read_4_bytes (abfd, info_ptr, info_ptr_end);
1170 attr->u.val = read_8_bytes (abfd, info_ptr, info_ptr_end);
1173 case DW_FORM_string:
1174 attr->u.str = read_string (abfd, info_ptr, info_ptr_end, &bytes_read);
1175 info_ptr += bytes_read;
1178 attr->u.str = read_indirect_string (unit, info_ptr, info_ptr_end, &bytes_read);
1179 info_ptr += bytes_read;
1181 case DW_FORM_line_strp:
1182 attr->u.str = read_indirect_line_string (unit, info_ptr, info_ptr_end, &bytes_read);
1183 info_ptr += bytes_read;
1185 case DW_FORM_GNU_strp_alt:
1186 attr->u.str = read_alt_indirect_string (unit, info_ptr, info_ptr_end, &bytes_read);
1187 info_ptr += bytes_read;
1189 case DW_FORM_exprloc:
1191 amt = sizeof (struct dwarf_block);
1192 blk = (struct dwarf_block *) bfd_alloc (abfd, amt);
1195 blk->size = _bfd_safe_read_leb128 (abfd, info_ptr, &bytes_read,
1196 FALSE, info_ptr_end);
1197 info_ptr += bytes_read;
1198 info_ptr = read_n_bytes (info_ptr, info_ptr_end, blk);
1201 case DW_FORM_block1:
1202 amt = sizeof (struct dwarf_block);
1203 blk = (struct dwarf_block *) bfd_alloc (abfd, amt);
1206 blk->size = read_1_byte (abfd, info_ptr, info_ptr_end);
1208 info_ptr = read_n_bytes (info_ptr, info_ptr_end, blk);
1212 attr->u.val = read_1_byte (abfd, info_ptr, info_ptr_end);
1216 attr->u.val = read_1_byte (abfd, info_ptr, info_ptr_end);
1219 case DW_FORM_flag_present:
1223 attr->u.sval = _bfd_safe_read_leb128 (abfd, info_ptr, &bytes_read,
1224 TRUE, info_ptr_end);
1225 info_ptr += bytes_read;
1228 attr->u.val = _bfd_safe_read_leb128 (abfd, info_ptr, &bytes_read,
1229 FALSE, info_ptr_end);
1230 info_ptr += bytes_read;
1233 attr->u.val = read_1_byte (abfd, info_ptr, info_ptr_end);
1237 attr->u.val = read_2_bytes (abfd, info_ptr, info_ptr_end);
1241 attr->u.val = read_4_bytes (abfd, info_ptr, info_ptr_end);
1245 attr->u.val = read_8_bytes (abfd, info_ptr, info_ptr_end);
1248 case DW_FORM_ref_sig8:
1249 attr->u.val = read_8_bytes (abfd, info_ptr, info_ptr_end);
1252 case DW_FORM_ref_udata:
1253 attr->u.val = _bfd_safe_read_leb128 (abfd, info_ptr, &bytes_read,
1254 FALSE, info_ptr_end);
1255 info_ptr += bytes_read;
1257 case DW_FORM_indirect:
1258 form = _bfd_safe_read_leb128 (abfd, info_ptr, &bytes_read,
1259 FALSE, info_ptr_end);
1260 info_ptr += bytes_read;
1261 if (form == DW_FORM_implicit_const)
1263 implicit_const = _bfd_safe_read_leb128 (abfd, info_ptr, &bytes_read,
1264 TRUE, info_ptr_end);
1265 info_ptr += bytes_read;
1267 info_ptr = read_attribute_value (attr, form, implicit_const, unit,
1268 info_ptr, info_ptr_end);
1270 case DW_FORM_implicit_const:
1271 attr->form = DW_FORM_sdata;
1272 attr->u.sval = implicit_const;
1275 _bfd_error_handler (_("DWARF error: invalid or unhandled FORM value: %#x"),
1277 bfd_set_error (bfd_error_bad_value);
1283 /* Read an attribute described by an abbreviated attribute. */
1286 read_attribute (struct attribute * attr,
1287 struct attr_abbrev * abbrev,
1288 struct comp_unit * unit,
1289 bfd_byte * info_ptr,
1290 bfd_byte * info_ptr_end)
1292 attr->name = abbrev->name;
1293 info_ptr = read_attribute_value (attr, abbrev->form, abbrev->implicit_const,
1294 unit, info_ptr, info_ptr_end);
1298 /* Return whether DW_AT_name will return the same as DW_AT_linkage_name
1302 non_mangled (int lang)
1312 case DW_LANG_Cobol74:
1313 case DW_LANG_Cobol85:
1314 case DW_LANG_Fortran77:
1315 case DW_LANG_Pascal83:
1325 /* Source line information table routines. */
1327 #define FILE_ALLOC_CHUNK 5
1328 #define DIR_ALLOC_CHUNK 5
1332 struct line_info * prev_line;
1336 unsigned int column;
1337 unsigned int discriminator;
1338 unsigned char op_index;
1339 unsigned char end_sequence; /* End of (sequential) code sequence. */
1350 struct line_sequence
1353 struct line_sequence* prev_sequence;
1354 struct line_info* last_line; /* Largest VMA. */
1355 struct line_info** line_info_lookup;
1356 bfd_size_type num_lines;
1359 struct line_info_table
1362 unsigned int num_files;
1363 unsigned int num_dirs;
1364 unsigned int num_sequences;
1367 struct fileinfo* files;
1368 struct line_sequence* sequences;
1369 struct line_info* lcl_head; /* Local head; used in 'add_line_info'. */
1372 /* Remember some information about each function. If the function is
1373 inlined (DW_TAG_inlined_subroutine) it may have two additional
1374 attributes, DW_AT_call_file and DW_AT_call_line, which specify the
1375 source code location where this function was inlined. */
1379 /* Pointer to previous function in list of all functions. */
1380 struct funcinfo * prev_func;
1381 /* Pointer to function one scope higher. */
1382 struct funcinfo * caller_func;
1383 /* Source location file name where caller_func inlines this func. */
1385 /* Source location file name. */
1387 /* Source location line number where caller_func inlines this func. */
1389 /* Source location line number. */
1392 bfd_boolean is_linkage;
1394 struct arange arange;
1395 /* Where the symbol is defined. */
1399 struct lookup_funcinfo
1401 /* Function information corresponding to this lookup table entry. */
1402 struct funcinfo * funcinfo;
1404 /* The lowest address for this specific function. */
1407 /* The highest address of this function before the lookup table is sorted.
1408 The highest address of all prior functions after the lookup table is
1409 sorted, which is used for binary search. */
1415 /* Pointer to previous variable in list of all variables */
1416 struct varinfo *prev_var;
1417 /* Source location file name */
1419 /* Source location line number */
1424 /* Where the symbol is defined */
1426 /* Is this a stack variable? */
1427 unsigned int stack: 1;
1430 /* Return TRUE if NEW_LINE should sort after LINE. */
1432 static inline bfd_boolean
1433 new_line_sorts_after (struct line_info *new_line, struct line_info *line)
1435 return (new_line->address > line->address
1436 || (new_line->address == line->address
1437 && new_line->op_index > line->op_index));
1441 /* Adds a new entry to the line_info list in the line_info_table, ensuring
1442 that the list is sorted. Note that the line_info list is sorted from
1443 highest to lowest VMA (with possible duplicates); that is,
1444 line_info->prev_line always accesses an equal or smaller VMA. */
1447 add_line_info (struct line_info_table *table,
1449 unsigned char op_index,
1452 unsigned int column,
1453 unsigned int discriminator,
1456 bfd_size_type amt = sizeof (struct line_info);
1457 struct line_sequence* seq = table->sequences;
1458 struct line_info* info = (struct line_info *) bfd_alloc (table->abfd, amt);
1463 /* Set member data of 'info'. */
1464 info->prev_line = NULL;
1465 info->address = address;
1466 info->op_index = op_index;
1468 info->column = column;
1469 info->discriminator = discriminator;
1470 info->end_sequence = end_sequence;
1472 if (filename && filename[0])
1474 info->filename = (char *) bfd_alloc (table->abfd, strlen (filename) + 1);
1475 if (info->filename == NULL)
1477 strcpy (info->filename, filename);
1480 info->filename = NULL;
1482 /* Find the correct location for 'info'. Normally we will receive
1483 new line_info data 1) in order and 2) with increasing VMAs.
1484 However some compilers break the rules (cf. decode_line_info) and
1485 so we include some heuristics for quickly finding the correct
1486 location for 'info'. In particular, these heuristics optimize for
1487 the common case in which the VMA sequence that we receive is a
1488 list of locally sorted VMAs such as
1489 p...z a...j (where a < j < p < z)
1491 Note: table->lcl_head is used to head an *actual* or *possible*
1492 sub-sequence within the list (such as a...j) that is not directly
1493 headed by table->last_line
1495 Note: we may receive duplicate entries from 'decode_line_info'. */
1498 && seq->last_line->address == address
1499 && seq->last_line->op_index == op_index
1500 && seq->last_line->end_sequence == end_sequence)
1502 /* We only keep the last entry with the same address and end
1503 sequence. See PR ld/4986. */
1504 if (table->lcl_head == seq->last_line)
1505 table->lcl_head = info;
1506 info->prev_line = seq->last_line->prev_line;
1507 seq->last_line = info;
1509 else if (!seq || seq->last_line->end_sequence)
1511 /* Start a new line sequence. */
1512 amt = sizeof (struct line_sequence);
1513 seq = (struct line_sequence *) bfd_malloc (amt);
1516 seq->low_pc = address;
1517 seq->prev_sequence = table->sequences;
1518 seq->last_line = info;
1519 table->lcl_head = info;
1520 table->sequences = seq;
1521 table->num_sequences++;
1523 else if (info->end_sequence
1524 || new_line_sorts_after (info, seq->last_line))
1526 /* Normal case: add 'info' to the beginning of the current sequence. */
1527 info->prev_line = seq->last_line;
1528 seq->last_line = info;
1530 /* lcl_head: initialize to head a *possible* sequence at the end. */
1531 if (!table->lcl_head)
1532 table->lcl_head = info;
1534 else if (!new_line_sorts_after (info, table->lcl_head)
1535 && (!table->lcl_head->prev_line
1536 || new_line_sorts_after (info, table->lcl_head->prev_line)))
1538 /* Abnormal but easy: lcl_head is the head of 'info'. */
1539 info->prev_line = table->lcl_head->prev_line;
1540 table->lcl_head->prev_line = info;
1544 /* Abnormal and hard: Neither 'last_line' nor 'lcl_head'
1545 are valid heads for 'info'. Reset 'lcl_head'. */
1546 struct line_info* li2 = seq->last_line; /* Always non-NULL. */
1547 struct line_info* li1 = li2->prev_line;
1551 if (!new_line_sorts_after (info, li2)
1552 && new_line_sorts_after (info, li1))
1555 li2 = li1; /* always non-NULL */
1556 li1 = li1->prev_line;
1558 table->lcl_head = li2;
1559 info->prev_line = table->lcl_head->prev_line;
1560 table->lcl_head->prev_line = info;
1561 if (address < seq->low_pc)
1562 seq->low_pc = address;
1567 /* Extract a fully qualified filename from a line info table.
1568 The returned string has been malloc'ed and it is the caller's
1569 responsibility to free it. */
1572 concat_filename (struct line_info_table *table, unsigned int file)
1576 if (table == NULL || file - 1 >= table->num_files)
1578 /* FILE == 0 means unknown. */
1581 (_("DWARF error: mangled line number section (bad file number)"));
1582 return strdup ("<unknown>");
1585 filename = table->files[file - 1].name;
1586 if (filename == NULL)
1587 return strdup ("<unknown>");
1589 if (!IS_ABSOLUTE_PATH (filename))
1591 char *dir_name = NULL;
1592 char *subdir_name = NULL;
1596 if (table->files[file - 1].dir
1597 /* PR 17512: file: 0317e960. */
1598 && table->files[file - 1].dir <= table->num_dirs
1599 /* PR 17512: file: 7f3d2e4b. */
1600 && table->dirs != NULL)
1601 subdir_name = table->dirs[table->files[file - 1].dir - 1];
1603 if (!subdir_name || !IS_ABSOLUTE_PATH (subdir_name))
1604 dir_name = table->comp_dir;
1608 dir_name = subdir_name;
1613 return strdup (filename);
1615 len = strlen (dir_name) + strlen (filename) + 2;
1619 len += strlen (subdir_name) + 1;
1620 name = (char *) bfd_malloc (len);
1622 sprintf (name, "%s/%s/%s", dir_name, subdir_name, filename);
1626 name = (char *) bfd_malloc (len);
1628 sprintf (name, "%s/%s", dir_name, filename);
1634 return strdup (filename);
1638 arange_add (const struct comp_unit *unit, struct arange *first_arange,
1639 bfd_vma low_pc, bfd_vma high_pc)
1641 struct arange *arange;
1643 /* Ignore empty ranges. */
1644 if (low_pc == high_pc)
1647 /* If the first arange is empty, use it. */
1648 if (first_arange->high == 0)
1650 first_arange->low = low_pc;
1651 first_arange->high = high_pc;
1655 /* Next see if we can cheaply extend an existing range. */
1656 arange = first_arange;
1659 if (low_pc == arange->high)
1661 arange->high = high_pc;
1664 if (high_pc == arange->low)
1666 arange->low = low_pc;
1669 arange = arange->next;
1673 /* Need to allocate a new arange and insert it into the arange list.
1674 Order isn't significant, so just insert after the first arange. */
1675 arange = (struct arange *) bfd_alloc (unit->abfd, sizeof (*arange));
1678 arange->low = low_pc;
1679 arange->high = high_pc;
1680 arange->next = first_arange->next;
1681 first_arange->next = arange;
1685 /* Compare function for line sequences. */
1688 compare_sequences (const void* a, const void* b)
1690 const struct line_sequence* seq1 = a;
1691 const struct line_sequence* seq2 = b;
1693 /* Sort by low_pc as the primary key. */
1694 if (seq1->low_pc < seq2->low_pc)
1696 if (seq1->low_pc > seq2->low_pc)
1699 /* If low_pc values are equal, sort in reverse order of
1700 high_pc, so that the largest region comes first. */
1701 if (seq1->last_line->address < seq2->last_line->address)
1703 if (seq1->last_line->address > seq2->last_line->address)
1706 if (seq1->last_line->op_index < seq2->last_line->op_index)
1708 if (seq1->last_line->op_index > seq2->last_line->op_index)
1714 /* Construct the line information table for quick lookup. */
1717 build_line_info_table (struct line_info_table * table,
1718 struct line_sequence * seq)
1721 struct line_info** line_info_lookup;
1722 struct line_info* each_line;
1723 unsigned int num_lines;
1724 unsigned int line_index;
1726 if (seq->line_info_lookup != NULL)
1729 /* Count the number of line information entries. We could do this while
1730 scanning the debug information, but some entries may be added via
1731 lcl_head without having a sequence handy to increment the number of
1734 for (each_line = seq->last_line; each_line; each_line = each_line->prev_line)
1740 /* Allocate space for the line information lookup table. */
1741 amt = sizeof (struct line_info*) * num_lines;
1742 line_info_lookup = (struct line_info**) bfd_alloc (table->abfd, amt);
1743 if (line_info_lookup == NULL)
1746 /* Create the line information lookup table. */
1747 line_index = num_lines;
1748 for (each_line = seq->last_line; each_line; each_line = each_line->prev_line)
1749 line_info_lookup[--line_index] = each_line;
1751 BFD_ASSERT (line_index == 0);
1753 seq->num_lines = num_lines;
1754 seq->line_info_lookup = line_info_lookup;
1759 /* Sort the line sequences for quick lookup. */
1762 sort_line_sequences (struct line_info_table* table)
1765 struct line_sequence* sequences;
1766 struct line_sequence* seq;
1768 unsigned int num_sequences = table->num_sequences;
1769 bfd_vma last_high_pc;
1771 if (num_sequences == 0)
1774 /* Allocate space for an array of sequences. */
1775 amt = sizeof (struct line_sequence) * num_sequences;
1776 sequences = (struct line_sequence *) bfd_alloc (table->abfd, amt);
1777 if (sequences == NULL)
1780 /* Copy the linked list into the array, freeing the original nodes. */
1781 seq = table->sequences;
1782 for (n = 0; n < num_sequences; n++)
1784 struct line_sequence* last_seq = seq;
1787 sequences[n].low_pc = seq->low_pc;
1788 sequences[n].prev_sequence = NULL;
1789 sequences[n].last_line = seq->last_line;
1790 sequences[n].line_info_lookup = NULL;
1791 sequences[n].num_lines = 0;
1792 seq = seq->prev_sequence;
1795 BFD_ASSERT (seq == NULL);
1797 qsort (sequences, n, sizeof (struct line_sequence), compare_sequences);
1799 /* Make the list binary-searchable by trimming overlapping entries
1800 and removing nested entries. */
1802 last_high_pc = sequences[0].last_line->address;
1803 for (n = 1; n < table->num_sequences; n++)
1805 if (sequences[n].low_pc < last_high_pc)
1807 if (sequences[n].last_line->address <= last_high_pc)
1808 /* Skip nested entries. */
1811 /* Trim overlapping entries. */
1812 sequences[n].low_pc = last_high_pc;
1814 last_high_pc = sequences[n].last_line->address;
1815 if (n > num_sequences)
1817 /* Close up the gap. */
1818 sequences[num_sequences].low_pc = sequences[n].low_pc;
1819 sequences[num_sequences].last_line = sequences[n].last_line;
1824 table->sequences = sequences;
1825 table->num_sequences = num_sequences;
1829 /* Add directory to TABLE. CUR_DIR memory ownership is taken by TABLE. */
1832 line_info_add_include_dir (struct line_info_table *table, char *cur_dir)
1834 if ((table->num_dirs % DIR_ALLOC_CHUNK) == 0)
1839 amt = table->num_dirs + DIR_ALLOC_CHUNK;
1840 amt *= sizeof (char *);
1842 tmp = (char **) bfd_realloc (table->dirs, amt);
1848 table->dirs[table->num_dirs++] = cur_dir;
1853 line_info_add_include_dir_stub (struct line_info_table *table, char *cur_dir,
1854 unsigned int dir ATTRIBUTE_UNUSED,
1855 unsigned int xtime ATTRIBUTE_UNUSED,
1856 unsigned int size ATTRIBUTE_UNUSED)
1858 return line_info_add_include_dir (table, cur_dir);
1861 /* Add file to TABLE. CUR_FILE memory ownership is taken by TABLE. */
1864 line_info_add_file_name (struct line_info_table *table, char *cur_file,
1865 unsigned int dir, unsigned int xtime,
1868 if ((table->num_files % FILE_ALLOC_CHUNK) == 0)
1870 struct fileinfo *tmp;
1873 amt = table->num_files + FILE_ALLOC_CHUNK;
1874 amt *= sizeof (struct fileinfo);
1876 tmp = (struct fileinfo *) bfd_realloc (table->files, amt);
1882 table->files[table->num_files].name = cur_file;
1883 table->files[table->num_files].dir = dir;
1884 table->files[table->num_files].time = xtime;
1885 table->files[table->num_files].size = size;
1890 /* Read directory or file name entry format, starting with byte of
1891 format count entries, ULEB128 pairs of entry formats, ULEB128 of
1892 entries count and the entries themselves in the described entry
1896 read_formatted_entries (struct comp_unit *unit, bfd_byte **bufp,
1897 bfd_byte *buf_end, struct line_info_table *table,
1898 bfd_boolean (*callback) (struct line_info_table *table,
1904 bfd *abfd = unit->abfd;
1905 bfd_byte format_count, formati;
1906 bfd_vma data_count, datai;
1907 bfd_byte *buf = *bufp;
1908 bfd_byte *format_header_data;
1909 unsigned int bytes_read;
1911 format_count = read_1_byte (abfd, buf, buf_end);
1913 format_header_data = buf;
1914 for (formati = 0; formati < format_count; formati++)
1916 _bfd_safe_read_leb128 (abfd, buf, &bytes_read, FALSE, buf_end);
1918 _bfd_safe_read_leb128 (abfd, buf, &bytes_read, FALSE, buf_end);
1922 data_count = _bfd_safe_read_leb128 (abfd, buf, &bytes_read, FALSE, buf_end);
1924 if (format_count == 0 && data_count != 0)
1926 _bfd_error_handler (_("DWARF error: zero format count"));
1927 bfd_set_error (bfd_error_bad_value);
1931 /* PR 22210. Paranoia check. Don't bother running the loop
1932 if we know that we are going to run out of buffer. */
1933 if (data_count > (bfd_vma) (buf_end - buf))
1936 (_("DWARF error: data count (%" PRIx64 ") larger than buffer size"),
1937 (uint64_t) data_count);
1938 bfd_set_error (bfd_error_bad_value);
1942 for (datai = 0; datai < data_count; datai++)
1944 bfd_byte *format = format_header_data;
1947 memset (&fe, 0, sizeof fe);
1948 for (formati = 0; formati < format_count; formati++)
1950 bfd_vma content_type, form;
1952 char **stringp = &string_trash;
1953 unsigned int uint_trash, *uintp = &uint_trash;
1954 struct attribute attr;
1956 content_type = _bfd_safe_read_leb128 (abfd, format, &bytes_read,
1958 format += bytes_read;
1959 switch (content_type)
1964 case DW_LNCT_directory_index:
1967 case DW_LNCT_timestamp:
1977 (_("DWARF error: unknown format content type %" PRIu64),
1978 (uint64_t) content_type);
1979 bfd_set_error (bfd_error_bad_value);
1983 form = _bfd_safe_read_leb128 (abfd, format, &bytes_read, FALSE,
1985 format += bytes_read;
1987 buf = read_attribute_value (&attr, form, 0, unit, buf, buf_end);
1992 case DW_FORM_string:
1993 case DW_FORM_line_strp:
1994 *stringp = attr.u.str;
2002 *uintp = attr.u.val;
2007 if (!callback (table, fe.name, fe.dir, fe.time, fe.size))
2015 /* Decode the line number information for UNIT. */
2017 static struct line_info_table*
2018 decode_line_info (struct comp_unit *unit, struct dwarf2_debug *stash)
2020 bfd *abfd = unit->abfd;
2021 struct line_info_table* table;
2024 struct line_head lh;
2025 unsigned int i, bytes_read, offset_size;
2026 char *cur_file, *cur_dir;
2027 unsigned char op_code, extended_op, adj_opcode;
2028 unsigned int exop_len;
2031 if (! read_section (abfd, &stash->debug_sections[debug_line],
2032 stash->syms, unit->line_offset,
2033 &stash->dwarf_line_buffer, &stash->dwarf_line_size))
2036 amt = sizeof (struct line_info_table);
2037 table = (struct line_info_table *) bfd_alloc (abfd, amt);
2041 table->comp_dir = unit->comp_dir;
2043 table->num_files = 0;
2044 table->files = NULL;
2046 table->num_dirs = 0;
2049 table->num_sequences = 0;
2050 table->sequences = NULL;
2052 table->lcl_head = NULL;
2054 if (stash->dwarf_line_size < 16)
2057 (_("DWARF error: line info section is too small (%" PRId64 ")"),
2058 (int64_t) stash->dwarf_line_size);
2059 bfd_set_error (bfd_error_bad_value);
2062 line_ptr = stash->dwarf_line_buffer + unit->line_offset;
2063 line_end = stash->dwarf_line_buffer + stash->dwarf_line_size;
2065 /* Read in the prologue. */
2066 lh.total_length = read_4_bytes (abfd, line_ptr, line_end);
2069 if (lh.total_length == 0xffffffff)
2071 lh.total_length = read_8_bytes (abfd, line_ptr, line_end);
2075 else if (lh.total_length == 0 && unit->addr_size == 8)
2077 /* Handle (non-standard) 64-bit DWARF2 formats. */
2078 lh.total_length = read_4_bytes (abfd, line_ptr, line_end);
2083 if (lh.total_length > (size_t) (line_end - line_ptr))
2086 /* xgettext: c-format */
2087 (_("DWARF error: line info data is bigger (%#" PRIx64 ")"
2088 " than the space remaining in the section (%#lx)"),
2089 (uint64_t) lh.total_length, (unsigned long) (line_end - line_ptr));
2090 bfd_set_error (bfd_error_bad_value);
2094 line_end = line_ptr + lh.total_length;
2096 lh.version = read_2_bytes (abfd, line_ptr, line_end);
2097 if (lh.version < 2 || lh.version > 5)
2100 (_("DWARF error: unhandled .debug_line version %d"), lh.version);
2101 bfd_set_error (bfd_error_bad_value);
2106 if (line_ptr + offset_size + (lh.version >= 5 ? 8 : (lh.version >= 4 ? 6 : 5))
2110 (_("DWARF error: ran out of room reading prologue"));
2111 bfd_set_error (bfd_error_bad_value);
2115 if (lh.version >= 5)
2117 unsigned int segment_selector_size;
2119 /* Skip address size. */
2120 read_1_byte (abfd, line_ptr, line_end);
2123 segment_selector_size = read_1_byte (abfd, line_ptr, line_end);
2125 if (segment_selector_size != 0)
2128 (_("DWARF error: line info unsupported segment selector size %u"),
2129 segment_selector_size);
2130 bfd_set_error (bfd_error_bad_value);
2135 if (offset_size == 4)
2136 lh.prologue_length = read_4_bytes (abfd, line_ptr, line_end);
2138 lh.prologue_length = read_8_bytes (abfd, line_ptr, line_end);
2139 line_ptr += offset_size;
2141 lh.minimum_instruction_length = read_1_byte (abfd, line_ptr, line_end);
2144 if (lh.version >= 4)
2146 lh.maximum_ops_per_insn = read_1_byte (abfd, line_ptr, line_end);
2150 lh.maximum_ops_per_insn = 1;
2152 if (lh.maximum_ops_per_insn == 0)
2155 (_("DWARF error: invalid maximum operations per instruction"));
2156 bfd_set_error (bfd_error_bad_value);
2160 lh.default_is_stmt = read_1_byte (abfd, line_ptr, line_end);
2163 lh.line_base = read_1_signed_byte (abfd, line_ptr, line_end);
2166 lh.line_range = read_1_byte (abfd, line_ptr, line_end);
2169 lh.opcode_base = read_1_byte (abfd, line_ptr, line_end);
2172 if (line_ptr + (lh.opcode_base - 1) >= line_end)
2174 _bfd_error_handler (_("DWARF error: ran out of room reading opcodes"));
2175 bfd_set_error (bfd_error_bad_value);
2179 amt = lh.opcode_base * sizeof (unsigned char);
2180 lh.standard_opcode_lengths = (unsigned char *) bfd_alloc (abfd, amt);
2182 lh.standard_opcode_lengths[0] = 1;
2184 for (i = 1; i < lh.opcode_base; ++i)
2186 lh.standard_opcode_lengths[i] = read_1_byte (abfd, line_ptr, line_end);
2190 if (lh.version >= 5)
2192 /* Read directory table. */
2193 if (!read_formatted_entries (unit, &line_ptr, line_end, table,
2194 line_info_add_include_dir_stub))
2197 /* Read file name table. */
2198 if (!read_formatted_entries (unit, &line_ptr, line_end, table,
2199 line_info_add_file_name))
2204 /* Read directory table. */
2205 while ((cur_dir = read_string (abfd, line_ptr, line_end, &bytes_read)) != NULL)
2207 line_ptr += bytes_read;
2209 if (!line_info_add_include_dir (table, cur_dir))
2213 line_ptr += bytes_read;
2215 /* Read file name table. */
2216 while ((cur_file = read_string (abfd, line_ptr, line_end, &bytes_read)) != NULL)
2218 unsigned int dir, xtime, size;
2220 line_ptr += bytes_read;
2222 dir = _bfd_safe_read_leb128 (abfd, line_ptr, &bytes_read, FALSE, line_end);
2223 line_ptr += bytes_read;
2224 xtime = _bfd_safe_read_leb128 (abfd, line_ptr, &bytes_read, FALSE, line_end);
2225 line_ptr += bytes_read;
2226 size = _bfd_safe_read_leb128 (abfd, line_ptr, &bytes_read, FALSE, line_end);
2227 line_ptr += bytes_read;
2229 if (!line_info_add_file_name (table, cur_file, dir, xtime, size))
2233 line_ptr += bytes_read;
2236 /* Read the statement sequences until there's nothing left. */
2237 while (line_ptr < line_end)
2239 /* State machine registers. */
2240 bfd_vma address = 0;
2241 unsigned char op_index = 0;
2242 char * filename = table->num_files ? concat_filename (table, 1) : NULL;
2243 unsigned int line = 1;
2244 unsigned int column = 0;
2245 unsigned int discriminator = 0;
2246 int is_stmt = lh.default_is_stmt;
2247 int end_sequence = 0;
2248 unsigned int dir, xtime, size;
2249 /* eraxxon@alumni.rice.edu: Against the DWARF2 specs, some
2250 compilers generate address sequences that are wildly out of
2251 order using DW_LNE_set_address (e.g. Intel C++ 6.0 compiler
2252 for ia64-Linux). Thus, to determine the low and high
2253 address, we must compare on every DW_LNS_copy, etc. */
2254 bfd_vma low_pc = (bfd_vma) -1;
2255 bfd_vma high_pc = 0;
2257 /* Decode the table. */
2258 while (!end_sequence && line_ptr < line_end)
2260 op_code = read_1_byte (abfd, line_ptr, line_end);
2263 if (op_code >= lh.opcode_base)
2265 /* Special operand. */
2266 adj_opcode = op_code - lh.opcode_base;
2267 if (lh.line_range == 0)
2269 if (lh.maximum_ops_per_insn == 1)
2270 address += (adj_opcode / lh.line_range
2271 * lh.minimum_instruction_length);
2274 address += ((op_index + adj_opcode / lh.line_range)
2275 / lh.maximum_ops_per_insn
2276 * lh.minimum_instruction_length);
2277 op_index = ((op_index + adj_opcode / lh.line_range)
2278 % lh.maximum_ops_per_insn);
2280 line += lh.line_base + (adj_opcode % lh.line_range);
2281 /* Append row to matrix using current values. */
2282 if (!add_line_info (table, address, op_index, filename,
2283 line, column, discriminator, 0))
2286 if (address < low_pc)
2288 if (address > high_pc)
2291 else switch (op_code)
2293 case DW_LNS_extended_op:
2294 exop_len = _bfd_safe_read_leb128 (abfd, line_ptr, &bytes_read,
2296 line_ptr += bytes_read;
2297 extended_op = read_1_byte (abfd, line_ptr, line_end);
2300 switch (extended_op)
2302 case DW_LNE_end_sequence:
2304 if (!add_line_info (table, address, op_index, filename, line,
2305 column, discriminator, end_sequence))
2308 if (address < low_pc)
2310 if (address > high_pc)
2312 if (!arange_add (unit, &unit->arange, low_pc, high_pc))
2315 case DW_LNE_set_address:
2316 address = read_address (unit, line_ptr, line_end);
2318 line_ptr += unit->addr_size;
2320 case DW_LNE_define_file:
2321 cur_file = read_string (abfd, line_ptr, line_end, &bytes_read);
2322 line_ptr += bytes_read;
2323 dir = _bfd_safe_read_leb128 (abfd, line_ptr, &bytes_read,
2325 line_ptr += bytes_read;
2326 xtime = _bfd_safe_read_leb128 (abfd, line_ptr, &bytes_read,
2328 line_ptr += bytes_read;
2329 size = _bfd_safe_read_leb128 (abfd, line_ptr, &bytes_read,
2331 line_ptr += bytes_read;
2332 if (!line_info_add_file_name (table, cur_file, dir,
2336 case DW_LNE_set_discriminator:
2338 _bfd_safe_read_leb128 (abfd, line_ptr, &bytes_read,
2340 line_ptr += bytes_read;
2342 case DW_LNE_HP_source_file_correlation:
2343 line_ptr += exop_len - 1;
2347 (_("DWARF error: mangled line number section"));
2348 bfd_set_error (bfd_error_bad_value);
2350 if (filename != NULL)
2356 if (!add_line_info (table, address, op_index,
2357 filename, line, column, discriminator, 0))
2360 if (address < low_pc)
2362 if (address > high_pc)
2365 case DW_LNS_advance_pc:
2366 if (lh.maximum_ops_per_insn == 1)
2367 address += (lh.minimum_instruction_length
2368 * _bfd_safe_read_leb128 (abfd, line_ptr,
2373 bfd_vma adjust = _bfd_safe_read_leb128 (abfd, line_ptr,
2376 address = ((op_index + adjust) / lh.maximum_ops_per_insn
2377 * lh.minimum_instruction_length);
2378 op_index = (op_index + adjust) % lh.maximum_ops_per_insn;
2380 line_ptr += bytes_read;
2382 case DW_LNS_advance_line:
2383 line += _bfd_safe_read_leb128 (abfd, line_ptr, &bytes_read,
2385 line_ptr += bytes_read;
2387 case DW_LNS_set_file:
2391 /* The file and directory tables are 0
2392 based, the references are 1 based. */
2393 file = _bfd_safe_read_leb128 (abfd, line_ptr, &bytes_read,
2395 line_ptr += bytes_read;
2398 filename = concat_filename (table, file);
2401 case DW_LNS_set_column:
2402 column = _bfd_safe_read_leb128 (abfd, line_ptr, &bytes_read,
2404 line_ptr += bytes_read;
2406 case DW_LNS_negate_stmt:
2407 is_stmt = (!is_stmt);
2409 case DW_LNS_set_basic_block:
2411 case DW_LNS_const_add_pc:
2412 if (lh.line_range == 0)
2414 if (lh.maximum_ops_per_insn == 1)
2415 address += (lh.minimum_instruction_length
2416 * ((255 - lh.opcode_base) / lh.line_range));
2419 bfd_vma adjust = ((255 - lh.opcode_base) / lh.line_range);
2420 address += (lh.minimum_instruction_length
2421 * ((op_index + adjust)
2422 / lh.maximum_ops_per_insn));
2423 op_index = (op_index + adjust) % lh.maximum_ops_per_insn;
2426 case DW_LNS_fixed_advance_pc:
2427 address += read_2_bytes (abfd, line_ptr, line_end);
2432 /* Unknown standard opcode, ignore it. */
2433 for (i = 0; i < lh.standard_opcode_lengths[op_code]; i++)
2435 (void) _bfd_safe_read_leb128 (abfd, line_ptr, &bytes_read,
2437 line_ptr += bytes_read;
2447 if (sort_line_sequences (table))
2451 while (table->sequences != NULL)
2453 struct line_sequence* seq = table->sequences;
2454 table->sequences = table->sequences->prev_sequence;
2457 if (table->files != NULL)
2458 free (table->files);
2459 if (table->dirs != NULL)
2464 /* If ADDR is within TABLE set the output parameters and return the
2465 range of addresses covered by the entry used to fill them out.
2466 Otherwise set * FILENAME_PTR to NULL and return 0.
2467 The parameters FILENAME_PTR, LINENUMBER_PTR and DISCRIMINATOR_PTR
2468 are pointers to the objects to be filled in. */
2471 lookup_address_in_line_info_table (struct line_info_table *table,
2473 const char **filename_ptr,
2474 unsigned int *linenumber_ptr,
2475 unsigned int *discriminator_ptr)
2477 struct line_sequence *seq = NULL;
2478 struct line_info *info;
2481 /* Binary search the array of sequences. */
2483 high = table->num_sequences;
2486 mid = (low + high) / 2;
2487 seq = &table->sequences[mid];
2488 if (addr < seq->low_pc)
2490 else if (addr >= seq->last_line->address)
2496 /* Check for a valid sequence. */
2497 if (!seq || addr < seq->low_pc || addr >= seq->last_line->address)
2500 if (!build_line_info_table (table, seq))
2503 /* Binary search the array of line information. */
2505 high = seq->num_lines;
2509 mid = (low + high) / 2;
2510 info = seq->line_info_lookup[mid];
2511 if (addr < info->address)
2513 else if (addr >= seq->line_info_lookup[mid + 1]->address)
2519 /* Check for a valid line information entry. */
2521 && addr >= info->address
2522 && addr < seq->line_info_lookup[mid + 1]->address
2523 && !(info->end_sequence || info == seq->last_line))
2525 *filename_ptr = info->filename;
2526 *linenumber_ptr = info->line;
2527 if (discriminator_ptr)
2528 *discriminator_ptr = info->discriminator;
2529 return seq->last_line->address - seq->low_pc;
2533 *filename_ptr = NULL;
2537 /* Read in the .debug_ranges section for future reference. */
2540 read_debug_ranges (struct comp_unit * unit)
2542 struct dwarf2_debug * stash = unit->stash;
2544 return read_section (unit->abfd, &stash->debug_sections[debug_ranges],
2546 &stash->dwarf_ranges_buffer,
2547 &stash->dwarf_ranges_size);
2550 /* Function table functions. */
2553 compare_lookup_funcinfos (const void * a, const void * b)
2555 const struct lookup_funcinfo * lookup1 = a;
2556 const struct lookup_funcinfo * lookup2 = b;
2558 if (lookup1->low_addr < lookup2->low_addr)
2560 if (lookup1->low_addr > lookup2->low_addr)
2562 if (lookup1->high_addr < lookup2->high_addr)
2564 if (lookup1->high_addr > lookup2->high_addr)
2571 build_lookup_funcinfo_table (struct comp_unit * unit)
2573 struct lookup_funcinfo *lookup_funcinfo_table = unit->lookup_funcinfo_table;
2574 unsigned int number_of_functions = unit->number_of_functions;
2575 struct funcinfo *each;
2576 struct lookup_funcinfo *entry;
2578 struct arange *range;
2579 bfd_vma low_addr, high_addr;
2581 if (lookup_funcinfo_table || number_of_functions == 0)
2584 /* Create the function info lookup table. */
2585 lookup_funcinfo_table = (struct lookup_funcinfo *)
2586 bfd_malloc (number_of_functions * sizeof (struct lookup_funcinfo));
2587 if (lookup_funcinfo_table == NULL)
2590 /* Populate the function info lookup table. */
2591 func_index = number_of_functions;
2592 for (each = unit->function_table; each; each = each->prev_func)
2594 entry = &lookup_funcinfo_table[--func_index];
2595 entry->funcinfo = each;
2597 /* Calculate the lowest and highest address for this function entry. */
2598 low_addr = entry->funcinfo->arange.low;
2599 high_addr = entry->funcinfo->arange.high;
2601 for (range = entry->funcinfo->arange.next; range; range = range->next)
2603 if (range->low < low_addr)
2604 low_addr = range->low;
2605 if (range->high > high_addr)
2606 high_addr = range->high;
2609 entry->low_addr = low_addr;
2610 entry->high_addr = high_addr;
2613 BFD_ASSERT (func_index == 0);
2615 /* Sort the function by address. */
2616 qsort (lookup_funcinfo_table,
2617 number_of_functions,
2618 sizeof (struct lookup_funcinfo),
2619 compare_lookup_funcinfos);
2621 /* Calculate the high watermark for each function in the lookup table. */
2622 high_addr = lookup_funcinfo_table[0].high_addr;
2623 for (func_index = 1; func_index < number_of_functions; func_index++)
2625 entry = &lookup_funcinfo_table[func_index];
2626 if (entry->high_addr > high_addr)
2627 high_addr = entry->high_addr;
2629 entry->high_addr = high_addr;
2632 unit->lookup_funcinfo_table = lookup_funcinfo_table;
2636 /* If ADDR is within UNIT's function tables, set FUNCTION_PTR, and return
2637 TRUE. Note that we need to find the function that has the smallest range
2638 that contains ADDR, to handle inlined functions without depending upon
2639 them being ordered in TABLE by increasing range. */
2642 lookup_address_in_function_table (struct comp_unit *unit,
2644 struct funcinfo **function_ptr)
2646 unsigned int number_of_functions = unit->number_of_functions;
2647 struct lookup_funcinfo* lookup_funcinfo = NULL;
2648 struct funcinfo* funcinfo = NULL;
2649 struct funcinfo* best_fit = NULL;
2650 bfd_vma best_fit_len = 0;
2651 bfd_size_type low, high, mid, first;
2652 struct arange *arange;
2654 if (number_of_functions == 0)
2657 if (!build_lookup_funcinfo_table (unit))
2660 if (unit->lookup_funcinfo_table[number_of_functions - 1].high_addr < addr)
2663 /* Find the first function in the lookup table which may contain the
2664 specified address. */
2666 high = number_of_functions;
2670 mid = (low + high) / 2;
2671 lookup_funcinfo = &unit->lookup_funcinfo_table[mid];
2672 if (addr < lookup_funcinfo->low_addr)
2674 else if (addr >= lookup_funcinfo->high_addr)
2680 /* Find the 'best' match for the address. The prior algorithm defined the
2681 best match as the function with the smallest address range containing
2682 the specified address. This definition should probably be changed to the
2683 innermost inline routine containing the address, but right now we want
2684 to get the same results we did before. */
2685 while (first < number_of_functions)
2687 if (addr < unit->lookup_funcinfo_table[first].low_addr)
2689 funcinfo = unit->lookup_funcinfo_table[first].funcinfo;
2691 for (arange = &funcinfo->arange; arange; arange = arange->next)
2693 if (addr < arange->low || addr >= arange->high)
2697 || arange->high - arange->low < best_fit_len
2698 /* The following comparison is designed to return the same
2699 match as the previous algorithm for routines which have the
2700 same best fit length. */
2701 || (arange->high - arange->low == best_fit_len
2702 && funcinfo > best_fit))
2704 best_fit = funcinfo;
2705 best_fit_len = arange->high - arange->low;
2715 *function_ptr = best_fit;
2719 /* If SYM at ADDR is within function table of UNIT, set FILENAME_PTR
2720 and LINENUMBER_PTR, and return TRUE. */
2723 lookup_symbol_in_function_table (struct comp_unit *unit,
2726 const char **filename_ptr,
2727 unsigned int *linenumber_ptr)
2729 struct funcinfo* each_func;
2730 struct funcinfo* best_fit = NULL;
2731 bfd_vma best_fit_len = 0;
2732 struct arange *arange;
2733 const char *name = bfd_asymbol_name (sym);
2734 asection *sec = bfd_get_section (sym);
2736 for (each_func = unit->function_table;
2738 each_func = each_func->prev_func)
2740 for (arange = &each_func->arange;
2742 arange = arange->next)
2744 if ((!each_func->sec || each_func->sec == sec)
2745 && addr >= arange->low
2746 && addr < arange->high
2748 && strcmp (name, each_func->name) == 0
2750 || arange->high - arange->low < best_fit_len))
2752 best_fit = each_func;
2753 best_fit_len = arange->high - arange->low;
2760 best_fit->sec = sec;
2761 *filename_ptr = best_fit->file;
2762 *linenumber_ptr = best_fit->line;
2769 /* Variable table functions. */
2771 /* If SYM is within variable table of UNIT, set FILENAME_PTR and
2772 LINENUMBER_PTR, and return TRUE. */
2775 lookup_symbol_in_variable_table (struct comp_unit *unit,
2778 const char **filename_ptr,
2779 unsigned int *linenumber_ptr)
2781 const char *name = bfd_asymbol_name (sym);
2782 asection *sec = bfd_get_section (sym);
2783 struct varinfo* each;
2785 for (each = unit->variable_table; each; each = each->prev_var)
2786 if (each->stack == 0
2787 && each->file != NULL
2788 && each->name != NULL
2789 && each->addr == addr
2790 && (!each->sec || each->sec == sec)
2791 && strcmp (name, each->name) == 0)
2797 *filename_ptr = each->file;
2798 *linenumber_ptr = each->line;
2806 find_abstract_instance (struct comp_unit * unit,
2807 bfd_byte * orig_info_ptr,
2808 struct attribute * attr_ptr,
2809 const char ** pname,
2810 bfd_boolean * is_linkage,
2811 char ** filename_ptr,
2812 int * linenumber_ptr)
2814 bfd *abfd = unit->abfd;
2816 bfd_byte *info_ptr_end;
2817 unsigned int abbrev_number, bytes_read, i;
2818 struct abbrev_info *abbrev;
2819 bfd_uint64_t die_ref = attr_ptr->u.val;
2820 struct attribute attr;
2821 const char *name = NULL;
2823 /* DW_FORM_ref_addr can reference an entry in a different CU. It
2824 is an offset from the .debug_info section, not the current CU. */
2825 if (attr_ptr->form == DW_FORM_ref_addr)
2827 /* We only support DW_FORM_ref_addr within the same file, so
2828 any relocations should be resolved already. Check this by
2829 testing for a zero die_ref; There can't be a valid reference
2830 to the header of a .debug_info section.
2831 DW_FORM_ref_addr is an offset relative to .debug_info.
2832 Normally when using the GNU linker this is accomplished by
2833 emitting a symbolic reference to a label, because .debug_info
2834 sections are linked at zero. When there are multiple section
2835 groups containing .debug_info, as there might be in a
2836 relocatable object file, it would be reasonable to assume that
2837 a symbolic reference to a label in any .debug_info section
2838 might be used. Since we lay out multiple .debug_info
2839 sections at non-zero VMAs (see place_sections), and read
2840 them contiguously into stash->info_ptr_memory, that means
2841 the reference is relative to stash->info_ptr_memory. */
2844 info_ptr = unit->stash->info_ptr_memory;
2845 info_ptr_end = unit->stash->info_ptr_end;
2846 total = info_ptr_end - info_ptr;
2849 else if (die_ref >= total)
2852 (_("DWARF error: invalid abstract instance DIE ref"));
2853 bfd_set_error (bfd_error_bad_value);
2856 info_ptr += die_ref;
2858 /* Now find the CU containing this pointer. */
2859 if (info_ptr >= unit->info_ptr_unit && info_ptr < unit->end_ptr)
2860 info_ptr_end = unit->end_ptr;
2863 /* Check other CUs to see if they contain the abbrev. */
2864 struct comp_unit * u;
2866 for (u = unit->prev_unit; u != NULL; u = u->prev_unit)
2867 if (info_ptr >= u->info_ptr_unit && info_ptr < u->end_ptr)
2871 for (u = unit->next_unit; u != NULL; u = u->next_unit)
2872 if (info_ptr >= u->info_ptr_unit && info_ptr < u->end_ptr)
2878 info_ptr_end = unit->end_ptr;
2880 /* else FIXME: What do we do now ? */
2883 else if (attr_ptr->form == DW_FORM_GNU_ref_alt)
2885 info_ptr = read_alt_indirect_ref (unit, die_ref);
2886 if (info_ptr == NULL)
2889 (_("DWARF error: unable to read alt ref %" PRIu64),
2890 (uint64_t) die_ref);
2891 bfd_set_error (bfd_error_bad_value);
2894 info_ptr_end = (unit->stash->alt_dwarf_info_buffer
2895 + unit->stash->alt_dwarf_info_size);
2897 /* FIXME: Do we need to locate the correct CU, in a similar
2898 fashion to the code in the DW_FORM_ref_addr case above ? */
2902 /* DW_FORM_ref1, DW_FORM_ref2, DW_FORM_ref4, DW_FORM_ref8 or
2903 DW_FORM_ref_udata. These are all references relative to the
2904 start of the current CU. */
2907 info_ptr = unit->info_ptr_unit;
2908 info_ptr_end = unit->end_ptr;
2909 total = info_ptr_end - info_ptr;
2910 if (!die_ref || die_ref >= total)
2913 (_("DWARF error: invalid abstract instance DIE ref"));
2914 bfd_set_error (bfd_error_bad_value);
2917 info_ptr += die_ref;
2920 abbrev_number = _bfd_safe_read_leb128 (abfd, info_ptr, &bytes_read,
2921 FALSE, info_ptr_end);
2922 info_ptr += bytes_read;
2926 abbrev = lookup_abbrev (abbrev_number, unit->abbrevs);
2930 (_("DWARF error: could not find abbrev number %u"), abbrev_number);
2931 bfd_set_error (bfd_error_bad_value);
2936 for (i = 0; i < abbrev->num_attrs; ++i)
2938 info_ptr = read_attribute (&attr, &abbrev->attrs[i], unit,
2939 info_ptr, info_ptr_end);
2940 if (info_ptr == NULL)
2942 /* It doesn't ever make sense for DW_AT_specification to
2943 refer to the same DIE. Stop simple recursion. */
2944 if (info_ptr == orig_info_ptr)
2947 (_("DWARF error: abstract instance recursion detected"));
2948 bfd_set_error (bfd_error_bad_value);
2954 /* Prefer DW_AT_MIPS_linkage_name or DW_AT_linkage_name
2956 if (name == NULL && is_str_attr (attr.form))
2959 if (non_mangled (unit->lang))
2963 case DW_AT_specification:
2964 if (!find_abstract_instance (unit, info_ptr, &attr,
2966 filename_ptr, linenumber_ptr))
2969 case DW_AT_linkage_name:
2970 case DW_AT_MIPS_linkage_name:
2971 /* PR 16949: Corrupt debug info can place
2972 non-string forms into these attributes. */
2973 if (is_str_attr (attr.form))
2979 case DW_AT_decl_file:
2980 *filename_ptr = concat_filename (unit->line_table,
2983 case DW_AT_decl_line:
2984 *linenumber_ptr = attr.u.val;
2997 read_rangelist (struct comp_unit *unit, struct arange *arange,
2998 bfd_uint64_t offset)
3000 bfd_byte *ranges_ptr;
3001 bfd_byte *ranges_end;
3002 bfd_vma base_address = unit->base_address;
3004 if (! unit->stash->dwarf_ranges_buffer)
3006 if (! read_debug_ranges (unit))
3010 ranges_ptr = unit->stash->dwarf_ranges_buffer + offset;
3011 if (ranges_ptr < unit->stash->dwarf_ranges_buffer)
3013 ranges_end = unit->stash->dwarf_ranges_buffer + unit->stash->dwarf_ranges_size;
3020 /* PR 17512: file: 62cada7d. */
3021 if (ranges_ptr + 2 * unit->addr_size > ranges_end)
3024 low_pc = read_address (unit, ranges_ptr, ranges_end);
3025 ranges_ptr += unit->addr_size;
3026 high_pc = read_address (unit, ranges_ptr, ranges_end);
3027 ranges_ptr += unit->addr_size;
3029 if (low_pc == 0 && high_pc == 0)
3031 if (low_pc == -1UL && high_pc != -1UL)
3032 base_address = high_pc;
3035 if (!arange_add (unit, arange,
3036 base_address + low_pc, base_address + high_pc))
3043 /* DWARF2 Compilation unit functions. */
3045 /* Scan over each die in a comp. unit looking for functions to add
3046 to the function table and variables to the variable table. */
3049 scan_unit_for_symbols (struct comp_unit *unit)
3051 bfd *abfd = unit->abfd;
3052 bfd_byte *info_ptr = unit->first_child_die_ptr;
3053 bfd_byte *info_ptr_end = unit->stash->info_ptr_end;
3054 int nesting_level = 0;
3055 struct nest_funcinfo {
3056 struct funcinfo *func;
3058 int nested_funcs_size;
3060 /* Maintain a stack of in-scope functions and inlined functions, which we
3061 can use to set the caller_func field. */
3062 nested_funcs_size = 32;
3063 nested_funcs = (struct nest_funcinfo *)
3064 bfd_malloc (nested_funcs_size * sizeof (*nested_funcs));
3065 if (nested_funcs == NULL)
3067 nested_funcs[nesting_level].func = 0;
3069 while (nesting_level >= 0)
3071 unsigned int abbrev_number, bytes_read, i;
3072 struct abbrev_info *abbrev;
3073 struct attribute attr;
3074 struct funcinfo *func;
3075 struct varinfo *var;
3077 bfd_vma high_pc = 0;
3078 bfd_boolean high_pc_relative = FALSE;
3080 /* PR 17512: file: 9f405d9d. */
3081 if (info_ptr >= info_ptr_end)
3084 abbrev_number = _bfd_safe_read_leb128 (abfd, info_ptr, &bytes_read,
3085 FALSE, info_ptr_end);
3086 info_ptr += bytes_read;
3088 if (! abbrev_number)
3094 abbrev = lookup_abbrev (abbrev_number, unit->abbrevs);
3097 static unsigned int previous_failed_abbrev = -1U;
3099 /* Avoid multiple reports of the same missing abbrev. */
3100 if (abbrev_number != previous_failed_abbrev)
3103 (_("DWARF error: could not find abbrev number %u"),
3105 previous_failed_abbrev = abbrev_number;
3107 bfd_set_error (bfd_error_bad_value);
3112 if (abbrev->tag == DW_TAG_subprogram
3113 || abbrev->tag == DW_TAG_entry_point
3114 || abbrev->tag == DW_TAG_inlined_subroutine)
3116 bfd_size_type amt = sizeof (struct funcinfo);
3117 func = (struct funcinfo *) bfd_zalloc (abfd, amt);
3120 func->tag = abbrev->tag;
3121 func->prev_func = unit->function_table;
3122 unit->function_table = func;
3123 unit->number_of_functions++;
3124 BFD_ASSERT (!unit->cached);
3126 if (func->tag == DW_TAG_inlined_subroutine)
3127 for (i = nesting_level; i-- != 0; )
3128 if (nested_funcs[i].func)
3130 func->caller_func = nested_funcs[i].func;
3133 nested_funcs[nesting_level].func = func;
3138 if (abbrev->tag == DW_TAG_variable)
3140 bfd_size_type amt = sizeof (struct varinfo);
3141 var = (struct varinfo *) bfd_zalloc (abfd, amt);
3144 var->tag = abbrev->tag;
3146 var->prev_var = unit->variable_table;
3147 unit->variable_table = var;
3148 /* PR 18205: Missing debug information can cause this
3149 var to be attached to an already cached unit. */
3152 /* No inline function in scope at this nesting level. */
3153 nested_funcs[nesting_level].func = 0;
3156 for (i = 0; i < abbrev->num_attrs; ++i)
3158 info_ptr = read_attribute (&attr, &abbrev->attrs[i],
3159 unit, info_ptr, info_ptr_end);
3160 if (info_ptr == NULL)
3167 case DW_AT_call_file:
3168 func->caller_file = concat_filename (unit->line_table,
3172 case DW_AT_call_line:
3173 func->caller_line = attr.u.val;
3176 case DW_AT_abstract_origin:
3177 case DW_AT_specification:
3178 if (!find_abstract_instance (unit, info_ptr, &attr,
3187 /* Prefer DW_AT_MIPS_linkage_name or DW_AT_linkage_name
3189 if (func->name == NULL && is_str_attr (attr.form))
3191 func->name = attr.u.str;
3192 if (non_mangled (unit->lang))
3193 func->is_linkage = TRUE;
3197 case DW_AT_linkage_name:
3198 case DW_AT_MIPS_linkage_name:
3199 /* PR 16949: Corrupt debug info can place
3200 non-string forms into these attributes. */
3201 if (is_str_attr (attr.form))
3203 func->name = attr.u.str;
3204 func->is_linkage = TRUE;
3209 low_pc = attr.u.val;
3213 high_pc = attr.u.val;
3214 high_pc_relative = attr.form != DW_FORM_addr;
3218 if (!read_rangelist (unit, &func->arange, attr.u.val))
3222 case DW_AT_decl_file:
3223 func->file = concat_filename (unit->line_table,
3227 case DW_AT_decl_line:
3228 func->line = attr.u.val;
3240 if (is_str_attr (attr.form))
3241 var->name = attr.u.str;
3244 case DW_AT_decl_file:
3245 var->file = concat_filename (unit->line_table,
3249 case DW_AT_decl_line:
3250 var->line = attr.u.val;
3253 case DW_AT_external:
3254 if (attr.u.val != 0)
3258 case DW_AT_location:
3262 case DW_FORM_block1:
3263 case DW_FORM_block2:
3264 case DW_FORM_block4:
3265 case DW_FORM_exprloc:
3266 if (attr.u.blk->data != NULL
3267 && *attr.u.blk->data == DW_OP_addr)
3271 /* Verify that DW_OP_addr is the only opcode in the
3272 location, in which case the block size will be 1
3273 plus the address size. */
3274 /* ??? For TLS variables, gcc can emit
3275 DW_OP_addr <addr> DW_OP_GNU_push_tls_address
3276 which we don't handle here yet. */
3277 if (attr.u.blk->size == unit->addr_size + 1U)
3278 var->addr = bfd_get (unit->addr_size * 8,
3280 attr.u.blk->data + 1);
3295 if (high_pc_relative)
3298 if (func && high_pc != 0)
3300 if (!arange_add (unit, &func->arange, low_pc, high_pc))
3304 if (abbrev->has_children)
3308 if (nesting_level >= nested_funcs_size)
3310 struct nest_funcinfo *tmp;
3312 nested_funcs_size *= 2;
3313 tmp = (struct nest_funcinfo *)
3314 bfd_realloc (nested_funcs,
3315 nested_funcs_size * sizeof (*nested_funcs));
3320 nested_funcs[nesting_level].func = 0;
3324 free (nested_funcs);
3328 free (nested_funcs);
3332 /* Parse a DWARF2 compilation unit starting at INFO_PTR. This
3333 includes the compilation unit header that proceeds the DIE's, but
3334 does not include the length field that precedes each compilation
3335 unit header. END_PTR points one past the end of this comp unit.
3336 OFFSET_SIZE is the size of DWARF2 offsets (either 4 or 8 bytes).
3338 This routine does not read the whole compilation unit; only enough
3339 to get to the line number information for the compilation unit. */
3341 static struct comp_unit *
3342 parse_comp_unit (struct dwarf2_debug *stash,
3343 bfd_vma unit_length,
3344 bfd_byte *info_ptr_unit,
3345 unsigned int offset_size)
3347 struct comp_unit* unit;
3348 unsigned int version;
3349 bfd_uint64_t abbrev_offset = 0;
3350 /* Initialize it just to avoid a GCC false warning. */
3351 unsigned int addr_size = -1;
3352 struct abbrev_info** abbrevs;
3353 unsigned int abbrev_number, bytes_read, i;
3354 struct abbrev_info *abbrev;
3355 struct attribute attr;
3356 bfd_byte *info_ptr = stash->info_ptr;
3357 bfd_byte *end_ptr = info_ptr + unit_length;
3360 bfd_vma high_pc = 0;
3361 bfd *abfd = stash->bfd_ptr;
3362 bfd_boolean high_pc_relative = FALSE;
3363 enum dwarf_unit_type unit_type;
3365 version = read_2_bytes (abfd, info_ptr, end_ptr);
3367 if (version < 2 || version > 5)
3369 /* PR 19872: A version number of 0 probably means that there is padding
3370 at the end of the .debug_info section. Gold puts it there when
3371 performing an incremental link, for example. So do not generate
3372 an error, just return a NULL. */
3376 (_("DWARF error: found dwarf version '%u', this reader"
3377 " only handles version 2, 3, 4 and 5 information"), version);
3378 bfd_set_error (bfd_error_bad_value);
3384 unit_type = DW_UT_compile;
3387 unit_type = read_1_byte (abfd, info_ptr, end_ptr);
3390 addr_size = read_1_byte (abfd, info_ptr, end_ptr);
3394 BFD_ASSERT (offset_size == 4 || offset_size == 8);
3395 if (offset_size == 4)
3396 abbrev_offset = read_4_bytes (abfd, info_ptr, end_ptr);
3398 abbrev_offset = read_8_bytes (abfd, info_ptr, end_ptr);
3399 info_ptr += offset_size;
3403 addr_size = read_1_byte (abfd, info_ptr, end_ptr);
3407 if (unit_type == DW_UT_type)
3409 /* Skip type signature. */
3412 /* Skip type offset. */
3413 info_ptr += offset_size;
3416 if (addr_size > sizeof (bfd_vma))
3419 /* xgettext: c-format */
3420 (_("DWARF error: found address size '%u', this reader"
3421 " can not handle sizes greater than '%u'"),
3423 (unsigned int) sizeof (bfd_vma));
3424 bfd_set_error (bfd_error_bad_value);
3428 if (addr_size != 2 && addr_size != 4 && addr_size != 8)
3431 ("DWARF error: found address size '%u', this reader"
3432 " can only handle address sizes '2', '4' and '8'", addr_size);
3433 bfd_set_error (bfd_error_bad_value);
3437 /* Read the abbrevs for this compilation unit into a table. */
3438 abbrevs = read_abbrevs (abfd, abbrev_offset, stash);
3442 abbrev_number = _bfd_safe_read_leb128 (abfd, info_ptr, &bytes_read,
3444 info_ptr += bytes_read;
3445 if (! abbrev_number)
3447 /* PR 19872: An abbrev number of 0 probably means that there is padding
3448 at the end of the .debug_abbrev section. Gold puts it there when
3449 performing an incremental link, for example. So do not generate
3450 an error, just return a NULL. */
3454 abbrev = lookup_abbrev (abbrev_number, abbrevs);
3457 _bfd_error_handler (_("DWARF error: could not find abbrev number %u"),
3459 bfd_set_error (bfd_error_bad_value);
3463 amt = sizeof (struct comp_unit);
3464 unit = (struct comp_unit *) bfd_zalloc (abfd, amt);
3468 unit->version = version;
3469 unit->addr_size = addr_size;
3470 unit->offset_size = offset_size;
3471 unit->abbrevs = abbrevs;
3472 unit->end_ptr = end_ptr;
3473 unit->stash = stash;
3474 unit->info_ptr_unit = info_ptr_unit;
3476 for (i = 0; i < abbrev->num_attrs; ++i)
3478 info_ptr = read_attribute (&attr, &abbrev->attrs[i], unit, info_ptr, end_ptr);
3479 if (info_ptr == NULL)
3482 /* Store the data if it is of an attribute we want to keep in a
3483 partial symbol table. */
3486 case DW_AT_stmt_list:
3488 unit->line_offset = attr.u.val;
3492 if (is_str_attr (attr.form))
3493 unit->name = attr.u.str;
3497 low_pc = attr.u.val;
3498 /* If the compilation unit DIE has a DW_AT_low_pc attribute,
3499 this is the base address to use when reading location
3500 lists or range lists. */
3501 if (abbrev->tag == DW_TAG_compile_unit)
3502 unit->base_address = low_pc;
3506 high_pc = attr.u.val;
3507 high_pc_relative = attr.form != DW_FORM_addr;
3511 if (!read_rangelist (unit, &unit->arange, attr.u.val))
3515 case DW_AT_comp_dir:
3517 char *comp_dir = attr.u.str;
3519 /* PR 17512: file: 1fe726be. */
3520 if (! is_str_attr (attr.form))
3523 (_("DWARF error: DW_AT_comp_dir attribute encountered with a non-string form"));
3529 /* Irix 6.2 native cc prepends <machine>.: to the compilation
3530 directory, get rid of it. */
3531 char *cp = strchr (comp_dir, ':');
3533 if (cp && cp != comp_dir && cp[-1] == '.' && cp[1] == '/')
3536 unit->comp_dir = comp_dir;
3540 case DW_AT_language:
3541 unit->lang = attr.u.val;
3548 if (high_pc_relative)
3552 if (!arange_add (unit, &unit->arange, low_pc, high_pc))
3556 unit->first_child_die_ptr = info_ptr;
3560 /* Return TRUE if UNIT may contain the address given by ADDR. When
3561 there are functions written entirely with inline asm statements, the
3562 range info in the compilation unit header may not be correct. We
3563 need to consult the line info table to see if a compilation unit
3564 really contains the given address. */
3567 comp_unit_contains_address (struct comp_unit *unit, bfd_vma addr)
3569 struct arange *arange;
3574 arange = &unit->arange;
3577 if (addr >= arange->low && addr < arange->high)
3579 arange = arange->next;
3586 /* If UNIT contains ADDR, set the output parameters to the values for
3587 the line containing ADDR. The output parameters, FILENAME_PTR,
3588 FUNCTION_PTR, and LINENUMBER_PTR, are pointers to the objects
3591 Returns the range of addresses covered by the entry that was used
3592 to fill in *LINENUMBER_PTR or 0 if it was not filled in. */
3595 comp_unit_find_nearest_line (struct comp_unit *unit,
3597 const char **filename_ptr,
3598 struct funcinfo **function_ptr,
3599 unsigned int *linenumber_ptr,
3600 unsigned int *discriminator_ptr,
3601 struct dwarf2_debug *stash)
3608 if (! unit->line_table)
3610 if (! unit->stmtlist)
3616 unit->line_table = decode_line_info (unit, stash);
3618 if (! unit->line_table)
3624 if (unit->first_child_die_ptr < unit->end_ptr
3625 && ! scan_unit_for_symbols (unit))
3632 *function_ptr = NULL;
3633 func_p = lookup_address_in_function_table (unit, addr, function_ptr);
3634 if (func_p && (*function_ptr)->tag == DW_TAG_inlined_subroutine)
3635 stash->inliner_chain = *function_ptr;
3637 return lookup_address_in_line_info_table (unit->line_table, addr,
3643 /* Check to see if line info is already decoded in a comp_unit.
3644 If not, decode it. Returns TRUE if no errors were encountered;
3648 comp_unit_maybe_decode_line_info (struct comp_unit *unit,
3649 struct dwarf2_debug *stash)
3654 if (! unit->line_table)
3656 if (! unit->stmtlist)
3662 unit->line_table = decode_line_info (unit, stash);
3664 if (! unit->line_table)
3670 if (unit->first_child_die_ptr < unit->end_ptr
3671 && ! scan_unit_for_symbols (unit))
3681 /* If UNIT contains SYM at ADDR, set the output parameters to the
3682 values for the line containing SYM. The output parameters,
3683 FILENAME_PTR, and LINENUMBER_PTR, are pointers to the objects to be
3686 Return TRUE if UNIT contains SYM, and no errors were encountered;
3690 comp_unit_find_line (struct comp_unit *unit,
3693 const char **filename_ptr,
3694 unsigned int *linenumber_ptr,
3695 struct dwarf2_debug *stash)
3697 if (!comp_unit_maybe_decode_line_info (unit, stash))
3700 if (sym->flags & BSF_FUNCTION)
3701 return lookup_symbol_in_function_table (unit, sym, addr,
3705 return lookup_symbol_in_variable_table (unit, sym, addr,
3710 static struct funcinfo *
3711 reverse_funcinfo_list (struct funcinfo *head)
3713 struct funcinfo *rhead;
3714 struct funcinfo *temp;
3716 for (rhead = NULL; head; head = temp)
3718 temp = head->prev_func;
3719 head->prev_func = rhead;
3725 static struct varinfo *
3726 reverse_varinfo_list (struct varinfo *head)
3728 struct varinfo *rhead;
3729 struct varinfo *temp;
3731 for (rhead = NULL; head; head = temp)
3733 temp = head->prev_var;
3734 head->prev_var = rhead;
3740 /* Extract all interesting funcinfos and varinfos of a compilation
3741 unit into hash tables for faster lookup. Returns TRUE if no
3742 errors were enountered; FALSE otherwise. */
3745 comp_unit_hash_info (struct dwarf2_debug *stash,
3746 struct comp_unit *unit,
3747 struct info_hash_table *funcinfo_hash_table,
3748 struct info_hash_table *varinfo_hash_table)
3750 struct funcinfo* each_func;
3751 struct varinfo* each_var;
3752 bfd_boolean okay = TRUE;
3754 BFD_ASSERT (stash->info_hash_status != STASH_INFO_HASH_DISABLED);
3756 if (!comp_unit_maybe_decode_line_info (unit, stash))
3759 BFD_ASSERT (!unit->cached);
3761 /* To preserve the original search order, we went to visit the function
3762 infos in the reversed order of the list. However, making the list
3763 bi-directional use quite a bit of extra memory. So we reverse
3764 the list first, traverse the list in the now reversed order and
3765 finally reverse the list again to get back the original order. */
3766 unit->function_table = reverse_funcinfo_list (unit->function_table);
3767 for (each_func = unit->function_table;
3769 each_func = each_func->prev_func)
3771 /* Skip nameless functions. */
3772 if (each_func->name)
3773 /* There is no need to copy name string into hash table as
3774 name string is either in the dwarf string buffer or
3775 info in the stash. */
3776 okay = insert_info_hash_table (funcinfo_hash_table, each_func->name,
3777 (void*) each_func, FALSE);
3779 unit->function_table = reverse_funcinfo_list (unit->function_table);
3783 /* We do the same for variable infos. */
3784 unit->variable_table = reverse_varinfo_list (unit->variable_table);
3785 for (each_var = unit->variable_table;
3787 each_var = each_var->prev_var)
3789 /* Skip stack vars and vars with no files or names. */
3790 if (each_var->stack == 0
3791 && each_var->file != NULL
3792 && each_var->name != NULL)
3793 /* There is no need to copy name string into hash table as
3794 name string is either in the dwarf string buffer or
3795 info in the stash. */
3796 okay = insert_info_hash_table (varinfo_hash_table, each_var->name,
3797 (void*) each_var, FALSE);
3800 unit->variable_table = reverse_varinfo_list (unit->variable_table);
3801 unit->cached = TRUE;
3805 /* Locate a section in a BFD containing debugging info. The search starts
3806 from the section after AFTER_SEC, or from the first section in the BFD if
3807 AFTER_SEC is NULL. The search works by examining the names of the
3808 sections. There are three permissiable names. The first two are given
3809 by DEBUG_SECTIONS[debug_info] (whose standard DWARF2 names are .debug_info
3810 and .zdebug_info). The third is a prefix .gnu.linkonce.wi.
3811 This is a variation on the .debug_info section which has a checksum
3812 describing the contents appended onto the name. This allows the linker to
3813 identify and discard duplicate debugging sections for different
3814 compilation units. */
3815 #define GNU_LINKONCE_INFO ".gnu.linkonce.wi."
3818 find_debug_info (bfd *abfd, const struct dwarf_debug_section *debug_sections,
3819 asection *after_sec)
3824 if (after_sec == NULL)
3826 look = debug_sections[debug_info].uncompressed_name;
3827 msec = bfd_get_section_by_name (abfd, look);
3831 look = debug_sections[debug_info].compressed_name;
3834 msec = bfd_get_section_by_name (abfd, look);
3839 for (msec = abfd->sections; msec != NULL; msec = msec->next)
3840 if (CONST_STRNEQ (msec->name, GNU_LINKONCE_INFO))
3846 for (msec = after_sec->next; msec != NULL; msec = msec->next)
3848 look = debug_sections[debug_info].uncompressed_name;
3849 if (strcmp (msec->name, look) == 0)
3852 look = debug_sections[debug_info].compressed_name;
3853 if (look != NULL && strcmp (msec->name, look) == 0)
3856 if (CONST_STRNEQ (msec->name, GNU_LINKONCE_INFO))
3863 /* Transfer VMAs from object file to separate debug file. */
3866 set_debug_vma (bfd *orig_bfd, bfd *debug_bfd)
3870 for (s = orig_bfd->sections, d = debug_bfd->sections;
3871 s != NULL && d != NULL;
3872 s = s->next, d = d->next)
3874 if ((d->flags & SEC_DEBUGGING) != 0)
3876 /* ??? Assumes 1-1 correspondence between sections in the
3878 if (strcmp (s->name, d->name) == 0)
3880 d->output_section = s->output_section;
3881 d->output_offset = s->output_offset;
3887 /* Unset vmas for adjusted sections in STASH. */
3890 unset_sections (struct dwarf2_debug *stash)
3893 struct adjusted_section *p;
3895 i = stash->adjusted_section_count;
3896 p = stash->adjusted_sections;
3897 for (; i > 0; i--, p++)
3898 p->section->vma = 0;
3901 /* Set VMAs for allocated and .debug_info sections in ORIG_BFD, a
3902 relocatable object file. VMAs are normally all zero in relocatable
3903 object files, so if we want to distinguish locations in sections by
3904 address we need to set VMAs so the sections do not overlap. We
3905 also set VMA on .debug_info so that when we have multiple
3906 .debug_info sections (or the linkonce variant) they also do not
3907 overlap. The multiple .debug_info sections make up a single
3908 logical section. ??? We should probably do the same for other
3912 place_sections (bfd *orig_bfd, struct dwarf2_debug *stash)
3915 struct adjusted_section *p;
3917 const char *debug_info_name;
3919 if (stash->adjusted_section_count != 0)
3921 i = stash->adjusted_section_count;
3922 p = stash->adjusted_sections;
3923 for (; i > 0; i--, p++)
3924 p->section->vma = p->adj_vma;
3928 debug_info_name = stash->debug_sections[debug_info].uncompressed_name;
3935 for (sect = abfd->sections; sect != NULL; sect = sect->next)
3939 if ((sect->output_section != NULL
3940 && sect->output_section != sect
3941 && (sect->flags & SEC_DEBUGGING) == 0)
3945 is_debug_info = (strcmp (sect->name, debug_info_name) == 0
3946 || CONST_STRNEQ (sect->name, GNU_LINKONCE_INFO));
3948 if (!((sect->flags & SEC_ALLOC) != 0 && abfd == orig_bfd)
3954 if (abfd == stash->bfd_ptr)
3956 abfd = stash->bfd_ptr;
3960 stash->adjusted_section_count = -1;
3963 bfd_vma last_vma = 0, last_dwarf = 0;
3964 bfd_size_type amt = i * sizeof (struct adjusted_section);
3966 p = (struct adjusted_section *) bfd_malloc (amt);
3970 stash->adjusted_sections = p;
3971 stash->adjusted_section_count = i;
3978 for (sect = abfd->sections; sect != NULL; sect = sect->next)
3983 if ((sect->output_section != NULL
3984 && sect->output_section != sect
3985 && (sect->flags & SEC_DEBUGGING) == 0)
3989 is_debug_info = (strcmp (sect->name, debug_info_name) == 0
3990 || CONST_STRNEQ (sect->name, GNU_LINKONCE_INFO));
3992 if (!((sect->flags & SEC_ALLOC) != 0 && abfd == orig_bfd)
3996 sz = sect->rawsize ? sect->rawsize : sect->size;
4000 BFD_ASSERT (sect->alignment_power == 0);
4001 sect->vma = last_dwarf;
4006 /* Align the new address to the current section
4008 last_vma = ((last_vma
4009 + ~(-((bfd_vma) 1 << sect->alignment_power)))
4010 & (-((bfd_vma) 1 << sect->alignment_power)));
4011 sect->vma = last_vma;
4016 p->adj_vma = sect->vma;
4019 if (abfd == stash->bfd_ptr)
4021 abfd = stash->bfd_ptr;
4025 if (orig_bfd != stash->bfd_ptr)
4026 set_debug_vma (orig_bfd, stash->bfd_ptr);
4031 /* Look up a funcinfo by name using the given info hash table. If found,
4032 also update the locations pointed to by filename_ptr and linenumber_ptr.
4034 This function returns TRUE if a funcinfo that matches the given symbol
4035 and address is found with any error; otherwise it returns FALSE. */
4038 info_hash_lookup_funcinfo (struct info_hash_table *hash_table,
4041 const char **filename_ptr,
4042 unsigned int *linenumber_ptr)
4044 struct funcinfo* each_func;
4045 struct funcinfo* best_fit = NULL;
4046 bfd_vma best_fit_len = 0;
4047 struct info_list_node *node;
4048 struct arange *arange;
4049 const char *name = bfd_asymbol_name (sym);
4050 asection *sec = bfd_get_section (sym);
4052 for (node = lookup_info_hash_table (hash_table, name);
4056 each_func = (struct funcinfo *) node->info;
4057 for (arange = &each_func->arange;
4059 arange = arange->next)
4061 if ((!each_func->sec || each_func->sec == sec)
4062 && addr >= arange->low
4063 && addr < arange->high
4065 || arange->high - arange->low < best_fit_len))
4067 best_fit = each_func;
4068 best_fit_len = arange->high - arange->low;
4075 best_fit->sec = sec;
4076 *filename_ptr = best_fit->file;
4077 *linenumber_ptr = best_fit->line;
4084 /* Look up a varinfo by name using the given info hash table. If found,
4085 also update the locations pointed to by filename_ptr and linenumber_ptr.
4087 This function returns TRUE if a varinfo that matches the given symbol
4088 and address is found with any error; otherwise it returns FALSE. */
4091 info_hash_lookup_varinfo (struct info_hash_table *hash_table,
4094 const char **filename_ptr,
4095 unsigned int *linenumber_ptr)
4097 const char *name = bfd_asymbol_name (sym);
4098 asection *sec = bfd_get_section (sym);
4099 struct varinfo* each;
4100 struct info_list_node *node;
4102 for (node = lookup_info_hash_table (hash_table, name);
4106 each = (struct varinfo *) node->info;
4107 if (each->addr == addr
4108 && (!each->sec || each->sec == sec))
4111 *filename_ptr = each->file;
4112 *linenumber_ptr = each->line;
4120 /* Update the funcinfo and varinfo info hash tables if they are
4121 not up to date. Returns TRUE if there is no error; otherwise
4122 returns FALSE and disable the info hash tables. */
4125 stash_maybe_update_info_hash_tables (struct dwarf2_debug *stash)
4127 struct comp_unit *each;
4129 /* Exit if hash tables are up-to-date. */
4130 if (stash->all_comp_units == stash->hash_units_head)
4133 if (stash->hash_units_head)
4134 each = stash->hash_units_head->prev_unit;
4136 each = stash->last_comp_unit;
4140 if (!comp_unit_hash_info (stash, each, stash->funcinfo_hash_table,
4141 stash->varinfo_hash_table))
4143 stash->info_hash_status = STASH_INFO_HASH_DISABLED;
4146 each = each->prev_unit;
4149 stash->hash_units_head = stash->all_comp_units;
4153 /* Check consistency of info hash tables. This is for debugging only. */
4155 static void ATTRIBUTE_UNUSED
4156 stash_verify_info_hash_table (struct dwarf2_debug *stash)
4158 struct comp_unit *each_unit;
4159 struct funcinfo *each_func;
4160 struct varinfo *each_var;
4161 struct info_list_node *node;
4164 for (each_unit = stash->all_comp_units;
4166 each_unit = each_unit->next_unit)
4168 for (each_func = each_unit->function_table;
4170 each_func = each_func->prev_func)
4172 if (!each_func->name)
4174 node = lookup_info_hash_table (stash->funcinfo_hash_table,
4178 while (node && !found)
4180 found = node->info == each_func;
4186 for (each_var = each_unit->variable_table;
4188 each_var = each_var->prev_var)
4190 if (!each_var->name || !each_var->file || each_var->stack)
4192 node = lookup_info_hash_table (stash->varinfo_hash_table,
4196 while (node && !found)
4198 found = node->info == each_var;
4206 /* Check to see if we want to enable the info hash tables, which consume
4207 quite a bit of memory. Currently we only check the number times
4208 bfd_dwarf2_find_line is called. In the future, we may also want to
4209 take the number of symbols into account. */
4212 stash_maybe_enable_info_hash_tables (bfd *abfd, struct dwarf2_debug *stash)
4214 BFD_ASSERT (stash->info_hash_status == STASH_INFO_HASH_OFF);
4216 if (stash->info_hash_count++ < STASH_INFO_HASH_TRIGGER)
4219 /* FIXME: Maybe we should check the reduce_memory_overheads
4220 and optimize fields in the bfd_link_info structure ? */
4222 /* Create hash tables. */
4223 stash->funcinfo_hash_table = create_info_hash_table (abfd);
4224 stash->varinfo_hash_table = create_info_hash_table (abfd);
4225 if (!stash->funcinfo_hash_table || !stash->varinfo_hash_table)
4227 /* Turn off info hashes if any allocation above fails. */
4228 stash->info_hash_status = STASH_INFO_HASH_DISABLED;
4231 /* We need a forced update so that the info hash tables will
4232 be created even though there is no compilation unit. That
4233 happens if STASH_INFO_HASH_TRIGGER is 0. */
4234 stash_maybe_update_info_hash_tables (stash);
4235 stash->info_hash_status = STASH_INFO_HASH_ON;
4238 /* Find the file and line associated with a symbol and address using the
4239 info hash tables of a stash. If there is a match, the function returns
4240 TRUE and update the locations pointed to by filename_ptr and linenumber_ptr;
4241 otherwise it returns FALSE. */
4244 stash_find_line_fast (struct dwarf2_debug *stash,
4247 const char **filename_ptr,
4248 unsigned int *linenumber_ptr)
4250 BFD_ASSERT (stash->info_hash_status == STASH_INFO_HASH_ON);
4252 if (sym->flags & BSF_FUNCTION)
4253 return info_hash_lookup_funcinfo (stash->funcinfo_hash_table, sym, addr,
4254 filename_ptr, linenumber_ptr);
4255 return info_hash_lookup_varinfo (stash->varinfo_hash_table, sym, addr,
4256 filename_ptr, linenumber_ptr);
4259 /* Save current section VMAs. */
4262 save_section_vma (const bfd *abfd, struct dwarf2_debug *stash)
4267 if (abfd->section_count == 0)
4269 stash->sec_vma = bfd_malloc (sizeof (*stash->sec_vma) * abfd->section_count);
4270 if (stash->sec_vma == NULL)
4272 for (i = 0, s = abfd->sections; i < abfd->section_count; i++, s = s->next)
4274 if (s->output_section != NULL)
4275 stash->sec_vma[i] = s->output_section->vma + s->output_offset;
4277 stash->sec_vma[i] = s->vma;
4282 /* Compare current section VMAs against those at the time the stash
4283 was created. If find_nearest_line is used in linker warnings or
4284 errors early in the link process, the debug info stash will be
4285 invalid for later calls. This is because we relocate debug info
4286 sections, so the stashed section contents depend on symbol values,
4287 which in turn depend on section VMAs. */
4290 section_vma_same (const bfd *abfd, const struct dwarf2_debug *stash)
4295 for (i = 0, s = abfd->sections; i < abfd->section_count; i++, s = s->next)
4299 if (s->output_section != NULL)
4300 vma = s->output_section->vma + s->output_offset;
4303 if (vma != stash->sec_vma[i])
4309 /* Read debug information from DEBUG_BFD when DEBUG_BFD is specified.
4310 If DEBUG_BFD is not specified, we read debug information from ABFD
4311 or its gnu_debuglink. The results will be stored in PINFO.
4312 The function returns TRUE iff debug information is ready. */
4315 _bfd_dwarf2_slurp_debug_info (bfd *abfd, bfd *debug_bfd,
4316 const struct dwarf_debug_section *debug_sections,
4319 bfd_boolean do_place)
4321 bfd_size_type amt = sizeof (struct dwarf2_debug);
4322 bfd_size_type total_size;
4324 struct dwarf2_debug *stash = (struct dwarf2_debug *) *pinfo;
4328 if (stash->orig_bfd == abfd
4329 && section_vma_same (abfd, stash))
4331 /* Check that we did previously find some debug information
4332 before attempting to make use of it. */
4333 if (stash->bfd_ptr != NULL)
4335 if (do_place && !place_sections (abfd, stash))
4342 _bfd_dwarf2_cleanup_debug_info (abfd, pinfo);
4343 memset (stash, 0, amt);
4347 stash = (struct dwarf2_debug *) bfd_zalloc (abfd, amt);
4351 stash->orig_bfd = abfd;
4352 stash->debug_sections = debug_sections;
4353 stash->syms = symbols;
4354 if (!save_section_vma (abfd, stash))
4359 if (debug_bfd == NULL)
4362 msec = find_debug_info (debug_bfd, debug_sections, NULL);
4363 if (msec == NULL && abfd == debug_bfd)
4365 char * debug_filename;
4367 debug_filename = bfd_follow_build_id_debuglink (abfd, DEBUGDIR);
4368 if (debug_filename == NULL)
4369 debug_filename = bfd_follow_gnu_debuglink (abfd, DEBUGDIR);
4371 if (debug_filename == NULL)
4372 /* No dwarf2 info, and no gnu_debuglink to follow.
4373 Note that at this point the stash has been allocated, but
4374 contains zeros. This lets future calls to this function
4375 fail more quickly. */
4378 /* Set BFD_DECOMPRESS to decompress debug sections. */
4379 if ((debug_bfd = bfd_openr (debug_filename, NULL)) == NULL
4380 || !(debug_bfd->flags |= BFD_DECOMPRESS,
4381 bfd_check_format (debug_bfd, bfd_object))
4382 || (msec = find_debug_info (debug_bfd,
4383 debug_sections, NULL)) == NULL
4384 || !bfd_generic_link_read_symbols (debug_bfd))
4387 bfd_close (debug_bfd);
4388 /* FIXME: Should we report our failure to follow the debuglink ? */
4389 free (debug_filename);
4393 symbols = bfd_get_outsymbols (debug_bfd);
4394 stash->syms = symbols;
4395 stash->close_on_cleanup = TRUE;
4397 stash->bfd_ptr = debug_bfd;
4400 && !place_sections (abfd, stash))
4403 /* There can be more than one DWARF2 info section in a BFD these
4404 days. First handle the easy case when there's only one. If
4405 there's more than one, try case two: none of the sections is
4406 compressed. In that case, read them all in and produce one
4407 large stash. We do this in two passes - in the first pass we
4408 just accumulate the section sizes, and in the second pass we
4409 read in the section's contents. (The allows us to avoid
4410 reallocing the data as we add sections to the stash.) If
4411 some or all sections are compressed, then do things the slow
4412 way, with a bunch of reallocs. */
4414 if (! find_debug_info (debug_bfd, debug_sections, msec))
4416 /* Case 1: only one info section. */
4417 total_size = msec->size;
4418 if (! read_section (debug_bfd, &stash->debug_sections[debug_info],
4420 &stash->info_ptr_memory, &total_size))
4425 /* Case 2: multiple sections. */
4426 for (total_size = 0;
4428 msec = find_debug_info (debug_bfd, debug_sections, msec))
4429 total_size += msec->size;
4431 stash->info_ptr_memory = (bfd_byte *) bfd_malloc (total_size);
4432 if (stash->info_ptr_memory == NULL)
4436 for (msec = find_debug_info (debug_bfd, debug_sections, NULL);
4438 msec = find_debug_info (debug_bfd, debug_sections, msec))
4446 if (!(bfd_simple_get_relocated_section_contents
4447 (debug_bfd, msec, stash->info_ptr_memory + total_size,
4455 stash->info_ptr = stash->info_ptr_memory;
4456 stash->info_ptr_end = stash->info_ptr + total_size;
4457 stash->sec = find_debug_info (debug_bfd, debug_sections, NULL);
4458 stash->sec_info_ptr = stash->info_ptr;
4462 /* Scan the debug information in PINFO looking for a DW_TAG_subprogram
4463 abbrev with a DW_AT_low_pc attached to it. Then lookup that same
4464 symbol in SYMBOLS and return the difference between the low_pc and
4465 the symbol's address. Returns 0 if no suitable symbol could be found. */
4468 _bfd_dwarf2_find_symbol_bias (asymbol ** symbols, void ** pinfo)
4470 struct dwarf2_debug *stash;
4471 struct comp_unit * unit;
4473 stash = (struct dwarf2_debug *) *pinfo;
4478 for (unit = stash->all_comp_units; unit; unit = unit->next_unit)
4480 struct funcinfo * func;
4482 if (unit->function_table == NULL)
4484 if (unit->line_table == NULL)
4485 unit->line_table = decode_line_info (unit, stash);
4486 if (unit->line_table != NULL)
4487 scan_unit_for_symbols (unit);
4490 for (func = unit->function_table; func != NULL; func = func->prev_func)
4491 if (func->name && func->arange.low)
4495 /* FIXME: Do we need to scan the aranges looking for the lowest pc value ? */
4497 for (psym = symbols; * psym != NULL; psym++)
4499 asymbol * sym = * psym;
4501 if (sym->flags & BSF_FUNCTION
4502 && sym->section != NULL
4503 && strcmp (sym->name, func->name) == 0)
4504 return ((bfd_signed_vma) func->arange.low) -
4505 ((bfd_signed_vma) (sym->value + sym->section->vma));
4513 /* Find the source code location of SYMBOL. If SYMBOL is NULL
4514 then find the nearest source code location corresponding to
4515 the address SECTION + OFFSET.
4516 Returns TRUE if the line is found without error and fills in
4517 FILENAME_PTR and LINENUMBER_PTR. In the case where SYMBOL was
4518 NULL the FUNCTIONNAME_PTR is also filled in.
4519 SYMBOLS contains the symbol table for ABFD.
4520 DEBUG_SECTIONS contains the name of the dwarf debug sections.
4521 ADDR_SIZE is the number of bytes in the initial .debug_info length
4522 field and in the abbreviation offset, or zero to indicate that the
4523 default value should be used. */
4526 _bfd_dwarf2_find_nearest_line (bfd *abfd,
4531 const char **filename_ptr,
4532 const char **functionname_ptr,
4533 unsigned int *linenumber_ptr,
4534 unsigned int *discriminator_ptr,
4535 const struct dwarf_debug_section *debug_sections,
4536 unsigned int addr_size,
4539 /* Read each compilation unit from the section .debug_info, and check
4540 to see if it contains the address we are searching for. If yes,
4541 lookup the address, and return the line number info. If no, go
4542 on to the next compilation unit.
4544 We keep a list of all the previously read compilation units, and
4545 a pointer to the next un-read compilation unit. Check the
4546 previously read units before reading more. */
4547 struct dwarf2_debug *stash;
4548 /* What address are we looking for? */
4550 struct comp_unit* each;
4551 struct funcinfo *function = NULL;
4552 bfd_boolean found = FALSE;
4553 bfd_boolean do_line;
4555 *filename_ptr = NULL;
4556 if (functionname_ptr != NULL)
4557 *functionname_ptr = NULL;
4558 *linenumber_ptr = 0;
4559 if (discriminator_ptr)
4560 *discriminator_ptr = 0;
4562 if (! _bfd_dwarf2_slurp_debug_info (abfd, NULL, debug_sections,
4564 (abfd->flags & (EXEC_P | DYNAMIC)) == 0))
4567 stash = (struct dwarf2_debug *) *pinfo;
4569 do_line = symbol != NULL;
4572 BFD_ASSERT (section == NULL && offset == 0 && functionname_ptr == NULL);
4573 section = bfd_get_section (symbol);
4574 addr = symbol->value;
4578 BFD_ASSERT (section != NULL && functionname_ptr != NULL);
4581 /* If we have no SYMBOL but the section we're looking at is not a
4582 code section, then take a look through the list of symbols to see
4583 if we have a symbol at the address we're looking for. If we do
4584 then use this to look up line information. This will allow us to
4585 give file and line results for data symbols. We exclude code
4586 symbols here, if we look up a function symbol and then look up the
4587 line information we'll actually return the line number for the
4588 opening '{' rather than the function definition line. This is
4589 because looking up by symbol uses the line table, in which the
4590 first line for a function is usually the opening '{', while
4591 looking up the function by section + offset uses the
4592 DW_AT_decl_line from the function DW_TAG_subprogram for the line,
4593 which will be the line of the function name. */
4594 if (symbols != NULL && (section->flags & SEC_CODE) == 0)
4598 for (tmp = symbols; (*tmp) != NULL; ++tmp)
4599 if ((*tmp)->the_bfd == abfd
4600 && (*tmp)->section == section
4601 && (*tmp)->value == offset
4602 && ((*tmp)->flags & BSF_SECTION_SYM) == 0)
4606 /* For local symbols, keep going in the hope we find a
4608 if ((symbol->flags & BSF_GLOBAL) != 0)
4614 if (section->output_section)
4615 addr += section->output_section->vma + section->output_offset;
4617 addr += section->vma;
4619 /* A null info_ptr indicates that there is no dwarf2 info
4620 (or that an error occured while setting up the stash). */
4621 if (! stash->info_ptr)
4624 stash->inliner_chain = NULL;
4626 /* Check the previously read comp. units first. */
4629 /* The info hash tables use quite a bit of memory. We may not want to
4630 always use them. We use some heuristics to decide if and when to
4632 if (stash->info_hash_status == STASH_INFO_HASH_OFF)
4633 stash_maybe_enable_info_hash_tables (abfd, stash);
4635 /* Keep info hash table up to date if they are available. Note that we
4636 may disable the hash tables if there is any error duing update. */
4637 if (stash->info_hash_status == STASH_INFO_HASH_ON)
4638 stash_maybe_update_info_hash_tables (stash);
4640 if (stash->info_hash_status == STASH_INFO_HASH_ON)
4642 found = stash_find_line_fast (stash, symbol, addr, filename_ptr,
4649 /* Check the previously read comp. units first. */
4650 for (each = stash->all_comp_units; each; each = each->next_unit)
4651 if ((symbol->flags & BSF_FUNCTION) == 0
4652 || each->arange.high == 0
4653 || comp_unit_contains_address (each, addr))
4655 found = comp_unit_find_line (each, symbol, addr, filename_ptr,
4656 linenumber_ptr, stash);
4664 bfd_vma min_range = (bfd_vma) -1;
4665 const char * local_filename = NULL;
4666 struct funcinfo *local_function = NULL;
4667 unsigned int local_linenumber = 0;
4668 unsigned int local_discriminator = 0;
4670 for (each = stash->all_comp_units; each; each = each->next_unit)
4672 bfd_vma range = (bfd_vma) -1;
4674 found = ((each->arange.high == 0
4675 || comp_unit_contains_address (each, addr))
4676 && (range = comp_unit_find_nearest_line (each, addr,
4680 & local_discriminator,
4684 /* PRs 15935 15994: Bogus debug information may have provided us
4685 with an erroneous match. We attempt to counter this by
4686 selecting the match that has the smallest address range
4687 associated with it. (We are assuming that corrupt debug info
4688 will tend to result in extra large address ranges rather than
4689 extra small ranges).
4691 This does mean that we scan through all of the CUs associated
4692 with the bfd each time this function is called. But this does
4693 have the benefit of producing consistent results every time the
4694 function is called. */
4695 if (range <= min_range)
4697 if (filename_ptr && local_filename)
4698 * filename_ptr = local_filename;
4700 function = local_function;
4701 if (discriminator_ptr && local_discriminator)
4702 * discriminator_ptr = local_discriminator;
4703 if (local_linenumber)
4704 * linenumber_ptr = local_linenumber;
4710 if (* linenumber_ptr)
4717 /* The DWARF2 spec says that the initial length field, and the
4718 offset of the abbreviation table, should both be 4-byte values.
4719 However, some compilers do things differently. */
4722 BFD_ASSERT (addr_size == 4 || addr_size == 8);
4724 /* Read each remaining comp. units checking each as they are read. */
4725 while (stash->info_ptr < stash->info_ptr_end)
4728 unsigned int offset_size = addr_size;
4729 bfd_byte *info_ptr_unit = stash->info_ptr;
4731 length = read_4_bytes (stash->bfd_ptr, stash->info_ptr, stash->info_ptr_end);
4732 /* A 0xffffff length is the DWARF3 way of indicating
4733 we use 64-bit offsets, instead of 32-bit offsets. */
4734 if (length == 0xffffffff)
4737 length = read_8_bytes (stash->bfd_ptr, stash->info_ptr + 4, stash->info_ptr_end);
4738 stash->info_ptr += 12;
4740 /* A zero length is the IRIX way of indicating 64-bit offsets,
4741 mostly because the 64-bit length will generally fit in 32
4742 bits, and the endianness helps. */
4743 else if (length == 0)
4746 length = read_4_bytes (stash->bfd_ptr, stash->info_ptr + 4, stash->info_ptr_end);
4747 stash->info_ptr += 8;
4749 /* In the absence of the hints above, we assume 32-bit DWARF2
4750 offsets even for targets with 64-bit addresses, because:
4751 a) most of the time these targets will not have generated
4752 more than 2Gb of debug info and so will not need 64-bit
4755 b) if they do use 64-bit offsets but they are not using
4756 the size hints that are tested for above then they are
4757 not conforming to the DWARF3 standard anyway. */
4758 else if (addr_size == 8)
4761 stash->info_ptr += 4;
4764 stash->info_ptr += 4;
4771 if (stash->info_ptr + length > stash->info_ptr_end)
4774 each = parse_comp_unit (stash, length, info_ptr_unit,
4777 /* The dwarf information is damaged, don't trust it any
4781 new_ptr = stash->info_ptr + length;
4782 /* PR 17512: file: 1500698c. */
4783 if (new_ptr < stash->info_ptr)
4785 /* A corrupt length value - do not trust the info any more. */
4790 stash->info_ptr = new_ptr;
4792 if (stash->all_comp_units)
4793 stash->all_comp_units->prev_unit = each;
4795 stash->last_comp_unit = each;
4797 each->next_unit = stash->all_comp_units;
4798 stash->all_comp_units = each;
4800 /* DW_AT_low_pc and DW_AT_high_pc are optional for
4801 compilation units. If we don't have them (i.e.,
4802 unit->high == 0), we need to consult the line info table
4803 to see if a compilation unit contains the given
4806 found = (((symbol->flags & BSF_FUNCTION) == 0
4807 || each->arange.high == 0
4808 || comp_unit_contains_address (each, addr))
4809 && comp_unit_find_line (each, symbol, addr,
4814 found = ((each->arange.high == 0
4815 || comp_unit_contains_address (each, addr))
4816 && comp_unit_find_nearest_line (each, addr,
4823 if ((bfd_vma) (stash->info_ptr - stash->sec_info_ptr)
4824 == stash->sec->size)
4826 stash->sec = find_debug_info (stash->bfd_ptr, debug_sections,
4828 stash->sec_info_ptr = stash->info_ptr;
4839 if (!function->is_linkage)
4844 fun = _bfd_elf_find_function (abfd, symbols, section, offset,
4845 *filename_ptr ? NULL : filename_ptr,
4847 sec_vma = section->vma;
4848 if (section->output_section != NULL)
4849 sec_vma = section->output_section->vma + section->output_offset;
4851 && fun->value + sec_vma == function->arange.low)
4852 function->name = *functionname_ptr;
4853 /* Even if we didn't find a linkage name, say that we have
4854 to stop a repeated search of symbols. */
4855 function->is_linkage = TRUE;
4857 *functionname_ptr = function->name;
4859 if ((abfd->flags & (EXEC_P | DYNAMIC)) == 0)
4860 unset_sections (stash);
4866 _bfd_dwarf2_find_inliner_info (bfd *abfd ATTRIBUTE_UNUSED,
4867 const char **filename_ptr,
4868 const char **functionname_ptr,
4869 unsigned int *linenumber_ptr,
4872 struct dwarf2_debug *stash;
4874 stash = (struct dwarf2_debug *) *pinfo;
4877 struct funcinfo *func = stash->inliner_chain;
4879 if (func && func->caller_func)
4881 *filename_ptr = func->caller_file;
4882 *functionname_ptr = func->caller_func->name;
4883 *linenumber_ptr = func->caller_line;
4884 stash->inliner_chain = func->caller_func;
4893 _bfd_dwarf2_cleanup_debug_info (bfd *abfd, void **pinfo)
4895 struct dwarf2_debug *stash = (struct dwarf2_debug *) *pinfo;
4896 struct comp_unit *each;
4898 if (abfd == NULL || stash == NULL)
4901 for (each = stash->all_comp_units; each; each = each->next_unit)
4903 struct abbrev_info **abbrevs = each->abbrevs;
4904 struct funcinfo *function_table = each->function_table;
4905 struct varinfo *variable_table = each->variable_table;
4908 for (i = 0; i < ABBREV_HASH_SIZE; i++)
4910 struct abbrev_info *abbrev = abbrevs[i];
4914 free (abbrev->attrs);
4915 abbrev = abbrev->next;
4919 if (each->line_table)
4921 free (each->line_table->dirs);
4922 free (each->line_table->files);
4925 while (function_table)
4927 if (function_table->file)
4929 free (function_table->file);
4930 function_table->file = NULL;
4933 if (function_table->caller_file)
4935 free (function_table->caller_file);
4936 function_table->caller_file = NULL;
4938 function_table = function_table->prev_func;
4941 if (each->lookup_funcinfo_table)
4943 free (each->lookup_funcinfo_table);
4944 each->lookup_funcinfo_table = NULL;
4947 while (variable_table)
4949 if (variable_table->file)
4951 free (variable_table->file);
4952 variable_table->file = NULL;
4955 variable_table = variable_table->prev_var;
4959 if (stash->funcinfo_hash_table)
4960 bfd_hash_table_free (&stash->funcinfo_hash_table->base);
4961 if (stash->varinfo_hash_table)
4962 bfd_hash_table_free (&stash->varinfo_hash_table->base);
4963 if (stash->dwarf_abbrev_buffer)
4964 free (stash->dwarf_abbrev_buffer);
4965 if (stash->dwarf_line_buffer)
4966 free (stash->dwarf_line_buffer);
4967 if (stash->dwarf_str_buffer)
4968 free (stash->dwarf_str_buffer);
4969 if (stash->dwarf_line_str_buffer)
4970 free (stash->dwarf_line_str_buffer);
4971 if (stash->dwarf_ranges_buffer)
4972 free (stash->dwarf_ranges_buffer);
4973 if (stash->info_ptr_memory)
4974 free (stash->info_ptr_memory);
4975 if (stash->close_on_cleanup)
4976 bfd_close (stash->bfd_ptr);
4977 if (stash->alt_dwarf_str_buffer)
4978 free (stash->alt_dwarf_str_buffer);
4979 if (stash->alt_dwarf_info_buffer)
4980 free (stash->alt_dwarf_info_buffer);
4982 free (stash->sec_vma);
4983 if (stash->adjusted_sections)
4984 free (stash->adjusted_sections);
4985 if (stash->alt_bfd_ptr)
4986 bfd_close (stash->alt_bfd_ptr);
4989 /* Find the function to a particular section and offset,
4990 for error reporting. */
4993 _bfd_elf_find_function (bfd *abfd,
4997 const char **filename_ptr,
4998 const char **functionname_ptr)
5000 struct elf_find_function_cache
5002 asection *last_section;
5004 const char *filename;
5005 bfd_size_type func_size;
5008 if (symbols == NULL)
5011 if (bfd_get_flavour (abfd) != bfd_target_elf_flavour)
5014 cache = elf_tdata (abfd)->elf_find_function_cache;
5017 cache = bfd_zalloc (abfd, sizeof (*cache));
5018 elf_tdata (abfd)->elf_find_function_cache = cache;
5022 if (cache->last_section != section
5023 || cache->func == NULL
5024 || offset < cache->func->value
5025 || offset >= cache->func->value + cache->func_size)
5030 /* ??? Given multiple file symbols, it is impossible to reliably
5031 choose the right file name for global symbols. File symbols are
5032 local symbols, and thus all file symbols must sort before any
5033 global symbols. The ELF spec may be interpreted to say that a
5034 file symbol must sort before other local symbols, but currently
5035 ld -r doesn't do this. So, for ld -r output, it is possible to
5036 make a better choice of file name for local symbols by ignoring
5037 file symbols appearing after a given local symbol. */
5038 enum { nothing_seen, symbol_seen, file_after_symbol_seen } state;
5039 const struct elf_backend_data *bed = get_elf_backend_data (abfd);
5043 state = nothing_seen;
5044 cache->filename = NULL;
5046 cache->func_size = 0;
5047 cache->last_section = section;
5049 for (p = symbols; *p != NULL; p++)
5055 if ((sym->flags & BSF_FILE) != 0)
5058 if (state == symbol_seen)
5059 state = file_after_symbol_seen;
5063 size = bed->maybe_function_sym (sym, section, &code_off);
5065 && code_off <= offset
5066 && (code_off > low_func
5067 || (code_off == low_func
5068 && size > cache->func_size)))
5071 cache->func_size = size;
5072 cache->filename = NULL;
5073 low_func = code_off;
5075 && ((sym->flags & BSF_LOCAL) != 0
5076 || state != file_after_symbol_seen))
5077 cache->filename = bfd_asymbol_name (file);
5079 if (state == nothing_seen)
5080 state = symbol_seen;
5084 if (cache->func == NULL)
5088 *filename_ptr = cache->filename;
5089 if (functionname_ptr)
5090 *functionname_ptr = bfd_asymbol_name (cache->func);