2 Copyright 1994, 1995, 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003,
3 2004, 2005 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. */
88 bfd_byte *info_ptr_end;
90 /* Pointer to the section and address of the beginning of the
93 bfd_byte *sec_info_ptr;
95 /* Pointer to the symbol table. */
98 /* Pointer to the .debug_abbrev section loaded into memory. */
99 bfd_byte *dwarf_abbrev_buffer;
101 /* Length of the loaded .debug_abbrev section. */
102 unsigned long dwarf_abbrev_size;
104 /* Buffer for decode_line_info. */
105 bfd_byte *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 bfd_byte *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 highest 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
156 bfd_byte *info_ptr_unit;
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 bfd_byte *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, bfd_byte *buf)
216 return bfd_get_8 (abfd, buf);
220 read_1_signed_byte (bfd *abfd ATTRIBUTE_UNUSED, bfd_byte *buf)
222 return bfd_get_signed_8 (abfd, buf);
226 read_2_bytes (bfd *abfd, bfd_byte *buf)
228 return bfd_get_16 (abfd, buf);
232 read_4_bytes (bfd *abfd, bfd_byte *buf)
234 return bfd_get_32 (abfd, buf);
238 read_8_bytes (bfd *abfd, bfd_byte *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. */
260 char *str = (char *) buf;
267 *bytes_read_ptr = strlen (str) + 1;
272 read_indirect_string (struct comp_unit* unit,
274 unsigned int *bytes_read_ptr)
277 struct dwarf2_debug *stash = unit->stash;
280 if (unit->offset_size == 4)
281 offset = read_4_bytes (unit->abfd, buf);
283 offset = read_8_bytes (unit->abfd, buf);
284 *bytes_read_ptr = unit->offset_size;
286 if (! stash->dwarf_str_buffer)
289 bfd *abfd = unit->abfd;
292 msec = bfd_get_section_by_name (abfd, ".debug_str");
295 (*_bfd_error_handler)
296 (_("Dwarf Error: Can't find .debug_str section."));
297 bfd_set_error (bfd_error_bad_value);
301 sz = msec->rawsize ? msec->rawsize : msec->size;
302 stash->dwarf_str_size = sz;
303 stash->dwarf_str_buffer = bfd_alloc (abfd, sz);
304 if (! stash->dwarf_str_buffer)
307 if (! bfd_get_section_contents (abfd, msec, stash->dwarf_str_buffer,
312 if (offset >= stash->dwarf_str_size)
314 (*_bfd_error_handler) (_("Dwarf Error: DW_FORM_strp offset (%lu) greater than or equal to .debug_str size (%lu)."),
315 (unsigned long) offset, stash->dwarf_str_size);
316 bfd_set_error (bfd_error_bad_value);
320 str = (char *) stash->dwarf_str_buffer + offset;
329 read_address (struct comp_unit *unit, bfd_byte *buf)
331 switch (unit->addr_size)
334 return bfd_get_64 (unit->abfd, buf);
336 return bfd_get_32 (unit->abfd, buf);
338 return bfd_get_16 (unit->abfd, buf);
344 /* Lookup an abbrev_info structure in the abbrev hash table. */
346 static struct abbrev_info *
347 lookup_abbrev (unsigned int number, struct abbrev_info **abbrevs)
349 unsigned int hash_number;
350 struct abbrev_info *abbrev;
352 hash_number = number % ABBREV_HASH_SIZE;
353 abbrev = abbrevs[hash_number];
357 if (abbrev->number == number)
360 abbrev = abbrev->next;
366 /* In DWARF version 2, the description of the debugging information is
367 stored in a separate .debug_abbrev section. Before we read any
368 dies from a section we read in all abbreviations and install them
371 static struct abbrev_info**
372 read_abbrevs (bfd *abfd, bfd_uint64_t offset, struct dwarf2_debug *stash)
374 struct abbrev_info **abbrevs;
375 bfd_byte *abbrev_ptr;
376 struct abbrev_info *cur_abbrev;
377 unsigned int abbrev_number, bytes_read, abbrev_name;
378 unsigned int abbrev_form, hash_number;
381 if (! stash->dwarf_abbrev_buffer)
385 msec = bfd_get_section_by_name (abfd, ".debug_abbrev");
388 (*_bfd_error_handler) (_("Dwarf Error: Can't find .debug_abbrev section."));
389 bfd_set_error (bfd_error_bad_value);
393 stash->dwarf_abbrev_size = msec->size;
394 stash->dwarf_abbrev_buffer
395 = bfd_simple_get_relocated_section_contents (abfd, msec, NULL,
397 if (! stash->dwarf_abbrev_buffer)
401 if (offset >= stash->dwarf_abbrev_size)
403 (*_bfd_error_handler) (_("Dwarf Error: Abbrev offset (%lu) greater than or equal to .debug_abbrev size (%lu)."),
404 (unsigned long) offset, stash->dwarf_abbrev_size);
405 bfd_set_error (bfd_error_bad_value);
409 amt = sizeof (struct abbrev_info*) * ABBREV_HASH_SIZE;
410 abbrevs = bfd_zalloc (abfd, amt);
412 abbrev_ptr = stash->dwarf_abbrev_buffer + offset;
413 abbrev_number = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
414 abbrev_ptr += bytes_read;
416 /* Loop until we reach an abbrev number of 0. */
417 while (abbrev_number)
419 amt = sizeof (struct abbrev_info);
420 cur_abbrev = bfd_zalloc (abfd, amt);
422 /* Read in abbrev header. */
423 cur_abbrev->number = abbrev_number;
424 cur_abbrev->tag = (enum dwarf_tag)
425 read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
426 abbrev_ptr += bytes_read;
427 cur_abbrev->has_children = read_1_byte (abfd, abbrev_ptr);
430 /* Now read in declarations. */
431 abbrev_name = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
432 abbrev_ptr += bytes_read;
433 abbrev_form = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
434 abbrev_ptr += bytes_read;
438 if ((cur_abbrev->num_attrs % ATTR_ALLOC_CHUNK) == 0)
440 amt = cur_abbrev->num_attrs + ATTR_ALLOC_CHUNK;
441 amt *= sizeof (struct attr_abbrev);
442 cur_abbrev->attrs = bfd_realloc (cur_abbrev->attrs, amt);
443 if (! cur_abbrev->attrs)
447 cur_abbrev->attrs[cur_abbrev->num_attrs].name
448 = (enum dwarf_attribute) abbrev_name;
449 cur_abbrev->attrs[cur_abbrev->num_attrs++].form
450 = (enum dwarf_form) abbrev_form;
451 abbrev_name = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
452 abbrev_ptr += bytes_read;
453 abbrev_form = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
454 abbrev_ptr += bytes_read;
457 hash_number = abbrev_number % ABBREV_HASH_SIZE;
458 cur_abbrev->next = abbrevs[hash_number];
459 abbrevs[hash_number] = cur_abbrev;
461 /* Get next abbreviation.
462 Under Irix6 the abbreviations for a compilation unit are not
463 always properly terminated with an abbrev number of 0.
464 Exit loop if we encounter an abbreviation which we have
465 already read (which means we are about to read the abbreviations
466 for the next compile unit) or if the end of the abbreviation
468 if ((unsigned int) (abbrev_ptr - stash->dwarf_abbrev_buffer)
469 >= stash->dwarf_abbrev_size)
471 abbrev_number = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
472 abbrev_ptr += bytes_read;
473 if (lookup_abbrev (abbrev_number,abbrevs) != NULL)
480 /* Read an attribute value described by an attribute form. */
483 read_attribute_value (struct attribute *attr,
485 struct comp_unit *unit,
488 bfd *abfd = unit->abfd;
489 unsigned int bytes_read;
490 struct dwarf_block *blk;
493 attr->form = (enum dwarf_form) form;
498 /* FIXME: DWARF3 draft says DW_FORM_ref_addr is offset_size. */
499 case DW_FORM_ref_addr:
500 attr->u.val = read_address (unit, info_ptr);
501 info_ptr += unit->addr_size;
504 amt = sizeof (struct dwarf_block);
505 blk = bfd_alloc (abfd, amt);
506 blk->size = read_2_bytes (abfd, info_ptr);
508 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
509 info_ptr += blk->size;
513 amt = sizeof (struct dwarf_block);
514 blk = bfd_alloc (abfd, amt);
515 blk->size = read_4_bytes (abfd, info_ptr);
517 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
518 info_ptr += blk->size;
522 attr->u.val = read_2_bytes (abfd, info_ptr);
526 attr->u.val = read_4_bytes (abfd, info_ptr);
530 attr->u.val = read_8_bytes (abfd, info_ptr);
534 attr->u.str = read_string (abfd, info_ptr, &bytes_read);
535 info_ptr += bytes_read;
538 attr->u.str = read_indirect_string (unit, info_ptr, &bytes_read);
539 info_ptr += bytes_read;
542 amt = sizeof (struct dwarf_block);
543 blk = bfd_alloc (abfd, amt);
544 blk->size = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
545 info_ptr += bytes_read;
546 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
547 info_ptr += blk->size;
551 amt = sizeof (struct dwarf_block);
552 blk = bfd_alloc (abfd, amt);
553 blk->size = read_1_byte (abfd, info_ptr);
555 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
556 info_ptr += blk->size;
560 attr->u.val = read_1_byte (abfd, info_ptr);
564 attr->u.val = read_1_byte (abfd, info_ptr);
568 attr->u.sval = read_signed_leb128 (abfd, info_ptr, &bytes_read);
569 info_ptr += bytes_read;
572 attr->u.val = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
573 info_ptr += bytes_read;
576 attr->u.val = read_1_byte (abfd, info_ptr);
580 attr->u.val = read_2_bytes (abfd, info_ptr);
584 attr->u.val = read_4_bytes (abfd, info_ptr);
588 attr->u.val = read_8_bytes (abfd, info_ptr);
591 case DW_FORM_ref_udata:
592 attr->u.val = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
593 info_ptr += bytes_read;
595 case DW_FORM_indirect:
596 form = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
597 info_ptr += bytes_read;
598 info_ptr = read_attribute_value (attr, form, unit, info_ptr);
601 (*_bfd_error_handler) (_("Dwarf Error: Invalid or unhandled FORM value: %u."),
603 bfd_set_error (bfd_error_bad_value);
608 /* Read an attribute described by an abbreviated attribute. */
611 read_attribute (struct attribute *attr,
612 struct attr_abbrev *abbrev,
613 struct comp_unit *unit,
616 attr->name = abbrev->name;
617 info_ptr = read_attribute_value (attr, abbrev->form, unit, info_ptr);
621 /* Source line information table routines. */
623 #define FILE_ALLOC_CHUNK 5
624 #define DIR_ALLOC_CHUNK 5
628 struct line_info* prev_line;
633 int end_sequence; /* End of (sequential) code sequence. */
644 struct line_info_table
647 unsigned int num_files;
648 unsigned int num_dirs;
651 struct fileinfo* files;
652 struct line_info* last_line; /* largest VMA */
653 struct line_info* lcl_head; /* local head; used in 'add_line_info' */
658 struct funcinfo *prev_func;
664 /* Adds a new entry to the line_info list in the line_info_table, ensuring
665 that the list is sorted. Note that the line_info list is sorted from
666 highest to lowest VMA (with possible duplicates); that is,
667 line_info->prev_line always accesses an equal or smaller VMA. */
670 add_line_info (struct line_info_table *table,
677 bfd_size_type amt = sizeof (struct line_info);
678 struct line_info* info = bfd_alloc (table->abfd, amt);
680 /* Find the correct location for 'info'. Normally we will receive
681 new line_info data 1) in order and 2) with increasing VMAs.
682 However some compilers break the rules (cf. decode_line_info) and
683 so we include some heuristics for quickly finding the correct
684 location for 'info'. In particular, these heuristics optimize for
685 the common case in which the VMA sequence that we receive is a
686 list of locally sorted VMAs such as
687 p...z a...j (where a < j < p < z)
689 Note: table->lcl_head is used to head an *actual* or *possible*
690 sequence within the list (such as a...j) that is not directly
691 headed by table->last_line
693 Note: we may receive duplicate entries from 'decode_line_info'. */
696 if (!table->last_line
697 || address >= table->last_line->address)
699 /* Normal case: add 'info' to the beginning of the list */
700 info->prev_line = table->last_line;
701 table->last_line = info;
703 /* lcl_head: initialize to head a *possible* sequence at the end. */
704 if (!table->lcl_head)
705 table->lcl_head = info;
708 else if (!table->lcl_head->prev_line
709 && table->lcl_head->address > address)
711 /* Abnormal but easy: lcl_head is 1) at the *end* of the line
712 list and 2) the head of 'info'. */
713 info->prev_line = NULL;
714 table->lcl_head->prev_line = info;
717 else if (table->lcl_head->prev_line
718 && table->lcl_head->address > address
719 && address >= table->lcl_head->prev_line->address)
721 /* Abnormal but easy: lcl_head is 1) in the *middle* of the line
722 list and 2) the head of 'info'. */
723 info->prev_line = table->lcl_head->prev_line;
724 table->lcl_head->prev_line = info;
729 /* Abnormal and hard: Neither 'last_line' nor 'lcl_head' are valid
730 heads for 'info'. Reset 'lcl_head' and repeat. */
731 struct line_info* li2 = table->last_line; /* always non-NULL */
732 struct line_info* li1 = li2->prev_line;
736 if (li2->address > address && address >= li1->address)
739 li2 = li1; /* always non-NULL */
740 li1 = li1->prev_line;
742 table->lcl_head = li2;
745 /* Set member data of 'info'. */
746 info->address = address;
748 info->column = column;
749 info->end_sequence = end_sequence;
751 if (filename && filename[0])
753 info->filename = bfd_alloc (table->abfd, strlen (filename) + 1);
755 strcpy (info->filename, filename);
758 info->filename = NULL;
761 /* Extract a fully qualified filename from a line info table.
762 The returned string has been malloc'ed and it is the caller's
763 responsibility to free it. */
766 concat_filename (struct line_info_table *table, unsigned int file)
770 if (file - 1 >= table->num_files)
772 (*_bfd_error_handler)
773 (_("Dwarf Error: mangled line number section (bad file number)."));
774 return strdup ("<unknown>");
777 filename = table->files[file - 1].name;
779 if (! IS_ABSOLUTE_PATH (filename))
781 char *dirname = (table->files[file - 1].dir
782 ? table->dirs[table->files[file - 1].dir - 1]
785 /* Not all tools set DW_AT_comp_dir, so dirname may be unknown.
786 The best we can do is return the filename part. */
789 unsigned int len = strlen (dirname) + strlen (filename) + 2;
792 name = bfd_malloc (len);
794 sprintf (name, "%s/%s", dirname, filename);
799 return strdup (filename);
803 arange_add (struct comp_unit *unit, bfd_vma low_pc, bfd_vma high_pc)
805 struct arange *arange;
807 /* First see if we can cheaply extend an existing range. */
808 arange = &unit->arange;
812 if (low_pc == arange->high)
814 arange->high = high_pc;
817 if (high_pc == arange->low)
819 arange->low = low_pc;
822 arange = arange->next;
826 if (unit->arange.high == 0)
828 /* This is the first address range: store it in unit->arange. */
829 unit->arange.next = 0;
830 unit->arange.low = low_pc;
831 unit->arange.high = high_pc;
835 /* Need to allocate a new arange and insert it into the arange list. */
836 arange = bfd_zalloc (unit->abfd, sizeof (*arange));
837 arange->low = low_pc;
838 arange->high = high_pc;
840 arange->next = unit->arange.next;
841 unit->arange.next = arange;
844 /* Decode the line number information for UNIT. */
846 static struct line_info_table*
847 decode_line_info (struct comp_unit *unit, struct dwarf2_debug *stash)
849 bfd *abfd = unit->abfd;
850 struct line_info_table* table;
854 unsigned int i, bytes_read, offset_size;
855 char *cur_file, *cur_dir;
856 unsigned char op_code, extended_op, adj_opcode;
859 if (! stash->dwarf_line_buffer)
863 msec = bfd_get_section_by_name (abfd, ".debug_line");
866 (*_bfd_error_handler) (_("Dwarf Error: Can't find .debug_line section."));
867 bfd_set_error (bfd_error_bad_value);
871 stash->dwarf_line_size = msec->size;
872 stash->dwarf_line_buffer
873 = bfd_simple_get_relocated_section_contents (abfd, msec, NULL,
875 if (! stash->dwarf_line_buffer)
879 /* It is possible to get a bad value for the line_offset. Validate
880 it here so that we won't get a segfault below. */
881 if (unit->line_offset >= stash->dwarf_line_size)
883 (*_bfd_error_handler) (_("Dwarf Error: Line offset (%lu) greater than or equal to .debug_line size (%lu)."),
884 unit->line_offset, stash->dwarf_line_size);
885 bfd_set_error (bfd_error_bad_value);
889 amt = sizeof (struct line_info_table);
890 table = bfd_alloc (abfd, amt);
892 table->comp_dir = unit->comp_dir;
894 table->num_files = 0;
901 table->last_line = NULL;
902 table->lcl_head = NULL;
904 line_ptr = stash->dwarf_line_buffer + unit->line_offset;
906 /* Read in the prologue. */
907 lh.total_length = read_4_bytes (abfd, line_ptr);
910 if (lh.total_length == 0xffffffff)
912 lh.total_length = read_8_bytes (abfd, line_ptr);
916 else if (lh.total_length == 0 && unit->addr_size == 8)
918 /* Handle (non-standard) 64-bit DWARF2 formats. */
919 lh.total_length = read_4_bytes (abfd, line_ptr);
923 line_end = line_ptr + lh.total_length;
924 lh.version = read_2_bytes (abfd, line_ptr);
926 if (offset_size == 4)
927 lh.prologue_length = read_4_bytes (abfd, line_ptr);
929 lh.prologue_length = read_8_bytes (abfd, line_ptr);
930 line_ptr += offset_size;
931 lh.minimum_instruction_length = read_1_byte (abfd, line_ptr);
933 lh.default_is_stmt = read_1_byte (abfd, line_ptr);
935 lh.line_base = read_1_signed_byte (abfd, line_ptr);
937 lh.line_range = read_1_byte (abfd, line_ptr);
939 lh.opcode_base = read_1_byte (abfd, line_ptr);
941 amt = lh.opcode_base * sizeof (unsigned char);
942 lh.standard_opcode_lengths = bfd_alloc (abfd, amt);
944 lh.standard_opcode_lengths[0] = 1;
946 for (i = 1; i < lh.opcode_base; ++i)
948 lh.standard_opcode_lengths[i] = read_1_byte (abfd, line_ptr);
952 /* Read directory table. */
953 while ((cur_dir = read_string (abfd, line_ptr, &bytes_read)) != NULL)
955 line_ptr += bytes_read;
957 if ((table->num_dirs % DIR_ALLOC_CHUNK) == 0)
959 amt = table->num_dirs + DIR_ALLOC_CHUNK;
960 amt *= sizeof (char *);
961 table->dirs = bfd_realloc (table->dirs, amt);
966 table->dirs[table->num_dirs++] = cur_dir;
969 line_ptr += bytes_read;
971 /* Read file name table. */
972 while ((cur_file = read_string (abfd, line_ptr, &bytes_read)) != NULL)
974 line_ptr += bytes_read;
976 if ((table->num_files % FILE_ALLOC_CHUNK) == 0)
978 amt = table->num_files + FILE_ALLOC_CHUNK;
979 amt *= sizeof (struct fileinfo);
980 table->files = bfd_realloc (table->files, amt);
985 table->files[table->num_files].name = cur_file;
986 table->files[table->num_files].dir =
987 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
988 line_ptr += bytes_read;
989 table->files[table->num_files].time =
990 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
991 line_ptr += bytes_read;
992 table->files[table->num_files].size =
993 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
994 line_ptr += bytes_read;
998 line_ptr += bytes_read;
1000 /* Read the statement sequences until there's nothing left. */
1001 while (line_ptr < line_end)
1003 /* State machine registers. */
1004 bfd_vma address = 0;
1005 char * filename = table->num_files ? concat_filename (table, 1) : NULL;
1006 unsigned int line = 1;
1007 unsigned int column = 0;
1008 int is_stmt = lh.default_is_stmt;
1009 int basic_block = 0;
1010 int end_sequence = 0;
1012 compilers generate address sequences that are wildly out of
1013 order using DW_LNE_set_address (e.g. Intel C++ 6.0 compiler
1014 for ia64-Linux). Thus, to determine the low and high
1015 address, we must compare on every DW_LNS_copy, etc. */
1017 bfd_vma high_pc = 0;
1019 /* Decode the table. */
1020 while (! end_sequence)
1022 op_code = read_1_byte (abfd, line_ptr);
1025 if (op_code >= lh.opcode_base)
1027 /* Special operand. */
1028 adj_opcode = op_code - lh.opcode_base;
1029 address += (adj_opcode / lh.line_range)
1030 * lh.minimum_instruction_length;
1031 line += lh.line_base + (adj_opcode % lh.line_range);
1032 /* Append row to matrix using current values. */
1033 add_line_info (table, address, filename, line, column, 0);
1035 if (low_pc == 0 || address < low_pc)
1037 if (address > high_pc)
1040 else switch (op_code)
1042 case DW_LNS_extended_op:
1043 /* Ignore length. */
1045 extended_op = read_1_byte (abfd, line_ptr);
1048 switch (extended_op)
1050 case DW_LNE_end_sequence:
1052 add_line_info (table, address, filename, line, column,
1054 if (low_pc == 0 || address < low_pc)
1056 if (address > high_pc)
1058 arange_add (unit, low_pc, high_pc);
1060 case DW_LNE_set_address:
1061 address = read_address (unit, line_ptr);
1062 line_ptr += unit->addr_size;
1064 case DW_LNE_define_file:
1065 cur_file = read_string (abfd, line_ptr, &bytes_read);
1066 line_ptr += bytes_read;
1067 if ((table->num_files % FILE_ALLOC_CHUNK) == 0)
1069 amt = table->num_files + FILE_ALLOC_CHUNK;
1070 amt *= sizeof (struct fileinfo);
1071 table->files = bfd_realloc (table->files, amt);
1075 table->files[table->num_files].name = cur_file;
1076 table->files[table->num_files].dir =
1077 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
1078 line_ptr += bytes_read;
1079 table->files[table->num_files].time =
1080 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
1081 line_ptr += bytes_read;
1082 table->files[table->num_files].size =
1083 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
1084 line_ptr += bytes_read;
1088 (*_bfd_error_handler) (_("Dwarf Error: mangled line number section."));
1089 bfd_set_error (bfd_error_bad_value);
1094 add_line_info (table, address, filename, line, column, 0);
1096 if (low_pc == 0 || address < low_pc)
1098 if (address > high_pc)
1101 case DW_LNS_advance_pc:
1102 address += lh.minimum_instruction_length
1103 * read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
1104 line_ptr += bytes_read;
1106 case DW_LNS_advance_line:
1107 line += read_signed_leb128 (abfd, line_ptr, &bytes_read);
1108 line_ptr += bytes_read;
1110 case DW_LNS_set_file:
1114 /* The file and directory tables are 0
1115 based, the references are 1 based. */
1116 file = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
1117 line_ptr += bytes_read;
1120 filename = concat_filename (table, file);
1123 case DW_LNS_set_column:
1124 column = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
1125 line_ptr += bytes_read;
1127 case DW_LNS_negate_stmt:
1128 is_stmt = (!is_stmt);
1130 case DW_LNS_set_basic_block:
1133 case DW_LNS_const_add_pc:
1134 address += lh.minimum_instruction_length
1135 * ((255 - lh.opcode_base) / lh.line_range);
1137 case DW_LNS_fixed_advance_pc:
1138 address += read_2_bytes (abfd, line_ptr);
1145 /* Unknown standard opcode, ignore it. */
1146 for (i = 0; i < lh.standard_opcode_lengths[op_code]; i++)
1148 (void) read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
1149 line_ptr += bytes_read;
1162 /* If ADDR is within TABLE set the output parameters and return TRUE,
1163 otherwise return FALSE. The output parameters, FILENAME_PTR and
1164 LINENUMBER_PTR, are pointers to the objects to be filled in. */
1167 lookup_address_in_line_info_table (struct line_info_table *table,
1169 struct funcinfo *function,
1170 const char **filename_ptr,
1171 unsigned int *linenumber_ptr)
1173 /* Note: table->last_line should be a descendingly sorted list. */
1174 struct line_info* next_line = table->last_line;
1175 struct line_info* each_line = NULL;
1176 *filename_ptr = NULL;
1181 each_line = next_line->prev_line;
1183 /* Check for large addresses */
1184 if (addr > next_line->address)
1185 each_line = NULL; /* ensure we skip over the normal case */
1187 /* Normal case: search the list; save */
1188 while (each_line && next_line)
1190 /* If we have an address match, save this info. This allows us
1191 to return as good as results as possible for strange debugging
1193 bfd_boolean addr_match = FALSE;
1194 if (each_line->address <= addr && addr <= next_line->address)
1198 /* If this line appears to span functions, and addr is in the
1199 later function, return the first line of that function instead
1200 of the last line of the earlier one. This check is for GCC
1201 2.95, which emits the first line number for a function late. */
1202 if (function != NULL
1203 && each_line->address < function->low
1204 && next_line->address > function->low)
1206 *filename_ptr = next_line->filename;
1207 *linenumber_ptr = next_line->line;
1211 *filename_ptr = each_line->filename;
1212 *linenumber_ptr = each_line->line;
1216 if (addr_match && !each_line->end_sequence)
1217 return TRUE; /* we have definitely found what we want */
1219 next_line = each_line;
1220 each_line = each_line->prev_line;
1223 /* At this point each_line is NULL but next_line is not. If we found
1224 a candidate end-of-sequence point in the loop above, we can return
1225 that (compatibility with a bug in the Intel compiler); otherwise,
1226 assuming that we found the containing function for this address in
1227 this compilation unit, return the first line we have a number for
1228 (compatibility with GCC 2.95). */
1229 if (*filename_ptr == NULL && function != NULL)
1231 *filename_ptr = next_line->filename;
1232 *linenumber_ptr = next_line->line;
1239 /* Function table functions. */
1241 /* If ADDR is within TABLE, set FUNCTIONNAME_PTR, and return TRUE. */
1244 lookup_address_in_function_table (struct funcinfo *table,
1246 struct funcinfo **function_ptr,
1247 const char **functionname_ptr)
1249 struct funcinfo* each_func;
1251 for (each_func = table;
1253 each_func = each_func->prev_func)
1255 if (addr >= each_func->low && addr < each_func->high)
1257 *functionname_ptr = each_func->name;
1258 *function_ptr = each_func;
1267 find_abstract_instance_name (struct comp_unit *unit, bfd_uint64_t die_ref)
1269 bfd *abfd = unit->abfd;
1271 unsigned int abbrev_number, bytes_read, i;
1272 struct abbrev_info *abbrev;
1273 struct attribute attr;
1276 info_ptr = unit->info_ptr_unit + die_ref;
1277 abbrev_number = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
1278 info_ptr += bytes_read;
1282 abbrev = lookup_abbrev (abbrev_number, unit->abbrevs);
1285 (*_bfd_error_handler) (_("Dwarf Error: Could not find abbrev number %u."),
1287 bfd_set_error (bfd_error_bad_value);
1291 for (i = 0; i < abbrev->num_attrs && !name; ++i)
1293 info_ptr = read_attribute (&attr, &abbrev->attrs[i], unit, info_ptr);
1299 case DW_AT_specification:
1300 name = find_abstract_instance_name (unit, attr.u.val);
1311 /* DWARF2 Compilation unit functions. */
1313 /* Scan over each die in a comp. unit looking for functions to add
1314 to the function table. */
1317 scan_unit_for_functions (struct comp_unit *unit)
1319 bfd *abfd = unit->abfd;
1320 bfd_byte *info_ptr = unit->first_child_die_ptr;
1321 int nesting_level = 1;
1323 while (nesting_level)
1325 unsigned int abbrev_number, bytes_read, i;
1326 struct abbrev_info *abbrev;
1327 struct attribute attr;
1328 struct funcinfo *func;
1330 abbrev_number = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
1331 info_ptr += bytes_read;
1333 if (! abbrev_number)
1339 abbrev = lookup_abbrev (abbrev_number,unit->abbrevs);
1342 (*_bfd_error_handler) (_("Dwarf Error: Could not find abbrev number %u."),
1344 bfd_set_error (bfd_error_bad_value);
1348 if (abbrev->tag == DW_TAG_subprogram
1349 || abbrev->tag == DW_TAG_inlined_subroutine)
1351 bfd_size_type amt = sizeof (struct funcinfo);
1352 func = bfd_zalloc (abfd, amt);
1353 func->prev_func = unit->function_table;
1354 unit->function_table = func;
1359 for (i = 0; i < abbrev->num_attrs; ++i)
1361 info_ptr = read_attribute (&attr, &abbrev->attrs[i], unit, info_ptr);
1367 case DW_AT_abstract_origin:
1368 func->name = find_abstract_instance_name (unit, attr.u.val);
1372 /* Prefer DW_AT_MIPS_linkage_name over DW_AT_name. */
1373 if (func->name == NULL)
1374 func->name = attr.u.str;
1377 case DW_AT_MIPS_linkage_name:
1378 func->name = attr.u.str;
1382 func->low = attr.u.val;
1386 func->high = attr.u.val;
1395 if (abbrev->has_children)
1402 /* Parse a DWARF2 compilation unit starting at INFO_PTR. This
1403 includes the compilation unit header that proceeds the DIE's, but
1404 does not include the length field that precedes each compilation
1405 unit header. END_PTR points one past the end of this comp unit.
1406 OFFSET_SIZE is the size of DWARF2 offsets (either 4 or 8 bytes).
1408 This routine does not read the whole compilation unit; only enough
1409 to get to the line number information for the compilation unit. */
1411 static struct comp_unit *
1412 parse_comp_unit (bfd *abfd,
1413 struct dwarf2_debug *stash,
1414 bfd_vma unit_length,
1415 bfd_byte *info_ptr_unit,
1416 unsigned int offset_size)
1418 struct comp_unit* unit;
1419 unsigned int version;
1420 bfd_uint64_t abbrev_offset = 0;
1421 unsigned int addr_size;
1422 struct abbrev_info** abbrevs;
1423 unsigned int abbrev_number, bytes_read, i;
1424 struct abbrev_info *abbrev;
1425 struct attribute attr;
1426 bfd_byte *info_ptr = stash->info_ptr;
1427 bfd_byte *end_ptr = info_ptr + unit_length;
1430 version = read_2_bytes (abfd, info_ptr);
1432 BFD_ASSERT (offset_size == 4 || offset_size == 8);
1433 if (offset_size == 4)
1434 abbrev_offset = read_4_bytes (abfd, info_ptr);
1436 abbrev_offset = read_8_bytes (abfd, info_ptr);
1437 info_ptr += offset_size;
1438 addr_size = read_1_byte (abfd, info_ptr);
1443 (*_bfd_error_handler) (_("Dwarf Error: found dwarf version '%u', this reader only handles version 2 information."), version);
1444 bfd_set_error (bfd_error_bad_value);
1448 if (addr_size > sizeof (bfd_vma))
1450 (*_bfd_error_handler) (_("Dwarf Error: found address size '%u', this reader can not handle sizes greater than '%u'."),
1452 (unsigned int) sizeof (bfd_vma));
1453 bfd_set_error (bfd_error_bad_value);
1457 if (addr_size != 2 && addr_size != 4 && addr_size != 8)
1459 (*_bfd_error_handler) ("Dwarf Error: found address size '%u', this reader can only handle address sizes '2', '4' and '8'.", addr_size);
1460 bfd_set_error (bfd_error_bad_value);
1464 /* Read the abbrevs for this compilation unit into a table. */
1465 abbrevs = read_abbrevs (abfd, abbrev_offset, stash);
1469 abbrev_number = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
1470 info_ptr += bytes_read;
1471 if (! abbrev_number)
1473 (*_bfd_error_handler) (_("Dwarf Error: Bad abbrev number: %u."),
1475 bfd_set_error (bfd_error_bad_value);
1479 abbrev = lookup_abbrev (abbrev_number, abbrevs);
1482 (*_bfd_error_handler) (_("Dwarf Error: Could not find abbrev number %u."),
1484 bfd_set_error (bfd_error_bad_value);
1488 amt = sizeof (struct comp_unit);
1489 unit = bfd_zalloc (abfd, amt);
1491 unit->addr_size = addr_size;
1492 unit->offset_size = offset_size;
1493 unit->abbrevs = abbrevs;
1494 unit->end_ptr = end_ptr;
1495 unit->stash = stash;
1496 unit->info_ptr_unit = info_ptr_unit;
1498 for (i = 0; i < abbrev->num_attrs; ++i)
1500 info_ptr = read_attribute (&attr, &abbrev->attrs[i], unit, info_ptr);
1502 /* Store the data if it is of an attribute we want to keep in a
1503 partial symbol table. */
1506 case DW_AT_stmt_list:
1508 unit->line_offset = attr.u.val;
1512 unit->name = attr.u.str;
1516 unit->arange.low = attr.u.val;
1520 unit->arange.high = attr.u.val;
1523 case DW_AT_comp_dir:
1525 char *comp_dir = attr.u.str;
1528 /* Irix 6.2 native cc prepends <machine>.: to the compilation
1529 directory, get rid of it. */
1530 char *cp = strchr (comp_dir, ':');
1532 if (cp && cp != comp_dir && cp[-1] == '.' && cp[1] == '/')
1535 unit->comp_dir = comp_dir;
1544 unit->first_child_die_ptr = info_ptr;
1548 /* Return TRUE if UNIT contains the address given by ADDR. */
1551 comp_unit_contains_address (struct comp_unit *unit, bfd_vma addr)
1553 struct arange *arange;
1558 arange = &unit->arange;
1561 if (addr >= arange->low && addr < arange->high)
1563 arange = arange->next;
1570 /* If UNIT contains ADDR, set the output parameters to the values for
1571 the line containing ADDR. The output parameters, FILENAME_PTR,
1572 FUNCTIONNAME_PTR, and LINENUMBER_PTR, are pointers to the objects
1575 Return TRUE if UNIT contains ADDR, and no errors were encountered;
1579 comp_unit_find_nearest_line (struct comp_unit *unit,
1581 const char **filename_ptr,
1582 const char **functionname_ptr,
1583 unsigned int *linenumber_ptr,
1584 struct dwarf2_debug *stash)
1588 struct funcinfo *function;
1593 if (! unit->line_table)
1595 if (! unit->stmtlist)
1601 unit->line_table = decode_line_info (unit, stash);
1603 if (! unit->line_table)
1609 if (unit->first_child_die_ptr < unit->end_ptr
1610 && ! scan_unit_for_functions (unit))
1618 func_p = lookup_address_in_function_table (unit->function_table, addr,
1619 &function, functionname_ptr);
1620 line_p = lookup_address_in_line_info_table (unit->line_table, addr,
1621 function, filename_ptr,
1623 return line_p || func_p;
1626 /* Locate a section in a BFD containing debugging info. The search starts
1627 from the section after AFTER_SEC, or from the first section in the BFD if
1628 AFTER_SEC is NULL. The search works by examining the names of the
1629 sections. There are two permissiable names. The first is .debug_info.
1630 This is the standard DWARF2 name. The second is a prefix .gnu.linkonce.wi.
1631 This is a variation on the .debug_info section which has a checksum
1632 describing the contents appended onto the name. This allows the linker to
1633 identify and discard duplicate debugging sections for different
1634 compilation units. */
1635 #define DWARF2_DEBUG_INFO ".debug_info"
1636 #define GNU_LINKONCE_INFO ".gnu.linkonce.wi."
1639 find_debug_info (bfd *abfd, asection *after_sec)
1644 msec = after_sec->next;
1646 msec = abfd->sections;
1650 if (strcmp (msec->name, DWARF2_DEBUG_INFO) == 0)
1653 if (strncmp (msec->name, GNU_LINKONCE_INFO, strlen (GNU_LINKONCE_INFO)) == 0)
1662 /* The DWARF2 version of find_nearest_line. Return TRUE if the line
1663 is found without error. ADDR_SIZE is the number of bytes in the
1664 initial .debug_info length field and in the abbreviation offset.
1665 You may use zero to indicate that the default value should be
1669 _bfd_dwarf2_find_nearest_line (bfd *abfd,
1673 const char **filename_ptr,
1674 const char **functionname_ptr,
1675 unsigned int *linenumber_ptr,
1676 unsigned int addr_size,
1679 /* Read each compilation unit from the section .debug_info, and check
1680 to see if it contains the address we are searching for. If yes,
1681 lookup the address, and return the line number info. If no, go
1682 on to the next compilation unit.
1684 We keep a list of all the previously read compilation units, and
1685 a pointer to the next un-read compilation unit. Check the
1686 previously read units before reading more. */
1687 struct dwarf2_debug *stash;
1689 /* What address are we looking for? */
1692 struct comp_unit* each;
1696 if (section->output_section)
1697 addr += section->output_section->vma + section->output_offset;
1699 addr += section->vma;
1700 *filename_ptr = NULL;
1701 *functionname_ptr = NULL;
1702 *linenumber_ptr = 0;
1704 /* The DWARF2 spec says that the initial length field, and the
1705 offset of the abbreviation table, should both be 4-byte values.
1706 However, some compilers do things differently. */
1709 BFD_ASSERT (addr_size == 4 || addr_size == 8);
1713 bfd_size_type total_size;
1715 bfd_size_type amt = sizeof (struct dwarf2_debug);
1717 stash = bfd_zalloc (abfd, amt);
1723 msec = find_debug_info (abfd, NULL);
1725 /* No dwarf2 info. Note that at this point the stash
1726 has been allocated, but contains zeros, this lets
1727 future calls to this function fail quicker. */
1730 /* There can be more than one DWARF2 info section in a BFD these days.
1731 Read them all in and produce one large stash. We do this in two
1732 passes - in the first pass we just accumulate the section sizes.
1733 In the second pass we read in the section's contents. The allows
1734 us to avoid reallocing the data as we add sections to the stash. */
1735 for (total_size = 0; msec; msec = find_debug_info (abfd, msec))
1736 total_size += msec->size;
1738 stash->info_ptr = bfd_alloc (abfd, total_size);
1739 if (stash->info_ptr == NULL)
1742 stash->info_ptr_end = stash->info_ptr;
1744 for (msec = find_debug_info (abfd, NULL);
1746 msec = find_debug_info (abfd, msec))
1749 bfd_size_type start;
1755 start = stash->info_ptr_end - stash->info_ptr;
1757 if ((bfd_simple_get_relocated_section_contents
1758 (abfd, msec, stash->info_ptr + start, symbols)) == NULL)
1761 stash->info_ptr_end = stash->info_ptr + start + size;
1764 BFD_ASSERT (stash->info_ptr_end == stash->info_ptr + total_size);
1766 stash->sec = find_debug_info (abfd, NULL);
1767 stash->sec_info_ptr = stash->info_ptr;
1768 stash->syms = symbols;
1771 /* A null info_ptr indicates that there is no dwarf2 info
1772 (or that an error occured while setting up the stash). */
1773 if (! stash->info_ptr)
1776 /* Check the previously read comp. units first. */
1777 for (each = stash->all_comp_units; each; each = each->next_unit)
1778 if (comp_unit_contains_address (each, addr))
1779 return comp_unit_find_nearest_line (each, addr, filename_ptr,
1780 functionname_ptr, linenumber_ptr,
1783 /* Read each remaining comp. units checking each as they are read. */
1784 while (stash->info_ptr < stash->info_ptr_end)
1788 unsigned int offset_size = addr_size;
1789 bfd_byte *info_ptr_unit = stash->info_ptr;
1791 length = read_4_bytes (abfd, stash->info_ptr);
1792 /* A 0xffffff length is the DWARF3 way of indicating we use
1793 64-bit offsets, instead of 32-bit offsets. */
1794 if (length == 0xffffffff)
1797 length = read_8_bytes (abfd, stash->info_ptr + 4);
1798 stash->info_ptr += 12;
1800 /* A zero length is the IRIX way of indicating 64-bit offsets,
1801 mostly because the 64-bit length will generally fit in 32
1802 bits, and the endianness helps. */
1803 else if (length == 0)
1806 length = read_4_bytes (abfd, stash->info_ptr + 4);
1807 stash->info_ptr += 8;
1809 /* In the absence of the hints above, we assume addr_size-sized
1810 offsets, for backward-compatibility with pre-DWARF3 64-bit
1812 else if (addr_size == 8)
1814 length = read_8_bytes (abfd, stash->info_ptr);
1815 stash->info_ptr += 8;
1818 stash->info_ptr += 4;
1822 each = parse_comp_unit (abfd, stash, length, info_ptr_unit,
1824 stash->info_ptr += length;
1826 if ((bfd_vma) (stash->info_ptr - stash->sec_info_ptr)
1827 == stash->sec->size)
1829 stash->sec = find_debug_info (abfd, stash->sec);
1830 stash->sec_info_ptr = stash->info_ptr;
1835 each->next_unit = stash->all_comp_units;
1836 stash->all_comp_units = each;
1838 /* DW_AT_low_pc and DW_AT_high_pc are optional for
1839 compilation units. If we don't have them (i.e.,
1840 unit->high == 0), we need to consult the line info
1841 table to see if a compilation unit contains the given
1843 if (each->arange.high > 0)
1845 if (comp_unit_contains_address (each, addr))
1846 return comp_unit_find_nearest_line (each, addr,
1854 found = comp_unit_find_nearest_line (each, addr,