2 Copyright 1994, 1995, 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003,
3 2004, 2005, 2006, 2007, 2008, 2009 Free Software Foundation, Inc.
5 Adapted from gdb/dwarf2read.c by Gavin Koch of Cygnus Solutions
8 From the dwarf2read.c header:
9 Adapted by Gary Funck (gary@intrepid.com), Intrepid Technology,
10 Inc. with support from Florida State University (under contract
11 with the Ada Joint Program Office), and Silicon Graphics, Inc.
12 Initial contribution by Brent Benson, Harris Computer Systems, Inc.,
13 based on Fred Fish's (Cygnus Support) implementation of DWARF 1
14 support in dwarfread.c
16 This file is part of BFD.
18 This program is free software; you can redistribute it and/or modify
19 it under the terms of the GNU General Public License as published by
20 the Free Software Foundation; either version 3 of the License, or (at
21 your option) any later version.
23 This program is distributed in the hope that it will be useful, but
24 WITHOUT ANY WARRANTY; without even the implied warranty of
25 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
26 General Public License for more details.
28 You should have received a copy of the GNU General Public License
29 along with this program; if not, write to the Free Software
30 Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
31 MA 02110-1301, USA. */
35 #include "libiberty.h"
40 /* The data in the .debug_line statement prologue looks like this. */
45 unsigned short version;
46 bfd_vma prologue_length;
47 unsigned char minimum_instruction_length;
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 /* The next unread compilation unit within the .debug_info section.
93 Zero indicates that the .debug_info section has not been loaded
97 /* Pointer to the end of the .debug_info section memory buffer. */
98 bfd_byte *info_ptr_end;
100 /* Pointer to the bfd, section and address of the beginning of the
101 section. The bfd might be different than expected because of
102 gnu_debuglink sections. */
105 bfd_byte *sec_info_ptr;
107 /* A pointer to the memory block allocated for info_ptr. Neither
108 info_ptr nor sec_info_ptr are guaranteed to stay pointing to the
109 beginning of the malloc block. This is used only to free the
111 bfd_byte *info_ptr_memory;
113 /* Pointer to the symbol table. */
116 /* Pointer to the .debug_abbrev section loaded into memory. */
117 bfd_byte *dwarf_abbrev_buffer;
119 /* Length of the loaded .debug_abbrev section. */
120 bfd_size_type dwarf_abbrev_size;
122 /* Buffer for decode_line_info. */
123 bfd_byte *dwarf_line_buffer;
125 /* Length of the loaded .debug_line section. */
126 bfd_size_type dwarf_line_size;
128 /* Pointer to the .debug_str section loaded into memory. */
129 bfd_byte *dwarf_str_buffer;
131 /* Length of the loaded .debug_str section. */
132 bfd_size_type dwarf_str_size;
134 /* Pointer to the .debug_ranges section loaded into memory. */
135 bfd_byte *dwarf_ranges_buffer;
137 /* Length of the loaded .debug_ranges section. */
138 bfd_size_type dwarf_ranges_size;
140 /* If the most recent call to bfd_find_nearest_line was given an
141 address in an inlined function, preserve a pointer into the
142 calling chain for subsequent calls to bfd_find_inliner_info to
144 struct funcinfo *inliner_chain;
146 /* Number of sections whose VMA we must adjust. */
147 unsigned int adjusted_section_count;
149 /* Array of sections with adjusted VMA. */
150 struct adjusted_section *adjusted_sections;
152 /* Number of times find_line is called. This is used in
153 the heuristic for enabling the info hash tables. */
156 #define STASH_INFO_HASH_TRIGGER 100
158 /* Hash table mapping symbol names to function infos. */
159 struct info_hash_table *funcinfo_hash_table;
161 /* Hash table mapping symbol names to variable infos. */
162 struct info_hash_table *varinfo_hash_table;
164 /* Head of comp_unit list in the last hash table update. */
165 struct comp_unit *hash_units_head;
167 /* Status of info hash. */
168 int info_hash_status;
169 #define STASH_INFO_HASH_OFF 0
170 #define STASH_INFO_HASH_ON 1
171 #define STASH_INFO_HASH_DISABLED 2
181 /* A minimal decoding of DWARF2 compilation units. We only decode
182 what's needed to get to the line number information. */
186 /* Chain the previously read compilation units. */
187 struct comp_unit *next_unit;
189 /* Likewise, chain the compilation unit read after this one.
190 The comp units are stored in reversed reading order. */
191 struct comp_unit *prev_unit;
193 /* Keep the bfd convenient (for memory allocation). */
196 /* The lowest and highest addresses contained in this compilation
197 unit as specified in the compilation unit header. */
198 struct arange arange;
200 /* The DW_AT_name attribute (for error messages). */
203 /* The abbrev hash table. */
204 struct abbrev_info **abbrevs;
206 /* Note that an error was found by comp_unit_find_nearest_line. */
209 /* The DW_AT_comp_dir attribute. */
212 /* TRUE if there is a line number table associated with this comp. unit. */
215 /* Pointer to the current comp_unit so that we can find a given entry
217 bfd_byte *info_ptr_unit;
219 /* The offset into .debug_line of the line number table. */
220 unsigned long line_offset;
222 /* Pointer to the first child die for the comp unit. */
223 bfd_byte *first_child_die_ptr;
225 /* The end of the comp unit. */
228 /* The decoded line number, NULL if not yet decoded. */
229 struct line_info_table *line_table;
231 /* A list of the functions found in this comp. unit. */
232 struct funcinfo *function_table;
234 /* A list of the variables found in this comp. unit. */
235 struct varinfo *variable_table;
237 /* Pointer to dwarf2_debug structure. */
238 struct dwarf2_debug *stash;
240 /* DWARF format version for this unit - from unit header. */
243 /* Address size for this unit - from unit header. */
244 unsigned char addr_size;
246 /* Offset size for this unit - from unit header. */
247 unsigned char offset_size;
249 /* Base address for this unit - from DW_AT_low_pc attribute of
250 DW_TAG_compile_unit DIE */
251 bfd_vma base_address;
253 /* TRUE if symbols are cached in hash table for faster lookup by name. */
257 /* This data structure holds the information of an abbrev. */
260 unsigned int number; /* Number identifying abbrev. */
261 enum dwarf_tag tag; /* DWARF tag. */
262 int has_children; /* Boolean. */
263 unsigned int num_attrs; /* Number of attributes. */
264 struct attr_abbrev *attrs; /* An array of attribute descriptions. */
265 struct abbrev_info *next; /* Next in chain. */
270 enum dwarf_attribute name;
271 enum dwarf_form form;
274 #ifndef ABBREV_HASH_SIZE
275 #define ABBREV_HASH_SIZE 121
277 #ifndef ATTR_ALLOC_CHUNK
278 #define ATTR_ALLOC_CHUNK 4
281 /* Variable and function hash tables. This is used to speed up look-up
282 in lookup_symbol_in_var_table() and lookup_symbol_in_function_table().
283 In order to share code between variable and function infos, we use
284 a list of untyped pointer for all variable/function info associated with
285 a symbol. We waste a bit of memory for list with one node but that
286 simplifies the code. */
288 struct info_list_node
290 struct info_list_node *next;
294 /* Info hash entry. */
295 struct info_hash_entry
297 struct bfd_hash_entry root;
298 struct info_list_node *head;
301 struct info_hash_table
303 struct bfd_hash_table base;
306 /* Function to create a new entry in info hash table. */
308 static struct bfd_hash_entry *
309 info_hash_table_newfunc (struct bfd_hash_entry *entry,
310 struct bfd_hash_table *table,
313 struct info_hash_entry *ret = (struct info_hash_entry *) entry;
315 /* Allocate the structure if it has not already been allocated by a
319 ret = (struct info_hash_entry *) bfd_hash_allocate (table,
325 /* Call the allocation method of the base class. */
326 ret = ((struct info_hash_entry *)
327 bfd_hash_newfunc ((struct bfd_hash_entry *) ret, table, string));
329 /* Initialize the local fields here. */
333 return (struct bfd_hash_entry *) ret;
336 /* Function to create a new info hash table. It returns a pointer to the
337 newly created table or NULL if there is any error. We need abfd
338 solely for memory allocation. */
340 static struct info_hash_table *
341 create_info_hash_table (bfd *abfd)
343 struct info_hash_table *hash_table;
345 hash_table = (struct info_hash_table *)
346 bfd_alloc (abfd, sizeof (struct info_hash_table));
350 if (!bfd_hash_table_init (&hash_table->base, info_hash_table_newfunc,
351 sizeof (struct info_hash_entry)))
353 bfd_release (abfd, hash_table);
360 /* Insert an info entry into an info hash table. We do not check of
361 duplicate entries. Also, the caller need to guarantee that the
362 right type of info in inserted as info is passed as a void* pointer.
363 This function returns true if there is no error. */
366 insert_info_hash_table (struct info_hash_table *hash_table,
371 struct info_hash_entry *entry;
372 struct info_list_node *node;
374 entry = (struct info_hash_entry*) bfd_hash_lookup (&hash_table->base,
379 node = (struct info_list_node *) bfd_hash_allocate (&hash_table->base,
385 node->next = entry->head;
391 /* Look up an info entry list from an info hash table. Return NULL
394 static struct info_list_node *
395 lookup_info_hash_table (struct info_hash_table *hash_table, const char *key)
397 struct info_hash_entry *entry;
399 entry = (struct info_hash_entry*) bfd_hash_lookup (&hash_table->base, key,
401 return entry ? entry->head : NULL;
404 /* Read a section into its appropriate place in the dwarf2_debug
405 struct (indicated by SECTION_BUFFER and SECTION_SIZE). If SYMS is
406 not NULL, use bfd_simple_get_relocated_section_contents to read the
407 section contents, otherwise use bfd_get_section_contents. Fail if
408 the located section does not contain at least OFFSET bytes. */
411 read_section (bfd * abfd,
412 const char * section_name,
413 const char * compressed_section_name,
416 bfd_byte ** section_buffer,
417 bfd_size_type * section_size)
420 bfd_boolean section_is_compressed = FALSE;
422 /* read_section is a noop if the section has already been read. */
423 if (!*section_buffer)
425 msec = bfd_get_section_by_name (abfd, section_name);
426 if (! msec && compressed_section_name)
428 msec = bfd_get_section_by_name (abfd, compressed_section_name);
429 section_is_compressed = TRUE;
433 (*_bfd_error_handler) (_("Dwarf Error: Can't find %s section."), section_name);
434 bfd_set_error (bfd_error_bad_value);
438 *section_size = msec->rawsize ? msec->rawsize : msec->size;
442 = bfd_simple_get_relocated_section_contents (abfd, msec, NULL, syms);
443 if (! *section_buffer)
448 *section_buffer = (bfd_byte *) bfd_malloc (*section_size);
449 if (! *section_buffer)
451 if (! bfd_get_section_contents (abfd, msec, *section_buffer,
456 if (section_is_compressed)
458 if (! bfd_uncompress_section_contents (section_buffer, section_size))
460 (*_bfd_error_handler) (_("Dwarf Error: unable to decompress %s section."), compressed_section_name);
461 bfd_set_error (bfd_error_bad_value);
467 /* It is possible to get a bad value for the offset into the section
468 that the client wants. Validate it here to avoid trouble later. */
469 if (offset != 0 && offset >= *section_size)
471 (*_bfd_error_handler) (_("Dwarf Error: Offset (%lu) greater than or equal to %s size (%lu)."),
472 (long) offset, section_name, *section_size);
473 bfd_set_error (bfd_error_bad_value);
481 The following function up to the END VERBATIM mark are
482 copied directly from dwarf2read.c. */
484 /* Read dwarf information from a buffer. */
487 read_1_byte (bfd *abfd ATTRIBUTE_UNUSED, bfd_byte *buf)
489 return bfd_get_8 (abfd, buf);
493 read_1_signed_byte (bfd *abfd ATTRIBUTE_UNUSED, bfd_byte *buf)
495 return bfd_get_signed_8 (abfd, buf);
499 read_2_bytes (bfd *abfd, bfd_byte *buf)
501 return bfd_get_16 (abfd, buf);
505 read_4_bytes (bfd *abfd, bfd_byte *buf)
507 return bfd_get_32 (abfd, buf);
511 read_8_bytes (bfd *abfd, bfd_byte *buf)
513 return bfd_get_64 (abfd, buf);
517 read_n_bytes (bfd *abfd ATTRIBUTE_UNUSED,
519 unsigned int size ATTRIBUTE_UNUSED)
525 read_string (bfd *abfd ATTRIBUTE_UNUSED,
527 unsigned int *bytes_read_ptr)
529 /* Return a pointer to the embedded string. */
530 char *str = (char *) buf;
538 *bytes_read_ptr = strlen (str) + 1;
545 read_indirect_string (struct comp_unit * unit,
547 unsigned int * bytes_read_ptr)
550 struct dwarf2_debug *stash = unit->stash;
553 if (unit->offset_size == 4)
554 offset = read_4_bytes (unit->abfd, buf);
556 offset = read_8_bytes (unit->abfd, buf);
558 *bytes_read_ptr = unit->offset_size;
560 if (! read_section (unit->abfd, ".debug_str", ".zdebug_str",
562 &stash->dwarf_str_buffer, &stash->dwarf_str_size))
565 str = (char *) stash->dwarf_str_buffer + offset;
572 read_address (struct comp_unit *unit, bfd_byte *buf)
574 int signed_vma = get_elf_backend_data (unit->abfd)->sign_extend_vma;
578 switch (unit->addr_size)
581 return bfd_get_signed_64 (unit->abfd, buf);
583 return bfd_get_signed_32 (unit->abfd, buf);
585 return bfd_get_signed_16 (unit->abfd, buf);
592 switch (unit->addr_size)
595 return bfd_get_64 (unit->abfd, buf);
597 return bfd_get_32 (unit->abfd, buf);
599 return bfd_get_16 (unit->abfd, buf);
606 /* Lookup an abbrev_info structure in the abbrev hash table. */
608 static struct abbrev_info *
609 lookup_abbrev (unsigned int number, struct abbrev_info **abbrevs)
611 unsigned int hash_number;
612 struct abbrev_info *abbrev;
614 hash_number = number % ABBREV_HASH_SIZE;
615 abbrev = abbrevs[hash_number];
619 if (abbrev->number == number)
622 abbrev = abbrev->next;
628 /* In DWARF version 2, the description of the debugging information is
629 stored in a separate .debug_abbrev section. Before we read any
630 dies from a section we read in all abbreviations and install them
633 static struct abbrev_info**
634 read_abbrevs (bfd *abfd, bfd_uint64_t offset, struct dwarf2_debug *stash)
636 struct abbrev_info **abbrevs;
637 bfd_byte *abbrev_ptr;
638 struct abbrev_info *cur_abbrev;
639 unsigned int abbrev_number, bytes_read, abbrev_name;
640 unsigned int abbrev_form, hash_number;
643 if (! read_section (abfd, ".debug_abbrev", ".zdebug_abbrev",
645 &stash->dwarf_abbrev_buffer, &stash->dwarf_abbrev_size))
648 amt = sizeof (struct abbrev_info*) * ABBREV_HASH_SIZE;
649 abbrevs = (struct abbrev_info **) bfd_zalloc (abfd, amt);
651 abbrev_ptr = stash->dwarf_abbrev_buffer + offset;
652 abbrev_number = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
653 abbrev_ptr += bytes_read;
655 /* Loop until we reach an abbrev number of 0. */
656 while (abbrev_number)
658 amt = sizeof (struct abbrev_info);
659 cur_abbrev = (struct abbrev_info *) bfd_zalloc (abfd, amt);
661 /* Read in abbrev header. */
662 cur_abbrev->number = abbrev_number;
663 cur_abbrev->tag = (enum dwarf_tag)
664 read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
665 abbrev_ptr += bytes_read;
666 cur_abbrev->has_children = read_1_byte (abfd, abbrev_ptr);
669 /* Now read in declarations. */
670 abbrev_name = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
671 abbrev_ptr += bytes_read;
672 abbrev_form = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
673 abbrev_ptr += bytes_read;
677 if ((cur_abbrev->num_attrs % ATTR_ALLOC_CHUNK) == 0)
679 struct attr_abbrev *tmp;
681 amt = cur_abbrev->num_attrs + ATTR_ALLOC_CHUNK;
682 amt *= sizeof (struct attr_abbrev);
683 tmp = (struct attr_abbrev *) bfd_realloc (cur_abbrev->attrs, amt);
688 for (i = 0; i < ABBREV_HASH_SIZE; i++)
690 struct abbrev_info *abbrev = abbrevs[i];
694 free (abbrev->attrs);
695 abbrev = abbrev->next;
700 cur_abbrev->attrs = tmp;
703 cur_abbrev->attrs[cur_abbrev->num_attrs].name
704 = (enum dwarf_attribute) abbrev_name;
705 cur_abbrev->attrs[cur_abbrev->num_attrs++].form
706 = (enum dwarf_form) abbrev_form;
707 abbrev_name = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
708 abbrev_ptr += bytes_read;
709 abbrev_form = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
710 abbrev_ptr += bytes_read;
713 hash_number = abbrev_number % ABBREV_HASH_SIZE;
714 cur_abbrev->next = abbrevs[hash_number];
715 abbrevs[hash_number] = cur_abbrev;
717 /* Get next abbreviation.
718 Under Irix6 the abbreviations for a compilation unit are not
719 always properly terminated with an abbrev number of 0.
720 Exit loop if we encounter an abbreviation which we have
721 already read (which means we are about to read the abbreviations
722 for the next compile unit) or if the end of the abbreviation
724 if ((unsigned int) (abbrev_ptr - stash->dwarf_abbrev_buffer)
725 >= stash->dwarf_abbrev_size)
727 abbrev_number = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
728 abbrev_ptr += bytes_read;
729 if (lookup_abbrev (abbrev_number,abbrevs) != NULL)
736 /* Read an attribute value described by an attribute form. */
739 read_attribute_value (struct attribute *attr,
741 struct comp_unit *unit,
744 bfd *abfd = unit->abfd;
745 unsigned int bytes_read;
746 struct dwarf_block *blk;
749 attr->form = (enum dwarf_form) form;
753 case DW_FORM_ref_addr:
754 /* DW_FORM_ref_addr is an address in DWARF2, and an offset in
756 if (unit->version == 3)
758 if (unit->offset_size == 4)
759 attr->u.val = read_4_bytes (unit->abfd, info_ptr);
761 attr->u.val = read_8_bytes (unit->abfd, info_ptr);
762 info_ptr += unit->offset_size;
767 attr->u.val = read_address (unit, info_ptr);
768 info_ptr += unit->addr_size;
771 amt = sizeof (struct dwarf_block);
772 blk = (struct dwarf_block *) bfd_alloc (abfd, amt);
773 blk->size = read_2_bytes (abfd, info_ptr);
775 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
776 info_ptr += blk->size;
780 amt = sizeof (struct dwarf_block);
781 blk = (struct dwarf_block *) bfd_alloc (abfd, amt);
782 blk->size = read_4_bytes (abfd, info_ptr);
784 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
785 info_ptr += blk->size;
789 attr->u.val = read_2_bytes (abfd, info_ptr);
793 attr->u.val = read_4_bytes (abfd, info_ptr);
797 attr->u.val = read_8_bytes (abfd, info_ptr);
801 attr->u.str = read_string (abfd, info_ptr, &bytes_read);
802 info_ptr += bytes_read;
805 attr->u.str = read_indirect_string (unit, info_ptr, &bytes_read);
806 info_ptr += bytes_read;
809 amt = sizeof (struct dwarf_block);
810 blk = (struct dwarf_block *) bfd_alloc (abfd, amt);
811 blk->size = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
812 info_ptr += bytes_read;
813 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
814 info_ptr += blk->size;
818 amt = sizeof (struct dwarf_block);
819 blk = (struct dwarf_block *) bfd_alloc (abfd, amt);
820 blk->size = read_1_byte (abfd, info_ptr);
822 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
823 info_ptr += blk->size;
827 attr->u.val = read_1_byte (abfd, info_ptr);
831 attr->u.val = read_1_byte (abfd, info_ptr);
835 attr->u.sval = read_signed_leb128 (abfd, info_ptr, &bytes_read);
836 info_ptr += bytes_read;
839 attr->u.val = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
840 info_ptr += bytes_read;
843 attr->u.val = read_1_byte (abfd, info_ptr);
847 attr->u.val = read_2_bytes (abfd, info_ptr);
851 attr->u.val = read_4_bytes (abfd, info_ptr);
855 attr->u.val = read_8_bytes (abfd, info_ptr);
858 case DW_FORM_ref_udata:
859 attr->u.val = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
860 info_ptr += bytes_read;
862 case DW_FORM_indirect:
863 form = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
864 info_ptr += bytes_read;
865 info_ptr = read_attribute_value (attr, form, unit, info_ptr);
868 (*_bfd_error_handler) (_("Dwarf Error: Invalid or unhandled FORM value: %u."),
870 bfd_set_error (bfd_error_bad_value);
875 /* Read an attribute described by an abbreviated attribute. */
878 read_attribute (struct attribute *attr,
879 struct attr_abbrev *abbrev,
880 struct comp_unit *unit,
883 attr->name = abbrev->name;
884 info_ptr = read_attribute_value (attr, abbrev->form, unit, info_ptr);
888 /* Source line information table routines. */
890 #define FILE_ALLOC_CHUNK 5
891 #define DIR_ALLOC_CHUNK 5
895 struct line_info* prev_line;
900 int end_sequence; /* End of (sequential) code sequence. */
911 struct line_info_table
914 unsigned int num_files;
915 unsigned int num_dirs;
918 struct fileinfo* files;
919 struct line_info* last_line; /* largest VMA */
920 struct line_info* lcl_head; /* local head; used in 'add_line_info' */
923 /* Remember some information about each function. If the function is
924 inlined (DW_TAG_inlined_subroutine) it may have two additional
925 attributes, DW_AT_call_file and DW_AT_call_line, which specify the
926 source code location where this function was inlined. */
930 struct funcinfo *prev_func; /* Pointer to previous function in list of all functions */
931 struct funcinfo *caller_func; /* Pointer to function one scope higher */
932 char *caller_file; /* Source location file name where caller_func inlines this func */
933 int caller_line; /* Source location line number where caller_func inlines this func */
934 char *file; /* Source location file name */
935 int line; /* Source location line number */
938 struct arange arange;
939 asection *sec; /* Where the symbol is defined */
944 /* Pointer to previous variable in list of all variables */
945 struct varinfo *prev_var;
946 /* Source location file name */
948 /* Source location line number */
953 /* Where the symbol is defined */
955 /* Is this a stack variable? */
956 unsigned int stack: 1;
959 /* Return TRUE if NEW_LINE should sort after LINE. */
961 static inline bfd_boolean
962 new_line_sorts_after (struct line_info *new_line, struct line_info *line)
964 return (new_line->address > line->address
965 || (new_line->address == line->address
966 && new_line->end_sequence < line->end_sequence));
970 /* Adds a new entry to the line_info list in the line_info_table, ensuring
971 that the list is sorted. Note that the line_info list is sorted from
972 highest to lowest VMA (with possible duplicates); that is,
973 line_info->prev_line always accesses an equal or smaller VMA. */
976 add_line_info (struct line_info_table *table,
983 bfd_size_type amt = sizeof (struct line_info);
984 struct line_info* info = (struct line_info *) bfd_alloc (table->abfd, amt);
986 /* Set member data of 'info'. */
987 info->address = address;
989 info->column = column;
990 info->end_sequence = end_sequence;
992 if (filename && filename[0])
994 info->filename = (char *) bfd_alloc (table->abfd, strlen (filename) + 1);
996 strcpy (info->filename, filename);
999 info->filename = NULL;
1001 /* Find the correct location for 'info'. Normally we will receive
1002 new line_info data 1) in order and 2) with increasing VMAs.
1003 However some compilers break the rules (cf. decode_line_info) and
1004 so we include some heuristics for quickly finding the correct
1005 location for 'info'. In particular, these heuristics optimize for
1006 the common case in which the VMA sequence that we receive is a
1007 list of locally sorted VMAs such as
1008 p...z a...j (where a < j < p < z)
1010 Note: table->lcl_head is used to head an *actual* or *possible*
1011 sequence within the list (such as a...j) that is not directly
1012 headed by table->last_line
1014 Note: we may receive duplicate entries from 'decode_line_info'. */
1016 if (table->last_line
1017 && table->last_line->address == address
1018 && table->last_line->end_sequence == end_sequence)
1020 /* We only keep the last entry with the same address and end
1021 sequence. See PR ld/4986. */
1022 if (table->lcl_head == table->last_line)
1023 table->lcl_head = info;
1024 info->prev_line = table->last_line->prev_line;
1025 table->last_line = info;
1027 else if (!table->last_line
1028 || new_line_sorts_after (info, table->last_line))
1030 /* Normal case: add 'info' to the beginning of the list */
1031 info->prev_line = table->last_line;
1032 table->last_line = info;
1034 /* lcl_head: initialize to head a *possible* sequence at the end. */
1035 if (!table->lcl_head)
1036 table->lcl_head = info;
1038 else if (!new_line_sorts_after (info, table->lcl_head)
1039 && (!table->lcl_head->prev_line
1040 || new_line_sorts_after (info, table->lcl_head->prev_line)))
1042 /* Abnormal but easy: lcl_head is the head of 'info'. */
1043 info->prev_line = table->lcl_head->prev_line;
1044 table->lcl_head->prev_line = info;
1048 /* Abnormal and hard: Neither 'last_line' nor 'lcl_head' are valid
1049 heads for 'info'. Reset 'lcl_head'. */
1050 struct line_info* li2 = table->last_line; /* always non-NULL */
1051 struct line_info* li1 = li2->prev_line;
1055 if (!new_line_sorts_after (info, li2)
1056 && new_line_sorts_after (info, li1))
1059 li2 = li1; /* always non-NULL */
1060 li1 = li1->prev_line;
1062 table->lcl_head = li2;
1063 info->prev_line = table->lcl_head->prev_line;
1064 table->lcl_head->prev_line = info;
1068 /* Extract a fully qualified filename from a line info table.
1069 The returned string has been malloc'ed and it is the caller's
1070 responsibility to free it. */
1073 concat_filename (struct line_info_table *table, unsigned int file)
1077 if (file - 1 >= table->num_files)
1079 /* FILE == 0 means unknown. */
1081 (*_bfd_error_handler)
1082 (_("Dwarf Error: mangled line number section (bad file number)."));
1083 return strdup ("<unknown>");
1086 filename = table->files[file - 1].name;
1088 if (!IS_ABSOLUTE_PATH (filename))
1090 char *dirname = NULL;
1091 char *subdirname = NULL;
1095 if (table->files[file - 1].dir)
1096 subdirname = table->dirs[table->files[file - 1].dir - 1];
1098 if (!subdirname || !IS_ABSOLUTE_PATH (subdirname))
1099 dirname = table->comp_dir;
1103 dirname = subdirname;
1108 return strdup (filename);
1110 len = strlen (dirname) + strlen (filename) + 2;
1114 len += strlen (subdirname) + 1;
1115 name = (char *) bfd_malloc (len);
1117 sprintf (name, "%s/%s/%s", dirname, subdirname, filename);
1121 name = (char *) bfd_malloc (len);
1123 sprintf (name, "%s/%s", dirname, filename);
1129 return strdup (filename);
1133 arange_add (bfd *abfd, struct arange *first_arange, bfd_vma low_pc, bfd_vma high_pc)
1135 struct arange *arange;
1137 /* If the first arange is empty, use it. */
1138 if (first_arange->high == 0)
1140 first_arange->low = low_pc;
1141 first_arange->high = high_pc;
1145 /* Next see if we can cheaply extend an existing range. */
1146 arange = first_arange;
1149 if (low_pc == arange->high)
1151 arange->high = high_pc;
1154 if (high_pc == arange->low)
1156 arange->low = low_pc;
1159 arange = arange->next;
1163 /* Need to allocate a new arange and insert it into the arange list.
1164 Order isn't significant, so just insert after the first arange. */
1165 arange = (struct arange *) bfd_zalloc (abfd, sizeof (*arange));
1166 arange->low = low_pc;
1167 arange->high = high_pc;
1168 arange->next = first_arange->next;
1169 first_arange->next = arange;
1172 /* Decode the line number information for UNIT. */
1174 static struct line_info_table*
1175 decode_line_info (struct comp_unit *unit, struct dwarf2_debug *stash)
1177 bfd *abfd = unit->abfd;
1178 struct line_info_table* table;
1181 struct line_head lh;
1182 unsigned int i, bytes_read, offset_size;
1183 char *cur_file, *cur_dir;
1184 unsigned char op_code, extended_op, adj_opcode;
1187 if (! read_section (abfd, ".debug_line", ".zdebug_line",
1188 stash->syms, unit->line_offset,
1189 &stash->dwarf_line_buffer, &stash->dwarf_line_size))
1192 amt = sizeof (struct line_info_table);
1193 table = (struct line_info_table *) bfd_alloc (abfd, amt);
1195 table->comp_dir = unit->comp_dir;
1197 table->num_files = 0;
1198 table->files = NULL;
1200 table->num_dirs = 0;
1203 table->files = NULL;
1204 table->last_line = NULL;
1205 table->lcl_head = NULL;
1207 line_ptr = stash->dwarf_line_buffer + unit->line_offset;
1209 /* Read in the prologue. */
1210 lh.total_length = read_4_bytes (abfd, line_ptr);
1213 if (lh.total_length == 0xffffffff)
1215 lh.total_length = read_8_bytes (abfd, line_ptr);
1219 else if (lh.total_length == 0 && unit->addr_size == 8)
1221 /* Handle (non-standard) 64-bit DWARF2 formats. */
1222 lh.total_length = read_4_bytes (abfd, line_ptr);
1226 line_end = line_ptr + lh.total_length;
1227 lh.version = read_2_bytes (abfd, line_ptr);
1229 if (offset_size == 4)
1230 lh.prologue_length = read_4_bytes (abfd, line_ptr);
1232 lh.prologue_length = read_8_bytes (abfd, line_ptr);
1233 line_ptr += offset_size;
1234 lh.minimum_instruction_length = read_1_byte (abfd, line_ptr);
1236 lh.default_is_stmt = read_1_byte (abfd, line_ptr);
1238 lh.line_base = read_1_signed_byte (abfd, line_ptr);
1240 lh.line_range = read_1_byte (abfd, line_ptr);
1242 lh.opcode_base = read_1_byte (abfd, line_ptr);
1244 amt = lh.opcode_base * sizeof (unsigned char);
1245 lh.standard_opcode_lengths = (unsigned char *) bfd_alloc (abfd, amt);
1247 lh.standard_opcode_lengths[0] = 1;
1249 for (i = 1; i < lh.opcode_base; ++i)
1251 lh.standard_opcode_lengths[i] = read_1_byte (abfd, line_ptr);
1255 /* Read directory table. */
1256 while ((cur_dir = read_string (abfd, line_ptr, &bytes_read)) != NULL)
1258 line_ptr += bytes_read;
1260 if ((table->num_dirs % DIR_ALLOC_CHUNK) == 0)
1264 amt = table->num_dirs + DIR_ALLOC_CHUNK;
1265 amt *= sizeof (char *);
1267 tmp = (char **) bfd_realloc (table->dirs, amt);
1276 table->dirs[table->num_dirs++] = cur_dir;
1279 line_ptr += bytes_read;
1281 /* Read file name table. */
1282 while ((cur_file = read_string (abfd, line_ptr, &bytes_read)) != NULL)
1284 line_ptr += bytes_read;
1286 if ((table->num_files % FILE_ALLOC_CHUNK) == 0)
1288 struct fileinfo *tmp;
1290 amt = table->num_files + FILE_ALLOC_CHUNK;
1291 amt *= sizeof (struct fileinfo);
1293 tmp = (struct fileinfo *) bfd_realloc (table->files, amt);
1296 free (table->files);
1303 table->files[table->num_files].name = cur_file;
1304 table->files[table->num_files].dir =
1305 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
1306 line_ptr += bytes_read;
1307 table->files[table->num_files].time =
1308 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
1309 line_ptr += bytes_read;
1310 table->files[table->num_files].size =
1311 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
1312 line_ptr += bytes_read;
1316 line_ptr += bytes_read;
1318 /* Read the statement sequences until there's nothing left. */
1319 while (line_ptr < line_end)
1321 /* State machine registers. */
1322 bfd_vma address = 0;
1323 char * filename = table->num_files ? concat_filename (table, 1) : NULL;
1324 unsigned int line = 1;
1325 unsigned int column = 0;
1326 int is_stmt = lh.default_is_stmt;
1327 int end_sequence = 0;
1328 /* eraxxon@alumni.rice.edu: Against the DWARF2 specs, some
1329 compilers generate address sequences that are wildly out of
1330 order using DW_LNE_set_address (e.g. Intel C++ 6.0 compiler
1331 for ia64-Linux). Thus, to determine the low and high
1332 address, we must compare on every DW_LNS_copy, etc. */
1333 bfd_vma low_pc = (bfd_vma) -1;
1334 bfd_vma high_pc = 0;
1336 /* Decode the table. */
1337 while (! end_sequence)
1339 op_code = read_1_byte (abfd, line_ptr);
1342 if (op_code >= lh.opcode_base)
1344 /* Special operand. */
1345 adj_opcode = op_code - lh.opcode_base;
1346 address += (adj_opcode / lh.line_range)
1347 * lh.minimum_instruction_length;
1348 line += lh.line_base + (adj_opcode % lh.line_range);
1349 /* Append row to matrix using current values. */
1350 add_line_info (table, address, filename, line, column, 0);
1351 if (address < low_pc)
1353 if (address > high_pc)
1356 else switch (op_code)
1358 case DW_LNS_extended_op:
1359 /* Ignore length. */
1361 extended_op = read_1_byte (abfd, line_ptr);
1364 switch (extended_op)
1366 case DW_LNE_end_sequence:
1368 add_line_info (table, address, filename, line, column,
1370 if (address < low_pc)
1372 if (address > high_pc)
1374 arange_add (unit->abfd, &unit->arange, low_pc, high_pc);
1376 case DW_LNE_set_address:
1377 address = read_address (unit, line_ptr);
1378 line_ptr += unit->addr_size;
1380 case DW_LNE_define_file:
1381 cur_file = read_string (abfd, line_ptr, &bytes_read);
1382 line_ptr += bytes_read;
1383 if ((table->num_files % FILE_ALLOC_CHUNK) == 0)
1385 struct fileinfo *tmp;
1387 amt = table->num_files + FILE_ALLOC_CHUNK;
1388 amt *= sizeof (struct fileinfo);
1389 tmp = (struct fileinfo *) bfd_realloc (table->files, amt);
1392 free (table->files);
1399 table->files[table->num_files].name = cur_file;
1400 table->files[table->num_files].dir =
1401 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
1402 line_ptr += bytes_read;
1403 table->files[table->num_files].time =
1404 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
1405 line_ptr += bytes_read;
1406 table->files[table->num_files].size =
1407 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
1408 line_ptr += bytes_read;
1411 case DW_LNE_set_discriminator:
1412 (void) read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
1413 line_ptr += bytes_read;
1416 (*_bfd_error_handler) (_("Dwarf Error: mangled line number section."));
1417 bfd_set_error (bfd_error_bad_value);
1419 free (table->files);
1425 add_line_info (table, address, filename, line, column, 0);
1426 if (address < low_pc)
1428 if (address > high_pc)
1431 case DW_LNS_advance_pc:
1432 address += lh.minimum_instruction_length
1433 * read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
1434 line_ptr += bytes_read;
1436 case DW_LNS_advance_line:
1437 line += read_signed_leb128 (abfd, line_ptr, &bytes_read);
1438 line_ptr += bytes_read;
1440 case DW_LNS_set_file:
1444 /* The file and directory tables are 0
1445 based, the references are 1 based. */
1446 file = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
1447 line_ptr += bytes_read;
1450 filename = concat_filename (table, file);
1453 case DW_LNS_set_column:
1454 column = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
1455 line_ptr += bytes_read;
1457 case DW_LNS_negate_stmt:
1458 is_stmt = (!is_stmt);
1460 case DW_LNS_set_basic_block:
1462 case DW_LNS_const_add_pc:
1463 address += lh.minimum_instruction_length
1464 * ((255 - lh.opcode_base) / lh.line_range);
1466 case DW_LNS_fixed_advance_pc:
1467 address += read_2_bytes (abfd, line_ptr);
1471 /* Unknown standard opcode, ignore it. */
1472 for (i = 0; i < lh.standard_opcode_lengths[op_code]; i++)
1474 (void) read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
1475 line_ptr += bytes_read;
1488 /* If ADDR is within TABLE set the output parameters and return TRUE,
1489 otherwise return FALSE. The output parameters, FILENAME_PTR and
1490 LINENUMBER_PTR, are pointers to the objects to be filled in. */
1493 lookup_address_in_line_info_table (struct line_info_table *table,
1495 const char **filename_ptr,
1496 unsigned int *linenumber_ptr)
1498 /* Note: table->last_line should be a descendingly sorted list. */
1499 struct line_info *each_line;
1501 for (each_line = table->last_line;
1503 each_line = each_line->prev_line)
1504 if (addr >= each_line->address)
1508 && !(each_line->end_sequence || each_line == table->last_line))
1510 *filename_ptr = each_line->filename;
1511 *linenumber_ptr = each_line->line;
1515 *filename_ptr = NULL;
1519 /* Read in the .debug_ranges section for future reference */
1522 read_debug_ranges (struct comp_unit *unit)
1524 struct dwarf2_debug *stash = unit->stash;
1525 return read_section (unit->abfd, ".debug_ranges", ".zdebug_ranges",
1527 &stash->dwarf_ranges_buffer, &stash->dwarf_ranges_size);
1530 /* Function table functions. */
1532 /* If ADDR is within TABLE, set FUNCTIONNAME_PTR, and return TRUE.
1533 Note that we need to find the function that has the smallest
1534 range that contains ADDR, to handle inlined functions without
1535 depending upon them being ordered in TABLE by increasing range. */
1538 lookup_address_in_function_table (struct comp_unit *unit,
1540 struct funcinfo **function_ptr,
1541 const char **functionname_ptr)
1543 struct funcinfo* each_func;
1544 struct funcinfo* best_fit = NULL;
1545 struct arange *arange;
1547 for (each_func = unit->function_table;
1549 each_func = each_func->prev_func)
1551 for (arange = &each_func->arange;
1553 arange = arange->next)
1555 if (addr >= arange->low && addr < arange->high)
1558 ((arange->high - arange->low) < (best_fit->arange.high - best_fit->arange.low)))
1559 best_fit = each_func;
1566 *functionname_ptr = best_fit->name;
1567 *function_ptr = best_fit;
1576 /* If SYM at ADDR is within function table of UNIT, set FILENAME_PTR
1577 and LINENUMBER_PTR, and return TRUE. */
1580 lookup_symbol_in_function_table (struct comp_unit *unit,
1583 const char **filename_ptr,
1584 unsigned int *linenumber_ptr)
1586 struct funcinfo* each_func;
1587 struct funcinfo* best_fit = NULL;
1588 struct arange *arange;
1589 const char *name = bfd_asymbol_name (sym);
1590 asection *sec = bfd_get_section (sym);
1592 for (each_func = unit->function_table;
1594 each_func = each_func->prev_func)
1596 for (arange = &each_func->arange;
1598 arange = arange->next)
1600 if ((!each_func->sec || each_func->sec == sec)
1601 && addr >= arange->low
1602 && addr < arange->high
1604 && strcmp (name, each_func->name) == 0
1606 || ((arange->high - arange->low)
1607 < (best_fit->arange.high - best_fit->arange.low))))
1608 best_fit = each_func;
1614 best_fit->sec = sec;
1615 *filename_ptr = best_fit->file;
1616 *linenumber_ptr = best_fit->line;
1623 /* Variable table functions. */
1625 /* If SYM is within variable table of UNIT, set FILENAME_PTR and
1626 LINENUMBER_PTR, and return TRUE. */
1629 lookup_symbol_in_variable_table (struct comp_unit *unit,
1632 const char **filename_ptr,
1633 unsigned int *linenumber_ptr)
1635 const char *name = bfd_asymbol_name (sym);
1636 asection *sec = bfd_get_section (sym);
1637 struct varinfo* each;
1639 for (each = unit->variable_table; each; each = each->prev_var)
1640 if (each->stack == 0
1641 && each->file != NULL
1642 && each->name != NULL
1643 && each->addr == addr
1644 && (!each->sec || each->sec == sec)
1645 && strcmp (name, each->name) == 0)
1651 *filename_ptr = each->file;
1652 *linenumber_ptr = each->line;
1660 find_abstract_instance_name (struct comp_unit *unit,
1661 struct attribute *attr_ptr)
1663 bfd *abfd = unit->abfd;
1665 unsigned int abbrev_number, bytes_read, i;
1666 struct abbrev_info *abbrev;
1667 bfd_uint64_t die_ref = attr_ptr->u.val;
1668 struct attribute attr;
1671 /* DW_FORM_ref_addr can reference an entry in a different CU. It
1672 is an offset from the .debug_info section, not the current CU. */
1673 if (attr_ptr->form == DW_FORM_ref_addr)
1675 /* We only support DW_FORM_ref_addr within the same file, so
1676 any relocations should be resolved already. */
1680 info_ptr = unit->stash->sec_info_ptr + die_ref;
1683 info_ptr = unit->info_ptr_unit + die_ref;
1684 abbrev_number = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
1685 info_ptr += bytes_read;
1689 abbrev = lookup_abbrev (abbrev_number, unit->abbrevs);
1692 (*_bfd_error_handler) (_("Dwarf Error: Could not find abbrev number %u."),
1694 bfd_set_error (bfd_error_bad_value);
1698 for (i = 0; i < abbrev->num_attrs; ++i)
1700 info_ptr = read_attribute (&attr, &abbrev->attrs[i], unit, info_ptr);
1704 /* Prefer DW_AT_MIPS_linkage_name over DW_AT_name. */
1708 case DW_AT_specification:
1709 name = find_abstract_instance_name (unit, &attr);
1711 case DW_AT_MIPS_linkage_name:
1724 read_rangelist (struct comp_unit *unit, struct arange *arange, bfd_uint64_t offset)
1726 bfd_byte *ranges_ptr;
1727 bfd_vma base_address = unit->base_address;
1729 if (! unit->stash->dwarf_ranges_buffer)
1731 if (! read_debug_ranges (unit))
1734 ranges_ptr = unit->stash->dwarf_ranges_buffer + offset;
1741 low_pc = read_address (unit, ranges_ptr);
1742 ranges_ptr += unit->addr_size;
1743 high_pc = read_address (unit, ranges_ptr);
1744 ranges_ptr += unit->addr_size;
1746 if (low_pc == 0 && high_pc == 0)
1748 if (low_pc == -1UL && high_pc != -1UL)
1749 base_address = high_pc;
1751 arange_add (unit->abfd, arange, base_address + low_pc, base_address + high_pc);
1755 /* DWARF2 Compilation unit functions. */
1757 /* Scan over each die in a comp. unit looking for functions to add
1758 to the function table and variables to the variable table. */
1761 scan_unit_for_symbols (struct comp_unit *unit)
1763 bfd *abfd = unit->abfd;
1764 bfd_byte *info_ptr = unit->first_child_die_ptr;
1765 int nesting_level = 1;
1766 struct funcinfo **nested_funcs;
1767 int nested_funcs_size;
1769 /* Maintain a stack of in-scope functions and inlined functions, which we
1770 can use to set the caller_func field. */
1771 nested_funcs_size = 32;
1772 nested_funcs = (struct funcinfo **)
1773 bfd_malloc (nested_funcs_size * sizeof (struct funcinfo *));
1774 if (nested_funcs == NULL)
1776 nested_funcs[nesting_level] = 0;
1778 while (nesting_level)
1780 unsigned int abbrev_number, bytes_read, i;
1781 struct abbrev_info *abbrev;
1782 struct attribute attr;
1783 struct funcinfo *func;
1784 struct varinfo *var;
1786 bfd_vma high_pc = 0;
1788 abbrev_number = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
1789 info_ptr += bytes_read;
1791 if (! abbrev_number)
1797 abbrev = lookup_abbrev (abbrev_number,unit->abbrevs);
1800 (*_bfd_error_handler) (_("Dwarf Error: Could not find abbrev number %u."),
1802 bfd_set_error (bfd_error_bad_value);
1803 free (nested_funcs);
1808 if (abbrev->tag == DW_TAG_subprogram
1809 || abbrev->tag == DW_TAG_entry_point
1810 || abbrev->tag == DW_TAG_inlined_subroutine)
1812 bfd_size_type amt = sizeof (struct funcinfo);
1813 func = (struct funcinfo *) bfd_zalloc (abfd, amt);
1814 func->tag = abbrev->tag;
1815 func->prev_func = unit->function_table;
1816 unit->function_table = func;
1817 BFD_ASSERT (!unit->cached);
1819 if (func->tag == DW_TAG_inlined_subroutine)
1820 for (i = nesting_level - 1; i >= 1; i--)
1821 if (nested_funcs[i])
1823 func->caller_func = nested_funcs[i];
1826 nested_funcs[nesting_level] = func;
1831 if (abbrev->tag == DW_TAG_variable)
1833 bfd_size_type amt = sizeof (struct varinfo);
1834 var = (struct varinfo *) bfd_zalloc (abfd, amt);
1835 var->tag = abbrev->tag;
1837 var->prev_var = unit->variable_table;
1838 unit->variable_table = var;
1839 BFD_ASSERT (!unit->cached);
1842 /* No inline function in scope at this nesting level. */
1843 nested_funcs[nesting_level] = 0;
1846 for (i = 0; i < abbrev->num_attrs; ++i)
1848 info_ptr = read_attribute (&attr, &abbrev->attrs[i], unit, info_ptr);
1854 case DW_AT_call_file:
1855 func->caller_file = concat_filename (unit->line_table, attr.u.val);
1858 case DW_AT_call_line:
1859 func->caller_line = attr.u.val;
1862 case DW_AT_abstract_origin:
1863 func->name = find_abstract_instance_name (unit, &attr);
1867 /* Prefer DW_AT_MIPS_linkage_name over DW_AT_name. */
1868 if (func->name == NULL)
1869 func->name = attr.u.str;
1872 case DW_AT_MIPS_linkage_name:
1873 func->name = attr.u.str;
1877 low_pc = attr.u.val;
1881 high_pc = attr.u.val;
1885 read_rangelist (unit, &func->arange, attr.u.val);
1888 case DW_AT_decl_file:
1889 func->file = concat_filename (unit->line_table,
1893 case DW_AT_decl_line:
1894 func->line = attr.u.val;
1906 var->name = attr.u.str;
1909 case DW_AT_decl_file:
1910 var->file = concat_filename (unit->line_table,
1914 case DW_AT_decl_line:
1915 var->line = attr.u.val;
1918 case DW_AT_external:
1919 if (attr.u.val != 0)
1923 case DW_AT_location:
1927 case DW_FORM_block1:
1928 case DW_FORM_block2:
1929 case DW_FORM_block4:
1930 if (*attr.u.blk->data == DW_OP_addr)
1934 /* Verify that DW_OP_addr is the only opcode in the
1935 location, in which case the block size will be 1
1936 plus the address size. */
1937 /* ??? For TLS variables, gcc can emit
1938 DW_OP_addr <addr> DW_OP_GNU_push_tls_address
1939 which we don't handle here yet. */
1940 if (attr.u.blk->size == unit->addr_size + 1U)
1941 var->addr = bfd_get (unit->addr_size * 8,
1943 attr.u.blk->data + 1);
1958 if (func && high_pc != 0)
1960 arange_add (unit->abfd, &func->arange, low_pc, high_pc);
1963 if (abbrev->has_children)
1967 if (nesting_level >= nested_funcs_size)
1969 struct funcinfo **tmp;
1971 nested_funcs_size *= 2;
1972 tmp = (struct funcinfo **)
1973 bfd_realloc (nested_funcs,
1974 (nested_funcs_size * sizeof (struct funcinfo *)));
1977 free (nested_funcs);
1982 nested_funcs[nesting_level] = 0;
1986 free (nested_funcs);
1990 /* Parse a DWARF2 compilation unit starting at INFO_PTR. This
1991 includes the compilation unit header that proceeds the DIE's, but
1992 does not include the length field that precedes each compilation
1993 unit header. END_PTR points one past the end of this comp unit.
1994 OFFSET_SIZE is the size of DWARF2 offsets (either 4 or 8 bytes).
1996 This routine does not read the whole compilation unit; only enough
1997 to get to the line number information for the compilation unit. */
1999 static struct comp_unit *
2000 parse_comp_unit (struct dwarf2_debug *stash,
2001 bfd_vma unit_length,
2002 bfd_byte *info_ptr_unit,
2003 unsigned int offset_size)
2005 struct comp_unit* unit;
2006 unsigned int version;
2007 bfd_uint64_t abbrev_offset = 0;
2008 unsigned int addr_size;
2009 struct abbrev_info** abbrevs;
2010 unsigned int abbrev_number, bytes_read, i;
2011 struct abbrev_info *abbrev;
2012 struct attribute attr;
2013 bfd_byte *info_ptr = stash->info_ptr;
2014 bfd_byte *end_ptr = info_ptr + unit_length;
2017 bfd_vma high_pc = 0;
2018 bfd *abfd = stash->bfd_ptr;
2020 version = read_2_bytes (abfd, info_ptr);
2022 BFD_ASSERT (offset_size == 4 || offset_size == 8);
2023 if (offset_size == 4)
2024 abbrev_offset = read_4_bytes (abfd, info_ptr);
2026 abbrev_offset = read_8_bytes (abfd, info_ptr);
2027 info_ptr += offset_size;
2028 addr_size = read_1_byte (abfd, info_ptr);
2031 if (version != 2 && version != 3)
2033 (*_bfd_error_handler) (_("Dwarf Error: found dwarf version '%u', this reader only handles version 2 and 3 information."), version);
2034 bfd_set_error (bfd_error_bad_value);
2038 if (addr_size > sizeof (bfd_vma))
2040 (*_bfd_error_handler) (_("Dwarf Error: found address size '%u', this reader can not handle sizes greater than '%u'."),
2042 (unsigned int) sizeof (bfd_vma));
2043 bfd_set_error (bfd_error_bad_value);
2047 if (addr_size != 2 && addr_size != 4 && addr_size != 8)
2049 (*_bfd_error_handler) ("Dwarf Error: found address size '%u', this reader can only handle address sizes '2', '4' and '8'.", addr_size);
2050 bfd_set_error (bfd_error_bad_value);
2054 /* Read the abbrevs for this compilation unit into a table. */
2055 abbrevs = read_abbrevs (abfd, abbrev_offset, stash);
2059 abbrev_number = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
2060 info_ptr += bytes_read;
2061 if (! abbrev_number)
2063 (*_bfd_error_handler) (_("Dwarf Error: Bad abbrev number: %u."),
2065 bfd_set_error (bfd_error_bad_value);
2069 abbrev = lookup_abbrev (abbrev_number, abbrevs);
2072 (*_bfd_error_handler) (_("Dwarf Error: Could not find abbrev number %u."),
2074 bfd_set_error (bfd_error_bad_value);
2078 amt = sizeof (struct comp_unit);
2079 unit = (struct comp_unit *) bfd_zalloc (abfd, amt);
2081 unit->version = version;
2082 unit->addr_size = addr_size;
2083 unit->offset_size = offset_size;
2084 unit->abbrevs = abbrevs;
2085 unit->end_ptr = end_ptr;
2086 unit->stash = stash;
2087 unit->info_ptr_unit = info_ptr_unit;
2089 for (i = 0; i < abbrev->num_attrs; ++i)
2091 info_ptr = read_attribute (&attr, &abbrev->attrs[i], unit, info_ptr);
2093 /* Store the data if it is of an attribute we want to keep in a
2094 partial symbol table. */
2097 case DW_AT_stmt_list:
2099 unit->line_offset = attr.u.val;
2103 unit->name = attr.u.str;
2107 low_pc = attr.u.val;
2108 /* If the compilation unit DIE has a DW_AT_low_pc attribute,
2109 this is the base address to use when reading location
2110 lists or range lists. */
2111 unit->base_address = low_pc;
2115 high_pc = attr.u.val;
2119 read_rangelist (unit, &unit->arange, attr.u.val);
2122 case DW_AT_comp_dir:
2124 char *comp_dir = attr.u.str;
2127 /* Irix 6.2 native cc prepends <machine>.: to the compilation
2128 directory, get rid of it. */
2129 char *cp = strchr (comp_dir, ':');
2131 if (cp && cp != comp_dir && cp[-1] == '.' && cp[1] == '/')
2134 unit->comp_dir = comp_dir;
2144 arange_add (unit->abfd, &unit->arange, low_pc, high_pc);
2147 unit->first_child_die_ptr = info_ptr;
2151 /* Return TRUE if UNIT may contain the address given by ADDR. When
2152 there are functions written entirely with inline asm statements, the
2153 range info in the compilation unit header may not be correct. We
2154 need to consult the line info table to see if a compilation unit
2155 really contains the given address. */
2158 comp_unit_contains_address (struct comp_unit *unit, bfd_vma addr)
2160 struct arange *arange;
2165 arange = &unit->arange;
2168 if (addr >= arange->low && addr < arange->high)
2170 arange = arange->next;
2177 /* If UNIT contains ADDR, set the output parameters to the values for
2178 the line containing ADDR. The output parameters, FILENAME_PTR,
2179 FUNCTIONNAME_PTR, and LINENUMBER_PTR, are pointers to the objects
2182 Return TRUE if UNIT contains ADDR, and no errors were encountered;
2186 comp_unit_find_nearest_line (struct comp_unit *unit,
2188 const char **filename_ptr,
2189 const char **functionname_ptr,
2190 unsigned int *linenumber_ptr,
2191 struct dwarf2_debug *stash)
2195 struct funcinfo *function;
2200 if (! unit->line_table)
2202 if (! unit->stmtlist)
2208 unit->line_table = decode_line_info (unit, stash);
2210 if (! unit->line_table)
2216 if (unit->first_child_die_ptr < unit->end_ptr
2217 && ! scan_unit_for_symbols (unit))
2225 func_p = lookup_address_in_function_table (unit, addr,
2226 &function, functionname_ptr);
2227 if (func_p && (function->tag == DW_TAG_inlined_subroutine))
2228 stash->inliner_chain = function;
2229 line_p = lookup_address_in_line_info_table (unit->line_table, addr,
2232 return line_p || func_p;
2235 /* Check to see if line info is already decoded in a comp_unit.
2236 If not, decode it. Returns TRUE if no errors were encountered;
2240 comp_unit_maybe_decode_line_info (struct comp_unit *unit,
2241 struct dwarf2_debug *stash)
2246 if (! unit->line_table)
2248 if (! unit->stmtlist)
2254 unit->line_table = decode_line_info (unit, stash);
2256 if (! unit->line_table)
2262 if (unit->first_child_die_ptr < unit->end_ptr
2263 && ! scan_unit_for_symbols (unit))
2273 /* If UNIT contains SYM at ADDR, set the output parameters to the
2274 values for the line containing SYM. The output parameters,
2275 FILENAME_PTR, and LINENUMBER_PTR, are pointers to the objects to be
2278 Return TRUE if UNIT contains SYM, and no errors were encountered;
2282 comp_unit_find_line (struct comp_unit *unit,
2285 const char **filename_ptr,
2286 unsigned int *linenumber_ptr,
2287 struct dwarf2_debug *stash)
2289 if (!comp_unit_maybe_decode_line_info (unit, stash))
2292 if (sym->flags & BSF_FUNCTION)
2293 return lookup_symbol_in_function_table (unit, sym, addr,
2297 return lookup_symbol_in_variable_table (unit, sym, addr,
2302 static struct funcinfo *
2303 reverse_funcinfo_list (struct funcinfo *head)
2305 struct funcinfo *rhead;
2306 struct funcinfo *temp;
2308 for (rhead = NULL; head; head = temp)
2310 temp = head->prev_func;
2311 head->prev_func = rhead;
2317 static struct varinfo *
2318 reverse_varinfo_list (struct varinfo *head)
2320 struct varinfo *rhead;
2321 struct varinfo *temp;
2323 for (rhead = NULL; head; head = temp)
2325 temp = head->prev_var;
2326 head->prev_var = rhead;
2332 /* Extract all interesting funcinfos and varinfos of a compilation
2333 unit into hash tables for faster lookup. Returns TRUE if no
2334 errors were enountered; FALSE otherwise. */
2337 comp_unit_hash_info (struct dwarf2_debug *stash,
2338 struct comp_unit *unit,
2339 struct info_hash_table *funcinfo_hash_table,
2340 struct info_hash_table *varinfo_hash_table)
2342 struct funcinfo* each_func;
2343 struct varinfo* each_var;
2344 bfd_boolean okay = TRUE;
2346 BFD_ASSERT (stash->info_hash_status != STASH_INFO_HASH_DISABLED);
2348 if (!comp_unit_maybe_decode_line_info (unit, stash))
2351 BFD_ASSERT (!unit->cached);
2353 /* To preserve the original search order, we went to visit the function
2354 infos in the reversed order of the list. However, making the list
2355 bi-directional use quite a bit of extra memory. So we reverse
2356 the list first, traverse the list in the now reversed order and
2357 finally reverse the list again to get back the original order. */
2358 unit->function_table = reverse_funcinfo_list (unit->function_table);
2359 for (each_func = unit->function_table;
2361 each_func = each_func->prev_func)
2363 /* Skip nameless functions. */
2364 if (each_func->name)
2365 /* There is no need to copy name string into hash table as
2366 name string is either in the dwarf string buffer or
2367 info in the stash. */
2368 okay = insert_info_hash_table (funcinfo_hash_table, each_func->name,
2369 (void*) each_func, FALSE);
2371 unit->function_table = reverse_funcinfo_list (unit->function_table);
2375 /* We do the same for variable infos. */
2376 unit->variable_table = reverse_varinfo_list (unit->variable_table);
2377 for (each_var = unit->variable_table;
2379 each_var = each_var->prev_var)
2381 /* Skip stack vars and vars with no files or names. */
2382 if (each_var->stack == 0
2383 && each_var->file != NULL
2384 && each_var->name != NULL)
2385 /* There is no need to copy name string into hash table as
2386 name string is either in the dwarf string buffer or
2387 info in the stash. */
2388 okay = insert_info_hash_table (varinfo_hash_table, each_var->name,
2389 (void*) each_var, FALSE);
2392 unit->variable_table = reverse_varinfo_list (unit->variable_table);
2393 unit->cached = TRUE;
2397 /* Locate a section in a BFD containing debugging info. The search starts
2398 from the section after AFTER_SEC, or from the first section in the BFD if
2399 AFTER_SEC is NULL. The search works by examining the names of the
2400 sections. There are two permissiable names. The first is .debug_info.
2401 This is the standard DWARF2 name. The second is a prefix .gnu.linkonce.wi.
2402 This is a variation on the .debug_info section which has a checksum
2403 describing the contents appended onto the name. This allows the linker to
2404 identify and discard duplicate debugging sections for different
2405 compilation units. */
2406 #define DWARF2_DEBUG_INFO ".debug_info"
2407 #define DWARF2_COMPRESSED_DEBUG_INFO ".zdebug_info"
2408 #define GNU_LINKONCE_INFO ".gnu.linkonce.wi."
2411 find_debug_info (bfd *abfd, asection *after_sec)
2415 msec = after_sec != NULL ? after_sec->next : abfd->sections;
2419 if (strcmp (msec->name, DWARF2_DEBUG_INFO) == 0)
2422 if (strcmp (msec->name, DWARF2_COMPRESSED_DEBUG_INFO) == 0)
2425 if (CONST_STRNEQ (msec->name, GNU_LINKONCE_INFO))
2434 /* Unset vmas for adjusted sections in STASH. */
2437 unset_sections (struct dwarf2_debug *stash)
2440 struct adjusted_section *p;
2442 i = stash->adjusted_section_count;
2443 p = stash->adjusted_sections;
2444 for (; i > 0; i--, p++)
2445 p->section->vma = 0;
2448 /* Set unique VMAs for loadable and DWARF sections in ABFD and save
2449 VMAs in STASH for unset_sections. */
2452 place_sections (bfd *abfd, struct dwarf2_debug *stash)
2454 struct adjusted_section *p;
2457 if (stash->adjusted_section_count != 0)
2459 i = stash->adjusted_section_count;
2460 p = stash->adjusted_sections;
2461 for (; i > 0; i--, p++)
2462 p->section->vma = p->adj_vma;
2467 bfd_vma last_vma = 0, last_dwarf = 0;
2471 for (sect = abfd->sections; sect != NULL; sect = sect->next)
2479 /* We need to adjust the VMAs of any .debug_info sections.
2480 Skip compressed ones, since no relocations could target
2481 them - they should not appear in object files anyway. */
2482 if (strcmp (sect->name, DWARF2_DEBUG_INFO) == 0)
2484 else if (CONST_STRNEQ (sect->name, GNU_LINKONCE_INFO))
2489 if (!is_debug_info && (sect->flags & SEC_LOAD) == 0)
2492 sz = sect->rawsize ? sect->rawsize : sect->size;
2499 amt = i * sizeof (struct adjusted_section);
2500 p = (struct adjusted_section *) bfd_zalloc (abfd, amt);
2504 stash->adjusted_sections = p;
2505 stash->adjusted_section_count = i;
2507 for (sect = abfd->sections; sect != NULL; sect = sect->next)
2515 /* We need to adjust the VMAs of any .debug_info sections.
2516 Skip compressed ones, since no relocations could target
2517 them - they should not appear in object files anyway. */
2518 if (strcmp (sect->name, DWARF2_DEBUG_INFO) == 0)
2520 else if (CONST_STRNEQ (sect->name, GNU_LINKONCE_INFO))
2525 if (!is_debug_info && (sect->flags & SEC_LOAD) == 0)
2528 sz = sect->rawsize ? sect->rawsize : sect->size;
2535 BFD_ASSERT (sect->alignment_power == 0);
2536 sect->vma = last_dwarf;
2539 else if (last_vma != 0)
2541 /* Align the new address to the current section
2543 last_vma = ((last_vma
2544 + ~((bfd_vma) -1 << sect->alignment_power))
2545 & ((bfd_vma) -1 << sect->alignment_power));
2546 sect->vma = last_vma;
2547 last_vma += sect->vma + sz;
2550 last_vma += sect->vma + sz;
2552 p->adj_vma = sect->vma;
2561 /* Look up a funcinfo by name using the given info hash table. If found,
2562 also update the locations pointed to by filename_ptr and linenumber_ptr.
2564 This function returns TRUE if a funcinfo that matches the given symbol
2565 and address is found with any error; otherwise it returns FALSE. */
2568 info_hash_lookup_funcinfo (struct info_hash_table *hash_table,
2571 const char **filename_ptr,
2572 unsigned int *linenumber_ptr)
2574 struct funcinfo* each_func;
2575 struct funcinfo* best_fit = NULL;
2576 struct info_list_node *node;
2577 struct arange *arange;
2578 const char *name = bfd_asymbol_name (sym);
2579 asection *sec = bfd_get_section (sym);
2581 for (node = lookup_info_hash_table (hash_table, name);
2585 each_func = (struct funcinfo *) node->info;
2586 for (arange = &each_func->arange;
2588 arange = arange->next)
2590 if ((!each_func->sec || each_func->sec == sec)
2591 && addr >= arange->low
2592 && addr < arange->high
2594 || ((arange->high - arange->low)
2595 < (best_fit->arange.high - best_fit->arange.low))))
2596 best_fit = each_func;
2602 best_fit->sec = sec;
2603 *filename_ptr = best_fit->file;
2604 *linenumber_ptr = best_fit->line;
2611 /* Look up a varinfo by name using the given info hash table. If found,
2612 also update the locations pointed to by filename_ptr and linenumber_ptr.
2614 This function returns TRUE if a varinfo that matches the given symbol
2615 and address is found with any error; otherwise it returns FALSE. */
2618 info_hash_lookup_varinfo (struct info_hash_table *hash_table,
2621 const char **filename_ptr,
2622 unsigned int *linenumber_ptr)
2624 const char *name = bfd_asymbol_name (sym);
2625 asection *sec = bfd_get_section (sym);
2626 struct varinfo* each;
2627 struct info_list_node *node;
2629 for (node = lookup_info_hash_table (hash_table, name);
2633 each = (struct varinfo *) node->info;
2634 if (each->addr == addr
2635 && (!each->sec || each->sec == sec))
2638 *filename_ptr = each->file;
2639 *linenumber_ptr = each->line;
2647 /* Update the funcinfo and varinfo info hash tables if they are
2648 not up to date. Returns TRUE if there is no error; otherwise
2649 returns FALSE and disable the info hash tables. */
2652 stash_maybe_update_info_hash_tables (struct dwarf2_debug *stash)
2654 struct comp_unit *each;
2656 /* Exit if hash tables are up-to-date. */
2657 if (stash->all_comp_units == stash->hash_units_head)
2660 if (stash->hash_units_head)
2661 each = stash->hash_units_head->prev_unit;
2663 each = stash->last_comp_unit;
2667 if (!comp_unit_hash_info (stash, each, stash->funcinfo_hash_table,
2668 stash->varinfo_hash_table))
2670 stash->info_hash_status = STASH_INFO_HASH_DISABLED;
2673 each = each->prev_unit;
2676 stash->hash_units_head = stash->all_comp_units;
2680 /* Check consistency of info hash tables. This is for debugging only. */
2682 static void ATTRIBUTE_UNUSED
2683 stash_verify_info_hash_table (struct dwarf2_debug *stash)
2685 struct comp_unit *each_unit;
2686 struct funcinfo *each_func;
2687 struct varinfo *each_var;
2688 struct info_list_node *node;
2691 for (each_unit = stash->all_comp_units;
2693 each_unit = each_unit->next_unit)
2695 for (each_func = each_unit->function_table;
2697 each_func = each_func->prev_func)
2699 if (!each_func->name)
2701 node = lookup_info_hash_table (stash->funcinfo_hash_table,
2705 while (node && !found)
2707 found = node->info == each_func;
2713 for (each_var = each_unit->variable_table;
2715 each_var = each_var->prev_var)
2717 if (!each_var->name || !each_var->file || each_var->stack)
2719 node = lookup_info_hash_table (stash->varinfo_hash_table,
2723 while (node && !found)
2725 found = node->info == each_var;
2733 /* Check to see if we want to enable the info hash tables, which consume
2734 quite a bit of memory. Currently we only check the number times
2735 bfd_dwarf2_find_line is called. In the future, we may also want to
2736 take the number of symbols into account. */
2739 stash_maybe_enable_info_hash_tables (bfd *abfd, struct dwarf2_debug *stash)
2741 BFD_ASSERT (stash->info_hash_status == STASH_INFO_HASH_OFF);
2743 if (stash->info_hash_count++ < STASH_INFO_HASH_TRIGGER)
2746 /* FIXME: Maybe we should check the reduce_memory_overheads
2747 and optimize fields in the bfd_link_info structure ? */
2749 /* Create hash tables. */
2750 stash->funcinfo_hash_table = create_info_hash_table (abfd);
2751 stash->varinfo_hash_table = create_info_hash_table (abfd);
2752 if (!stash->funcinfo_hash_table || !stash->varinfo_hash_table)
2754 /* Turn off info hashes if any allocation above fails. */
2755 stash->info_hash_status = STASH_INFO_HASH_DISABLED;
2758 /* We need a forced update so that the info hash tables will
2759 be created even though there is no compilation unit. That
2760 happens if STASH_INFO_HASH_TRIGGER is 0. */
2761 stash_maybe_update_info_hash_tables (stash);
2762 stash->info_hash_status = STASH_INFO_HASH_ON;
2765 /* Find the file and line associated with a symbol and address using the
2766 info hash tables of a stash. If there is a match, the function returns
2767 TRUE and update the locations pointed to by filename_ptr and linenumber_ptr;
2768 otherwise it returns FALSE. */
2771 stash_find_line_fast (struct dwarf2_debug *stash,
2774 const char **filename_ptr,
2775 unsigned int *linenumber_ptr)
2777 BFD_ASSERT (stash->info_hash_status == STASH_INFO_HASH_ON);
2779 if (sym->flags & BSF_FUNCTION)
2780 return info_hash_lookup_funcinfo (stash->funcinfo_hash_table, sym, addr,
2781 filename_ptr, linenumber_ptr);
2782 return info_hash_lookup_varinfo (stash->varinfo_hash_table, sym, addr,
2783 filename_ptr, linenumber_ptr);
2786 /* Find the source code location of SYMBOL. If SYMBOL is NULL
2787 then find the nearest source code location corresponding to
2788 the address SECTION + OFFSET.
2789 Returns TRUE if the line is found without error and fills in
2790 FILENAME_PTR and LINENUMBER_PTR. In the case where SYMBOL was
2791 NULL the FUNCTIONNAME_PTR is also filled in.
2792 SYMBOLS contains the symbol table for ABFD.
2793 ADDR_SIZE is the number of bytes in the initial .debug_info length
2794 field and in the abbreviation offset, or zero to indicate that the
2795 default value should be used. */
2798 find_line (bfd *abfd,
2803 const char **filename_ptr,
2804 const char **functionname_ptr,
2805 unsigned int *linenumber_ptr,
2806 unsigned int addr_size,
2809 /* Read each compilation unit from the section .debug_info, and check
2810 to see if it contains the address we are searching for. If yes,
2811 lookup the address, and return the line number info. If no, go
2812 on to the next compilation unit.
2814 We keep a list of all the previously read compilation units, and
2815 a pointer to the next un-read compilation unit. Check the
2816 previously read units before reading more. */
2817 struct dwarf2_debug *stash;
2818 /* What address are we looking for? */
2820 struct comp_unit* each;
2821 bfd_vma found = FALSE;
2822 bfd_boolean do_line;
2824 stash = (struct dwarf2_debug *) *pinfo;
2828 bfd_size_type amt = sizeof (struct dwarf2_debug);
2830 stash = (struct dwarf2_debug *) bfd_zalloc (abfd, amt);
2835 /* In a relocatable file, 2 functions may have the same address.
2836 We change the section vma so that they won't overlap. */
2837 if ((abfd->flags & (EXEC_P | DYNAMIC)) == 0)
2839 if (! place_sections (abfd, stash))
2843 do_line = (section == NULL
2845 && functionname_ptr == NULL
2849 addr = symbol->value;
2850 section = bfd_get_section (symbol);
2852 else if (section != NULL
2853 && functionname_ptr != NULL
2859 if (section->output_section)
2860 addr += section->output_section->vma + section->output_offset;
2862 addr += section->vma;
2863 *filename_ptr = NULL;
2865 *functionname_ptr = NULL;
2866 *linenumber_ptr = 0;
2871 bfd_size_type total_size;
2876 msec = find_debug_info (abfd, NULL);
2879 char * debug_filename = bfd_follow_gnu_debuglink (abfd, DEBUGDIR);
2881 if (debug_filename == NULL)
2882 /* No dwarf2 info, and no gnu_debuglink to follow.
2883 Note that at this point the stash has been allocated, but
2884 contains zeros. This lets future calls to this function
2885 fail more quickly. */
2888 if ((debug_bfd = bfd_openr (debug_filename, NULL)) == NULL
2889 || ! bfd_check_format (debug_bfd, bfd_object)
2890 || (msec = find_debug_info (debug_bfd, NULL)) == NULL)
2893 bfd_close (debug_bfd);
2894 /* FIXME: Should we report our failure to follow the debuglink ? */
2895 free (debug_filename);
2902 /* There can be more than one DWARF2 info section in a BFD these
2903 days. First handle the easy case when there's only one. If
2904 there's more than one, try case two: none of the sections is
2905 compressed. In that case, read them all in and produce one
2906 large stash. We do this in two passes - in the first pass we
2907 just accumulate the section sizes, and in the second pass we
2908 read in the section's contents. (The allows us to avoid
2909 reallocing the data as we add sections to the stash.) If
2910 some or all sections are compressed, then do things the slow
2911 way, with a bunch of reallocs. */
2913 if (! find_debug_info (debug_bfd, msec))
2915 /* Case 1: only one info section. */
2916 total_size = msec->size;
2917 if (! read_section (debug_bfd, ".debug_info", ".zdebug_info",
2919 &stash->info_ptr_memory, &total_size))
2924 int all_uncompressed = 1;
2925 for (total_size = 0; msec; msec = find_debug_info (debug_bfd, msec))
2927 total_size += msec->size;
2928 if (strcmp (msec->name, DWARF2_COMPRESSED_DEBUG_INFO) == 0)
2929 all_uncompressed = 0;
2931 if (all_uncompressed)
2933 /* Case 2: multiple sections, but none is compressed. */
2934 stash->info_ptr_memory = (bfd_byte *) bfd_malloc (total_size);
2935 if (stash->info_ptr_memory == NULL)
2939 for (msec = find_debug_info (debug_bfd, NULL);
2941 msec = find_debug_info (debug_bfd, msec))
2949 if (!(bfd_simple_get_relocated_section_contents
2950 (debug_bfd, msec, stash->info_ptr_memory + total_size,
2959 /* Case 3: multiple sections, some or all compressed. */
2960 stash->info_ptr_memory = NULL;
2962 for (msec = find_debug_info (debug_bfd, NULL);
2964 msec = find_debug_info (debug_bfd, msec))
2966 bfd_size_type size = msec->size;
2972 buffer = (bfd_simple_get_relocated_section_contents
2973 (debug_bfd, msec, NULL, symbols));
2977 if (strcmp (msec->name, DWARF2_COMPRESSED_DEBUG_INFO) == 0)
2979 if (! bfd_uncompress_section_contents (&buffer, &size))
2985 stash->info_ptr_memory = (bfd_byte *)
2986 bfd_realloc (stash->info_ptr_memory, total_size + size);
2987 memcpy (stash->info_ptr_memory + total_size, buffer, size);
2994 stash->info_ptr = stash->info_ptr_memory;
2995 stash->info_ptr_end = stash->info_ptr + total_size;
2996 stash->sec = find_debug_info (debug_bfd, NULL);
2997 stash->sec_info_ptr = stash->info_ptr;
2998 stash->syms = symbols;
2999 stash->bfd_ptr = debug_bfd;
3002 /* A null info_ptr indicates that there is no dwarf2 info
3003 (or that an error occured while setting up the stash). */
3004 if (! stash->info_ptr)
3007 stash->inliner_chain = NULL;
3009 /* Check the previously read comp. units first. */
3012 /* The info hash tables use quite a bit of memory. We may not want to
3013 always use them. We use some heuristics to decide if and when to
3015 if (stash->info_hash_status == STASH_INFO_HASH_OFF)
3016 stash_maybe_enable_info_hash_tables (abfd, stash);
3018 /* Keep info hash table up to date if they are available. Note that we
3019 may disable the hash tables if there is any error duing update. */
3020 if (stash->info_hash_status == STASH_INFO_HASH_ON)
3021 stash_maybe_update_info_hash_tables (stash);
3023 if (stash->info_hash_status == STASH_INFO_HASH_ON)
3025 found = stash_find_line_fast (stash, symbol, addr, filename_ptr,
3032 /* Check the previously read comp. units first. */
3033 for (each = stash->all_comp_units; each; each = each->next_unit)
3034 if ((symbol->flags & BSF_FUNCTION) == 0
3035 || comp_unit_contains_address (each, addr))
3037 found = comp_unit_find_line (each, symbol, addr, filename_ptr,
3038 linenumber_ptr, stash);
3046 for (each = stash->all_comp_units; each; each = each->next_unit)
3048 found = (comp_unit_contains_address (each, addr)
3049 && comp_unit_find_nearest_line (each, addr,
3059 /* The DWARF2 spec says that the initial length field, and the
3060 offset of the abbreviation table, should both be 4-byte values.
3061 However, some compilers do things differently. */
3064 BFD_ASSERT (addr_size == 4 || addr_size == 8);
3066 /* Read each remaining comp. units checking each as they are read. */
3067 while (stash->info_ptr < stash->info_ptr_end)
3070 unsigned int offset_size = addr_size;
3071 bfd_byte *info_ptr_unit = stash->info_ptr;
3073 length = read_4_bytes (stash->bfd_ptr, stash->info_ptr);
3074 /* A 0xffffff length is the DWARF3 way of indicating
3075 we use 64-bit offsets, instead of 32-bit offsets. */
3076 if (length == 0xffffffff)
3079 length = read_8_bytes (stash->bfd_ptr, stash->info_ptr + 4);
3080 stash->info_ptr += 12;
3082 /* A zero length is the IRIX way of indicating 64-bit offsets,
3083 mostly because the 64-bit length will generally fit in 32
3084 bits, and the endianness helps. */
3085 else if (length == 0)
3088 length = read_4_bytes (stash->bfd_ptr, stash->info_ptr + 4);
3089 stash->info_ptr += 8;
3091 /* In the absence of the hints above, we assume 32-bit DWARF2
3092 offsets even for targets with 64-bit addresses, because:
3093 a) most of the time these targets will not have generated
3094 more than 2Gb of debug info and so will not need 64-bit
3097 b) if they do use 64-bit offsets but they are not using
3098 the size hints that are tested for above then they are
3099 not conforming to the DWARF3 standard anyway. */
3100 else if (addr_size == 8)
3103 stash->info_ptr += 4;
3106 stash->info_ptr += 4;
3110 each = parse_comp_unit (stash, length, info_ptr_unit,
3113 /* The dwarf information is damaged, don't trust it any
3116 stash->info_ptr += length;
3118 if (stash->all_comp_units)
3119 stash->all_comp_units->prev_unit = each;
3121 stash->last_comp_unit = each;
3123 each->next_unit = stash->all_comp_units;
3124 stash->all_comp_units = each;
3126 /* DW_AT_low_pc and DW_AT_high_pc are optional for
3127 compilation units. If we don't have them (i.e.,
3128 unit->high == 0), we need to consult the line info table
3129 to see if a compilation unit contains the given
3132 found = (((symbol->flags & BSF_FUNCTION) == 0
3133 || each->arange.high == 0
3134 || comp_unit_contains_address (each, addr))
3135 && comp_unit_find_line (each, symbol, addr,
3140 found = ((each->arange.high == 0
3141 || comp_unit_contains_address (each, addr))
3142 && comp_unit_find_nearest_line (each, addr,
3148 if ((bfd_vma) (stash->info_ptr - stash->sec_info_ptr)
3149 == stash->sec->size)
3151 stash->sec = find_debug_info (stash->bfd_ptr, stash->sec);
3152 stash->sec_info_ptr = stash->info_ptr;
3161 if ((abfd->flags & (EXEC_P | DYNAMIC)) == 0)
3162 unset_sections (stash);
3167 /* The DWARF2 version of find_nearest_line.
3168 Return TRUE if the line is found without error. */
3171 _bfd_dwarf2_find_nearest_line (bfd *abfd,
3175 const char **filename_ptr,
3176 const char **functionname_ptr,
3177 unsigned int *linenumber_ptr,
3178 unsigned int addr_size,
3181 return find_line (abfd, section, offset, NULL, symbols, filename_ptr,
3182 functionname_ptr, linenumber_ptr, addr_size,
3186 /* The DWARF2 version of find_line.
3187 Return TRUE if the line is found without error. */
3190 _bfd_dwarf2_find_line (bfd *abfd,
3193 const char **filename_ptr,
3194 unsigned int *linenumber_ptr,
3195 unsigned int addr_size,
3198 return find_line (abfd, NULL, 0, symbol, symbols, filename_ptr,
3199 NULL, linenumber_ptr, addr_size,
3204 _bfd_dwarf2_find_inliner_info (bfd *abfd ATTRIBUTE_UNUSED,
3205 const char **filename_ptr,
3206 const char **functionname_ptr,
3207 unsigned int *linenumber_ptr,
3210 struct dwarf2_debug *stash;
3212 stash = (struct dwarf2_debug *) *pinfo;
3215 struct funcinfo *func = stash->inliner_chain;
3217 if (func && func->caller_func)
3219 *filename_ptr = func->caller_file;
3220 *functionname_ptr = func->caller_func->name;
3221 *linenumber_ptr = func->caller_line;
3222 stash->inliner_chain = func->caller_func;
3231 _bfd_dwarf2_cleanup_debug_info (bfd *abfd)
3233 struct comp_unit *each;
3234 struct dwarf2_debug *stash;
3236 if (abfd == NULL || elf_tdata (abfd) == NULL)
3239 stash = (struct dwarf2_debug *) elf_tdata (abfd)->dwarf2_find_line_info;
3244 for (each = stash->all_comp_units; each; each = each->next_unit)
3246 struct abbrev_info **abbrevs = each->abbrevs;
3247 struct funcinfo *function_table = each->function_table;
3248 struct varinfo *variable_table = each->variable_table;
3251 for (i = 0; i < ABBREV_HASH_SIZE; i++)
3253 struct abbrev_info *abbrev = abbrevs[i];
3257 free (abbrev->attrs);
3258 abbrev = abbrev->next;
3262 if (each->line_table)
3264 free (each->line_table->dirs);
3265 free (each->line_table->files);
3268 while (function_table)
3270 if (function_table->file)
3272 free (function_table->file);
3273 function_table->file = NULL;
3276 if (function_table->caller_file)
3278 free (function_table->caller_file);
3279 function_table->caller_file = NULL;
3281 function_table = function_table->prev_func;
3284 while (variable_table)
3286 if (variable_table->file)
3288 free (variable_table->file);
3289 variable_table->file = NULL;
3292 variable_table = variable_table->prev_var;
3296 if (stash->dwarf_abbrev_buffer)
3297 free (stash->dwarf_abbrev_buffer);
3298 if (stash->dwarf_line_buffer)
3299 free (stash->dwarf_line_buffer);
3300 if (stash->dwarf_str_buffer)
3301 free (stash->dwarf_str_buffer);
3302 if (stash->dwarf_ranges_buffer)
3303 free (stash->dwarf_ranges_buffer);
3304 if (stash->info_ptr_memory)
3305 free (stash->info_ptr_memory);