2 Copyright 1994, 95, 96, 97, 98, 99, 2000 Free Software Foundation, Inc.
4 Adapted from gdb/dwarf2read.c by Gavin Koch of Cygnus Solutions
7 From the dwarf2read.c header:
8 Adapted by Gary Funck (gary@intrepid.com), Intrepid Technology,
9 Inc. with support from Florida State University (under contract
10 with the Ada Joint Program Office), and Silicon Graphics, Inc.
11 Initial contribution by Brent Benson, Harris Computer Systems, Inc.,
12 based on Fred Fish's (Cygnus Support) implementation of DWARF 1
13 support in dwarfread.c
15 This file is part of BFD.
17 This program is free software; you can redistribute it and/or modify
18 it under the terms of the GNU General Public License as published by
19 the Free Software Foundation; either version 2 of the License, or (at
20 your option) any later version.
22 This program is distributed in the hope that it will be useful, but
23 WITHOUT ANY WARRANTY; without even the implied warranty of
24 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
25 General Public License for more details.
27 You should have received a copy of the GNU General Public License
28 along with this program; if not, write to the Free Software
29 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
33 #include "libiberty.h"
36 #include "elf/dwarf2.h"
38 /* The data in the .debug_line statement prologue looks like this. */
41 unsigned int total_length;
42 unsigned short version;
43 unsigned int prologue_length;
44 unsigned char minimum_instruction_length;
45 unsigned char default_is_stmt;
47 unsigned char line_range;
48 unsigned char opcode_base;
49 unsigned char *standard_opcode_lengths;
52 /* Attributes have a name and a value */
55 enum dwarf_attribute name;
60 struct dwarf_block *blk;
68 /* Get at parts of an attribute structure */
70 #define DW_STRING(attr) ((attr)->u.str)
71 #define DW_UNSND(attr) ((attr)->u.unsnd)
72 #define DW_BLOCK(attr) ((attr)->u.blk)
73 #define DW_SND(attr) ((attr)->u.snd)
74 #define DW_ADDR(attr) ((attr)->u.addr)
76 /* Blocks are a bunch of untyped bytes. */
86 /* A list of all previously read comp_units. */
87 struct comp_unit* all_comp_units;
89 /* The next unread compilation unit within the .debug_info section.
90 Zero indicates that the .debug_info section has not been loaded
94 /* Pointer to the end of the .debug_info section memory buffer. */
97 /* Pointer to the .debug_abbrev section loaded into memory. */
98 char* dwarf_abbrev_buffer;
100 /* Length of the loaded .debug_abbrev section. */
101 unsigned long dwarf_abbrev_size;
103 /* Buffer for decode_line_info. */
104 char *dwarf_line_buffer;
106 /* Length of the loaded .debug_line section. */
107 unsigned long dwarf_line_size;
117 /* A minimal decoding of DWARF2 compilation units. We only decode
118 what's needed to get to the line number information. */
122 /* Chain the previously read compilation units. */
123 struct comp_unit* next_unit;
125 /* Keep the bdf convenient (for memory allocation). */
128 /* The lowest and higest addresses contained in this compilation
129 unit as specified in the compilation unit header. */
130 struct arange arange;
132 /* The DW_AT_name attribute (for error messages). */
135 /* The abbrev hash table. */
136 struct abbrev_info** abbrevs;
138 /* Note that an error was found by comp_unit_find_nearest_line. */
141 /* The DW_AT_comp_dir attribute */
144 /* True if there is a line number table associated with this comp. unit. */
147 /* The offset into .debug_line of the line number table. */
148 unsigned long line_offset;
150 /* Pointer to the first child die for the comp unit. */
151 char *first_child_die_ptr;
153 /* The end of the comp unit. */
156 /* The decoded line number, NULL if not yet decoded. */
157 struct line_info_table* line_table;
159 /* A list of the functions found in this comp. unit. */
160 struct funcinfo* function_table;
162 /* Address size for this unit - from unit header */
163 unsigned char addr_size;
169 The following function up to the END VERBATIM mark are
170 copied directly from dwarf2read.c. */
172 /* read dwarf information from a buffer */
175 read_1_byte (abfd, buf)
176 bfd *abfd ATTRIBUTE_UNUSED;
179 return bfd_get_8 (abfd, (bfd_byte *) buf);
183 read_1_signed_byte (abfd, buf)
184 bfd *abfd ATTRIBUTE_UNUSED;
187 return bfd_get_signed_8 (abfd, (bfd_byte *) buf);
191 read_2_bytes (abfd, buf)
195 return bfd_get_16 (abfd, (bfd_byte *) buf);
200 /* This is not used. */
203 read_2_signed_bytes (abfd, buf)
207 return bfd_get_signed_16 (abfd, (bfd_byte *) buf);
213 read_4_bytes (abfd, buf)
217 return bfd_get_32 (abfd, (bfd_byte *) buf);
222 /* This is not used. */
225 read_4_signed_bytes (abfd, buf)
229 return bfd_get_signed_32 (abfd, (bfd_byte *) buf);
235 read_8_bytes (abfd, buf)
239 return bfd_get_64 (abfd, (bfd_byte *) buf);
243 read_n_bytes (abfd, buf, size)
244 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 (abfd, buf, bytes_read_ptr)
256 bfd *abfd ATTRIBUTE_UNUSED;
258 unsigned int *bytes_read_ptr;
260 /* If the size of a host char is 8 bits, we can return a pointer
261 to the string, otherwise we have to copy the string to a buffer
262 allocated on the temporary obstack. */
268 *bytes_read_ptr = strlen (buf) + 1;
273 read_unsigned_leb128 (abfd, buf, bytes_read_ptr)
274 bfd *abfd ATTRIBUTE_UNUSED;
276 unsigned int *bytes_read_ptr;
279 unsigned int num_read;
289 byte = bfd_get_8 (abfd, (bfd_byte *) buf);
292 result |= ((byte & 0x7f) << shift);
297 * bytes_read_ptr = num_read;
303 read_signed_leb128 (abfd, buf, bytes_read_ptr)
304 bfd *abfd ATTRIBUTE_UNUSED;
306 unsigned int * bytes_read_ptr;
319 byte = bfd_get_8 (abfd, (bfd_byte *) buf);
322 result |= ((byte & 0x7f) << shift);
327 if ((shift < 32) && (byte & 0x40))
328 result |= -(1 << shift);
330 * bytes_read_ptr = num_read;
338 read_address (unit, buf)
339 struct comp_unit* unit;
342 switch (unit->addr_size)
345 return bfd_get_64 (unit->abfd, (bfd_byte *) buf);
347 return bfd_get_32 (unit->abfd, (bfd_byte *) buf);
349 return bfd_get_16 (unit->abfd, (bfd_byte *) buf);
359 /* This data structure holds the information of an abbrev. */
362 unsigned int number; /* number identifying abbrev */
363 enum dwarf_tag tag; /* dwarf tag */
364 int has_children; /* boolean */
365 unsigned int num_attrs; /* number of attributes */
366 struct attr_abbrev *attrs; /* an array of attribute descriptions */
367 struct abbrev_info *next; /* next in chain */
372 enum dwarf_attribute name;
373 enum dwarf_form form;
376 #ifndef ABBREV_HASH_SIZE
377 #define ABBREV_HASH_SIZE 121
379 #ifndef ATTR_ALLOC_CHUNK
380 #define ATTR_ALLOC_CHUNK 4
383 /* Lookup an abbrev_info structure in the abbrev hash table. */
385 static struct abbrev_info *
386 lookup_abbrev (number,abbrevs)
388 struct abbrev_info **abbrevs;
390 unsigned int hash_number;
391 struct abbrev_info *abbrev;
393 hash_number = number % ABBREV_HASH_SIZE;
394 abbrev = abbrevs[hash_number];
398 if (abbrev->number == number)
401 abbrev = abbrev->next;
406 /* In DWARF version 2, the description of the debugging information is
407 stored in a separate .debug_abbrev section. Before we read any
408 dies from a section we read in all abbreviations and install them
411 static struct abbrev_info**
412 read_abbrevs (abfd, offset)
416 struct abbrev_info **abbrevs;
418 struct abbrev_info *cur_abbrev;
419 unsigned int abbrev_number, bytes_read, abbrev_name;
420 unsigned int abbrev_form, hash_number;
421 struct dwarf2_debug *stash;
423 stash = elf_tdata(abfd)->dwarf2_find_line_info;
425 if (! stash->dwarf_abbrev_buffer)
429 msec = bfd_get_section_by_name (abfd, ".debug_abbrev");
432 (*_bfd_error_handler) (_("Dwarf Error: Can't find .debug_abbrev section."));
433 bfd_set_error (bfd_error_bad_value);
437 stash->dwarf_abbrev_size = msec->_raw_size;
438 stash->dwarf_abbrev_buffer = (char*) bfd_alloc (abfd, stash->dwarf_abbrev_size);
439 if (! stash->dwarf_abbrev_buffer)
442 if (! bfd_get_section_contents (abfd, msec,
443 stash->dwarf_abbrev_buffer, 0,
444 stash->dwarf_abbrev_size))
448 if (offset > stash->dwarf_abbrev_size)
450 (*_bfd_error_handler) (_("Dwarf Error: Abbrev offset (%u) bigger than abbrev size (%u)."),
451 offset, stash->dwarf_abbrev_size );
452 bfd_set_error (bfd_error_bad_value);
456 abbrevs = (struct abbrev_info**) bfd_zalloc (abfd, sizeof(struct abbrev_info*) * ABBREV_HASH_SIZE);
458 abbrev_ptr = stash->dwarf_abbrev_buffer + offset;
459 abbrev_number = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
460 abbrev_ptr += bytes_read;
462 /* loop until we reach an abbrev number of 0 */
463 while (abbrev_number)
465 cur_abbrev = (struct abbrev_info*)bfd_zalloc (abfd, sizeof (struct abbrev_info));
467 /* read in abbrev header */
468 cur_abbrev->number = abbrev_number;
469 cur_abbrev->tag = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
470 abbrev_ptr += bytes_read;
471 cur_abbrev->has_children = read_1_byte (abfd, abbrev_ptr);
474 /* now read in declarations */
475 abbrev_name = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
476 abbrev_ptr += bytes_read;
477 abbrev_form = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
478 abbrev_ptr += bytes_read;
481 if ((cur_abbrev->num_attrs % ATTR_ALLOC_CHUNK) == 0)
483 cur_abbrev->attrs = (struct attr_abbrev *)
484 bfd_realloc (cur_abbrev->attrs,
485 (cur_abbrev->num_attrs + ATTR_ALLOC_CHUNK)
486 * sizeof (struct attr_abbrev));
487 if (! cur_abbrev->attrs)
490 cur_abbrev->attrs[cur_abbrev->num_attrs].name = abbrev_name;
491 cur_abbrev->attrs[cur_abbrev->num_attrs++].form = abbrev_form;
492 abbrev_name = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
493 abbrev_ptr += bytes_read;
494 abbrev_form = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
495 abbrev_ptr += bytes_read;
498 hash_number = abbrev_number % ABBREV_HASH_SIZE;
499 cur_abbrev->next = abbrevs[hash_number];
500 abbrevs[hash_number] = cur_abbrev;
502 /* Get next abbreviation.
503 Under Irix6 the abbreviations for a compilation unit are not
504 always properly terminated with an abbrev number of 0.
505 Exit loop if we encounter an abbreviation which we have
506 already read (which means we are about to read the abbreviations
507 for the next compile unit) or if the end of the abbreviation
509 if ((unsigned int) (abbrev_ptr - stash->dwarf_abbrev_buffer)
510 >= stash->dwarf_abbrev_size)
512 abbrev_number = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
513 abbrev_ptr += bytes_read;
514 if (lookup_abbrev (abbrev_number,abbrevs) != NULL)
521 /* Read an attribute described by an abbreviated attribute. */
524 read_attribute (attr, abbrev, unit, info_ptr)
525 struct attribute *attr;
526 struct attr_abbrev *abbrev;
527 struct comp_unit *unit;
530 bfd *abfd = unit->abfd;
531 unsigned int bytes_read;
532 struct dwarf_block *blk;
534 attr->name = abbrev->name;
535 attr->form = abbrev->form;
536 switch (abbrev->form)
539 case DW_FORM_ref_addr:
540 DW_ADDR (attr) = read_address (unit, info_ptr);
541 info_ptr += unit->addr_size;
544 blk = (struct dwarf_block *) bfd_alloc (abfd, sizeof (struct dwarf_block));
545 blk->size = read_2_bytes (abfd, info_ptr);
547 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
548 info_ptr += blk->size;
549 DW_BLOCK (attr) = blk;
552 blk = (struct dwarf_block *) bfd_alloc (abfd, sizeof (struct dwarf_block));
553 blk->size = read_4_bytes (abfd, info_ptr);
555 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
556 info_ptr += blk->size;
557 DW_BLOCK (attr) = blk;
560 DW_UNSND (attr) = read_2_bytes (abfd, info_ptr);
564 DW_UNSND (attr) = read_4_bytes (abfd, info_ptr);
568 DW_UNSND (attr) = read_8_bytes (abfd, info_ptr);
572 DW_STRING (attr) = read_string (abfd, info_ptr, &bytes_read);
573 info_ptr += bytes_read;
576 blk = (struct dwarf_block *) bfd_alloc (abfd, sizeof (struct dwarf_block));
577 blk->size = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
578 info_ptr += bytes_read;
579 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
580 info_ptr += blk->size;
581 DW_BLOCK (attr) = blk;
584 blk = (struct dwarf_block *) bfd_alloc (abfd, sizeof (struct dwarf_block));
585 blk->size = read_1_byte (abfd, info_ptr);
587 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
588 info_ptr += blk->size;
589 DW_BLOCK (attr) = blk;
592 DW_UNSND (attr) = read_1_byte (abfd, info_ptr);
596 DW_UNSND (attr) = read_1_byte (abfd, info_ptr);
600 DW_SND (attr) = read_signed_leb128 (abfd, info_ptr, &bytes_read);
601 info_ptr += bytes_read;
604 DW_UNSND (attr) = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
605 info_ptr += bytes_read;
608 DW_UNSND (attr) = read_1_byte (abfd, info_ptr);
612 DW_UNSND (attr) = read_2_bytes (abfd, info_ptr);
616 DW_UNSND (attr) = read_4_bytes (abfd, info_ptr);
620 DW_UNSND (attr) = read_8_bytes (abfd, info_ptr);
623 case DW_FORM_ref_udata:
624 DW_UNSND (attr) = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
625 info_ptr += bytes_read;
628 case DW_FORM_indirect:
630 (*_bfd_error_handler) (_("Dwarf Error: Invalid or unhandled FORM value: %d."),
632 bfd_set_error (bfd_error_bad_value);
638 /* Source line information table routines. */
640 #define FILE_ALLOC_CHUNK 5
641 #define DIR_ALLOC_CHUNK 5
644 struct line_info* prev_line;
650 int end_sequence; /* end of (sequential) code sequence */
660 struct line_info_table {
663 unsigned int num_files;
664 unsigned int num_dirs;
668 struct fileinfo* files;
669 struct line_info* last_line;
673 add_line_info (table, address, filename, line, column, end_sequence)
674 struct line_info_table* table;
681 struct line_info* info = (struct line_info*)
682 bfd_alloc (table->abfd, sizeof (struct line_info));
684 info->prev_line = table->last_line;
685 table->last_line = info;
687 info->address = address;
688 info->filename = filename;
690 info->column = column;
691 info->end_sequence = end_sequence;
695 concat_filename (table, file)
696 struct line_info_table* table;
701 if (file - 1 >= table->num_files)
703 (*_bfd_error_handler) (_("Dwarf Error: mangled line number "
704 "section (bad file number)."));
708 filename = table->files[file - 1].name;
709 if (*filename == '/')
714 char* dirname = (table->files[file - 1].dir
715 ? table->dirs[table->files[file - 1].dir - 1]
717 return (char*) concat (dirname, "/", filename, NULL);
722 arange_add (unit, low_pc, high_pc)
723 struct comp_unit *unit;
727 struct arange *arange;
729 /* first see if we can cheaply extend an existing range: */
730 arange = &unit->arange;
733 if (low_pc == arange->high)
735 arange->high = high_pc;
738 if (high_pc == arange->low)
740 arange->low = low_pc;
743 arange = arange->next;
747 if (unit->arange.high == 0)
749 /* this is the first address range: store it in unit->arange: */
750 unit->arange.next = 0;
751 unit->arange.low = low_pc;
752 unit->arange.high = high_pc;
756 /* need to allocate a new arange and insert it into the arange list: */
757 arange = bfd_zalloc (unit->abfd, sizeof (*arange));
758 arange->low = low_pc;
759 arange->high = high_pc;
761 arange->next = unit->arange.next;
762 unit->arange.next = arange;
765 /* Decode the line number information for UNIT. */
767 static struct line_info_table*
768 decode_line_info (unit)
769 struct comp_unit *unit;
771 bfd *abfd = unit->abfd;
773 struct dwarf2_debug *stash;
775 struct line_info_table* table;
780 unsigned int i, bytes_read;
781 char *cur_file, *cur_dir;
782 unsigned char op_code, extended_op, adj_opcode;
784 stash = elf_tdata (abfd)->dwarf2_find_line_info;
786 if (! stash->dwarf_line_buffer)
790 msec = bfd_get_section_by_name (abfd, ".debug_line");
793 (*_bfd_error_handler) (_("Dwarf Error: Can't find .debug_line section."));
794 bfd_set_error (bfd_error_bad_value);
798 stash->dwarf_line_size = msec->_raw_size;
799 stash->dwarf_line_buffer = (char *) bfd_alloc (abfd, stash->dwarf_line_size);
800 if (! stash->dwarf_line_buffer)
803 if (! bfd_get_section_contents (abfd, msec,
804 stash->dwarf_line_buffer, 0,
805 stash->dwarf_line_size))
808 /* FIXME: We ought to apply the relocs against this section before
812 /* Since we are using un-relocated data, it is possible to get a bad value
813 for the line_offset. Validate it here so that we won't get a segfault
815 if (unit->line_offset >= stash->dwarf_line_size)
817 (*_bfd_error_handler) (_("Dwarf Error: Line offset (%u) bigger than line size (%u)."),
818 unit->line_offset, stash->dwarf_line_size);
819 bfd_set_error (bfd_error_bad_value);
823 table = (struct line_info_table*) bfd_alloc (abfd,
824 sizeof (struct line_info_table));
826 table->comp_dir = unit->comp_dir;
828 table->num_files = 0;
835 table->last_line = NULL;
837 line_ptr = stash->dwarf_line_buffer + unit->line_offset;
839 /* read in the prologue */
840 lh.total_length = read_4_bytes (abfd, line_ptr);
842 line_end = line_ptr + lh.total_length;
843 lh.version = read_2_bytes (abfd, line_ptr);
845 lh.prologue_length = read_4_bytes (abfd, line_ptr);
847 lh.minimum_instruction_length = read_1_byte (abfd, line_ptr);
849 lh.default_is_stmt = read_1_byte (abfd, line_ptr);
851 lh.line_base = read_1_signed_byte (abfd, line_ptr);
853 lh.line_range = read_1_byte (abfd, line_ptr);
855 lh.opcode_base = read_1_byte (abfd, line_ptr);
857 lh.standard_opcode_lengths = (unsigned char *)
858 bfd_alloc (abfd, lh.opcode_base * sizeof (unsigned char));
860 lh.standard_opcode_lengths[0] = 1;
861 for (i = 1; i < lh.opcode_base; ++i)
863 lh.standard_opcode_lengths[i] = read_1_byte (abfd, line_ptr);
867 /* Read directory table */
868 while ((cur_dir = read_string (abfd, line_ptr, &bytes_read)) != NULL)
870 line_ptr += bytes_read;
871 if ((table->num_dirs % DIR_ALLOC_CHUNK) == 0)
873 table->dirs = (char **)
874 bfd_realloc (table->dirs,
875 (table->num_dirs + DIR_ALLOC_CHUNK) * sizeof (char *));
879 table->dirs[table->num_dirs++] = cur_dir;
881 line_ptr += bytes_read;
883 /* Read file name table */
884 while ((cur_file = read_string (abfd, line_ptr, &bytes_read)) != NULL)
886 line_ptr += bytes_read;
887 if ((table->num_files % FILE_ALLOC_CHUNK) == 0)
889 table->files = (struct fileinfo *)
890 bfd_realloc (table->files,
891 (table->num_files + FILE_ALLOC_CHUNK)
892 * sizeof (struct fileinfo));
896 table->files[table->num_files].name = cur_file;
897 table->files[table->num_files].dir =
898 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
899 line_ptr += bytes_read;
900 table->files[table->num_files].time =
901 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
902 line_ptr += bytes_read;
903 table->files[table->num_files].size =
904 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
905 line_ptr += bytes_read;
908 line_ptr += bytes_read;
910 /* Read the statement sequences until there's nothing left. */
911 while (line_ptr < line_end)
913 /* state machine registers */
915 char* filename = concat_filename (table, 1);
916 unsigned int line = 1;
917 unsigned int column = 0;
918 int is_stmt = lh.default_is_stmt;
920 int end_sequence = 0, need_low_pc = 1;
923 /* Decode the table. */
924 while (! end_sequence)
926 op_code = read_1_byte (abfd, line_ptr);
930 case DW_LNS_extended_op:
931 line_ptr += 1; /* ignore length */
932 extended_op = read_1_byte (abfd, line_ptr);
936 case DW_LNE_end_sequence:
938 add_line_info (table, address, filename, line, column,
945 arange_add (unit, low_pc, address);
947 case DW_LNE_set_address:
948 address = read_address (unit, line_ptr);
949 line_ptr += unit->addr_size;
951 case DW_LNE_define_file:
952 cur_file = read_string (abfd, line_ptr, &bytes_read);
953 line_ptr += bytes_read;
954 if ((table->num_files % FILE_ALLOC_CHUNK) == 0)
956 table->files = (struct fileinfo *)
957 bfd_realloc (table->files,
958 (table->num_files + FILE_ALLOC_CHUNK)
959 * sizeof (struct fileinfo));
963 table->files[table->num_files].name = cur_file;
964 table->files[table->num_files].dir =
965 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
966 line_ptr += bytes_read;
967 table->files[table->num_files].time =
968 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
969 line_ptr += bytes_read;
970 table->files[table->num_files].size =
971 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
972 line_ptr += bytes_read;
976 (*_bfd_error_handler) (_("Dwarf Error: mangled line number section."));
977 bfd_set_error (bfd_error_bad_value);
982 add_line_info (table, address, filename, line, column, 0);
990 case DW_LNS_advance_pc:
991 address += lh.minimum_instruction_length
992 * read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
993 line_ptr += bytes_read;
995 case DW_LNS_advance_line:
996 line += read_signed_leb128 (abfd, line_ptr, &bytes_read);
997 line_ptr += bytes_read;
999 case DW_LNS_set_file:
1003 /* The file and directory tables are 0 based, the references
1005 file = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
1006 line_ptr += bytes_read;
1007 filename = concat_filename (table, file);
1010 case DW_LNS_set_column:
1011 column = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
1012 line_ptr += bytes_read;
1014 case DW_LNS_negate_stmt:
1015 is_stmt = (!is_stmt);
1017 case DW_LNS_set_basic_block:
1020 case DW_LNS_const_add_pc:
1021 address += lh.minimum_instruction_length
1022 * ((255 - lh.opcode_base) / lh.line_range);
1024 case DW_LNS_fixed_advance_pc:
1025 address += read_2_bytes (abfd, line_ptr);
1028 default: /* special operand */
1029 adj_opcode = op_code - lh.opcode_base;
1030 address += (adj_opcode / lh.line_range)
1031 * lh.minimum_instruction_length;
1032 line += lh.line_base + (adj_opcode % lh.line_range);
1033 /* append row to matrix using current values */
1034 add_line_info (table, address, filename, line, column, 0);
1049 /* If ADDR is within TABLE set the output parameters and return true,
1050 otherwise return false. The output parameters, FILENAME_PTR and
1051 LINENUMBER_PTR, are pointers to the objects to be filled in. */
1054 lookup_address_in_line_info_table (table,
1058 struct line_info_table* table;
1060 const char **filename_ptr;
1061 unsigned int *linenumber_ptr;
1063 struct line_info* next_line = table->last_line;
1064 struct line_info* each_line;
1069 each_line = next_line->prev_line;
1071 while (each_line && next_line)
1073 if (!each_line->end_sequence
1074 && addr >= each_line->address && addr < next_line->address)
1076 *filename_ptr = each_line->filename;
1077 *linenumber_ptr = each_line->line;
1080 next_line = each_line;
1081 each_line = each_line->prev_line;
1090 /* Function table functions. */
1093 struct funcinfo *prev_func;
1101 /* If ADDR is within TABLE, set FUNCTIONNAME_PTR, and return true. */
1104 lookup_address_in_function_table (table,
1107 struct funcinfo* table;
1109 const char **functionname_ptr;
1111 struct funcinfo* each_func;
1113 for (each_func = table;
1115 each_func = each_func->prev_func)
1117 if (addr >= each_func->low && addr < each_func->high)
1119 *functionname_ptr = each_func->name;
1130 /* DWARF2 Compilation unit functions. */
1133 /* Scan over each die in a comp. unit looking for functions to add
1134 to the function table. */
1137 scan_unit_for_functions (unit)
1138 struct comp_unit *unit;
1140 bfd *abfd = unit->abfd;
1141 char *info_ptr = unit->first_child_die_ptr;
1142 int nesting_level = 1;
1144 while (nesting_level)
1146 unsigned int abbrev_number, bytes_read, i;
1147 struct abbrev_info *abbrev;
1148 struct attribute attr;
1149 struct funcinfo *func;
1152 abbrev_number = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
1153 info_ptr += bytes_read;
1155 if (! abbrev_number)
1161 abbrev = lookup_abbrev (abbrev_number,unit->abbrevs);
1164 (*_bfd_error_handler) (_("Dwarf Error: Could not find abbrev number %d."),
1166 bfd_set_error (bfd_error_bad_value);
1170 if (abbrev->tag == DW_TAG_subprogram)
1172 func = (struct funcinfo*) bfd_zalloc (abfd, sizeof (struct funcinfo));
1173 func->prev_func = unit->function_table;
1174 unit->function_table = func;
1179 for (i = 0; i < abbrev->num_attrs; ++i)
1181 info_ptr = read_attribute (&attr, &abbrev->attrs[i], unit, info_ptr);
1189 name = DW_STRING (&attr);
1191 /* Prefer DW_AT_MIPS_linkage_name over DW_AT_name. */
1192 if (func->name == NULL)
1193 func->name = DW_STRING (&attr);
1196 case DW_AT_MIPS_linkage_name:
1197 func->name = DW_STRING (&attr);
1201 func->low = DW_ADDR (&attr);
1205 func->high = DW_ADDR (&attr);
1217 name = DW_STRING (&attr);
1226 if (abbrev->has_children)
1238 /* Parse a DWARF2 compilation unit starting at INFO_PTR. This
1239 includes the compilation unit header that proceeds the DIE's, but
1240 does not include the length field that preceeds each compilation
1241 unit header. END_PTR points one past the end of this comp unit.
1242 If ABBREV_LENGTH is 0, then the length of the abbreviation offset
1243 is assumed to be four bytes. Otherwise, it it is the size given.
1245 This routine does not read the whole compilation unit; only enough
1246 to get to the line number information for the compilation unit. */
1248 static struct comp_unit *
1249 parse_comp_unit (abfd, info_ptr, end_ptr, abbrev_length)
1253 unsigned int abbrev_length;
1255 struct comp_unit* unit;
1257 unsigned short version;
1258 unsigned int abbrev_offset = 0;
1259 unsigned char addr_size;
1260 struct abbrev_info** abbrevs;
1262 unsigned int abbrev_number, bytes_read, i;
1263 struct abbrev_info *abbrev;
1264 struct attribute attr;
1266 version = read_2_bytes (abfd, info_ptr);
1268 BFD_ASSERT (abbrev_length == 0
1269 || abbrev_length == 4
1270 || abbrev_length == 8);
1271 if (abbrev_length == 0 || abbrev_length == 4)
1272 abbrev_offset = read_4_bytes (abfd, info_ptr);
1273 else if (abbrev_length == 8)
1274 abbrev_offset = read_8_bytes (abfd, info_ptr);
1275 info_ptr += abbrev_length;
1276 addr_size = read_1_byte (abfd, info_ptr);
1281 (*_bfd_error_handler) (_("Dwarf Error: found dwarf version '%hu', this reader only handles version 2 information."), version );
1282 bfd_set_error (bfd_error_bad_value);
1286 if (addr_size > sizeof (bfd_vma))
1288 (*_bfd_error_handler) (_("Dwarf Error: found address size '%u', this reader can not handle sizes greater than '%u'."),
1291 bfd_set_error (bfd_error_bad_value);
1295 if (addr_size != 2 && addr_size != 4 && addr_size != 8)
1297 (*_bfd_error_handler) ("Dwarf Error: found address size '%u', this reader can only handle address sizes '2', '4' and '8'.", addr_size );
1298 bfd_set_error (bfd_error_bad_value);
1302 /* Read the abbrevs for this compilation unit into a table */
1303 abbrevs = read_abbrevs (abfd, abbrev_offset);
1307 abbrev_number = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
1308 info_ptr += bytes_read;
1309 if (! abbrev_number)
1311 (*_bfd_error_handler) (_("Dwarf Error: Bad abbrev number: %d."),
1313 bfd_set_error (bfd_error_bad_value);
1317 abbrev = lookup_abbrev (abbrev_number, abbrevs);
1320 (*_bfd_error_handler) (_("Dwarf Error: Could not find abbrev number %d."),
1322 bfd_set_error (bfd_error_bad_value);
1326 unit = (struct comp_unit*) bfd_zalloc (abfd, sizeof (struct comp_unit));
1328 unit->addr_size = addr_size;
1329 unit->abbrevs = abbrevs;
1330 unit->end_ptr = end_ptr;
1332 for (i = 0; i < abbrev->num_attrs; ++i)
1334 info_ptr = read_attribute (&attr, &abbrev->attrs[i], unit, info_ptr);
1336 /* Store the data if it is of an attribute we want to keep in a
1337 partial symbol table. */
1340 case DW_AT_stmt_list:
1342 unit->line_offset = DW_UNSND (&attr);
1346 unit->name = DW_STRING (&attr);
1350 unit->arange.low = DW_ADDR (&attr);
1354 unit->arange.high = DW_ADDR (&attr);
1357 case DW_AT_comp_dir:
1359 char* comp_dir = DW_STRING (&attr);
1362 /* Irix 6.2 native cc prepends <machine>.: to the compilation
1363 directory, get rid of it. */
1364 char *cp = (char*) strchr (comp_dir, ':');
1366 if (cp && cp != comp_dir && cp[-1] == '.' && cp[1] == '/')
1369 unit->comp_dir = comp_dir;
1378 unit->first_child_die_ptr = info_ptr;
1386 /* Return true if UNIT contains the address given by ADDR. */
1389 comp_unit_contains_address (unit, addr)
1390 struct comp_unit* unit;
1393 struct arange *arange;
1398 arange = &unit->arange;
1401 if (addr >= arange->low && addr < arange->high)
1403 arange = arange->next;
1410 /* If UNIT contains ADDR, set the output parameters to the values for
1411 the line containing ADDR. The output parameters, FILENAME_PTR,
1412 FUNCTIONNAME_PTR, and LINENUMBER_PTR, are pointers to the objects
1415 Return true of UNIT contains ADDR, and no errors were encountered;
1419 comp_unit_find_nearest_line (unit, addr,
1420 filename_ptr, functionname_ptr, linenumber_ptr)
1421 struct comp_unit* unit;
1423 const char **filename_ptr;
1424 const char **functionname_ptr;
1425 unsigned int *linenumber_ptr;
1433 if (! unit->line_table)
1435 if (! unit->stmtlist)
1441 unit->line_table = decode_line_info (unit);
1443 if (! unit->line_table)
1449 if (! scan_unit_for_functions (unit))
1456 line_p = lookup_address_in_line_info_table (unit->line_table,
1460 func_p = lookup_address_in_function_table (unit->function_table,
1463 return line_p || func_p;
1466 /* The DWARF2 version of find_nearest line. Return true if the line
1467 is found without error. ADDR_SIZE is the number of bytes in the
1468 initial .debug_info length field and in the abbreviation offset.
1469 You may use zero to indicate that the default value should be
1473 _bfd_dwarf2_find_nearest_line (abfd, section, symbols, offset,
1474 filename_ptr, functionname_ptr,
1479 asymbol **symbols ATTRIBUTE_UNUSED;
1481 const char **filename_ptr;
1482 const char **functionname_ptr;
1483 unsigned int *linenumber_ptr;
1484 unsigned int addr_size;
1486 /* Read each compilation unit from the section .debug_info, and check
1487 to see if it contains the address we are searching for. If yes,
1488 lookup the address, and return the line number info. If no, go
1489 on to the next compilation unit.
1491 We keep a list of all the previously read compilation units, and
1492 a pointer to the next un-read compilation unit. Check the
1493 previously read units before reading more.
1496 struct dwarf2_debug *stash = elf_tdata (abfd)->dwarf2_find_line_info;
1498 /* What address are we looking for? */
1499 bfd_vma addr = offset + section->vma;
1501 struct comp_unit* each;
1503 *filename_ptr = NULL;
1504 *functionname_ptr = NULL;
1505 *linenumber_ptr = 0;
1507 /* The DWARF2 spec says that the initial length field, and the
1508 offset of the abbreviation table, should both be 4-byte values.
1509 However, some compilers do things differently. */
1512 BFD_ASSERT (addr_size == 4 || addr_size == 8);
1519 stash = elf_tdata (abfd)->dwarf2_find_line_info =
1520 (struct dwarf2_debug*) bfd_zalloc (abfd, sizeof (struct dwarf2_debug));
1525 msec = bfd_get_section_by_name (abfd, ".debug_info");
1528 /* No dwarf2 info. Note that at this point the stash
1529 has been allocated, but contains zeros, this lets
1530 future calls to this function fail quicker. */
1534 size = msec->_raw_size;
1538 stash->info_ptr = (char *) bfd_alloc (abfd, size);
1540 if (! stash->info_ptr)
1543 if (! bfd_get_section_contents (abfd, msec, stash->info_ptr, 0, size))
1545 stash->info_ptr = 0;
1549 stash->info_ptr_end = stash->info_ptr + size;
1551 /* FIXME: There is a problem with the contents of the
1552 .debug_info section. The 'low' and 'high' addresses of the
1553 comp_units are computed by relocs against symbols in the
1554 .text segment. We need these addresses in order to determine
1555 the nearest line number, and so we have to resolve the
1556 relocs. There is a similar problem when the .debug_line
1557 section is processed as well (e.g., there may be relocs
1558 against the operand of the DW_LNE_set_address operator).
1560 Unfortunately getting hold of the reloc information is hard...
1562 For now, this means that disassembling object files (as
1563 opposed to fully executables) does not always work as well as
1567 /* A null info_ptr indicates that there is no dwarf2 info
1568 (or that an error occured while setting up the stash). */
1570 if (! stash->info_ptr)
1573 /* Check the previously read comp. units first. */
1575 for (each = stash->all_comp_units; each; each = each->next_unit)
1576 if (comp_unit_contains_address (each, addr))
1577 return comp_unit_find_nearest_line (each, addr, filename_ptr,
1578 functionname_ptr, linenumber_ptr);
1580 /* Read each remaining comp. units checking each as they are read. */
1581 while (stash->info_ptr < stash->info_ptr_end)
1583 struct comp_unit* each;
1588 length = read_4_bytes (abfd, stash->info_ptr);
1590 length = read_8_bytes (abfd, stash->info_ptr);
1591 stash->info_ptr += addr_size;
1595 each = parse_comp_unit (abfd, stash->info_ptr,
1596 stash->info_ptr + length,
1598 stash->info_ptr += length;
1602 each->next_unit = stash->all_comp_units;
1603 stash->all_comp_units = each;
1605 /* DW_AT_low_pc and DW_AT_high_pc are optional for
1606 compilation units. If we don't have them (i.e.,
1607 unit->high == 0), we need to consult the line info
1608 table to see if a compilation unit contains the given
1610 if (each->arange.high > 0)
1612 if (comp_unit_contains_address (each, addr))
1613 return comp_unit_find_nearest_line (each, addr,
1620 found = comp_unit_find_nearest_line (each, addr,