2 Copyright 1994, 1995, 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003,
3 2004, 2005, 2006 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 2 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, MA 02110-1301, USA. */
34 #include "libiberty.h"
37 #include "elf/dwarf2.h"
39 /* The data in the .debug_line statement prologue looks like this. */
44 unsigned short version;
45 bfd_vma prologue_length;
46 unsigned char minimum_instruction_length;
47 unsigned char default_is_stmt;
49 unsigned char line_range;
50 unsigned char opcode_base;
51 unsigned char *standard_opcode_lengths;
54 /* Attributes have a name and a value. */
58 enum dwarf_attribute name;
63 struct dwarf_block *blk;
70 /* Blocks are a bunch of untyped bytes. */
77 struct loadable_section
85 /* A list of all previously read comp_units. */
86 struct comp_unit *all_comp_units;
88 /* The next unread compilation unit within the .debug_info section.
89 Zero indicates that the .debug_info section has not been loaded
93 /* Pointer to the end of the .debug_info section memory buffer. */
94 bfd_byte *info_ptr_end;
96 /* Pointer to the section and address of the beginning of the
99 bfd_byte *sec_info_ptr;
101 /* Pointer to the symbol table. */
104 /* Pointer to the .debug_abbrev section loaded into memory. */
105 bfd_byte *dwarf_abbrev_buffer;
107 /* Length of the loaded .debug_abbrev section. */
108 unsigned long dwarf_abbrev_size;
110 /* Buffer for decode_line_info. */
111 bfd_byte *dwarf_line_buffer;
113 /* Length of the loaded .debug_line section. */
114 unsigned long dwarf_line_size;
116 /* Pointer to the .debug_str section loaded into memory. */
117 bfd_byte *dwarf_str_buffer;
119 /* Length of the loaded .debug_str section. */
120 unsigned long dwarf_str_size;
122 /* Pointer to the .debug_ranges section loaded into memory. */
123 bfd_byte *dwarf_ranges_buffer;
125 /* Length of the loaded .debug_ranges section. */
126 unsigned long dwarf_ranges_size;
128 /* If the most recent call to bfd_find_nearest_line was given an
129 address in an inlined function, preserve a pointer into the
130 calling chain for subsequent calls to bfd_find_inliner_info to
132 struct funcinfo *inliner_chain;
134 /* Number of loadable sections. */
135 unsigned int loadable_section_count;
137 /* Array of loadable sections. */
138 struct loadable_section *loadable_sections;
148 /* A minimal decoding of DWARF2 compilation units. We only decode
149 what's needed to get to the line number information. */
153 /* Chain the previously read compilation units. */
154 struct comp_unit *next_unit;
156 /* Keep the bfd convenient (for memory allocation). */
159 /* The lowest and highest addresses contained in this compilation
160 unit as specified in the compilation unit header. */
161 struct arange arange;
163 /* The DW_AT_name attribute (for error messages). */
166 /* The abbrev hash table. */
167 struct abbrev_info **abbrevs;
169 /* Note that an error was found by comp_unit_find_nearest_line. */
172 /* The DW_AT_comp_dir attribute. */
175 /* TRUE if there is a line number table associated with this comp. unit. */
178 /* Pointer to the current comp_unit so that we can find a given entry
180 bfd_byte *info_ptr_unit;
182 /* The offset into .debug_line of the line number table. */
183 unsigned long line_offset;
185 /* Pointer to the first child die for the comp unit. */
186 bfd_byte *first_child_die_ptr;
188 /* The end of the comp unit. */
191 /* The decoded line number, NULL if not yet decoded. */
192 struct line_info_table *line_table;
194 /* A list of the functions found in this comp. unit. */
195 struct funcinfo *function_table;
197 /* A list of the variables found in this comp. unit. */
198 struct varinfo *variable_table;
200 /* Pointer to dwarf2_debug structure. */
201 struct dwarf2_debug *stash;
203 /* Address size for this unit - from unit header. */
204 unsigned char addr_size;
206 /* Offset size for this unit - from unit header. */
207 unsigned char offset_size;
209 /* Base address for this unit - from DW_AT_low_pc attribute of
210 DW_TAG_compile_unit DIE */
211 bfd_vma base_address;
214 /* This data structure holds the information of an abbrev. */
217 unsigned int number; /* Number identifying abbrev. */
218 enum dwarf_tag tag; /* DWARF tag. */
219 int has_children; /* Boolean. */
220 unsigned int num_attrs; /* Number of attributes. */
221 struct attr_abbrev *attrs; /* An array of attribute descriptions. */
222 struct abbrev_info *next; /* Next in chain. */
227 enum dwarf_attribute name;
228 enum dwarf_form form;
231 #ifndef ABBREV_HASH_SIZE
232 #define ABBREV_HASH_SIZE 121
234 #ifndef ATTR_ALLOC_CHUNK
235 #define ATTR_ALLOC_CHUNK 4
239 The following function up to the END VERBATIM mark are
240 copied directly from dwarf2read.c. */
242 /* Read dwarf information from a buffer. */
245 read_1_byte (bfd *abfd ATTRIBUTE_UNUSED, bfd_byte *buf)
247 return bfd_get_8 (abfd, buf);
251 read_1_signed_byte (bfd *abfd ATTRIBUTE_UNUSED, bfd_byte *buf)
253 return bfd_get_signed_8 (abfd, buf);
257 read_2_bytes (bfd *abfd, bfd_byte *buf)
259 return bfd_get_16 (abfd, buf);
263 read_4_bytes (bfd *abfd, bfd_byte *buf)
265 return bfd_get_32 (abfd, buf);
269 read_8_bytes (bfd *abfd, bfd_byte *buf)
271 return bfd_get_64 (abfd, buf);
275 read_n_bytes (bfd *abfd ATTRIBUTE_UNUSED,
277 unsigned int size ATTRIBUTE_UNUSED)
279 /* If the size of a host char is 8 bits, we can return a pointer
280 to the buffer, otherwise we have to copy the data to a buffer
281 allocated on the temporary obstack. */
286 read_string (bfd *abfd ATTRIBUTE_UNUSED,
288 unsigned int *bytes_read_ptr)
290 /* Return a pointer to the embedded string. */
291 char *str = (char *) buf;
298 *bytes_read_ptr = strlen (str) + 1;
303 read_indirect_string (struct comp_unit* unit,
305 unsigned int *bytes_read_ptr)
308 struct dwarf2_debug *stash = unit->stash;
311 if (unit->offset_size == 4)
312 offset = read_4_bytes (unit->abfd, buf);
314 offset = read_8_bytes (unit->abfd, buf);
315 *bytes_read_ptr = unit->offset_size;
317 if (! stash->dwarf_str_buffer)
320 bfd *abfd = unit->abfd;
323 msec = bfd_get_section_by_name (abfd, ".debug_str");
326 (*_bfd_error_handler)
327 (_("Dwarf Error: Can't find .debug_str section."));
328 bfd_set_error (bfd_error_bad_value);
332 sz = msec->rawsize ? msec->rawsize : msec->size;
333 stash->dwarf_str_size = sz;
334 stash->dwarf_str_buffer = bfd_alloc (abfd, sz);
335 if (! stash->dwarf_str_buffer)
338 if (! bfd_get_section_contents (abfd, msec, stash->dwarf_str_buffer,
343 if (offset >= stash->dwarf_str_size)
345 (*_bfd_error_handler) (_("Dwarf Error: DW_FORM_strp offset (%lu) greater than or equal to .debug_str size (%lu)."),
346 (unsigned long) offset, stash->dwarf_str_size);
347 bfd_set_error (bfd_error_bad_value);
351 str = (char *) stash->dwarf_str_buffer + offset;
360 read_address (struct comp_unit *unit, bfd_byte *buf)
362 int signed_vma = get_elf_backend_data (unit->abfd)->sign_extend_vma;
366 switch (unit->addr_size)
369 return bfd_get_signed_64 (unit->abfd, buf);
371 return bfd_get_signed_32 (unit->abfd, buf);
373 return bfd_get_signed_16 (unit->abfd, buf);
380 switch (unit->addr_size)
383 return bfd_get_64 (unit->abfd, buf);
385 return bfd_get_32 (unit->abfd, buf);
387 return bfd_get_16 (unit->abfd, buf);
394 /* Lookup an abbrev_info structure in the abbrev hash table. */
396 static struct abbrev_info *
397 lookup_abbrev (unsigned int number, struct abbrev_info **abbrevs)
399 unsigned int hash_number;
400 struct abbrev_info *abbrev;
402 hash_number = number % ABBREV_HASH_SIZE;
403 abbrev = abbrevs[hash_number];
407 if (abbrev->number == number)
410 abbrev = abbrev->next;
416 /* In DWARF version 2, the description of the debugging information is
417 stored in a separate .debug_abbrev section. Before we read any
418 dies from a section we read in all abbreviations and install them
421 static struct abbrev_info**
422 read_abbrevs (bfd *abfd, bfd_uint64_t offset, struct dwarf2_debug *stash)
424 struct abbrev_info **abbrevs;
425 bfd_byte *abbrev_ptr;
426 struct abbrev_info *cur_abbrev;
427 unsigned int abbrev_number, bytes_read, abbrev_name;
428 unsigned int abbrev_form, hash_number;
431 if (! stash->dwarf_abbrev_buffer)
435 msec = bfd_get_section_by_name (abfd, ".debug_abbrev");
438 (*_bfd_error_handler) (_("Dwarf Error: Can't find .debug_abbrev section."));
439 bfd_set_error (bfd_error_bad_value);
443 stash->dwarf_abbrev_size = msec->size;
444 stash->dwarf_abbrev_buffer
445 = bfd_simple_get_relocated_section_contents (abfd, msec, NULL,
447 if (! stash->dwarf_abbrev_buffer)
451 if (offset >= stash->dwarf_abbrev_size)
453 (*_bfd_error_handler) (_("Dwarf Error: Abbrev offset (%lu) greater than or equal to .debug_abbrev size (%lu)."),
454 (unsigned long) offset, stash->dwarf_abbrev_size);
455 bfd_set_error (bfd_error_bad_value);
459 amt = sizeof (struct abbrev_info*) * ABBREV_HASH_SIZE;
460 abbrevs = bfd_zalloc (abfd, amt);
462 abbrev_ptr = stash->dwarf_abbrev_buffer + offset;
463 abbrev_number = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
464 abbrev_ptr += bytes_read;
466 /* Loop until we reach an abbrev number of 0. */
467 while (abbrev_number)
469 amt = sizeof (struct abbrev_info);
470 cur_abbrev = bfd_zalloc (abfd, amt);
472 /* Read in abbrev header. */
473 cur_abbrev->number = abbrev_number;
474 cur_abbrev->tag = (enum dwarf_tag)
475 read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
476 abbrev_ptr += bytes_read;
477 cur_abbrev->has_children = read_1_byte (abfd, abbrev_ptr);
480 /* Now read in declarations. */
481 abbrev_name = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
482 abbrev_ptr += bytes_read;
483 abbrev_form = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
484 abbrev_ptr += bytes_read;
488 if ((cur_abbrev->num_attrs % ATTR_ALLOC_CHUNK) == 0)
490 struct attr_abbrev *tmp;
492 amt = cur_abbrev->num_attrs + ATTR_ALLOC_CHUNK;
493 amt *= sizeof (struct attr_abbrev);
494 tmp = bfd_realloc (cur_abbrev->attrs, amt);
499 for (i = 0; i < ABBREV_HASH_SIZE; i++)
501 struct abbrev_info *abbrev = abbrevs[i];
505 free (abbrev->attrs);
506 abbrev = abbrev->next;
511 cur_abbrev->attrs = tmp;
514 cur_abbrev->attrs[cur_abbrev->num_attrs].name
515 = (enum dwarf_attribute) abbrev_name;
516 cur_abbrev->attrs[cur_abbrev->num_attrs++].form
517 = (enum dwarf_form) abbrev_form;
518 abbrev_name = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
519 abbrev_ptr += bytes_read;
520 abbrev_form = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
521 abbrev_ptr += bytes_read;
524 hash_number = abbrev_number % ABBREV_HASH_SIZE;
525 cur_abbrev->next = abbrevs[hash_number];
526 abbrevs[hash_number] = cur_abbrev;
528 /* Get next abbreviation.
529 Under Irix6 the abbreviations for a compilation unit are not
530 always properly terminated with an abbrev number of 0.
531 Exit loop if we encounter an abbreviation which we have
532 already read (which means we are about to read the abbreviations
533 for the next compile unit) or if the end of the abbreviation
535 if ((unsigned int) (abbrev_ptr - stash->dwarf_abbrev_buffer)
536 >= stash->dwarf_abbrev_size)
538 abbrev_number = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
539 abbrev_ptr += bytes_read;
540 if (lookup_abbrev (abbrev_number,abbrevs) != NULL)
547 /* Read an attribute value described by an attribute form. */
550 read_attribute_value (struct attribute *attr,
552 struct comp_unit *unit,
555 bfd *abfd = unit->abfd;
556 unsigned int bytes_read;
557 struct dwarf_block *blk;
560 attr->form = (enum dwarf_form) form;
565 /* FIXME: DWARF3 draft says DW_FORM_ref_addr is offset_size. */
566 case DW_FORM_ref_addr:
567 attr->u.val = read_address (unit, info_ptr);
568 info_ptr += unit->addr_size;
571 amt = sizeof (struct dwarf_block);
572 blk = bfd_alloc (abfd, amt);
573 blk->size = read_2_bytes (abfd, info_ptr);
575 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
576 info_ptr += blk->size;
580 amt = sizeof (struct dwarf_block);
581 blk = bfd_alloc (abfd, amt);
582 blk->size = read_4_bytes (abfd, info_ptr);
584 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
585 info_ptr += blk->size;
589 attr->u.val = read_2_bytes (abfd, info_ptr);
593 attr->u.val = read_4_bytes (abfd, info_ptr);
597 attr->u.val = read_8_bytes (abfd, info_ptr);
601 attr->u.str = read_string (abfd, info_ptr, &bytes_read);
602 info_ptr += bytes_read;
605 attr->u.str = read_indirect_string (unit, info_ptr, &bytes_read);
606 info_ptr += bytes_read;
609 amt = sizeof (struct dwarf_block);
610 blk = bfd_alloc (abfd, amt);
611 blk->size = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
612 info_ptr += bytes_read;
613 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
614 info_ptr += blk->size;
618 amt = sizeof (struct dwarf_block);
619 blk = bfd_alloc (abfd, amt);
620 blk->size = read_1_byte (abfd, info_ptr);
622 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
623 info_ptr += blk->size;
627 attr->u.val = read_1_byte (abfd, info_ptr);
631 attr->u.val = read_1_byte (abfd, info_ptr);
635 attr->u.sval = read_signed_leb128 (abfd, info_ptr, &bytes_read);
636 info_ptr += bytes_read;
639 attr->u.val = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
640 info_ptr += bytes_read;
643 attr->u.val = read_1_byte (abfd, info_ptr);
647 attr->u.val = read_2_bytes (abfd, info_ptr);
651 attr->u.val = read_4_bytes (abfd, info_ptr);
655 attr->u.val = read_8_bytes (abfd, info_ptr);
658 case DW_FORM_ref_udata:
659 attr->u.val = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
660 info_ptr += bytes_read;
662 case DW_FORM_indirect:
663 form = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
664 info_ptr += bytes_read;
665 info_ptr = read_attribute_value (attr, form, unit, info_ptr);
668 (*_bfd_error_handler) (_("Dwarf Error: Invalid or unhandled FORM value: %u."),
670 bfd_set_error (bfd_error_bad_value);
675 /* Read an attribute described by an abbreviated attribute. */
678 read_attribute (struct attribute *attr,
679 struct attr_abbrev *abbrev,
680 struct comp_unit *unit,
683 attr->name = abbrev->name;
684 info_ptr = read_attribute_value (attr, abbrev->form, unit, info_ptr);
688 /* Source line information table routines. */
690 #define FILE_ALLOC_CHUNK 5
691 #define DIR_ALLOC_CHUNK 5
695 struct line_info* prev_line;
700 int end_sequence; /* End of (sequential) code sequence. */
711 struct line_info_table
714 unsigned int num_files;
715 unsigned int num_dirs;
718 struct fileinfo* files;
719 struct line_info* last_line; /* largest VMA */
720 struct line_info* lcl_head; /* local head; used in 'add_line_info' */
723 /* Remember some information about each function. If the function is
724 inlined (DW_TAG_inlined_subroutine) it may have two additional
725 attributes, DW_AT_call_file and DW_AT_call_line, which specify the
726 source code location where this function was inlined. */
730 struct funcinfo *prev_func; /* Pointer to previous function in list of all functions */
731 struct funcinfo *caller_func; /* Pointer to function one scope higher */
732 char *caller_file; /* Source location file name where caller_func inlines this func */
733 int caller_line; /* Source location line number where caller_func inlines this func */
734 char *file; /* Source location file name */
735 int line; /* Source location line number */
738 struct arange arange;
739 asection *sec; /* Where the symbol is defined */
744 /* Pointer to previous variable in list of all variables */
745 struct varinfo *prev_var;
746 /* Source location file name */
748 /* Source location line number */
753 /* Where the symbol is defined */
755 /* Is this a stack variable? */
756 unsigned int stack: 1;
759 /* Return TRUE if NEW_LINE should sort after LINE. */
761 static inline bfd_boolean
762 new_line_sorts_after (struct line_info *new_line, struct line_info *line)
764 return (new_line->address > line->address
765 || (new_line->address == line->address
766 && new_line->end_sequence < line->end_sequence));
770 /* Adds a new entry to the line_info list in the line_info_table, ensuring
771 that the list is sorted. Note that the line_info list is sorted from
772 highest to lowest VMA (with possible duplicates); that is,
773 line_info->prev_line always accesses an equal or smaller VMA. */
776 add_line_info (struct line_info_table *table,
783 bfd_size_type amt = sizeof (struct line_info);
784 struct line_info* info = bfd_alloc (table->abfd, amt);
786 /* Set member data of 'info'. */
787 info->address = address;
789 info->column = column;
790 info->end_sequence = end_sequence;
792 if (filename && filename[0])
794 info->filename = bfd_alloc (table->abfd, strlen (filename) + 1);
796 strcpy (info->filename, filename);
799 info->filename = NULL;
801 /* Find the correct location for 'info'. Normally we will receive
802 new line_info data 1) in order and 2) with increasing VMAs.
803 However some compilers break the rules (cf. decode_line_info) and
804 so we include some heuristics for quickly finding the correct
805 location for 'info'. In particular, these heuristics optimize for
806 the common case in which the VMA sequence that we receive is a
807 list of locally sorted VMAs such as
808 p...z a...j (where a < j < p < z)
810 Note: table->lcl_head is used to head an *actual* or *possible*
811 sequence within the list (such as a...j) that is not directly
812 headed by table->last_line
814 Note: we may receive duplicate entries from 'decode_line_info'. */
816 if (!table->last_line
817 || new_line_sorts_after (info, table->last_line))
819 /* Normal case: add 'info' to the beginning of the list */
820 info->prev_line = table->last_line;
821 table->last_line = info;
823 /* lcl_head: initialize to head a *possible* sequence at the end. */
824 if (!table->lcl_head)
825 table->lcl_head = info;
827 else if (!new_line_sorts_after (info, table->lcl_head)
828 && (!table->lcl_head->prev_line
829 || new_line_sorts_after (info, table->lcl_head->prev_line)))
831 /* Abnormal but easy: lcl_head is the head of 'info'. */
832 info->prev_line = table->lcl_head->prev_line;
833 table->lcl_head->prev_line = info;
837 /* Abnormal and hard: Neither 'last_line' nor 'lcl_head' are valid
838 heads for 'info'. Reset 'lcl_head'. */
839 struct line_info* li2 = table->last_line; /* always non-NULL */
840 struct line_info* li1 = li2->prev_line;
844 if (!new_line_sorts_after (info, li2)
845 && new_line_sorts_after (info, li1))
848 li2 = li1; /* always non-NULL */
849 li1 = li1->prev_line;
851 table->lcl_head = li2;
852 info->prev_line = table->lcl_head->prev_line;
853 table->lcl_head->prev_line = info;
857 /* Extract a fully qualified filename from a line info table.
858 The returned string has been malloc'ed and it is the caller's
859 responsibility to free it. */
862 concat_filename (struct line_info_table *table, unsigned int file)
866 if (file - 1 >= table->num_files)
868 /* FILE == 0 means unknown. */
870 (*_bfd_error_handler)
871 (_("Dwarf Error: mangled line number section (bad file number)."));
872 return strdup ("<unknown>");
875 filename = table->files[file - 1].name;
877 if (! IS_ABSOLUTE_PATH (filename))
879 char *dirname = (table->files[file - 1].dir
880 ? table->dirs[table->files[file - 1].dir - 1]
883 /* Not all tools set DW_AT_comp_dir, so dirname may be unknown.
884 The best we can do is return the filename part. */
887 unsigned int len = strlen (dirname) + strlen (filename) + 2;
890 name = bfd_malloc (len);
892 sprintf (name, "%s/%s", dirname, filename);
897 return strdup (filename);
901 arange_add (bfd *abfd, struct arange *first_arange, bfd_vma low_pc, bfd_vma high_pc)
903 struct arange *arange;
905 /* If the first arange is empty, use it. */
906 if (first_arange->high == 0)
908 first_arange->low = low_pc;
909 first_arange->high = high_pc;
913 /* Next see if we can cheaply extend an existing range. */
914 arange = first_arange;
917 if (low_pc == arange->high)
919 arange->high = high_pc;
922 if (high_pc == arange->low)
924 arange->low = low_pc;
927 arange = arange->next;
931 /* Need to allocate a new arange and insert it into the arange list.
932 Order isn't significant, so just insert after the first arange. */
933 arange = bfd_zalloc (abfd, sizeof (*arange));
934 arange->low = low_pc;
935 arange->high = high_pc;
936 arange->next = first_arange->next;
937 first_arange->next = arange;
940 /* Decode the line number information for UNIT. */
942 static struct line_info_table*
943 decode_line_info (struct comp_unit *unit, struct dwarf2_debug *stash)
945 bfd *abfd = unit->abfd;
946 struct line_info_table* table;
950 unsigned int i, bytes_read, offset_size;
951 char *cur_file, *cur_dir;
952 unsigned char op_code, extended_op, adj_opcode;
955 if (! stash->dwarf_line_buffer)
959 msec = bfd_get_section_by_name (abfd, ".debug_line");
962 (*_bfd_error_handler) (_("Dwarf Error: Can't find .debug_line section."));
963 bfd_set_error (bfd_error_bad_value);
967 stash->dwarf_line_size = msec->size;
968 stash->dwarf_line_buffer
969 = bfd_simple_get_relocated_section_contents (abfd, msec, NULL,
971 if (! stash->dwarf_line_buffer)
975 /* It is possible to get a bad value for the line_offset. Validate
976 it here so that we won't get a segfault below. */
977 if (unit->line_offset >= stash->dwarf_line_size)
979 (*_bfd_error_handler) (_("Dwarf Error: Line offset (%lu) greater than or equal to .debug_line size (%lu)."),
980 unit->line_offset, stash->dwarf_line_size);
981 bfd_set_error (bfd_error_bad_value);
985 amt = sizeof (struct line_info_table);
986 table = bfd_alloc (abfd, amt);
988 table->comp_dir = unit->comp_dir;
990 table->num_files = 0;
997 table->last_line = NULL;
998 table->lcl_head = NULL;
1000 line_ptr = stash->dwarf_line_buffer + unit->line_offset;
1002 /* Read in the prologue. */
1003 lh.total_length = read_4_bytes (abfd, line_ptr);
1006 if (lh.total_length == 0xffffffff)
1008 lh.total_length = read_8_bytes (abfd, line_ptr);
1012 else if (lh.total_length == 0 && unit->addr_size == 8)
1014 /* Handle (non-standard) 64-bit DWARF2 formats. */
1015 lh.total_length = read_4_bytes (abfd, line_ptr);
1019 line_end = line_ptr + lh.total_length;
1020 lh.version = read_2_bytes (abfd, line_ptr);
1022 if (offset_size == 4)
1023 lh.prologue_length = read_4_bytes (abfd, line_ptr);
1025 lh.prologue_length = read_8_bytes (abfd, line_ptr);
1026 line_ptr += offset_size;
1027 lh.minimum_instruction_length = read_1_byte (abfd, line_ptr);
1029 lh.default_is_stmt = read_1_byte (abfd, line_ptr);
1031 lh.line_base = read_1_signed_byte (abfd, line_ptr);
1033 lh.line_range = read_1_byte (abfd, line_ptr);
1035 lh.opcode_base = read_1_byte (abfd, line_ptr);
1037 amt = lh.opcode_base * sizeof (unsigned char);
1038 lh.standard_opcode_lengths = bfd_alloc (abfd, amt);
1040 lh.standard_opcode_lengths[0] = 1;
1042 for (i = 1; i < lh.opcode_base; ++i)
1044 lh.standard_opcode_lengths[i] = read_1_byte (abfd, line_ptr);
1048 /* Read directory table. */
1049 while ((cur_dir = read_string (abfd, line_ptr, &bytes_read)) != NULL)
1051 line_ptr += bytes_read;
1053 if ((table->num_dirs % DIR_ALLOC_CHUNK) == 0)
1057 amt = table->num_dirs + DIR_ALLOC_CHUNK;
1058 amt *= sizeof (char *);
1060 tmp = bfd_realloc (table->dirs, amt);
1069 table->dirs[table->num_dirs++] = cur_dir;
1072 line_ptr += bytes_read;
1074 /* Read file name table. */
1075 while ((cur_file = read_string (abfd, line_ptr, &bytes_read)) != NULL)
1077 line_ptr += bytes_read;
1079 if ((table->num_files % FILE_ALLOC_CHUNK) == 0)
1081 struct fileinfo *tmp;
1083 amt = table->num_files + FILE_ALLOC_CHUNK;
1084 amt *= sizeof (struct fileinfo);
1086 tmp = bfd_realloc (table->files, amt);
1089 free (table->files);
1096 table->files[table->num_files].name = cur_file;
1097 table->files[table->num_files].dir =
1098 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
1099 line_ptr += bytes_read;
1100 table->files[table->num_files].time =
1101 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
1102 line_ptr += bytes_read;
1103 table->files[table->num_files].size =
1104 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
1105 line_ptr += bytes_read;
1109 line_ptr += bytes_read;
1111 /* Read the statement sequences until there's nothing left. */
1112 while (line_ptr < line_end)
1114 /* State machine registers. */
1115 bfd_vma address = 0;
1116 char * filename = table->num_files ? concat_filename (table, 1) : NULL;
1117 unsigned int line = 1;
1118 unsigned int column = 0;
1119 int is_stmt = lh.default_is_stmt;
1120 int basic_block = 0;
1121 int end_sequence = 0;
1122 /* eraxxon@alumni.rice.edu: Against the DWARF2 specs, some
1123 compilers generate address sequences that are wildly out of
1124 order using DW_LNE_set_address (e.g. Intel C++ 6.0 compiler
1125 for ia64-Linux). Thus, to determine the low and high
1126 address, we must compare on every DW_LNS_copy, etc. */
1127 bfd_vma low_pc = (bfd_vma) -1;
1128 bfd_vma high_pc = 0;
1130 /* Decode the table. */
1131 while (! end_sequence)
1133 op_code = read_1_byte (abfd, line_ptr);
1136 if (op_code >= lh.opcode_base)
1138 /* Special operand. */
1139 adj_opcode = op_code - lh.opcode_base;
1140 address += (adj_opcode / lh.line_range)
1141 * lh.minimum_instruction_length;
1142 line += lh.line_base + (adj_opcode % lh.line_range);
1143 /* Append row to matrix using current values. */
1144 add_line_info (table, address, filename, line, column, 0);
1146 if (address < low_pc)
1148 if (address > high_pc)
1151 else switch (op_code)
1153 case DW_LNS_extended_op:
1154 /* Ignore length. */
1156 extended_op = read_1_byte (abfd, line_ptr);
1159 switch (extended_op)
1161 case DW_LNE_end_sequence:
1163 add_line_info (table, address, filename, line, column,
1165 if (address < low_pc)
1167 if (address > high_pc)
1169 arange_add (unit->abfd, &unit->arange, low_pc, high_pc);
1171 case DW_LNE_set_address:
1172 address = read_address (unit, line_ptr);
1173 line_ptr += unit->addr_size;
1175 case DW_LNE_define_file:
1176 cur_file = read_string (abfd, line_ptr, &bytes_read);
1177 line_ptr += bytes_read;
1178 if ((table->num_files % FILE_ALLOC_CHUNK) == 0)
1180 struct fileinfo *tmp;
1182 amt = table->num_files + FILE_ALLOC_CHUNK;
1183 amt *= sizeof (struct fileinfo);
1184 tmp = bfd_realloc (table->files, amt);
1187 free (table->files);
1194 table->files[table->num_files].name = cur_file;
1195 table->files[table->num_files].dir =
1196 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
1197 line_ptr += bytes_read;
1198 table->files[table->num_files].time =
1199 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
1200 line_ptr += bytes_read;
1201 table->files[table->num_files].size =
1202 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
1203 line_ptr += bytes_read;
1207 (*_bfd_error_handler) (_("Dwarf Error: mangled line number section."));
1208 bfd_set_error (bfd_error_bad_value);
1210 free (table->files);
1216 add_line_info (table, address, filename, line, column, 0);
1218 if (address < low_pc)
1220 if (address > high_pc)
1223 case DW_LNS_advance_pc:
1224 address += lh.minimum_instruction_length
1225 * read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
1226 line_ptr += bytes_read;
1228 case DW_LNS_advance_line:
1229 line += read_signed_leb128 (abfd, line_ptr, &bytes_read);
1230 line_ptr += bytes_read;
1232 case DW_LNS_set_file:
1236 /* The file and directory tables are 0
1237 based, the references are 1 based. */
1238 file = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
1239 line_ptr += bytes_read;
1242 filename = concat_filename (table, file);
1245 case DW_LNS_set_column:
1246 column = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
1247 line_ptr += bytes_read;
1249 case DW_LNS_negate_stmt:
1250 is_stmt = (!is_stmt);
1252 case DW_LNS_set_basic_block:
1255 case DW_LNS_const_add_pc:
1256 address += lh.minimum_instruction_length
1257 * ((255 - lh.opcode_base) / lh.line_range);
1259 case DW_LNS_fixed_advance_pc:
1260 address += read_2_bytes (abfd, line_ptr);
1267 /* Unknown standard opcode, ignore it. */
1268 for (i = 0; i < lh.standard_opcode_lengths[op_code]; i++)
1270 (void) read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
1271 line_ptr += bytes_read;
1284 /* If ADDR is within TABLE set the output parameters and return TRUE,
1285 otherwise return FALSE. The output parameters, FILENAME_PTR and
1286 LINENUMBER_PTR, are pointers to the objects to be filled in. */
1289 lookup_address_in_line_info_table (struct line_info_table *table,
1291 struct funcinfo *function,
1292 const char **filename_ptr,
1293 unsigned int *linenumber_ptr)
1295 /* Note: table->last_line should be a descendingly sorted list. */
1296 struct line_info* next_line = table->last_line;
1297 struct line_info* each_line = NULL;
1298 *filename_ptr = NULL;
1303 each_line = next_line->prev_line;
1305 /* Check for large addresses */
1306 if (addr > next_line->address)
1307 each_line = NULL; /* ensure we skip over the normal case */
1309 /* Normal case: search the list; save */
1310 while (each_line && next_line)
1312 /* If we have an address match, save this info. This allows us
1313 to return as good as results as possible for strange debugging
1315 bfd_boolean addr_match = FALSE;
1316 if (each_line->address <= addr && addr < next_line->address)
1320 /* If this line appears to span functions, and addr is in the
1321 later function, return the first line of that function instead
1322 of the last line of the earlier one. This check is for GCC
1323 2.95, which emits the first line number for a function late. */
1325 if (function != NULL)
1328 struct arange *arange;
1330 /* Find the lowest address in the function's range list */
1331 lowest_pc = function->arange.low;
1332 for (arange = &function->arange;
1334 arange = arange->next)
1336 if (function->arange.low < lowest_pc)
1337 lowest_pc = function->arange.low;
1339 /* Check for spanning function and set outgoing line info */
1340 if (addr >= lowest_pc
1341 && each_line->address < lowest_pc
1342 && next_line->address > lowest_pc)
1344 *filename_ptr = next_line->filename;
1345 *linenumber_ptr = next_line->line;
1349 *filename_ptr = each_line->filename;
1350 *linenumber_ptr = each_line->line;
1355 *filename_ptr = each_line->filename;
1356 *linenumber_ptr = each_line->line;
1360 if (addr_match && !each_line->end_sequence)
1361 return TRUE; /* we have definitely found what we want */
1363 next_line = each_line;
1364 each_line = each_line->prev_line;
1367 /* At this point each_line is NULL but next_line is not. If we found
1368 a candidate end-of-sequence point in the loop above, we can return
1369 that (compatibility with a bug in the Intel compiler); otherwise,
1370 assuming that we found the containing function for this address in
1371 this compilation unit, return the first line we have a number for
1372 (compatibility with GCC 2.95). */
1373 if (*filename_ptr == NULL && function != NULL)
1375 *filename_ptr = next_line->filename;
1376 *linenumber_ptr = next_line->line;
1383 /* Read in the .debug_ranges section for future reference */
1386 read_debug_ranges (struct comp_unit *unit)
1388 struct dwarf2_debug *stash = unit->stash;
1389 if (! stash->dwarf_ranges_buffer)
1391 bfd *abfd = unit->abfd;
1394 msec = bfd_get_section_by_name (abfd, ".debug_ranges");
1397 (*_bfd_error_handler) (_("Dwarf Error: Can't find .debug_ranges section."));
1398 bfd_set_error (bfd_error_bad_value);
1402 stash->dwarf_ranges_size = msec->size;
1403 stash->dwarf_ranges_buffer
1404 = bfd_simple_get_relocated_section_contents (abfd, msec, NULL,
1406 if (! stash->dwarf_ranges_buffer)
1412 /* Function table functions. */
1414 /* If ADDR is within TABLE, set FUNCTIONNAME_PTR, and return TRUE.
1415 Note that we need to find the function that has the smallest
1416 range that contains ADDR, to handle inlined functions without
1417 depending upon them being ordered in TABLE by increasing range. */
1420 lookup_address_in_function_table (struct comp_unit *unit,
1422 struct funcinfo **function_ptr,
1423 const char **functionname_ptr)
1425 struct funcinfo* each_func;
1426 struct funcinfo* best_fit = NULL;
1427 struct arange *arange;
1429 for (each_func = unit->function_table;
1431 each_func = each_func->prev_func)
1433 for (arange = &each_func->arange;
1435 arange = arange->next)
1437 if (addr >= arange->low && addr < arange->high)
1440 ((arange->high - arange->low) < (best_fit->arange.high - best_fit->arange.low)))
1441 best_fit = each_func;
1448 *functionname_ptr = best_fit->name;
1449 *function_ptr = best_fit;
1458 /* If SYM at ADDR is within function table of UNIT, set FILENAME_PTR
1459 and LINENUMBER_PTR, and return TRUE. */
1462 lookup_symbol_in_function_table (struct comp_unit *unit,
1465 const char **filename_ptr,
1466 unsigned int *linenumber_ptr)
1468 struct funcinfo* each_func;
1469 struct funcinfo* best_fit = NULL;
1470 struct arange *arange;
1471 const char *name = bfd_asymbol_name (sym);
1472 asection *sec = bfd_get_section (sym);
1474 for (each_func = unit->function_table;
1476 each_func = each_func->prev_func)
1478 for (arange = &each_func->arange;
1480 arange = arange->next)
1482 if ((!each_func->sec || each_func->sec == sec)
1483 && addr >= arange->low
1484 && addr < arange->high
1486 && strcmp (name, each_func->name) == 0
1488 || ((arange->high - arange->low)
1489 < (best_fit->arange.high - best_fit->arange.low))))
1490 best_fit = each_func;
1496 best_fit->sec = sec;
1497 *filename_ptr = best_fit->file;
1498 *linenumber_ptr = best_fit->line;
1505 /* Variable table functions. */
1507 /* If SYM is within variable table of UNIT, set FILENAME_PTR and
1508 LINENUMBER_PTR, and return TRUE. */
1511 lookup_symbol_in_variable_table (struct comp_unit *unit,
1514 const char **filename_ptr,
1515 unsigned int *linenumber_ptr)
1517 const char *name = bfd_asymbol_name (sym);
1518 asection *sec = bfd_get_section (sym);
1519 struct varinfo* each;
1521 for (each = unit->variable_table; each; each = each->prev_var)
1522 if (each->stack == 0
1523 && each->file != NULL
1524 && each->name != NULL
1525 && each->addr == addr
1526 && (!each->sec || each->sec == sec)
1527 && strcmp (name, each->name) == 0)
1533 *filename_ptr = each->file;
1534 *linenumber_ptr = each->line;
1542 find_abstract_instance_name (struct comp_unit *unit, bfd_uint64_t die_ref)
1544 bfd *abfd = unit->abfd;
1546 unsigned int abbrev_number, bytes_read, i;
1547 struct abbrev_info *abbrev;
1548 struct attribute attr;
1551 info_ptr = unit->info_ptr_unit + die_ref;
1552 abbrev_number = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
1553 info_ptr += bytes_read;
1557 abbrev = lookup_abbrev (abbrev_number, unit->abbrevs);
1560 (*_bfd_error_handler) (_("Dwarf Error: Could not find abbrev number %u."),
1562 bfd_set_error (bfd_error_bad_value);
1566 for (i = 0; i < abbrev->num_attrs; ++i)
1568 info_ptr = read_attribute (&attr, &abbrev->attrs[i], unit, info_ptr);
1572 /* Prefer DW_AT_MIPS_linkage_name over DW_AT_name. */
1576 case DW_AT_specification:
1577 name = find_abstract_instance_name (unit, attr.u.val);
1579 case DW_AT_MIPS_linkage_name:
1592 read_rangelist (struct comp_unit *unit, struct arange *arange, bfd_uint64_t offset)
1594 bfd_byte *ranges_ptr;
1595 bfd_vma base_address = unit->base_address;
1597 if (! unit->stash->dwarf_ranges_buffer)
1599 if (! read_debug_ranges (unit))
1602 ranges_ptr = unit->stash->dwarf_ranges_buffer + offset;
1609 if (unit->addr_size == 4)
1611 low_pc = read_4_bytes (unit->abfd, ranges_ptr);
1613 high_pc = read_4_bytes (unit->abfd, ranges_ptr);
1618 low_pc = read_8_bytes (unit->abfd, ranges_ptr);
1620 high_pc = read_8_bytes (unit->abfd, ranges_ptr);
1623 if (low_pc == 0 && high_pc == 0)
1625 if (low_pc == -1UL && high_pc != -1UL)
1626 base_address = high_pc;
1628 arange_add (unit->abfd, arange, base_address + low_pc, base_address + high_pc);
1632 /* DWARF2 Compilation unit functions. */
1634 /* Scan over each die in a comp. unit looking for functions to add
1635 to the function table and variables to the variable table. */
1638 scan_unit_for_symbols (struct comp_unit *unit)
1640 bfd *abfd = unit->abfd;
1641 bfd_byte *info_ptr = unit->first_child_die_ptr;
1642 int nesting_level = 1;
1643 struct funcinfo **nested_funcs;
1644 int nested_funcs_size;
1646 /* Maintain a stack of in-scope functions and inlined functions, which we
1647 can use to set the caller_func field. */
1648 nested_funcs_size = 32;
1649 nested_funcs = bfd_malloc (nested_funcs_size * sizeof (struct funcinfo *));
1650 if (nested_funcs == NULL)
1652 nested_funcs[nesting_level] = 0;
1654 while (nesting_level)
1656 unsigned int abbrev_number, bytes_read, i;
1657 struct abbrev_info *abbrev;
1658 struct attribute attr;
1659 struct funcinfo *func;
1660 struct varinfo *var;
1662 bfd_vma high_pc = 0;
1664 abbrev_number = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
1665 info_ptr += bytes_read;
1667 if (! abbrev_number)
1673 abbrev = lookup_abbrev (abbrev_number,unit->abbrevs);
1676 (*_bfd_error_handler) (_("Dwarf Error: Could not find abbrev number %u."),
1678 bfd_set_error (bfd_error_bad_value);
1679 free (nested_funcs);
1684 if (abbrev->tag == DW_TAG_subprogram
1685 || abbrev->tag == DW_TAG_entry_point
1686 || abbrev->tag == DW_TAG_inlined_subroutine)
1688 bfd_size_type amt = sizeof (struct funcinfo);
1689 func = bfd_zalloc (abfd, amt);
1690 func->tag = abbrev->tag;
1691 func->prev_func = unit->function_table;
1692 unit->function_table = func;
1694 if (func->tag == DW_TAG_inlined_subroutine)
1695 for (i = nesting_level - 1; i >= 1; i--)
1696 if (nested_funcs[i])
1698 func->caller_func = nested_funcs[i];
1701 nested_funcs[nesting_level] = func;
1706 if (abbrev->tag == DW_TAG_variable)
1708 bfd_size_type amt = sizeof (struct varinfo);
1709 var = bfd_zalloc (abfd, amt);
1710 var->tag = abbrev->tag;
1712 var->prev_var = unit->variable_table;
1713 unit->variable_table = var;
1716 /* No inline function in scope at this nesting level. */
1717 nested_funcs[nesting_level] = 0;
1720 for (i = 0; i < abbrev->num_attrs; ++i)
1722 info_ptr = read_attribute (&attr, &abbrev->attrs[i], unit, info_ptr);
1728 case DW_AT_call_file:
1729 func->caller_file = concat_filename (unit->line_table, attr.u.val);
1732 case DW_AT_call_line:
1733 func->caller_line = attr.u.val;
1736 case DW_AT_abstract_origin:
1737 func->name = find_abstract_instance_name (unit, attr.u.val);
1741 /* Prefer DW_AT_MIPS_linkage_name over DW_AT_name. */
1742 if (func->name == NULL)
1743 func->name = attr.u.str;
1746 case DW_AT_MIPS_linkage_name:
1747 func->name = attr.u.str;
1751 low_pc = attr.u.val;
1755 high_pc = attr.u.val;
1759 read_rangelist (unit, &func->arange, attr.u.val);
1762 case DW_AT_decl_file:
1763 func->file = concat_filename (unit->line_table,
1767 case DW_AT_decl_line:
1768 func->line = attr.u.val;
1780 var->name = attr.u.str;
1783 case DW_AT_decl_file:
1784 var->file = concat_filename (unit->line_table,
1788 case DW_AT_decl_line:
1789 var->line = attr.u.val;
1792 case DW_AT_external:
1793 if (attr.u.val != 0)
1797 case DW_AT_location:
1801 case DW_FORM_block1:
1802 case DW_FORM_block2:
1803 case DW_FORM_block4:
1804 if (*attr.u.blk->data == DW_OP_addr)
1808 /* Verify that DW_OP_addr is the only opcode in the
1809 location, in which case the block size will be 1
1810 plus the address size. */
1811 /* ??? For TLS variables, gcc can emit
1812 DW_OP_addr <addr> DW_OP_GNU_push_tls_address
1813 which we don't handle here yet. */
1814 if (attr.u.blk->size == unit->addr_size + 1U)
1815 var->addr = bfd_get (unit->addr_size * 8,
1817 attr.u.blk->data + 1);
1832 if (func && high_pc != 0)
1834 arange_add (unit->abfd, &func->arange, low_pc, high_pc);
1837 if (abbrev->has_children)
1841 if (nesting_level >= nested_funcs_size)
1843 struct funcinfo **tmp;
1845 nested_funcs_size *= 2;
1846 tmp = bfd_realloc (nested_funcs,
1848 * sizeof (struct funcinfo *)));
1851 free (nested_funcs);
1856 nested_funcs[nesting_level] = 0;
1860 free (nested_funcs);
1864 /* Parse a DWARF2 compilation unit starting at INFO_PTR. This
1865 includes the compilation unit header that proceeds the DIE's, but
1866 does not include the length field that precedes each compilation
1867 unit header. END_PTR points one past the end of this comp unit.
1868 OFFSET_SIZE is the size of DWARF2 offsets (either 4 or 8 bytes).
1870 This routine does not read the whole compilation unit; only enough
1871 to get to the line number information for the compilation unit. */
1873 static struct comp_unit *
1874 parse_comp_unit (bfd *abfd,
1875 struct dwarf2_debug *stash,
1876 bfd_vma unit_length,
1877 bfd_byte *info_ptr_unit,
1878 unsigned int offset_size)
1880 struct comp_unit* unit;
1881 unsigned int version;
1882 bfd_uint64_t abbrev_offset = 0;
1883 unsigned int addr_size;
1884 struct abbrev_info** abbrevs;
1885 unsigned int abbrev_number, bytes_read, i;
1886 struct abbrev_info *abbrev;
1887 struct attribute attr;
1888 bfd_byte *info_ptr = stash->info_ptr;
1889 bfd_byte *end_ptr = info_ptr + unit_length;
1892 bfd_vma high_pc = 0;
1894 version = read_2_bytes (abfd, info_ptr);
1896 BFD_ASSERT (offset_size == 4 || offset_size == 8);
1897 if (offset_size == 4)
1898 abbrev_offset = read_4_bytes (abfd, info_ptr);
1900 abbrev_offset = read_8_bytes (abfd, info_ptr);
1901 info_ptr += offset_size;
1902 addr_size = read_1_byte (abfd, info_ptr);
1907 (*_bfd_error_handler) (_("Dwarf Error: found dwarf version '%u', this reader only handles version 2 information."), version);
1908 bfd_set_error (bfd_error_bad_value);
1912 if (addr_size > sizeof (bfd_vma))
1914 (*_bfd_error_handler) (_("Dwarf Error: found address size '%u', this reader can not handle sizes greater than '%u'."),
1916 (unsigned int) sizeof (bfd_vma));
1917 bfd_set_error (bfd_error_bad_value);
1921 if (addr_size != 2 && addr_size != 4 && addr_size != 8)
1923 (*_bfd_error_handler) ("Dwarf Error: found address size '%u', this reader can only handle address sizes '2', '4' and '8'.", addr_size);
1924 bfd_set_error (bfd_error_bad_value);
1928 /* Read the abbrevs for this compilation unit into a table. */
1929 abbrevs = read_abbrevs (abfd, abbrev_offset, stash);
1933 abbrev_number = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
1934 info_ptr += bytes_read;
1935 if (! abbrev_number)
1937 (*_bfd_error_handler) (_("Dwarf Error: Bad abbrev number: %u."),
1939 bfd_set_error (bfd_error_bad_value);
1943 abbrev = lookup_abbrev (abbrev_number, abbrevs);
1946 (*_bfd_error_handler) (_("Dwarf Error: Could not find abbrev number %u."),
1948 bfd_set_error (bfd_error_bad_value);
1952 amt = sizeof (struct comp_unit);
1953 unit = bfd_zalloc (abfd, amt);
1955 unit->addr_size = addr_size;
1956 unit->offset_size = offset_size;
1957 unit->abbrevs = abbrevs;
1958 unit->end_ptr = end_ptr;
1959 unit->stash = stash;
1960 unit->info_ptr_unit = info_ptr_unit;
1962 for (i = 0; i < abbrev->num_attrs; ++i)
1964 info_ptr = read_attribute (&attr, &abbrev->attrs[i], unit, info_ptr);
1966 /* Store the data if it is of an attribute we want to keep in a
1967 partial symbol table. */
1970 case DW_AT_stmt_list:
1972 unit->line_offset = attr.u.val;
1976 unit->name = attr.u.str;
1980 low_pc = attr.u.val;
1981 /* If the compilation unit DIE has a DW_AT_low_pc attribute,
1982 this is the base address to use when reading location
1983 lists or range lists. */
1984 unit->base_address = low_pc;
1988 high_pc = attr.u.val;
1992 read_rangelist (unit, &unit->arange, attr.u.val);
1995 case DW_AT_comp_dir:
1997 char *comp_dir = attr.u.str;
2000 /* Irix 6.2 native cc prepends <machine>.: to the compilation
2001 directory, get rid of it. */
2002 char *cp = strchr (comp_dir, ':');
2004 if (cp && cp != comp_dir && cp[-1] == '.' && cp[1] == '/')
2007 unit->comp_dir = comp_dir;
2017 arange_add (unit->abfd, &unit->arange, low_pc, high_pc);
2020 unit->first_child_die_ptr = info_ptr;
2024 /* Return TRUE if UNIT may contain the address given by ADDR. When
2025 there are functions written entirely with inline asm statements, the
2026 range info in the compilation unit header may not be correct. We
2027 need to consult the line info table to see if a compilation unit
2028 really contains the given address. */
2031 comp_unit_contains_address (struct comp_unit *unit, bfd_vma addr)
2033 struct arange *arange;
2038 arange = &unit->arange;
2041 if (addr >= arange->low && addr < arange->high)
2043 arange = arange->next;
2050 /* If UNIT contains ADDR, set the output parameters to the values for
2051 the line containing ADDR. The output parameters, FILENAME_PTR,
2052 FUNCTIONNAME_PTR, and LINENUMBER_PTR, are pointers to the objects
2055 Return TRUE if UNIT contains ADDR, and no errors were encountered;
2059 comp_unit_find_nearest_line (struct comp_unit *unit,
2061 const char **filename_ptr,
2062 const char **functionname_ptr,
2063 unsigned int *linenumber_ptr,
2064 struct dwarf2_debug *stash)
2068 struct funcinfo *function;
2073 if (! unit->line_table)
2075 if (! unit->stmtlist)
2081 unit->line_table = decode_line_info (unit, stash);
2083 if (! unit->line_table)
2089 if (unit->first_child_die_ptr < unit->end_ptr
2090 && ! scan_unit_for_symbols (unit))
2098 func_p = lookup_address_in_function_table (unit, addr,
2099 &function, functionname_ptr);
2100 if (func_p && (function->tag == DW_TAG_inlined_subroutine))
2101 stash->inliner_chain = function;
2102 line_p = lookup_address_in_line_info_table (unit->line_table, addr,
2103 function, filename_ptr,
2105 return line_p || func_p;
2108 /* If UNIT contains SYM at ADDR, set the output parameters to the
2109 values for the line containing SYM. The output parameters,
2110 FILENAME_PTR, and LINENUMBER_PTR, are pointers to the objects to be
2113 Return TRUE if UNIT contains SYM, and no errors were encountered;
2117 comp_unit_find_line (struct comp_unit *unit,
2120 const char **filename_ptr,
2121 unsigned int *linenumber_ptr,
2122 struct dwarf2_debug *stash)
2127 if (! unit->line_table)
2129 if (! unit->stmtlist)
2135 unit->line_table = decode_line_info (unit, stash);
2137 if (! unit->line_table)
2143 if (unit->first_child_die_ptr < unit->end_ptr
2144 && ! scan_unit_for_symbols (unit))
2151 if (sym->flags & BSF_FUNCTION)
2152 return lookup_symbol_in_function_table (unit, sym, addr,
2156 return lookup_symbol_in_variable_table (unit, sym, addr,
2161 /* Locate a section in a BFD containing debugging info. The search starts
2162 from the section after AFTER_SEC, or from the first section in the BFD if
2163 AFTER_SEC is NULL. The search works by examining the names of the
2164 sections. There are two permissiable names. The first is .debug_info.
2165 This is the standard DWARF2 name. The second is a prefix .gnu.linkonce.wi.
2166 This is a variation on the .debug_info section which has a checksum
2167 describing the contents appended onto the name. This allows the linker to
2168 identify and discard duplicate debugging sections for different
2169 compilation units. */
2170 #define DWARF2_DEBUG_INFO ".debug_info"
2171 #define GNU_LINKONCE_INFO ".gnu.linkonce.wi."
2174 find_debug_info (bfd *abfd, asection *after_sec)
2179 msec = after_sec->next;
2181 msec = abfd->sections;
2185 if (strcmp (msec->name, DWARF2_DEBUG_INFO) == 0)
2188 if (strncmp (msec->name, GNU_LINKONCE_INFO, strlen (GNU_LINKONCE_INFO)) == 0)
2197 /* Unset vmas for loadable sections in STASH. */
2200 unset_sections (struct dwarf2_debug *stash)
2203 struct loadable_section *p;
2205 i = stash->loadable_section_count;
2206 p = stash->loadable_sections;
2207 for (; i > 0; i--, p++)
2208 p->section->vma = 0;
2211 /* Set unique vmas for loadable sections in ABFD and save vmas in
2212 STASH for unset_sections. */
2215 place_sections (bfd *abfd, struct dwarf2_debug *stash)
2217 struct loadable_section *p;
2220 if (stash->loadable_section_count != 0)
2222 i = stash->loadable_section_count;
2223 p = stash->loadable_sections;
2224 for (; i > 0; i--, p++)
2225 p->section->vma = p->adj_vma;
2230 bfd_vma last_vma = 0;
2232 struct loadable_section *p;
2235 for (sect = abfd->sections; sect != NULL; sect = sect->next)
2239 if (sect->vma != 0 || (sect->flags & SEC_LOAD) == 0)
2242 sz = sect->rawsize ? sect->rawsize : sect->size;
2249 amt = i * sizeof (struct loadable_section);
2250 p = (struct loadable_section *) bfd_zalloc (abfd, amt);
2254 stash->loadable_sections = p;
2255 stash->loadable_section_count = i;
2257 for (sect = abfd->sections; sect != NULL; sect = sect->next)
2261 if (sect->vma != 0 || (sect->flags & SEC_LOAD) == 0)
2264 sz = sect->rawsize ? sect->rawsize : sect->size;
2271 /* Align the new address to the current section
2273 last_vma = ((last_vma
2274 + ~((bfd_vma) -1 << sect->alignment_power))
2275 & ((bfd_vma) -1 << sect->alignment_power));
2276 sect->vma = last_vma;
2278 p->adj_vma = sect->vma;
2279 last_vma += sect->vma + sz;
2288 /* The DWARF2 version of find_nearest_line. Return TRUE if the line
2289 is found without error. ADDR_SIZE is the number of bytes in the
2290 initial .debug_info length field and in the abbreviation offset.
2291 You may use zero to indicate that the default value should be
2295 _bfd_dwarf2_find_nearest_line (bfd *abfd,
2299 const char **filename_ptr,
2300 const char **functionname_ptr,
2301 unsigned int *linenumber_ptr,
2302 unsigned int addr_size,
2305 /* Read each compilation unit from the section .debug_info, and check
2306 to see if it contains the address we are searching for. If yes,
2307 lookup the address, and return the line number info. If no, go
2308 on to the next compilation unit.
2310 We keep a list of all the previously read compilation units, and
2311 a pointer to the next un-read compilation unit. Check the
2312 previously read units before reading more. */
2313 struct dwarf2_debug *stash;
2315 /* What address are we looking for? */
2318 struct comp_unit* each;
2320 bfd_vma found = FALSE;
2326 bfd_size_type amt = sizeof (struct dwarf2_debug);
2328 stash = bfd_zalloc (abfd, amt);
2333 /* In a relocatable file, 2 functions may have the same address.
2334 We change the section vma so that they won't overlap. */
2335 if ((abfd->flags & (EXEC_P | DYNAMIC)) == 0)
2337 if (! place_sections (abfd, stash))
2342 if (section->output_section)
2343 addr += section->output_section->vma + section->output_offset;
2345 addr += section->vma;
2346 *filename_ptr = NULL;
2347 *functionname_ptr = NULL;
2348 *linenumber_ptr = 0;
2350 /* The DWARF2 spec says that the initial length field, and the
2351 offset of the abbreviation table, should both be 4-byte values.
2352 However, some compilers do things differently. */
2355 BFD_ASSERT (addr_size == 4 || addr_size == 8);
2359 bfd_size_type total_size;
2364 msec = find_debug_info (abfd, NULL);
2366 /* No dwarf2 info. Note that at this point the stash
2367 has been allocated, but contains zeros, this lets
2368 future calls to this function fail quicker. */
2371 /* There can be more than one DWARF2 info section in a BFD these days.
2372 Read them all in and produce one large stash. We do this in two
2373 passes - in the first pass we just accumulate the section sizes.
2374 In the second pass we read in the section's contents. The allows
2375 us to avoid reallocing the data as we add sections to the stash. */
2376 for (total_size = 0; msec; msec = find_debug_info (abfd, msec))
2377 total_size += msec->size;
2379 stash->info_ptr = bfd_alloc (abfd, total_size);
2380 if (stash->info_ptr == NULL)
2383 stash->info_ptr_end = stash->info_ptr;
2385 for (msec = find_debug_info (abfd, NULL);
2387 msec = find_debug_info (abfd, msec))
2390 bfd_size_type start;
2396 start = stash->info_ptr_end - stash->info_ptr;
2398 if ((bfd_simple_get_relocated_section_contents
2399 (abfd, msec, stash->info_ptr + start, symbols)) == NULL)
2402 stash->info_ptr_end = stash->info_ptr + start + size;
2405 BFD_ASSERT (stash->info_ptr_end == stash->info_ptr + total_size);
2407 stash->sec = find_debug_info (abfd, NULL);
2408 stash->sec_info_ptr = stash->info_ptr;
2409 stash->syms = symbols;
2412 /* A null info_ptr indicates that there is no dwarf2 info
2413 (or that an error occured while setting up the stash). */
2414 if (! stash->info_ptr)
2417 stash->inliner_chain = NULL;
2419 /* Check the previously read comp. units first. */
2420 for (each = stash->all_comp_units; each; each = each->next_unit)
2421 if (comp_unit_contains_address (each, addr)
2422 && comp_unit_find_nearest_line (each, addr, filename_ptr,
2424 linenumber_ptr, stash))
2430 /* Read each remaining comp. units checking each as they are read. */
2431 while (stash->info_ptr < stash->info_ptr_end)
2434 unsigned int offset_size = addr_size;
2435 bfd_byte *info_ptr_unit = stash->info_ptr;
2437 length = read_4_bytes (abfd, stash->info_ptr);
2438 /* A 0xffffff length is the DWARF3 way of indicating we use
2439 64-bit offsets, instead of 32-bit offsets. */
2440 if (length == 0xffffffff)
2443 length = read_8_bytes (abfd, stash->info_ptr + 4);
2444 stash->info_ptr += 12;
2446 /* A zero length is the IRIX way of indicating 64-bit offsets,
2447 mostly because the 64-bit length will generally fit in 32
2448 bits, and the endianness helps. */
2449 else if (length == 0)
2452 length = read_4_bytes (abfd, stash->info_ptr + 4);
2453 stash->info_ptr += 8;
2455 /* In the absence of the hints above, we assume addr_size-sized
2456 offsets, for backward-compatibility with pre-DWARF3 64-bit
2458 else if (addr_size == 8)
2460 length = read_8_bytes (abfd, stash->info_ptr);
2461 stash->info_ptr += 8;
2464 stash->info_ptr += 4;
2468 each = parse_comp_unit (abfd, stash, length, info_ptr_unit,
2470 stash->info_ptr += length;
2472 if ((bfd_vma) (stash->info_ptr - stash->sec_info_ptr)
2473 == stash->sec->size)
2475 stash->sec = find_debug_info (abfd, stash->sec);
2476 stash->sec_info_ptr = stash->info_ptr;
2481 each->next_unit = stash->all_comp_units;
2482 stash->all_comp_units = each;
2484 /* DW_AT_low_pc and DW_AT_high_pc are optional for
2485 compilation units. If we don't have them (i.e.,
2486 unit->high == 0), we need to consult the line info
2487 table to see if a compilation unit contains the given
2489 if ((each->arange.high == 0
2490 || comp_unit_contains_address (each, addr))
2491 && comp_unit_find_nearest_line (each, addr,
2505 if ((abfd->flags & (EXEC_P | DYNAMIC)) == 0)
2506 unset_sections (stash);
2511 /* The DWARF2 version of find_line. Return TRUE if the line is found
2515 _bfd_dwarf2_find_line (bfd *abfd,
2518 const char **filename_ptr,
2519 unsigned int *linenumber_ptr,
2520 unsigned int addr_size,
2523 /* Read each compilation unit from the section .debug_info, and check
2524 to see if it contains the address we are searching for. If yes,
2525 lookup the address, and return the line number info. If no, go
2526 on to the next compilation unit.
2528 We keep a list of all the previously read compilation units, and
2529 a pointer to the next un-read compilation unit. Check the
2530 previously read units before reading more. */
2531 struct dwarf2_debug *stash;
2533 /* What address are we looking for? */
2536 struct comp_unit* each;
2540 bfd_boolean found = FALSE;
2542 section = bfd_get_section (symbol);
2548 bfd_size_type amt = sizeof (struct dwarf2_debug);
2550 stash = bfd_zalloc (abfd, amt);
2555 /* In a relocatable file, 2 functions may have the same address.
2556 We change the section vma so that they won't overlap. */
2557 if (!stash && (abfd->flags & (EXEC_P | DYNAMIC)) == 0)
2559 if (! place_sections (abfd, stash))
2563 addr = symbol->value;
2564 if (section->output_section)
2565 addr += section->output_section->vma + section->output_offset;
2567 addr += section->vma;
2569 *filename_ptr = NULL;
2570 *filename_ptr = NULL;
2571 *linenumber_ptr = 0;
2575 bfd_size_type total_size;
2580 msec = find_debug_info (abfd, NULL);
2582 /* No dwarf2 info. Note that at this point the stash
2583 has been allocated, but contains zeros, this lets
2584 future calls to this function fail quicker. */
2587 /* There can be more than one DWARF2 info section in a BFD these days.
2588 Read them all in and produce one large stash. We do this in two
2589 passes - in the first pass we just accumulate the section sizes.
2590 In the second pass we read in the section's contents. The allows
2591 us to avoid reallocing the data as we add sections to the stash. */
2592 for (total_size = 0; msec; msec = find_debug_info (abfd, msec))
2593 total_size += msec->size;
2595 stash->info_ptr = bfd_alloc (abfd, total_size);
2596 if (stash->info_ptr == NULL)
2599 stash->info_ptr_end = stash->info_ptr;
2601 for (msec = find_debug_info (abfd, NULL);
2603 msec = find_debug_info (abfd, msec))
2606 bfd_size_type start;
2612 start = stash->info_ptr_end - stash->info_ptr;
2614 if ((bfd_simple_get_relocated_section_contents
2615 (abfd, msec, stash->info_ptr + start, symbols)) == NULL)
2618 stash->info_ptr_end = stash->info_ptr + start + size;
2621 BFD_ASSERT (stash->info_ptr_end == stash->info_ptr + total_size);
2623 stash->sec = find_debug_info (abfd, NULL);
2624 stash->sec_info_ptr = stash->info_ptr;
2625 stash->syms = symbols;
2628 /* A null info_ptr indicates that there is no dwarf2 info
2629 (or that an error occured while setting up the stash). */
2630 if (! stash->info_ptr)
2633 stash->inliner_chain = NULL;
2635 /* Check the previously read comp. units first. */
2636 for (each = stash->all_comp_units; each; each = each->next_unit)
2637 if ((symbol->flags & BSF_FUNCTION) == 0
2638 || comp_unit_contains_address (each, addr))
2640 found = comp_unit_find_line (each, symbol, addr, filename_ptr,
2641 linenumber_ptr, stash);
2646 /* The DWARF2 spec says that the initial length field, and the
2647 offset of the abbreviation table, should both be 4-byte values.
2648 However, some compilers do things differently. */
2651 BFD_ASSERT (addr_size == 4 || addr_size == 8);
2653 /* Read each remaining comp. units checking each as they are read. */
2654 while (stash->info_ptr < stash->info_ptr_end)
2657 unsigned int offset_size = addr_size;
2658 bfd_byte *info_ptr_unit = stash->info_ptr;
2660 length = read_4_bytes (abfd, stash->info_ptr);
2661 /* A 0xffffff length is the DWARF3 way of indicating we use
2662 64-bit offsets, instead of 32-bit offsets. */
2663 if (length == 0xffffffff)
2666 length = read_8_bytes (abfd, stash->info_ptr + 4);
2667 stash->info_ptr += 12;
2669 /* A zero length is the IRIX way of indicating 64-bit offsets,
2670 mostly because the 64-bit length will generally fit in 32
2671 bits, and the endianness helps. */
2672 else if (length == 0)
2675 length = read_4_bytes (abfd, stash->info_ptr + 4);
2676 stash->info_ptr += 8;
2678 /* In the absence of the hints above, we assume addr_size-sized
2679 offsets, for backward-compatibility with pre-DWARF3 64-bit
2681 else if (addr_size == 8)
2683 length = read_8_bytes (abfd, stash->info_ptr);
2684 stash->info_ptr += 8;
2687 stash->info_ptr += 4;
2691 each = parse_comp_unit (abfd, stash, length, info_ptr_unit,
2693 stash->info_ptr += length;
2695 if ((bfd_vma) (stash->info_ptr - stash->sec_info_ptr)
2696 == stash->sec->size)
2698 stash->sec = find_debug_info (abfd, stash->sec);
2699 stash->sec_info_ptr = stash->info_ptr;
2704 each->next_unit = stash->all_comp_units;
2705 stash->all_comp_units = each;
2707 /* DW_AT_low_pc and DW_AT_high_pc are optional for
2708 compilation units. If we don't have them (i.e.,
2709 unit->high == 0), we need to consult the line info
2710 table to see if a compilation unit contains the given
2712 found = (((symbol->flags & BSF_FUNCTION) == 0
2713 || each->arange.high <= 0
2714 || comp_unit_contains_address (each, addr))
2715 && comp_unit_find_line (each, symbol, addr,
2726 if ((abfd->flags & (EXEC_P | DYNAMIC)) == 0)
2727 unset_sections (stash);
2733 _bfd_dwarf2_find_inliner_info (bfd *abfd ATTRIBUTE_UNUSED,
2734 const char **filename_ptr,
2735 const char **functionname_ptr,
2736 unsigned int *linenumber_ptr,
2739 struct dwarf2_debug *stash;
2744 struct funcinfo *func = stash->inliner_chain;
2745 if (func && func->caller_func)
2747 *filename_ptr = func->caller_file;
2748 *functionname_ptr = func->caller_func->name;
2749 *linenumber_ptr = func->caller_line;
2750 stash->inliner_chain = func->caller_func;
2759 _bfd_dwarf2_cleanup_debug_info (bfd *abfd)
2761 struct comp_unit *each;
2762 struct dwarf2_debug *stash;
2764 if (abfd == NULL || elf_tdata (abfd) == NULL)
2767 stash = elf_tdata (abfd)->dwarf2_find_line_info;
2772 for (each = stash->all_comp_units; each; each = each->next_unit)
2774 struct abbrev_info **abbrevs = each->abbrevs;
2777 for (i = 0; i < ABBREV_HASH_SIZE; i++)
2779 struct abbrev_info *abbrev = abbrevs[i];
2783 free (abbrev->attrs);
2784 abbrev = abbrev->next;
2788 if (each->line_table)
2790 free (each->line_table->dirs);
2791 free (each->line_table->files);
2795 free (stash->dwarf_abbrev_buffer);
2796 free (stash->dwarf_line_buffer);
2797 free (stash->dwarf_ranges_buffer);