2 Copyright 1994, 1995, 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003,
3 2004 Free Software Foundation, Inc.
5 Adapted from gdb/dwarf2read.c by Gavin Koch of Cygnus Solutions
8 From the dwarf2read.c header:
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., 59 Temple Place - Suite 330, Boston, MA 02111-1307, 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. */
79 /* A list of all previously read comp_units. */
80 struct comp_unit* all_comp_units;
82 /* The next unread compilation unit within the .debug_info section.
83 Zero indicates that the .debug_info section has not been loaded
87 /* Pointer to the end of the .debug_info section memory buffer. */
90 /* Pointer to the section and address of the beginning of the
95 /* Pointer to the symbol table. */
98 /* Pointer to the .debug_abbrev section loaded into memory. */
99 char* dwarf_abbrev_buffer;
101 /* Length of the loaded .debug_abbrev section. */
102 unsigned long dwarf_abbrev_size;
104 /* Buffer for decode_line_info. */
105 char *dwarf_line_buffer;
107 /* Length of the loaded .debug_line section. */
108 unsigned long dwarf_line_size;
110 /* Pointer to the .debug_str section loaded into memory. */
111 char* dwarf_str_buffer;
113 /* Length of the loaded .debug_str section. */
114 unsigned long dwarf_str_size;
124 /* A minimal decoding of DWARF2 compilation units. We only decode
125 what's needed to get to the line number information. */
129 /* Chain the previously read compilation units. */
130 struct comp_unit* next_unit;
132 /* Keep the bdf convenient (for memory allocation). */
135 /* The lowest and higest addresses contained in this compilation
136 unit as specified in the compilation unit header. */
137 struct arange arange;
139 /* The DW_AT_name attribute (for error messages). */
142 /* The abbrev hash table. */
143 struct abbrev_info** abbrevs;
145 /* Note that an error was found by comp_unit_find_nearest_line. */
148 /* The DW_AT_comp_dir attribute. */
151 /* TRUE if there is a line number table associated with this comp. unit. */
154 /* Pointer to the current comp_unit so that we can find a given entry
158 /* The offset into .debug_line of the line number table. */
159 unsigned long line_offset;
161 /* Pointer to the first child die for the comp unit. */
162 char *first_child_die_ptr;
164 /* The end of the comp unit. */
167 /* The decoded line number, NULL if not yet decoded. */
168 struct line_info_table* line_table;
170 /* A list of the functions found in this comp. unit. */
171 struct funcinfo* function_table;
173 /* Pointer to dwarf2_debug structure. */
174 struct dwarf2_debug *stash;
176 /* Address size for this unit - from unit header. */
177 unsigned char addr_size;
179 /* Offset size for this unit - from unit header. */
180 unsigned char offset_size;
183 /* This data structure holds the information of an abbrev. */
186 unsigned int number; /* Number identifying abbrev. */
187 enum dwarf_tag tag; /* DWARF tag. */
188 int has_children; /* Boolean. */
189 unsigned int num_attrs; /* Number of attributes. */
190 struct attr_abbrev *attrs; /* An array of attribute descriptions. */
191 struct abbrev_info *next; /* Next in chain. */
196 enum dwarf_attribute name;
197 enum dwarf_form form;
200 #ifndef ABBREV_HASH_SIZE
201 #define ABBREV_HASH_SIZE 121
203 #ifndef ATTR_ALLOC_CHUNK
204 #define ATTR_ALLOC_CHUNK 4
208 The following function up to the END VERBATIM mark are
209 copied directly from dwarf2read.c. */
211 /* Read dwarf information from a buffer. */
214 read_1_byte (bfd *abfd ATTRIBUTE_UNUSED, char *buf)
216 return bfd_get_8 (abfd, buf);
220 read_1_signed_byte (bfd *abfd ATTRIBUTE_UNUSED, char *buf)
222 return bfd_get_signed_8 (abfd, buf);
226 read_2_bytes (bfd *abfd, char *buf)
228 return bfd_get_16 (abfd, buf);
232 read_4_bytes (bfd *abfd, char *buf)
234 return bfd_get_32 (abfd, buf);
238 read_8_bytes (bfd *abfd, char *buf)
240 return bfd_get_64 (abfd, buf);
244 read_n_bytes (bfd *abfd ATTRIBUTE_UNUSED,
246 unsigned int size ATTRIBUTE_UNUSED)
248 /* If the size of a host char is 8 bits, we can return a pointer
249 to the buffer, otherwise we have to copy the data to a buffer
250 allocated on the temporary obstack. */
255 read_string (bfd *abfd ATTRIBUTE_UNUSED,
257 unsigned int *bytes_read_ptr)
259 /* Return a pointer to the embedded string. */
266 *bytes_read_ptr = strlen (buf) + 1;
271 read_indirect_string (struct comp_unit* unit,
273 unsigned int *bytes_read_ptr)
276 struct dwarf2_debug *stash = unit->stash;
278 if (unit->offset_size == 4)
279 offset = read_4_bytes (unit->abfd, buf);
281 offset = read_8_bytes (unit->abfd, buf);
282 *bytes_read_ptr = unit->offset_size;
284 if (! stash->dwarf_str_buffer)
287 bfd *abfd = unit->abfd;
290 msec = bfd_get_section_by_name (abfd, ".debug_str");
293 (*_bfd_error_handler)
294 (_("Dwarf Error: Can't find .debug_str section."));
295 bfd_set_error (bfd_error_bad_value);
299 sz = msec->rawsize ? msec->rawsize : msec->size;
300 stash->dwarf_str_size = sz;
301 stash->dwarf_str_buffer = bfd_alloc (abfd, sz);
302 if (! stash->dwarf_abbrev_buffer)
305 if (! bfd_get_section_contents (abfd, msec, stash->dwarf_str_buffer,
310 if (offset >= stash->dwarf_str_size)
312 (*_bfd_error_handler) (_("Dwarf Error: DW_FORM_strp offset (%lu) greater than or equal to .debug_str size (%lu)."),
313 (unsigned long) offset, stash->dwarf_str_size);
314 bfd_set_error (bfd_error_bad_value);
318 buf = stash->dwarf_str_buffer + offset;
327 read_address (struct comp_unit *unit, char *buf)
329 switch (unit->addr_size)
332 return bfd_get_64 (unit->abfd, buf);
334 return bfd_get_32 (unit->abfd, buf);
336 return bfd_get_16 (unit->abfd, buf);
342 /* Lookup an abbrev_info structure in the abbrev hash table. */
344 static struct abbrev_info *
345 lookup_abbrev (unsigned int number, struct abbrev_info **abbrevs)
347 unsigned int hash_number;
348 struct abbrev_info *abbrev;
350 hash_number = number % ABBREV_HASH_SIZE;
351 abbrev = abbrevs[hash_number];
355 if (abbrev->number == number)
358 abbrev = abbrev->next;
364 /* In DWARF version 2, the description of the debugging information is
365 stored in a separate .debug_abbrev section. Before we read any
366 dies from a section we read in all abbreviations and install them
369 static struct abbrev_info**
370 read_abbrevs (bfd *abfd, bfd_uint64_t offset, struct dwarf2_debug *stash)
372 struct abbrev_info **abbrevs;
374 struct abbrev_info *cur_abbrev;
375 unsigned int abbrev_number, bytes_read, abbrev_name;
376 unsigned int abbrev_form, hash_number;
379 if (! stash->dwarf_abbrev_buffer)
383 msec = bfd_get_section_by_name (abfd, ".debug_abbrev");
386 (*_bfd_error_handler) (_("Dwarf Error: Can't find .debug_abbrev section."));
387 bfd_set_error (bfd_error_bad_value);
391 stash->dwarf_abbrev_size = msec->size;
392 stash->dwarf_abbrev_buffer
393 = bfd_simple_get_relocated_section_contents (abfd, msec, NULL,
395 if (! stash->dwarf_abbrev_buffer)
399 if (offset >= stash->dwarf_abbrev_size)
401 (*_bfd_error_handler) (_("Dwarf Error: Abbrev offset (%lu) greater than or equal to .debug_abbrev size (%lu)."),
402 (unsigned long) offset, stash->dwarf_abbrev_size);
403 bfd_set_error (bfd_error_bad_value);
407 amt = sizeof (struct abbrev_info*) * ABBREV_HASH_SIZE;
408 abbrevs = bfd_zalloc (abfd, amt);
410 abbrev_ptr = stash->dwarf_abbrev_buffer + offset;
411 abbrev_number = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
412 abbrev_ptr += bytes_read;
414 /* Loop until we reach an abbrev number of 0. */
415 while (abbrev_number)
417 amt = sizeof (struct abbrev_info);
418 cur_abbrev = bfd_zalloc (abfd, amt);
420 /* Read in abbrev header. */
421 cur_abbrev->number = abbrev_number;
422 cur_abbrev->tag = (enum dwarf_tag)
423 read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
424 abbrev_ptr += bytes_read;
425 cur_abbrev->has_children = read_1_byte (abfd, abbrev_ptr);
428 /* Now read in declarations. */
429 abbrev_name = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
430 abbrev_ptr += bytes_read;
431 abbrev_form = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
432 abbrev_ptr += bytes_read;
436 if ((cur_abbrev->num_attrs % ATTR_ALLOC_CHUNK) == 0)
438 amt = cur_abbrev->num_attrs + ATTR_ALLOC_CHUNK;
439 amt *= sizeof (struct attr_abbrev);
440 cur_abbrev->attrs = bfd_realloc (cur_abbrev->attrs, amt);
441 if (! cur_abbrev->attrs)
445 cur_abbrev->attrs[cur_abbrev->num_attrs].name
446 = (enum dwarf_attribute) abbrev_name;
447 cur_abbrev->attrs[cur_abbrev->num_attrs++].form
448 = (enum dwarf_form) abbrev_form;
449 abbrev_name = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
450 abbrev_ptr += bytes_read;
451 abbrev_form = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
452 abbrev_ptr += bytes_read;
455 hash_number = abbrev_number % ABBREV_HASH_SIZE;
456 cur_abbrev->next = abbrevs[hash_number];
457 abbrevs[hash_number] = cur_abbrev;
459 /* Get next abbreviation.
460 Under Irix6 the abbreviations for a compilation unit are not
461 always properly terminated with an abbrev number of 0.
462 Exit loop if we encounter an abbreviation which we have
463 already read (which means we are about to read the abbreviations
464 for the next compile unit) or if the end of the abbreviation
466 if ((unsigned int) (abbrev_ptr - stash->dwarf_abbrev_buffer)
467 >= stash->dwarf_abbrev_size)
469 abbrev_number = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
470 abbrev_ptr += bytes_read;
471 if (lookup_abbrev (abbrev_number,abbrevs) != NULL)
478 /* Read an attribute value described by an attribute form. */
481 read_attribute_value (struct attribute *attr,
483 struct comp_unit *unit,
486 bfd *abfd = unit->abfd;
487 unsigned int bytes_read;
488 struct dwarf_block *blk;
491 attr->form = (enum dwarf_form) form;
496 /* FIXME: DWARF3 draft says DW_FORM_ref_addr is offset_size. */
497 case DW_FORM_ref_addr:
498 attr->u.val = read_address (unit, info_ptr);
499 info_ptr += unit->addr_size;
502 amt = sizeof (struct dwarf_block);
503 blk = bfd_alloc (abfd, amt);
504 blk->size = read_2_bytes (abfd, info_ptr);
506 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
507 info_ptr += blk->size;
511 amt = sizeof (struct dwarf_block);
512 blk = bfd_alloc (abfd, amt);
513 blk->size = read_4_bytes (abfd, info_ptr);
515 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
516 info_ptr += blk->size;
520 attr->u.val = read_2_bytes (abfd, info_ptr);
524 attr->u.val = read_4_bytes (abfd, info_ptr);
528 attr->u.val = read_8_bytes (abfd, info_ptr);
532 attr->u.str = read_string (abfd, info_ptr, &bytes_read);
533 info_ptr += bytes_read;
536 attr->u.str = read_indirect_string (unit, info_ptr, &bytes_read);
537 info_ptr += bytes_read;
540 amt = sizeof (struct dwarf_block);
541 blk = bfd_alloc (abfd, amt);
542 blk->size = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
543 info_ptr += bytes_read;
544 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
545 info_ptr += blk->size;
549 amt = sizeof (struct dwarf_block);
550 blk = bfd_alloc (abfd, amt);
551 blk->size = read_1_byte (abfd, info_ptr);
553 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
554 info_ptr += blk->size;
558 attr->u.val = read_1_byte (abfd, info_ptr);
562 attr->u.val = read_1_byte (abfd, info_ptr);
566 attr->u.sval = read_signed_leb128 (abfd, info_ptr, &bytes_read);
567 info_ptr += bytes_read;
570 attr->u.val = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
571 info_ptr += bytes_read;
574 attr->u.val = read_1_byte (abfd, info_ptr);
578 attr->u.val = read_2_bytes (abfd, info_ptr);
582 attr->u.val = read_4_bytes (abfd, info_ptr);
586 attr->u.val = read_8_bytes (abfd, info_ptr);
589 case DW_FORM_ref_udata:
590 attr->u.val = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
591 info_ptr += bytes_read;
593 case DW_FORM_indirect:
594 form = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
595 info_ptr += bytes_read;
596 info_ptr = read_attribute_value (attr, form, unit, info_ptr);
599 (*_bfd_error_handler) (_("Dwarf Error: Invalid or unhandled FORM value: %u."),
601 bfd_set_error (bfd_error_bad_value);
606 /* Read an attribute described by an abbreviated attribute. */
609 read_attribute (struct attribute *attr,
610 struct attr_abbrev *abbrev,
611 struct comp_unit *unit,
614 attr->name = abbrev->name;
615 info_ptr = read_attribute_value (attr, abbrev->form, unit, info_ptr);
619 /* Source line information table routines. */
621 #define FILE_ALLOC_CHUNK 5
622 #define DIR_ALLOC_CHUNK 5
626 struct line_info* prev_line;
631 int end_sequence; /* End of (sequential) code sequence. */
642 struct line_info_table
645 unsigned int num_files;
646 unsigned int num_dirs;
649 struct fileinfo* files;
650 struct line_info* last_line; /* largest VMA */
651 struct line_info* lcl_head; /* local head; used in 'add_line_info' */
656 struct funcinfo *prev_func;
662 /* Adds a new entry to the line_info list in the line_info_table, ensuring
663 that the list is sorted. Note that the line_info list is sorted from
664 highest to lowest VMA (with possible duplicates); that is,
665 line_info->prev_line always accesses an equal or smaller VMA. */
668 add_line_info (struct line_info_table *table,
675 bfd_size_type amt = sizeof (struct line_info);
676 struct line_info* info = bfd_alloc (table->abfd, amt);
678 /* Find the correct location for 'info'. Normally we will receive
679 new line_info data 1) in order and 2) with increasing VMAs.
680 However some compilers break the rules (cf. decode_line_info) and
681 so we include some heuristics for quickly finding the correct
682 location for 'info'. In particular, these heuristics optimize for
683 the common case in which the VMA sequence that we receive is a
684 list of locally sorted VMAs such as
685 p...z a...j (where a < j < p < z)
687 Note: table->lcl_head is used to head an *actual* or *possible*
688 sequence within the list (such as a...j) that is not directly
689 headed by table->last_line
691 Note: we may receive duplicate entries from 'decode_line_info'. */
694 if (!table->last_line
695 || address >= table->last_line->address)
697 /* Normal case: add 'info' to the beginning of the list */
698 info->prev_line = table->last_line;
699 table->last_line = info;
701 /* lcl_head: initialize to head a *possible* sequence at the end. */
702 if (!table->lcl_head)
703 table->lcl_head = info;
706 else if (!table->lcl_head->prev_line
707 && table->lcl_head->address > address)
709 /* Abnormal but easy: lcl_head is 1) at the *end* of the line
710 list and 2) the head of 'info'. */
711 info->prev_line = NULL;
712 table->lcl_head->prev_line = info;
715 else if (table->lcl_head->prev_line
716 && table->lcl_head->address > address
717 && address >= table->lcl_head->prev_line->address)
719 /* Abnormal but easy: lcl_head is 1) in the *middle* of the line
720 list and 2) the head of 'info'. */
721 info->prev_line = table->lcl_head->prev_line;
722 table->lcl_head->prev_line = info;
727 /* Abnormal and hard: Neither 'last_line' nor 'lcl_head' are valid
728 heads for 'info'. Reset 'lcl_head' and repeat. */
729 struct line_info* li2 = table->last_line; /* always non-NULL */
730 struct line_info* li1 = li2->prev_line;
734 if (li2->address > address && address >= li1->address)
737 li2 = li1; /* always non-NULL */
738 li1 = li1->prev_line;
740 table->lcl_head = li2;
743 /* Set member data of 'info'. */
744 info->address = address;
746 info->column = column;
747 info->end_sequence = end_sequence;
749 if (filename && filename[0])
751 info->filename = bfd_alloc (table->abfd, strlen (filename) + 1);
753 strcpy (info->filename, filename);
756 info->filename = NULL;
759 /* Extract a fully qualified filename from a line info table.
760 The returned string has been malloc'ed and it is the caller's
761 responsibility to free it. */
764 concat_filename (struct line_info_table *table, unsigned int file)
768 if (file - 1 >= table->num_files)
770 (*_bfd_error_handler)
771 (_("Dwarf Error: mangled line number section (bad file number)."));
772 return strdup ("<unknown>");
775 filename = table->files[file - 1].name;
777 if (! IS_ABSOLUTE_PATH (filename))
779 char* dirname = (table->files[file - 1].dir
780 ? table->dirs[table->files[file - 1].dir - 1]
783 /* Not all tools set DW_AT_comp_dir, so dirname may be unknown.
784 The best we can do is return the filename part. */
787 unsigned int len = strlen (dirname) + strlen (filename) + 2;
790 name = bfd_malloc (len);
792 sprintf (name, "%s/%s", dirname, filename);
797 return strdup (filename);
801 arange_add (struct comp_unit *unit, bfd_vma low_pc, bfd_vma high_pc)
803 struct arange *arange;
805 /* First see if we can cheaply extend an existing range. */
806 arange = &unit->arange;
810 if (low_pc == arange->high)
812 arange->high = high_pc;
815 if (high_pc == arange->low)
817 arange->low = low_pc;
820 arange = arange->next;
824 if (unit->arange.high == 0)
826 /* This is the first address range: store it in unit->arange. */
827 unit->arange.next = 0;
828 unit->arange.low = low_pc;
829 unit->arange.high = high_pc;
833 /* Need to allocate a new arange and insert it into the arange list. */
834 arange = bfd_zalloc (unit->abfd, sizeof (*arange));
835 arange->low = low_pc;
836 arange->high = high_pc;
838 arange->next = unit->arange.next;
839 unit->arange.next = arange;
842 /* Decode the line number information for UNIT. */
844 static struct line_info_table*
845 decode_line_info (struct comp_unit *unit, struct dwarf2_debug *stash)
847 bfd *abfd = unit->abfd;
848 struct line_info_table* table;
852 unsigned int i, bytes_read, offset_size;
853 char *cur_file, *cur_dir;
854 unsigned char op_code, extended_op, adj_opcode;
857 if (! stash->dwarf_line_buffer)
861 msec = bfd_get_section_by_name (abfd, ".debug_line");
864 (*_bfd_error_handler) (_("Dwarf Error: Can't find .debug_line section."));
865 bfd_set_error (bfd_error_bad_value);
869 stash->dwarf_line_size = msec->size;
870 stash->dwarf_line_buffer
871 = bfd_simple_get_relocated_section_contents (abfd, msec, NULL,
873 if (! stash->dwarf_line_buffer)
877 /* It is possible to get a bad value for the line_offset. Validate
878 it here so that we won't get a segfault below. */
879 if (unit->line_offset >= stash->dwarf_line_size)
881 (*_bfd_error_handler) (_("Dwarf Error: Line offset (%lu) greater than or equal to .debug_line size (%lu)."),
882 unit->line_offset, stash->dwarf_line_size);
883 bfd_set_error (bfd_error_bad_value);
887 amt = sizeof (struct line_info_table);
888 table = bfd_alloc (abfd, amt);
890 table->comp_dir = unit->comp_dir;
892 table->num_files = 0;
899 table->last_line = NULL;
900 table->lcl_head = NULL;
902 line_ptr = stash->dwarf_line_buffer + unit->line_offset;
904 /* Read in the prologue. */
905 lh.total_length = read_4_bytes (abfd, line_ptr);
908 if (lh.total_length == 0xffffffff)
910 lh.total_length = read_8_bytes (abfd, line_ptr);
914 else if (lh.total_length == 0 && unit->addr_size == 8)
916 /* Handle (non-standard) 64-bit DWARF2 formats. */
917 lh.total_length = read_4_bytes (abfd, line_ptr);
921 line_end = line_ptr + lh.total_length;
922 lh.version = read_2_bytes (abfd, line_ptr);
924 if (offset_size == 4)
925 lh.prologue_length = read_4_bytes (abfd, line_ptr);
927 lh.prologue_length = read_8_bytes (abfd, line_ptr);
928 line_ptr += offset_size;
929 lh.minimum_instruction_length = read_1_byte (abfd, line_ptr);
931 lh.default_is_stmt = read_1_byte (abfd, line_ptr);
933 lh.line_base = read_1_signed_byte (abfd, line_ptr);
935 lh.line_range = read_1_byte (abfd, line_ptr);
937 lh.opcode_base = read_1_byte (abfd, line_ptr);
939 amt = lh.opcode_base * sizeof (unsigned char);
940 lh.standard_opcode_lengths = bfd_alloc (abfd, amt);
942 lh.standard_opcode_lengths[0] = 1;
944 for (i = 1; i < lh.opcode_base; ++i)
946 lh.standard_opcode_lengths[i] = read_1_byte (abfd, line_ptr);
950 /* Read directory table. */
951 while ((cur_dir = read_string (abfd, line_ptr, &bytes_read)) != NULL)
953 line_ptr += bytes_read;
955 if ((table->num_dirs % DIR_ALLOC_CHUNK) == 0)
957 amt = table->num_dirs + DIR_ALLOC_CHUNK;
958 amt *= sizeof (char *);
959 table->dirs = bfd_realloc (table->dirs, amt);
964 table->dirs[table->num_dirs++] = cur_dir;
967 line_ptr += bytes_read;
969 /* Read file name table. */
970 while ((cur_file = read_string (abfd, line_ptr, &bytes_read)) != NULL)
972 line_ptr += bytes_read;
974 if ((table->num_files % FILE_ALLOC_CHUNK) == 0)
976 amt = table->num_files + FILE_ALLOC_CHUNK;
977 amt *= sizeof (struct fileinfo);
978 table->files = bfd_realloc (table->files, amt);
983 table->files[table->num_files].name = cur_file;
984 table->files[table->num_files].dir =
985 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
986 line_ptr += bytes_read;
987 table->files[table->num_files].time =
988 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
989 line_ptr += bytes_read;
990 table->files[table->num_files].size =
991 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
992 line_ptr += bytes_read;
996 line_ptr += bytes_read;
998 /* Read the statement sequences until there's nothing left. */
999 while (line_ptr < line_end)
1001 /* State machine registers. */
1002 bfd_vma address = 0;
1003 char * filename = table->num_files ? concat_filename (table, 1) : NULL;
1004 unsigned int line = 1;
1005 unsigned int column = 0;
1006 int is_stmt = lh.default_is_stmt;
1007 int basic_block = 0;
1008 int end_sequence = 0;
1010 compilers generate address sequences that are wildly out of
1011 order using DW_LNE_set_address (e.g. Intel C++ 6.0 compiler
1012 for ia64-Linux). Thus, to determine the low and high
1013 address, we must compare on every DW_LNS_copy, etc. */
1015 bfd_vma high_pc = 0;
1017 /* Decode the table. */
1018 while (! end_sequence)
1020 op_code = read_1_byte (abfd, line_ptr);
1023 if (op_code >= lh.opcode_base)
1025 /* Special operand. */
1026 adj_opcode = op_code - lh.opcode_base;
1027 address += (adj_opcode / lh.line_range)
1028 * lh.minimum_instruction_length;
1029 line += lh.line_base + (adj_opcode % lh.line_range);
1030 /* Append row to matrix using current values. */
1031 add_line_info (table, address, filename, line, column, 0);
1033 if (low_pc == 0 || address < low_pc)
1035 if (address > high_pc)
1038 else switch (op_code)
1040 case DW_LNS_extended_op:
1041 /* Ignore length. */
1043 extended_op = read_1_byte (abfd, line_ptr);
1046 switch (extended_op)
1048 case DW_LNE_end_sequence:
1050 add_line_info (table, address, filename, line, column,
1052 if (low_pc == 0 || address < low_pc)
1054 if (address > high_pc)
1056 arange_add (unit, low_pc, high_pc);
1058 case DW_LNE_set_address:
1059 address = read_address (unit, line_ptr);
1060 line_ptr += unit->addr_size;
1062 case DW_LNE_define_file:
1063 cur_file = read_string (abfd, line_ptr, &bytes_read);
1064 line_ptr += bytes_read;
1065 if ((table->num_files % FILE_ALLOC_CHUNK) == 0)
1067 amt = table->num_files + FILE_ALLOC_CHUNK;
1068 amt *= sizeof (struct fileinfo);
1069 table->files = bfd_realloc (table->files, amt);
1073 table->files[table->num_files].name = cur_file;
1074 table->files[table->num_files].dir =
1075 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
1076 line_ptr += bytes_read;
1077 table->files[table->num_files].time =
1078 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
1079 line_ptr += bytes_read;
1080 table->files[table->num_files].size =
1081 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
1082 line_ptr += bytes_read;
1086 (*_bfd_error_handler) (_("Dwarf Error: mangled line number section."));
1087 bfd_set_error (bfd_error_bad_value);
1092 add_line_info (table, address, filename, line, column, 0);
1094 if (low_pc == 0 || address < low_pc)
1096 if (address > high_pc)
1099 case DW_LNS_advance_pc:
1100 address += lh.minimum_instruction_length
1101 * read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
1102 line_ptr += bytes_read;
1104 case DW_LNS_advance_line:
1105 line += read_signed_leb128 (abfd, line_ptr, &bytes_read);
1106 line_ptr += bytes_read;
1108 case DW_LNS_set_file:
1112 /* The file and directory tables are 0
1113 based, the references are 1 based. */
1114 file = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
1115 line_ptr += bytes_read;
1118 filename = concat_filename (table, file);
1121 case DW_LNS_set_column:
1122 column = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
1123 line_ptr += bytes_read;
1125 case DW_LNS_negate_stmt:
1126 is_stmt = (!is_stmt);
1128 case DW_LNS_set_basic_block:
1131 case DW_LNS_const_add_pc:
1132 address += lh.minimum_instruction_length
1133 * ((255 - lh.opcode_base) / lh.line_range);
1135 case DW_LNS_fixed_advance_pc:
1136 address += read_2_bytes (abfd, line_ptr);
1143 /* Unknown standard opcode, ignore it. */
1144 for (i = 0; i < lh.standard_opcode_lengths[op_code]; i++)
1146 (void) read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
1147 line_ptr += bytes_read;
1160 /* If ADDR is within TABLE set the output parameters and return TRUE,
1161 otherwise return FALSE. The output parameters, FILENAME_PTR and
1162 LINENUMBER_PTR, are pointers to the objects to be filled in. */
1165 lookup_address_in_line_info_table (struct line_info_table *table,
1167 struct funcinfo *function,
1168 const char **filename_ptr,
1169 unsigned int *linenumber_ptr)
1171 /* Note: table->last_line should be a descendingly sorted list. */
1172 struct line_info* next_line = table->last_line;
1173 struct line_info* each_line = NULL;
1174 *filename_ptr = NULL;
1179 each_line = next_line->prev_line;
1181 /* Check for large addresses */
1182 if (addr > next_line->address)
1183 each_line = NULL; /* ensure we skip over the normal case */
1185 /* Normal case: search the list; save */
1186 while (each_line && next_line)
1188 /* If we have an address match, save this info. This allows us
1189 to return as good as results as possible for strange debugging
1191 bfd_boolean addr_match = FALSE;
1192 if (each_line->address <= addr && addr <= next_line->address)
1196 /* If this line appears to span functions, and addr is in the
1197 later function, return the first line of that function instead
1198 of the last line of the earlier one. This check is for GCC
1199 2.95, which emits the first line number for a function late. */
1200 if (function != NULL
1201 && each_line->address < function->low
1202 && next_line->address > function->low)
1204 *filename_ptr = next_line->filename;
1205 *linenumber_ptr = next_line->line;
1209 *filename_ptr = each_line->filename;
1210 *linenumber_ptr = each_line->line;
1214 if (addr_match && !each_line->end_sequence)
1215 return TRUE; /* we have definitely found what we want */
1217 next_line = each_line;
1218 each_line = each_line->prev_line;
1221 /* At this point each_line is NULL but next_line is not. If we found
1222 a candidate end-of-sequence point in the loop above, we can return
1223 that (compatibility with a bug in the Intel compiler); otherwise,
1224 assuming that we found the containing function for this address in
1225 this compilation unit, return the first line we have a number for
1226 (compatibility with GCC 2.95). */
1227 if (*filename_ptr == NULL && function != NULL)
1229 *filename_ptr = next_line->filename;
1230 *linenumber_ptr = next_line->line;
1237 /* Function table functions. */
1239 /* If ADDR is within TABLE, set FUNCTIONNAME_PTR, and return TRUE. */
1242 lookup_address_in_function_table (struct funcinfo *table,
1244 struct funcinfo **function_ptr,
1245 const char **functionname_ptr)
1247 struct funcinfo* each_func;
1249 for (each_func = table;
1251 each_func = each_func->prev_func)
1253 if (addr >= each_func->low && addr < each_func->high)
1255 *functionname_ptr = each_func->name;
1256 *function_ptr = each_func;
1265 find_abstract_instance_name (struct comp_unit *unit, bfd_uint64_t die_ref)
1267 bfd *abfd = unit->abfd;
1269 unsigned int abbrev_number, bytes_read, i;
1270 struct abbrev_info *abbrev;
1271 struct attribute attr;
1274 info_ptr = unit->info_ptr_unit + die_ref;
1275 abbrev_number = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
1276 info_ptr += bytes_read;
1280 abbrev = lookup_abbrev (abbrev_number, unit->abbrevs);
1283 (*_bfd_error_handler) (_("Dwarf Error: Could not find abbrev number %u."),
1285 bfd_set_error (bfd_error_bad_value);
1289 for (i = 0; i < abbrev->num_attrs && !name; ++i)
1291 info_ptr = read_attribute (&attr, &abbrev->attrs[i], unit, info_ptr);
1292 if (attr.name == DW_AT_name)
1300 /* DWARF2 Compilation unit functions. */
1302 /* Scan over each die in a comp. unit looking for functions to add
1303 to the function table. */
1306 scan_unit_for_functions (struct comp_unit *unit)
1308 bfd *abfd = unit->abfd;
1309 char *info_ptr = unit->first_child_die_ptr;
1310 int nesting_level = 1;
1312 while (nesting_level)
1314 unsigned int abbrev_number, bytes_read, i;
1315 struct abbrev_info *abbrev;
1316 struct attribute attr;
1317 struct funcinfo *func;
1320 abbrev_number = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
1321 info_ptr += bytes_read;
1323 if (! abbrev_number)
1329 abbrev = lookup_abbrev (abbrev_number,unit->abbrevs);
1332 (*_bfd_error_handler) (_("Dwarf Error: Could not find abbrev number %u."),
1334 bfd_set_error (bfd_error_bad_value);
1338 if (abbrev->tag == DW_TAG_subprogram
1339 || abbrev->tag == DW_TAG_inlined_subroutine)
1341 bfd_size_type amt = sizeof (struct funcinfo);
1342 func = bfd_zalloc (abfd, amt);
1343 func->prev_func = unit->function_table;
1344 unit->function_table = func;
1349 for (i = 0; i < abbrev->num_attrs; ++i)
1351 info_ptr = read_attribute (&attr, &abbrev->attrs[i], unit, info_ptr);
1357 case DW_AT_abstract_origin:
1358 func->name = find_abstract_instance_name (unit, attr.u.val);
1365 /* Prefer DW_AT_MIPS_linkage_name over DW_AT_name. */
1366 if (func->name == NULL)
1367 func->name = attr.u.str;
1370 case DW_AT_MIPS_linkage_name:
1371 func->name = attr.u.str;
1375 func->low = attr.u.val;
1379 func->high = attr.u.val;
1400 if (abbrev->has_children)
1407 /* Parse a DWARF2 compilation unit starting at INFO_PTR. This
1408 includes the compilation unit header that proceeds the DIE's, but
1409 does not include the length field that precedes each compilation
1410 unit header. END_PTR points one past the end of this comp unit.
1411 OFFSET_SIZE is the size of DWARF2 offsets (either 4 or 8 bytes).
1413 This routine does not read the whole compilation unit; only enough
1414 to get to the line number information for the compilation unit. */
1416 static struct comp_unit *
1417 parse_comp_unit (bfd *abfd,
1418 struct dwarf2_debug *stash,
1419 bfd_vma unit_length,
1420 char *info_ptr_unit,
1421 unsigned int offset_size)
1423 struct comp_unit* unit;
1424 unsigned int version;
1425 bfd_uint64_t abbrev_offset = 0;
1426 unsigned int addr_size;
1427 struct abbrev_info** abbrevs;
1428 unsigned int abbrev_number, bytes_read, i;
1429 struct abbrev_info *abbrev;
1430 struct attribute attr;
1431 char *info_ptr = stash->info_ptr;
1432 char *end_ptr = info_ptr + unit_length;
1435 version = read_2_bytes (abfd, info_ptr);
1437 BFD_ASSERT (offset_size == 4 || offset_size == 8);
1438 if (offset_size == 4)
1439 abbrev_offset = read_4_bytes (abfd, info_ptr);
1441 abbrev_offset = read_8_bytes (abfd, info_ptr);
1442 info_ptr += offset_size;
1443 addr_size = read_1_byte (abfd, info_ptr);
1448 (*_bfd_error_handler) (_("Dwarf Error: found dwarf version '%u', this reader only handles version 2 information."), version);
1449 bfd_set_error (bfd_error_bad_value);
1453 if (addr_size > sizeof (bfd_vma))
1455 (*_bfd_error_handler) (_("Dwarf Error: found address size '%u', this reader can not handle sizes greater than '%u'."),
1457 (unsigned int) sizeof (bfd_vma));
1458 bfd_set_error (bfd_error_bad_value);
1462 if (addr_size != 2 && addr_size != 4 && addr_size != 8)
1464 (*_bfd_error_handler) ("Dwarf Error: found address size '%u', this reader can only handle address sizes '2', '4' and '8'.", addr_size);
1465 bfd_set_error (bfd_error_bad_value);
1469 /* Read the abbrevs for this compilation unit into a table. */
1470 abbrevs = read_abbrevs (abfd, abbrev_offset, stash);
1474 abbrev_number = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
1475 info_ptr += bytes_read;
1476 if (! abbrev_number)
1478 (*_bfd_error_handler) (_("Dwarf Error: Bad abbrev number: %u."),
1480 bfd_set_error (bfd_error_bad_value);
1484 abbrev = lookup_abbrev (abbrev_number, abbrevs);
1487 (*_bfd_error_handler) (_("Dwarf Error: Could not find abbrev number %u."),
1489 bfd_set_error (bfd_error_bad_value);
1493 amt = sizeof (struct comp_unit);
1494 unit = bfd_zalloc (abfd, amt);
1496 unit->addr_size = addr_size;
1497 unit->offset_size = offset_size;
1498 unit->abbrevs = abbrevs;
1499 unit->end_ptr = end_ptr;
1500 unit->stash = stash;
1501 unit->info_ptr_unit = info_ptr_unit;
1503 for (i = 0; i < abbrev->num_attrs; ++i)
1505 info_ptr = read_attribute (&attr, &abbrev->attrs[i], unit, info_ptr);
1507 /* Store the data if it is of an attribute we want to keep in a
1508 partial symbol table. */
1511 case DW_AT_stmt_list:
1513 unit->line_offset = attr.u.val;
1517 unit->name = attr.u.str;
1521 unit->arange.low = attr.u.val;
1525 unit->arange.high = attr.u.val;
1528 case DW_AT_comp_dir:
1530 char* comp_dir = attr.u.str;
1533 /* Irix 6.2 native cc prepends <machine>.: to the compilation
1534 directory, get rid of it. */
1535 char *cp = strchr (comp_dir, ':');
1537 if (cp && cp != comp_dir && cp[-1] == '.' && cp[1] == '/')
1540 unit->comp_dir = comp_dir;
1549 unit->first_child_die_ptr = info_ptr;
1553 /* Return TRUE if UNIT contains the address given by ADDR. */
1556 comp_unit_contains_address (struct comp_unit *unit, bfd_vma addr)
1558 struct arange *arange;
1563 arange = &unit->arange;
1566 if (addr >= arange->low && addr < arange->high)
1568 arange = arange->next;
1575 /* If UNIT contains ADDR, set the output parameters to the values for
1576 the line containing ADDR. The output parameters, FILENAME_PTR,
1577 FUNCTIONNAME_PTR, and LINENUMBER_PTR, are pointers to the objects
1580 Return TRUE if UNIT contains ADDR, and no errors were encountered;
1584 comp_unit_find_nearest_line (struct comp_unit *unit,
1586 const char **filename_ptr,
1587 const char **functionname_ptr,
1588 unsigned int *linenumber_ptr,
1589 struct dwarf2_debug *stash)
1593 struct funcinfo *function;
1598 if (! unit->line_table)
1600 if (! unit->stmtlist)
1606 unit->line_table = decode_line_info (unit, stash);
1608 if (! unit->line_table)
1614 if (unit->first_child_die_ptr < unit->end_ptr
1615 && ! scan_unit_for_functions (unit))
1623 func_p = lookup_address_in_function_table (unit->function_table, addr,
1624 &function, functionname_ptr);
1625 line_p = lookup_address_in_line_info_table (unit->line_table, addr,
1626 function, filename_ptr,
1628 return line_p || func_p;
1631 /* Locate a section in a BFD containing debugging info. The search starts
1632 from the section after AFTER_SEC, or from the first section in the BFD if
1633 AFTER_SEC is NULL. The search works by examining the names of the
1634 sections. There are two permissiable names. The first is .debug_info.
1635 This is the standard DWARF2 name. The second is a prefix .gnu.linkonce.wi.
1636 This is a variation on the .debug_info section which has a checksum
1637 describing the contents appended onto the name. This allows the linker to
1638 identify and discard duplicate debugging sections for different
1639 compilation units. */
1640 #define DWARF2_DEBUG_INFO ".debug_info"
1641 #define GNU_LINKONCE_INFO ".gnu.linkonce.wi."
1644 find_debug_info (bfd *abfd, asection *after_sec)
1649 msec = after_sec->next;
1651 msec = abfd->sections;
1655 if (strcmp (msec->name, DWARF2_DEBUG_INFO) == 0)
1658 if (strncmp (msec->name, GNU_LINKONCE_INFO, strlen (GNU_LINKONCE_INFO)) == 0)
1667 /* The DWARF2 version of find_nearest_line. Return TRUE if the line
1668 is found without error. ADDR_SIZE is the number of bytes in the
1669 initial .debug_info length field and in the abbreviation offset.
1670 You may use zero to indicate that the default value should be
1674 _bfd_dwarf2_find_nearest_line (bfd *abfd,
1678 const char **filename_ptr,
1679 const char **functionname_ptr,
1680 unsigned int *linenumber_ptr,
1681 unsigned int addr_size,
1684 /* Read each compilation unit from the section .debug_info, and check
1685 to see if it contains the address we are searching for. If yes,
1686 lookup the address, and return the line number info. If no, go
1687 on to the next compilation unit.
1689 We keep a list of all the previously read compilation units, and
1690 a pointer to the next un-read compilation unit. Check the
1691 previously read units before reading more. */
1692 struct dwarf2_debug *stash;
1694 /* What address are we looking for? */
1697 struct comp_unit* each;
1701 if (section->output_section)
1702 addr += section->output_section->vma + section->output_offset;
1704 addr += section->vma;
1705 *filename_ptr = NULL;
1706 *functionname_ptr = NULL;
1707 *linenumber_ptr = 0;
1709 /* The DWARF2 spec says that the initial length field, and the
1710 offset of the abbreviation table, should both be 4-byte values.
1711 However, some compilers do things differently. */
1714 BFD_ASSERT (addr_size == 4 || addr_size == 8);
1718 bfd_size_type total_size;
1720 bfd_size_type amt = sizeof (struct dwarf2_debug);
1722 stash = bfd_zalloc (abfd, amt);
1728 msec = find_debug_info (abfd, NULL);
1730 /* No dwarf2 info. Note that at this point the stash
1731 has been allocated, but contains zeros, this lets
1732 future calls to this function fail quicker. */
1735 /* There can be more than one DWARF2 info section in a BFD these days.
1736 Read them all in and produce one large stash. We do this in two
1737 passes - in the first pass we just accumulate the section sizes.
1738 In the second pass we read in the section's contents. The allows
1739 us to avoid reallocing the data as we add sections to the stash. */
1740 for (total_size = 0; msec; msec = find_debug_info (abfd, msec))
1741 total_size += msec->size;
1743 stash->info_ptr = bfd_alloc (abfd, total_size);
1744 if (stash->info_ptr == NULL)
1747 stash->info_ptr_end = stash->info_ptr;
1749 for (msec = find_debug_info (abfd, NULL);
1751 msec = find_debug_info (abfd, msec))
1754 bfd_size_type start;
1760 start = stash->info_ptr_end - stash->info_ptr;
1762 if ((bfd_simple_get_relocated_section_contents
1763 (abfd, msec, stash->info_ptr + start, symbols)) == NULL)
1766 stash->info_ptr_end = stash->info_ptr + start + size;
1769 BFD_ASSERT (stash->info_ptr_end == stash->info_ptr + total_size);
1771 stash->sec = find_debug_info (abfd, NULL);
1772 stash->sec_info_ptr = stash->info_ptr;
1773 stash->syms = symbols;
1776 /* A null info_ptr indicates that there is no dwarf2 info
1777 (or that an error occured while setting up the stash). */
1778 if (! stash->info_ptr)
1781 /* Check the previously read comp. units first. */
1782 for (each = stash->all_comp_units; each; each = each->next_unit)
1783 if (comp_unit_contains_address (each, addr))
1784 return comp_unit_find_nearest_line (each, addr, filename_ptr,
1785 functionname_ptr, linenumber_ptr,
1788 /* Read each remaining comp. units checking each as they are read. */
1789 while (stash->info_ptr < stash->info_ptr_end)
1793 unsigned int offset_size = addr_size;
1794 char *info_ptr_unit = stash->info_ptr;
1796 length = read_4_bytes (abfd, stash->info_ptr);
1797 /* A 0xffffff length is the DWARF3 way of indicating we use
1798 64-bit offsets, instead of 32-bit offsets. */
1799 if (length == 0xffffffff)
1802 length = read_8_bytes (abfd, stash->info_ptr + 4);
1803 stash->info_ptr += 12;
1805 /* A zero length is the IRIX way of indicating 64-bit offsets,
1806 mostly because the 64-bit length will generally fit in 32
1807 bits, and the endianness helps. */
1808 else if (length == 0)
1811 length = read_4_bytes (abfd, stash->info_ptr + 4);
1812 stash->info_ptr += 8;
1814 /* In the absence of the hints above, we assume addr_size-sized
1815 offsets, for backward-compatibility with pre-DWARF3 64-bit
1817 else if (addr_size == 8)
1819 length = read_8_bytes (abfd, stash->info_ptr);
1820 stash->info_ptr += 8;
1823 stash->info_ptr += 4;
1827 each = parse_comp_unit (abfd, stash, length, info_ptr_unit,
1829 stash->info_ptr += length;
1831 if ((bfd_vma) (stash->info_ptr - stash->sec_info_ptr)
1832 == stash->sec->size)
1834 stash->sec = find_debug_info (abfd, stash->sec);
1835 stash->sec_info_ptr = stash->info_ptr;
1840 each->next_unit = stash->all_comp_units;
1841 stash->all_comp_units = each;
1843 /* DW_AT_low_pc and DW_AT_high_pc are optional for
1844 compilation units. If we don't have them (i.e.,
1845 unit->high == 0), we need to consult the line info
1846 table to see if a compilation unit contains the given
1848 if (each->arange.high > 0)
1850 if (comp_unit_contains_address (each, addr))
1851 return comp_unit_find_nearest_line (each, addr,
1859 found = comp_unit_find_nearest_line (each, addr,