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 /* 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 loadable sections. */
147 unsigned int loadable_section_count;
149 /* Array of loadable sections. */
150 struct loadable_section *loadable_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 /* Address size for this unit - from unit header. */
241 unsigned char addr_size;
243 /* Offset size for this unit - from unit header. */
244 unsigned char offset_size;
246 /* Base address for this unit - from DW_AT_low_pc attribute of
247 DW_TAG_compile_unit DIE */
248 bfd_vma base_address;
250 /* TRUE if symbols are cached in hash table for faster lookup by name. */
254 /* This data structure holds the information of an abbrev. */
257 unsigned int number; /* Number identifying abbrev. */
258 enum dwarf_tag tag; /* DWARF tag. */
259 int has_children; /* Boolean. */
260 unsigned int num_attrs; /* Number of attributes. */
261 struct attr_abbrev *attrs; /* An array of attribute descriptions. */
262 struct abbrev_info *next; /* Next in chain. */
267 enum dwarf_attribute name;
268 enum dwarf_form form;
271 #ifndef ABBREV_HASH_SIZE
272 #define ABBREV_HASH_SIZE 121
274 #ifndef ATTR_ALLOC_CHUNK
275 #define ATTR_ALLOC_CHUNK 4
278 /* Variable and function hash tables. This is used to speed up look-up
279 in lookup_symbol_in_var_table() and lookup_symbol_in_function_table().
280 In order to share code between variable and function infos, we use
281 a list of untyped pointer for all variable/function info associated with
282 a symbol. We waste a bit of memory for list with one node but that
283 simplifies the code. */
285 struct info_list_node
287 struct info_list_node *next;
291 /* Info hash entry. */
292 struct info_hash_entry
294 struct bfd_hash_entry root;
295 struct info_list_node *head;
298 struct info_hash_table
300 struct bfd_hash_table base;
303 /* Function to create a new entry in info hash table. */
305 static struct bfd_hash_entry *
306 info_hash_table_newfunc (struct bfd_hash_entry *entry,
307 struct bfd_hash_table *table,
310 struct info_hash_entry *ret = (struct info_hash_entry *) entry;
312 /* Allocate the structure if it has not already been allocated by a
316 ret = bfd_hash_allocate (table, sizeof (* ret));
321 /* Call the allocation method of the base class. */
322 ret = ((struct info_hash_entry *)
323 bfd_hash_newfunc ((struct bfd_hash_entry *) ret, table, string));
325 /* Initialize the local fields here. */
329 return (struct bfd_hash_entry *) ret;
332 /* Function to create a new info hash table. It returns a pointer to the
333 newly created table or NULL if there is any error. We need abfd
334 solely for memory allocation. */
336 static struct info_hash_table *
337 create_info_hash_table (bfd *abfd)
339 struct info_hash_table *hash_table;
341 hash_table = bfd_alloc (abfd, sizeof (struct info_hash_table));
345 if (!bfd_hash_table_init (&hash_table->base, info_hash_table_newfunc,
346 sizeof (struct info_hash_entry)))
348 bfd_release (abfd, hash_table);
355 /* Insert an info entry into an info hash table. We do not check of
356 duplicate entries. Also, the caller need to guarantee that the
357 right type of info in inserted as info is passed as a void* pointer.
358 This function returns true if there is no error. */
361 insert_info_hash_table (struct info_hash_table *hash_table,
366 struct info_hash_entry *entry;
367 struct info_list_node *node;
369 entry = (struct info_hash_entry*) bfd_hash_lookup (&hash_table->base,
374 node = bfd_hash_allocate (&hash_table->base, sizeof (*node));
379 node->next = entry->head;
385 /* Look up an info entry list from an info hash table. Return NULL
388 static struct info_list_node *
389 lookup_info_hash_table (struct info_hash_table *hash_table, const char *key)
391 struct info_hash_entry *entry;
393 entry = (struct info_hash_entry*) bfd_hash_lookup (&hash_table->base, key,
395 return entry ? entry->head : NULL;
398 /* Read a section into its appropriate place in the dwarf2_debug
399 struct (indicated by SECTION_BUFFER and SECTION_SIZE). If syms is
400 not NULL, use bfd_simple_get_relocated_section_contents to read the
401 section contents, otherwise use bfd_get_section_contents. */
404 read_section (bfd *abfd,
405 const char* section_name, const char* compressed_section_name,
406 asymbol** syms, bfd_uint64_t offset,
407 bfd_byte **section_buffer, bfd_size_type *section_size)
410 bfd_boolean section_is_compressed = FALSE;
412 /* read_section is a noop if the section has already been read. */
416 msec = bfd_get_section_by_name (abfd, section_name);
417 if (! msec && compressed_section_name)
419 msec = bfd_get_section_by_name (abfd, compressed_section_name);
420 section_is_compressed = TRUE;
424 (*_bfd_error_handler) (_("Dwarf Error: Can't find %s section."), section_name);
425 bfd_set_error (bfd_error_bad_value);
431 *section_size = msec->size;
433 = bfd_simple_get_relocated_section_contents (abfd, msec, NULL, syms);
434 if (! *section_buffer)
439 *section_size = msec->rawsize ? msec->rawsize : msec->size;
440 *section_buffer = bfd_malloc (*section_size);
441 if (! *section_buffer)
443 if (! bfd_get_section_contents (abfd, msec, *section_buffer,
448 if (section_is_compressed)
450 if (! bfd_uncompress_section_contents (section_buffer, section_size))
452 (*_bfd_error_handler) (_("Dwarf Error: unable to decompress %s section."), compressed_section_name);
453 bfd_set_error (bfd_error_bad_value);
458 /* It is possible to get a bad value for the offset into the section
459 * that the client wants. Validate it here to avoid trouble later. */
460 if (offset != 0 && offset >= *section_size)
462 (*_bfd_error_handler) (_("Dwarf Error: Offset (%lu) greater than or equal to %s size (%lu)."),
463 offset, section_name, *section_size);
464 bfd_set_error (bfd_error_bad_value);
472 The following function up to the END VERBATIM mark are
473 copied directly from dwarf2read.c. */
475 /* Read dwarf information from a buffer. */
478 read_1_byte (bfd *abfd ATTRIBUTE_UNUSED, bfd_byte *buf)
480 return bfd_get_8 (abfd, buf);
484 read_1_signed_byte (bfd *abfd ATTRIBUTE_UNUSED, bfd_byte *buf)
486 return bfd_get_signed_8 (abfd, buf);
490 read_2_bytes (bfd *abfd, bfd_byte *buf)
492 return bfd_get_16 (abfd, buf);
496 read_4_bytes (bfd *abfd, bfd_byte *buf)
498 return bfd_get_32 (abfd, buf);
502 read_8_bytes (bfd *abfd, bfd_byte *buf)
504 return bfd_get_64 (abfd, buf);
508 read_n_bytes (bfd *abfd ATTRIBUTE_UNUSED,
510 unsigned int size ATTRIBUTE_UNUSED)
512 /* If the size of a host char is 8 bits, we can return a pointer
513 to the buffer, otherwise we have to copy the data to a buffer
514 allocated on the temporary obstack. */
519 read_string (bfd *abfd ATTRIBUTE_UNUSED,
521 unsigned int *bytes_read_ptr)
523 /* Return a pointer to the embedded string. */
524 char *str = (char *) buf;
531 *bytes_read_ptr = strlen (str) + 1;
536 read_indirect_string (struct comp_unit* unit,
538 unsigned int *bytes_read_ptr)
541 struct dwarf2_debug *stash = unit->stash;
544 if (unit->offset_size == 4)
545 offset = read_4_bytes (unit->abfd, buf);
547 offset = read_8_bytes (unit->abfd, buf);
548 *bytes_read_ptr = unit->offset_size;
550 if (! read_section (unit->abfd, ".debug_str", ".zdebug_str",
552 &stash->dwarf_str_buffer, &stash->dwarf_str_size))
555 str = (char *) stash->dwarf_str_buffer + offset;
564 read_address (struct comp_unit *unit, bfd_byte *buf)
566 int signed_vma = get_elf_backend_data (unit->abfd)->sign_extend_vma;
570 switch (unit->addr_size)
573 return bfd_get_signed_64 (unit->abfd, buf);
575 return bfd_get_signed_32 (unit->abfd, buf);
577 return bfd_get_signed_16 (unit->abfd, buf);
584 switch (unit->addr_size)
587 return bfd_get_64 (unit->abfd, buf);
589 return bfd_get_32 (unit->abfd, buf);
591 return bfd_get_16 (unit->abfd, buf);
598 /* Lookup an abbrev_info structure in the abbrev hash table. */
600 static struct abbrev_info *
601 lookup_abbrev (unsigned int number, struct abbrev_info **abbrevs)
603 unsigned int hash_number;
604 struct abbrev_info *abbrev;
606 hash_number = number % ABBREV_HASH_SIZE;
607 abbrev = abbrevs[hash_number];
611 if (abbrev->number == number)
614 abbrev = abbrev->next;
620 /* In DWARF version 2, the description of the debugging information is
621 stored in a separate .debug_abbrev section. Before we read any
622 dies from a section we read in all abbreviations and install them
625 static struct abbrev_info**
626 read_abbrevs (bfd *abfd, bfd_uint64_t offset, struct dwarf2_debug *stash)
628 struct abbrev_info **abbrevs;
629 bfd_byte *abbrev_ptr;
630 struct abbrev_info *cur_abbrev;
631 unsigned int abbrev_number, bytes_read, abbrev_name;
632 unsigned int abbrev_form, hash_number;
635 if (! read_section (abfd, ".debug_abbrev", ".zdebug_abbrev",
637 &stash->dwarf_abbrev_buffer, &stash->dwarf_abbrev_size))
640 amt = sizeof (struct abbrev_info*) * ABBREV_HASH_SIZE;
641 abbrevs = bfd_zalloc (abfd, amt);
643 abbrev_ptr = stash->dwarf_abbrev_buffer + offset;
644 abbrev_number = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
645 abbrev_ptr += bytes_read;
647 /* Loop until we reach an abbrev number of 0. */
648 while (abbrev_number)
650 amt = sizeof (struct abbrev_info);
651 cur_abbrev = bfd_zalloc (abfd, amt);
653 /* Read in abbrev header. */
654 cur_abbrev->number = abbrev_number;
655 cur_abbrev->tag = (enum dwarf_tag)
656 read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
657 abbrev_ptr += bytes_read;
658 cur_abbrev->has_children = read_1_byte (abfd, abbrev_ptr);
661 /* Now read in declarations. */
662 abbrev_name = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
663 abbrev_ptr += bytes_read;
664 abbrev_form = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
665 abbrev_ptr += bytes_read;
669 if ((cur_abbrev->num_attrs % ATTR_ALLOC_CHUNK) == 0)
671 struct attr_abbrev *tmp;
673 amt = cur_abbrev->num_attrs + ATTR_ALLOC_CHUNK;
674 amt *= sizeof (struct attr_abbrev);
675 tmp = bfd_realloc (cur_abbrev->attrs, amt);
680 for (i = 0; i < ABBREV_HASH_SIZE; i++)
682 struct abbrev_info *abbrev = abbrevs[i];
686 free (abbrev->attrs);
687 abbrev = abbrev->next;
692 cur_abbrev->attrs = tmp;
695 cur_abbrev->attrs[cur_abbrev->num_attrs].name
696 = (enum dwarf_attribute) abbrev_name;
697 cur_abbrev->attrs[cur_abbrev->num_attrs++].form
698 = (enum dwarf_form) abbrev_form;
699 abbrev_name = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
700 abbrev_ptr += bytes_read;
701 abbrev_form = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
702 abbrev_ptr += bytes_read;
705 hash_number = abbrev_number % ABBREV_HASH_SIZE;
706 cur_abbrev->next = abbrevs[hash_number];
707 abbrevs[hash_number] = cur_abbrev;
709 /* Get next abbreviation.
710 Under Irix6 the abbreviations for a compilation unit are not
711 always properly terminated with an abbrev number of 0.
712 Exit loop if we encounter an abbreviation which we have
713 already read (which means we are about to read the abbreviations
714 for the next compile unit) or if the end of the abbreviation
716 if ((unsigned int) (abbrev_ptr - stash->dwarf_abbrev_buffer)
717 >= stash->dwarf_abbrev_size)
719 abbrev_number = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
720 abbrev_ptr += bytes_read;
721 if (lookup_abbrev (abbrev_number,abbrevs) != NULL)
728 /* Read an attribute value described by an attribute form. */
731 read_attribute_value (struct attribute *attr,
733 struct comp_unit *unit,
736 bfd *abfd = unit->abfd;
737 unsigned int bytes_read;
738 struct dwarf_block *blk;
741 attr->form = (enum dwarf_form) form;
746 /* FIXME: DWARF3 draft says DW_FORM_ref_addr is offset_size. */
747 case DW_FORM_ref_addr:
748 attr->u.val = read_address (unit, info_ptr);
749 info_ptr += unit->addr_size;
752 amt = sizeof (struct dwarf_block);
753 blk = bfd_alloc (abfd, amt);
754 blk->size = read_2_bytes (abfd, info_ptr);
756 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
757 info_ptr += blk->size;
761 amt = sizeof (struct dwarf_block);
762 blk = bfd_alloc (abfd, amt);
763 blk->size = read_4_bytes (abfd, info_ptr);
765 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
766 info_ptr += blk->size;
770 attr->u.val = read_2_bytes (abfd, info_ptr);
774 attr->u.val = read_4_bytes (abfd, info_ptr);
778 attr->u.val = read_8_bytes (abfd, info_ptr);
782 attr->u.str = read_string (abfd, info_ptr, &bytes_read);
783 info_ptr += bytes_read;
786 attr->u.str = read_indirect_string (unit, info_ptr, &bytes_read);
787 info_ptr += bytes_read;
790 amt = sizeof (struct dwarf_block);
791 blk = bfd_alloc (abfd, amt);
792 blk->size = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
793 info_ptr += bytes_read;
794 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
795 info_ptr += blk->size;
799 amt = sizeof (struct dwarf_block);
800 blk = bfd_alloc (abfd, amt);
801 blk->size = read_1_byte (abfd, info_ptr);
803 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
804 info_ptr += blk->size;
808 attr->u.val = read_1_byte (abfd, info_ptr);
812 attr->u.val = read_1_byte (abfd, info_ptr);
816 attr->u.sval = read_signed_leb128 (abfd, info_ptr, &bytes_read);
817 info_ptr += bytes_read;
820 attr->u.val = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
821 info_ptr += bytes_read;
824 attr->u.val = read_1_byte (abfd, info_ptr);
828 attr->u.val = read_2_bytes (abfd, info_ptr);
832 attr->u.val = read_4_bytes (abfd, info_ptr);
836 attr->u.val = read_8_bytes (abfd, info_ptr);
839 case DW_FORM_ref_udata:
840 attr->u.val = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
841 info_ptr += bytes_read;
843 case DW_FORM_indirect:
844 form = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
845 info_ptr += bytes_read;
846 info_ptr = read_attribute_value (attr, form, unit, info_ptr);
849 (*_bfd_error_handler) (_("Dwarf Error: Invalid or unhandled FORM value: %u."),
851 bfd_set_error (bfd_error_bad_value);
856 /* Read an attribute described by an abbreviated attribute. */
859 read_attribute (struct attribute *attr,
860 struct attr_abbrev *abbrev,
861 struct comp_unit *unit,
864 attr->name = abbrev->name;
865 info_ptr = read_attribute_value (attr, abbrev->form, unit, info_ptr);
869 /* Source line information table routines. */
871 #define FILE_ALLOC_CHUNK 5
872 #define DIR_ALLOC_CHUNK 5
876 struct line_info* prev_line;
881 int end_sequence; /* End of (sequential) code sequence. */
892 struct line_info_table
895 unsigned int num_files;
896 unsigned int num_dirs;
899 struct fileinfo* files;
900 struct line_info* last_line; /* largest VMA */
901 struct line_info* lcl_head; /* local head; used in 'add_line_info' */
904 /* Remember some information about each function. If the function is
905 inlined (DW_TAG_inlined_subroutine) it may have two additional
906 attributes, DW_AT_call_file and DW_AT_call_line, which specify the
907 source code location where this function was inlined. */
911 struct funcinfo *prev_func; /* Pointer to previous function in list of all functions */
912 struct funcinfo *caller_func; /* Pointer to function one scope higher */
913 char *caller_file; /* Source location file name where caller_func inlines this func */
914 int caller_line; /* Source location line number where caller_func inlines this func */
915 char *file; /* Source location file name */
916 int line; /* Source location line number */
919 struct arange arange;
920 asection *sec; /* Where the symbol is defined */
925 /* Pointer to previous variable in list of all variables */
926 struct varinfo *prev_var;
927 /* Source location file name */
929 /* Source location line number */
934 /* Where the symbol is defined */
936 /* Is this a stack variable? */
937 unsigned int stack: 1;
940 /* Return TRUE if NEW_LINE should sort after LINE. */
942 static inline bfd_boolean
943 new_line_sorts_after (struct line_info *new_line, struct line_info *line)
945 return (new_line->address > line->address
946 || (new_line->address == line->address
947 && new_line->end_sequence < line->end_sequence));
951 /* Adds a new entry to the line_info list in the line_info_table, ensuring
952 that the list is sorted. Note that the line_info list is sorted from
953 highest to lowest VMA (with possible duplicates); that is,
954 line_info->prev_line always accesses an equal or smaller VMA. */
957 add_line_info (struct line_info_table *table,
964 bfd_size_type amt = sizeof (struct line_info);
965 struct line_info* info = bfd_alloc (table->abfd, amt);
967 /* Set member data of 'info'. */
968 info->address = address;
970 info->column = column;
971 info->end_sequence = end_sequence;
973 if (filename && filename[0])
975 info->filename = bfd_alloc (table->abfd, strlen (filename) + 1);
977 strcpy (info->filename, filename);
980 info->filename = NULL;
982 /* Find the correct location for 'info'. Normally we will receive
983 new line_info data 1) in order and 2) with increasing VMAs.
984 However some compilers break the rules (cf. decode_line_info) and
985 so we include some heuristics for quickly finding the correct
986 location for 'info'. In particular, these heuristics optimize for
987 the common case in which the VMA sequence that we receive is a
988 list of locally sorted VMAs such as
989 p...z a...j (where a < j < p < z)
991 Note: table->lcl_head is used to head an *actual* or *possible*
992 sequence within the list (such as a...j) that is not directly
993 headed by table->last_line
995 Note: we may receive duplicate entries from 'decode_line_info'. */
998 && table->last_line->address == address
999 && table->last_line->end_sequence == end_sequence)
1001 /* We only keep the last entry with the same address and end
1002 sequence. See PR ld/4986. */
1003 if (table->lcl_head == table->last_line)
1004 table->lcl_head = info;
1005 info->prev_line = table->last_line->prev_line;
1006 table->last_line = info;
1008 else if (!table->last_line
1009 || new_line_sorts_after (info, table->last_line))
1011 /* Normal case: add 'info' to the beginning of the list */
1012 info->prev_line = table->last_line;
1013 table->last_line = info;
1015 /* lcl_head: initialize to head a *possible* sequence at the end. */
1016 if (!table->lcl_head)
1017 table->lcl_head = info;
1019 else if (!new_line_sorts_after (info, table->lcl_head)
1020 && (!table->lcl_head->prev_line
1021 || new_line_sorts_after (info, table->lcl_head->prev_line)))
1023 /* Abnormal but easy: lcl_head is the head of 'info'. */
1024 info->prev_line = table->lcl_head->prev_line;
1025 table->lcl_head->prev_line = info;
1029 /* Abnormal and hard: Neither 'last_line' nor 'lcl_head' are valid
1030 heads for 'info'. Reset 'lcl_head'. */
1031 struct line_info* li2 = table->last_line; /* always non-NULL */
1032 struct line_info* li1 = li2->prev_line;
1036 if (!new_line_sorts_after (info, li2)
1037 && new_line_sorts_after (info, li1))
1040 li2 = li1; /* always non-NULL */
1041 li1 = li1->prev_line;
1043 table->lcl_head = li2;
1044 info->prev_line = table->lcl_head->prev_line;
1045 table->lcl_head->prev_line = info;
1049 /* Extract a fully qualified filename from a line info table.
1050 The returned string has been malloc'ed and it is the caller's
1051 responsibility to free it. */
1054 concat_filename (struct line_info_table *table, unsigned int file)
1058 if (file - 1 >= table->num_files)
1060 /* FILE == 0 means unknown. */
1062 (*_bfd_error_handler)
1063 (_("Dwarf Error: mangled line number section (bad file number)."));
1064 return strdup ("<unknown>");
1067 filename = table->files[file - 1].name;
1069 if (!IS_ABSOLUTE_PATH (filename))
1071 char *dirname = NULL;
1072 char *subdirname = NULL;
1076 if (table->files[file - 1].dir)
1077 subdirname = table->dirs[table->files[file - 1].dir - 1];
1079 if (!subdirname || !IS_ABSOLUTE_PATH (subdirname))
1080 dirname = table->comp_dir;
1084 dirname = subdirname;
1089 return strdup (filename);
1091 len = strlen (dirname) + strlen (filename) + 2;
1095 len += strlen (subdirname) + 1;
1096 name = bfd_malloc (len);
1098 sprintf (name, "%s/%s/%s", dirname, subdirname, filename);
1102 name = bfd_malloc (len);
1104 sprintf (name, "%s/%s", dirname, filename);
1110 return strdup (filename);
1114 arange_add (bfd *abfd, struct arange *first_arange, bfd_vma low_pc, bfd_vma high_pc)
1116 struct arange *arange;
1118 /* If the first arange is empty, use it. */
1119 if (first_arange->high == 0)
1121 first_arange->low = low_pc;
1122 first_arange->high = high_pc;
1126 /* Next see if we can cheaply extend an existing range. */
1127 arange = first_arange;
1130 if (low_pc == arange->high)
1132 arange->high = high_pc;
1135 if (high_pc == arange->low)
1137 arange->low = low_pc;
1140 arange = arange->next;
1144 /* Need to allocate a new arange and insert it into the arange list.
1145 Order isn't significant, so just insert after the first arange. */
1146 arange = bfd_zalloc (abfd, sizeof (*arange));
1147 arange->low = low_pc;
1148 arange->high = high_pc;
1149 arange->next = first_arange->next;
1150 first_arange->next = arange;
1153 /* Decode the line number information for UNIT. */
1155 static struct line_info_table*
1156 decode_line_info (struct comp_unit *unit, struct dwarf2_debug *stash)
1158 bfd *abfd = unit->abfd;
1159 struct line_info_table* table;
1162 struct line_head lh;
1163 unsigned int i, bytes_read, offset_size;
1164 char *cur_file, *cur_dir;
1165 unsigned char op_code, extended_op, adj_opcode;
1168 if (! read_section (abfd, ".debug_line", ".zdebug_line",
1169 stash->syms, unit->line_offset,
1170 &stash->dwarf_line_buffer, &stash->dwarf_line_size))
1173 amt = sizeof (struct line_info_table);
1174 table = bfd_alloc (abfd, amt);
1176 table->comp_dir = unit->comp_dir;
1178 table->num_files = 0;
1179 table->files = NULL;
1181 table->num_dirs = 0;
1184 table->files = NULL;
1185 table->last_line = NULL;
1186 table->lcl_head = NULL;
1188 line_ptr = stash->dwarf_line_buffer + unit->line_offset;
1190 /* Read in the prologue. */
1191 lh.total_length = read_4_bytes (abfd, line_ptr);
1194 if (lh.total_length == 0xffffffff)
1196 lh.total_length = read_8_bytes (abfd, line_ptr);
1200 else if (lh.total_length == 0 && unit->addr_size == 8)
1202 /* Handle (non-standard) 64-bit DWARF2 formats. */
1203 lh.total_length = read_4_bytes (abfd, line_ptr);
1207 line_end = line_ptr + lh.total_length;
1208 lh.version = read_2_bytes (abfd, line_ptr);
1210 if (offset_size == 4)
1211 lh.prologue_length = read_4_bytes (abfd, line_ptr);
1213 lh.prologue_length = read_8_bytes (abfd, line_ptr);
1214 line_ptr += offset_size;
1215 lh.minimum_instruction_length = read_1_byte (abfd, line_ptr);
1217 lh.default_is_stmt = read_1_byte (abfd, line_ptr);
1219 lh.line_base = read_1_signed_byte (abfd, line_ptr);
1221 lh.line_range = read_1_byte (abfd, line_ptr);
1223 lh.opcode_base = read_1_byte (abfd, line_ptr);
1225 amt = lh.opcode_base * sizeof (unsigned char);
1226 lh.standard_opcode_lengths = bfd_alloc (abfd, amt);
1228 lh.standard_opcode_lengths[0] = 1;
1230 for (i = 1; i < lh.opcode_base; ++i)
1232 lh.standard_opcode_lengths[i] = read_1_byte (abfd, line_ptr);
1236 /* Read directory table. */
1237 while ((cur_dir = read_string (abfd, line_ptr, &bytes_read)) != NULL)
1239 line_ptr += bytes_read;
1241 if ((table->num_dirs % DIR_ALLOC_CHUNK) == 0)
1245 amt = table->num_dirs + DIR_ALLOC_CHUNK;
1246 amt *= sizeof (char *);
1248 tmp = bfd_realloc (table->dirs, amt);
1257 table->dirs[table->num_dirs++] = cur_dir;
1260 line_ptr += bytes_read;
1262 /* Read file name table. */
1263 while ((cur_file = read_string (abfd, line_ptr, &bytes_read)) != NULL)
1265 line_ptr += bytes_read;
1267 if ((table->num_files % FILE_ALLOC_CHUNK) == 0)
1269 struct fileinfo *tmp;
1271 amt = table->num_files + FILE_ALLOC_CHUNK;
1272 amt *= sizeof (struct fileinfo);
1274 tmp = bfd_realloc (table->files, amt);
1277 free (table->files);
1284 table->files[table->num_files].name = cur_file;
1285 table->files[table->num_files].dir =
1286 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
1287 line_ptr += bytes_read;
1288 table->files[table->num_files].time =
1289 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
1290 line_ptr += bytes_read;
1291 table->files[table->num_files].size =
1292 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
1293 line_ptr += bytes_read;
1297 line_ptr += bytes_read;
1299 /* Read the statement sequences until there's nothing left. */
1300 while (line_ptr < line_end)
1302 /* State machine registers. */
1303 bfd_vma address = 0;
1304 char * filename = table->num_files ? concat_filename (table, 1) : NULL;
1305 unsigned int line = 1;
1306 unsigned int column = 0;
1307 int is_stmt = lh.default_is_stmt;
1308 int end_sequence = 0;
1309 /* eraxxon@alumni.rice.edu: Against the DWARF2 specs, some
1310 compilers generate address sequences that are wildly out of
1311 order using DW_LNE_set_address (e.g. Intel C++ 6.0 compiler
1312 for ia64-Linux). Thus, to determine the low and high
1313 address, we must compare on every DW_LNS_copy, etc. */
1314 bfd_vma low_pc = (bfd_vma) -1;
1315 bfd_vma high_pc = 0;
1317 /* Decode the table. */
1318 while (! end_sequence)
1320 op_code = read_1_byte (abfd, line_ptr);
1323 if (op_code >= lh.opcode_base)
1325 /* Special operand. */
1326 adj_opcode = op_code - lh.opcode_base;
1327 address += (adj_opcode / lh.line_range)
1328 * lh.minimum_instruction_length;
1329 line += lh.line_base + (adj_opcode % lh.line_range);
1330 /* Append row to matrix using current values. */
1331 add_line_info (table, address, filename, line, column, 0);
1332 if (address < low_pc)
1334 if (address > high_pc)
1337 else switch (op_code)
1339 case DW_LNS_extended_op:
1340 /* Ignore length. */
1342 extended_op = read_1_byte (abfd, line_ptr);
1345 switch (extended_op)
1347 case DW_LNE_end_sequence:
1349 add_line_info (table, address, filename, line, column,
1351 if (address < low_pc)
1353 if (address > high_pc)
1355 arange_add (unit->abfd, &unit->arange, low_pc, high_pc);
1357 case DW_LNE_set_address:
1358 address = read_address (unit, line_ptr);
1359 line_ptr += unit->addr_size;
1361 case DW_LNE_define_file:
1362 cur_file = read_string (abfd, line_ptr, &bytes_read);
1363 line_ptr += bytes_read;
1364 if ((table->num_files % FILE_ALLOC_CHUNK) == 0)
1366 struct fileinfo *tmp;
1368 amt = table->num_files + FILE_ALLOC_CHUNK;
1369 amt *= sizeof (struct fileinfo);
1370 tmp = bfd_realloc (table->files, amt);
1373 free (table->files);
1380 table->files[table->num_files].name = cur_file;
1381 table->files[table->num_files].dir =
1382 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
1383 line_ptr += bytes_read;
1384 table->files[table->num_files].time =
1385 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
1386 line_ptr += bytes_read;
1387 table->files[table->num_files].size =
1388 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
1389 line_ptr += bytes_read;
1393 (*_bfd_error_handler) (_("Dwarf Error: mangled line number section."));
1394 bfd_set_error (bfd_error_bad_value);
1396 free (table->files);
1402 add_line_info (table, address, filename, line, column, 0);
1403 if (address < low_pc)
1405 if (address > high_pc)
1408 case DW_LNS_advance_pc:
1409 address += lh.minimum_instruction_length
1410 * read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
1411 line_ptr += bytes_read;
1413 case DW_LNS_advance_line:
1414 line += read_signed_leb128 (abfd, line_ptr, &bytes_read);
1415 line_ptr += bytes_read;
1417 case DW_LNS_set_file:
1421 /* The file and directory tables are 0
1422 based, the references are 1 based. */
1423 file = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
1424 line_ptr += bytes_read;
1427 filename = concat_filename (table, file);
1430 case DW_LNS_set_column:
1431 column = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
1432 line_ptr += bytes_read;
1434 case DW_LNS_negate_stmt:
1435 is_stmt = (!is_stmt);
1437 case DW_LNS_set_basic_block:
1439 case DW_LNS_const_add_pc:
1440 address += lh.minimum_instruction_length
1441 * ((255 - lh.opcode_base) / lh.line_range);
1443 case DW_LNS_fixed_advance_pc:
1444 address += read_2_bytes (abfd, line_ptr);
1451 /* Unknown standard opcode, ignore it. */
1452 for (i = 0; i < lh.standard_opcode_lengths[op_code]; i++)
1454 (void) read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
1455 line_ptr += bytes_read;
1468 /* If ADDR is within TABLE set the output parameters and return TRUE,
1469 otherwise return FALSE. The output parameters, FILENAME_PTR and
1470 LINENUMBER_PTR, are pointers to the objects to be filled in. */
1473 lookup_address_in_line_info_table (struct line_info_table *table,
1475 struct funcinfo *function,
1476 const char **filename_ptr,
1477 unsigned int *linenumber_ptr)
1479 /* Note: table->last_line should be a descendingly sorted list. */
1480 struct line_info* next_line = table->last_line;
1481 struct line_info* each_line = NULL;
1482 *filename_ptr = NULL;
1487 each_line = next_line->prev_line;
1489 /* Check for large addresses */
1490 if (addr > next_line->address)
1491 each_line = NULL; /* ensure we skip over the normal case */
1493 /* Normal case: search the list; save */
1494 while (each_line && next_line)
1496 /* If we have an address match, save this info. This allows us
1497 to return as good as results as possible for strange debugging
1499 bfd_boolean addr_match = FALSE;
1500 if (each_line->address <= addr && addr < next_line->address)
1504 /* If this line appears to span functions, and addr is in the
1505 later function, return the first line of that function instead
1506 of the last line of the earlier one. This check is for GCC
1507 2.95, which emits the first line number for a function late. */
1509 if (function != NULL)
1512 struct arange *arange;
1514 /* Find the lowest address in the function's range list */
1515 lowest_pc = function->arange.low;
1516 for (arange = &function->arange;
1518 arange = arange->next)
1520 if (function->arange.low < lowest_pc)
1521 lowest_pc = function->arange.low;
1523 /* Check for spanning function and set outgoing line info */
1524 if (addr >= lowest_pc
1525 && each_line->address < lowest_pc
1526 && next_line->address > lowest_pc)
1528 *filename_ptr = next_line->filename;
1529 *linenumber_ptr = next_line->line;
1533 *filename_ptr = each_line->filename;
1534 *linenumber_ptr = each_line->line;
1539 *filename_ptr = each_line->filename;
1540 *linenumber_ptr = each_line->line;
1544 if (addr_match && !each_line->end_sequence)
1545 return TRUE; /* we have definitely found what we want */
1547 next_line = each_line;
1548 each_line = each_line->prev_line;
1551 /* At this point each_line is NULL but next_line is not. If we found
1552 a candidate end-of-sequence point in the loop above, we can return
1553 that (compatibility with a bug in the Intel compiler); otherwise,
1554 assuming that we found the containing function for this address in
1555 this compilation unit, return the first line we have a number for
1556 (compatibility with GCC 2.95). */
1557 if (*filename_ptr == NULL && function != NULL)
1559 *filename_ptr = next_line->filename;
1560 *linenumber_ptr = next_line->line;
1567 /* Read in the .debug_ranges section for future reference */
1570 read_debug_ranges (struct comp_unit *unit)
1572 struct dwarf2_debug *stash = unit->stash;
1573 return read_section (unit->abfd, ".debug_ranges", ".zdebug_ranges",
1575 &stash->dwarf_ranges_buffer, &stash->dwarf_ranges_size);
1578 /* Function table functions. */
1580 /* If ADDR is within TABLE, set FUNCTIONNAME_PTR, and return TRUE.
1581 Note that we need to find the function that has the smallest
1582 range that contains ADDR, to handle inlined functions without
1583 depending upon them being ordered in TABLE by increasing range. */
1586 lookup_address_in_function_table (struct comp_unit *unit,
1588 struct funcinfo **function_ptr,
1589 const char **functionname_ptr)
1591 struct funcinfo* each_func;
1592 struct funcinfo* best_fit = NULL;
1593 struct arange *arange;
1595 for (each_func = unit->function_table;
1597 each_func = each_func->prev_func)
1599 for (arange = &each_func->arange;
1601 arange = arange->next)
1603 if (addr >= arange->low && addr < arange->high)
1606 ((arange->high - arange->low) < (best_fit->arange.high - best_fit->arange.low)))
1607 best_fit = each_func;
1614 *functionname_ptr = best_fit->name;
1615 *function_ptr = best_fit;
1624 /* If SYM at ADDR is within function table of UNIT, set FILENAME_PTR
1625 and LINENUMBER_PTR, and return TRUE. */
1628 lookup_symbol_in_function_table (struct comp_unit *unit,
1631 const char **filename_ptr,
1632 unsigned int *linenumber_ptr)
1634 struct funcinfo* each_func;
1635 struct funcinfo* best_fit = NULL;
1636 struct arange *arange;
1637 const char *name = bfd_asymbol_name (sym);
1638 asection *sec = bfd_get_section (sym);
1640 for (each_func = unit->function_table;
1642 each_func = each_func->prev_func)
1644 for (arange = &each_func->arange;
1646 arange = arange->next)
1648 if ((!each_func->sec || each_func->sec == sec)
1649 && addr >= arange->low
1650 && addr < arange->high
1652 && strcmp (name, each_func->name) == 0
1654 || ((arange->high - arange->low)
1655 < (best_fit->arange.high - best_fit->arange.low))))
1656 best_fit = each_func;
1662 best_fit->sec = sec;
1663 *filename_ptr = best_fit->file;
1664 *linenumber_ptr = best_fit->line;
1671 /* Variable table functions. */
1673 /* If SYM is within variable table of UNIT, set FILENAME_PTR and
1674 LINENUMBER_PTR, and return TRUE. */
1677 lookup_symbol_in_variable_table (struct comp_unit *unit,
1680 const char **filename_ptr,
1681 unsigned int *linenumber_ptr)
1683 const char *name = bfd_asymbol_name (sym);
1684 asection *sec = bfd_get_section (sym);
1685 struct varinfo* each;
1687 for (each = unit->variable_table; each; each = each->prev_var)
1688 if (each->stack == 0
1689 && each->file != NULL
1690 && each->name != NULL
1691 && each->addr == addr
1692 && (!each->sec || each->sec == sec)
1693 && strcmp (name, each->name) == 0)
1699 *filename_ptr = each->file;
1700 *linenumber_ptr = each->line;
1708 find_abstract_instance_name (struct comp_unit *unit, bfd_uint64_t die_ref)
1710 bfd *abfd = unit->abfd;
1712 unsigned int abbrev_number, bytes_read, i;
1713 struct abbrev_info *abbrev;
1714 struct attribute attr;
1717 info_ptr = unit->info_ptr_unit + die_ref;
1718 abbrev_number = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
1719 info_ptr += bytes_read;
1723 abbrev = lookup_abbrev (abbrev_number, unit->abbrevs);
1726 (*_bfd_error_handler) (_("Dwarf Error: Could not find abbrev number %u."),
1728 bfd_set_error (bfd_error_bad_value);
1732 for (i = 0; i < abbrev->num_attrs; ++i)
1734 info_ptr = read_attribute (&attr, &abbrev->attrs[i], unit, info_ptr);
1738 /* Prefer DW_AT_MIPS_linkage_name over DW_AT_name. */
1742 case DW_AT_specification:
1743 name = find_abstract_instance_name (unit, attr.u.val);
1745 case DW_AT_MIPS_linkage_name:
1758 read_rangelist (struct comp_unit *unit, struct arange *arange, bfd_uint64_t offset)
1760 bfd_byte *ranges_ptr;
1761 bfd_vma base_address = unit->base_address;
1763 if (! unit->stash->dwarf_ranges_buffer)
1765 if (! read_debug_ranges (unit))
1768 ranges_ptr = unit->stash->dwarf_ranges_buffer + offset;
1775 if (unit->addr_size == 4)
1777 low_pc = read_4_bytes (unit->abfd, ranges_ptr);
1779 high_pc = read_4_bytes (unit->abfd, ranges_ptr);
1784 low_pc = read_8_bytes (unit->abfd, ranges_ptr);
1786 high_pc = read_8_bytes (unit->abfd, ranges_ptr);
1789 if (low_pc == 0 && high_pc == 0)
1791 if (low_pc == -1UL && high_pc != -1UL)
1792 base_address = high_pc;
1794 arange_add (unit->abfd, arange, base_address + low_pc, base_address + high_pc);
1798 /* DWARF2 Compilation unit functions. */
1800 /* Scan over each die in a comp. unit looking for functions to add
1801 to the function table and variables to the variable table. */
1804 scan_unit_for_symbols (struct comp_unit *unit)
1806 bfd *abfd = unit->abfd;
1807 bfd_byte *info_ptr = unit->first_child_die_ptr;
1808 int nesting_level = 1;
1809 struct funcinfo **nested_funcs;
1810 int nested_funcs_size;
1812 /* Maintain a stack of in-scope functions and inlined functions, which we
1813 can use to set the caller_func field. */
1814 nested_funcs_size = 32;
1815 nested_funcs = bfd_malloc (nested_funcs_size * sizeof (struct funcinfo *));
1816 if (nested_funcs == NULL)
1818 nested_funcs[nesting_level] = 0;
1820 while (nesting_level)
1822 unsigned int abbrev_number, bytes_read, i;
1823 struct abbrev_info *abbrev;
1824 struct attribute attr;
1825 struct funcinfo *func;
1826 struct varinfo *var;
1828 bfd_vma high_pc = 0;
1830 abbrev_number = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
1831 info_ptr += bytes_read;
1833 if (! abbrev_number)
1839 abbrev = lookup_abbrev (abbrev_number,unit->abbrevs);
1842 (*_bfd_error_handler) (_("Dwarf Error: Could not find abbrev number %u."),
1844 bfd_set_error (bfd_error_bad_value);
1845 free (nested_funcs);
1850 if (abbrev->tag == DW_TAG_subprogram
1851 || abbrev->tag == DW_TAG_entry_point
1852 || abbrev->tag == DW_TAG_inlined_subroutine)
1854 bfd_size_type amt = sizeof (struct funcinfo);
1855 func = bfd_zalloc (abfd, amt);
1856 func->tag = abbrev->tag;
1857 func->prev_func = unit->function_table;
1858 unit->function_table = func;
1859 BFD_ASSERT (!unit->cached);
1861 if (func->tag == DW_TAG_inlined_subroutine)
1862 for (i = nesting_level - 1; i >= 1; i--)
1863 if (nested_funcs[i])
1865 func->caller_func = nested_funcs[i];
1868 nested_funcs[nesting_level] = func;
1873 if (abbrev->tag == DW_TAG_variable)
1875 bfd_size_type amt = sizeof (struct varinfo);
1876 var = bfd_zalloc (abfd, amt);
1877 var->tag = abbrev->tag;
1879 var->prev_var = unit->variable_table;
1880 unit->variable_table = var;
1881 BFD_ASSERT (!unit->cached);
1884 /* No inline function in scope at this nesting level. */
1885 nested_funcs[nesting_level] = 0;
1888 for (i = 0; i < abbrev->num_attrs; ++i)
1890 info_ptr = read_attribute (&attr, &abbrev->attrs[i], unit, info_ptr);
1896 case DW_AT_call_file:
1897 func->caller_file = concat_filename (unit->line_table, attr.u.val);
1900 case DW_AT_call_line:
1901 func->caller_line = attr.u.val;
1904 case DW_AT_abstract_origin:
1905 func->name = find_abstract_instance_name (unit, attr.u.val);
1909 /* Prefer DW_AT_MIPS_linkage_name over DW_AT_name. */
1910 if (func->name == NULL)
1911 func->name = attr.u.str;
1914 case DW_AT_MIPS_linkage_name:
1915 func->name = attr.u.str;
1919 low_pc = attr.u.val;
1923 high_pc = attr.u.val;
1927 read_rangelist (unit, &func->arange, attr.u.val);
1930 case DW_AT_decl_file:
1931 func->file = concat_filename (unit->line_table,
1935 case DW_AT_decl_line:
1936 func->line = attr.u.val;
1948 var->name = attr.u.str;
1951 case DW_AT_decl_file:
1952 var->file = concat_filename (unit->line_table,
1956 case DW_AT_decl_line:
1957 var->line = attr.u.val;
1960 case DW_AT_external:
1961 if (attr.u.val != 0)
1965 case DW_AT_location:
1969 case DW_FORM_block1:
1970 case DW_FORM_block2:
1971 case DW_FORM_block4:
1972 if (*attr.u.blk->data == DW_OP_addr)
1976 /* Verify that DW_OP_addr is the only opcode in the
1977 location, in which case the block size will be 1
1978 plus the address size. */
1979 /* ??? For TLS variables, gcc can emit
1980 DW_OP_addr <addr> DW_OP_GNU_push_tls_address
1981 which we don't handle here yet. */
1982 if (attr.u.blk->size == unit->addr_size + 1U)
1983 var->addr = bfd_get (unit->addr_size * 8,
1985 attr.u.blk->data + 1);
2000 if (func && high_pc != 0)
2002 arange_add (unit->abfd, &func->arange, low_pc, high_pc);
2005 if (abbrev->has_children)
2009 if (nesting_level >= nested_funcs_size)
2011 struct funcinfo **tmp;
2013 nested_funcs_size *= 2;
2014 tmp = bfd_realloc (nested_funcs,
2016 * sizeof (struct funcinfo *)));
2019 free (nested_funcs);
2024 nested_funcs[nesting_level] = 0;
2028 free (nested_funcs);
2032 /* Parse a DWARF2 compilation unit starting at INFO_PTR. This
2033 includes the compilation unit header that proceeds the DIE's, but
2034 does not include the length field that precedes each compilation
2035 unit header. END_PTR points one past the end of this comp unit.
2036 OFFSET_SIZE is the size of DWARF2 offsets (either 4 or 8 bytes).
2038 This routine does not read the whole compilation unit; only enough
2039 to get to the line number information for the compilation unit. */
2041 static struct comp_unit *
2042 parse_comp_unit (struct dwarf2_debug *stash,
2043 bfd_vma unit_length,
2044 bfd_byte *info_ptr_unit,
2045 unsigned int offset_size)
2047 struct comp_unit* unit;
2048 unsigned int version;
2049 bfd_uint64_t abbrev_offset = 0;
2050 unsigned int addr_size;
2051 struct abbrev_info** abbrevs;
2052 unsigned int abbrev_number, bytes_read, i;
2053 struct abbrev_info *abbrev;
2054 struct attribute attr;
2055 bfd_byte *info_ptr = stash->info_ptr;
2056 bfd_byte *end_ptr = info_ptr + unit_length;
2059 bfd_vma high_pc = 0;
2060 bfd *abfd = stash->bfd;
2062 version = read_2_bytes (abfd, info_ptr);
2064 BFD_ASSERT (offset_size == 4 || offset_size == 8);
2065 if (offset_size == 4)
2066 abbrev_offset = read_4_bytes (abfd, info_ptr);
2068 abbrev_offset = read_8_bytes (abfd, info_ptr);
2069 info_ptr += offset_size;
2070 addr_size = read_1_byte (abfd, info_ptr);
2075 (*_bfd_error_handler) (_("Dwarf Error: found dwarf version '%u', this reader only handles version 2 information."), version);
2076 bfd_set_error (bfd_error_bad_value);
2080 if (addr_size > sizeof (bfd_vma))
2082 (*_bfd_error_handler) (_("Dwarf Error: found address size '%u', this reader can not handle sizes greater than '%u'."),
2084 (unsigned int) sizeof (bfd_vma));
2085 bfd_set_error (bfd_error_bad_value);
2089 if (addr_size != 2 && addr_size != 4 && addr_size != 8)
2091 (*_bfd_error_handler) ("Dwarf Error: found address size '%u', this reader can only handle address sizes '2', '4' and '8'.", addr_size);
2092 bfd_set_error (bfd_error_bad_value);
2096 /* Read the abbrevs for this compilation unit into a table. */
2097 abbrevs = read_abbrevs (abfd, abbrev_offset, stash);
2101 abbrev_number = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
2102 info_ptr += bytes_read;
2103 if (! abbrev_number)
2105 (*_bfd_error_handler) (_("Dwarf Error: Bad abbrev number: %u."),
2107 bfd_set_error (bfd_error_bad_value);
2111 abbrev = lookup_abbrev (abbrev_number, abbrevs);
2114 (*_bfd_error_handler) (_("Dwarf Error: Could not find abbrev number %u."),
2116 bfd_set_error (bfd_error_bad_value);
2120 amt = sizeof (struct comp_unit);
2121 unit = bfd_zalloc (abfd, amt);
2123 unit->addr_size = addr_size;
2124 unit->offset_size = offset_size;
2125 unit->abbrevs = abbrevs;
2126 unit->end_ptr = end_ptr;
2127 unit->stash = stash;
2128 unit->info_ptr_unit = info_ptr_unit;
2130 for (i = 0; i < abbrev->num_attrs; ++i)
2132 info_ptr = read_attribute (&attr, &abbrev->attrs[i], unit, info_ptr);
2134 /* Store the data if it is of an attribute we want to keep in a
2135 partial symbol table. */
2138 case DW_AT_stmt_list:
2140 unit->line_offset = attr.u.val;
2144 unit->name = attr.u.str;
2148 low_pc = attr.u.val;
2149 /* If the compilation unit DIE has a DW_AT_low_pc attribute,
2150 this is the base address to use when reading location
2151 lists or range lists. */
2152 unit->base_address = low_pc;
2156 high_pc = attr.u.val;
2160 read_rangelist (unit, &unit->arange, attr.u.val);
2163 case DW_AT_comp_dir:
2165 char *comp_dir = attr.u.str;
2168 /* Irix 6.2 native cc prepends <machine>.: to the compilation
2169 directory, get rid of it. */
2170 char *cp = strchr (comp_dir, ':');
2172 if (cp && cp != comp_dir && cp[-1] == '.' && cp[1] == '/')
2175 unit->comp_dir = comp_dir;
2185 arange_add (unit->abfd, &unit->arange, low_pc, high_pc);
2188 unit->first_child_die_ptr = info_ptr;
2192 /* Return TRUE if UNIT may contain the address given by ADDR. When
2193 there are functions written entirely with inline asm statements, the
2194 range info in the compilation unit header may not be correct. We
2195 need to consult the line info table to see if a compilation unit
2196 really contains the given address. */
2199 comp_unit_contains_address (struct comp_unit *unit, bfd_vma addr)
2201 struct arange *arange;
2206 arange = &unit->arange;
2209 if (addr >= arange->low && addr < arange->high)
2211 arange = arange->next;
2218 /* If UNIT contains ADDR, set the output parameters to the values for
2219 the line containing ADDR. The output parameters, FILENAME_PTR,
2220 FUNCTIONNAME_PTR, and LINENUMBER_PTR, are pointers to the objects
2223 Return TRUE if UNIT contains ADDR, and no errors were encountered;
2227 comp_unit_find_nearest_line (struct comp_unit *unit,
2229 const char **filename_ptr,
2230 const char **functionname_ptr,
2231 unsigned int *linenumber_ptr,
2232 struct dwarf2_debug *stash)
2236 struct funcinfo *function;
2241 if (! unit->line_table)
2243 if (! unit->stmtlist)
2249 unit->line_table = decode_line_info (unit, stash);
2251 if (! unit->line_table)
2257 if (unit->first_child_die_ptr < unit->end_ptr
2258 && ! scan_unit_for_symbols (unit))
2266 func_p = lookup_address_in_function_table (unit, addr,
2267 &function, functionname_ptr);
2268 if (func_p && (function->tag == DW_TAG_inlined_subroutine))
2269 stash->inliner_chain = function;
2270 line_p = lookup_address_in_line_info_table (unit->line_table, addr,
2271 function, filename_ptr,
2273 return line_p || func_p;
2276 /* Check to see if line info is already decoded in a comp_unit.
2277 If not, decode it. Returns TRUE if no errors were encountered;
2281 comp_unit_maybe_decode_line_info (struct comp_unit *unit,
2282 struct dwarf2_debug *stash)
2287 if (! unit->line_table)
2289 if (! unit->stmtlist)
2295 unit->line_table = decode_line_info (unit, stash);
2297 if (! unit->line_table)
2303 if (unit->first_child_die_ptr < unit->end_ptr
2304 && ! scan_unit_for_symbols (unit))
2314 /* If UNIT contains SYM at ADDR, set the output parameters to the
2315 values for the line containing SYM. The output parameters,
2316 FILENAME_PTR, and LINENUMBER_PTR, are pointers to the objects to be
2319 Return TRUE if UNIT contains SYM, and no errors were encountered;
2323 comp_unit_find_line (struct comp_unit *unit,
2326 const char **filename_ptr,
2327 unsigned int *linenumber_ptr,
2328 struct dwarf2_debug *stash)
2330 if (!comp_unit_maybe_decode_line_info (unit, stash))
2333 if (sym->flags & BSF_FUNCTION)
2334 return lookup_symbol_in_function_table (unit, sym, addr,
2338 return lookup_symbol_in_variable_table (unit, sym, addr,
2343 static struct funcinfo *
2344 reverse_funcinfo_list (struct funcinfo *head)
2346 struct funcinfo *rhead;
2347 struct funcinfo *temp;
2349 for (rhead = NULL; head; head = temp)
2351 temp = head->prev_func;
2352 head->prev_func = rhead;
2358 static struct varinfo *
2359 reverse_varinfo_list (struct varinfo *head)
2361 struct varinfo *rhead;
2362 struct varinfo *temp;
2364 for (rhead = NULL; head; head = temp)
2366 temp = head->prev_var;
2367 head->prev_var = rhead;
2373 /* Extract all interesting funcinfos and varinfos of a compilation
2374 unit into hash tables for faster lookup. Returns TRUE if no
2375 errors were enountered; FALSE otherwise. */
2378 comp_unit_hash_info (struct dwarf2_debug *stash,
2379 struct comp_unit *unit,
2380 struct info_hash_table *funcinfo_hash_table,
2381 struct info_hash_table *varinfo_hash_table)
2383 struct funcinfo* each_func;
2384 struct varinfo* each_var;
2385 bfd_boolean okay = TRUE;
2387 BFD_ASSERT (stash->info_hash_status != STASH_INFO_HASH_DISABLED);
2389 if (!comp_unit_maybe_decode_line_info (unit, stash))
2392 BFD_ASSERT (!unit->cached);
2394 /* To preserve the original search order, we went to visit the function
2395 infos in the reversed order of the list. However, making the list
2396 bi-directional use quite a bit of extra memory. So we reverse
2397 the list first, traverse the list in the now reversed order and
2398 finally reverse the list again to get back the original order. */
2399 unit->function_table = reverse_funcinfo_list (unit->function_table);
2400 for (each_func = unit->function_table;
2402 each_func = each_func->prev_func)
2404 /* Skip nameless functions. */
2405 if (each_func->name)
2406 /* There is no need to copy name string into hash table as
2407 name string is either in the dwarf string buffer or
2408 info in the stash. */
2409 okay = insert_info_hash_table (funcinfo_hash_table, each_func->name,
2410 (void*) each_func, FALSE);
2412 unit->function_table = reverse_funcinfo_list (unit->function_table);
2416 /* We do the same for variable infos. */
2417 unit->variable_table = reverse_varinfo_list (unit->variable_table);
2418 for (each_var = unit->variable_table;
2420 each_var = each_var->prev_var)
2422 /* Skip stack vars and vars with no files or names. */
2423 if (each_var->stack == 0
2424 && each_var->file != NULL
2425 && each_var->name != NULL)
2426 /* There is no need to copy name string into hash table as
2427 name string is either in the dwarf string buffer or
2428 info in the stash. */
2429 okay = insert_info_hash_table (varinfo_hash_table, each_var->name,
2430 (void*) each_var, FALSE);
2433 unit->variable_table = reverse_varinfo_list (unit->variable_table);
2434 unit->cached = TRUE;
2438 /* Locate a section in a BFD containing debugging info. The search starts
2439 from the section after AFTER_SEC, or from the first section in the BFD if
2440 AFTER_SEC is NULL. The search works by examining the names of the
2441 sections. There are two permissiable names. The first is .debug_info.
2442 This is the standard DWARF2 name. The second is a prefix .gnu.linkonce.wi.
2443 This is a variation on the .debug_info section which has a checksum
2444 describing the contents appended onto the name. This allows the linker to
2445 identify and discard duplicate debugging sections for different
2446 compilation units. */
2447 #define DWARF2_DEBUG_INFO ".debug_info"
2448 #define DWARF2_COMPRESSED_DEBUG_INFO ".zdebug_info"
2449 #define GNU_LINKONCE_INFO ".gnu.linkonce.wi."
2452 find_debug_info (bfd *abfd, asection *after_sec)
2456 msec = after_sec != NULL ? after_sec->next : abfd->sections;
2460 if (strcmp (msec->name, DWARF2_DEBUG_INFO) == 0)
2463 if (strcmp (msec->name, DWARF2_COMPRESSED_DEBUG_INFO) == 0)
2466 if (CONST_STRNEQ (msec->name, GNU_LINKONCE_INFO))
2475 /* Unset vmas for loadable sections in STASH. */
2478 unset_sections (struct dwarf2_debug *stash)
2481 struct loadable_section *p;
2483 i = stash->loadable_section_count;
2484 p = stash->loadable_sections;
2485 for (; i > 0; i--, p++)
2486 p->section->vma = 0;
2489 /* Set unique vmas for loadable sections in ABFD and save vmas in
2490 STASH for unset_sections. */
2493 place_sections (bfd *abfd, struct dwarf2_debug *stash)
2495 struct loadable_section *p;
2498 if (stash->loadable_section_count != 0)
2500 i = stash->loadable_section_count;
2501 p = stash->loadable_sections;
2502 for (; i > 0; i--, p++)
2503 p->section->vma = p->adj_vma;
2508 bfd_vma last_vma = 0;
2510 struct loadable_section *p;
2513 for (sect = abfd->sections; sect != NULL; sect = sect->next)
2517 if (sect->vma != 0 || (sect->flags & SEC_LOAD) == 0)
2520 sz = sect->rawsize ? sect->rawsize : sect->size;
2527 amt = i * sizeof (struct loadable_section);
2528 p = (struct loadable_section *) bfd_zalloc (abfd, amt);
2532 stash->loadable_sections = p;
2533 stash->loadable_section_count = i;
2535 for (sect = abfd->sections; sect != NULL; sect = sect->next)
2539 if (sect->vma != 0 || (sect->flags & SEC_LOAD) == 0)
2542 sz = sect->rawsize ? sect->rawsize : sect->size;
2549 /* Align the new address to the current section
2551 last_vma = ((last_vma
2552 + ~((bfd_vma) -1 << sect->alignment_power))
2553 & ((bfd_vma) -1 << sect->alignment_power));
2554 sect->vma = last_vma;
2556 p->adj_vma = sect->vma;
2557 last_vma += sect->vma + sz;
2566 /* Look up a funcinfo by name using the given info hash table. If found,
2567 also update the locations pointed to by filename_ptr and linenumber_ptr.
2569 This function returns TRUE if a funcinfo that matches the given symbol
2570 and address is found with any error; otherwise it returns FALSE. */
2573 info_hash_lookup_funcinfo (struct info_hash_table *hash_table,
2576 const char **filename_ptr,
2577 unsigned int *linenumber_ptr)
2579 struct funcinfo* each_func;
2580 struct funcinfo* best_fit = NULL;
2581 struct info_list_node *node;
2582 struct arange *arange;
2583 const char *name = bfd_asymbol_name (sym);
2584 asection *sec = bfd_get_section (sym);
2586 for (node = lookup_info_hash_table (hash_table, name);
2590 each_func = node->info;
2591 for (arange = &each_func->arange;
2593 arange = arange->next)
2595 if ((!each_func->sec || each_func->sec == sec)
2596 && addr >= arange->low
2597 && addr < arange->high
2599 || ((arange->high - arange->low)
2600 < (best_fit->arange.high - best_fit->arange.low))))
2601 best_fit = each_func;
2607 best_fit->sec = sec;
2608 *filename_ptr = best_fit->file;
2609 *linenumber_ptr = best_fit->line;
2616 /* Look up a varinfo by name using the given info hash table. If found,
2617 also update the locations pointed to by filename_ptr and linenumber_ptr.
2619 This function returns TRUE if a varinfo that matches the given symbol
2620 and address is found with any error; otherwise it returns FALSE. */
2623 info_hash_lookup_varinfo (struct info_hash_table *hash_table,
2626 const char **filename_ptr,
2627 unsigned int *linenumber_ptr)
2629 const char *name = bfd_asymbol_name (sym);
2630 asection *sec = bfd_get_section (sym);
2631 struct varinfo* each;
2632 struct info_list_node *node;
2634 for (node = lookup_info_hash_table (hash_table, name);
2639 if (each->addr == addr
2640 && (!each->sec || each->sec == sec))
2643 *filename_ptr = each->file;
2644 *linenumber_ptr = each->line;
2652 /* Update the funcinfo and varinfo info hash tables if they are
2653 not up to date. Returns TRUE if there is no error; otherwise
2654 returns FALSE and disable the info hash tables. */
2657 stash_maybe_update_info_hash_tables (struct dwarf2_debug *stash)
2659 struct comp_unit *each;
2661 /* Exit if hash tables are up-to-date. */
2662 if (stash->all_comp_units == stash->hash_units_head)
2665 if (stash->hash_units_head)
2666 each = stash->hash_units_head->prev_unit;
2668 each = stash->last_comp_unit;
2672 if (!comp_unit_hash_info (stash, each, stash->funcinfo_hash_table,
2673 stash->varinfo_hash_table))
2675 stash->info_hash_status = STASH_INFO_HASH_DISABLED;
2678 each = each->prev_unit;
2681 stash->hash_units_head = stash->all_comp_units;
2685 /* Check consistency of info hash tables. This is for debugging only. */
2687 static void ATTRIBUTE_UNUSED
2688 stash_verify_info_hash_table (struct dwarf2_debug *stash)
2690 struct comp_unit *each_unit;
2691 struct funcinfo *each_func;
2692 struct varinfo *each_var;
2693 struct info_list_node *node;
2696 for (each_unit = stash->all_comp_units;
2698 each_unit = each_unit->next_unit)
2700 for (each_func = each_unit->function_table;
2702 each_func = each_func->prev_func)
2704 if (!each_func->name)
2706 node = lookup_info_hash_table (stash->funcinfo_hash_table,
2710 while (node && !found)
2712 found = node->info == each_func;
2718 for (each_var = each_unit->variable_table;
2720 each_var = each_var->prev_var)
2722 if (!each_var->name || !each_var->file || each_var->stack)
2724 node = lookup_info_hash_table (stash->varinfo_hash_table,
2728 while (node && !found)
2730 found = node->info == each_var;
2738 /* Check to see if we want to enable the info hash tables, which consume
2739 quite a bit of memory. Currently we only check the number times
2740 bfd_dwarf2_find_line is called. In the future, we may also want to
2741 take the number of symbols into account. */
2744 stash_maybe_enable_info_hash_tables (bfd *abfd, struct dwarf2_debug *stash)
2746 BFD_ASSERT (stash->info_hash_status == STASH_INFO_HASH_OFF);
2748 if (stash->info_hash_count++ < STASH_INFO_HASH_TRIGGER)
2751 /* FIXME: Maybe we should check the reduce_memory_overheads
2752 and optimize fields in the bfd_link_info structure ? */
2754 /* Create hash tables. */
2755 stash->funcinfo_hash_table = create_info_hash_table (abfd);
2756 stash->varinfo_hash_table = create_info_hash_table (abfd);
2757 if (!stash->funcinfo_hash_table || !stash->varinfo_hash_table)
2759 /* Turn off info hashes if any allocation above fails. */
2760 stash->info_hash_status = STASH_INFO_HASH_DISABLED;
2763 /* We need a forced update so that the info hash tables will
2764 be created even though there is no compilation unit. That
2765 happens if STASH_INFO_HASH_TRIGGER is 0. */
2766 stash_maybe_update_info_hash_tables (stash);
2767 stash->info_hash_status = STASH_INFO_HASH_ON;
2770 /* Find the file and line associated with a symbol and address using the
2771 info hash tables of a stash. If there is a match, the function returns
2772 TRUE and update the locations pointed to by filename_ptr and linenumber_ptr;
2773 otherwise it returns FALSE. */
2776 stash_find_line_fast (struct dwarf2_debug *stash,
2779 const char **filename_ptr,
2780 unsigned int *linenumber_ptr)
2782 BFD_ASSERT (stash->info_hash_status == STASH_INFO_HASH_ON);
2784 if (sym->flags & BSF_FUNCTION)
2785 return info_hash_lookup_funcinfo (stash->funcinfo_hash_table, sym, addr,
2786 filename_ptr, linenumber_ptr);
2787 return info_hash_lookup_varinfo (stash->varinfo_hash_table, sym, addr,
2788 filename_ptr, linenumber_ptr);
2791 /* Find the source code location of SYMBOL. If SYMBOL is NULL
2792 then find the nearest source code location corresponding to
2793 the address SECTION + OFFSET.
2794 Returns TRUE if the line is found without error and fills in
2795 FILENAME_PTR and LINENUMBER_PTR. In the case where SYMBOL was
2796 NULL the FUNCTIONNAME_PTR is also filled in.
2797 SYMBOLS contains the symbol table for ABFD.
2798 ADDR_SIZE is the number of bytes in the initial .debug_info length
2799 field and in the abbreviation offset, or zero to indicate that the
2800 default value should be used. */
2803 find_line (bfd *abfd,
2808 const char **filename_ptr,
2809 const char **functionname_ptr,
2810 unsigned int *linenumber_ptr,
2811 unsigned int addr_size,
2814 /* Read each compilation unit from the section .debug_info, and check
2815 to see if it contains the address we are searching for. If yes,
2816 lookup the address, and return the line number info. If no, go
2817 on to the next compilation unit.
2819 We keep a list of all the previously read compilation units, and
2820 a pointer to the next un-read compilation unit. Check the
2821 previously read units before reading more. */
2822 struct dwarf2_debug *stash;
2823 /* What address are we looking for? */
2825 struct comp_unit* each;
2826 bfd_vma found = FALSE;
2827 bfd_boolean do_line;
2833 bfd_size_type amt = sizeof (struct dwarf2_debug);
2835 stash = bfd_zalloc (abfd, amt);
2840 /* In a relocatable file, 2 functions may have the same address.
2841 We change the section vma so that they won't overlap. */
2842 if ((abfd->flags & (EXEC_P | DYNAMIC)) == 0)
2844 if (! place_sections (abfd, stash))
2848 do_line = (section == NULL
2850 && functionname_ptr == NULL
2854 addr = symbol->value;
2855 section = bfd_get_section (symbol);
2857 else if (section != NULL
2858 && functionname_ptr != NULL
2864 if (section->output_section)
2865 addr += section->output_section->vma + section->output_offset;
2867 addr += section->vma;
2868 *filename_ptr = NULL;
2870 *functionname_ptr = NULL;
2871 *linenumber_ptr = 0;
2876 bfd_size_type total_size;
2881 msec = find_debug_info (abfd, NULL);
2884 char * debug_filename = bfd_follow_gnu_debuglink (abfd, DEBUGDIR);
2886 if (debug_filename == NULL)
2887 /* No dwarf2 info, and no gnu_debuglink to follow.
2888 Note that at this point the stash has been allocated, but
2889 contains zeros. This lets future calls to this function
2890 fail more quickly. */
2893 if ((debug_bfd = bfd_openr (debug_filename, NULL)) == NULL
2894 || ! bfd_check_format (debug_bfd, bfd_object)
2895 || (msec = find_debug_info (debug_bfd, NULL)) == NULL)
2898 bfd_close (debug_bfd);
2899 /* FIXME: Should we report our failure to follow the debuglink ? */
2900 free (debug_filename);
2907 /* There can be more than one DWARF2 info section in a BFD these
2908 days. First handle the easy case when there's only one. If
2909 there's more than one, try case two: none of the sections is
2910 compressed. In that case, read them all in and produce one
2911 large stash. We do this in two passes - in the first pass we
2912 just accumulate the section sizes, and in the second pass we
2913 read in the section's contents. (The allows us to avoid
2914 reallocing the data as we add sections to the stash.) If
2915 some or all sections are compressed, then do things the slow
2916 way, with a bunch of reallocs. */
2918 if (! find_debug_info (debug_bfd, msec))
2920 /* Case 1: only one info section. */
2921 total_size = msec->size;
2922 if (! read_section (debug_bfd, ".debug_info", ".zdebug_info",
2924 &stash->info_ptr_memory, &total_size))
2926 stash->info_ptr = stash->info_ptr_memory;
2927 stash->info_ptr_end = stash->info_ptr + total_size;
2931 int all_uncompressed = 1;
2932 for (total_size = 0; msec; msec = find_debug_info (debug_bfd, msec))
2934 total_size += msec->size;
2935 if (strcmp (msec->name, DWARF2_COMPRESSED_DEBUG_INFO) == 0)
2936 all_uncompressed = 0;
2938 if (all_uncompressed)
2940 /* Case 2: multiple sections, but none is compressed. */
2941 stash->info_ptr_memory = bfd_malloc (total_size);
2942 if (stash->info_ptr_memory == NULL)
2945 stash->info_ptr = stash->info_ptr_memory;
2946 stash->info_ptr_end = stash->info_ptr;
2948 for (msec = find_debug_info (debug_bfd, NULL);
2950 msec = find_debug_info (debug_bfd, msec))
2953 bfd_size_type start;
2959 start = stash->info_ptr_end - stash->info_ptr;
2961 if ((bfd_simple_get_relocated_section_contents
2962 (debug_bfd, msec, stash->info_ptr + start, symbols))
2966 stash->info_ptr_end = stash->info_ptr + start + size;
2969 BFD_ASSERT (stash->info_ptr_end == stash->info_ptr + total_size);
2973 /* Case 3: multiple sections, some or all compressed. */
2974 stash->info_ptr_memory = bfd_malloc (1);
2975 stash->info_ptr = stash->info_ptr_memory;
2976 stash->info_ptr_end = stash->info_ptr;
2977 for (msec = find_debug_info (debug_bfd, NULL);
2979 msec = find_debug_info (debug_bfd, msec))
2981 bfd_size_type size = msec->size;
2983 = (bfd_simple_get_relocated_section_contents
2984 (debug_bfd, msec, NULL, symbols));
2987 if (strcmp (msec->name, DWARF2_COMPRESSED_DEBUG_INFO) == 0)
2989 if (! bfd_uncompress_section_contents (&buffer, &size))
2992 stash->info_ptr = bfd_realloc (stash->info_ptr,
2994 - stash->info_ptr + size);
2995 memcpy (stash->info_ptr_end, buffer, size);
2997 stash->info_ptr_end += size;
3002 stash->sec = find_debug_info (debug_bfd, NULL);
3003 stash->sec_info_ptr = stash->info_ptr;
3004 stash->syms = symbols;
3005 stash->bfd = debug_bfd;
3008 /* A null info_ptr indicates that there is no dwarf2 info
3009 (or that an error occured while setting up the stash). */
3010 if (! stash->info_ptr)
3013 stash->inliner_chain = NULL;
3015 /* Check the previously read comp. units first. */
3018 /* The info hash tables use quite a bit of memory. We may not want to
3019 always use them. We use some heuristics to decide if and when to
3021 if (stash->info_hash_status == STASH_INFO_HASH_OFF)
3022 stash_maybe_enable_info_hash_tables (abfd, stash);
3024 /* Keep info hash table up to date if they are available. Note that we
3025 may disable the hash tables if there is any error duing update. */
3026 if (stash->info_hash_status == STASH_INFO_HASH_ON)
3027 stash_maybe_update_info_hash_tables (stash);
3029 if (stash->info_hash_status == STASH_INFO_HASH_ON)
3031 found = stash_find_line_fast (stash, symbol, addr, filename_ptr,
3038 /* Check the previously read comp. units first. */
3039 for (each = stash->all_comp_units; each; each = each->next_unit)
3040 if ((symbol->flags & BSF_FUNCTION) == 0
3041 || comp_unit_contains_address (each, addr))
3043 found = comp_unit_find_line (each, symbol, addr, filename_ptr,
3044 linenumber_ptr, stash);
3052 for (each = stash->all_comp_units; each; each = each->next_unit)
3054 found = (comp_unit_contains_address (each, addr)
3055 && comp_unit_find_nearest_line (each, addr,
3065 /* The DWARF2 spec says that the initial length field, and the
3066 offset of the abbreviation table, should both be 4-byte values.
3067 However, some compilers do things differently. */
3070 BFD_ASSERT (addr_size == 4 || addr_size == 8);
3072 /* Read each remaining comp. units checking each as they are read. */
3073 while (stash->info_ptr < stash->info_ptr_end)
3076 unsigned int offset_size = addr_size;
3077 bfd_byte *info_ptr_unit = stash->info_ptr;
3079 length = read_4_bytes (stash->bfd, stash->info_ptr);
3080 /* A 0xffffff length is the DWARF3 way of indicating
3081 we use 64-bit offsets, instead of 32-bit offsets. */
3082 if (length == 0xffffffff)
3085 length = read_8_bytes (stash->bfd, stash->info_ptr + 4);
3086 stash->info_ptr += 12;
3088 /* A zero length is the IRIX way of indicating 64-bit offsets,
3089 mostly because the 64-bit length will generally fit in 32
3090 bits, and the endianness helps. */
3091 else if (length == 0)
3094 length = read_4_bytes (stash->bfd, stash->info_ptr + 4);
3095 stash->info_ptr += 8;
3097 /* In the absence of the hints above, we assume 32-bit DWARF2
3098 offsets even for targets with 64-bit addresses, because:
3099 a) most of the time these targets will not have generated
3100 more than 2Gb of debug info and so will not need 64-bit
3103 b) if they do use 64-bit offsets but they are not using
3104 the size hints that are tested for above then they are
3105 not conforming to the DWARF3 standard anyway. */
3106 else if (addr_size == 8)
3109 stash->info_ptr += 4;
3112 stash->info_ptr += 4;
3116 each = parse_comp_unit (stash, length, info_ptr_unit,
3119 /* The dwarf information is damaged, don't trust it any
3122 stash->info_ptr += length;
3124 if ((bfd_vma) (stash->info_ptr - stash->sec_info_ptr)
3125 == stash->sec->size)
3127 stash->sec = find_debug_info (stash->bfd, stash->sec);
3128 stash->sec_info_ptr = stash->info_ptr;
3131 if (stash->all_comp_units)
3132 stash->all_comp_units->prev_unit = each;
3134 stash->last_comp_unit = each;
3136 each->next_unit = stash->all_comp_units;
3137 stash->all_comp_units = each;
3139 /* DW_AT_low_pc and DW_AT_high_pc are optional for
3140 compilation units. If we don't have them (i.e.,
3141 unit->high == 0), we need to consult the line info table
3142 to see if a compilation unit contains the given
3145 found = (((symbol->flags & BSF_FUNCTION) == 0
3146 || each->arange.high == 0
3147 || comp_unit_contains_address (each, addr))
3148 && comp_unit_find_line (each, symbol, addr,
3153 found = ((each->arange.high == 0
3154 || comp_unit_contains_address (each, addr))
3155 && comp_unit_find_nearest_line (each, addr,
3166 if ((abfd->flags & (EXEC_P | DYNAMIC)) == 0)
3167 unset_sections (stash);
3172 /* The DWARF2 version of find_nearest_line.
3173 Return TRUE if the line is found without error. */
3176 _bfd_dwarf2_find_nearest_line (bfd *abfd,
3180 const char **filename_ptr,
3181 const char **functionname_ptr,
3182 unsigned int *linenumber_ptr,
3183 unsigned int addr_size,
3186 return find_line (abfd, section, offset, NULL, symbols, filename_ptr,
3187 functionname_ptr, linenumber_ptr, addr_size,
3191 /* The DWARF2 version of find_line.
3192 Return TRUE if the line is found without error. */
3195 _bfd_dwarf2_find_line (bfd *abfd,
3198 const char **filename_ptr,
3199 unsigned int *linenumber_ptr,
3200 unsigned int addr_size,
3203 return find_line (abfd, NULL, 0, symbol, symbols, filename_ptr,
3204 NULL, linenumber_ptr, addr_size,
3209 _bfd_dwarf2_find_inliner_info (bfd *abfd ATTRIBUTE_UNUSED,
3210 const char **filename_ptr,
3211 const char **functionname_ptr,
3212 unsigned int *linenumber_ptr,
3215 struct dwarf2_debug *stash;
3220 struct funcinfo *func = stash->inliner_chain;
3222 if (func && func->caller_func)
3224 *filename_ptr = func->caller_file;
3225 *functionname_ptr = func->caller_func->name;
3226 *linenumber_ptr = func->caller_line;
3227 stash->inliner_chain = func->caller_func;
3236 _bfd_dwarf2_cleanup_debug_info (bfd *abfd)
3238 struct comp_unit *each;
3239 struct dwarf2_debug *stash;
3241 if (abfd == NULL || elf_tdata (abfd) == NULL)
3244 stash = elf_tdata (abfd)->dwarf2_find_line_info;
3249 for (each = stash->all_comp_units; each; each = each->next_unit)
3251 struct abbrev_info **abbrevs = each->abbrevs;
3252 struct funcinfo *function_table = each->function_table;
3253 struct varinfo *variable_table = each->variable_table;
3256 for (i = 0; i < ABBREV_HASH_SIZE; i++)
3258 struct abbrev_info *abbrev = abbrevs[i];
3262 free (abbrev->attrs);
3263 abbrev = abbrev->next;
3267 if (each->line_table)
3269 free (each->line_table->dirs);
3270 free (each->line_table->files);
3273 while (function_table)
3275 if (function_table->file)
3277 free (function_table->file);
3278 function_table->file = NULL;
3281 if (function_table->caller_file)
3283 free (function_table->caller_file);
3284 function_table->caller_file = NULL;
3286 function_table = function_table->prev_func;
3289 while (variable_table)
3291 if (variable_table->file)
3293 free (variable_table->file);
3294 variable_table->file = NULL;
3297 variable_table = variable_table->prev_var;
3301 free (stash->dwarf_abbrev_buffer);
3302 free (stash->dwarf_line_buffer);
3303 free (stash->dwarf_ranges_buffer);
3304 free (stash->info_ptr_memory);