2 Copyright 1994, 1995, 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003,
3 2004, 2005, 2006, 2007, 2008 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"
38 #include "elf/dwarf2.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 loadable_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 /* Pointer to the symbol table. */
110 /* Pointer to the .debug_abbrev section loaded into memory. */
111 bfd_byte *dwarf_abbrev_buffer;
113 /* Length of the loaded .debug_abbrev section. */
114 bfd_size_type dwarf_abbrev_size;
116 /* Buffer for decode_line_info. */
117 bfd_byte *dwarf_line_buffer;
119 /* Length of the loaded .debug_line section. */
120 bfd_size_type dwarf_line_size;
122 /* Pointer to the .debug_str section loaded into memory. */
123 bfd_byte *dwarf_str_buffer;
125 /* Length of the loaded .debug_str section. */
126 bfd_size_type dwarf_str_size;
128 /* Pointer to the .debug_ranges section loaded into memory. */
129 bfd_byte *dwarf_ranges_buffer;
131 /* Length of the loaded .debug_ranges section. */
132 bfd_size_type dwarf_ranges_size;
134 /* If the most recent call to bfd_find_nearest_line was given an
135 address in an inlined function, preserve a pointer into the
136 calling chain for subsequent calls to bfd_find_inliner_info to
138 struct funcinfo *inliner_chain;
140 /* Number of loadable sections. */
141 unsigned int loadable_section_count;
143 /* Array of loadable sections. */
144 struct loadable_section *loadable_sections;
146 /* Number of times find_line is called. This is used in
147 the heuristic for enabling the info hash tables. */
150 #define STASH_INFO_HASH_TRIGGER 100
152 /* Hash table mapping symbol names to function infos. */
153 struct info_hash_table *funcinfo_hash_table;
155 /* Hash table mapping symbol names to variable infos. */
156 struct info_hash_table *varinfo_hash_table;
158 /* Head of comp_unit list in the last hash table update. */
159 struct comp_unit *hash_units_head;
161 /* Status of info hash. */
162 int info_hash_status;
163 #define STASH_INFO_HASH_OFF 0
164 #define STASH_INFO_HASH_ON 1
165 #define STASH_INFO_HASH_DISABLED 2
175 /* A minimal decoding of DWARF2 compilation units. We only decode
176 what's needed to get to the line number information. */
180 /* Chain the previously read compilation units. */
181 struct comp_unit *next_unit;
183 /* Likewise, chain the compilation unit read after this one.
184 The comp units are stored in reversed reading order. */
185 struct comp_unit *prev_unit;
187 /* Keep the bfd convenient (for memory allocation). */
190 /* The lowest and highest addresses contained in this compilation
191 unit as specified in the compilation unit header. */
192 struct arange arange;
194 /* The DW_AT_name attribute (for error messages). */
197 /* The abbrev hash table. */
198 struct abbrev_info **abbrevs;
200 /* Note that an error was found by comp_unit_find_nearest_line. */
203 /* The DW_AT_comp_dir attribute. */
206 /* TRUE if there is a line number table associated with this comp. unit. */
209 /* Pointer to the current comp_unit so that we can find a given entry
211 bfd_byte *info_ptr_unit;
213 /* The offset into .debug_line of the line number table. */
214 unsigned long line_offset;
216 /* Pointer to the first child die for the comp unit. */
217 bfd_byte *first_child_die_ptr;
219 /* The end of the comp unit. */
222 /* The decoded line number, NULL if not yet decoded. */
223 struct line_info_table *line_table;
225 /* A list of the functions found in this comp. unit. */
226 struct funcinfo *function_table;
228 /* A list of the variables found in this comp. unit. */
229 struct varinfo *variable_table;
231 /* Pointer to dwarf2_debug structure. */
232 struct dwarf2_debug *stash;
234 /* Address size for this unit - from unit header. */
235 unsigned char addr_size;
237 /* Offset size for this unit - from unit header. */
238 unsigned char offset_size;
240 /* Base address for this unit - from DW_AT_low_pc attribute of
241 DW_TAG_compile_unit DIE */
242 bfd_vma base_address;
244 /* TRUE if symbols are cached in hash table for faster lookup by name. */
248 /* This data structure holds the information of an abbrev. */
251 unsigned int number; /* Number identifying abbrev. */
252 enum dwarf_tag tag; /* DWARF tag. */
253 int has_children; /* Boolean. */
254 unsigned int num_attrs; /* Number of attributes. */
255 struct attr_abbrev *attrs; /* An array of attribute descriptions. */
256 struct abbrev_info *next; /* Next in chain. */
261 enum dwarf_attribute name;
262 enum dwarf_form form;
265 #ifndef ABBREV_HASH_SIZE
266 #define ABBREV_HASH_SIZE 121
268 #ifndef ATTR_ALLOC_CHUNK
269 #define ATTR_ALLOC_CHUNK 4
272 /* Variable and function hash tables. This is used to speed up look-up
273 in lookup_symbol_in_var_table() and lookup_symbol_in_function_table().
274 In order to share code between variable and function infos, we use
275 a list of untyped pointer for all variable/function info associated with
276 a symbol. We waste a bit of memory for list with one node but that
277 simplifies the code. */
279 struct info_list_node
281 struct info_list_node *next;
285 /* Info hash entry. */
286 struct info_hash_entry
288 struct bfd_hash_entry root;
289 struct info_list_node *head;
292 struct info_hash_table
294 struct bfd_hash_table base;
297 /* Function to create a new entry in info hash table. */
299 static struct bfd_hash_entry *
300 info_hash_table_newfunc (struct bfd_hash_entry *entry,
301 struct bfd_hash_table *table,
304 struct info_hash_entry *ret = (struct info_hash_entry *) entry;
306 /* Allocate the structure if it has not already been allocated by a
310 ret = bfd_hash_allocate (table, sizeof (* ret));
315 /* Call the allocation method of the base class. */
316 ret = ((struct info_hash_entry *)
317 bfd_hash_newfunc ((struct bfd_hash_entry *) ret, table, string));
319 /* Initialize the local fields here. */
323 return (struct bfd_hash_entry *) ret;
326 /* Function to create a new info hash table. It returns a pointer to the
327 newly created table or NULL if there is any error. We need abfd
328 solely for memory allocation. */
330 static struct info_hash_table *
331 create_info_hash_table (bfd *abfd)
333 struct info_hash_table *hash_table;
335 hash_table = bfd_alloc (abfd, sizeof (struct info_hash_table));
339 if (!bfd_hash_table_init (&hash_table->base, info_hash_table_newfunc,
340 sizeof (struct info_hash_entry)))
342 bfd_release (abfd, hash_table);
349 /* Insert an info entry into an info hash table. We do not check of
350 duplicate entries. Also, the caller need to guarantee that the
351 right type of info in inserted as info is passed as a void* pointer.
352 This function returns true if there is no error. */
355 insert_info_hash_table (struct info_hash_table *hash_table,
360 struct info_hash_entry *entry;
361 struct info_list_node *node;
363 entry = (struct info_hash_entry*) bfd_hash_lookup (&hash_table->base,
368 node = bfd_hash_allocate (&hash_table->base, sizeof (*node));
373 node->next = entry->head;
379 /* Look up an info entry list from an info hash table. Return NULL
382 static struct info_list_node *
383 lookup_info_hash_table (struct info_hash_table *hash_table, const char *key)
385 struct info_hash_entry *entry;
387 entry = (struct info_hash_entry*) bfd_hash_lookup (&hash_table->base, key,
389 return entry ? entry->head : NULL;
392 /* Read a section into its appropriate place in the dwarf2_debug
393 struct (indicated by SECTION_BUFFER and SECTION_SIZE). If syms is
394 not NULL, use bfd_simple_get_relocated_section_contents to read the
395 section contents, otherwise use bfd_get_section_contents. */
398 read_section (bfd *abfd,
399 const char* section_name, const char* compressed_section_name,
400 asymbol** syms, bfd_uint64_t offset,
401 bfd_byte **section_buffer, bfd_size_type *section_size)
404 bfd_boolean section_is_compressed = FALSE;
406 /* read_section is a noop if the section has already been read. */
410 msec = bfd_get_section_by_name (abfd, section_name);
411 if (! msec && compressed_section_name)
413 msec = bfd_get_section_by_name (abfd, compressed_section_name);
414 section_is_compressed = TRUE;
418 (*_bfd_error_handler) (_("Dwarf Error: Can't find %s section."), section_name);
419 bfd_set_error (bfd_error_bad_value);
425 *section_size = msec->size;
427 = bfd_simple_get_relocated_section_contents (abfd, msec, NULL, syms);
428 if (! *section_buffer)
433 *section_size = msec->rawsize ? msec->rawsize : msec->size;
434 *section_buffer = bfd_malloc (*section_size);
435 if (! *section_buffer)
437 if (! bfd_get_section_contents (abfd, msec, *section_buffer,
442 if (section_is_compressed)
444 if (! bfd_uncompress_section_contents (section_buffer, section_size))
446 (*_bfd_error_handler) (_("Dwarf Error: unable to decompress %s section."), compressed_section_name);
447 bfd_set_error (bfd_error_bad_value);
452 /* It is possible to get a bad value for the offset into the section
453 * that the client wants. Validate it here to avoid trouble later. */
454 if (offset != 0 && offset >= *section_size)
456 (*_bfd_error_handler) (_("Dwarf Error: Offset (%lu) greater than or equal to %s size (%lu)."),
457 offset, section_name, *section_size);
458 bfd_set_error (bfd_error_bad_value);
466 The following function up to the END VERBATIM mark are
467 copied directly from dwarf2read.c. */
469 /* Read dwarf information from a buffer. */
472 read_1_byte (bfd *abfd ATTRIBUTE_UNUSED, bfd_byte *buf)
474 return bfd_get_8 (abfd, buf);
478 read_1_signed_byte (bfd *abfd ATTRIBUTE_UNUSED, bfd_byte *buf)
480 return bfd_get_signed_8 (abfd, buf);
484 read_2_bytes (bfd *abfd, bfd_byte *buf)
486 return bfd_get_16 (abfd, buf);
490 read_4_bytes (bfd *abfd, bfd_byte *buf)
492 return bfd_get_32 (abfd, buf);
496 read_8_bytes (bfd *abfd, bfd_byte *buf)
498 return bfd_get_64 (abfd, buf);
502 read_n_bytes (bfd *abfd ATTRIBUTE_UNUSED,
504 unsigned int size ATTRIBUTE_UNUSED)
506 /* If the size of a host char is 8 bits, we can return a pointer
507 to the buffer, otherwise we have to copy the data to a buffer
508 allocated on the temporary obstack. */
513 read_string (bfd *abfd ATTRIBUTE_UNUSED,
515 unsigned int *bytes_read_ptr)
517 /* Return a pointer to the embedded string. */
518 char *str = (char *) buf;
525 *bytes_read_ptr = strlen (str) + 1;
530 read_indirect_string (struct comp_unit* unit,
532 unsigned int *bytes_read_ptr)
535 struct dwarf2_debug *stash = unit->stash;
538 if (unit->offset_size == 4)
539 offset = read_4_bytes (unit->abfd, buf);
541 offset = read_8_bytes (unit->abfd, buf);
542 *bytes_read_ptr = unit->offset_size;
544 if (! read_section (unit->abfd, ".debug_str", ".zdebug_str",
546 &stash->dwarf_str_buffer, &stash->dwarf_str_size))
549 str = (char *) stash->dwarf_str_buffer + offset;
558 read_address (struct comp_unit *unit, bfd_byte *buf)
560 int signed_vma = get_elf_backend_data (unit->abfd)->sign_extend_vma;
564 switch (unit->addr_size)
567 return bfd_get_signed_64 (unit->abfd, buf);
569 return bfd_get_signed_32 (unit->abfd, buf);
571 return bfd_get_signed_16 (unit->abfd, buf);
578 switch (unit->addr_size)
581 return bfd_get_64 (unit->abfd, buf);
583 return bfd_get_32 (unit->abfd, buf);
585 return bfd_get_16 (unit->abfd, buf);
592 /* Lookup an abbrev_info structure in the abbrev hash table. */
594 static struct abbrev_info *
595 lookup_abbrev (unsigned int number, struct abbrev_info **abbrevs)
597 unsigned int hash_number;
598 struct abbrev_info *abbrev;
600 hash_number = number % ABBREV_HASH_SIZE;
601 abbrev = abbrevs[hash_number];
605 if (abbrev->number == number)
608 abbrev = abbrev->next;
614 /* In DWARF version 2, the description of the debugging information is
615 stored in a separate .debug_abbrev section. Before we read any
616 dies from a section we read in all abbreviations and install them
619 static struct abbrev_info**
620 read_abbrevs (bfd *abfd, bfd_uint64_t offset, struct dwarf2_debug *stash)
622 struct abbrev_info **abbrevs;
623 bfd_byte *abbrev_ptr;
624 struct abbrev_info *cur_abbrev;
625 unsigned int abbrev_number, bytes_read, abbrev_name;
626 unsigned int abbrev_form, hash_number;
629 if (! read_section (abfd, ".debug_abbrev", ".zdebug_abbrev",
631 &stash->dwarf_abbrev_buffer, &stash->dwarf_abbrev_size))
634 amt = sizeof (struct abbrev_info*) * ABBREV_HASH_SIZE;
635 abbrevs = bfd_zalloc (abfd, amt);
637 abbrev_ptr = stash->dwarf_abbrev_buffer + offset;
638 abbrev_number = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
639 abbrev_ptr += bytes_read;
641 /* Loop until we reach an abbrev number of 0. */
642 while (abbrev_number)
644 amt = sizeof (struct abbrev_info);
645 cur_abbrev = bfd_zalloc (abfd, amt);
647 /* Read in abbrev header. */
648 cur_abbrev->number = abbrev_number;
649 cur_abbrev->tag = (enum dwarf_tag)
650 read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
651 abbrev_ptr += bytes_read;
652 cur_abbrev->has_children = read_1_byte (abfd, abbrev_ptr);
655 /* Now read in declarations. */
656 abbrev_name = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
657 abbrev_ptr += bytes_read;
658 abbrev_form = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
659 abbrev_ptr += bytes_read;
663 if ((cur_abbrev->num_attrs % ATTR_ALLOC_CHUNK) == 0)
665 struct attr_abbrev *tmp;
667 amt = cur_abbrev->num_attrs + ATTR_ALLOC_CHUNK;
668 amt *= sizeof (struct attr_abbrev);
669 tmp = bfd_realloc (cur_abbrev->attrs, amt);
674 for (i = 0; i < ABBREV_HASH_SIZE; i++)
676 struct abbrev_info *abbrev = abbrevs[i];
680 free (abbrev->attrs);
681 abbrev = abbrev->next;
686 cur_abbrev->attrs = tmp;
689 cur_abbrev->attrs[cur_abbrev->num_attrs].name
690 = (enum dwarf_attribute) abbrev_name;
691 cur_abbrev->attrs[cur_abbrev->num_attrs++].form
692 = (enum dwarf_form) abbrev_form;
693 abbrev_name = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
694 abbrev_ptr += bytes_read;
695 abbrev_form = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
696 abbrev_ptr += bytes_read;
699 hash_number = abbrev_number % ABBREV_HASH_SIZE;
700 cur_abbrev->next = abbrevs[hash_number];
701 abbrevs[hash_number] = cur_abbrev;
703 /* Get next abbreviation.
704 Under Irix6 the abbreviations for a compilation unit are not
705 always properly terminated with an abbrev number of 0.
706 Exit loop if we encounter an abbreviation which we have
707 already read (which means we are about to read the abbreviations
708 for the next compile unit) or if the end of the abbreviation
710 if ((unsigned int) (abbrev_ptr - stash->dwarf_abbrev_buffer)
711 >= stash->dwarf_abbrev_size)
713 abbrev_number = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
714 abbrev_ptr += bytes_read;
715 if (lookup_abbrev (abbrev_number,abbrevs) != NULL)
722 /* Read an attribute value described by an attribute form. */
725 read_attribute_value (struct attribute *attr,
727 struct comp_unit *unit,
730 bfd *abfd = unit->abfd;
731 unsigned int bytes_read;
732 struct dwarf_block *blk;
735 attr->form = (enum dwarf_form) form;
740 /* FIXME: DWARF3 draft says DW_FORM_ref_addr is offset_size. */
741 case DW_FORM_ref_addr:
742 attr->u.val = read_address (unit, info_ptr);
743 info_ptr += unit->addr_size;
746 amt = sizeof (struct dwarf_block);
747 blk = bfd_alloc (abfd, amt);
748 blk->size = read_2_bytes (abfd, info_ptr);
750 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
751 info_ptr += blk->size;
755 amt = sizeof (struct dwarf_block);
756 blk = bfd_alloc (abfd, amt);
757 blk->size = read_4_bytes (abfd, info_ptr);
759 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
760 info_ptr += blk->size;
764 attr->u.val = read_2_bytes (abfd, info_ptr);
768 attr->u.val = read_4_bytes (abfd, info_ptr);
772 attr->u.val = read_8_bytes (abfd, info_ptr);
776 attr->u.str = read_string (abfd, info_ptr, &bytes_read);
777 info_ptr += bytes_read;
780 attr->u.str = read_indirect_string (unit, info_ptr, &bytes_read);
781 info_ptr += bytes_read;
784 amt = sizeof (struct dwarf_block);
785 blk = bfd_alloc (abfd, amt);
786 blk->size = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
787 info_ptr += bytes_read;
788 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
789 info_ptr += blk->size;
793 amt = sizeof (struct dwarf_block);
794 blk = bfd_alloc (abfd, amt);
795 blk->size = read_1_byte (abfd, info_ptr);
797 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
798 info_ptr += blk->size;
802 attr->u.val = read_1_byte (abfd, info_ptr);
806 attr->u.val = read_1_byte (abfd, info_ptr);
810 attr->u.sval = read_signed_leb128 (abfd, info_ptr, &bytes_read);
811 info_ptr += bytes_read;
814 attr->u.val = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
815 info_ptr += bytes_read;
818 attr->u.val = read_1_byte (abfd, info_ptr);
822 attr->u.val = read_2_bytes (abfd, info_ptr);
826 attr->u.val = read_4_bytes (abfd, info_ptr);
830 attr->u.val = read_8_bytes (abfd, info_ptr);
833 case DW_FORM_ref_udata:
834 attr->u.val = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
835 info_ptr += bytes_read;
837 case DW_FORM_indirect:
838 form = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
839 info_ptr += bytes_read;
840 info_ptr = read_attribute_value (attr, form, unit, info_ptr);
843 (*_bfd_error_handler) (_("Dwarf Error: Invalid or unhandled FORM value: %u."),
845 bfd_set_error (bfd_error_bad_value);
850 /* Read an attribute described by an abbreviated attribute. */
853 read_attribute (struct attribute *attr,
854 struct attr_abbrev *abbrev,
855 struct comp_unit *unit,
858 attr->name = abbrev->name;
859 info_ptr = read_attribute_value (attr, abbrev->form, unit, info_ptr);
863 /* Source line information table routines. */
865 #define FILE_ALLOC_CHUNK 5
866 #define DIR_ALLOC_CHUNK 5
870 struct line_info* prev_line;
875 int end_sequence; /* End of (sequential) code sequence. */
886 struct line_info_table
889 unsigned int num_files;
890 unsigned int num_dirs;
893 struct fileinfo* files;
894 struct line_info* last_line; /* largest VMA */
895 struct line_info* lcl_head; /* local head; used in 'add_line_info' */
898 /* Remember some information about each function. If the function is
899 inlined (DW_TAG_inlined_subroutine) it may have two additional
900 attributes, DW_AT_call_file and DW_AT_call_line, which specify the
901 source code location where this function was inlined. */
905 struct funcinfo *prev_func; /* Pointer to previous function in list of all functions */
906 struct funcinfo *caller_func; /* Pointer to function one scope higher */
907 char *caller_file; /* Source location file name where caller_func inlines this func */
908 int caller_line; /* Source location line number where caller_func inlines this func */
909 char *file; /* Source location file name */
910 int line; /* Source location line number */
913 struct arange arange;
914 asection *sec; /* Where the symbol is defined */
919 /* Pointer to previous variable in list of all variables */
920 struct varinfo *prev_var;
921 /* Source location file name */
923 /* Source location line number */
928 /* Where the symbol is defined */
930 /* Is this a stack variable? */
931 unsigned int stack: 1;
934 /* Return TRUE if NEW_LINE should sort after LINE. */
936 static inline bfd_boolean
937 new_line_sorts_after (struct line_info *new_line, struct line_info *line)
939 return (new_line->address > line->address
940 || (new_line->address == line->address
941 && new_line->end_sequence < line->end_sequence));
945 /* Adds a new entry to the line_info list in the line_info_table, ensuring
946 that the list is sorted. Note that the line_info list is sorted from
947 highest to lowest VMA (with possible duplicates); that is,
948 line_info->prev_line always accesses an equal or smaller VMA. */
951 add_line_info (struct line_info_table *table,
958 bfd_size_type amt = sizeof (struct line_info);
959 struct line_info* info = bfd_alloc (table->abfd, amt);
961 /* Set member data of 'info'. */
962 info->address = address;
964 info->column = column;
965 info->end_sequence = end_sequence;
967 if (filename && filename[0])
969 info->filename = bfd_alloc (table->abfd, strlen (filename) + 1);
971 strcpy (info->filename, filename);
974 info->filename = NULL;
976 /* Find the correct location for 'info'. Normally we will receive
977 new line_info data 1) in order and 2) with increasing VMAs.
978 However some compilers break the rules (cf. decode_line_info) and
979 so we include some heuristics for quickly finding the correct
980 location for 'info'. In particular, these heuristics optimize for
981 the common case in which the VMA sequence that we receive is a
982 list of locally sorted VMAs such as
983 p...z a...j (where a < j < p < z)
985 Note: table->lcl_head is used to head an *actual* or *possible*
986 sequence within the list (such as a...j) that is not directly
987 headed by table->last_line
989 Note: we may receive duplicate entries from 'decode_line_info'. */
992 && table->last_line->address == address
993 && table->last_line->end_sequence == end_sequence)
995 /* We only keep the last entry with the same address and end
996 sequence. See PR ld/4986. */
997 if (table->lcl_head == table->last_line)
998 table->lcl_head = info;
999 info->prev_line = table->last_line->prev_line;
1000 table->last_line = info;
1002 else if (!table->last_line
1003 || new_line_sorts_after (info, table->last_line))
1005 /* Normal case: add 'info' to the beginning of the list */
1006 info->prev_line = table->last_line;
1007 table->last_line = info;
1009 /* lcl_head: initialize to head a *possible* sequence at the end. */
1010 if (!table->lcl_head)
1011 table->lcl_head = info;
1013 else if (!new_line_sorts_after (info, table->lcl_head)
1014 && (!table->lcl_head->prev_line
1015 || new_line_sorts_after (info, table->lcl_head->prev_line)))
1017 /* Abnormal but easy: lcl_head is the head of 'info'. */
1018 info->prev_line = table->lcl_head->prev_line;
1019 table->lcl_head->prev_line = info;
1023 /* Abnormal and hard: Neither 'last_line' nor 'lcl_head' are valid
1024 heads for 'info'. Reset 'lcl_head'. */
1025 struct line_info* li2 = table->last_line; /* always non-NULL */
1026 struct line_info* li1 = li2->prev_line;
1030 if (!new_line_sorts_after (info, li2)
1031 && new_line_sorts_after (info, li1))
1034 li2 = li1; /* always non-NULL */
1035 li1 = li1->prev_line;
1037 table->lcl_head = li2;
1038 info->prev_line = table->lcl_head->prev_line;
1039 table->lcl_head->prev_line = info;
1043 /* Extract a fully qualified filename from a line info table.
1044 The returned string has been malloc'ed and it is the caller's
1045 responsibility to free it. */
1048 concat_filename (struct line_info_table *table, unsigned int file)
1052 if (file - 1 >= table->num_files)
1054 /* FILE == 0 means unknown. */
1056 (*_bfd_error_handler)
1057 (_("Dwarf Error: mangled line number section (bad file number)."));
1058 return strdup ("<unknown>");
1061 filename = table->files[file - 1].name;
1063 if (!IS_ABSOLUTE_PATH (filename))
1065 char *dirname = NULL;
1066 char *subdirname = NULL;
1070 if (table->files[file - 1].dir)
1071 subdirname = table->dirs[table->files[file - 1].dir - 1];
1073 if (!subdirname || !IS_ABSOLUTE_PATH (subdirname))
1074 dirname = table->comp_dir;
1078 dirname = subdirname;
1083 return strdup (filename);
1085 len = strlen (dirname) + strlen (filename) + 2;
1089 len += strlen (subdirname) + 1;
1090 name = bfd_malloc (len);
1092 sprintf (name, "%s/%s/%s", dirname, subdirname, filename);
1096 name = bfd_malloc (len);
1098 sprintf (name, "%s/%s", dirname, filename);
1104 return strdup (filename);
1108 arange_add (bfd *abfd, struct arange *first_arange, bfd_vma low_pc, bfd_vma high_pc)
1110 struct arange *arange;
1112 /* If the first arange is empty, use it. */
1113 if (first_arange->high == 0)
1115 first_arange->low = low_pc;
1116 first_arange->high = high_pc;
1120 /* Next see if we can cheaply extend an existing range. */
1121 arange = first_arange;
1124 if (low_pc == arange->high)
1126 arange->high = high_pc;
1129 if (high_pc == arange->low)
1131 arange->low = low_pc;
1134 arange = arange->next;
1138 /* Need to allocate a new arange and insert it into the arange list.
1139 Order isn't significant, so just insert after the first arange. */
1140 arange = bfd_zalloc (abfd, sizeof (*arange));
1141 arange->low = low_pc;
1142 arange->high = high_pc;
1143 arange->next = first_arange->next;
1144 first_arange->next = arange;
1147 /* Decode the line number information for UNIT. */
1149 static struct line_info_table*
1150 decode_line_info (struct comp_unit *unit, struct dwarf2_debug *stash)
1152 bfd *abfd = unit->abfd;
1153 struct line_info_table* table;
1156 struct line_head lh;
1157 unsigned int i, bytes_read, offset_size;
1158 char *cur_file, *cur_dir;
1159 unsigned char op_code, extended_op, adj_opcode;
1162 if (! read_section (abfd, ".debug_line", ".zdebug_line",
1163 stash->syms, unit->line_offset,
1164 &stash->dwarf_line_buffer, &stash->dwarf_line_size))
1167 amt = sizeof (struct line_info_table);
1168 table = bfd_alloc (abfd, amt);
1170 table->comp_dir = unit->comp_dir;
1172 table->num_files = 0;
1173 table->files = NULL;
1175 table->num_dirs = 0;
1178 table->files = NULL;
1179 table->last_line = NULL;
1180 table->lcl_head = NULL;
1182 line_ptr = stash->dwarf_line_buffer + unit->line_offset;
1184 /* Read in the prologue. */
1185 lh.total_length = read_4_bytes (abfd, line_ptr);
1188 if (lh.total_length == 0xffffffff)
1190 lh.total_length = read_8_bytes (abfd, line_ptr);
1194 else if (lh.total_length == 0 && unit->addr_size == 8)
1196 /* Handle (non-standard) 64-bit DWARF2 formats. */
1197 lh.total_length = read_4_bytes (abfd, line_ptr);
1201 line_end = line_ptr + lh.total_length;
1202 lh.version = read_2_bytes (abfd, line_ptr);
1204 if (offset_size == 4)
1205 lh.prologue_length = read_4_bytes (abfd, line_ptr);
1207 lh.prologue_length = read_8_bytes (abfd, line_ptr);
1208 line_ptr += offset_size;
1209 lh.minimum_instruction_length = read_1_byte (abfd, line_ptr);
1211 lh.default_is_stmt = read_1_byte (abfd, line_ptr);
1213 lh.line_base = read_1_signed_byte (abfd, line_ptr);
1215 lh.line_range = read_1_byte (abfd, line_ptr);
1217 lh.opcode_base = read_1_byte (abfd, line_ptr);
1219 amt = lh.opcode_base * sizeof (unsigned char);
1220 lh.standard_opcode_lengths = bfd_alloc (abfd, amt);
1222 lh.standard_opcode_lengths[0] = 1;
1224 for (i = 1; i < lh.opcode_base; ++i)
1226 lh.standard_opcode_lengths[i] = read_1_byte (abfd, line_ptr);
1230 /* Read directory table. */
1231 while ((cur_dir = read_string (abfd, line_ptr, &bytes_read)) != NULL)
1233 line_ptr += bytes_read;
1235 if ((table->num_dirs % DIR_ALLOC_CHUNK) == 0)
1239 amt = table->num_dirs + DIR_ALLOC_CHUNK;
1240 amt *= sizeof (char *);
1242 tmp = bfd_realloc (table->dirs, amt);
1251 table->dirs[table->num_dirs++] = cur_dir;
1254 line_ptr += bytes_read;
1256 /* Read file name table. */
1257 while ((cur_file = read_string (abfd, line_ptr, &bytes_read)) != NULL)
1259 line_ptr += bytes_read;
1261 if ((table->num_files % FILE_ALLOC_CHUNK) == 0)
1263 struct fileinfo *tmp;
1265 amt = table->num_files + FILE_ALLOC_CHUNK;
1266 amt *= sizeof (struct fileinfo);
1268 tmp = bfd_realloc (table->files, amt);
1271 free (table->files);
1278 table->files[table->num_files].name = cur_file;
1279 table->files[table->num_files].dir =
1280 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
1281 line_ptr += bytes_read;
1282 table->files[table->num_files].time =
1283 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
1284 line_ptr += bytes_read;
1285 table->files[table->num_files].size =
1286 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
1287 line_ptr += bytes_read;
1291 line_ptr += bytes_read;
1293 /* Read the statement sequences until there's nothing left. */
1294 while (line_ptr < line_end)
1296 /* State machine registers. */
1297 bfd_vma address = 0;
1298 char * filename = table->num_files ? concat_filename (table, 1) : NULL;
1299 unsigned int line = 1;
1300 unsigned int column = 0;
1301 int is_stmt = lh.default_is_stmt;
1302 int end_sequence = 0;
1303 /* eraxxon@alumni.rice.edu: Against the DWARF2 specs, some
1304 compilers generate address sequences that are wildly out of
1305 order using DW_LNE_set_address (e.g. Intel C++ 6.0 compiler
1306 for ia64-Linux). Thus, to determine the low and high
1307 address, we must compare on every DW_LNS_copy, etc. */
1308 bfd_vma low_pc = (bfd_vma) -1;
1309 bfd_vma high_pc = 0;
1311 /* Decode the table. */
1312 while (! end_sequence)
1314 op_code = read_1_byte (abfd, line_ptr);
1317 if (op_code >= lh.opcode_base)
1319 /* Special operand. */
1320 adj_opcode = op_code - lh.opcode_base;
1321 address += (adj_opcode / lh.line_range)
1322 * lh.minimum_instruction_length;
1323 line += lh.line_base + (adj_opcode % lh.line_range);
1324 /* Append row to matrix using current values. */
1325 add_line_info (table, address, filename, line, column, 0);
1326 if (address < low_pc)
1328 if (address > high_pc)
1331 else switch (op_code)
1333 case DW_LNS_extended_op:
1334 /* Ignore length. */
1336 extended_op = read_1_byte (abfd, line_ptr);
1339 switch (extended_op)
1341 case DW_LNE_end_sequence:
1343 add_line_info (table, address, filename, line, column,
1345 if (address < low_pc)
1347 if (address > high_pc)
1349 arange_add (unit->abfd, &unit->arange, low_pc, high_pc);
1351 case DW_LNE_set_address:
1352 address = read_address (unit, line_ptr);
1353 line_ptr += unit->addr_size;
1355 case DW_LNE_define_file:
1356 cur_file = read_string (abfd, line_ptr, &bytes_read);
1357 line_ptr += bytes_read;
1358 if ((table->num_files % FILE_ALLOC_CHUNK) == 0)
1360 struct fileinfo *tmp;
1362 amt = table->num_files + FILE_ALLOC_CHUNK;
1363 amt *= sizeof (struct fileinfo);
1364 tmp = bfd_realloc (table->files, amt);
1367 free (table->files);
1374 table->files[table->num_files].name = cur_file;
1375 table->files[table->num_files].dir =
1376 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
1377 line_ptr += bytes_read;
1378 table->files[table->num_files].time =
1379 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
1380 line_ptr += bytes_read;
1381 table->files[table->num_files].size =
1382 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
1383 line_ptr += bytes_read;
1387 (*_bfd_error_handler) (_("Dwarf Error: mangled line number section."));
1388 bfd_set_error (bfd_error_bad_value);
1390 free (table->files);
1396 add_line_info (table, address, filename, line, column, 0);
1397 if (address < low_pc)
1399 if (address > high_pc)
1402 case DW_LNS_advance_pc:
1403 address += lh.minimum_instruction_length
1404 * read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
1405 line_ptr += bytes_read;
1407 case DW_LNS_advance_line:
1408 line += read_signed_leb128 (abfd, line_ptr, &bytes_read);
1409 line_ptr += bytes_read;
1411 case DW_LNS_set_file:
1415 /* The file and directory tables are 0
1416 based, the references are 1 based. */
1417 file = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
1418 line_ptr += bytes_read;
1421 filename = concat_filename (table, file);
1424 case DW_LNS_set_column:
1425 column = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
1426 line_ptr += bytes_read;
1428 case DW_LNS_negate_stmt:
1429 is_stmt = (!is_stmt);
1431 case DW_LNS_set_basic_block:
1433 case DW_LNS_const_add_pc:
1434 address += lh.minimum_instruction_length
1435 * ((255 - lh.opcode_base) / lh.line_range);
1437 case DW_LNS_fixed_advance_pc:
1438 address += read_2_bytes (abfd, line_ptr);
1445 /* Unknown standard opcode, ignore it. */
1446 for (i = 0; i < lh.standard_opcode_lengths[op_code]; i++)
1448 (void) read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
1449 line_ptr += bytes_read;
1462 /* If ADDR is within TABLE set the output parameters and return TRUE,
1463 otherwise return FALSE. The output parameters, FILENAME_PTR and
1464 LINENUMBER_PTR, are pointers to the objects to be filled in. */
1467 lookup_address_in_line_info_table (struct line_info_table *table,
1469 struct funcinfo *function,
1470 const char **filename_ptr,
1471 unsigned int *linenumber_ptr)
1473 /* Note: table->last_line should be a descendingly sorted list. */
1474 struct line_info* next_line = table->last_line;
1475 struct line_info* each_line = NULL;
1476 *filename_ptr = NULL;
1481 each_line = next_line->prev_line;
1483 /* Check for large addresses */
1484 if (addr > next_line->address)
1485 each_line = NULL; /* ensure we skip over the normal case */
1487 /* Normal case: search the list; save */
1488 while (each_line && next_line)
1490 /* If we have an address match, save this info. This allows us
1491 to return as good as results as possible for strange debugging
1493 bfd_boolean addr_match = FALSE;
1494 if (each_line->address <= addr && addr < next_line->address)
1498 /* If this line appears to span functions, and addr is in the
1499 later function, return the first line of that function instead
1500 of the last line of the earlier one. This check is for GCC
1501 2.95, which emits the first line number for a function late. */
1503 if (function != NULL)
1506 struct arange *arange;
1508 /* Find the lowest address in the function's range list */
1509 lowest_pc = function->arange.low;
1510 for (arange = &function->arange;
1512 arange = arange->next)
1514 if (function->arange.low < lowest_pc)
1515 lowest_pc = function->arange.low;
1517 /* Check for spanning function and set outgoing line info */
1518 if (addr >= lowest_pc
1519 && each_line->address < lowest_pc
1520 && next_line->address > lowest_pc)
1522 *filename_ptr = next_line->filename;
1523 *linenumber_ptr = next_line->line;
1527 *filename_ptr = each_line->filename;
1528 *linenumber_ptr = each_line->line;
1533 *filename_ptr = each_line->filename;
1534 *linenumber_ptr = each_line->line;
1538 if (addr_match && !each_line->end_sequence)
1539 return TRUE; /* we have definitely found what we want */
1541 next_line = each_line;
1542 each_line = each_line->prev_line;
1545 /* At this point each_line is NULL but next_line is not. If we found
1546 a candidate end-of-sequence point in the loop above, we can return
1547 that (compatibility with a bug in the Intel compiler); otherwise,
1548 assuming that we found the containing function for this address in
1549 this compilation unit, return the first line we have a number for
1550 (compatibility with GCC 2.95). */
1551 if (*filename_ptr == NULL && function != NULL)
1553 *filename_ptr = next_line->filename;
1554 *linenumber_ptr = next_line->line;
1561 /* Read in the .debug_ranges section for future reference */
1564 read_debug_ranges (struct comp_unit *unit)
1566 struct dwarf2_debug *stash = unit->stash;
1567 return read_section (unit->abfd, ".debug_ranges", ".zdebug_ranges",
1569 &stash->dwarf_ranges_buffer, &stash->dwarf_ranges_size);
1572 /* Function table functions. */
1574 /* If ADDR is within TABLE, set FUNCTIONNAME_PTR, and return TRUE.
1575 Note that we need to find the function that has the smallest
1576 range that contains ADDR, to handle inlined functions without
1577 depending upon them being ordered in TABLE by increasing range. */
1580 lookup_address_in_function_table (struct comp_unit *unit,
1582 struct funcinfo **function_ptr,
1583 const char **functionname_ptr)
1585 struct funcinfo* each_func;
1586 struct funcinfo* best_fit = NULL;
1587 struct arange *arange;
1589 for (each_func = unit->function_table;
1591 each_func = each_func->prev_func)
1593 for (arange = &each_func->arange;
1595 arange = arange->next)
1597 if (addr >= arange->low && addr < arange->high)
1600 ((arange->high - arange->low) < (best_fit->arange.high - best_fit->arange.low)))
1601 best_fit = each_func;
1608 *functionname_ptr = best_fit->name;
1609 *function_ptr = best_fit;
1618 /* If SYM at ADDR is within function table of UNIT, set FILENAME_PTR
1619 and LINENUMBER_PTR, and return TRUE. */
1622 lookup_symbol_in_function_table (struct comp_unit *unit,
1625 const char **filename_ptr,
1626 unsigned int *linenumber_ptr)
1628 struct funcinfo* each_func;
1629 struct funcinfo* best_fit = NULL;
1630 struct arange *arange;
1631 const char *name = bfd_asymbol_name (sym);
1632 asection *sec = bfd_get_section (sym);
1634 for (each_func = unit->function_table;
1636 each_func = each_func->prev_func)
1638 for (arange = &each_func->arange;
1640 arange = arange->next)
1642 if ((!each_func->sec || each_func->sec == sec)
1643 && addr >= arange->low
1644 && addr < arange->high
1646 && strcmp (name, each_func->name) == 0
1648 || ((arange->high - arange->low)
1649 < (best_fit->arange.high - best_fit->arange.low))))
1650 best_fit = each_func;
1656 best_fit->sec = sec;
1657 *filename_ptr = best_fit->file;
1658 *linenumber_ptr = best_fit->line;
1665 /* Variable table functions. */
1667 /* If SYM is within variable table of UNIT, set FILENAME_PTR and
1668 LINENUMBER_PTR, and return TRUE. */
1671 lookup_symbol_in_variable_table (struct comp_unit *unit,
1674 const char **filename_ptr,
1675 unsigned int *linenumber_ptr)
1677 const char *name = bfd_asymbol_name (sym);
1678 asection *sec = bfd_get_section (sym);
1679 struct varinfo* each;
1681 for (each = unit->variable_table; each; each = each->prev_var)
1682 if (each->stack == 0
1683 && each->file != NULL
1684 && each->name != NULL
1685 && each->addr == addr
1686 && (!each->sec || each->sec == sec)
1687 && strcmp (name, each->name) == 0)
1693 *filename_ptr = each->file;
1694 *linenumber_ptr = each->line;
1702 find_abstract_instance_name (struct comp_unit *unit, bfd_uint64_t die_ref)
1704 bfd *abfd = unit->abfd;
1706 unsigned int abbrev_number, bytes_read, i;
1707 struct abbrev_info *abbrev;
1708 struct attribute attr;
1711 info_ptr = unit->info_ptr_unit + die_ref;
1712 abbrev_number = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
1713 info_ptr += bytes_read;
1717 abbrev = lookup_abbrev (abbrev_number, unit->abbrevs);
1720 (*_bfd_error_handler) (_("Dwarf Error: Could not find abbrev number %u."),
1722 bfd_set_error (bfd_error_bad_value);
1726 for (i = 0; i < abbrev->num_attrs; ++i)
1728 info_ptr = read_attribute (&attr, &abbrev->attrs[i], unit, info_ptr);
1732 /* Prefer DW_AT_MIPS_linkage_name over DW_AT_name. */
1736 case DW_AT_specification:
1737 name = find_abstract_instance_name (unit, attr.u.val);
1739 case DW_AT_MIPS_linkage_name:
1752 read_rangelist (struct comp_unit *unit, struct arange *arange, bfd_uint64_t offset)
1754 bfd_byte *ranges_ptr;
1755 bfd_vma base_address = unit->base_address;
1757 if (! unit->stash->dwarf_ranges_buffer)
1759 if (! read_debug_ranges (unit))
1762 ranges_ptr = unit->stash->dwarf_ranges_buffer + offset;
1769 if (unit->addr_size == 4)
1771 low_pc = read_4_bytes (unit->abfd, ranges_ptr);
1773 high_pc = read_4_bytes (unit->abfd, ranges_ptr);
1778 low_pc = read_8_bytes (unit->abfd, ranges_ptr);
1780 high_pc = read_8_bytes (unit->abfd, ranges_ptr);
1783 if (low_pc == 0 && high_pc == 0)
1785 if (low_pc == -1UL && high_pc != -1UL)
1786 base_address = high_pc;
1788 arange_add (unit->abfd, arange, base_address + low_pc, base_address + high_pc);
1792 /* DWARF2 Compilation unit functions. */
1794 /* Scan over each die in a comp. unit looking for functions to add
1795 to the function table and variables to the variable table. */
1798 scan_unit_for_symbols (struct comp_unit *unit)
1800 bfd *abfd = unit->abfd;
1801 bfd_byte *info_ptr = unit->first_child_die_ptr;
1802 int nesting_level = 1;
1803 struct funcinfo **nested_funcs;
1804 int nested_funcs_size;
1806 /* Maintain a stack of in-scope functions and inlined functions, which we
1807 can use to set the caller_func field. */
1808 nested_funcs_size = 32;
1809 nested_funcs = bfd_malloc (nested_funcs_size * sizeof (struct funcinfo *));
1810 if (nested_funcs == NULL)
1812 nested_funcs[nesting_level] = 0;
1814 while (nesting_level)
1816 unsigned int abbrev_number, bytes_read, i;
1817 struct abbrev_info *abbrev;
1818 struct attribute attr;
1819 struct funcinfo *func;
1820 struct varinfo *var;
1822 bfd_vma high_pc = 0;
1824 abbrev_number = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
1825 info_ptr += bytes_read;
1827 if (! abbrev_number)
1833 abbrev = lookup_abbrev (abbrev_number,unit->abbrevs);
1836 (*_bfd_error_handler) (_("Dwarf Error: Could not find abbrev number %u."),
1838 bfd_set_error (bfd_error_bad_value);
1839 free (nested_funcs);
1844 if (abbrev->tag == DW_TAG_subprogram
1845 || abbrev->tag == DW_TAG_entry_point
1846 || abbrev->tag == DW_TAG_inlined_subroutine)
1848 bfd_size_type amt = sizeof (struct funcinfo);
1849 func = bfd_zalloc (abfd, amt);
1850 func->tag = abbrev->tag;
1851 func->prev_func = unit->function_table;
1852 unit->function_table = func;
1853 BFD_ASSERT (!unit->cached);
1855 if (func->tag == DW_TAG_inlined_subroutine)
1856 for (i = nesting_level - 1; i >= 1; i--)
1857 if (nested_funcs[i])
1859 func->caller_func = nested_funcs[i];
1862 nested_funcs[nesting_level] = func;
1867 if (abbrev->tag == DW_TAG_variable)
1869 bfd_size_type amt = sizeof (struct varinfo);
1870 var = bfd_zalloc (abfd, amt);
1871 var->tag = abbrev->tag;
1873 var->prev_var = unit->variable_table;
1874 unit->variable_table = var;
1875 BFD_ASSERT (!unit->cached);
1878 /* No inline function in scope at this nesting level. */
1879 nested_funcs[nesting_level] = 0;
1882 for (i = 0; i < abbrev->num_attrs; ++i)
1884 info_ptr = read_attribute (&attr, &abbrev->attrs[i], unit, info_ptr);
1890 case DW_AT_call_file:
1891 func->caller_file = concat_filename (unit->line_table, attr.u.val);
1894 case DW_AT_call_line:
1895 func->caller_line = attr.u.val;
1898 case DW_AT_abstract_origin:
1899 func->name = find_abstract_instance_name (unit, attr.u.val);
1903 /* Prefer DW_AT_MIPS_linkage_name over DW_AT_name. */
1904 if (func->name == NULL)
1905 func->name = attr.u.str;
1908 case DW_AT_MIPS_linkage_name:
1909 func->name = attr.u.str;
1913 low_pc = attr.u.val;
1917 high_pc = attr.u.val;
1921 read_rangelist (unit, &func->arange, attr.u.val);
1924 case DW_AT_decl_file:
1925 func->file = concat_filename (unit->line_table,
1929 case DW_AT_decl_line:
1930 func->line = attr.u.val;
1942 var->name = attr.u.str;
1945 case DW_AT_decl_file:
1946 var->file = concat_filename (unit->line_table,
1950 case DW_AT_decl_line:
1951 var->line = attr.u.val;
1954 case DW_AT_external:
1955 if (attr.u.val != 0)
1959 case DW_AT_location:
1963 case DW_FORM_block1:
1964 case DW_FORM_block2:
1965 case DW_FORM_block4:
1966 if (*attr.u.blk->data == DW_OP_addr)
1970 /* Verify that DW_OP_addr is the only opcode in the
1971 location, in which case the block size will be 1
1972 plus the address size. */
1973 /* ??? For TLS variables, gcc can emit
1974 DW_OP_addr <addr> DW_OP_GNU_push_tls_address
1975 which we don't handle here yet. */
1976 if (attr.u.blk->size == unit->addr_size + 1U)
1977 var->addr = bfd_get (unit->addr_size * 8,
1979 attr.u.blk->data + 1);
1994 if (func && high_pc != 0)
1996 arange_add (unit->abfd, &func->arange, low_pc, high_pc);
1999 if (abbrev->has_children)
2003 if (nesting_level >= nested_funcs_size)
2005 struct funcinfo **tmp;
2007 nested_funcs_size *= 2;
2008 tmp = bfd_realloc (nested_funcs,
2010 * sizeof (struct funcinfo *)));
2013 free (nested_funcs);
2018 nested_funcs[nesting_level] = 0;
2022 free (nested_funcs);
2026 /* Parse a DWARF2 compilation unit starting at INFO_PTR. This
2027 includes the compilation unit header that proceeds the DIE's, but
2028 does not include the length field that precedes each compilation
2029 unit header. END_PTR points one past the end of this comp unit.
2030 OFFSET_SIZE is the size of DWARF2 offsets (either 4 or 8 bytes).
2032 This routine does not read the whole compilation unit; only enough
2033 to get to the line number information for the compilation unit. */
2035 static struct comp_unit *
2036 parse_comp_unit (struct dwarf2_debug *stash,
2037 bfd_vma unit_length,
2038 bfd_byte *info_ptr_unit,
2039 unsigned int offset_size)
2041 struct comp_unit* unit;
2042 unsigned int version;
2043 bfd_uint64_t abbrev_offset = 0;
2044 unsigned int addr_size;
2045 struct abbrev_info** abbrevs;
2046 unsigned int abbrev_number, bytes_read, i;
2047 struct abbrev_info *abbrev;
2048 struct attribute attr;
2049 bfd_byte *info_ptr = stash->info_ptr;
2050 bfd_byte *end_ptr = info_ptr + unit_length;
2053 bfd_vma high_pc = 0;
2054 bfd *abfd = stash->bfd;
2056 version = read_2_bytes (abfd, info_ptr);
2058 BFD_ASSERT (offset_size == 4 || offset_size == 8);
2059 if (offset_size == 4)
2060 abbrev_offset = read_4_bytes (abfd, info_ptr);
2062 abbrev_offset = read_8_bytes (abfd, info_ptr);
2063 info_ptr += offset_size;
2064 addr_size = read_1_byte (abfd, info_ptr);
2069 (*_bfd_error_handler) (_("Dwarf Error: found dwarf version '%u', this reader only handles version 2 information."), version);
2070 bfd_set_error (bfd_error_bad_value);
2074 if (addr_size > sizeof (bfd_vma))
2076 (*_bfd_error_handler) (_("Dwarf Error: found address size '%u', this reader can not handle sizes greater than '%u'."),
2078 (unsigned int) sizeof (bfd_vma));
2079 bfd_set_error (bfd_error_bad_value);
2083 if (addr_size != 2 && addr_size != 4 && addr_size != 8)
2085 (*_bfd_error_handler) ("Dwarf Error: found address size '%u', this reader can only handle address sizes '2', '4' and '8'.", addr_size);
2086 bfd_set_error (bfd_error_bad_value);
2090 /* Read the abbrevs for this compilation unit into a table. */
2091 abbrevs = read_abbrevs (abfd, abbrev_offset, stash);
2095 abbrev_number = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
2096 info_ptr += bytes_read;
2097 if (! abbrev_number)
2099 (*_bfd_error_handler) (_("Dwarf Error: Bad abbrev number: %u."),
2101 bfd_set_error (bfd_error_bad_value);
2105 abbrev = lookup_abbrev (abbrev_number, abbrevs);
2108 (*_bfd_error_handler) (_("Dwarf Error: Could not find abbrev number %u."),
2110 bfd_set_error (bfd_error_bad_value);
2114 amt = sizeof (struct comp_unit);
2115 unit = bfd_zalloc (abfd, amt);
2117 unit->addr_size = addr_size;
2118 unit->offset_size = offset_size;
2119 unit->abbrevs = abbrevs;
2120 unit->end_ptr = end_ptr;
2121 unit->stash = stash;
2122 unit->info_ptr_unit = info_ptr_unit;
2124 for (i = 0; i < abbrev->num_attrs; ++i)
2126 info_ptr = read_attribute (&attr, &abbrev->attrs[i], unit, info_ptr);
2128 /* Store the data if it is of an attribute we want to keep in a
2129 partial symbol table. */
2132 case DW_AT_stmt_list:
2134 unit->line_offset = attr.u.val;
2138 unit->name = attr.u.str;
2142 low_pc = attr.u.val;
2143 /* If the compilation unit DIE has a DW_AT_low_pc attribute,
2144 this is the base address to use when reading location
2145 lists or range lists. */
2146 unit->base_address = low_pc;
2150 high_pc = attr.u.val;
2154 read_rangelist (unit, &unit->arange, attr.u.val);
2157 case DW_AT_comp_dir:
2159 char *comp_dir = attr.u.str;
2162 /* Irix 6.2 native cc prepends <machine>.: to the compilation
2163 directory, get rid of it. */
2164 char *cp = strchr (comp_dir, ':');
2166 if (cp && cp != comp_dir && cp[-1] == '.' && cp[1] == '/')
2169 unit->comp_dir = comp_dir;
2179 arange_add (unit->abfd, &unit->arange, low_pc, high_pc);
2182 unit->first_child_die_ptr = info_ptr;
2186 /* Return TRUE if UNIT may contain the address given by ADDR. When
2187 there are functions written entirely with inline asm statements, the
2188 range info in the compilation unit header may not be correct. We
2189 need to consult the line info table to see if a compilation unit
2190 really contains the given address. */
2193 comp_unit_contains_address (struct comp_unit *unit, bfd_vma addr)
2195 struct arange *arange;
2200 arange = &unit->arange;
2203 if (addr >= arange->low && addr < arange->high)
2205 arange = arange->next;
2212 /* If UNIT contains ADDR, set the output parameters to the values for
2213 the line containing ADDR. The output parameters, FILENAME_PTR,
2214 FUNCTIONNAME_PTR, and LINENUMBER_PTR, are pointers to the objects
2217 Return TRUE if UNIT contains ADDR, and no errors were encountered;
2221 comp_unit_find_nearest_line (struct comp_unit *unit,
2223 const char **filename_ptr,
2224 const char **functionname_ptr,
2225 unsigned int *linenumber_ptr,
2226 struct dwarf2_debug *stash)
2230 struct funcinfo *function;
2235 if (! unit->line_table)
2237 if (! unit->stmtlist)
2243 unit->line_table = decode_line_info (unit, stash);
2245 if (! unit->line_table)
2251 if (unit->first_child_die_ptr < unit->end_ptr
2252 && ! scan_unit_for_symbols (unit))
2260 func_p = lookup_address_in_function_table (unit, addr,
2261 &function, functionname_ptr);
2262 if (func_p && (function->tag == DW_TAG_inlined_subroutine))
2263 stash->inliner_chain = function;
2264 line_p = lookup_address_in_line_info_table (unit->line_table, addr,
2265 function, filename_ptr,
2267 return line_p || func_p;
2270 /* Check to see if line info is already decoded in a comp_unit.
2271 If not, decode it. Returns TRUE if no errors were encountered;
2275 comp_unit_maybe_decode_line_info (struct comp_unit *unit,
2276 struct dwarf2_debug *stash)
2281 if (! unit->line_table)
2283 if (! unit->stmtlist)
2289 unit->line_table = decode_line_info (unit, stash);
2291 if (! unit->line_table)
2297 if (unit->first_child_die_ptr < unit->end_ptr
2298 && ! scan_unit_for_symbols (unit))
2308 /* If UNIT contains SYM at ADDR, set the output parameters to the
2309 values for the line containing SYM. The output parameters,
2310 FILENAME_PTR, and LINENUMBER_PTR, are pointers to the objects to be
2313 Return TRUE if UNIT contains SYM, and no errors were encountered;
2317 comp_unit_find_line (struct comp_unit *unit,
2320 const char **filename_ptr,
2321 unsigned int *linenumber_ptr,
2322 struct dwarf2_debug *stash)
2324 if (!comp_unit_maybe_decode_line_info (unit, stash))
2327 if (sym->flags & BSF_FUNCTION)
2328 return lookup_symbol_in_function_table (unit, sym, addr,
2332 return lookup_symbol_in_variable_table (unit, sym, addr,
2337 static struct funcinfo *
2338 reverse_funcinfo_list (struct funcinfo *head)
2340 struct funcinfo *rhead;
2341 struct funcinfo *temp;
2343 for (rhead = NULL; head; head = temp)
2345 temp = head->prev_func;
2346 head->prev_func = rhead;
2352 static struct varinfo *
2353 reverse_varinfo_list (struct varinfo *head)
2355 struct varinfo *rhead;
2356 struct varinfo *temp;
2358 for (rhead = NULL; head; head = temp)
2360 temp = head->prev_var;
2361 head->prev_var = rhead;
2367 /* Extract all interesting funcinfos and varinfos of a compilation
2368 unit into hash tables for faster lookup. Returns TRUE if no
2369 errors were enountered; FALSE otherwise. */
2372 comp_unit_hash_info (struct dwarf2_debug *stash,
2373 struct comp_unit *unit,
2374 struct info_hash_table *funcinfo_hash_table,
2375 struct info_hash_table *varinfo_hash_table)
2377 struct funcinfo* each_func;
2378 struct varinfo* each_var;
2379 bfd_boolean okay = TRUE;
2381 BFD_ASSERT (stash->info_hash_status != STASH_INFO_HASH_DISABLED);
2383 if (!comp_unit_maybe_decode_line_info (unit, stash))
2386 BFD_ASSERT (!unit->cached);
2388 /* To preserve the original search order, we went to visit the function
2389 infos in the reversed order of the list. However, making the list
2390 bi-directional use quite a bit of extra memory. So we reverse
2391 the list first, traverse the list in the now reversed order and
2392 finally reverse the list again to get back the original order. */
2393 unit->function_table = reverse_funcinfo_list (unit->function_table);
2394 for (each_func = unit->function_table;
2396 each_func = each_func->prev_func)
2398 /* Skip nameless functions. */
2399 if (each_func->name)
2400 /* There is no need to copy name string into hash table as
2401 name string is either in the dwarf string buffer or
2402 info in the stash. */
2403 okay = insert_info_hash_table (funcinfo_hash_table, each_func->name,
2404 (void*) each_func, FALSE);
2406 unit->function_table = reverse_funcinfo_list (unit->function_table);
2410 /* We do the same for variable infos. */
2411 unit->variable_table = reverse_varinfo_list (unit->variable_table);
2412 for (each_var = unit->variable_table;
2414 each_var = each_var->prev_var)
2416 /* Skip stack vars and vars with no files or names. */
2417 if (each_var->stack == 0
2418 && each_var->file != NULL
2419 && each_var->name != NULL)
2420 /* There is no need to copy name string into hash table as
2421 name string is either in the dwarf string buffer or
2422 info in the stash. */
2423 okay = insert_info_hash_table (varinfo_hash_table, each_var->name,
2424 (void*) each_var, FALSE);
2427 unit->variable_table = reverse_varinfo_list (unit->variable_table);
2428 unit->cached = TRUE;
2432 /* Locate a section in a BFD containing debugging info. The search starts
2433 from the section after AFTER_SEC, or from the first section in the BFD if
2434 AFTER_SEC is NULL. The search works by examining the names of the
2435 sections. There are two permissiable names. The first is .debug_info.
2436 This is the standard DWARF2 name. The second is a prefix .gnu.linkonce.wi.
2437 This is a variation on the .debug_info section which has a checksum
2438 describing the contents appended onto the name. This allows the linker to
2439 identify and discard duplicate debugging sections for different
2440 compilation units. */
2441 #define DWARF2_DEBUG_INFO ".debug_info"
2442 #define DWARF2_COMPRESSED_DEBUG_INFO ".zdebug_info"
2443 #define GNU_LINKONCE_INFO ".gnu.linkonce.wi."
2446 find_debug_info (bfd *abfd, asection *after_sec)
2450 msec = after_sec != NULL ? after_sec->next : abfd->sections;
2454 if (strcmp (msec->name, DWARF2_DEBUG_INFO) == 0)
2457 if (strcmp (msec->name, DWARF2_COMPRESSED_DEBUG_INFO) == 0)
2460 if (CONST_STRNEQ (msec->name, GNU_LINKONCE_INFO))
2469 /* Unset vmas for loadable sections in STASH. */
2472 unset_sections (struct dwarf2_debug *stash)
2475 struct loadable_section *p;
2477 i = stash->loadable_section_count;
2478 p = stash->loadable_sections;
2479 for (; i > 0; i--, p++)
2480 p->section->vma = 0;
2483 /* Set unique vmas for loadable sections in ABFD and save vmas in
2484 STASH for unset_sections. */
2487 place_sections (bfd *abfd, struct dwarf2_debug *stash)
2489 struct loadable_section *p;
2492 if (stash->loadable_section_count != 0)
2494 i = stash->loadable_section_count;
2495 p = stash->loadable_sections;
2496 for (; i > 0; i--, p++)
2497 p->section->vma = p->adj_vma;
2502 bfd_vma last_vma = 0;
2504 struct loadable_section *p;
2507 for (sect = abfd->sections; sect != NULL; sect = sect->next)
2511 if (sect->vma != 0 || (sect->flags & SEC_LOAD) == 0)
2514 sz = sect->rawsize ? sect->rawsize : sect->size;
2521 amt = i * sizeof (struct loadable_section);
2522 p = (struct loadable_section *) bfd_zalloc (abfd, amt);
2526 stash->loadable_sections = p;
2527 stash->loadable_section_count = i;
2529 for (sect = abfd->sections; sect != NULL; sect = sect->next)
2533 if (sect->vma != 0 || (sect->flags & SEC_LOAD) == 0)
2536 sz = sect->rawsize ? sect->rawsize : sect->size;
2543 /* Align the new address to the current section
2545 last_vma = ((last_vma
2546 + ~((bfd_vma) -1 << sect->alignment_power))
2547 & ((bfd_vma) -1 << sect->alignment_power));
2548 sect->vma = last_vma;
2550 p->adj_vma = sect->vma;
2551 last_vma += sect->vma + sz;
2560 /* Look up a funcinfo by name using the given info hash table. If found,
2561 also update the locations pointed to by filename_ptr and linenumber_ptr.
2563 This function returns TRUE if a funcinfo that matches the given symbol
2564 and address is found with any error; otherwise it returns FALSE. */
2567 info_hash_lookup_funcinfo (struct info_hash_table *hash_table,
2570 const char **filename_ptr,
2571 unsigned int *linenumber_ptr)
2573 struct funcinfo* each_func;
2574 struct funcinfo* best_fit = NULL;
2575 struct info_list_node *node;
2576 struct arange *arange;
2577 const char *name = bfd_asymbol_name (sym);
2578 asection *sec = bfd_get_section (sym);
2580 for (node = lookup_info_hash_table (hash_table, name);
2584 each_func = node->info;
2585 for (arange = &each_func->arange;
2587 arange = arange->next)
2589 if ((!each_func->sec || each_func->sec == sec)
2590 && addr >= arange->low
2591 && addr < arange->high
2593 || ((arange->high - arange->low)
2594 < (best_fit->arange.high - best_fit->arange.low))))
2595 best_fit = each_func;
2601 best_fit->sec = sec;
2602 *filename_ptr = best_fit->file;
2603 *linenumber_ptr = best_fit->line;
2610 /* Look up a varinfo by name using the given info hash table. If found,
2611 also update the locations pointed to by filename_ptr and linenumber_ptr.
2613 This function returns TRUE if a varinfo that matches the given symbol
2614 and address is found with any error; otherwise it returns FALSE. */
2617 info_hash_lookup_varinfo (struct info_hash_table *hash_table,
2620 const char **filename_ptr,
2621 unsigned int *linenumber_ptr)
2623 const char *name = bfd_asymbol_name (sym);
2624 asection *sec = bfd_get_section (sym);
2625 struct varinfo* each;
2626 struct info_list_node *node;
2628 for (node = lookup_info_hash_table (hash_table, name);
2633 if (each->addr == addr
2634 && (!each->sec || each->sec == sec))
2637 *filename_ptr = each->file;
2638 *linenumber_ptr = each->line;
2646 /* Update the funcinfo and varinfo info hash tables if they are
2647 not up to date. Returns TRUE if there is no error; otherwise
2648 returns FALSE and disable the info hash tables. */
2651 stash_maybe_update_info_hash_tables (struct dwarf2_debug *stash)
2653 struct comp_unit *each;
2655 /* Exit if hash tables are up-to-date. */
2656 if (stash->all_comp_units == stash->hash_units_head)
2659 if (stash->hash_units_head)
2660 each = stash->hash_units_head->prev_unit;
2662 each = stash->last_comp_unit;
2666 if (!comp_unit_hash_info (stash, each, stash->funcinfo_hash_table,
2667 stash->varinfo_hash_table))
2669 stash->info_hash_status = STASH_INFO_HASH_DISABLED;
2672 each = each->prev_unit;
2675 stash->hash_units_head = stash->all_comp_units;
2679 /* Check consistency of info hash tables. This is for debugging only. */
2681 static void ATTRIBUTE_UNUSED
2682 stash_verify_info_hash_table (struct dwarf2_debug *stash)
2684 struct comp_unit *each_unit;
2685 struct funcinfo *each_func;
2686 struct varinfo *each_var;
2687 struct info_list_node *node;
2690 for (each_unit = stash->all_comp_units;
2692 each_unit = each_unit->next_unit)
2694 for (each_func = each_unit->function_table;
2696 each_func = each_func->prev_func)
2698 if (!each_func->name)
2700 node = lookup_info_hash_table (stash->funcinfo_hash_table,
2704 while (node && !found)
2706 found = node->info == each_func;
2712 for (each_var = each_unit->variable_table;
2714 each_var = each_var->prev_var)
2716 if (!each_var->name || !each_var->file || each_var->stack)
2718 node = lookup_info_hash_table (stash->varinfo_hash_table,
2722 while (node && !found)
2724 found = node->info == each_var;
2732 /* Check to see if we want to enable the info hash tables, which consume
2733 quite a bit of memory. Currently we only check the number times
2734 bfd_dwarf2_find_line is called. In the future, we may also want to
2735 take the number of symbols into account. */
2738 stash_maybe_enable_info_hash_tables (bfd *abfd, struct dwarf2_debug *stash)
2740 BFD_ASSERT (stash->info_hash_status == STASH_INFO_HASH_OFF);
2742 if (stash->info_hash_count++ < STASH_INFO_HASH_TRIGGER)
2745 /* FIXME: Maybe we should check the reduce_memory_overheads
2746 and optimize fields in the bfd_link_info structure ? */
2748 /* Create hash tables. */
2749 stash->funcinfo_hash_table = create_info_hash_table (abfd);
2750 stash->varinfo_hash_table = create_info_hash_table (abfd);
2751 if (!stash->funcinfo_hash_table || !stash->varinfo_hash_table)
2753 /* Turn off info hashes if any allocation above fails. */
2754 stash->info_hash_status = STASH_INFO_HASH_DISABLED;
2757 /* We need a forced update so that the info hash tables will
2758 be created even though there is no compilation unit. That
2759 happens if STASH_INFO_HASH_TRIGGER is 0. */
2760 stash_maybe_update_info_hash_tables (stash);
2761 stash->info_hash_status = STASH_INFO_HASH_ON;
2764 /* Find the file and line associated with a symbol and address using the
2765 info hash tables of a stash. If there is a match, the function returns
2766 TRUE and update the locations pointed to by filename_ptr and linenumber_ptr;
2767 otherwise it returns FALSE. */
2770 stash_find_line_fast (struct dwarf2_debug *stash,
2773 const char **filename_ptr,
2774 unsigned int *linenumber_ptr)
2776 BFD_ASSERT (stash->info_hash_status == STASH_INFO_HASH_ON);
2778 if (sym->flags & BSF_FUNCTION)
2779 return info_hash_lookup_funcinfo (stash->funcinfo_hash_table, sym, addr,
2780 filename_ptr, linenumber_ptr);
2781 return info_hash_lookup_varinfo (stash->varinfo_hash_table, sym, addr,
2782 filename_ptr, linenumber_ptr);
2785 /* Find the source code location of SYMBOL. If SYMBOL is NULL
2786 then find the nearest source code location corresponding to
2787 the address SECTION + OFFSET.
2788 Returns TRUE if the line is found without error and fills in
2789 FILENAME_PTR and LINENUMBER_PTR. In the case where SYMBOL was
2790 NULL the FUNCTIONNAME_PTR is also filled in.
2791 SYMBOLS contains the symbol table for ABFD.
2792 ADDR_SIZE is the number of bytes in the initial .debug_info length
2793 field and in the abbreviation offset, or zero to indicate that the
2794 default value should be used. */
2797 find_line (bfd *abfd,
2802 const char **filename_ptr,
2803 const char **functionname_ptr,
2804 unsigned int *linenumber_ptr,
2805 unsigned int addr_size,
2808 /* Read each compilation unit from the section .debug_info, and check
2809 to see if it contains the address we are searching for. If yes,
2810 lookup the address, and return the line number info. If no, go
2811 on to the next compilation unit.
2813 We keep a list of all the previously read compilation units, and
2814 a pointer to the next un-read compilation unit. Check the
2815 previously read units before reading more. */
2816 struct dwarf2_debug *stash;
2817 /* What address are we looking for? */
2819 struct comp_unit* each;
2820 bfd_vma found = FALSE;
2821 bfd_boolean do_line;
2827 bfd_size_type amt = sizeof (struct dwarf2_debug);
2829 stash = bfd_zalloc (abfd, amt);
2834 /* In a relocatable file, 2 functions may have the same address.
2835 We change the section vma so that they won't overlap. */
2836 if ((abfd->flags & (EXEC_P | DYNAMIC)) == 0)
2838 if (! place_sections (abfd, stash))
2842 do_line = (section == NULL
2844 && functionname_ptr == NULL
2848 addr = symbol->value;
2849 section = bfd_get_section (symbol);
2851 else if (section != NULL
2852 && functionname_ptr != NULL
2858 if (section->output_section)
2859 addr += section->output_section->vma + section->output_offset;
2861 addr += section->vma;
2862 *filename_ptr = NULL;
2864 *functionname_ptr = NULL;
2865 *linenumber_ptr = 0;
2870 bfd_size_type total_size;
2875 msec = find_debug_info (abfd, NULL);
2878 char * debug_filename = bfd_follow_gnu_debuglink (abfd, DEBUGDIR);
2880 if (debug_filename == NULL)
2881 /* No dwarf2 info, and no gnu_debuglink to follow.
2882 Note that at this point the stash has been allocated, but
2883 contains zeros. This lets future calls to this function
2884 fail more quickly. */
2887 if ((debug_bfd = bfd_openr (debug_filename, NULL)) == NULL
2888 || ! bfd_check_format (debug_bfd, bfd_object)
2889 || (msec = find_debug_info (debug_bfd, NULL)) == NULL)
2892 bfd_close (debug_bfd);
2893 /* FIXME: Should we report our failure to follow the debuglink ? */
2894 free (debug_filename);
2901 /* There can be more than one DWARF2 info section in a BFD these
2902 days. First handle the easy case when there's only one. If
2903 there's more than one, try case two: none of the sections is
2904 compressed. In that case, read them all in and produce one
2905 large stash. We do this in two passes - in the first pass we
2906 just accumulate the section sizes, and in the second pass we
2907 read in the section's contents. (The allows us to avoid
2908 reallocing the data as we add sections to the stash.) If
2909 some or all sections are compressed, then do things the slow
2910 way, with a bunch of reallocs. */
2912 if (! find_debug_info (debug_bfd, msec))
2914 /* Case 1: only one info section. */
2915 total_size = msec->size;
2916 if (! read_section (debug_bfd, ".debug_info", ".zdebug_info",
2918 &stash->info_ptr, &total_size))
2920 stash->info_ptr_end = stash->info_ptr + 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 = bfd_malloc (total_size);
2935 if (stash->info_ptr == NULL)
2938 stash->info_ptr_end = stash->info_ptr;
2940 for (msec = find_debug_info (debug_bfd, NULL);
2942 msec = find_debug_info (debug_bfd, msec))
2945 bfd_size_type start;
2951 start = stash->info_ptr_end - stash->info_ptr;
2953 if ((bfd_simple_get_relocated_section_contents
2954 (debug_bfd, msec, stash->info_ptr + start, symbols))
2958 stash->info_ptr_end = stash->info_ptr + start + size;
2961 BFD_ASSERT (stash->info_ptr_end == stash->info_ptr + total_size);
2965 /* Case 3: multiple sections, some or all compressed. */
2966 stash->info_ptr = bfd_malloc (1);
2967 stash->info_ptr_end = stash->info_ptr;
2968 for (msec = find_debug_info (debug_bfd, NULL);
2970 msec = find_debug_info (debug_bfd, msec))
2972 bfd_size_type size = msec->size;
2974 = (bfd_simple_get_relocated_section_contents
2975 (debug_bfd, msec, NULL, symbols));
2978 if (strcmp (msec->name, DWARF2_COMPRESSED_DEBUG_INFO) == 0)
2980 if (! bfd_uncompress_section_contents (&buffer, &size))
2983 stash->info_ptr = bfd_realloc (stash->info_ptr,
2985 - stash->info_ptr + size);
2986 memcpy (stash->info_ptr_end, buffer, size);
2988 stash->info_ptr_end += size;
2993 stash->sec = find_debug_info (debug_bfd, NULL);
2994 stash->sec_info_ptr = stash->info_ptr;
2995 stash->syms = symbols;
2996 stash->bfd = debug_bfd;
2999 /* A null info_ptr indicates that there is no dwarf2 info
3000 (or that an error occured while setting up the stash). */
3001 if (! stash->info_ptr)
3004 stash->inliner_chain = NULL;
3006 /* Check the previously read comp. units first. */
3009 /* The info hash tables use quite a bit of memory. We may not want to
3010 always use them. We use some heuristics to decide if and when to
3012 if (stash->info_hash_status == STASH_INFO_HASH_OFF)
3013 stash_maybe_enable_info_hash_tables (abfd, stash);
3015 /* Keep info hash table up to date if they are available. Note that we
3016 may disable the hash tables if there is any error duing update. */
3017 if (stash->info_hash_status == STASH_INFO_HASH_ON)
3018 stash_maybe_update_info_hash_tables (stash);
3020 if (stash->info_hash_status == STASH_INFO_HASH_ON)
3022 found = stash_find_line_fast (stash, symbol, addr, filename_ptr,
3029 /* Check the previously read comp. units first. */
3030 for (each = stash->all_comp_units; each; each = each->next_unit)
3031 if ((symbol->flags & BSF_FUNCTION) == 0
3032 || comp_unit_contains_address (each, addr))
3034 found = comp_unit_find_line (each, symbol, addr, filename_ptr,
3035 linenumber_ptr, stash);
3043 for (each = stash->all_comp_units; each; each = each->next_unit)
3045 found = (comp_unit_contains_address (each, addr)
3046 && comp_unit_find_nearest_line (each, addr,
3056 /* The DWARF2 spec says that the initial length field, and the
3057 offset of the abbreviation table, should both be 4-byte values.
3058 However, some compilers do things differently. */
3061 BFD_ASSERT (addr_size == 4 || addr_size == 8);
3063 /* Read each remaining comp. units checking each as they are read. */
3064 while (stash->info_ptr < stash->info_ptr_end)
3067 unsigned int offset_size = addr_size;
3068 bfd_byte *info_ptr_unit = stash->info_ptr;
3070 length = read_4_bytes (stash->bfd, stash->info_ptr);
3071 /* A 0xffffff length is the DWARF3 way of indicating
3072 we use 64-bit offsets, instead of 32-bit offsets. */
3073 if (length == 0xffffffff)
3076 length = read_8_bytes (stash->bfd, stash->info_ptr + 4);
3077 stash->info_ptr += 12;
3079 /* A zero length is the IRIX way of indicating 64-bit offsets,
3080 mostly because the 64-bit length will generally fit in 32
3081 bits, and the endianness helps. */
3082 else if (length == 0)
3085 length = read_4_bytes (stash->bfd, stash->info_ptr + 4);
3086 stash->info_ptr += 8;
3088 /* In the absence of the hints above, we assume 32-bit DWARF2
3089 offsets even for targets with 64-bit addresses, because:
3090 a) most of the time these targets will not have generated
3091 more than 2Gb of debug info and so will not need 64-bit
3094 b) if they do use 64-bit offsets but they are not using
3095 the size hints that are tested for above then they are
3096 not conforming to the DWARF3 standard anyway. */
3097 else if (addr_size == 8)
3100 stash->info_ptr += 4;
3103 stash->info_ptr += 4;
3107 each = parse_comp_unit (stash, length, info_ptr_unit,
3110 /* The dwarf information is damaged, don't trust it any
3113 stash->info_ptr += length;
3115 if ((bfd_vma) (stash->info_ptr - stash->sec_info_ptr)
3116 == stash->sec->size)
3118 stash->sec = find_debug_info (stash->bfd, stash->sec);
3119 stash->sec_info_ptr = stash->info_ptr;
3122 if (stash->all_comp_units)
3123 stash->all_comp_units->prev_unit = each;
3125 stash->last_comp_unit = each;
3127 each->next_unit = stash->all_comp_units;
3128 stash->all_comp_units = each;
3130 /* DW_AT_low_pc and DW_AT_high_pc are optional for
3131 compilation units. If we don't have them (i.e.,
3132 unit->high == 0), we need to consult the line info table
3133 to see if a compilation unit contains the given
3136 found = (((symbol->flags & BSF_FUNCTION) == 0
3137 || each->arange.high == 0
3138 || comp_unit_contains_address (each, addr))
3139 && comp_unit_find_line (each, symbol, addr,
3144 found = ((each->arange.high == 0
3145 || comp_unit_contains_address (each, addr))
3146 && comp_unit_find_nearest_line (each, addr,
3157 if ((abfd->flags & (EXEC_P | DYNAMIC)) == 0)
3158 unset_sections (stash);
3163 /* The DWARF2 version of find_nearest_line.
3164 Return TRUE if the line is found without error. */
3167 _bfd_dwarf2_find_nearest_line (bfd *abfd,
3171 const char **filename_ptr,
3172 const char **functionname_ptr,
3173 unsigned int *linenumber_ptr,
3174 unsigned int addr_size,
3177 return find_line (abfd, section, offset, NULL, symbols, filename_ptr,
3178 functionname_ptr, linenumber_ptr, addr_size,
3182 /* The DWARF2 version of find_line.
3183 Return TRUE if the line is found without error. */
3186 _bfd_dwarf2_find_line (bfd *abfd,
3189 const char **filename_ptr,
3190 unsigned int *linenumber_ptr,
3191 unsigned int addr_size,
3194 return find_line (abfd, NULL, 0, symbol, symbols, filename_ptr,
3195 NULL, linenumber_ptr, addr_size,
3200 _bfd_dwarf2_find_inliner_info (bfd *abfd ATTRIBUTE_UNUSED,
3201 const char **filename_ptr,
3202 const char **functionname_ptr,
3203 unsigned int *linenumber_ptr,
3206 struct dwarf2_debug *stash;
3211 struct funcinfo *func = stash->inliner_chain;
3213 if (func && func->caller_func)
3215 *filename_ptr = func->caller_file;
3216 *functionname_ptr = func->caller_func->name;
3217 *linenumber_ptr = func->caller_line;
3218 stash->inliner_chain = func->caller_func;
3227 _bfd_dwarf2_cleanup_debug_info (bfd *abfd)
3229 struct comp_unit *each;
3230 struct dwarf2_debug *stash;
3232 if (abfd == NULL || elf_tdata (abfd) == NULL)
3235 stash = elf_tdata (abfd)->dwarf2_find_line_info;
3240 for (each = stash->all_comp_units; each; each = each->next_unit)
3242 struct abbrev_info **abbrevs = each->abbrevs;
3243 struct funcinfo *function_table = each->function_table;
3244 struct varinfo *variable_table = each->variable_table;
3247 for (i = 0; i < ABBREV_HASH_SIZE; i++)
3249 struct abbrev_info *abbrev = abbrevs[i];
3253 free (abbrev->attrs);
3254 abbrev = abbrev->next;
3258 if (each->line_table)
3260 free (each->line_table->dirs);
3261 free (each->line_table->files);
3264 while (function_table)
3266 if (function_table->file)
3268 free (function_table->file);
3269 function_table->file = NULL;
3272 if (function_table->caller_file)
3274 free (function_table->caller_file);
3275 function_table->caller_file = NULL;
3277 function_table = function_table->prev_func;
3280 while (variable_table)
3282 if (variable_table->file)
3284 free (variable_table->file);
3285 variable_table->file = NULL;
3288 variable_table = variable_table->prev_var;
3292 free (stash->dwarf_abbrev_buffer);
3293 free (stash->dwarf_line_buffer);
3294 free (stash->dwarf_ranges_buffer);
3295 free (stash->sec_info_ptr);